File: x_font_manager.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 (568 lines) | stat: -rw-r--r-- 11,847 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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
 *	$Id$
 */

#include  "x_font_manager.h"

#include  <string.h>		/* strcat */
#include  <stdio.h>		/* sprintf */
#include  <kiklib/kik_mem.h>	/* malloc/alloca */
#include  <kiklib/kik_util.h>	/* DIGIT_STR_LEN */


#if  0
#define  __DEBUG
#endif


typedef struct encoding_to_cs_table
{
	ml_char_encoding_t  encoding ;
	mkf_charset_t  cs ;

} encoding_to_cs_table_t ;


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

/*
 * !!! Notice !!!
 * The order should be the same as ml_char_encoding_t.
 * US_ASCII font for encodings after ML_UTF8 is ISO8859_1_R. (see x_get_usascii_font_cs())
 */
static encoding_to_cs_table_t  usascii_font_cs_table[] =
{
	{ ML_ISO8859_1 , ISO8859_1_R } ,
	{ ML_ISO8859_2 , ISO8859_2_R } ,
	{ ML_ISO8859_3 , ISO8859_3_R } ,
	{ ML_ISO8859_4 , ISO8859_4_R } ,
	{ ML_ISO8859_5 , ISO8859_5_R } ,
	{ ML_ISO8859_6 , ISO8859_6_R } ,
	{ ML_ISO8859_7 , ISO8859_7_R } ,
	{ ML_ISO8859_8 , ISO8859_8_R } ,
	{ ML_ISO8859_9 , ISO8859_9_R } ,
	{ ML_ISO8859_10 , ISO8859_10_R } ,
	{ ML_TIS620 , TIS620_2533 } ,
	{ ML_ISO8859_13 , ISO8859_13_R } ,
	{ ML_ISO8859_14 , ISO8859_14_R } ,
	{ ML_ISO8859_15 , ISO8859_15_R } ,
	{ ML_ISO8859_16 , ISO8859_16_R } ,
	{ ML_TCVN5712 , TCVN5712_3_1993 } ,
	
	{ ML_ISCII_ASSAMESE , ISO8859_1_R } ,
	{ ML_ISCII_BENGALI , ISO8859_1_R } ,
	{ ML_ISCII_GUJARATI , ISO8859_1_R } ,
	{ ML_ISCII_HINDI , ISO8859_1_R } ,
	{ ML_ISCII_KANNADA , ISO8859_1_R } ,
	{ ML_ISCII_MALAYALAM , ISO8859_1_R } ,
	{ ML_ISCII_ORIYA , ISO8859_1_R } ,
	{ ML_ISCII_PUNJABI , ISO8859_1_R } ,
	{ ML_ISCII_ROMAN , ISO8859_1_R } ,
	{ ML_ISCII_TAMIL , ISO8859_1_R } ,
	{ ML_ISCII_TELUGU , ISO8859_1_R } ,
	{ ML_VISCII , VISCII } ,
	{ ML_KOI8_R , KOI8_R } ,
	{ ML_KOI8_U , KOI8_U } ,
	{ ML_KOI8_T , KOI8_T } ,
	{ ML_GEORGIAN_PS , GEORGIAN_PS } ,
	{ ML_CP1250 , CP1250 } ,
	{ ML_CP1251 , CP1251 } ,
	{ ML_CP1252 , CP1252 } ,
	{ ML_CP1253 , CP1253 } ,
	{ ML_CP1254 , CP1254 } ,
	{ ML_CP1255 , CP1255 } ,
	{ ML_CP1256 , CP1256 } ,
	{ ML_CP1257 , CP1257 } ,
	{ ML_CP1258 , CP1258 } ,
	{ ML_CP874 , CP874 } ,

	{ ML_UTF8 , ISO10646_UCS4_1 } ,
	
} ;


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

static int
change_font_cache(
	x_font_manager_t *  font_man ,
	x_font_cache_t *  font_cache
	)
{
	x_release_font_cache( font_man->font_cache) ;
	font_man->font_cache = font_cache ;

	return  1 ;
}


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

