OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 6:58 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: How rounding in TrueType hinting programs works?
PostPosted: Mon May 25, 2020 7:00 am 
Offline
Member
Member
User avatar

Joined: Mon Jan 15, 2018 2:27 pm
Posts: 201
Hi. I am working on implementing my own TrueType rasterizer for my OS. I got it mostly working but now I am stuck on point coordinate rounding in hinting programs. I have troubles understanding how exactly rounding routine should work.

For example, let's focus on RDTG instruction. Both, Microsoft and Apple docs provide examples of it's usage. But the problem is that they both show pretty special case instead of general one. Namely:
Image
Please note that both freedom and projection vectors are axis aligned and are both the same.
But what would RDTG instruction do if this was not the case? For example, if F and P vectors were not axis aligned and not the same. Plus, there is that thing called minimum_distance. What is it for?


Top
 Profile  
 
 Post subject: Re: How rounding in TrueType hinting programs works?
PostPosted: Tue May 26, 2020 5:05 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5137
pvc wrote:
But what would RDTG instruction do if this was not the case? For example, if F and P vectors were not axis aligned and not the same.

From what I gather, you would round the point by moving it along the freedom vector. I'm no TrueType expert, though.

pvc wrote:
Plus, there is that thing called minimum_distance. What is it for?

When rounding, points might get closer together. This is used to keep them from getting too close together.


Top
 Profile  
 
 Post subject: Re: How rounding in TrueType hinting programs works?
PostPosted: Tue May 26, 2020 7:57 am 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1584
Before you dig too deep, you should know that most fonts don't have hinting instructions, or a very mediocre ones at bests. Only professional fonts have good hinting code, but they are not for free. Good hinting generators are very (very) expensive proprietary tools, and there are no good Open Source alternatives. Even the best FOSS alternative generates terrible hinting code. Also hinting is a very bad design in the first place, if you change anything in a font (just moving a curve coordinate by one pixel) you can throw it out of the window and start over.

Otherwise I'd suggest to use FontForge, that's a very good tool which has a hinting editor and hinting code assember / disassembler. By modifying the hinting instructions you'll be able to see how it affects the rendering. BTW, it is an Open Source tool, so don't trust its hinting code generator, that's just terrible.

FYI, after quite some time spent with vector fonts, I ended up eliminating hinting alltogether just like Apple does.

Cheers,
bzt


Top
 Profile  
 
 Post subject: Re: How rounding in TrueType hinting programs works?
PostPosted: Tue May 26, 2020 8:47 am 
Offline
Member
Member
User avatar

Joined: Mon Jan 15, 2018 2:27 pm
Posts: 201
Now, that I have it almost done, I think I might just as well finish it :wink:
I've spent half a day, figuring out how to use hint debugger in FontForge. Turns out that it has to be enabled at compile time. But now, works great as a reference. I also decided to analyze FreeType source code a little bit. It was really good idea, since Apple and MS documentation seems to be missing a lot of stuff that comments in the source document.

bzt wrote:
FYI, after quite some time spent with vector fonts, I ended up eliminating hinting alltogether just like Apple does.

How exactly did you tackle small size rasterization? Do you just do antialiasing and hope for the best? Or is there some way of making small fonts readable (possibly with just 2 level rasterization)?


Top
 Profile  
 
 Post subject: Re: How rounding in TrueType hinting programs works?
PostPosted: Tue May 26, 2020 4:52 pm 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1584
pvc wrote:
Now, that I have it almost done, I think I might just as well finish it :wink:
I've spent half a day, figuring out how to use hint debugger in FontForge. Turns out that it has to be enabled at compile time. But now, works great as a reference. I also decided to analyze FreeType source code a little bit. It was really good idea, since Apple and MS documentation seems to be missing a lot of stuff that comments in the source document.
Good to hear you get it working! Yes, there are no good docs about vector fonts. At first they look good, but there's always some little detail missing... It worth reading the FontForge and FreeType docs too, they explain things that are not mentioned anywhere else (however they never were intended to be a TTF doc, they just explain things as they describe the user interface / API).

