]> git.donarmstrong.com Git - lilypond.git/blob - lily/pango-font.cc
* scm/framework-ps.scm (output-variables): separately scale the
[lilypond.git] / lily / pango-font.cc
1 /*
2   pango-font.cc --  implement Pango_font
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004 Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8 */
9
10
11 #define PANGO_ENABLE_BACKEND // ugh, why necessary?
12
13 #include <pango/pangoft2.h>
14
15 #include "lookup.hh"
16 #include "dimensions.hh"
17 #include "pango-font.hh"
18
19 #if HAVE_PANGO_FT2
20 #include "stencil.hh" 
21
22
23 Pango_font::Pango_font (PangoFT2FontMap *fontmap,
24                         Direction dir,
25                         PangoFontDescription *description)
26 {
27   subfonts_ = SCM_EOL;
28   PangoDirection pango_dir = (dir == RIGHT)
29     ? PANGO_DIRECTION_LTR
30     : PANGO_DIRECTION_RTL;
31   context_ =
32     pango_ft2_get_context (PANGO_RESOLUTION, PANGO_RESOLUTION);
33   //  context_ = pango_ft2_font_map_create_context (fontmap);  
34   attribute_list_= pango_attr_list_new();
35
36   /*
37     urgh. I don't understand this. Why isn't this 1/(scale * resolution)
38
39     --hwn
40    */
41   scale_ = INCH_TO_BP / (Real (PANGO_SCALE) * Real (PANGO_RESOLUTION));
42   
43   pango_context_set_language (context_, pango_language_from_string ("en_US"));
44   pango_context_set_base_dir (context_, pango_dir);
45   pango_context_set_font_description (context_, description);
46 }
47
48 Pango_font::~Pango_font ()
49 {
50   g_object_unref (context_);
51   pango_attr_list_unref (attribute_list_);
52 }
53
54 void
55 Pango_font::register_font_file (String filename, String ps_name) 
56 {
57   subfonts_ = scm_cons (scm_makfrom0str (filename.to_str0 ()),
58                         subfonts_);
59 }
60
61 void
62 Pango_font::derived_mark () const
63 {
64   scm_gc_mark (subfonts_);
65 }
66
67 Stencil
68 Pango_font::pango_item_string_stencil (PangoItem *item, String str, Real dx) const
69 {      
70   const int GLYPH_NAME_LEN = 256;
71   char glyph_name[GLYPH_NAME_LEN];
72   PangoAnalysis *pa = &(item->analysis);
73   PangoGlyphString *pgs = pango_glyph_string_new ();
74
75   pango_shape (str.to_str0 () + item->offset,
76                item->length, pa, pgs);
77
78   PangoRectangle logical_rect;
79   PangoRectangle ink_rect;
80   pango_glyph_string_extents (pgs, pa->font, &ink_rect, &logical_rect);
81       
82   PangoFcFont *fcfont = G_TYPE_CHECK_INSTANCE_CAST(pa->font,
83                                                    PANGO_TYPE_FC_FONT,
84                                                    PangoFcFont);
85       
86   FT_Face ftface = pango_fc_font_lock_face (fcfont);
87   Box b (Interval (PANGO_LBEARING(logical_rect),
88                    PANGO_RBEARING(logical_rect)),
89          Interval (-PANGO_DESCENT(logical_rect),
90                    PANGO_ASCENT(logical_rect)));
91              
92   b.scale (scale_);
93
94   SCM glyph_exprs = SCM_EOL;
95   SCM *tail = &glyph_exprs;
96   for (int i = 0; i < pgs->num_glyphs; i++)
97     {
98       PangoGlyphInfo *pgi = pgs->glyphs + i;
99           
100       PangoGlyph pg = pgi->glyph;
101       PangoGlyphGeometry ggeo = pgi->geometry;
102
103       FT_Get_Glyph_Name (ftface, pg, glyph_name, GLYPH_NAME_LEN);
104       *tail = scm_cons (scm_list_3 (scm_from_double (ggeo.x_offset * scale_ + dx),
105                                     scm_from_double (ggeo.y_offset * scale_),
106                                     scm_makfrom0str (glyph_name)),
107                         SCM_EOL);
108       dx = 0.0;
109       tail = SCM_CDRLOC (*tail);
110     }
111
112   PangoFontDescription *descr = pango_font_describe (pa->font);
113   Real size = pango_font_description_get_size (descr)
114     /  (Real (PANGO_SCALE));
115       
116   FcPattern *fcpat = fcfont->font_pattern;
117   char *filename = 0;
118   FcPatternGetString(fcpat, FC_FILE, 0, (FcChar8 **) &filename);
119   char const *ps_name = FT_Get_Postscript_Name (ftface);
120   ((Pango_font *) this)->register_font_file (filename, ps_name);
121   pango_fc_font_unlock_face (fcfont);
122       
123   SCM expr = scm_list_4 (ly_symbol2scm ("glyph-string"),
124                          scm_makfrom0str (ps_name),
125                          scm_from_double (size),
126                          ly_quote_scm (glyph_exprs));
127
128   Stencil item_stencil (b, expr);
129
130   return item_stencil;  
131 }
132
133 Stencil
134 Pango_font::text_stencil (String str) const
135 {
136   GList *items = pango_itemize (context_,
137                                 str.to_str0 (),
138                                 0, str.length (), attribute_list_,
139                                 NULL);
140   
141   GList *ptr = items;
142   Stencil dest;
143   Real x = 0.0;
144   while (ptr)
145     {
146       PangoItem *item = (PangoItem*) ptr->data;
147
148       Stencil item_stencil = pango_item_string_stencil (item, str, x);
149
150       x = item_stencil.extent (X_AXIS)[RIGHT];
151       
152       dest.add_stencil (item_stencil);
153       
154       ptr = ptr->next;      
155     }
156
157   return dest;
158 }
159
160 SCM
161 Pango_font::sub_fonts () const
162 {
163   return subfonts_;
164 }
165
166 SCM 
167 Pango_font::font_file_name () const
168 {
169   return SCM_BOOL_F;
170 }
171
172 LY_DEFINE (ly_pango_font_p, "ly:pango-font?",
173            1, 0, 0,
174            (SCM f),
175            "Is @var{f} a pango font?")
176 {
177   return scm_from_bool (dynamic_cast<Pango_font*> (unsmob_metrics (f)));
178 }
179
180
181 #endif
182