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