Back to index

texmacs  1.0.7.15
charmap.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : charmap.cpp
00004 * DESCRIPTION: character maps for agglomerated fonts
00005 * COPYRIGHT  : (C) 2005  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 "charmap.hpp"
00013 #include "translator.hpp"
00014 #include "analyze.hpp"
00015 
00016 RESOURCE_CODE(charmap);
00017 
00018 /******************************************************************************
00019 * The charmap structure for finding the physical font
00020 ******************************************************************************/
00021 
00022 charmap_rep::charmap_rep (string name):
00023   rep<charmap> (name), slow_map (-1), slow_subst ("")
00024 {
00025   int ch; string r;
00026   for (int i=0; i<256; i++)
00027     if (((char) i) == '<' || ((char) i) == '>') fast_map[i]= -1;
00028     else {
00029       string s ((unsigned char) i);
00030       lookup (s, ch, r);
00031       fast_map[i]= (r == s? ch: -1);
00032     }
00033 }
00034 
00035 void
00036 charmap_rep::advance (string s, int& pos, string& r, int& ch) {
00037   int n= N(s), start= pos;
00038   if (s[pos] != '<') {
00039     ch= fast_map [(unsigned char) s[pos++]];
00040     while (pos<n && s[pos] != '<' &&
00041           fast_map [(unsigned char) s[pos]] == ch) pos++;
00042     r= s (start, pos);
00043     if (pos == n || s[pos] != '<') return;
00044   }
00045   else {
00046     int start= pos;
00047     tm_char_forwards (s, pos);
00048     cached_lookup (s (start, pos), ch, r);
00049   }
00050   int ch2; string r2;
00051   while (pos<n) {
00052     int start= pos;
00053     tm_char_forwards (s, pos);
00054     cached_lookup (s (start, pos), ch2, r2);
00055     if (ch2 != ch) { pos= start; break; }
00056     else r << r2;
00057   }
00058 }
00059 
00060 charmap
00061 any_charmap () {
00062   if (charmap::instances -> contains ("any"))
00063     return charmap ("any");
00064   return make (charmap, "any", tm_new<charmap_rep> ("any"));
00065 }
00066 
00067 /******************************************************************************
00068 * Explicit charmaps
00069 ******************************************************************************/
00070 
00071 struct ec_charmap_rep: public charmap_rep {
00072   ec_charmap_rep (): charmap_rep ("ec") {}
00073   void lookup (string s, int& ch, string& r) {
00074     if (N(s) == 1 || s == "<less>" || s == "<gtr>") { ch= 0; r= s; }
00075     else { ch= -1; r= ""; }
00076   }
00077 };
00078 
00079 charmap
00080 ec_charmap () {
00081   if (charmap::instances -> contains ("ec"))
00082     return charmap ("ec");
00083   return make (charmap, "ec", tm_new<ec_charmap_rep> ());
00084 }
00085 
00086 struct range_charmap_rep: public charmap_rep {
00087   int start, end;
00088   range_charmap_rep (int start2, int end2):
00089     charmap_rep (as_hexadecimal (start2) * "--" * as_hexadecimal (end2)),
00090     start (start2), end (end2) {}
00091   inline bool between (int what, int begin, int end) {
00092     return what >= begin && what <= end;
00093   }
00094   void lookup (string s, int& ch, string& r) {
00095     if (N(s) >= 3 && s[0] == '<' && s[1] == '#' && s[N(s)-1] == '>' &&
00096        between (from_hexadecimal (s (2, N(s)-1)), start, end)) { ch=0; r=s; }
00097     else { ch= -1; r= ""; }
00098   }
00099 };
00100 
00101 charmap
00102 range_charmap (int start, int end) {
00103   string name= as_hexadecimal (start) * "--" * as_hexadecimal (end);
00104   if (charmap::instances -> contains (name)) return charmap (name);
00105   return make (charmap, name, tm_new<range_charmap_rep> (start, end));
00106 }
00107 
00108 charmap
00109 la_charmap () {
00110   return range_charmap (0x400, 0x4ff);
00111 }
00112 
00113 charmap
00114 hangul_charmap () {
00115   return range_charmap (0xac00, 0xd7a3);
00116 }
00117 
00118 charmap
00119 oriental_charmap () {
00120   return range_charmap (0x3000, 0xffff);
00121 }
00122 
00123 struct explicit_charmap_rep: public charmap_rep {
00124   translator trl;
00125   explicit_charmap_rep (string name):
00126     charmap_rep (name), trl (load_translator (name)) {}
00127   void lookup (string s, int& ch, string& r) {
00128     if (trl->dict->contains (s)) { ch= 0; r= string ((char) trl->dict[s]); }
00129     else { ch= -1; r= ""; }
00130   }
00131 };
00132 
00133 charmap
00134 explicit_charmap (string name) {
00135   if (charmap::instances -> contains (name))
00136     return charmap (name);
00137   return make (charmap, name, tm_new<explicit_charmap_rep> (name));
00138 }
00139 
00140 /******************************************************************************
00141 * Joined charmaps
00142 ******************************************************************************/
00143  
00144 string
00145 join_name (charmap* a, int n) {
00146   string acc= copy (a[0]->res_name);
00147   for (int i=1; i<n; i++)
00148     acc << ":" << a[i]->res_name;
00149   return acc;
00150 }
00151 
00152 struct join_charmap_rep: public charmap_rep {
00153   charmap* ja;
00154   int      jn;
00155   join_charmap_rep (charmap* a, int n):
00156     charmap_rep (join_name (a, n)), ja (a), jn (n) {}
00157   int arity () {
00158     int i, sum= 0;
00159     for (i=0; i<jn; i++)
00160       sum += ja[i] -> arity ();
00161     return sum;
00162   }
00163   charmap child (int ch) {
00164     int i, sum= 0;
00165     for (i=0; i<jn; i++) {
00166       int p= ja[i] -> arity ();
00167       if (ch >= sum && ch < sum+p) return ja[i] -> child (i-sum);
00168       sum += p;
00169     }
00170     FAILED ("bad child");
00171     return this;
00172   }
00173   void lookup (string s, int& ch, string& r) {
00174     int i, sum= 0;
00175     for (i=0; i<jn; i++) {
00176       ja[i]->lookup (s, ch, r);
00177       //cout << i << "\t" << s << " -> " << ch << ", " << r << "\n";
00178       if (ch >= 0) {
00179        ch += sum;
00180        return;
00181       }
00182       sum += ja[i] -> arity ();
00183     }
00184     ch= -1; r= "";
00185   }  
00186 };
00187 
00188 charmap
00189 join_charmap (charmap* a, int n) {
00190   string name= join_name (a, n);
00191   if (charmap::instances -> contains (name))
00192     return charmap (name);
00193   return make (charmap, name, tm_new<join_charmap_rep> (a, n));
00194 }
00195 
00196 /******************************************************************************
00197 * High level interface
00198 ******************************************************************************/
00199 
00200 charmap
00201 load_charmap (tree def) {
00202   int i, n= N (def);
00203   charmap* a= tm_new_array<charmap> (n);
00204   for (i=0; i<n; i++) {
00205     //cout << i << "\t" << def[i] << "\n";
00206     if (def[i] == "any") a[i]= any_charmap ();
00207     else if (def[i] == "ec") a[i]= ec_charmap ();
00208     else if (def[i] == "hangul") a[i]= hangul_charmap ();
00209     else if (def[i] == "la") a[i]= la_charmap ();
00210     else if (def[i] == "oriental") a[i]= oriental_charmap ();
00211     else a[i]= explicit_charmap (as_string (def[i]));
00212   }
00213   return join_charmap (a, n);
00214 }