1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-11-23 05:05:20 +02:00

sch2fig/: use bounding box to create PNG of correct size

This commit is contained in:
Werner Almesberger 2016-07-31 23:53:14 -03:00
parent dd1eb7ddc3
commit 8590b960e9
2 changed files with 57 additions and 28 deletions

View File

@ -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);
}

View File

@ -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);
}