]> git.donarmstrong.com Git - lilypond.git/blob - lily/pango-select.cc
31f9f79e772eed906cd3de3c116b1cd2a177875a
[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_get (ly_symbol2scm ("font-name"), chain, SCM_BOOL_F);
19
20   PangoFontDescription *description = 0;
21   if (scm_is_string (name))
22     {
23       String name_str = ly_scm2string (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     pstyle = PANGO_STYLE_ITALIC;
73   else if (style == ly_symbol2scm ("oblique")
74            || style == ly_symbol2scm ("slanted"))
75     pstyle = PANGO_STYLE_OBLIQUE;
76
77   return pstyle;
78 }
79
80 PangoVariant
81 symbol_to_pango_variant (SCM variant)
82 {
83   PangoVariant pvariant = PANGO_VARIANT_NORMAL;
84   if (variant == ly_symbol2scm ("caps"))
85     pvariant = PANGO_VARIANT_SMALL_CAPS;
86   return pvariant;
87 }
88
89 PangoWeight
90 symbol_to_pango_weight (SCM weight)
91 {
92   PangoWeight pw = PANGO_WEIGHT_NORMAL;
93   if (weight == ly_symbol2scm ("bold"))
94     pw = PANGO_WEIGHT_BOLD;
95   if (weight == ly_symbol2scm ("heavy"))
96     pw = PANGO_WEIGHT_HEAVY;
97   if (weight == ly_symbol2scm ("ultrabold"))
98     pw = PANGO_WEIGHT_ULTRABOLD;
99   if (weight == ly_symbol2scm ("light"))
100     pw = PANGO_WEIGHT_LIGHT;
101   if (weight == ly_symbol2scm ("ultralight"))
102     pw = PANGO_WEIGHT_ULTRALIGHT;
103
104   return pw;
105 }
106
107 PangoStretch
108 symbol_to_pango_stretch (SCM) //  stretch)
109 {
110   PangoStretch ps = PANGO_STRETCH_NORMAL;
111
112   /*
113   // TODO
114
115   PANGO_STRETCH_ULTRA_CONDENSED,
116   PANGO_STRETCH_EXTRA_CONDENSED,
117   PANGO_STRETCH_CONDENSED,
118   PANGO_STRETCH_SEMI_CONDENSED,
119
120   PANGO_STRETCH_SEMI_EXPANDED,
121   PANGO_STRETCH_EXPANDED,
122   PANGO_STRETCH_EXTRA_EXPANDED,
123   PANGO_STRETCH_ULTRA_EXPANDED
124   */
125   return ps;
126 }
127
128 PangoFontDescription *
129 symbols_to_pango_font_description (SCM family,
130                                    SCM style,
131                                    SCM variant,
132                                    SCM weight,
133                                    SCM stretch)
134 {
135   PangoFontDescription *description = pango_font_description_new ();
136
137   String family_str = "roman";
138   if (scm_is_symbol (family))
139     family_str = ly_symbol2string (family);
140   else if (scm_is_string (family))
141     family_str = ly_scm2string (family);
142
143   pango_font_description_set_family (description,
144                                      family_str.to_str0 ());
145   pango_font_description_set_style (description,
146                                     symbol_to_pango_style (style));
147   pango_font_description_set_variant (description,
148                                       symbol_to_pango_variant (variant));
149   pango_font_description_set_weight (description,
150                                      symbol_to_pango_weight (weight));
151   pango_font_description_set_stretch (description,
152                                       symbol_to_pango_stretch (stretch));
153
154   return description;
155 }