Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Revert "use skiasharp font instead of SixLabor fonts" #459

Merged
merged 1 commit into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
683 changes: 195 additions & 488 deletions src/EPPlus/EPPlus/ExcelRangeBase.cs

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions src/EPPlus/EPPlus/ExcelWorkbook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -653,11 +653,7 @@ public XmlDocument StylesXml

StringBuilder xml = new StringBuilder("<styleSheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\">");
xml.Append("<numFmts />");
xml.Append("<fonts count=\"2\">");
xml.Append("<font><sz val=\"11\" /><name val=\"Calibri\" /></font>");
xml.Append("<font><sz val=\"11\" /><name val=\"FangSong\" /></font>");
// As the System.Drawing.Graphics is a GDI+, no matter SixLabors or SkiaSharp, always give inconsistent text measuring result in Calibri font. Use a FangSong instead.
xml.Append("</fonts>");
xml.Append("<fonts count=\"1\"><font><sz val=\"11\" /><name val=\"Calibri\" /></font></fonts>");
xml.Append("<fills><fill><patternFill patternType=\"none\" /></fill><fill><patternFill patternType=\"gray125\" /></fill></fills>");
xml.Append("<borders><border><left /><right /><top /><bottom /><diagonal /></border></borders>");
xml.Append("<cellStyleXfs count=\"1\"><xf numFmtId=\"0\" fontId=\"0\" /></cellStyleXfs>");
Expand Down
2 changes: 1 addition & 1 deletion src/EPPlus/EPPlus/Magicodes.IE.EPPlus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.0" />
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="2.1.1" />
<PackageReference Include="SkiaSharp" Version="2.88.3" />
<PackageReference Include="SixLabors.Fonts" Version="1.0.0-beta18" />
</ItemGroup>
</Project>
17 changes: 9 additions & 8 deletions src/EPPlus/EPPlus/Style/ExcelFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
*******************************************************************************/

using System;
using SkiaSharp;
using SixLabors.ImageSharp;
using SixLabors.Fonts;

namespace OfficeOpenXml.Style
{
Expand Down Expand Up @@ -206,15 +207,15 @@ public ExcelVerticalAlignmentFont VerticalAlign
/// <summary>
/// Set the font from a Font object
/// </summary>
/// <param name="font"></param>
public void SetFromFont(SKFont font)
/// <param name="textRun"></param>
public void SetFromTextRun(TextRun textRun)
{
Name = font.Typeface.FamilyName;
var font = textRun.Font;
Size = font.Size;
Bold = font.Typeface.IsBold;
Italic = font.Typeface.IsItalic;
UnderLine = font.Metrics.UnderlineThickness != null;
Strike = font.Metrics.StrikeoutThickness != null;
Bold = font.IsBold;
Italic = font.IsItalic;
UnderLine = (textRun.TextDecorations & TextDecorations.Underline) != 0;
Strike = (textRun.TextDecorations & TextDecorations.Strikeout) != 0;
}

internal override string Id
Expand Down
95 changes: 43 additions & 52 deletions src/EPPlus/EPPlus/Style/ExcelTextFont.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@
* Jan Källman Initial Release 2009-10-01
* Jan Källman License changed GPL-->LGPL 2011-12-16
*******************************************************************************/

using System;
using SixLabors.ImageSharp;
using System.Globalization;
using System.Xml;
using Magicodes.IE.EPPlus.SixLabors;
using SixLabors.Fonts;
using SixLabors.ImageSharp.PixelFormats;
using SkiaSharp;

namespace OfficeOpenXml.Style
{
Expand Down Expand Up @@ -64,7 +63,6 @@ public enum eUnderLineType
WavyHeavy,
Words
}

/// <summary>
/// Type of font strike
/// </summary>
Expand All @@ -74,17 +72,14 @@ public enum eStrikeType
No,
Single
}

/// <summary>
/// Used by Rich-text and Paragraphs.
/// </summary>
public class ExcelTextFont : XmlHelper
{
string _path;
XmlNode _rootNode;

internal ExcelTextFont(XmlNamespaceManager namespaceManager, XmlNode rootNode, string path,
string[] schemaNodeOrder)
internal ExcelTextFont(XmlNamespaceManager namespaceManager, XmlNode rootNode, string path, string[] schemaNodeOrder)
: base(namespaceManager, rootNode)
{
SchemaNodeOrder = schemaNodeOrder;
Expand All @@ -97,15 +92,15 @@ internal ExcelTextFont(XmlNamespaceManager namespaceManager, XmlNode rootNode, s
TopNode = node;
}
}

_path = path;
}

