Skip to content

Commit

Permalink
v2.2.2 - Obsoleting some imprecise constructors, providing better ove…
Browse files Browse the repository at this point in the history
…rrides
  • Loading branch information
monoman committed Dec 19, 2021
1 parent b83c49b commit e676baa
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 196 deletions.
42 changes: 25 additions & 17 deletions SvgDocTest/DocForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion SvgDocTest/DocForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private void Button2_Click(object sender, EventArgs e) {
},

new SvgAElement("https://github.com/managed-commons/SvgNet").AddChildren(
new SvgTextElement("Textastic!", 30, 20) {
new SvgTextElement("Textastic!", "30px", "20px") {
Style = "fill:midnightblue;stroke:navy;stroke-width:1px;font-size:30px;font-family:Calibri"
})
);
Expand Down
43 changes: 43 additions & 0 deletions SvgNet/Elements/SvgBaseTextElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright © 2003 RiskCare Ltd. All rights reserved.
Copyright © 2010 SvgNet & SvgGdi Bridge Project. All rights reserved.
Copyright © 2015-2019 Rafael Teixeira, Mojmír Němeček, Benjamin Peterson and Other Contributors
Original source code licensed with BSD-2-Clause spirit, treat it thus, see accompanied LICENSE for more
*/

using SvgNet.SvgTypes;

namespace SvgNet.SvgElements {
public abstract class SvgBaseTextElement : SvgStyledTransformedElement {
public SvgLength DX {
get => (SvgLength)_atts["dx"];
set => _atts["dx"] = value;
}

public SvgLength DY {
get => (SvgLength)_atts["dy"];
set => _atts["dy"] = value;
}

public string LengthAdjust {
get => (string)_atts["lengthAdjust"];
set => _atts["lengthAdjust"] = value;
}

public SvgNumList Rotate {
get => (SvgNumList)_atts["rotate"];
set => _atts["rotate"] = value;
}

public SvgLength X {
get => (SvgLength)_atts["x"];
set => _atts["x"] = value;
}

public SvgLength Y {
get => (SvgLength)_atts["y"];
set => _atts["y"] = value;
}
}
}
84 changes: 4 additions & 80 deletions SvgNet/Elements/SvgTextElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,10 @@
*/

using System;

using SvgNet.SvgTypes;

