load charset

This commit is contained in:
jrkb 2023-02-27 16:24:14 +01:00
parent 7b47d9b743
commit 494a1e702d
4 changed files with 46 additions and 16 deletions

View file

@ -1,9 +1,13 @@
#include "Atlas.h" #include "Atlas.h"
#include "BitmapRef.hpp" #include "BitmapRef.hpp"
#include "GlyphBox.h" #include "GlyphBox.h"
#include "conversion.h"
#include "edge-coloring.h"
#include "import-font.h"
#include "ofFileUtils.h" #include "ofFileUtils.h"
#include "ofTrueTypeFont.h" #include "ofTrueTypeFont.h"
#include <cstdlib>
#include <set>
ofxMsdfgen::Atlas::Atlas(){ 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. // 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. // In the last argument, you can specify a charset other than ASCII.
// To load specific glyph indices, use loadGlyphs instead. // To load specific glyph indices, use loadGlyphs instead.
if(settings.characters == "charset::ascii"){
fontGeometry.loadCharset(font, 1.0, msdf_atlas::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. // Apply MSDF edge coloring. See edge-coloring.h for other coloring strategies.
for(msdf_atlas::GlyphGeometry & glyph : glyphs){ 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 <msdfgen::Shape &>(glyph.getShape());
shape.inverseYAxis = true;
glyph.edgeColoring(&msdfgen::edgeColoringInkTrap, glyph.edgeColoring(&msdfgen::edgeColoringInkTrap,
settings.maxCornerAngle, settings.maxCornerAngle,
0); 0);
@ -55,7 +80,8 @@ bool ofxMsdfgen::Atlas::generate(){
// setDimensions or setDimensionsConstraint to find the best value // setDimensions or setDimensionsConstraint to find the best value
packer.setDimensionsConstraint(msdf_atlas::TightAtlasPacker::DimensionsConstraint::SQUARE); packer.setDimensionsConstraint(msdf_atlas::TightAtlasPacker::DimensionsConstraint::SQUARE);
// setScale for a fixed size or setMinimumScale to use the largest that fits // 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 // setPixelRange or setUnitRange
packer.setPixelRange(settings.pixelRange); packer.setPixelRange(settings.pixelRange);
packer.setMiterLimit(settings.miterLimit); packer.setMiterLimit(settings.miterLimit);
@ -85,21 +111,11 @@ bool ofxMsdfgen::Atlas::generate(){
std::vector <msdf_atlas::GlyphBox> layout = generator.getLayout(); std::vector <msdf_atlas::GlyphBox> layout = generator.getLayout();
int i = 0; int i = 0;
cout << "glyphs.size() = " << ofToString(glyphs.size()) << endl;
for(const msdf_atlas::GlyphBox & gb : layout){ for(const msdf_atlas::GlyphBox & gb : layout){
msdf_atlas::Rectangle rect = gb.rect;
msdf_atlas::GlyphGeometry gg = glyphs.data()[i]; 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++; i++;
glyphGeometries.push_back(gg);
} }
// Cleanup // Cleanup
msdfgen::destroyFont(font); msdfgen::destroyFont(font);
@ -113,3 +129,7 @@ bool ofxMsdfgen::Atlas::generate(){
const ofImage & ofxMsdfgen::Atlas::getAtlasImage(){ const ofImage & ofxMsdfgen::Atlas::getAtlasImage(){
return atlasImage; return atlasImage;
} }
const vector <ofxMsdfgen::GlyphGeometry> & ofxMsdfgen::Atlas::getGlyphGeometries(){
return glyphGeometries;
}

View file

@ -4,14 +4,17 @@
#include "ofTrueTypeFont.h" #include "ofTrueTypeFont.h"
#include <freetype2/freetype/freetype.h> #include <freetype2/freetype/freetype.h>
#include <msdf-atlas-gen/msdf-atlas-gen.h> #include <msdf-atlas-gen/msdf-atlas-gen.h>
#include <msdf-atlas-gen/types.h>
#include "conversion.h" #include "conversion.h"
namespace ofxMsdfgen { namespace ofxMsdfgen {
struct AtlasSettings { struct AtlasSettings {
float maxCornerAngle = 3.0; float maxCornerAngle = 3.0;
float scale = 256.0;
float minimumScale = 24.0; float minimumScale = 24.0;
float pixelRange = 2.0; float pixelRange = 2.0;
float miterLimit = 1.0; float miterLimit = 1.0;
string characters = "charset::ascii";
#ifdef TARGET_EMSCRIPTEN #ifdef TARGET_EMSCRIPTEN
int threadCount = 1; int threadCount = 1;
#else #else
@ -26,9 +29,11 @@ class Atlas {
void setup(string _fontPath, AtlasSettings _settings); void setup(string _fontPath, AtlasSettings _settings);
bool generate(); bool generate();
const ofImage & getAtlasImage(); const ofImage & getAtlasImage();
const vector <GlyphGeometry> & getGlyphGeometries();
AtlasSettings settings; AtlasSettings settings;
private: private:
string fontPath; string fontPath;
ofImage atlasImage; ofImage atlasImage;
vector <GlyphGeometry> glyphGeometries;
}; };
} }

View file

@ -13,8 +13,8 @@ void ofxMsdfgen::toOfImage(const msdfgen::Bitmap <float, 3> bitmap,
ofPixels & pixels = image.getPixels(); ofPixels & pixels = image.getPixels();
int w = bitmap.width(); int w = bitmap.width();
int h = bitmap.height(); int h = bitmap.height();
for(int y = 0; y < h; y++){ for(int y = 0; y < h; ++y){
for(int x = 0; x < w; x++){ for(int x = 0; x < w; ++x){
const float * rgb = bitmap(x, y); const float * rgb = bitmap(x, y);
int index = 3 * (x + y * w); int index = 3 * (x + y * w);
pixels.getData()[index] = (unsigned char)std::clamp(rgb[0] * 256.0, pixels.getData()[index] = (unsigned char)std::clamp(rgb[0] * 256.0,

View file

@ -1,11 +1,16 @@
#pragma once #pragma once
#include "GlyphBox.h"
#include "glyph-generators.h"
#include "ofMain.h" #include "ofMain.h"
#include "BitmapAtlasStorage.h" #include "BitmapAtlasStorage.h"
#include "GlyphGeometry.h" #include "GlyphGeometry.h"
#include <msdf-atlas-gen/msdf-atlas-gen.h> #include <msdf-atlas-gen/msdf-atlas-gen.h>
namespace ofxMsdfgen { namespace ofxMsdfgen {
using GlyphBox = msdf_atlas::GlyphBox;
using GlyphGeometry = msdf_atlas::GlyphGeometry;
void toOfImage(const 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,