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

=head1 NAME

Font::TTF::OS_2 - the OS/2 table in a TTF font

=head1 DESCRIPTION

The OS/2 table has two versions and forms, one an extension of the other. This
module supports both forms and the switching between them.

=head1 INSTANCE VARIABLES

No other variables than those in table and those in the standard:

    Version
    xAvgCharWidth
    usWeightClass
    usWidthClass
    fsType
    ySubscriptXSize
    ySubScriptYSize
    ySubscriptXOffset
    ySubscriptYOffset
    ySuperscriptXSize
    ySuperscriptYSize
    ySuperscriptXOffset
    ySuperscriptYOffset
    yStrikeoutSize
    yStrikeoutPosition
    sFamilyClass
    bFamilyType
    bSerifStyle
    bWeight
    bProportion
    bContrast
    bStrokeVariation
    bArmStyle
    bLetterform
    bMidline
    bXheight
    ulUnicodeRange1
    ulUnicodeRange2
    ulUnicodeRange3
    ulUnicodeRange4
    achVendID
    fsSelection
    usFirstCharIndex
    usLastCharIndex
    sTypoAscender
    sTypoDescender
    sTypoLineGap
    usWinAscent
    usWinDescent
    ulCodePageRange1
    ulCodePageRange2
    xHeight
    CapHeight
    defaultChar
    breakChar
    maxLookups

Notice that versions 0, 1, 2 &amp; 3 of the table are supported. Notice also that the
Panose variable has been broken down into its elements.

=head1 METHODS

=cut

use strict;
use vars qw(@ISA @fields @lens @field_info @weights);
use Font::TTF::Table;

@ISA = qw(Font::TTF::Table);
@field_info = (
    'xAvgCharWidth' =&gt; 's',
    'usWeightClass' =&gt; 'S',
    'usWidthClass' =&gt; 'S',
    'fsType' =&gt; 's',
    'ySubscriptXSize' =&gt; 's',
    'ySubScriptYSize' =&gt; 's',
    'ySubscriptXOffset' =&gt; 's',
    'ySubscriptYOffset' =&gt; 's',
    'ySuperscriptXSize' =&gt; 's',
    'ySuperscriptYSize' =&gt; 's',
    'ySuperscriptXOffset' =&gt; 's',
    'ySuperscriptYOffset' =&gt; 's',
    'yStrikeoutSize' =&gt; 's',
    'yStrikeoutPosition' =&gt; 's',
    'sFamilyClass' =&gt; 's',
    'bFamilyType' =&gt; 'C',
    'bSerifStyle' =&gt; 'C',
    'bWeight' =&gt; 'C',
    'bProportion' =&gt; 'C',
    'bContrast' =&gt; 'C',
    'bStrokeVariation' =&gt; 'C',
    'bArmStyle' =&gt; 'C',
    'bLetterform' =&gt; 'C',
    'bMidline' =&gt; 'C',
    'bXheight' =&gt; 'C',
    'ulUnicodeRange1' =&gt; 'L',
    'ulUnicodeRange2' =&gt; 'L',
    'ulUnicodeRange3' =&gt; 'L',
    'ulUnicodeRange4' =&gt; 'L',
    'achVendID' =&gt; 'L',
    'fsSelection' =&gt; 'S',
    'usFirstCharIndex' =&gt; 'S',
    'usLastCharIndex' =&gt; 'S',
    'sTypoAscender' =&gt; 'S',
    'sTypoDescender' =&gt; 's',
    'sTypoLineGap' =&gt; 'S',
    'usWinAscent' =&gt; 'S',
    'usWinDescent' =&gt; 'S',
    '' =&gt; '',
    'ulCodePageRange1' =&gt; 'L',
    'ulCodePageRange2' =&gt; 'L',
    '' =&gt; '',
    'xHeight' =&gt; 's',
    'CapHeight' =&gt; 's',
    'defaultChar' =&gt; 'S',
    'breakChar' =&gt; 'S',
    'maxLookups' =&gt; 's',
    '' =&gt; '',            # i.e. v3 is basically same as v2
    );

@weights = qw(64 14 27 35 100 20 14 42 63 3 6 35 20 56 56 17 4 49 56 71 31 10 18 3 18 2 166);

