2016-07-31 06:04:43 +03:00
|
|
|
/*
|
2016-08-02 00:11:42 +03:00
|
|
|
* cro.c - Cairo graphics back-end
|
2016-07-31 06:04:43 +03:00
|
|
|
*
|
|
|
|
* Written 2016 by Werner Almesberger
|
|
|
|
* Copyright 2016 by Werner Almesberger
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-08-01 07:34:27 +03:00
|
|
|
#include <stddef.h>
|
2016-07-31 06:04:43 +03:00
|
|
|
#include <stdbool.h>
|
2016-08-01 20:38:36 +03:00
|
|
|
#include <stdint.h>
|
2016-08-01 06:11:53 +03:00
|
|
|
#include <stdlib.h>
|
2016-07-31 06:04:43 +03:00
|
|
|
#include <stdint.h>
|
2016-08-01 06:11:53 +03:00
|
|
|
#include <unistd.h>
|
2016-07-31 06:04:43 +03:00
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include <cairo/cairo.h>
|
2016-08-01 06:37:32 +03:00
|
|
|
#include <cairo/cairo-pdf.h>
|
2016-07-31 06:04:43 +03:00
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "style.h"
|
2016-07-31 09:17:10 +03:00
|
|
|
#include "text.h"
|
2016-07-31 06:04:43 +03:00
|
|
|
#include "gfx.h"
|
2016-08-01 17:32:22 +03:00
|
|
|
#include "record.h"
|
2016-08-01 06:11:53 +03:00
|
|
|
#include "main.h"
|
2016-08-02 00:11:42 +03:00
|
|
|
#include "cro.h"
|
2016-07-31 06:04:43 +03:00
|
|
|
|
|
|
|
|
2016-08-01 06:11:53 +03:00
|
|
|
/*
|
|
|
|
* FIG works with 1/1200 in
|
|
|
|
* KiCad works with mil
|
|
|
|
* 1 point = 1/72 in
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DEFAULT_SCALE (72.0 / 1200)
|
2016-07-31 06:04:43 +03:00
|
|
|
|
|
|
|
|
2016-08-02 00:11:42 +03:00
|
|
|
struct cro_ctx {
|
2016-08-01 17:32:22 +03:00
|
|
|
struct record record; /* must be first */
|
2016-08-01 06:11:53 +03:00
|
|
|
|
2016-08-01 05:53:14 +03:00
|
|
|
int xo, yo;
|
2016-08-01 06:11:53 +03:00
|
|
|
float scale;
|
|
|
|
|
2016-07-31 06:04:43 +03:00
|
|
|
cairo_t *cr;
|
|
|
|
cairo_surface_t *s;
|
|
|
|
|
2016-08-01 17:32:22 +03:00
|
|
|
struct record *sheets; /* for PDF */
|
2016-08-01 07:34:27 +03:00
|
|
|
unsigned n_sheets;
|
|
|
|
|
2016-08-01 06:11:53 +03:00
|
|
|
const char *output_name;
|
|
|
|
};
|
2016-07-31 06:04:43 +03:00
|
|
|
|
|
|
|
|
2016-08-02 00:11:42 +03:00
|
|
|
static inline int cd(struct cro_ctx *cc, int x)
|
2016-07-31 06:04:43 +03:00
|
|
|
{
|
2016-08-01 06:11:53 +03:00
|
|
|
return x * cc->scale;
|
2016-07-31 06:04:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-02 00:11:42 +03:00
|
|
|
static inline int cx(struct cro_ctx *cc, int x)
|
2016-07-31 09:28:08 +03:00
|
|
|
{
|
2016-08-01 06:11:53 +03:00
|
|
|
return cc->xo + x * cc->scale;
|
2016-07-31 09:28:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-02 00:11:42 +03:00
|
|
|
static inline int xc(struct cro_ctx *cc, int x)
|
2016-07-31 06:04:43 +03:00
|
|
|
{
|
2016-08-01 06:11:53 +03:00
|
|
|
return (x - cc->xo) / cc->scale;
|
2016-07-31 06:04:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-02 00:11:42 +03:00
|
|
|
static inline int cy(struct cro_ctx *cc, int y)
|
2016-07-31 06:04:43 +03:00
|
|
|
{
|
2016-08-01 06:11:53 +03:00
|
|
|
return cc->yo + y * cc->scale;
|
2016-08-01 05:53:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-02 00:11:42 +03:00
|
|
|
static inline float pt(struct cro_ctx *cc, int x)
|
2016-08-01 05:53:14 +03:00
|
|
|
{
|
|
|
|
return cd(cc, x) * 72 * 1.5 / 1200.0;
|
2016-07-31 06:04:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void set_color(cairo_t *cr, int color)
|
|
|
|
{
|
|
|
|
uint32_t c;
|
|
|
|
|
|
|
|
if (color < 0)
|
|
|
|
return;
|
|
|
|
c = color_rgb[color];
|
|
|
|
cairo_set_source_rgb(cr, (c >> 16) / 255.0, ((c >> 8) & 255) / 255.0,
|
|
|
|
(c & 255) / 255.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void paint(cairo_t *cr, int color, int fill_color)
|
|
|
|
{
|
|
|
|
if (fill_color != COLOR_NONE) {
|
|
|
|
set_color(cr, fill_color);
|
|
|
|
if (color == COLOR_NONE)
|
|
|
|
cairo_fill(cr);
|
|
|
|
else
|
|
|
|
cairo_fill_preserve(cr);
|
|
|
|
}
|
|
|
|
if (color != COLOR_NONE) {
|
|
|
|
set_color(cr, color);
|
|
|
|
cairo_stroke(cr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----- General items ----------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2016-08-01 16:52:08 +03:00
|
|
|
static void cr_line(void *ctx, int sx, int sy, int ex, int ey,
|
|
|
|
int color, unsigned layer)
|
|
|
|
{
|
2016-08-02 00:11:42 +03:00
|
|
|
struct cro_ctx *cc = ctx;
|
2016-08-01 16:52:08 +03:00
|
|
|
static const double dashes[] = { 4, 2 };
|
|
|
|
|
|
|
|
cairo_new_path(cc->cr);
|
|
|
|
cairo_move_to(cc->cr, cx(cc, sx), cy(cc, sy));
|
|
|
|
cairo_line_to(cc->cr, cx(cc, ex), cy(cc, ey));
|
|
|
|
cairo_set_dash(cc->cr, dashes, ARRAY_ELEMENTS(dashes), 0);
|
|
|
|
paint(cc->cr, color, COLOR_NONE);
|
|
|
|
cairo_set_dash(cc->cr, NULL, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-01 16:12:29 +03:00
|
|
|
static void cr_poly(void *ctx,
|
|
|
|
int points, const int x[points], const int y[points],
|
2016-07-31 06:04:43 +03:00
|
|
|
int color, int fill_color, unsigned layer)
|
|
|
|
{
|
2016-08-02 00:11:42 +03:00
|
|
|
struct cro_ctx *cc = ctx;
|
2016-07-31 06:04:43 +03:00
|
|
|
bool closed;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (points < 2)
|
|
|
|
return;
|
|
|
|
closed = x[0] == x[points - 1] && y[0] == y[points - 1];
|
|
|
|
|
|
|
|
cairo_new_path(cc->cr);
|
2016-08-01 05:53:14 +03:00
|
|
|
cairo_move_to(cc->cr, cx(cc, x[0]), cy(cc, y[0]));
|
2016-07-31 06:04:43 +03:00
|
|
|
|
|
|
|
for (i = 1; i != points - closed; i++)
|
2016-08-01 05:53:14 +03:00
|
|
|
cairo_line_to(cc->cr, cx(cc, x[i]), cy(cc, y[i]));
|
2016-07-31 06:04:43 +03:00
|
|
|
if (closed)
|
|
|
|
cairo_close_path(cc->cr);
|
|
|
|
|
|
|
|
paint(cc->cr, color, fill_color);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-31 20:07:29 +03:00
|
|
|
static void cr_circ(void *ctx, int x, int y, int r,
|
2016-07-31 06:04:43 +03:00
|
|
|
int color, int fill_color, unsigned layer)
|
|
|
|
{
|
2016-08-02 00:11:42 +03:00
|
|
|
struct cro_ctx *cc = ctx;
|
2016-07-31 06:04:43 +03:00
|
|
|
|
|
|
|
cairo_new_path(cc->cr);
|
2016-08-01 05:53:14 +03:00
|
|
|
cairo_arc(cc->cr, cx(cc, x), cy(cc, y), cd(cc, r), 0, 2 * M_PI);
|
2016-07-31 06:04:43 +03:00
|
|
|
paint(cc->cr, color, fill_color);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-31 20:07:29 +03:00
|
|
|
static void cr_arc(void *ctx, int x, int y, int r, int sa, int ea,
|
2016-07-31 06:04:43 +03:00
|
|
|
int color, int fill_color, unsigned layer)
|
|
|
|
{
|
2016-08-02 00:11:42 +03:00
|
|
|
struct cro_ctx *cc = ctx;
|
2016-07-31 06:04:43 +03:00
|
|
|
|
|
|
|
cairo_new_path(cc->cr);
|
2016-08-01 05:53:14 +03:00
|
|
|
cairo_arc(cc->cr, cx(cc, x), cy(cc, y), cd(cc, r),
|
2016-07-31 06:04:43 +03:00
|
|
|
-ea / 180.0 * M_PI, -sa / 180.0 * M_PI);
|
|
|
|
paint(cc->cr, color, fill_color);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-31 09:28:08 +03:00
|
|
|
#define TEXT_STRETCH 1.3
|
|
|
|
|
|
|
|
|
2016-07-31 20:07:29 +03:00
|
|
|
static void cr_text(void *ctx, int x, int y, const char *s, unsigned size,
|
2016-07-31 06:04:43 +03:00
|
|
|
enum text_align align, int rot, unsigned color, unsigned layer)
|
|
|
|
{
|
2016-08-02 00:11:42 +03:00
|
|
|
struct cro_ctx *cc = ctx;
|
2016-07-31 09:17:10 +03:00
|
|
|
cairo_text_extents_t ext;
|
|
|
|
cairo_matrix_t m;
|
|
|
|
|
2016-08-01 05:53:14 +03:00
|
|
|
cairo_set_font_size(cc->cr, cd(cc, size) * TEXT_STRETCH);
|
2016-07-31 09:17:10 +03:00
|
|
|
cairo_text_extents(cc->cr, s, &ext);
|
|
|
|
|
|
|
|
set_color(cc->cr, color);
|
|
|
|
|
2016-08-01 05:53:14 +03:00
|
|
|
cairo_move_to(cc->cr, cx(cc, x), cy(cc, y));
|
2016-07-31 09:17:10 +03:00
|
|
|
|
|
|
|
cairo_get_matrix(cc->cr, &m);
|
|
|
|
cairo_rotate(cc->cr, -rot / 180.0 * M_PI);
|
|
|
|
|
|
|
|
switch (align) {
|
|
|
|
case text_min:
|
|
|
|
break;
|
|
|
|
case text_mid:
|
|
|
|
cairo_rel_move_to(cc->cr, -ext.width / 2.0, 0);
|
|
|
|
break;
|
|
|
|
case text_max:
|
|
|
|
cairo_rel_move_to(cc->cr, -ext.width, 0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_show_text(cc->cr, s);
|
|
|
|
cairo_set_matrix(cc->cr, &m);
|
2016-07-31 06:04:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-31 20:07:29 +03:00
|
|
|
static unsigned cr_text_width(void *ctx, const char *s, unsigned size)
|
2016-07-31 09:28:08 +03:00
|
|
|
{
|
2016-08-02 00:11:42 +03:00
|
|
|
struct cro_ctx *cc = ctx;
|
2016-07-31 09:28:08 +03:00
|
|
|
cairo_text_extents_t ext;
|
|
|
|
|
2016-08-01 05:53:14 +03:00
|
|
|
cairo_set_font_size(cc->cr, cx(cc, size) * TEXT_STRETCH);
|
2016-07-31 09:28:08 +03:00
|
|
|
cairo_text_extents(cc->cr, s, &ext);
|
2016-08-01 16:58:50 +03:00
|
|
|
return xc(cc, ext.width) * 1.05; /* @@@ Cairo seems to underestimate */
|
2016-07-31 09:28:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-31 06:04:43 +03:00
|
|
|
/* ----- Initializatio and termination ------------------------------------- */
|
|
|
|
|
|
|
|
|
2016-08-02 00:11:42 +03:00
|
|
|
static const struct gfx_ops real_cro_ops = {
|
2016-08-01 05:07:05 +03:00
|
|
|
.name = "cairo",
|
2016-08-01 16:52:08 +03:00
|
|
|
.line = cr_line,
|
2016-08-01 05:07:05 +03:00
|
|
|
.poly = cr_poly,
|
|
|
|
.circ = cr_circ,
|
|
|
|
.arc = cr_arc,
|
|
|
|
.text = cr_text,
|
|
|
|
.text_width = cr_text_width,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-08-02 00:11:42 +03:00
|
|
|
static struct cro_ctx *init_common(int argc, char *const *argv)
|
2016-07-31 06:04:43 +03:00
|
|
|
{
|
2016-08-02 00:11:42 +03:00
|
|
|
struct cro_ctx *cc;
|
2016-08-01 06:11:53 +03:00
|
|
|
char c;
|
2016-07-31 06:04:43 +03:00
|
|
|
|
2016-08-02 00:11:42 +03:00
|
|
|
cc = alloc_type(struct cro_ctx);
|
2016-08-01 05:53:14 +03:00
|
|
|
cc->xo = cc->yo = 0;
|
2016-08-01 06:11:53 +03:00
|
|
|
cc->scale = DEFAULT_SCALE;
|
|
|
|
|
2016-08-01 07:34:27 +03:00
|
|
|
cc->sheets = NULL;
|
|
|
|
cc->n_sheets = 0;
|
|
|
|
|
2016-08-01 06:11:53 +03:00
|
|
|
cc->output_name = NULL;
|
|
|
|
while ((c = getopt(argc, argv, "o:s:")) != EOF)
|
|
|
|
switch (c) {
|
|
|
|
case 'o':
|
|
|
|
cc->output_name = optarg;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
cc->scale = atof(optarg) * DEFAULT_SCALE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(*argv);
|
|
|
|
}
|
|
|
|
|
2016-08-02 00:11:42 +03:00
|
|
|
record_init(&cc->record, &real_cro_ops, cc);
|
2016-08-01 05:07:05 +03:00
|
|
|
|
2016-08-01 06:37:32 +03:00
|
|
|
return cc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void *cr_png_init(int argc, char *const *argv)
|
|
|
|
{
|
2016-08-02 00:11:42 +03:00
|
|
|
struct cro_ctx *cc;
|
2016-08-01 06:37:32 +03:00
|
|
|
|
|
|
|
cc = init_common(argc, argv);
|
|
|
|
|
2016-08-01 05:53:14 +03:00
|
|
|
/* cr_text_width needs *something* to work with */
|
2016-07-31 09:17:10 +03:00
|
|
|
|
2016-08-01 05:53:14 +03:00
|
|
|
cc->s = cairo_image_surface_create(CAIRO_FORMAT_RGB24, 16, 16);
|
|
|
|
cc->cr = cairo_create(cc->s);
|
2016-07-31 06:04:43 +03:00
|
|
|
|
|
|
|
return cc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-01 06:37:32 +03:00
|
|
|
static void *cr_pdf_init(int argc, char *const *argv)
|
2016-07-31 06:04:43 +03:00
|
|
|
{
|
2016-08-02 00:11:42 +03:00
|
|
|
struct cro_ctx *cc;
|
2016-08-01 06:37:32 +03:00
|
|
|
|
|
|
|
cc = init_common(argc, argv);
|
|
|
|
|
|
|
|
/* cr_text_width needs *something* to work with */
|
|
|
|
|
|
|
|
cc->s = cairo_pdf_surface_create(NULL, 16, 16);
|
|
|
|
cc->cr = cairo_create(cc->s);
|
|
|
|
|
|
|
|
return cc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-02 00:11:42 +03:00
|
|
|
static void end_common(struct cro_ctx *cc, int *w, int *h)
|
2016-08-01 06:37:32 +03:00
|
|
|
{
|
|
|
|
int x, y;
|
2016-08-01 05:53:14 +03:00
|
|
|
|
|
|
|
cairo_surface_destroy(cc->s);
|
|
|
|
cairo_destroy(cc->cr);
|
|
|
|
|
2016-08-01 17:32:22 +03:00
|
|
|
record_bbox(&cc->record, &x, &y, w, h);
|
2016-08-01 05:53:14 +03:00
|
|
|
|
2016-08-01 06:37:32 +03:00
|
|
|
// fprintf(stderr, "%dx%d%+d%+d\n", *w, *h, x, y);
|
2016-08-01 05:53:14 +03:00
|
|
|
cc->xo = -cd(cc, x);
|
|
|
|
cc->yo = -cd(cc, y);
|
2016-08-01 06:37:32 +03:00
|
|
|
*w = cd(cc, *w);
|
|
|
|
*h = cd(cc, *h);
|
|
|
|
// fprintf(stderr, "%dx%d%+d%+d\n", *w, *h, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void cr_png_end(void *ctx)
|
|
|
|
{
|
2016-08-02 00:11:42 +03:00
|
|
|
struct cro_ctx *cc = ctx;
|
2016-08-01 06:37:32 +03:00
|
|
|
int w, h;
|
|
|
|
|
|
|
|
end_common(cc, &w, &h);
|
2016-08-01 05:53:14 +03:00
|
|
|
|
|
|
|
cc->s = cairo_image_surface_create(CAIRO_FORMAT_RGB24, w, h);
|
|
|
|
cc->cr = cairo_create(cc->s);
|
|
|
|
|
|
|
|
set_color(cc->cr, COLOR_WHITE);
|
|
|
|
cairo_paint(cc->cr);
|
|
|
|
|
|
|
|
cairo_select_font_face(cc->cr, "Helvetica", CAIRO_FONT_SLANT_NORMAL,
|
|
|
|
CAIRO_FONT_WEIGHT_BOLD);
|
2016-08-01 16:52:08 +03:00
|
|
|
cairo_set_line_width(cc->cr, 2);
|
2016-07-31 06:04:43 +03:00
|
|
|
|
2016-08-01 17:32:22 +03:00
|
|
|
record_replay(&cc->record);
|
|
|
|
record_destroy(&cc->record);
|
2016-08-01 05:07:05 +03:00
|
|
|
|
2016-08-01 06:11:53 +03:00
|
|
|
if (cc->output_name)
|
|
|
|
cairo_surface_write_to_png(cc->s, cc->output_name);
|
2016-07-31 06:04:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-01 07:34:27 +03:00
|
|
|
static void cr_pdf_new_sheet(void *ctx)
|
|
|
|
{
|
2016-08-02 00:11:42 +03:00
|
|
|
struct cro_ctx *cc = ctx;
|
2016-08-01 07:34:27 +03:00
|
|
|
|
|
|
|
cc->n_sheets++;
|
2016-08-01 17:32:22 +03:00
|
|
|
cc->sheets = realloc(cc->sheets, sizeof(struct record) * cc->n_sheets);
|
2016-08-01 07:34:27 +03:00
|
|
|
if (!cc->sheets) {
|
|
|
|
perror("realloc");
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-08-01 17:32:22 +03:00
|
|
|
cc->sheets[cc->n_sheets - 1] = cc->record;
|
|
|
|
record_wipe(&cc->record);
|
2016-08-01 07:34:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-01 06:37:32 +03:00
|
|
|
static void cr_pdf_end(void *ctx)
|
|
|
|
{
|
2016-08-02 00:11:42 +03:00
|
|
|
struct cro_ctx *cc = ctx;
|
2016-08-01 06:37:32 +03:00
|
|
|
int w, h;
|
2016-08-01 07:34:27 +03:00
|
|
|
unsigned i;
|
2016-08-01 06:37:32 +03:00
|
|
|
|
|
|
|
end_common(cc, &w, &h);
|
|
|
|
|
|
|
|
cc->s = cairo_pdf_surface_create(cc->output_name, w, h);
|
|
|
|
cc->cr = cairo_create(cc->s);
|
|
|
|
|
|
|
|
cairo_select_font_face(cc->cr, "Helvetica", CAIRO_FONT_SLANT_NORMAL,
|
|
|
|
CAIRO_FONT_WEIGHT_BOLD);
|
2016-08-01 07:39:47 +03:00
|
|
|
cairo_set_line_width(cc->cr, 2);
|
2016-08-01 06:37:32 +03:00
|
|
|
|
2016-08-01 07:34:27 +03:00
|
|
|
for (i = 0; i != cc->n_sheets; i++) {
|
|
|
|
set_color(cc->cr, COLOR_WHITE);
|
|
|
|
cairo_paint(cc->cr);
|
|
|
|
|
2016-08-01 17:32:22 +03:00
|
|
|
record_replay(cc->sheets + i);
|
|
|
|
record_destroy(cc->sheets + i);
|
2016-08-01 07:34:27 +03:00
|
|
|
|
|
|
|
cairo_show_page(cc->cr);
|
|
|
|
}
|
|
|
|
|
2016-08-01 17:32:22 +03:00
|
|
|
record_replay(&cc->record);
|
|
|
|
record_destroy(&cc->record);
|
2016-08-01 06:37:32 +03:00
|
|
|
|
|
|
|
cairo_show_page(cc->cr);
|
|
|
|
|
|
|
|
cairo_surface_destroy(cc->s);
|
|
|
|
cairo_destroy(cc->cr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-02 00:11:42 +03:00
|
|
|
uint32_t *cro_img_end(void *ctx, int *w, int *h, int *stride)
|
2016-08-01 20:38:36 +03:00
|
|
|
{
|
2016-08-02 00:11:42 +03:00
|
|
|
struct cro_ctx *cc = ctx;
|
2016-08-01 20:38:36 +03:00
|
|
|
uint32_t *data;
|
|
|
|
|
|
|
|
end_common(cc, w, h);
|
|
|
|
|
|
|
|
*stride = cairo_format_stride_for_width(CAIRO_FORMAT_RGB24, *w);
|
|
|
|
data = alloc_size(*stride * *h);
|
|
|
|
|
|
|
|
cc->s = cairo_image_surface_create_for_data((unsigned char *) data,
|
|
|
|
CAIRO_FORMAT_RGB24, *w, *h, *stride);
|
|
|
|
cc->cr = cairo_create(cc->s);
|
|
|
|
|
|
|
|
set_color(cc->cr, COLOR_WHITE);
|
|
|
|
cairo_paint(cc->cr);
|
|
|
|
|
|
|
|
cairo_select_font_face(cc->cr, "Helvetica", CAIRO_FONT_SLANT_NORMAL,
|
|
|
|
CAIRO_FONT_WEIGHT_BOLD);
|
|
|
|
cairo_set_line_width(cc->cr, 2);
|
|
|
|
|
|
|
|
record_replay(&cc->record);
|
|
|
|
record_destroy(&cc->record);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-02 00:11:42 +03:00
|
|
|
void cro_img_write(void *ctx, const char *name)
|
2016-08-01 20:38:36 +03:00
|
|
|
{
|
2016-08-02 00:11:42 +03:00
|
|
|
struct cro_ctx *cc = ctx;
|
2016-08-01 20:38:36 +03:00
|
|
|
|
|
|
|
cairo_surface_write_to_png(cc->s, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-31 06:04:43 +03:00
|
|
|
/* ----- Operations -------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2016-08-02 00:11:42 +03:00
|
|
|
const struct gfx_ops cro_png_ops = {
|
2016-08-01 06:18:22 +03:00
|
|
|
.name = "png",
|
2016-08-01 17:32:22 +03:00
|
|
|
.line = record_line,
|
|
|
|
.poly = record_poly,
|
|
|
|
.circ = record_circ,
|
|
|
|
.arc = record_arc,
|
|
|
|
.text = record_text,
|
2016-07-31 09:28:08 +03:00
|
|
|
.text_width = cr_text_width,
|
2016-08-01 06:37:32 +03:00
|
|
|
.init = cr_png_init,
|
|
|
|
.end = cr_png_end,
|
|
|
|
};
|
|
|
|
|
2016-08-02 00:11:42 +03:00
|
|
|
const struct gfx_ops cro_pdf_ops = {
|
2016-08-01 06:37:32 +03:00
|
|
|
.name = "pdf",
|
2016-08-01 17:32:22 +03:00
|
|
|
.line = record_line,
|
|
|
|
.poly = record_poly,
|
|
|
|
.circ = record_circ,
|
|
|
|
.arc = record_arc,
|
|
|
|
.text = record_text,
|
2016-08-01 06:37:32 +03:00
|
|
|
.text_width = cr_text_width,
|
|
|
|
.init = cr_pdf_init,
|
2016-08-01 07:34:27 +03:00
|
|
|
.new_sheet = cr_pdf_new_sheet,
|
2016-08-01 06:37:32 +03:00
|
|
|
.end = cr_pdf_end,
|
2016-07-31 06:04:43 +03:00
|
|
|
};
|