2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2004--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 // Necessary for supporting pango_context_new() and
21 // pango_context_set_font_map() in Pango < 1.22
22 #define PANGO_ENABLE_BACKEND
24 #include <pango/pangoft2.h>
25 #include "freetype.hh"
33 #include "pango-font.hh"
34 #include "dimensions.hh"
35 #include "file-name.hh"
36 #include "international.hh"
37 #include "lookup.hh" // debugging
38 #include "ly-module.hh"
40 #include "string-convert.hh"
42 #include "all-font-metrics.hh"
43 #include "program-option.hh"
44 #include "open-type-font.hh"
49 Pango_font::Pango_font (PangoFT2FontMap *fontmap,
50 PangoFontDescription const *description,
53 // This line looks stupid, but if we don't initialize physical_font_tab_ before
54 // we allocate memory in scm_c_make_hash_table, then that could trigger a garbage
56 physical_font_tab_ = SCM_EOL;
57 physical_font_tab_ = scm_c_make_hash_table (11);
58 PangoDirection pango_dir = PANGO_DIRECTION_LTR;
59 context_ = pango_context_new ();
60 pango_context_set_font_map (context_, PANGO_FONT_MAP (fontmap));
62 pango_description_ = pango_font_description_copy (description);
63 attribute_list_ = pango_attr_list_new ();
65 // urgh. I don't understand this. Why isn't this 1/(scale *
66 // resolution * output_scale)
69 output_scale_ = output_scale;
71 / (Real (PANGO_SCALE) * Real (PANGO_RESOLUTION) * output_scale);
73 // ugh. Should make this configurable.
74 pango_context_set_language (context_, pango_language_from_string ("en_US"));
75 pango_context_set_base_dir (context_, pango_dir);
76 pango_context_set_font_description (context_, description);
79 Pango_font::~Pango_font ()
81 pango_font_description_free (pango_description_);
82 g_object_unref (context_);
83 pango_attr_list_unref (attribute_list_);
87 Pango_font::register_font_file (const string &filename,
88 const string &ps_name,
91 scm_hash_set_x (physical_font_tab_,
92 ly_string2scm (ps_name),
93 scm_list_2 (ly_string2scm (filename),
94 scm_from_int (face_index)));
98 Pango_font::name_to_index (string nm) const
100 PangoFcFont *fcfont = PANGO_FC_FONT (pango_context_load_font (context_, pango_description_));
101 FT_Face face = pango_fc_font_lock_face (fcfont);
102 char *nm_str = (char *) nm.c_str ();
103 if (FT_UInt idx = FT_Get_Name_Index (face, nm_str))
105 pango_fc_font_unlock_face (fcfont);
109 pango_fc_font_unlock_face (fcfont);
114 Pango_font::derived_mark () const
116 scm_gc_mark (physical_font_tab_);
120 get_glyph_index_name (char *s,
123 sprintf (s, "glyphIndex%lX", code);
127 get_unicode_name (char *s,
131 sprintf (s, "u%lX", code);
133 sprintf (s, "uni%04lX", code);
137 Pango_font::get_unscaled_indexed_char_dimensions (size_t signed_idx) const
139 PangoFcFont *fcfont = PANGO_FC_FONT (pango_context_load_font (context_, pango_description_));
140 FT_Face face = pango_fc_font_lock_face (fcfont);
141 Box b = ly_FT_get_unscaled_indexed_char_dimensions (face, signed_idx);
142 pango_fc_font_unlock_face (fcfont);
147 Pango_font::get_scaled_indexed_char_dimensions (size_t signed_idx) const
149 PangoFont *font = pango_context_load_font (context_, pango_description_);
150 PangoRectangle logical_rect;
151 PangoRectangle ink_rect;
152 pango_font_get_glyph_extents (font, signed_idx, &ink_rect, &logical_rect);
153 Box out (Interval (PANGO_LBEARING (ink_rect),
154 PANGO_RBEARING (ink_rect)),
155 Interval (-PANGO_DESCENT (ink_rect),
156 PANGO_ASCENT (ink_rect)));
162 Pango_font::get_glyph_outline_bbox (size_t signed_idx) const
164 PangoFcFont *fcfont = PANGO_FC_FONT (pango_context_load_font (context_, pango_description_));
165 FT_Face face = pango_fc_font_lock_face (fcfont);
166 Box b = ly_FT_get_glyph_outline_bbox (face, signed_idx);
167 pango_fc_font_unlock_face (fcfont);
172 Pango_font::get_glyph_outline (size_t signed_idx) const
174 PangoFcFont *fcfont = PANGO_FC_FONT (pango_context_load_font (context_, pango_description_));
175 FT_Face face = pango_fc_font_lock_face (fcfont);
176 SCM s = ly_FT_get_glyph_outline (face, signed_idx);
177 pango_fc_font_unlock_face (fcfont);
182 Pango_font::pango_item_string_stencil (PangoGlyphItem const *glyph_item) const
184 const int GLYPH_NAME_LEN = 256;
185 char glyph_name[GLYPH_NAME_LEN];
187 PangoAnalysis const *pa = &(glyph_item->item->analysis);
188 PangoGlyphString *pgs = glyph_item->glyphs;
190 PangoRectangle logical_rect;
191 PangoRectangle ink_rect;
192 pango_glyph_string_extents (pgs, pa->font, &ink_rect, &logical_rect);
194 PangoFcFont *fcfont = PANGO_FC_FONT (pa->font);
195 FT_Face ftface = pango_fc_font_lock_face (fcfont);
197 Box b (Interval (PANGO_LBEARING (logical_rect),
198 PANGO_RBEARING (logical_rect)),
199 Interval (-PANGO_DESCENT (ink_rect),
200 PANGO_ASCENT (ink_rect)));
204 const string ps_name_str0 = get_postscript_name (ftface);
205 FcPattern *fcpat = fcfont->font_pattern;
207 FcChar8 *file_name_as_ptr = 0;
208 FcPatternGetString (fcpat, FC_FILE, 0, &file_name_as_ptr);
210 // due to a bug in FreeType 2.3.7 and earlier we can't use
211 // ftface->face_index; it is always zero for some font formats,
212 // in particular TTCs which we are interested in
214 FcPatternGetInteger (fcpat, FC_INDEX, 0, &face_index);
217 if (file_name_as_ptr)
218 // Normalize file name.
219 file_name = File_name ((char const *)file_name_as_ptr).to_string ();
221 SCM glyph_exprs = SCM_EOL;
222 SCM *tail = &glyph_exprs;
224 Index_to_charcode_map const *cmap = 0;
225 bool has_glyph_names = ftface->face_flags & FT_FACE_FLAG_GLYPH_NAMES;
226 if (!has_glyph_names)
227 cmap = all_fonts_global->get_index_to_charcode_map (file_name, face_index, ftface);
229 bool is_ttf = string (FT_Get_X11_Font_Format (ftface)) == "TrueType";
230 bool cid_keyed = false;
232 for (int i = 0; i < pgs->num_glyphs; i++)
234 PangoGlyphInfo *pgi = pgs->glyphs + i;
236 PangoGlyph pg = pgi->glyph;
237 PangoGlyphGeometry ggeo = pgi->geometry;
240 Zero-width characters are valid Unicode characters,
241 but glyph lookups need to be skipped.
243 if (!(pg ^ PANGO_GLYPH_EMPTY))
246 if (pg & PANGO_GLYPH_UNKNOWN_FLAG)
248 warning (_f ("no glyph for character U+%0X in font `%s'",
249 pg & ~PANGO_GLYPH_UNKNOWN_FLAG, file_name.c_str ()));
253 glyph_name[0] = '\0';
256 FT_Error errorcode = FT_Get_Glyph_Name (ftface, pg, glyph_name,
259 programming_error (_f ("FT_Get_Glyph_Name () error: %s",
260 freetype_error_string (errorcode).c_str ()));
263 SCM char_id = SCM_EOL;
264 if (glyph_name[0] == '\0'
267 && cmap->find (pg) != cmap->end ())
269 FT_ULong char_code = cmap->find (pg)->second;
270 get_unicode_name (glyph_name, char_code);
273 if (glyph_name[0] == '\0' && has_glyph_names)
275 programming_error (_f ("Glyph has no name, but font supports glyph naming.\n"
276 "Skipping glyph U+%0X, file %s",
277 pg, file_name.c_str ()));
281 if (glyph_name == string (".notdef") && is_ttf)
282 glyph_name[0] = '\0';
284 if (glyph_name[0] == '\0' && is_ttf)
285 // Access by glyph index directly.
286 get_glyph_index_name (glyph_name, pg);
288 if (glyph_name[0] == '\0')
292 char_id = scm_from_uint32 (pg);
295 char_id = scm_from_utf8_string (glyph_name);
297 PangoRectangle logical_sub_rect;
298 PangoRectangle ink_sub_rect;
300 pango_glyph_string_extents_range (pgs, i, i + 1, pa->font, &ink_sub_rect, &logical_sub_rect);
301 Box b_sub (Interval (PANGO_LBEARING (logical_sub_rect),
302 PANGO_RBEARING (logical_sub_rect)),
303 Interval (-PANGO_DESCENT (ink_sub_rect),
304 PANGO_ASCENT (ink_sub_rect)));
306 b_sub.scale (scale_);
308 *tail = scm_cons (scm_list_5 (scm_from_double (b_sub[X_AXIS][RIGHT] - b_sub[X_AXIS][LEFT]),
309 scm_cons (scm_from_double (b_sub[Y_AXIS][DOWN]),
310 scm_from_double (b_sub[Y_AXIS][UP])),
311 scm_from_double (ggeo.x_offset * scale_),
312 scm_from_double (- ggeo.y_offset * scale_),
315 tail = SCM_CDRLOC (*tail);
317 pango_fc_font_unlock_face (fcfont);
318 pango_glyph_string_free (pgs);
320 PangoFontDescription *descr = pango_font_describe (pa->font);
321 Real size = pango_font_description_get_size (descr)
322 / (Real (PANGO_SCALE));
324 if (ps_name_str0.empty ())
325 warning (_f ("no PostScript font name for font `%s'", file_name));
328 if (ps_name_str0.empty ()
330 && (file_name.find (".otf") != NPOS
331 || file_name.find (".cff") != NPOS))
333 // UGH: kludge a PS name for OTF/CFF fonts.
334 string name = file_name;
335 ssize idx = file_name.find (".otf");
337 idx = file_name.find (".cff");
339 name = name.substr (0, idx);
341 ssize slash_idx = name.rfind ('/');
342 if (slash_idx != NPOS)
345 name = name.substr (slash_idx,
346 name.length () - slash_idx);
349 string initial = name.substr (0, 1);
350 initial = String_convert::to_upper (initial);
351 name = name.substr (1, name.length () - 1);
352 name = String_convert::to_lower (name);
353 ps_name = initial + name;
355 else if (!ps_name_str0.empty ())
356 ps_name = ps_name_str0;
358 if (ps_name.length ())
360 ((Pango_font *) this)->register_font_file (file_name,
364 SCM expr = scm_list_n (ly_symbol2scm ("glyph-string"),
366 ly_string2scm (ps_name),
367 scm_from_double (size),
368 scm_from_bool (cid_keyed),
369 ly_quote_scm (glyph_exprs),
372 return Stencil (b, expr);
375 warning (_ ("FreeType face has no PostScript font name"));
380 Pango_font::physical_font_tab () const
382 return physical_font_tab_;
385 extern bool music_strings_to_paths;
388 Pango_font::text_stencil (Output_def * /* state */,
389 const string &str, bool music_string) const
392 The text assigned to a PangoLayout is automatically divided
393 into sections and reordered according to the Unicode
394 Bidirectional Algorithm, if necessary.
396 PangoLayout *layout = pango_layout_new (context_);
397 pango_layout_set_text (layout, str.c_str (), -1);
398 GSList *lines = pango_layout_get_lines (layout);
403 for (GSList *l = lines; l; l = l->next)
405 PangoLayoutLine *line = (PangoLayoutLine *) l->data;
406 GSList *layout_runs = line->runs;
408 for (GSList *p = layout_runs; p; p = p->next)
410 PangoGlyphItem *item = (PangoGlyphItem *) p->data;
411 Stencil item_stencil = pango_item_string_stencil (item);
413 item_stencil.translate_axis (last_x, X_AXIS);
414 last_x = item_stencil.extent (X_AXIS)[RIGHT];
416 #if 0 // Check extents.
417 if (!item_stencil.extent_box ()[X_AXIS].is_empty ())
419 Stencil frame = Lookup::frame (item_stencil.extent_box (), 0.1, 0.1);
422 Stencil dimless_frame (empty, frame.expr ());
423 dest.add_stencil (frame);
427 dest.add_stencil (item_stencil);
431 string name = get_output_backend_name ();
432 string output_mod = "scm output-" + name;
433 SCM mod = scm_c_resolve_module (output_mod.c_str ());
435 bool has_utf8_string = false;
437 if (ly_is_module (mod))
439 SCM utf8_string = ly_module_lookup (mod, ly_symbol2scm ("utf-8-string"));
441 has_utf8_string should only be true when utf8_string is a
442 variable that is bound to a *named* procedure, i.e. not a
445 if (scm_is_true (utf8_string)
446 && scm_is_true (scm_procedure_name (SCM_VARIABLE_REF (utf8_string))))
447 has_utf8_string = true;
450 bool to_paths = music_strings_to_paths;
453 Backends with the utf-8-string expression use it when
454 1) the -dmusic-strings-to-paths option is set
455 and `str' is not a music string, or
456 2) the -dmusic-strings-to-paths option is not set.
458 if (has_utf8_string && ((to_paths && !music_string) || !to_paths))
460 // For Pango based backends, we take a shortcut.
461 SCM exp = scm_list_3 (ly_symbol2scm ("utf-8-string"),
462 ly_string2scm (description_string ()),
463 ly_string2scm (str));
465 Box b (Interval (0, 0), Interval (0, 0));
466 b.unite (dest.extent_box ());
467 return Stencil (b, exp);
474 Pango_font::description_string () const
476 char *descr_string = pango_font_description_to_string (pango_description_);
477 string s (descr_string);
478 g_free (descr_string);
483 Pango_font::font_file_name () const
488 #endif // HAVE_PANGO_FT2