Back to index

texmacs  1.0.7.15
tt_font.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : tt_font.cpp
00004 * DESCRIPTION: True Type fonts (using FreeType II)
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 "Freetype/free_type.hpp"
00014 #include "Freetype/tt_file.hpp"
00015 #include "Freetype/tt_face.hpp"
00016 
00017 #ifdef USE_FREETYPE
00018 
00019 /******************************************************************************
00020 * True Type fonts
00021 ******************************************************************************/
00022 
00023 struct tt_font_rep: font_rep {
00024   font_metric fnm;
00025   font_glyphs fng;
00026 
00027   tt_font_rep (string name, string family, int size, int dpi);
00028 
00029   void get_extents (string s, metric& ex);
00030   void get_xpositions (string s, SI* xpos);
00031   void draw (renderer ren, string s, SI x, SI y);
00032   glyph get_glyph (string s);
00033 };
00034 
00035 /******************************************************************************
00036 * Initialization of main font parameters
00037 ******************************************************************************/
00038 
00039 #define conv(x) ((SI) (((double) (x))*unit))
00040 
00041 tt_font_rep::tt_font_rep (string name, string family, int size2, int dpi):
00042   font_rep (name)
00043 {
00044   size= size2;
00045   fnm = tt_font_metric (family, size, dpi);
00046   fng = tt_font_glyphs (family, size, dpi);
00047   if (fnm->bad_font_metric || fng->bad_font_glyphs) {
00048     fnm= std_font_metric (res_name, NULL, 0, -1);
00049     fng= std_font_glyphs (res_name, NULL, 0, -1);
00050     if (DEBUG_AUTO)
00051       cout << "TeXmacs] Font " << family << " " << size
00052           << "pt at " << dpi << " dpi could not be loaded\n";
00053     
00054   }
00055 
00056   // get main font parameters
00057   metric ex;
00058   get_extents ("f", ex);
00059   y1= ex->y1;
00060   y2= ex->y2;
00061   display_size = y2-y1;
00062   design_size  = size << 8;
00063 
00064   // get character dimensions
00065   get_extents ("x", ex);
00066   yx           = ex->y2;
00067   get_extents ("M", ex);
00068   wquad        = ex->x2;
00069 
00070   // compute other heights
00071   yfrac        = yx >> 1;
00072   ysub_lo_base = -yx/3;
00073   ysub_hi_lim  = (5*yx)/6;
00074   ysup_lo_lim  = yx/2;
00075   ysup_lo_base = (5*yx)/6;
00076   ysup_hi_lim  = yx;
00077   yshift       = yx/6;
00078 
00079   // compute other widths
00080   wpt          = (dpi*PIXEL)/72;
00081   wfn          = (wpt*design_size) >> 8;
00082   wline        = wfn/20;
00083 
00084   // get fraction bar parameters
00085   get_extents ("-", ex);
00086   yfrac= (ex->y3 + ex->y4) >> 1;
00087 
00088   // get space length
00089   get_extents (" ", ex);
00090   spc  = space ((3*(ex->x2-ex->x1))>>2, ex->x2-ex->x1, (ex->x2-ex->x1)<<1);
00091   extra= spc;
00092   sep  = wfn/10;
00093 
00094   // get_italic space
00095   get_extents ("f", ex);
00096   SI italic_spc= (ex->x4-ex->x3)-(ex->x2-ex->x1);
00097   slope= ((double) italic_spc) / ((double) display_size);
00098   if (slope<0.15) slope= 0.0;
00099 }
00100 
00101 /******************************************************************************
00102 * Routines for font
00103 ******************************************************************************/
00104 
00105 void
00106 tt_font_rep::get_extents (string s, metric& ex) {
00107   if (N(s)==0) {
00108     ex->x1= ex->x3= ex->x2= ex->x4=0;
00109     ex->y3= ex->y1= 0; ex->y4= ex->y2= yx;
00110   }
00111   else {
00112     QN c= s[0];
00113     metric_struct* first= fnm->get (c);
00114     ex->x1= first->x1; ex->y1= first->y1;
00115     ex->x2= first->x2; ex->y2= first->y2;
00116     ex->x3= first->x3; ex->y3= first->y3;
00117     ex->x4= first->x4; ex->y4= first->y4;
00118     SI x= first->x2;
00119 
00120     int i;
00121     for (i=1; i<N(s); i++) {
00122       QN c= s[i];
00123       metric_struct* next= fnm->get (c);
00124       ex->x1= min (ex->x1, x+ next->x1); ex->y1= min (ex->y1, next->y1);
00125       ex->x2= max (ex->x2, x+ next->x2); ex->y2= max (ex->y2, next->y2);
00126       ex->x3= min (ex->x3, x+ next->x3); ex->y3= min (ex->y3, next->y3);
00127       ex->x4= max (ex->x4, x+ next->x4); ex->y4= max (ex->y4, next->y4);
00128       x += next->x2;
00129     }
00130   }
00131 }
00132 
00133 void
00134 tt_font_rep::get_xpositions (string s, SI* xpos) {
00135   register int i, n= N(s);
00136   if (n == 0) return;
00137   
00138   register SI x= 0;
00139   for (i=0; i<N(s); i++) {
00140     metric_struct* next= fnm->get ((QN) s[i]);
00141     x += next->x2;
00142     xpos[i+1]= x;
00143   }
00144 }
00145 
00146 void
00147 tt_font_rep::draw (renderer ren, string s, SI x, SI y) {
00148   if (N(s)!=0) {
00149     int i;
00150     for (i=0; i<N(s); i++) {
00151       QN c= s[i];
00152       ren->draw (c, fng, x, y);
00153       metric_struct* ex= fnm->get (c);
00154       x += ex->x2;
00155     }
00156   }
00157 }
00158 
00159 glyph
00160 tt_font_rep::get_glyph (string s) {
00161   if (N(s)!=1) return font_rep::get_glyph (s);
00162   int c= ((QN) s[0]);
00163   glyph gl= fng->get (c);
00164   if (is_nil (gl)) return font_rep::get_glyph (s);
00165   return gl;
00166 }
00167 
00168 /******************************************************************************
00169 * Interface
00170 ******************************************************************************/
00171 
00172 font
00173 tt_font (string family, int size, int dpi) {
00174   string name= "tt:" * family * as_string (size) * "@" * as_string(dpi);
00175   return make (font, name,
00176     tm_new<tt_font_rep> (name, family, size, dpi));
00177 }
00178 
00179 #else
00180 
00181 font
00182 tt_font (string family, int size, int dpi) {
00183   string name= "tt:" * family * as_string (size) * "@" * as_string(dpi);
00184   cerr << "\n\nFont name= " << name << "\n";
00185   FAILED ("true type support was disabled");
00186   return font ();
00187 }
00188 
00189 #endif