简体   繁体   English

SDL2_ttf子像素抗锯齿

[英]SDL2_ttf Subpixel Antialiasing

Is it possible to render text with the SDL2 library using a subpixel rendering technique to increase the quality on LCD screens? 是否可以使用子像素渲染技术使用SDL2库渲染文本以​​提高LCD屏幕的质量? (Preferably with standard SDL2_ttf.) (最好使用标准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: 下图显示了我的目标,最左边的图像是放大时可以看到的实际结果:

子像素渲染示例

If this is not possible 'out of the box', but you believe it is possible to implement in the open-source SDL2_ttf; 如果这不可能“开箱即用”,但您认为可以在开源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. 我已经开始查看SDL2_ttf源代码 ,并得出结论,为了实现亚像素LCD抗锯齿,我必须对其进行修改。

From what I've researched about FreeType, I would have to change the following lines of code: 根据我对FreeType的研究,我将不得不更改以下代码行:

// (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: 以下是'灰色2位'像素模式的代码:

// (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. 干杯。

The best SDL2_ttf can do is grayscale anti-aliasing in the Shaded and Blended modes. SDL2_ttf可以做的最好的是着色和混合模式下的灰度抗锯齿。

To get sub-pixel rendering you'll have to drop back to FreeType . 要获得子像素渲染,您必须回退到FreeType

A new hinter setting TTF_HINTING_LIGHT_SUBPIXEL introduced in a recent HG commit looks like it will give you grayscale subpixel positioning (it maps down to FreeType's FT_LOAD_TARGET_LIGHT ). 最近的HG提交中引入的新的hinter设置TTF_HINTING_LIGHT_SUBPIXEL看起来会给你灰度子像素定位(它映射到FreeType的FT_LOAD_TARGET_LIGHT )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM