mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-10-14 10:55:04 +08:00
add test for svg exporter and escape xml characters
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
namespace UglyToad.PdfPig.DocumentLayoutAnalysis.Export
|
||||
using System.Xml;
|
||||
|
||||
namespace UglyToad.PdfPig.DocumentLayoutAnalysis.Export
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -44,16 +46,17 @@
|
||||
}
|
||||
}
|
||||
|
||||
var doc = new XmlDocument();
|
||||
foreach (var letter in page.Letters)
|
||||
{
|
||||
builder.Append(LetterToSvg(letter, page.Height));
|
||||
builder.Append(LetterToSvg(letter, page.Height, doc));
|
||||
}
|
||||
|
||||
builder.Append("</g></svg>");
|
||||
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 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'";
|
||||
|
||||
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)
|
||||
@@ -121,6 +129,13 @@
|
||||
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)
|
||||
{
|
||||
if (color == null) return "";
|
||||
|
@@ -1,4 +1,6 @@
|
||||
namespace UglyToad.PdfPig.Tests.Integration
|
||||
using UglyToad.PdfPig.DocumentLayoutAnalysis.Export;
|
||||
|
||||
namespace UglyToad.PdfPig.Tests.Integration
|
||||
{
|
||||
using System;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
[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()
|
||||
{
|
||||
|
Reference in New Issue
Block a user