From 494a1e702d7b2a892902e746b5470e7fe51377a2 Mon Sep 17 00:00:00 2001 From: themancalledjakob Date: Mon, 27 Feb 2023 16:24:14 +0100 Subject: [PATCH] load charset --- src/Atlas.cpp | 48 ++++++++++++++++++++++++++++++++-------------- src/Atlas.h | 5 +++++ src/conversion.cpp | 4 ++-- src/conversion.h | 5 +++++ 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/src/Atlas.cpp b/src/Atlas.cpp index 1216ffe..2a88e77 100644 --- a/src/Atlas.cpp +++ b/src/Atlas.cpp @@ -1,9 +1,13 @@ #include "Atlas.h" #include "BitmapRef.hpp" #include "GlyphBox.h" +#include "conversion.h" +#include "edge-coloring.h" +#include "import-font.h" #include "ofFileUtils.h" #include "ofTrueTypeFont.h" - +#include +#include ofxMsdfgen::Atlas::Atlas(){ } @@ -42,9 +46,30 @@ bool ofxMsdfgen::Atlas::generate(){ // 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); + if(settings.characters == "charset::ascii"){ + fontGeometry.loadCharset(font, 1.0, msdf_atlas::Charset::ASCII); + }else if(settings.characters == ""){ + msdfgen::GlyphIndex glyphIndex; + }else{ + msdf_atlas::Charset charset; + for(const unsigned char c : settings.characters){ + msdf_atlas::unicode_t ut(c); + cout << "add char(" << c << " " << ofToString(ut) << ")" << endl; + charset.add(ut); + cout << "charset.size: " << charset.size() << endl; + } + fontGeometry.loadCharset(font, 1.0, charset); + if(glyphs.size() != settings.characters.length()){ + fontGeometry.getGlyphs().empty(); + fontGeometry.loadCharset(font, 1.0, charset, false); + } + } // Apply MSDF edge coloring. See edge-coloring.h for other coloring strategies. for(msdf_atlas::GlyphGeometry & glyph : glyphs){ + //glyph.getShape().inverseYAxis = true; + //FIXME: we do not need to inverse y axis, something else is wrong + msdfgen::Shape & shape = const_cast (glyph.getShape()); + shape.inverseYAxis = true; glyph.edgeColoring(&msdfgen::edgeColoringInkTrap, settings.maxCornerAngle, 0); @@ -55,7 +80,8 @@ bool ofxMsdfgen::Atlas::generate(){ // setDimensions or setDimensionsConstraint to find the best value packer.setDimensionsConstraint(msdf_atlas::TightAtlasPacker::DimensionsConstraint::SQUARE); // setScale for a fixed size or setMinimumScale to use the largest that fits - packer.setMinimumScale(settings.minimumScale); + //packer.setMinimumScale(settings.minimumScale); + packer.setScale(settings.scale); // setPixelRange or setUnitRange packer.setPixelRange(settings.pixelRange); packer.setMiterLimit(settings.miterLimit); @@ -85,21 +111,11 @@ bool ofxMsdfgen::Atlas::generate(){ 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++; + glyphGeometries.push_back(gg); } - // Cleanup msdfgen::destroyFont(font); @@ -113,3 +129,7 @@ bool ofxMsdfgen::Atlas::generate(){ const ofImage & ofxMsdfgen::Atlas::getAtlasImage(){ return atlasImage; } + +const vector & ofxMsdfgen::Atlas::getGlyphGeometries(){ + return glyphGeometries; +} diff --git a/src/Atlas.h b/src/Atlas.h index 06097bd..a7d7e25 100644 --- a/src/Atlas.h +++ b/src/Atlas.h @@ -4,14 +4,17 @@ #include "ofTrueTypeFont.h" #include #include +#include #include "conversion.h" namespace ofxMsdfgen { struct AtlasSettings { float maxCornerAngle = 3.0; + float scale = 256.0; float minimumScale = 24.0; float pixelRange = 2.0; float miterLimit = 1.0; + string characters = "charset::ascii"; #ifdef TARGET_EMSCRIPTEN int threadCount = 1; #else @@ -26,9 +29,11 @@ class Atlas { void setup(string _fontPath, AtlasSettings _settings); bool generate(); const ofImage & getAtlasImage(); + const vector & getGlyphGeometries(); AtlasSettings settings; private: string fontPath; ofImage atlasImage; + vector glyphGeometries; }; } diff --git a/src/conversion.cpp b/src/conversion.cpp index 3404188..806eda1 100644 --- a/src/conversion.cpp +++ b/src/conversion.cpp @@ -13,8 +13,8 @@ void ofxMsdfgen::toOfImage(const msdfgen::Bitmap bitmap, ofPixels & pixels = image.getPixels(); int w = bitmap.width(); int h = bitmap.height(); - for(int y = 0; y < h; y++){ - for(int x = 0; x < w; x++){ + for(int y = 0; y < h; ++y){ + for(int x = 0; x < w; ++x){ const float * rgb = bitmap(x, y); int index = 3 * (x + y * w); pixels.getData()[index] = (unsigned char)std::clamp(rgb[0] * 256.0, diff --git a/src/conversion.h b/src/conversion.h index c1ab334..28da6a7 100644 --- a/src/conversion.h +++ b/src/conversion.h @@ -1,11 +1,16 @@ #pragma once +#include "GlyphBox.h" +#include "glyph-generators.h" #include "ofMain.h" #include "BitmapAtlasStorage.h" #include "GlyphGeometry.h" #include namespace ofxMsdfgen { +using GlyphBox = msdf_atlas::GlyphBox; +using GlyphGeometry = msdf_atlas::GlyphGeometry; + void toOfImage(const msdfgen::Bitmap bitmap, ofFloatImage & image); void toOfImage(const msdfgen::Bitmap bitmap,