{"id":3148,"date":"2014-03-15T18:06:55","date_gmt":"2014-03-15T08:06:55","guid":{"rendered":"http:\/\/www.vectorstorm.org\/?p=3148"},"modified":"2014-03-15T18:16:32","modified_gmt":"2014-03-15T08:16:32","slug":"font-rendering-2","status":"publish","type":"post","link":"https:\/\/www.vectorstorm.com.au\/2014\/03\/15\/font-rendering-2\/","title":{"rendered":"More on font rendering"},"content":{"rendered":"
Another technical post today, since I’ve been banging away at revamping a problematic piece of VectorStorm’s API.<\/p>\n
In VectorStorm, here’s how one would normally create renderable text, assuming that you have a pointer to a vsFont in a variable called ‘font’:<\/p>\n
[code language=”cpp”]
\nAddFragment(
\n font->CreateString_Fragment(
\n FontContext_2D,
\n "My String",
\n 30.0,
\n Justification_Center,
\n vsBox2D::CenteredBox( vsVector2D(100.f, 50.f) ),
\n vsColor(1.0f,1.0f,1.0f,1.0f)
\n )
\n);
\n[\/code]<\/p>\n
That’s a pretty massive line of code — big enough that I needed to split it out into a bunch of lines just so that it doesn’t scroll off the side of the webpage. Ordinarily, one says that a single line of code should be no more than something in the range of 76 to 80 characters long. This one (when not broken out across multiple lines like this) is 166 characters long. That’s a lot<\/em> of typing to do for something as simple as drawing some text!<\/p>\n This interface is as bad as it is because VectorStorm’s font system is very old, and has had a lot of features slowly bolted onto it over time, and its API has been growing and growing as new features have been added without any coherent plan behind the whole system. The problem with simply going in and refactoring this code is that all this information really is necessary:<\/p>\n AddFragment()<\/strong> registers the created vsFragment so that it gets drawn automatically each frame. We need this.<\/p>\n CreateString_Fragment()<\/strong> is long and wordy. The awkward function naming is because we have an older function CreateString(), which returned a vsDisplayList. That function still exists and works (and is occasionally necessary), so we can’t just replace it. Alternately, we might have called CreateStringInDisplayList() or CreateStringInDisplayList_NoClear(), or one of a few other minor variants which take different parameters or have slightly different behaviours.\u00a0 Cleaning up this mess of different interfaces was a core goal of the refactoring.<\/p>\n FontContext_2D<\/strong> tells the font renderer whether this text is going into a 2D scene (where Y is down) or into a 3D scene (where Y is up); it needs to draw the text the other way up, if it’s going into a 3D scene. Can’t get rid of this parameter — it’s absolutely essential. \u00a0 [1]<\/a><\/p>\n “My string”<\/strong> is the string which is being built into renderable geometry. Can’t get rid of that.<\/p>\n 30.0<\/strong> is how many units tall we want the string to be. (That’s expressed in whatever units are being used in the local coordinate space. Which should normally be pixels, if you don’t want to cause pain for yourself.) Definitely can’t get rid of this.<\/p>\n Justification_Center<\/strong> tells us how the text should be placed relative to the object it’s attached to. In this case, centered. Alternately, we could say Justification_Left or Justification_Right.<\/p>\n vsBox2D::CenteredBox( vsVector2D(100.f, 50.f) )<\/strong> creates a box, centered on 0,0, which is 100 units wide and 50 units tall. Our created string will be constrained inside that box. The rendering system might need to give us smaller text than we asked for, or introduce line breaks in order to make it fit into that space. This is tremendously useful functionality, particularly when you’re dealing with user-provided strings, or with translated string data. But in practice, this parameter is really long and fiddly — it’s another parameter which we’d like to improve.<\/p>\n vsColor(1.0f,1.0f,1.0f,1.0f)<\/strong> specifies the color in which to draw the text. In practice, specifying pure white like this doesn’t actually accomplish anything (since the font is defined as being white in the first place). And there’s another version of this function which doesn’t require this parameter — CreateString_NoColor_Fragment().<\/p>\n So each bit of rendered text really does need to specify all these parameters for the text rendering system to know how to handle the text. But when writing the code it’s awkward to need to remember in what order they come, and when reading the code it can be non-obvious what they all mean.<\/p>\n What’s more, in many situations we want to create many different pieces of text all with the same style. And it’s awkward to have to keep specifying and re-specifying all those parameters every time we just want to make another bit of text in a single style. So based on these complaints, I’ve implemented a new, simpler API.<\/p>\n Under the new API, the standard way to draw text in VectorStorm now looks like this:<\/p>\n [code language=”cpp”] To my eyes, this is much<\/i> easier to read and understand. It doesn’t require you to set any variables you don’t care about, and you don’t have to try to remember the order that all those parameters are supposed to provided in, in a huge single-function API.<\/p>\n The best bit about this approach is that the ‘vsFontRenderer’ object can be stored and reused, to render more text using the same settings at any point later on. If you want to render to a display list instead, you call Finally, this has allowed me to completely delete all those mildly-tweaked variants of the old font drawing code, so now everything goes through a single implementation no matter whether you want colors or not, or a custom transform or not, or etc, instead of having custom implementations for all those different combinations.<\/p>\n In my book, this change was a big, big win.<\/p>\n For those who are interested, the refactored code is up on VectorStorm’s Github repository<\/a>, in the v2 branch.<\/p>\n <\/a>1. Note that most people look at me funny when I tell them that VectorStorm points its Y axis in different directions in 2D scenes than in 3D scenes. But the indisputable and inarguable facts are that in any sane 2D environment, Y increases as you go down the screen, while in any sane 3D environment, Y increases as you move up from the ground, so I’m not the crazy one in this conversation. Also, tabs are better than spaces, vim is better than emacs, cats are better than dogs, and I don’t care what anybody says, big-endian is intrinsically better than little-endian<\/a>. :)<\/p>\n","protected":false},"excerpt":{"rendered":" Another technical post today, since I’ve been banging away at revamping a problematic piece of VectorStorm’s API. In VectorStorm, here’s how one would normally create renderable text, assuming that you have a pointer to a vsFont in a variable called ‘font’: [code language=”cpp”] AddFragment( font->CreateString_Fragment( FontContext_2D, "My String", 30.0, Justification_Center, vsBox2D::CenteredBox( vsVector2D(100.f, 50.f) ), vsColor(1.0f,1.0f,1.0f,1.0f)…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[33],"tags":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/po9WK-OM","_links":{"self":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts\/3148"}],"collection":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/comments?post=3148"}],"version-history":[{"count":9,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts\/3148\/revisions"}],"predecessor-version":[{"id":3157,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/posts\/3148\/revisions\/3157"}],"wp:attachment":[{"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/media?parent=3148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/categories?post=3148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vectorstorm.com.au\/wp-json\/wp\/v2\/tags?post=3148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
\nvsFontRenderer fr(font);
\nfr.SetSize(30.f);
\nfr.SetJustificationType( Justification_Center );
\nfr.SetMaxWidthAndHeight( 100.f, 50.f );
\nAddFragment( fr.Fragment2D("My string") );
\n[\/code]<\/p>\nfr.DisplayList2D(\"My string\");<\/code>, or you can use
fr.DisplayList2D( myList, \"My string\" )<\/code> if you want to provide your own display list.<\/p>\n