File: ui_font_cache.c

package info (click to toggle)
mlterm 3.9.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,340 kB
  • sloc: ansic: 154,713; sh: 5,302; cpp: 2,953; objc: 2,776; java: 2,472; makefile: 2,445; perl: 1,674; xml: 44
file content (469 lines) | stat: -rw-r--r-- 12,323 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
/* -*- c-basic-offset:2; tab-width:2; indent-tabs-mode:nil -*- */

#include "ui_font_cache.h"

#include <stdio.h>        /* sprintf */
#include <pobl/bl_util.h> /* DIGIT_STR_LEN */

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

static ui_font_cache_t **font_caches;
static u_int num_caches;
static int leftward_double_drawing;
static int compose_dec_special_font;

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

#ifdef DEBUG

static void dump_cached_fonts(ui_font_cache_t *font_cache) {
  u_int count;
  u_int size;
  BL_PAIR(ui_font) * f_array;

  bl_debug_printf(BL_DEBUG_TAG " cached fonts info\n");
  bl_map_get_pairs_array(font_cache->uifont_table, f_array, size);
  for (count = 0; count < size; count++) {
    if (f_array[count]->value != NULL) {
      ui_font_dump(f_array[count]->value);
    }
  }
}

#endif

/*
 * Call this function after init all members except font_table
 */
static int init_usascii_font(ui_font_cache_t *font_cache) {
  return (font_cache->usascii_font = ui_font_cache_get_font(
              font_cache, NORMAL_FONT_OF(font_cache->usascii_font_cs))) != NULL;
}

static BL_MAP(ui_font) uifont_table_new(void) {
  BL_MAP(ui_font) uifont_table;

  bl_map_new_with_size(vt_font_t, ui_font_t*, uifont_table, bl_map_hash_int,
                       bl_map_compare_int, 16);

  return uifont_table;
}

static void uifont_table_destroy(BL_MAP(ui_font) uifont_table, ui_font_t *exception) {
  u_int count;
  u_int size;
  BL_PAIR(ui_font) * f_array;

  bl_map_get_pairs_array(uifont_table, f_array, size);

  for (count = 0; count < size; count++) {
    if (f_array[count]->value != NULL && f_array[count]->value != exception) {
      ui_font_destroy(f_array[count]->value);
    }
  }

  bl_map_destroy(uifont_table);
}

#ifdef USE_XLIB
static char *get_font_name_list_for_fontset(ui_font_cache_t *font_cache) {
  char *font_name_list;
  char *p;
  size_t list_len;

  if (font_cache->font_config->type_engine != TYPE_XCORE) {
    ui_font_config_t *font_config;

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

      ui_release_font_config(font_config);
    }
  } else {
    font_name_list = ui_get_config_font_names_all(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 + 29 + 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;
}
#endif

static ui_font_t *create_decsp_font_decsp(ui_font_t *usascii_font, vt_font_t font, int size_attr) {
  u_int w = usascii_font->width;
  u_int h = usascii_font->height;
  u_int a = usascii_font->ascent;

  if (size_attr) {
    if (size_attr >= DOUBLE_HEIGHT_TOP) {
      w *= 2;
      h *= 2;
      a *= 2;
    } else {
      w *= 2;
    }
  }

  return ui_font_new_for_decsp(usascii_font->display, font, w, h, a);
}

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

void ui_set_use_leftward_double_drawing(int use) {
  if (num_caches > 0) {
    bl_msg_printf("Unable to change leftward_double_drawing option.\n");
  } else {
    leftward_double_drawing = use;
  }
}

void ui_compose_dec_special_font(void) {
  compose_dec_special_font = 1;
}

ui_font_cache_t *ui_acquire_font_cache(Display *display, u_int font_size,
                                       ef_charset_t usascii_font_cs, ui_font_config_t *font_config,
                                       int letter_space) {
  int count;
  ui_font_cache_t *font_cache;
  void *p;

  for (count = 0; count < num_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]->letter_space == letter_space) {
      font_caches[count]->ref_count++;

      return font_caches[count];
    }
  }

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

  font_caches = p;

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

  font_cache->font_config = font_config;

  font_cache->uifont_table = uifont_table_new();

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

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

  if (!init_usascii_font(font_cache)) {
    uifont_table_destroy(font_cache->uifont_table, NULL);
    free(font_cache);

    return NULL;
  }

  return font_caches[num_caches++] = font_cache;
}

