Added readFreetypeOutline to font import

This commit is contained in:
Chlumsky 2023-02-18 11:43:15 +01:00
parent 7ff249bcd4
commit eeec8f222d
2 changed files with 23 additions and 19 deletions

View File

@ -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;
@ -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) {

View File

@ -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.