x_font_manager_t *
x_font_manager_new(
	Display *  display ,
	x_type_engine_t  type_engine ,
	x_font_present_t  font_present ,
	u_int  font_size ,
	mkf_charset_t  usascii_font_cs ,
	int  usascii_font_cs_changable ,
	int  use_multi_col_char ,
	u_int  step_in_changing_font_size ,
	u_int  letter_space
	)
{
	x_font_manager_t *  font_man ;
	
	if( ! ( font_man = malloc( sizeof( x_font_manager_t))))
	{
	#ifdef  DEBUG
		kik_warn_printf( KIK_DEBUG_TAG " malloc() failed.\n") ;
	#endif
	
		return  NULL ;
	}

	if( ( ! ( font_man->font_config = x_acquire_font_config( type_engine , font_present)) ||
	      ! ( font_man->font_cache = x_acquire_font_cache( display , font_size ,
						usascii_font_cs , font_man->font_config ,
						use_multi_col_char , letter_space))))
	{
		x_type_engine_t  engine ;

	#ifdef  DEBUG
		kik_debug_printf( KIK_DEBUG_TAG " Could not handle %s fonts.\n" ,
			x_get_type_engine_name( type_engine)) ;
	#endif

		for( engine = TYPE_XCORE ; ; engine ++)
		{
			if( engine == type_engine)
			{
				continue ;
			}

			if( font_man->font_config)
			{
				x_release_font_config( font_man->font_config) ;
			}

			if( engine >= TYPE_ENGINE_MAX)
			{
				free( font_man) ;

				return  NULL ;
			}

			if( ( font_man->font_config = x_acquire_font_config(
								engine , font_present)) &&
			    ( font_man->font_cache = x_acquire_font_cache( display , font_size ,
						usascii_font_cs , font_man->font_config ,
						use_multi_col_char , letter_space)) )
			{
				break ;
			}
		}

		kik_msg_printf( "Fall back to %s.\n" , x_get_type_engine_name( engine)) ;
	}

	font_man->usascii_font_cs_changable = usascii_font_cs_changable ;
	if(  x_get_max_font_size() - x_get_min_font_size() >= step_in_changing_font_size)
	{
		font_man->step_in_changing_font_size = step_in_changing_font_size ;
	}
	else
	{
		 font_man->step_in_changing_font_size = x_get_max_font_size() - x_get_min_font_size() ;
	}
	
	return  font_man ;
}

int
x_font_manager_delete(
	x_font_manager_t *  font_man
	)
{
	x_release_font_cache( font_man->font_cache) ;

	x_release_font_config( font_man->font_config) ;
	font_man->font_config = NULL ;
	
	free( font_man) ;

	return  1 ;
}

x_font_t *
x_get_font(
	x_font_manager_t *  font_man ,
	ml_font_t  font
	)
{
	x_font_t *  xfont ;

	if( ( xfont = x_font_cache_get_xfont( font_man->font_cache , font)))
	{
		return  xfont ;
	}
	else
	{
		return  font_man->font_cache->usascii_font ;
	}
}

x_font_t *
x_get_usascii_font(
	x_font_manager_t *  font_man
	)
{
	return  font_man->font_cache->usascii_font ;
}

int
x_font_manager_usascii_font_cs_changed(
	x_font_manager_t *  font_man ,
	mkf_charset_t  usascii_font_cs
	)
{
	x_font_cache_t *  font_cache ;

	if( usascii_font_cs == font_man->font_cache->usascii_font_cs)
	{
		return  1 ;
	}
	
	if( ! font_man->usascii_font_cs_changable)
	{
		return  0 ;
	}

	if( ( font_cache = x_acquire_font_cache( font_man->font_cache->display ,
				font_man->font_cache->font_size , usascii_font_cs ,
				font_man->font_config ,
				font_man->font_cache->use_multi_col_char ,
				font_man->font_cache->letter_space)) == NULL)
	{
		return  0 ;
	}

	change_font_cache( font_man , font_cache) ;

	return  1 ;
}

