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>
10 #include <pango/pangoft2.h>
12 #include "pango-font.hh"
16 symbol_to_pango_style (SCM style)
18 PangoStyle pstyle = PANGO_STYLE_NORMAL;
19 if (style == ly_symbol2scm ("italic"))
21 pstyle = PANGO_STYLE_NORMAL;
23 else if (style == ly_symbol2scm ("oblique")
24 || style == ly_symbol2scm ("slanted")
27 pstyle = PANGO_STYLE_OBLIQUE;
34 symbol_to_pango_variant (SCM variant)
36 PangoVariant pvariant;
37 if (variant == ly_symbol2scm ("caps"))
39 pvariant = PANGO_VARIANT_SMALL_CAPS;
46 symbol_to_pango_weight (SCM weight)
48 PangoWeight pw = PANGO_WEIGHT_NORMAL;
49 if (weight == ly_symbol2scm ("bold"))
51 pw = PANGO_WEIGHT_BOLD;
53 if (weight == ly_symbol2scm ("heavy"))
55 pw = PANGO_WEIGHT_HEAVY;
57 if (weight == ly_symbol2scm ("ultrabold"))
59 pw = PANGO_WEIGHT_ULTRABOLD;
61 if (weight == ly_symbol2scm ("light"))
63 pw = PANGO_WEIGHT_LIGHT;
65 if (weight == ly_symbol2scm ("ultralight"))
67 pw = PANGO_WEIGHT_ULTRALIGHT;
74 symbol_to_pango_stretch (SCM stretch)
76 PangoStretch ps = PANGO_STRETCH_NORMAL;
81 PANGO_STRETCH_ULTRA_CONDENSED,
82 PANGO_STRETCH_EXTRA_CONDENSED,
83 PANGO_STRETCH_CONDENSED,
84 PANGO_STRETCH_SEMI_CONDENSED,
86 PANGO_STRETCH_SEMI_EXPANDED,
87 PANGO_STRETCH_EXPANDED,
88 PANGO_STRETCH_EXTRA_EXPANDED,
89 PANGO_STRETCH_ULTRA_EXPANDED
97 symbols_to_pango_font_description(SCM family,
104 PangoFontDescription * description = pango_font_description_new ();
106 pango_font_description_set_family (description,
107 ly_symbol2string (family).to_str0 ());
108 pango_font_description_set_style (description,
109 symbol_to_pango_style (style));
110 pango_font_description_set_variant (description,
111 symbol_to_pango_variant (variant));
112 pango_font_description_set_weight (description,
113 symbol_to_pango_weight (weight));
114 pango_font_description_set_stretch (description,
115 symbol_to_pango_stretch (stretch));
116 pango_font_description_set_size (description,
117 gint (scm_to_double (size) * PANGO_SCALE));
122 Pango_font::Pango_font (PangoFT2FontMap *fontmap,
125 PangoFontDescription * description)
127 PangoDirection pango_dir = (dir==RIGHT)
128 ? PANGO_DIRECTION_LTR
129 : PANGO_DIRECTION_RTL;
130 context_ = pango_ft2_font_map_create_context (fontmap);
131 attribute_list_= pango_attr_list_new();
132 scale_ = PANGO_SCALE * resolution * 72.27;
134 pango_context_set_language (context_, pango_language_from_string ("en_US"));
135 pango_context_set_base_dir (context_, pango_dir);
136 pango_context_set_font_description (context_, description);
139 Pango_font::~Pango_font ()
141 g_object_unref (context_);
142 pango_attr_list_unref (attribute_list_);
147 Pango_font::text_stencil (String str) const
149 GList * items = pango_itemize (context_,
151 0, str.length (), attribute_list_,
154 const int GLYPH_NAME_LEN = 256;
155 char glyph_name[GLYPH_NAME_LEN];
158 dest_extent.set_empty ();
160 SCM glyph_exprs = SCM_EOL;
163 PangoItem *item = (PangoItem*) ptr->data;
164 PangoAnalysis *pa = &(item->analysis);
165 PangoGlyphString *pgs = pango_glyph_string_new();
166 pango_shape (str.to_str0 (), str.length (), pa, pgs);
168 PangoRectangle logical_rect;
169 pango_glyph_string_extents (pgs, pa->font, NULL, &logical_rect);
171 PangoFcFont * fcfont = G_TYPE_CHECK_INSTANCE_CAST(pa->font,
174 FT_Face ftface = pango_fc_font_lock_face (fcfont);
175 Box b (Interval (0, logical_rect.width),
176 Interval (0, logical_rect.height));
178 b.translate (Offset (- logical_rect.x, -logical_rect.y));
181 dest_extent.unite (b);
183 for (int i = 0; i < pgs->num_glyphs; i++)
185 PangoGlyphInfo *pgi = pgs->glyphs + i;
187 PangoGlyph pg = pgi->glyph;
188 PangoGlyphGeometry ggeo = pgi->geometry;
190 FT_Get_Glyph_Name (ftface, pg, glyph_name, GLYPH_NAME_LEN);
191 glyph_exprs = scm_cons (scm_list_5 (ly_symbol2scm ("named-ps-glyph"),
192 scm_from_int (ggeo.x_offset),
193 scm_from_int (ggeo.y_offset),
194 scm_makfrom0str (glyph_name),
195 scm_makfrom0str (FT_Get_Postscript_Name (ftface))),
202 Stencil dest (dest_extent,
203 scm_cons (ly_symbol2scm ("combine-stencil"),