Mercurial > MadButterfly
annotate src/shape_stext.c @ 394:b3d5ce48670a
Use fontconfig to find out a font for a family/slant/weight pattern
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Wed, 10 Jun 2009 08:16:04 +0800 |
parents | 27774b93521e |
children | d30b575a4ad4 |
rev | line source |
---|---|
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
|
1 #if 0 |
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
|
2 |
393
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
3 #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
|
4 #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
|
5 #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
|
6 #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
|
7 #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
|
8 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
9 #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
|
10 #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
|
11 #endif |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
12 #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
|
13 #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
|
14 |
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
|
15 static |
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
|
16 FcPattern *query_fontconfig(const char *family, int slant, int weight) { |
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
|
17 FcPattern *ptn, *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
|
18 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
|
19 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
|
20 FcBool 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
|
21 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
|
22 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
|
23 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
|
24 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
|
25 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
|
26 |
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
|
27 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
|
28 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
|
29 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
|
30 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
|
31 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
|
32 |
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
|
33 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
|
34 val.u.s = family; |
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
|
35 FcPatternAdd(ptn, "family", &val, FcTrue); |
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
|
36 |
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
|
37 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
|
38 val.u.i = slant_map[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
|
39 FcPatternAdd(ptn, "slant", &val, FcTrue); |
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
|
40 |
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
|
41 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
|
42 val.u.i = weight; |
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
|
43 FcPatternAdd(ptn, "weight", &val, FcTrue); |
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
|
44 |
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
|
45 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
|
46 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
|
47 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
|
48 |
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
|
49 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
|
50 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
|
51 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
|
52 |
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
|
53 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
|
54 |
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
|
55 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
|
56 |
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
|
57 return 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
|
58 |
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
|
59 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
|
60 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
|
61 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
|
62 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
|
63 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
|
64 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
|
65 |
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
|
66 } |
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
|
67 |
393
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
68 /*! \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
|
69 * |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
70 * 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
|
71 * 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
|
72 * 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
|
73 * 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
|
74 * |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
75 * \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
|
76 * \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
|
77 * \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
|
78 */ |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
79 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
|
80 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
|
81 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
|
82 int weight) { |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
83 cairo_font_face_t *cface; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
84 FcPattern *ptn; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
85 |
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
|
86 ptn = query_fontconfig(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
|
87 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
88 cface = cairo_ft_font_face_create_for_pattern(ptn); |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
89 FcPatternDestroy(ptn); |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
90 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
91 return (mb_font_face_t *)cface; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
92 } |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
93 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
94 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
|
95 cairo_font_face_t *cface = (cairo_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
|
96 ASSERT(face != NULL); |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
97 cairo_font_face_destroy(face); |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
98 } |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
99 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
100 /*! \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
|
101 * |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
102 */ |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
103 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
|
104 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
|
105 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
|
106 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
|
107 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
|
108 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
|
109 co_aix x, y; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
110 cairo_scaled_font_t **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
|
111 } 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
|
112 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
113 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
|
114 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
|
115 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
|
116 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
117 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
|
118 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
119 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
|
120 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
|
121 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
|
122 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
123 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
|
124 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
|
125 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
126 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
|
127 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
|
128 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
|
129 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
|
130 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
|
131 txt_o->y = y; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
132 txt_o->exts = NULL; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
133 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
134 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
|
135 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
|
136 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
|
137 } |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
138 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
139 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
|
140 } |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
141 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
142 static |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
143 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
|
144 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
|
145 const char *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
|
146 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
147 for(i = 0; i < n && *p; i++) { |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
148 if(*p++ & 0x80) |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
149 continue; /* single byte */ |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
150 /* multi-bytes */ |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
151 while(*p && ((*p & 0xc0) == 0x80)) |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
152 p++; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
153 } |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
154 if(i < n) |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
155 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
|
156 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
157 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
|
158 } |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
159 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
160 static |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
161 void compute_text_extents(char *txt, int 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
|
162 cairo_scaled_font_t *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
|
163 cairo_text_extents_t *extents) { |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
164 char saved; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
165 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
166 saved = txt[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
|
167 txt[txt_len] = 0; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
168 cairo_scaled_font_text_extents(scaled_font, txt, extents); |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
169 txt[txt_len] = saved; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
170 } |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
171 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
172 static |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
173 cairo_scaled_font_t *make_scaled_font_face(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
|
174 cairo_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
|
175 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
|
176 co_aix font_sz) { |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
177 cairo_matrix_t cmtx; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
178 static cairo_matrix_t cid; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
179 static cairo_font_options_t *fopt = NULL; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
180 co_aix *matrix; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
181 cairo_scaled_font_t *scaled; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
182 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
|
183 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
184 if(fopt == NULL) { |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
185 cairo_matrix_init_identify(&cid); |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
186 fopt = cairo_font_options_create(); |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
187 if(fopt == NULL) |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
188 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
|
189 } |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
190 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
191 matrix = sh_get_aggr_matrix((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
|
192 i = 0; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
193 cmtx.xx = matrix[i++]; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
194 cmtx.xy = matrix[i++]; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
195 cmtx.x0 = matrix[i++] + shift_x; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
196 cmtx.yx = matrix[i++]; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
197 cmtx.yy = matrix[i++]; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
198 cmtx.y0 = matrix[i] + 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
|
199 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
200 scaled = cairo_scaled_font_create(face, &cmtx, &cid, fopt); |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
201 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
202 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
|
203 } |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
204 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
205 static |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
206 void compute_extents(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
|
207 co_aix adv_x, adv_y; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
208 cairo_text_extents_t extents; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
209 mb_style_blk_t *blk; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
210 cairo_scaled_font_t *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
|
211 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
|
212 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
|
213 int 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
|
214 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
|
215 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
216 adv_x = adv_y = 0; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
217 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
|
218 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
|
219 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
|
220 shift_x = txt_o->x; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
221 shift_y = txt_o->y; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
222 for(i = 0; i < txt_o->nblks; i++) { |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
223 *scaled_font = make_scaled_fonts(txt_o, blk->face, |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
224 shift_x, shift_y, font_sz); |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
225 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
|
226 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
227 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
|
228 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
|
229 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
230 compute_text_extents(txt, blk_txt_len, *scaled_font, &extents); |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
231 adv_x += extents.x_advance; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
232 adv_y += extents.y_advance; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
233 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
234 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
|
235 blk++; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
236 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
|
237 } |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
238 } |
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 /* |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
241 * 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
|
242 * - 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
|
243 * - 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
|
244 * - 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
|
245 * - 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
|
246 */ |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
247 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
|
248 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
|
249 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
250 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
|
251 } |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
252 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
253 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
|
254 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
|
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 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
|
257 } |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
258 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
259 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
|
260 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
|
261 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
|
262 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
|
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 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
|
265 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
|
266 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
267 sz = strlen(txt) + 1; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
268 new_txt = realloc(txt_o->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
|
269 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
|
270 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
|
271 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
272 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
|
273 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
|
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 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
|
276 } |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
277 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
278 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
|
279 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
|
280 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
|
281 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
|
282 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
|
283 co_aix *new_exts; |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
284 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
|
285 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
286 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
|
287 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
|
288 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
289 if(nblks > txt_o->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
|
290 sz = nblks * (sizeof(mb_style_blk_t) + sizeof(int)); |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
291 new_blks = (mb_style_blk_t *)realloc(txt_o->style_blks, sz); |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
292 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
|
293 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
|
294 new_exts = (int *)(new_blks + nblks); |
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 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
|
297 txt_o->exts = new_exts; |
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->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
|
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 memcpy(txt_o->blks, blks, nblks * sizeof(mb_style_blk_t)); |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
302 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
|
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 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
|
305 } |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
306 |
27774b93521e
Add sh_stext_t to implement a simple version of text shape.
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
307 #endif /* 0 */ |