<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Font::TTF::Hmtx;

=head1 NAME

Font::TTF::Hmtx - Horizontal Metrics

=head1 DESCRIPTION

Contains the advance width and left side bearing for each glyph. Given the
compressability of the data onto disk, this table uses information from
other tables, and thus must do part of its output during the output of
other tables

=head1 INSTANCE VARIABLES

The horizontal metrics are kept in two arrays by glyph id. The variable names
do not start with a space

=over 4

=item advance

An array containing the advance width for each glyph

=item lsb

An array containing the left side bearing for each glyph

=back

=head1 METHODS

=cut

use strict;
use vars qw(@ISA);
require Font::TTF::Table;

@ISA = qw(Font::TTF::Table);


=head2 $t-&gt;read

Reads the horizontal metrics from the TTF file into memory

=cut

sub read
{
    my ($self) = @_;
    my ($numh, $numg);

    $numh = $self-&gt;{' PARENT'}{'hhea'}-&gt;read-&gt;{'numberOfHMetrics'};
    $numg = $self-&gt;{' PARENT'}{'maxp'}{'numGlyphs'};
    $self-&gt;_read($numg, $numh, "advance", "lsb");
}

sub _read
{
    my ($self, $numg, $numh, $tAdv, $tLsb) = @_;
    my ($fh) = $self-&gt;{' INFILE'};
    my ($i, $dat);
    
    $self-&gt;SUPER::read or return $self;

    for ($i = 0; $i &lt; $numh; $i++)
    {
        $fh-&gt;read($dat, 4);
        ($self-&gt;{$tAdv}[$i], $self-&gt;{$tLsb}[$i]) = unpack("nn", $dat);
        $self-&gt;{$tLsb}[$i] -= 65536 if ($self-&gt;{$tLsb}[$i] &gt;= 32768);
    }
    
    $i--;
    while (++$i &lt; $numg)
    {
        $fh-&gt;read($dat, 2);
        $self-&gt;{$tAdv}[$i] = $self-&gt;{$tAdv}[$numh - 1];
        $self-&gt;{$tLsb}[$i] = unpack("n", $dat);
        $self-&gt;{$tLsb}[$i] -= 65536 if ($self-&gt;{$tLsb}[$i] &gt;= 32768);
    }
    $self;
}
    
=head2 $t-&gt;numMetrics

Calculates again the number of long metrics required to store the information
here. Returns undef if the table has not been read.

=cut

sub numMetrics
{
    my ($self) = @_;
    my ($numg) = $self-&gt;{' PARENT'}{'maxp'}{'numGlyphs'};
    my ($i);

    return undef unless $self-&gt;{' read'};

    for ($i = $numg - 2; $i &gt;= 0; $i--)
    { last if ($self-&gt;{'advance'}[$i] != $self-&gt;{'advance'}[$i + 1]); }

    return $i + 2;
}


=head2 $t-&gt;out($fh)

Writes the metrics to a TTF file. Assumes that the C&lt;hhea&gt; has updated the
numHMetrics from here

=cut

sub out
{
    my ($self, $fh) = @_;
    my ($numg) = $self-&gt;{' PARENT'}{'maxp'}{'numGlyphs'};
    my ($numh) = $self-&gt;{' PARENT'}{'hhea'}-&gt;read-&gt;{'numberOfHMetrics'};
    $self-&gt;_out($fh, $numg, $numh, "advance", "lsb");
}

sub _out
{
    my ($self, $fh, $numg, $numh, $tAdv, $tLsb) = @_;
    my ($i, $lsb);

    return $self-&gt;SUPER::out($fh) unless ($self-&gt;{' read'});

    for ($i = 0; $i &lt; $numg; $i++)
    {
        $lsb = $self-&gt;{$tLsb}[$i];
        $lsb += 65536 if $lsb &lt; 0;
        if ($i &gt;= $numh)
        { $fh-&gt;print(pack("n", $lsb)); }
        else
        { $fh-&gt;print(pack("n2", $self-&gt;{$tAdv}[$i], $lsb)); }
    }
    $self;
}


=head2 $t-&gt;update

Updates the lsb values from the xMin from the each glyph

=cut

sub update
{
    my ($self) = @_;
    my ($numg) = $self-&gt;{' PARENT'}{'maxp'}{'numGlyphs'};
    my ($i);

    return undef unless ($self-&gt;SUPER::update);
# lsb &amp; xMin must always be the same, regardless of any flags!
#    return $self unless ($self-&gt;{' PARENT'}{'head'}{'flags'} &amp; 2);        # lsb &amp; xMin the same

    $self-&gt;{' PARENT'}{'loca'}-&gt;update;
    for ($i = 0; $i &lt; $numg; $i++)
    {
        my ($g) = $self-&gt;{' PARENT'}{'loca'}{'glyphs'}[$i];
        if ($g)
        { $self-&gt;{'lsb'}[$i] = $g-&gt;read-&gt;update_bbox-&gt;{'xMin'}; }
        else
        { $self-&gt;{'lsb'}[$i] = 0; }
    }
    $self-&gt;{' PARENT'}{'head'}{'flags'} |= 2;
    $self;
}
    

=head2 $t-&gt;out_xml($context, $depth)

Outputs the table in XML

=cut

sub out_xml
{
    my ($self, $context, $depth) = @_;
    my ($fh) = $context-&gt;{'fh'};
    my ($numg) = $self-&gt;{' PARENT'}{'maxp'}{'numGlyphs'};
    my ($addr) = ($self =~ m/\((.+)\)$/o);
    my ($i);

    if ($context-&gt;{'addresses'}{$addr})
    {
        $fh-&gt;printf("%s&lt;%s id_ref='%s'/&gt;\n", $depth, $context-&gt;{'name'}, $addr);
        return $self;
    }
    else
    { $fh-&gt;printf("%s&lt;%s id='%s'&gt;\n", $depth, $context-&gt;{'name'}, $addr); }

    $self-&gt;read;

    for ($i = 0; $i &lt; $numg; $i++)
    { $fh-&gt;print("$depth$context-&gt;{'indent'}&lt;width adv='$self-&gt;{'advance'}[$i]' lsb='$self-&gt;{'lsb'}[$i]'/&gt;\n"); }

    $fh-&gt;print("$depth&lt;/$context-&gt;{'name'}&gt;\n");
    $self;
}

1;

=head1 BUGS

None known

=head1 AUTHOR

Martin Hosken Martin_Hosken@sil.org. See L&lt;Font::TTF::Font&gt; for copyright and
licensing.

=cut

</pre></body></html>