]> git.cworth.org Git - spritext/commitdiff
More refactoring out of main (get_max_width_height)
authorCarl Worth <cworth@cworth.org>
Wed, 13 Feb 2008 19:06:08 +0000 (11:06 -0800)
committerCarl Worth <cworth@cworth.org>
Wed, 13 Feb 2008 19:06:08 +0000 (11:06 -0800)
spritext.c

index 62e1d2f681539976672a8c3103c18bd414801604..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;
@@ -107,6 +137,42 @@ load_ft_face_for_family (const char *family)
     return ft_face;
 }
 
+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;
+
+    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
+                                         1, 1);
+
+    cr = cairo_create (surface);
+
+/*
+  cairo_select_font_face (cr, fontname,
+  CAIRO_FONT_SLANT_NORMAL,
+  CAIRO_FONT_WEIGHT_NORMAL);
+*/
+    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, size);
+
+    cairo_set_line_width (cr, 1.0);
+
+    *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)
 {
@@ -133,6 +199,10 @@ main (void)
     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);
 
@@ -149,36 +219,9 @@ main (void)
 
     use_kerning = FT_HAS_KERNING( ft_face );
 
-    /* Compute max-width and max-height */
-    cairo_surface_t *surface;
-    cairo_t *cr;
-    cairo_font_face_t *cr_face;
-
-    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
-                                         1, 1);
-
-    cr = cairo_create (surface);
-
-/*
-  cairo_select_font_face (cr, fontname,
-  CAIRO_FONT_SLANT_NORMAL,
-  CAIRO_FONT_WEIGHT_NORMAL);
-*/
-    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_line_width (cr, 1.0);
-
-    double max_width = get_max_width(cr, characters);
-    double max_height = get_max_height(cr, characters);
-
-    cairo_font_face_destroy (cr_face);
-
-    cairo_destroy (cr);
-
-    cairo_surface_destroy(surface);
+    get_characters_max_width_height (ft_face, args.size,
+                                    characters,
+                                    &max_width, &max_height);
 
     /* Draw */
     surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
@@ -358,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;
-}
-