| 1 | # /=====================================================================\ # | ||||||||||||
| 2 | # | LaTeXML::Common::Dimension | # | ||||||||||||
| 3 | # | Representation of Dimensions | # | ||||||||||||
| 4 | # |=====================================================================| # | ||||||||||||
| 5 | # | Part of LaTeXML: | # | ||||||||||||
| 6 | # | Public domain software, produced as part of work done by the | # | ||||||||||||
| 7 | # | United States Government & not subject to copyright in the US. | # | ||||||||||||
| 8 | # |---------------------------------------------------------------------| # | ||||||||||||
| 9 | # | Bruce Miller <bruce.miller@nist.gov> #_# | # | ||||||||||||
| 10 | # | http://dlmf.nist.gov/LaTeXML/ (o o) | # | ||||||||||||
| 11 | # \=========================================================ooo==U==ooo=/ # | ||||||||||||
| 12 | package LaTeXML::Common::Dimension; | ||||||||||||
| 13 | 6 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 6 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 6 | ▶ | use LaTeXML::Global; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 6 | ▶ | use LaTeXML::Common::Object; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 6 | ▶ | use LaTeXML::Common::Number; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 6 | ▶ | use base qw(LaTeXML::Common::Number); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 6 | ▶ | use base qw(Exporter); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 6 | our @EXPORT = (qw(&Dimension &fixedformat &fixpoint $UNITY)); | |||||||||||
| 21 | |||||||||||||
| 22 | #====================================================================== | ||||||||||||
| 23 | # Exported constructor. | ||||||||||||
| 24 | |||||||||||||
| 25 | # Create a Dimension given either a float with unit OR a number in scaled points | ||||||||||||
| 26 | 0 | ▶ | sub Dimension { | ||||||||||
|
Subroutine
Dimension: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 27 | 0 | my ($spec) = @_; | |||||||||||
| 28 | 0 | return LaTeXML::Common::Dimension->new($spec); } | |||||||||||
| 29 | |||||||||||||
| 30 | #====================================================================== | ||||||||||||
| 31 | 0 | ▶ | sub _unit { return 'pt'; } | ||||||||||
|
Subroutine
_unit: not called CC=1 SCAR=6.9
| |||||||||||||
| 32 | |||||||||||||
| 33 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||
| 34 | 0 | my ($class, $spec) = @_; | |||||||||||
| 35 | 0 | ▶ | $spec = "0" unless $spec; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 36 | 0 | ▶ | $spec = ToString($spec) if ref $spec; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 37 | 0 | ▶ | if ($spec =~ /^(-?\d*\.?\d*)([a-zA-Z][a-zA-Z])$/) { # Dimensions given. | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | 0 | return bless [fixpoint($1, $STATE->convertUnit($2))], $class; } | |||||||||||
| 39 | else { | ||||||||||||
| 40 | # When scaled points passed in (typically the result of Perl calculations on other Dimensions), | ||||||||||||
| 41 | # you might think truncation (int) is more TeX-like. | ||||||||||||
| 42 | # Recall that TeX arithmatic truncates, whereas reading by Gullet tends to round. | ||||||||||||
| 43 | # The Perl arithmetic is replacing an unknown combination of those truncates & rounds. | ||||||||||||
| 44 | # As it turns out, using int() here results in non-terminating loops in pgf/tikz. | ||||||||||||
| 45 | # So, we use round (Knuth style) | ||||||||||||
| 46 | # Note that divide and such explicitly use int(), however! | ||||||||||||
| 47 | 0 | ▶ | return bless [kround($spec || 0)], $class; } } | ||||||||||
|
Condition0%0/2
$spec || 0
MC/DC0%0/1
$spec || 0
$spec
Truth table0%0/2
A
A= $spec
| |||||||||||||
| 48 | |||||||||||||
| 49 | 0 | ▶ | sub toString { | ||||||||||
|
Subroutine
toString: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 50 | 0 | my ($self) = @_; | |||||||||||
| 51 | 0 | return fixedformat($$self[0], $self->_unit); } | |||||||||||
| 52 | |||||||||||||
| 53 | 0 | ▶ | sub toAttribute { | ||||||||||
|
Subroutine
toAttribute: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 54 | 0 | my ($self) = @_; | |||||||||||
| 55 | 0 | return attributeformat($$self[0], $self->_unit); } | |||||||||||
| 56 | |||||||||||||
| 57 | 0 | ▶ | sub stringify { | ||||||||||
|
Subroutine
stringify: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 58 | 0 | my ($self) = @_; | |||||||||||
| 59 | 0 | return "Dimension[" . $$self[0] . "]"; } | |||||||||||
| 60 | |||||||||||||
| 61 | # One in fixpoint. | ||||||||||||
| 62 | 6 | our $UNITY = 65536; | |||||||||||
| 63 | # Convert $float to a fixed-point number | ||||||||||||
| 64 | # If $unit is given, it is number of units PER SCALED-POINT! (hence, extra division) | ||||||||||||
| 65 | # AND, note that the float is rounded and THEN truncated after multiplying by units! | ||||||||||||
| 66 | # to mimic TeX's behavior. | ||||||||||||
| 67 | 0 | ▶ | sub fixpoint { | ||||||||||
|
Subroutine
fixpoint: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 68 | 0 | my ($float, $unit) = @_; | |||||||||||
| 69 | 0 | my $fix = kround($float * $UNITY); | |||||||||||
| 70 | 0 | ▶ | return (defined $unit ? int($fix * $unit / $UNITY) : $fix); } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | |||||||||||||
| 72 | # This is Knuth's print_scaled (See TeX the Program, \S 103) | ||||||||||||
| 73 | # It (should) round-trip with kround. | ||||||||||||
| 74 | 0 | ▶ | sub fixedformat { | ||||||||||
|
Subroutine
fixedformat: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||
| 75 | 0 | my ($s, $unit) = @_; | |||||||||||
| 76 | 0 | $s = int($s); | |||||||||||
| 77 | 0 | my $string = ''; | |||||||||||
| 78 | 0 | ▶ | if ($s < 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | 0 | $string .= '-'; $s = -$s; } | |||||||||||
| 80 | 0 | $string .= int($s / $UNITY); | |||||||||||
| 81 | 0 | $string .= '.'; | |||||||||||
| 82 | 0 | $s = 10 * ($s % $UNITY) + 5; | |||||||||||
| 83 | 0 | my $delta = 10; | |||||||||||
| 84 | 0 | ▶ | do { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 85 | 0 | ▶ | $s += 0x8000 - 50000 if $delta > $UNITY; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | 0 | $string .= "0" + int($s / $UNITY); | |||||||||||
| 87 | 0 | $s = 10 * ($s % $UNITY); | |||||||||||
| 88 | 0 | $delta *= 10; | |||||||||||
| 89 | } until $s <= $delta; | ||||||||||||
| 90 | 0 | ▶ | return $string . ($unit || ''); } | ||||||||||
|
Condition0%0/2
$unit || ''
MC/DC0%0/1
$unit || ''
$unit
Truth table0%0/2
A
A= $unit
| |||||||||||||
| 91 | |||||||||||||
| 92 | 0 | ▶ | sub attributeformat { | ||||||||||
|
Subroutine
attributeformat: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 93 | 0 | my ($sp, $unit) = @_; | |||||||||||
| 94 | 0 | ▶ | return sprintf('%.1f', LaTeXML::Common::Number::roundto($sp / $UNITY, 1)) . ($unit || 'pt'); } | ||||||||||
|
Condition0%0/2
$unit || 'pt'
MC/DC0%0/1
$unit || 'pt'
$unit
Truth table0%0/2
A
A= $unit
| |||||||||||||
| 95 | |||||||||||||
| 96 | 0 | ▶ | sub ptValue { | ||||||||||
|
Subroutine
ptValue: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 97 | 0 | my ($self, $prec) = @_; | |||||||||||
| 98 | 0 | return roundto($$self[0] / $UNITY, $prec); } | |||||||||||
| 99 | |||||||||||||
| 100 | 0 | ▶ | sub pxValue { | ||||||||||
|
Subroutine
pxValue: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 101 | 0 | my ($self, $prec) = @_; | |||||||||||
| 102 | 0 | ▶ | return roundto($$self[0] / $UNITY * ($STATE->lookupValue('DPI') || 100) / 72.27, $prec); } | ||||||||||
|
Condition0%0/2
$LaTeXML::Common::Dimension::STATE->lookupValue('DPI') || 100
MC/DC0%0/1
$LaTeXML::Common::Dimension::STATE->lookupValue('DPI') || 100
$LaTeXML::Common::Dimension::STATE->lookupValue('DPI')
Truth table0%0/2
A
A= $LaTeXML::Common::Dimension::STATE->lookupValue('DPI')
| |||||||||||||
| 103 | |||||||||||||
| 104 | 0 | ▶ | sub spValue { | ||||||||||
|
Subroutine
spValue: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 105 | 0 | my ($self, $prec) = @_; | |||||||||||
| 106 | 0 | return kround($$self[0]); } | |||||||||||
| 107 | |||||||||||||
| 108 | #====================================================================== | ||||||||||||
| 109 | 6 | 1; | |||||||||||
| 110 | |||||||||||||
| 111 | __END__ | ||||||||||||