diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-02-04 19:17:59 -0500 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-02-04 19:17:59 -0500 |
| commit | 61e179f9580c985cb5ca80ea732fc7572d31c489 (patch) | |
| tree | 122ff15c6d94ce5aebff4ff6f1fc6d13317abe16 /editor | |
| parent | a3fb454f9935960dc2d367455f891d8fedfb9016 (diff) | |
pke: chkpt: text rendering, no errors but blank
Diffstat (limited to 'editor')
| -rw-r--r-- | editor/editor.cpp | 163 |
1 files changed, 93 insertions, 70 deletions
diff --git a/editor/editor.cpp b/editor/editor.cpp index 5cb6a9d..c0eafcb 100644 --- a/editor/editor.cpp +++ b/editor/editor.cpp @@ -715,80 +715,103 @@ void RecordImGuiEntityTypes() { ImGui::End(); } +int SortFontGlyphChar(const void *a, const void *b) { + return ((FontGlyphChar*)a)->unicode > ((FontGlyphChar*)b)->unicode; +} + void GenerateMTSDF(const Asset *a) { assert(PK_HAS_FLAG(a->type, PKE_ASSET_TYPE_FONT)); + NULL_CHAR_ARR(path_txtr, 128); + NULL_CHAR_ARR(path_glyphs, 128); std::error_code e; - std::filesystem::create_directory("./cache", e); + std::filesystem::create_directory("./font", e); + AssetHandle ah_image = AssetHandle_MAX; + AssetHandle ah_glyphs = AssetHandle_MAX; + snprintf(path_txtr, 128, "%s/%s.%s", "./font", a->key, "png"); + snprintf(path_glyphs, 128, "%s/%s.%s", "./font", a->key, "glyph"); if (msdfgen::FreetypeHandle *ft = msdfgen::initializeFreetype()) { - // Load font file - if (msdfgen::FontHandle *font = msdfgen::loadFont(ft, a->basePath)) { - // Storage for glyph geometry and their coordinates in the atlas - std::vector<msdf_atlas::GlyphGeometry> glyphs; - // FontGeometry is a helper class that loads a set of glyphs from a single font. - // It can also be used to get additional font metrics, kerning information, etc. - msdf_atlas::FontGeometry fontGeometry(&glyphs); - // Load a set of character glyphs: - // The second argument can be ignored unless you mix different font sizes in one atlas. - // In the last argument, you can specify a charset other than ASCII. - // To load specific glyph indices, use loadGlyphs instead. - fontGeometry.loadCharset(font, 1.0, msdf_atlas::Charset::ASCII); - // Apply MSDF edge coloring. See edge-coloring.h for other coloring strategies. - const double maxCornerAngle = 3.0; - for (msdf_atlas::GlyphGeometry &glyph : glyphs) - glyph.edgeColoring(&msdfgen::edgeColoringInkTrap, maxCornerAngle, 0); - // TightAtlasPacker class computes the layout of the atlas. - msdf_atlas::TightAtlasPacker packer; - // Set atlas parameters: - // setDimensions or setDimensionsConstraint to find the best value - packer.setDimensionsConstraint(msdf_atlas::DimensionsConstraint::SQUARE); - // setScale for a fixed size or setMinimumScale to use the largest that fits - packer.setMinimumScale(24.0); - // setPixelRange or setUnitRange - packer.setPixelRange(2.0); - packer.setMiterLimit(1.0); - // Compute atlas layout - pack glyphs - packer.pack(glyphs.data(), glyphs.size()); - // Get final atlas dimensions - int width = 0, height = 0; - packer.getDimensions(width, height); - // The ImmediateAtlasGenerator class facilitates the generation of the atlas bitmap. - msdf_atlas::ImmediateAtlasGenerator< - float, // pixel type of buffer for individual glyphs depends on generator function - 4, // number of atlas color channels - &msdf_atlas::mtsdfGenerator, // function to generate bitmaps for individual glyphs - msdf_atlas::BitmapAtlasStorage<float, 4> // class that stores the atlas bitmap - // For example, a custom atlas storage class that stores it in VRAM can be used. - > generator(width, height); - // GeneratorAttributes can be modified to change the generator's default settings. - msdf_atlas::GeneratorAttributes attributes; - generator.setAttributes(attributes); - // generator.setThreadCount(4); - // Generate atlas bitmap - generator.generate(glyphs.data(), glyphs.size()); - // The atlas bitmap can now be retrieved via atlasStorage as a BitmapConstRef. - // The glyphs array (or fontGeometry) contains positioning data for typesetting text. - msdf_atlas::BitmapAtlasStorage<float, 4> storage = generator.atlasStorage(); - msdf_atlas::saveImage<4>(storage, msdf_atlas::ImageFormat::PNG, "cache/test-font.png", msdf_atlas::YDirection::TOP_DOWN); - - auto f = fopen("cache/test-font.glyphs", "w"); - pk_arr arr_glyphs = {}; - arr_glyphs.alignment = 8; - arr_glyphs.stride = sizeof(FontGlyphChar); - pk_arr_resize(&arr_glyphs, glyphs.size()); - FontGlyphChar *arr = reinterpret_cast<FontGlyphChar *>(arr_glyphs.data); - for (uint64_t i = 0; i < glyphs.size(); ++i) { - arr[i].unicode = glyphs[i].getCodepoint(); - glyphs[i].getBoxRect(arr[i].bounds.x, arr[i].bounds.y, arr[i].bounds.z, arr[i].bounds.a); - } - - fwrite(glyphs.data(), sizeof(msdf_atlas::GlyphGeometry) * glyphs.size(), 1, f); - fclose(f); - // _ = myProject::submitAtlasBitmapAndLayout(generator.atlasStorage(), glyphs); - // Cleanup - msdfgen::destroyFont(font); - } - msdfgen::deinitializeFreetype(ft); - } + // Load font file + if (msdfgen::FontHandle *font = msdfgen::loadFont(ft, a->basePath)) { + // Storage for glyph geometry and their coordinates in the atlas + std::vector<msdf_atlas::GlyphGeometry> glyphs; + // FontGeometry is a helper class that loads a set of glyphs from a single font. + // It can also be used to get additional font metrics, kerning information, etc. + msdf_atlas::FontGeometry fontGeometry(&glyphs); + // Load a set of character glyphs: + // The second argument can be ignored unless you mix different font sizes in one atlas. + // In the last argument, you can specify a charset other than ASCII. + // To load specific glyph indices, use loadGlyphs instead. + fontGeometry.loadCharset(font, 1.0, msdf_atlas::Charset::ASCII); + // Apply MSDF edge coloring. See edge-coloring.h for other coloring strategies. + const double maxCornerAngle = 3.0; + for (msdf_atlas::GlyphGeometry &glyph : glyphs) + glyph.edgeColoring(&msdfgen::edgeColoringInkTrap, maxCornerAngle, 0); + // TightAtlasPacker class computes the layout of the atlas. + msdf_atlas::TightAtlasPacker packer; + // Set atlas parameters: + // setDimensions or setDimensionsConstraint to find the best value + packer.setDimensionsConstraint(msdf_atlas::DimensionsConstraint::SQUARE); + // setScale for a fixed size or setMinimumScale to use the largest that fits + packer.setMinimumScale(24.0); + // setPixelRange or setUnitRange + packer.setPixelRange(2.0); + packer.setMiterLimit(1.0); + // Compute atlas layout - pack glyphs + packer.pack(glyphs.data(), glyphs.size()); + // Get final atlas dimensions + int width = 0, height = 0; + packer.getDimensions(width, height); + // The ImmediateAtlasGenerator class facilitates the generation of the atlas bitmap. + msdf_atlas::ImmediateAtlasGenerator< + float, // pixel type of buffer for individual glyphs depends on generator function + 4, // number of atlas color channels + &msdf_atlas::mtsdfGenerator, // function to generate bitmaps for individual glyphs + msdf_atlas::BitmapAtlasStorage<float, 4> // class that stores the atlas bitmap + // For example, a custom atlas storage class that stores it in VRAM can be used. + > generator(width, height); + // GeneratorAttributes can be modified to change the generator's default settings. + msdf_atlas::GeneratorAttributes attributes; + generator.setAttributes(attributes); + // generator.setThreadCount(4); + // Generate atlas bitmap + generator.generate(glyphs.data(), glyphs.size()); + // The atlas bitmap can now be retrieved via atlasStorage as a BitmapConstRef. + // The glyphs array (or fontGeometry) contains positioning data for typesetting text. + msdf_atlas::BitmapAtlasStorage<float, 4> storage = generator.atlasStorage(); + msdf_atlas::saveImage<4>(storage, msdf_atlas::ImageFormat::PNG, path_txtr, msdf_atlas::YDirection::TOP_DOWN); + ah_image = AM_Register(path_txtr, PKE_ASSET_TYPE_UNSET); + + auto f = fopen(path_glyphs, "w+"); + pk_arr arr_glyphs = {}; + arr_glyphs.alignment = 8; + arr_glyphs.stride = sizeof(FontGlyphChar); + pk_arr_resize(&arr_glyphs, glyphs.size()); + FontGlyphChar *arr = reinterpret_cast<FontGlyphChar *>(arr_glyphs.data); + for (uint64_t i = 0; i < glyphs.size(); ++i) { + arr[i].unicode = glyphs[i].getCodepoint(); + glyphs[i].getBoxRect(arr[i].bounds.x, arr[i].bounds.y, arr[i].bounds.z, arr[i].bounds.a); + } + qsort(arr_glyphs.data, arr_glyphs.next, arr_glyphs.stride, SortFontGlyphChar); + fwrite(arr_glyphs.data, arr_glyphs.stride, arr_glyphs.next, f); + fclose(f); + ah_glyphs = AM_Register(path_glyphs, PKE_ASSET_TYPE_UNSET); + + // Cleanup + msdfgen::destroyFont(font); + } else { + fprintf(stderr, "failed to load font"); + return; + } + msdfgen::deinitializeFreetype(ft); + } + pk_cstr font_title; + font_title.length = strlen(a->key); + font_title.reserved = font_title.length + 1; + // TODO specific bucket? + font_title.val = pk_new<char>(font_title.reserved); + snprintf((char *)font_title.val, AssetKeyLength, "%s", a->key); + assert(ah_image != ah_glyphs && ah_image != AssetHandle_MAX); + FontType_RegisterFont(font_title, ah_image, ah_glyphs); } void RecordImGuiAssets() { |
