File: Font_Test.pm

package info (click to toggle)
libpostscript-file-perl 2.22%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 820 kB
  • ctags: 274
  • sloc: perl: 7,056; sh: 51; makefile: 2
file content (133 lines) | stat: -rw-r--r-- 3,869 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#---------------------------------------------------------------------
package Font_Test;
#
# Copyright 2009 Christopher J. Madsen
#
# Author: Christopher J. Madsen <perl@cjmweb.net>
# Created: 31 Oct 2009
#
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See either the
# GNU General Public License or the Artistic License for more details.
#
# Compare our font metrics against Font::AFM
#---------------------------------------------------------------------

use 5.008;
our $VERSION = '2.00';

use strict;
use warnings;
use Exporter 5.57 'import';
use constant number_of_tests => 275;
use Test::More tests => number_of_tests;

use PostScript::File::Metrics;

our @EXPORT = qw(test_font);

our %attribute = qw(
  FullName           full_name
  FamilyName         family
  Weight             weight
  IsFixedPitch       fixed_pitch
  ItalicAngle        italic_angle
  FontBBox           font_bbox
);

our %dimension_attribute = qw(
  UnderlinePosition  underline_position
  UnderlineThickness underline_thickness
  CapHeight          cap_height
  XHeight            x_height
  Ascender           ascender
  Descender          descender
);
# We don't test this:
#  Version            version

#=====================================================================

sub test_font
{
  my ($font) = @_;

  my $size = 125;
  my $factor = 1000 / $size;

  my $metrics = PostScript::File::Metrics->new($font, $size, 'iso-8859-1');

  isa_ok($metrics, 'PostScript::File::Metrics');

  ok(!$INC{'PostScript/File/Metrics/Loader.pm'},
     'used pre-compiled metrics');

  SKIP: {
    my $testsInBlock = number_of_tests - 2;

    # RECOMMEND PREREQ: Font::AFM
    # Construct the Font::AFM object, or skip the remaining tests:
    eval { require Font::AFM };

    skip "Font::AFM not installed", $testsInBlock if $@;

    my $afm = eval { Font::AFM->new($font) };

    skip "Font::AFM can't find $font.afm", $testsInBlock if $@;

    # Compare the font attributes:
    foreach my $afm_method (sort keys %attribute) {
      my $metrics_method = $attribute{$afm_method};
      my $got = $metrics->$metrics_method;
      if ($afm_method eq 'FontBBox') {
        $_ *= $factor for @$got;
        $got = "@$got" ;
      } # end if FontBBox
      $got = $got ? 'true' : 'false' if $afm_method eq 'IsFixedPitch';
      is($got, $afm->$afm_method, $afm_method);
    }

    # Compare the font dimension attributes:
    foreach my $afm_method (sort keys %dimension_attribute) {
      my $metrics_method = $dimension_attribute{$afm_method};
      my $got = $metrics->$metrics_method;
      is($got * $factor, $afm->$afm_method, $afm_method);
    }

    # Compare the character widths:
    my $wx = $afm->latin1_wx_table;
    $metrics->set_auto_hyphen(0); # Font::AFM doesn't translate hyphen-minus
    $metrics->set_size(undef);    # Switch to default size to match Font::AFM

    for my $char (0 .. 255) {
      my $name = sprintf 'width of char \%03o, \x%02X', $char, $char;
      $name = sprintf '%s (%c)', $name, $char
          if $char >= 0x20 and $char < 0x7F;
      is( $metrics->width(pack 'C', $char), $wx->[$char], $name);
    } # end for $char

    # Test width vs stringwidth:
    $metrics->set_size($size);  # Switch back to old size
    while (<DATA>) {
      chomp $_;
      is( $metrics->width($_), $afm->stringwidth($_, $size), $_);
    }

  } # end SKIP
} # end test_font

#=====================================================================
# Package Return Value:

1;

__DATA__
Now is the time for all good men to come to the aid of their country.
The quick brown fox jumps over the lazy dog.
carwash
car-wash
-1