如何为字体生成正确的位图?

发布于 2024-10-30 13:55:07 字数 1721 浏览 0 评论 0原文

我有一个关于位图字体的问题。我需要为屏幕设备创建字体。它应该由所有可打印字符的位图组成。为了获取所有字符的位图,我使用以下方法:

public MFont(Font font, int first = 32, int last = 126)
{        
    var characters = new List<Character>();
    Bitmap objBmpImage = new Bitmap(1, 1);

    // Create a graphics object to measure the text's width and height.
    Graphics objGraphics = Graphics.FromImage(objBmpImage);

    for (int i = first; i <= last; i++)
    {
        char c = Convert.ToChar(i);
        int intWidth;
        int intHeight;
        string s = "" + c;

        // This is where the bitmap size is determined.                
        intWidth = (int)objGraphics.MeasureString(s, font).Width;
        intHeight = (int)objGraphics.MeasureString(s, font).Height;

        // Create the bmpImage again with the correct size for the text and font.
        objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));    

        objGraphics = Graphics.FromImage(objBmpImage);    
        // Set Background color
        objGraphics.Clear(Color.White);
        objGraphics.SmoothingMode = SmoothingMode.None;
        objGraphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
        objGraphics.DrawString(s, font, new SolidBrush(Color.Black), 0, 0);           
        objGraphics.Flush();                  

        characters.Add(
            new Character
                {
                    Bitmap = objBmpImage,
                    Code = i,
                    Size = objBmpImage.Size                        

                }
            );
    }
}

问题是所有字符位图的左侧和右侧都有太多空间。因此,当我在屏幕上使用这些位图显示文本时,文本就像在每个字符后面添加了一个空格。我该如何修复它?也许我对字体或它们应该如何显示有些不了解。我知道我可以手动裁剪位图,但这不是很准确和清晰。除此之外,有些字符根本没有任何多余的空间。

I have a question concerning Bitmap fonts. I need to create fonts for screen device. It should consists of bitmaps of all printable characters. To get a bitmaps of all characters I am using the following approach:

public MFont(Font font, int first = 32, int last = 126)
{        
    var characters = new List<Character>();
    Bitmap objBmpImage = new Bitmap(1, 1);

    // Create a graphics object to measure the text's width and height.
    Graphics objGraphics = Graphics.FromImage(objBmpImage);

    for (int i = first; i <= last; i++)
    {
        char c = Convert.ToChar(i);
        int intWidth;
        int intHeight;
        string s = "" + c;

        // This is where the bitmap size is determined.                
        intWidth = (int)objGraphics.MeasureString(s, font).Width;
        intHeight = (int)objGraphics.MeasureString(s, font).Height;

        // Create the bmpImage again with the correct size for the text and font.
        objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));    

        objGraphics = Graphics.FromImage(objBmpImage);    
        // Set Background color
        objGraphics.Clear(Color.White);
        objGraphics.SmoothingMode = SmoothingMode.None;
        objGraphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
        objGraphics.DrawString(s, font, new SolidBrush(Color.Black), 0, 0);           
        objGraphics.Flush();                  

        characters.Add(
            new Character
                {
                    Bitmap = objBmpImage,
                    Code = i,
                    Size = objBmpImage.Size                        

                }
            );
    }
}

The problem is that all character bitmaps have too many space on both left and right sides. So, when I display text using thees bitmaps on the screen, the text is like after each character a space was added. How can I repair that? Maybe there is something I am not aware about fonts, or how they should be displayed. I know that I can crop bitmaps manually, but this is not very accurate and clear. Besides that some characters don't have any redundant space at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

○闲身 2024-11-06 13:55:07

两侧的额外空间就是填充。您必须在标志上指定 NoPadding。阅读以下链接:

http://msdn.microsoft.com/en-us/ magazine/cc751527.aspx

另外,请注意字符串测量可能会考虑斜体文本所需的空间 - 因此每个字符的右侧可能有额外的间距。

你说有些字符没有多余的空格。这意味着您看到的额外间距可能是由于字距调整或缺乏字距调整造成的。

您必须实现自己的“字距调整”以将字符紧密排列(对于比例字体)。否则,你看起来总是不太理想。

有多种方法可以调整字距,但它需要您对位图进行一些后期处理。

That extra space on either sides are the padding. You have to specify NoPadding on the flags. Read the following link:

http://msdn.microsoft.com/en-us/magazine/cc751527.aspx

Also, beware that the string measurement may take into consideration the space needed for italics text -- so you may have extra spacing on the right of each character.

You say that some characters don't have redundant spaces. Which means that the extra spacing you see is can be due to kerning, or the lack thereof.

You have to implement you own "kerning" to pack the characters tight (for proportional fonts). Otherwise, you're always going to look sub-optimal.

There are ways to fudge kerning, but it requires you to do some post processing on your bitmaps.

默嘫て 2024-11-06 13:55:07

尝试使用 TextRenderer.MeasureText() 而不是 Graphics.MeasureString()。它允许您指定测量期间将使用的 TextFormatFlags。我怀疑默认情况下会添加填充,因此请尝试将 TextFormatFlags.NoPadding 传递给该方法,看看结果是否会发生变化。

Try using TextRenderer.MeasureText() instead of Graphics.MeasureString(). It allows you to specify the TextFormatFlags that will be used during measurement. I suspect that padding is added by default, so try passing TextFormatFlags.NoPadding to the method and see if your results change.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文