There are a lot of differences with the positioning of textObjects between the retail version of EMI and ResdiualVM’s implementation. Let’s take the scene wed and compare the text positions. First, here’s the scene in the retail version:
And here’s the same scene in ResidualVM:
And finally, here’s the difference between the two:
We can ignore the ship in the background, since it’s moving, and I didn’t capture it in exactly the same place. We also can ignore the shadows, Akz has already addressed that problem. That leaves us with the text errors, exactly what we’re trying to work out!
While it’s obvious to the eye that the text “There’s an enemy pirate over there.” is in the wrong place and has a different wrapping point, there’s also a mismatch between where the text is placed for “Look at enemy pirate”.
Additionally, in some cut scenes, with subtitles on, the text is placed in the wrong location. A good example of this problem is the scene during travel to Jambalaya Island, where the exclamations of the crew are drawn right on top of each other! Another common issue is that the text is placed in the wrong place, obscuring the game play. A good example of this is when reading the directions to get to Pegnose Pete’s house.
In ResidualVM, text elements are drawn using a textObject. In the Lua scripts, textObjects are created by calling the Lua/C function MakeTextObject. The parameters for the textObject are the string that’s going to be displayed and a parameters table, with various options for configuring the text. We can remove this textObject from the screen by calling KillTextObject and passing the handle that was returned by MakeTextObject. Let’s try drawing some text with the retail version of EMI and observe the behavior. Let’s start by just writing some text to the 0, 0 coordinate position:
textObj = MakeTextObject("Hello Blog Readers!", { x = 0.0, y = 0.0, font = verb_font, fgcolor = White })
Here’s the output of running that command:
An interesting aspect to the positioning of the text is that it appears to be using a coordinate system with a basis in the center of the screen. Next, lets try exploring the extents of the text placement. Do negative values work? (Yes!) What is the highest and lowest X and Y value for placing text that still appears on the screen? (It appears that -1, -1 is the lower left corner and 1,1 is the upper right corner). Is this mechanism the only way text is added to the screen? (Nope, there’s also SayLine, PrintLine and other methods, so when debugging, the method used will be important).
With this information in hand, I started by rebasing Botje’s changes (to allow the X and Y values to be passed as floats) against the current master. These changes, made the text appear more correct for scenes where the X and Y values are passed from the Lua, such as the directions to Pegnose Pete’s House, but this change didn’t effect most of the erroneous text placement.
I then inspected the SayLine Lua/C function and found that it sets a variable when X and Y aren’t passed in from the Lua script, which has the actor set the position relative to the actor’s bounding box. Simply using the other side of the bounding box for the Y position moves the actor’s speech to a much more correct location:
The next task I tackled was the incorrect word wrapping, as seen in the speech bubble, “There’s an enemy pirate fighting over there.”. Although the ResidualVM version actually looks a little bit better than the retail version here, there are other scenes where the word wrapping is worse, so let’s try to fix it!
The word wrapping is done in the function setupText, which is found in the textobject.cpp file. Inside this function, the line width is computed by adding each character width in the line together. Strangely, this uses the maximum value between the character width and something called the dataWidth. Without a comment, it’s hard to tell what dataWidth actually means, so, I first started by identifying when this code was added to ResidualVM. This was a harder task than anticipated because the file was moved twice over the course of development. To trace its history, I first reset git to the first commit that moved the file and then, pushed the HEAD of the repository back one more commit. This restored the original version of the moved file and let me explore its history further. After one more file move, I found that this was the commit that added the dataWidth field. Unfortunately, there wasn’t much information in the commit message either, so I decided to try to test some strings in the retail version and measure what the width of the displayed text in pixels.
So, it appears that the dataWidth is the actual width of the character, but the width includes adjustments to the spacing (or kerning) between the letters to make them look nice. As an example, let’s look at a simple string, “Wa”:
MakeTextObject("Wa", { x = 0.0, y = 0.0, fgcolor = White))
Which results in:
As can be seen, the letter “a” actually overlaps with the letter W by two pixels. It appears that ResidualVM gets this right and does use the correct values for drawing the characters as this matches perfectly with the retail version. So, our issue is with determining when to word wrap, which depends on the width of the rendered text. Let’s examine the width variables to figure out what combination results in the correct width being calculated.
For each font character, there’s the dataWidth and width sizes, which of these is the actual character width and which is the kerned width? Here’s the dataWidth and width values for “W” and “a”:
Value | W |
---|---|
width | 18 |
dataWidth | 21 |
Value | a |
width | 7 |
dataWidth | 9 |
So, it appears that the “width” value is the kerned value, while dataWidth is the actual size of the font bitmap image. Checking this against the image above shows that the dataWidth values match the pixel size of the letters exactly. So, if we add the dataWidth values together, our characters will be wider than what’s actually displayed to the screen. We can fix this by always using the width value instead of the maximum of the two as is done now.
So, let’s take a look at the result difference after this change, when Guybrush is speaking the line “There’s an enemy pirate fighting over there.”:
We can see now that the lines are the same length now, and appear to be in the correct location on the X axis, but shifted in the Y axis. So, first, is there a common shift between all of these lines? Or in other words, are the lines shifted by a constant value? Unfortunately, it appears that this isn’t the case. The top line of Guybrush’s speech is 1 pixel too far to the right and 7 pixels too far towards the bottom of the screen, while the bottom line is offset by 1 pixel to the right and 13 pixels too far towards the bottom of the screen. The text “Look at enemy pirate” is also offset by 14 pixels too high towards the top of the screen.
Let’s look into the difference in line spacing between the two lines for Guybrush’s speech. It turns out that in ResidualVM, the height of the font is modified when loaded if the character is taller than the specified font height. As before, this is intentional because of kerning. We do want some of the characters to sink lower or rise higher than the rest because it’s pleasing to the eye. Simply commenting the section of code that modifies the height results in the correct behavior, so I looked up why that work around was inserted in the first place, which led to this commit. So, it appears that there was a crash bug associated with the height. I suspect that the problem would be fixed if the renderer uses the dataHeight instead of the height to draw the text.
As this blog post is getting long and I need to check with the other developers about this code, we’ll wrap it up here for now. I’ll continue in the next blog post with fixing the remaining font issues. All of the changes in this post can be found in PR #964.