]> git.donarmstrong.com Git - lilypond.git/blob - lily/pango-font.cc
* lily/pango-font.cc (text_stencil): Quick try at glyph->charcode
[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 "pango-font.hh"
16
17 #if HAVE_PANGO_FT2
18 #include "stencil.hh" 
19
20
21 Pango_font::Pango_font (PangoFT2FontMap *fontmap,
22                         int resolution,
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_ = pango_ft2_font_map_create_context (fontmap);  
31   attribute_list_= pango_attr_list_new();
32   scale_ = 1.0 / (PANGO_SCALE * resolution * 72.27);
33   
34   pango_context_set_language (context_, pango_language_from_string ("en_US"));
35   pango_context_set_base_dir (context_, pango_dir);
36   pango_context_set_font_description (context_, description);
37 }
38
39 Pango_font::~Pango_font ()
40 {
41   g_object_unref (context_);
42   pango_attr_list_unref (attribute_list_);
43 }
44
45 void
46 Pango_font::register_font_file (String filename, String ps_name) 
47 {
48   subfonts_ = scm_cons (//scm_cons (scm_makfrom0str (ps_name.to_str0 ()),
49                         scm_makfrom0str (filename.to_str0 ()),
50                         subfonts_);
51 }
52
53 void
54 Pango_font::derived_mark () const
55 {
56   scm_gc_mark (subfonts_);
57 }
58
59 Stencil
60 Pango_font::text_stencil (String str) const
61 {
62   GList *items = pango_itemize (context_,
63                                 str.to_str0 (),
64                                 0, str.length (), attribute_list_,
65                                 NULL);
66
67   const int GLYPH_NAME_LEN = 256;
68   char glyph_name[GLYPH_NAME_LEN];
69   
70   GList *ptr = items;
71   Stencil dest;  
72   while (ptr)
73     {
74
75       // FIXME: factor this out
76       PangoItem *item = (PangoItem*) ptr->data;
77       PangoAnalysis *pa = &(item->analysis);
78       PangoGlyphString *pgs = pango_glyph_string_new ();
79
80       pango_shape (str.to_str0 (), str.length (), pa, pgs);
81
82       PangoRectangle logical_rect;
83       pango_glyph_string_extents (pgs, pa->font, NULL, &logical_rect);
84       
85       PangoFcFont *fcfont = G_TYPE_CHECK_INSTANCE_CAST(pa->font,
86                                                        PANGO_TYPE_FC_FONT,
87                                                        PangoFcFont);
88       FT_Face ftface = pango_fc_font_lock_face (fcfont);
89       Box b (Interval (0, logical_rect.width),
90              Interval (0, logical_rect.height));
91
92       if (!face_)
93         {
94           /* FIXME.  This obvious shortcut apparently does not work.
95              It seems there are different faces per text string and a
96              map of face_ and charcode mapping is needed.  */
97           Pango_font *barf = (Pango_font*) this;
98           barf->face_ = ftface;
99           barf->index_to_charcode_map_ = make_index_to_charcode_map (face_);
100         }
101
102       b.translate (Offset (- logical_rect.x, -logical_rect.y));
103       
104       b.scale (scale_);
105
106       SCM glyph_exprs = SCM_EOL;
107       SCM *tail = &glyph_exprs;
108       for (int i = 0; i < pgs->num_glyphs; i++)
109         {
110           PangoGlyphInfo *pgi = pgs->glyphs + i;
111           
112           PangoGlyph pg = pgi->glyph;
113           PangoGlyphGeometry ggeo = pgi->geometry;
114           
115           FT_Get_Glyph_Name (ftface, pg, glyph_name, GLYPH_NAME_LEN);
116           *tail = scm_cons (scm_list_3 (scm_from_double (ggeo.x_offset * scale_),
117                                         scm_from_double (ggeo.y_offset * scale_),
118                                         scm_makfrom0str (glyph_name)),
119                             SCM_EOL);
120           tail = SCM_CDRLOC (*tail);
121         }
122
123       FcPattern *fcpat = fcfont->font_pattern;
124       char *filename = 0;
125       FcPatternGetString(fcpat, FC_FILE, 0, (FcChar8 **) &filename);
126       char const *ps_name = FT_Get_Postscript_Name (ftface);
127       ((Pango_font *) this)->register_font_file (filename, ps_name);
128       
129       SCM expr = scm_list_4 (ly_symbol2scm ("glyph-string"),
130                              self_scm (),
131                              scm_makfrom0str (ps_name),
132                              ly_quote_scm (glyph_exprs));
133
134       Stencil item_stencil (b, expr);
135
136       dest.add_stencil (item_stencil);
137       
138       pango_fc_font_unlock_face (fcfont);
139       ptr = ptr->next;      
140     }
141
142   return dest;
143 }
144
145 SCM
146 Pango_font::sub_fonts () const
147 {
148   return subfonts_;
149 }
150
151 SCM 
152 Pango_font::font_file_name () const
153 {
154   return SCM_BOOL_F;
155 }
156
157 int
158 Pango_font::name_to_index (String nm) const
159 {
160   char *nm_str = (char*) nm.to_str0 ();
161   if (int idx = FT_Get_Name_Index (face_, nm_str))
162     return idx;
163   return -1;
164 }
165
166 unsigned
167 Pango_font::index_to_charcode (int i) const
168 {
169   return ((Pango_font*) this)->index_to_charcode_map_[i];
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