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