comparison src/shape_stext.c @ 421:1e48453bb282

Make compute_styled_extents_n_scaled_font() pass the test.
author Thinker K.F. Li <thinker@branda.to>
date Sun, 26 Jul 2009 22:13:36 +0800
parents 485c563514f4
children c6c0d017dc8e
comparison
equal deleted inserted replaced
420:485c563514f4 421:1e48453bb282
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <cairo.h> 2 #include <cairo.h>
3 #include <cairo-ft.h> 3 #include <cairo-ft.h>
4 #include <fontconfig/fontconfig.h> 4 #include <fontconfig/fontconfig.h>
5 #include "mb_shapes.h" 5 #include "mb_shapes.h"
6
7 #ifdef UNITTEST
8 typedef struct _ut_shape {
9 co_aix aggr[6];
10 void (*free)(struct _ut_shape *);
11 } ut_shape_t;
12 #define shape_t ut_shape_t
13
14 #undef sh_get_aggr_matrix
15 #define sh_get_aggr_matrix(sh) (sh)->aggr
16
17 #undef mb_obj_init
18 #define mb_obj_init(o, t)
19 #undef rdman_shape_man
20 #define rdman_shape_man(rdman, sh)
21
22 #define rdman_shape_stext_new ut_rdman_shape_stext_new
23 #define sh_stext_transform ut_sh_stext_transform
24 #define sh_stext_draw ut_sh_stext_draw
25 #define sh_stext_set_text ut_sh_stext_set_text
26 #define sh_stext_set_style ut_sh_stext_set_style
27
28 #endif /* UNITTEST */
6 29
7 #ifndef ASSERT 30 #ifndef ASSERT
8 #define ASSERT(x) 31 #define ASSERT(x)
9 #endif 32 #endif
10 #define OK 0 33 #define OK 0
426 /*! \brief Compute extents of a stext object according style blocks. 449 /*! \brief Compute extents of a stext object according style blocks.
427 * 450 *
428 * It create scaled fonts for style blocks, compute their extents, 451 * It create scaled fonts for style blocks, compute their extents,
429 * and compute where they should be draw acoording advance of style 452 * and compute where they should be draw acoording advance of style
430 * blocks before a style block. 453 * blocks before a style block.
454 *
455 * The scaled font that is created by this function for style blocks
456 * are drawed at origin. So, extents of these blocks are also based
457 * on that the text are drawed at origin of user space. But,
458 * aggreagated extents (sh_stext_t::extents) accounts x/y advances
459 * to create correct extents for full text string with style blocks.
431 */ 460 */
432 static 461 static
433 void compute_styled_extents_n_scaled_font(sh_stext_t *txt_o) { 462 void compute_styled_extents_n_scaled_font(sh_stext_t *txt_o) {
434 mb_text_extents_t sub_extents; 463 mb_text_extents_t sub_extents;
435 const mb_style_blk_t *blk; 464 const mb_style_blk_t *blk;
436 int blk_txt_len; 465 int blk_txt_len;
437 mb_scaled_font_t **scaled_font; 466 mb_scaled_font_t **scaled_font;
438 char *txt, saved; 467 char *txt, saved;
439 co_aix shift_x, shift_y; 468 int i, nscaled;
440 int i;
441 469
442 memset(&txt_o->extents, sizeof(mb_text_extents_t), 0); 470 memset(&txt_o->extents, sizeof(mb_text_extents_t), 0);
443 471
444 blk = txt_o->style_blks; 472 blk = txt_o->style_blks;
473
445 scaled_font = txt_o->scaled_fonts; 474 scaled_font = txt_o->scaled_fonts;
475 nscaled = txt_o->nscaled;
476 for(i = 0; i < nscaled; i++) {
477 scaled_font_free(*scaled_font);
478 scaled_font++;
479 }
480 txt_o->nscaled = 0;
481
482 if(txt_o->maxscaled >= txt_o->nblks)
483 scaled_font = txt_o->scaled_fonts;
484 else {
485 scaled_font = (mb_scaled_font_t **)
486 realloc(txt_o->scaled_fonts,
487 txt_o->nblks * sizeof(mb_scaled_font_t *));
488 ASSERT(scaled_font != NULL);
489
490 txt_o->maxscaled = txt_o->nblks;
491 txt_o->scaled_fonts = scaled_font;
492 }
493
446 txt = (char *)txt_o->txt; 494 txt = (char *)txt_o->txt;
447 for(i = 0; i < txt_o->nblks; i++) { 495 for(i = 0; i < txt_o->nblks; i++) {
448 shift_x = txt_o->x + MBE_GET_X_ADV(&txt_o->extents);
449 shift_y = txt_o->y + MBE_GET_Y_ADV(&txt_o->extents);
450
451 *scaled_font = make_scaled_font_face(txt_o, blk->face, 496 *scaled_font = make_scaled_font_face(txt_o, blk->face,
452 shift_x, shift_y, blk->font_sz); 497 0, 0, blk->font_sz);
453 ASSERT(*scaled_font != NULL); 498 ASSERT(*scaled_font != NULL);
454 499
455 blk_txt_len = compute_utf8_chars_sz(txt, blk->n_chars); 500 blk_txt_len = compute_utf8_chars_sz(txt, blk->n_chars);
456 ASSERT(blk_txt_len != ERR); 501 ASSERT(blk_txt_len != ERR);
457 502
464 509
465 scaled_font++; 510 scaled_font++;
466 blk++; 511 blk++;
467 txt += blk_txt_len; 512 txt += blk_txt_len;
468 } 513 }
514
515 txt_o->nscaled = txt_o->nblks;
469 } 516 }
470 517
471 /* 518 /*
472 * What we have to do in sh_stext_transform() is 519 * What we have to do in sh_stext_transform() is
473 * - computing bounding box for the text, 520 * - computing bounding box for the text,
525 572
526 txt_o->style_blks = new_blks; 573 txt_o->style_blks = new_blks;
527 txt_o->max_nblks = nblks; 574 txt_o->max_nblks = nblks;
528 } 575 }
529 576
530 memcpy(txt_o->style_blks, blks, nblks * sizeof(mb_style_blk_t)); 577 memcpy((mb_style_blk_t *)txt_o->style_blks,
578 blks, nblks * sizeof(mb_style_blk_t));
531 txt_o->nblks = nblks; 579 txt_o->nblks = nblks;
532 580
533 return OK; 581 return OK;
534 } 582 }
535 583
542 mb_font_face_t *face; 590 mb_font_face_t *face;
543 cairo_status_t status; 591 cairo_status_t status;
544 592
545 face = query_font_face("serif", MB_FONT_SLANT_ROMAN, 100); 593 face = query_font_face("serif", MB_FONT_SLANT_ROMAN, 100);
546 CU_ASSERT(face != NULL); 594 CU_ASSERT(face != NULL);
547 status = cairo_font_face_status(face); 595 status = cairo_font_face_status((cairo_font_face_t *)face);
548 CU_ASSERT(status == CAIRO_STATUS_SUCCESS); 596 CU_ASSERT(status == CAIRO_STATUS_SUCCESS);
549 597
550 free_font_face(face); 598 free_font_face(face);
551 } 599 }
552 600
557 mb_scaled_font_t *scaled; 605 mb_scaled_font_t *scaled;
558 cairo_status_t status; 606 cairo_status_t status;
559 607
560 face = query_font_face("serif", MB_FONT_SLANT_ROMAN, 100); 608 face = query_font_face("serif", MB_FONT_SLANT_ROMAN, 100);
561 CU_ASSERT(face != NULL); 609 CU_ASSERT(face != NULL);
562 status = cairo_font_face_status(face); 610 status = cairo_font_face_status((cairo_font_face_t *)face);
563 CU_ASSERT(status == CAIRO_STATUS_SUCCESS); 611 CU_ASSERT(status == CAIRO_STATUS_SUCCESS);
564 612
565 scaled = make_scaled_font_face_matrix(face, matrix); 613 scaled = make_scaled_font_face_matrix(face, matrix);
566 CU_ASSERT(scaled != NULL); 614 CU_ASSERT(scaled != NULL);
567 status = cairo_scaled_font_status((cairo_scaled_font_t *)scaled); 615 status = cairo_scaled_font_status((cairo_scaled_font_t *)scaled);
629 677
630 sz = compute_utf8_chars_sz(str, 9); 678 sz = compute_utf8_chars_sz(str, 9);
631 CU_ASSERT(sz == ERR); 679 CU_ASSERT(sz == ERR);
632 } 680 }
633 681
682 static
683 void test_compute_styled_extents_n_scaled_font(void) {
684 sh_stext_t *txt_o;
685 co_aix *aggr;
686 mb_style_blk_t blks[2];
687 mb_font_face_t *face;
688 mb_text_extents_t *ext;
689 int r;
690
691 txt_o = (sh_stext_t *)rdman_shape_stext_new((redraw_man_t *)NULL,
692 10, 15, "Hello World");
693 CU_ASSERT(txt_o != NULL);
694
695 aggr = txt_o->shape.aggr;
696 aggr[0] = 1;
697 aggr[1] = 0;
698 aggr[2] = 0;
699 aggr[3] = 0;
700 aggr[4] = 1;
701 aggr[5] = 0;
702
703 face = query_font_face("serif", MB_FONT_SLANT_ROMAN, 100);
704 CU_ASSERT(face != NULL);
705
706 blks[0].n_chars = 5;
707 blks[0].face = face;
708 blks[0].font_sz = 10;
709
710 blks[1].n_chars = 4;
711 blks[1].face = face;
712 blks[1].font_sz = 15.5;
713
714 r = sh_stext_set_style((shape_t *)txt_o, blks, 2);
715 CU_ASSERT(r == OK);
716
717 compute_styled_extents_n_scaled_font(txt_o);
718
719 ext = &txt_o->extents;
720 CU_ASSERT(MBE_GET_HEIGHT(ext) > 11);
721 CU_ASSERT(MBE_GET_HEIGHT(ext) < 20);
722 CU_ASSERT(MBE_GET_WIDTH(ext) > 36);
723 CU_ASSERT(MBE_GET_WIDTH(ext) < 72);
724 CU_ASSERT(MBE_GET_X_ADV(ext) > 36);
725 CU_ASSERT(MBE_GET_X_ADV(ext) < 72);
726 CU_ASSERT(MBE_GET_Y_ADV(ext) == 0);
727
728 _rdman_shape_stext_free((shape_t *)txt_o);
729 free_font_face(face);
730 }
731
634 #include <CUnit/Basic.h> 732 #include <CUnit/Basic.h>
635 CU_pSuite get_stext_suite(void) { 733 CU_pSuite get_stext_suite(void) {
636 CU_pSuite suite; 734 CU_pSuite suite;
637 735
638 suite = CU_add_suite("Suite_stext", NULL, NULL); 736 suite = CU_add_suite("Suite_stext", NULL, NULL);
639 CU_ADD_TEST(suite, test_query_font_face); 737 CU_ADD_TEST(suite, test_query_font_face);
640 CU_ADD_TEST(suite, test_make_scaled_font_face_matrix); 738 CU_ADD_TEST(suite, test_make_scaled_font_face_matrix);
641 CU_ADD_TEST(suite, test_compute_text_extents); 739 CU_ADD_TEST(suite, test_compute_text_extents);
642 CU_ADD_TEST(suite, test_extent_extents); 740 CU_ADD_TEST(suite, test_extent_extents);
643 CU_ADD_TEST(suite, test_compute_utf8_chars_sz); 741 CU_ADD_TEST(suite, test_compute_utf8_chars_sz);
742 CU_ADD_TEST(suite, test_compute_styled_extents_n_scaled_font);
644 743
645 return suite; 744 return suite;
646 } 745 }
647 746
648 #endif /* UNITTEST */ 747 #endif /* UNITTEST */