From 0cf09c110c9919efcbc3ff3b59cb83622b4ce1b2 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Sat, 23 Feb 2019 18:04:22 +0100 Subject: Render text with font --- libstorycode/scparse_priv.h | 1 + libstorycode/slide.h | 23 +--------- libstorycode/slide_render_cairo.c | 17 +++---- libstorycode/storycode.y | 36 +++++++++++---- libstorycode/stylesheet.c | 95 ++++++++++++++++++++++++++++++++++++++- libstorycode/stylesheet.h | 29 ++++++++++++ 6 files changed, 161 insertions(+), 40 deletions(-) (limited to 'libstorycode') diff --git a/libstorycode/scparse_priv.h b/libstorycode/scparse_priv.h index fc73bcb..5812c94 100644 --- a/libstorycode/scparse_priv.h +++ b/libstorycode/scparse_priv.h @@ -41,6 +41,7 @@ struct scpctx /* Frame options */ struct frame_geom geom; int geom_set; + char *font; }; #endif /* SCPARSE_PRIV_H */ diff --git a/libstorycode/slide.h b/libstorycode/slide.h index a7574e1..ca67c2a 100644 --- a/libstorycode/slide.h +++ b/libstorycode/slide.h @@ -30,28 +30,7 @@ typedef struct _slide Slide; typedef struct _slideitem SlideItem; -enum length_unit -{ - LENGTH_FRAC, - LENGTH_UNIT -}; - - -struct length -{ - double len; - enum length_unit unit; -}; - - -struct frame_geom -{ - struct length x; - struct length y; - struct length w; - struct length h; -}; - +#include "stylesheet.h" extern Slide *slide_new(void); extern void slide_free(Slide *s); diff --git a/libstorycode/slide_render_cairo.c b/libstorycode/slide_render_cairo.c index b3c8a8f..87ee49a 100644 --- a/libstorycode/slide_render_cairo.c +++ b/libstorycode/slide_render_cairo.c @@ -64,21 +64,19 @@ static PangoAlignment to_pangoalignment(enum alignment align) static void render_text(struct slide_item *item, cairo_t *cr, PangoContext *pc, - double parent_w, double parent_h) + Stylesheet *ss, double parent_w, double parent_h) { int i; double x, y, w, h; PangoRectangle rect; + PangoFontDescription *fontdesc; 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); + fontdesc = pango_font_description_from_string(stylesheet_get_slide_text_font(ss)); if ( item->layouts == NULL ) { item->layouts = malloc(item->n_paras*sizeof(PangoLayout *)); @@ -100,6 +98,8 @@ static void render_text(struct slide_item *item, cairo_t *cr, PangoContext *pc, pango_layout_set_alignment(item->layouts[i], to_pangoalignment(item->align)); + pango_layout_set_font_description(item->layouts[i], fontdesc); + /* FIXME: Handle *bold*, _underline_, /italic/ etc. */ //pango_layout_set_attributes(item->layouts[i], attrs); //pango_attr_list_unref(attrs); @@ -119,7 +119,8 @@ static void render_text(struct slide_item *item, cairo_t *cr, PangoContext *pc, static void render_image(struct slide_item *item, cairo_t *cr, - ImageStore *is, double parent_w, double parent_h) + Stylesheet *ss, ImageStore *is, + double parent_w, double parent_h) { double x, y, w, h; double wd, hd; @@ -168,12 +169,12 @@ int slide_render_cairo(Slide *s, cairo_t *cr, ImageStore *is, Stylesheet *styles switch ( s->items[i].type ) { case SLIDE_ITEM_TEXT : - render_text(&s->items[i], cr, pc, + render_text(&s->items[i], cr, pc, stylesheet, s->logical_w, s->logical_h); break; case SLIDE_ITEM_IMAGE : - render_image(&s->items[i], cr, is, + render_image(&s->items[i], cr, stylesheet, is, s->logical_w, s->logical_h); break; diff --git a/libstorycode/storycode.y b/libstorycode/storycode.y index 9d1e74f..483d0ea 100644 --- a/libstorycode/storycode.y +++ b/libstorycode/storycode.y @@ -85,7 +85,6 @@ %type bulletpoint %type frameopt %type geometry -%type style_slidesize %type length %type slidetitle %type UNIT @@ -107,10 +106,12 @@ ctx->str = malloc(ctx->max_str*sizeof(char *)); if ( ctx->str == NULL ) ctx->max_str = 0; + style_reset(ctx); frameopts_reset(ctx); } %{ + void frameopts_reset(struct scpctx *ctx) { ctx->geom_set = 0; @@ -124,6 +125,11 @@ void frameopts_reset(struct scpctx *ctx) ctx->geom.h.unit = LENGTH_FRAC; } +void style_reset(struct scpctx *ctx) +{ + ctx->font = NULL; +} + void str_reset(struct scpctx *ctx) { ctx->n_str = 0; @@ -139,6 +145,13 @@ void add_str(struct scpctx *ctx, char *str) ctx->str[ctx->n_str++] = str; } + +void set_text_style(struct scpctx *ctx) +{ + stylesheet_set_slide_text_font(ctx->ss, ctx->font); + style_reset(ctx); +} + %} %% @@ -281,14 +294,21 @@ style_slide: style_slide_def: %empty -| style_slide_def style_slide_prestitle { printf("slide prestitle\n"); } -| style_slide_def style_slide_text { printf("slide text\n"); } -| style_slide_def style_slide_title { printf("slide title\n"); } -| style_slide_def style_slidesize { printf("slide size\n"); } +| style_slide_def style_slide_prestitle { } +| style_slide_def style_slide_text { } +| style_slide_def style_slide_title { } +| style_slide_def style_slidesize { } ; style_slidesize: - SIZE length TIMES length { $$.w = $2; $$.h = $4; $$.x.len = 0.0; $$.y.len = 0.0; printf("size\n");} + SIZE length TIMES length { if ( ($2.unit != LENGTH_UNIT) + || ($4.unit != LENGTH_UNIT) ) + { + fprintf(stderr, "Wrong slide size units\n"); + } else { + stylesheet_set_default_slide_size(ctx->ss, $2.len, $4.len); + } + } ; style_slide_prestitle: @@ -300,7 +320,7 @@ style_slide_title: ; style_slide_text: - TEXTFRAME OPENBRACE styledefs CLOSEBRACE { printf("slide text style\n"); } + TEXTFRAME OPENBRACE styledefs CLOSEBRACE { set_text_style(ctx); } ; styledefs: @@ -309,7 +329,7 @@ styledefs: ; styledef: - FONT STRING { printf("font def: '%s'\n", $2); } + FONT STRING { ctx->font = $2; } | GEOMETRY STRING { printf("type def: '%s'\n", $2); } | PAD STRING { printf("pad def: '%s'\n", $2); } | PARASPACE STRING { printf("align def: '%s'\n", $2); } diff --git a/libstorycode/stylesheet.c b/libstorycode/stylesheet.c index 00210ec..2d715a2 100644 --- a/libstorycode/stylesheet.c +++ b/libstorycode/stylesheet.c @@ -30,18 +30,82 @@ #include "stylesheet.h" +struct style +{ + struct frame_geom geom; + char *font; + double fgcol[4]; /* r g b a */ + double bgcol[4]; /* r g b a */ + double bgcol2[4]; /* r g b a, if gradient */ + double paraspace[4]; /* l r t b */ + double padding[4]; /* l r t b */ +}; + + struct _stylesheet { - int n_items; + struct style narrative; + + double default_slide_w; + double default_slide_h; + struct style slide_text; + struct style slide_prestitle; + struct style slide_slidetitle; }; +static void default_style(struct style *s) +{ + s->geom.x.len = 0.0; + s->geom.x.unit = LENGTH_FRAC; + s->geom.y.len = 0.0; + s->geom.y.unit = LENGTH_FRAC; + s->geom.w.len = 1.0; + s->geom.w.unit = LENGTH_FRAC; + s->geom.h.len = 1.0; + s->geom.h.unit = LENGTH_FRAC; + + s->font = strdup("Sans 12"); + + s->fgcol[0] = 0.0; + s->fgcol[1] = 0.0; + s->fgcol[2] = 0.0; + s->fgcol[3] = 1.0; + + s->bgcol[0] = 1.0; + s->bgcol[1] = 1.0; + s->bgcol[2] = 1.0; + s->bgcol[3] = 1.0; + + s->bgcol2[0] = 1.0; + s->bgcol2[1] = 1.0; + s->bgcol2[2] = 1.0; + s->bgcol2[3] = 1.0; + + s->paraspace[0] = 0.0; + s->paraspace[1] = 0.0; + s->paraspace[2] = 0.0; + s->paraspace[3] = 0.0; + + s->padding[0] = 0.0; + s->padding[1] = 0.0; + s->padding[2] = 0.0; + s->padding[3] = 0.0; +} + + Stylesheet *stylesheet_new() { Stylesheet *s; s = malloc(sizeof(*s)); if ( s == NULL ) return NULL; - s->n_items = 0; + + /* Ultimate defaults */ + default_style(&s->narrative); + default_style(&s->slide_text); + default_style(&s->slide_prestitle); + default_style(&s->slide_slidetitle); + return s; } @@ -49,3 +113,30 @@ void stylesheet_free(Stylesheet *s) { free(s); } + + +int stylesheet_set_default_slide_size(Stylesheet *s, double w, double h) +{ + if ( s == NULL ) return 1; + s->default_slide_w = w; + s->default_slide_h = h; + return 0; +} + + +int stylesheet_set_slide_text_font(Stylesheet *s, char *font) +{ + if ( s == NULL ) return 1; + if ( s->slide_text.font != NULL ) { + free(s->slide_text.font); + } + s->slide_text.font = font; + return 0; +} + + +const char *stylesheet_get_slide_text_font(Stylesheet *s) +{ + if ( s == NULL ) return NULL; + return s->slide_text.font; +} diff --git a/libstorycode/stylesheet.h b/libstorycode/stylesheet.h index f7af223..56a6969 100644 --- a/libstorycode/stylesheet.h +++ b/libstorycode/stylesheet.h @@ -29,8 +29,37 @@ typedef struct _stylesheet Stylesheet; + +enum length_unit +{ + LENGTH_FRAC, + LENGTH_UNIT +}; + + +struct length +{ + double len; + enum length_unit unit; +}; + + +struct frame_geom +{ + struct length x; + struct length y; + struct length w; + struct length h; +}; + + extern Stylesheet *stylesheet_new(void); extern void stylesheet_free(Stylesheet *s); +extern int stylesheet_set_default_slide_size(Stylesheet *s, double w, double h); +extern int stylesheet_set_slide_text_font(Stylesheet *s, char *font); + +extern const char *stylesheet_get_slide_text_font(Stylesheet *s); + #endif /* STYLESHEET_H */ -- cgit v1.2.3