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>
17 #include "pango-font.hh"
18 #include "dimensions.hh"
19 #include "file-name.hh"
20 #include "international.hh"
21 #include "lookup.hh" // debugging
23 #include "string-convert.hh"
25 #include "all-font-metrics.hh"
30 Pango_font::Pango_font (PangoFT2FontMap *fontmap,
31 PangoFontDescription *description,
35 physical_font_tab_ = scm_c_make_hash_table (11);
36 PangoDirection pango_dir = PANGO_DIRECTION_LTR;
38 = pango_ft2_get_context (PANGO_RESOLUTION, PANGO_RESOLUTION);
39 // context_ = pango_ft2_font_map_create_context (fontmap);
41 pango_description_ = pango_font_description_copy (description);
42 attribute_list_ = pango_attr_list_new ();
45 urgh. I don't understand this. Why isn't this 1/(scale *
46 resolution * output_scale)
50 output_scale_ = output_scale;
51 scale_ = INCH_TO_BP / (Real (PANGO_SCALE) * Real (PANGO_RESOLUTION) * output_scale);
54 ugh. Should make this configurable.
56 pango_context_set_language (context_, pango_language_from_string ("en_US"));
57 pango_context_set_base_dir (context_, pango_dir);
58 pango_context_set_font_description (context_, description);
61 Pango_font::~Pango_font ()
63 pango_font_description_free (pango_description_);
64 g_object_unref (context_);
65 pango_attr_list_unref (attribute_list_);
69 Pango_font::register_font_file (string filename, string ps_name)
71 scm_hash_set_x (physical_font_tab_,
72 scm_makfrom0str (ps_name.c_str ()),
73 scm_makfrom0str (filename.c_str ()));
77 Pango_font::derived_mark () const
79 scm_gc_mark (physical_font_tab_);
84 get_glyph_index_name (char *s, FT_ULong code)
86 sprintf (s, "glyphIndex%lX", code);
90 get_unicode_name (char*s, FT_ULong code)
93 sprintf (s, "u%lX", code);
95 sprintf (s, "uni%04lX", code);
100 Pango_font::pango_item_string_stencil (PangoItem const *item, string str,
101 bool tight_bbox) const
103 const int GLYPH_NAME_LEN = 256;
104 char glyph_name[GLYPH_NAME_LEN];
105 PangoAnalysis const *pa = &(item->analysis);
106 PangoGlyphString *pgs = pango_glyph_string_new ();
108 pango_shape (str.c_str () + item->offset,
109 item->length, (PangoAnalysis*) pa, pgs);
111 PangoRectangle logical_rect;
112 PangoRectangle ink_rect;
113 pango_glyph_string_extents (pgs, pa->font, &ink_rect, &logical_rect);
115 PangoFcFont *fcfont = G_TYPE_CHECK_INSTANCE_CAST (pa->font,
119 FT_Face ftface = pango_fc_font_lock_face (fcfont);
121 PangoRectangle const *which_rect
126 Box b (Interval (PANGO_LBEARING (logical_rect),
127 PANGO_RBEARING (logical_rect)),
128 Interval (-PANGO_DESCENT (*which_rect),
129 PANGO_ASCENT (*which_rect)));
132 char const *ps_name_str0 = FT_Get_Postscript_Name (ftface);
133 FcPattern *fcpat = fcfont->font_pattern;
134 FcChar8 *file_name_as_ptr = 0;
135 FcPatternGetString (fcpat, FC_FILE, 0, &file_name_as_ptr);
138 if (file_name_as_ptr)
140 /* Normalize file name. */
141 file_name = File_name ((char const *)file_name_as_ptr).to_string ();
144 SCM glyph_exprs = SCM_EOL;
145 SCM *tail = &glyph_exprs;
147 Index_to_charcode_map const *cmap = 0;
148 bool has_glyph_names = ftface->face_flags & FT_FACE_FLAG_GLYPH_NAMES;
149 if (! has_glyph_names)
150 cmap = all_fonts_global->get_index_to_charcode_map (file_name, ftface);
152 bool is_ttf = string (FT_Get_X11_Font_Format (ftface)) == "TrueType";
153 bool cid_keyed = false;
154 for (int i = 0; i < pgs->num_glyphs; i++)
156 PangoGlyphInfo *pgi = pgs->glyphs + i;
158 PangoGlyph pg = pgi->glyph;
159 PangoGlyphGeometry ggeo = pgi->geometry;
161 glyph_name[0] = '\0';
164 int errorcode = FT_Get_Glyph_Name (ftface, pg, glyph_name, GLYPH_NAME_LEN);
166 programming_error ("FT_Get_Glyph_Name returns error");
169 SCM char_id = SCM_EOL;
170 if (glyph_name[0] == '\0'
173 && cmap->find (pg) != cmap->end ())
175 FT_ULong char_code = cmap->find (pg)->second;
176 get_unicode_name (glyph_name, char_code);
179 if (glyph_name[0] == '\0' && has_glyph_names)
181 programming_error (_f ("Glyph has no name, but font supports glyph naming.\n"
182 "Skipping glyph U+%0X, file %s",
184 file_name.c_str ()));
189 if (glyph_name == string (".notdef") && is_ttf)
190 glyph_name[0] = '\0';
192 if (glyph_name[0] == '\0' && is_ttf)
194 // access by glyph index directly.
195 get_glyph_index_name (glyph_name, pg);
198 if (glyph_name[0] == '\0')
204 char_id = scm_from_uint32 (pg);
207 char_id = scm_makfrom0str (glyph_name);
209 *tail = scm_cons (scm_list_4 (scm_from_double (ggeo.width * scale_),
210 scm_from_double (ggeo.x_offset * scale_),
211 scm_from_double (ggeo.y_offset * scale_),
215 tail = SCM_CDRLOC (*tail);
218 PangoFontDescription *descr = pango_font_describe (pa->font);
219 Real size = pango_font_description_get_size (descr)
220 / (Real (PANGO_SCALE));
224 warning (_f ("no PostScript font name for font `%s'", file_name));
229 && (file_name.find (".otf") != NPOS
230 || file_name.find (".cff") != NPOS))
233 /* UGH: kludge a PS name for OTF/CFF fonts. */
234 string name = file_name;
235 ssize idx = file_name.find (".otf");
237 idx = file_name.find (".cff");
239 name = name.substr (0, idx);
241 ssize slash_idx = name.rfind ('/');
242 if (slash_idx != NPOS)
245 name = name.substr (slash_idx,
246 name.length () - slash_idx);
249 string initial = name.substr (0, 1);
250 initial = String_convert::to_upper (initial);
251 name = name.substr (1, name.length () - 1);
252 name = String_convert::to_lower (name);
253 ps_name = initial + name;
255 else if (ps_name_str0)
256 ps_name = ps_name_str0;
258 if (ps_name.length ())
260 ((Pango_font *) this)->register_font_file (file_name, ps_name);
261 pango_fc_font_unlock_face (fcfont);
263 SCM expr = scm_list_5 (ly_symbol2scm ("glyph-string"),
264 scm_makfrom0str (ps_name.c_str ()),
265 scm_from_double (size),
266 scm_from_bool (cid_keyed),
267 ly_quote_scm (glyph_exprs));
269 return Stencil (b, expr);
272 warning (_ ("FreeType face has no PostScript font name"));
277 Pango_font::physical_font_tab () const
279 return physical_font_tab_;
284 Pango_font::word_stencil (string str) const
286 return text_stencil (str, true);
290 Pango_font::text_stencil (string str) const
292 return text_stencil (str, false);
296 Pango_font::text_stencil (string str, bool tight) const
299 = pango_itemize (context_,
301 0, str.length (), attribute_list_,
308 Direction text_dir = RIGHT;
309 for (GList *p = items; p; p = p->next)
311 PangoItem *item = (PangoItem *) p->data;
312 if (item->analysis.level == PANGO_DIRECTION_RTL)
316 for (GList *ptr = items; ptr; ptr = ptr->next)
318 PangoItem *item = (PangoItem *) ptr->data;
320 Stencil item_stencil = pango_item_string_stencil (item, str, tight);
322 if (text_dir == RIGHT)
324 item_stencil.translate_axis (last_x, X_AXIS);
325 last_x = item_stencil.extent (X_AXIS)[RIGHT];
327 else if (text_dir == LEFT)
329 dest.translate_axis (item_stencil.extent (X_AXIS)[RIGHT], X_AXIS);
332 #if 0 /* Check extents. */
333 if (!item_stencil.extent_box ()[X_AXIS].is_empty ())
335 Stencil frame = Lookup::frame (item_stencil.extent_box (), 0.1, 0.1);
338 Stencil dimless_frame (empty, frame.expr ());
339 dest.add_stencil (frame);
343 dest.add_stencil (item_stencil);
347 UGH. Should have flags per output format signifying supported
350 if (output_backend_global != "ps"
351 && output_backend_global != "eps")
354 For Pango based backends, we take a shortcut.
357 = scm_list_3 (ly_symbol2scm ("utf-8-string"),
358 scm_makfrom0str (description_string ().c_str ()),
359 scm_makfrom0str (str.c_str ()));
361 Box b (Interval (0, 0), Interval (0, 0));
362 b.unite (dest.extent_box ());
363 return Stencil (b, exp);
371 Pango_font::description_string () const
373 char *descr_string = pango_font_description_to_string (pango_description_);
374 string s (descr_string);
375 g_free (descr_string);
381 Pango_font::font_file_name () const