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