[cairo-commit] cairo-demo/png ChangeLog,1.1,1.2 text.c,1.2,1.3

Carl Worth commit at pdx.freedesktop.org
Mon Dec 15 18:34:15 PST 2003


Committed by: cworth

Update of /cvs/cairo/cairo-demo/png
In directory pdx:/tmp/cvs-serv24143

Modified Files:
	ChangeLog text.c 
Log Message:

        * 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.


Index: ChangeLog
===================================================================
RCS file: /cvs/cairo/cairo-demo/png/ChangeLog,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** ChangeLog	11 Dec 2003 15:09:12 -0000	1.1
--- ChangeLog	16 Dec 2003 02:34:12 -0000	1.2
***************
*** 1,2 ****
--- 1,8 ----
+ 2003-12-15  Carl Worth  <cworth at isi.edu>
+ 
+ 	* 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.
+ 
  2003-12-11  Carl Worth  <cworth at east.isi.edu>
  

Index: text.c
===================================================================
RCS file: /cvs/cairo/cairo-demo/png/text.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** text.c	25 Oct 2003 00:51:26 -0000	1.2
--- text.c	16 Dec 2003 02:34:12 -0000	1.3
***************
*** 31,44 ****
  #include "write_png.h"
  
! #define WIDTH 80
! #define HEIGHT 20
  #define STRIDE (WIDTH * 4)
  
  char image[STRIDE*HEIGHT];
  
  int
  main (void)
  {
      cairo_t *cr;
  
      cr = cairo_create ();
--- 31,105 ----
  #include "write_png.h"
  
! #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)
+ 
  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_text_extents (cr, TEXT, &extents);
+     line_width = cairo_current_line_width (cr);
+     cairo_rectangle (cr,
+ 		     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);
+     cairo_stroke (cr);
+ 
+     cairo_move_to (cr, x, y);
+     cairo_show_text (cr, TEXT);
+ }
+ 
+ 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_glyph_extents (cr, glyphs, num_glyphs, &extents);
+     line_width = cairo_current_line_width (cr);
+     cairo_rectangle (cr,
+ 		     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);
+     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);
+     for (i=0; i < num_glyphs; i++) {
+ 	glyphs[i].x -= x;
+ 	glyphs[i].y -= y;
+     }
+ }
+ 
  int
  main (void)
  {
+     int i;
      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];
  
      cr = cairo_create ();
***************
*** 56,62 ****
      cairo_restore (cr);
  
!     cairo_scale_font (cr, 10);
!     cairo_move_to (cr, 10, 15);
!     cairo_show_text (cr, "hello, world");
  
      write_png_argb32 (image, "text.png", WIDTH, HEIGHT, STRIDE);
--- 117,186 ----
      cairo_restore (cr);
  
!     cairo_select_font (cr, "serif", 0, 0);
!     cairo_scale_font (cr, 40);
! #define XXX_DEMONSTRATE_EXTENTS_BUGS_WHEN_FONT_IS_TRANSFORMED 0
! #if XXX_DEMONSTRATE_EXTENTS_BUGS_WHEN_FONT_IS_TRANSFORMED
!     {
! 	cairo_matrix_t *matrix = cairo_matrix_create ();
! 	cairo_matrix_scale (matrix, 1.0, -1.0);
! 	cairo_transform_font (cr, matrix);
! 	cairo_matrix_destroy (matrix);
!     }
! 
!     cairo_scale (cr, 1.0, -1.0);
!     cairo_translate (cr, 0.0, - HEIGHT);
! #endif
! 
!     cairo_current_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_create ();
! 	cairo_matrix_rotate (matrix, 10 * M_PI / 180.0);
! 	cairo_transform_font (cr, matrix);
! 	cairo_matrix_destroy (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);
  
      write_png_argb32 (image, "text.png", WIDTH, HEIGHT, STRIDE);
***************
*** 64,67 ****
--- 188,193 ----
      cairo_destroy (cr);
  
+     FcFini ();
+ 
      return 0;
  }





More information about the cairo-commit mailing list