NanoXLSX.Formatting 3.0.0
Loading...
Searching...
No Matches
FontIdResolver.cs
1/*
2 * NanoXLSX is a small .NET library to generate and read XLSX (Microsoft Excel 2007 or newer) files in an easy and native way
3 * Copyright Raphael Stoeckli © 2026
4 * This library is licensed under the MIT License.
5 * You find a copy of the license in project folder or on: http://opensource.org/licenses/MIT
6 */
7
8using System.Collections.Generic;
9using System.Diagnostics.CodeAnalysis;
10using System.Linq;
11using NanoXLSX.Interfaces.Writer;
12using NanoXLSX.Registry;
13using NanoXLSX.Registry.Attributes;
14using NanoXLSX.Styles;
15using NanoXLSX.Utils.Xml;
16
18{
22 [NanoXlsxQueuePlugIn(PlugInUUID = "FORMATTED_TEXT_FONT_ID_RESOLVER", QueueUUID = PlugInUUID.WriterPrependingQueue)]
23 internal class FontIdResolver : IPluginWriter
24 {
25 #region privateFields
26 private StyleManager styleManager;
27 #endregion
28
29 #region properties
33 public Workbook Workbook { get; set; }
34
38 [ExcludeFromCodeCoverage] // NoOp
39 public XmlElement XmlElement { get { return null; } } // NoOp
40 #endregion
41
42 #region methods
46 public void Execute()
47 {
48 Font[] fonts = styleManager.GetFonts();
49 Dictionary<Font, int> fontIndexLookup = fonts
50 .Select((font, index) => new { font, index })
51 .ToDictionary(x => x.font, x => x.index);
52
53 for (int i = 0; i < Workbook.Worksheets.Count; i++)
54 {
55 List<FormattedText> formattedTextCells = Workbook.Worksheets[i].Cells.Values
56 .Select(c => c.Value)
57 .OfType<FormattedText>()
58 .Where(ft => ft.PhoneticProperties?.FontReference != null)
59 .ToList();
60 if (formattedTextCells.Count == 0)
61 {
62 continue;
63 }
64 foreach (FormattedText formattedText in formattedTextCells)
65 {
67 Font fontReference = props.FontReference;
68 if (fontIndexLookup.TryGetValue(fontReference, out int fontIndex))
69 {
70 props.FontId = fontIndex;
71 }
72 }
73 }
74 }
75
80 public void Init(IBaseWriter baseWriter)
81 {
82 this.Workbook = baseWriter.Workbook;
83 this.styleManager = baseWriter.Styles;
84 }
85 #endregion
86 }
87}
Represents phonetic properties, applied to the phonetic runs of a formatted text.
Represents a formatted text entry in Excel shared strings, supporting rich text with multiple runs an...
PhoneticProperties PhoneticProperties
Phonetic properties for the formatted text (Phonetic run / Ruby text).