head 1.12; access; symbols; locks; strict; comment @ * @; 1.12 date 2005.09.14.18.39.35; author cworth; state Exp; branches; next 1.11; commitid 6ee543286e674567; 1.11 date 2005.05.17.15.24.39; author cworth; state Exp; branches; next 1.10; commitid 22b4428a0cb74567; 1.10 date 2005.05.17.14.52.23; author cworth; state Exp; branches; next 1.9; commitid e64428a05274567; 1.9 date 2005.05.17.14.31.43; author cworth; state Exp; branches; next 1.8; commitid 2e0428a004e4567; 1.8 date 2005.04.02.04.20.39; author cworth; state Exp; branches; next 1.7; 1.7 date 2005.01.11.19.47.53; author cworth; state Exp; branches; next 1.6; 1.6 date 2004.02.19.23.22.15; author dajobe; state Exp; branches; next 1.5; 1.5 date 2003.12.16.15.23.36; author cworth; state Exp; branches; next 1.4; 1.4 date 2003.12.16.03.01.54; author cworth; state Exp; branches; next 1.3; 1.3 date 2003.12.16.02.34.12; author cworth; state Exp; branches; next 1.2; 1.2 date 2003.10.25.00.51.26; author cworth; state Exp; branches; next 1.1; 1.1 date 2003.10.24.20.41.50; author cworth; state Exp; branches; next ; desc @@ 1.12 log @ 2005-09-14 Carl Worth * text.c (box_text): Update for change in cairo_show_text semantics in that it now moves the current point. @ text @/* * Copyright © 2003 USC, Information Sciences Institute * * Permission to use, copy, modify, distribute, and sell this software * and its documentation for any purpose is hereby granted without * fee, provided that the above copyright notice appear in all copies * and that both that copyright notice and this permission notice * appear in supporting documentation, and that the name of the * University of Southern California not be used in advertising or * publicity pertaining to distribution of the software without * specific, written prior permission. The University of Southern * California makes no representations about the suitability of this * software for any purpose. It is provided "as is" without express * or implied warranty. * * THE UNIVERSITY OF SOUTHERN CALIFORNIA DISCLAIMS ALL WARRANTIES WITH * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE UNIVERSITY OF * SOUTHERN CALIFORNIA BE LIABLE FOR ANY SPECIAL, INDIRECT OR * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * Author: Carl D. Worth */ #include #include #define TEXT "hello, world" #define WIDTH 450 #define HEIGHT 600 #define STRIDE (WIDTH * 4) #define DOUBLE_TO_26_6(d) ((FT_F26Dot6)((d) * 64.0)) #define DOUBLE_FROM_26_6(t) ((double)(t) / 64.0) #define DOUBLE_TO_16_16(d) ((FT_Fixed)((d) * 65536.0)) #define DOUBLE_FROM_16_16(t) ((double)(t) / 65536.0) unsigned char image[STRIDE*HEIGHT]; static void box_text (cairo_t *cr, const char *utf8, double x, double y) { double line_width; cairo_text_extents_t extents; cairo_save (cr); cairo_text_extents (cr, TEXT, &extents); line_width = cairo_get_line_width (cr); cairo_rectangle (cr, x + extents.x_bearing - line_width, y + extents.y_bearing - line_width, extents.width + 2 * line_width, extents.height + 2 * line_width); cairo_stroke (cr); cairo_move_to (cr, x, y); cairo_show_text (cr, utf8); cairo_move_to (cr, x, y); cairo_text_path (cr, utf8); cairo_set_source_rgb (cr, 1, 0, 0); cairo_set_line_width (cr, 1.0); cairo_stroke (cr); cairo_restore (cr); } static void box_glyphs (cairo_t *cr, cairo_glyph_t *glyphs, int num_glyphs, double x, double y) { int i; double line_width; cairo_text_extents_t extents; cairo_save (cr); cairo_glyph_extents (cr, glyphs, num_glyphs, &extents); line_width = cairo_get_line_width (cr); cairo_rectangle (cr, x + extents.x_bearing - line_width, y + extents.y_bearing - line_width, extents.width + 2 * line_width, extents.height + 2 * line_width); cairo_stroke (cr); for (i=0; i < num_glyphs; i++) { glyphs[i].x += x; glyphs[i].y += y; } cairo_show_glyphs (cr, glyphs, num_glyphs); cairo_glyph_path (cr, glyphs, num_glyphs); cairo_set_source_rgb (cr, 1, 0, 0); cairo_set_line_width (cr, 1.0); cairo_stroke (cr); for (i=0; i < num_glyphs; i++) { glyphs[i].x -= x; glyphs[i].y -= y; } cairo_restore (cr); } int main (void) { int i; cairo_surface_t *surface; cairo_t *cr; cairo_text_extents_t extents; cairo_font_extents_t font_extents; double dx, dy; double height; #define NUM_GLYPHS 10 cairo_glyph_t glyphs[NUM_GLYPHS]; surface = cairo_image_surface_create_for_data (image, CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT, STRIDE); cr = cairo_create (surface); cairo_set_source_rgb (cr, 0., 0., 0.); cairo_set_line_width (cr, 2.0); cairo_save (cr); cairo_rectangle (cr, 0., 0., WIDTH, HEIGHT); cairo_set_source_rgba (cr, 0., 0., 0., 0.); cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE); cairo_fill (cr); cairo_restore (cr); cairo_select_font_face (cr, "sans", 0, 0); cairo_set_font_size (cr, 40); #define XXX_DEMONSTRATE_EXTENTS_BUGS_WHEN_FONT_IS_TRANSFORMED 1 #if XXX_DEMONSTRATE_EXTENTS_BUGS_WHEN_FONT_IS_TRANSFORMED { cairo_matrix_t matrix; cairo_matrix_init_scale (&matrix, 40.0, -40.0); cairo_set_font_matrix (cr, &matrix); } cairo_scale (cr, 1.0, -1.0); cairo_translate (cr, 0.0, - HEIGHT); #endif cairo_font_extents (cr, &font_extents); height = font_extents.height; dx = 0.0; dy = 0.0; for (i=0; i < NUM_GLYPHS; i++) { glyphs[i].index = i + 4; glyphs[i].x = dx; glyphs[i].y = dy; cairo_glyph_extents (cr, &glyphs[i], 1, &extents); dx += extents.x_advance; dy += extents.y_advance; } box_text (cr, TEXT, 10, height); cairo_translate (cr, 0, height); cairo_save (cr); { cairo_translate (cr, 10, height); cairo_rotate (cr, 10 * M_PI / 180.0); box_text (cr, TEXT, 0, 0); } cairo_restore (cr); cairo_translate (cr, 0, 2 * height); cairo_save (cr); { cairo_matrix_t matrix; cairo_matrix_init_identity (&matrix); cairo_matrix_scale (&matrix, 40, -40); cairo_matrix_rotate (&matrix, -10 * M_PI / 180.0); cairo_set_font_matrix (cr, &matrix); } box_text (cr, TEXT, 10, height); cairo_restore (cr); cairo_translate (cr, 0, 2 * height); box_glyphs (cr, glyphs, NUM_GLYPHS, 10, height); cairo_translate (cr, 10, 2 * height); cairo_save (cr); { cairo_rotate (cr, 10 * M_PI / 180.0); box_glyphs (cr, glyphs, NUM_GLYPHS, 0, 0); } cairo_restore (cr); cairo_translate (cr, 0, height); for (i=0; i < NUM_GLYPHS; i++) glyphs[i].y += i * 5; box_glyphs (cr, glyphs, NUM_GLYPHS, 10, height); cairo_surface_write_to_png (surface, "text.png"); cairo_destroy (cr); cairo_surface_destroy (surface); return 0; } @ 1.11 log @ * bevels.c: * caps_joins.c: * hering.c: * outline.c: * snapping.c: * spiral.c: * splines_tolerance.c: * star_and_ring.c: * stars.c: * text-rotate.c: * text.c: Remove stale includes of write_png.h. @ text @d63 1 d139 1 a139 1 #define XXX_DEMONSTRATE_EXTENTS_BUGS_WHEN_FONT_IS_TRANSFORMED 0 d142 3 a144 4 cairo_matrix_t *matrix = cairo_matrix_create (); cairo_matrix_scale (matrix, 1.0, -1.0); cairo_set_font_matrix (cr, matrix); cairo_matrix_destroy (matrix); d181 2 a182 2 cairo_matrix_scale (&matrix, 40, 40); cairo_matrix_rotate (&matrix, 10 * M_PI / 180.0); @ 1.10 log @ * text.c (main): Remove #warning since I figured out what I was doing wrong with cairo_set_font_matrix. @ text @a30 2 #include "write_png.h" @ 1.9 log @ * Makefile: * bevels.c: (set_hex_color), (main): * caps_joins.c: (main), (stroke_v_twice), (draw_caps_joins): * hering.c: (main), (draw_hering): * outline.c: (main), (create_gradient), (draw_outlines): * snapping.c: (snap_point_for_fill), (snap_point_for_stroke), (snap_line_width), (snap_path_for_fill), (snap_path_for_stroke), (draw_nested), (main): * spiral.c: (main), (draw_spiral): * splines_tolerance.c: (main), (draw_splines): * star_and_ring.c: (star_path), (fill_ring), (fill_star), (main): * stars.c: (main), (draw_stars): * text-rotate.c: (main): * text.c: (box_text), (box_glyphs), (main): Update all programs to match recent cairo API changes. @ text @a184 1 #warning it looks like cairo_set_font_matrix is not currently working @ 1.8 log @ * caps_joins.c: * hering.c: * outline.c: * spiral.c: * splines_tolerance.c: * stars.c: * text-rotate.c: * text.c: * write_png.c: * write_png.h: Convert file encodings to utf-8. @ text @d44 1 a44 1 char image[STRIDE*HEIGHT]; d55 1 a55 1 line_width = cairo_current_line_width (cr); d66 1 a66 1 cairo_set_rgb_color (cr, 1, 0, 0); d84 1 a84 1 line_width = cairo_current_line_width (cr); d98 1 a98 1 cairo_set_rgb_color (cr, 1, 0, 0); d113 1 d122 2 a123 1 cr = cairo_create (); a124 2 cairo_set_target_image (cr, image, CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT, STRIDE); d126 3 a128 1 cairo_set_rgb_color (cr, 0., 0., 0.); d133 2 a134 2 cairo_set_alpha (cr, 0.); cairo_set_operator (cr, CAIRO_OPERATOR_SRC); d138 2 a139 2 cairo_select_font (cr, "sans", 0, 0); cairo_scale_font (cr, 40); d145 1 a145 1 cairo_transform_font (cr, matrix); d153 1 a153 1 cairo_current_font_extents (cr, &font_extents); d181 6 a186 4 cairo_matrix_t *matrix = cairo_matrix_create (); cairo_matrix_rotate (matrix, 10 * M_PI / 180.0); cairo_transform_font (cr, matrix); cairo_matrix_destroy (matrix); d207 1 a207 1 write_png_argb32 (image, "text.png", WIDTH, HEIGHT, STRIDE); d211 2 @ 1.7 log @Change to use sans rather then serif font @ text @d2 1 a2 1 * Copyright © 2003 USC, Information Sciences Institute @ 1.6 log @ * text.c, text-rotate.c: Remove FcFini call from newer fontconfig than I have here (2.2.1). May leak memory if valgrind checking. @ text @d136 1 a136 1 cairo_select_font (cr, "serif", 0, 0); @ 1.5 log @ * text.c (box_text): (box_glyphs): Add calls to cairo_text_path and cairo_glyph_path to demonstrate how to draw stroked (outlined) text. @ text @a206 2 FcFini (); @ 1.4 log @ (box_glyphs): (box_text): Track changes in cairo_text_extents_t structure. @ text @d52 2 d57 4 a60 4 x + extents.x_bearing - line_width / 2.0, y + extents.y_bearing - line_width / 2.0, extents.width + line_width, extents.height + line_width); d64 7 a70 1 cairo_show_text (cr, TEXT); d81 2 d86 4 a89 4 x + extents.x_bearing - line_width / 2.0, y + extents.y_bearing - line_width / 2.0, extents.width + line_width, extents.height + line_width); d97 4 d105 2 d127 1 @ 1.3 log @ * text.c: Add boxes to demonstrate use of cairo_text_extents and cairo_glyph_extents. Add several text examples to demonstrate behavior of extents under various transformations. @ text @d55 4 a58 4 x + extents.left_side_bearing - line_width / 2.0, y + extents.ascent - line_width / 2.0, extents.right_side_bearing - extents.left_side_bearing + line_width, -extents.ascent + extents.descent + line_width); d76 4 a79 4 x + extents.left_side_bearing - line_width / 2.0, y + extents.ascent - line_width / 2.0, extents.right_side_bearing - extents.left_side_bearing + line_width, - extents.ascent + extents.descent + line_width); @ 1.2 log @Change text to "hello, world". Fix to generate a .png with transparent background. @ text @d33 4 a36 2 #define WIDTH 80 #define HEIGHT 20 d39 5 d46 47 d96 1 d98 6 d119 66 a184 3 cairo_scale_font (cr, 10); cairo_move_to (cr, 10, 15); cairo_show_text (cr, "hello, world"); d190 2 @ 1.1 log @Add text.c to demonstrate libic bug. @ text @d33 1 a33 1 #define WIDTH 500 d49 6 a54 2 cairo_rectangle (cr, 0, 0, WIDTH, HEIGHT); cairo_set_rgb_color (cr, 1, 1, 1); d56 1 a56 2 cairo_set_rgb_color (cr, 0, 0, 0); d60 1 a60 1 cairo_show_text (cr, "S"); /* Hello World. ABCDEFGHIJKLMOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"); */ @