]> git.donarmstrong.com Git - lilypond.git/blob - lily/font-metric-scheme.cc
(char): use ly:wide-char->utf-8
[lilypond.git] / lily / font-metric-scheme.cc
1 /*
2   font-metric-scheme.cc -- implement Font_metric scheme bindings
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "warn.hh"
10 #include "stencil.hh"
11 #include "font-metric.hh"
12 #include "modified-font-metric.hh"
13
14 LY_DEFINE (ly_font_get_glyph, "ly:font-get-glyph",
15            2, 0, 0,
16            (SCM font, SCM name),
17            "Return a Stencil from @var{font} for the glyph named @var{name}.  "
18            "@var{font} must be available as an AFM file.  If the glyph "
19            "is not available, return @code{#f}.")
20 {
21   Font_metric *fm = unsmob_metrics (font);
22   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
23   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string");
24
25   Stencil m = fm->find_by_name (ly_scm2string (name));
26
27   /* TODO: make optional argument for default if not found.  */
28   return m.smobbed_copy ();
29 }
30
31 LY_DEFINE (ly_get_glyph, "ly:get-glyph",
32            2, 0, 0,
33            (SCM font, SCM index),
34            "Retrieve a Stencil for the glyph numbered @var{index} "
35            "in @var{font}.")
36 {
37   Font_metric *fm = unsmob_metrics (font);
38   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
39   SCM_ASSERT_TYPE (scm_is_number (index), index, SCM_ARG2, __FUNCTION__, "number");
40
41   return fm->get_ascii_char_stencil (scm_to_int (index)).smobbed_copy ();
42 }
43
44 LY_DEFINE (ly_font_glyph_name_to_index, "ly:font-glyph-name-to-index",
45            2, 0, 0,
46            (SCM font, SCM name),
47            "Return the index for @var{name} in @var{font}.")
48 {
49   Font_metric *fm = unsmob_metrics (font);
50   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
51   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string");
52
53   return scm_from_int (fm->name_to_index (ly_scm2string (name)));
54 }
55
56 LY_DEFINE (ly_font_index_to_charcode, "ly:font-index-to-charcode",
57            2, 0, 0,
58            (SCM font, SCM index),
59            "Return the character code for @var{index} @var{font}.")
60 {
61   Font_metric *fm = unsmob_metrics (font);
62   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
63   SCM_ASSERT_TYPE (scm_is_integer (index), index, SCM_ARG2, __FUNCTION__, "index");
64
65   return scm_from_unsigned_integer (fm->index_to_charcode (scm_to_int (index)));
66 }
67
68 LY_DEFINE (ly_font_glyph_name_to_charcode, "ly:font-glyph-name-to-charcode",
69            2, 0, 0,
70            (SCM font, SCM name),
71            "Return the character code for glyph @var{name} in @var{font}.")
72 {
73   Font_metric *fm = unsmob_metrics (font);
74   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
75   SCM_ASSERT_TYPE (scm_is_string (name), name, SCM_ARG2, __FUNCTION__, "string");
76 #if 1
77   return scm_from_unsigned_integer (fm->index_to_charcode (fm->name_to_index (ly_scm2string (name))));
78 #else
79   return scm_from_unsigned_integer (fm->glyph_name_to_charcode (ly_scm2string (name)));
80 #endif
81 }
82
83 LY_DEFINE (ly_text_dimension, "ly:text-dimension",
84            2, 0, 0,
85            (SCM font, SCM text),
86            "Given the font metric in @var{font} and the string @var{text}, "
87            "compute the extents of that text in that font.  "
88            "The return value is a pair of number-pairs.")
89 {
90   Box b;
91   Modified_font_metric *fm = dynamic_cast<Modified_font_metric *>
92     (unsmob_metrics (font));
93
94   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "modified font metric");
95   SCM_ASSERT_TYPE (scm_is_string (text), text, SCM_ARG2, __FUNCTION__, "string");
96   Stencil stc (fm->text_stencil (ly_scm2string (text)));
97   return scm_cons (ly_interval2scm (stc.extent (X_AXIS)),
98                    ly_interval2scm (stc.extent (Y_AXIS)));
99 }
100
101
102 /*
103   TODO: when are non string retvals allowed?
104  */
105 LY_DEFINE (ly_font_file_name, "ly:font-file-name",
106            1, 0, 0,
107            (SCM font),
108            "Given the font metric @var{font}, "
109            "return the corresponding file name.")
110 {
111   Font_metric *fm = unsmob_metrics (font);
112   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
113   SCM name = fm->font_file_name ();
114
115   return name;
116 }
117
118 LY_DEFINE (ly_font_name, "ly:font-name",
119            1, 0, 0,
120            (SCM font),
121            "Given the font metric @var{font}, "
122            "return the corresponding name.")
123 {
124   Font_metric *fm = unsmob_metrics (font);
125
126   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
127   return scm_makfrom0str (fm->font_name ().c_str ());
128 }
129
130 LY_DEFINE (ly_font_magnification, "ly:font-magnification", 1, 0, 0,
131            (SCM font),
132            "Given the font metric @var{font}, return the "
133            "magnification, relative to the current outputs-cale.")
134 {
135   Font_metric *fm = unsmob_metrics (font);
136   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
137   return scm_cdr (fm->description_);
138 }
139
140 LY_DEFINE (ly_font_design_size, "ly:font-design-size", 1, 0, 0,
141            (SCM font),
142            "Given the font metric @var{font}, return the "
143            "design size, relative to the current output-scale.")
144 {
145   Font_metric *fm = unsmob_metrics (font);
146   SCM_ASSERT_TYPE (fm, font, SCM_ARG1, __FUNCTION__, "font-metric");
147   return scm_from_double (fm->design_size ());
148 }
149