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