add test for svg exporter and escape xml characters

This commit is contained in:
Eliot Jones
2020-04-05 17:55:36 +01:00
parent 45ac8c8a60
commit 2a0a3fae69
2 changed files with 35 additions and 5 deletions

View File

@@ -1,4 +1,6 @@
namespace UglyToad.PdfPig.DocumentLayoutAnalysis.Export using System.Xml;
namespace UglyToad.PdfPig.DocumentLayoutAnalysis.Export
{ {
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -44,16 +46,17 @@
} }
} }
var doc = new XmlDocument();
foreach (var letter in page.Letters) foreach (var letter in page.Letters)
{ {
builder.Append(LetterToSvg(letter, page.Height)); builder.Append(LetterToSvg(letter, page.Height, doc));
} }
builder.Append("</g></svg>"); builder.Append("</g></svg>");
return builder.ToString(); return builder.ToString();
} }
private static string LetterToSvg(Letter l, double height) private static string LetterToSvg(Letter l, double height, XmlDocument doc)
{ {
string fontFamily = GetFontFamily(l.FontName, out string style, out string weight); string fontFamily = GetFontFamily(l.FontName, out string style, out string weight);
string rotation = ""; string rotation = "";
@@ -64,7 +67,12 @@
string fontSize = l.FontSize != 1 ? $"font-size='{l.FontSize:0}'" : $"style='font-size:{Math.Round(l.GlyphRectangle.Height, 2)}px'"; string fontSize = l.FontSize != 1 ? $"font-size='{l.FontSize:0}'" : $"style='font-size:{Math.Round(l.GlyphRectangle.Height, 2)}px'";
return $"<text x='{Math.Round(l.StartBaseLine.X, Rounding)}' y='{Math.Round(height - l.StartBaseLine.Y, Rounding)}'{rotation} font-family='{fontFamily}' font-style='{style}' font-weight='{weight}' {fontSize} fill='{ColorToSvg(l.Color)}'>{l.Value}</text>"; var safeValue = XmlEscape(l, doc);
var x = Math.Round(l.StartBaseLine.X, Rounding);
var y = Math.Round(height - l.StartBaseLine.Y, Rounding);
return $"<text x='{x}' y='{y}'{rotation} font-family='{fontFamily}' font-style='{style}' font-weight='{weight}' {fontSize} fill='{ColorToSvg(l.Color)}'>{safeValue}</text>"
+ Environment.NewLine;
} }
private static string GetFontFamily(string fontName, out string style, out string weight) private static string GetFontFamily(string fontName, out string style, out string weight)
@@ -121,6 +129,13 @@
return fontName; return fontName;
} }
private static string XmlEscape(Letter letter, XmlDocument doc)
{
XmlNode node = doc.CreateElement("root");
node.InnerText = letter.Value;
return node.InnerXml;
}
private static string ColorToSvg(IColor color) private static string ColorToSvg(IColor color)
{ {
if (color == null) return ""; if (color == null) return "";

View File

@@ -1,4 +1,6 @@
namespace UglyToad.PdfPig.Tests.Integration using UglyToad.PdfPig.DocumentLayoutAnalysis.Export;
namespace UglyToad.PdfPig.Tests.Integration
{ {
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@@ -145,6 +147,19 @@ used per estimate, we introduce a “complement class” Naive Bayes is often us
Assert.StartsWith("<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'", text); Assert.StartsWith("<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'", text);
} }
} }
[Fact]
public void CanExportSvg()
{
using (var document = PdfDocument.Open(GetFilename(), new ParsingOptions{ ClipPaths = true }))
{
var page = document.GetPage(1);
var svg = new SvgTextExporter().Get(page);
Assert.NotNull(svg);
}
}
private static IReadOnlyList<AssertablePositionData> GetPdfBoxPositionData() private static IReadOnlyList<AssertablePositionData> GetPdfBoxPositionData()
{ {