]> git.donarmstrong.com Git - lilypond.git/blob - lily/pango-select.cc
(LY_DEFINE): 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 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
47     * pow (2.0, step / 6.0) * point_constant;
48   
49   pango_font_description_set_size (description,
50                                    gint (size * PANGO_SCALE));
51   return description;
52 }
53
54 Font_metric *
55 select_pango_font (Output_def *layout, SCM chain)
56 {
57   PangoFontDescription *pfd =properties_to_pango_description (chain,
58                                    layout->get_dimension (ly_symbol2scm ("text-font-size")));
59
60   Font_metric * fm = all_fonts_global->find_pango_font (pfd);
61
62   return find_scaled_font (layout, fm, 1.0);
63 }
64
65 PangoStyle
66 symbol_to_pango_style (SCM style)
67 {
68   PangoStyle pstyle = PANGO_STYLE_NORMAL;
69   if (style == ly_symbol2scm ("italic"))
70     {
71       pstyle = PANGO_STYLE_NORMAL;
72     }
73   else if (style == ly_symbol2scm ("oblique")
74            || style == ly_symbol2scm ("slanted")
75            )
76     {
77       pstyle = PANGO_STYLE_OBLIQUE;
78     }
79
80   return pstyle;
81 }
82
83 PangoVariant
84 symbol_to_pango_variant (SCM variant)
85 {
86   PangoVariant pvariant;
87   if (variant == ly_symbol2scm ("caps"))
88     {
89       pvariant = PANGO_VARIANT_SMALL_CAPS;
90     }
91   return pvariant;
92 }
93
94
95 PangoWeight
96 symbol_to_pango_weight (SCM weight)
97 {
98   PangoWeight pw = PANGO_WEIGHT_NORMAL;
99   if (weight == ly_symbol2scm ("bold"))
100     {
101       pw = PANGO_WEIGHT_BOLD;
102     }
103   if (weight == ly_symbol2scm ("heavy"))
104     {
105       pw = PANGO_WEIGHT_HEAVY;
106     }
107   if (weight == ly_symbol2scm ("ultrabold"))
108     {
109       pw = PANGO_WEIGHT_ULTRABOLD;
110     }
111   if (weight == ly_symbol2scm ("light"))
112     {
113       pw = PANGO_WEIGHT_LIGHT;
114     }
115   if (weight == ly_symbol2scm ("ultralight"))
116     {
117       pw = PANGO_WEIGHT_ULTRALIGHT;
118     }
119
120   return pw;
121 }
122
123 PangoStretch
124 symbol_to_pango_stretch (SCM) //  stretch)
125 {
126   PangoStretch ps = PANGO_STRETCH_NORMAL;
127
128   /*
129     // TODO
130     
131   PANGO_STRETCH_ULTRA_CONDENSED,
132   PANGO_STRETCH_EXTRA_CONDENSED,
133   PANGO_STRETCH_CONDENSED,
134   PANGO_STRETCH_SEMI_CONDENSED,
135   
136   PANGO_STRETCH_SEMI_EXPANDED,
137   PANGO_STRETCH_EXPANDED,
138   PANGO_STRETCH_EXTRA_EXPANDED,
139   PANGO_STRETCH_ULTRA_EXPANDED
140   */ 
141   return ps;
142 }
143
144
145
146 PangoFontDescription* 
147 symbols_to_pango_font_description(SCM family,
148                                   SCM style,
149                                   SCM variant,
150                                   SCM weight,
151                                   SCM stretch)
152 {
153   PangoFontDescription * description = pango_font_description_new ();
154
155   String family_str = scm_is_symbol (family)
156     ? ly_symbol2string (family)
157     : String("roman");
158   pango_font_description_set_family (description,
159                                      family_str.to_str0 ());
160   pango_font_description_set_style (description,
161                                     symbol_to_pango_style (style));
162   pango_font_description_set_variant (description,
163                                       symbol_to_pango_variant (variant));
164   pango_font_description_set_weight (description,
165                                      symbol_to_pango_weight (weight));
166   pango_font_description_set_stretch (description,
167                                       symbol_to_pango_stretch (stretch));
168
169   return description;
170 }