findfont.ph 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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:
  11. ##
  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.
  18. ##
  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.
  32. ##
  33. ## --------------------------------------------------------------------------
  34. #
  35. # Try our best to find a specific PostScipt font in the system.
  36. # We need to find the font files so we can extract the metrics.
  37. # Sadly there isn't any reasonable Perl module to do this for us,
  38. # as far as I can tell.
  39. #
  40. use strict;
  41. use File::Spec;
  42. use File::Find;
  43. require 'afmmetrics.ph';
  44. require 'ttfmetrics.ph';
  45. my %font_info_hash = ();
  46. my $fonts_scanned = 0;
  47. my %prefs = { 'otf' => 1, 'ttf' => 2, 'pfa' => 3, 'pfb' => 4 };
  48. sub add_file_to_font_hash($) {
  49. my($filename) = @_;
  50. return unless ( -f $filename );
  51. return unless ( $filename =~ /^(.*)\.([[:alnum:]]+)$/ );
  52. my $filestem = $1;
  53. my $fonttype = $2;
  54. my $fontdata;
  55. if ( $filename =~ /\.(otf|ttf)$/i ) {
  56. $fontdata = parse_ttf_file($filename);
  57. } elsif ( $filename =~ /\.(pfa|pfb)$/i ) {
  58. if ( -f "${filestem}.afm" ) {
  59. $fontdata = parse_afm_file($filestem, $fonttype);
  60. }
  61. }
  62. return unless (defined($fontdata));
  63. my $oldinfo = $font_info_hash{$fontdata->{name}};
  64. if (!defined($oldinfo) ||
  65. $prefs{$fontdata->{type}} < $prefs{$oldinfo->{type}}) {
  66. $font_info_hash{$fontdata->{name}} = $fontdata;
  67. }
  68. }
  69. my $win32_ok = eval {
  70. require Win32::TieRegistry;
  71. Win32::TieRegistry->import();
  72. 1;
  73. };
  74. # Based on Font::TTF::Win32 by
  75. # Martin Hosken <http://scripts.sil.org/FontUtils>.
  76. # LICENSING
  77. #
  78. # Copyright (c) 1998-2014, SIL International (http://www.sil.org)
  79. #
  80. # This module is released under the terms of the Artistic License 2.0.
  81. # For details, see the full text of the license in the file LICENSE.
  82. sub scanfonts_win32() {
  83. return unless ($win32_ok);
  84. my $Reg = $::Registry->Open('', {Access=>'KEY_READ', Delimiter=>'/'});
  85. my $fd;
  86. foreach my $win ('Windows NT', 'Windows') {
  87. $fd = $Reg->{"HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/$win/CurrentVersion/Fonts"};
  88. last if (defined($fd));
  89. }
  90. return unless (defined($fd));
  91. foreach my $font (keys(%$fd)) {
  92. my($fname, $ftype) = ($font =~ m:^/(.+?)(| \([^\(\)]+\))$:);
  93. next unless ($ftype =~ / \((TrueType|OpenType)\)$/);
  94. my $file = File::Spec->rel2abs($fd->{$font}, $ENV{'windir'}.'\\fonts');
  95. add_file_to_font_hash($file);
  96. }
  97. }
  98. sub font_search_file {
  99. add_file_to_font_hash($_);
  100. }
  101. sub findfont($) {
  102. my($fontname) = @_;
  103. my $win32 = eval {
  104. require Font::TTF::Win32;
  105. Font::TTF::Win32->import();
  106. 1;
  107. };
  108. my($file, $psname, $fontdata);
  109. if (exists($font_info_hash{$fontname})) {
  110. return $font_info_hash{$fontname};
  111. }
  112. # Are we on a system that uses fontconfig?
  113. # NOTE: use a single string for the command here, or this
  114. # script dies horribly on Windows, even though this isn't really
  115. # applicable there...
  116. if (open(my $fh, '-|',
  117. "fc-match -f \"%{file}\\n%{postscriptname}\\n\" ".
  118. "\" : postscriptname=$fontname\"")) {
  119. chomp($file = <$fh>);
  120. chomp($psname = <$fh>);
  121. close($fh);
  122. if ( -f $file ) {
  123. if ($psname eq $fontname) {
  124. add_file_to_font_hash($file);
  125. }
  126. if (!exists($font_info_hash{$fontname})) {
  127. $font_info_hash{$fontname} = undef;
  128. }
  129. return $font_info_hash{$fontname};
  130. }
  131. }
  132. if (exists($font_info_hash{$fontname})) {
  133. return $font_info_hash{$fontname};
  134. } elsif ($fonts_scanned >= 1) {
  135. return $font_info_hash{$fontname} = undef;
  136. }
  137. scanfonts_win32();
  138. $fonts_scanned = 1;
  139. if (exists($font_info_hash{$fontname})) {
  140. return $font_info_hash{$fontname};
  141. } elsif ($fonts_scanned >= 2) {
  142. return $font_info_hash{$fontname} = undef;
  143. }
  144. # Search a set of possible locations for a file, from a few different
  145. # systems...
  146. my @dirs = ('fonts', '/usr/share/fonts', '/usr/lib/fonts', '/Library/Fonts');
  147. push @dirs, $ENV{'windir'}.'\\fonts' if (defined $ENV{'windir'});
  148. push @dirs, $ENV{'HOME'}.'/.fonts', $ENV{'HOME'}.'/Library/Fonts'
  149. if (defined $ENV{'HOME'});
  150. find({wanted => \&font_search_file, follow=>1, no_chdir=>1}, @dirs);
  151. $fonts_scanned = 2;
  152. return $font_info_hash{$fontname};
  153. }
  154. 1;