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