/*
 * Return 1 if font present is successfully changed.
 * Return 0 if not changed.
 */
int
x_change_font_present(
	x_font_manager_t *  font_man ,
	x_type_engine_t  type_engine ,
	x_font_present_t  font_present
	)
{
	x_font_config_t *  font_config ;
	x_font_cache_t *  font_cache ;

#ifndef  USE_WIN32GUI
	/*
	 * FONT_AA is effective in xft, so following hack is necessary in xlib.
	 */
	if( ( type_engine == TYPE_XCORE) && ( font_man->font_config->font_present & FONT_AA))
	{
		font_present &= ~FONT_AA ;
	}
	else if( ( font_present & FONT_AA) &&
		font_man->font_config->type_engine == TYPE_XCORE && type_engine == TYPE_XCORE)
	{
	#if  ! defined(USE_TYPE_XFT) && defined(USE_TYPE_CAIRO)
		type_engine = TYPE_CAIRO ;
	#else
		type_engine = TYPE_XFT ;
	#endif
	}
#endif

	if( font_present == font_man->font_config->font_present &&
		type_engine == font_man->font_config->type_engine)
	{
		/* Same as current settings. */
		
		return  0 ;
	}

	if( ( font_config = x_acquire_font_config( type_engine , font_present)) == NULL)
	{
		return  0 ;
	}

	if( ( font_cache = x_acquire_font_cache( font_man->font_cache->display ,
				font_man->font_cache->font_size ,
				font_man->font_cache->usascii_font_cs ,
				font_config , font_man->font_cache->use_multi_col_char ,
				font_man->font_cache->letter_space)) == NULL)
	{
		x_release_font_config( font_config) ;

		return  0 ;
	}

	change_font_cache( font_man , font_cache) ;

	x_release_font_config( font_man->font_config) ;
	font_man->font_config = font_config ;

	return  1 ;
}

x_type_engine_t
x_get_type_engine(
	x_font_manager_t *  font_man
	)
{
	return  font_man->font_config->type_engine ;
}

x_font_present_t
x_get_font_present(
	x_font_manager_t *  font_man
	)
{
	return  font_man->font_config->font_present ;
}

int
x_change_font_size(
	x_font_manager_t *  font_man ,
	u_int  font_size
	)
{
	x_font_cache_t *  font_cache ;
		
	if( font_size == font_man->font_cache->font_size)
	{
		/* not changed (pretending to succeed) */
		
		return  1 ;
	}

	if( font_size < x_get_min_font_size() || x_get_max_font_size() < font_size)
	{
		return  0 ;
	}

	if( ( font_cache = x_acquire_font_cache( font_man->font_cache->display ,
				font_size , font_man->font_cache->usascii_font_cs , 
				font_man->font_config ,
				font_man->font_cache->use_multi_col_char ,
				font_man->font_cache->letter_space)) == NULL)
	{
		return  0 ;
	}

	change_font_cache( font_man , font_cache) ;

	return  1 ;
}

int
x_larger_font(
	x_font_manager_t *  font_man
	)
{
	u_int  font_size ;
	x_font_cache_t *  font_cache ;

	if( font_man->font_cache->font_size + font_man->step_in_changing_font_size > x_get_max_font_size())
	{
		font_size = x_get_min_font_size() ;
	}
	else
	{
		font_size = font_man->font_cache->font_size + font_man->step_in_changing_font_size ;
	}
	
	if( ( font_cache = x_acquire_font_cache( font_man->font_cache->display ,
				font_size , font_man->font_cache->usascii_font_cs , 
				font_man->font_config ,
				font_man->font_cache->use_multi_col_char ,
				font_man->font_cache->letter_space)) == NULL)
	{
		return  0 ;
	}
	
	change_font_cache( font_man , font_cache) ;

	return  1 ;
}

