Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ap/postscript-decodeglyphname

PostScript glyph name to Unicode conversion
https://github.com/ap/postscript-decodeglyphname

perl postscript

Last synced: 10 days ago
JSON representation

PostScript glyph name to Unicode conversion

Awesome Lists containing this project

README

        

use 5.008001; use strict; use warnings;

package PostScript::DecodeGlyphName;

our $VERSION = '0.002';

use Exporter::Tidy all => [qw( decode_glyph parse_adobeglyphlist )];

sub _croak { require Carp; goto &Carp::croak }

sub _utf8ify { no warnings 'utf8'; map { pack "U", hex } @_ }

my $uni_notation = qr{
\A uni
(
(?:
[0-9ABCEF] [\dA-F] {3}
|
D [0-7] [\dA-F] {2}
)+
)
\z
}x;

# this is a sexeger
my $u_notation = qr{
\A
(
[\dA-F] {2}
(?:
[0-7] D 0? 0?
|
[\dA-F] [\dABCEF] [\dA-F] {0,2}
)
)
u \z
}x;

my %agl;

sub decode_glyph {
my $digits;
return join '', map {
exists $agl{ $_ }
? $agl{ $_ }
: ( ( $digits ) = m/$uni_notation/ ) ? _utf8ify $digits =~ /(....)/g
: ( ( $digits ) = reverse =~ m/$u_notation/ ) ? _utf8ify scalar reverse $digits
: '';
}
map { split /_/ }
map { /\A(.+?)\./ ? $1 : $_ }
map { split } @_;
}

sub parse_adobeglyphlist {
my ( $input_method, $data ) = @_;

my %reader = (
array => sub { $_[0] },
data => sub { [ split /^/m, shift ] },
fh => sub { [ <$_[0]> ] },
file => sub {
open my $fh, '<', $_[0]
or _croak( "Error opening $_[0]: $!" );
[ <$fh> ];
},
);

_croak( "No such input type '$input_method'" )
unless exists $reader{ $input_method };

my $lines = $reader{ $input_method }->( $data );

@$lines = grep !/\A \s* (?: \# | \z)/x, @$lines;

chomp @$lines;

%agl = map {
my ( $code_pt, $glyph ) = split /;/;
( $glyph => _utf8ify $code_pt );
} @$lines;

delete $agl{ '.notdef' };

return 1;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

PostScript::DecodeGlyphName - PostScript glyph name to Unicode conversion

=head1 SYNOPSIS

use PostScript::GlyphToUnicode file => '/usr/doc/PostScript/aglfn13.txt';
print PostScript::GlyphToUnicode::map('Euro'), "\n";

=head1 DESCRIPTION

This module implements (most of) the PostScript glyph name to Unicode codepoint
conversion algorithm as described by Adobe at
L.

To do something more than marginally useful with this module you will need to
download the S from
L.

=head1 INTERFACE

=head2 parse_adobeglyphlist

This function parses an S file and returns true on success.
On failure, it returns false and supplies an error message in the package
variable C<$ERROR>. It expects its first argument to specify how to retrieve
the data. The following options exist:

=over 4

=item C

Takes the name of a file containing the S.

=item C

Takes a filehandle reference that should be open on a file containing the
S.

=item C

Takes an array reference. Each array element is expected to contain one line
from the S.

=item C

Takes a scalar that is expected to contain the entire S file.

=back

For convenience, you can pass the same parameters to the module's C
function, as exemplified in L"SYNOPSIS">. It will croak if it encounters any
errors.

=head2 C

This function takes a list of strings, each containing whitespace separated
PostScript glyphs, and returns them concatenated as a single character string.

(You may want to memoize this function when processing large PostScript
documents.)

=head1 LIMITATIONS

The C function does not take the font into account and therefore
will produce incorrect results for glyphs from the I font.

=cut