void ui_release_font_cache(ui_font_cache_t *font_cache) {
  int count;

  if (--font_cache->ref_count > 0) {
    return;
  }

  for (count = 0; count < num_caches; count++) {
    if (font_caches[count] == font_cache) {
      font_caches[count] = font_caches[--num_caches];

      uifont_table_destroy(font_cache->uifont_table, NULL);
      free(font_cache);

      if (num_caches == 0) {
        free(font_caches);
        font_caches = NULL;
      }

      return;
    }
  }
}

void ui_font_cache_unload(ui_font_cache_t *font_cache) {
  ui_font_t *prev_usascii_font;

  /*
   * Discarding existing cache.
   */

  uifont_table_destroy(font_cache->uifont_table, font_cache->usascii_font);
  prev_usascii_font = font_cache->usascii_font;
  font_cache->usascii_font = NULL;
  font_cache->prev_cache.font = 0;
  font_cache->prev_cache.uifont = NULL;

  /*
   * Creating new cache.
   */
  font_cache->uifont_table = uifont_table_new();

  if (!init_usascii_font(font_cache)) {
    int result;

#ifdef DEBUG
    bl_debug_printf(BL_DEBUG_TAG " ui_font_cache_unload() failed. Use old usascii font.\n");
#endif

    font_cache->usascii_font = prev_usascii_font;
    bl_map_set(result, font_cache->uifont_table, NORMAL_FONT_OF(font_cache->usascii_font_cs),
               prev_usascii_font);
  } else {
    ui_font_destroy(prev_usascii_font);
  }
}

void ui_font_cache_unload_all(void) {
  u_int count;

  for (count = 0; count < num_caches; count++) {
    ui_font_cache_unload(font_caches[count]);
  }
}

