2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2004--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
20 #include "freetype.hh"
26 FT_Library freetype2_library;
31 FT_Error errorcode = FT_Init_FreeType (&freetype2_library);
33 error ("cannot initialize FreeType");
37 ly_FT_get_unscaled_indexed_char_dimensions (FT_Face const &face, size_t signed_idx)
39 FT_UInt idx = FT_UInt (signed_idx);
40 FT_Load_Glyph (face, idx, FT_LOAD_NO_SCALE);
42 FT_Glyph_Metrics m = face->glyph->metrics;
43 FT_Pos hb = m.horiBearingX;
44 FT_Pos vb = m.horiBearingY;
46 // is this viable for all grobs?
47 return Box (Interval (Real (hb), Real (hb + m.width)),
48 Interval (Real (vb - m.height), Real (vb)));
52 box_to_scheme_lines (Box b)
54 return scm_list_4 (scm_list_4 (scm_from_double (b[X_AXIS][LEFT]),
55 scm_from_double (b[Y_AXIS][DOWN]),
56 scm_from_double (b[X_AXIS][RIGHT]),
57 scm_from_double (b[Y_AXIS][DOWN])),
58 scm_list_4 (scm_from_double (b[X_AXIS][RIGHT]),
59 scm_from_double (b[Y_AXIS][DOWN]),
60 scm_from_double (b[X_AXIS][RIGHT]),
61 scm_from_double (b[Y_AXIS][UP])),
62 scm_list_4 (scm_from_double (b[X_AXIS][RIGHT]),
63 scm_from_double (b[Y_AXIS][UP]),
64 scm_from_double (b[X_AXIS][LEFT]),
65 scm_from_double (b[Y_AXIS][UP])),
66 scm_list_4 (scm_from_double (b[X_AXIS][LEFT]),
67 scm_from_double (b[Y_AXIS][UP]),
68 scm_from_double (b[X_AXIS][LEFT]),
69 scm_from_double (b[Y_AXIS][DOWN])));
73 ly_FT_get_glyph_outline_bbox (FT_Face const &face, size_t signed_idx)
75 FT_UInt idx = FT_UInt (signed_idx);
76 FT_Load_Glyph (face, idx, FT_LOAD_NO_SCALE);
78 if (!(face->glyph->format == FT_GLYPH_FORMAT_OUTLINE))
81 // will generate a lot of warnings
82 warning ("Cannot make glyph outline");
84 return Box (Interval (infinity_f, -infinity_f), Interval (infinity_f, -infinity_f));
87 outline = &(face->glyph->outline);
90 FT_Outline_Get_BBox (outline, &bbox);
92 return Box (Interval (bbox.xMin, bbox.xMax), Interval (bbox.yMin, bbox.yMax));
96 ly_FT_get_glyph_outline (FT_Face const &face, size_t signed_idx)
98 FT_UInt idx = FT_UInt (signed_idx);
99 FT_Load_Glyph (face, idx, FT_LOAD_NO_SCALE);
101 if (!(face->glyph->format == FT_GLYPH_FORMAT_OUTLINE))
104 // will generate a lot of warnings
105 warning ("Cannot make glyph outline");
107 return box_to_scheme_lines (ly_FT_get_unscaled_indexed_char_dimensions (face, signed_idx));
111 outline = &(face->glyph->outline);
116 while (j < outline->n_points)
120 firstpos = Offset (outline->points[j].x, outline->points[j].y);
124 else if (outline->tags[j] & 1)
127 out = scm_cons (scm_list_4 (scm_from_double (lastpos[X_AXIS]),
128 scm_from_double (lastpos[Y_AXIS]),
129 scm_from_double (outline->points[j].x),
130 scm_from_double (outline->points[j].y)),
132 lastpos = Offset (outline->points[j].x, outline->points[j].y);
135 else if (outline->tags[j] & 2)
137 // it is a third order bezier
138 out = scm_cons (scm_list_n (scm_from_double (lastpos[X_AXIS]),
139 scm_from_double (lastpos[Y_AXIS]),
140 scm_from_double (outline->points[j].x),
141 scm_from_double (outline->points[j].y),
142 scm_from_double (outline->points[j + 1].x),
143 scm_from_double (outline->points[j + 1].y),
144 scm_from_double (outline->points[j + 2].x),
145 scm_from_double (outline->points[j + 2].y),
148 lastpos = Offset (outline->points[j + 2].x, outline->points[j + 2].y);
153 // it is a second order bezier
154 Real x0 = lastpos[X_AXIS];
155 Real x1 = outline->points[j].x;
156 Real x2 = outline->points[j + 1].x;
158 Real y0 = lastpos[Y_AXIS];
159 Real y1 = outline->points[j].y;
160 Real y2 = outline->points[j + 1].y;
162 Real qx2 = x0 + x2 - (2 * x1);
163 Real qx1 = (2 * x1) - (2 * x0);
166 Real qy2 = y0 + y2 - (2 * y1);
167 Real qy1 = (2 * y1) - (2 * y0);
171 Real cx1 = qx0 + (qx1 / 3);
172 Real cx2 = qx0 + (2 * qx1 / 3) + (qx2 / 3);
173 Real cx3 = qx0 + qx1 + qx2;
176 Real cy1 = qy0 + (qy1 / 3);
177 Real cy2 = qy0 + (2 * qy1 / 3) + (qy2 / 3);
178 Real cy3 = qy0 + qy1 + qy2;
180 out = scm_cons (scm_list_n (scm_from_double (cx0),
181 scm_from_double (cy0),
182 scm_from_double (cx1),
183 scm_from_double (cy1),
184 scm_from_double (cx2),
185 scm_from_double (cy2),
186 scm_from_double (cx3),
187 scm_from_double (cy3),
190 lastpos = Offset (outline->points[j + 1].x, outline->points[j + 1].y);
195 // just in case, close the figure
196 out = scm_cons (scm_list_4 (scm_from_double (lastpos[X_AXIS]),
197 scm_from_double (lastpos[Y_AXIS]),
198 scm_from_double (firstpos[X_AXIS]),
199 scm_from_double (firstpos[Y_AXIS])),
202 out = scm_reverse_x (out, SCM_EOL);