]> git.donarmstrong.com Git - lilypond.git/blob - lily/pango-select.cc
lily/system.cc (get_paper_system): reinstate the backwards-
[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--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "dimensions.hh"
10 #include "all-font-metrics.hh"
11 #include "output-def.hh"
12 #include "pango-font.hh"
13
14 PangoFontDescription *
15 properties_to_pango_description (SCM chain, Real text_size)
16 {
17   SCM name = ly_chain_assoc_get (ly_symbol2scm ("font-name"), chain, SCM_BOOL_F);
18
19   PangoFontDescription *description = 0;
20   if (scm_is_string (name))
21     {
22       string name_str = ly_scm2string (name);
23       description = pango_font_description_from_string (name_str.c_str ());
24     }
25   else
26     {
27       SCM family = ly_chain_assoc_get (ly_symbol2scm ("font-family"), chain,
28                                        SCM_BOOL_F);
29       SCM variant = ly_chain_assoc_get (ly_symbol2scm ("font-shape"), chain,
30                                         SCM_BOOL_F);
31
32       SCM style = ly_chain_assoc_get (ly_symbol2scm ("font-shape"), chain,
33                                       SCM_BOOL_F);
34       SCM weight = ly_chain_assoc_get (ly_symbol2scm ("font-series"), chain,
35                                        SCM_BOOL_F);
36
37       description
38         = symbols_to_pango_font_description (family, style, variant, weight,
39                                              SCM_BOOL_F);
40     }
41
42   Real step = robust_scm2double (ly_chain_assoc_get (ly_symbol2scm ("font-size"), chain, SCM_BOOL_F),
43                                  0.0);
44   Real size = text_size * pow (2.0, step / 6.0);
45
46   pango_font_description_set_size (description, gint (size * PANGO_SCALE));
47   return description;
48 }
49
50 Font_metric *
51 select_pango_font (Output_def *layout, SCM chain)
52 {
53   PangoFontDescription *pfd
54     = properties_to_pango_description (chain,
55                                        point_constant
56                                        * layout->get_dimension (ly_symbol2scm ("text-font-size")));
57
58   char *str = pango_font_description_to_string (pfd);
59   SCM scm_str = scm_makfrom0str (str);
60   g_free (str);
61
62   return find_pango_font (layout, scm_str, 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     pstyle = PANGO_STYLE_ITALIC;
71   else if (style == ly_symbol2scm ("oblique")
72            || style == ly_symbol2scm ("slanted"))
73     pstyle = PANGO_STYLE_OBLIQUE;
74
75   return pstyle;
76 }
77
78 PangoVariant
79 symbol_to_pango_variant (SCM variant)
80 {
81   PangoVariant pvariant = PANGO_VARIANT_NORMAL;
82   if (variant == ly_symbol2scm ("caps"))
83     pvariant = PANGO_VARIANT_SMALL_CAPS;
84   return pvariant;
85 }
86
87 PangoWeight
88 symbol_to_pango_weight (SCM weight)
89 {
90   PangoWeight pw = PANGO_WEIGHT_NORMAL;
91   if (weight == ly_symbol2scm ("bold"))
92     pw = PANGO_WEIGHT_BOLD;
93   if (weight == ly_symbol2scm ("heavy"))
94     pw = PANGO_WEIGHT_HEAVY;
95   if (weight == ly_symbol2scm ("ultrabold"))
96     pw = PANGO_WEIGHT_ULTRABOLD;
97   if (weight == ly_symbol2scm ("light"))
98     pw = PANGO_WEIGHT_LIGHT;
99   if (weight == ly_symbol2scm ("ultralight"))
100     pw = PANGO_WEIGHT_ULTRALIGHT;
101
102   return pw;
103 }
104
105 PangoStretch
106 symbol_to_pango_stretch (SCM) //  stretch)
107 {
108   PangoStretch ps = PANGO_STRETCH_NORMAL;
109
110   /*
111   // TODO
112
113   PANGO_STRETCH_ULTRA_CONDENSED,
114   PANGO_STRETCH_EXTRA_CONDENSED,
115   PANGO_STRETCH_CONDENSED,
116   PANGO_STRETCH_SEMI_CONDENSED,
117
118   PANGO_STRETCH_SEMI_EXPANDED,
119   PANGO_STRETCH_EXPANDED,
120   PANGO_STRETCH_EXTRA_EXPANDED,
121   PANGO_STRETCH_ULTRA_EXPANDED
122   */
123   return ps;
124 }
125
126 PangoFontDescription *
127 symbols_to_pango_font_description (SCM family,
128                                    SCM style,
129                                    SCM variant,
130                                    SCM weight,
131                                    SCM stretch)
132 {
133   PangoFontDescription *description = pango_font_description_new ();
134
135   string family_str = "roman";
136   if (scm_is_symbol (family))
137     family_str = ly_symbol2string (family);
138   else if (scm_is_string (family))
139     family_str = ly_scm2string (family);
140
141   pango_font_description_set_family (description,
142                                      family_str.c_str ());
143   pango_font_description_set_style (description,
144                                     symbol_to_pango_style (style));
145   pango_font_description_set_variant (description,
146                                       symbol_to_pango_variant (variant));
147   pango_font_description_set_weight (description,
148                                      symbol_to_pango_weight (weight));
149   pango_font_description_set_stretch (description,
150                                       symbol_to_pango_stretch (stretch));
151
152   return description;
153 }