Skip to content

Commit 6a792e2

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 3e5b592 commit 6a792e2

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

src/ft2font.cpp

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -318,29 +318,13 @@ void FT2Font::set_kerning_factor(int factor)
318318
}
319319
}
320320

321-
void FT2Font::set_text(
322-
std::u32string_view text, double angle, FT_Int32 flags,
321+
std::vector<raqm_glyph_t> FT2Font::layout(
322+
std::u32string_view text, FT_Int32 flags,
323323
std::optional<std::vector<std::string>> features, LanguageType languages,
324-
std::vector<double> &xys)
324+
std::set<FT_String*>& glyph_seen_fonts)
325325
{
326-
FT_Matrix matrix; /* transformation matrix */
327-
328-
angle = angle * (2 * M_PI / 360.0);
329-
330-
// this computes width and height in subpixels so we have to multiply by 64
331-
double cosangle = cos(angle) * 0x10000L;
332-
double sinangle = sin(angle) * 0x10000L;
333-
334-
matrix.xx = (FT_Fixed)cosangle;
335-
matrix.xy = (FT_Fixed)-sinangle;
336-
matrix.yx = (FT_Fixed)sinangle;
337-
matrix.yy = (FT_Fixed)cosangle;
338-
339326
clear();
340327

341-
bbox.xMin = bbox.yMin = 32000;
342-
bbox.xMax = bbox.yMax = -32000;
343-
344328
auto rq = raqm_create();
345329
if (!rq) {
346330
throw std::runtime_error("failed to compute text layout");
@@ -387,7 +371,6 @@ void FT2Font::set_text(
387371
}
388372

389373
std::vector<std::pair<size_t, const FT_Face&>> face_substitutions;
390-
std::set<FT_String*> glyph_seen_fonts;
391374
glyph_seen_fonts.insert(face->family_name);
392375

393376
// Attempt to use fallback fonts if necessary.
@@ -474,9 +457,34 @@ void FT2Font::set_text(
474457
size_t num_glyphs = 0;
475458
auto const& rq_glyphs = raqm_get_glyphs(rq, &num_glyphs);
476459

477-
for (size_t i = 0; i < num_glyphs; i++) {
478-
auto const& rglyph = rq_glyphs[i];
460+
return std::vector<raqm_glyph_t>(rq_glyphs, rq_glyphs + num_glyphs);
461+
}
462+
463+
void FT2Font::set_text(
464+
std::u32string_view text, double angle, FT_Int32 flags,
465+
std::optional<std::vector<std::string>> features, LanguageType languages,
466+
std::vector<double> &xys)
467+
{
468+
FT_Matrix matrix; /* transformation matrix */
469+
470+
angle = angle * (2 * M_PI / 360.0);
471+
472+
// this computes width and height in subpixels so we have to multiply by 64
473+
double cosangle = cos(angle) * 0x10000L;
474+
double sinangle = sin(angle) * 0x10000L;
475+
476+
matrix.xx = (FT_Fixed)cosangle;
477+
matrix.xy = (FT_Fixed)-sinangle;
478+
matrix.yx = (FT_Fixed)sinangle;
479+
matrix.yy = (FT_Fixed)cosangle;
480+
481+
bbox.xMin = bbox.yMin = 32000;
482+
bbox.xMax = bbox.yMax = -32000;
483+
484+
std::set<FT_String*> glyph_seen_fonts;
485+
auto rq_glyphs = layout(text, flags, features, languages, glyph_seen_fonts);
479486

487+
for (auto const& rglyph : rq_glyphs) {
480488
// Warn for missing glyphs.
481489
if (rglyph.index == 0) {
482490
ft_glyph_warn(text[rglyph.cluster], glyph_seen_fonts);

src/ft2font.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ class FT2Font
113113
void set_size(double ptsize, double dpi);
114114
void set_charmap(int i);
115115
void select_charmap(unsigned long i);
116+
std::vector<raqm_glyph_t> layout(std::u32string_view text, FT_Int32 flags,
117+
std::optional<std::vector<std::string>> features,
118+
LanguageType languages,
119+
std::set<FT_String*>& glyph_seen_fonts);
116120
void set_text(std::u32string_view codepoints, double angle, FT_Int32 flags,
117121
std::optional<std::vector<std::string>> features,
118122
LanguageType languages, std::vector<double> &xys);

0 commit comments

Comments
 (0)