Mercurial > MadButterfly
comparison src/shape_stext.c @ 415:fef1b8076041
Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sun, 26 Jul 2009 15:31:40 +0800 |
parents | bbf036c315be |
children | abc420282c70 |
comparison
equal
deleted
inserted
replaced
414:bbf036c315be | 415:fef1b8076041 |
---|---|
303 | 303 |
304 static | 304 static |
305 int compute_utf8_chars_sz(const char *txt, int n_chars) { | 305 int compute_utf8_chars_sz(const char *txt, int n_chars) { |
306 int i; | 306 int i; |
307 const char *p = txt; | 307 const char *p = txt; |
308 const char *v; | |
308 | 309 |
309 for(i = 0; i < n_chars && *p; i++) { | 310 for(i = 0; i < n_chars && *p; i++) { |
310 if(*p++ & 0x80) | 311 if(!(*p & 0x80)) /* single byte */ |
311 continue; /* single byte */ | |
312 /* multi-bytes */ | |
313 while(*p && ((*p & 0xc0) == 0x80)) | |
314 p++; | 312 p++; |
313 else if((*p & 0xe0) == 0xc0) /* 2 bytes */ | |
314 p += 2; | |
315 else if((*p & 0xf0) == 0xe0) /* 3 bytes */ | |
316 p += 3; | |
317 else if((*p & 0xf8) == 0xf0) /* 4 bytes */ | |
318 p += 4; | |
319 else | |
320 return ERR; | |
315 } | 321 } |
316 if(i < n_chars) | 322 if(i < n_chars) |
317 return ERR; | 323 return ERR; |
324 | |
325 for(v = txt; v != p; v++) | |
326 if(*v == '\x0') | |
327 return ERR; | |
318 | 328 |
319 return p - txt; | 329 return p - txt; |
320 } | 330 } |
321 | 331 |
322 static | 332 static |
579 CU_ASSERT(MBE_GET_Y_BEARING(&ext1) == -14); | 589 CU_ASSERT(MBE_GET_Y_BEARING(&ext1) == -14); |
580 CU_ASSERT(MBE_GET_X_ADV(&ext1) == 53); | 590 CU_ASSERT(MBE_GET_X_ADV(&ext1) == 53); |
581 CU_ASSERT(MBE_GET_Y_ADV(&ext1) == -8); | 591 CU_ASSERT(MBE_GET_Y_ADV(&ext1) == -8); |
582 } | 592 } |
583 | 593 |
594 static | |
595 void test_compute_utf8_chars_sz(void) { | |
596 const char *str = "\xe4\xb8\xad\xe6\x96\x87test\xe6\xb8\xac\xe8\xa9\xa6"; | |
597 int sz; | |
598 | |
599 sz = compute_utf8_chars_sz(str, 4); | |
600 CU_ASSERT(sz == 8); | |
601 | |
602 sz = compute_utf8_chars_sz(str, 9); | |
603 CU_ASSERT(sz == ERR); | |
604 } | |
605 | |
584 #include <CUnit/Basic.h> | 606 #include <CUnit/Basic.h> |
585 CU_pSuite get_stext_suite(void) { | 607 CU_pSuite get_stext_suite(void) { |
586 CU_pSuite suite; | 608 CU_pSuite suite; |
587 | 609 |
588 suite = CU_add_suite("Suite_stext", NULL, NULL); | 610 suite = CU_add_suite("Suite_stext", NULL, NULL); |
589 CU_ADD_TEST(suite, test_query_font_face); | 611 CU_ADD_TEST(suite, test_query_font_face); |
590 CU_ADD_TEST(suite, test_make_scaled_font_face_matrix); | 612 CU_ADD_TEST(suite, test_make_scaled_font_face_matrix); |
591 CU_ADD_TEST(suite, test_compute_text_extents); | 613 CU_ADD_TEST(suite, test_compute_text_extents); |
592 CU_ADD_TEST(suite, test_extent_extents); | 614 CU_ADD_TEST(suite, test_extent_extents); |
615 CU_ADD_TEST(suite, test_compute_utf8_chars_sz); | |
593 | 616 |
594 return suite; | 617 return suite; |
595 } | 618 } |
596 | 619 |
597 #endif /* UNITTEST */ | 620 #endif /* UNITTEST */ |