Back to index

texmacs  1.0.7.15
font.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : font.cpp
00004 * DESCRIPTION: fonts
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 "font.hpp"
00013 #include "gui.hpp"
00014 #include "Freetype/tt_file.hpp"
00015 
00016 RESOURCE_CODE(font);
00017 
00018 /******************************************************************************
00019 * Constructors for fonts
00020 ******************************************************************************/
00021 
00022 font_rep::font_rep (string s):
00023   rep<font> (s),
00024   type      (FONT_TYPE_TEX),
00025   spc       (0),
00026   extra     (0)
00027 {
00028 }
00029 
00030 font_rep::font_rep (string s, font fn):
00031   rep<font>    (s),
00032   type         (fn->type),
00033   size         (fn->size),
00034   design_size  (fn->design_size),
00035   display_size (fn->display_size),
00036   slope        (fn->slope),
00037   spc          (fn->spc),
00038   extra        (fn->extra),
00039   sep          (fn->sep)
00040 {
00041   copy_math_pars (fn);
00042 }
00043 
00044 void
00045 font_rep::copy_math_pars (font fn) {
00046   y1           = fn->y1;
00047   y2           = fn->y2;
00048   yx           = fn->yx;
00049   yfrac        = fn->yfrac;
00050   ysub_lo_base = fn->ysub_lo_base;
00051   ysub_hi_lim  = fn->ysub_hi_lim;
00052   ysup_lo_lim  = fn->ysup_lo_lim;
00053   ysup_lo_base = fn->ysup_lo_base;
00054   ysup_hi_lim  = fn->ysup_hi_lim;
00055   yshift       = fn->yshift;
00056   wpt          = fn->wpt;
00057   wfn          = fn->wfn;
00058   wline        = fn->wline;
00059   wquad        = fn->wquad;
00060 }
00061 
00062 double font_rep::get_left_slope  (string s) { (void) s; return slope; }
00063 double font_rep::get_right_slope (string s) { (void) s; return slope; }
00064 SI     font_rep::get_left_correction  (string s) { (void) s; return 0; }
00065 SI     font_rep::get_right_correction (string s) { (void) s; return 0; }
00066 
00067 void
00068 font_rep::get_xpositions (string s, SI* xpos) {
00069   int i= 0;
00070   SI  x= 0;
00071   metric ex;
00072   while (i < N(s)) {
00073     if (s[i] == '<')
00074       while ((i < N(s)) && (s[i] != '>')) {
00075        i++;
00076        xpos[i]= x;
00077       }
00078     i++;
00079     get_extents (s (0, i), ex);
00080     x= ex->x2;
00081     xpos[i]= x;
00082   }
00083 }
00084 
00085 void
00086 font_rep::var_get_extents (string s, metric& ex) {
00087   bool flag=true;
00088   int start=0, end;
00089   get_extents ("", ex);
00090   while (start<N(s)) {
00091     for (end=start; (end<N(s)) && (s[end]!=' '); end++) {}
00092     if (start<end) {
00093       metric ey;
00094       get_extents (s (start, end), ey);
00095       if (flag) {
00096        ex->x3= ey->x3+ ex->x2; ex->y3= ey->y3+ ex->x2;
00097        ex->x4= ey->x4; ex->y4= ey->y4;
00098        ex->x2 += ey->x2;
00099        flag= false;
00100       }
00101       else {
00102        ex->x3= min (ex->x3, ex->x2+ ey->x3);
00103        ex->x4= max (ex->x4, ex->x2+ ey->x4);
00104        ex->y3= min (ex->y3, ey->y3);
00105        ex->y4= max (ex->y4, ey->y4);
00106        ex->x2 += ey->x2;
00107       }
00108     }
00109     for (; (end<N(s)) && (s[end]==' '); end++) ex->x2 += spc->def;
00110     start= end;
00111   }
00112 }
00113 
00114 void
00115 font_rep::var_get_xpositions (string s, SI* xpos) {
00116   (void) s; (void) xpos;
00117   FAILED ("not yet implemented");
00118 }
00119 
00120 void
00121 font_rep::var_draw (renderer ren, string s, SI x, SI y) {
00122   SI dx=0;
00123   int start=0, end;
00124   while (start<N(s)) {
00125     for (end=start; (end<N(s)) && (s[end]!=' '); end++) {}
00126     if (start<end) {
00127       metric ex;
00128       draw (ren, s (start, end), x+dx, y);
00129       get_extents (s (start, end), ex);
00130       dx += ex->x2;
00131     }
00132     for (; (end<N(s)) && (s[end]==' '); end++) dx += spc->def;
00133     start= end;
00134   }
00135 }
00136 
00137 glyph
00138 font_rep::get_glyph (string s) {
00139   cerr << "glyph name: " << s << "\n";
00140   FAILED ("no bitmap available");
00141   return glyph ();
00142 }
00143 
00144 /******************************************************************************
00145 * Error font: used to draw unindentified characters
00146 ******************************************************************************/
00147 
00148 struct error_font_rep: font_rep {
00149   font fn;
00150   error_font_rep (string name, font fn);
00151   void get_extents (string s, metric& ex);
00152   void get_xpositions (string s, SI* xpos);
00153   void draw (renderer ren, string s, SI x, SI y);
00154 };
00155 
00156 error_font_rep::error_font_rep (string name, font fnb):
00157   font_rep (name, fnb), fn (fnb) {}
00158 
00159 void
00160 error_font_rep::get_extents (string s, metric& ex) {
00161   fn->get_extents (s, ex);
00162 }
00163 
00164 void
00165 error_font_rep::get_xpositions (string s, SI* xpos) {
00166   fn->get_xpositions (s, xpos);
00167 }
00168 
00169 void
00170 error_font_rep::draw (renderer ren, string s, SI x, SI y) {
00171   ren->set_color (red);
00172   fn->draw (ren, s, x, y);
00173 }
00174 
00175 font
00176 error_font (font fn) {
00177   string name= "error-" * fn->res_name;
00178   return make (font, name, tm_new<error_font_rep> (name, fn));
00179 }
00180 
00181 /******************************************************************************
00182 * Miscellaneous
00183 ******************************************************************************/
00184 
00185 int
00186 script (int sz, int level) {
00187   int i;
00188   if (level<0) level=0;
00189   if (level>2) level=2;
00190   for (i=0; i<level; i++) sz= (sz*2+2)/3;
00191   return sz;
00192 }
00193 
00194 string
00195 default_chinese_font_name () {
00196   if (tt_font_exists ("fireflysung")) return "fireflysung";
00197   if (tt_font_exists ("uming")) return "uming";
00198   if (tt_font_exists ("儷黑 Pro")) return "lihei";
00199   if (tt_font_exists ("华文细黑")) return "heiti";
00200   if (tt_font_exists ("simsun")) return "simsun";
00201   if (tt_font_exists ("SimSun")) return "apple-simsun";
00202   return "roman";
00203 }
00204 
00205 string
00206 default_japanese_font_name () {
00207   if (tt_font_exists ("ipam")) return "modern";
00208   if (tt_font_exists ("sazanami")) return "sazanami";
00209   if (tt_font_exists ("ttf-japanese-gothic")) return "ttf-japanese";
00210   if (tt_font_exists ("ヒラギノ明朝 ProN W6")) return "kaku";
00211   if (tt_font_exists ("MS PGothic")) return "ms-gothic";
00212   if (tt_font_exists ("MS PMincho")) return "ms-mincho";
00213   return "roman";  
00214 }
00215 
00216 string
00217 default_korean_font_name () {
00218   if (tt_font_exists ("unbatang")) return "modern";
00219   if (tt_font_exists ("UnBatang")) return "modern";
00220   if (tt_font_exists ("AppleGothic")) return "apple-gothic";
00221   if (tt_font_exists ("Gulim")) return "gulim";
00222   return "roman";
00223 }