diff --git a/src/Utils.h b/src/Utils.h index 60a3ce6..971c954 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -22,6 +23,16 @@ struct FontVariation { float value; }; +#ifndef F26DOT6_TO_DOUBLE + #define F26DOT6_TO_DOUBLE(x) (1 / 64. * double(x)) +#endif +#ifndef F16DOT16_TO_DOUBLE + #define F16DOT16_TO_DOUBLE(x) (1 / 65536. * double(x)) +#endif +#ifndef DOUBLE_TO_F16DOT16 + #define DOUBLE_TO_F16DOT16(x) FT_Fixed(65536. * x) +#endif + template inline void hash_combine(std::size_t & seed, const T & v){ std::hash hasher; @@ -71,4 +82,56 @@ void getVFlip(const VFlipBehaviour & vFlipBehaviour, VFlipState & vFlip); +static bool listFontVariationAxes(FT_Library & library, + FT_Face & face, + std::vector & axes){ + if(face->face_flags & FT_FACE_FLAG_MULTIPLE_MASTERS){ + FT_MM_Var * master = NULL; + if(FT_Get_MM_Var(face, &master)){ + return false; + } + axes.resize(master->num_axis); + for(FT_UInt i = 0; i < master->num_axis; i++){ + FontVariationAxis & axis = axes[i]; + axis.name = master->axis[i].name; + axis.minValue = F16DOT16_TO_DOUBLE(master->axis[i].minimum); + axis.maxValue = F16DOT16_TO_DOUBLE(master->axis[i].maximum); + axis.defaultValue = F16DOT16_TO_DOUBLE(master->axis[i].def); + } + FT_Done_MM_Var(library, master); + return true; + } + return false; +} + +static bool listFontVariationAxes(std::string fontPath, + std::vector & axes){ + FT_Library library; /* handle to library */ + FT_Face face; /* handle to face object */ + + FT_Error error = FT_Init_FreeType(&library); + if(error){ + FT_Done_FreeType(library); + return false; + } + error = FT_New_Face(library, + fontPath.c_str(), + 0, + &face); + if(error == FT_Err_Unknown_File_Format){ + FT_Done_Face(face); + FT_Done_FreeType(library); + return false; + }else if(error){ + FT_Done_Face(face); + FT_Done_FreeType(library); + return false; + } + bool success = listFontVariationAxes(library, face, axes); + // clean the fuck up + FT_Done_Face(face); + FT_Done_FreeType(library); + return success; +} + }