]> git.donarmstrong.com Git - lilypond.git/blob - lily/pango-select.cc
Web-ja: update introduction
[lilypond.git] / lily / pango-select.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2004--2015 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 "libc-extension.hh"
23 #include "output-def.hh"
24 #include "pango-font.hh"
25
26 PangoFontDescription *
27 properties_to_pango_description (SCM chain, Real text_size)
28 {
29   SCM name = ly_chain_assoc_get (ly_symbol2scm ("font-name"), chain, SCM_BOOL_F);
30
31   PangoFontDescription *description = 0;
32   if (scm_is_string (name))
33     {
34       string name_str = ly_scm2string (name);
35       description = pango_font_description_from_string (name_str.c_str ());
36     }
37   else
38     {
39       SCM family = ly_chain_assoc_get (ly_symbol2scm ("font-family"), chain,
40                                        SCM_BOOL_F);
41       SCM variant = ly_chain_assoc_get (ly_symbol2scm ("font-shape"), chain,
42                                         SCM_BOOL_F);
43
44       SCM style = ly_chain_assoc_get (ly_symbol2scm ("font-shape"), chain,
45                                       SCM_BOOL_F);
46       SCM weight = ly_chain_assoc_get (ly_symbol2scm ("font-series"), chain,
47                                        SCM_BOOL_F);
48
49       description
50         = symbols_to_pango_font_description (family, style, variant, weight,
51                                              SCM_BOOL_F);
52     }
53
54   Real step = robust_scm2double (ly_chain_assoc_get (ly_symbol2scm ("font-size"), chain, SCM_BOOL_F),
55                                  0.0);
56   Real size = text_size * pow (2.0, step / 6.0);
57
58   pango_font_description_set_size (description,
59                                    gint (my_round (size * PANGO_SCALE)));
60   return description;
61 }
62
63 Font_metric *
64 select_pango_font (Output_def *layout, SCM chain)
65 {
66   PangoFontDescription *pfd
67     = properties_to_pango_description (chain,
68                                        point_constant
69                                        * layout->get_dimension (ly_symbol2scm ("text-font-size")));
70
71   char *str = pango_font_description_to_string (pfd);
72   SCM scm_str = scm_from_locale_string (str);
73   g_free (str);
74   pango_font_description_free (pfd);
75
76   return find_pango_font (layout, scm_str, 1.0);
77 }
78
79 PangoStyle
80 symbol_to_pango_style (SCM style)
81 {
82   PangoStyle pstyle = PANGO_STYLE_NORMAL;
83   if (scm_is_eq (style, ly_symbol2scm ("italic")))
84     pstyle = PANGO_STYLE_ITALIC;
85   else if (scm_is_eq (style, ly_symbol2scm ("oblique"))
86            || scm_is_eq (style, ly_symbol2scm ("slanted")))
87     pstyle = PANGO_STYLE_OBLIQUE;
88
89   return pstyle;
90 }
91
92 PangoVariant
93 symbol_to_pango_variant (SCM variant)
94 {
95   PangoVariant pvariant = PANGO_VARIANT_NORMAL;
96   if (scm_is_eq (variant, ly_symbol2scm ("caps")))
97     pvariant = PANGO_VARIANT_SMALL_CAPS;
98   return pvariant;
99 }
100
101 PangoWeight
102 symbol_to_pango_weight (SCM weight)
103 {
104   PangoWeight pw = PANGO_WEIGHT_NORMAL;
105   if (scm_is_eq (weight, ly_symbol2scm ("bold")))
106     pw = PANGO_WEIGHT_BOLD;
107   if (scm_is_eq (weight, ly_symbol2scm ("heavy")))
108     pw = PANGO_WEIGHT_HEAVY;
109   if (scm_is_eq (weight, ly_symbol2scm ("ultrabold")))
110     pw = PANGO_WEIGHT_ULTRABOLD;
111   if (scm_is_eq (weight, ly_symbol2scm ("light")))
112     pw = PANGO_WEIGHT_LIGHT;
113   if (scm_is_eq (weight, ly_symbol2scm ("ultralight")))
114     pw = PANGO_WEIGHT_ULTRALIGHT;
115
116   return pw;
117 }
118
119 PangoStretch
120 symbol_to_pango_stretch (SCM) //  stretch)
121 {
122   PangoStretch ps = PANGO_STRETCH_NORMAL;
123
124   /*
125   // TODO
126
127   PANGO_STRETCH_ULTRA_CONDENSED,
128   PANGO_STRETCH_EXTRA_CONDENSED,
129   PANGO_STRETCH_CONDENSED,
130   PANGO_STRETCH_SEMI_CONDENSED,
131
132   PANGO_STRETCH_SEMI_EXPANDED,
133   PANGO_STRETCH_EXPANDED,
134   PANGO_STRETCH_EXTRA_EXPANDED,
135   PANGO_STRETCH_ULTRA_EXPANDED
136   */
137   return ps;
138 }
139
140 PangoFontDescription *
141 symbols_to_pango_font_description (SCM family,
142                                    SCM style,
143                                    SCM variant,
144                                    SCM weight,
145                                    SCM stretch)
146 {
147   PangoFontDescription *description = pango_font_description_new ();
148
149   string family_str = "roman";
150   if (scm_is_symbol (family))
151     family_str = ly_symbol2string (family);
152   else if (scm_is_string (family))
153     family_str = ly_scm2string (family);
154
155   pango_font_description_set_family (description,
156                                      family_str.c_str ());
157   pango_font_description_set_style (description,
158                                     symbol_to_pango_style (style));
159   pango_font_description_set_variant (description,
160                                       symbol_to_pango_variant (variant));
161   pango_font_description_set_weight (description,
162                                      symbol_to_pango_weight (weight));
163   pango_font_description_set_stretch (description,
164                                       symbol_to_pango_stretch (stretch));
165
166   return description;
167 }