2 pango-font.cc -- implement Pango_font
4 source file of the GNU LilyPond music typesetter
6 (c) 2004--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #define PANGO_ENABLE_BACKEND // ugh, why necessary?
10 #include <pango/pangoft2.h>
11 #include <freetype/ftxf86.h>
14 #include "pango-font.hh"
16 #include "dimensions.hh"
17 #include "file-name.hh"
18 #include "international.hh"
21 #include "string-convert.hh"
27 Pango_font::Pango_font (PangoFT2FontMap *fontmap,
28 PangoFontDescription *description,
32 physical_font_tab_ = scm_c_make_hash_table (11);
33 PangoDirection pango_dir = PANGO_DIRECTION_LTR;
35 = pango_ft2_get_context (PANGO_RESOLUTION, PANGO_RESOLUTION);
36 // context_ = pango_ft2_font_map_create_context (fontmap);
38 pango_description_ = pango_font_description_copy (description);
39 attribute_list_ = pango_attr_list_new ();
42 urgh. I don't understand this. Why isn't this 1/(scale *
43 resolution * output_scale)
47 output_scale_ = output_scale;
48 scale_ = INCH_TO_BP / (Real (PANGO_SCALE) * Real (PANGO_RESOLUTION) * output_scale);
51 ugh. Should make this configurable.
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);
58 Pango_font::~Pango_font ()
60 pango_font_description_free (pango_description_);
61 g_object_unref (context_);
62 pango_attr_list_unref (attribute_list_);
66 Pango_font::register_font_file (string filename, string ps_name)
68 scm_hash_set_x (physical_font_tab_,
69 scm_makfrom0str (ps_name.c_str ()),
70 scm_makfrom0str (filename.c_str ()));
74 Pango_font::derived_mark () const
76 scm_gc_mark (physical_font_tab_);
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);
84 Index_to_charcode_map const *
85 get_index_to_charcode_map (string filename, FT_Face face)
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);
90 if (filename_charcode_maps_map.find (filename) == filename_charcode_maps_map.end ())
93 return &filename_charcode_maps_map[filename];
97 get_glyph_index_name (char *s, FT_ULong code)
99 sprintf (s, "glyphIndex%lX", code);
103 get_unicode_name (char*s, FT_ULong code)
106 sprintf (s, "u%lX", code);
108 sprintf (s, "uni%04lX", code);
113 Pango_font::pango_item_string_stencil (PangoItem const *item, string str,
114 bool tight_bbox) const
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 ();
121 pango_shape (str.c_str () + item->offset,
122 item->length, (PangoAnalysis*) pa, pgs);
124 PangoRectangle logical_rect;
125 PangoRectangle ink_rect;
126 pango_glyph_string_extents (pgs, pa->font, &ink_rect, &logical_rect);
128 PangoFcFont *fcfont = G_TYPE_CHECK_INSTANCE_CAST (pa->font,
132 FT_Face ftface = pango_fc_font_lock_face (fcfont);
134 PangoRectangle const *which_rect
139 Box b (Interval (PANGO_LBEARING (logical_rect),
140 PANGO_RBEARING (logical_rect)),
141 Interval (-PANGO_DESCENT (*which_rect),
142 PANGO_ASCENT (*which_rect)));
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);
151 if (file_name_as_ptr)
153 /* Normalize file name. */
154 file_name = File_name ((char const *)file_name_as_ptr).to_string ();
157 SCM glyph_exprs = SCM_EOL;
158 SCM *tail = &glyph_exprs;
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);
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++)
169 PangoGlyphInfo *pgi = pgs->glyphs + i;
171 PangoGlyph pg = pgi->glyph;
172 PangoGlyphGeometry ggeo = pgi->geometry;
174 glyph_name[0] = '\0';
177 int errorcode = FT_Get_Glyph_Name (ftface, pg, glyph_name, GLYPH_NAME_LEN);
179 programming_error ("FT_Get_Glyph_Name returns error");
182 SCM char_id = SCM_EOL;
183 if (glyph_name[0] == '\0'
186 && cmap->find (pg) != cmap->end ())
188 FT_ULong char_code = cmap->find (pg)->second;
189 get_unicode_name (glyph_name, char_code);
192 if (glyph_name[0] == '\0' && has_glyph_names)
194 programming_error (_f ("Glyph has no name, but font supports glyph naming.\n"
195 "Skipping glyph U+%0X, file %s",
197 file_name.c_str ()));
202 if (glyph_name == string (".notdef") && is_ttf)
203 glyph_name[0] = '\0';
205 if (glyph_name[0] == '\0' && is_ttf)
207 // access by glyph index directly.
208 get_glyph_index_name (glyph_name, pg);
211 if (glyph_name[0] == '\0')
217 char_id = scm_from_uint32 (pg);
220 char_id = scm_makfrom0str (glyph_name);
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_),
228 tail = SCM_CDRLOC (*tail);
231 PangoFontDescription *descr = pango_font_describe (pa->font);
232 Real size = pango_font_description_get_size (descr)
233 / (Real (PANGO_SCALE));
237 warning (_f ("no PostScript font name for font `%s'", file_name));
242 && (file_name.find (".otf") != NPOS
243 || file_name.find (".cff") != NPOS))
246 /* UGH: kludge a PS name for OTF/CFF fonts. */
247 string name = file_name;
248 ssize idx = file_name.find (".otf");
250 idx = file_name.find (".cff");
252 name = name.substr (0, idx);
254 ssize slash_idx = name.rfind ('/');
255 if (slash_idx != NPOS)
258 name = name.substr (slash_idx,
259 name.length () - slash_idx);
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;
268 else if (ps_name_str0)
269 ps_name = ps_name_str0;
271 if (ps_name.length ())
273 ((Pango_font *) this)->register_font_file (file_name, ps_name);
274 pango_fc_font_unlock_face (fcfont);
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));
282 return Stencil (b, expr);
285 warning (_ ("FreeType face has no PostScript font name"));
290 Pango_font::physical_font_tab () const
292 return physical_font_tab_;
297 Pango_font::word_stencil (string str) const
299 return text_stencil (str, true);
303 Pango_font::text_stencil (string str) const
305 return text_stencil (str, false);
309 Pango_font::text_stencil (string str, bool tight) const
312 = pango_itemize (context_,
314 0, str.length (), attribute_list_,
321 Direction text_dir = RIGHT;
322 for (GList *p = items; p; p = p->next)
324 PangoItem *item = (PangoItem *) p->data;
325 if (item->analysis.level == PANGO_DIRECTION_RTL)
329 for (GList *ptr = items; ptr; ptr = ptr->next)
331 PangoItem *item = (PangoItem *) ptr->data;
333 Stencil item_stencil = pango_item_string_stencil (item, str, tight);
335 if (text_dir == RIGHT)
337 item_stencil.translate_axis (last_x, X_AXIS);
338 last_x = item_stencil.extent (X_AXIS)[RIGHT];
340 else if (text_dir == LEFT)
342 dest.translate_axis (item_stencil.extent (X_AXIS)[RIGHT], X_AXIS);
345 #if 0 /* Check extents. */
346 if (!item_stencil.extent_box ()[X_AXIS].is_empty ())
348 Stencil frame = Lookup::frame (item_stencil.extent_box (), 0.1, 0.1);
351 Stencil dimless_frame (empty, frame.expr ());
352 dest.add_stencil (frame);
356 dest.add_stencil (item_stencil);
360 UGH. Should have flags per output format signifying supported
363 if (output_backend_global != "ps"
364 && output_backend_global != "eps")
367 For Pango based backends, we take a shortcut.
370 = scm_list_3 (ly_symbol2scm ("utf-8-string"),
371 scm_makfrom0str (description_string ().c_str ()),
372 scm_makfrom0str (str.c_str ()));
374 Box b (Interval (0, 0), Interval (0, 0));
375 b.unite (dest.extent_box ());
376 return Stencil (b, exp);
384 Pango_font::description_string () const
386 char *descr_string = pango_font_description_to_string (pango_description_);
387 string s (descr_string);
388 g_free (descr_string);
394 Pango_font::font_file_name () const