none
MS Gothic text are not having line spacing RRS feed

  • Question

  • Hi,

    I have tried to draw text using GDI with "MS Gothic" Font but the text are not having line spacing. I have tried with different like "Times New Roman" Font and text are render with proper line spacing. 

    Code snippet:

    Image bitmap = new Bitmap(400, 500,PixelFormat.Format32bppArgb);
    Graphics graphics = Graphics.FromImage(bitmap);
                graphics.PageUnit = GraphicsUnit.Point;
                string[] drawString = { "First","Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth" };
                graphics.FillRectangle(Brushes.White, new System.Drawing.Rectangle(0, 0, (int)640,(int)720));
                // Create font and brush.
                Font drawFont = new Font("MS Gothic", 11,FontStyle.Regular);
                //Font drawFont = new Font("Times New Roman", 11,FontStyle.Regular);
                SolidBrush drawBrush = new SolidBrush(Color.Black);
                StringFormat format = new StringFormat(StringFormat.GenericTypographic);
    float x = 72f;
                float y = 72f;
                foreach (string text in drawString)
                {
                    SizeF size = graphics.MeasureString(text, drawFont, new PointF(0, 0), format);
                    //graphics.SetClip(new RectangleF(x, 50.65f, size.Width, 15f), System.Drawing.Drawing2D.CombineMode.Replace);
                    graphics.DrawString(text, drawFont, drawBrush, new RectangleF(x, y, size.Width, size.Height), format);
                    y += size.Height;
                }
                graphics.Dispose();
    	bitmap.Save(@"MSGothic.jpeg");

    how to get the line spacing for "MS Gothic" Font?

    Regards,

    Madhan K

    Tuesday, March 31, 2020 4:18 AM

