comparison src/shape_stext.c @ 427:8f900da42eed

Make sh_text_draw() passes first test case.
author Thinker K.F. Li <thinker@branda.to>
date Tue, 28 Jul 2009 15:11:42 +0800
parents aa320386072c
children 9d5506968efb
comparison
equal deleted inserted replaced
426:aa320386072c 427:8f900da42eed
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 9
10 #define NO_DOX(x) x
10 #define UT_FAKE(r, x, param) \ 11 #define UT_FAKE(r, x, param) \
11 r x param {} 12 r x param {}
12 13
13 UT_FAKE(void, sh_stext_transform, (shape_t *shape)); 14 UT_FAKE(void, sh_stext_transform, (shape_t *shape));
14 UT_FAKE(void, sh_stext_draw, (shape_t *shape, cairo_t *cr)); 15 UT_FAKE(void, sh_stext_draw, (shape_t *shape, cairo_t *cr));
40 #define rdman_shape_stext_new ut_rdman_shape_stext_new 41 #define rdman_shape_stext_new ut_rdman_shape_stext_new
41 #define sh_stext_transform ut_sh_stext_transform 42 #define sh_stext_transform ut_sh_stext_transform
42 #define sh_stext_draw ut_sh_stext_draw 43 #define sh_stext_draw ut_sh_stext_draw
43 #define sh_stext_set_text ut_sh_stext_set_text 44 #define sh_stext_set_text ut_sh_stext_set_text
44 #define sh_stext_set_style ut_sh_stext_set_style 45 #define sh_stext_set_style ut_sh_stext_set_style
46
47 #undef cairo_get_scaled_font
48 #define cairo_get_scaled_font(cr) ((cairo_scaled_font_t *)NULL)
49 #undef cairo_set_scaled_font
50 #define cairo_set_scaled_font(cr, scaled) ((cairo_scaled_font_t *)NULL)
51 #undef cairo_scaled_font_reference
52 #define cairo_scaled_font_reference(x)
53 #define MAX_MOVE 32
54 NO_DOX(static co_aix move_xys[MAX_MOVE][2]);
55 NO_DOX(static int move_cnt = 0);
56 #undef cairo_move_to
57 #define cairo_move_to(cr, x, y) \
58 do { \
59 move_xys[move_cnt][0] = x; \
60 move_xys[move_cnt++][1] = y; \
61 } while(0)
62 #undef cairo_show_text
63 #define cairo_show_text(cr, txt)
64 #undef cairo_scaled_font_destroy
65 #define cairo_scaled_font_destroy(scaled) \
66 if(scaled != NULL) cairo_scaled_font_destroy(scaled)
45 67
46 #endif /* UNITTEST */ 68 #endif /* UNITTEST */
47 69
48 #ifndef ASSERT 70 #ifndef ASSERT
49 #define ASSERT(x) 71 #define ASSERT(x)
270 } 292 }
271 293
272 static 294 static
273 void mb_text_extents_free(mb_text_extents_t *extents) { 295 void mb_text_extents_free(mb_text_extents_t *extents) {
274 free(extents); 296 free(extents);
297 }
298
299 static
300 void draw_text_scaled(cairo_t *cr, const char *txt, int tlen,
301 mb_scaled_font_t *scaled, co_aix x, co_aix y) {
302 cairo_scaled_font_t *saved_scaled;
303 int total_tlen;
304 const char *buf;
305
306 total_tlen = strlen(txt);
307 if(total_tlen > tlen)
308 buf = strndup(txt, tlen);
309 else
310 buf = txt;
311
312 saved_scaled = cairo_get_scaled_font(cr);
313 cairo_scaled_font_reference(saved_scaled);
314 cairo_set_scaled_font(cr, (cairo_scaled_font_t *)scaled);
315
316 cairo_move_to(cr, x, y);
317 cairo_show_text(cr, buf);
318
319 cairo_set_scaled_font(cr, saved_scaled);
320 cairo_scaled_font_destroy(saved_scaled);
321
322 if(total_tlen > tlen)
323 free((char *)buf);
275 } 324 }
276 325
277 /* @} */ 326 /* @} */
278 327
279 /*! \brief Query and return a font face for a specified attribute vector. 328 /*! \brief Query and return a font face for a specified attribute vector.
564 area->h = MBE_GET_HEIGHT(ext); 613 area->h = MBE_GET_HEIGHT(ext);
565 } 614 }
566 615
567 void sh_stext_draw(shape_t *shape, cairo_t *cr) { 616 void sh_stext_draw(shape_t *shape, cairo_t *cr) {
568 sh_stext_t *txt_o = (sh_stext_t *)shape; 617 sh_stext_t *txt_o = (sh_stext_t *)shape;
618 co_aix x, y;
619 const char *txt;
620 scaled_fonts_lst_t *scaled_fonts;
621 mb_scaled_font_t *scaled;
622 style_blks_lst_t *style_blks;
623 mb_style_blk_t *blk;
624 mb_text_extents_t *ext;
625 int blk_txt_len;
626 int i;
569 627
570 ASSERT(txt_o != NULL); 628 ASSERT(txt_o != NULL);
629
630 x = txt_o->dx;
631 y = txt_o->dy;
632 txt = txt_o->txt;
633 scaled_fonts = &txt_o->scaled_fonts;
634 style_blks = &txt_o->style_blks;
635 ext = txt_o->sub_exts.ds;
636
637 for(i = 0; i < scaled_fonts->num; i++) {
638 scaled = scaled_fonts->ds[i];
639 blk = style_blks->ds + i;
640 blk_txt_len = compute_utf8_chars_sz(txt, blk->n_chars);
641 draw_text_scaled(cr, txt, blk_txt_len, scaled, x, y);
642
643 x += MBE_GET_X_ADV(ext);
644 y += MBE_GET_Y_ADV(ext);
645 txt += blk_txt_len;
646 }
571 } 647 }
572 648
573 int sh_stext_set_text(shape_t *shape, const char *txt) { 649 int sh_stext_set_text(shape_t *shape, const char *txt) {
574 sh_stext_t *txt_o = (sh_stext_t *)shape; 650 sh_stext_t *txt_o = (sh_stext_t *)shape;
575 char *new_txt; 651 char *new_txt;
820 896
821 _rdman_shape_stext_free((shape_t *)txt_o); 897 _rdman_shape_stext_free((shape_t *)txt_o);
822 free_font_face(face); 898 free_font_face(face);
823 } 899 }
824 900
901 static
902 void test_sh_stext_draw(void) {
903 sh_stext_t *txt_o;
904 mb_style_blk_t blks[2];
905 co_aix *aggr;
906 mb_font_face_t *face;
907 area_t *area;
908 int r;
909
910 txt_o = (sh_stext_t *)rdman_shape_stext_new(NULL, 100, 50, "hello world");
911 CU_ASSERT(txt_o != NULL);
912
913 aggr = txt_o->shape.aggr;
914 aggr[0] = 2;
915 aggr[1] = 0;
916 aggr[2] = 0;
917 aggr[3] = 0;
918 aggr[4] = 1;
919 aggr[5] = 0;
920
921 face = query_font_face("serif", MB_FONT_SLANT_ROMAN, 100);
922 CU_ASSERT(face != NULL);
923
924 blks[0].n_chars = 5;
925 blks[0].face = face;
926 blks[0].font_sz = 10;
927
928 blks[1].n_chars = 6;
929 blks[1].face = face;
930 blks[1].font_sz = 15.5;
931
932 r = sh_stext_set_style((shape_t *)txt_o, blks, 2);
933 CU_ASSERT(r == OK);
934
935 sh_stext_transform((shape_t *)txt_o);
936
937 sh_stext_draw((shape_t *)txt_o, NULL);
938 CU_ASSERT(move_cnt == 2);
939 CU_ASSERT(move_xys[0][0] == 200);
940 CU_ASSERT(move_xys[0][1] == 50);
941 CU_ASSERT(move_xys[1][0] >= 240 && move_xys[1][0] < 270);
942 CU_ASSERT(move_xys[1][1] == 50);
943
944 _rdman_shape_stext_free((shape_t *)txt_o);
945 free_font_face(face);
946 }
947
825 #include <CUnit/Basic.h> 948 #include <CUnit/Basic.h>
826 CU_pSuite get_stext_suite(void) { 949 CU_pSuite get_stext_suite(void) {
827 CU_pSuite suite; 950 CU_pSuite suite;
828 951
829 suite = CU_add_suite("Suite_stext", NULL, NULL); 952 suite = CU_add_suite("Suite_stext", NULL, NULL);
832 CU_ADD_TEST(suite, test_compute_text_extents); 955 CU_ADD_TEST(suite, test_compute_text_extents);
833 CU_ADD_TEST(suite, test_extent_extents); 956 CU_ADD_TEST(suite, test_extent_extents);
834 CU_ADD_TEST(suite, test_compute_utf8_chars_sz); 957 CU_ADD_TEST(suite, test_compute_utf8_chars_sz);
835 CU_ADD_TEST(suite, test_compute_styled_extents_n_scaled_font); 958 CU_ADD_TEST(suite, test_compute_styled_extents_n_scaled_font);
836 CU_ADD_TEST(suite, test_sh_stext_transform); 959 CU_ADD_TEST(suite, test_sh_stext_transform);
960 CU_ADD_TEST(suite, test_sh_stext_draw);
837 961
838 return suite; 962 return suite;
839 } 963 }
840 964
841 #endif /* UNITTEST */ 965 #endif /* UNITTEST */