Blender V5.3
blf_glyph_curves.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <cmath>
12#include <cstdio>
13#include <cstdlib>
14#include <cstring>
15
16#include <ft2build.h>
17
18#include FT_OUTLINE_H
19
20#include "MEM_guardedalloc.h"
21
22#include "BLI_listbase.hh"
23#include "BLI_math_geom_c.hh"
24
25#include "BLF_api.hh"
26
27#include "DNA_curve_types.h"
28
29#include "blf_internal.hh"
30#include "blf_internal_types.hh"
31
32#include "BLI_math_vector_c.hh"
33
34#include "BLI_strict_flags.hh" /* IWYU pragma: keep. Keep last. */
35
36namespace blender {
37
38/* -------------------------------------------------------------------- */
41
45static FT_Fixed to_16dot16(const double val)
46{
47 return FT_Fixed(lround(val * 65536.0));
48}
49
51
52/* -------------------------------------------------------------------- */
55
136
137static void blf_glyph_to_curves(const FT_Outline &ftoutline,
138 ListBaseT<Nurb> *nurbsbase,
139 const float scale)
140{
141 const float eps = 0.0001f;
142 const float eps_sq = eps * eps;
143 Nurb *nu;
144 BezTriple *bezt;
145 float dx, dy;
146 int j, k, l, l_first = 0;
147
148 /* initialize as -1 to add 1 on first loop each time */
149 int contour_prev;
150
151 /* Start converting the FT data */
152 int *onpoints = MEM_new_array_zeroed<int>(size_t(ftoutline.n_contours), "onpoints");
153
154 /* Get number of on-curve points for bezier-triples (including conic virtual on-points). */
155 for (j = 0, contour_prev = -1; j < ftoutline.n_contours; j++) {
156 const int n = ftoutline.contours[j] - contour_prev;
157 contour_prev = ftoutline.contours[j];
158
159 for (k = 0; k < n; k++) {
160 l = (j > 0) ? (k + ftoutline.contours[j - 1] + 1) : k;
161 if (k == 0) {
162 l_first = l;
163 }
164
165 if (ftoutline.tags[l] == FT_Curve_Tag_On) {
166 onpoints[j]++;
167 }
168
169 {
170 const int l_next = (k < n - 1) ? (l + 1) : l_first;
171 if (ftoutline.tags[l] == FT_Curve_Tag_Conic &&
172 ftoutline.tags[l_next] == FT_Curve_Tag_Conic)
173 {
174 onpoints[j]++;
175 }
176 }
177 }
178 }
179
180 /* contour loop, bezier & conic styles merged */
181 for (j = 0, contour_prev = -1; j < ftoutline.n_contours; j++) {
182 const int n = ftoutline.contours[j] - contour_prev;
183 contour_prev = ftoutline.contours[j];
184
185 /* add new curve */
186 nu = MEM_new<Nurb>("objfnt_nurb");
187 bezt = MEM_new_array_zeroed<BezTriple>(size_t(onpoints[j]), "objfnt_bezt");
188 BLI_addtail(nurbsbase, nu);
189
190 nu->type = CU_BEZIER;
191 nu->pntsu = onpoints[j];
192 nu->resolu = 8;
193 nu->flagu = CU_NURB_CYCLIC;
194 nu->bezt = bezt;
195
196 /* individual curve loop, start-end */
197 for (k = 0; k < n; k++) {
198 l = (j > 0) ? (k + ftoutline.contours[j - 1] + 1) : k;
199 if (k == 0) {
200 l_first = l;
201 }
202
203 /* virtual conic on-curve points */
204 {
205 const int l_next = (k < n - 1) ? (l + 1) : l_first;
206 if (ftoutline.tags[l] == FT_Curve_Tag_Conic &&
207 ftoutline.tags[l_next] == FT_Curve_Tag_Conic)
208 {
209 dx = float(ftoutline.points[l].x + ftoutline.points[l_next].x) * scale / 2.0f;
210 dy = float(ftoutline.points[l].y + ftoutline.points[l_next].y) * scale / 2.0f;
211
212 /* left handle */
213 bezt->vec[0][0] = (dx + (2.0f * float(ftoutline.points[l].x)) * scale) / 3.0f;
214 bezt->vec[0][1] = (dy + (2.0f * float(ftoutline.points[l].y)) * scale) / 3.0f;
215
216 /* midpoint (virtual on-curve point) */
217 bezt->vec[1][0] = dx;
218 bezt->vec[1][1] = dy;
219
220 /* right handle */
221 bezt->vec[2][0] = (dx + (2.0f * float(ftoutline.points[l_next].x)) * scale) / 3.0f;
222 bezt->vec[2][1] = (dy + (2.0f * float(ftoutline.points[l_next].y)) * scale) / 3.0f;
223
224 bezt->h1 = bezt->h2 = HD_ALIGN;
225 bezt->radius = 1.0f;
226 bezt++;
227 }
228 }
229
230 /* on-curve points */
231 if (ftoutline.tags[l] == FT_Curve_Tag_On) {
232 const int l_prev = (k > 0) ? (l - 1) : ftoutline.contours[j];
233 const int l_next = (k < n - 1) ? (l + 1) : l_first;
234
235 /* left handle */
236 if (ftoutline.tags[l_prev] == FT_Curve_Tag_Cubic) {
237 bezt->vec[0][0] = float(ftoutline.points[l_prev].x) * scale;
238 bezt->vec[0][1] = float(ftoutline.points[l_prev].y) * scale;
239 bezt->h1 = HD_FREE;
240 }
241 else if (ftoutline.tags[l_prev] == FT_Curve_Tag_Conic) {
242 bezt->vec[0][0] = (float(ftoutline.points[l].x) +
243 (2.0f * float(ftoutline.points[l_prev].x))) *
244 scale / 3.0f;
245 bezt->vec[0][1] = (float(ftoutline.points[l].y) +
246 (2.0f * float(ftoutline.points[l_prev].y))) *
247 scale / 3.0f;
248 bezt->h1 = HD_FREE;
249 }
250 else {
251 bezt->vec[0][0] = float(ftoutline.points[l].x) * scale -
252 (float(ftoutline.points[l].x) - float(ftoutline.points[l_prev].x)) *
253 scale / 3.0f;
254 bezt->vec[0][1] = float(ftoutline.points[l].y) * scale -
255 (float(ftoutline.points[l].y) - float(ftoutline.points[l_prev].y)) *
256 scale / 3.0f;
257 bezt->h1 = HD_VECT;
258 }
259
260 /* midpoint (on-curve point) */
261 bezt->vec[1][0] = float(ftoutline.points[l].x) * scale;
262 bezt->vec[1][1] = float(ftoutline.points[l].y) * scale;
263
264 /* right handle */
265 if (ftoutline.tags[l_next] == FT_Curve_Tag_Cubic) {
266 bezt->vec[2][0] = float(ftoutline.points[l_next].x) * scale;
267 bezt->vec[2][1] = float(ftoutline.points[l_next].y) * scale;
268 bezt->h2 = HD_FREE;
269 }
270 else if (ftoutline.tags[l_next] == FT_Curve_Tag_Conic) {
271 bezt->vec[2][0] = (float(ftoutline.points[l].x) +
272 (2.0f * float(ftoutline.points[l_next].x))) *
273 scale / 3.0f;
274 bezt->vec[2][1] = (float(ftoutline.points[l].y) +
275 (2.0f * float(ftoutline.points[l_next].y))) *
276 scale / 3.0f;
277 bezt->h2 = HD_FREE;
278 }
279 else {
280 bezt->vec[2][0] = float(ftoutline.points[l].x) * scale -
281 (float(ftoutline.points[l].x) - float(ftoutline.points[l_next].x)) *
282 scale / 3.0f;
283 bezt->vec[2][1] = float(ftoutline.points[l].y) * scale -
284 (float(ftoutline.points[l].y) - float(ftoutline.points[l_next].y)) *
285 scale / 3.0f;
286 bezt->h2 = HD_VECT;
287 }
288
289 /* get the handles that are aligned, tricky...
290 * - check if one of them is a vector handle.
291 * - dist_squared_to_line_v2, check if the three beztriple points are on one line
292 * - len_squared_v2v2, see if there's a distance between the three points
293 * - len_squared_v2v2 again, to check the angle between the handles
294 */
295 if ((bezt->h1 != HD_VECT && bezt->h2 != HD_VECT) &&
296 (dist_squared_to_line_v2(bezt->vec[0], bezt->vec[1], bezt->vec[2]) <
297 (0.001f * 0.001f)) &&
298 (len_squared_v2v2(bezt->vec[0], bezt->vec[1]) > eps_sq) &&
299 (len_squared_v2v2(bezt->vec[1], bezt->vec[2]) > eps_sq) &&
300 (len_squared_v2v2(bezt->vec[0], bezt->vec[2]) > eps_sq) &&
301 (len_squared_v2v2(bezt->vec[0], bezt->vec[2]) >
302 max_ff(len_squared_v2v2(bezt->vec[0], bezt->vec[1]),
303 len_squared_v2v2(bezt->vec[1], bezt->vec[2]))))
304 {
305 bezt->h1 = bezt->h2 = HD_ALIGN;
306 }
307 bezt->radius = 1.0f;
308 bezt++;
309 }
310 }
311 }
312
313 MEM_delete(onpoints);
314}
315
324static void blf_glyph_metrics_scale(FT_Glyph_Metrics &m, const FT_Long num, const FT_Long den)
325{
326 m.width = FT_MulDiv(m.width, num, den);
327 m.height = FT_MulDiv(m.height, num, den);
328 m.horiBearingX = FT_MulDiv(m.horiBearingX, num, den);
329 m.horiBearingY = FT_MulDiv(m.horiBearingY, num, den);
330 m.horiAdvance = FT_MulDiv(m.horiAdvance, num, den);
331 m.vertBearingX = FT_MulDiv(m.vertBearingX, num, den);
332 m.vertBearingY = FT_MulDiv(m.vertBearingY, num, den);
333 m.vertAdvance = FT_MulDiv(m.vertAdvance, num, den);
334}
335
336static FT_GlyphSlot blf_glyphslot_ensure_outline(FontBLF *font, uint charcode, bool use_fallback)
337{
338 if (charcode < 32) {
339 if (ELEM(charcode, 0x10, 0x13)) {
340 /* Do not render line feed or carriage return. #134972. */
341 return nullptr;
342 }
343 /* Other C0 controls (U+0000 - U+001F) can show as space. #135421. */
344 /* TODO: Return all but TAB as ".notdef" character when we have our own. */
345 charcode = ' ';
346 }
347
348 /* Glyph might not come from the initial font. */
349 FontBLF *font_with_glyph = font;
350 FT_UInt glyph_index = use_fallback ? blf_glyph_index_from_charcode(&font_with_glyph, charcode) :
351 blf_get_char_index(font_with_glyph, charcode);
352
353 if (!glyph_index) {
354 return nullptr;
355 }
356
357 if (!blf_ensure_face(font_with_glyph)) {
358 return nullptr;
359 }
360
361 FT_GlyphSlot glyph = blf_glyph_render_outline(font, font_with_glyph, glyph_index, charcode, 0);
362
363 if (font != font_with_glyph) {
364 if (!blf_ensure_face(font)) {
365 return nullptr;
366 }
367 const FT_Long num = FT_Long(font->face->units_per_EM);
368 const FT_Long den = FT_Long(font_with_glyph->face->units_per_EM);
369 const double ratio = double(num) / double(den);
370 const FT_Matrix transform = {to_16dot16(ratio), 0, 0, to_16dot16(ratio)};
371 FT_Outline_Transform(&glyph->outline, &transform);
372 glyph->advance.x = FT_Pos(double(glyph->advance.x) * ratio);
373 blf_glyph_metrics_scale(glyph->metrics, num, den);
374 }
375
376 return glyph;
377}
378
380 uint unicode,
381 ListBaseT<Nurb> *nurbsbase,
382 const float scale,
383 bool use_fallback,
384 float *r_advance,
385 rctf *r_bounds)
386{
387 FT_GlyphSlot glyph = blf_glyphslot_ensure_outline(font, unicode, use_fallback);
388 if (!glyph) {
389 *r_advance = 0.0f;
390 *r_bounds = {0.0f, 0.0f, 0.0f, 0.0f};
391 return false;
392 }
393
394 blf_glyph_to_curves(glyph->outline, nurbsbase, scale);
395 *r_advance = float(glyph->advance.x) * scale;
396
397 const FT_Glyph_Metrics &m = glyph->metrics;
398 r_bounds->xmin = float(m.horiBearingX) * scale;
399 r_bounds->xmax = float(m.horiBearingX + m.width) * scale;
400 r_bounds->ymin = float(m.horiBearingY - m.height) * scale;
401 r_bounds->ymax = float(m.horiBearingY) * scale;
402 return true;
403}
404
406
407} // namespace blender
Read Blender memory allocation and freeing.
#define ELEM(...)
float3x3 scale(float3x3 mat, float2 scale)
BMesh Iterators.
bool blf_character_to_curves(FontBLF *font, uint unicode, ListBaseT< Nurb > *nurbsbase, const float scale, bool use_fallback, float *r_advance, rctf *r_bounds)
bool blf_ensure_face(FontBLF *font)
Definition blf_font.cc:2087
unsigned int uint
Definition BKE_text.h:55
FT_GlyphSlot blf_glyph_render_outline(FontBLF *settings_font, FontBLF *glyph_font, FT_UInt glyph_index, uint charcode, int fixed_width)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
uint blf_get_char_index(FontBLF *font, const uint charcode)
Definition blf_font.cc:155
static void blf_glyph_metrics_scale(FT_Glyph_Metrics &m, const FT_Long num, const FT_Long den)
FT_UInt blf_glyph_index_from_charcode(FontBLF **font, const uint charcode)
Definition blf_glyph.cc:795
ATTR_WARN_UNUSED_RESULT const size_t num
static FT_GlyphSlot blf_glyphslot_ensure_outline(FontBLF *font, uint charcode, bool use_fallback)
static void blf_glyph_to_curves(const FT_Outline &ftoutline, ListBaseT< Nurb > *nurbsbase, const float scale)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
static FT_Fixed to_16dot16(const double val)
MINLINE float max_ff(float a, float b)
float dist_squared_to_line_v2(const float p[2], const float l1[2], const float l2[2])
Definition math_geom.cc:294
MINLINE float len_squared_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
const btScalar eps
Definition poly34.cpp:11
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eBezTriple_Handle h2
eBezTriple_Handle h1
BezTriple * bezt
eNurbKnotFlag flagu