Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ImportedProjects
SVG
Commits
b044459f
Commit
b044459f
authored
Dec 24, 2011
by
Tebjan Halm
Browse files
* correct text alignment on base line
* added precise MeasureString method
parent
9d37b566
Changes
3
Show whitespace changes
Inline
Side-by-side
Source/SvgRenderer.cs
View file @
b044459f
...
...
@@ -132,5 +132,20 @@ namespace Svg
{
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
Source/Text/SvgText.cs
View file @
b044459f
...
...
@@ -191,6 +191,7 @@ namespace Svg
{
GraphicsPath
p
=
new
GraphicsPath
();
p
.
AddString
(
text
,
font
.
FontFamily
,
0
,
font
.
Size
,
new
PointF
(
0.0f
,
0.0f
),
StringFormat
.
GenericTypographic
);
p
.
Transform
(
renderer
.
Transform
);
return
p
.
GetBounds
();
}
...
...
@@ -211,23 +212,22 @@ namespace Svg
{
fontSize
=
1.0f
;
}
RectangleF
stringBounds
;
PointF
location
=
PointF
.
Empty
;
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.
switch
(
this
.
TextAnchor
)
{
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
;
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
)
-
this
.
_fontSize
);
location
=
new
PointF
(
this
.
X
.
ToDeviceValue
(
this
)
-
(
stringBounds
.
Width
/
2
),
this
.
Y
.
ToDeviceValue
(
this
,
true
)
-
stringBounds
.
Height
);
break
;
case
SvgTextAnchor
.
End
:
stringBounds
=
SvgText
.
MeasureString
(
_stringMeasure
,
this
.
Text
,
font
);
location
=
new
PointF
(
this
.
X
.
ToDeviceValue
(
this
)
-
stringBounds
.
Width
,
this
.
Y
.
ToDeviceValue
(
this
,
true
)
-
this
.
_fontSize
);
location
=
new
PointF
(
this
.
X
.
ToDeviceValue
(
this
)
-
stringBounds
.
Width
,
this
.
Y
.
ToDeviceValue
(
this
,
true
)
-
stringBounds
.
Height
);
break
;
}
...
...
Source/Text/SvgTextAnchor.cs
View file @
b044459f
...
...
@@ -2,12 +2,14 @@
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Text
;
using
System.ComponentModel
;
namespace
Svg
{
/// <summary>
/// Text anchor is used to align (start-, middle- or end-alignment) a string of text relative to a given point.
/// </summary>
[
TypeConverter
(
typeof
(
SvgTextAnchorConverter
))]
public
enum
SvgTextAnchor
{
/// <summary>
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment