File: x_font_cache.c

package info (click to toggle)
mlterm 3.1.2-1.3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 23,168 kB
  • sloc: ansic: 102,795; sh: 9,676; java: 2,018; perl: 1,601; makefile: 1,595; cpp: 771; sed: 16
file content (419 lines) | stat: -rw-r--r-- 7,877 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
 *	$Id$
 */

#include  "x_font_cache.h"

#include  <stdio.h>		/* sprintf */
#include  <kiklib/kik_util.h>	/* DIGIT_STR_LEN */


/* --- static variables --- */

static x_font_cache_t **  font_caches ;
static u_int  num_of_caches ;


/* --- static functions --- */

#ifdef  DEBUG

static void
dump_cached_fonts(
	x_font_cache_t *  font_cache
	)
{
	int  count ;
	u_int  size ;
	KIK_PAIR( x_font) *  f_array ;
	
	kik_debug_printf( KIK_DEBUG_TAG " cached fonts info\n") ;
	kik_map_get_pairs_array( font_cache->xfont_table , f_array , size) ;
	for( count = 0 ; count < size ; count++)
	{
		if( f_array[count]->value != NULL)
		{
			x_font_dump( f_array[count]->value) ;
		}
	}
}

#endif

static int
font_hash(
	ml_font_t  font ,
	u_int  size
	)
{
	return  font % size ;
}

static int
font_compare(
	ml_font_t  key1 ,
	ml_font_t  key2
	)
{
	return  (key1 == key2) ;
}

/*
 * Call this function after init all members except font_table
 */
static int
init_usascii_font(
	x_font_cache_t *  font_cache
	)
{
	u_int  beg_font_size ;
	
	beg_font_size = font_cache->font_size ;
	while( ( font_cache->usascii_font = x_font_cache_get_xfont( font_cache ,
					NORMAL_FONT_OF(font_cache->usascii_font_cs))) == NULL)
	{
		if( ++ font_cache->font_size > x_get_max_font_size())
		{
			font_cache->font_size = x_get_min_font_size() ;
		}
		else if( font_cache->font_size == beg_font_size)
		{
			return  0 ;
		}
	}

	return  1 ;
}

static KIK_MAP( x_font)
xfont_table_new(void)
{
	KIK_MAP( x_font)  xfont_table ;
	
	kik_map_new_with_size( ml_font_t , x_font_t * , xfont_table ,
		font_hash , font_compare , 16) ;

	return  xfont_table ;
}

static int
xfont_table_delete(
	KIK_MAP( x_font)  xfont_table
	)
{
	int  count ;
	u_int  size ;
	KIK_PAIR( x_font) *  f_array ;

	kik_map_get_pairs_array( xfont_table , f_array , size) ;
	
	for( count = 0 ; count < size ; count ++)
	{
		if( f_array[count]->value != NULL)
		{
			x_font_delete( f_array[count]->value) ;
		}
	}

	kik_map_delete( xfont_table) ;

	return  1 ;
}


/* --- global functions --- */

x_font_cache_t *
x_acquire_font_cache(
	Display *  display ,
	u_int  font_size ,
	mkf_charset_t  usascii_font_cs ,
	x_font_config_t *  font_config ,
	int  use_multi_col_char ,
	u_int  letter_space
	)
{
	int  count ;
	x_font_cache_t *  font_cache ;
	void *  p ;

	for( count = 0 ; count < num_of_caches ; count ++)
	{
		if( font_caches[count]->display == display &&
			font_caches[count]->font_size == font_size &&
			font_caches[count]->usascii_font_cs == usascii_font_cs &&
			font_caches[count]->font_config == font_config &&
			font_caches[count]->use_multi_col_char == use_multi_col_char &&
			font_caches[count]->letter_space == letter_space)
		{
			font_caches[count]->ref_count ++ ;

			return  font_caches[count] ;
		}
	}

	if( ( p = realloc( font_caches , sizeof( x_font_cache_t*) * (num_of_caches + 1))) == NULL)
	{
		return  NULL ;
	}

	font_caches = p ;

	if( ( font_cache = malloc( sizeof( x_font_cache_t))) == NULL)
	{
		return  NULL ;
	}

	font_cache->font_config = font_config ;

	font_cache->xfont_table = xfont_table_new() ;

	font_cache->display = display ;
	font_cache->font_size = font_size ;
	font_cache->usascii_font_cs = usascii_font_cs ;
	font_cache->use_multi_col_char = use_multi_col_char ;
	font_cache->letter_space = letter_space ;
	font_cache->ref_count = 1 ;

	font_cache->prev_cache.font = 0 ;
	font_cache->prev_cache.xfont = NULL ;

	if( ! init_usascii_font( font_cache))
	{
		xfont_table_delete( font_cache->xfont_table) ;
		free( font_cache) ;

		return  NULL ;
	}

	return  font_caches[num_of_caches++] = font_cache ;
}

int
x_release_font_cache(
	x_font_cache_t *  font_cache
	)
{
	int  count ;
	
	if( -- font_cache->ref_count > 0)
	{
		return  1 ;
	}

	for( count = 0 ; count < num_of_caches ; count ++)
	{
		if( font_caches[count] == font_cache)
		{
			font_caches[count] = font_caches[--num_of_caches] ;
			
			xfont_table_delete( font_cache->xfont_table) ;
			free( font_cache) ;

			if( num_of_caches == 0)
			{
				free( font_caches) ;
				font_caches = NULL ;
			}
			
			return  1 ;
		}
	}

	return  0 ;
}

