|
| 1 | +#include "rive/renderer.hpp" |
| 2 | +#include "SkData.h" |
| 3 | + |
| 4 | +#include <ft2build.h> |
| 5 | +#include FT_FREETYPE_H |
| 6 | + |
| 7 | +#include <freetype/ftadvanc.h> |
| 8 | +#include <freetype/ftimage.h> |
| 9 | +#include <freetype/ftbitmap.h> |
| 10 | +#ifdef FT_COLOR_H // 2.10.0 |
| 11 | +# include <freetype/ftcolor.h> |
| 12 | +#endif |
| 13 | +#include <freetype/freetype.h> |
| 14 | +#include <freetype/ftlcdfil.h> |
| 15 | +#include <freetype/ftmodapi.h> |
| 16 | +#include <freetype/ftmm.h> |
| 17 | +#include <freetype/ftoutln.h> |
| 18 | +#include <freetype/ftsizes.h> |
| 19 | +#include <freetype/ftsystem.h> |
| 20 | +#include <freetype/tttables.h> |
| 21 | +#include <freetype/t1tables.h> |
| 22 | +#include <freetype/ftfntfmt.h> |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +typedef struct FT_LibraryRec_* FT_Library; |
| 28 | +typedef struct FT_FaceRec_* FT_Face; |
| 29 | +typedef struct FT_StreamRec_* FT_Stream; |
| 30 | +typedef signed long FT_Pos; |
| 31 | +typedef struct FT_BBox_ FT_BBox; |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +struct FTLib { |
| 36 | + FT_Library m_Lib; |
| 37 | + |
| 38 | + FTLib() : m_Lib(nullptr) { |
| 39 | + int err = FT_Init_FreeType(&m_Lib); |
| 40 | + if (err) { |
| 41 | + printf("FT_Init_FreeType returned %d\n", err); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + FT_Add_Default_Modules(m_Lib); |
| 46 | + FT_Set_Default_Properties(m_Lib); |
| 47 | + } |
| 48 | + ~FTLib() { |
| 49 | + if (m_Lib) { |
| 50 | + FT_Done_Library(m_Lib); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + operator bool() const { return m_Lib != nullptr; } |
| 55 | +}; |
| 56 | + |
| 57 | +struct FTFace { |
| 58 | + sk_sp<SkData> m_Data; |
| 59 | + FT_Face m_Face; |
| 60 | + |
| 61 | + FTFace() : m_Face(nullptr) {} |
| 62 | + ~FTFace() { |
| 63 | + if (m_Face) { |
| 64 | + FT_Done_Face(m_Face); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + bool load(FT_Library lib, sk_sp<SkData> data) { |
| 69 | + int face_index = 0; // todo |
| 70 | + int err = FT_New_Memory_Face(lib, (const FT_Byte*)data->data(), |
| 71 | + data->size(), face_index, &m_Face); |
| 72 | + if (err) { |
| 73 | + printf("FT_New_Memory_Face returned %d\n", err); |
| 74 | + return false; |
| 75 | + } |
| 76 | + m_Data = std::move(data); |
| 77 | + return true; |
| 78 | + } |
| 79 | +}; |
| 80 | + |
| 81 | +void test_fonts(const char path[]) { |
| 82 | + FTLib lib; |
| 83 | + if (!lib) { |
| 84 | + printf("failed to init freetype\n"); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + auto data = SkData::MakeFromFileName(path); |
| 89 | + if (!data) { |
| 90 | + printf("failed to load file %s\n", path); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + FTFace face; |
| 95 | + if (!face.load(lib.m_Lib, data)) { |
| 96 | + printf("failed to load file\n"); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + printf("%s has %ld glyphs and %d upem\n", |
| 101 | + path, face.m_Face->num_glyphs, face.m_Face->units_per_EM); |
| 102 | +} |
0 commit comments