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>
15 #include "pango-font.hh"
21 Pango_font::Pango_font (PangoFT2FontMap *fontmap,
24 PangoFontDescription *description)
27 PangoDirection pango_dir = (dir == RIGHT)
29 : PANGO_DIRECTION_RTL;
30 context_ = pango_ft2_font_map_create_context (fontmap);
31 attribute_list_= pango_attr_list_new();
32 scale_ = 1.0 / (PANGO_SCALE * resolution * 72.27);
34 pango_context_set_language (context_, pango_language_from_string ("en_US"));
35 pango_context_set_base_dir (context_, pango_dir);
36 pango_context_set_font_description (context_, description);
39 Pango_font::~Pango_font ()
41 g_object_unref (context_);
42 pango_attr_list_unref (attribute_list_);
46 Pango_font::register_font_file (String filename, String ps_name)
48 subfonts_ = scm_cons (//scm_cons (scm_makfrom0str (ps_name.to_str0 ()),
49 scm_makfrom0str (filename.to_str0 ()),
54 Pango_font::derived_mark () const
56 scm_gc_mark (subfonts_);
60 Pango_font::text_stencil (String str) const
62 GList *items = pango_itemize (context_,
64 0, str.length (), attribute_list_,
67 const int GLYPH_NAME_LEN = 256;
68 char glyph_name[GLYPH_NAME_LEN];
75 // FIXME: factor this out
76 PangoItem *item = (PangoItem*) ptr->data;
77 PangoAnalysis *pa = &(item->analysis);
78 PangoGlyphString *pgs = pango_glyph_string_new ();
80 pango_shape (str.to_str0 (), str.length (), pa, pgs);
82 PangoRectangle logical_rect;
83 pango_glyph_string_extents (pgs, pa->font, NULL, &logical_rect);
85 PangoFcFont *fcfont = G_TYPE_CHECK_INSTANCE_CAST(pa->font,
88 FT_Face ftface = pango_fc_font_lock_face (fcfont);
89 Box b (Interval (0, logical_rect.width),
90 Interval (0, logical_rect.height));
94 /* FIXME. This obvious shortcut apparently does not work.
95 It seems there are different faces per text string and a
96 map of face_ and charcode mapping is needed. */
97 Pango_font *barf = (Pango_font*) this;
99 barf->index_to_charcode_map_ = make_index_to_charcode_map (face_);
102 b.translate (Offset (- logical_rect.x, -logical_rect.y));
106 SCM glyph_exprs = SCM_EOL;
107 SCM *tail = &glyph_exprs;
108 for (int i = 0; i < pgs->num_glyphs; i++)
110 PangoGlyphInfo *pgi = pgs->glyphs + i;
112 PangoGlyph pg = pgi->glyph;
113 PangoGlyphGeometry ggeo = pgi->geometry;
115 FT_Get_Glyph_Name (ftface, pg, glyph_name, GLYPH_NAME_LEN);
116 *tail = scm_cons (scm_list_3 (scm_from_double (ggeo.x_offset * scale_),
117 scm_from_double (ggeo.y_offset * scale_),
118 scm_makfrom0str (glyph_name)),
120 tail = SCM_CDRLOC (*tail);
123 FcPattern *fcpat = fcfont->font_pattern;
125 FcPatternGetString(fcpat, FC_FILE, 0, (FcChar8 **) &filename);
126 char const *ps_name = FT_Get_Postscript_Name (ftface);
127 ((Pango_font *) this)->register_font_file (filename, ps_name);
129 SCM expr = scm_list_4 (ly_symbol2scm ("glyph-string"),
131 scm_makfrom0str (ps_name),
132 ly_quote_scm (glyph_exprs));
134 Stencil item_stencil (b, expr);
136 dest.add_stencil (item_stencil);
138 pango_fc_font_unlock_face (fcfont);
146 Pango_font::sub_fonts () const
152 Pango_font::font_file_name () const
158 Pango_font::name_to_index (String nm) const
160 char *nm_str = (char*) nm.to_str0 ();
161 if (int idx = FT_Get_Name_Index (face_, nm_str))
167 Pango_font::index_to_charcode (int i) const
169 return ((Pango_font*) this)->index_to_charcode_map_[i];
172 LY_DEFINE (ly_pango_font_p, "ly:pango-font?",
175 "Is @var{f} a pango font?")
177 return scm_from_bool (dynamic_cast<Pango_font*> (unsmob_metrics (f)));