int
x_font_cache_unload(
	x_font_cache_t *  font_cache
	)
{
	/*
	 * Discarding existing cache.
	 */
	xfont_table_delete( font_cache->xfont_table) ;
	font_cache->usascii_font = NULL ;
	font_cache->prev_cache.font = 0 ;
	font_cache->prev_cache.xfont = NULL ;

	/*
	 * Creating new cache.
	 */
	font_cache->xfont_table = xfont_table_new() ;

	if( ! init_usascii_font( font_cache))
	{
	#ifdef  DEBUG
		kik_debug_printf( KIK_DEBUG_TAG
		" x_font_cache_unload failed. Should x_release_font_cache this font cache.\n") ;
	#endif
	
		return  0 ;
	}
	
	return  1 ;
}

int
x_font_cache_unload_all(void)
{
	int  count ;

	for( count = 0 ; count < num_of_caches ; count++)
	{
		x_font_cache_unload( font_caches[count]) ;
	}

	return  1 ;
}

x_font_t *
x_font_cache_get_xfont(
	x_font_cache_t *  font_cache ,
	ml_font_t  font
	)
{
	int  result ;
	x_font_t *  xfont ;
	KIK_PAIR( x_font)  fn_pair ;
	char *  fontname ;
	int  use_medium_for_bold ;
	u_int  col_width ;

	if( FONT_CS(font) == US_ASCII)
	{
		font &= ~US_ASCII ;
		font |= font_cache->usascii_font_cs ;
	}

	if( font_cache->prev_cache.xfont && font_cache->prev_cache.font == font)
	{
		return  font_cache->prev_cache.xfont ;
	}

	if( font == NORMAL_FONT_OF(font_cache->usascii_font_cs))
	{
		col_width = 0 ;
	}
	else
	{
		col_width = font_cache->usascii_font->width ;
	}

	kik_map_get( result , font_cache->xfont_table , font , fn_pair) ;
	if( result)
	{
		return  fn_pair->value ;
	}

	use_medium_for_bold = 0 ;
	
	if( ( fontname = x_get_config_font_name( font_cache->font_config , font_cache->font_size ,
							font)) == NULL)
	{
		if( font & FONT_BOLD)
		{
			if( ( fontname = x_get_config_font_name( font_cache->font_config ,
						font_cache->font_size , font & ~FONT_BOLD)))
			{
				use_medium_for_bold = 1 ;
			}
		}
	}

	if( ( xfont = x_font_new( font_cache->display , font ,
				font_cache->font_config->type_engine ,
				font_cache->font_config->font_present , fontname ,
				font_cache->font_size , col_width , use_medium_for_bold ,
				font_cache->letter_space)))
	{
		if( ! font_cache->use_multi_col_char)
		{
			x_change_font_cols( xfont , 1) ;
		}
	}
#ifdef  DEBUG
	else
	{
		kik_warn_printf( KIK_DEBUG_TAG " font for id %x doesn't exist.\n" , font) ;
	}
#endif

	free( fontname) ;
	
	/*
	 * If this font doesn't exist, NULL(which indicates it) is cached.
	 */
	kik_map_set( result , font_cache->xfont_table , font , xfont) ;

	font_cache->prev_cache.font = font ;
	font_cache->prev_cache.xfont = xfont ;

#ifdef  DEBUG
	kik_debug_printf( KIK_DEBUG_TAG " Font %x for id %x was cached.%s\n" ,
		xfont , font , use_medium_for_bold ? "(medium font is used for bold.)" : "") ;
#endif

	return  xfont ;
}

char *
x_get_font_name_list_for_fontset(
	x_font_cache_t *  font_cache
	)
{
	char *  font_name_list ;
	char *  p ;
	size_t  list_len ;
	
	if( font_cache->font_config->type_engine != TYPE_XCORE)
	{
		x_font_config_t *  font_config ;

		if( ( font_config = x_acquire_font_config( TYPE_XCORE ,
					font_cache->font_config->font_present & ~FONT_AA)) == NULL)
		{
			font_name_list = NULL ;
		}
		else
		{
			font_name_list = x_get_all_config_font_names( font_config , font_cache->font_size) ;

			x_release_font_config( font_config) ;
		}
	}
	else
	{
		font_name_list = x_get_all_config_font_names( font_cache->font_config ,
					font_cache->font_size) ;
	}

	if( font_name_list)
	{
		list_len = strlen( font_name_list) ;
	}
	else
	{
		list_len = 0 ;
	}

	if( ( p = malloc( list_len + 28 + DIGIT_STR_LEN(font_cache->font_size) + 1))
			== NULL)
	{
		return  font_name_list ;
	}

	if( font_name_list)
	{
		sprintf( p , "%s,-*-*-medium-r-*--%d-*-*-*-*-*" ,
			font_name_list , font_cache->font_size) ;
		free( font_name_list) ;
	}
	else
	{
		sprintf( p , "-*-*-medium-r-*--%d-*-*-*-*-*" ,
			font_cache->font_size) ;
	}

	return  p ;
}