/usr/lib/perl5/Prima/PS/Fonts.pm is in libprima-perl 1.28-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | #
# Copyright (c) 1997-2002 The Protein Laboratory, University of Copenhagen
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# Created by Dmitry Karasik <dk@plab.ku.dk>
# $Id: Fonts.pm,v 1.11 2007/09/13 15:12:25 dk Exp $
#
package Prima::PS::Fonts;
use strict;
use Prima;
use Prima::Utils;
use Prima::PS::Encodings;
use vars qw(%files %enum_families $defaultFontName
$variablePitchName $fixedPitchName $symbolFontName);
my %cache;
$defaultFontName = 'Helvetica';
$variablePitchName = 'Helvetica';
$fixedPitchName = 'Courier';
$symbolFontName = 'Symbol';
sub query_metrics
{
my $f = $_[0];
my ( $file, $name, $family);
if ( exists $enum_families{$f}) {
$family = $f;
$f = $enum_families{$f};
}
$name = $f;
if ( exists $files{$f}) {
$file = $files{ $f};
unless ( defined $family) {
# pick up family
for ( keys %enum_families) {
$family = $_, last if $f =~ m[^$_];
}
}
$family = $defaultFontName unless defined $family;
} else {
$family = $defaultFontName;
$name = $enum_families{$family};
$file = $files{ $name};
}
return $cache{$file} if exists $cache{$file};
my $defFN = $files{ $defaultFontName};
my $fx = Prima::Utils::find_image( $file);
unless ( $fx) {
if ( $name eq $defaultFontName) {
warn("Prima::PS::Fonts: can't load default font\n");
return undef;
} else {
warn("Broken Prima::PS::Fonts installation: $name not found\n"),
return query_metrics( $defaultFontName)
}
}
unless ( open F, $fx) {
warn( "Prima::PS::Fonts: cannot open file: $fx\n") ;
return undef if $f eq $defaultFontName;
return query_metrics( $defaultFontName);
}
{
local $/;
my @ra;
my $z = '@ra = ' . <F>;
close F;
eval($z);
if ( $@ || scalar(@ra) < 2) {
warn( "Prima::PS::Fonts: invalid file: $fx\n");
return undef if $name eq $defaultFontName;
return query_metrics( $defaultFontName);
}
$ra[1]-> {docname} = $name;
$ra[1]-> {name} = $name;
$ra[1]-> {family} = $family;
$ra[1]-> {charheight} = $ra[1]-> {height};
$cache{$file} = $ra[1];
}
return $cache{$file};
}
sub enum_fonts
{
my ( $family, $encoding) = @_;
my @names;
$family = undef if defined $family && !length $family;
$encoding = undef if defined $encoding && !length $encoding;
if ( defined $family) {
return [] unless defined $enum_families{$family};
for ( keys %enum_families) {
push @names, $_ if $_ eq $family;
}
} else {
@names = keys %enum_families;
}
my @ret;
for ( @names) {
my %x = %{query_metrics( $_)};
$x{name} = $x{family};
delete $x{docname};
delete $x{chardata};
$_ = \%x;
my $latin = exists( $Prima::PS::Encodings::files{$x{encoding}});
next if defined($encoding) && (
$latin ?
! exists $Prima::PS::Encodings::files{$encoding} :
($x{encoding} ne $encoding)
);
if ( !defined $family && !defined $encoding) {
$x{encodings} = $latin ? [ Prima::PS::Encodings::unique ] : [ $x{encoding} ];
}
if (
defined $family &&
!defined $encoding &&
exists( $Prima::PS::Encodings::files{$x{encoding}})
) {
push @ret, map {
my %n = %x;
$n{encoding} = $_;
\%n;
} Prima::PS::Encodings::unique;
} else {
$_-> {encoding} = $encoding if defined( $encoding) && $latin;
push @ret, $_;
}
}
return \@ret;
}
sub enum_family
{
my $family = $_[0];
my @names;
return unless defined $enum_families{$family};
for ( keys %files) {
push @names, $_ if m/^$family/;
}
return @names;
}
sub font_pick
{
my ( $src, $dest, %options) = @_;
my $bySize = exists( $src-> {size}) && !exists( $src-> {height});
$dest = Prima::Drawable-> font_match( $src, $dest, 0);
$dest-> {encoding} = ''
if ( ! exists ( $Prima::PS::Encodings::fontspecific{ $dest-> {encoding}}) &&
! exists ( $Prima::PS::Encodings::files{ $dest-> {encoding}}));
# find name
my $m1 = query_metrics( $dest-> {name});
# find encoding
if ( length $dest-> {encoding}) {
if ( defined $dest-> {encoding}) {
if ( exists ( $Prima::PS::Encodings::files{ $dest-> {encoding}} ) &&
! exists ( $Prima::PS::Encodings::files{ $m1-> {encoding}})) {
$m1 = query_metrics( $fixedPitchName);
$dest-> {encoding} = $m1-> {encoding};
$dest-> {name} = $m1-> {name};
$dest-> {family} = $m1-> {family};
} elsif (
exists ( $Prima::PS::Encodings::fontspecific{ $dest-> {encoding}}) &&
! exists ( $Prima::PS::Encodings::fontspecific{ $m1-> {encoding}})
) {
$m1 = query_metrics( $symbolFontName);
$dest-> {encoding} = $m1-> {encoding};
$dest-> {name} = $m1-> {name};
$dest-> {family} = $m1-> {family};
}
}
} else {
$dest-> {encoding} = $m1-> {encoding};
}
# find pitch
if ( $dest-> {pitch} != fp::Default && $dest-> {pitch} != $m1-> {pitch}) {
if ( $dest-> {pitch} == fp::Variable) {
$m1 = query_metrics( $variablePitchName);
} else {
$m1 = query_metrics( $fixedPitchName);
}
}
# get all family members
my @famx = map { query_metrics( $_) } enum_family( $m1-> {family});
# find style
my $m2;
for ( @famx) { # exact match
$m2 = $_, last if $_-> {style} == $dest-> {style};
}
unless ( $m2) { # second pass
my $maxDiff = 1000;
my ( $italic, $bold) = (
( $dest-> {style} & fs::Italic) ? 1 : 0,
($dest-> {style} & fs::Bold) ? 1 :0
);
for ( @famx) {
my ( $i, $b) = (
( $_-> {style} & fs::Italic) ? 1 : 0,
( $_-> {style} & fs::Bold) ? 1 : 0
);
my $diff = (( $i == $italic) ? 0 : 2) + (( $b == $bold) ? 0 : 1);
$m2 = $_, $maxDiff = $diff if $diff < $maxDiff;
}
}
$m2 = $m1 unless defined $m2;
# scale dimensions
my $res = $options{resolution} ? $options{resolution} : $m2-> {yDeviceRes};
if ( $bySize) {
$dest-> {height} = int( $dest-> {size} * $res / 72.27 + 0.5);
} else {
$dest-> {size} = int( $dest-> {height} * 72.27 / $res + 0.5);
}
my $a = $dest-> {height} / $m2-> {height};
my %muls = %$m2;
my $charheight = $muls{height};
my $du = $dest-> {style} & fs::Underlined;
my $ds = $dest-> {style} & fs::StruckOut;
my $dw = $dest-> {width};
$muls{$_} = int ( $muls{$_} * $a + 0.5) for
qw( height ascent descent width maximalWidth internalLeading externalLeading);
delete $muls{size};
my $enc = $dest-> {encoding};
$dest-> {$_} = $muls{$_} for keys %muls;
$dest-> {encoding} = $enc;
$dest-> {style} |= fs::Underlined if $du;
$dest-> {style} |= fs::StruckOut if $ds;
$dest-> {width} = $dw if $dw != 0;
$dest-> {charheight} = $charheight;
return $dest;
}
%files = map { $_ => "PS/fonts/$_" } (
'Bookman-Demi' ,
'Bookman-DemiItalic' ,
'Bookman-Light' ,
'Bookman-LightItalic' ,
'Courier' ,
'Courier-Oblique' ,
'Courier-Bold' ,
'Courier-BoldOblique' ,
'AvantGarde-Book' ,
'AvantGarde-BookOblique' ,
'AvantGarde-Demi' ,
'AvantGarde-DemiOblique' ,
'Helvetica' ,
'Helvetica-Oblique' ,
'Helvetica-Bold' ,
'Helvetica-BoldOblique' ,
'Helvetica-Narrow' ,
'Helvetica-Narrow-Oblique' ,
'Helvetica-Narrow-Bold' ,
'Helvetica-Narrow-BoldOblique',
'Palatino-Roman' ,
'Palatino-Italic' ,
'Palatino-Bold' ,
'Palatino-BoldItalic' ,
'NewCenturySchlbk-Roman' ,
'NewCenturySchlbk-Italic' ,
'NewCenturySchlbk-Bold' ,
'NewCenturySchlbk-BoldItalic' ,
'Times-Roman' ,
'Times-Italic' ,
'Times-Bold' ,
'Times-BoldItalic' ,
'Symbol' ,
'ZapfChancery-MediumItalic' ,
'ZapfDingbats' ,
);
# The keys of %enum_families are only font names, - in Prima terms.
# The only problem that the family field is always the same as the
# font name
%enum_families = (
'Bookman' => 'Bookman-Light',
'Courier' => 'Courier',
'AvantGarde' => 'AvantGarde-Book',
'Helvetica' => 'Helvetica',
'Helvetica-Narrow' => 'Helvetica-Narrow',
'Palatino' => 'Palatino-Roman',
'NewCenturySchlbk' => 'NewCenturySchlbk-Roman',
'Times' => 'Times-Roman',
'Symbol' => 'Symbol',
'ZapfChancery' => 'ZapfChancery-MediumItalic',
'ZapfDingbats' => 'ZapfDingbats',
);
1;
__END__
=pod
=head1 NAME
Prima::PS::Fonts - PostScript device fonts metrics
=head1 SYNOPSIS
use Prima;
use Prima::PS::Fonts;
=head1 DESCRIPTION
This module primary use is to be invoked from Prima::PS::Drawable module.
Assumed that some common fonts like Times and Courier are supported by PS
interpreter, and it is assumed that typeface is preserved more-less the
same, so typesetting based on font's a-b-c metrics can be valid.
35 font files are supplied with 11 font families. Font files with metrics
located into 'fonts' subdirectory.
=over
=item query_metrics( $fontName)
Returns font metric hash with requested font data, uses $defaultFontName
if give name is not found. Metric hash is the same as Prima::Types::Font
record, plus 3 extra fields: 'docname' containing font name ( equals
always to 'name'), 'chardata' - hash of named glyphs, 'charheight' -
the height that 'chardata' is rendered to. Every hash
entry in 'chardata' record contains four numbers - suggested character
index and a, b and c glyph dimensions with height equals 'charheight'.
=item enum_fonts( $fontFamily)
Returns font records for given family, or all families
perpesented by one member, if no family name given.
If encoding specified, returns only the fonts with the encoding given.
Compliant to Prima::Application::fonts interface.
=item files & enum_families
Hash with paths to font metric files. File names not necessarily
should be as font names, and it is possible to override font name
contained in the file just by specifying different font key - this
case will be recognized on loading stage and loaded font structure
patched correspondingly.
Example:
$Prima::PS::Fonts::files{Standard Symbols} = $Prima::PS::Fonts::files{Symbol};
$Prima::PS::Fonts::files{'Device-specific symbols, set 1'} = 'my/devspec/data.1';
$Prima::PS::Fonts::files{'Device-specific symbols, set 2'} = 'my/devspec/data.2';
$Prima::PS::Fonts::enum_families{DevSpec} = 'Device-specific symbols, set 1';
=item font_pick( $src, $dest, %options)
Merges two font records using Prima::Drawable::font_match, picks
the result and returns new record. $variablePitchName and
$fixedPitchName used on this stage.
Options can include the following fields:
- resolution - vertical resolution. The default value is taken from
font resolution.
=item enum_family( $fontFamily)
Returns font names that are presented in given family
=back
=cut
|