revert msdfgen modification, emscriptenify atlas

This commit is contained in:
jrkb 2023-02-22 18:28:56 +01:00
parent 39f2373958
commit f15be89fa4
5 changed files with 51 additions and 14 deletions

View file

@ -1,5 +1,14 @@
#include "Atlas.h" #include "Atlas.h"
#include "BitmapRef.hpp" #include "BitmapRef.hpp"
#include "GlyphBox.h"
#include "ofFileUtils.h"
#include "ofTrueTypeFont.h"
ofxMsdfgen::Atlas::Atlas(){
}
ofxMsdfgen::Atlas::~Atlas(){
}
void ofxMsdfgen::Atlas::setup(string _fontPath){ void ofxMsdfgen::Atlas::setup(string _fontPath){
fontPath = _fontPath; fontPath = _fontPath;
@ -18,10 +27,12 @@ void ofxMsdfgen::Atlas::setup(string _fontPath,
bool ofxMsdfgen::Atlas::generate(){ bool ofxMsdfgen::Atlas::generate(){
bool success = false; bool success = false;
// Initialize instance of FreeType library //{
if(msdfgen::FreetypeHandle * ft = msdfgen::initializeFreetype()){ msdfgen::FreetypeHandle * ft = msdfgen::initializeFreetype();
// Load font file if(ft){
if(msdfgen::FontHandle * font = msdfgen::loadFont(ft, fontPath.c_str())){ 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 // Storage for glyph geometry and their coordinates in the atlas
std::vector <msdf_atlas::GlyphGeometry> glyphs; std::vector <msdf_atlas::GlyphGeometry> glyphs;
// FontGeometry is a helper class that loads a set of glyphs from a single font. // 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); packer.getDimensions(width, height);
// The ImmediateAtlasGenerator class facilitates the generation of the atlas bitmap. // The ImmediateAtlasGenerator class facilitates the generation of the atlas bitmap.
msdf_atlas::ImmediateAtlasGenerator < msdf_atlas::ImmediateAtlasGenerator <
float, // pixel type of buffer for individual glyphs depends on generator function float, // pixel type of buffer for individual glyphs depends on generator function
3, // number of atlas color channels 3, // number of atlas color channels
& msdf_atlas::msdfGenerator, // function to generate bitmaps for individual glyphs & msdf_atlas::msdfGenerator, // function to generate bitmaps for individual glyphs
msdf_atlas::BitmapAtlasStorage <float, 3> // class that stores the atlas bitmap msdf_atlas::BitmapAtlasStorage <float, 3> // class that stores the atlas bitmap
// For example, a custom atlas storage class that stores it in VRAM can be used. // For example, a custom atlas storage class that stores it in VRAM can be used.
> generator(width, height); > generator(width, height);
// GeneratorAttributes can be modified to change the generator's default settings. // GeneratorAttributes can be modified to change the generator's default settings.
@ -72,10 +83,27 @@ bool ofxMsdfgen::Atlas::generate(){
msdfgen::Bitmap <float, 3> bitmap(generator.atlasStorage()); msdfgen::Bitmap <float, 3> bitmap(generator.atlasStorage());
ofxMsdfgen::toOfImage(bitmap, atlasImage); ofxMsdfgen::toOfImage(bitmap, atlasImage);
std::vector <msdf_atlas::GlyphBox> 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 // Cleanup
msdfgen::destroyFont(font); 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); msdfgen::deinitializeFreetype(ft);
} }

View file

@ -1,6 +1,8 @@
#pragma once #pragma once
#include "ofMain.h" #include "ofMain.h"
#include "ofTrueTypeFont.h"
#include <freetype2/freetype/freetype.h>
#include <msdf-atlas-gen/msdf-atlas-gen.h> #include <msdf-atlas-gen/msdf-atlas-gen.h>
#include "conversion.h" #include "conversion.h"
@ -10,10 +12,16 @@ struct AtlasSettings {
float minimumScale = 24.0; float minimumScale = 24.0;
float pixelRange = 2.0; float pixelRange = 2.0;
float miterLimit = 1.0; float miterLimit = 1.0;
int threadCount = 4; #ifdef TARGET_EMSCRIPTEN
int threadCount = 1;
#else
int threadCount = 4;
#endif
}; };
class Atlas { class Atlas {
public: public:
Atlas();
~Atlas();
void setup(string _fontPath); void setup(string _fontPath);
void setup(string _fontPath, AtlasSettings _settings); void setup(string _fontPath, AtlasSettings _settings);
bool generate(); bool generate();

View file

@ -1,9 +1,9 @@
#include "conversion.h" #include "conversion.h"
void ofxMsdfgen::toOfImage(msdfgen::Bitmap <float, 3> bitmap, void ofxMsdfgen::toOfImage(const msdfgen::Bitmap <float, 3> bitmap,
ofFloatImage & image){ ofFloatImage & image){
image.allocate(bitmap.width(), bitmap.height(), OF_IMAGE_COLOR); image.allocate(bitmap.width(), bitmap.height(), OF_IMAGE_COLOR);
memcpy(image.getPixels().getData(), static_cast <float *>(bitmap), bitmap.width() * bitmap.height() * 3 * sizeof(float)); memcpy(image.getPixels().getData(), static_cast <const float *>(bitmap), bitmap.width() * bitmap.height() * 3 * sizeof(float));
image.update(); // float is slow here image.update(); // float is slow here
} }
@ -32,7 +32,7 @@ void ofxMsdfgen::toOfImage(const msdfgen::Bitmap <float, 3> bitmap,
void ofxMsdfgen::toOfImage(const msdfgen::Bitmap <byte, 3> bitmap, void ofxMsdfgen::toOfImage(const msdfgen::Bitmap <byte, 3> bitmap,
ofImage & image){ ofImage & image){
image.allocate(bitmap.width(), bitmap.height(), OF_IMAGE_COLOR); 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 <const byte *>(bitmap), bitmap.width() * bitmap.height() * 3 * sizeof(byte));
image.update(); image.update();
} }

View file

@ -6,7 +6,7 @@
#include <msdf-atlas-gen/msdf-atlas-gen.h> #include <msdf-atlas-gen/msdf-atlas-gen.h>
namespace ofxMsdfgen { namespace ofxMsdfgen {
void toOfImage(msdfgen::Bitmap <float, 3> bitmap, void toOfImage(const msdfgen::Bitmap <float, 3> bitmap,
ofFloatImage & image); ofFloatImage & image);
void toOfImage(const msdfgen::Bitmap <float, 3> bitmap, void toOfImage(const msdfgen::Bitmap <float, 3> bitmap,
ofImage & image); ofImage & image);

View file

@ -3,6 +3,7 @@
#include "BitmapAtlasStorage.h" #include "BitmapAtlasStorage.h"
#include "GlyphGeometry.h" #include "GlyphGeometry.h"
#include "ofMain.h" #include "ofMain.h"
#include "freetype2/freetype/freetype.h"
#include <msdf-atlas-gen/msdf-atlas-gen.h> #include <msdf-atlas-gen/msdf-atlas-gen.h>
#include "Atlas.h" #include "Atlas.h"
#include "conversion.h" #include "conversion.h"