We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 4266 - some TTF font rendering will be offset, without any warning of anything wrong detected
Summary: some TTF font rendering will be offset, without any warning of anything wrong...
Status: RESOLVED FIXED
Alias: None
Product: SDL_ttf
Classification: Unclassified
Component: misc (show other bugs)
Version: unspecified
Hardware: All All
: P2 normal
Assignee: Sam Lantinga
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-09-23 12:24 UTC by HAIHONG(Mr.LUO)
Modified: 2018-10-26 21:12 UTC (History)
2 users (show)

See Also:


Attachments
font file I met problem with, and the rendered capture/saved in picture (31.05 KB, application/x-font-otf)
2018-09-23 12:24 UTC, HAIHONG(Mr.LUO)
Details
rendered surface captured as a PNG file (5.31 KB, image/png)
2018-10-09 13:43 UTC, HAIHONG(Mr.LUO)
Details
patch (4.54 KB, patch)
2018-10-10 15:34 UTC, Sylvain
Details | Diff
patch (4.49 KB, patch)
2018-10-11 12:03 UTC, Sylvain
Details | Diff
patch (4.84 KB, patch)
2018-10-11 15:53 UTC, Sylvain
Details | Diff
patch (23.18 KB, patch)
2018-10-24 16:20 UTC, Sylvain
Details | Diff
patch (10.14 KB, patch)
2018-10-24 19:14 UTC, Sylvain
Details | Diff
patch for SDL-1.2 branch (7.88 KB, patch)
2018-10-24 21:40 UTC, Ozkan Sezer
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description HAIHONG(Mr.LUO) 2018-09-23 12:24:46 UTC
Created attachment 3308 [details]
font file I met problem with, and the rendered capture/saved in picture

called with, 
...
pFloorFont=TTF_OpenFont(".//ITCAvantGardeStd-Demi.ttf",297);
...
, later further used for rendering:
TTF_RenderUTF8_Blended(pFloorFont,"8", whiteColor);
...

result is quite off/ascended.
Comment 1 Sylvain 2018-09-27 10:55:55 UTC
I have tried the fond and I am not sure to see your issue.

You forgot to upload your screenshot
Comment 2 HAIHONG(Mr.LUO) 2018-10-09 13:43:47 UTC
Created attachment 3357 [details]
rendered surface captured as a PNG file

Pls see the attached.
Comment 3 Sylvain 2018-10-09 16:46:19 UTC
Thanks, I see that also now, when the font size is big enough.

Looking at SDL_ttf, it hits this line: 

             if ((row + glyph->yoffset) < 0) {
                 continue;
             }

so the first rows are not used.


// yoffset = font->ascent - cached->maxy;
// some values:  skip row=0 glyph->yoffset=-2 font->ascent=96 glyph->maxy=98

Not sure how it should be handled ...

https://www.freetype.org/freetype2/docs/glyphs/glyphs-3.html
Comment 4 HAIHONG(Mr.LUO) 2018-10-10 02:55:42 UTC
But (assusming this criteria of comparison is correct without need to consider ascending/descending .etc) this should skip only two (pixel) rows?

I might not be knowledgable enough to go to this deep yet.
But a clue might help, if we use TTF library's utilities to open/draw with same font size, no issue, and I believe there should be quite some big space (maybe line space) above "8" rendered.  Is there a way to just adopt TTF ways of criteria/formula?
Comment 5 Sylvain 2018-10-10 09:18:00 UTC
In my example it skips two pixels rows. But it can skip more or not depending on the size.

I have checked and I also have a few glyph that get cropped. for instance upper case with accent (eg Å).

If I try not to crop it, by making yoffset always positive. 

 if (cached->yoffset < 0) ached->yoffset = 0;

the glyph is fully drawn but, shift under the baseline.
Comment 6 Sylvain 2018-10-10 09:19:27 UTC
BTW, that was adding to fix a memory issue in bug 2749, https://hg.libsdl.org/SDL_ttf/rev/b9d515ee0aaf
Comment 7 Sylvain 2018-10-10 15:34:49 UTC
Created attachment 3360 [details]
patch

