Skip to content

Commit d1c490f

Browse files
authored
Merge pull request #56 from drewnoakes/line-caps
Initial support for line segment start/end caps
2 parents ea6d581 + d1737ee commit d1c490f

File tree

8 files changed

+108
-13
lines changed

8 files changed

+108
-13
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ root = true
55

66
# Don't use tabs for indentation.
77
[*.cs]
8-
indent_style = space
8+
indent_style = tabs
99
indent_size = 2
1010
insert_final_newline = true
1111
charset = utf-8-bom

src/D2DLibExport/D2DEnums.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,32 @@ public enum D2DDashStyle
299299
Custom = 5,
300300
}
301301

302+
/// <summary>
303+
/// From <c>D2D1_CAP_STYLE</c>.
304+
/// </summary>
305+
public enum D2DCapStyle
306+
{
307+
/// <summary>
308+
/// Flat line cap.
309+
/// </summary>
310+
Flat = 0,
311+
312+
/// <summary>
313+
/// Square line cap.
314+
/// </summary>
315+
Square = 1,
316+
317+
/// <summary>
318+
/// Round line cap.
319+
/// </summary>
320+
Round = 2,
321+
322+
/// <summary>
323+
/// Triangle line cap.
324+
/// </summary>
325+
Triangle = 3
326+
}
327+
302328
public enum D2D1_ARC_SIZE
303329
{
304330
D2D1_ARC_SIZE_SMALL = 0,

src/D2DLibExport/D2DGraphics.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ namespace unvell.D2DLib
3939
{
4040
public class D2DGraphics
4141
{
42-
internal HANDLE Handle { get; private set; }
42+
internal HANDLE Handle { get; }
4343

44-
public D2DDevice Device { get; private set; }
44+
public D2DDevice Device { get; }
4545

4646
public D2DGraphics(D2DDevice context)
4747
: this(context.Handle)
@@ -97,15 +97,17 @@ public bool Antialias
9797
}
9898

9999
public void DrawLine(FLOAT x1, FLOAT y1, FLOAT x2, FLOAT y2, D2DColor color,
100-
FLOAT weight = 1, D2DDashStyle dashStyle = D2DDashStyle.Solid)
100+
FLOAT weight = 1, D2DDashStyle dashStyle = D2DDashStyle.Solid,
101+
D2DCapStyle startCap = D2DCapStyle.Flat, D2DCapStyle endCap = D2DCapStyle.Flat)
101102
{
102-
DrawLine(new D2DPoint(x1, y1), new D2DPoint(x2, y2), color, weight, dashStyle);
103+
DrawLine(new D2DPoint(x1, y1), new D2DPoint(x2, y2), color, weight, dashStyle, startCap, endCap);
103104
}
104105

105106
public void DrawLine(D2DPoint start, D2DPoint end, D2DColor color,
106-
FLOAT weight = 1, D2DDashStyle dashStyle = D2DDashStyle.Solid)
107+
FLOAT weight = 1, D2DDashStyle dashStyle = D2DDashStyle.Solid,
108+
D2DCapStyle startCap = D2DCapStyle.Flat, D2DCapStyle endCap = D2DCapStyle.Flat)
107109
{
108-
D2D.DrawLine(this.Handle, start, end, color, weight, dashStyle);
110+
D2D.DrawLine(this.Handle, start, end, color, weight, dashStyle, startCap, endCap);
109111
}
110112

111113
public void DrawLines(D2DPoint[] points, D2DColor color, FLOAT weight = 1, D2DDashStyle dashStyle = D2DDashStyle.Solid)