use Font::TTF::Utils;

sub init
{
    my ($k, $v, $c, $n, $i, $t, $j);

    $n = 0;
    @lens = (76, 84, 94, 94);
    for ($j = 0; $j &lt; $#field_info; $j += 2)
    {
        if ($field_info[$j] eq '')
        {
            $n++;
            next;
        }
        ($k, $v, $c) = TTF_Init_Fields($field_info[$j], $c, $field_info[$j+1]);
        next unless defined $k &amp;&amp; $k ne "";
        for ($i = $n; $i &lt; 4; $i++)
        { $fields[$i]{$k} = $v; }
    }
}


=head2 $t-&gt;read

Reads in the various values from disk (see details of OS/2 table)

=cut

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

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

    init unless defined $fields[2]{'xAvgCharWidth'};
    $self-&gt;{' INFILE'}-&gt;read($dat, 2);
    $ver = unpack("n", $dat);
    $self-&gt;{'Version'} = $ver;
    if ($ver &lt; 4)
    {
        $self-&gt;{' INFILE'}-&gt;read($dat, $lens[$ver]);
        TTF_Read_Fields($self, $dat, $fields[$ver]);
    }
    $self;
}


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

Writes the table to a file either from memory or by copying.

=cut

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

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

    $ver = $self-&gt;{'Version'};
    $fh-&gt;print(pack("n", $ver));
    $fh-&gt;print(TTF_Out_Fields($self, $fields[$ver], $lens[$ver]));
    $self;
}


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

Tidies up the hex values to output them in hex

=cut

sub XML_element
{
    my ($self) = shift;
    my ($context, $depth, $key, $value) = @_;
    my ($fh) = $context-&gt;{'fh'};

    if ($key =~ m/^ul(?:Unicode|CodePage)Range\d$/o)
    { $fh-&gt;printf("%s&lt;%s&gt;%08X&lt;/%s&gt;\n", $depth, $key, $value, $key); }
    elsif ($key eq 'achVendID')
    { $fh-&gt;printf("%s&lt;%s name='%s'/&gt;\n", $depth, $key, pack('N', $value)); }
    else
    { return $self-&gt;SUPER::XML_element(@_); }
    $self;
}


=head2 $t-&gt;XML_end($context, $tag, %attrs)

Now handle them on the way back in

=cut

sub XML_end
{
    my ($self) = shift;
    my ($context, $tag, %attrs) = @_;

    if ($tag =~ m/^ul(?:Unicode|CodePage)Range\d$/o)
    { return hex($context-&gt;{'text'}); }
    elsif ($tag eq 'achVendID')
    { return unpack('N', $attrs{'name'}); }
    else
    { return $self-&gt;SUPER::XML_end(@_); }
}

=head2 $t-&gt;update

Updates the OS/2 table by getting information from other sources:

Updates the C&lt;firstChar&gt; and C&lt;lastChar&gt; values based on the MS table in the
cmap.

Updates the sTypoAscender, sTypoDescender &amp; sTypoLineGap to be the same values
as Ascender, Descender and Linegap from the hhea table (assuming it is dirty)
and also sets usWinAscent to be the sum of Ascender+Linegap and usWinDescent to
be the negative of Descender.

=cut

