From 8590b960e9bc75ce107f1a6d0db74756065da1bd Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Sun, 31 Jul 2016 23:53:14 -0300 Subject: [PATCH] sch2fig/: use bounding box to create PNG of correct size --- sch2fig/cairo.c | 79 +++++++++++++++++++++++++++++++++---------------- sch2fig/layer.c | 6 ++-- 2 files changed, 57 insertions(+), 28 deletions(-) diff --git a/sch2fig/cairo.c b/sch2fig/cairo.c index 42c3f47..5027537 100644 --- a/sch2fig/cairo.c +++ b/sch2fig/cairo.c @@ -38,6 +38,8 @@ struct cairo_ctx { struct layer layer; /* must be first */ + int xo, yo; + int scale; cairo_t *cr; cairo_surface_t *s; }; @@ -49,27 +51,33 @@ struct cairo_ctx { */ -static inline int cx(int x) +static inline int cd(struct cairo_ctx *cc, int x) { - return x / 5; + return x / cc->scale; } -static inline int xc(int x) +static inline int cx(struct cairo_ctx *cc, int x) { - return x * 5; + return cc->xo + x / cc->scale; } -static inline int cy(int y) +static inline int xc(struct cairo_ctx *cc, int x) { - return y / 5; + return (x - cc->xo) * cc->scale; } -static inline float pt(int x) +static inline int cy(struct cairo_ctx *cc, int y) { - return cx(x) * 72 * 1.5 / 1200.0; + return cc->yo + y / cc->scale; +} + + +static inline float pt(struct cairo_ctx *cc, int x) +{ + return cd(cc, x) * 72 * 1.5 / 1200.0; } @@ -116,10 +124,10 @@ static void cr_poly(void *ctx, int points, int x[points], int y[points], closed = x[0] == x[points - 1] && y[0] == y[points - 1]; cairo_new_path(cc->cr); - cairo_move_to(cc->cr, cx(x[0]), cy(y[0])); + cairo_move_to(cc->cr, cx(cc, x[0]), cy(cc, y[0])); for (i = 1; i != points - closed; i++) - cairo_line_to(cc->cr, cx(x[i]), cy(y[i])); + cairo_line_to(cc->cr, cx(cc, x[i]), cy(cc, y[i])); if (closed) cairo_close_path(cc->cr); @@ -133,7 +141,7 @@ static void cr_circ(void *ctx, int x, int y, int r, struct cairo_ctx *cc = ctx; cairo_new_path(cc->cr); - cairo_arc(cc->cr, cx(x), cy(y), cx(r), 0, 2 * M_PI); + cairo_arc(cc->cr, cx(cc, x), cy(cc, y), cd(cc, r), 0, 2 * M_PI); paint(cc->cr, color, fill_color); } @@ -144,7 +152,7 @@ static void cr_arc(void *ctx, int x, int y, int r, int sa, int ea, struct cairo_ctx *cc = ctx; cairo_new_path(cc->cr); - cairo_arc(cc->cr, cx(x), cy(y), cx(r), + cairo_arc(cc->cr, cx(cc, x), cy(cc, y), cd(cc, r), -ea / 180.0 * M_PI, -sa / 180.0 * M_PI); paint(cc->cr, color, fill_color); } @@ -160,12 +168,12 @@ static void cr_text(void *ctx, int x, int y, const char *s, unsigned size, cairo_text_extents_t ext; cairo_matrix_t m; - cairo_set_font_size(cc->cr, cx(size) * TEXT_STRETCH); + cairo_set_font_size(cc->cr, cd(cc, size) * TEXT_STRETCH); cairo_text_extents(cc->cr, s, &ext); set_color(cc->cr, color); - cairo_move_to(cc->cr, cx(x), cy(y)); + cairo_move_to(cc->cr, cx(cc, x), cy(cc, y)); cairo_get_matrix(cc->cr, &m); cairo_rotate(cc->cr, -rot / 180.0 * M_PI); @@ -193,9 +201,9 @@ static unsigned cr_text_width(void *ctx, const char *s, unsigned size) struct cairo_ctx *cc = ctx; cairo_text_extents_t ext; - cairo_set_font_size(cc->cr, cx(size) * TEXT_STRETCH); + cairo_set_font_size(cc->cr, cx(cc, size) * TEXT_STRETCH); cairo_text_extents(cc->cr, s, &ext); - return xc(ext.width); + return xc(cc, ext.width); } @@ -218,9 +226,37 @@ static void *cr_init(int argc, char *const *argv) struct cairo_ctx *cc; cc = alloc_type(struct cairo_ctx); + cc->xo = cc->yo = 0; + cc->scale = 5; layer_init(&cc->layer, &real_cairo_ops, cc); - cc->s = cairo_image_surface_create(CAIRO_FORMAT_RGB24, 2000, 1414); + /* cr_text_width needs *something* to work with */ + + cc->s = cairo_image_surface_create(CAIRO_FORMAT_RGB24, 16, 16); + cc->cr = cairo_create(cc->s); + + return cc; +} + + +static void cr_end(void *ctx) +{ + struct cairo_ctx *cc = ctx; + int x, y, w, h; + + cairo_surface_destroy(cc->s); + cairo_destroy(cc->cr); + + layer_bbox(&cc->layer, &x, &y, &w, &h); + +// fprintf(stderr, "%dx%d%+d%+d\n", w, h, x, y); + cc->xo = -cd(cc, x); + cc->yo = -cd(cc, y); + w = cd(cc, w); + h = cd(cc, h); +// fprintf(stderr, "%dx%d%+d%+d\n", w, h, x, y); + + cc->s = cairo_image_surface_create(CAIRO_FORMAT_RGB24, w, h); cc->cr = cairo_create(cc->s); set_color(cc->cr, COLOR_WHITE); @@ -230,19 +266,10 @@ static void *cr_init(int argc, char *const *argv) CAIRO_FONT_WEIGHT_BOLD); cairo_set_line_width(cc->cr, 3); - return cc; -} - - -static void cr_end(void *ctx) -{ - struct cairo_ctx *cc = ctx; - layer_replay(&cc->layer); layer_destroy(&cc->layer); cairo_surface_write_to_png(cc->s, "test.png"); - cairo_surface_destroy(cc->s); } diff --git a/sch2fig/layer.c b/sch2fig/layer.c index 03ba621..73c2c06 100644 --- a/sch2fig/layer.c +++ b/sch2fig/layer.c @@ -78,9 +78,11 @@ static void bb(struct layer *layer, int x, int y) static void bb_rot(struct layer *layer, int x, int y, int rot) { - double a = rot / 180.0 * M_PI; + double a = -rot / 180.0 * M_PI; - bb(layer, cos(a) * x + sin(a) * y, cos(a) * y - sin(a) * y); + // @@@ figure this out later +return; + bb(layer, cos(a) * x + sin(a) * y, cos(a) * y - sin(a) * x); }