From a716de927edbe4aaeb2025c6f11d988ca3ee05b9 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Sat, 23 Feb 2019 10:22:26 +0100 Subject: Unformatted text rendering --- libstorycode/slide.c | 1 + libstorycode/slide_priv.h | 10 ++++++ libstorycode/slide_render_cairo.c | 66 +++++++++++++++++++++++++++++++++++---- libstorycode/storycode.h | 8 +++++ meson.build | 3 ++ 5 files changed, 82 insertions(+), 6 deletions(-) diff --git a/libstorycode/slide.c b/libstorycode/slide.c index 9ce0930..ca40347 100644 --- a/libstorycode/slide.c +++ b/libstorycode/slide.c @@ -111,6 +111,7 @@ int slide_add_text(Slide *s, char **text, int n_text, struct frame_geom geom) item->n_paras = n_text; item->geom = geom; + item->align = ALIGN_LEFT; return 0; } diff --git a/libstorycode/slide_priv.h b/libstorycode/slide_priv.h index 65d2459..fb535ba 100644 --- a/libstorycode/slide_priv.h +++ b/libstorycode/slide_priv.h @@ -23,6 +23,12 @@ #ifndef SLIDE_PRIV_H #define SLIDE_PRIV_H +#ifdef HAVE_PANGO +#include +#endif + +#include "storycode.h" + enum slide_item_type { SLIDE_ITEM_TEXT, @@ -40,6 +46,10 @@ struct slide_item /* For TEXT */ char **paragraphs; int n_paras; + enum alignment align; +#ifdef HAVE_PANGO + PangoLayout **layouts; +#endif /* For IMAGE */ char *filename; diff --git a/libstorycode/slide_render_cairo.c b/libstorycode/slide_render_cairo.c index e701aed..8e1eeba 100644 --- a/libstorycode/slide_render_cairo.c +++ b/libstorycode/slide_render_cairo.c @@ -50,16 +50,69 @@ static double lcalc(struct length l, double pd) } -static void render_text(struct slide_item *item, cairo_t *cr, +static PangoAlignment to_pangoalignment(enum alignment align) +{ + switch ( align ) { + case ALIGN_LEFT : return PANGO_ALIGN_LEFT; + case ALIGN_RIGHT : return PANGO_ALIGN_RIGHT; + case ALIGN_CENTER : return PANGO_ALIGN_CENTER; + default: return PANGO_ALIGN_LEFT; + } +} + + +static void render_text(struct slide_item *item, cairo_t *cr, PangoContext *pc, double parent_w, double parent_h) { - cairo_rectangle(cr, lcalc(item->geom.x, parent_w), - lcalc(item->geom.y, parent_h), - lcalc(item->geom.w, parent_w), - lcalc(item->geom.h, parent_h)); + int i; + double x, y, w, h; + PangoRectangle rect; + + x = lcalc(item->geom.x, parent_w); + y = lcalc(item->geom.y, parent_h); + w = lcalc(item->geom.w, parent_w); + h = lcalc(item->geom.h, parent_h); + + cairo_rectangle(cr, x, y, w, h); cairo_set_source_rgb(cr, 0.0, 0.0, 0.0); cairo_set_line_width(cr, 1.0); cairo_stroke(cr); + + if ( item->layouts == NULL ) { + item->layouts = malloc(item->n_paras*sizeof(PangoLayout *)); + if ( item->layouts == NULL ) return; + for ( i=0; in_paras; i++ ) { + item->layouts[i] = NULL; + } + } + + for ( i=0; in_paras; i++ ) { + + if ( item->layouts[i] == NULL ) { + item->layouts[i] = pango_layout_new(pc); + } + pango_layout_set_width(item->layouts[i], + pango_units_from_double(w)); + pango_layout_set_text(item->layouts[i], item->paragraphs[i], -1); + + pango_layout_set_alignment(item->layouts[i], + to_pangoalignment(item->align)); + + /* FIXME: Handle *bold*, _underline_, /italic/ etc. */ + //pango_layout_set_attributes(item->layouts[i], attrs); + //pango_attr_list_unref(attrs); + + cairo_save(cr); + cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0); + cairo_translate(cr, x, y); + pango_cairo_update_layout(cr, item->layouts[i]); + pango_cairo_show_layout(cr, item->layouts[i]); + pango_layout_get_extents(item->layouts[i], NULL, &rect); + y += pango_units_to_double(rect.height); + cairo_fill(cr); + cairo_restore(cr); + + } } @@ -93,7 +146,8 @@ int slide_render_cairo(Slide *s, cairo_t *cr, Stylesheet *stylesheet, switch ( s->items[i].type ) { case SLIDE_ITEM_TEXT : - render_text(&s->items[i], cr, s->logical_w, s->logical_h); + render_text(&s->items[i], cr, pc, + s->logical_w, s->logical_h); break; case SLIDE_ITEM_IMAGE : diff --git a/libstorycode/storycode.h b/libstorycode/storycode.h index 476941e..6e07a3b 100644 --- a/libstorycode/storycode.h +++ b/libstorycode/storycode.h @@ -29,6 +29,14 @@ #include "presentation.h" +enum alignment +{ + ALIGN_LEFT, + ALIGN_RIGHT, + ALIGN_CENTER +}; + + extern Presentation *storycode_parse_presentation(const char *sc); diff --git a/meson.build b/meson.build index 2f05a81..814da89 100644 --- a/meson.build +++ b/meson.build @@ -28,6 +28,9 @@ pangocairo_dep = dependency('pangocairo', required : true) gdkpixbuf_dep = dependency('gdk-pixbuf-2.0', required : true) cc = meson.get_compiler('c') +if pangocairo_dep.found() + add_project_arguments('-DHAVE_PANGO', language : 'c') +endif # Compiled-in resources gresources = gnome.compile_resources('colloquium-resources', -- cgit v1.2.3