2 pango-font.cc -- implement Pango_font
4 source file of the GNU LilyPond music typesetter
6 (c) 2004 Han-Wen Nienhuys <hanwen@xs4all.nl>
11 #define PANGO_ENABLE_BACKEND // ugh, why necessary?
13 #include <pango/pangoft2.h>
17 #include "dimensions.hh"
18 #include "pango-font.hh"
24 Pango_font::Pango_font (PangoFT2FontMap *fontmap,
26 PangoFontDescription *description)
29 PangoDirection pango_dir = (dir == RIGHT)
31 : PANGO_DIRECTION_RTL;
33 pango_ft2_get_context (PANGO_RESOLUTION, PANGO_RESOLUTION);
35 pango_description_ = pango_font_description_copy (description);
36 // context_ = pango_ft2_font_map_create_context (fontmap);
37 attribute_list_= pango_attr_list_new();
41 urgh. I don't understand this. Why isn't this 1/(scale * resolution)
45 scale_ = INCH_TO_BP / (Real (PANGO_SCALE) * Real (PANGO_RESOLUTION));
47 pango_context_set_language (context_, pango_language_from_string ("en_US"));
48 pango_context_set_base_dir (context_, pango_dir);
49 pango_context_set_font_description (context_, description);
52 Pango_font::~Pango_font ()
54 pango_font_description_free (pango_description_);
55 g_object_unref (context_);
56 pango_attr_list_unref (attribute_list_);
60 Pango_font::register_font_file (String filename, String ps_name)
62 subfonts_ = scm_cons (scm_makfrom0str (filename.to_str0 ()),
67 Pango_font::derived_mark () const
69 scm_gc_mark (subfonts_);
73 Pango_font::pango_item_string_stencil (PangoItem *item, String str, Real dx) const
75 const int GLYPH_NAME_LEN = 256;
76 char glyph_name[GLYPH_NAME_LEN];
77 PangoAnalysis *pa = &(item->analysis);
78 PangoGlyphString *pgs = pango_glyph_string_new ();
80 pango_shape (str.to_str0 () + item->offset,
81 item->length, pa, pgs);
83 PangoRectangle logical_rect;
84 PangoRectangle ink_rect;
85 pango_glyph_string_extents (pgs, pa->font, &ink_rect, &logical_rect);
87 PangoFcFont *fcfont = G_TYPE_CHECK_INSTANCE_CAST(pa->font,
91 FT_Face ftface = pango_fc_font_lock_face (fcfont);
92 Box b (Interval (PANGO_LBEARING(logical_rect),
93 PANGO_RBEARING(logical_rect)),
94 Interval (-PANGO_DESCENT(logical_rect),
95 PANGO_ASCENT(logical_rect)));
99 SCM glyph_exprs = SCM_EOL;
100 SCM *tail = &glyph_exprs;
101 for (int i = 0; i < pgs->num_glyphs; i++)
103 PangoGlyphInfo *pgi = pgs->glyphs + i;
105 PangoGlyph pg = pgi->glyph;
106 PangoGlyphGeometry ggeo = pgi->geometry;
108 FT_Get_Glyph_Name (ftface, pg, glyph_name, GLYPH_NAME_LEN);
109 *tail = scm_cons (scm_list_3 (scm_from_double (ggeo.x_offset * scale_ + dx),
110 scm_from_double (ggeo.y_offset * scale_),
111 scm_makfrom0str (glyph_name)),
114 tail = SCM_CDRLOC (*tail);
117 PangoFontDescription *descr = pango_font_describe (pa->font);
118 Real size = pango_font_description_get_size (descr)
119 / (Real (PANGO_SCALE));
121 FcPattern *fcpat = fcfont->font_pattern;
123 FcPatternGetString(fcpat, FC_FILE, 0, (FcChar8 **) &filename);
124 char const *ps_name = FT_Get_Postscript_Name (ftface);
125 ((Pango_font *) this)->register_font_file (filename, ps_name);
126 pango_fc_font_unlock_face (fcfont);
128 SCM expr = scm_list_4 (ly_symbol2scm ("glyph-string"),
129 scm_makfrom0str (ps_name),
130 scm_from_double (size),
131 ly_quote_scm (glyph_exprs));
133 Stencil item_stencil (b, expr);
140 Pango_font::text_stencil (String str) const
142 GList *items = pango_itemize (context_,
144 0, str.length (), attribute_list_,
152 PangoItem *item = (PangoItem*) ptr->data;
154 Stencil item_stencil = pango_item_string_stencil (item, str, x);
156 x = item_stencil.extent (X_AXIS)[RIGHT];
158 dest.add_stencil (item_stencil);
163 if (output_backend_global != "ps")
166 For Pango based backends, we take a shortcut.
169 = scm_list_3 (ly_symbol2scm ("utf-8-string"),
170 scm_makfrom0str (pango_font_description_to_filename (pango_description_)),
171 scm_makfrom0str (str.to_str0 ()));
174 return Stencil (dest.extent_box (),
182 Pango_font::sub_fonts () const
188 Pango_font::font_file_name () const
193 LY_DEFINE (ly_pango_font_p, "ly:pango-font?",
196 "Is @var{f} a pango font?")
198 return scm_from_bool (dynamic_cast<Pango_font*> (unsmob_metrics (f)));