]> git.donarmstrong.com Git - lilypond.git/blob - lily/pango-font.cc
* lily/include/main.hh: rename format to backend.
[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 "main.hh"
16 #include "lookup.hh"
17 #include "dimensions.hh"
18 #include "pango-font.hh"
19
20 #if HAVE_PANGO_FT2
21 #include "stencil.hh" 
22
23
24 Pango_font::Pango_font (PangoFT2FontMap *fontmap,
25                         Direction dir,
26                         PangoFontDescription *description)
27 {
28   subfonts_ = SCM_EOL;
29   PangoDirection pango_dir = (dir == RIGHT)
30     ? PANGO_DIRECTION_LTR
31     : PANGO_DIRECTION_RTL;
32   context_ =
33     pango_ft2_get_context (PANGO_RESOLUTION, PANGO_RESOLUTION);
34
35   pango_description_ = pango_font_description_copy (description);
36   //  context_ = pango_ft2_font_map_create_context (fontmap);  
37   attribute_list_= pango_attr_list_new();
38
39   /*
40     urgh. I don't understand this. Why isn't this 1/(scale * resolution)
41
42     --hwn
43    */
44   scale_ = INCH_TO_BP / (Real (PANGO_SCALE) * Real (PANGO_RESOLUTION));
45   
46   pango_context_set_language (context_, pango_language_from_string ("en_US"));
47   pango_context_set_base_dir (context_, pango_dir);
48   pango_context_set_font_description (context_, description);
49 }
50
51 Pango_font::~Pango_font ()
52 {
53   pango_font_description_free (pango_description_);
54   g_object_unref (context_);
55   pango_attr_list_unref (attribute_list_);
56 }
57
58 void
59 Pango_font::register_font_file (String filename, String ps_name) 
60 {
61   subfonts_ = scm_cons (scm_makfrom0str (filename.to_str0 ()),
62                         subfonts_);
63 }
64
65 void
66 Pango_font::derived_mark () const
67 {
68   scm_gc_mark (subfonts_);
69 }
70
71 Stencil
72 Pango_font::pango_item_string_stencil (PangoItem *item, String str, Real dx) const
73 {      
74   const int GLYPH_NAME_LEN = 256;
75   char glyph_name[GLYPH_NAME_LEN];
76   PangoAnalysis *pa = &(item->analysis);
77   PangoGlyphString *pgs = pango_glyph_string_new ();
78
79   pango_shape (str.to_str0 () + item->offset,
80                item->length, pa, pgs);
81
82   PangoRectangle logical_rect;
83   PangoRectangle ink_rect;
84   pango_glyph_string_extents (pgs, pa->font, &ink_rect, &logical_rect);
85       
86   PangoFcFont *fcfont = G_TYPE_CHECK_INSTANCE_CAST(pa->font,
87                                                    PANGO_TYPE_FC_FONT,
88                                                    PangoFcFont);
89       
90   FT_Face ftface = pango_fc_font_lock_face (fcfont);
91   Box b (Interval (PANGO_LBEARING(logical_rect),
92                    PANGO_RBEARING(logical_rect)),
93          Interval (-PANGO_DESCENT(logical_rect),
94                    PANGO_ASCENT(logical_rect)));
95              
96   b.scale (scale_);
97
98   SCM glyph_exprs = SCM_EOL;
99   SCM *tail = &glyph_exprs;
100   for (int i = 0; i < pgs->num_glyphs; i++)
101     {
102       PangoGlyphInfo *pgi = pgs->glyphs + i;
103           
104       PangoGlyph pg = pgi->glyph;
105       PangoGlyphGeometry ggeo = pgi->geometry;
106
107       FT_Get_Glyph_Name (ftface, pg, glyph_name, GLYPH_NAME_LEN);
108       *tail = scm_cons (scm_list_3 (scm_from_double (ggeo.x_offset * scale_ + dx),
109                                     scm_from_double (ggeo.y_offset * scale_),
110                                     scm_makfrom0str (glyph_name)),
111                         SCM_EOL);
112       dx = 0.0;
113       tail = SCM_CDRLOC (*tail);
114     }
115
116   PangoFontDescription *descr = pango_font_describe (pa->font);
117   Real size = pango_font_description_get_size (descr)
118     /  (Real (PANGO_SCALE));
119       
120   FcPattern *fcpat = fcfont->font_pattern;
121   char *filename = 0;
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);
125   pango_fc_font_unlock_face (fcfont);
126       
127   SCM expr = scm_list_4 (ly_symbol2scm ("glyph-string"),
128                          scm_makfrom0str (ps_name),
129                          scm_from_double (size),
130                          ly_quote_scm (glyph_exprs));
131
132   Stencil item_stencil (b, expr);
133
134   return item_stencil;  
135 }
136
137
138 Stencil
139 Pango_font::text_stencil (String str) const
140 {
141   GList *items = pango_itemize (context_,
142                                 str.to_str0 (),
143                                 0, str.length (), attribute_list_,
144                                 NULL);
145   
146   GList *ptr = items;
147   Stencil dest;
148   Real x = 0.0;
149   while (ptr)
150     {
151       PangoItem *item = (PangoItem*) ptr->data;
152
153       Stencil item_stencil = pango_item_string_stencil (item, str, x);
154
155       x = item_stencil.extent (X_AXIS)[RIGHT];
156       
157       dest.add_stencil (item_stencil);
158       
159       ptr = ptr->next;      
160     }
161
162   if (output_backend_global != "ps")
163     {
164       /*
165         For Pango based backends, we take a shortcut.
166        */
167       SCM exp
168         = scm_list_3 (ly_symbol2scm ("utf-8-string"),
169                       scm_makfrom0str (pango_font_description_to_filename (pango_description_)),
170                       scm_makfrom0str (str.to_str0 ()));
171
172
173       return Stencil (dest.extent_box (),
174                       exp);
175     }
176   
177   return dest;
178 }
179
180 SCM
181 Pango_font::sub_fonts () const
182 {
183   return subfonts_;
184 }
185
186 SCM 
187 Pango_font::font_file_name () const
188 {
189   return SCM_BOOL_F;
190 }
191
192 LY_DEFINE (ly_pango_font_p, "ly:pango-font?",
193            1, 0, 0,
194            (SCM f),
195            "Is @var{f} a pango font?")
196 {
197   return scm_from_bool (dynamic_cast<Pango_font*> (unsmob_metrics (f)));
198 }
199
200
201 #endif
202