Skip to content

Commit

Permalink
Added readFreetypeOutline to font import
Browse files Browse the repository at this point in the history
  • Loading branch information
Chlumsky committed Feb 18, 2023
1 parent 7ff249b commit eeec8f2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
37 changes: 19 additions & 18 deletions ext/import-font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,24 @@ FontHandle * adoptFreetypeFont(FT_Face ftFace) {
return handle;
}

FT_Error readFreetypeOutline(Shape &output, FT_Outline *outline) {
output.contours.clear();
output.inverseYAxis = false;
FtContext context = { };
context.shape = &output;
FT_Outline_Funcs ftFunctions;
ftFunctions.move_to = &ftMoveTo;
ftFunctions.line_to = &ftLineTo;
ftFunctions.conic_to = &ftConicTo;
ftFunctions.cubic_to = &ftCubicTo;
ftFunctions.shift = 0;
ftFunctions.delta = 0;
FT_Error error = FT_Outline_Decompose(outline, &ftFunctions, &context);
if (!output.contours.empty() && output.contours.back().edges.empty())
output.contours.pop_back();
return error;
}

FontHandle * loadFont(FreetypeHandle *library, const char *filename) {
if (!library)
return NULL;
Expand Down Expand Up @@ -181,26 +199,9 @@ bool loadGlyph(Shape &output, FontHandle *font, GlyphIndex glyphIndex, double *a
FT_Error error = FT_Load_Glyph(font->face, glyphIndex.getIndex(), FT_LOAD_NO_SCALE);
if (error)
return false;
output.contours.clear();
output.inverseYAxis = false;
if (advance)
*advance = F26DOT6_TO_DOUBLE(font->face->glyph->advance.x);

FtContext context = { };
context.shape = &output;
FT_Outline_Funcs ftFunctions;
ftFunctions.move_to = &ftMoveTo;
ftFunctions.line_to = &ftLineTo;
ftFunctions.conic_to = &ftConicTo;
ftFunctions.cubic_to = &ftCubicTo;
ftFunctions.shift = 0;
ftFunctions.delta = 0;
error = FT_Outline_Decompose(&font->face->glyph->outline, &ftFunctions, &context);
if (error)
return false;
if (!output.contours.empty() && output.contours.back().edges.empty())
output.contours.pop_back();
return true;
return !readFreetypeOutline(output, &font->face->glyph->outline);
}

bool loadGlyph(Shape &output, FontHandle *font, unicode_t unicode, double *advance) {
Expand Down
5 changes: 4 additions & 1 deletion ext/import-font.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ FreetypeHandle * initializeFreetype();
/// Deinitializes the FreeType library.
void deinitializeFreetype(FreetypeHandle *library);

#ifdef FT_FREETYPE_H
#ifdef FT_LOAD_DEFAULT // FreeType included
/// Creates a FontHandle from FT_Face that was loaded by the user. destroyFont must still be called but will not affect the FT_Face.
FontHandle * adoptFreetypeFont(FT_Face ftFace);
/// Converts the geometry of FreeType's FT_Outline to a Shape object.
FT_Error readFreetypeOutline(Shape &output, FT_Outline *outline);
#endif

/// Loads a font file and returns its handle.
FontHandle * loadFont(FreetypeHandle *library, const char *filename);
/// Loads a font from binary data and returns its handle.
Expand Down

0 comments on commit eeec8f2

Please sign in to comment.