Overriding Font - Exception; Could not find font Arial

2/10/2020 4:22 PM

I have a report of an issue from one of my users where they are getting an exception due to CadLib not being able to find Arial.

I am using this code to override the font;

C# Code:
if (m_overrideFont)
    WW.Cad.Model.DxfModel.FallbackShxFont = null;
    DxfModel.FallbackTrueTypeFonts = new[] {
        new TrueTypeFontFallback(FontStyle.Regular, "arial.ttf"),
        new TrueTypeFontFallback(FontStyle.Bold, "arialbd.ttf"),
        new TrueTypeFontFallback(FontStyle.Italic, "ariali.ttf"),
        new TrueTypeFontFallback(FontStyle.Bold | FontStyle.Italic, "arialbi.ttf")
var dimensions = layout.Entities.OfType<DxfDimension>();
foreach (var dim in dimensions)
    dim.DimensionStyle.TextStyle.FontFilename = "arial.ttf";

This works fine on my dev and test environments. However, the user is getting;

C# Code:
System.Exception: Could not find font arial.ttf or any of the fallback fonts arial.ttf, arial.ttf.
   at ?.?(String ?, Boolean ?, Boolean ?, FontFamily& ?, FontStyle& ?, ?& ?)
   at ?.?(String ?, Boolean ?, Boolean ?, FontFamily& ?, FontStyle& ?, ?& ?)
   at ?.?..ctor(String ?, Boolean ?, Boolean ?)
   at ?.?(String ?, Boolean ?, Boolean ?)
   at ?.get_?()
   at ?..ctor(? ?)
   at ?.?(String ?, Double ?, Double ?, Double ?, DxfTextStyle ?, Color ?, String& ?)
   at ?.?(DxfText ?, Color ?, Int16 ?, Matrix4D ?, Bounds2D ?, Vector2D& ?, Vector2D& ?)
   at WW.Cad.Model.Entities.DxfText.?(IPathDrawer ?, Color ?, Int16 ?)
   at WW.Cad.Model.Entities.DxfText.DrawInternal(Wireframe context, IWireframeGraphicsFactory graphicsFactory)
   at WW.Cad.Model.Entities.DxfEntity.Draw(Wireframe context, IWireframeGraphicsFactory graphicsFactory)
   at WW.Cad.Drawing.BoundsCalculator.GetBounds(DxfModel model, Matrix4D modelTransform)
   at WW.Cad.Drawing.BoundsCalculator.GetBounds(DxfModel model)

The user has sent me a screenshot showing Arial exists.

I am using of CadLib.

Where should I start?

2/11/2020 11:16 AM

I don't know to be honest without knowing any details about the user's system. Looks like you specified the lookup dir correctly, although it shouldn't be necessary because CadLib will look in %WINDIR%\Fonts for true type fonts.

- Wout

2/11/2020 10:07 PM

I don't know why I hardcoded the Windows directory - thanks for pointing that out. It's resolved!

2/11/2020 10:21 PM

Great. But the DxfModel.AddTrueTypeFontLookupDirectories call shouldn't be necessary, as CadLib already looks in win fonts, doesn't it work without that call?

- Wout

2/12/2020 1:00 AM

It does yes. I removed that line entirely.