LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1125|回复: 6

LaTeX 字体安装程序 (更好的选择)

[复制链接]
发表于 2005-10-29 23:36:26 | 显示全部楼层 |阅读模式
这些从SuSE 里面拷贝出来的脚本 (bash 和 perl ),是SuSEconfig 的一个组件。
为了让更多的人方便的使用LaTeX,所以贴在这里。而且:
希望有人能把这里面SuSE 特有的部分替换,扩展脚本通用性,完善脚本,从而让更多人受益。

组件1:bash 脚本 SuSEconfig.cjk-latex

  1. #!/bin/sh
  2. #
  3. # Copyright (c) 2002 SuSE Linux AG, Nuernberg, Germany.  All rights reserved.
  4. #
  5. # Author: Mike Fabian <mfabian@suse.de>, 2002
  6. #

  7. # check if we are started as root
  8. # only one of UID and USER must be set correctly

  9. if test "$UID" != 0 -a "$USER" != root; then
  10.     echo "You must be root to start $0."
  11.     exit 1
  12. fi

  13. test -n "$ROOT" && exit 0

  14. # call perl script to generate .tfm files and .pfb files:
  15. LC_ALL=POSIX /usr/sbin/cjk-latex-config

  16. for map in wadalab.map
  17. do
  18.     updmap --enable Map=$map
  19. done

复制代码



