lib/LaTeXML/Common/Font/Metric.pm

filter: Statementstmt 19.4% 22 / 113 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 70.0% 7 / 10 Podpod 0.0% 0 / 3 totaltotal 16.1% 29 / 180 CC 23 SCAR 48.4
CC
23
Cov
14%
read_tfm
210.0
process_lig_kern
110.0
new
2.0
CRAP 126.5
« lib/LaTeXML/Common/Object.pm ↑ Summary lib/LaTeXML/Common/Model/RelaxNG.pm »
1 # /=====================================================================\ #
2 # | LaTeXML::Common::Font::Metric | #
3 # | Representaion of Font Metrics | #
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::Font::Metric;
13 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use LaTeXML::Global;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use LaTeXML::Common::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use LaTeXML::Common::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use LaTeXML::Common::Dimension;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use base qw(LaTeXML::Common::Object);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # Likely options will include family, face, etc...
22 # encoding,
23 # designsize, exheight, quad (emwidth),
24 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
25 0 my ($class, $encoding, $file, %options) = @_;
26 0 my $self = bless { encoding => $encoding, file => $file,
27 sizes => {}, kerns => {}, ligatures => {}, %options }, $class;
28 0 $self->read_tfm();
29 0 return $self; }
30  
31 # TeX's rendering of characters uses little drawings stored by position within a font;
32 # A font encoding implies which (somewhat abstract) chracter is at which position.
33 # The metrics (size, kerning, etc) are stored within a TeX Font Metric (tfm) file,
34 # which is also indexed by the glyph's position within the font.
35  
36 # LaTeXML uses "font maps" to associate each position within a font (encoding)
37 # with a Unicode codepoint. Here, we use a representaive tfm to determine what
38 # TeX might think an appropriate size for each such codepoint. Of course, it will
39 # certainly be different on whatever eventual rendering device, according to browser fonts, etc.
40  
41 # The tfm reading code is freely adapted from Font::TFM.
42 # See TeX the Program, Section 539
43 0 ▶sub read_tfm {
Subroutine read_tfm: not called CC=14 SCAR=53.5
Pod undocumented
44 0 my ($self) = @_;
45 0 my $fontmap = $STATE->lookupValue($$self{encoding} . '_fontmap');
46 # my $pathname = FindFile($$self{file}, 'tfm');
47 0 my $pathname = $$self{file};
48 # Read the TFM raw data.
49 0 my $TFM;
50 0 ▶ if (!open($TFM, '<', $pathname)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $TFM, "<", $pathname) 0 0 0
51 0 Error('expected', $pathname, undef,
52 "Couldn't open TFM files '$pathname': $!");
53 0 return; }
54 # ----------------------------------------
55 # read & extract heading info
56 0 my $buffer = '';
57 0 ▶ if (read($TFM, $buffer, 24) != 24) {
Branch: 1 branch
BranchTrueFalseTotal
if (read($TFM, $buffer, 24) != 24) 0 0 0
58 0 Error("Error reading TFM header: $!"); return; }
59  
60 # Extract 12 unsigned short (16bit, network order) fields from the header
61 # length of file & header; begin & end char,
62 # number of widths,heights,depts,italic corrections, ligkern, extensions, parameters
63 0 my ($lf, $lh, $bc, $ec, $nw, $nh, $nd, $ni, $nl, $nk, $ne, $np) = unpack("n12", $buffer);
64 0 my $nc = $ec - $bc + 1;
65  
66 # ----------------------------------------
67 # Read & extract data tables
68 0 my $lt = $lf * 4 - 24; # length of tables.
69 0 ▶ if (read($TFM, $buffer, $lt) != $lt) {
Branch: 1 branch
BranchTrueFalseTotal
if (read($TFM, $buffer, $lt) != $lt) 0 0 0
70 0 Error("Error reading body: $!"); return; }
71 0 close $TFM;
72  
73 # Disassemble the relevant data tables
74 0 my (@header, @char_info, @width, @height, @depth, @italic, @lig_kern, @kern, @exten, @param);
75 0 (@header[0 .. $lh - 1],
76 @char_info[0 .. $nc - 1], # a4 x $ncodes
77 @width[0 .. $nw - 1],
78 @height[0 .. $nh - 1],
79 @depth[0 .. $nd - 1],
80 @italic[0 .. $ni - 1],
81 @lig_kern[0 .. $nl - 1], # a4 x $nl
82 @kern[0 .. $nk - 1],
83 @exten[0 .. $ne - 1], # a4 x $ne
84 @param[0 .. $np - 1],
85 ) = unpack "N$lh" . "a4" x $nc . "l>$nw" . "l>$nh" . "l>$nd" . "l>$ni" .
86 "a4" x $nl . "l>$nk" . "a4" x $ne . "l>$np", $buffer;
87 # Mostly can ignore the header; but $header[1] is the design size; do we need that?
88 # Note: fixword are fixed point being 16 scaled points !
89 # But: MetaFont book says "sharp units (printers points)" TTP says "design size units" ???
90 # Note that these sizes DO still get multiplied by the font size (design size?) in Font.pm
91 # Question: should we just store them that way (more compact?) and deal w/units in Font.pm?
92 0 @width = map { $_ / 16 } @width;
93 0 @height = map { $_ / 16 } @height;
94 0 @depth = map { $_ / 16 } @depth;
95 0 @italic = map { $_ / 16 } @italic;
96 0 @kern = map { $_ / 16 } @kern;
97 0 @param = map { $_ / 16 } @param;
98  
99 # First & last lig_kern entries relate to "boundary" char (what IS that???)
100 0 ▶ if (@lig_kern) { # check for boundary char
Branch: 1 branch
BranchTrueFalseTotal
if (@lig_kern) 0 0 0
101 0 my ($skip, $next, $op, $remainder);
102 0 ($skip, $next) = unpack "CC", $lig_kern[0];
103 0 ▶ if (($skip == 255) && (defined $$fontmap[$next])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip == 255 and defined $fontmap->[$next]) 0 0 0
Condition0%0/3
$skip == 255 and defined $fontmap->[$next]
!ll&&!rl&&r
000
MC/DC0%0/2
$skip == 255 and defined $fontmap->[$next]
$skip == 255 defined $fontmap->[$next]
Truth table0%0/3
A and B
A= $skip == 255B= defined $fontmap->[$next]
ABresult
0X0
100
111
104 0 $$self{boundary} = $$fontmap[$next]; }
105 0 ($skip, $next, $op, $remainder) = unpack "CCCC", $lig_kern[-1];
106 0 ▶ if ($skip == 255) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip == 255) 0 0 0
107 0 $self->process_lig_kern("boundary", \@lig_kern, 256 * $op + $remainder, $fontmap, \@kern); }
108 }
109 # Interpret char_info (for w,h,d,ital) and lig_kern program
110 0 my $sizes = $$self{sizes};
111 0 ▶ for (my $code = $bc ; $code <= $ec ; $code++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code <= $ec) 0 0 0
112 0 my $char = $$fontmap[$code];
113 0 ▶ if (defined $char) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $char) 0 0 0
114 0 my ($wloc, $hdloc, $ixloc, $remainder) = unpack "C4", $char_info[$code];
115 0 my ($hloc, $dloc, $iloc, $tag) = ($hdloc >> 4, $hdloc & 0x0F, $ixloc >> 2, $ixloc & 0x03);
116 0 ▶ if (!$$sizes{$char}) { # Don't replace?
Branch: 1 branch
BranchTrueFalseTotal
unless ($sizes->{$char}) 0 0 0
117 0 $$sizes{$char} = [$width[$wloc], $height[$hloc], $depth[$dloc], $italic[$iloc]]; }
118 0 ▶ if ($tag == 1) { # lig/kern program; Ignore tag=2 (larger); tag=3 (extensible)
Branch: 1 branch
BranchTrueFalseTotal
if ($tag == 1) 0 0 0
119 0 $self->process_lig_kern($char, \@lig_kern, $remainder, $fontmap, \@kern); }
120 } }
121 # And install interesting parameters (offset by 1)
122 0 $$self{slant} = $param[0];
123 0 $$self{space} = $param[1];
124 0 $$self{spacestretch} = $param[2];
125 0 $$self{spaceshrink} = $param[3];
126 0 $$self{exheight} = $param[4];
127 0 $$self{emwidth} = $param[5];
128 0 $$self{quad} = $param[5];
129 0 $$self{extraspace} = $param[6];
130 # Add metrics for fake chars (from TeX perspective; Basically various space-like things)
131 # What about \x{2002}, \x{2009} ?
132 0 my $em = $param[5];
133 0 my $mu = $em / 18;
134 0 my %para = (" " => $param[1], pack('U', 0xA0) => $param[1], "\n" => $param[1],
135 "\x{2002}" => $em / 2, "\x{2003}" => $em,
136 "\x{2006}" => $em / 6, "\x{2009}" => $em / 6,
137 "\x{200b}" => 3 * $mu, "\x{2005}" => 4 * $mu, "\x{2004}" => 5 * $mu);
138 0 foreach my $char (sort keys %para) {
139 0 ▶ if (!$$sizes{$char}) { # Don't replace?
Branch: 1 branch
BranchTrueFalseTotal
unless ($sizes->{$char}) 0 0 0
140 0 $$sizes{$char} = [$para{$char}, 0, 0]; } }
141 0 return $self; }
142  
143 # Process the ligature/kerning program for a character
144 0 ▶sub process_lig_kern {
Subroutine process_lig_kern: not called CC=10 SCAR=47.0
Pod undocumented
145 0 my ($self, $char, $lig_kernref, $prognum, $fontmap, $kernref) = @_;
146 0 my $firstinstr = 1;
147 0 my $kerns = $$self{kerns};
148 0 my $ligs = $$self{ligatures};
149 0 while (1) {
150 0 my ($skip, $next, $op, $remainder) = unpack "CCCC", $$lig_kernref[$prognum];
151 0 ▶ if ($firstinstr && ($skip > 128)) { # Effectively a goto
Branch: 1 branch
BranchTrueFalseTotal
if ($firstinstr and $skip > 128) 0 0 0
Condition0%0/3
$firstinstr and $skip > 128
!ll&&!rl&&r
000
MC/DC0%0/2
$firstinstr and $skip > 128
$firstinstr $skip > 128
Truth table0%0/3
A and B
A= $firstinstrB= $skip > 128
ABresult
0X0
100
111
152 0 $prognum = 256 * $op + $remainder; $firstinstr = 0;
153 0 next; }
154 0 $next = $$fontmap[$next];
155 0 my $pair = $char . $next;
156 0 ▶ if (($op >= 128) && !exists $$kerns{$pair}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op >= 128 and not exists $kerns->{$pair}) 0 0 0
Condition0%0/3
$op >= 128 and not exists $kerns->{$pair}
!ll&&!rl&&r
000
MC/DC0%0/2
$op >= 128 and not exists $kerns->{$pair}
$op >= 128 not exists $kerns->{$pair}
Truth table0%0/3
A and not(B)
A= $op >= 128B= not exists $kerns->{$pair}
ABresult
0X0
100
111
157 0 $$kerns{$pair} = $$kernref[256 * ($op - 128) + $remainder]; }
158 0 ▶ if (($op < 128) && !exists $$ligs{$pair}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op < 128 and not exists $ligs->{$pair}) 0 0 0
Condition0%0/3
$op < 128 and not exists $ligs->{$pair}
!ll&&!rl&&r
000
MC/DC0%0/2
$op < 128 and not exists $ligs->{$pair}
$op < 128 not exists $ligs->{$pair}
Truth table0%0/3
A and not(B)
A= $op < 128B= not exists $ligs->{$pair}
ABresult
0X0
100
111
159 0 my ($pass, $keepc, $keepn) = ($op >> 2, ($op >> 1) & 0x01, $op & 0x01);
160 0 ▶ my $lig = ($keepc ? $char : "") . $$fontmap[$remainder] . ($keepn ? $next : '');
Branch: 2 branches
BranchTrueFalseTotal
$keepc ? : 0 0 0
$keepn ? : 0 0 0
161 0 $$ligs{$pair} = [$lig, $pass]; } # ligature & number of chars to "passover" (always 0???)
162 0 ▶ last if ($skip >= 128);
Branch: 1 branch
BranchTrueFalseTotal
if $skip >= 128 0 0 0
163 0 $prognum += $skip + 1;
164 0 $firstinstr = 0; }
165 0 return; }
166  
167 5 1;
« lib/LaTeXML/Common/Object.pm ↑ Summary lib/LaTeXML/Common/Model/RelaxNG.pm »