string _fontLatinPath = "a:latin/@typeface";

public string LatinFont
{
get { return GetXmlNodeString(_fontLatinPath); }
get
{
return GetXmlNodeString(_fontLatinPath);
}
set
{
CreateTopNode();
Expand All @@ -121,45 +116,46 @@ protected internal void CreateTopNode()
TopNode = _rootNode.SelectSingleNode(_path, NameSpaceManager);
}
}

string _fontCsPath = "a:cs/@typeface";

public string ComplexFont
{
get { return GetXmlNodeString(_fontCsPath); }
get
{
return GetXmlNodeString(_fontCsPath);
}
set
{
CreateTopNode();
SetXmlNodeString(_fontCsPath, value);
}
}

string _boldPath = "@b";

public bool Bold
{
get { return GetXmlNodeBool(_boldPath); }
get
{
return GetXmlNodeBool(_boldPath);
}
set
{
CreateTopNode();
SetXmlNodeString(_boldPath, value ? "1" : "0");
}
}

string _underLinePath = "@u";

public eUnderLineType UnderLine
{
get { return TranslateUnderline(GetXmlNodeString(_underLinePath)); }
get
{
return TranslateUnderline(GetXmlNodeString(_underLinePath));
}
set
{
CreateTopNode();
SetXmlNodeString(_underLinePath, TranslateUnderlineText(value));
}
}

string _underLineColorPath = "a:uFill/a:solidFill/a:srgbClr/@val";

public Color UnderLineColor
{
get
Expand All @@ -178,48 +174,49 @@ public Color UnderLineColor
set
{
CreateTopNode();
SetXmlNodeString(_underLineColorPath, value.ToArgbHex() /*.Substring(2)*/);
SetXmlNodeString(_underLineColorPath, value.ToArgbHex()/*.Substring(2)*/);
}
}

string _italicPath = "@i";

public bool Italic
{
get { return GetXmlNodeBool(_italicPath); }
get
{
return GetXmlNodeBool(_italicPath);
}
set
{
CreateTopNode();
SetXmlNodeString(_italicPath, value ? "1" : "0");
}
}

string _strikePath = "@strike";

public eStrikeType Strike
{
get { return TranslateStrike(GetXmlNodeString(_strikePath)); }
get
{
return TranslateStrike(GetXmlNodeString(_strikePath));
}
set
{
CreateTopNode();
SetXmlNodeString(_strikePath, TranslateStrikeText(value));
}
}

string _sizePath = "@sz";

public float Size
{
get { return GetXmlNodeInt(_sizePath) / 100; }
get
{
return GetXmlNodeInt(_sizePath) / 100;
}
set
{
CreateTopNode();
SetXmlNodeString(_sizePath, ((int)(value * 100)).ToString());
}
}

string _colorPath = "a:solidFill/a:srgbClr/@val";

public Color Color
{
get
Expand All @@ -238,12 +235,10 @@ public Color Color
set
{
CreateTopNode();
SetXmlNodeString(_colorPath, value.ToArgbHex() /*.Substring(2)*/);
SetXmlNodeString(_colorPath, value.ToArgbHex()/*.Substring(2)*/);
}
}

#region "Translate methods"

private eUnderLineType TranslateUnderline(string text)
{
switch (text)
Expand All @@ -258,7 +253,6 @@ private eUnderLineType TranslateUnderline(string text)
return (eUnderLineType)Enum.Parse(typeof(eUnderLineType), text);
}
}

private string TranslateUnderlineText(eUnderLineType value)
{
switch (value)
Expand All @@ -272,7 +266,6 @@ private string TranslateUnderlineText(eUnderLineType value)
return ret.Substring(0, 1).ToLower(CultureInfo.InvariantCulture) + ret.Substring(1, ret.Length - 1);
}
}

private eStrikeType TranslateStrike(string text)
{
switch (text)
Expand All @@ -285,7 +278,6 @@ private eStrikeType TranslateStrike(string text)
return eStrikeType.No;
}
}

private string TranslateStrikeText(eStrikeType value)
{
switch (value)
Expand All @@ -298,22 +290,21 @@ private string TranslateStrikeText(eStrikeType value)
return "noStrike";
}
}

#endregion

