Apply most styles
authorThomas White <taw@bitwiz.me.uk>
Wed, 17 Oct 2018 20:41:03 +0000 (22:41 +0200)
committerThomas White <taw@bitwiz.me.uk>
Wed, 17 Oct 2018 20:41:03 +0000 (22:41 +0200)
src/sc_interp.c
src/stylesheet.c
stylesheet.json

index b603c61..d7f7b6b 100644 (file)
@@ -1025,11 +1025,107 @@ static int add_text(struct frame *fr, PangoContext *pc, SCBlock *bl,
 }
 
 
+static void apply_style(SCInterpreter *scin, Stylesheet *ss, const char *path)
+{
+       char fullpath[256];
+       size_t len;
+       char *result;
+
+       len = strlen(path);
+       if ( len > 160 ) {
+               fprintf(stderr, "Can't apply style: path too long.\n");
+               return;
+       }
+
+       /* Font */
+       strcpy(fullpath, path);
+       strcat(fullpath, ".font");
+       result = stylesheet_lookup(ss, fullpath);
+       if ( result != NULL ) set_font(scin, result);
+
+       /* Foreground colour */
+       strcpy(fullpath, path);
+       strcat(fullpath, ".fgcol");
+       result = stylesheet_lookup(ss, fullpath);
+       if ( result != NULL ) set_colour(scin, result);
+
+       /* Background (vertical gradient) */
+       strcpy(fullpath, path);
+       strcat(fullpath, ".bggradv");
+       result = stylesheet_lookup(ss, fullpath);
+       if ( result != NULL ) set_bggrad(scin, result, GRAD_VERT);
+
+       /* Background (horizontal gradient) */
+       strcpy(fullpath, path);
+       strcat(fullpath, ".bggradh");
+       result = stylesheet_lookup(ss, fullpath);
+       if ( result != NULL ) set_bggrad(scin, result, GRAD_HORIZ);
+
+       /* Background (solid colour) */
+       strcpy(fullpath, path);
+       strcat(fullpath, ".bgcol");
+       result = stylesheet_lookup(ss, fullpath);
+       if ( result != NULL ) set_bgcol(scin, result);
+
+       /* Padding */
+       strcpy(fullpath, path);
+       strcat(fullpath, ".pad");
+       result = stylesheet_lookup(ss, fullpath);
+       if ( result != NULL ) set_padding(sc_interp_get_frame(scin), result);
+
+       update_bg(scin);
+}
+
+
+static void output_frame(SCInterpreter *scin, SCBlock *bl, Stylesheet *ss,
+                         const char *stylename)
+{
+       struct frame *fr;
+       SCBlock *child = sc_block_child(bl);
+       const char *options = sc_block_options(bl);
+       char fullpath[256];
+       size_t len;
+       char *result;
+
+       len = strlen(stylename);
+       if ( len > 160 ) {
+               fprintf(stderr, "Can't apply style: path too long.\n");
+               return;
+       }
+
+       fr = add_subframe(sc_interp_get_frame(scin));
+       fr->scblocks = bl;
+       fr->resizable = 1;
+       if ( fr == NULL ) {
+               fprintf(stderr, _("Failed to add frame.\n"));
+               return;
+       }
+
+       /* Lowest priority: current state of interpreter */
+       set_frame_default_style(fr, scin);
+
+       /* Next priority: geometry from stylesheet */
+       strcpy(fullpath, stylename);
+       strcat(fullpath, ".geometry");
+       result = stylesheet_lookup(ss, fullpath);
+       if ( result != NULL ) {
+               parse_frame_options(fr, sc_interp_get_frame(scin), result);
+       }
+
+       /* Highest priority: parameters to \f (or \slidetitle etc) */
+       parse_frame_options(fr, sc_interp_get_frame(scin), options);
+
+       maybe_recurse_before(scin, child);
+       set_frame(scin, fr);
+       apply_style(scin, ss, stylename);
+       maybe_recurse_after(scin, child, ss);
+}
+
+
 static int check_outputs(SCBlock *bl, SCInterpreter *scin, Stylesheet *ss)
 {
        const char *name = sc_block_name(bl);
        const char *options = sc_block_options(bl);
-       SCBlock *child = sc_block_child(bl);
 
        if ( name == NULL ) {
                add_text(sc_interp_get_frame(scin),
@@ -1051,28 +1147,18 @@ static int check_outputs(SCBlock *bl, SCInterpreter *scin, Stylesheet *ss)
                }
 
        } else if ( strcmp(name, "f")==0 ) {
+               output_frame(scin, bl, ss, "$.slide.frame");
 
-               struct frame *fr;
-
-               fr = add_subframe(sc_interp_get_frame(scin));
-               fr->scblocks = bl;
-               fr->resizable = 1;
-               if ( fr == NULL ) {
-                       fprintf(stderr, _("Failed to add frame.\n"));
-                       return 1;
-               }
+       } else if ( strcmp(name, "slidetitle")==0 ) {
+               output_frame(scin, bl, ss, "$.slide.slidetitle");
 
-               set_frame_default_style(fr, scin);
+       } else if ( strcmp(name, "prestitle")==0 ) {
+               output_frame(scin, bl, ss, "$.slide.prestitle");
 
-               parse_frame_options(fr, sc_interp_get_frame(scin), options);
-
-               maybe_recurse_before(scin, child);
-               set_frame(scin, fr);
-               /* FIXME: Set frame style */
-               maybe_recurse_after(scin, child, ss);
+       } else if ( strcmp(name, "author")==0 ) {
+               output_frame(scin, bl, ss, "$.slide.author");
 
        } else if ( strcmp(name, "newpara")==0 ) {
-
                struct frame *fr = sc_interp_get_frame(scin);
                SCBlock *rbl = bl;
                add_newpara(fr, bl, rbl);
@@ -1085,27 +1171,6 @@ static int check_outputs(SCBlock *bl, SCInterpreter *scin, Stylesheet *ss)
 }
 
 
-static void apply_style(SCInterpreter *scin, Stylesheet *ss, const char *path)
-{
-       char fullpath[256];
-       size_t len;
-       char *bgcol1;
-
-       len = strlen(path);
-       if ( len > 160 ) {
-               fprintf(stderr, "Can't apply style: path too long.\n");
-               return;
-       }
-
-       strcpy(fullpath, path);
-       strcat(fullpath, ".bggradv");
-       bgcol1 = stylesheet_lookup(ss, fullpath);
-       set_bggrad(scin, bgcol1, GRAD_VERT);
-
-       update_bg(scin);
-}
-
-
 int sc_interp_add_block(SCInterpreter *scin, SCBlock *bl, Stylesheet *ss)
 {
        const char *name = sc_block_name(bl);
@@ -1133,7 +1198,7 @@ int sc_interp_add_block(SCInterpreter *scin, SCBlock *bl, Stylesheet *ss)
 
        } else if ( strcmp(name, "slide") == 0 ) {
                maybe_recurse_before(scin, child);
-               /* FIXME: Apply slide style */
+               apply_style(scin, ss, "$.slide");
                maybe_recurse_after(scin, child, ss);
 
        } else if ( strcmp(name, "font") == 0 ) {
index 51cce6e..228c331 100644 (file)
@@ -74,6 +74,11 @@ char *stylesheet_lookup(Stylesheet *ss, const char *path)
        node = json_path_query(path, ss->root, &err);
        array = json_node_get_array(node);
 
+       if ( json_array_get_length(array) < 1 ) {
+               json_node_unref(node);
+               return NULL;
+       }
+
        v = json_array_get_string_element(array, 0);
        if ( v == NULL ) return NULL;
 
index 518c21b..16649b9 100644 (file)
@@ -1,56 +1,40 @@
 {
        "narrative": {
-               "font": "Comfortaa 20",
+               "font": "Comfortaa 16",
                "fgcol": "#000000",
-               "bggradv": "#729fcf,#d3d7cf"
+               "pad": "10,10,10,10"
        },
 
        "slide": {
                "width": 1280,
                "height": 720,
-               "bggrad": "vertical",
-               "bgcol1": "#729fcf",
-               "bgcol2": "#d3d7cf",
+               "bggradv": "#729fcf,#d3d7cf",
                "frame": {
-                       "font": "Comfortaa 20"
+                       "font": "Comfortaa 20",
+                       "bgcol": "#ffffff00"
                },
                "slidetitle": {
-                       "width": "1f",
-                       "height": "90u",
-                       "posx": 0,
-                       "posy": 0,
-                       "padl": 30,
-                       "padr": 30,
-                       "padt": 30,
-                       "padb": 30,
+                       "geometry": "1fx90u+0+0",
+                       "pad": "30,30,30,30",
                        "fgcol": "#000000",
-                       "font": "Comfortaa Bold 40"
+                       "font": "Comfortaa Bold 40",
+                       "bgcol": "#ffffff00"
                },
                "prestitle": {
-                       "width": "1f",
-                       "height": "240u",
-                       "posx": 0,
-                       "posy": 160,
-                       "padl": 50,
-                       "padr": 50,
-                       "padt": 50,
-                       "padb": 50,
+                       "geometry": "1fx240u+0+160",
+                       "pad": "50,50,50,50",
                        "fgcol": "#000000",
                        "font": "Comfortaa Bold 45",
-                       "alignment": "center"
+                       "alignment": "center",
+                       "bgcol": "#ffffff00"
                },
                "author": {
-                       "width": "1f",
-                       "height": "240u",
-                       "posx": 0,
-                       "posy": 360,
-                       "padl": 80,
-                       "padr": 80,
-                       "padt": 80,
-                       "padb": 80,
+                       "geometry": "1fx240u+0+360",
+                       "pad": "80,80,80,80",
                        "fgcol": "#000000",
                        "font": "Comfortaa 20",
-                       "alignment": "center"
+                       "alignment": "center",
+                       "bgcol": "#ffffff00"
                }
        }
 }