|
@@ -1,4 +1,5 @@
|
|
|
-using netDxf;
|
|
|
+using InABox.Core;
|
|
|
+using netDxf;
|
|
|
using netDxf.Tables;
|
|
|
using Svg;
|
|
|
using Svg.Transforms;
|
|
@@ -28,6 +29,7 @@ public enum TextAlignment
|
|
|
Left,
|
|
|
Center,
|
|
|
Right,
|
|
|
+ Fit,
|
|
|
Justify
|
|
|
}
|
|
|
|
|
@@ -260,6 +262,8 @@ public interface IGraphics
|
|
|
|
|
|
void DrawLine(Color color, float thickness, params PointF[] points);
|
|
|
|
|
|
+ void DrawPoint(Color color, float thickness, PointF point);
|
|
|
+
|
|
|
void FillPolygon(Color color, params PointF[] points);
|
|
|
|
|
|
/// <summary>
|
|
@@ -271,6 +275,8 @@ public interface IGraphics
|
|
|
void SetFont(string fontName, float fontSize, FontStyle fontStyle = FontStyle.Regular);
|
|
|
|
|
|
void DrawText(string text, Color color, PointF position, float rotation, TextFormat format);
|
|
|
+ void DrawText(string text, Color color, PointF position, float rotation, float width, PointF scale);
|
|
|
+ void DrawText(string text, Color color, PointF position, float rotation, SizeF size, PointF scale);
|
|
|
|
|
|
void Clear(Color color);
|
|
|
|
|
@@ -307,13 +313,26 @@ public class GdiGraphics : IGraphics
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public void DrawPoint(Color color, float thickness, PointF point)
|
|
|
+ {
|
|
|
+ Graphics.FillEllipse(new SolidBrush(color), point.X - thickness / 2, point.Y - thickness / 2, point.X + thickness / 2, point.Y + thickness / 2);
|
|
|
+ }
|
|
|
+
|
|
|
private void TransformText(string text, Font font, PointF position, float rotation, TextFormat format)
|
|
|
{
|
|
|
DrawData.Translate(position);
|
|
|
DrawData.Rotate(rotation);
|
|
|
DrawData.Scale(1, -1);
|
|
|
|
|
|
+ var ascentRatio = (float)font.FontFamily.GetCellAscent(font.Style) / font.FontFamily.GetLineSpacing(font.Style);
|
|
|
+
|
|
|
+ DrawLine(Color.Black, 1, new(), new(100, 0));
|
|
|
+
|
|
|
var size = Graphics.MeasureString(text, font, new PointF(), StringFormat.GenericTypographic);
|
|
|
+
|
|
|
+ var scaleFactor = ascentRatio * font.Height / size.Height;
|
|
|
+ DrawData.Scale(scaleFactor, scaleFactor);
|
|
|
+
|
|
|
switch (format.LineAlignment)
|
|
|
{
|
|
|
case TextLineAlignment.Center:
|
|
@@ -323,16 +342,14 @@ public class GdiGraphics : IGraphics
|
|
|
DrawData.Translate(new PointF(0, -size.Height));
|
|
|
break;
|
|
|
case TextLineAlignment.Baseline:
|
|
|
- var ascent = font.FontFamily.GetCellAscent(font.Style);
|
|
|
- var baseline = ascent * font.Height / font.FontFamily.GetEmHeight(font.Style);
|
|
|
+ var baseline = ascentRatio * font.Height;
|
|
|
DrawData.Translate(new PointF(0, -baseline));
|
|
|
break;
|
|
|
case TextLineAlignment.Top:
|
|
|
- ascent = font.FontFamily.GetCellAscent(font.Style);
|
|
|
- baseline = ascent * font.Height / font.FontFamily.GetEmHeight(font.Style);
|
|
|
+ baseline = ascentRatio * font.Height;
|
|
|
var lineSpace = font.FontFamily.GetLineSpacing(font.Style);
|
|
|
var ratio = font.GetHeight(Graphics) / lineSpace;
|
|
|
- DrawData.Translate(new PointF(0, -baseline + ascent * ratio));
|
|
|
+ DrawData.Translate(new PointF(0, -baseline + font.FontFamily.GetCellAscent(font.Style) * ratio));
|
|
|
break;
|
|
|
}
|
|
|
|
|
@@ -351,6 +368,10 @@ public class GdiGraphics : IGraphics
|
|
|
|
|
|
public void DrawText(string text, Color color, PointF position, float rotation, TextFormat format)
|
|
|
{
|
|
|
+ if (text.IsNullOrWhiteSpace())
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
DrawData.PushTransform();
|
|
|
TransformText(text, Font, position, rotation, format);
|
|
|
|
|
@@ -358,6 +379,29 @@ public class GdiGraphics : IGraphics
|
|
|
|
|
|
DrawData.PopTransform();
|
|
|
}
|
|
|
+ public void DrawText(string text, Color color, PointF position, float rotation, float width, PointF scale)
|
|
|
+ {
|
|
|
+ var size = Graphics.MeasureString(text, Font, new PointF(), StringFormat.GenericTypographic);
|
|
|
+ DrawText(text, color, position, rotation, new SizeF(width, size.Height * width / size.Width), scale);
|
|
|
+ }
|
|
|
+ public void DrawText(string text, Color color, PointF position, float rotation, SizeF size, PointF scale)
|
|
|
+ {
|
|
|
+ DrawData.PushTransform();
|
|
|
+ DrawData.Translate(position);
|
|
|
+ DrawData.Rotate(rotation);
|
|
|
+ DrawData.Scale(1, -1);
|
|
|
+
|
|
|
+ var (width, height) = (size.Width, size.Height);
|
|
|
+
|
|
|
+ DrawData.Translate(width / 2, height / 2);
|
|
|
+ DrawData.Scale(scale.X, scale.Y);
|
|
|
+ DrawData.Translate(-width / 2, -height / 2);
|
|
|
+
|
|
|
+ DrawData.Scale(width / size.Width, height / size.Height);
|
|
|
+ Graphics.DrawString(text, Font, new SolidBrush(color), new PointF(0, 0), StringFormat.GenericTypographic);
|
|
|
+
|
|
|
+ DrawData.PopTransform();
|
|
|
+ }
|
|
|
|
|
|
public void FillPolygon(Color color, params PointF[] points)
|
|
|
{
|
|
@@ -402,6 +446,7 @@ public class PdfGraphics : IGraphics
|
|
|
private Matrix _transform = new();
|
|
|
|
|
|
private PdfFont _font = new PdfStandardFont(PdfFontFamily.Helvetica, 12, PdfFontStyle.Regular);
|
|
|
+ private float _ascentRatio = 0;
|
|
|
|
|
|
public PdfGraphics(SPdfGraphics graphics)
|
|
|
{
|
|
@@ -428,13 +473,24 @@ public class PdfGraphics : IGraphics
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public void DrawPoint(Color color, float thickness, PointF point)
|
|
|
+ {
|
|
|
+ var transformed = _transform.Transform(point);
|
|
|
+ var brush = new PdfSolidBrush(color);
|
|
|
+ Graphics.DrawEllipse(brush, transformed.X - thickness / 2, transformed.Y - thickness / 2, transformed.X + thickness / 2, transformed.Y + thickness / 2);
|
|
|
+ }
|
|
|
+
|
|
|
public void SetFont(float fontSize, FontStyle fontStyle = FontStyle.Regular)
|
|
|
{
|
|
|
- _font = new PdfTrueTypeFont(new Font(SystemFonts.DefaultFont.FontFamily, fontSize));
|
|
|
+ var font = new Font(SystemFonts.DefaultFont.FontFamily, fontSize);
|
|
|
+ _ascentRatio = (float)font.FontFamily.GetCellAscent(font.Style) / (float)font.FontFamily.GetLineSpacing(font.Style);
|
|
|
+ _font = new PdfTrueTypeFont(font);
|
|
|
}
|
|
|
|
|
|
public void SetFont(string fontName, float fontSize, FontStyle fontStyle = FontStyle.Regular)
|
|
|
{
|
|
|
+ var font = new Font(fontName, fontSize);
|
|
|
+ _ascentRatio = (float)font.FontFamily.GetCellAscent(font.Style) / (float)font.FontFamily.GetLineSpacing(font.Style);
|
|
|
_font = new PdfTrueTypeFont(new Font(fontName, fontSize));
|
|
|
}
|
|
|
|
|
@@ -452,6 +508,7 @@ public class PdfGraphics : IGraphics
|
|
|
{
|
|
|
TextLineAlignment.Center => PdfVerticalAlignment.Middle,
|
|
|
TextLineAlignment.Bottom => PdfVerticalAlignment.Bottom,
|
|
|
+ TextLineAlignment.Baseline => PdfVerticalAlignment.Bottom,
|
|
|
_ => PdfVerticalAlignment.Top
|
|
|
};
|
|
|
if(format.LineAlignment == TextLineAlignment.Baseline)
|
|
@@ -464,12 +521,64 @@ public class PdfGraphics : IGraphics
|
|
|
var point = _transform.Transform(position);
|
|
|
Graphics.TranslateTransform(point.X, point.Y);
|
|
|
Graphics.RotateTransform(-(_transform.GetRotation() + rotation));
|
|
|
- var scale = (_font.Height / _font.Size) * _transform.GetScale();
|
|
|
- Graphics.ScaleTransform(scale, scale);
|
|
|
+
|
|
|
+ var scaled = _transform.TransformVector(new(1, -1));
|
|
|
+
|
|
|
+ Graphics.ScaleTransform(scaled.X / _ascentRatio, scaled.Y / _ascentRatio);
|
|
|
Graphics.DrawString(text, _font, new PdfSolidBrush(color), new PointF(), fmt);
|
|
|
|
|
|
Graphics.Restore();
|
|
|
}
|
|
|
+ public void DrawText(string text, Color color, PointF position, float rotation, float width, PointF scale)
|
|
|
+ {
|
|
|
+ Graphics.Save();
|
|
|
+
|
|
|
+ var point = _transform.Transform(position);
|
|
|
+ Graphics.TranslateTransform(point.X, point.Y);
|
|
|
+ Graphics.RotateTransform(-(_transform.GetRotation()) + rotation);
|
|
|
+ var scaleFactor = (_font.Height / _font.Size) * _transform.GetScale();
|
|
|
+ Graphics.ScaleTransform(scaleFactor, scaleFactor * scale.Y);
|
|
|
+ if(scale.X != 1)
|
|
|
+ {
|
|
|
+ Graphics.TranslateTransform(width / 2, 0);
|
|
|
+ Graphics.ScaleTransform(scale.X, 1);
|
|
|
+ Graphics.TranslateTransform(-width / 2, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ Graphics.DrawString(text, _font, new PdfSolidBrush(color), new RectangleF(0, 0, width, float.MaxValue), new PdfStringFormat
|
|
|
+ {
|
|
|
+ Alignment = PdfTextAlignment.Justify,
|
|
|
+ LineAlignment = PdfVerticalAlignment.Top,
|
|
|
+ EnableBaseline = true
|
|
|
+ });
|
|
|
+
|
|
|
+ Graphics.Restore();
|
|
|
+ }
|
|
|
+ public void DrawText(string text, Color color, PointF position, float rotation, SizeF size, PointF scale)
|
|
|
+ {
|
|
|
+ Graphics.Save();
|
|
|
+
|
|
|
+ var point = _transform.Transform(position);
|
|
|
+ Graphics.TranslateTransform(point.X, point.Y);
|
|
|
+ Graphics.RotateTransform(-(_transform.GetRotation()) + rotation);
|
|
|
+ var scaleFactor = (_font.Height / _font.Size) * _transform.GetScale();
|
|
|
+ Graphics.ScaleTransform(scaleFactor, scaleFactor * scale.Y);
|
|
|
+ if(scale.X != 1 || scale.Y != 1)
|
|
|
+ {
|
|
|
+ Graphics.TranslateTransform(size.Width / 2, size.Height / 2);
|
|
|
+ Graphics.ScaleTransform(scale.X, 1);
|
|
|
+ Graphics.TranslateTransform(-size.Width / 2, size.Height / 2);
|
|
|
+ }
|
|
|
+
|
|
|
+ Graphics.DrawString(text, _font, new PdfSolidBrush(color), new RectangleF(0, 0, size.Width, size.Height), new PdfStringFormat
|
|
|
+ {
|
|
|
+ Alignment = PdfTextAlignment.Justify,
|
|
|
+ LineAlignment = PdfVerticalAlignment.Top,
|
|
|
+ EnableBaseline = true
|
|
|
+ });
|
|
|
+
|
|
|
+ Graphics.Restore();
|
|
|
+ }
|
|
|
|
|
|
public void FillPolygon(Color color, params PointF[] points)
|
|
|
{
|
|
@@ -586,6 +695,10 @@ public class SvgGraphics : IGraphics
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public void DrawPoint(Color color, float thickness, PointF point)
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
public void FillPolygon(Color color, params PointF[] points)
|
|
|
{
|
|
|
}
|
|
@@ -610,4 +723,12 @@ public class SvgGraphics : IGraphics
|
|
|
{
|
|
|
PushGroup();
|
|
|
}
|
|
|
+
|
|
|
+ public void DrawText(string text, Color color, PointF position, float rotation, float width, PointF scale)
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ public void DrawText(string text, Color color, PointF position, float rotation, SizeF size, PointF scale)
|
|
|
+ {
|
|
|
+ }
|
|
|
}
|