sub update
{
    my ($self) = @_;
    my ($map, @keys, $table, $i, $avg, $hmtx);

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

    $self-&gt;{' PARENT'}{'cmap'}-&gt;update;
    $map = $self-&gt;{' PARENT'}{'cmap'}-&gt;find_ms || return undef;
    $hmtx = $self-&gt;{' PARENT'}{'hmtx'}-&gt;read;

    @keys = sort {$a &lt;=&gt; $b} grep {$_ &lt; 0x10000} keys %{$map-&gt;{'val'}};

    $self-&gt;{'usFirstCharIndex'} = $keys[0];
    $self-&gt;{'usLastCharIndex'} = $keys[-1];

    $table = $self-&gt;{' PARENT'}{'hhea'}-&gt;read;
    
    # try any way we can to get some real numbers passed around!
    if (($self-&gt;{'fsSelection'} &amp; 128) != 0)
    {
        # assume the user knows what they are doing and has sensible values already
    }
    elsif ($table-&gt;{'Ascender'} != 0 || $table-&gt;{'Descender'} != 0)
    {
        $self-&gt;{'sTypoAscender'} = $table-&gt;{'Ascender'};
        $self-&gt;{'sTypoDescender'} = $table-&gt;{'Descender'};
        $self-&gt;{'sTypoLineGap'} = $table-&gt;{'LineGap'};
        $self-&gt;{'usWinAscent'} = $self-&gt;{'sTypoAscender'} + $self-&gt;{'sTypoLineGap'};
        $self-&gt;{'usWinDescent'} = -$self-&gt;{'sTypoDescender'};
    }
    elsif ($self-&gt;{'sTypoAscender'} != 0 || $self-&gt;{'sTypoDescender'} != 0)
    {
        $table-&gt;{'Ascender'} = $self-&gt;{'sTypoAscender'};
        $table-&gt;{'Descender'} = $self-&gt;{'sTypoDescender'};
        $table-&gt;{'LineGap'} = $self-&gt;{'sTypoLineGap'};
        $self-&gt;{'usWinAscent'} = $self-&gt;{'sTypoAscender'} + $self-&gt;{'sTypoLineGap'};
        $self-&gt;{'usWinDescent'} = -$self-&gt;{'sTypoDescender'};
    } 
    elsif ($self-&gt;{'usWinAscent'} != 0 || $self-&gt;{'usWinDescent'} != 0)
    {
        $self-&gt;{'sTypoAscender'} = $table-&gt;{'Ascender'} = $self-&gt;{'usWinAscent'};
        $self-&gt;{'sTypoDescender'} = $table-&gt;{'Descender'} = -$self-&gt;{'usWinDescent'};
        $self-&gt;{'sTypoLineGap'} = $table-&gt;{'LineGap'} = 0;
    }

    if ($self-&gt;{'Version'} &lt; 3)
    {
        for ($i = 0; $i &lt; 26; $i++)
        { $avg += $hmtx-&gt;{'advance'}[$map-&gt;{'val'}{$i + 0x0061}] * $weights[$i]; }
        $avg += $hmtx-&gt;{'advance'}[$map-&gt;{'val'}{0x0020}] * $weights[-1];
        $self-&gt;{'xAvgCharWidth'} = $avg / 1000;
    }
    elsif ($self-&gt;{'Version'} &gt; 2)
    {
        $i = 0; $avg = 0;
        foreach (@{$hmtx-&gt;{'advance'}})
        {
            next unless ($_);
            $i++;
            $avg += $_;
        }
        $avg /= $i if ($i);
        $self-&gt;{'xAvgCharWidth'} = $avg;
    }

    foreach $i (keys %{$map-&gt;{'val'}})
    {
        if ($i &gt;= 0x10000)
        {
            $self-&gt;{'ulUnicodeRange2'} |= 0x2000000;
            last;
        }
    }

    $self-&gt;{'Version'} = 1 if (defined $self-&gt;{'ulCodePageRange1'} &amp;&amp; $self-&gt;{'Version'} &lt; 1);
    $self-&gt;{'Version'} = 2 if (defined $self-&gt;{'maxLookups'} &amp;&amp; $self-&gt;{'Version'} &lt; 2);
    
    if ((exists $self-&gt;{' PARENT'}{'GPOS'} &amp;&amp; $self-&gt;{' PARENT'}{'GPOS'}{' read'}) ||
        (exists $self-&gt;{' PARENT'}{'GSUB'} &amp;&amp; $self-&gt;{' PARENT'}{'GSUB'}{' read'}))
    {
        # one or both of GPOS &amp; GSUB exist and have been read or modified; so update usMaxContexts
        my ($lp, $ls);
        $lp = $self-&gt;{' PARENT'}{'GPOS'}-&gt;maxContext if exists $self-&gt;{' PARENT'}{'GPOS'};
        $ls = $self-&gt;{' PARENT'}{'GSUB'}-&gt;maxContext if exists $self-&gt;{' PARENT'}{'GSUB'};
        $self-&gt;{'maxLookups'} = $lp &gt; $ls ? $lp : $ls;
    }
    
    $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>