Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/EPPlus.Export.ImageRenderer.Test/TestTextContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void TestNonStandardFontSizesMultiLineLargeFont()
Assert.AreEqual(expectedHeight, Math.Round(container.Height, 0));
}

[TestMethod, Ignore]
[TestMethod]
public void TestNonStandardFontSizesMultiLineLargeFontGoudyStout()
{
string content = "TextBox\r\na very long line2\r\nline3";
Expand All @@ -95,6 +95,7 @@ public void TestNonStandardFontSizesMultiLineLargeFontGoudyStout()
var container = new TextContainer(content, mf, true, true);
//0,072265625 * font size width correction for pixels
//0,0442708333333333 * font size height correction for pixels
Assert.AreEqual(expectedWidth, Math.Round(container.Width, 0));
Assert.AreEqual(expectedHeight, Math.Round(container.Height, 0));
}
}
Expand Down
115 changes: 92 additions & 23 deletions src/EPPlus.Export.ImageRenderer/Svg/SvgParagraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Date Author Change
*************************************************************************************************
27/11/2025 EPPlus Software AB EPPlus 9
*************************************************************************************************/
using EPPlus.Fonts.OpenType;
using EPPlus.Fonts.OpenType.Utils;
using EPPlusImageRenderer.RenderItems;
using OfficeOpenXml.Drawing;
Expand All @@ -18,12 +19,15 @@ Date Author Change
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace EPPlusImageRenderer.Svg
{
internal class SvgParagraph : SvgRenderItem
{
int numLines = 0;

public override void Render(StringBuilder sb)
{
sb.Append("<text ");
Expand Down Expand Up @@ -134,16 +138,84 @@ public SvgParagraph(ExcelDrawingParagraph p, RectBase paragraphArea, string Vert
GetBounds(out double l, out double t, out double r, out double b);
var textMaxWidth = r - l;

paragraphHeight = p.GetParagraphHeightInPixels(fmtt, textMaxWidth.PixelToPoint());

lnType = p.LineSpacing.LineSpacingType;

foreach (var run in p.TextRuns)
{
AddTextRun(run, paragraphArea.Bottom, yPosition);
}

if (p._paragraphs.WrapText == eTextWrappingType.Square)
{
List<string> textFragments = new List<string>();
List<MeasurementFont> fonts = new List<MeasurementFont>();

foreach (var txtRun in p.TextRuns)
{
textFragments.Add(txtRun.Text);
fonts.Add(txtRun.GetMeasurementFont());
}

var trueTypeMeasurer = (FontMeasurerTrueType)fmtt;
var maxWidthPoints = textMaxWidth.PixelToPoint();
var svgLines = trueTypeMeasurer.WrapMultipleTextFragments(textFragments, fonts, maxWidthPoints);

numLines = svgLines.Count;

List<string> txtRunStrings = new List<string>();
List<int> txtRunStartIndicies = new List<int>();
List<int> txtRunEndIndicies = new List<int>();

int lastIndex = 0;
for (int i = 0; i < TextRuns.Count; i++)
{
var txtString = TextRuns[i].originalText;
txtRunStrings.Add(txtString);
var indexOfRun = p.Text.IndexOf(txtString, lastIndex);
txtRunStartIndicies.Add(indexOfRun);
txtRunEndIndicies.Add(indexOfRun + txtString.Length);
lastIndex = indexOfRun;
}

List<int> lineIndicies = new List<int>();
lastIndex = 0;

//Last line should be handled by paragraph handling
for (int i = 0; i< svgLines.Count(); i++)
{
var txtString = svgLines[i];
txtRunStrings.Add(txtString);
var startIndex = p.Text.IndexOf(txtString, lastIndex);
lastIndex = startIndex + txtString.Length;
lineIndicies.Add(startIndex + txtString.Length);
}

for (int i = 0; i < lineIndicies.Count; i++)
{
var lnBreakPosition = lineIndicies[i];
for (int j = 0; j < txtRunEndIndicies.Count; j++)
{
var start = txtRunStartIndicies[j];
var end = txtRunEndIndicies[j];

bool containsBreak = (start <= lnBreakPosition && lnBreakPosition < end);
if (containsBreak)
{
var localLnBreakPosition = lnBreakPosition - start;
TextRuns[j].InsertLineBreak(localLnBreakPosition);
break;
}
}
}
}
else
{
numLines = p.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).Count();
}
}



/// <summary>
/// First paragraph must use different linespacing
/// </summary>
Expand Down Expand Up @@ -257,6 +329,7 @@ internal void AddTextRun(ExcelParagraphTextRunBase txtRun, double clippingHeight

SvgTextRun textRun;


if (TextRuns.Count == 0 && IsFirstParagraph == true)
{
textRun = new SvgTextRun(txtRun, lineSpacing, textMaxWidth, clippingHeight, XPos, yPosition, LineSpacingAscendantOnly);
Expand All @@ -278,27 +351,23 @@ internal void AddTextRun(ExcelParagraphTextRunBase txtRun, double clippingHeight

internal double GetBottomYPosition()
{
//int numberOfLines = 0;
//foreach (var textRun in TextRuns)
//{
// numberOfLines += textRun.GetLineCount();
//}

//double lineSpacingTotal;
//if(IsFirstParagraph)
//{
// lineSpacingTotal = LineSpacingAscendantOnly + LineSpacing * (numberOfLines - 1);
//}
//else
//{
// lineSpacingTotal = LineSpacing * numberOfLines;
//}

//heigh

//var totalY = ParagraphArea.Top + lineSpacingTotal;

return ParagraphArea.Top + paragraphHeight;
double bottomY = 0;
if (IsFirstParagraph)
{
bottomY = LineSpacingAscendantOnly + LineSpacing * (numLines - 1);
}
else
{
bottomY = LineSpacing * numLines;
}
return ParagraphArea.Top + bottomY;
}

internal void CalculateTextWrapping(double maxWidth, MeasurementFont mFont, string fullParagraphText)
{
List<string> NewContentLines = new List<string>();
fmtt.SetFont(mFont);
var textWidth = fmtt.MeasureText(fullParagraphText, mFont);
}
}
}
6 changes: 0 additions & 6 deletions src/EPPlus.Export.ImageRenderer/Svg/SvgShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,6 @@ private void LoadTextBox()
//Paragraph level begins
foreach (var paragraph in _shape.TextBody.Paragraphs)
{
//Do not render empty paragraphs
if (paragraph.TextRuns.Count == 0)
{
continue;
}

var svgParagraph = textBox.AddParagraph(paragraph);
}
}
Expand Down
34 changes: 26 additions & 8 deletions src/EPPlus.Export.ImageRenderer/Svg/SvgTextRun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ namespace EPPlusImageRenderer.Svg
internal class SvgTextRun : SvgRenderItem
{
double LineSpacingPerNewLine;
double _yPosition;
double _yPositionOriginal;
double dyPos;
double _yEndPos;
double ClippingHeight = Double.NaN;
double fontSizeInPixels;
eTextAlignment horizontalTextAlignment;
Expand All @@ -46,7 +48,8 @@ internal class SvgTextRun : SvgRenderItem
double BaselineSpacing;

string horizontalAttribute;
string originalText;
internal readonly string originalText;
private string currentText;

double _xPosition;

Expand All @@ -63,6 +66,7 @@ internal SvgTextRun(ExcelParagraphTextRunBase textRun, double lineSpacing, doubl
{
originalText = textRun.Text;
Lines = SplitIntoLines(originalText);
currentText = originalText;

measurementFont = textRun.GetMeasureFont();

Expand All @@ -83,7 +87,8 @@ internal SvgTextRun(ExcelParagraphTextRunBase textRun, double lineSpacing, doubl
BaselineSpacing = baselineLineSpacing;

_xPosition = xPosition;
_yPosition = yPosition;
_yPositionOriginal = yPosition;
_yEndPos = yPosition;

//origin.X = xPosition;
//origin.Y = yPosition;
Expand Down Expand Up @@ -183,7 +188,8 @@ internal SvgTextRun(string text, ExcelTextFont font, double lineSpacing, double
BaselineSpacing = baselineLineSpacing;

_xPosition = xPosition;
_yPosition = yPosition;
_yPositionOriginal = yPosition;
_yEndPos = yPosition;

//origin.X = xPosition;
//origin.Y = yPosition;
Expand Down Expand Up @@ -341,7 +347,8 @@ internal SvgTextRun(ExcelRichText textRun, double lineSpacing, double textMaxX,
BaselineSpacing = fmExact.GetBaseLine().PointToPixel(true);

_xPosition = xPosition;
_yPosition = yPosition;
_yPositionOriginal = yPosition;
_yEndPos = yPosition;

fontSizeInPixels = ((double)mf.Size).PointToPixel(true);

Expand All @@ -367,6 +374,7 @@ public override void Render(StringBuilder sb)
{
string finalString = "";
bool useBaselineSpacing = double.IsNaN(BaselineSpacing) == false;
Lines = SplitIntoLines(currentText);

foreach (var line in Lines)
{
Expand All @@ -383,8 +391,8 @@ public override void Render(StringBuilder sb)

yIncrease = EPPlus.Fonts.OpenType.Utils.TextUtils.RoundToWhole(yIncrease);

_yPosition += yIncrease;
if (Double.IsNaN(ClippingHeight) == false && _yPosition >= ClippingHeight)
_yEndPos += yIncrease;
if (Double.IsNaN(ClippingHeight) == false && _yEndPos >= ClippingHeight)
{
visibility = "display=\"none\"";
}
Expand Down Expand Up @@ -424,7 +432,12 @@ private int GetNumberOfLines()
{
if (originalText != null)
{
SplitIntoLines(originalText);
if(currentText == null)
{
currentText = originalText;
}

SplitIntoLines(currentText);
return Lines.Count;
}
else
Expand Down Expand Up @@ -512,5 +525,10 @@ internal void CalculateTextWrapping(double maxWidth)
var newLines = textMesurer.MeasureAndWrapText(originalText, measurementFont, maxWidth);
Lines = newLines;
}

internal void InsertLineBreak(int insertPosition)
{
currentText = currentText.Insert(insertPosition, Environment.NewLine);
}
}
}
27 changes: 0 additions & 27 deletions src/EPPlus.Export.ImageRenderer/Text/LineFormatter.cs

This file was deleted.

11 changes: 9 additions & 2 deletions src/EPPlus.Export.ImageRenderer/Text/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,18 @@ internal SvgParagraph AddParagraph(ExcelDrawingParagraph item)
bool isFirst = Paragraphs.Count == 0;
var svgParagraph = new SvgParagraph(item, area, vertAlignAttribute, posY, isFirst);

//Set starting position for next paragraph
paragraphStartPosY = svgParagraph.GetBottomYPosition();

svgParagraph.FillColor = string.IsNullOrEmpty(fontColor) ? item.DefaultRunProperties.Fill.Color.Name : fontColor;

paragraphStartPosY = svgParagraph.GetBottomYPosition();
//Above we've calculated heights from empty paragraphs
//But below we do not add them for rendering (As they would not have any visible effect anyway)
if (item.TextRuns.Count != 0)
{
Paragraphs.Add(svgParagraph);
}

Paragraphs.Add(svgParagraph);
return svgParagraph;
}

Expand Down
12 changes: 0 additions & 12 deletions src/EPPlus.Export.ImageRenderer/Text/TextLine.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>latest</LangVersion>
Expand All @@ -20,12 +19,6 @@
<ProjectReference Include="..\EPPlus\EPPlus.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="MSTest.TestAdapter" />
<PackageReference Include="MSTest.TestFramework" />
</ItemGroup>

<ItemGroup>
<None Update="Fonts\BIZUDGothic-Bold.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
Loading