Commit b044459f authored by Tebjan Halm's avatar Tebjan Halm
Browse files

* correct text alignment on base line

* added precise MeasureString method
parent 9d37b566
...@@ -132,5 +132,20 @@ namespace Svg ...@@ -132,5 +132,20 @@ namespace Svg
{ {
this._innerGraphics.Dispose(); this._innerGraphics.Dispose();
} }
public SizeF MeasureString(string text, Font font)
{
var ff = font.FontFamily;
float lineSpace = ff.GetLineSpacing(font.Style);
float ascent = ff.GetCellAscent(font.Style);
float baseline = font.GetHeight(this._innerGraphics) * ascent / lineSpace;
StringFormat format = StringFormat.GenericTypographic;
format.SetMeasurableCharacterRanges(new CharacterRange[]{new CharacterRange(0, text.Length)});
Region[] r = this._innerGraphics.MeasureCharacterRanges(text, font, new Rectangle(0, 0, 1000, 1000), format);
RectangleF rect = r[0].GetBounds(this._innerGraphics);
return new SizeF(rect.Width, baseline);
}
} }
} }
\ No newline at end of file
...@@ -191,6 +191,7 @@ namespace Svg ...@@ -191,6 +191,7 @@ namespace Svg
{ {
GraphicsPath p = new GraphicsPath(); GraphicsPath p = new GraphicsPath();
p.AddString(text, font.FontFamily, 0, font.Size, new PointF(0.0f, 0.0f), StringFormat.GenericTypographic); p.AddString(text, font.FontFamily, 0, font.Size, new PointF(0.0f, 0.0f), StringFormat.GenericTypographic);
p.Transform(renderer.Transform); p.Transform(renderer.Transform);
return p.GetBounds(); return p.GetBounds();
} }
...@@ -211,23 +212,22 @@ namespace Svg ...@@ -211,23 +212,22 @@ namespace Svg
{ {
fontSize = 1.0f; fontSize = 1.0f;
} }
RectangleF stringBounds;
PointF location = PointF.Empty; PointF location = PointF.Empty;
Font font = new Font(this._fontFamily, fontSize, FontStyle.Regular, GraphicsUnit.Pixel); Font font = new Font(this._fontFamily, fontSize, FontStyle.Regular, GraphicsUnit.Pixel);
SizeF stringBounds = _stringMeasure.MeasureString(this.Text, font);
// Minus FontSize because the x/y coords mark the bottom left, not bottom top. // Minus FontSize because the x/y coords mark the bottom left, not bottom top.
switch (this.TextAnchor) switch (this.TextAnchor)
{ {
case SvgTextAnchor.Start: case SvgTextAnchor.Start:
location = new PointF(this.X.ToDeviceValue(this), this.Y.ToDeviceValue(this, true) - this._fontSize); location = new PointF(this.X.ToDeviceValue(this), this.Y.ToDeviceValue(this, true) - stringBounds.Height);
break; break;
case SvgTextAnchor.Middle: case SvgTextAnchor.Middle:
stringBounds = SvgText.MeasureString(_stringMeasure, this.Text, font); location = new PointF(this.X.ToDeviceValue(this) - (stringBounds.Width / 2), this.Y.ToDeviceValue(this, true) - stringBounds.Height);
location = new PointF(this.X.ToDeviceValue(this) - (stringBounds.Width / 2), this.Y.ToDeviceValue(this, true) - this._fontSize);
break; break;
case SvgTextAnchor.End: case SvgTextAnchor.End:
stringBounds = SvgText.MeasureString(_stringMeasure, this.Text, font); location = new PointF(this.X.ToDeviceValue(this) - stringBounds.Width, this.Y.ToDeviceValue(this, true) - stringBounds.Height);
location = new PointF(this.X.ToDeviceValue(this) - stringBounds.Width, this.Y.ToDeviceValue(this, true) - this._fontSize);
break; break;
} }
......
...@@ -2,12 +2,14 @@ ...@@ -2,12 +2,14 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.ComponentModel;
namespace Svg namespace Svg
{ {
/// <summary> /// <summary>
/// Text anchor is used to align (start-, middle- or end-alignment) a string of text relative to a given point. /// Text anchor is used to align (start-, middle- or end-alignment) a string of text relative to a given point.
/// </summary> /// </summary>
[TypeConverter(typeof(SvgTextAnchorConverter))]
public enum SvgTextAnchor public enum SvgTextAnchor
{ {
/// <summary> /// <summary>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment