]> git.cworth.org Git - spritext/commitdiff
Refactor argument parsing into parse_args instead of just being in main
authorCarl Worth <cworth@cworth.org>
Wed, 13 Feb 2008 18:34:30 +0000 (10:34 -0800)
committerCarl Worth <cworth@cworth.org>
Wed, 13 Feb 2008 18:34:48 +0000 (10:34 -0800)
Also, fix things so that default arguments are actually initialized
in the non-CGI case. With this change, the output is actually non-
empty now. Hurrah!

spritext.c

index a188f3e04328b0279dbcb90e6f0cd376348d57c4..1c67ab5ffad97416e59d1778836fd049c232134b 100644 (file)
@@ -30,9 +30,57 @@ get_max_width (cairo_t *cr, char *characters);
 double
 get_max_height (cairo_t *cr, char *characters);
 
+typedef struct {
+    char *family;
+    double size;
+    struct {
+       double red;
+       double green;
+       double blue;
+    } color;
+} args_t;
+
+static void
+parse_args (args_t *args)
+{
+    /* First some defaults */
+    args->family = "Vera";
+    args->size = 20;
+    args->color.red = 0.0;
+    args->color.green = 0.0;
+    args->color.blue = 0.0;
+
+    /* XXX: Next, we should override the defaults based on
+     * command-line arguments. */
+
+    /* XXX: Finally, we'll want to allow CGI-based arguments as
+     * well. */
+#if USE_CGIG
+    /* QueryString */
+    int fontsize;
+    char format[5];
+    char fillcolor[20];
+    char fontname[20];
+    cgiFormStringNoNewlines("fontname", fontname, 20);
+    cgiFormInteger("fontsize", &fontsize, 50);
+    cgiFormStringNoNewlines("format", format, 5);
+    cgiFormStringNoNewlines("fillcolor", fillcolor, 20);
+
+    int fillcolor_red;
+    int fillcolor_green;
+    int fillcolor_blue;
+    sscanf (fillcolor, " rgb : %d , %d , %d ", &fillcolor_red, &fillcolor_green, &fillcolor_blue);
+    if ( format[0] == 'j' && format[1] == 's' && format[2] == 'o' && format[3] == 'n' )
+       outputJson = TRUE;
+#endif
+}
+
 int
 main (void)
 {
+    /* XXX: Instead of a hard-coded filename here, we should really be
+     * taking args.family and using fontconfig to map that to a
+     * filename. */
     const char font_filename[] = "./Vera.ttf";
     char outputJson = FALSE;
 
@@ -52,27 +100,9 @@ main (void)
     };
 
     int i;
+    args_t args;
 
-    /* QueryString */
-    int fontsize;
-    char format[5];
-    char fillcolor[20];
-
-#if USE_CGIG
-    char fontname[20];
-    cgiFormStringNoNewlines("fontname", fontname, 20);
-    cgiFormInteger("fontsize", &fontsize, 50);
-    cgiFormStringNoNewlines("format", format, 5);
-    cgiFormStringNoNewlines("fillcolor", fillcolor, 20);
-#endif
-
-    int fillcolor_red;
-    int fillcolor_green;
-    int fillcolor_blue;
-    sscanf (fillcolor, " rgb : %d , %d , %d ", &fillcolor_red, &fillcolor_green, &fillcolor_blue);
-
-    if ( format[0] == 'j' && format[1] == 's' && format[2] == 'o' && format[3] == 'n' )
-       outputJson = TRUE;
+    parse_args (&args);
 
 #if USE_CGIC
     if (outputJson)
@@ -127,7 +157,7 @@ main (void)
     cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
     cairo_set_font_face (cr, cr_face);
 
-    cairo_set_font_size (cr, fontsize);
+    cairo_set_font_size (cr, args.size);
 
     cairo_set_line_width (cr, 1.0);
 
@@ -155,7 +185,7 @@ main (void)
     cr_face = cairo_ft_font_face_create_for_ft_face (ft_face, 0);
     cairo_set_font_face (cr, cr_face);
 
-    cairo_set_font_size (cr, fontsize);
+    cairo_set_font_size (cr, args.size);
 
     cairo_text_extents_t extents;
     double x = 0.0;
@@ -211,10 +241,10 @@ main (void)
   
        cairo_move_to (cr, x, y);
        cairo_set_source_rgb (cr, 0., 0., 0.); // black
-       if ( 0 <= fillcolor_red && fillcolor_red <= 255.0
-            && 0 <= fillcolor_green && fillcolor_green <= 255.0
-            && 0 <= fillcolor_blue && fillcolor_blue <= 255.0 )
-           cairo_set_source_rgb (cr, fillcolor_red / 255.0, fillcolor_green / 255.0, fillcolor_blue / 255.0);
+       cairo_set_source_rgb (cr,
+                             args.color.red,
+                             args.color.green,
+                             args.color.blue);
        cairo_text_path (cr, string);
        cairo_fill (cr);