Here's a patch I've tested. It determines for each string a padding value to compensate negative yoffsets. Most of the time, it's zero, except with bad fonts.

With this padding added, no need to check overflows at the beginning. 
(eg.  "if ((row + glyph->yoffset) < 0) { continue".. )
Comment 8 Sylvain 2018-10-11 12:03:29 UTC
Created attachment 3363 [details]
patch

Updated patch. Use a static global variable, we don't need a per TTF_font instance variable.
Comment 9 Sylvain 2018-10-11 15:53:35 UTC
Created attachment 3364 [details]
patch

Add a new version so that underline and strike through still works.
Comment 10 Sylvain 2018-10-17 07:31:38 UTC
@ HAIHONG(Mr.LUO)  Did you have the chance to try the patch ?
Comment 11 Sylvain 2018-10-23 10:45:01 UTC
Please don't merge this patch. I have a local version that remove the static variable and fixes a few others issues.
Comment 12 HAIHONG(Mr.LUO) 2018-10-23 16:22:20 UTC
Most of app work I've done, as well as the test environment I am familiar to debug with,  is SDL v1.2 series, which contains the SDL v2.0.11 for SDL-v1.2. 

Can I have the version (hopefully a targeted SDL v2.0.12) to work with SDL-v1.2 so that I can try it out?

SDLv2 is just totally new and scary learning curve for me.
Comment 13 Sylvain 2018-10-24 15:56:20 UTC
Sorry, I can't make a version for SDL-1.2-branch as I don't use it anymore. But I guess SDL_ttf-2 should be directly compatible with SDL-1.2.
Comment 14 Sylvain 2018-10-24 16:20:53 UTC
Created attachment 3396 [details]
patch

Here's a new version of the patch, with more diffs.
Fix bug 2749 by initializing properly 'xstart'
Fix bug 4266 by adding an ystart 'value'
Enable underline and strike-through styles for the 'Wrapped' functions.


Remove three checks: 
Unneeded because xstart is now initialized so that it never gets negative:

                if ((xstart + glyph->minx) < 0) {
                    xstart -= (xstart + glyph->minx);
                }

Unneeded because an ystart offset has been added, so we never read at a negative position:

                if ((row + glyph->yoffset) < 0) {
                    continue;
                }

Unneeded because it's redundant with the bound checking "dst < dst_check":

                if ((row + glyph->yoffset) >= textbuf->h) {
                    continue;
                }

Clean up: 
- use SDL_min/max where it makes the code more readable
- a few warnings fixed

I have a long test-suite that uses many fonts and many languages, with valid and invalid inputs (only disable text shaping). I have seen no invalid read/write or uninitialized things.
Comment 15 Ozkan Sezer 2018-10-24 17:06:30 UTC
The fix for bug #2749 is already in (and packported to SDL-1.2
branch too.)

Please re-diff your patch against current hg, and please do
not include cleanup / warning fix parts: include _only_ the
fix for this bug #4266,  and post a different patch for the
cleanups/warning fixes if necessary.
Comment 16 Sylvain 2018-10-24 19:14:05 UTC
Created attachment 3397 [details]
patch

Here's the new version with only the fix for this bug
Comment 17 Ozkan Sezer 2018-10-24 21:40:16 UTC
Created attachment 3398 [details]
patch for SDL-1.2 branch

Here is backport of the patch for SDL-1.2 branch.

If no one objects, I can apply this weekend.
Comment 18 Sylvain 2018-10-26 10:05:37 UTC
The patch for "Enable underline and strike-through styles for the 'Wrapped' functions." is in bug 4333
Comment 19 Sylvain 2018-10-26 10:20:00 UTC
And the warning patch is in bug 4030
Comment 20 Ozkan Sezer 2018-10-26 21:12:12 UTC
Patch added.
SDL-2.0: http://hg.libsdl.org/SDL_ttf/rev/73ae6927d584
SDL-1.2: http://hg.libsdl.org/SDL_ttf/rev/2495a164b8a9

Closing.