annotate 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
rev   line source
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
1 #include <stdio.h>
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
2 #include <cairo.h>
394
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
3 #include <cairo-ft.h>
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
4 #include <fontconfig/fontconfig.h>
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5 #include "mb_shapes.h"
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
6
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
7 #ifndef ASSERT
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
8 #define ASSERT(x)
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
9 #endif
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 #define OK 0
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11 #define ERR -1
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12
407
92a459a1c5aa more comment
Thinker K.F. Li <thinker@branda.to>
parents: 403
diff changeset
13 /*! \page stext Simple Text
92a459a1c5aa more comment
Thinker K.F. Li <thinker@branda.to>
parents: 403
diff changeset
14 *
92a459a1c5aa more comment
Thinker K.F. Li <thinker@branda.to>
parents: 403
diff changeset
15 * A sh_stext_t is broken into fragments. Each fragment comprises the text
92a459a1c5aa more comment
Thinker K.F. Li <thinker@branda.to>
parents: 403
diff changeset
16 * and the styles applied on the fragement. The styles determines font face
92a459a1c5aa more comment
Thinker K.F. Li <thinker@branda.to>
parents: 403
diff changeset
17 * used to show the text. The fragment of text with styles is called
92a459a1c5aa more comment
Thinker K.F. Li <thinker@branda.to>
parents: 403
diff changeset
18 * styled block.
92a459a1c5aa more comment
Thinker K.F. Li <thinker@branda.to>
parents: 403
diff changeset
19 */
92a459a1c5aa more comment
Thinker K.F. Li <thinker@branda.to>
parents: 403
diff changeset
20
396
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
21 /*! \defgroup fontconfig_freetype Fontconfig and FreeType Layer.
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
22 *
411
1633551e495d Add doc
Thinker K.F. Li <thinker@branda.to>
parents: 410
diff changeset
23 * This layer implements a font provider to rest of the system.
396
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
24 * It bases on fontconfig and FreeType supporting of Cairo.
411
1633551e495d Add doc
Thinker K.F. Li <thinker@branda.to>
parents: 410
diff changeset
25 * If you want to provide stext with technologies other than fontconfig and
1633551e495d Add doc
Thinker K.F. Li <thinker@branda.to>
parents: 410
diff changeset
26 * FreeType, just replace this layer with the implmenetation that you want.
396
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
27 *
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
28 * @{
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
29 */
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
30 /*! \brief Stakeholder of scaled font.
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
31 *
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
32 * It is actually a cairo_scaled_font_t, now. But, it should not be
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
33 * noticed by out-siders. Only \ref fontconfig_freetype
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
34 * should known it.
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
35 */
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
36 typedef struct _mb_scaled_font mb_scaled_font_t;
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
37
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
38 /*! \brief Stakeholder of scaled font.
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
39 *
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
40 * Although, mb_text_extents_t is defined as a cairo_scaled_font_t, but
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
41 * programmers should assume it is opague.
407
92a459a1c5aa more comment
Thinker K.F. Li <thinker@branda.to>
parents: 403
diff changeset
42 *
92a459a1c5aa more comment
Thinker K.F. Li <thinker@branda.to>
parents: 403
diff changeset
43 * An extents is the span of showing a fragement of text on the output device.
92a459a1c5aa more comment
Thinker K.F. Li <thinker@branda.to>
parents: 403
diff changeset
44 * It includes x and y advance of cursor after showinng the text.
92a459a1c5aa more comment
Thinker K.F. Li <thinker@branda.to>
parents: 403
diff changeset
45 * The cursor maybe not really existed. But, the advance is computed as
92a459a1c5aa more comment
Thinker K.F. Li <thinker@branda.to>
parents: 403
diff changeset
46 * the cursor existed. It also includes width and height of the text.
92a459a1c5aa more comment
Thinker K.F. Li <thinker@branda.to>
parents: 403
diff changeset
47 * The bearing of a styled block is the left-top corner of the bounding box.
92a459a1c5aa more comment
Thinker K.F. Li <thinker@branda.to>
parents: 403
diff changeset
48 * The bounding box of a styled block is the minimal rectangle, on the
92a459a1c5aa more comment
Thinker K.F. Li <thinker@branda.to>
parents: 403
diff changeset
49 * output device, that can contain the text. The bearing is related to
92a459a1c5aa more comment
Thinker K.F. Li <thinker@branda.to>
parents: 403
diff changeset
50 * the base line for an extents.
396
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
51 */
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
52 typedef cairo_text_extents_t mb_text_extents_t;
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
53
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
54 #define MBE_GET_X_ADV(ext) ((ext)->x_advance)
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
55 #define MBE_GET_Y_ADV(ext) ((ext)->y_advance)
397
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
56 #define MBE_GET_X_BEARING(ext) ((ext)->x_bearing)
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
57 #define MBE_GET_Y_BEARING(ext) ((ext)->y_bearing)
396
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
58 #define MBE_GET_WIDTH(ext) ((ext)->width)
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
59 #define MBE_GET_HEIGHT(ext) ((ext)->height)
403
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
60 #define MBE_SET_X_ADV(ext, v) do { ((ext)->x_advance) = v; } while(0)
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
61 #define MBE_SET_Y_ADV(ext, v) do { ((ext)->y_advance) = v; } while(0)
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
62 #define MBE_SET_X_BEARING(ext, v) do { ((ext)->x_bearing) = v; } while(0)
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
63 #define MBE_SET_Y_BEARING(ext, v) do { ((ext)->y_bearing) = v; } while(0)
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
64 #define MBE_SET_WIDTH(ext, v) do { ((ext)->width) = v; } while(0)
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
65 #define MBE_SET_HEIGHT(ext, v) do { ((ext)->height) = v; } while(0)
396
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
66
395
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
67 /*! \brief Find out a font pattern.
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
68 *
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
69 * This function use fontconfig to decide a font file in pattern. It can
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
70 * replaced by other mechanism if you think it is not what you want.
412
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
71 *
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
72 * \param slant make font prune if it it non-zero.
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
73 * \param weight make font normal if it is 100.
395
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
74 */
394
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
75 static
395
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
76 FcPattern *query_font_pattern(const char *family, int slant, int weight) {
413
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
77 FcPattern *ptn, *p, *fn_ptn;
394
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
78 FcValue val;
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
79 FcConfig *cfg;
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
80 FcBool r;
413
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
81 FcResult result;
394
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
82 static int slant_map[] = { /* from MB_FONT_SLANT_* to FC_SLANT_* */
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
83 FC_SLANT_ROMAN,
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
84 FC_SLANT_ROMAN,
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
85 FC_SLANT_ITALIC,
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
86 FC_SLANT_OBLIQUE};
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
87
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
88 cfg = FcConfigGetCurrent();
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
89 ptn = FcPatternCreate();
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
90 p = FcPatternCreate();
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
91 if(ptn == NULL || p == NULL)
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
92 goto err;
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
93
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
94 val.type = FcTypeString;
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
95 val.u.s = family;
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
96 FcPatternAdd(ptn, "family", val, FcTrue);
394
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
97
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
98 val.type = FcTypeInteger;
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
99 val.u.i = slant_map[slant];
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
100 FcPatternAdd(ptn, "slant", val, FcTrue);
394
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
101
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
102 val.type = FcTypeInteger;
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
103 val.u.i = weight;
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
104 FcPatternAdd(ptn, "weight", val, FcTrue);
394
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
105
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
106 r = FcConfigSubstituteWithPat(cfg, ptn, NULL, FcMatchPattern);
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
107 if(!r)
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
108 goto err;
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
109
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
110 r = FcConfigSubstituteWithPat(cfg, p, ptn, FcMatchFont);
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
111 if(!r)
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
112 goto err;
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
113
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
114 FcDefaultSubstitute(p);
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
115
413
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
116 fn_ptn = FcFontMatch(cfg, p, &result);
394
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
117
413
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
118 /* It is supposed to return FcResultMatch. But, it is no, now.
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
119 * I don't know why. Someone should figure out the issue.
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
120 */
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
121 #if 0
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
122 if(result != FcResultMatch) {
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
123 printf("%d %d\n", result, FcResultMatch);
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
124 goto err;
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
125 }
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
126 #endif
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
127 if(fn_ptn == NULL)
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
128 goto err;
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
129
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
130 FcPatternDestroy(ptn);
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
131 FcPatternDestroy(p);
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
132
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
133 return fn_ptn;
394
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
134
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
135 err:
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
136 if(ptn)
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
137 FcPatternDestroy(ptn);
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
138 if(p)
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
139 FcPatternDestroy(p);
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
140 return NULL;
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
141
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
142 }
b3d5ce48670a Use fontconfig to find out a font for a family/slant/weight pattern
Thinker K.F. Li <thinker@branda.to>
parents: 393
diff changeset
143
395
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
144 /*! \brief Find out a font face for a pattern specified.
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
145 *
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
146 * The pattern, here, is a vector of family, slant, and weight.
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
147 * This function base on fontconfig and cairo FreeType font supporting.
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
148 * You can replace this function with other font mechanisms.
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
149 */
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
150 static
396
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
151 mb_font_face_t *query_font_face(const char *family, int slant, int weight) {
395
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
152 cairo_font_face_t *cface;
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
153 FcPattern *ptn;
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
154
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
155 ptn = query_font_pattern(family, slant, weight);
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
156 cface = cairo_ft_font_face_create_for_pattern(ptn);
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
157 FcPatternDestroy(ptn);
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
158
396
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
159 return (mb_font_face_t *)cface;
395
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
160 }
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
161
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
162 static
396
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
163 void free_font_face(mb_font_face_t *face) {
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
164 ASSERT(face == NULL);
395
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
165
396
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
166 cairo_font_face_destroy((cairo_font_face_t *)face);
395
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
167 }
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
168
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
169 /*! \brief This is scaled font for specified size and extent.
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
170 *
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
171 * Font face only specified which font would be used to draw text
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
172 * message. But, it also need to scale glyphs to specified size and
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
173 * rotation. This function return a scaled font specified by a
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
174 * matrix that transform glyph from design space of the font to
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
175 * user space of cairo surface.
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
176 */
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
177 static
397
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
178 mb_scaled_font_t *make_scaled_font_face_matrix(mb_font_face_t *face,
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
179 co_aix *matrix) {
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
180 cairo_scaled_font_t *scaled_font;
395
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
181 cairo_matrix_t font_matrix;
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
182 static cairo_matrix_t id = {
395
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
183 1, 0,
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
184 0, 1,
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
185 0, 0
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
186 };
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
187 static cairo_font_options_t *opt = NULL;
413
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
188
395
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
189 ASSERT(matrix != NULL);
413
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
190
395
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
191 if(opt == NULL) {
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
192 opt = cairo_font_options_create();
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
193 if(opt == NULL)
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
194 return NULL;
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
195 }
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
196
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
197 font_matrix.xx = *matrix++;
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
198 font_matrix.xy = *matrix++;
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
199 font_matrix.x0 = *matrix++;
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
200 font_matrix.yx = *matrix++;
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
201 font_matrix.yy = *matrix++;
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
202 font_matrix.y0 = *matrix;
396
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
203 scaled_font = cairo_scaled_font_create((cairo_font_face_t *)face,
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
204 &font_matrix,
395
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
205 &id, opt);
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
206
396
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
207 return (mb_scaled_font_t *)scaled_font;
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
208 }
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
209
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
210 static
397
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
211 void scaled_font_free(mb_scaled_font_t *scaled_font) {
396
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
212 cairo_scaled_font_destroy((cairo_scaled_font_t *)scaled_font);
395
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
213 }
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
214
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
215 static
397
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
216 void compute_text_extents(mb_scaled_font_t *scaled_font, const char *txt,
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
217 mb_text_extents_t *extents) {
396
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
218 cairo_scaled_font_text_extents((cairo_scaled_font_t *)scaled_font,
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
219 txt,
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
220 (cairo_text_extents_t *)extents);
395
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
221 }
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
222
396
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
223 static
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
224 mb_text_extents_t *mb_text_extents_new(void) {
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
225 cairo_text_extents_t *extents;
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
226
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
227 extents = (cairo_text_extents_t *)malloc(sizeof(cairo_text_extents_t));
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
228 return extents;
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
229 }
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
230
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
231 static
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
232 void mb_text_extents_free(mb_text_extents_t *extents) {
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
233 free(extents);
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
234 }
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
235
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
236 /* @} */
395
d30b575a4ad4 Add fontconfig layer
Thinker K.F. Li <thinker@branda.to>
parents: 394
diff changeset
237
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
238 /*! \brief Query and return a font face for a specified attribute vector.
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
239 *
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
240 * Programmers use mb_font_face_t to specify fonts used to show a
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
241 * block of text on the output device. They can get mb_font_face_t with
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
242 * this function. The objects return by mb_font_face_query() should be
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
243 * freed with mb_font_face_free() when they are not more used.
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
244 *
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
245 * \param family is the name of a font family (times).
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
246 * \param slant is one of \ref MB_FONT_SLANTS.
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
247 * \param weight decides if a font is thin or heavy.
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
248 */
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
249 mb_font_face_t *mb_font_face_query(redraw_man_t *rdman,
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
250 const char *family,
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
251 int slant,
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
252 int weight) {
396
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
253 return query_font_face(family, slant, weight);
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
254 }
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
255
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
256 void mb_font_face_free(mb_font_face_t *face) {
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
257 ASSERT(face != NULL);
396
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
258 free_font_face(face);
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
259 }
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
260
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
261 /*! \brief A simple implementation of text shape.
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
262 *
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
263 */
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
264 typedef struct _sh_stext {
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
265 shape_t shape;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
266 const char *txt; /*!< \brief Text to be showed */
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
267 const mb_style_blk_t *style_blks; /*!< \brief Style of text */
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
268 int nblks; /*!< \brief Number of style blocks */
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
269 int max_nblks;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
270 co_aix x, y;
396
7fe0b1ee92b6 Add extents supporting into Fontconfig and FreeType Layer
Thinker K.F. Li <thinker@branda.to>
parents: 395
diff changeset
271 mb_scaled_font_t **scaled_fonts;
403
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
272 mb_text_extents_t extents;
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
273 } sh_stext_t;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
274
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
275 shape_t *rdman_shape_stext_new(redraw_man_t *rdman, co_aix x, co_aix y,
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
276 const char *txt) {
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
277 sh_stext_t *txt_o;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
278
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
279 ASSERT(txt != NULL);
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
280
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
281 txt_o = (sh_stext_t *)malloc(sizeof(sh_stext_t));
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
282 if(txt_o == NULL)
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
283 return NULL;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
284
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
285 memset(&txt_o->shape, 0, sizeof(shape_t));
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
286 mb_obj_init(txt_o, MBO_STEXT);
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
287
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
288 txt_o->txt = strdup(txt);
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
289 txt_o->style_blks = NULL;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
290 txt_o->nblks = 0;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
291 txt_o->max_nblks = 0;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
292 txt_o->x = x;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
293 txt_o->y = y;
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
294 txt_o->scaled_fonts = NULL;
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
295
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
296 if(txt_o->txt == NULL) {
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
297 free(txt_o);
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
298 txt_o = NULL;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
299 }
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
300
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
301 return (shape_t *)txt_o;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
302 }
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
303
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
304 static
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
305 int compute_utf8_chars_sz(const char *txt, int n_chars) {
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
306 int i;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
307 const char *p = txt;
415
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
308 const char *v;
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
309
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
310 for(i = 0; i < n_chars && *p; i++) {
415
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
311 if(!(*p & 0x80)) /* single byte */
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
312 p++;
415
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
313 else if((*p & 0xe0) == 0xc0) /* 2 bytes */
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
314 p += 2;
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
315 else if((*p & 0xf0) == 0xe0) /* 3 bytes */
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
316 p += 3;
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
317 else if((*p & 0xf8) == 0xf0) /* 4 bytes */
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
318 p += 4;
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
319 else
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
320 return ERR;
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
321 }
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
322 if(i < n_chars)
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
323 return ERR;
415
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
324
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
325 for(v = txt; v != p; v++)
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
326 if(*v == '\x0')
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
327 return ERR;
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
328
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
329 return p - txt;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
330 }
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
331
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
332 static
397
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
333 mb_scaled_font_t *make_scaled_font_face(sh_stext_t *txt_o,
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
334 mb_font_face_t *face,
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
335 co_aix shift_x, co_aix shift_y,
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
336 co_aix font_sz) {
397
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
337 co_aix matrix[6], scaled_matrix[6];
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
338 co_aix *aggr;
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
339 mb_scaled_font_t *scaled;
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
340
397
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
341 aggr = sh_get_aggr_matrix((shape_t *)txt_o);
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
342 matrix[0] = font_sz;
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
343 matrix[1] = 0;
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
344 matrix[2] = shift_x;
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
345 matrix[3] = 0;
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
346 matrix[4] = font_sz;
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
347 matrix[5] += shift_y;
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
348 matrix_mul(aggr, matrix, scaled_matrix);
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
349
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
350 scaled = make_scaled_font_face_matrix(face, scaled_matrix);
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
351
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
352 return scaled;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
353 }
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
354
403
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
355 /*! \brief Extend an extents from another sub-extents.
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
356 *
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
357 * A styled text is styled by several styled blocks, so extents of
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
358 * blocks should be computed separated, collected, and aggreagated
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
359 * into a full extents.
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
360 */
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
361 static
403
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
362 void extent_extents(mb_text_extents_t *full, mb_text_extents_t *sub) {
407
92a459a1c5aa more comment
Thinker K.F. Li <thinker@branda.to>
parents: 403
diff changeset
363 co_aix f_rbx, f_rby; /* rb stands for right button */
403
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
364 co_aix s_rbx, s_rby;
414
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
365 co_aix s_xbr, s_ybr;
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
366 co_aix new_x_adv, new_y_adv;
403
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
367
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
368 f_rbx = MBE_GET_X_BEARING(full) + MBE_GET_WIDTH(full);
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
369 f_rby = MBE_GET_Y_BEARING(full) + MBE_GET_HEIGHT(full);
414
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
370 s_xbr = MBE_GET_X_BEARING(sub) + MBE_GET_X_ADV(full);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
371 s_ybr = MBE_GET_Y_BEARING(sub) + MBE_GET_Y_ADV(full);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
372 s_rbx = s_xbr + MBE_GET_WIDTH(sub);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
373 s_rby = s_ybr + MBE_GET_HEIGHT(sub);
403
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
374
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
375 /* set bearing */
414
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
376 if(MBE_GET_X_BEARING(full) > s_xbr)
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
377 MBE_SET_X_BEARING(full, s_xbr);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
378 if(MBE_GET_Y_BEARING(full) > s_ybr)
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
379 MBE_SET_Y_BEARING(full, s_ybr);
403
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
380
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
381 /* set width/height */
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
382 if(f_rbx < s_rbx)
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
383 MBE_SET_WIDTH(full, s_rbx - MBE_GET_X_BEARING(full));
414
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
384 else
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
385 MBE_SET_WIDTH(full, f_rbx - MBE_GET_X_BEARING(full));
403
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
386 if(f_rby < s_rby)
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
387 MBE_SET_HEIGHT(full, s_rby - MBE_GET_Y_BEARING(full));
414
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
388 else
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
389 MBE_SET_HEIGHT(full, f_rby - MBE_GET_Y_BEARING(full));
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
390
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
391 /* set x/y advance */
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
392 new_x_adv = MBE_GET_X_ADV(full) + MBE_GET_X_ADV(sub);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
393 new_y_adv = MBE_GET_Y_ADV(full) + MBE_GET_Y_ADV(sub);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
394 MBE_SET_X_ADV(full, new_x_adv);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
395 MBE_SET_Y_ADV(full, new_y_adv);
403
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
396 }
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
397
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
398 /*! \brief Compute extents of a stext object according style blocks.
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
399 *
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
400 * It create scaled fonts for style blocks, compute their extents,
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
401 * and compute where they should be draw acoording advance of style
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
402 * blocks before a style block.
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
403 */
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
404 static
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
405 void compute_styled_extents_n_scaled_font(sh_stext_t *txt_o) {
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
406 mb_text_extents_t sub_extents;
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
407 const mb_style_blk_t *blk;
397
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
408 int blk_txt_len;
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
409 mb_scaled_font_t **scaled_font;
397
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
410 char *txt, saved;
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
411 co_aix shift_x, shift_y;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
412 int i;
397
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
413
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
414 memset(&txt_o->extents, sizeof(mb_text_extents_t), 0);
397
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
415
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
416 blk = txt_o->style_blks;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
417 scaled_font = txt_o->scaled_fonts;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
418 txt = (char *)txt_o->txt;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
419 for(i = 0; i < txt_o->nblks; i++) {
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
420 shift_x = txt_o->x + MBE_GET_X_ADV(&txt_o->extents);
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
421 shift_y = txt_o->y + MBE_GET_Y_ADV(&txt_o->extents);
397
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
422
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
423 *scaled_font = make_scaled_font_face(txt_o, blk->face,
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
424 shift_x, shift_y, blk->font_sz);
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
425 ASSERT(*scaled_font != NULL);
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
426
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
427 blk_txt_len = compute_utf8_chars_sz(txt, blk->n_chars);
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
428 ASSERT(blk_txt_len != ERR);
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
429
397
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
430 saved = txt[blk_txt_len];
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
431 txt[blk_txt_len] = 0;
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
432 compute_text_extents(*scaled_font, txt, &sub_extents);
397
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
433 txt[blk_txt_len] = saved;
55f38c2cdb8f Refactor function of computing text extents
Thinker K.F. Li <thinker@branda.to>
parents: 396
diff changeset
434
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
435 extent_extents(&txt_o->extents, &sub_extents);
403
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
436
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
437 scaled_font++;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
438 blk++;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
439 txt += blk_txt_len;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
440 }
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
441 }
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
442
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
443 /*
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
444 * What we have to do in sh_stext_transform() is
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
445 * - computing bounding box for the text,
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
446 * - computing offset x,y for the text of style blocks,
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
447 * - free old scaled fonts, and
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
448 * - making scaled fonts for style blocks.
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
449 */
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
450 void sh_stext_transform(shape_t *shape) {
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
451 sh_stext_t *txt_o = (sh_stext_t *)shape;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
452
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
453 ASSERT(txt_o != NULL);
403
aee0f66b1154 Compute extents of a styled text
Thinker K.F. Li <thinker@branda.to>
parents: 397
diff changeset
454 compute_styled_extents_n_scaled_font(txt_o);
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
455 }
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
456
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
457 void sh_stext_draw(shape_t *shape, cairo_t *cr) {
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
458 sh_stext_t *txt_o = (sh_stext_t *)shape;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
459
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
460 ASSERT(txt_o != NULL);
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
461 }
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
462
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
463 int sh_stext_set_text(shape_t *shape, const char *txt) {
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
464 sh_stext_t *txt_o = (sh_stext_t *)shape;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
465 char *new_txt;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
466 int sz;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
467
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
468 ASSERT(txt_o != NULL);
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
469 ASSERT(txt != NULL);
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
470
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
471 sz = strlen(txt) + 1;
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
472 new_txt = (char *)realloc((void *)txt_o->txt, sz);
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
473 if(new_txt == NULL)
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
474 return ERR;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
475
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
476 memcpy(new_txt, txt, sz);
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
477 txt_o->txt = new_txt;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
478
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
479 return OK;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
480 }
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
481
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
482 int sh_stext_set_style(shape_t *shape,
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
483 const mb_style_blk_t *blks,
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
484 int nblks) {
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
485 sh_stext_t *txt_o = (sh_stext_t *)shape;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
486 mb_style_blk_t *new_blks;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
487 int sz;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
488
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
489 ASSERT(txt_o != NULL);
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
490 ASSERT(nblks >= 0);
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
491
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
492 if(nblks > txt_o->max_nblks) {
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
493 sz = nblks * sizeof(mb_style_blk_t);
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
494 new_blks = (mb_style_blk_t *)realloc((void *)txt_o->style_blks, sz);
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
495 if(new_blks == NULL)
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
496 return ERR;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
497
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
498 txt_o->style_blks = new_blks;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
499 txt_o->max_nblks = nblks;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
500 }
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
501
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
502 memcpy(txt_o->style_blks, blks, nblks * sizeof(mb_style_blk_t));
393
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
503 txt_o->nblks = nblks;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
504
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
505 return OK;
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
506 }
27774b93521e Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
507
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
508 #ifdef UNITTEST
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
509
412
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
510 #include <CUnit/Basic.h>
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
511
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
512 static
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
513 void test_query_font_face(void) {
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
514 mb_font_face_t *face;
413
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
515 cairo_status_t status;
412
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
516
413
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
517 face = query_font_face("serif", MB_FONT_SLANT_ROMAN, 100);
412
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
518 CU_ASSERT(face != NULL);
413
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
519 status = cairo_font_face_status(face);
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
520 CU_ASSERT(status == CAIRO_STATUS_SUCCESS);
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
521
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
522 free_font_face(face);
412
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
523 }
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
524
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
525 static
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
526 void test_make_scaled_font_face_matrix(void) {
413
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
527 co_aix matrix[6] = {5, 0, 0, 0, 5, 0};
412
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
528 mb_font_face_t *face;
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
529 mb_scaled_font_t *scaled;
413
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
530 cairo_status_t status;
412
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
531
413
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
532 face = query_font_face("serif", MB_FONT_SLANT_ROMAN, 100);
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
533 CU_ASSERT(face != NULL);
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
534 status = cairo_font_face_status(face);
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
535 CU_ASSERT(status == CAIRO_STATUS_SUCCESS);
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
536
412
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
537 scaled = make_scaled_font_face_matrix(face, matrix);
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
538 CU_ASSERT(scaled != NULL);
413
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
539 status = cairo_scaled_font_status(scaled);
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
540 CU_ASSERT(status == CAIRO_STATUS_SUCCESS);
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
541
412
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
542 scaled_font_free(scaled);
413
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
543 free_font_face(face);
412
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
544 }
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
545
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
546 static
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
547 void test_compute_text_extents(void) {
413
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
548 co_aix matrix[6] = {10, 0, 0, 0, 10, 0};
412
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
549 mb_font_face_t *face;
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
550 mb_scaled_font_t *scaled;
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
551 mb_text_extents_t ext;
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
552
413
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
553 face = query_font_face("serif", MB_FONT_SLANT_ROMAN, 100);
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
554 CU_ASSERT(face != NULL)
412
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
555 scaled = make_scaled_font_face_matrix(face, matrix);
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
556 CU_ASSERT(scaled != NULL);
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
557
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
558 compute_text_extents(scaled, "test", &ext);
414
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
559 CU_ASSERT(MBE_GET_HEIGHT(&ext) >= 5 && MBE_GET_HEIGHT(&ext) <= 12);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
560 CU_ASSERT(MBE_GET_WIDTH(&ext) >= 16 && MBE_GET_WIDTH(&ext) <= 48);
412
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
561
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
562 scaled_font_free(scaled);
413
35712e4bad0e Make shape_stext.c pass test cases.
Thinker K.F. Li <thinker@branda.to>
parents: 412
diff changeset
563 free_font_face(face);
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
564 }
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
565
414
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
566 static
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
567 void test_extent_extents(void) {
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
568 mb_text_extents_t ext1, ext2;
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
569
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
570 MBE_SET_WIDTH(&ext1, 20);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
571 MBE_SET_HEIGHT(&ext1, 10);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
572 MBE_SET_X_BEARING(&ext1, 1);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
573 MBE_SET_Y_BEARING(&ext1, -8);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
574 MBE_SET_X_ADV(&ext1, 21);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
575 MBE_SET_Y_ADV(&ext1, -3);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
576
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
577 MBE_SET_WIDTH(&ext2, 30);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
578 MBE_SET_HEIGHT(&ext2, 11);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
579 MBE_SET_X_BEARING(&ext2, 2);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
580 MBE_SET_Y_BEARING(&ext2, -11);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
581 MBE_SET_X_ADV(&ext2, 32);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
582 MBE_SET_Y_ADV(&ext2, -5);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
583
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
584 extent_extents(&ext1, &ext2);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
585
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
586 CU_ASSERT(MBE_GET_WIDTH(&ext1) == 52);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
587 CU_ASSERT(MBE_GET_HEIGHT(&ext1) == 16);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
588 CU_ASSERT(MBE_GET_X_BEARING(&ext1) == 1);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
589 CU_ASSERT(MBE_GET_Y_BEARING(&ext1) == -14);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
590 CU_ASSERT(MBE_GET_X_ADV(&ext1) == 53);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
591 CU_ASSERT(MBE_GET_Y_ADV(&ext1) == -8);
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
592 }
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
593
415
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
594 static
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
595 void test_compute_utf8_chars_sz(void) {
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
596 const char *str = "\xe4\xb8\xad\xe6\x96\x87test\xe6\xb8\xac\xe8\xa9\xa6";
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
597 int sz;
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
598
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
599 sz = compute_utf8_chars_sz(str, 4);
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
600 CU_ASSERT(sz == 8);
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
601
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
602 sz = compute_utf8_chars_sz(str, 9);
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
603 CU_ASSERT(sz == ERR);
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
604 }
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
605
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
606 #include <CUnit/Basic.h>
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
607 CU_pSuite get_stext_suite(void) {
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
608 CU_pSuite suite;
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
609
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
610 suite = CU_add_suite("Suite_stext", NULL, NULL);
412
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
611 CU_ADD_TEST(suite, test_query_font_face);
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
612 CU_ADD_TEST(suite, test_make_scaled_font_face_matrix);
a456e267279a Test cases for shape_stext.
Thinker K.F. Li <thinker@branda.to>
parents: 411
diff changeset
613 CU_ADD_TEST(suite, test_compute_text_extents);
414
bbf036c315be Fix extent_extents to make it pass the test case.
Thinker K.F. Li <thinker@branda.to>
parents: 413
diff changeset
614 CU_ADD_TEST(suite, test_extent_extents);
415
fef1b8076041 Make compute_utf8_chars_sz to compute length of n UTF 8 characters correctly.
Thinker K.F. Li <thinker@branda.to>
parents: 414
diff changeset
615 CU_ADD_TEST(suite, test_compute_utf8_chars_sz);
410
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
616
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
617 return suite;
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
618 }
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
619
1a923ea699c1 shape_stext.c, now, is unittested.
Thinker K.F. Li <thinker@branda.to>
parents: 407
diff changeset
620 #endif /* UNITTEST */