All replies

  • Lots of CJK fonts are historically designed this way in order to support vertical rendering of fonts before built-in support for vertical rendering was added.

    That's why the Asian Language Pack of MS Office is designed to inject artificial line spacing when these fonts are detected.

    Tuesday, March 31, 2020 6:05 AM
    Answerer
  • Hi Madhan kumarasamy,

    Thank you for posting here.

    I think your purpose is to make the line spacing between the text in the generated bitmap larger, don't you?

    This can be achieved with very simple code changes.

                foreach (string text in drawString)
                {
                    SizeF size = graphics.MeasureString(text, drawFont, new PointF(0, 0), format);
                    //graphics.SetClip(new RectangleF(x, 50.65f, size.Width, 15f), System.Drawing.Drawing2D.CombineMode.Replace);
                    graphics.DrawString(text, drawFont, drawBrush, new RectangleF(x, y, size.Width, size.Height), format);
                    y += size.Height;
                    y += 15;
                }
    Result:

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, March 31, 2020 7:02 AM
  • I think he is complaining about the fact that when rendering font in English, line spacing is automatically included, but when rendering a Japanese font, there is no line spacing added.
    Tuesday, March 31, 2020 7:48 AM
    Answerer
  • Hi cheong00,

    Exactly that is my issue, in normal font like "Times New Roman", have height includes with line spacing but "MS Gothic" font height does not include the line spacing. how to have spacing value for that font.

    Tuesday, March 31, 2020 8:21 AM
  • Hi Timon,

    Thanks for your update.

    But my problem is why the japanese font "MS Gothic" not having line spacing in its height. how to added the proper line spacing value to that font and it applicable to various font size.

    Regards,

    Madhan k

    Tuesday, March 31, 2020 8:30 AM
  • I think you can try to dump the drawFont.FontFamily.GetLineSpacing(drawFont.Style) to check.

    If it is the same as drawFont.FontFamily.GetEmHeight(drawFont.style) then add some artifical adjustment to "y" below your DrawString() statement.



    Tuesday, March 31, 2020 8:46 AM
    Answerer
  • Hi cheong00,

    As per your suggestion, i have check value for GetLineSpacing(drawFont.Style) and GetEmHeight(drawFont.Style), both values are same. There is any other way to find its line spacing, Else we have to add some constant integer value to "y" in the draw string?


    Tuesday, March 31, 2020 11:28 AM
  • Hi,

    Please take a look at this.

    Font.Height Property

    Font.Size Property

    According to the documentation, Font.Height is the line spacing of this font, in pixels.

    Line spacing is the vertical distance between the baselines of two consecutive lines of text. Therefore, line spacing includes the space between lines and the height of the characters themselves.

    Font.Size is the em-size of this Font.

    If you want to get the white space between two lines, the difference between them may be what you want.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 1, 2020 5:53 AM
  • Hi Timon,

    I have look the Font.Height Property and Font.Size Property. I have comparing the result of Font.GetHeight(Graphics) and Font.Size to find the line spacing for list of font ["Times New Roman", "Calibri" and "MS Gothic"]. Both Times and calibri font are having line spacing value but "MS Gothic" font have same value in both Font.GetHeight(Graphics) and Font.Size, line spacing is 0.

    So, we have to manually increase the height for the "MS Gothic" font to have line spacing between two lines?

    Regards,

    Madhan k

    Wednesday, April 1, 2020 7:17 AM
  • Oh, if it still doesn't work, I'm afraid there is nothing I can do for you.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Wednesday, April 1, 2020 9:35 AM
  • Have you read any of the responses here?  The very first response from cheong00 has the answer.  CJK fonts often do not have line spacing embedded because CJK fonts are so often rendered vertically.  In that case, the "line spacing" is really the "character spacing".

    It's up to your app to compensate for this.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Wednesday, April 1, 2020 6:49 PM
  • Madhan,

    If I run your code I get this result. 

    Therefore what is exactly the problem with that.


    Success
    Cor

    Wednesday, April 1, 2020 10:51 PM
  • What I was trying to tell you is that, if both values are the same, the font itself don't include line spacing and therefore you have to do render adjustment yourself.

    Just move Y of RectangleF up by half of size.Height, then set Height of RectangleF to double of size.Height to create "single line spacing" for yourself if you need to write with some other character in different fonts.

    Regardless with whether you have such requirement, just add double of size.Height to y when done with a line on "font itself don't include line spacing".


    Thursday, April 2, 2020 8:57 AM
    Answerer
  • What I was trying to tell you is that, if both values are the same, the font itself don't include line spacing and therefore you have to do render adjustment yourself.

    Just move Y of RectangleF up by half of size.Height, then set Height of RectangleF to double of size.Height to create "single line spacing" for yourself if you need to write with some other character in different fonts.

    Regardless with whether you have such requirement, just add double of size.Height to y when done with a line on "font itself don't include line spacing".


    cheong, 

    I'm only used to greek  based characters, what has line spacing to do with drawing. It is not writing that kind of characters to text lines with a line feed. 

    Or in other words,  why is this long thread replied mainly by all kind of persons not used natively to those characters.


    Success
    Cor

    Thursday, April 2, 2020 12:43 PM
  • > what has line spacing to do with drawing.

    It's because the asker is talking about drawing string to image "line by line".

    Because of this, the line spacing becomes important as if you neglecting it the lines using CJK fonts will become very close.

    Now we know that lots of CJK fonts (or to be more specific, fonts from languages that has "square characters" and naturally need to support vertical writing style) do not have line spacing included in the font itself. So I provided a way to detect this and add additional spacing with code when such font is detected (by drawing it upward by half of the character's height, and double the adjustment of starting Y coordinate of next line).

    > Or in other words,  why is this long thread replied mainly by all kind of persons not used natively to those characters.

    I have no idea.

    P.S.: Actually it is still commonly used on web where forums often don't provide support for "writing-mode" CSS attribute.

    低 舉 疑 床
    頭 頭 是 前
    思 望 地 明
    故 明 上 月
    鄉 月 霜 光
    


    Friday, April 3, 2020 5:55 AM
    Answerer