]> git.donarmstrong.com Git - lilypond.git/blob - lily/pango-select.cc
* flower/file-path.cc (find): try to open directly as well, so we
[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   else
27     {
28       SCM family = ly_chain_assoc_get (ly_symbol2scm ("font-family"), chain,
29                                        SCM_BOOL_F);
30       SCM variant = ly_chain_assoc_get (ly_symbol2scm ("font-shape"), chain,
31                                         SCM_BOOL_F);
32         
33       SCM style = ly_chain_assoc_get (ly_symbol2scm ("font-shape"), chain,
34                                       SCM_BOOL_F);
35       SCM weight = ly_chain_assoc_get (ly_symbol2scm ("font-series"), chain,
36                                        SCM_BOOL_F);
37       
38       Real step = robust_scm2double (ly_symbol2scm ("font-size"), 0.0);
39       Real size = layout->get_dimension (ly_symbol2scm ("text-font-size"))
40         * pow (2.0, step / 6.0);
41
42       description
43         = symbols_to_pango_font_description (family, style, variant, weight,
44                                              SCM_BOOL_F,
45                                              size);
46     }
47
48   Font_metric * fm = all_fonts_global->find_pango_font (description);
49
50   /*
51     Klutz the pango font into font table, otherwise it doesn't show up
52     in the output.
53    */
54   SCM font_table = get_font_table (layout);
55   SCM sizes = scm_hashq_ref (font_table, fm->self_scm (), SCM_BOOL_F);
56   if (sizes != SCM_BOOL_F)
57     {
58       SCM met = scm_assoc (scm_make_real (1.0), sizes);
59       if (scm_is_pair (met))
60         return unsmob_metrics (scm_cdr (met));
61     }
62   else
63     sizes = SCM_EOL;
64
65   sizes = scm_acons (scm_make_real (1.0), fm->self_scm(), sizes);
66   scm_hashq_set_x (font_table, fm->self_scm (), sizes);
67   
68   return fm;
69 }
70
71
72 PangoStyle
73 symbol_to_pango_style (SCM style)
74 {
75   PangoStyle pstyle = PANGO_STYLE_NORMAL;
76   if (style == ly_symbol2scm ("italic"))
77     {
78       pstyle = PANGO_STYLE_NORMAL;
79     }
80   else if (style == ly_symbol2scm ("oblique")
81            || style == ly_symbol2scm ("slanted")
82            )
83     {
84       pstyle = PANGO_STYLE_OBLIQUE;
85     }
86
87   return pstyle;
88 }
89
90 PangoVariant
91 symbol_to_pango_variant (SCM variant)
92 {
93   PangoVariant pvariant;
94   if (variant == ly_symbol2scm ("caps"))
95     {
96       pvariant = PANGO_VARIANT_SMALL_CAPS;
97     }
98   return pvariant;
99 }
100
101
102 PangoWeight
103 symbol_to_pango_weight (SCM weight)
104 {
105   PangoWeight pw = PANGO_WEIGHT_NORMAL;
106   if (weight == ly_symbol2scm ("bold"))
107     {
108       pw = PANGO_WEIGHT_BOLD;
109     }
110   if (weight == ly_symbol2scm ("heavy"))
111     {
112       pw = PANGO_WEIGHT_HEAVY;
113     }
114   if (weight == ly_symbol2scm ("ultrabold"))
115     {
116       pw = PANGO_WEIGHT_ULTRABOLD;
117     }
118   if (weight == ly_symbol2scm ("light"))
119     {
120       pw = PANGO_WEIGHT_LIGHT;
121     }
122   if (weight == ly_symbol2scm ("ultralight"))
123     {
124       pw = PANGO_WEIGHT_ULTRALIGHT;
125     }
126
127   return pw;
128 }
129
130 PangoStretch
131 symbol_to_pango_stretch (SCM) //  stretch)
132 {
133   PangoStretch ps = PANGO_STRETCH_NORMAL;
134
135   /*
136     // TODO
137     
138   PANGO_STRETCH_ULTRA_CONDENSED,
139   PANGO_STRETCH_EXTRA_CONDENSED,
140   PANGO_STRETCH_CONDENSED,
141   PANGO_STRETCH_SEMI_CONDENSED,
142   
143   PANGO_STRETCH_SEMI_EXPANDED,
144   PANGO_STRETCH_EXPANDED,
145   PANGO_STRETCH_EXTRA_EXPANDED,
146   PANGO_STRETCH_ULTRA_EXPANDED
147   */ 
148   return ps;
149 }
150
151
152
153 PangoFontDescription* 
154 symbols_to_pango_font_description(SCM family,
155                                   SCM style,
156                                   SCM variant,
157                                   SCM weight,
158                                   SCM stretch,
159                                   Real size)
160 {
161   PangoFontDescription * description = pango_font_description_new ();
162
163   String family_str = scm_is_symbol (family)
164     ? ly_symbol2string (family)
165     : String("roman");
166   pango_font_description_set_family (description,
167                                      family_str.to_str0 ());
168   pango_font_description_set_style (description,
169                                     symbol_to_pango_style (style));
170   pango_font_description_set_variant (description,
171                                       symbol_to_pango_variant (variant));
172   pango_font_description_set_weight (description,
173                                      symbol_to_pango_weight (weight));
174   pango_font_description_set_stretch (description,
175                                       symbol_to_pango_stretch (stretch));
176   pango_font_description_set_size (description,
177                                    gint (size * PANGO_SCALE));
178
179   return description;
180 }