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