Skip to content

Commit b35e5cd

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 44c17bc commit b35e5cd

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-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
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");
@@ -375,7 +359,6 @@ void FT2Font::set_text(
375359
}
376360

377361
std::vector<std::pair<size_t, const FT_Face&>> face_substitutions;
378-
std::set<FT_String*> glyph_seen_fonts;
379362
glyph_seen_fonts.insert(face->family_name);
380363

381364
// Attempt to use fallback fonts if necessary.
@@ -452,9 +435,34 @@ void FT2Font::set_text(
452435
size_t num_glyphs = 0;
453436
auto const& rq_glyphs = raqm_get_glyphs(rq, &num_glyphs);
454437

455-
for (size_t i = 0; i < num_glyphs; i++) {
456-
auto const& rglyph = rq_glyphs[i];
438+
return std::vector<raqm_glyph_t>(rq_glyphs, rq_glyphs + num_glyphs);
439+
}
440+
441+
void FT2Font::set_text(
442+
std::u32string_view text, double angle, FT_Int32 flags,
443+
LanguageType languages,
444+
std::vector<double> &xys)
445+
{
446+
FT_Matrix matrix; /* transformation matrix */
447+
448+
angle = angle * (2 * M_PI / 360.0);
449+
450+
// this computes width and height in subpixels so we have to multiply by 64
451+
double cosangle = cos(angle) * 0x10000L;
452+
double sinangle = sin(angle) * 0x10000L;
453+
454+
matrix.xx = (FT_Fixed)cosangle;
455+
matrix.xy = (FT_Fixed)-sinangle;
456+
matrix.yx = (FT_Fixed)sinangle;
457+
matrix.yy = (FT_Fixed)cosangle;
458+
459+
std::set<FT_String*> glyph_seen_fonts;
460+
auto rq_glyphs = layout(text, flags, languages, glyph_seen_fonts);
461+
462+
bbox.xMin = bbox.yMin = 32000;
463+
bbox.xMax = bbox.yMax = -32000;
457464

465+
for (auto const& rglyph : rq_glyphs) {
458466
// Warn for missing glyphs.
459467
if (rglyph.index == 0) {
460468
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
@@ -113,6 +113,9 @@ 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+
LanguageType languages,
118+
std::set<FT_String*>& glyph_seen_fonts);
116119
void set_text(std::u32string_view codepoints, double angle, FT_Int32 flags,
117120
LanguageType languages, std::vector<double> &xys);
118121
int get_kerning(FT_UInt left, FT_UInt right, FT_Kerning_Mode mode);

0 commit comments

Comments
 (0)