]> git.donarmstrong.com Git - lilypond.git/blob - lily/pango-font.cc
* lily/ttf.cc (print_trailer): if all else fails: use
[lilypond.git] / lily / pango-font.cc
1 /*
2   pango-font.cc -- implement Pango_font
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #define PANGO_ENABLE_BACKEND // ugh, why necessary?
10 #include <pango/pangoft2.h>
11
12 /* Ugh.  */
13 #include "pango-font.hh"
14
15 #include "dimensions.hh"
16 #include "file-name.hh"
17 #include "international.hh"
18 #include "lookup.hh"
19 #include "main.hh"
20 #include "string-convert.hh"
21 #include "warn.hh"
22
23 #if HAVE_PANGO_FT2
24 #include "stencil.hh"
25
26 Pango_font::Pango_font (PangoFT2FontMap *fontmap,
27                         PangoFontDescription *description,
28                         Real output_scale)
29 {
30   (void) fontmap;
31   physical_font_tab_ = scm_c_make_hash_table (11);
32   PangoDirection pango_dir = PANGO_DIRECTION_LTR;
33   context_
34     = pango_ft2_get_context (PANGO_RESOLUTION, PANGO_RESOLUTION);
35   //  context_ = pango_ft2_font_map_create_context (fontmap);
36
37   pango_description_ = pango_font_description_copy (description);
38   attribute_list_ = pango_attr_list_new ();
39
40   /*
41     urgh. I don't understand this. Why isn't this 1/(scale *
42     resolution * output_scale)
43
44     --hwn
45   */
46   output_scale_ = output_scale;
47   scale_ = INCH_TO_BP / (Real (PANGO_SCALE) * Real (PANGO_RESOLUTION) * output_scale);
48
49   /*
50     ugh. Should make this configurable.
51   */
52   pango_context_set_language (context_, pango_language_from_string ("en_US"));
53   pango_context_set_base_dir (context_, pango_dir);
54   pango_context_set_font_description (context_, description);
55 }
56
57 Pango_font::~Pango_font ()
58 {
59   pango_font_description_free (pango_description_);
60   g_object_unref (context_);
61   pango_attr_list_unref (attribute_list_);
62 }
63
64 void
65 Pango_font::register_font_file (string filename, string ps_name)
66 {
67   scm_hash_set_x (physical_font_tab_,
68                   scm_makfrom0str (ps_name.c_str ()),
69                   scm_makfrom0str (filename.c_str ()));
70 }
71
72 void
73 Pango_font::derived_mark () const
74 {
75   scm_gc_mark (physical_font_tab_);
76 }
77
78
79 map<string, Index_to_charcode_map > filename_charcode_maps_map;
80 Index_to_charcode_map const *get_index_to_charcode_map (string postscript_name, FT_Face face);
81
82
83 Index_to_charcode_map const *
84 get_index_to_charcode_map (string filename, FT_Face face)
85 {
86   if (filename_charcode_maps_map.find (filename) == filename_charcode_maps_map.end ())
87     filename_charcode_maps_map[filename] = make_index_to_charcode_map (face);
88
89   if (filename_charcode_maps_map.find (filename) == filename_charcode_maps_map.end ())
90     return 0;
91   
92   return &filename_charcode_maps_map[filename];
93 }
94
95 void
96 get_glyph_index_name (char *s, FT_ULong code)
97 {
98   sprintf (s, "glyphIndex%lX", code);
99 }
100
101 void
102 get_unicode_name (char*s, FT_ULong code)
103 {
104   if (code > 0xFFFF)
105     sprintf (s,  "u%lX", code);
106   else
107     sprintf (s,  "uni%04lX", code);
108 }
109
110
111 Stencil
112 Pango_font::pango_item_string_stencil (PangoItem const *item, string str,
113                                        bool tight_bbox) const
114 {
115   const int GLYPH_NAME_LEN = 256;
116   char glyph_name[GLYPH_NAME_LEN];
117   PangoAnalysis const *pa = &(item->analysis);
118   PangoGlyphString *pgs = pango_glyph_string_new ();
119
120   pango_shape (str.c_str () + item->offset,
121                item->length, (PangoAnalysis*) pa, pgs);
122
123   PangoRectangle logical_rect;
124   PangoRectangle ink_rect;
125   pango_glyph_string_extents (pgs, pa->font, &ink_rect, &logical_rect);
126
127   PangoFcFont *fcfont = G_TYPE_CHECK_INSTANCE_CAST (pa->font,
128                                                     PANGO_TYPE_FC_FONT,
129                                                     PangoFcFont);
130
131   FT_Face ftface = pango_fc_font_lock_face (fcfont);
132
133   PangoRectangle const *which_rect
134     = (tight_bbox)
135     ? &ink_rect
136     : &logical_rect;
137     
138   Box b (Interval (PANGO_LBEARING (logical_rect),
139                    PANGO_RBEARING (logical_rect)),
140          Interval (-PANGO_DESCENT (*which_rect),
141                    PANGO_ASCENT (*which_rect)));
142
143   b.scale (scale_);
144   char const *ps_name_str0 = FT_Get_Postscript_Name (ftface);
145   FcPattern *fcpat = fcfont->font_pattern;
146   FcChar8 *file_name_as_ptr = 0;
147   FcPatternGetString (fcpat, FC_FILE, 0, &file_name_as_ptr);
148
149   string file_name;
150   if (file_name_as_ptr)
151     {
152       /* Normalize file name.  */
153       file_name = File_name ((char const *)file_name_as_ptr).to_string ();
154     }
155   
156   SCM glyph_exprs = SCM_EOL;
157   SCM *tail = &glyph_exprs;
158       
159   Index_to_charcode_map const *cmap = 0;
160   bool has_glyph_names = ftface->face_flags & FT_FACE_FLAG_GLYPH_NAMES;
161   if  (! has_glyph_names)
162     cmap = get_index_to_charcode_map (file_name, ftface);
163   bool is_ttf = (file_name.find (".ttf") != NPOS
164                  || file_name.find (".TTF") != NPOS);
165   bool cid_keyed = false;
166   for (int i = 0; i < pgs->num_glyphs; i++)
167     {
168       PangoGlyphInfo *pgi = pgs->glyphs + i;
169
170       PangoGlyph pg = pgi->glyph;
171       PangoGlyphGeometry ggeo = pgi->geometry;
172
173       glyph_name[0] = '\0';
174       if (has_glyph_names)
175         {
176           int errorcode = FT_Get_Glyph_Name (ftface, pg, glyph_name, GLYPH_NAME_LEN);
177           if (errorcode)
178             programming_error ("FT_Get_Glyph_Name returns error");
179         }
180       
181       SCM char_id = SCM_EOL;
182       if (glyph_name[0] == '\0'
183           && cmap
184
185           /* Ugh should ask FreeType about font type. */
186           && is_ttf
187           && cmap->find (pg) != cmap->end ())
188         {
189           FT_ULong char_code = cmap->find (pg)->second;
190           get_unicode_name (glyph_name, char_code);
191         }
192
193       if (glyph_name[0] ==  '\0' && has_glyph_names)
194         {
195           programming_error (_f ("Glyph has no name, but font supports glyph naming.\n"
196                                  "Skipping glyph U+%0X, file %s",
197                                  pg,
198                                  file_name.c_str ()));
199           continue;
200         }
201
202       if (glyph_name[0] == '\0' && is_ttf)
203         {
204           // access by glyph index directly.
205           get_glyph_index_name (glyph_name, pg);
206         }
207       
208       if (glyph_name[0] == '\0')
209         {
210           /*
211             CID entry
212           */
213           cid_keyed = true;
214           char_id = scm_from_uint32 (pg);
215         }
216       else
217         char_id = scm_makfrom0str (glyph_name);
218       
219       *tail = scm_cons (scm_list_4 (scm_from_double (ggeo.width * scale_),
220                                     scm_from_double (ggeo.x_offset * scale_),
221                                     scm_from_double (ggeo.y_offset * scale_),
222                                     
223                                     char_id),
224                         SCM_EOL);
225       tail = SCM_CDRLOC (*tail);
226     }
227
228   PangoFontDescription *descr = pango_font_describe (pa->font);
229   Real size = pango_font_description_get_size (descr)
230     / (Real (PANGO_SCALE));
231
232
233   if (!ps_name_str0)
234     warning (_f ("no PostScript font name for font `%s'", file_name));
235
236   string ps_name;
237   if (!ps_name_str0
238       && file_name != ""
239       && (file_name.find (".otf") != NPOS
240           || file_name.find (".cff") != NPOS))
241     {
242
243       /* UGH: kludge a PS name for OTF/CFF fonts.  */
244       string name = file_name;
245       ssize idx = file_name.find (".otf");
246       if (idx == NPOS)
247         idx = file_name.find (".cff");
248
249       name = name.substr (0, idx);
250
251       ssize slash_idx = name.rfind ('/');
252       if (slash_idx != NPOS)
253         {
254           slash_idx ++; 
255           name = name.substr (slash_idx,
256                               name.length () - slash_idx);
257         }
258       
259       string initial = name.substr (0, 1);
260       initial = String_convert::to_upper (initial);
261       name = name.substr (1, name.length () - 1);
262       name = String_convert::to_lower (name);
263       ps_name = initial + name;
264     }
265   else if (ps_name_str0)
266     ps_name = ps_name_str0;
267
268   if (ps_name.length ())
269     {
270       ((Pango_font *) this)->register_font_file (file_name, ps_name);
271       pango_fc_font_unlock_face (fcfont);
272
273       SCM expr = scm_list_5 (ly_symbol2scm ("glyph-string"),
274                              scm_makfrom0str (ps_name.c_str ()),
275                              scm_from_double (size),
276                              scm_from_bool (cid_keyed),
277                              ly_quote_scm (glyph_exprs));
278
279       return Stencil (b, expr);
280     }
281
282   warning (_ ("FreeType face has no PostScript font name"));
283   return Stencil ();
284 }
285
286 SCM
287 Pango_font::physical_font_tab () const
288 {
289   return physical_font_tab_;
290 }
291
292
293 Stencil
294 Pango_font::word_stencil (string str) const
295 {
296   return text_stencil (str, true);
297 }
298   
299 Stencil
300 Pango_font::text_stencil (string str) const
301 {
302   return text_stencil (str, false);
303 }
304
305 Stencil
306 Pango_font::text_stencil (string str, bool tight) const
307 {
308   GList *items
309     = pango_itemize (context_,
310                      str.c_str (),
311                      0, str.length (), attribute_list_,
312                      NULL);
313
314   Stencil dest;
315
316   Real last_x = 0.0;
317
318   Direction text_dir = RIGHT;
319   for (GList *p = items; p; p = p->next)
320     {
321       PangoItem *item = (PangoItem *) p->data;
322       if (item->analysis.level == PANGO_DIRECTION_RTL)
323         text_dir = LEFT;
324     }
325  
326   for (GList *ptr = items; ptr; ptr = ptr->next)
327     {
328       PangoItem *item = (PangoItem *) ptr->data;
329
330       Stencil item_stencil = pango_item_string_stencil (item, str, tight);
331
332       if (text_dir == RIGHT)
333         {
334           item_stencil.translate_axis (last_x, X_AXIS);
335           last_x = item_stencil.extent (X_AXIS)[RIGHT];
336         }
337       else if (text_dir == LEFT)
338         {
339           dest.translate_axis (item_stencil.extent (X_AXIS)[RIGHT], X_AXIS);
340         }
341
342 #if 0 /* Check extents.  */
343       if (!item_stencil.extent_box ()[X_AXIS].is_empty ())
344         {
345           Stencil frame = Lookup::frame (item_stencil.extent_box (), 0.1, 0.1);
346           Box empty;
347           empty.set_empty ();
348           Stencil dimless_frame (empty, frame.expr ());
349           dest.add_stencil (frame);
350         }
351 #endif
352   
353       dest.add_stencil (item_stencil);
354     }
355
356   /*
357     UGH. Should have flags per output format signifying supported
358     options.
359   */
360   if (output_backend_global != "ps"
361       && output_backend_global != "eps")
362     {
363       /*
364         For Pango based backends, we take a shortcut.
365       */
366       SCM exp
367         = scm_list_3 (ly_symbol2scm ("utf-8-string"),
368                       scm_makfrom0str (description_string ().c_str ()),
369                       scm_makfrom0str (str.c_str ()));
370
371       Box b (Interval (0, 0), Interval (0, 0));
372       b.unite (dest.extent_box ());
373       return Stencil (b, exp);
374     }
375
376
377   return dest;
378 }
379
380 string
381 Pango_font::description_string () const
382 {
383   char *descr_string = pango_font_description_to_string (pango_description_);
384   string s (descr_string);
385   g_free (descr_string);
386   return s;
387 }
388
389
390 SCM
391 Pango_font::font_file_name () const
392 {
393   return SCM_BOOL_F;
394 }
395
396 #endif