Skip to content

Commit 04a56ec

Browse files
committed
Replaced more of old functionality
1 parent 9923688 commit 04a56ec

17 files changed

Lines changed: 638 additions & 623 deletions

File tree

src/EPPlus.Export.ImageRenderer/DrawingBase.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Date Author Change
1414
using EPPlus.Export.ImageRenderer;
1515
using EPPlus.Export.ImageRenderer.Utils;
1616
using EPPlus.Fonts.OpenType;
17+
using EPPlus.Fonts.OpenType.Integration;
1718
using EPPlus.Graphics;
1819
using EPPlusImageRenderer.RenderItems;
1920
using OfficeOpenXml;
@@ -35,7 +36,11 @@ internal DrawingBase(ExcelDrawing drawing)
3536

3637
var wb = drawing._drawings.Worksheet.Workbook;
3738
Theme = wb.ThemeManager.GetOrCreateTheme();
38-
TextMeasurer = new FontMeasurerTrueType();
39+
40+
//Theme.FontScheme.MajorFont[0].Typeface
41+
//var shaper = OpenTypeFonts.GetTextShaper(
42+
//Theme.FontScheme.MajorFont[0].Typeface, Theme.FontScheme.MinorFont[0].Typeface)
43+
TextMeasurer = new OpenTypeFontTextMeasurer();
3944
}
4045

4146

src/EPPlus.Export.ImageRenderer/RenderItems/Shared/ParagraphItem.cs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using EPPlus.Fonts.OpenType;
22
using EPPlus.Fonts.OpenType.Integration;
33
using EPPlus.Fonts.OpenType.TextShaping;
4-
using EPPlus.Fonts.OpenType.Integration;
5-
using EPPlus.Fonts.OpenType.TextShaping;
64
using EPPlus.Fonts.OpenType.Utils;
75
using EPPlus.Graphics;
86
using EPPlusImageRenderer;
@@ -19,13 +17,12 @@
1917
using System.Text;
2018
using System.Text.RegularExpressions;
2119
using EPPlusColorConverter = OfficeOpenXml.Utils.TypeConversion.ColorConverter;
22-
using EPPlus.Fonts.OpenType.TextShaping.DataHolders;
2320

