Skip to content

Commit f5c22e6

Browse files
boronhubKarthikRIyer
authored andcommitted
Added single renderer function to draw Rectangles (#95)
1 parent 44eb97c commit f5c22e6

File tree

7 files changed

+60
-19
lines changed

7 files changed

+60
-19
lines changed

Sources/AGGRenderer/AGGRenderer/AGGRenderer.swift

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class AGGRenderer: Renderer{
1919
self.imageSize = Size(width: w, height: h)
2020
self.agg_object = initializePlot(imageSize.width, imageSize.height, fontPath)
2121
}
22-
22+
2323
func getPoints(from rect: Rect) -> (tL: Point, tR: Point, bL: Point, bR: Point) {
2424
let rect = rect.normalized
2525
return (
@@ -95,22 +95,19 @@ public class AGGRenderer: Renderer{
9595
y.append(pts.bR.y + yOffset)
9696
y.append(pts.bL.y + yOffset)
9797

98-
draw_solid_rect(x,
98+
draw_solid_rect_with_border(
99+
x,
99100
y,
101+
thickness,
100102
fillColor.r,
101103
fillColor.g,
102104
fillColor.b,
103105
fillColor.a,
104-
0,
106+
borderColor.r,
107+
borderColor.g,
108+
borderColor.b,
109+
borderColor.a,
105110
agg_object)
106-
draw_rect(x,
107-
y,
108-
thickness,
109-
borderColor.r,
110-
borderColor.g,
111-
borderColor.b,
112-
borderColor.a,
113-
agg_object)
114111
}
115112

116113
public func drawSolidCircle(center c: Point,
@@ -233,7 +230,7 @@ public class AGGRenderer: Renderer{
233230
get_text_size(text, size, &width, &height, agg_object)
234231
return Size(width: width, height: height)
235232
}
236-
233+
237234
struct DrawOutputError: Error, CustomStringConvertible {
238235
let errorCode: UInt32
239236
let description: String
@@ -251,7 +248,7 @@ public class AGGRenderer: Renderer{
251248
var _bufferPtr: UnsafeMutablePointer<UInt8>?
252249
var errorDescPtr: UnsafePointer<Int8>?
253250
var bufferSize = 0
254-
251+
255252
let err = create_png_buffer(&_bufferPtr, &bufferSize, &errorDescPtr, agg_object)
256253
guard let bufferPtr = _bufferPtr, err == 0 else {
257254
// We'll probably never make this throwing, but log the error all the same.
@@ -261,13 +258,13 @@ public class AGGRenderer: Renderer{
261258
)
262259
return ""
263260
}
264-
261+
265262
let base64String = Data(
266263
bytesNoCopy: UnsafeMutableRawPointer(mutating: bufferPtr),
267264
count: bufferSize,
268265
deallocator: .none
269266
).base64EncodedString()
270-
267+
271268
free_png_buffer(&_bufferPtr)
272269
return base64String
273270
}

Sources/AGGRenderer/CAGGRenderer/CAGGRenderer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ void draw_solid_rect(const float *x, const float *y, float r, float g, float b,
1818
CPPAGGRenderer::draw_solid_rect(x, y, r, g, b, a, hatch_pattern, object);
1919
}
2020

21+
void draw_solid_rect_with_border(const float *x, const float *y, float thickness, float r_fill, float g_fill, float b_fill, float a_fill, float r_stroke, float g_stroke, float b_stroke, float a_stroke, const void *object){
22+
CPPAGGRenderer::draw_solid_rect_with_border(x, y, thickness, r_fill, g_fill, b_fill, a_fill, r_stroke, g_stroke, b_stroke, a_stroke, object);
23+
}
24+
2125
void draw_solid_circle(float cx, float cy, float radius, float r, float g, float b, float a, const void *object){
2226
CPPAGGRenderer::draw_solid_circle(cx, cy, radius, r, g, b, a, object);
2327
}

Sources/AGGRenderer/CAGGRenderer/include/CAGGRenderer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ void draw_rect(const float *x, const float *y, float thickness, float r, float g
1212

1313
void draw_solid_rect(const float *x, const float *y, float r, float g, float b, float a, int hatch_pattern, const void *object);
1414

15+
void draw_solid_rect_with_border(const float *x, const float *y, float thickness, float r_fill, float g_fill, float b_fill, float a_fill, float r_stroke, float g_stroke, float b_stroke, float a_stroke, const void *object);
16+
1517
void draw_solid_circle(float cx, float cy, float radius, float r, float g, float b, float a, const void *object);
1618

1719
void draw_solid_triangle(float x1, float x2, float x3, float y1, float y2, float y3, float r, float g, float b, float a, const void *object);

Sources/AGGRenderer/CPPAGGRenderer/CPPAGGRenderer.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,36 @@ namespace CPPAGGRenderer{
279279
agg::render_scanlines(m_ras, m_sl_p8, ren_aa);
280280
}
281281

282+
void draw_solid_rect_with_border(const float *x, const float *y, float thickness, float r_fill, float g_fill, float b_fill, float a_fill, float r_stroke, float g_stroke, float b_stroke, float a_stroke){
283+
agg::rendering_buffer rbuf = agg::rendering_buffer(buffer, frame_width, frame_height, -frame_width*3);
284+
pixfmt pixf = pixfmt(rbuf);
285+
renderer_base rb = renderer_base(pixf);
286+
ren_aa = renderer_aa(rb);
287+
pixfmt_pre pixf_pre(rbuf);
288+
renderer_base_pre rb_pre(pixf_pre);
289+
agg::path_storage rect_path;
290+
rect_path.move_to(*x, *y);
291+
for (int i = 1; i < 4; i++) {
292+
rect_path.line_to(*(x+i),*(y+i));
293+
}
294+
rect_path.close_polygon();
295+
agg::trans_affine matrix;
296+
matrix *= agg::trans_affine_translation(0, 0);
297+
agg::conv_transform<agg::path_storage, agg::trans_affine> trans(rect_path, matrix);
298+
Color c1(r_fill, g_fill, b_fill, a_fill);
299+
m_ras.add_path(trans);
300+
ren_aa.color(c1);
301+
agg::render_scanlines(m_ras, m_sl_p8, ren_aa);
302+
agg::conv_curve<agg::conv_transform<agg::path_storage, agg::trans_affine>> curve(trans);
303+
agg::conv_stroke<agg::conv_curve<agg::conv_transform<agg::path_storage, agg::trans_affine>>> stroke(curve);
304+
stroke.width(thickness);
305+
m_ras.add_path(stroke);
306+
Color c2(r_stroke, g_stroke, b_stroke, a_stroke);
307+
ren_aa.color(c2);
308+
agg::render_scanlines(m_ras, m_sl_p8, ren_aa);
309+
310+
}
311+
282312
void draw_solid_circle(float cx, float cy, float radius, float r, float g, float b, float a) {
283313
agg::rendering_buffer rbuf = agg::rendering_buffer(buffer, frame_width, frame_height, -frame_width*3);
284314
pixfmt pixf = pixfmt(rbuf);
@@ -499,6 +529,11 @@ namespace CPPAGGRenderer{
499529
plot -> draw_solid_rect(x, y, r, g, b, a, hatch_pattern);
500530
}
501531

532+
void draw_solid_rect_with_border(const float *x, const float *y, float thickness, float r_fill, float g_fill, float b_fill, float a_fill, float r_stroke, float g_stroke, float b_stroke, float a_stroke, const void *object){
533+
Plot *plot = (Plot *)object;
534+
plot -> draw_solid_rect_with_border(x, y, thickness, r_fill, g_fill, b_fill, a_fill, r_stroke, g_stroke, b_stroke, a_stroke);
535+
}
536+
502537
void draw_solid_circle(float cx, float cy, float radius, float r, float g, float b, float a, const void *object){
503538
Plot *plot = (Plot *)object;
504539
plot -> draw_solid_circle(cx, cy, radius, r, g, b, a);

Sources/AGGRenderer/CPPAGGRenderer/include/CPPAGGRenderer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace CPPAGGRenderer{
1010

1111
void draw_solid_rect(const float *x, const float *y, float r, float g, float b, float a, int hatch_pattern, const void *object);
1212

13+
void draw_solid_rect_with_border(const float *x, const float *y, float thickness, float r_fill, float g_fill, float b_fill, float a_fill, float r_stroke, float g_stroke, float b_stroke, float a_stroke, const void *object);
14+
1315
void draw_solid_circle(float cx, float cy, float radius, float r, float g, float b, float a, const void *object);
1416

1517
void draw_solid_triangle(float x1, float x2, float x3, float y1, float y2, float y3, float r, float g, float b, float a, const void *object);

Sources/SVGRenderer/SVGRenderer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class SVGRenderer: Renderer{
6969
lines.append(rectStr)
7070
drawHatchingRect(rect, hatchPattern: hatchPattern)
7171
}
72-
72+
7373
func drawHatchingRect(_ rect: Rect,
7474
hatchPattern: BarGraphSeriesOptions.Hatching) {
7575
let patternName: String

Sources/SwiftPlot/Renderer.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ public protocol Renderer: AnyObject{
111111
* This is decided by the boolean parameter isOriginShifted.
112112
*/
113113
func drawSolidRectWithBorder(_ rect: Rect,
114-
strokeWidth thickness: Float, fillColor: Color,
114+
strokeWidth thickness: Float,
115+
fillColor: Color,
115116
borderColor: Color)
116117

117118
/*drawSolidCircle()
@@ -172,7 +173,7 @@ public protocol Renderer: AnyObject{
172173
}
173174

174175
extension Renderer {
175-
176+
176177
public var xOffset: Float {
177178
get { return offset.x }
178179
set { offset.x = newValue}
@@ -184,7 +185,7 @@ extension Renderer {
184185
func getTextWidth(text: String, textSize size: Float) -> Float {
185186
return getTextLayoutSize(text: text, textSize: size).width
186187
}
187-
188+
188189
public func withAdditionalOffset(_ offset: Point, _ perform: (Self) throws -> Void) rethrows {
189190
let oldOffset = (self.xOffset, self.yOffset)
190191
self.xOffset += offset.x

0 commit comments

Comments
 (0)