Font size

Jun 1, 2013 at 2:21 PM
Hi and thanks for this project.

I have made a slight modification in order to make the math image a bit more clear. While detecting the font size, I have used MeasureCharacterRanges and check based on both width and height.
Moreover when I add the string in the path, I convert the font size to em using float emSize = graphics.DpiY * font.Size / 72 as discussed here.

The final code is :
 // Set up the text format.
                    var format = new StringFormat
                    {
                        Alignment = StringAlignment.Center,
                        LineAlignment = StringAlignment.Center,
                        FormatFlags = StringFormatFlags.NoWrap|StringFormatFlags.NoClip,
                        Trimming = StringTrimming.None
                    };

                    format.SetMeasurableCharacterRanges(new CharacterRange[]{new CharacterRange(0,text.Length)});


                    // Set up the text font.
                    RectangleF size;
                    float fontSize = rect.Height + 1;
                    Font font;
                    // Adjust the font size until the text fits within the image.
                    do
                    {
                        fontSize--;
                        font = new Font(
                            familyName,
                            fontSize,
                            FontStyle.Bold);
                        size = g.MeasureCharacterRanges(text, font, rect, format)[0].GetBounds(g);
                    } while (size.Width > rect.Width || size.Height> rect.Height);
                    // Check http://stackoverflow.com/questions/2292812/font-in-graphicspath-addstring-is-smaller-than-usual-font on why we have to convert to em
                    // Create a path using the text and warp it randomly.
                    var path = new GraphicsPath();
                    path.AddString(
                        text,
                        font.FontFamily,
                        (int) font.Style,
                        g.DpiY * font.Size / 72, rect,
                        format);
Hope this helps.
Coordinator
Jun 1, 2013 at 4:38 PM
Hi there,
Great, thanks in future versions, I will try to implement it.

Regards,
Vyacheslav Volkov