list font variation axes

This commit is contained in:
jrkb 2023-04-14 18:24:04 +02:00
parent bd81be30df
commit c071d3bd9c

View file

@ -2,6 +2,7 @@
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include <iostream>
#include <set> #include <set>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <ft2build.h> #include <ft2build.h>
@ -22,6 +23,16 @@ struct FontVariation {
float value; 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 <class T> template <class T>
inline void hash_combine(std::size_t & seed, const T & v){ inline void hash_combine(std::size_t & seed, const T & v){
std::hash <T> hasher; std::hash <T> hasher;
@ -71,4 +82,56 @@ void getVFlip(const VFlipBehaviour & vFlipBehaviour,
VFlipState & vFlip); VFlipState & vFlip);
static bool listFontVariationAxes(FT_Library & library,
FT_Face & face,
std::vector <FontVariationAxis> & 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 <FontVariationAxis> & 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;
}
} }