pvc wrote:
How exactly did you tackle small size rasterization? Do you just do antialiasing and hope for the best? Or is there some way of making small fonts readable (possibly with just 2 level rasterization)?
Well, for SSFN 1.0 I did a neat trick. First, I counted the points in each coloumn. Those that exceeded a certain limit, I called important coloumns. Then instead of a single interpolation, I've calculated a contiguous series of interpolations (that I called partitions) in which those important coloumns were always at exact pixel coordinates. Hope this makes sense to you. For example normally you would use one interpolation to scale 100 grid points to 16 pixels. The 48th grid point would be a partial pixel coordinate, and would be rendered non-sharp by the anti-aliasing routine. So if we want the 48th coloumn to be sharp, instead there would be two interpolations, one for 0 - 47 grid points scaled to 0 - 7 pixels, and another, 48 - 100 scaled to the pixels 8 - 16. This guarantees that important coloumns (like the 6 straight vertical lines in the glyph "m") are always rendered with 100% alpha, and that there's always at least a 1 pixel gap between them, even in small sizes.

Then in SSFN 2.0 I simply didn't care, just used a bilinear interpolation, and interestingly everybody (including me) likes those rendered letters much much better :-)

Cheers,
bzt

ps: take a look at the window with the small "m" in the middle on the attachment.
Here you can see the counted points in each coloumns with white at the bottom (normalized). Those that exceed the limit, are continued in a cyan dotted line. Those are the coloumns that specify partitions. For this font and this glyph, there would be a different scaling for 0 - 387, 388 - 421, 422 - 495, etc. coloumns. (And you can also see that the font designer was lazy, as the upper right part of the "head" is one pixel off to the "leg", it is at 421 instead of 422 :-) Expect such errors in all Open Source fonts: FreeSerif, FreeSans, Vera, etc. One has unaligned $, the other has problems with H etc.)


Attachments:
sfnedit4.png
sfnedit4.png [ 5.31 KiB | Viewed 1413 times ]
Top
 Profile  
 
 Post subject: Re: How rounding in TrueType hinting programs works?
PostPosted: Wed May 27, 2020 4:02 am 
Offline
Member
Member
User avatar

Joined: Mon Jan 15, 2018 2:27 pm
Posts: 201
@bzt I just realized, you are the author of SSFN. If that's the case then I think I will try it. Would be awesome if your format became kind of our OSDev standard :) And the demo you have on GitLab looks pretty damn good too.


Top
 Profile  
 
 Post subject: Re: How rounding in TrueType hinting programs works?
PostPosted: Sun May 31, 2020 8:01 am 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1584
pvc wrote:
Would be awesome if your format became kind of our OSDev standard :)
Thanks for your kind words, but I don't intend to create standards. I've just created a simple scalable font library with as few dependency as possible for my OS. Then I've offered it as FOSS to help out other OSDev members, that's all. Give a star to the repo if you like it! :-)

Cheers,
bzt


Top
 Profile  
 
 Post subject: Re: How rounding in TrueType hinting programs works?
PostPosted: Sun May 31, 2020 11:00 am 
Offline
Member
Member
User avatar

Joined: Mon Jun 05, 2006 11:00 pm
Posts: 2293
Location: USA (and Australia)
bzt wrote:
Give a star to the repo if you like it! :-)


Starred! I'm definitely interested in using your library when I get around to UI work.

_________________
My OS is Perception.


Top
 Profile  
 
 Post subject: Re: How rounding in TrueType hinting programs works?
PostPosted: Wed Jun 03, 2020 7:04 am 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1584
AndrewAPrice wrote:
Starred! I'm definitely interested in using your library when I get around to UI work.
You accidentally starred the old obsolete repo :-) The new SSFN 2.0 repository is here. Anyway, thanks for the star!

Cheers,
bzt


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: MichaelPetch and 169 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group