如何使字形粗体使用freetype库

How to make glyph bold using freetype library?

本文关键字:freetype 何使 字形      更新时间:2023-10-16

我一直在使用字形,我知道要使一些字体加粗,你必须加载该字体的加粗版本。但我想要实现的是使常规字体加粗使用自由类型。我已经使用FT_TRANSFORM实现了斜体样式,现在我想把它变成粗体。什么好主意吗?这可能吗?我已经阅读了FreeType API参考指南,但没有找到幸运!关于

为了模拟粗体,您可以将相同的字形打印两次,偏移1 px。虽然我不认为你会得到完美的结果,但至少有些东西。

我知道这是一个很老的问题。

SFML代码显示了如何做到这一点:https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/Font.cpp L558

重要的位似乎是FT_Outline_Embolden和FT_Bitmap_Embolden函数

我以前写过。在这里我添加了一行用于二次打印,使字母像@c-smile提到的那样加粗。我不推荐这样的把戏。如果有一个标准的方法,那就更好了。

void DrawFTGlyph(FT_Bitmap* pBitmap, int x, int y)
{
    int i, j, p, q;
    int xMax = x + pBitmap->width;
    int yMax = y + pBitmap->rows;
    D3DXCOLOR color = D3DCOLOR_RGBA(255, 255, 255, 1);
    for (i = x, p = 0; i < xMax; i++, p++)
    {
        for (j = y, q = 0; j < yMax; j++, q++)
        {
            if (i < 0 || j < 0 || 
                i >= texture.Size().Width() || 
                j >= texture.Size().Height())
            {
                continue;
            }
            BYTE intensity = pBitmap->buffer[q * pBitmap->pitch + p];
            D3DXCOLOR pixel(color.r * intensity, color.g * intensity, color.b * intensity, color.a * intensity);
            texture.SetPixel(i, j, pixel);
            texture.SetPixel(i + 2, j + 2, pixel); // Second print
        }
    }
}