ui_font_t *ui_font_cache_get_font(ui_font_cache_t *font_cache, vt_font_t font) {
  ef_charset_t cs;
  ui_font_present_t font_present;
  vt_font_t font_for_config;
  int result;
  ui_font_t *uifont;
  BL_PAIR(ui_font) fn_pair;
  char *fontname;
  int use_medium_for_bold;
  u_int col_width;
  int size_attr;

  cs = FONT_CS(font);
  font_present = font_cache->font_config->font_present;

  if (cs == US_ASCII) {
    font &= ~US_ASCII;
    font |= font_cache->usascii_font_cs;
    font_for_config = font;
  } else if (cs == DEC_SPECIAL) {
    /* Bold, Italic etc are ignored. */
    font_for_config = font = SIZE_ATTR_FONT(DEC_SPECIAL, SIZE_ATTR_OF(font));
  } else {
    font_for_config = font;

    if (cs == ISO10646_UCS4_1_V) {
      font_present |= FONT_VAR_WIDTH;
      font_for_config &= ~ISO10646_UCS4_1_V;
      font_for_config |= ISO10646_UCS4_1;
    }
  }

  /* Note that 'cs' is not updated above, so be careful to use it below. */

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

  bl_map_get(font_cache->uifont_table, font, fn_pair);
  if (fn_pair) {
    return fn_pair->value;
  }

  size_attr = SIZE_ATTR_OF(font);

  /* If compose_dec_special_font is true, always create decsp font for DEC_SPECIAL. */
  if (cs == DEC_SPECIAL && compose_dec_special_font &&
      (uifont = create_decsp_font_decsp(font_cache->usascii_font, font, size_attr))) {
    goto font_found;
  }

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

  use_medium_for_bold = 0;

  if ((fontname = ui_get_config_font_name(font_cache->font_config, font_cache->font_size,
                                          font_for_config)) == NULL) {
    vt_font_t next_font;
    int scalable;

    next_font = font_for_config;

#ifndef TYPE_XCORE_SCALABLE
    if (font_cache->font_config->type_engine == TYPE_XCORE) {
      /*
       * If the type engine doesn't support scalable fonts,
       * medium weight font (drawn doubly) is used for bold.
       */
      scalable = 0;
    } else
#endif
    {
      /*
       * If the type engine supports scalable fonts,
       * the face of medium weight / r slant font is used
       * for bold and italic.
       */
      scalable = 1;
    }

    while (next_font & (FONT_BOLD | FONT_ITALIC)) {
      if (next_font & FONT_BOLD) {
        next_font &= ~FONT_BOLD;
      } else /* if(next_font & FONT_ITALIC) */
      {
        if (!scalable) {
          break;
        }

        next_font &= ~FONT_ITALIC;
      }

      if ((fontname = ui_get_config_font_name(font_cache->font_config, font_cache->font_size,
                                              next_font))) {
        if ((font & FONT_BOLD) && !scalable) {
          use_medium_for_bold = 1;
        }

        goto font_name_found;
      }
    }
  }

font_name_found:
  if ((uifont = ui_font_new(font_cache->display, NO_SIZE_ATTR(font), size_attr,
                            font_cache->font_config->type_engine, font_present, fontname,
                            font_cache->font_size, col_width, use_medium_for_bold,
                            font_cache->letter_space))) {
    if (size_attr &&
        (uifont = ui_font_new(font_cache->display, NORMAL_FONT_OF(font_cache->usascii_font_cs),
                              size_attr, font_cache->font_config->type_engine, font_present,
                              fontname, font_cache->font_size, col_width, use_medium_for_bold,
                              font_cache->letter_space))) {
      if (uifont->double_draw_gap && leftward_double_drawing) {
        uifont->double_draw_gap = -1;
      }
    }
#ifdef DEBUG
    else {
      bl_warn_printf(BL_DEBUG_TAG " font for id %x doesn't exist.\n", font);
    }
#endif
  } else if (cs == DEC_SPECIAL) {
    /*
     * If ui_font_new() fails, create decsp font for DEC_SPECIAL.
     * In most cases, if fontname is NULL, create decsp font for DEC_SPECIAL,
     * but if type_engine is TYPE_XCORE on xlib, don't create it but use
     * the default pcf font.
     * (See ui_font.c)
     */
    uifont = create_decsp_font_decsp(font_cache->usascii_font, font, size_attr);
  }
#ifdef DEBUG
  else {
    bl_warn_printf(BL_DEBUG_TAG " font for id %x doesn't exist.\n", font);
  }
#endif

  free(fontname);

font_found:
  /*
   * If this font doesn't exist, NULL(which indicates it) is cached.
   */
  bl_map_set(result, font_cache->uifont_table, font, uifont);

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

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

  return uifont;
}

XFontSet ui_font_cache_get_fontset(ui_font_cache_t *font_cache) {
#if defined(USE_XLIB)

  XFontSet fontset;
  char *list_str;
  char **missing;
  int miss_num;
  char *def_str;

  if ((list_str = get_font_name_list_for_fontset(font_cache)) == NULL) {
    return None;
  }

#ifdef __DEBUG
  bl_debug_printf(BL_DEBUG_TAG " font set list -> %s\n", list_str);
#endif

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

  free(list_str);

#ifdef DEBUG
  if (miss_num) {
    int count;

    bl_warn_printf(BL_DEBUG_TAG " missing charsets ...\n");
    for (count = 0; count < miss_num; count++) {
      bl_msg_printf(" %s\n", missing[count]);
    }
  }
#endif

  XFreeStringList(missing);

  return fontset;

#elif defined(USE_WIN32GUI)

  static LOGFONT logfont;

  ZeroMemory(&logfont, sizeof(logfont));
  GetObject(font_cache->usascii_font->xfont->fid, sizeof(logfont), &logfont);

  return &logfont;

#else

  return None;

#endif
}