Back to index

texmacs  1.0.7.15
tex_files.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : tex_files.cpp
00004 * DESCRIPTION: manipulation of TeX font files
00005 * COPYRIGHT  : (C) 1999  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "tex_files.hpp"
00013 #include "boot.hpp"
00014 #include "file.hpp"
00015 #include "sys_utils.hpp"
00016 #include "path.hpp"
00017 #include "hashmap.hpp"
00018 #include "analyze.hpp"
00019 #include "timer.hpp"
00020 #include "data_cache.hpp"
00021 
00022 static url the_tfm_path= url_none ();
00023 static url the_pk_path = url_none ();
00024 static url the_pfb_path= url_none ();
00025 
00026 /******************************************************************************
00027 * Finding a TeX font
00028 ******************************************************************************/
00029 
00030 static string
00031 kpsewhich (string name) {
00032   bench_start ("kpsewhich");
00033   string which= var_eval_system ("kpsewhich " * name);
00034   bench_cumul ("kpsewhich");
00035   return which;
00036 }
00037 
00038 static url
00039 resolve_tfm (url name) {
00040   if (get_setting ("KPSEWHICH") == "true") {
00041     string which= kpsewhich (as_string (name));
00042     if ((which!="") && exists (url_system (which))) return url_system (which);
00043     // cout << "Missed " << name << "\n";
00044   }
00045   return resolve (the_tfm_path * name);
00046 }
00047 
00048 static url
00049 resolve_pk (url name) {
00050 #ifndef OS_WIN32 // The kpsewhich from MikTeX is bugged for pk fonts
00051   if (get_setting ("KPSEWHICH") == "true") {
00052     string which= kpsewhich (as_string (name));
00053     if ((which!="") && exists (url_system (which))) return url_system (which);
00054     // cout << "Missed " << name << "\n";
00055   }
00056 #endif
00057   return resolve (the_pk_path * name);
00058 }
00059 
00060 static url
00061 resolve_pfb (url name) {
00062 #ifndef OS_WIN32 // The kpsewhich from MikTeX is bugged for pfb fonts
00063   if (get_setting ("KPSEWHICH") == "true") {
00064     string which= kpsewhich (as_string (name));
00065     if ((which!="") && exists (url_system (which))) return url_system (which);
00066     // cout << "Missed " << name << "\n";
00067   }
00068 #endif
00069   return resolve (the_pfb_path * name);
00070 }
00071 
00072 /******************************************************************************
00073 * Caching results
00074 ******************************************************************************/
00075 
00076 url
00077 resolve_tex (url name) {
00078   string s= as_string (name);
00079   if (is_cached ("font_cache.scm", s)) {
00080     url u= url_system (cache_get ("font_cache.scm", s) -> label);
00081     if (exists (u)) return u;
00082     cache_reset ("font_cache.scm", s);
00083   }
00084 
00085   bench_start ("resolve tex");
00086   url u= url_none ();
00087   if (ends (s, "mf" )) {
00088     u= resolve_tfm (name);
00089 #ifdef OS_WIN32
00090     if (is_none (u))
00091       u= resolve_tfm (replace (s, ".mf", ".tfm"));
00092 #endif
00093   }
00094   if (ends (s, "tfm")) u= resolve_tfm (name);
00095   if (ends (s, "pk" )) u= resolve_pk  (name);
00096   if (ends (s, "pfb")) u= resolve_pfb (name);
00097   bench_cumul ("resolve tex");
00098 
00099   if (!is_none (u)) cache_set ("font_cache.scm", s, as_string (u));
00100   //cout << "Resolve " << name << " -> " << u << "\n";
00101   return u;
00102 }
00103 
00104 bool
00105 exists_in_tex (url u) {
00106   return !is_none (resolve_tex (u));
00107 }
00108 
00109 /******************************************************************************
00110 * Automatically generate missing fonts
00111 ******************************************************************************/
00112 
00113 void
00114 make_tex_tfm (string name) {
00115   string s;
00116   if (get_setting ("MAKETFM") == "MakeTeXTFM") {
00117     s= "MakeTeXTFM " * name;
00118     if (DEBUG_VERBOSE) cout << "TeXmacs] Executing " << s << "\n";
00119     system (s);
00120   }
00121   if (get_setting ("MAKETFM") == "mktextfm") {
00122     url tfm_dir ("$TEXMACS_HOME_PATH/fonts/tfm");
00123     s= "mktextfm " *
00124       string ("--destdir ") * as_string (tfm_dir) * " " *
00125       name;
00126     if (DEBUG_VERBOSE) cout << "TeXmacs] Executing " << s << "\n";
00127     system (s);
00128     string superfluous= name * ".600pk";
00129     if (ends (name, ".tfm")) superfluous= name (0, N(name)-4) * ".600pk";
00130     remove (tfm_dir * superfluous);
00131   }
00132   if (get_setting ("MAKETFM") == "maketfm"){
00133     if (name(N(name) - 4, N(name)) == ".tfm")
00134       name = name (0, N(name) - 4);
00135     s = "maketfm --dest-dir \"" * get_env("$TEXMACS_HOME_PATH")
00136       * "\\fonts\\tfm\" " * name;
00137     if (DEBUG_VERBOSE) cout << "TeXmacs] Executing " << s << "\n";
00138     system (s);
00139   }
00140 }
00141 
00142 void
00143 make_tex_pk (string name, int dpi, int design_dpi) {
00144   string s;
00145   if (get_setting ("MAKEPK") == "MakeTeXPK") {
00146     s="MakeTeXPK " * name * " " *
00147       as_string (dpi) * " " * as_string (design_dpi) * " " *
00148       as_string (dpi) * "/" * as_string (design_dpi) * " localfont";
00149     if (DEBUG_VERBOSE) cout << "TeXmacs] Executing " << s << "\n";
00150     system (s);
00151   }
00152   if (get_setting ("MAKEPK") == "mktexpk") {
00153     url pk_dir ("$TEXMACS_HOME_PATH/fonts/pk");
00154     s="mktexpk " *
00155       string ("--dpi ") * as_string (dpi) * " " *
00156       string ("--bdpi ") * as_string (design_dpi) * " " *
00157       string ("--mag ") * as_string (dpi) *"/"* as_string (design_dpi) * " " *
00158       string ("--destdir ") * as_string (pk_dir) * " " *
00159       name;
00160     if (DEBUG_VERBOSE) cout << "TeXmacs] Executing " << s << "\n";
00161     system (s);
00162   }
00163   if (get_setting ("MAKEPK") == "makepk") {
00164 #ifdef OS_WIN32
00165     s = "makepk --dest-dir \""
00166       * get_env("$TEXMACS_HOME_PATH") * "\\fonts\\pk\" "
00167       * name * " " * as_string(dpi) * " " * as_string(design_dpi)
00168       * " " * as_string(dpi) * "%//" * as_string(design_dpi);
00169 #else
00170     s = "makepk --dest-dir \""
00171       * get_env("$TEXMACS_HOME_PATH") * "\\fonts\\pk\" "
00172       * name * " " * as_string(dpi) * " " * as_string(design_dpi)
00173       * " " * as_string(dpi) * "/" * as_string(design_dpi);
00174 #endif
00175     if (DEBUG_VERBOSE) cout << "TeXmacs] Executing " << s << "\n";
00176     system (s);
00177   }
00178 }
00179 
00180 /******************************************************************************
00181 * Automatic determination of paths where TeX fonts might have been generated
00182 ******************************************************************************/
00183 
00184 static url
00185 get_kpsepath (string s) {
00186   // FIXME: adapt to WIN32
00187   if (get_setting ("KPSEPATH") != "true") return url_none ();
00188   string r= var_eval_system ("kpsepath " * s);
00189   if (N(r)==0) return url_none ();
00190 
00191   int i, start, end;
00192   url p= url_none ();
00193   for (i=0; i<N(r); i++) {
00194     while ((i<N(r)) && (r[i]=='!')) i++;
00195     start=i;
00196     while ((i<N(r)) && (r[i]!=':')) i++;
00197     end=i;
00198     while ((end>start) && (r[end-1]=='/')) end--;
00199     string dir= r (start, end);
00200     if (dir == ".") continue;
00201     p= expand (complete (dir * url_wildcard (), "dr")) | p;
00202   }
00203   return p;
00204 }
00205 
00206 static url
00207 search_sub_dirs (url root) {
00208   url dirs= complete (root * url_wildcard (), "dr");
00209   return expand (dirs);
00210 }
00211 
00212 void
00213 reset_tfm_path (bool rehash) { (void) rehash;
00214   // if (rehash && (get_setting ("TEXHASH") == "true")) system ("texhash");
00215   string tfm= get_setting ("TFM");
00216   the_tfm_path=
00217     url_here () |
00218     search_sub_dirs ("$TEXMACS_HOME_PATH/fonts/tfm") |
00219     search_sub_dirs ("$TEXMACS_PATH/fonts/tfm") |
00220     "$TEX_TFM_PATH" |
00221     (tfm == ""? url_none (): tfm);
00222   if ((get_setting ("MAKETFM") != "false") ||
00223       (get_setting ("TEXHASH") == "true"))
00224     if (get_setting ("KPSEWHICH") != "true")
00225       the_tfm_path= the_tfm_path | get_kpsepath ("tfm");
00226   the_tfm_path= expand (factor (the_tfm_path));
00227 }
00228 
00229 void
00230 reset_pk_path (bool rehash) { (void) rehash;
00231   // if (rehash && (get_setting ("TEXHASH") == "true")) system ("texhash");
00232   string pk= get_setting ("PK");
00233   the_pk_path=
00234     url_here () |
00235     search_sub_dirs ("$TEXMACS_HOME_PATH/fonts/pk") |
00236     search_sub_dirs ("$TEXMACS_PATH/fonts/pk") |
00237     "$TEX_PK_PATH" |
00238     (pk == ""? url_none (): pk);
00239   if ((get_setting ("MAKEPK") != "false") ||
00240       (get_setting ("TEXHASH") == "true"))
00241     if (get_setting ("KPSEWHICH") != "true")
00242       the_pk_path= the_pk_path | get_kpsepath ("pk");
00243   the_pk_path= expand (factor (the_pk_path));
00244 }
00245 
00246 void
00247 reset_pfb_path () {
00248   string pfb= get_setting ("PFB");
00249   the_pfb_path=
00250     url_here () |
00251     search_sub_dirs ("$TEXMACS_HOME_PATH/fonts/type1") |
00252     search_sub_dirs ("$TEXMACS_PATH/fonts/type1") |
00253     "$TEX_PFB_PATH" |
00254     (pfb == ""? url_none (): url_system (pfb));
00255   the_pfb_path= expand (factor (the_pfb_path));
00256 }