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>
16 #include "dimensions.hh"
17 #include "pango-font.hh"
23 Pango_font::Pango_font (PangoFT2FontMap *fontmap,
25 PangoFontDescription *description)
28 PangoDirection pango_dir = (dir == RIGHT)
30 : PANGO_DIRECTION_RTL;
32 pango_ft2_get_context (PANGO_DPI, PANGO_DPI);
33 // context_ = pango_ft2_font_map_create_context (fontmap);
34 attribute_list_= pango_attr_list_new();
35 scale_ = inch_constant / (Real (PANGO_SCALE) * Real (PANGO_DPI));
37 pango_context_set_language (context_, pango_language_from_string ("en_US"));
38 pango_context_set_base_dir (context_, pango_dir);
39 pango_context_set_font_description (context_, description);
42 Pango_font::~Pango_font ()
44 g_object_unref (context_);
45 pango_attr_list_unref (attribute_list_);
49 Pango_font::register_font_file (String filename, String ps_name)
51 subfonts_ = scm_cons (scm_makfrom0str (filename.to_str0 ()),
56 Pango_font::derived_mark () const
58 scm_gc_mark (subfonts_);
62 Pango_font::pango_item_string_stencil (PangoItem *item, String str, Real dx) const
64 const int GLYPH_NAME_LEN = 256;
65 char glyph_name[GLYPH_NAME_LEN];
66 PangoAnalysis *pa = &(item->analysis);
67 PangoGlyphString *pgs = pango_glyph_string_new ();
69 pango_shape (str.to_str0 () + item->offset,
70 item->length, pa, pgs);
72 PangoRectangle logical_rect;
73 PangoRectangle ink_rect;
74 pango_glyph_string_extents (pgs, pa->font, &ink_rect, &logical_rect);
76 PangoFcFont *fcfont = G_TYPE_CHECK_INSTANCE_CAST(pa->font,
80 FT_Face ftface = pango_fc_font_lock_face (fcfont);
81 Box b (Interval (PANGO_LBEARING(logical_rect),
82 PANGO_RBEARING(logical_rect)),
83 Interval (-PANGO_DESCENT(logical_rect),
84 PANGO_ASCENT(logical_rect)));
88 SCM glyph_exprs = SCM_EOL;
89 SCM *tail = &glyph_exprs;
90 for (int i = 0; i < pgs->num_glyphs; i++)
92 PangoGlyphInfo *pgi = pgs->glyphs + i;
94 PangoGlyph pg = pgi->glyph;
95 PangoGlyphGeometry ggeo = pgi->geometry;
97 FT_Get_Glyph_Name (ftface, pg, glyph_name, GLYPH_NAME_LEN);
98 *tail = scm_cons (scm_list_3 (scm_from_double (ggeo.x_offset * scale_ + dx),
99 scm_from_double (ggeo.y_offset * scale_),
100 scm_makfrom0str (glyph_name)),
103 tail = SCM_CDRLOC (*tail);
106 PangoFontDescription *descr = pango_font_describe (pa->font);
107 Real size = pango_font_description_get_size (descr)
108 / (Real (PANGO_SCALE)) ;
110 FcPattern *fcpat = fcfont->font_pattern;
112 FcPatternGetString(fcpat, FC_FILE, 0, (FcChar8 **) &filename);
113 char const *ps_name = FT_Get_Postscript_Name (ftface);
114 ((Pango_font *) this)->register_font_file (filename, ps_name);
115 pango_fc_font_unlock_face (fcfont);
117 SCM expr = scm_list_4 (ly_symbol2scm ("glyph-string"),
118 scm_makfrom0str (ps_name),
119 scm_from_double (size),
120 ly_quote_scm (glyph_exprs));
122 Stencil item_stencil (b, expr);
128 Pango_font::text_stencil (String str) const
130 GList *items = pango_itemize (context_,
132 0, str.length (), attribute_list_,
140 PangoItem *item = (PangoItem*) ptr->data;
142 Stencil item_stencil = pango_item_string_stencil (item, str, x);
144 x = item_stencil.extent (X_AXIS)[RIGHT];
146 dest.add_stencil (item_stencil);
155 Pango_font::sub_fonts () const
161 Pango_font::font_file_name () const
166 LY_DEFINE (ly_pango_font_p, "ly:pango-font?",
169 "Is @var{f} a pango font?")
171 return scm_from_bool (dynamic_cast<Pango_font*> (unsmob_metrics (f)));