|
| 1 | +/************************************************************************************************* |
| 2 | + Required Notice: Copyright (C) EPPlus Software AB. |
| 3 | + This software is licensed under PolyForm Noncommercial License 1.0.0 |
| 4 | + and may only be used for noncommercial purposes |
| 5 | + https://polyformproject.org/licenses/noncommercial/1.0.0/ |
| 6 | +
|
| 7 | + A commercial license to use this software can be purchased at https://epplussoftware.com |
| 8 | + ************************************************************************************************* |
| 9 | + Date Author Change |
| 10 | + ************************************************************************************************* |
| 11 | + 10/07/2025 EPPlus Software AB EPPlus.Fonts.OpenType 1.0 |
| 12 | + *************************************************************************************************/ |
| 13 | +using System; |
| 14 | +using System.Collections.Generic; |
| 15 | + |
| 16 | +namespace EPPlus.Fonts.OpenType |
| 17 | +{ |
| 18 | + /// <summary> |
| 19 | + /// Default font provider with automatic Noto Emoji fallback. |
| 20 | + /// </summary> |
| 21 | + public class DefaultFontProvider : IFontProvider |
| 22 | + { |
| 23 | + private readonly OpenTypeFont _primaryFont; |
| 24 | + private OpenTypeFont _emojiFont; |
| 25 | + private readonly object _lock = new object(); |
| 26 | + |
| 27 | + public OpenTypeFont PrimaryFont |
| 28 | + { |
| 29 | + get { return _primaryFont; } |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Creates a font provider with automatic emoji fallback. |
| 34 | + /// </summary> |
| 35 | + /// <param name="primaryFont">The user's primary font</param> |
| 36 | + public DefaultFontProvider(OpenTypeFont primaryFont) |
| 37 | + { |
| 38 | + if (primaryFont == null) |
| 39 | + throw new ArgumentNullException("primaryFont"); |
| 40 | + |
| 41 | + _primaryFont = primaryFont; |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Gets the emoji font, loading it on first access (lazy loading). |
| 46 | + /// Thread-safe for .NET 3.5 compatibility. |
| 47 | + /// </summary> |
| 48 | + private OpenTypeFont GetEmojiFontLazy() |
| 49 | + { |
| 50 | + if (_emojiFont == null) |
| 51 | + { |
| 52 | + lock (_lock) |
| 53 | + { |
| 54 | + if (_emojiFont == null) |
| 55 | + { |
| 56 | + _emojiFont = EmbeddedFonts.LoadNotoEmoji(); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + return _emojiFont; |
| 61 | + } |
| 62 | + |
| 63 | + public bool TryGetGlyphFont(uint codePoint, out OpenTypeFont font, out ushort glyphId) |
| 64 | + { |
| 65 | + // Try primary font first |
| 66 | + if (_primaryFont.CmapTable.TryGetGlyphId(codePoint, out glyphId)) |
| 67 | + { |
| 68 | + font = _primaryFont; |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
| 72 | + // Fallback to embedded Noto Emoji (lazy-loaded) |
| 73 | + var emojiFont = GetEmojiFontLazy(); |
| 74 | + if (emojiFont.CmapTable.TryGetGlyphId(codePoint, out glyphId)) |
| 75 | + { |
| 76 | + font = emojiFont; |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + // Not found - return primary with .notdef |
| 81 | + font = _primaryFont; |
| 82 | + glyphId = 0; |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + public IEnumerable<OpenTypeFont> GetAllFonts() |
| 87 | + { |
| 88 | + yield return _primaryFont; |
| 89 | + |
| 90 | + // Only include emoji font if it was actually loaded |
| 91 | + if (_emojiFont != null) |
| 92 | + { |
| 93 | + yield return _emojiFont; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments