WPF FormattedText rasterises/cuts when ligatures used in some fonts (ti, tf, etc.)
Asked Answered
H

1

12

I'm currently using the font Carlito to render some FormattedText in WPF, in order to then print an eventual image as so:

DrawingVisual vis = new DrawingVisual();
DrawingContext dc = vis.RenderOpen();
...
FormattedText someText = new FormattedText("Delightful", System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typefaceCarlito, 52, brushBlack);
dc.DrawText(someText, new Point(rightX - someText.Width, 2900 - (someText.Height / 2)));
...
dlg.PrintVisual(vis, "Test print"));

I've chosen the text "Delightful" specifically here, as it contains one of the ligatures ('tf') that seem to give me an odd problem. Printing works fine without such ligatures, and printing to a PDF shows it being sent as a vector:

Correct text rendering

However, if I (re)introduce the ligature, the following happens:

Incorrect text rendering

The text becomes rasterised (ignore the pixelated line, that's a background image), and finishes short. In some cases, it stops at the ligature position. In others, it cuts off shortly afterward.

It does not happen with all fonts - most system fonts are fine, as is another third-party font I have chosen to use - but I still need the ability to stop this happening. The pixelation is not so much of an issue, but the cutoff is.

Is there a way I can force FormattedText to not transform consecutive characters into their ligature equivalent, or better still stop this happening?

Hypophyge answered 22/11, 2016 at 10:45 Comment(4)
I tried to reproduce your issue, but this just doesn't happen here. I created a new wpf project, pasted your code (and added a dc.close()) then printed it to a pdf file and everything looks just fine. I used Chrome to view the pdf.Dative
Can't reproduce either, maybe it's due to your PDF printer driver? I used "Microsoft Print To PDF", or to the font?Fimbriation
Here's my test code: pastebin.com/99mV87tUFimbriation
What versions of .NET and Visual Studio are you using? Can you still reproduce this?Sporangium
V
0

FormattedText's culture should not be taken from the current thread, because it's not entirely accurate, try fetching the WPF UI Culture like below instead:-

    var someText = new FormattedText(
            "Delightful",
            //CultureInfo.CreateSpecificCulture("en-US"),
            //System.Globalization.CultureInfo.CurrentCulture,
            this.Language.GetEquivalentCulture(),
            FlowDirection.LeftToRight,
            new Typeface("Carlito"),
            52,
            Brushes.Black,
            VisualTreeHelper.GetDpi(this).PixelsPerDip);

Also, the extra DPI parameter is recommended because old constructor is now obsolete.

The WPF UI Culture doesn't necessarily take the default OS culture, it needs to be explicitly set using Language property on FrameworkElement or via satellite assemblies!

Refer these for further clarification:- Globalization and localization overview - WPF .NET Framework | Microsoft Docs https://learn.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement.language?view=net-5.0

Vigue answered 12/8, 2022 at 20:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.