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