From 678d5f954caab8fe81a86ef97f67288f0fca9511 Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Wed, 13 Feb 2008 11:06:08 -0800 Subject: [PATCH] More refactoring out of main (get_max_width_height) --- spritext.c | 148 ++++++++++++++++++++++++++++------------------------- 1 file changed, 77 insertions(+), 71 deletions(-) diff --git a/spritext.c b/spritext.c index 62e1d2f..39c05a5 100644 --- a/spritext.c +++ b/spritext.c @@ -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; -} - -- 2.43.0