comparison src/shape_stext.c @ 424:585baa462778

Make sh_stext_transform() pass first test case for him.
author Thinker K.F. Li <thinker@branda.to>
date Tue, 28 Jul 2009 15:11:42 +0800
parents 3ba48126c49c
children 09a66063b25d
comparison
equal deleted inserted replaced
423:3ba48126c49c 424:585baa462778
4 #include <fontconfig/fontconfig.h> 4 #include <fontconfig/fontconfig.h>
5 #include "mb_shapes.h" 5 #include "mb_shapes.h"
6 #include "mb_tools.h" 6 #include "mb_tools.h"
7 7
8 #ifdef UNITTEST 8 #ifdef UNITTEST
9 typedef struct _ut_area {
10 co_aix x, y;
11 co_aix w, h;
12 } ut_area_t;
13 #define area_t ut_area_t
14
9 typedef struct _ut_shape { 15 typedef struct _ut_shape {
10 co_aix aggr[6]; 16 co_aix aggr[6];
11 void (*free)(struct _ut_shape *); 17 void (*free)(struct _ut_shape *);
18 ut_area_t area;
12 } ut_shape_t; 19 } ut_shape_t;
13 #define shape_t ut_shape_t 20 #define shape_t ut_shape_t
14 21
15 #undef sh_get_aggr_matrix 22 #undef sh_get_aggr_matrix
16 #define sh_get_aggr_matrix(sh) (sh)->aggr 23 #define sh_get_aggr_matrix(sh) (sh)->aggr
24
25 #undef sh_get_area
26 #define sh_get_area(sh) (&(sh)->area)
17 27
18 #undef mb_obj_init 28 #undef mb_obj_init
19 #define mb_obj_init(o, t) 29 #define mb_obj_init(o, t)
20 #undef rdman_shape_man 30 #undef rdman_shape_man
21 #define rdman_shape_man(rdman, sh) 31 #define rdman_shape_man(rdman, sh)
293 typedef struct _sh_stext { 303 typedef struct _sh_stext {
294 shape_t shape; 304 shape_t shape;
295 const char *txt; /*!< \brief Text to be showed */ 305 const char *txt; /*!< \brief Text to be showed */
296 style_blks_lst_t style_blks; 306 style_blks_lst_t style_blks;
297 co_aix x, y; 307 co_aix x, y;
308 co_aix dx, dy;
298 scaled_fonts_lst_t scaled_fonts; 309 scaled_fonts_lst_t scaled_fonts;
299 mb_text_extents_t extents; 310 mb_text_extents_t extents;
300 } sh_stext_t; 311 } sh_stext_t;
301 312
302 static 313 static
450 * The scaled font that is created by this function for style blocks 461 * The scaled font that is created by this function for style blocks
451 * are drawed at origin. So, extents of these blocks are also based 462 * are drawed at origin. So, extents of these blocks are also based
452 * on that the text are drawed at origin of user space. But, 463 * on that the text are drawed at origin of user space. But,
453 * aggreagated extents (sh_stext_t::extents) accounts x/y advances 464 * aggreagated extents (sh_stext_t::extents) accounts x/y advances
454 * to create correct extents for full text string with style blocks. 465 * to create correct extents for full text string with style blocks.
466 *
467 * This function assumes texts are always drawed at 0, 0. So,
468 * transform function should adjust x and y bearing corresponding
469 * x, y of text to compute area.
455 */ 470 */
456 static 471 static
457 void compute_styled_extents_n_scaled_font(sh_stext_t *txt_o) { 472 void compute_styled_extents_n_scaled_font(sh_stext_t *txt_o) {
458 mb_text_extents_t sub_extents; 473 mb_text_extents_t sub_extents;
459 mb_style_blk_t *blk; 474 mb_style_blk_t *blk;
500 * What we have to do in sh_stext_transform() is 515 * What we have to do in sh_stext_transform() is
501 * - computing bounding box for the text, 516 * - computing bounding box for the text,
502 * - computing offset x,y for the text of style blocks, 517 * - computing offset x,y for the text of style blocks,
503 * - free old scaled fonts, and 518 * - free old scaled fonts, and
504 * - making scaled fonts for style blocks. 519 * - making scaled fonts for style blocks.
520 *
521 * Extents of style blocks are computed with x,y at 0,0.
522 * So, we should add x,y, after transforming by the aggreagated matrix,
523 * to output area.
505 */ 524 */
506 void sh_stext_transform(shape_t *shape) { 525 void sh_stext_transform(shape_t *shape) {
507 sh_stext_t *txt_o = (sh_stext_t *)shape; 526 sh_stext_t *txt_o = (sh_stext_t *)shape;
527 area_t *area;
528 mb_text_extents_t *ext;
529 co_aix *aggr;
508 530
509 ASSERT(txt_o != NULL); 531 ASSERT(txt_o != NULL);
532
533 aggr = sh_get_aggr_matrix(shape);
534
535 txt_o->dx = txt_o->x;
536 txt_o->dy = txt_o->y;
537 matrix_trans_pos(aggr, &txt_o->dx, &txt_o->dy);
538
510 compute_styled_extents_n_scaled_font(txt_o); 539 compute_styled_extents_n_scaled_font(txt_o);
540 ext = &txt_o->extents;
541
542 area = sh_get_area(shape);
543 area->x = MBE_GET_X_BEARING(ext) + txt_o->dx;
544 area->y = MBE_GET_Y_BEARING(ext) + txt_o->dy;
545 area->w = MBE_GET_WIDTH(ext);
546 area->h = MBE_GET_HEIGHT(ext);
511 } 547 }
512 548
513 void sh_stext_draw(shape_t *shape, cairo_t *cr) { 549 void sh_stext_draw(shape_t *shape, cairo_t *cr) {
514 sh_stext_t *txt_o = (sh_stext_t *)shape; 550 sh_stext_t *txt_o = (sh_stext_t *)shape;
515 551
720 756
721 _rdman_shape_stext_free((shape_t *)txt_o); 757 _rdman_shape_stext_free((shape_t *)txt_o);
722 free_font_face(face); 758 free_font_face(face);
723 } 759 }
724 760
761 static
762 void test_sh_stext_transform(void) {
763 sh_stext_t *txt_o;
764 mb_style_blk_t blks[2];
765 co_aix *aggr;
766 mb_font_face_t *face;
767 area_t *area;
768 int r;
769
770 txt_o = (sh_stext_t *)rdman_shape_stext_new(NULL, 100, 50, "hello world");
771 CU_ASSERT(txt_o != NULL);
772
773 aggr = txt_o->shape.aggr;
774 aggr[0] = 2;
775 aggr[1] = 0;
776 aggr[2] = 0;
777 aggr[3] = 0;
778 aggr[4] = 1;
779 aggr[5] = 0;
780
781 face = query_font_face("serif", MB_FONT_SLANT_ROMAN, 100);
782 CU_ASSERT(face != NULL);
783
784 blks[0].n_chars = 5;
785 blks[0].face = face;
786 blks[0].font_sz = 10;
787
788 blks[1].n_chars = 4;
789 blks[1].face = face;
790 blks[1].font_sz = 15.5;
791
792 r = sh_stext_set_style((shape_t *)txt_o, blks, 2);
793 CU_ASSERT(r == OK);
794
795 sh_stext_transform((shape_t *)txt_o);
796
797 area = sh_get_area((shape_t *)txt_o);
798 CU_ASSERT(area->x >= 200 && area->x < 220);
799 CU_ASSERT(area->y >= 40 && area->y < 50);
800 CU_ASSERT(area->w >= 80 && area->w < 120);
801 CU_ASSERT(area->h >= 8 && area->h < 12);
802
803 _rdman_shape_stext_free((shape_t *)txt_o);
804 free_font_face(face);
805 }
806
725 #include <CUnit/Basic.h> 807 #include <CUnit/Basic.h>
726 CU_pSuite get_stext_suite(void) { 808 CU_pSuite get_stext_suite(void) {
727 CU_pSuite suite; 809 CU_pSuite suite;
728 810
729 suite = CU_add_suite("Suite_stext", NULL, NULL); 811 suite = CU_add_suite("Suite_stext", NULL, NULL);
731 CU_ADD_TEST(suite, test_make_scaled_font_face_matrix); 813 CU_ADD_TEST(suite, test_make_scaled_font_face_matrix);
732 CU_ADD_TEST(suite, test_compute_text_extents); 814 CU_ADD_TEST(suite, test_compute_text_extents);
733 CU_ADD_TEST(suite, test_extent_extents); 815 CU_ADD_TEST(suite, test_extent_extents);
734 CU_ADD_TEST(suite, test_compute_utf8_chars_sz); 816 CU_ADD_TEST(suite, test_compute_utf8_chars_sz);
735 CU_ADD_TEST(suite, test_compute_styled_extents_n_scaled_font); 817 CU_ADD_TEST(suite, test_compute_styled_extents_n_scaled_font);
818 CU_ADD_TEST(suite, test_sh_stext_transform);
736 819
737 return suite; 820 return suite;
738 } 821 }
739 822
740 #endif /* UNITTEST */ 823 #endif /* UNITTEST */