summaryrefslogtreecommitdiffstatsci
diff options
context:
space:
mode:
authorsuyambulingamrm <suyambu.rm@samsung.com>2014-08-02 17:03:05 +0530
committersuyambulingamrm <suyambu.rm@samsung.com>2014-08-02 17:03:05 +0530
commit7cb71d5ed95246a80723550d04fbce102a0f440b (patch)
treedd0631cda5dfc95807af2c1198607ec9e79cdb89
parent8f9cde1ab8bc38f7341da62e4654d04c1fb15534 (diff)
Issue #130. Add Cairo tests for curves, linear_gradient and radial_gradient.
-rw-r--r--src/cairo-tests/curves.cpp48
-rw-r--r--src/cairo-tests/linear-gradient.cpp54
-rw-r--r--src/cairo-tests/radial-gradient.cpp50
3 files changed, 112 insertions, 40 deletions
diff --git a/src/cairo-tests/curves.cpp b/src/cairo-tests/curves.cpp
index 270e510..e40d3b2 100644
--- a/src/cairo-tests/curves.cpp
+++ b/src/cairo-tests/curves.cpp
@@ -6,17 +6,22 @@
*/
#include <config.h>
-#include <stdio.h>
#include <stdlib.h>
#include <cairo.h>
+#include <math.h>
#include "caskbench.h"
#include "caskbench_context.h"
#include "cairo-shapes.h"
+static double delta = 0;
+
int
ca_setup_curves(caskbench_context_t *ctx)
{
+ cairo_set_antialias (ctx->cairo_cr, CAIRO_ANTIALIAS_NONE);
+ cairo_set_line_width (ctx->cairo_cr, 1);
+
return 1;
}
@@ -28,6 +33,47 @@ ca_teardown_curves(void)
int
ca_test_curves(caskbench_context_t *ctx)
{
+ cairo_t *cr = ctx->cairo_cr;
+ int cp = ctx->canvas_width / 2; // center point
+ int rr = ctx->canvas_width / 2;
+ int nn = 32; // must be even
+ double step = (2 * M_PI) / nn;
+
+ for (int i=0; i<ctx->size; i++) {
+ double angle = delta; // in radians
+ double x = cp + rr * cos(angle);
+ double y = cp + rr * sin(angle);
+
+ // begin path
+ cairo_new_path(cr);
+ cairo_move_to (cr, x, y);
+
+ // segments
+ for (int j=0; j<nn; j++) {
+ double r = rr * drand48() / 8.0;
+
+ double angle1 = j * step + delta;
+ double x1 = cp + r * cos(angle1);
+ double y1 = cp + r * sin(angle1);
+
+ double x2 = cp + rr * cos(angle1);
+ double y2 = cp + rr * sin(angle1);
+
+ cairo_curve_to (ctx->cairo_cr,
+ x1, y1,
+ x2, y2, x2,y2);
+ }
+ cairo_close_path(cr);
+ cairoRandomizeColor(ctx);
+
+ // TODO: apply other fill types
+ if (ctx->shape_defaults.fill_type == CB_FILL_SOLID) {
+ cairo_fill (cr);
+ }
+ cairo_stroke (cr);
+
+ }
+ delta += 0.5;
return 1;
}
diff --git a/src/cairo-tests/linear-gradient.cpp b/src/cairo-tests/linear-gradient.cpp
index 9096cd5..6997c81 100644
--- a/src/cairo-tests/linear-gradient.cpp
+++ b/src/cairo-tests/linear-gradient.cpp
@@ -6,19 +6,17 @@
*/
#include <config.h>
-#include <stdio.h>
#include <stdlib.h>
#include <cairo.h>
#include "caskbench.h"
#include "caskbench_context.h"
#include "cairo-shapes.h"
+#include <math.h>
int
ca_setup_linear_gradient(caskbench_context_t *ctx)
{
- cairo_set_antialias (ctx->cairo_cr, CAIRO_ANTIALIAS_NONE);
- cairo_set_line_width (ctx->cairo_cr, 1);
return 1;
}
@@ -33,7 +31,19 @@ ca_test_linear_gradient(caskbench_context_t *ctx)
int w = ctx->canvas_width;
int h = ctx->canvas_height;
- // TODO: Implement linear gradient
+ int stops = 2;
+ double x1,y1 ,x2,y2;
+ x1 = y1 = 0;
+ x2 = y2 = 100;
+
+ cairo_t *cr = ctx->cairo_cr;
+ cairo_pattern_t *pattern = cairo_pattern_create_linear (0,0,100,100);
+
+ for (int i = 1; i <= stops; i++) {
+ double red, green, blue, alpha;
+ generate_random_color(red,green,blue,alpha);
+ cairo_pattern_add_color_stop_rgba (pattern, i/(stops), red, green, blue, alpha);
+ }
shapes_t shape;
shape_copy(&ctx->shape_defaults, &shape);
@@ -42,26 +52,30 @@ ca_test_linear_gradient(caskbench_context_t *ctx)
double x2 = (double)rand()/RAND_MAX * w;
double y1 = (double)rand()/RAND_MAX * h;
double y2 = (double)rand()/RAND_MAX * h;
+ double x = MIN(x1, x2);
+ double y = MIN(y1, y2);
+
+ double width_ratio = float(fabs(x2 - x1)) / float(100);
+ double height_ratio = float(fabs(y2 - y1)) / float(100);
- shape.x = MIN(x1, x2);
- shape.y = MIN(x1, x2);
- shape.width = abs(x2 - x1);
- shape.height = abs(y2 - y1);
+ cairo_save(cr);
+ cairo_translate(cr, x, y);
- cairoRandomizeColor(ctx);
+ cairo_scale(cr, width_ratio, height_ratio);
+ // transform(shape.width/100, 0, 0, shape.height/100, 0, 0)
+ shape.x = 0;
+ shape.y = 0;
+ shape.width = 100;
+ shape.height = 100;
+ shape.fill_type = CB_FILL_LINEAR_GRADIENT;
+
+ cairo_set_source (cr, pattern);
cairoDrawRectangle(ctx, &shape);
- switch (ctx->shape_defaults.fill_type) {
- case CB_FILL_NONE:
- cairo_stroke(ctx->cairo_cr);
- break;
- case CB_FILL_SOLID:
- cairo_fill(ctx->cairo_cr);
- break;
- default:
- break;
- }
- }
+ cairo_fill (cr);
+ cairo_restore(cr);
+ }
+ cairo_pattern_destroy(pattern);
return 1;
}
diff --git a/src/cairo-tests/radial-gradient.cpp b/src/cairo-tests/radial-gradient.cpp
index fbdc4c6..3fef5b6 100644
--- a/src/cairo-tests/radial-gradient.cpp
+++ b/src/cairo-tests/radial-gradient.cpp
@@ -6,19 +6,17 @@
*/
#include <config.h>
-#include <stdio.h>
#include <stdlib.h>
#include <cairo.h>
#include "caskbench.h"
#include "caskbench_context.h"
#include "cairo-shapes.h"
+#include <math.h>
int
ca_setup_radial_gradient(caskbench_context_t *ctx)
{
- cairo_set_antialias (ctx->cairo_cr, CAIRO_ANTIALIAS_NONE);
- cairo_set_line_width (ctx->cairo_cr, 1);
return 1;
}
@@ -33,7 +31,16 @@ ca_test_radial_gradient(caskbench_context_t *ctx)
int w = ctx->canvas_width;
int h = ctx->canvas_height;
- // TODO: Implement radial gradient
+ int stops = 2;
+ cairo_pattern_t *pattern = cairo_pattern_create_radial (20,20,20,50,50,70);
+
+ for (int i = 0; i < stops; i++) {
+ double red, green, blue, alpha;
+ generate_random_color(red,green,blue,alpha);
+ cairo_pattern_add_color_stop_rgba (pattern, i/(stops-1), red, green, blue, alpha);
+ }
+
+ cairo_t *cr = ctx->cairo_cr;
shapes_t shape;
shape_copy(&ctx->shape_defaults, &shape);
@@ -42,25 +49,30 @@ ca_test_radial_gradient(caskbench_context_t *ctx)
double x2 = (double)rand()/RAND_MAX * w;
double y1 = (double)rand()/RAND_MAX * h;
double y2 = (double)rand()/RAND_MAX * h;
+ double x = MIN(x1, x2);
+ double y = MIN(y1, y2);
+
+ double width_ratio = float(fabs(x2 - x1)) / float(100);
+ double height_ratio = float(fabs(y2 - y1)) / float(100);
- shape.x = MIN(x1, x2);
- shape.y = MIN(x1, x2);
- shape.width = abs(x2 - x1);
- shape.height = abs(y2 - y1);
+ cairo_save(cr);
+ cairo_translate(cr,x, y);
- cairoRandomizeColor(ctx);
+ cairo_scale(cr, width_ratio, height_ratio);
+
+ shape.x = 0;
+ shape.y = 0;
+ shape.width = 100;
+ shape.height = 100;
+ shape.fill_type = CB_FILL_LINEAR_GRADIENT;
+
+ cairo_set_source (cr, pattern);
cairoDrawRectangle(ctx, &shape);
- switch (ctx->shape_defaults.fill_type) {
- case CB_FILL_NONE:
- cairo_stroke(ctx->cairo_cr);
- break;
- case CB_FILL_SOLID:
- cairo_fill(ctx->cairo_cr);
- break;
- default:
- break;
- }
+
+ cairo_fill (cr);
+ cairo_restore(cr);
}
+ cairo_pattern_destroy(pattern);
return 1;
}