From f15be89fa4419ed4a6ced9dbfb22d1d2d90fb907 Mon Sep 17 00:00:00 2001 From: themancalledjakob Date: Wed, 22 Feb 2023 18:28:56 +0100 Subject: [PATCH] revert msdfgen modification, emscriptenify atlas --- src/Atlas.cpp | 46 +++++++++++++++++++++++++++++++++++++--------- src/Atlas.h | 10 +++++++++- src/conversion.cpp | 6 +++--- src/conversion.h | 2 +- src/ofxMsdfgen.h | 1 + 5 files changed, 51 insertions(+), 14 deletions(-) diff --git a/src/Atlas.cpp b/src/Atlas.cpp index f5490de..1216ffe 100644 --- a/src/Atlas.cpp +++ b/src/Atlas.cpp @@ -1,5 +1,14 @@ #include "Atlas.h" #include "BitmapRef.hpp" +#include "GlyphBox.h" +#include "ofFileUtils.h" +#include "ofTrueTypeFont.h" + + +ofxMsdfgen::Atlas::Atlas(){ +} +ofxMsdfgen::Atlas::~Atlas(){ +} void ofxMsdfgen::Atlas::setup(string _fontPath){ fontPath = _fontPath; @@ -18,10 +27,12 @@ void ofxMsdfgen::Atlas::setup(string _fontPath, bool ofxMsdfgen::Atlas::generate(){ bool success = false; - // Initialize instance of FreeType library - if(msdfgen::FreetypeHandle * ft = msdfgen::initializeFreetype()){ - // Load font file - if(msdfgen::FontHandle * font = msdfgen::loadFont(ft, fontPath.c_str())){ + //{ + msdfgen::FreetypeHandle * ft = msdfgen::initializeFreetype(); + if(ft){ + const char * fontPath_c_str = fontPath.c_str(); + msdfgen::FontHandle * font = loadFont(ft, fontPath_c_str); + if(font){ // Storage for glyph geometry and their coordinates in the atlas std::vector glyphs; // FontGeometry is a helper class that loads a set of glyphs from a single font. @@ -55,10 +66,10 @@ bool ofxMsdfgen::Atlas::generate(){ 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 - 3, // number of atlas color channels - & msdf_atlas::msdfGenerator, // function to generate bitmaps for individual glyphs - msdf_atlas::BitmapAtlasStorage // class that stores the atlas bitmap + float, // pixel type of buffer for individual glyphs depends on generator function + 3, // number of atlas color channels + & msdf_atlas::msdfGenerator, // function to generate bitmaps for individual glyphs + msdf_atlas::BitmapAtlasStorage // 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. @@ -72,10 +83,27 @@ bool ofxMsdfgen::Atlas::generate(){ msdfgen::Bitmap bitmap(generator.atlasStorage()); ofxMsdfgen::toOfImage(bitmap, atlasImage); + std::vector layout = generator.getLayout(); + int i = 0; + cout << "glyphs.size() = " << ofToString(glyphs.size()) << endl; + for(const msdf_atlas::GlyphBox & gb : layout){ + msdf_atlas::Rectangle rect = gb.rect; + msdf_atlas::GlyphGeometry gg = glyphs.data()[i]; + cout << "glyphbox " << ofToString(i) << "/" << layout.size() << endl; + cout + << "\t\tindex: " << gb.index << endl + << "\t\tadvance: " << gb.advance << endl + << "\t\tr x: " << rect.x << endl + << "\t\tr y: " << rect.y << endl + << "\t\tr w: " << rect.w << endl + << "\t\tr h: " << rect.h << endl; + i++; + } + // Cleanup msdfgen::destroyFont(font); - success = true; // FIXME: always turns true, why do we have this + success = true; // FIXME: always turns true, why do we have this } msdfgen::deinitializeFreetype(ft); } diff --git a/src/Atlas.h b/src/Atlas.h index 5c81267..06097bd 100644 --- a/src/Atlas.h +++ b/src/Atlas.h @@ -1,6 +1,8 @@ #pragma once #include "ofMain.h" +#include "ofTrueTypeFont.h" +#include #include #include "conversion.h" @@ -10,10 +12,16 @@ struct AtlasSettings { float minimumScale = 24.0; float pixelRange = 2.0; float miterLimit = 1.0; - int threadCount = 4; + #ifdef TARGET_EMSCRIPTEN + int threadCount = 1; + #else + int threadCount = 4; + #endif }; class Atlas { public: + Atlas(); + ~Atlas(); void setup(string _fontPath); void setup(string _fontPath, AtlasSettings _settings); bool generate(); diff --git a/src/conversion.cpp b/src/conversion.cpp index b9f021f..3404188 100644 --- a/src/conversion.cpp +++ b/src/conversion.cpp @@ -1,9 +1,9 @@ #include "conversion.h" -void ofxMsdfgen::toOfImage(msdfgen::Bitmap bitmap, +void ofxMsdfgen::toOfImage(const msdfgen::Bitmap bitmap, ofFloatImage & image){ image.allocate(bitmap.width(), bitmap.height(), OF_IMAGE_COLOR); - memcpy(image.getPixels().getData(), static_cast (bitmap), bitmap.width() * bitmap.height() * 3 * sizeof(float)); + memcpy(image.getPixels().getData(), static_cast (bitmap), bitmap.width() * bitmap.height() * 3 * sizeof(float)); image.update(); // float is slow here } @@ -32,7 +32,7 @@ void ofxMsdfgen::toOfImage(const msdfgen::Bitmap bitmap, void ofxMsdfgen::toOfImage(const msdfgen::Bitmap bitmap, ofImage & image){ image.allocate(bitmap.width(), bitmap.height(), OF_IMAGE_COLOR); - memcpy(image.getPixels().getData(), bitmap.getPixels(), bitmap.width() * bitmap.height() * 3 * sizeof(byte)); + memcpy(image.getPixels().getData(), static_cast (bitmap), bitmap.width() * bitmap.height() * 3 * sizeof(byte)); image.update(); } diff --git a/src/conversion.h b/src/conversion.h index 4710cb0..c1ab334 100644 --- a/src/conversion.h +++ b/src/conversion.h @@ -6,7 +6,7 @@ #include namespace ofxMsdfgen { -void toOfImage(msdfgen::Bitmap bitmap, +void toOfImage(const msdfgen::Bitmap bitmap, ofFloatImage & image); void toOfImage(const msdfgen::Bitmap bitmap, ofImage & image); diff --git a/src/ofxMsdfgen.h b/src/ofxMsdfgen.h index fcb9fab..08740a0 100644 --- a/src/ofxMsdfgen.h +++ b/src/ofxMsdfgen.h @@ -3,6 +3,7 @@ #include "BitmapAtlasStorage.h" #include "GlyphGeometry.h" #include "ofMain.h" +#include "freetype2/freetype/freetype.h" #include #include "Atlas.h" #include "conversion.h"