Skip to content

Commit

Permalink
Fix XSSFRichTextString.ApplyFont
Browse files Browse the repository at this point in the history
Closes #1246
  • Loading branch information
Bykiev committed Feb 12, 2024
1 parent a14413d commit a0ef355
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
13 changes: 7 additions & 6 deletions ooxml/XSSF/UserModel/XSSFRichTextString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,20 @@ public void ApplyFont(int startIndex, int endIndex, short fontIndex)
ApplyFont(startIndex, endIndex, font);
}
internal void ApplyFont(SortedDictionary<int, CT_RPrElt> formats, int startIndex, int endIndex, CT_RPrElt fmt)
{

{
// delete format runs that fit between startIndex and endIndex
// runs intersecting startIndex and endIndex remain
//int runStartIdx = 0;
int runStartIdx = 0;
List<int> toRemoveKeys=new List<int>();
for (SortedDictionary<int, CT_RPrElt>.KeyCollection.Enumerator it = formats.Keys.GetEnumerator(); it.MoveNext(); )
{
int runIdx = it.Current;
if (runIdx >= startIndex && runIdx < endIndex)
int runEndIdx = it.Current;
if (runStartIdx >= startIndex && runEndIdx < endIndex)
{
toRemoveKeys.Add(runIdx);
toRemoveKeys.Add(runEndIdx);
}

runStartIdx = runEndIdx;
}
foreach (int key in toRemoveKeys)
{
Expand Down
26 changes: 26 additions & 0 deletions testcases/ooxml/XSSF/UserModel/TestXSSFRichTextString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ limitations under the License.
using NUnit.Framework;
using SixLabors.ImageSharp;
using System;
using System.IO;

namespace TestCases.XSSF.UserModel
{
Expand Down Expand Up @@ -595,5 +596,30 @@ public void Test59008Font()
//Assert.AreEqual("<xml-fragment/>", rts.GetFontAtIndex(s3 - 1).ToString());
Assert.AreEqual("<font></font>", rts.GetFontAtIndex(s3 - 1).ToString());
}

[Test]
public void TestMultipleFonts()
{
var f1 = new XSSFFont();
f1.FontName = "Arial";
f1.FontHeight = 18 * 20;
f1.IsBold = false;
f1.Color = IndexedColors.Red.Index;

var f2 = new XSSFFont();
f2.FontName = "Arial";
f2.FontHeight = 18 * 20;
f2.IsBold = true;
f2.Color = IndexedColors.Blue.Index;

XSSFRichTextString s = new XSSFRichTextString("0123");
s.ApplyFont(0, 2, f1);
s.ApplyFont(2, 4, f2);

Assert.AreEqual(s.GetFontAtIndex(0).Color, IndexedColors.Red.Index);
Assert.AreEqual(s.GetFontAtIndex(1).Color, IndexedColors.Red.Index);
Assert.AreEqual(s.GetFontAtIndex(2).Color, IndexedColors.Blue.Index);
Assert.AreEqual(s.GetFontAtIndex(3).Color, IndexedColors.Blue.Index);
}
}
}

0 comments on commit a0ef355

Please sign in to comment.