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