forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharpDXVectorStyle.cs
More file actions
179 lines (155 loc) · 6.7 KB
/
SharpDXVectorStyle.cs
File metadata and controls
179 lines (155 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Drawing;
using SharpDX;
using SharpDX.Direct2D1;
using SharpDX.WIC;
using SharpMap.Styles;
using Bitmap = SharpDX.Direct2D1.Bitmap;
using Brush = SharpDX.Direct2D1.Brush;
using GdiPixelFormat = System.Drawing.Imaging.PixelFormat;
using GdiSolidBrush = System.Drawing.SolidBrush;
using GdiTextureBrush = System.Drawing.TextureBrush;
namespace SharpMap.Layers.Styles
{
internal class SharpDXVectorStyle : Style
{
private SharpDXVectorStyle() { }
public static SharpDXVectorStyle FromVectorStyle(RenderTarget rt, Factory f, VectorStyle vs)
{
var res = new SharpDXVectorStyle
{
// Global
Enabled = vs.Enabled,
MinVisible = vs.MinVisible,
MaxVisible = vs.MaxVisible,
};
// Point
if (vs.PointColor != null)
{
res.PointColor = Converter.ToSharpDXBrush(rt, vs.PointColor);
res.PointSize = vs.PointSize;
}
if (vs.Symbol != null)
{
res.Symbol = Converter.ToSharpDXBitmap(rt, vs.Symbol as System.Drawing.Bitmap, vs.SymbolScale);
res.SymbolOffset = Converter.ToSharpDXPoint(vs.SymbolOffset);
//res.SymbolScale = vs.SymbolScale;
res.SymbolRotation = vs.SymbolRotation;
}
// Line
if (vs.Line != null)
{
res.Line = Converter.ToSharpDXBrush(rt, vs.Line.Brush);
res.LineWidth = vs.Line.Width;
res.LineStrokeStyle = Converter.ToSharpDXStrokeStyle(f, vs.Line);
res.LineOffset = vs.LineOffset;
}
if (vs.Outline != null)
{
res.EnableOutline = vs.EnableOutline;
res.Outline = Converter.ToSharpDXBrush(rt, vs.Outline.Brush);
res.OutlineWidth = vs.Outline.Width;
res.OutlineStrokeStyle = Converter.ToSharpDXStrokeStyle(f, vs.Line);
}
// Fill
if (vs.Fill != null)
{
res.Fill = Converter.ToSharpDXBrush(rt, vs.Fill);
}
return res;
}
public Brush PointColor { get; private set; }
public float PointSize { get; private set; }
public Bitmap Symbol { get; private set; }
public Vector2 SymbolOffset { get; private set; }
public float SymbolScale { get; private set; }
public float SymbolRotation { get; private set; }
public Brush Line { get; private set; }
public float LineOffset { get; private set; }
public float LineWidth { get; private set; }
public StrokeStyle LineStrokeStyle { get; private set; }
public bool EnableOutline { get; private set; }
public Brush Outline { get; private set; }
public float OutlineWidth { get; private set; }
public StrokeStyle OutlineStrokeStyle { get; private set; }
public Brush Fill { get; private set; }
}
internal static class Converter
{
internal static Brush ToSharpDXBrush(RenderTarget rt, System.Drawing.Brush brush)
{
if (brush is System.Drawing.SolidBrush)
{
var res = new SolidColorBrush(rt,
ToSharpDXColor(((System.Drawing.SolidBrush) brush).Color));
//res.Opacity = ToPortion(((SolidBrush) brush).Color.A);
return res;
}
if (brush is System.Drawing.TextureBrush)
{
var tb = (TextureBrush) brush;
var sharpDXBitmap = Converter.ToSharpDXBitmap(rt, tb.Image as System.Drawing.Bitmap, 1f);
return new SharpDX.Direct2D1.BitmapBrush(rt, sharpDXBitmap);
}
throw new NotSupportedException();
}
internal static Bitmap ToSharpDXBitmap(RenderTarget rt, System.Drawing.Bitmap image, float symbolScale)
{
if (image == null)
throw new ArgumentNullException("image");
if (image.PixelFormat != GdiPixelFormat.Format32bppPArgb)
return null;
var imageData = image.LockBits(new System.Drawing.Rectangle(0, 0, image.Width, image.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly, image.PixelFormat);
var dataStream = new DataStream(imageData.Scan0, imageData.Stride*imageData.Height, true, false);
var properties = new BitmapProperties
{
PixelFormat = new SharpDX.Direct2D1.PixelFormat
{
Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
AlphaMode = AlphaMode.Premultiplied
}
};
// ToDo apply scaling here!
//var scaler = new BitmapScaler(rt.Factory.NativePointer);
//scaler.
//Load the image from the gdi resource
var result = new Bitmap(rt, new Size2(image.Width, image.Height), dataStream, imageData.Stride, properties);
image.UnlockBits(imageData);
return result;
}
private static float ToPortion(int value, int max = 255)
{
return (float) value/max;
}
internal static Color4 ToSharpDXColor4(System.Drawing.Color color)
{
return new Color4(ToPortion(color.R), ToPortion(color.G), ToPortion(color.B), ToPortion(color.A));
}
internal static SharpDX.Color ToSharpDXColor(System.Drawing.Color color)
{
return new SharpDX.Color(color.R, color.G, color.B, (byte)255);
}
public static Vector2 ToSharpDXPoint(PointF symbolOffset)
{
var tmp = System.Drawing.Point.Round(symbolOffset);
return new Vector2(tmp.X, tmp.Y);
}
public static StrokeStyle ToSharpDXStrokeStyle(Factory factory, System.Drawing.Pen pen)
{
var sp = new StrokeStyleProperties
{
DashCap = (CapStyle) pen.DashCap,
DashOffset = pen.DashOffset,
DashStyle = (DashStyle) pen.DashStyle,
StartCap = (CapStyle)pen.StartCap,
EndCap = (CapStyle)pen.EndCap,
LineJoin = (LineJoin)pen.LineJoin,
MiterLimit = pen.MiterLimit,
};
if (pen.DashStyle == System.Drawing.Drawing2D.DashStyle.Custom)
return new StrokeStyle(factory, sp, pen.DashPattern);
return new StrokeStyle(factory, sp);
}
}
}