]> git.cworth.org Git - spritext/commitdiff
Move drawing code into its own function outside of main
authorCarl Worth <cworth@cworth.org>
Wed, 13 Feb 2008 19:17:00 +0000 (11:17 -0800)
committerCarl Worth <cworth@cworth.org>
Wed, 13 Feb 2008 19:17:00 +0000 (11:17 -0800)
The JSON/kerning code is removed as collateral damage with this
commit. Sorry about that. But at least it can be found here in
the history as needed.

spritext.c

index 39c05a59cf3417056d899ea23acd245b840d500a..212b87214dc1cb9cec7569946892cb8f9690c083 100644 (file)
@@ -25,13 +25,13 @@ stdio_write (void *closure, const unsigned char *data, unsigned int length);
 #endif
 
 static double
-get_max_width (cairo_t *cr, const char *characters)
+get_max_width (cairo_t *cr, const char *characters, int num_characters)
 {
     int i;
     double max_width = 0.;
     char string[2];
     string[1] = '\0';
-    for (i = 0; i < ARRAY_SIZE(characters); i++) {
+    for (i = 0; i < num_characters; i++) {
        cairo_text_extents_t extents;
        string[0] = characters[i];
        cairo_text_extents (cr, string, &extents);
@@ -42,13 +42,13 @@ get_max_width (cairo_t *cr, const char *characters)
 }
 
 static double
-get_max_height (cairo_t *cr, const char *characters)
+get_max_height (cairo_t *cr, const char *characters, int num_characters)
 {
     int i;
     double max_height = 0.;
     char string[2];
     string[1] = '\0';
-    for (i = 0; i < ARRAY_SIZE(characters); i++) {
+    for (i = 0; i < num_characters; i++) {
        cairo_text_extents_t extents;
        string[0] = characters[i];
        cairo_text_extents (cr, string, &extents);
@@ -139,7 +139,7 @@ load_ft_face_for_family (const char *family)
 
 static void
 get_characters_max_width_height (FT_Face ft_face, double size,
-                                const char *characters,
+                                const char *characters, int num_characters,
                                 double *max_width, double *max_height)
 {
     cairo_surface_t *surface;
@@ -163,8 +163,8 @@ get_characters_max_width_height (FT_Face ft_face, double size,
 
     cairo_set_line_width (cr, 1.0);
 
-    *max_width = get_max_width(cr, characters);
-    *max_height = get_max_height(cr, characters);
+    *max_width = get_max_width(cr, characters, num_characters);
+    *max_height = get_max_height(cr, characters, num_characters);
 
     cairo_font_face_destroy (cr_face);
 
@@ -173,62 +173,14 @@ get_characters_max_width_height (FT_Face ft_face, double size,
     cairo_surface_destroy(surface);
 }
 
-int
-main (void)
+static void
+draw_character_table (cairo_t *cr,
+                     FT_Face ft_face, double size,
+                     const char *characters, int num_characters,
+                     double character_width, double character_height)
 {
-    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,
-                                         (max_width + 8) * 10,
-                                         (max_height + 8) * 10);
-
-    cr = cairo_create (surface);
+    int i;
 
 /*
   cairo_select_font_face (cr, fontname,
@@ -238,71 +190,37 @@ 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_text_extents_t extents;
     double x = 0.0;
     double y = 0.0;
 
-    if (outputJson)
-       printf("{");
-
-    cairo_translate (cr, 0, -(max_height / 2.50));
+    cairo_translate (cr, 0, -(character_height / 2.50));
 
 //    cairo_translate (cr, 0, -5.0);
 
     char string[2];
     string[1] = '\0';
-    for (i = 0; i < ARRAY_SIZE(characters); i++)
+    for (i = 0; i < num_characters; i++)
     {
        if (i % 10 == 0)
        {
            x = 0.;
-           y += max_height + 8;
+           y += character_height + 8;
        }
        string[0] = characters[i];
 
        cairo_text_extents (cr, string, &extents);
 
-       if (outputJson)
-       {
-           if (i > 0)
-               printf (",\n");
-           printf ("u%i:{", characters[i]);
-           printf ("x:%.1f,y:%.1f,w:%.1f,h:%.1f",
-                   x,
-                   y - (max_height * 1.15),
-                   extents.width + extents.x_bearing,
-                   extents.height);
-           int j;
-           for ( j = 0; j < ARRAY_SIZE(characters); j++ )
-           {
-               right_index = FT_Get_Char_Index( ft_face, characters[i] );
-               left_index = FT_Get_Char_Index( ft_face, characters[j] );
-    
-               FT_Get_Kerning( ft_face, left_index, right_index,
-                               FT_KERNING_UNSCALED, &kerning );
-    
-               if ( kerning.x )
-                   printf(",k%d:%ld", characters[j], kerning.x);
-           }
-
-           printf ("}");
-       }
-
        x -= extents.x_bearing;
   
        cairo_move_to (cr, x, y);
-       cairo_set_source_rgb (cr, 0., 0., 0.); // black
-       cairo_set_source_rgb (cr,
-                             args.color.red,
-                             args.color.green,
-                             args.color.blue);
        cairo_text_path (cr, string);
        cairo_fill (cr);
 
        x += extents.x_bearing;
-       x += max_width + 8;
+       x += character_width + 8;
     }
 
 /*
@@ -370,6 +288,64 @@ cairo_mask (cr, gradient);
 
 cairo_pattern_destroy (gradient);
 */
+}
+
+int
+main (void)
+{
+    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', '{', '|', '}', '~'
+    };
+
+    args_t args;
+    FT_Face ft_face;
+    double max_width, max_height;
+    cairo_surface_t *surface;
+    cairo_t *cr;
+
+    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);
+
+    get_characters_max_width_height (ft_face, args.size,
+                                    characters, ARRAY_SIZE (characters),
+                                    &max_width, &max_height);
+
+    /* Draw */
+    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
+                                         (max_width + 8) * 10,
+                                         (max_height + 8) * 10);
+
+    cr = cairo_create (surface);
+
+    cairo_set_source_rgb (cr,
+                         args.color.red,
+                         args.color.green,
+                         args.color.blue);
+
+    draw_character_table (cr, ft_face, args.size,
+                         characters, ARRAY_SIZE (characters),
+                         max_width, max_height);
 
 #if USE_CGIC
     if (outputJson)