Skip to content

Commit 73441fc

Browse files
committed
ft2font: Split layouting from set_text
The former may be used even on PS/PDF backend where nothing is rendered.
1 parent 45a7668 commit 73441fc

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

src/ft2font.cpp

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -401,27 +401,12 @@ void FT2Font::set_kerning_factor(int factor)
401401
}
402402
}
403403

404-
void FT2Font::set_text(
405-
std::u32string_view text, double angle, FT_Int32 flags, std::vector<double> &xys)
404+
void FT2Font::layout(std::u32string_view text, FT_Int32 flags,
405+
std::set<FT_String*>& glyph_seen_fonts,
406+
std::vector<raqm_glyph_t>& glyphs)
406407
{
407-
FT_Matrix matrix; /* transformation matrix */
408-
409-
angle = angle * (2 * M_PI / 360.0);
410-
411-
// this computes width and height in subpixels so we have to multiply by 64
412-
double cosangle = cos(angle) * 0x10000L;
413-
double sinangle = sin(angle) * 0x10000L;
414-
415-
matrix.xx = (FT_Fixed)cosangle;
416-
matrix.xy = (FT_Fixed)-sinangle;
417-
matrix.yx = (FT_Fixed)sinangle;
418-
matrix.yy = (FT_Fixed)cosangle;
419-
420408
clear();
421409

422-
bbox.xMin = bbox.yMin = 32000;
423-
bbox.xMax = bbox.yMax = -32000;
424-
425410
auto rq = raqm_create();
426411
if (!rq) {
427412
throw std::runtime_error("failed to compute text layout");
@@ -449,7 +434,6 @@ void FT2Font::set_text(
449434
}
450435

451436
std::vector<std::pair<size_t, const FT_Face&>> face_substitutions;
452-
std::set<FT_String*> glyph_seen_fonts;
453437
glyph_seen_fonts.insert(face->family_name);
454438

455439
// Attempt to use fallback fonts if necessary.
@@ -503,9 +487,34 @@ void FT2Font::set_text(
503487
size_t num_glyphs = 0;
504488
auto const& rq_glyphs = raqm_get_glyphs(rq, &num_glyphs);
505489

506-
for (size_t i = 0; i < num_glyphs; i++) {
507-
auto const& rglyph = rq_glyphs[i];
490+
glyphs.resize(num_glyphs);
491+
memcpy(glyphs.data(), rq_glyphs, sizeof(raqm_glyph_t) * num_glyphs);
492+
}
493+
494+
void FT2Font::set_text(
495+
std::u32string_view text, double angle, FT_Int32 flags, std::vector<double> &xys)
496+
{
497+
FT_Matrix matrix; /* transformation matrix */
498+
499+
angle = angle * (2 * M_PI / 360.0);
500+
501+
// this computes width and height in subpixels so we have to multiply by 64
502+
double cosangle = cos(angle) * 0x10000L;
503+
double sinangle = sin(angle) * 0x10000L;
504+
505+
matrix.xx = (FT_Fixed)cosangle;
506+
matrix.xy = (FT_Fixed)-sinangle;
507+
matrix.yx = (FT_Fixed)sinangle;
508+
matrix.yy = (FT_Fixed)cosangle;
509+
510+
bbox.xMin = bbox.yMin = 32000;
511+
bbox.xMax = bbox.yMax = -32000;
512+
513+
std::set<FT_String*> glyph_seen_fonts;
514+
std::vector<raqm_glyph_t> rq_glyphs;
515+
layout(text, flags, glyph_seen_fonts, rq_glyphs);
508516

517+
for (auto const& rglyph : rq_glyphs) {
509518
// Warn for missing glyphs.
510519
if (rglyph.index == 0) {
511520
ft_glyph_warn(text[rglyph.cluster], glyph_seen_fonts);

src/ft2font.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ class FT2Font
8080
void set_size(double ptsize, double dpi);
8181
void set_charmap(int i);
8282
void select_charmap(unsigned long i);
83+
void layout(std::u32string_view text, FT_Int32 flags,
84+
std::set<FT_String*>& glyph_seen_fonts,
85+
std::vector<raqm_glyph_t> &glyphs);
8386
void set_text(std::u32string_view codepoints, double angle, FT_Int32 flags,
8487
std::vector<double> &xys);
8588
int get_kerning(FT_UInt left, FT_UInt right, FT_Kerning_Mode mode, bool fallback);

0 commit comments

Comments
 (0)