summaryrefslogtreecommitdiff
path: root/test/hairline.c
diff options
context:
space:
mode:
authorRick Yorgason <rick@firefang.com>2021-08-15 06:58:54 +0000
committerAdrian Johnson <ajohnson@redneon.com>2021-08-15 06:58:54 +0000
commitecec0419f8e178d71e449b52acfdfe9ac03aed37 (patch)
treee6ede3cdc9a0ae02a6f56dfec38ff8b05cdcf9bb /test/hairline.c
parent4dd48f0979f693dfc515eb3eff266ec6429f8b33 (diff)
downloadcairo-ecec0419f8e178d71e449b52acfdfe9ac03aed37.tar.gz
Added hairline support to cairo
Diffstat (limited to 'test/hairline.c')
-rw-r--r--test/hairline.c175
1 files changed, 175 insertions, 0 deletions
diff --git a/test/hairline.c b/test/hairline.c
new file mode 100644
index 000000000..a14b944e3
--- /dev/null
+++ b/test/hairline.c
@@ -0,0 +1,175 @@
+#include "cairo-test.h"
+
+/**
+ * draw:
+ * @cr: a #cairo_t
+ * @width: Width of the test image
+ * @height: Height of the test image
+ * @scale_width: Percentage to scale the width
+ * @scale_height: Percentage to scale the height
+ * @fit_to_scale: Whether or not to adjust the image to fit in the scale parameters
+ * regardless of the scale parameters
+ * @correct_scale: Tests if the hairlines render correctly regardless of
+ * whether or not the scale is set "correctly", as per
+ * https://cairographics.org/cookbook/ellipses/
+ */
+static cairo_test_status_t
+draw (cairo_t *cr, int width, int height, double scale_width, double scale_height, cairo_bool_t fit_to_scale, cairo_bool_t correct_scale)
+{
+ cairo_matrix_t save_matrix;
+ double fit_width = fit_to_scale ? scale_width : 1.0;
+ double fit_height = fit_to_scale ? scale_height : 1.0;
+ double fit_max = MAX(fit_width, fit_height);
+ double dash[] = {3.0};
+
+ if (cairo_get_hairline (cr) == TRUE) {
+ return CAIRO_TEST_ERROR;
+ }
+
+ /* Clear background */
+ cairo_set_source_rgb (cr, 1, 1, 1);
+ cairo_paint (cr);
+
+ cairo_set_source_rgb (cr, 0, 0, 0);
+ cairo_set_line_width (cr, 100.0); /* If everything is working right, this value should never get used */
+
+ /* Hairline sample */
+ if (correct_scale) {
+ cairo_get_matrix (cr, &save_matrix);
+ }
+ cairo_scale (cr, scale_width, scale_height);
+
+ cairo_set_hairline (cr, TRUE);
+ if (cairo_get_hairline (cr) == FALSE) {
+ return CAIRO_TEST_ERROR;
+ }
+
+ cairo_move_to (cr, 0, 0);
+ cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
+ cairo_move_to (cr, width/fit_width/2, 0);
+ cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
+ cairo_move_to (cr, 0, height/fit_height/2);
+ cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
+ cairo_move_to (cr, width/fit_width/4, 0);
+ cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
+ cairo_arc (cr, width/fit_width/2, height/fit_height/2, width/fit_max/4, M_PI*0.5, M_PI*1.0);
+
+ if (correct_scale) {
+ cairo_set_matrix (cr, &save_matrix);
+ }
+ cairo_stroke (cr);
+
+ /* Dashed sample */
+ if (correct_scale) {
+ cairo_get_matrix (cr, &save_matrix);
+ cairo_scale (cr, scale_width, scale_height);
+ }
+ cairo_set_dash (cr, dash, 1, 0);
+ cairo_arc (cr, width/fit_width/2, height/fit_height/2, width/fit_max/4, M_PI*1.0, M_PI*1.5);
+ if (correct_scale) {
+ cairo_set_matrix (cr, &save_matrix);
+ }
+ cairo_stroke (cr);
+
+ /* Control sample */
+ if (correct_scale) {
+ cairo_get_matrix (cr, &save_matrix);
+ cairo_scale (cr, scale_width, scale_height);
+ }
+
+ cairo_set_line_width (cr, 3.0);
+ cairo_set_hairline (cr, FALSE);
+ if (cairo_get_hairline (cr) == TRUE) {
+ return CAIRO_TEST_ERROR;
+ }
+
+ cairo_set_dash (cr, 0, 0, 0);
+
+ cairo_move_to (cr, width/fit_width, height/fit_height);
+ cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
+ cairo_move_to (cr, width/fit_width/2, height/fit_height);
+ cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
+ cairo_move_to (cr, width/fit_width, height/fit_height/2);
+ cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
+ cairo_move_to (cr, width/fit_width*0.75, height/fit_height);
+ cairo_line_to (cr, width/fit_width/2, height/fit_height/2);
+ cairo_arc (cr, width/fit_width/2, height/fit_height/2, width/fit_max/4, M_PI*1.5, M_PI*2.0);
+
+ if (correct_scale) {
+ cairo_set_matrix (cr, &save_matrix);
+ }
+ cairo_stroke (cr);
+
+ /* Dashed sample */
+ if (correct_scale) {
+ cairo_get_matrix (cr, &save_matrix);
+ cairo_scale (cr, scale_width, scale_height);
+ }
+ cairo_set_dash (cr, dash, 1, 0);
+ cairo_arc (cr, width/fit_width/2, height/fit_height/2, width/fit_max/4, 0, M_PI*0.5);
+ if (correct_scale) {
+ cairo_set_matrix (cr, &save_matrix);
+ }
+ cairo_stroke (cr);
+
+ return CAIRO_TEST_SUCCESS;
+}
+
+static cairo_test_status_t
+draw_typical (cairo_t *cr, int width, int height)
+{
+ return draw (cr, width, height, 1.0, 1.0, TRUE, TRUE);
+}
+
+static cairo_test_status_t
+draw_scaled (cairo_t *cr, int width, int height)
+{
+ return draw (cr, width, height, 0.5, 0.5, FALSE, TRUE);
+}
+
+static cairo_test_status_t
+draw_anisotropic (cairo_t *cr, int width, int height)
+{
+ return draw (cr, width, height, 2.0, 5.0, TRUE, TRUE);
+}
+
+static cairo_test_status_t
+draw_anisotropic_incorrect (cairo_t *cr, int width, int height)
+{
+ return draw (cr, width, height, 2.0, 5.0, TRUE, FALSE);
+}
+
+CAIRO_TEST (hairline,
+ "Tests hairlines are drawn at a single pixel width",
+ "path, stroke, hairline", /* keywords */
+ NULL, /* requirements */
+ 49, 49,
+ NULL, draw_typical)
+
+CAIRO_TEST (hairline_big,
+ "Tests hairlines are drawn at a single pixel width",
+ "path, stroke, hairline", /* keywords */
+ NULL, /* requirements */
+ 99, 99,
+ NULL, draw_typical)
+
+CAIRO_TEST (hairline_scaled,
+ "Tests hairlines are drawn at a single pixel width",
+ "path, stroke, hairline", /* keywords */
+ NULL, /* requirements */
+ 99, 99,
+ NULL, draw_scaled)
+
+CAIRO_TEST (hairline_anisotropic,
+ "Tests hairlines with a really lopsided scale parameter",
+ "path, stroke, hairline", /* keywords */
+ NULL, /* requirements */
+ 99, 99,
+ NULL, draw_anisotropic)
+
+CAIRO_TEST (hairline_anisotropic_incorrect,
+ "Tests hairlines with a really lopsided scale parameter",
+ "path, stroke, hairline", /* keywords */
+ NULL, /* requirements */
+ 99, 99,
+ NULL, draw_anisotropic_incorrect) \ No newline at end of file