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

=head1 NAME

Font::TTF::Head - The head table for a TTF Font

=head1 DESCRIPTION

This is a very basic table with just instance variables as described in the
TTF documentation, using the same names. One of the most commonly used is
C&lt;unitsPerEm&gt;.

=head1 INSTANCE VARIABLES

The C&lt;head&gt; table has no internal instance variables beyond those common to all
tables and those specified in the standard:

    version
    fontRevision
    checkSumAdjustment
    magicNumber
    flags
    unitsPerEm
    created
    modified
    xMin
    yMin
    xMax
    yMax
    macStyle
    lowestRecPPEM
    fontDirectionHint
    indexToLocFormat
    glyphDataFormat

The two dates are held as an array of two unsigned longs (32-bits)

=head1 METHODS

=cut

use strict;
use vars qw(@ISA %fields @field_info);

require Font::TTF::Table;
use Font::TTF::Utils;

@ISA = qw(Font::TTF::Table);
@field_info = (
    'version' =&gt; 'v',
    'fontRevision' =&gt; 'f',
    'checkSumAdjustment' =&gt; 'L',
    'magicNumber' =&gt; 'L',
    'flags' =&gt; 'S',
    'unitsPerEm' =&gt; 'S',
    'created' =&gt; 'L2',
    'modified' =&gt; 'L2',
    'xMin' =&gt; 's',
    'yMin' =&gt; 's',
    'xMax' =&gt; 's',
    'yMax' =&gt; 's',
    'macStyle' =&gt; 'S',
    'lowestRecPPEM' =&gt; 'S',
    'fontDirectionHint' =&gt; 's',
    'indexToLocFormat' =&gt; 's',
    'glyphDataFormat' =&gt; 's');

sub init
{
    my ($k, $v, $c, $i);
    for ($i = 0; $i &lt; $#field_info; $i += 2)
    {
        ($k, $v, $c) = TTF_Init_Fields($field_info[$i], $c, $field_info[$i + 1]);
        next unless defined $k &amp;&amp; $k ne "";
        $fields{$k} = $v;
    }
}


=head2 $t-&gt;read

Reads the table into memory thanks to some utility functions

=cut

sub read
{
    my ($self) = @_;
    my ($dat);

    $self-&gt;SUPER::read || return $self;

    init unless defined $fields{'Ascender'};
    $self-&gt;{' INFILE'}-&gt;read($dat, 54);

    TTF_Read_Fields($self, $dat, \%fields);
    $self;
}


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

Writes the table to a file either from memory or by copying. If in memory
(which is usually) the checkSumAdjustment field is set to 0 as per the default
if the file checksum is not to be considered.

=cut

sub out
{
    my ($self, $fh) = @_;

    return $self-&gt;SUPER::out($fh) unless $self-&gt;{' read'};      # this is never true
#    $self-&gt;{'checkSumAdjustment'} = 0 unless $self-&gt;{' PARENT'}{' wantsig'};
    $fh-&gt;print(TTF_Out_Fields($self, \%fields, 54));
    $self;
}


=head2 $t-&gt;XML_element($context, $depth, $key, $value)

Handles date process for the XML exporter

=cut

sub XML_element
{
    my ($self) = shift;
    my ($context, $depth, $key, $value) = @_;
    my ($fh) = $context-&gt;{'fh'};
    my ($output, @time);
    my (@month) = qw(JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC);

    return $self-&gt;SUPER::XML_element(@_) unless ($key eq 'created' || $key eq 'modified');

    @time = gmtime($self-&gt;getdate($key eq 'created'));
    $output = sprintf("%d/%s/%d %d:%d:%d", $time[3], $month[$time[4]], $time[5] + 1900,
            $time[2], $time[1], $time[0]);
    $fh-&gt;print("$depth&lt;$key&gt;$output&lt;/$key&gt;\n");
    $self;
}
    

=head2 $t-&gt;update

Updates the head table based on the glyph data and the hmtx table

=cut

sub update
{
    my ($self) = @_;
    my ($num, $i, $loc, $hmtx);
    my ($xMin, $yMin, $xMax, $yMax, $lsbx);

    return undef unless ($self-&gt;SUPER::update);

    $num = $self-&gt;{' PARENT'}{'maxp'}{'numGlyphs'};
    return undef unless (defined $self-&gt;{' PARENT'}{'hmtx'} &amp;&amp; defined $self-&gt;{' PARENT'}{'loca'});
    $hmtx = $self-&gt;{' PARENT'}{'hmtx'}-&gt;read;
    
    $self-&gt;{' PARENT'}{'loca'}-&gt;update;
    $hmtx-&gt;update;              # if we updated, then the flags will be set anyway.
    $lsbx = 1;
    for ($i = 0; $i &lt; $num; $i++)
    {
        $loc = $self-&gt;{' PARENT'}{'loca'}{'glyphs'}[$i];
        next unless defined $loc;
        $loc-&gt;read-&gt;update_bbox;
        $xMin = $loc-&gt;{'xMin'} if ($loc-&gt;{'xMin'} &lt; $xMin || $i == 0);
        $yMin = $loc-&gt;{'yMin'} if ($loc-&gt;{'yMin'} &lt; $yMin || $i == 0);
        $xMax = $loc-&gt;{'xMax'} if ($loc-&gt;{'xMax'} &gt; $xMax);
        $yMax = $loc-&gt;{'yMax'} if ($loc-&gt;{'yMax'} &gt; $yMax);
        $lsbx &amp;= ($loc-&gt;{'xMin'} == $hmtx-&gt;{'lsb'}[$i]);
    }
    $self-&gt;{'xMin'} = $xMin;
    $self-&gt;{'yMin'} = $yMin;
    $self-&gt;{'xMax'} = $xMax;
    $self-&gt;{'yMax'} = $yMax;
    if ($lsbx)
    { $self-&gt;{'flags'} |= 2; }
    else
    { $self-&gt;{'flags'} &amp;= ~2; }
    $self;
}


=head2 $t-&gt;getdate($is_create)

Converts font modification time (or creation time if $is_create is set) to a 32-bit integer as returned
from time(). Returns undef if the value is out of range, either before the epoch or after the maximum
storable time.

=cut

sub getdate
{
    my ($self, $is_create) = @_;
    my ($arr) = $self-&gt;{$is_create ? 'created' : 'modified'};

    $arr-&gt;[1] -= 2082844800;        # seconds between 1/Jan/1904 and 1/Jan/1970 (midnight)
    if ($arr-&gt;[1] &lt; 0)
    {
        $arr-&gt;[1] += 0xFFFFFFF; $arr-&gt;[1]++;
        $arr-&gt;[0]--;
    }
    return undef if $arr-&gt;[0] != 0;
    return $arr-&gt;[1];
}


=head2 $t-&gt;setdate($time, $is_create)

Sets the time information for modification (or creation time if $is_create is set) according to the 32-bit
time information.

=cut

sub setdate
{
    my ($self, $time, $is_create) = @_;
    my (@arr);

    $arr[1] = $time;
    if ($arr[1] &gt;= 0x83DA4F80)
    {
        $arr[1] -= 0xFFFFFFFF;
        $arr[1]--;
        $arr[0]++;
    }
    $arr[1] += 2082844800;
    $self-&gt;{$is_create ? 'created' : 'modified'} = \@arr;
    $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>