【问题标题】:Rendering a Glyph to a bitmap image in C++ using SFML使用 SFML 将字形渲染到 C++ 中的位图图像
【发布时间】:2014-10-29 08:54:25
【问题描述】:

我想生成一个字形的位图图像,以便我可以将它与另一个图像中未知字母的像素值进行比较。

我使用的是 Visual Studio 2008,我的项目是 C++,我使用的是 SFML。我可以很好地从 ttf 文件加载字体,我尝试执行以下操作:

sf::Font myFont;
myFont.loadFromFile("Path\\arial.ttf");

sf::Texture myTexture = myFont.getTexture(48);
sf::Image textureImage = myTexture.copyToImage();

sf::Glyph myGlyph = myFont.getGlyph(65, 12, false); // get the 'A' glyph

sf::Image glyphImage;
glyphImage.create(myGlyph.bounds.width, myGlyph.bounds.height, sf::Color::White);
glyphImage.copy(textureImage, 0, 0, myGlyph.textureRect); 

我认为这不起作用,因为我只是从字形所在的纹理部分创建图像,而不是字形本身的像素值。

有人可以帮忙吗?

【问题讨论】:

    标签: c++ visual-studio-2008 bitmap sfml glyph


    【解决方案1】:

    这对我来说很好用:

    unsigned int size = 20;
    sf::Glyph glyph = font.getGlyph('A', size, false);
    sf::Texture bitmap = font.getTexture(size);
    
    sf::Image image;
    image.create(glyph.bounds.width, glyph.bounds.height);
    image.copy(bitmap.copyToImage(), 0, 0, glyph.textureRect);
    

    请注意,在此示例中,当调用 getGlyphgetTexture 时,字符大小是相同的,而您上面的代码中并非如此。

    此外,为了便于阅读,不要使用 65 之类的幻数。

    【讨论】:

    • 太好了,这对我有用 :) 导致失败的是字符大小的差异。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多