NanoXLSX.Formatting 3.2.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.Linq;
10using NanoXLSX.Interfaces.Writer;
11using NanoXLSX.Registry;
12using NanoXLSX.Registry.Attributes;
13using NanoXLSX.Styles;
14
16{
20 [NanoXlsxQueuePlugIn(PlugInUUID = "FORMATTED_TEXT_FONT_ID_RESOLVER", QueueUUID = PlugInUUID.PreparingInlineProcessor)]
21 internal class FontIdResolver : IPluginInlineWriteProcessor
22 {
23 #region properties
27 public IWriteContext WriteContext { get; set; }
28 #endregion
29
30 #region methods
34 public void Execute()
35 {
36 Workbook workbook = WriteContext.Workbook;
37 StyleManager styleManager = WriteContext.WriterProcessingData.StyleManager;
38 Font[] fonts = styleManager.GetFonts();
39 Dictionary<Font, int> fontIndexLookup = fonts
40 .Select((font, index) => new { font, index })
41 .ToDictionary(x => x.font, x => x.index);
42
43 for (int i = 0; i < workbook.Worksheets.Count; i++)
44 {
45 List<FormattedText> formattedTextCells = workbook.Worksheets[i].Cells.Values
46 .Select(c => c.Value)
47 .OfType<FormattedText>()
48 .Where(ft => ft.PhoneticProperties?.FontReference != null)
49 .ToList();
50 if (formattedTextCells.Count == 0)
51 {
52 continue;
53 }
54 foreach (FormattedText formattedText in formattedTextCells)
55 {
57 Font fontReference = props.FontReference;
58 if (fontIndexLookup.TryGetValue(fontReference, out int fontIndex))
59 {
60 props.FontId = fontIndex;
61 }
62 }
63 }
64 }
65
70 public void Init(IWriteContext context)
71 {
72 this.WriteContext = context;
73 }
74 #endregion
75 }
76}
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).