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