namespace SvgNet.SvgElements {
public abstract class SvgBaseTextElement : SvgStyledTransformedElement {
public SvgLength DX {
get => (SvgLength)_atts["dx"];
set => _atts["dx"] = value;
}

public SvgLength DY {
get => (SvgLength)_atts["dy"];
set => _atts["dy"] = value;
}

public string LengthAdjust {
get => (string)_atts["lengthAdjust"];
set => _atts["lengthAdjust"] = value;
}

public SvgNumList Rotate {
get => (SvgNumList)_atts["rotate"];
set => _atts["rotate"] = value;
}

public SvgLength X {
get => (SvgLength)_atts["x"];
set => _atts["x"] = value;
}

public SvgLength Y {
get => (SvgLength)_atts["y"];
set => _atts["y"] = value;
}
}

/// <summary>
/// Represents a <c>text</c> element. The SVG text element is unusual in that it expects actual XML text nodes below
Expand All @@ -49,6 +19,7 @@ public SvgLength Y {
/// to XML slightly differently.
/// </summary>
public class SvgTextElement : SvgBaseTextElement, IElementWithText {

public SvgTextElement() {
}

Expand All @@ -57,7 +28,7 @@ public SvgTextElement(string s) {
AddChild(tn);
}

[Obsolete()]
[Obsolete("Must pass SvgLength, for x and y, which specifies a unit. Use the new constructor override")]
public SvgTextElement(string s, float x, float y) {
var tn = new TextNode(s);
AddChild(tn);
Expand All @@ -84,51 +55,4 @@ public SvgLength TextLength {
set => _atts["textLength"] = value;
}
}

/// <summary>
/// Represents a <c>tref</c> element.
/// </summary>
[Obsolete("Don't use it anymore")]
public class SvgTrefElement : SvgBaseTextElement, IElementWithXRef {
public SvgTrefElement() {
}

public SvgTrefElement(string href) => Href = href;

public SvgTrefElement(string href, float x, float y) {
Href = href;
X = x;
Y = y;
}

public string Href {
get => (string)_atts["xlink:href"];
set => _atts["xlink:href"] = value;
}

public override string Name => "tref";

public SvgXRef XRef {
get => new(this);
set => value.WriteToElement(this);
}
}

/// <summary>
/// Represents a <c>tspan</c> element. The tspan element is unique in that it expects actual XML text nodes below
/// it, rather than consisting only of attributes and child elements. <c>SvgTextElement</c> therefore has to be serialized
/// to XML slightly differently.
/// </summary>
public class SvgTspanElement : SvgTextElement {
public SvgTspanElement() {
}

public SvgTspanElement(string s) : base(s) {
}

public SvgTspanElement(string s, float x, float y) : base(s, x, y) {
}

public override string Name => "tspan";
}
}
}
43 changes: 43 additions & 0 deletions SvgNet/Elements/SvgTrefElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright © 2003 RiskCare Ltd. All rights reserved.
Copyright © 2010 SvgNet & SvgGdi Bridge Project. All rights reserved.
Copyright © 2015-2019 Rafael Teixeira, Mojmír Němeček, Benjamin Peterson and Other Contributors
Original source code licensed with BSD-2-Clause spirit, treat it thus, see accompanied LICENSE for more
*/

using System;

using SvgNet.SvgTypes;

namespace SvgNet.SvgElements {
/// <summary>
/// Represents a <c>tref</c> element.
/// </summary>
[Obsolete("Don't use it anymore")]
public class SvgTrefElement : SvgBaseTextElement, IElementWithXRef {

public SvgTrefElement() {
}

public SvgTrefElement(string href) => Href = href;

public SvgTrefElement(string href, float x, float y) {
Href = href;
X = x;
Y = y;
}

public string Href {
get => (string)_atts["xlink:href"];
set => _atts["xlink:href"] = value;
}

public override string Name => "tref";

public SvgXRef XRef {
get => new(this);
set => value.WriteToElement(this);
}
}
}
36 changes: 36 additions & 0 deletions SvgNet/Elements/SvgTspanElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright © 2003 RiskCare Ltd. All rights reserved.
Copyright © 2010 SvgNet & SvgGdi Bridge Project. All rights reserved.
Copyright © 2015-2019 Rafael Teixeira, Mojmír Němeček, Benjamin Peterson and Other Contributors
Original source code licensed with BSD-2-Clause spirit, treat it thus, see accompanied LICENSE for more
*/

using System;

using SvgNet.SvgTypes;

namespace SvgNet.SvgElements {
/// <summary>
/// Represents a <c>tspan</c> element. The tspan element is unique in that it expects actual XML text nodes below
/// it, rather than consisting only of attributes and child elements. <c>SvgTextElement</c> therefore has to be serialized
/// to XML slightly differently.
/// </summary>
public class SvgTspanElement : SvgTextElement {

public SvgTspanElement() {
}

public SvgTspanElement(string s) : base(s) {
}

[Obsolete("Use constructor override that receives SvgLength for x and y")]
public SvgTspanElement(string s, float x, float y) : base(s, x, y) {
}

public SvgTspanElement(string s, SvgLength x, SvgLength y) : base(s, x, y) {
}

public override string Name => "tspan";
}
}
3 changes: 2 additions & 1 deletion SvgNet/Graphics/SVGGraphics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Globalization;
using System.IO;
using System.Text;

using SvgNet.SvgElements;
using SvgNet.SvgTypes;

Expand Down Expand Up @@ -2021,7 +2022,7 @@ private void DrawText(string s, Font font, Brush brush, RectangleF rect, StringF
if (s?.Contains('\n') == true)
throw new SvgGdiNotImplementedException("DrawText multiline text");

var txt = new SvgTextElement(s, rect.X, rect.Y) {
var txt = new SvgTextElement(s, new SvgLength(rect.X, SvgLengthType.SVG_LENGTHTYPE_PX), new SvgLength(rect.Y, SvgLengthType.SVG_LENGTHTYPE_PX)) {
//GDI takes x and y as the upper left corner; svg takes them as the lower left.
//We must therefore move the text one line down, but SVG does not understand about lines,
//so we do as best we can, applying a downward translation before the current GDI translation.
Expand Down
Loading

0 comments on commit e676baa

Please sign in to comment.