Skip to content

Commit

Permalink
Improve SVG reading and writing
Browse files Browse the repository at this point in the history
  • Loading branch information
eltos committed Mar 17, 2024
1 parent 42f561c commit 0ffd1c3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 42 deletions.
69 changes: 35 additions & 34 deletions PasteIntoFile/ClipboardContents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,44 +226,38 @@ public override void AddTo(IDataObject data) {
/// <summary>
/// Class to hold SVG data
/// </summary>
public class SvgContent : BaseContent {
public class SvgContent : TextLikeContent {
public SvgContent(string xml) : base(xml) { }

public static SvgContent FromClipboard() {
var format = "image/svg+xml";
if (Clipboard.ContainsData(format) && Clipboard.GetData(format) is MemoryStream stream)
return new SvgContent(stream);
return null;
}
public SvgContent(Stream data) {
Data = data;
}

public Stream Stream => Data as Stream;
public string XmlString {
public string Xml {
get {
Stream.Seek(0, SeekOrigin.Begin);
return new StreamReader(Stream).ReadToEnd();
var xml = Text;
if (!xml.StartsWith("<?xml"))
xml = "<?xml version=\"1.0\" encoding=\"" + Encoding.BodyName + "\"?>\n" + xml;
return xml;
}
}

public override string[] Extensions => new[] { "svg" };
public override string Description => Resources.str_preview_svg;

public override void SaveAs(string path, string extension, bool append = false) {
if (append)
throw new AppendNotSupportedException();
switch (extension) {
case "svg":
using (FileStream w = File.Create(path)) {
Stream.Seek(0, SeekOrigin.Begin);
Stream.CopyTo(w);
}
default:
Save(path, Xml);
break;
}
}

public override void AddTo(IDataObject data) {
data.SetData("image/svg+xml", Stream);
}
public override string TextPreview(string extension) {
return Xml;
}
}


Expand All @@ -273,12 +267,13 @@ public TextLikeContent(string text) {
Data = text;
}
public string Text => Data as string;
public Stream Stream => new MemoryStream(Encoding.GetBytes(Text));
public static readonly Encoding Encoding = new UTF8Encoding(false); // omit unnecessary BOM bytes
public override void SaveAs(string path, string extension, bool append = false) {
Save(path, Text, append);
}

public static void Save(string path, string text, bool append = false) {
protected static void Save(string path, string text, bool append = false) {
using (StreamWriter streamWriter = new StreamWriter(path, append))
streamWriter.Write(EnsureNewline(text), Encoding);
}
Expand Down Expand Up @@ -503,7 +498,7 @@ public override void SaveAs(string path, string extension, bool append = false)
break;

default:
TextLikeContent.Save(path, FileListString, append);
new TextContent(FileListString).SaveAs(path, extension, append);
break;
}
}
Expand Down Expand Up @@ -678,8 +673,8 @@ public static ClipboardContents FromClipboard() {
&& ReadClipboardString(DataFormats.Dif) is string dif)
container.Contents.Add(new DifContent(dif));

if (SvgContent.FromClipboard() is BaseContent content)
container.Contents.Add(content);
if (ReadClipboardString("image/svg+xml") is string svg)
container.Contents.Add(new SvgContent(svg));

if (Clipboard.ContainsText() && Uri.IsWellFormedUriString(Clipboard.GetText().Trim(), UriKind.Absolute))
container.Contents.Add(new UrlContent(Clipboard.GetText().Trim()));
Expand Down Expand Up @@ -756,21 +751,27 @@ public static ClipboardContents FromFile(string path) {
var contents = File.ReadAllText(path);
container.Contents.Add(new TextContent(contents));

// html files
string firstLine = File.ReadLines(path).First();
if (ext == "html" || ext == "htm" || firstLine.StartsWith("<!DOCTYPE html>")) {
container.Contents.Add(new HtmlContent(contents));
// check for doctype
var firstLines = "";
using (var reader = new StringReader(contents)) {
for (var i = 0; i < 2; i++)
firstLines += reader.ReadLine() + "\n";
}
var doctype = new Regex(@"<!DOCTYPE\s+(\S+).*>").Match(firstLines).Groups[1].Value.ToLower();

// csv files
if (ext == "csv") {
// text like contents
if (ext == "html" || ext == "htm" || doctype == "html")
container.Contents.Add(new HtmlContent(contents));
if (ext == "svg" || doctype == "svg")
container.Contents.Add(new SvgContent(contents));
if (ext == "csv")
container.Contents.Add(new CsvContent(contents));
}

// rtf files
if (ext == "rtf") {
if (ext == "dif")
container.Contents.Add(new DifContent(contents));
if (ext == "rtf")
container.Contents.Add(new RtfContent(contents));
}
if (ext == "syk")
container.Contents.Add(new SylkContent(contents));

}

Expand Down
10 changes: 4 additions & 6 deletions PasteIntoFile/Dialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Threading.Tasks;
using System.Windows.Forms;
using PasteIntoFile.Properties;
using Svg;
using WK.Libraries.BetterFolderBrowserNS;
using WK.Libraries.SharpClipboardNS;

Expand Down Expand Up @@ -314,6 +313,10 @@ private void updateContentPreview() {
htmlPreview.DocumentText = htmlContent.Text;
htmlPreview.Show();

} else if (content is SvgContent svgContent) {
htmlPreview.DocumentText = svgContent.Xml;
htmlPreview.Show();

} else if (content is TextLikeContent textLikeContent) {
if (content is RtfContent)
textPreview.Rtf = textLikeContent.TextPreview(ext);
Expand All @@ -335,11 +338,6 @@ private void updateContentPreview() {
treePreview.Show();
}

} else if (content is SvgContent svgContent) {
// Render SVG for preview
imagePreview.Image = SvgDocument.FromSvg<SvgDocument>(svgContent.XmlString).Draw();
imagePreview.Show();

}


Expand Down
1 change: 0 additions & 1 deletion PasteIntoFile/PasteIntoFile.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.2" />
<PackageReference Include="PDFsharp" Version="1.50.5147" />
<PackageReference Include="SharpClipboard" Version="3.5.2" />
<PackageReference Include="Svg" Version="3.4.3" />
</ItemGroup>
<ItemGroup Condition="'$(Flavor)'=='Portable'">
<PackageReference Include="PortableSettingsProvider" Version="0.2.4" />
Expand Down
2 changes: 1 addition & 1 deletion PasteIntoFile/RegistryUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public ContextMenuEntryReplaceAppend() : base("*", "PasteIntoFile_replace_append
private static string APPEND_NOT_SUPPORTED = "System.FileExtension:=." + string.Join(" OR System.FileExtension:=.",
new ImageContent(null).Extensions.Concat(
new VectorImageContent(null).Extensions.Concat(
new SvgContent(null).Extensions.Concat(
new SvgContent("").Extensions.Concat(
new UrlContent(null).Extensions))));


Expand Down

0 comments on commit 0ffd1c3

Please sign in to comment.