SDL2_ttf Subpixel Antialiasing

Viewed 1734

Is it possible to render text with the SDL2 library using a subpixel rendering technique to increase the quality on LCD screens? (Preferably with standard SDL2_ttf.)

The following image demonstrates precisely what I'm aiming for, where the image on the far-left is the actual result that would be seen upon zooming in:

subpixel rendering example

If this is not possible 'out of the box', but you believe it is possible to implement in the open-source SDL2_ttf; I'd appreciate some ideas or pointers in the right direction as to how it could be implemented.


EDIT:

I've started looking through the source code of SDL2_ttf, and came to the conclusion that it I would have to modify it in order to achieve subpixel LCD antialiasing.

From what I've researched about FreeType, I would have to change the following lines of code:

// (from SDL_ttf.c, approx. line 650)
/* Render the glyph */
error = FT_Render_Glyph(glyph, mono ? ft_render_mode_mono : ft_render_mode_normal);

To

/* Render the glyph, (ft_render_mode_mono is also deprecated) */
error = FT_Render_Glyph(glyph, mono ? FT_RENDER_MODE_MONO : FT_RENDER_MODE_LCD);

Additionally, some code needs to be added to write to the bitmap. I have absolutely no idea how to do this, as I barely understand what's already there, due to the lack of comments. The following is the 'grey 2-bit' pixel-mode's code:

// (from SDL_ttf.c, approx. line 800)
// ... other pixel modes handled above this...
else if (src->pixel_mode == FT_PIXEL_MODE_GRAY2) {
    // srcp appears to be the source buffer, 
    // and dstp the destination buffer.
    unsigned char *srcp = src->buffer + soffset;
    unsigned char *dstp = dst->buffer + doffset;
    unsigned char c;
    unsigned int j, k;

    // No idea how any of this works.
    // I'd guess 'j' should increment by 1
    // instead of 4, since LCD uses 8-bits.
    for (j = 0; j < src->width; j += 4) {
        c = *srcp++;
        for (k = 0; k < 4; ++k) 
        {
            // Literally no idea where they pulled these numbers out.
            if ((c&0xA0) >> 6) {
                *dstp++ = NUM_GRAYS * ((c&0xA0) >> 6) / 3 - 1;
            } 
            else {
                *dstp++ = 0x00;
            }
        }
        c <<= 2;
    }
}
// 4-bit grey is done down here...

I'm hoping someone is capable of helping me out on this one...
Cheers.

1 Answers
Related