]> git.donarmstrong.com Git - lilypond.git/blob - lily/pango-font.cc
* scm/paper.scm (layout-set-staff-size): use text-font-size in
[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 "dimensions.hh"
16 #include "pango-font.hh"
17
18 #if HAVE_PANGO_FT2
19 #include "stencil.hh" 
20
21
22 Pango_font::Pango_font (PangoFT2FontMap *fontmap,
23                         Direction dir,
24                         PangoFontDescription *description)
25 {
26   subfonts_ = SCM_EOL;
27   PangoDirection pango_dir = (dir == RIGHT)
28     ? PANGO_DIRECTION_LTR
29     : PANGO_DIRECTION_RTL;
30   context_ =
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));
35   
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);
39 }
40
41 Pango_font::~Pango_font ()
42 {
43   g_object_unref (context_);
44   pango_attr_list_unref (attribute_list_);
45 }
46
47 void
48 Pango_font::register_font_file (String filename, String ps_name) 
49 {
50   subfonts_ = scm_cons (scm_makfrom0str (filename.to_str0 ()),
51                         subfonts_);
52 }
53
54 void
55 Pango_font::derived_mark () const
56 {
57   scm_gc_mark (subfonts_);
58 }
59
60 Stencil
61 Pango_font::text_stencil (String str) const
62 {
63   GList *items = pango_itemize (context_,
64                                 str.to_str0 (),
65                                 0, str.length (), attribute_list_,
66                                 NULL);
67
68   const int GLYPH_NAME_LEN = 256;
69   char glyph_name[GLYPH_NAME_LEN];
70   
71   GList *ptr = items;
72   Stencil dest;  
73   while (ptr)
74     {
75
76       // FIXME: factor this out
77       PangoItem *item = (PangoItem*) ptr->data;
78       PangoAnalysis *pa = &(item->analysis);
79       PangoGlyphString *pgs = pango_glyph_string_new ();
80
81       pango_shape (str.to_str0 () + item->offset,
82                    item->length, pa, pgs);
83
84       PangoRectangle logical_rect;
85       PangoRectangle ink_rect;
86       pango_glyph_string_extents (pgs, pa->font, &ink_rect, &logical_rect);
87       
88       PangoFcFont *fcfont = G_TYPE_CHECK_INSTANCE_CAST(pa->font,
89                                                        PANGO_TYPE_FC_FONT,
90                                                        PangoFcFont);
91       
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)));
97              
98       b.scale (scale_);
99
100       SCM glyph_exprs = SCM_EOL;
101       SCM *tail = &glyph_exprs;
102       for (int i = 0; i < pgs->num_glyphs; i++)
103         {
104           PangoGlyphInfo *pgi = pgs->glyphs + i;
105           
106           PangoGlyph pg = pgi->glyph;
107           PangoGlyphGeometry ggeo = pgi->geometry;
108
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)),
113                             SCM_EOL);
114           tail = SCM_CDRLOC (*tail);
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       
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));
130
131       Stencil item_stencil (b, expr);
132
133       dest.add_stencil (item_stencil);
134       
135       pango_fc_font_unlock_face (fcfont);
136       ptr = ptr->next;      
137     }
138
139   return dest;
140 }
141
142 SCM
143 Pango_font::sub_fonts () const
144 {
145   return subfonts_;
146 }
147
148 SCM 
149 Pango_font::font_file_name () const
150 {
151   return SCM_BOOL_F;
152 }
153
154 LY_DEFINE (ly_pango_font_p, "ly:pango-font?",
155            1, 0, 0,
156            (SCM f),
157            "Is @var{f} a pango font?")
158 {
159   return scm_from_bool (dynamic_cast<Pango_font*> (unsmob_metrics (f)));
160 }
161
162
163 #endif
164