组件2:perl 脚本 cjk-latex-config

  1. #!/usr/bin/perl -w
  2. #
  3. # Copyright (c) 2002 SuSE Linux AG, Nuernberg, Germany.  All rights reserved.
  4. #
  5. # Author: Mike Fabian <mfabian@suse.de>, 2002
  6. #

  7. use English;
  8. use Getopt::Long;

  9. # check if we are started as root
  10. # only one of UID and USER must be set correctly

  11. if ($UID != 0 && $ENV{USER} !~ /root/) {
  12.     print "You must be root to start $0\n";
  13.     exit 1;
  14. }

  15. if (system ("rpm -q freetype-tools >/dev/null 2>&1") != 0) {
  16.   print "freetype-tools package missing, exiting.\n";
  17.   exit 1;
  18. }


  19. sub usage {
  20.   print "Usage: cjk-latex-config [--verbose|v] [--force|f] [--type1|t] [--smoothing|s]\n";
  21.   exit 1;
  22. }

  23. # Process command line options
  24. my %opt;
  25. unless (GetOptions(\%opt,
  26.                    'verbose|v', \$OPT_VERBOSE,
  27.                    'force|f',   \$OPT_FORCE,
  28.                    'type1|t',   \$OPT_TYPE1,
  29.                    'smoothing|s', \$OPT_SMOOTHING,
  30.                   )) {
  31.   &usage();
  32.   exit 1;
  33. }

  34. # to make sure ttf2tfm finds the .sdf files:
  35. system("texhash");

  36. $tfm_created = 0;
  37. $type1_created = 0;

  38. system("mkdir -p /usr/share/texmf/fonts/truetype/");

  39. open (TTFONTS_MAP, "/etc/ttf2pk/ttfonts.map");

  40. while (<TTFONTS_MAP>) {
  41.   
  42.   chomp($ARG);
  43.   
  44.   if ($ARG =~ /\@[a-zA-Z0-9\/]+\@/) {

  45.     if($OPT_VERBOSE) {
  46.       print "----------------------------------------------------------------------\n";
  47.       print "$ARG\n";
  48.     }
  49.    
  50.     @fields = split(/\s+/, $ARG);
  51.    
  52.     $tt_dir = "/usr/X11R6/lib/X11/fonts/truetype/";
  53.     $tt_basename = $fields[1];
  54.    
  55.     if ($fields[0] =~ /([^\s]+)\@[a-zA-Z0-9\/]+\@/) {
  56.       $latex_font_name = $1;
  57.     } else {
  58.       print "can't find latex font name.\n";
  59.       exit 1
  60.     }
  61.    
  62.     if ($fields[0] =~ /\@([a-zA-Z0-9\/]+)\@/) {
  63.       $sfd_name = $1;
  64.       $sfd_name =~ /.*\/([a-zA-Z0-9]+)/;
  65.       $sfd_basename = $1;
  66.     } else {
  67.       print "can't find sfd_name.\n";
  68.       exit 1
  69.     }

  70.     if ($ARG =~ /Pid=([0-9]+)/) {
  71.       $pid = "$1";
  72.     } else {
  73.       $pid = "3";
  74.     }

  75.     if ($ARG =~ /Eid=([0-9]+)/) {
  76.       $eid = "$1";
  77.     } else {
  78.       $eid = "1";
  79.     }

  80.     if ($ARG =~ /Slant=([0-9.]+)/) {
  81.       $slant = $1;
  82.       $slant_opt = "-s $1";
  83.     } else {
  84.       $slant = 0;
  85.       $slant_opt = "-s 0";
  86.     }

  87.     if ($ARG =~ /Rotate=(Yes)/) {
  88.       $rotate = 1;
  89.       $rotate_opt = "-x";
  90.     } else {
  91.       $rotate = 0;
  92.       $rotate_opt = "";
  93.     }

  94.     if (-e "$tt_dir/$tt_basename") {
  95.       symlink("$tt_dir/$tt_basename",
  96.               "/usr/share/texmf/fonts/truetype/$tt_basename");
  97.       $tfm_dir = "/usr/share/texmf/fonts/tfm/cjk-latex/";
  98.       $type1_dir = "/usr/share/texmf/fonts/type1/cjk-latex/";

  99.       if (0 != create_or_update_tfm ()) {
  100.         print "creating .tfm failed.\n";
  101.       }
  102.       if ($OPT_TYPE1 && $slant == 0 && $rotate == 0) {
  103.         if (0 != create_or_update_type1 ()) {
  104.           print "creating type1 font failed.\n";
  105.         }
  106.       }
  107.     }
  108.   }
  109. }

  110. if ($type1_created) {
  111.   $command = "cjk-latex-t1mapgen $type1_dir";
  112.   if (0 != system ($command)) {
  113.     print "$command failed.\n";
  114.     exit 1;
  115.   }
  116. }

  117. if ($tfm_created || $type1_created) {
  118.   system("texhash");
  119. }


  120. exit 0;


  121. ######################################################################

  122. sub create_or_update_tfm {
  123.   
  124.   if ($OPT_FORCE ||
  125.       mtime_differs_or_missing ("$tt_dir/$tt_basename",
  126.                                 "$tfm_dir/$sfd_basename/$latex_font_name/")) {
  127.       
  128.     if (0 != system ("mkdir -p $tfm_dir/$sfd_basename/$latex_font_name/")) {
  129.       print "mkdir -p $tfm_dir/$sfd_basename/$latex_font_name/ failed.\n";
  130.       exit 1;
  131.     }
  132.     if (! chdir ("$tfm_dir/$sfd_basename/$latex_font_name/")) {
  133.       print "can't chdir to $tfm_dir/$sfd_basename/$latex_font_name/\n";
  134.       exit 1;
  135.     }
  136.    
  137.     $command = "ttf2tfm $tt_dir/$tt_basename ";
  138.     unless ($OPT_VERBOSE) {
  139.       $command .= " -q";
  140.     }
  141.     $command .= " -P $pid -E $eid $rotate_opt $slant_opt $latex_font_name\@$sfd_name\@";
  142.     if ($OPT_VERBOSE) {
  143.       print "$command\n";
  144.     } else {
  145.       $command .= " > /dev/null 2>&1";
  146.       print "$latex_font_name\@$sfd_name\@: calling ttf2tfm ...\n";
  147.     }
  148.     if (0 != system($command)) {
  149.       print "$command failed.\n";
  150.       return 1;
  151.     }

  152.     # success, mark this by giving the created directory the same time stamp
  153.     # as the TT-font:
  154.     system("touch -r $tt_dir/$tt_basename $tfm_dir/$sfd_basename/$latex_font_name/");
  155.     $tfm_created = 1;
  156.     return 0;
  157.   }
  158.   
  159. }

  160. ######################################################################

  161. sub create_or_update_type1 {

  162.   if ($OPT_FORCE ||
  163.       mtime_differs_or_missing ("$tt_dir/$tt_basename",
  164.                                 "$type1_dir/$sfd_basename/$latex_font_name/")) {
  165.       
  166.     if (0 != system ("mkdir -p $type1_dir/$sfd_basename/$latex_font_name/")) {
  167.       print "mkdir -p $type1_dir/$sfd_basename/$latex_font_name/ failed.\n";
  168.       exit 1;
  169.     }
  170.     if (! chdir ("$type1_dir/$sfd_basename/$latex_font_name/")) {
  171.       print "can't chdir to $type1_dir/$sfd_basename/$latex_font_name/\n";
  172.       exit 1;
  173.     }

  174.     if (grep(/$tt_basename/,("wadalab-gothic.ttf","watanabe-mincho.ttf"))) {
  175.       print "$tt_basename does not work with ttf2pt1, skipping ...\n";
  176.       return 0;
  177.     }
  178.    
  179.     # disable smoothing of outlines by default, who knows how many
  180.     # broken fonts are out there (for details see 'man ttf2pt1'):
  181.     my $smoothing_opt = " -O s ";
  182.     # switch smoothing on only on special request:
  183.     if ($OPT_SMOOTHING) {
  184.       $smoothing_opt = " ";
  185.     }
  186.     # always force disabling of outline smoothing for known problematic fonts:
  187.     if (grep(/$tt_basename/,("kochi-gothic.ttf", "kochi-mincho.ttf",
  188.                              "kochi-gothic-subst.ttf", "kochi-mincho-subst.ttf"))) {
  189.       print "$tt_basename broken, disabling smoothing of outlines.\n";
  190.        $smoothing_opt = " -O s ";
  191.     }   
  192.    
  193.     $sfd_file = "/usr/share/texmf/ttf2tfm/$sfd_basename.sfd";
  194.     $map_file = `mktemp /tmp/cjk-latex-config-map.XXXXXX`;
  195.     chomp $map_file;
  196.     if ($map_file eq "") {
  197.       print "mktemp /tmp/cjk-latex-config-map.XXXXXX failed.\n";
  198.     }
  199.    
  200.     @planes = sfd2map($sfd_file,$map_file);
  201.     if ($#planes == -1) {
  202.       print "sfd2map($sfd_file,$map_file) failed.\n";
  203.       unlink $map_file;
  204.       return 1;
  205.     }
  206.    
  207.     for my $plane (@planes) {
  208.       if ($OPT_VERBOSE) {
  209.         $command = "ttf2pt1 -W 99 ";
  210.       } else {
  211.         $command = "ttf2pt1 -W 0 ";
  212.       }
  213.       $command .= $smoothing_opt;
  214.       $command .= " -p ft -b -G a -m h=5000 ";
  215.       $command .= " -L $map_file+pid=$pid,eid=$eid,$plane ";
  216.       $command .= " $tt_dir/$tt_basename $latex_font_name$plane";
  217.       if ($OPT_VERBOSE) {
  218.         print "$command\n";
  219.       } else {
  220.         $command .= " > /dev/null 2>&1";
  221.         print "$latex_font_name\@$sfd_name\@, plane=$plane: calling ttf2pt1 ...\n";
  222.       }
  223.       if (0 != system($command)) {
  224.         print "$command failed.\n";
  225.         unlink $map_file;
  226.         return 1;
  227.       }      
  228.     }

  229.     unlink $map_file;
  230.    
  231.     # success, mark this by giving the created directory the same time stamp
  232.     # as the TT-font:
  233.     system("touch -r $tt_dir/$tt_basename $type1_dir/$sfd_basename/$latex_font_name/");
  234.     $type1_created = 1;
  235.     return 0;
  236.   }
  237. }

  238. ######################################################################

  239. sub sfd2map {
  240.   my($sfd_file,$map_file) = @_;
  241.   if (! open (SFD, "<$sfd_file")) {
  242.     print "cannot open $sfd_file\n";
  243.     return ();
  244.   }
  245.   if (! open (MAP, ">$map_file")) {
  246.     print "cannot open $map_file\n";
  247.     close (SFD);
  248.     return ();
  249.   }

  250.   my(@planes) = ();

  251.   while (<SFD>) {
  252.   
  253.     if ( ! ($ARG =~ /^[[:space:]]*\#/)) {  # skip comment lines

  254.       # handle plane numbers:
  255.       if ( $ARG =~ /^([[:xdigit:]]{2})[[:space:]]*/ ) {
  256.         $ARG =~ s/^([[:xdigit:]]{2})[[:space:]]*/   /;
  257.         print MAP "plane $1\n";
  258.         print MAP "at 0x00\n";
  259.         $planes[$#planes + 1] = $1;
  260.       }
  261.    
  262.       # remove continuation chars '\':
  263.       $ARG =~ s/\\$//;
  264.    
  265.       $ARG =~ s/(0x[[:xdigit:]]{1,4})/$1,/g;
  266.       # handle ranges like 0xF800_0xF8FF
  267.       $ARG =~ s/(0x[[:xdigit:]]{1,4}),_/$1-/g;
  268.     }

  269.     print MAP $ARG;

  270.   }

  271.   close (MAP);
  272.   close (SFD);
  273.   return @planes;
  274. }


  275. # Returns true if the modification time of $f1 differs from
  276. # the modification time of $f2
  277. sub mtime_differs {
  278.   my($f1,$f2) = @_;
  279.   if( -e $f1 && -e $f2) {
  280.     local (@f1s) = stat ($f1);
  281.     local (@f2s) = stat ($f2);
  282.     return ($f1s[9] != $f2s[9]);
  283.   } else {
  284.     return 0;
  285.   }
  286. }

  287. # Returns true if the modification time of $f1 differs from
  288. # the modification time of $f2 or if one of the files is missing
  289. sub mtime_differs_or_missing {
  290.     my($f1,$f2) = @_;
  291.     if (! -e $f1 || ! -e $f2 || mtime_differs($f1,$f2)) {
  292.       return 1;
  293.     } else {
  294.       return 0;
  295.     }
  296. }  

  297. # Returns true if $f1 is newer than $f2
  298. sub newer {
  299.   my($f1,$f2) = @_;
  300.   if( -e $f1 && -e $f2) {
  301.     local (@f1s) = stat ($f1);
  302.     local (@f2s) = stat ($f2);
  303.     return ($f1s[9] > $f2s[9]);
  304.   } else {
  305.     return 0;
  306.   }
  307. }

  308. # Returns true if $f1 is newer than $f2 or if one of the files is missing
  309. sub newer_or_missing {
  310.     my($f1,$f2) = @_;
  311.     if (! -e $f1 || ! -e $f2 || newer($f1,$f2)) {
  312.       return 1;
  313.     } else {
  314.       return 0;
  315.     }
  316. }
复制代码



组件3:bash  脚本 cjk-latex-t1mapgen

  1. #!/bin/sh
  2. #
  3. # Copyright (c) 2002, 2003 SuSE Linux AG, Nuernberg, Germany.  All rights reserved.
  4. #
  5. # Author: Mike Fabian <mfabian@suse.de>, 2002
  6. #

  7. TYPE1_DIR=$1
  8. if [ -z $TYPE1_DIR ] ; then
  9.     TYPE1_DIR=/usr/share/texmf/fonts/type1/cjk-latex/
  10. fi

  11. CJK_LATEX_TYPE1_MAP_DIR=/usr/share/texmf/fonts/map/dvips/cjk-latex
  12. mkdir -p $CJK_LATEX_TYPE1_MAP_DIR
  13. CJK_LATEX_TYPE1_MAP_FILE=${CJK_LATEX_TYPE1_MAP_DIR}/cjk-latex.map

  14. echo "creating $CJK_LATEX_TYPE1_MAP_FILE ..."

  15. TMPFILE=`mktemp /tmp/cjk-latex-t1mapgen.XXXXXX` || exit 1

  16. if [ -d $TYPE1_DIR ] ; then
  17.     for FILE in $( find $TYPE1_DIR -name "*.pfb" )
  18.     do
  19.         BASENAME_WITHOUT_EXT=$( basename $FILE )
  20.         BASENAME_WITHOUT_EXT=${BASENAME_WITHOUT_EXT%.pfb}
  21.         FONT_NAME=$( awk '/\/FontName/ { print substr($2,2); exit }' ${FILE} )
  22.         echo "${BASENAME_WITHOUT_EXT} ${FONT_NAME} <${BASENAME_WITHOUT_EXT}.pfb" >> $TMPFILE
  23.     done
  24. fi

  25. mv $TMPFILE $CJK_LATEX_TYPE1_MAP_FILE
  26. chmod 644 $CJK_LATEX_TYPE1_MAP_FILE

  27. texhash
  28. for map in cjk-latex.map
  29. do
  30.     updmap --enable Map=$map
  31. done

  32. # pdflatex seems to prefer pk fonts if they exist. That seems strange
  33. # but I couldn't find out how to change this.
  34. # Deleting all the pk fonts from /var/cache/fonts/pk/ is probably a bit overkill
  35. # but it helps. 'dvips' will regenerate the pk fonts as needed but will not
  36. # regenerate pk fonts for the pfb fonts listed in the map file generated above.
  37. # Therefore, deleting /var/cache/fonts/pk/* makes sure that all available pfb
  38. # fonts are used:

  39. find /var/cache/fonts/pk/ -type f -print0 | xargs -r -l100 -0 -- /usr/bin/safe-rm
复制代码



组件4:perl 脚本 sfd2map

  1. #!/usr/bin/perl -w
  2. #
  3. # Copyright (c) 2002 Mike Fabian <mike.fabian@gmx.de>
  4. #
  5. # This program is free software; you can redistribute it and/or modify it
  6. # under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2, or (at your option)
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. # General Public License for more details.

  14. use English;
  15. use Getopt::Long;

  16. sub usage {
  17.   print "Usage: sfd2map [-debug|d]\n\n";
  18.   print "Converts a .sfd files in the format used by ttf2pk and ttf2tfm\n";
  19.   print "into .map files in the format expected by ttf2pt1. For example:\n\n";
  20.   print "    sfd2map < UJIS.sfd > UJIS.map \n\n";
  21.   exit 1;
  22. }

  23. # Process command line options
  24. my %opt;
  25. unless (GetOptions(\%opt,
  26.         '-debug|d' , \$OPT_DEBUG
  27.        )) {
  28.     &usage();
  29.     exit 1;
  30. }

  31. if($OPT_DEBUG) {
  32. }

  33. open (MAP, ">&STDOUT");
  34. open (SFD, "<&STDIN");

  35. print MAP "# generated from a .sfd file by sfd2map to make it usable with ttf2pt1\n";
  36. print MAP "#\n";

  37. while (<SFD>) {
  38.   
  39.   if ( ! ($ARG =~ /^[[:space:]]*\#/)) { # skip comment lines

  40.     # handle plane numbers:
  41.     if ( $ARG =~ /^([[:xdigit:]]{2})[[:space:]]*/ ) {
  42.       $ARG =~ s/^([[:xdigit:]]{2})[[:space:]]*/   /;
  43.       print MAP "plane $1\n";
  44.       print MAP "at 0x00\n";
  45.     }
  46.    
  47.     # remove continuation chars '\':
  48.     $ARG =~ s/\\$//;

  49.     $ARG =~ s/(0x[[:xdigit:]]{1,4})/$1,/g;
  50.     # handle ranges like 0xF800_0xF8FF
  51.     $ARG =~ s/(0x[[:xdigit:]]{1,4}),_/$1-/g;
  52.    
  53.   }

  54.   print MAP $ARG;

  55. }


复制代码




注释和说明
1 /usr/X11R6/lib/X11/fonts/truetype/ 是SuSE 的 ttf 字体的目录,一般的发行版是/usr/share/fonts/truetype 。
2 这些脚本的大致作用是把一个ttf 字体仍到/usr/X11R6/lib/X11/fonts/truetype/, LaTeX 就也可以使用了。
发表于 2005-10-30 01:04:59 | 显示全部楼层
赞一下 SUSE

CJK-latex 是他们做的

流行的 Uming 上海宋字体 也是 德国人做的
回复 支持 反对

使用道具 举报

发表于 2005-10-30 21:49:15 | 显示全部楼层
不懂perl
回复 支持 反对

使用道具 举报

发表于 2005-10-31 14:00:04 | 显示全部楼层
Post by jhuangjiahua

流行的Uming 上海宋字体也是 德国人做的


没看出来是德国人做的,那些名字很象是中国人的。
回复 支持 反对

使用道具 举报

发表于 2005-10-31 20:13:48 | 显示全部楼层
上海宋字体的作者是  Arne Goetje <arne (at)  linux.org.tw>

是个现在居住在我国台湾的德国骇客

还会说点客家话
回复 支持 反对

使用道具 举报

发表于 2005-10-31 20:45:03 | 显示全部楼层
Post by jhuangjiahua
上海宋字体的作者是  Arne Goetje <arne _A__T_ linux.org.tw>

是个现在居住在我国台湾的德国骇客

还会说点客家话


jhuangjiahua 把邮件地址修改一下吧
回复 支持 反对

使用道具 举报

发表于 2005-11-1 00:55:07 | 显示全部楼层
Post by jhuangjiahua
上海宋字体的作者是  Arne Goetje <arne@linux.org.tw>

是个现在居住在我国台湾的德国骇客

还会说点客家话


这么严重!
难怪上面看到有haka的输入法。
服了。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表