|
|
@@ -1,4 +1,6 @@
|
|
|
-using System.Collections.Generic;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
using QuestPDF.Drawing;
|
|
|
using QuestPDF.Drawing.Exceptions;
|
|
|
using QuestPDF.Elements.Text.Items;
|
|
|
@@ -16,45 +18,99 @@ namespace QuestPDF.Elements.Text
|
|
|
public TextStyle Style { get; set; }
|
|
|
}
|
|
|
|
|
|
+ public class FallbackOption
|
|
|
+ {
|
|
|
+ public TextStyle Style { get; set; }
|
|
|
+ public SKFont Font { get; set; }
|
|
|
+ public SKTypeface Typeface { get; set; }
|
|
|
+ }
|
|
|
+
|
|
|
private static SKFontManager FontManager => SKFontManager.Default;
|
|
|
|
|
|
public static IEnumerable<TextRun> SplitWithFontFallback(this string text, TextStyle textStyle)
|
|
|
{
|
|
|
- var partStartIndex = 0;
|
|
|
- var partTextStyle = textStyle;
|
|
|
-
|
|
|
+ var fallbackOptions = GetFallbackOptions(textStyle).ToArray();
|
|
|
+
|
|
|
+ var spanStartIndex = 0;
|
|
|
+ var spanFallbackOption = fallbackOptions[0];
|
|
|
+
|
|
|
for (var i = 0; i < text.Length; i += char.IsSurrogatePair(text, i) ? 2 : 1)
|
|
|
{
|
|
|
var codepoint = char.ConvertToUtf32(text, i);
|
|
|
- var font = partTextStyle.ToFont();
|
|
|
- var typeface = font.Typeface;
|
|
|
+ var newFallbackOption = MatchFallbackOption(fallbackOptions, codepoint);
|
|
|
|
|
|
- if (font.ContainsGlyph(codepoint))
|
|
|
+ if (newFallbackOption == spanFallbackOption)
|
|
|
continue;
|
|
|
-
|
|
|
- var fallbackTypeface = FontManager.MatchCharacter(typeface.FamilyName, typeface.FontWeight, typeface.FontWidth, typeface.FontSlant, null, codepoint);
|
|
|
-
|
|
|
- if (fallbackTypeface == null)
|
|
|
- throw new DocumentDrawingException($"Could not find an appropriate font fallback for text: '{text}'");
|
|
|
|
|
|
yield return new TextRun
|
|
|
{
|
|
|
- Content = text.Substring(partStartIndex, i - partStartIndex),
|
|
|
- Style = partTextStyle
|
|
|
+ Content = text.Substring(spanStartIndex, i - spanStartIndex),
|
|
|
+ Style = spanFallbackOption.Style
|
|
|
};
|
|
|
|
|
|
- partStartIndex = i;
|
|
|
- partTextStyle = textStyle.FontFamily(fallbackTypeface.FamilyName).Weight((FontWeight)fallbackTypeface.FontWeight);
|
|
|
+ spanStartIndex = i;
|
|
|
+ spanFallbackOption = newFallbackOption;
|
|
|
}
|
|
|
|
|
|
- if (partStartIndex > text.Length)
|
|
|
+ if (spanStartIndex > text.Length)
|
|
|
yield break;
|
|
|
|
|
|
yield return new TextRun
|
|
|
{
|
|
|
- Content = text.Substring(partStartIndex, text.Length - partStartIndex),
|
|
|
- Style = partTextStyle
|
|
|
+ Content = text.Substring(spanStartIndex, text.Length - spanStartIndex),
|
|
|
+ Style = spanFallbackOption.Style
|
|
|
};
|
|
|
+
|
|
|
+ static IEnumerable<FallbackOption> GetFallbackOptions(TextStyle? textStyle)
|
|
|
+ {
|
|
|
+ while (textStyle != null)
|
|
|
+ {
|
|
|
+ var font = textStyle.ToFont();
|
|
|
+
|
|
|
+ yield return new FallbackOption
|
|
|
+ {
|
|
|
+ Style = textStyle,
|
|
|
+ Font = font,
|
|
|
+ Typeface = font.Typeface
|
|
|
+ };
|
|
|
+
|
|
|
+ textStyle = textStyle.Fallback;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ static FallbackOption MatchFallbackOption(ICollection<FallbackOption> fallbackOptions, int codepoint)
|
|
|
+ {
|
|
|
+ foreach (var fallbackOption in fallbackOptions)
|
|
|
+ {
|
|
|
+ if (fallbackOption.Font.ContainsGlyph(codepoint))
|
|
|
+ return fallbackOption;
|
|
|
+ }
|
|
|
+
|
|
|
+ var character = char.ConvertFromUtf32(codepoint);
|
|
|
+ var unicode = $"U-{codepoint:X4}";
|
|
|
+
|
|
|
+
|
|
|
+ var proposedFonts = FindFontsContainingGlyph(codepoint);
|
|
|
+ var proposedFontsFormatted = proposedFonts.Any() ? string.Join(", ", proposedFonts) : "no fonts available";
|
|
|
+
|
|
|
+ throw new DocumentDrawingException(
|
|
|
+ $"Could not find an appropriate font fallback for glyph: {unicode} '{character}'. " +
|
|
|
+ $"Font families available on current environment that contain this glyph: {proposedFontsFormatted}. " +
|
|
|
+ $"Possible solutions: " +
|
|
|
+ $"1) Use one of the listed fonts as the primary font in your document. " +
|
|
|
+ $"2) Configure the fallback TextStyle using the 'TextStyle.Fallback' method with one of the listed fonts. ");
|
|
|
+ }
|
|
|
+
|
|
|
+ static IEnumerable<string> FindFontsContainingGlyph(int codepoint)
|
|
|
+ {
|
|
|
+ var fontManager = SKFontManager.Default;
|
|
|
+
|
|
|
+ return fontManager
|
|
|
+ .GetFontFamilies()
|
|
|
+ .Select(fontManager.MatchFamily)
|
|
|
+ .Where(x => x.ContainsGlyph(codepoint))
|
|
|
+ .Select(x => x.FamilyName);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public static IEnumerable<ITextBlockItem> ApplyFontFallback(this ICollection<ITextBlockItem> textBlockItems)
|
|
|
@@ -63,6 +119,13 @@ namespace QuestPDF.Elements.Text
|
|
|
{
|
|
|
if (textBlockItem is TextBlockSpan textBlockSpan)
|
|
|
{
|
|
|
+ // perform font-fallback operation only when any fallback is available
|
|
|
+ if (textBlockSpan.Style.Fallback == null)
|
|
|
+ {
|
|
|
+ yield return textBlockSpan;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
var textRuns = textBlockSpan.Text.SplitWithFontFallback(textBlockSpan.Style);
|
|
|
|
|
|
foreach (var textRun in textRuns)
|