2421
namespace EPPlus.Export.ImageRenderer.RenderItems.Shared
2522
{
2623
internal abstract class ParagraphItem : RenderItem
2724
{
28-
ITextMeasurerWrap _measurer;
25+
TextLayoutEngine _layout;
2926

3027
double _leftMargin;
3128
double _rightMargin;
@@ -37,7 +34,7 @@ internal abstract class ParagraphItem : RenderItem
3734
List<string> _paragraphLines = new List<string>();
3835
protected List<string> _textRunDisplayText = new List<string>();
3936

40-
TextFragmentCollection _textFragments;
37+
TextFragmentCollectionSimple _textFragments;
4138
List<EPPlus.Fonts.OpenType.Integration.TextFragment> _newTextFragments;
4239
internal protected MeasurementFont _paragraphFont;
4340
internal TextBodyItem ParentTextBody { get; set; }
@@ -56,8 +53,8 @@ public ParagraphItem(TextBodyItem textBody, DrawingBase renderer, BoundingBox pa
5653
var defaultFont = new MeasurementFont { FontFamily = "Aptos Narrow", Size = 11, Style = MeasurementFontStyles.Regular };
5754
_paragraphFont = defaultFont;
5855

59-
_measurer = new FontMeasurerTrueType(defaultFont);
60-
ParagraphLineSpacing = GetParagraphLineSpacingInPoints(100, _measurer);
56+
_layout = OpenTypeFonts.GetTextLayoutEngineForFont(defaultFont);
57+
ParagraphLineSpacing = GetParagraphLineSpacingInPoints(100, (TextShaper)OpenTypeFonts.GetShaperForFont(defaultFont), defaultFont.Size);
6158
}
6259

6360
public ParagraphItem(TextBodyItem textBody, DrawingBase renderer, BoundingBox parent, ExcelDrawingParagraph p, string textIfEmpty = null) : base(renderer, parent)
@@ -140,45 +137,42 @@ public ParagraphItem(TextBodyItem textBody, DrawingBase renderer, BoundingBox pa
140137
Bounds.Width = parent.Width - _rightMargin - _leftMargin;
141138
}
142139

140+
//---Initialize / calculate lines and runs---
141+
//measurer must be set before AddLinesAndRichText
142+
_paragraphFont = p.DefaultRunProperties.GetMeasureFont();
143+
143144
//---Get measurer---
144-
_measurer = p._prd.Package.Settings.TextSettings.GenericTextMeasurerTrueType;
145+
_layout = OpenTypeFonts.GetTextLayoutEngineForFont(_paragraphFont);
145146

146147
//---Calculate linespacing---
147148
int numLines = _paragraphLines.Count;
148149
_lsType = p.LineSpacing.LineSpacingType;
149-
ParagraphLineSpacing = GetParagraphLineSpacingInPoints(p.LineSpacing.Value, _measurer);
150-
151-
//---Initialize / calculate lines and runs---
152-
//measurer must be set before AddLinesAndRichText
153-
_paragraphFont = p.DefaultRunProperties.GetMeasureFont();
154-
_measurer.SetFont(_paragraphFont);
150+
ParagraphLineSpacing = GetParagraphLineSpacingInPoints(p.LineSpacing.Value,
151+
(TextShaper) OpenTypeFonts.GetShaperForFont(_paragraphFont),
152+
_paragraphFont.Size);
155153

156154
AddLinesAndTextRuns(p, textIfEmpty);
157155
}
158156

159-
private double GetParagraphLineSpacingInPoints(double spacingValue, ITextMeasurerWrap fmExact)
157+
private double GetParagraphLineSpacingInPoints(double spacingValue, TextShaper fmExact, float fontSize)
160158
{
161159
if (_lsType == eDrawingTextLineSpacing.Exactly)
162160
{
163161
if (IsFirstParagraph)
164162
{
165163
_lineSpacingAscendantOnly = spacingValue;
166-
_lineSpacingAscendantOnly = spacingValue;
167164
}
168165
return spacingValue;
169-
return spacingValue;
170166
}
171167
else
172168
{
173169
var multiplier = (spacingValue / 100);
174170
_lsMultiplier = multiplier;
175171
if (IsFirstParagraph)
176172
{
177-
_lineSpacingAscendantOnly = multiplier * fmExact.GetBaseLine();
178-
_lineSpacingAscendantOnly = multiplier * fmExact.GetBaseLine();
173+
_lineSpacingAscendantOnly = multiplier * fmExact.GetAscentInPoints(fontSize);
179174
}
180-
return multiplier * fmExact.GetSingleLineSpacing();
181-
return multiplier * fmExact.GetSingleLineSpacing();
175+
return multiplier * fmExact.GetLineHeightInPoints(fontSize);
182176
}
183177
}
184178

@@ -201,8 +195,9 @@ public void AddText(string text, double prevWidth)
201195
}
202196
public void AddText(string text, ExcelTextFont font, bool isOld)
203197
{
204-
var measurer = new FontMeasurerTrueType();
205-
var displayText = measurer.MeasureAndWrapText(text, font.GetMeasureFont(), ParentTextBody.MaxWidth);
198+
var mf = font.GetMeasureFont();
199+
var measurer = OpenTypeFonts.GetTextLayoutEngineForFont(mf);
200+
var displayText = measurer.WrapText(text, mf.Size, ParentTextBody.MaxWidth);
206201
var container = CreateTextRun(text, font, Bounds, string.Join("\r\n", displayText.ToArray()));
207202
//container.BaseLineSpacing = _lineSpacingAscendantOnly;
208203
//container.LineSpacingPerNewLine = _lsMultiplier.Value * _measurer.GetSingleLineSpacing().PointToPixel(true);
@@ -214,7 +209,8 @@ public void AddText(string text, ExcelTextFont font, bool isOld)
214209

215210
public void AddText(string text, ExcelTextFont font, double prevWidth)
216211
{
217-
var measurer = new FontMeasurerTrueType();
212+
var mf = font.GetMeasureFont();
213+
var measurer = OpenTypeFonts.GetTextLayoutEngineForFont(mf);
218214
//var displayText = measurer.MeasureAndWrapTextLines(text, font.GetMeasureFont(), ParentTextBody.MaxWidth);
219215

220216
var container = CreateTextRun(text, font, Bounds, text);
@@ -278,9 +274,14 @@ internal void AddLinesAndTextRuns(string textIfEmpty)
278274
GenerateTextFragments(textIfEmpty);
279275
var lines = new List<TextLineSimple>();
280276

281-
var measurer = new FontMeasurerTrueType();
277+
var measurer = OpenTypeFonts.GetTextLayoutEngineForFont(_paragraphFont);
282278
var maxWidth = ParentTextBody.MaxWidth + 0.001; //TODO: fix for equal width issue;
283-
lines = measurer.MeasureAndWrapTextLines_New(textIfEmpty, _paragraphFont, maxWidth);
279+
280+
List<TextFragment> textFragments = new List<TextFragment>();
281+
var fragment = new TextFragment() { Font = _paragraphFont, Text = textIfEmpty };
282+
textFragments.Add(fragment);
283+
284+
lines = measurer.WrapRichTextLines(textFragments, maxWidth);
284285
bool lineSpacingIsExact = _lsMultiplier.HasValue == false;
285286
double runLineSpacing = 0;
286287
double greatestWidth = 0;
@@ -484,7 +485,7 @@ private void AddLinesAndTextRuns(ExcelDrawingParagraph p, string textIfEmpty)
484485

485486
List<TextLineSimple> WrapToSimpleTextLines(ExcelDrawingParagraph p)
486487
{
487-
var ttMeasurer = (FontMeasurerTrueType)_measurer;
488+
var ttMeasurer = (FontMeasurerTrueType)_layout;
488489

489490
if (_newTextFragments.Count > 0)
490491
{

src/EPPlus.Export.ImageRenderer/RenderItems/Shared/TextBodyItem.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using OfficeOpenXml.Drawing;
88
using OfficeOpenXml.Drawing.Theme;
99
using OfficeOpenXml.Interfaces.Drawing.Text;
10+
using OfficeOpenXml.Interfaces.Fonts;
1011
using OfficeOpenXml.Style;
1112
using OfficeOpenXml.Utils;
1213
using System;
@@ -174,7 +175,8 @@ internal virtual void ImportTextBody(ExcelTextBody body, ExcelHorizontalAlignmen
174175

175176
Bounds.Top = GetAlignmentVertical();
176177
}
177-
private double GetParagraphAscendantSpacingInPixels(eDrawingTextLineSpacing lineSpacingType, double spacingValue, ITextMeasurerWrap fmExact, out double multiplier)
178+
179+
private double GetParagraphAscendantSpacingInPixels(eDrawingTextLineSpacing lineSpacingType, double spacingValue, ITextShaper fmExact, float fontSize, out double multiplier)
178180
{
179181
if (lineSpacingType == eDrawingTextLineSpacing.Exactly)
180182
{
@@ -184,7 +186,7 @@ private double GetParagraphAscendantSpacingInPixels(eDrawingTextLineSpacing line
184186
else
185187
{
186188
multiplier = (spacingValue / 100);
187-
return multiplier * fmExact.GetBaseLine().PointToPixel();
189+
return multiplier * fmExact.GetAscentInPoints(fontSize).PointToPixel();
188190
}
189191
}
190192

src/EPPlus.Export.Pdf/PdfLayout/PdfHeaderFooterLayout.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Date Author Change
1414
using EPPlus.Export.Pdf.PdfSettings;
1515
using EPPlus.Fonts.OpenType;
1616
using EPPlus.Fonts.OpenType.Integration;
17+
using EPPlus.Fonts.OpenType.TextShaping;
1718
using EPPlus.Graphics;
1819
using EPPlus.Graphics.Math;
1920
using OfficeOpenXml;
@@ -112,8 +113,11 @@ public PdfHeaderFooterLayout(ExcelHeaderFooterTextCollection textCollection, Exc
112113
0 => MeasurementFontStyles.Regular,
113114
var s => s
114115
};
115-
FontMeasurerTrueType fontMeasurerTrueType = new FontMeasurerTrueType();
116-
var result = fontMeasurerTrueType.MeasureText(textFormat.Text, font);
116+
117+
var shaper = (TextShaper)OpenTypeFonts.GetShaperForFont(font);
118+
var measurer = new OpenTypeFontTextMeasurer(shaper);
119+
120+
var result = measurer.MeasureText(textFormat.Text, font);
117121
textFormat.TextLength = result.Width;
118122
textLength += result.Width;
119123
textFormat.TextHeight = result.Height;

src/EPPlus.Export.Pdf/PdfLayout/PdfWorksheetLayout.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ Date Author Change
1313
using EPPlus.Export.Pdf.PdfResources;
1414
using EPPlus.Export.Pdf.PdfSettings;
1515
using EPPlus.Fonts.OpenType;
16+
using EPPlus.Fonts.OpenType.Integration;
17+
using EPPlus.Fonts.OpenType.TextShaping;
1618
using EPPlus.Graphics;
1719
using EPPlus.Graphics.Math;
1820
using EPPlus.Graphics.Units;
1921
using OfficeOpenXml;
22+
using OfficeOpenXml.Drawing.Chart;
2023
using OfficeOpenXml.Interfaces.Drawing.Text;
2124
using OfficeOpenXml.Style;
2225
using OfficeOpenXml.Style.Table;
@@ -81,8 +84,11 @@ public static int AddColumnsForNonWrappedText(ExcelWorksheet ws)
8184
{
8285
double columnWidth = UnitConversion.ExcelColumnWidthToPoints(ws.Column(ws.Dimension._toCol).Width, ZeroCharWidth);
8386
int columnsToAdd = 0;
84-
FontMeasurerTrueType fontMeasurerTrueType = new FontMeasurerTrueType();
87+
8588
MeasurementFont font = new MeasurementFont();
89+
var shaper = (TextShaper)OpenTypeFonts.GetShaperForFont(font);
90+
OpenTypeFontTextMeasurer fontMeasurerTrueType = new OpenTypeFontTextMeasurer(shaper);
91+
8692
double textLength = 0;
8793
for (int row = 1; row <= ws.Dimension._toRow; row++)
8894
{
@@ -424,12 +430,14 @@ private void HandleDrawings(ExcelWorksheet worksheet)
424430
//Get the width of the character 0 from the current themes default font style. It's used to calculate width of cells.
425431
private double GetThemeFont0Width(ExcelWorksheet ws)
426432
{
427-
FontMeasurerTrueType fontMeasurerTrueType = new FontMeasurerTrueType();
428433
MeasurementFont font = new MeasurementFont();
429434
var ns = ws.Workbook.Styles.GetNormalStyle();
430435
font.FontFamily = ns.Style.Font.Name;
431436
font.Size = ns.Style.Font.Size;
432437
font.Style = MeasurementFontStyles.Regular;
438+
439+
var shaper = OpenTypeFonts.GetShaperForFont(font);
440+
OpenTypeFontTextMeasurer fontMeasurerTrueType = new OpenTypeFontTextMeasurer(shaper);
433441
var result = fontMeasurerTrueType.MeasureText("0", font);
434442
return result.Width;
435443
}

src/EPPlus.Fonts.OpenType.Tests/Reading/TtfReadingTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ public void TestWrapText()
216216
Style = MeasurementFontStyles.Regular
217217
};
218218

219-
FontMeasurerTrueType fontMeasurer = new FontMeasurerTrueType(fontSize, fontName);
220-
var strings = fontMeasurer.MeasureAndWrapText(testStr, mf, MaxPixelWidth);
219+
var fontMeasurer = OpenTypeFonts.GetTextLayoutEngineForFont(mf);
220+
var strings = fontMeasurer.WrapText(testStr, mf.Size, MaxPixelWidth);
221221

222222
Assert.AreEqual("hello the", strings[0]);
223223
Assert.AreEqual("most", strings[1]);
@@ -238,8 +238,8 @@ public void TestWrapTextWhenLineBreaks()
238238
Style = MeasurementFontStyles.Regular
239239
};
240240

241-
FontMeasurerTrueType fontMeasurer = new FontMeasurerTrueType(fontSize, fontName);
242-
var strings = fontMeasurer.MeasureAndWrapText(testStr, mf, MaxPixelWidth);
241+
var fontMeasurer = OpenTypeFonts.GetTextLayoutEngineForFont(mf);
242+
var strings = fontMeasurer.WrapText(testStr, mf.Size, MaxPixelWidth);
243243

244244
Assert.AreEqual("hello", strings[0]);
245245
Assert.AreEqual(" the", strings[1]);

src/EPPlus.Fonts.OpenType.Tests/Regression/RegressionTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,7 @@ public void GetFromCacheBoldItalicShouldWork()
267267
Style = MeasurementFontStyles.Bold | MeasurementFontStyles.Italic
268268
};
269269

270-
var ttTextMeasurer = new FontMeasurerTrueType();
271-
ttTextMeasurer.SetFont(boldItalic);
270+
var ttTextMeasurer = OpenTypeFonts.LoadFont(boldItalic.FontFamily, FontSubFamily.BoldItalic);
272271

273272
var cacheKey = OpenTypeFonts.BuildCacheKey("Aptos Narrow", FontSubFamily.BoldItalic, Enumerable.Empty<string>(), searchSystemDirectories: true);
274273
var cachedFont = OpenTypeFontCache.GetFromCache(cacheKey);

src/EPPlus.Fonts.OpenType.Tests/TextFragmentCollectionTests.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ public void MeasureWrappedWidthsWithInternalLineBreaks()
115115
var maxSizePoints = Math.Round(300d, 0, MidpointRounding.AwayFromZero).PixelToPoint();
116116
var ttMeasurer = OpenTypeFonts.GetTextLayoutEngineForFont(font1);
117117

118-
var textFragments = new TextFragmentCollection(lstOfRichText);
118+
var textFragments = new TextFragmentCollectionSimple(fonts, lstOfRichText);
119119

120-
var wrappedLines = ttMeasurer.WrapRichTextLines(fonts, maxSizePoints);
120+
var wrappedLines = ttMeasurer.WrapRichTextLines(textFragments, maxSizePoints);
121121

122122
var line1 = wrappedLines[0];
123123

@@ -221,11 +221,11 @@ public void MeasureWrappedWidths()
221221
List<MeasurementFont> fonts = new() {font2, font3, font4, font5, font6 };
222222

223223
var maxSizePoints = Math.Round(300d, 0, MidpointRounding.AwayFromZero).PixelToPoint();
224-
var ttMeasurer = new FontMeasurerTrueType(font2);
224+
var ttMeasurer = OpenTypeFonts.GetTextLayoutEngineForFont(font2);
225225

226-
var textFragments = new TextFragmentCollection(lstOfRichText);
226+
var textFragments = new TextFragmentCollectionSimple(fonts, lstOfRichText);
227227

228-
var wrappedLines = ttMeasurer.WrapMultipleTextFragmentsToTextLines(textFragments, fonts, maxSizePoints);
228+
var wrappedLines = ttMeasurer.WrapRichTextLines(textFragments, maxSizePoints);
229229

230230
var pixels1 = Math.Round(wrappedLines[0].RtFragments[0].Width.PointToPixel(), 0, MidpointRounding.AwayFromZero);
231231
var pixels2 = Math.Round(wrappedLines[0].RtFragments[1].Width.PointToPixel(), 0, MidpointRounding.AwayFromZero);
@@ -273,20 +273,20 @@ public void CorrectTextLinesAreReturnedWhenSmallMaxWidth()
273273
Style = MeasurementFontStyles.Regular
274274
};
275275

276-
var ttMeasurer = new FontMeasurerTrueType(defaultFont);
276+
var ttMeasurer = OpenTypeFonts.GetTextLayoutEngineForFont(defaultFont);
277277

278278
var txt = "This is my text";
279279

280280
//This should be small enough to put each word on a new row.
281281
var maxWidth = 21.5d;
282282

283-
var txtLines = ttMeasurer.MeasureAndWrapTextLines(txt, defaultFont, maxWidth);
283+
var txtLines = ttMeasurer.WrapText(txt, 11f, maxWidth);
284284

285285
Assert.AreEqual(4, txtLines.Count);
286-
Assert.AreEqual("This", txtLines[0].Text);
287-
Assert.AreEqual("is", txtLines[1].Text);
288-
Assert.AreEqual("my", txtLines[2].Text);
289-
Assert.AreEqual("text", txtLines[3].Text);
286+
Assert.AreEqual("This", txtLines[0]);
287+
Assert.AreEqual("is", txtLines[1]);
288+
Assert.AreEqual("my", txtLines[2]);
289+
Assert.AreEqual("text", txtLines[3]);
290290
}
291291
}
292292
}

0 commit comments

Comments
 (0)