summaryrefslogtreecommitdiff
path: root/gfx
diff options
context:
space:
mode:
authorWerner Almesberger <werner@almesberger.net>2016-08-24 19:17:24 -0300
committerWerner Almesberger <werner@almesberger.net>2016-08-24 19:17:24 -0300
commit891cec01ba4dace8feba7636c0f9aa293324cf09 (patch)
tree1f881019d6e6624503ff93e38970b718d2840ace /gfx
parent26a2ecbc82cde50d34c2828a810b84dd616a43de (diff)
downloadeeshow-891cec01ba4dace8feba7636c0f9aa293324cf09.tar.gz
eeshow-891cec01ba4dace8feba7636c0f9aa293324cf09.tar.bz2
eeshow-891cec01ba4dace8feba7636c0f9aa293324cf09.zip
gfx/cro.c (cr_text): experimental Pango support; option -P to enable
Diffstat (limited to 'gfx')
-rw-r--r--gfx/cro.c96
-rw-r--r--gfx/cro.h3
2 files changed, 89 insertions, 10 deletions
diff --git a/gfx/cro.c b/gfx/cro.c
index 3e713a6..3ba3bb0 100644
--- a/gfx/cro.c
+++ b/gfx/cro.c
@@ -21,6 +21,7 @@
#include <cairo/cairo.h>
#include <cairo/cairo-pdf.h>
+#include <pango/pangocairo.h>
#include "misc/util.h"
#include "misc/diag.h"
@@ -42,6 +43,9 @@
#define DEFAULT_SCALE (72.0 / 1200)
+bool use_pango = 0;
+
+
struct cro_ctx {
struct record record; /* must be first */
@@ -51,6 +55,9 @@ struct cro_ctx {
cairo_t *cr;
cairo_surface_t *s;
+ PangoFontDescription *pango_desc;
+ PangoLayout *pango_layout;
+
struct record *sheets; /* for PDF */
unsigned n_sheets;
@@ -193,7 +200,7 @@ static void cr_arc(void *ctx, int x, int y, int r, int sa, int ea,
#define TEXT_STRETCH 1.3
-static void cr_text(void *ctx, int x, int y, const char *s, unsigned size,
+static void cr_text_cairo(void *ctx, int x, int y, const char *s, unsigned size,
enum text_align align, int rot, unsigned color, unsigned layer)
{
struct cro_ctx *cc = ctx;
@@ -228,6 +235,62 @@ static void cr_text(void *ctx, int x, int y, const char *s, unsigned size,
}
+static void cr_text_pango(void *ctx, int x, int y, const char *s, unsigned size,
+ enum text_align align, int rot, unsigned color, unsigned layer)
+{
+ struct cro_ctx *cc = ctx;
+ PangoRectangle ink;
+
+ pango_font_description_set_absolute_size(cc->pango_desc,
+// pango_font_description_set_size(cc->pango_desc,
+ cd(cc, size) * TEXT_STRETCH * PANGO_SCALE);
+ pango_layout_set_text(cc->pango_layout, s, -1);
+ pango_layout_set_font_description(cc->pango_layout, cc->pango_desc);
+ pango_layout_get_extents(cc->pango_layout, &ink, NULL);
+
+ set_color(cc, color);
+
+ cairo_move_to(cc->cr, cx(cc, x), cy(cc, y));
+
+ cairo_save(cc->cr);
+ cairo_rotate(cc->cr, -rot / 180.0 * M_PI);
+
+ switch (align) {
+ case text_min:
+ cairo_rel_move_to(cc->cr,
+ -ink.x / PANGO_SCALE,
+ -(ink.y + ink.height) / PANGO_SCALE);
+ break;
+ case text_mid:
+ cairo_rel_move_to(cc->cr,
+ -(ink.x + ink.width / 2.0) / PANGO_SCALE,
+ -(ink.y + ink.height) / PANGO_SCALE);
+ break;
+ case text_max:
+ cairo_rel_move_to(cc->cr,
+ -(ink.x + ink.width) / PANGO_SCALE,
+ -(ink.y + + ink.height) / PANGO_SCALE);
+ break;
+ default:
+ BUG("invalid alignment %d", align);
+ }
+
+ pango_cairo_update_layout(cc->cr, cc->pango_layout);
+ pango_cairo_show_layout(cc->cr, cc->pango_layout);
+ cairo_restore(cc->cr);
+}
+
+
+static void cr_text(void *ctx, int x, int y, const char *s, unsigned size,
+ enum text_align align, int rot, unsigned color, unsigned layer)
+{
+ if (use_pango)
+ cr_text_pango(ctx, x, y, s, size, align, rot, color, layer);
+ else
+ cr_text_cairo(ctx, x, y, s, size, align, rot, color, layer);
+}
+
+
static unsigned cr_text_width(void *ctx, const char *s, unsigned size)
{
struct cro_ctx *cc = ctx;
@@ -289,6 +352,22 @@ static struct cro_ctx *new_cc(void)
}
+static void setup_pango(struct cro_ctx *cc)
+{
+ if (use_pango) {
+ cc->pango_desc =
+ pango_font_description_from_string("Helvetica Bold");
+ cc->pango_layout = pango_cairo_create_layout(cc->cr);
+ // pango_font_description_free(cc->pango_desc);
+ } else {
+ cairo_select_font_face(cc->cr, "Helvetica",
+ CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
+ }
+
+ // @@@ to destroy pango_layout, g_object_unref(layout);
+}
+
+
static bool cr_args(void *ctx, int argc, char *const *argv)
{
struct cro_ctx *cc = ctx;
@@ -448,8 +527,7 @@ static void cr_pdf_end(void *ctx)
cc->cr = cairo_create(cc->s);
cairo_scale(cc->cr, 1.0 / 16.0, 1.0 / 16);
- cairo_select_font_face(cc->cr, "Helvetica", CAIRO_FONT_SLANT_NORMAL,
- CAIRO_FONT_WEIGHT_BOLD);
+ setup_pango(cc);
cairo_set_line_width(cc->cr, 0.5 * cc->scale);
/* @@@ CAIRO_LINE_CAP_ROUND makes all non-dashed lines disappear */
cairo_set_line_cap(cc->cr, CAIRO_LINE_CAP_SQUARE);
@@ -522,8 +600,7 @@ uint32_t *cro_img_end(struct cro_ctx *cc, int *w, int *h, int *stride)
set_color(cc, COLOR_WHITE);
cairo_paint(cc->cr);
- cairo_select_font_face(cc->cr, "Helvetica", CAIRO_FONT_SLANT_NORMAL,
- CAIRO_FONT_WEIGHT_BOLD);
+ setup_pango(cc);
cairo_set_line_width(cc->cr, 2);
cairo_set_line_cap(cc->cr, CAIRO_LINE_CAP_ROUND);
@@ -564,9 +641,6 @@ void cro_canvas_prepare(cairo_t *cr)
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_paint(cr);
- cairo_select_font_face(cr, "Helvetica", CAIRO_FONT_SLANT_NORMAL,
- CAIRO_FONT_WEIGHT_BOLD);
-
cairo_set_line_width(cr, 2);
cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
}
@@ -577,6 +651,8 @@ void cro_canvas_draw(struct cro_ctx *cc, cairo_t *cr, int xo, int yo,
{
cc->cr = cr;
+ setup_pango(cc);
+
cc->scale = scale;
cc->xo = xo;
cc->yo = yo;
@@ -606,8 +682,6 @@ uint32_t *cro_img(struct cro_ctx *ctx, struct cro_ctx *ctx_extra,
cairo_set_source_rgb(cr, 1, 1, 1);
cairo_paint(cr);
- cairo_select_font_face(cr, "Helvetica", CAIRO_FONT_SLANT_NORMAL,
- CAIRO_FONT_WEIGHT_BOLD);
cairo_set_line_width(cr, 2);
cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
@@ -618,6 +692,8 @@ uint32_t *cro_img(struct cro_ctx *ctx, struct cro_ctx *ctx_extra,
ctx->scale = scale;
ctx->color_override = COLOR_NONE;
+ setup_pango(ctx);
+
if (ctx_extra) {
ctx_extra->record.user = ctx->record.user; /* @@@ eww ! */
record_replay(&ctx_extra->record);
diff --git a/gfx/cro.h b/gfx/cro.h
index c7edd4a..1f2e829 100644
--- a/gfx/cro.h
+++ b/gfx/cro.h
@@ -14,6 +14,7 @@
#ifndef GFX_CRO_H
#define GFX_CRO_H
+#include <stdbool.h>
#include <stdint.h>
#include <cairo/cairo.h>
@@ -24,6 +25,8 @@
struct cro_ctx;
+extern bool use_pango;
+
extern const struct gfx_ops cro_png_ops;
extern const struct gfx_ops cro_pdf_ops;