2 font-metric.cc -- implement Font_metric
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 Mats Bengtsson <matsb@s3.kth.se> (the ugly TeX parsing in text_dimension)
15 #include "molecule.hh"
16 #include "ly-smobs.icc"
17 #include "font-metric.hh"
21 Font_metric::text_dimension (String text
) const
26 for (int i
= 0; i
< text
.length (); i
++)
32 // accent marks use width of base letter
33 if (i
+1 < text
.length ())
35 if (text
[i
+1]=='\'' || text
[i
+1]=='`' || text
[i
+1]=='"' ||
41 // for string width \\ is a \ and \_ is a _.
42 if (text
[i
+1]=='\\' || text
[i
+1]=='_')
48 for (i
++; (i
< text
.length ()) && !isspace (text
[i
])
49 && text
[i
]!='{' && text
[i
]!='}'; i
++)
52 i
--; // Compensate for the increment in the outer loop!
54 case '{': // Skip '{' and '}'
59 Box b
= get_char ((unsigned char)text
[i
]);
61 // Ugh, use the width of 'x' for unknown characters
62 if (b
[X_AXIS
].length () == 0)
63 b
= get_char ((unsigned char)'x');
65 w
+= b
[X_AXIS
].length ();
66 ydims
.unite (b
[Y_AXIS
]);
71 ydims
= Interval (0,0);
73 return Box (Interval (0, w
), ydims
);
78 Font_metric::~Font_metric ()
82 Font_metric::Font_metric ()
84 description_
= SCM_EOL
;
89 Font_metric::Font_metric (Font_metric
const &)
94 Font_metric::count () const
100 Font_metric::get_char (int)const
102 return Box (Interval (0,0),Interval (0,0));
107 Font_metric::derived_mark ()const
115 Font_metric::mark_smob (SCM s
)
117 Font_metric
* m
= (Font_metric
*) SCM_CELL_WORD_1 (s
);
120 return m
->description_
;
124 Font_metric::print_smob (SCM s
, SCM port
, scm_print_state
*)
126 Font_metric
*m
= unsmob_metrics (s
);
127 scm_puts ("#<Font_metric ", port
);
128 scm_write (m
->description_
, port
);
129 scm_puts (">", port
);
135 IMPLEMENT_SMOBS (Font_metric
);
136 IMPLEMENT_DEFAULT_EQUAL_P (Font_metric
);
137 IMPLEMENT_TYPE_P (Font_metric
, "ly:font-metric?");
140 Font_metric::find_by_name (String
) const
146 LY_DEFINE(ly_find_glyph_by_name
, "ly:find-glyph-by-name", 2 , 0, 0,
147 (SCM font
, SCM name
),
148 "This function retrieves a Molecule for the glyph named @var{name} in "
149 "@var{font}. The font must be available as an AFM file. If the glyph "
150 "is not found, #f is returned. ")
152 Font_metric
*fm
= unsmob_metrics (font
);
153 SCM_ASSERT_TYPE(fm
, font
, SCM_ARG1
, __FUNCTION__
, "font-metric");
154 SCM_ASSERT_TYPE(gh_string_p (name
), name
, SCM_ARG2
, __FUNCTION__
, "string");
156 Molecule m
= fm
->find_by_name (ly_scm2string (name
));
160 should add to calling interface.
162 if (m
.get_expr () != SCM_EOL
)
163 return m
.smobbed_copy ();
167 return m
.smobbed_copy ();
170 LY_DEFINE(ly_get_glyph
, "ly:get-glyph", 2 , 0, 0,
171 (SCM font
, SCM index
),
172 "This function retrieves a Molecule for the glyph numbered @var{index} in "
175 Font_metric
*fm
= unsmob_metrics (font
);
176 SCM_ASSERT_TYPE(fm
, font
, SCM_ARG1
, __FUNCTION__
, "font-metric");
177 SCM_ASSERT_TYPE(gh_number_p (index
), index
, SCM_ARG2
, __FUNCTION__
, "number");
179 return fm
->get_char_molecule (gh_scm2int (index
)).smobbed_copy ();
182 LY_DEFINE(ly_text_dimension
,"ly:text-dimension", 2 , 0, 0,
183 (SCM font
, SCM text
),
184 "Given the font metric in @var{font} and the string @var{text}, compute "
185 "the extents of that text in that font. The return value is a pair of "
189 Font_metric
*fm
= unsmob_metrics (font
);
190 SCM_ASSERT_TYPE(fm
, font
, SCM_ARG1
, __FUNCTION__
, "font-metric");
191 SCM_ASSERT_TYPE(gh_string_p (text
), text
, SCM_ARG2
, __FUNCTION__
, "string");
193 b
= fm
->text_dimension (ly_scm2string (text
));
195 return gh_cons (ly_interval2scm (b
[X_AXIS
]), ly_interval2scm(b
[Y_AXIS
]));
199 Font_metric::get_char_molecule (int code
) const
201 SCM at
= scm_list_n (ly_symbol2scm ("char"), gh_int2scm (code
),
203 at
= fontify_atom (this, at
);
204 Box b
= get_char (code
);
205 return Molecule (b
, at
);