@@ -155,14 +155,11 @@ typedef struct {
155155 int height ;
156156 PangoFontDescription * desc ;
157157 PangoLayout * layout ;
158- PangoLayout * split_layout ;
159158} font_elem ;
160159
161- static GHashTable * font_table = NULL ;
162-
163160static PangoContext * top_pango_context = NULL ;
164161
165- void font_elem_add ( int id , int height , char * family , int style , int variant , int weight , int stretch ) {
162+ static font_elem * font_elem_new ( int height , char * family , int style , int variant , int weight , int stretch ) {
166163 if (top_pango_context == NULL ) {
167164 top_pango_context = gtk_widget_get_pango_context (top );
168165 }
@@ -177,27 +174,51 @@ void font_elem_add(int id, int height, char *family, int style, int variant, int
177174 pango_font_description_set_stretch (e -> desc , stretch );
178175 e -> layout = pango_layout_new (top_pango_context );
179176 pango_layout_set_font_description (e -> layout , e -> desc );
180- e -> split_layout = pango_layout_new (top_pango_context );
181- pango_layout_set_font_description (e -> split_layout , e -> desc );
182- pango_layout_set_wrap (e -> split_layout , PANGO_WRAP_WORD_CHAR );
183- if (font_table == NULL ) {
184- font_table = g_hash_table_new (g_direct_hash , g_direct_equal );
185- }
186- g_hash_table_insert (font_table , GINT_TO_POINTER (id ), e );
177+ return e ;
187178}
188179
189- void font_elem_rem (int id ) {
190- font_elem * e = g_hash_table_lookup (font_table , GINT_TO_POINTER (id ));
191- g_assert (e );
192- g_hash_table_remove (font_table , GINT_TO_POINTER (id ));
193- g_object_unref (e -> split_layout );
180+ static void font_elem_free (font_elem * e ) {
194181 g_object_unref (e -> layout );
195182 pango_font_description_free (e -> desc );
196183 g_free (e );
197184}
198185
186+ static GHashTable * font_elem_table = NULL ;
187+
188+ void 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 );
190+ if (font_elem_table == NULL ) {
191+ font_elem_table = g_hash_table_new (g_direct_hash , g_direct_equal );
192+ }
193+ g_hash_table_insert (font_elem_table , GINT_TO_POINTER (id ), e );
194+ }
195+
196+ void font_elem_rem (int id ) {
197+ font_elem * e = g_hash_table_lookup (font_elem_table , GINT_TO_POINTER (id ));
198+ g_assert (e );
199+ g_hash_table_remove (font_elem_table , GINT_TO_POINTER (id ));
200+ font_elem_free (e );
201+ }
202+
203+ static GHashTable * font_metric_table = NULL ;
204+
205+ void 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 );
207+ if (font_metric_table == NULL ) {
208+ font_metric_table = g_hash_table_new (g_direct_hash , g_direct_equal );
209+ }
210+ g_hash_table_insert (font_metric_table , GINT_TO_POINTER (id ), e );
211+ }
212+
213+ void font_metric_rem (int id ) {
214+ font_elem * e = g_hash_table_lookup (font_metric_table , GINT_TO_POINTER (id ));
215+ g_assert (e );
216+ g_hash_table_remove (font_metric_table , GINT_TO_POINTER (id ));
217+ font_elem_free (e );
218+ }
219+
199220void get_font_metrics (int fontid , int16_t * lineheight , int16_t * baseline , int16_t * ascent , int16_t * descent ) {
200- font_elem * f = g_hash_table_lookup (font_table , GINT_TO_POINTER (fontid ));
221+ font_elem * f = g_hash_table_lookup (font_metric_table , GINT_TO_POINTER (fontid ));
201222 g_assert (f );
202223 * baseline = (int16_t )(pango_layout_get_baseline (f -> layout ) / PANGO_SCALE );
203224 PangoFontMetrics * metrics = pango_context_get_metrics (pango_layout_get_context (f -> layout ), f -> desc , NULL );
@@ -209,13 +230,14 @@ void get_font_metrics(int fontid, int16_t *lineheight, int16_t *baseline, int16_
209230
210231// text split
211232
212- int16_t * font_split_text (int fontid , char * text , int edge , int indent ) {
213- font_elem * f = g_hash_table_lookup (font_table , GINT_TO_POINTER (fontid ));
233+ int16_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 ));
214235 g_assert (f );
215- pango_layout_set_width (f -> split_layout , PANGO_SCALE * edge );
216- pango_layout_set_indent (f -> split_layout , PANGO_SCALE * indent );
217- pango_layout_set_text (f -> split_layout , text , -1 );
218- GSList * top = pango_layout_get_lines_readonly (f -> split_layout );
236+ pango_layout_set_wrap (f -> layout , PANGO_WRAP_WORD_CHAR );
237+ pango_layout_set_width (f -> layout , PANGO_SCALE * edge );
238+ pango_layout_set_indent (f -> layout , PANGO_SCALE * indent );
239+ pango_layout_set_text (f -> layout , text , -1 );
240+ GSList * top = pango_layout_get_lines_readonly (f -> layout );
219241 int16_t length = 0 ;
220242 for (GSList * e = top ; e != NULL ; e = e -> next ) {
221243 length ++ ;
@@ -227,13 +249,14 @@ int16_t *font_split_text(int fontid, char *text, int edge, int indent) {
227249 PangoLayoutLine * line = e -> data ;
228250 * pos ++ = (int16_t )(line -> length );
229251 }
252+ pango_layout_set_wrap (f -> layout , PANGO_WRAP_NONE );
230253 return out ;
231254}
232255
233256// text rect
234257
235- void font_rect_text (int fontid , char * text , int16_t * width , int16_t * height ) {
236- font_elem * f = g_hash_table_lookup (font_table , GINT_TO_POINTER (fontid ));
258+ void font_metric_rect_text (int fontid , char * text , int16_t * width , int16_t * height ) {
259+ font_elem * f = g_hash_table_lookup (font_metric_table , GINT_TO_POINTER (fontid ));
237260 g_assert (f );
238261 pango_layout_set_text (f -> layout , text , -1 );
239262 int w , h ;
@@ -262,7 +285,7 @@ static inline void elem_text_draw(cairo_t *cr, text_elem *e) {
262285static inline void elem_text_destroy (text_elem * e ) { g_free (e -> text ); }
263286
264287void elem_text_add (int id , double x , double y , char * text , int fontid , double r , double g , double b , double a ) {
265- font_elem * f = g_hash_table_lookup (font_table , GINT_TO_POINTER (fontid ));
288+ font_elem * f = g_hash_table_lookup (font_elem_table , GINT_TO_POINTER (fontid ));
266289 g_assert (f );
267290 text_elem * e = g_malloc (sizeof (text_elem ));
268291 e -> type = DRAW_ELEM_TEXT ;
0 commit comments