danomatika / swig-openframeworks

a SWIG interface for openFrameworks with included Makefile, submodule this in your language wrapper addons
Other
39 stars 11 forks source link

Enable drawing unicode strings by default #21

Closed cuinjune closed 5 years ago

cuinjune commented 5 years ago

Hi, I wanted to use unicode strings in my ofxLua based project and I learned that these unicode related classes are disabled by default. So I opened openframeworks/graphics.i file and removed the following lines of code.

// DIFF:   ignoring ofUnicode and ofAlphabet
%ignore ofUnicode;
%ignore ofAlphabet;
%ignore ofUnicode::range; // nested struct
// DIFF:   ignoring ofTrueTypeFontSettings struct
%ignore ofTrueTypeFontSettings;
%ignore ofTrueTypeFont::Settings;
%ignore ofTrueTypeFont::load(const ofTrueTypeFontSettings &);
%ignore ofTrueTypeFont::setDirection(ofTrueTypeFontDirection);

And then generated bindings using make desktop LANG=lua NAME=ofBindings. When I rebuild my project using the new bindings, I could successfully draw a Korean text.

Screen Shot 2019-04-30 at 9 10 05 PM

May I ask why some of these classes are disabled in swig interface by default even they seem to work fine? Thank you in advance!

danomatika commented 5 years ago

I often don't have time to wrap more complicated/obscure parts of Openframeworks, so I simply ignore them. In the past, I have used unicode just fine via UTF8 in simple strings but nothing too complicated. What is your use case? Do you have some sample code of how this works as I've never used ofUnicode or ofAlphabet.

cuinjune commented 5 years ago

I just wanted to draw non-english texts in ofxLua project. Here's my sample code.

ofApp.cpp file:

//--------------------------------------------------------------
void ofApp::setup() {
    lua.init(true);
    lua.addListener(this);
    lua.doScript("scripts/unicodeExample.lua", true);
    lua.scriptSetup();
}

//--------------------------------------------------------------
void ofApp::update() {
    lua.scriptUpdate();
}

//--------------------------------------------------------------
void ofApp::draw() {
    lua.scriptDraw();
}

//--------------------------------------------------------------
void ofApp::exit() {
    lua.scriptExit();
    lua.clear();
}

unicodeExample.lua file:

font = of.TrueTypeFont()

----------------------------------------------------
function setup()
    of.setWindowTitle("unicode example")
    of.setBackgroundColor(0, 0, 0)
    local settings = of.TrueTypeFontSettings("fonts/gungsuh.ttf", 30)
    settings:addRange(of.Unicode.HangulSyllables)
    font:load(settings)
end

----------------------------------------------------
function update()
end

----------------------------------------------------
function draw()
    of.setColor(255, 255, 255)
    font:drawString("안녕하세요", 100, 100)
end

----------------------------------------------------
function exit()
    print("script finished")
end

It all seem to work fine except when using of.TrueTypeFontSettings:addRanges() function which takes an argument std::initializer_list<ofUnicode::range> alphabet but I think users can just use addRange() multiple times instead. So I think it's probably okay to not ignore generating these bindings in the graphics.i interface file. It is just my small opinion.

danomatika commented 5 years ago

Ok, that makes sense.

It all seem to work fine except when using of.TrueTypeFontSettings:addRanges() function which takes an argument std::initializer_list<ofUnicode::range> alphabet

Yeah, it's things like this which I ignore unless I have to deal with them as wrapping CPP iterators, etc is a pain in the ass. We simply use the basic function alternatives.

danomatika commented 5 years ago

Fixed in edf4ddc95c479eaa5daf3acf8e4d1701cd6e1bbd.

Note, the ofAlphabet range naming is slightly different: of.Alphabet_Korean, of.Alphabet_Latin, etc as this is faked using an enum instead of the std::initializer_list type.

danomatika commented 5 years ago

Also, you can now use addRanges:

settings:addRanges(of.Alphabet_Korean)
settings:addRanges(of.Alphabet_Latin)
...
font:drawString("hello world", 100, 100)
font:drawString("안녕하세요", 100, 150)