]> git.donarmstrong.com Git - lilypond.git/blob - lily/pango-select.cc
new file.
[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 */
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
17 PangoFontDescription *
18 properties_to_pango_description (SCM chain, Real text_size)
19 {
20   SCM name = ly_chain_assoc (ly_symbol2scm ("font-name"), chain);
21
22   PangoFontDescription *description = 0;
23   if (scm_is_pair (name) && scm_is_string (scm_cdr (name)))
24     {
25       String name_str = ly_scm2string (scm_cdr (name));
26       description = pango_font_description_from_string (name_str.to_str0 ());
27     }
28   else
29     {
30       SCM family = ly_chain_assoc_get (ly_symbol2scm ("font-family"), chain,
31                                        SCM_BOOL_F);
32       SCM variant = ly_chain_assoc_get (ly_symbol2scm ("font-shape"), chain,
33                                         SCM_BOOL_F);
34         
35       SCM style = ly_chain_assoc_get (ly_symbol2scm ("font-shape"), chain,
36                                       SCM_BOOL_F);
37       SCM weight = ly_chain_assoc_get (ly_symbol2scm ("font-series"), chain,
38                                        SCM_BOOL_F);
39       
40       description
41         = symbols_to_pango_font_description (family, style, variant, weight,
42                                              SCM_BOOL_F);
43     }
44
45   Real step = robust_scm2double (ly_symbol2scm ("font-size"), 0.0);
46   Real size = text_size * pow (2.0, step / 6.0);
47   
48   pango_font_description_set_size (description,
49                                    gint (size * PANGO_SCALE));
50   return description;
51 }
52
53 Font_metric *
54 select_pango_font (Output_def *layout, SCM chain)
55 {
56   PangoFontDescription *pfd = properties_to_pango_description (chain,
57                                                                point_constant * layout->get_dimension (ly_symbol2scm ("text-font-size")));
58
59   char * str = pango_font_description_to_string (pfd);
60   SCM scm_str = scm_makfrom0str (str);
61   g_free (str);
62     
63   return find_pango_font (layout, scm_str, 1.0);
64 }
65
66 PangoStyle
67 symbol_to_pango_style (SCM style)
68 {
69   PangoStyle pstyle = PANGO_STYLE_NORMAL;
70   if (style == ly_symbol2scm ("italic"))
71     {
72       pstyle = PANGO_STYLE_ITALIC;
73     }
74   else if (style == ly_symbol2scm ("oblique")
75            || style == ly_symbol2scm ("slanted")
76            )
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
96 PangoWeight
97 symbol_to_pango_weight (SCM weight)
98 {
99   PangoWeight pw = PANGO_WEIGHT_NORMAL;
100   if (weight == ly_symbol2scm ("bold"))
101     {
102       pw = PANGO_WEIGHT_BOLD;
103     }
104   if (weight == ly_symbol2scm ("heavy"))
105     {
106       pw = PANGO_WEIGHT_HEAVY;
107     }
108   if (weight == ly_symbol2scm ("ultrabold"))
109     {
110       pw = PANGO_WEIGHT_ULTRABOLD;
111     }
112   if (weight == ly_symbol2scm ("light"))
113     {
114       pw = PANGO_WEIGHT_LIGHT;
115     }
116   if (weight == ly_symbol2scm ("ultralight"))
117     {
118       pw = PANGO_WEIGHT_ULTRALIGHT;
119     }
120
121   return pw;
122 }
123
124 PangoStretch
125 symbol_to_pango_stretch (SCM) //  stretch)
126 {
127   PangoStretch ps = PANGO_STRETCH_NORMAL;
128
129   /*
130     // TODO
131     
132   PANGO_STRETCH_ULTRA_CONDENSED,
133   PANGO_STRETCH_EXTRA_CONDENSED,
134   PANGO_STRETCH_CONDENSED,
135   PANGO_STRETCH_SEMI_CONDENSED,
136   
137   PANGO_STRETCH_SEMI_EXPANDED,
138   PANGO_STRETCH_EXPANDED,
139   PANGO_STRETCH_EXTRA_EXPANDED,
140   PANGO_STRETCH_ULTRA_EXPANDED
141   */ 
142   return ps;
143 }
144
145
146
147 PangoFontDescription* 
148 symbols_to_pango_font_description(SCM family,
149                                   SCM style,
150                                   SCM variant,
151                                   SCM weight,
152                                   SCM stretch)
153 {
154   PangoFontDescription * description = pango_font_description_new ();
155
156   String family_str = "roman";
157   if (scm_is_symbol (family))
158     family_str = ly_symbol2string (family);
159   else if (scm_is_string (family))
160     family_str = ly_scm2string (family);
161
162   pango_font_description_set_family (description,
163                                      family_str.to_str0 ());
164   pango_font_description_set_style (description,
165                                     symbol_to_pango_style (style));
166   pango_font_description_set_variant (description,
167                                       symbol_to_pango_variant (variant));
168   pango_font_description_set_weight (description,
169                                      symbol_to_pango_weight (weight));
170   pango_font_description_set_stretch (description,
171                                       symbol_to_pango_stretch (stretch));
172
173   return description;
174 }