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 "dimensions.hh"
16 #include "pango-font.hh"
22 Pango_font::Pango_font (PangoFT2FontMap *fontmap,
24 PangoFontDescription *description)
27 PangoDirection pango_dir = (dir == RIGHT)
29 : PANGO_DIRECTION_RTL;
31 pango_ft2_get_context (PANGO_DPI, PANGO_DPI);
32 // context_ = pango_ft2_font_map_create_context (fontmap);
33 attribute_list_= pango_attr_list_new();
34 scale_ = inch_constant / (Real (PANGO_SCALE) * Real (PANGO_DPI));
36 pango_context_set_language (context_, pango_language_from_string ("en_US"));
37 pango_context_set_base_dir (context_, pango_dir);
38 pango_context_set_font_description (context_, description);
41 Pango_font::~Pango_font ()
43 g_object_unref (context_);
44 pango_attr_list_unref (attribute_list_);
48 Pango_font::register_font_file (String filename, String ps_name)
50 subfonts_ = scm_cons (scm_makfrom0str (filename.to_str0 ()),
55 Pango_font::derived_mark () const
57 scm_gc_mark (subfonts_);
61 Pango_font::text_stencil (String str) const
63 GList *items = pango_itemize (context_,
65 0, str.length (), attribute_list_,
68 const int GLYPH_NAME_LEN = 256;
69 char glyph_name[GLYPH_NAME_LEN];
76 // FIXME: factor this out
77 PangoItem *item = (PangoItem*) ptr->data;
78 PangoAnalysis *pa = &(item->analysis);
79 PangoGlyphString *pgs = pango_glyph_string_new ();
81 pango_shape (str.to_str0 () + item->offset,
82 item->length, pa, pgs);
84 PangoRectangle logical_rect;
85 PangoRectangle ink_rect;
86 pango_glyph_string_extents (pgs, pa->font, &ink_rect, &logical_rect);
88 PangoFcFont *fcfont = G_TYPE_CHECK_INSTANCE_CAST(pa->font,
92 FT_Face ftface = pango_fc_font_lock_face (fcfont);
93 Box b (Interval (PANGO_LBEARING(logical_rect),
94 PANGO_RBEARING(logical_rect)),
95 Interval (-PANGO_DESCENT(logical_rect),
96 PANGO_ASCENT(logical_rect)));
100 SCM glyph_exprs = SCM_EOL;
101 SCM *tail = &glyph_exprs;
102 for (int i = 0; i < pgs->num_glyphs; i++)
104 PangoGlyphInfo *pgi = pgs->glyphs + i;
106 PangoGlyph pg = pgi->glyph;
107 PangoGlyphGeometry ggeo = pgi->geometry;
109 FT_Get_Glyph_Name (ftface, pg, glyph_name, GLYPH_NAME_LEN);
110 *tail = scm_cons (scm_list_3 (scm_from_double (ggeo.x_offset * scale_),
111 scm_from_double (ggeo.y_offset * scale_),
112 scm_makfrom0str (glyph_name)),
114 tail = SCM_CDRLOC (*tail);
116 PangoFontDescription *descr = pango_font_describe (pa->font);
117 Real size = pango_font_description_get_size (descr)
118 / (Real (PANGO_SCALE)) ;
120 FcPattern *fcpat = fcfont->font_pattern;
122 FcPatternGetString(fcpat, FC_FILE, 0, (FcChar8 **) &filename);
123 char const *ps_name = FT_Get_Postscript_Name (ftface);
124 ((Pango_font *) this)->register_font_file (filename, ps_name);
126 SCM expr = scm_list_4 (ly_symbol2scm ("glyph-string"),
127 scm_makfrom0str (ps_name),
128 scm_from_double (size),
129 ly_quote_scm (glyph_exprs));
131 Stencil item_stencil (b, expr);
133 dest.add_stencil (item_stencil);
135 pango_fc_font_unlock_face (fcfont);
143 Pango_font::sub_fonts () const
149 Pango_font::font_file_name () const
154 LY_DEFINE (ly_pango_font_p, "ly:pango-font?",
157 "Is @var{f} a pango font?")
159 return scm_from_bool (dynamic_cast<Pango_font*> (unsmob_metrics (f)));