output: macho -- Add support for N_PEXT in macho output
[nasm.git] / doc / findfont.ph
blob60047b84b7dda7188da61ef6391dcd87a27f77ca
1 #!/usr/bin/perl
2 ## --------------------------------------------------------------------------
3 ##
4 ## Copyright 1996-2017 The NASM Authors - All Rights Reserved
5 ## See the file AUTHORS included with the NASM distribution for
6 ## the specific copyright holders.
7 ##
8 ## Redistribution and use in source and binary forms, with or without
9 ## modification, are permitted provided that the following
10 ## conditions are met:
12 ## * Redistributions of source code must retain the above copyright
13 ## notice, this list of conditions and the following disclaimer.
14 ## * Redistributions in binary form must reproduce the above
15 ## copyright notice, this list of conditions and the following
16 ## disclaimer in the documentation and/or other materials provided
17 ## with the distribution.
19 ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
20 ## CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
21 ## INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 ## DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 ## NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 ## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 ## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 ## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 ## OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31 ## EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ## --------------------------------------------------------------------------
36 # Try our best to find a specific PostScipt font in the system.
37 # We need to find the font files so we can extract the metrics.
38 # Sadly there isn't any reasonable Perl module to do this for us,
39 # as far as I can tell.
42 use strict;
43 use File::Spec;
44 use File::Find;
46 require 'afmmetrics.ph';
47 require 'ttfmetrics.ph';
49 my %font_info_hash = ();
50 my $fonts_scanned = 0;
51 my %prefs = { 'otf' => 1, 'ttf' => 2, 'pfa' => 3, 'pfb' => 4 };
53 sub add_file_to_font_hash($) {
54 my($filename) = @_;
56 return unless ( -f $filename );
57 return unless ( $filename =~ /^(.*)\.([[:alnum:]]+)$/ );
59 my $filestem = $1;
60 my $fonttype = $2;
61 my $fontdata;
63 if ( $filename =~ /\.(otf|ttf)$/i ) {
64 $fontdata = parse_ttf_file($filename);
65 } elsif ( $filename =~ /\.(pfa|pfb)$/i ) {
66 if ( -f "${filestem}.afm" ) {
67 $fontdata = parse_afm_file($filestem, $fonttype);
71 return unless (defined($fontdata));
73 my $oldinfo = $font_info_hash{$fontdata->{name}};
75 if (!defined($oldinfo) ||
76 $prefs{$fontdata->{type}} < $prefs{$oldinfo->{type}}) {
77 $font_info_hash{$fontdata->{name}} = $fontdata;
81 my $win32_ok = eval {
82 require Win32::TieRegistry;
83 Win32::TieRegistry->import();
87 # Based on Font::TTF::Win32 by
88 # Martin Hosken <http://scripts.sil.org/FontUtils>.
89 # LICENSING
91 # Copyright (c) 1998-2014, SIL International (http://www.sil.org)
93 # This module is released under the terms of the Artistic License 2.0.
94 # For details, see the full text of the license in the file LICENSE.
95 sub scanfonts_win32() {
96 return unless ($win32_ok);
98 my $Reg = $::Registry->Open('', {Access=>'KEY_READ', Delimiter=>'/'});
99 my $fd;
100 foreach my $win ('Windows NT', 'Windows') {
101 $fd = $Reg->{"HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/$win/CurrentVersion/Fonts"};
102 last if (defined($fd));
104 return unless (defined($fd));
106 foreach my $font (keys(%$fd)) {
107 my($fname, $ftype) = ($font =~ m:^/(.+?)(| \([^\(\)]+\))$:);
108 next unless ($ftype =~ / \((TrueType|OpenType)\)$/);
109 my $file = File::Spec->rel2abs($fd->{$font}, $ENV{'windir'}.'\\fonts');
110 add_file_to_font_hash($file);
114 sub font_search_file {
115 add_file_to_font_hash($_);
118 sub findfont($) {
119 my($fontname) = @_;
120 my $win32 = eval {
121 require Font::TTF::Win32;
122 Font::TTF::Win32->import();
125 my($file, $psname, $fontdata);
127 if (exists($font_info_hash{$fontname})) {
128 return $font_info_hash{$fontname};
131 # Are we on a system that uses fontconfig?
132 # NOTE: use a single string for the command here, or this
133 # script dies horribly on Windows, even though this isn't really
134 # applicable there...
135 if (open(my $fh, '-|',
136 "fc-match -f \"%{file}\\n%{postscriptname}\\n\" ".
137 "\" : postscriptname=$fontname\"")) {
138 chomp($file = <$fh>);
139 chomp($psname = <$fh>);
140 close($fh);
141 if ( -f $file ) {
142 if ($psname eq $fontname) {
143 add_file_to_font_hash($file);
145 if (!exists($font_info_hash{$fontname})) {
146 $font_info_hash{$fontname} = undef;
148 return $font_info_hash{$fontname};
152 if (exists($font_info_hash{$fontname})) {
153 return $font_info_hash{$fontname};
154 } elsif ($fonts_scanned >= 1) {
155 return $font_info_hash{$fontname} = undef;
158 scanfonts_win32();
159 $fonts_scanned = 1;
161 if (exists($font_info_hash{$fontname})) {
162 return $font_info_hash{$fontname};
163 } elsif ($fonts_scanned >= 2) {
164 return $font_info_hash{$fontname} = undef;
167 # Search a set of possible locations for a file, from a few different
168 # systems...
169 my @dirs = ('fonts', '/usr/share/fonts', '/usr/lib/fonts', '/Library/Fonts');
170 push @dirs, $ENV{'windir'}.'\\fonts' if (defined $ENV{'windir'});
171 push @dirs, $ENV{'HOME'}.'/.fonts', $ENV{'HOME'}.'/Library/Fonts'
172 if (defined $ENV{'HOME'});
174 find({wanted => \&font_search_file, follow=>1, no_chdir=>1}, @dirs);
175 $fonts_scanned = 2;
177 return $font_info_hash{$fontname};