]> git.donarmstrong.com Git - lilypond.git/blob - lily/pango-select.cc
* input/regression/font-name.ly: show Pango fonts for
[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_chain_assoc_get (ly_symbol2scm ("font-size"), chain, SCM_BOOL_F),
44                                  0.0);
45   Real size = text_size * pow (2.0, step / 6.0);
46
47   pango_font_description_set_size (description,
48                                    gint (size * PANGO_SCALE));
49   return description;
50 }
51
52 Font_metric *
53 select_pango_font (Output_def *layout, SCM chain)
54 {
55   PangoFontDescription *pfd
56     = properties_to_pango_description (chain,
57                                        point_constant
58                                        * layout->get_dimension (ly_symbol2scm ("text-font-size")));
59
60   char *str = pango_font_description_to_string (pfd);
61   SCM scm_str = scm_makfrom0str (str);
62   g_free (str);
63
64   return find_pango_font (layout, scm_str, 1.0);
65 }
66
67 PangoStyle
68 symbol_to_pango_style (SCM style)
69 {
70   PangoStyle pstyle = PANGO_STYLE_NORMAL;
71   if (style == ly_symbol2scm ("italic"))
72     {
73       pstyle = PANGO_STYLE_ITALIC;
74     }
75   else if (style == ly_symbol2scm ("oblique")
76            || style == ly_symbol2scm ("slanted"))
77     {
78       pstyle = PANGO_STYLE_OBLIQUE;
79     }
80
81   return pstyle;
82 }
83
84 PangoVariant
85 symbol_to_pango_variant (SCM variant)
86 {
87   PangoVariant pvariant = PANGO_VARIANT_NORMAL;
88   if (variant == ly_symbol2scm ("caps"))
89     {
90       pvariant = PANGO_VARIANT_SMALL_CAPS;
91     }
92   return pvariant;
93 }
94
95 PangoWeight
96 symbol_to_pango_weight (SCM weight)
97 {
98   PangoWeight pw = PANGO_WEIGHT_NORMAL;
99   if (weight == ly_symbol2scm ("bold"))
100     {
101       pw = PANGO_WEIGHT_BOLD;
102     }
103   if (weight == ly_symbol2scm ("heavy"))
104     {
105       pw = PANGO_WEIGHT_HEAVY;
106     }
107   if (weight == ly_symbol2scm ("ultrabold"))
108     {
109       pw = PANGO_WEIGHT_ULTRABOLD;
110     }
111   if (weight == ly_symbol2scm ("light"))
112     {
113       pw = PANGO_WEIGHT_LIGHT;
114     }
115   if (weight == ly_symbol2scm ("ultralight"))
116     {
117       pw = PANGO_WEIGHT_ULTRALIGHT;
118     }
119
120   return pw;
121 }
122
123 PangoStretch
124 symbol_to_pango_stretch (SCM) //  stretch)
125 {
126   PangoStretch ps = PANGO_STRETCH_NORMAL;
127
128   /*
129   // TODO
130
131   PANGO_STRETCH_ULTRA_CONDENSED,
132   PANGO_STRETCH_EXTRA_CONDENSED,
133   PANGO_STRETCH_CONDENSED,
134   PANGO_STRETCH_SEMI_CONDENSED,
135
136   PANGO_STRETCH_SEMI_EXPANDED,
137   PANGO_STRETCH_EXPANDED,
138   PANGO_STRETCH_EXTRA_EXPANDED,
139   PANGO_STRETCH_ULTRA_EXPANDED
140   */
141   return ps;
142 }
143
144 PangoFontDescription *
145 symbols_to_pango_font_description (SCM family,
146                                    SCM style,
147                                    SCM variant,
148                                    SCM weight,
149                                    SCM stretch)
150 {
151   PangoFontDescription *description = pango_font_description_new ();
152
153   String family_str = "roman";
154   if (scm_is_symbol (family))
155     family_str = ly_symbol2string (family);
156   else if (scm_is_string (family))
157     family_str = ly_scm2string (family);
158
159   pango_font_description_set_family (description,
160                                      family_str.to_str0 ());
161   pango_font_description_set_style (description,
162                                     symbol_to_pango_style (style));
163   pango_font_description_set_variant (description,
164                                       symbol_to_pango_variant (variant));
165   pango_font_description_set_weight (description,
166                                      symbol_to_pango_weight (weight));
167   pango_font_description_set_stretch (description,
168                                       symbol_to_pango_stretch (stretch));
169
170   return description;
171 }