TrueType-Like Fonts

Monkey Forums/Monkey Programming/TrueType-Like Fonts

c.k.(Posted 2012) [#1]
Is there any functionality out there for monkey to let us use cross-platform, sizable fonts? Something with an interface like

[monkeycode]
Local myFont:Font = New Font("Times New Roman")
Local fontSize:Int = 12
PlotText "My Test", myFont, fontSize, xpos, ypos ...
[/monkeycode]

Using bitmap fonts seems too restrictive, especially for cross-platform work. I guess if you don't make any dynamic elements to your interface it doesn't matter... :-/


marksibly(Posted 2012) [#2]
Hi,

There is no sizeable font support. Something like freetype would need to be written 'from the ground up' in Monkey to be able to achieve this on all targets, and no one's taken that on as yet...


TheRedFox(Posted 2012) [#3]
Well, I am using Pharo Smalltalk (http://pharo-project.org) and there is a package in there that load and shows TrueType on the display. This is the software-only TT renderer (TTGlyph and TTKernPair). It can read a .ttf font directly.

It uses a drawing engine underneath but nothing Monkey cannot do.

A screenshot of my environment along with some inspectors open:



Obviously, porting all of this Smalltalk code down to Monkey will be quite an undertaking. Smalltalk has a tendency to gear towards terse and short code that requires much much more in other languages.

But for learning how to deal with TTFonts, this is the best way to go as you are dealing with a live system.

Truth be told, I am working on some kind of Pharo to Monkey translator, which is in turn translating to native code on the Monkey supported platforms. This is based on what we call 'Slang', a subset of Smalltalk that generates C-code for the Smalltalk interpeter VM. So I am making a kind of 'Mlang'.

Anybody wanting my Pharo image along with the VM, just send me a mail to: phil@...


Ben(Posted 2012) [#4]
I'm interested in trying to program a TTF class. I want to be able to include it in the future. My issue is I'm not sure of how to understand how to load it. If you could supply me with the required info I might be able to work some magic... Meanwhile I'll try and find out the info as well as work around the project.


AdamRedwoods(Posted 2012) [#5]
truly noble.

https://developer.apple.com/fonts/TTRefMan/RM06/Chap6.html

and OTF...
http://www.microsoft.com/typography/otspec/otff.htm


Ben(Posted 2012) [#6]
This is quite the challenge. I think that I'd actually like to explore integrating the native classes that are set up first (that stuff is scary O.O). I'm thinking that it might be better to use what each language already has instead of reinventing the wheel. Do you know of a good example of externing a class? I've tried:
Class ExUITextField="UITextField"
End
but I can't init that. (ios target btw) I'm trying to redo what mojo does but that is also a challenge. It would be awesome if we could do a project like this for each language. The way I see it, it needs to have a set number of proscribed functions as well. At the very least, monkey should provide access to native text classes/functions.
If I have to I'll read through the docs and try to build it from scratch but I don't think that, that's the most elegant solution in this case.


AdamRedwoods(Posted 2012) [#7]
i have a forum post on GC somehwere, which shows a bit of Class-extern shananigans and how to handle these things.

Not an easy thing to do unfortunately. TTF is NOT easy either. Definitely use the native libraries for each OS, i think they exist for each platform. You could probably create a TTF char set to a bitmap and send that to monkey... but you will reach hardships with the monkey<-->native barrier (and GC). That said, since the databuffers are updated V66+, you should use those for loading raw bitmap data to images (and the rest handled by monkey).
(ie, native ttf font->native bitmap-> native databuffer->monkey)

Personally, I'm looking into SVG which might be able to handle vector fonts.


impixi(Posted 2012) [#8]

Personally, I'm looking into SVG which might be able to handle vector fonts.



For x-platform that's not a bad idea. According to wikipedia there's some level of SVG support (text element though?) in most mobile platforms, but I get the impression it is referring to browser (rather than native) support.

How far have you looked into it?

Regarding HTML5 vector text support: that's trivial. It's built into the canvas element, for most of the desktop browsers at least. I posted a little wrapper in the archives: http://www.monkeycoder.co.nz/Community/posts.php?topic=3804. In my tests it's significantly faster than DrawText and seems to respect all of Mojo's other commands (color, alpha, scale, rotation, etc) as you'd expect. Don't know for mobile platforms though.


Ben(Posted 2012) [#9]
I think that I could handle flash a lot easier but I really want to get this working for ios. It's a pretty big limitation that this is not an option for monkey. For all the ios devices, being able to scale text is important and with that in mind, using bitmap fonts is horrible. There's also the other side to consider, the option that we have right now (AngelFont) uses a lot more processing than it should.

What I'm thinking is looking into making a class that's pretty much function based. I've tried directly linking but as you probably know/would figure, this does not work.

How would you go about rendering an externed class? Does it have to be implemented already in the native code or is there some way to create a new class then start rendering it?

Edit: I've found the reference that AdamRedwoods mentioned. http://monkeycoder.co.nz/Community/posts.php?topic=1412#12807 This is similar to the example that is given in the monkey documentation but the difference is this one shows what is expected on the native side of the code. This is good.


AdamRedwoods(Posted 2012) [#10]
Externing can get complicated very quickly when garbage collection is not automatic in the native language. If you are using just functions, this is fine. But if you are using classes, beware of the consequences.

For multiple fonts, have you tried using large fonts, then reducing the size?
This is where I would start. Does it look bad on retina displays?

Even with Flash (the Adobe suite), I still use bitmapped fonts at times over vector fonts since you can get it very crisp with PNG.


Ben(Posted 2012) [#11]
The problem that I see with using bitmaps is twofold and one contributes to the other. The first issue like you mentioned is how crisp the text is. As you say I'm sure that it can be made to be more crisp. I suppose that this can lead to two different issues however. Either you only use a few fonts and just do a ton of scaling. This however can really hurt performance. The other side of it is if you use multiple fonts you have an issue of drastically increasing the filesize. Either way, good luck in giving it a smooth transition between the fonts.

I'm not so concerned with garbage collection at this point. I know that it's important, however, I'm more concerned with something actually working.

There's only been two advantages with using bitmaps that I can see. 1 is the bitmap uses less processing and 2 you can make more complex things with it. The advantage with the speed is lost with scaling.

If someone has a simple example of the rendering aspect of an externed class or am I naive in thinking that there's a simple solution.


Ben(Posted 2013) [#12]
So I've looked over what AdamRedwoods has supplied and I've found several problems.

1) The only way to load raw byte data is through a DataBuffer. The problem with this is it only provides a very limited format with loading data. I don't think that I can load shorts or longs or especially unsigned anything. Unless I'm completely out of there, unless I can properly scan through it with the data types that it set up with, I won't be able to properly load TTF fonts.

2) When I try to load a TTF file using the brl.DataBuffer with html5 I get this error: "Monkey Runtime Error : TypeError: Cannot call method 'setInt8' of undefined"


Playniax(Posted 2013) [#13]
The Ignition framework comes with a tool that converts ttf files so that the Ignition font loader can handle them.

I have also created a batch tool/script that can convert a whole directory at once in size you set.

I will try to release the batch tool in the next update.




Ben(Posted 2013) [#14]
That thought actually crossed my mind as a solution. Although it was heading down some extremely unfamiliar territory for me. It's too bad it doesn't make it into a true vector based font.


Ben(Posted 2013) [#15]
To further elaborate what I was thinking. I don't see any way to properly load a ttf file so I think that a new format needs to be made for monkey. Maybe MTF for Monkey Type Font. So a tool probably in java needs to be made to convert ttf to MTF. It would then be awesome if this could be combined to work with bitmap fonts. I feel that something more could be done with AngelFont to give it better performance.

The main thing that I need to figure out is to understand exactly what the binary data means.


AdamRedwoods(Posted 2013) [#16]
You know, with all the embedding/distribution issues with fonts these days, it may be safer just to stick with an external bitmap glyph maker. Flash is having these difficulties as well related to licensing issues.


Beaker(Posted 2013) [#17]
Ben - How can I give AngelFont better performance? I'm all ears. :)


Ben(Posted 2013) [#18]
Well, I was thinking that for static text fields you could just paint all the letters into a single picture. You could potentially do this with words as well. Pretty much the only options I can think of will increase the memory requirements. Another thing that I'm thinking of is you can try to dice the picture up into each letter then get rid of the original pict. Then again I'm not sure how this would improve the performance.

On critique that I have for your AngelFont is it's not arranged to work like a normal textfield. I had to do my own rearranging to get more comfortable with it but that's just me.

AdamRedwoods just because there's licensing issues doesn't mean you should avoid ttf altogether, the option should be there.

PS Sorry for the late reply, I didn't notice the posts because of the lack of notification. I also just noticed the subscribe link.


Beaker(Posted 2013) [#19]
Ben - if you take a look at the latest version in the angelfont thread, you will see that you can already cache any words or lines of text to an atlas. Don't forget you can also just not re-render the whole screen when nothing is animated. Dicing the picture up will only slow things down on most targets, and yield no improvement on others.

As regards the textfield critique, the whole angelfont thing was only meant as a starting point for you to build on.


Ben(Posted 2013) [#20]
I see, I hadn't realized that you were still working on the class. Do you have a default link for the current code and also a stable form? Cleaver work with the multichannel aspect although would it be possible to stack all letters together with a slight difference in the channel?

As for the TTF side of things. If someone would be interested with the binary side of things then I can look into the rastering side of things.

Btw, have you figured out the scale aspect of things as far as word wrapping is concerned?


Nobuyuki(Posted 2013) [#21]
Is AngelFont still being worked on? I don't mean to hijack the thread, but I've been toiling away at adding full Unicode support to it, and the original code feels like it could use a bit of an overhaul! I would like to consolidate this work with the official branch, if I can, but it's a lot of work for just one person and frankly, I made the code an even bigger mess than before by adding in my UTF8String overrides. It feels like it might take some community involvement.

There's several things about the code choices made in AngelFont I'd like to discuss, if the module is still being actively developed. When I'm ready to announce my UTF8 work (it should be sometime today!), I'll lay them out there. Needless to say, even before hacking in full Unicode, I'd made a wrapper for AngelFont+SimpleTextBox because the object model seems to have changed a lot over time and has become a bit.... inconsistent?

Edit: I've uploaded my module and laid out my thoughts here. http://www.monkeycoder.co.nz/Community/posts.php?topic=4878


Ben(Posted 2013) [#22]
The last post by beaker that I could find that I think is probably the right place is post #58 http://www.monkeycoder.co.nz/Community/posts.php?topic=141

I agree that a lot more work needs to be done with the code. For example I did some work making it so letters could be partially rendered if clipping is needed. Not to say that I'm hatting. It's cool that you've made something like this Beaker.


Nobuyuki(Posted 2013) [#23]
seeing as how there appears to be a master branch right now (that's different from the version that comes with monkey), and also a branch with those particular packing additions, I think it might be wise to github this thing up so that it will be easier to merge all these changes later on..