/// <summary>
/// Set the font style from a font object
/// Set the font style from a textRun object
/// </summary>
/// <param name="font"></param>
public void SetFromFont(SKFont font)
/// <param name="textRun"></param>
public void SetFromTextRun(TextRun textRun)
{
LatinFont = font.Typeface.FamilyName;
ComplexFont = font.Typeface.FamilyName;
var font = textRun.Font;
LatinFont = font.Name;
ComplexFont = font.Name;
Size = font.Size;
if (font.Typeface.IsBold) Bold = font.Typeface.IsBold;
if (font.Typeface.IsItalic) Italic = font.Typeface.IsItalic;
if(font.Metrics.UnderlineThickness != null) UnderLine = eUnderLineType.Single;
if(font.Metrics.StrikeoutThickness != null) Strike = eStrikeType.Single;
if (font.IsBold) Bold = font.IsBold;
if (font.IsItalic) Italic = font.IsItalic;
if ((textRun.TextDecorations & TextDecorations.Underline) != 0) UnderLine = eUnderLineType.Single;
if ((textRun.TextDecorations & TextDecorations.Strikeout) != 0) Strike = eStrikeType.Single;
}
}
}
}
15 changes: 8 additions & 7 deletions src/EPPlus/EPPlus/Style/XmlAccess/ExcelFontXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
* Jan Källman Initial Release 2009-10-01
* Jan Källman License changed GPL-->LGPL 2011-12-16
*******************************************************************************/
using SkiaSharp;
using System;
using SixLabors.ImageSharp;
using System.Globalization;
using System.Xml;
using SixLabors.Fonts;

namespace OfficeOpenXml.Style.XmlAccess
{
Expand Down Expand Up @@ -268,14 +269,14 @@ public string VerticalAlign
_verticalAlign = value;
}
}
public void SetFromFont(SKFont font)
public void SetFromTextRun(TextRun textRun)
{
Name = font.Typeface.FamilyName;
var font = textRun.Font;
Size = font.Size;
Bold = font.Typeface.IsBold;
Italic = font.Typeface.IsItalic;
UnderLine = font.Metrics.UnderlineThickness != null;
Strike = font.Metrics.StrikeoutThickness != null;
Bold = font.IsBold;
Italic = font.IsItalic;
UnderLine = (textRun.TextDecorations & TextDecorations.Underline) != 0;
Strike = (textRun.TextDecorations & TextDecorations.Strikeout) != 0;
}
public static float GetFontHeight(string name, float size)
{
Expand Down
19 changes: 12 additions & 7 deletions src/EPPlus/EPPlusTest/DrawingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
using OfficeOpenXml.Drawing;
using OfficeOpenXml.Drawing.Chart;
using OfficeOpenXml.Style;
using SixLabors.Fonts;
using SixLabors.ImageSharp;
using SkiaSharp;

namespace EPPlusTest
{
Expand Down Expand Up @@ -293,9 +293,11 @@ public void Scatter()
ser.DataLabel.Fill.Color = Color.BlueViolet;
ser.DataLabel.Font.Color = Color.White;
ser.DataLabel.Font.Italic = true;
using var skFontTypeface = SKTypeface.FromFamilyName("bookman old style");
using var font = new SKFont(skFontTypeface, 8f);
ser.DataLabel.Font.SetFromFont(font);
ser.DataLabel.Font.SetFromTextRun(
new TextRun
{
Font = SystemFonts.CreateFont("bookman old style", CultureInfo.CurrentCulture, 8)
});
Assert.IsTrue(chrt.ChartType == eChartType.XYScatterSmoothNoMarkers, "Invalid Charttype");
chrt.Series[0].Header = "Test serie";
chrt = ws.Drawings.AddChart("ScatterChart2", eChartType.XYScatterSmooth) as ExcelScatterChart;
Expand Down Expand Up @@ -619,9 +621,12 @@ public void Drawings()
rt = (ws.Drawings["shape2"] as ExcelShape).RichText.Add("\r\nAdded formated richtext");
rt.Bold = true;
rt.Color = Color.DarkGoldenrod;
using var skFontTypeface = SKTypeface.FromFamilyName("Times new roman");
using var font = new SKFont(skFontTypeface, 18f);
rt.SetFromFont(font);
rt.SetFromTextRun(
new TextRun
{
Font = SystemFonts.CreateFont("Times new roman", CultureInfo.CurrentCulture, 18),
TextDecorations = TextDecorations.Underline
});
rt.UnderLineColor = Color.Green;


Expand Down
Loading