This project has moved. For the latest updates, please go here.

Issue with custom fonts

Apr 5, 2016 at 10:53 PM
Hi All,

Currently I'm writting a tool which will generate font images based on user choice. Currently everything works apart from using custom fonts. Currently it always seems to go to a default, but I need it to be able to use ".ttf" files.

This is my current implementation.
private void OutputFont(Char output)
        {
            MagickImage fontTest = new MagickImage(Color.Transparent, Int32.Parse(Width_Text_Box.Text), Int32.Parse(Height_Text_Box.Text))
            {
                Format = MagickFormat.Png
            };
            // Font parameters
            DrawableFont font = new DrawableFont("Fonts/28_Days_Later.ttf");
            DrawableFontPointSize pointSize = new DrawableFontPointSize(Int32.Parse(FontSize_TxtBox.Text));
            DrawableFillColor fillColor = new DrawableFillColor(_FillColour);
            // Outline
            DrawableStrokeColor strokeColor = new DrawableStrokeColor(_OutlineColour);
            DrawableStrokeWidth strokeWidth = new DrawableStrokeWidth(1);
            // Antialiasing
            DrawableStrokeAntialias strokeAntialias = new DrawableStrokeAntialias(true);
            DrawableTextAntialias textAntialias = new DrawableTextAntialias(true);
            // Text output
            DrawableText text = new DrawableText(0, 0, $"{output}");
            DrawableGravity gravity = new DrawableGravity(Gravity.Center);
            // Settings
            IDrawable[] parameters = new IDrawable[] { font, pointSize, fillColor, strokeColor, strokeWidth, strokeAntialias, textAntialias, text, gravity };

            fontTest.Draw(parameters);
            String upper = Char.IsUpper(output) ? "u" : "";
            fontTest.Write($"Output/{output}{upper}.png");
        }
Does anyone have any experience with doing something like this?

Thanks in advance,

Elliott
Coordinator
Apr 13, 2016 at 9:00 PM
Have you tried specifying the whole path to the font?
Apr 28, 2016 at 10:28 PM
I have just tried the full path and it still isnt working.

The code I used for the path is:
DrawableFont font = new DrawableFont($"{AppDomain.CurrentDomain.BaseDirectory}\\Fonts\\28_Days_Later.ttf");
Coordinator
Apr 29, 2016 at 8:34 AM
Edited Apr 29, 2016 at 8:34 AM
It seems you found a bug in the DrawableFont class. I just pushed a patch to the GIT repository to make sure this is fixed in the next release. The next release will be published tomorrow. That release will also include some syntax-sugar for the drawable classes. Your code can be rewritten to this:
private void OutputFont(Char output)
{
  MagickImage fontTest = new MagickImage(Color.Transparent, Int32.Parse(Width_Text_Box.Text), Int32.Parse(Height_Text_Box.Text))
  {
    Format = MagickFormat.Png
  };

  fontTest.Draw(new Drawables()
    // Font parameters
    .Font($@"{AppDomain.CurrentDomain.BaseDirectory}\FontsFonts\28_Days_Later.ttf")
    .FontPointSize(Int32.Parse(FontSize_TxtBox.Text))
    .FillColor(_FillColour)
    // Outline
    .StrokeColor(_OutlineColour)
    .StrokeWidth(1)
    // Antialiasing
    .StrokeAntialias(true)
    .TextAntialias(true)
    // Text output
    .Text(0, 0, $"{output}")
    .Gravity(Gravity.Center));

  String upper = Char.IsUpper(output) ? "u" : "";
  fontTest.Write($"Output/{output}{upper}.png");
}
Apr 29, 2016 at 11:12 PM
Thanks for looking into this and thanks for the code sugar. I will update you once I have got the update to let you know how it all works :)

Thanks for the quick responses
May 1, 2016 at 1:02 AM
Hey dlemstra,

After testing this, it all works. Thanks for looking into this and getting a patch out so quickly :)