]> git.cworth.org Git - spritext/blobdiff - spritext.c
More refactoring out of main (get_max_width_height)
[spritext] / spritext.c
index 1c67ab5ffad97416e59d1778836fd049c232134b..39c05a59cf3417056d899ea23acd245b840d500a 100644 (file)
@@ -24,11 +24,41 @@ static cairo_status_t
 stdio_write (void *closure, const unsigned char *data, unsigned int length);
 #endif
 
-double
-get_max_width (cairo_t *cr, char *characters);
+static double
+get_max_width (cairo_t *cr, const char *characters)
+{
+    int i;
+    double max_width = 0.;
+    char string[2];
+    string[1] = '\0';
+    for (i = 0; i < ARRAY_SIZE(characters); i++) {
+       cairo_text_extents_t extents;
+       string[0] = characters[i];
+       cairo_text_extents (cr, string, &extents);
+       if ((extents.width + (extents.x_bearing * 2)) > max_width)
+           max_width = extents.width + (extents.x_bearing * 2);
+    }
+    return max_width;
+}
 
-double
-get_max_height (cairo_t *cr, char *characters);
+static double
+get_max_height (cairo_t *cr, const char *characters)
+{
+    int i;
+    double max_height = 0.;
+    char string[2];
+    string[1] = '\0';
+    for (i = 0; i < ARRAY_SIZE(characters); i++) {
+       cairo_text_extents_t extents;
+       string[0] = characters[i];
+       cairo_text_extents (cr, string, &extents);
+//    if ((extents.height - extents.y_bearing) > max_height)
+//      max_height = extents.height - extents.y_bearing;
+       if ((extents.height) > max_height)
+           max_height = extents.height;
+    }
+    return max_height;
+}
 
 typedef struct {
     char *family;
@@ -75,50 +105,17 @@ parse_args (args_t *args)
 #endif
 }
 
-int
-main (void)
+static FT_Face
+load_ft_face_for_family (const char *family)
 {
     /* XXX: Instead of a hard-coded filename here, we should really be
-     * taking args.family and using fontconfig to map that to a
+     * taking family and using fontconfig to map that to a
      * filename. */
     const char font_filename[] = "./Vera.ttf";
-    char outputJson = FALSE;
-
-    char characters[] = {
-       '!', '"', '#', '$', '%', '&','\'', '(',
-       ')', '*', '+', ',', '-', '.', '/', '0',
-       '1', '2', '3', '4', '5', '6', '7', '8',
-       '9', ':', ';', '<', '=', '>', '?', '@',
-       'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
-       'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
-       'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
-       'Y', 'Z', '[','\\', ']', '^', '_', '`',
-       'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
-       'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
-       'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
-       'y', 'z', '{', '|', '}', '~'
-    };
 
-    int i;
-    args_t args;
-
-    parse_args (&args);
-
-#if USE_CGIC
-    if (outputJson)
-    {
-       cgiHeaderContentType("application/json");
-    } else {
-       cgiHeaderContentType("image/png");
-    }
-#endif
-
-    int error;  
+    int error;
+    FT_Face     ft_face;
     FT_Library         library;
-    FT_Face    ft_face;
-    FT_Bool     use_kerning;
-    FT_UInt     left_index, right_index;
-    FT_Vector   kerning;
 
     error = FT_Init_FreeType( &library );
     if ( error ) {
@@ -137,9 +134,14 @@ main (void)
        exit (1);
     }
 
-    use_kerning = FT_HAS_KERNING( ft_face );
+    return ft_face;
+}
 
-    /* Compute max-width and max-height */
+static void
+get_characters_max_width_height (FT_Face ft_face, double size,
+                                const char *characters,
+                                double *max_width, double *max_height)
+{
     cairo_surface_t *surface;
     cairo_t *cr;
     cairo_font_face_t *cr_face;
@@ -157,18 +159,69 @@ 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, args.size);
+    cairo_set_font_size (cr, size);
 
     cairo_set_line_width (cr, 1.0);
 
-    double max_width = get_max_width(cr, characters);
-    double max_height = get_max_height(cr, characters);
+    *max_width = get_max_width(cr, characters);
+    *max_height = get_max_height(cr, characters);
 
     cairo_font_face_destroy (cr_face);
 
     cairo_destroy (cr);
 
     cairo_surface_destroy(surface);
+}
+
+int
+main (void)
+{
+    char outputJson = FALSE;
+
+    char characters[] = {
+       '!', '"', '#', '$', '%', '&','\'', '(',
+       ')', '*', '+', ',', '-', '.', '/', '0',
+       '1', '2', '3', '4', '5', '6', '7', '8',
+       '9', ':', ';', '<', '=', '>', '?', '@',
+       'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
+       'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
+       'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
+       'Y', 'Z', '[','\\', ']', '^', '_', '`',
+       'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
+       'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
+       'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
+       'y', 'z', '{', '|', '}', '~'
+    };
+
+    int i;
+    args_t args;
+    FT_Face ft_face;
+    FT_UInt     left_index, right_index;
+    FT_Bool     use_kerning;
+    FT_Vector   kerning;
+    double max_width, max_height;
+    cairo_surface_t *surface;
+    cairo_t *cr;
+    cairo_font_face_t *cr_face;
+
+    parse_args (&args);
+
+#if USE_CGIC
+    if (outputJson)
+    {
+       cgiHeaderContentType("application/json");
+    } else {
+       cgiHeaderContentType("image/png");
+    }
+#endif
+
+    ft_face = load_ft_face_for_family (args.family);
+
+    use_kerning = FT_HAS_KERNING( ft_face );
+
+    get_characters_max_width_height (ft_face, args.size,
+                                    characters,
+                                    &max_width, &max_height);
 
     /* Draw */
     surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
@@ -348,40 +401,3 @@ stdio_write (void *closure, const unsigned char *data, unsigned int length)
        return CAIRO_STATUS_WRITE_ERROR;
 }
 #endif
-
-double
-get_max_width(cairo_t *cr, char *characters)
-{
-    int i;
-    double max_width = 0.;
-    char string[2];
-    string[1] = '\0';
-    for (i = 0; i < ARRAY_SIZE(characters); i++) {
-       cairo_text_extents_t extents;
-       string[0] = characters[i];
-       cairo_text_extents (cr, string, &extents);
-       if ((extents.width + (extents.x_bearing * 2)) > max_width)
-           max_width = extents.width + (extents.x_bearing * 2);
-    }
-    return max_width;
-}
-
-double
-get_max_height(cairo_t *cr, char *characters)
-{
-    int i;
-    double max_height = 0.;
-    char string[2];
-    string[1] = '\0';
-    for (i = 0; i < ARRAY_SIZE(characters); i++) {
-       cairo_text_extents_t extents;
-       string[0] = characters[i];
-       cairo_text_extents (cr, string, &extents);
-//    if ((extents.height - extents.y_bearing) > max_height)
-//      max_height = extents.height - extents.y_bearing;
-       if ((extents.height) > max_height)
-           max_height = extents.height;
-    }
-    return max_height;
-}
-