Skip to content

Commit 96285d6

Browse files
committed
Add Border support
Signed-off-by: Aravinda VK <vkaravinda7@gmail.com>
1 parent c4cbfc1 commit 96285d6

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

source/chitra/elements/rect.d

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import chitra.elements.core;
99

1010
struct Rect
1111
{
12-
double x_, y_;
13-
double w_, h_;
14-
double r_, rtl_, rtr_, rbl_, rbr_;
12+
float x_, y_;
13+
float w_, h_;
14+
float r_, rtl_, rtr_, rbl_, rbr_;
1515
ShapeProperties shapeProps;
1616

17-
this(double x, double y, double w, double h, double r, double rtl, double rtr, double rbr, double rbl)
17+
this(float x, float y, float w, float h, float r, float rtl, float rtr, float rbr, float rbl)
1818
{
1919
this.x_ = x;
2020
this.y_ = y;
@@ -49,10 +49,10 @@ struct Rect
4949
if (rbl < 0) rbl = r;
5050
if (rbr < 0) rbr = r;
5151

52-
double fromX = x + rtl;
53-
double toX = x + w - rtr;
54-
double fromY = y;
55-
double toY = y;
52+
float fromX = x + rtl;
53+
float toX = x + w - rtr;
54+
float fromY = y;
55+
float toY = y;
5656
cairo_move_to(cairoCtx, fromX, fromY);
5757
cairo_line_to(cairoCtx, toX, toY);
5858
cairo_arc(cairoCtx, toX, toY+rtr, rtr, PI*3/2, 0);
@@ -82,6 +82,8 @@ struct Rect
8282

8383
mixin template rectFunctions()
8484
{
85+
import std.typecons;
86+
8587
/**
8688
Draw a Square or Rectangle.
8789
@@ -95,7 +97,7 @@ mixin template rectFunctions()
9597
ctx.rect(50, 50, 100, 50);
9698
---
9799
*/
98-
void rect(double x, double y, double w, double h = 0.0, double r = 0, double rtl = -1.0, double rtr = -1.0, double rbr = -1.0, double rbl = -1.0)
100+
void rect(float x, float y, float w, float h = 0.0, float r = 0, float rtl = -1.0, float rtr = -1.0, float rbr = -1.0, float rbl = -1.0)
99101
{
100102
h = h == 0.0 ? w : h;
101103
auto rct = Rect(x, y, w, h, r, rtl, rtr, rbr, rbl);
@@ -113,7 +115,7 @@ mixin template rectFunctions()
113115
ctx.square(50, 50, 100);
114116
---
115117
*/
116-
void square(double x, double y, double w, double r = 0, double rtl = -1.0, double rtr = -1.0, double rbr = -1.0, double rbl = -1.0)
118+
void square(float x, float y, float w, float r = 0, float rtl = -1.0, float rtr = -1.0, float rbr = -1.0, float rbl = -1.0)
117119
{
118120
rect(x, y, w, w, r, rtl, rtr, rbr, rbl);
119121
}
@@ -128,11 +130,37 @@ mixin template rectFunctions()
128130
ctx.pixel(50, 50, 2);
129131
---
130132
*/
131-
void pixel(double x, double y, int w=1)
133+
void pixel(float x, float y, int w=1)
132134
{
133135
auto prevStrokeWidth = this.shapeProps.strokeWidth;
134136
strokeWidth(0);
135137
rect(x, y, w.px);
136138
strokeWidth(prevStrokeWidth);
137139
}
140+
141+
void border(int thickness = 2, Nullable!RGBA color = color(0),
142+
float m = 0.0, float mt = -1.0, float mr = -1.0, float mb = -1.0, float ml = -1.0,
143+
float r = 0.0, float rtl = -1.0, float rtr = -1.0, float rbr = -1.0, float rbl = -1.0)
144+
{
145+
auto prevFillColor = this.shapeProps.fill;
146+
auto prevStrokeColor = this.shapeProps.stroke;
147+
auto prevStrokeWidth = this.shapeProps.strokeWidth;
148+
// Transparent fill and strokeWidth to given thickness
149+
fillOpacity(0);
150+
this.shapeProps.stroke = color.get;
151+
strokeWidth(thickness);
152+
153+
if (mt < 0) mt = m;
154+
if (mr < 0) mr = m;
155+
if (mb < 0) mb = m;
156+
if (ml < 0) ml = m;
157+
158+
auto halfThickness = thickness / 2.0;
159+
rect(ml + halfThickness, mt + halfThickness, this.width - ml - mr - thickness, this.height - mt - mb - thickness,
160+
r: r, rtl: rtl, rtr: rtr, rbl: rbl, rbr: rbr);
161+
162+
this.shapeProps.fill = prevFillColor;
163+
this.shapeProps.stroke = prevStrokeColor;
164+
this.shapeProps.strokeWidth = prevStrokeWidth;
165+
}
138166
}

0 commit comments

Comments
 (0)