]> git.donarmstrong.com Git - lilypond.git/blob - lily/pango-select.cc
* flower
[lilypond.git] / lily / pango-select.cc
1 /*
2   pango-select.cc -- implement lily font selection for Pango_fonts.
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2004--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8 #include <math.h>
9
10 #include "dimensions.hh"
11 #include "all-font-metrics.hh"
12 #include "output-def.hh"
13 #include "pango-font.hh"
14
15 PangoFontDescription *
16 properties_to_pango_description (SCM chain, Real text_size)
17 {
18   SCM name = ly_chain_assoc (ly_symbol2scm ("font-name"), chain);
19
20   PangoFontDescription *description = 0;
21   if (scm_is_pair (name) && scm_is_string (scm_cdr (name)))
22     {
23       String name_str = ly_scm2string (scm_cdr (name));
24       description = pango_font_description_from_string (name_str.to_str0 ());
25     }
26   else
27     {
28       SCM family = ly_chain_assoc_get (ly_symbol2scm ("font-family"), chain,
29                                        SCM_BOOL_F);
30       SCM variant = ly_chain_assoc_get (ly_symbol2scm ("font-shape"), chain,
31                                         SCM_BOOL_F);
32
33       SCM style = ly_chain_assoc_get (ly_symbol2scm ("font-shape"), chain,
34                                       SCM_BOOL_F);
35       SCM weight = ly_chain_assoc_get (ly_symbol2scm ("font-series"), chain,
36                                        SCM_BOOL_F);
37
38       description
39         = symbols_to_pango_font_description (family, style, variant, weight,
40                                              SCM_BOOL_F);
41     }
42
43   Real step = robust_scm2double (ly_symbol2scm ("font-size"), 0.0);
44   Real size = text_size * pow (2.0, step / 6.0);
45
46   pango_font_description_set_size (description,
47                                    gint (size * PANGO_SCALE));
48   return description;
49 }
50
51 Font_metric *
52 select_pango_font (Output_def *layout, SCM chain)
53 {
54   PangoFontDescription *pfd = properties_to_pango_description (chain,
55                                                                point_constant * layout->get_dimension (ly_symbol2scm ("text-font-size")));
56
57   char *str = pango_font_description_to_string (pfd);
58   SCM scm_str = scm_makfrom0str (str);
59   g_free (str);
60
61   return find_pango_font (layout, scm_str, 1.0);
62 }
63
64 PangoStyle
65 symbol_to_pango_style (SCM style)
66 {
67   PangoStyle pstyle = PANGO_STYLE_NORMAL;
68   if (style == ly_symbol2scm ("italic"))
69     {
70       pstyle = PANGO_STYLE_ITALIC;
71     }
72   else if (style == ly_symbol2scm ("oblique")
73            || style == ly_symbol2scm ("slanted"))
74     {
75       pstyle = PANGO_STYLE_OBLIQUE;
76     }
77
78   return pstyle;
79 }
80
81 PangoVariant
82 symbol_to_pango_variant (SCM variant)
83 {
84   PangoVariant pvariant = PANGO_VARIANT_NORMAL;
85   if (variant == ly_symbol2scm ("caps"))
86     {
87       pvariant = PANGO_VARIANT_SMALL_CAPS;
88     }
89   return pvariant;
90 }
91
92 PangoWeight
93 symbol_to_pango_weight (SCM weight)
94 {
95   PangoWeight pw = PANGO_WEIGHT_NORMAL;
96   if (weight == ly_symbol2scm ("bold"))
97     {
98       pw = PANGO_WEIGHT_BOLD;
99     }
100   if (weight == ly_symbol2scm ("heavy"))
101     {
102       pw = PANGO_WEIGHT_HEAVY;
103     }
104   if (weight == ly_symbol2scm ("ultrabold"))
105     {
106       pw = PANGO_WEIGHT_ULTRABOLD;
107     }
108   if (weight == ly_symbol2scm ("light"))
109     {
110       pw = PANGO_WEIGHT_LIGHT;
111     }
112   if (weight == ly_symbol2scm ("ultralight"))
113     {
114       pw = PANGO_WEIGHT_ULTRALIGHT;
115     }
116
117   return pw;
118 }
119
120 PangoStretch
121 symbol_to_pango_stretch (SCM) //  stretch)
122 {
123   PangoStretch ps = PANGO_STRETCH_NORMAL;
124
125   /*
126   // TODO
127
128   PANGO_STRETCH_ULTRA_CONDENSED,
129   PANGO_STRETCH_EXTRA_CONDENSED,
130   PANGO_STRETCH_CONDENSED,
131   PANGO_STRETCH_SEMI_CONDENSED,
132
133   PANGO_STRETCH_SEMI_EXPANDED,
134   PANGO_STRETCH_EXPANDED,
135   PANGO_STRETCH_EXTRA_EXPANDED,
136   PANGO_STRETCH_ULTRA_EXPANDED
137   */
138   return ps;
139 }
140
141 PangoFontDescription *
142 symbols_to_pango_font_description (SCM family,
143                                    SCM style,
144                                    SCM variant,
145                                    SCM weight,
146                                    SCM stretch)
147 {
148   PangoFontDescription *description = pango_font_description_new ();
149
150   String family_str = "roman";
151   if (scm_is_symbol (family))
152     family_str = ly_symbol2string (family);
153   else if (scm_is_string (family))
154     family_str = ly_scm2string (family);
155
156   pango_font_description_set_family (description,
157                                      family_str.to_str0 ());
158   pango_font_description_set_style (description,
159                                     symbol_to_pango_style (style));
160   pango_font_description_set_variant (description,
161                                       symbol_to_pango_variant (variant));
162   pango_font_description_set_weight (description,
163                                      symbol_to_pango_weight (weight));
164   pango_font_description_set_stretch (description,
165                                       symbol_to_pango_stretch (stretch));
166
167   return description;
168 }