int
x_smaller_font(
	x_font_manager_t *  font_man
	)
{
	u_int  font_size ;
	x_font_cache_t *  font_cache ;

	if( font_man->font_cache->font_size < x_get_min_font_size() + font_man->step_in_changing_font_size)
	{
		font_size = x_get_max_font_size() ;
	}
	else
	{
		font_size = font_man->font_cache->font_size - font_man->step_in_changing_font_size ;
	}
	
	if( ( font_cache = x_acquire_font_cache( font_man->font_cache->display ,
				font_size , font_man->font_cache->usascii_font_cs , 
				font_man->font_config ,
				font_man->font_cache->use_multi_col_char ,
				font_man->font_cache->letter_space)) == NULL)
	{
		return  0 ;
	}

	change_font_cache( font_man , font_cache) ;

	return  1 ;
}

u_int
x_get_font_size(
	x_font_manager_t *  font_man
	)
{
	return  font_man->font_cache->font_size ;
}

int
x_set_use_multi_col_char(
	x_font_manager_t *  font_man ,
	int  flag
	)
{
	x_font_cache_t *  font_cache ;

	if( font_man->font_cache->use_multi_col_char == flag)
	{
		return  0 ;
	}

	if( ( font_cache = x_acquire_font_cache( font_man->font_cache->display ,
				font_man->font_cache->font_size ,
				font_man->font_cache->usascii_font_cs ,
				font_man->font_config , flag ,
				font_man->font_cache->letter_space)) == NULL)
	{
		return  0 ;
	}

	change_font_cache( font_man , font_cache) ;

	return  1 ;
}

int
x_set_letter_space(
	x_font_manager_t *  font_man ,
	u_int  letter_space
	)
{
	x_font_cache_t *  font_cache ;

	if( font_man->font_cache->letter_space == letter_space)
	{
		return  0 ;
	}

	if( ( font_cache = x_acquire_font_cache( font_man->font_cache->display ,
				font_man->font_cache->font_size ,
				font_man->font_cache->usascii_font_cs ,
				font_man->font_config , font_man->font_cache->use_multi_col_char ,
				letter_space)) == NULL)
	{
		return  0 ;
	}

	change_font_cache( font_man , font_cache) ;

	return  1 ;
}

XFontSet
x_get_fontset(
	x_font_manager_t *  font_man
	)
{
#ifdef  USE_WIN32GUI
	return  None ;
#else
	XFontSet  fontset ;
	char *  list_str ;
	char **  missing ;
	int  miss_num ;
	char *  def_str ;

	if( ( list_str = x_get_font_name_list_for_fontset( font_man->font_cache)) == NULL)
	{
		return  None ;
	}
	
#ifdef  __DEBUG
	kik_debug_printf( KIK_DEBUG_TAG " font set list -> %s\n" , list_str) ;
#endif

	fontset = XCreateFontSet( font_man->font_cache->display , list_str ,
			&missing , &miss_num , &def_str) ;

	free( list_str) ;
	
#ifdef  DEBUG
	if( miss_num)
	{
		int  count ;
		
		kik_warn_printf( KIK_DEBUG_TAG " missing charsets ...\n") ;
		for( count = 0 ; count < miss_num ; count ++)
		{
			kik_msg_printf( " %s\n" , missing[count]) ;
		}
	}
#endif

	XFreeStringList( missing) ;

	return  fontset ;
#endif
}

mkf_charset_t
x_get_usascii_font_cs(
	ml_char_encoding_t  encoding
	)
{
	if( encoding < 0 ||
		sizeof( usascii_font_cs_table) / sizeof( usascii_font_cs_table[0]) <= encoding)
	{
		return  ISO8859_1_R ;
	}
#ifdef  DEBUG
	else if( encoding != usascii_font_cs_table[encoding].encoding)
	{
		kik_warn_printf( KIK_DEBUG_TAG " %x is illegal encoding.\n" , encoding) ;

		return  ISO8859_1_R ;
	}
#endif
	else
	{
	#ifdef  __DEBUG
		kik_debug_printf( KIK_DEBUG_TAG " us ascii font is %x cs\n" ,
			usascii_font_cs_table[encoding].cs) ;
	#endif
	
		return  usascii_font_cs_table[encoding].cs ;
	}
}