]> git.donarmstrong.com Git - lilypond.git/blob - lily/pango-font.cc
* flower/file-path.cc (find): try to open directly as well, so we
[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
40 Pango_font::~Pango_font ()
41 {
42   g_object_unref (context_);
43   pango_attr_list_unref (attribute_list_);
44 }
45
46 void
47 Pango_font::register_font_file (String filename, String ps_name) 
48 {
49   subfonts_ = scm_cons (//scm_cons (scm_makfrom0str (ps_name.to_str0 ()),
50                         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 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       PangoItem *item = (PangoItem*) ptr->data;
75       PangoAnalysis *pa = &(item->analysis);
76       PangoGlyphString *pgs = pango_glyph_string_new();
77
78       pango_shape (str.to_str0 (), str.length (), pa, pgs);
79
80       PangoRectangle logical_rect;
81       pango_glyph_string_extents (pgs, pa->font, NULL, &logical_rect);
82       
83       PangoFcFont * fcfont = G_TYPE_CHECK_INSTANCE_CAST(pa->font,
84                                                         PANGO_TYPE_FC_FONT,
85                                                         PangoFcFont);
86       FT_Face ftface = pango_fc_font_lock_face (fcfont);
87       Box b (Interval (0, logical_rect.width),
88              Interval (0, logical_rect.height));
89
90       b.translate (Offset (- logical_rect.x, -logical_rect.y));
91       
92       b.scale (scale_);
93
94       SCM glyph_exprs = SCM_EOL;
95       SCM *tail = &glyph_exprs;
96       for (int i = 0; i < pgs->num_glyphs; i++)
97         {
98           PangoGlyphInfo *pgi = pgs->glyphs + i;
99           
100           PangoGlyph pg = pgi->glyph;
101           PangoGlyphGeometry ggeo = pgi->geometry;
102           
103           FT_Get_Glyph_Name (ftface, pg, glyph_name, GLYPH_NAME_LEN);
104           *tail = scm_cons (scm_list_3 (scm_from_double (ggeo.x_offset * scale_),
105                                         scm_from_double (ggeo.y_offset * scale_),
106                                         scm_makfrom0str (glyph_name)),
107                             SCM_EOL);
108           tail = SCM_CDRLOC(*tail);
109         }
110
111       FcPattern *fcpat = fcfont->font_pattern;
112       char *filename = 0;
113       FcPatternGetString(fcpat, FC_FILE, 0, (FcChar8 **) &filename);
114       char const *ps_name = FT_Get_Postscript_Name (ftface);
115       ((Pango_font *) this)->register_font_file (filename, ps_name);
116       
117       SCM expr = scm_list_3 (ly_symbol2scm ("glyph-string"),
118                              scm_makfrom0str (ps_name),
119                              ly_quote_scm (glyph_exprs));
120
121       Stencil item_stencil (b, expr);
122
123       dest.add_stencil (item_stencil);
124       
125       pango_fc_font_unlock_face (fcfont);
126       ptr = ptr->next;      
127     }
128
129   return dest;
130 }
131
132 SCM
133 Pango_font::sub_fonts () const
134 {
135   return subfonts_;
136 }
137
138 SCM 
139 Pango_font::font_file_name () const
140 {
141   return SCM_BOOL_F;
142 }
143
144 LY_DEFINE(ly_pango_font_p, "ly:pango-font?",
145           1,0,0,
146           (SCM f),
147           "Is @var{f} a pango font?")
148
149 {
150   return scm_from_bool (dynamic_cast<Pango_font*> (unsmob_metrics (f)));
151 }
152
153 #endif