ONO::Lib::PDF::Elements::Ruler

package ONO::Lib::PDF::Elements::Ruler;
################################################################################
# COPYRIGHT / LICENSE #
################################################################################
#
# This file is part of the ONO Software Project.
#
# Copyright (C) 2000-2025 Jos KIRPS [ www.kirps.com | jos_AT_kirps_DOT_com ]
# and The Joopita Project [ www.joopita.org | contact_AT_joopita_DOT_com ]
#
# This file, as well as other parts of the ONO Software Project or related
# elements, are FREE SOFTWARE available under the ARTISTIC LICENSE 2.0.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# For the full license, see /ono/osr/license/LICENSE.txt, or write to
# jos_AT_kirps_DOT_com or contact_AT_joopita_DOT_com.
#
################################################################################
# END OF COPYRIGHT / LICENSE, HERE COMES THE CODE ... #
################################################################################

use strict;

use ONO::Lib::PDF::Draw;
use ONO::Lib::Image::Canvas::Draw;

#: This module generates specific PDF content.

sub ruler {

my (
$self,
$canvas,
$pdf_x,
$pdf_y,
$width,
$height,
$start,
$step,
$color,
$switches,
$setup_ref,
) = @_;

#: This generates a ruler image.
#:
#: Switches:
#:
#: -B b/w (overrules both colors and -C switch)
#: -c comma as label delimited (non-US)
#: -C color: random
#: -E EU comma delimiter
#: -F only first label is displayed
#: -H HTML mode (no PDF)
#: -L no labels at all
#: -m millimeters
#: -p pre-roll (lines before zero)
#: -z zero, add zero to all displayed labels (can be combined with -Z to have 3 zeroes)
#: -Z double zero, adds 2 zeroes to all displayed labels (can be combined with -Z to have 3 zeroes)

my (%setup,$HTML);

if ($setup_ref) {
%setup = %$setup_ref;
}

my $MODE = "pdf";
if ($switches =~ /H/) {
$MODE = "html";
}

my $canvas2 = "0:0";
my $html_scale = 8;

if ($height < 7) {
$height = 7;
}
if ($color eq "") {
$color = "0:233:166:33";
}

my $color2 = "0:255:244:222";

if ($switches =~ /C/) {
$color = &ruler_random_color();
}
if ($switches =~ /B/) {
$color = "0:222:222:222";
$color2 = "0:250:250:250";
}

my $font = 9;

my $ZEROES;
if ($switches =~ /z/) {
$ZEROES .= "0";
$font = 8;
}
if ($switches =~ /Z/) {
$ZEROES .= "00";
$font = 7;
}
if (length ($start+$width) > 2) {
$font = 8;
}
if (length ($start+$width) > 3) {
$font = 7;
}

if ($MODE eq "html") {

$HTML .= ONO::Lib::Image::Canvas::Draw->rect($canvas2,$pdf_x,$pdf_y,$pdf_x+$width,$pdf_y+$height,1,"0:25:25:25",$color2,2);
$HTML .= ONO::Lib::Image::Canvas::Draw->rect($canvas2,$pdf_x,$pdf_y+8*$html_scale,$pdf_x+$width,$pdf_y+$height,1,"0:25:25:25",$color,2);

} else {

ONO::Lib::PDF::Draw->rect($canvas,$pdf_x,$pdf_y,$pdf_x+$width,$pdf_y+$height,1,"0:25:25:25",$color2,2);
if ($height > 11) {
ONO::Lib::PDF::Draw->rect($canvas,$pdf_x,$pdf_y+8,$pdf_x+$width,$pdf_y+$height,1,"0:25:25:25",$color,2);
ONO::Lib::PDF::Draw->rect($canvas,$pdf_x,$pdf_y+7,$pdf_x+$width,$pdf_y+10,1,$color,$color);
ONO::Lib::PDF::Draw->rect($canvas,$pdf_x,$pdf_y,$pdf_x+$width,$pdf_y+$height,1,"0:25:25:25","",2);
}

if ($height > 13 && !$start) {
ONO::Lib::PDF::Draw->circle($canvas,$pdf_x+4,$pdf_y+10.5,1.5,1,"0:255:255:255");
}

ONO::Lib::PDF::Draw->font("H",$font);

}

my $pre = 0;
if ($switches =~ /p/) {
$pre = -2;
}

my $label = 0;

my $iwidth = $width-3;

if ($MODE eq "html") {
$iwidth = $width/$html_scale;
}

for (my $i = $pre; $i < $iwidth; $i++) {
my ($mm,$cm,$top);
if ($i%5 == 0) {
$cm = 0.75;
}
if ($i%10 == 0) {
$cm = 1.5;
$mm = 1;
my $LABEL = $i/10+$start;

my ($exclude,$include);
if ($switches =~ /L/) {
$exclude++;
}
if ($switches =~ /F/ && $i != 0) {
$exclude++;
}
if ($setup{"force_display_label_pos_$label"}) {
$include++;
}

if (!$exclude || $include) {
$LABEL = "$LABEL$ZEROES";
if ($setup{'label_divider'} > 0) {
$LABEL = $LABEL/$setup{'label_divider'};
if ($LABEL !~ /\./) {
if ($setup{'label_divider'} == 10) {
$LABEL = "$LABEL.0";
}
if ($setup{'label_divider'} == 100) {
$LABEL = "$LABEL.00";
}
}
}
if ($setup{"label_calc_sub"}) {
$LABEL = $LABEL - $setup{"label_calc_sub"};
}
if ($switches =~ /c/) {
$LABEL =~ s~\.~\,~;
}
if ($MODE eq "html") {
$HTML .= ONO::Lib::Image::Canvas::Draw->text($canvas2,$pdf_x+(3+$i)*$html_scale,$pdf_y+6*$html_scale,$LABEL,"c","",36);
} else {
ONO::Lib::PDF::Draw->text($canvas,$pdf_x+3+$i,$pdf_y+6,$LABEL,"c");
}
}
$label++;
}
if ($mm || $switches =~ /m/) {
if ($MODE eq "html") {
$HTML .= ONO::Lib::Image::Canvas::Draw->line($canvas2,$pdf_x+(3+$i)*$html_scale,$pdf_y,$pdf_x+(3+$i)*$html_scale,$pdf_y+(1.5+$cm)*$html_scale,0.5);
} else {
ONO::Lib::PDF::Draw->line($canvas,$pdf_x+3+$i,$pdf_y,$pdf_x+3+$i,$pdf_y+1.5+$cm,0.5);
}
}
}

if ($MODE eq "html") {
$HTML = ONO::Lib::Image::Canvas::Draw->canvas_render($HTML,$width,$height);
} else {
ONO::Lib::PDF::Draw->font("H",12);
}

return $HTML;

}

sub ruler_random_color {

my $c1 = 105+int(rand(135));
my $c2 = 55+int(rand(120));
my $c3 = 5+int(rand(100));

return "0:$c1:$c2:$c3";

}

###############################################################################
# end of script
###############################################################################

1;

__END__