]> git.donarmstrong.com Git - lilypond.git/blob - lily/pango-select.cc
Merge branch 'master' into lilypond/translation
[lilypond.git] / lily / pango-select.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2004--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "dimensions.hh"
21 #include "all-font-metrics.hh"
22 #include "output-def.hh"
23 #include "pango-font.hh"
24
25 PangoFontDescription *
26 properties_to_pango_description (SCM chain, Real text_size)
27 {
28   SCM name = ly_chain_assoc_get (ly_symbol2scm ("font-name"), chain, SCM_BOOL_F);
29
30   PangoFontDescription *description = 0;
31   if (scm_is_string (name))
32     {
33       string name_str = ly_scm2string (name);
34       description = pango_font_description_from_string (name_str.c_str ());
35     }
36   else
37     {
38       SCM family = ly_chain_assoc_get (ly_symbol2scm ("font-family"), chain,
39                                        SCM_BOOL_F);
40       SCM variant = ly_chain_assoc_get (ly_symbol2scm ("font-shape"), chain,
41                                         SCM_BOOL_F);
42
43       SCM style = ly_chain_assoc_get (ly_symbol2scm ("font-shape"), chain,
44                                       SCM_BOOL_F);
45       SCM weight = ly_chain_assoc_get (ly_symbol2scm ("font-series"), chain,
46                                        SCM_BOOL_F);
47
48       description
49         = symbols_to_pango_font_description (family, style, variant, weight,
50                                              SCM_BOOL_F);
51     }
52
53   Real step = robust_scm2double (ly_chain_assoc_get (ly_symbol2scm ("font-size"), chain, SCM_BOOL_F),
54                                  0.0);
55   Real size = text_size * pow (2.0, step / 6.0);
56
57   pango_font_description_set_size (description, gint (size * PANGO_SCALE));
58   return description;
59 }
60
61 Font_metric *
62 select_pango_font (Output_def *layout, SCM chain)
63 {
64   PangoFontDescription *pfd
65     = properties_to_pango_description (chain,
66                                        point_constant
67                                        * layout->get_dimension (ly_symbol2scm ("text-font-size")));
68
69   char *str = pango_font_description_to_string (pfd);
70   SCM scm_str = scm_from_locale_string (str);
71   g_free (str);
72   pango_font_description_free (pfd);
73   
74   return find_pango_font (layout, scm_str, 1.0);
75 }
76
77 PangoStyle
78 symbol_to_pango_style (SCM style)
79 {
80   PangoStyle pstyle = PANGO_STYLE_NORMAL;
81   if (style == ly_symbol2scm ("italic"))
82     pstyle = PANGO_STYLE_ITALIC;
83   else if (style == ly_symbol2scm ("oblique")
84            || style == ly_symbol2scm ("slanted"))
85     pstyle = PANGO_STYLE_OBLIQUE;
86
87   return pstyle;
88 }
89
90 PangoVariant
91 symbol_to_pango_variant (SCM variant)
92 {
93   PangoVariant pvariant = PANGO_VARIANT_NORMAL;
94   if (variant == ly_symbol2scm ("caps"))
95     pvariant = PANGO_VARIANT_SMALL_CAPS;
96   return pvariant;
97 }
98
99 PangoWeight
100 symbol_to_pango_weight (SCM weight)
101 {
102   PangoWeight pw = PANGO_WEIGHT_NORMAL;
103   if (weight == ly_symbol2scm ("bold"))
104     pw = PANGO_WEIGHT_BOLD;
105   if (weight == ly_symbol2scm ("heavy"))
106     pw = PANGO_WEIGHT_HEAVY;
107   if (weight == ly_symbol2scm ("ultrabold"))
108     pw = PANGO_WEIGHT_ULTRABOLD;
109   if (weight == ly_symbol2scm ("light"))
110     pw = PANGO_WEIGHT_LIGHT;
111   if (weight == ly_symbol2scm ("ultralight"))
112     pw = PANGO_WEIGHT_ULTRALIGHT;
113
114   return pw;
115 }
116
117 PangoStretch
118 symbol_to_pango_stretch (SCM) //  stretch)
119 {
120   PangoStretch ps = PANGO_STRETCH_NORMAL;
121
122   /*
123   // TODO
124
125   PANGO_STRETCH_ULTRA_CONDENSED,
126   PANGO_STRETCH_EXTRA_CONDENSED,
127   PANGO_STRETCH_CONDENSED,
128   PANGO_STRETCH_SEMI_CONDENSED,
129
130   PANGO_STRETCH_SEMI_EXPANDED,
131   PANGO_STRETCH_EXPANDED,
132   PANGO_STRETCH_EXTRA_EXPANDED,
133   PANGO_STRETCH_ULTRA_EXPANDED
134   */
135   return ps;
136 }
137
138 PangoFontDescription *
139 symbols_to_pango_font_description (SCM family,
140                                    SCM style,
141                                    SCM variant,
142                                    SCM weight,
143                                    SCM stretch)
144 {
145   PangoFontDescription *description = pango_font_description_new ();
146
147   string family_str = "roman";
148   if (scm_is_symbol (family))
149     family_str = ly_symbol2string (family);
150   else if (scm_is_string (family))
151     family_str = ly_scm2string (family);
152
153   pango_font_description_set_family (description,
154                                      family_str.c_str ());
155   pango_font_description_set_style (description,
156                                     symbol_to_pango_style (style));
157   pango_font_description_set_variant (description,
158                                       symbol_to_pango_variant (variant));
159   pango_font_description_set_weight (description,
160                                      symbol_to_pango_weight (weight));
161   pango_font_description_set_stretch (description,
162                                       symbol_to_pango_stretch (stretch));
163
164   return description;
165 }