11#include "terminal.h"
22#include <gtk/gtk.h>
33
4+ // draw elem
5+
46#define DRAW_ELEM_FILL 1
57#define DRAW_ELEM_LINE 2
68#define DRAW_ELEM_TEXT 3
79#define DRAW_ELEM_IMAGE 4
810
9- // general
10-
1111typedef struct {
1212 int type ;
13- } type_elem ;
13+ union {
14+ struct {
15+ double x , y , width , height ;
16+ double r , g , b , a ;
17+ } fill ;
18+ struct {
19+ double x0 , y0 , x1 , y1 ;
20+ double r , g , b , a ;
21+ } line ;
22+ struct {
23+ double x , y ;
24+ double scale_x , scale_y ;
25+ cairo_surface_t * bitmap ;
26+ } image ;
27+ struct {
28+ double x , y ;
29+ char * text ;
30+ PangoLayout * layout ;
31+ double r , g , b , a ;
32+ } text ;
33+ } data ;
34+ } DrawElem ;
1435
1536// fill
1637
17- typedef struct {
18- int type ;
19- double x , y , width , height ;
20- double r , g , b , a ;
21- } fill_elem ;
22-
23- static inline void elem_fill_draw (cairo_t * cr , fill_elem * e ) {
24- cairo_set_source_rgba (cr , e -> r , e -> g , e -> b , e -> a );
25- cairo_rectangle (cr , e -> x , e -> y , e -> width , e -> height );
38+ static inline void elem_fill_draw (cairo_t * cr , DrawElem * e ) {
39+ cairo_set_source_rgba (cr , e -> data .fill .r , e -> data .fill .g , e -> data .fill .b , e -> data .fill .a );
40+ cairo_rectangle (cr , e -> data .fill .x , e -> data .fill .y , e -> data .fill .width , e -> data .fill .height );
2641 cairo_fill (cr );
2742}
2843
2944void elem_fill_add (int id , double x , double y , double width , double height , double r , double g , double b , double a ) {
30- fill_elem * e = g_malloc (sizeof (fill_elem ));
45+ DrawElem * e = g_malloc (sizeof (DrawElem ));
3146 e -> type = DRAW_ELEM_FILL ;
32- e -> x = x ;
33- e -> y = y ;
34- e -> width = width ;
35- e -> height = height ;
36- e -> r = r ;
37- e -> g = g ;
38- e -> b = b ;
39- e -> a = a ;
47+ e -> data . fill . x = x ;
48+ e -> data . fill . y = y ;
49+ e -> data . fill . width = width ;
50+ e -> data . fill . height = height ;
51+ e -> data . fill . r = r ;
52+ e -> data . fill . g = g ;
53+ e -> data . fill . b = b ;
54+ e -> data . fill . a = a ;
4055 window_add_draw (id , e );
4156}
4257
4358// line
4459
45- typedef struct {
46- int type ;
47- double x0 , y0 , x1 , y1 ;
48- double r , g , b , a ;
49- } line_elem ;
50-
51- static inline void elem_line_draw (cairo_t * cr , line_elem * e ) {
52- cairo_set_source_rgba (cr , e -> r , e -> g , e -> b , e -> a );
60+ static inline void elem_line_draw (cairo_t * cr , DrawElem * e ) {
61+ cairo_set_source_rgba (cr , e -> data .line .r , e -> data .line .g , e -> data .line .b , e -> data .line .a );
5362 cairo_set_line_width (cr , 1 );
54- cairo_move_to (cr , e -> x0 , e -> y0 );
55- cairo_line_to (cr , e -> x1 , e -> y1 );
63+ cairo_move_to (cr , e -> data . line . x0 , e -> data . line . y0 );
64+ cairo_line_to (cr , e -> data . line . x1 , e -> data . line . y1 );
5665 cairo_stroke (cr );
5766}
5867
@@ -64,16 +73,16 @@ void elem_line_add(int id, double x0, double y0, double x1, double y1, double r,
6473 y0 += 0.5 ;
6574 y1 += 0.5 ;
6675 }
67- line_elem * e = g_malloc (sizeof (line_elem ));
76+ DrawElem * e = g_malloc (sizeof (DrawElem ));
6877 e -> type = DRAW_ELEM_LINE ;
69- e -> x0 = x0 ;
70- e -> y0 = y0 ;
71- e -> x1 = x1 ;
72- e -> y1 = y1 ;
73- e -> r = r ;
74- e -> g = g ;
75- e -> b = b ;
76- e -> a = a ;
78+ e -> data . line . x0 = x0 ;
79+ e -> data . line . y0 = y0 ;
80+ e -> data . line . x1 = x1 ;
81+ e -> data . line . y1 = y1 ;
82+ e -> data . line . r = r ;
83+ e -> data . line . g = g ;
84+ e -> data . line . b = b ;
85+ e -> data . line . a = a ;
7786 window_add_draw (id , e );
7887}
7988
@@ -83,7 +92,7 @@ typedef struct {
8392 unsigned char * data ;
8493 double width , height ;
8594 cairo_surface_t * bitmap ;
86- } bitmap_elem ;
95+ } BitmapElem ;
8796
8897static GHashTable * bitmap_table = NULL ;
8998
@@ -101,7 +110,7 @@ void bitmap_add(int id, int width, int height, unsigned char *data) {
101110 }
102111 row += stride ;
103112 }
104- bitmap_elem * e = g_malloc (sizeof (bitmap_elem ));
113+ BitmapElem * e = g_malloc (sizeof (BitmapElem ));
105114 e -> data = data ;
106115 e -> width = width ;
107116 e -> height = height ;
@@ -113,7 +122,7 @@ void bitmap_add(int id, int width, int height, unsigned char *data) {
113122}
114123
115124void bitmap_rem (int id ) {
116- bitmap_elem * e = g_hash_table_lookup (bitmap_table , GINT_TO_POINTER (id ));
125+ BitmapElem * e = g_hash_table_lookup (bitmap_table , GINT_TO_POINTER (id ));
117126 g_assert (e );
118127 g_hash_table_remove (bitmap_table , GINT_TO_POINTER (id ));
119128 cairo_surface_destroy (e -> bitmap );
@@ -123,29 +132,22 @@ void bitmap_rem(int id) {
123132
124133// image drawing
125134
126- typedef struct {
127- int type ;
128- double x , y ;
129- double scale_x , scale_y ;
130- cairo_surface_t * bitmap ;
131- } image_elem ;
132-
133- static inline void elem_image_draw (cairo_t * cr , image_elem * e ) {
134- cairo_surface_set_device_scale (e -> bitmap , e -> scale_x , e -> scale_y );
135- cairo_set_source_surface (cr , e -> bitmap , e -> x , e -> y );
135+ static inline void elem_image_draw (cairo_t * cr , DrawElem * e ) {
136+ cairo_surface_set_device_scale (e -> data .image .bitmap , e -> data .image .scale_x , e -> data .image .scale_y );
137+ cairo_set_source_surface (cr , e -> data .image .bitmap , e -> data .image .x , e -> data .image .y );
136138 cairo_paint (cr );
137139}
138140
139141void elem_image_add (int id , double x , double y , double width , double height , int imageid ) {
140- image_elem * e = g_malloc (sizeof (image_elem ));
141- bitmap_elem * b = g_hash_table_lookup (bitmap_table , GINT_TO_POINTER (imageid ));
142+ BitmapElem * b = g_hash_table_lookup (bitmap_table , GINT_TO_POINTER (imageid ));
142143 g_assert (b );
144+ DrawElem * e = g_malloc (sizeof (DrawElem ));
143145 e -> type = DRAW_ELEM_IMAGE ;
144- e -> x = x ;
145- e -> y = y ;
146- e -> scale_x = b -> width / width ;
147- e -> scale_y = b -> height / height ;
148- e -> bitmap = b -> bitmap ;
146+ e -> data . image . x = x ;
147+ e -> data . image . y = y ;
148+ e -> data . image . scale_x = b -> width / width ;
149+ e -> data . image . scale_y = b -> height / height ;
150+ e -> data . image . bitmap = b -> bitmap ;
149151 window_add_draw (id , e );
150152}
151153
@@ -155,15 +157,15 @@ typedef struct {
155157 int height ;
156158 PangoFontDescription * desc ;
157159 PangoLayout * layout ;
158- } font_elem ;
160+ } FontElem ;
159161
160162static PangoContext * top_pango_context = NULL ;
161163
162- static font_elem * font_elem_new (int height , char * family , int style , int variant , int weight , int stretch ) {
164+ static FontElem * font_elem_new (int height , char * family , int style , int variant , int weight , int stretch ) {
163165 if (top_pango_context == NULL ) {
164166 top_pango_context = gtk_widget_get_pango_context (top );
165167 }
166- font_elem * e = g_malloc (sizeof (font_elem ));
168+ FontElem * e = g_malloc (sizeof (FontElem ));
167169 e -> height = height ;
168170 e -> desc = pango_font_description_new ();
169171 pango_font_description_set_family (e -> desc , family );
@@ -177,7 +179,7 @@ static font_elem *font_elem_new(int height, char *family, int style, int variant
177179 return e ;
178180}
179181
180- static void font_elem_free (font_elem * e ) {
182+ static void font_elem_free (FontElem * e ) {
181183 g_object_unref (e -> layout );
182184 pango_font_description_free (e -> desc );
183185 g_free (e );
@@ -186,15 +188,15 @@ static void font_elem_free(font_elem *e) {
186188static GHashTable * font_elem_table = NULL ;
187189
188190void font_elem_add (int id , int height , char * family , int style , int variant , int weight , int stretch ) {
189- font_elem * e = font_elem_new (height , family , style , variant , weight , stretch );
191+ FontElem * e = font_elem_new (height , family , style , variant , weight , stretch );
190192 if (font_elem_table == NULL ) {
191193 font_elem_table = g_hash_table_new (g_direct_hash , g_direct_equal );
192194 }
193195 g_hash_table_insert (font_elem_table , GINT_TO_POINTER (id ), e );
194196}
195197
196198void font_elem_rem (int id ) {
197- font_elem * e = g_hash_table_lookup (font_elem_table , GINT_TO_POINTER (id ));
199+ FontElem * e = g_hash_table_lookup (font_elem_table , GINT_TO_POINTER (id ));
198200 g_assert (e );
199201 g_hash_table_remove (font_elem_table , GINT_TO_POINTER (id ));
200202 font_elem_free (e );
@@ -203,22 +205,22 @@ void font_elem_rem(int id) {
203205static GHashTable * font_metric_table = NULL ;
204206
205207void font_metric_add (int id , int height , char * family , int style , int variant , int weight , int stretch ) {
206- font_elem * e = font_elem_new (height , family , style , variant , weight , stretch );
208+ FontElem * e = font_elem_new (height , family , style , variant , weight , stretch );
207209 if (font_metric_table == NULL ) {
208210 font_metric_table = g_hash_table_new (g_direct_hash , g_direct_equal );
209211 }
210212 g_hash_table_insert (font_metric_table , GINT_TO_POINTER (id ), e );
211213}
212214
213215void font_metric_rem (int id ) {
214- font_elem * e = g_hash_table_lookup (font_metric_table , GINT_TO_POINTER (id ));
216+ FontElem * e = g_hash_table_lookup (font_metric_table , GINT_TO_POINTER (id ));
215217 g_assert (e );
216218 g_hash_table_remove (font_metric_table , GINT_TO_POINTER (id ));
217219 font_elem_free (e );
218220}
219221
220222void get_font_metrics (int fontid , int16_t * lineheight , int16_t * baseline , int16_t * ascent , int16_t * descent ) {
221- font_elem * f = g_hash_table_lookup (font_metric_table , GINT_TO_POINTER (fontid ));
223+ FontElem * f = g_hash_table_lookup (font_metric_table , GINT_TO_POINTER (fontid ));
222224 g_assert (f );
223225 * baseline = (int16_t )(pango_layout_get_baseline (f -> layout ) / PANGO_SCALE );
224226 PangoFontMetrics * metrics = pango_context_get_metrics (pango_layout_get_context (f -> layout ), f -> desc , NULL );
@@ -231,7 +233,7 @@ void get_font_metrics(int fontid, int16_t *lineheight, int16_t *baseline, int16_
231233// text split
232234
233235int16_t * font_metric_split_text (int fontid , char * text , int edge , int indent ) {
234- font_elem * f = g_hash_table_lookup (font_metric_table , GINT_TO_POINTER (fontid ));
236+ FontElem * f = g_hash_table_lookup (font_metric_table , GINT_TO_POINTER (fontid ));
235237 g_assert (f );
236238 pango_layout_set_wrap (f -> layout , PANGO_WRAP_WORD_CHAR );
237239 pango_layout_set_width (f -> layout , PANGO_SCALE * edge );
@@ -251,19 +253,16 @@ int16_t *font_metric_split_text(int fontid, char *text, int edge, int indent) {
251253 }
252254#ifdef PANGO_AVAILABLE_ENUMERATOR_IN_1_56
253255 pango_layout_set_wrap (f -> layout , PANGO_WRAP_NONE );
254- #else
255- static char * zero = "\0" ;
256- pango_layout_set_text (f -> layout , zero , 0 );
256+ #endif
257257 pango_layout_set_width (f -> layout , -1 );
258258 pango_layout_set_indent (f -> layout , 0 );
259- #endif
260259 return out ;
261260}
262261
263262// text rect
264263
265264void font_metric_rect_text (int fontid , char * text , int16_t * width , int16_t * height ) {
266- font_elem * f = g_hash_table_lookup (font_metric_table , GINT_TO_POINTER (fontid ));
265+ FontElem * f = g_hash_table_lookup (font_metric_table , GINT_TO_POINTER (fontid ));
267266 g_assert (f );
268267 pango_layout_set_text (f -> layout , text , -1 );
269268 int w , h ;
@@ -272,45 +271,38 @@ void font_metric_rect_text(int fontid, char *text, int16_t *width, int16_t *heig
272271 * height = (int16_t )h ;
273272}
274273
275- // text
274+ // text drawing
276275
277- typedef struct {
278- int type ;
279- double x , y ;
280- char * text ;
281- PangoLayout * layout ;
282- double r , g , b , a ;
283- } text_elem ;
284-
285- static inline void elem_text_draw (cairo_t * cr , text_elem * e ) {
286- pango_layout_set_text (e -> layout , e -> text , -1 );
287- cairo_set_source_rgba (cr , e -> r , e -> g , e -> b , e -> a );
288- cairo_move_to (cr , e -> x , e -> y );
289- pango_cairo_show_layout (cr , e -> layout );
276+ static inline void elem_text_draw (cairo_t * cr , DrawElem * e ) {
277+ pango_layout_set_text (e -> data .text .layout , e -> data .text .text , -1 );
278+ cairo_set_source_rgba (cr , e -> data .text .r , e -> data .text .g , e -> data .text .b , e -> data .text .a );
279+ cairo_move_to (cr , e -> data .text .x , e -> data .text .y );
280+ pango_cairo_show_layout (cr , e -> data .text .layout );
290281}
291282
292- static inline void elem_text_destroy (text_elem * e ) { g_free (e -> text ); }
283+ static inline void elem_text_destroy (DrawElem * e ) { g_free (e -> data . text . text ); }
293284
294285void elem_text_add (int id , double x , double y , char * text , int fontid , double r , double g , double b , double a ) {
295- font_elem * f = g_hash_table_lookup (font_elem_table , GINT_TO_POINTER (fontid ));
286+ FontElem * f = g_hash_table_lookup (font_elem_table , GINT_TO_POINTER (fontid ));
296287 g_assert (f );
297- text_elem * e = g_malloc (sizeof (text_elem ));
288+ DrawElem * e = g_malloc (sizeof (DrawElem ));
298289 e -> type = DRAW_ELEM_TEXT ;
299- e -> x = x ;
300- e -> y = y ;
301- e -> text = text ;
302- e -> layout = f -> layout ;
303- e -> r = r ;
304- e -> g = g ;
305- e -> b = b ;
306- e -> a = a ;
290+ e -> data . text . x = x ;
291+ e -> data . text . y = y ;
292+ e -> data . text . text = text ;
293+ e -> data . text . layout = f -> layout ;
294+ e -> data . text . r = r ;
295+ e -> data . text . g = g ;
296+ e -> data . text . b = b ;
297+ e -> data . text . a = a ;
307298 window_add_draw (id , e );
308299}
309300
310301// any elem
311302
312- void draw_any_elem (gpointer e , gpointer cr ) {
313- switch (((type_elem * )e )-> type ) {
303+ void draw_any_elem (gpointer data , gpointer cr ) {
304+ DrawElem * e = data ;
305+ switch (e -> type ) {
314306 case DRAW_ELEM_FILL :
315307 elem_fill_draw (cr , e );
316308 break ;
@@ -326,8 +318,9 @@ void draw_any_elem(gpointer e, gpointer cr) {
326318 }
327319}
328320
329- void elem_draw_destroy (gpointer e ) {
330- switch (((type_elem * )e )-> type ) {
321+ void elem_draw_destroy (gpointer data ) {
322+ DrawElem * e = data ;
323+ switch (e -> type ) {
331324 case DRAW_ELEM_TEXT :
332325 elem_text_destroy (e );
333326 break ;
0 commit comments