src/D2DLibExport/D2DLib.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ public static extern HANDLE PushLayer(HANDLE ctx, HANDLE layerHandle, ref D2DRec
163163

164164
[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
165165
public static extern void DrawLine(HANDLE context, D2DPoint start, D2DPoint end, D2DColor color,
166-
FLOAT weight = 1, D2DDashStyle dashStyle = D2DDashStyle.Solid);
166+
FLOAT weight = 1, D2DDashStyle dashStyle = D2DDashStyle.Solid,
167+
D2DCapStyle startCap = D2DCapStyle.Flat, D2DCapStyle endCap = D2DCapStyle.Flat);
167168

168169
[DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
169170
public static extern void DrawLines(HANDLE context, D2DPoint[] points, UINT count, D2DColor color,

src/Examples/Examples.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
<Compile Update="SampleCode\ClipMaskDraw.cs">
1818
<SubType>Form</SubType>
1919
</Compile>
20+
<Compile Update="SampleCode\LineCapStyle.cs">
21+
<SubType>Form</SubType>
22+
</Compile>
2023
</ItemGroup>
2124

2225
</Project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2017-2021 Jingwood, unvell.com. All right reserved.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
using System;
26+
using System.Collections.Generic;
27+
using System.Drawing;
28+
using System.Windows.Forms;
29+
using unvell.D2DLib.WinForm;
30+
31+
namespace unvell.D2DLib.Examples.SampleCode
32+
{
33+
public partial class LineCapStyle : ExampleForm
34+
{
35+
public LineCapStyle()
36+
{
37+
Text = "LineCapStyle Tests - d2dlib Examples";
38+
}
39+
40+
protected override void OnRender(D2DGraphics g)
41+
{
42+
g.DrawText("Flat", D2DColor.DimGray, "Arials", 20, 110, 83);
43+
g.DrawLine(300, 100, 600, 100, D2DColor.Black, 20, D2DDashStyle.Solid, D2DCapStyle.Flat, D2DCapStyle.Flat);
44+
45+
g.DrawText("Round", D2DColor.DimGray, "Arials", 20, 110, 183);
46+
g.DrawLine(300, 200, 600, 200, D2DColor.Black, 20, D2DDashStyle.Solid, D2DCapStyle.Round, D2DCapStyle.Round);
47+
48+
g.DrawText("Square", D2DColor.DimGray, "Arials", 20, 110, 283);
49+
g.DrawLine(300, 300, 600, 300, D2DColor.Black, 20, D2DDashStyle.Solid, D2DCapStyle.Square, D2DCapStyle.Square);
50+
51+
g.DrawText("Triangle", D2DColor.DimGray, "Arials", 20, 110, 383);
52+
g.DrawLine(300, 400, 600, 400, D2DColor.Black, 20, D2DDashStyle.Solid, D2DCapStyle.Triangle, D2DCapStyle.Triangle);
53+
54+
g.DrawText("Round, Triangle", D2DColor.DimGray, "Arials", 20, 110, 483);
55+
g.DrawLine(300, 500, 600, 500, D2DColor.Black, 20, D2DDashStyle.Solid, D2DCapStyle.Round, D2DCapStyle.Triangle);
56+
}
57+
}
58+
59+
}

src/d2dlib/Simple.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include "Brush.h"
2929

3030
void DrawLine(HANDLE ctx, D2D1_POINT_2F start, D2D1_POINT_2F end, D2D1_COLOR_F color,
31-
FLOAT width, D2D1_DASH_STYLE dashStyle)
31+
FLOAT width, D2D1_DASH_STYLE dashStyle, D2D1_CAP_STYLE startCap, D2D1_CAP_STYLE endCap)
3232
{
3333
RetrieveContext(ctx);
3434

@@ -39,11 +39,13 @@ void DrawLine(HANDLE ctx, D2D1_POINT_2F start, D2D1_POINT_2F end, D2D1_COLOR_F c
3939

4040
if (brush != NULL) {
4141

42-
if (dashStyle != D2D1_DASH_STYLE_SOLID)
42+
if (dashStyle != D2D1_DASH_STYLE_SOLID ||
43+
startCap != D2D1_CAP_STYLE_FLAT ||
44+
endCap != D2D1_CAP_STYLE_FLAT)
4345
{
4446
context->factory->CreateStrokeStyle(D2D1::StrokeStyleProperties(
45-
D2D1_CAP_STYLE_FLAT,
46-
D2D1_CAP_STYLE_FLAT,
47+
startCap,
48+
endCap,
4749
D2D1_CAP_STYLE_ROUND,
4850
D2D1_LINE_JOIN_MITER,
4951
10.0f,

src/d2dlib/Simple.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
extern "C"
3030
{
3131
D2DLIB_API void DrawLine(HANDLE ctx, D2D1_POINT_2F start, D2D1_POINT_2F end, D2D1_COLOR_F color,
32-
FLOAT width = 1, D2D1_DASH_STYLE dashStyle = D2D1_DASH_STYLE::D2D1_DASH_STYLE_SOLID);
32+
FLOAT width = 1, D2D1_DASH_STYLE dashStyle = D2D1_DASH_STYLE::D2D1_DASH_STYLE_SOLID,
33+
D2D1_CAP_STYLE startCap = D2D1_CAP_STYLE::D2D1_CAP_STYLE_FLAT,
34+
D2D1_CAP_STYLE endCap = D2D1_CAP_STYLE::D2D1_CAP_STYLE_FLAT);
3335

3436
D2DLIB_API void DrawLineWithPen(HANDLE ctx, D2D1_POINT_2F start, D2D1_POINT_2F end, HANDLE pen, FLOAT width = 1);
3537

0 commit comments

Comments
 (0)