]> git.donarmstrong.com Git - lilypond.git/blob - lily/pango-select.cc
(get_font_by_design_size): retrieve
[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
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   Font_metric * fm = all_fonts_global->find_pango_font (pfd);
60
61   return find_scaled_font (layout, fm, 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_NORMAL;
71     }
72   else if (style == ly_symbol2scm ("oblique")
73            || style == ly_symbol2scm ("slanted")
74            )
75     {
76       pstyle = PANGO_STYLE_OBLIQUE;
77     }
78
79   return pstyle;
80 }
81
82 PangoVariant
83 symbol_to_pango_variant (SCM variant)
84 {
85   PangoVariant pvariant;
86   if (variant == ly_symbol2scm ("caps"))
87     {
88       pvariant = PANGO_VARIANT_SMALL_CAPS;
89     }
90   return pvariant;
91 }
92
93
94 PangoWeight
95 symbol_to_pango_weight (SCM weight)
96 {
97   PangoWeight pw = PANGO_WEIGHT_NORMAL;
98   if (weight == ly_symbol2scm ("bold"))
99     {
100       pw = PANGO_WEIGHT_BOLD;
101     }
102   if (weight == ly_symbol2scm ("heavy"))
103     {
104       pw = PANGO_WEIGHT_HEAVY;
105     }
106   if (weight == ly_symbol2scm ("ultrabold"))
107     {
108       pw = PANGO_WEIGHT_ULTRABOLD;
109     }
110   if (weight == ly_symbol2scm ("light"))
111     {
112       pw = PANGO_WEIGHT_LIGHT;
113     }
114   if (weight == ly_symbol2scm ("ultralight"))
115     {
116       pw = PANGO_WEIGHT_ULTRALIGHT;
117     }
118
119   return pw;
120 }
121
122 PangoStretch
123 symbol_to_pango_stretch (SCM) //  stretch)
124 {
125   PangoStretch ps = PANGO_STRETCH_NORMAL;
126
127   /*
128     // TODO
129     
130   PANGO_STRETCH_ULTRA_CONDENSED,
131   PANGO_STRETCH_EXTRA_CONDENSED,
132   PANGO_STRETCH_CONDENSED,
133   PANGO_STRETCH_SEMI_CONDENSED,
134   
135   PANGO_STRETCH_SEMI_EXPANDED,
136   PANGO_STRETCH_EXPANDED,
137   PANGO_STRETCH_EXTRA_EXPANDED,
138   PANGO_STRETCH_ULTRA_EXPANDED
139   */ 
140   return ps;
141 }
142
143
144
145 PangoFontDescription* 
146 symbols_to_pango_font_description(SCM family,
147                                   SCM style,
148                                   SCM variant,
149                                   SCM weight,
150                                   SCM stretch)
151 {
152   PangoFontDescription * description = pango_font_description_new ();
153
154   String family_str = "roman";
155   if (scm_is_symbol (family))
156     family_str = ly_symbol2string (family);
157   else if (scm_is_string (family))
158     family_str = ly_scm2string (family);
159
160   pango_font_description_set_family (description,
161                                      family_str.to_str0 ());
162   pango_font_description_set_style (description,
163                                     symbol_to_pango_style (style));
164   pango_font_description_set_variant (description,
165                                       symbol_to_pango_variant (variant));
166   pango_font_description_set_weight (description,
167                                      symbol_to_pango_weight (weight));
168   pango_font_description_set_stretch (description,
169                                       symbol_to_pango_stretch (stretch));
170
171   return description;
172 }