gdkpango-fb.c 25.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#include <glib.h>
#include "gdkprivate-fb.h"
#include "gdkpango.h"
#include <dirent.h>
#include <string.h>
#include <stdlib.h>

#include <pango/pango.h>
#include <pango/pango-modules.h>

#include <freetype/freetype.h>
12
#include <freetype/ftglyph.h>
13

14
#if !defined(FREETYPE_MAJOR) || FREETYPE_MAJOR != 2
15
#error "We need Freetype 2.0"
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
#endif

#define PANGO_RENDER_TYPE_FB "PangoRenderTypeFB"

typedef struct {
  PangoFontMap parent_instance;

  GPtrArray *all_descs;
  GHashTable *all_fonts;

  int n_fonts;

  double resolution;
} PangoFBFontMap;

typedef struct {
  PangoFontMapClass parent_class;
} PangoFBFontMapClass;

typedef struct {
  PangoFontDescription desc;
  FT_Face ftf;
} PangoFBFontListing;

FT_Library gdk_fb_ft_lib = NULL;

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
#define MAX_ALIASES 3

typedef struct {
  PangoFontDescription alias;
  PangoFontDescription descriptions[MAX_ALIASES];
} PangoFBAlias;

static PangoFBAlias alias_table[] =
{
  /* sans: */
  {
    {"Sans", PANGO_STYLE_NORMAL, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_NORMAL, PANGO_STRETCH_NORMAL},
    {
      {"Arial", PANGO_STYLE_NORMAL, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_NORMAL, PANGO_STRETCH_NORMAL},
      {"URW Gothic L", PANGO_STYLE_NORMAL, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_NORMAL, PANGO_STRETCH_NORMAL},
    }
  },
  {
    {"Sans", PANGO_STYLE_OBLIQUE, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_NORMAL, PANGO_STRETCH_NORMAL},
    {
      {"Arial", PANGO_STYLE_OBLIQUE, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_NORMAL, PANGO_STRETCH_NORMAL},
      {"URW Gothic L", PANGO_STYLE_OBLIQUE, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_NORMAL, PANGO_STRETCH_NORMAL},
    }
  },
  {
    {"Sans", PANGO_STYLE_NORMAL, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_BOLD, PANGO_STRETCH_NORMAL},
    {
      {"Arial", PANGO_STYLE_NORMAL, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_BOLD, PANGO_STRETCH_NORMAL},
      {"URW Gothic L", PANGO_STYLE_NORMAL, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_BOLD, PANGO_STRETCH_NORMAL},
    }
  },
  {
    {"Sans", PANGO_STYLE_OBLIQUE, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_BOLD, PANGO_STRETCH_NORMAL},
    {
      {"Arial", PANGO_STYLE_OBLIQUE, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_BOLD, PANGO_STRETCH_NORMAL},
      {"URW Gothic L", PANGO_STYLE_OBLIQUE, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_BOLD, PANGO_STRETCH_NORMAL},
    }
  },

  /* serif: */
  {
    {"Serif", PANGO_STYLE_NORMAL, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_NORMAL, PANGO_STRETCH_NORMAL},
    {
      {"Times New Roman", PANGO_STYLE_NORMAL, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_NORMAL, PANGO_STRETCH_NORMAL},
      {"URW Bookman L", PANGO_STYLE_NORMAL, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_NORMAL, PANGO_STRETCH_NORMAL},
    }
  },
  {
    {"Serif", PANGO_STYLE_OBLIQUE, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_NORMAL, PANGO_STRETCH_NORMAL},
    {
      {"Times New Roman", PANGO_STYLE_OBLIQUE, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_NORMAL, PANGO_STRETCH_NORMAL},
      {"URW Bookman L", PANGO_STYLE_OBLIQUE, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_NORMAL, PANGO_STRETCH_NORMAL},
    }
  },
  {
    {"Serif", PANGO_STYLE_NORMAL, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_BOLD, PANGO_STRETCH_NORMAL},
    {
      {"Times New Roman", PANGO_STYLE_NORMAL, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_BOLD, PANGO_STRETCH_NORMAL},
      {"URW Bookman L", PANGO_STYLE_NORMAL, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_BOLD, PANGO_STRETCH_NORMAL},
    }
  },
  {
    {"Serif", PANGO_STYLE_OBLIQUE, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_BOLD, PANGO_STRETCH_NORMAL},
    {
      {"Times New Roman", PANGO_STYLE_OBLIQUE, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_BOLD, PANGO_STRETCH_NORMAL},
      {"URW Bookman L", PANGO_STYLE_OBLIQUE, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_BOLD, PANGO_STRETCH_NORMAL},
    }
  },
  
  /* monospace: */
  {
    {"Monospace", PANGO_STYLE_NORMAL, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_NORMAL, PANGO_STRETCH_NORMAL},
    {
      {"Courier New", PANGO_STYLE_NORMAL, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_NORMAL, PANGO_STRETCH_NORMAL},
      {"Courier", PANGO_STYLE_NORMAL, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_NORMAL, PANGO_STRETCH_NORMAL},
    }
  },
  {
    {"Monospace", PANGO_STYLE_OBLIQUE, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_NORMAL, PANGO_STRETCH_NORMAL},
    {
      {"Courier New", PANGO_STYLE_OBLIQUE, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_NORMAL, PANGO_STRETCH_NORMAL},
      {"Courier", PANGO_STYLE_OBLIQUE, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_NORMAL, PANGO_STRETCH_NORMAL},
    }
  },
  {
    {"Monospace", PANGO_STYLE_NORMAL, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_BOLD, PANGO_STRETCH_NORMAL},
    {
      {"Courier New", PANGO_STYLE_NORMAL, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_BOLD, PANGO_STRETCH_NORMAL},
      {"Courier", PANGO_STYLE_NORMAL, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_BOLD, PANGO_STRETCH_NORMAL},
    }
  },
  {
    {"Monospace", PANGO_STYLE_OBLIQUE, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_BOLD, PANGO_STRETCH_NORMAL},
    {
      {"Courier New", PANGO_STYLE_OBLIQUE, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_BOLD, PANGO_STRETCH_NORMAL},
      {"Courier", PANGO_STYLE_OBLIQUE, PANGO_VARIANT_NORMAL, PANGO_WEIGHT_BOLD, PANGO_STRETCH_NORMAL},
    }
  },
};

#define ALIAS_TABLE_LEN (sizeof(alias_table)/sizeof(PangoFBAlias))
143

144
void
145
gdk_fb_font_init (void)
146
{
147
  FT_Init_FreeType (&gdk_fb_ft_lib);
148
149
150
}

void
151
gdk_fb_font_fini (void)
152
{
153
  FT_Done_FreeType (gdk_fb_ft_lib);
154
155
}

156
157
158
159
160
161
162
163
164
165
166
void              pango_fb_font_set_size          (PangoFont                    *font);
static void       pango_fb_font_map_init          (PangoFBFontMap               *fontmap);
static PangoFont *pango_fb_font_map_load_font     (PangoFontMap                 *fontmap,
						   const PangoFontDescription   *desc);
static void       pango_fb_font_map_list_fonts    (PangoFontMap                 *fontmap,
						   const gchar                  *family,
						   PangoFontDescription       ***descs,
						   int                          *n_descs);
static void       pango_fb_font_map_list_families (PangoFontMap                 *fontmap,
						   gchar                      ***families,
						   int                          *n_families);
167

168
169

static void
170
pango_fb_font_map_class_init (PangoFBFontMapClass *class)
171
172
173
174
175
176
177
{
  class->parent_class.load_font = pango_fb_font_map_load_font;
  class->parent_class.list_fonts = pango_fb_font_map_list_fonts;
  class->parent_class.list_families = pango_fb_font_map_list_families;
}

GType
178
pango_fb_font_map_get_type (void)
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
{
  static GType object_type = 0;

  if (!object_type)
    {
      static const GTypeInfo object_info =
      {
        sizeof (PangoFBFontMapClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) pango_fb_font_map_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (PangoFBFontMap),
        0,              /* n_preallocs */
        (GInstanceInitFunc) pango_fb_font_map_init,
      };
      
      object_type = g_type_register_static (PANGO_TYPE_FONT_MAP,
                                            "PangoFBFontMap",
199
200
                                            &object_info,
					    0);
201
202
203
204
205
    }
  
  return object_type;
}

206
207
208
209
210
211
212
213
214
215
216
217
218
static gboolean
pango_font_description_equal_nosize (const PangoFontDescription *d1,
				     const PangoFontDescription *d2)
{
  return
    (g_strcasecmp (d1->family_name, d2->family_name)==0) &&
    d1->style == d2->style &&
    d1->variant == d2->variant &&
    d1->weight == d2->weight &&
    d1->stretch == d2->stretch;
}


219
static PangoFont *
220
221
222
pango_fb_lookup_descr (PangoFontMap *fontmap,
		       const PangoFontDescription *desc,
		       const PangoFontDescription *save_as)
223
224
225
{
  PangoFBFontMap *fbfm = (PangoFBFontMap *)fontmap;
  PangoFBFont *retval;
Elliot Lee's avatar
Elliot Lee committed
226
  PangoFBFontListing *fl = NULL;
227
228
  int i;

229
  retval = g_hash_table_lookup (fbfm->all_fonts, desc);
230

231
  if (retval)
232
    {
233
      g_object_ref (G_OBJECT(retval));
234
      return (PangoFont *)retval;
235
236
    }

237
  for (i = 0; i < fbfm->all_descs->len; i++)
238
    {
239
      fl = g_ptr_array_index (fbfm->all_descs, i);
240

241
      if (pango_font_description_equal_nosize(desc, &fl->desc))
242
243
	break;
    }
244
  
245
  if (i >= fbfm->all_descs->len)
246
247
    return NULL;

248
  retval = (PangoFBFont *)g_object_new (PANGO_TYPE_FB_FONT, NULL);
249

250
251
  retval->desc = *save_as;
  retval->desc.family_name = g_strdup (save_as->family_name);
252
253
  retval->ftf = fl->ftf;

254
  g_hash_table_insert (fbfm->all_fonts, &retval->desc, retval);
255
256
  g_object_ref (G_OBJECT (retval)); /* XXX FIXME: We have to keep the font in the cache
				       forever because I'm too clueless to see
257
				       signals in gobject */
258
259

  return (PangoFont *)retval;
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
  
}

static PangoFont *
pango_fb_font_map_load_font (PangoFontMap *fontmap,
			     const PangoFontDescription *desc)
{
  PangoFont *font;
  const PangoFontDescription *descs;
  const PangoFontDescription *alias;
  int i, j;

  font = pango_fb_lookup_descr (fontmap, desc, desc);

  if (font)
    return font;

  for (i=0;i<ALIAS_TABLE_LEN;i++)
    {
      alias = &alias_table[i].alias;
      
      if (pango_font_description_equal_nosize (desc, alias))
	{
	  descs = &alias_table[i].descriptions[0];
	  j = 0;
	  while ((j < MAX_ALIASES) &&
		 (descs[j].family_name != NULL))
	    {
	      font = pango_fb_lookup_descr (fontmap, &descs[j], desc);
	      if (font)
		return font;
	      j++;
	    }
	  return NULL;
	}
    }
  return NULL;
297
298
299
}

static void
300
301
302
303
list_fonts (PangoFBFontMap *fm,
	    const char *family,
	    GPtrArray *descs,
	    const char *dir)
304
305
306
307
{
  DIR *dirh;
  struct dirent *dent;

308
309
  dirh = opendir (dir);
  if (!dirh)
310
311
    return;

312
  while ((dent = readdir (dirh)))
313
314
315
316
317
318
319
320
    {
      PangoFBFontListing *pfd;
      char *ctmp;
      char buf[1024];
      FT_Face ftf;
      FT_Error ec;
      int i = 0, n = 1;

321
322
323
324
325
      ctmp = strrchr (dent->d_name, '.');
      if (!ctmp ||
	  (strcasecmp (ctmp, ".ttf") &&
	   strcasecmp (ctmp, ".pfa") &&
	   strcasecmp (ctmp, ".pfb")))
326
327
	continue;

328
      g_snprintf (buf, sizeof(buf), "%s/%s", dir, dent->d_name);
329

330
      while (i < n)
331
	{
332
333
	  ec = FT_New_Face (gdk_fb_ft_lib, buf, i, &ftf);
	  if (ec)
334
	    break; /* error opening */
335
336
337

	  n = ftf->num_faces;

338
	  if (!ftf->family_name || !ftf->style_name)
339
	    {
340
341
	      g_warning ("No family/style on %s", buf);
	      FT_Done_Face (ftf);
342
	      break;
343
344
	    }

345
	  pfd = g_new0 (PangoFBFontListing, 1);
346
	  /* Now add the item */
347
348
	  if (ftf->family_name[0] == '/')
	    pfd->desc.family_name = g_strdup (ftf->family_name+1);
349
	  else
350
	    pfd->desc.family_name = g_strdup (ftf->family_name);
351
352
353
354
355
356

	  pfd->desc.style = PANGO_STYLE_NORMAL;
	  pfd->desc.variant = PANGO_VARIANT_NORMAL;
	  pfd->desc.weight = PANGO_WEIGHT_NORMAL;
	  pfd->desc.stretch = PANGO_STRETCH_NORMAL;

357
	  if (ftf->style_name)
358
359
	    {
	      char lcstr[512];
360
361
	      strcpy (lcstr, ftf->style_name);
	      g_strdown (lcstr);
362

363
	      if (strstr (lcstr, "italic"))
364
		pfd->desc.style = PANGO_STYLE_ITALIC;
365
	      else if (strstr (lcstr, "oblique"))
366
367
		pfd->desc.style = PANGO_STYLE_OBLIQUE;

368
	      if (strstr (lcstr, "bold"))
369
370
		pfd->desc.weight = PANGO_WEIGHT_BOLD;

371
	      if (strstr (lcstr, "condensed"))
372
		pfd->desc.stretch = PANGO_STRETCH_CONDENSED;
373
	      else if (strstr (lcstr, "expanded"))
374
375
376
377
378
		pfd->desc.stretch = PANGO_STRETCH_EXPANDED;
	    }

	  pfd->ftf = ftf;

379
	  g_ptr_array_add (descs, pfd);
380
381
382
383

	  i++;
	}
    }
384

385
  closedir (dirh);
386
387
388
}

static guint
389
pango_font_description_hash (gconstpointer a)
390
391
392
{
  const PangoFontDescription *fa = a;

393
  return g_str_hash (fa->family_name) ^ (fa->style + fa->weight + fa->stretch + fa->variant + fa->size);
394
395
396
}

static void
397
pango_fb_font_map_init (PangoFBFontMap *fontmap)
398
399
400
401
402
403
404
405
406
407
{
  static const char *font_dirs[] = {
    "/usr/share/fonts/default/TrueType",
    "/usr/share/fonts/default/Type1",
    "/usr/lib/X11/fonts/TrueType",
    "/usr/lib/X11/fonts/Type1",
    NULL
  };
  int i;

408
409
410
411
  fontmap->all_fonts = g_hash_table_new (pango_font_description_hash, (GEqualFunc)pango_font_description_equal);
  fontmap->all_descs = g_ptr_array_new ();
  for (i = 0; font_dirs[i]; i++)
    list_fonts (fontmap, NULL, fontmap->all_descs, font_dirs[i]);
412
413
414
}

static void
415
416
417
418
pango_fb_font_map_list_fonts (PangoFontMap *fontmap,
			      const gchar *family,
			      PangoFontDescription ***descs,
			      int *n_descs)
419
420
421
422
{
  PangoFBFontMap *fbfm = (PangoFBFontMap *)fontmap;
  int i, n;

423
424
  *descs = g_new (PangoFontDescription *, fbfm->all_descs->len + ALIAS_TABLE_LEN);
  *n_descs = fbfm->all_descs->len + ALIAS_TABLE_LEN;
425

426
  for (i = n = 0; i < fbfm->all_descs->len; i++)
427
    {
428
      PangoFontDescription *pfd = g_ptr_array_index (fbfm->all_descs, i);
429

430
431
432
433
434
435
436
437
438
      if (strcasecmp (family, pfd->family_name))
	continue;

      (*descs)[n++] = pango_font_description_copy (pfd);
    }
  for (i = 0; i < ALIAS_TABLE_LEN; i++)
    {
      PangoFontDescription *pfd = &alias_table[i].alias;

439
      if (strcasecmp (family, pfd->family_name))
440
441
	continue;

442
      (*descs)[n++] = pango_font_description_copy (pfd);
443
444
    }
  *n_descs = n;
445
  *descs = g_realloc (*descs, n * sizeof(PangoFontDescription *));
446
447
448
449
450
451
452
453
}

struct famlist {
  gchar **families;
  int last_added;
};

static void
454
add_entry (gpointer key, gpointer value, gpointer data)
455
456
{
  struct famlist *fl = data;
457
  fl->families[fl->last_added++] = g_strdup (key);
458
459
460
}

static void
461
462
463
pango_fb_font_map_list_families (PangoFontMap *fontmap,
				 gchar ***families,
				 int *n_families)
464
465
{
  PangoFBFontMap *fbfm = (PangoFBFontMap *)fontmap;
466
  GHashTable *thash = g_hash_table_new (g_str_hash, g_str_equal);
467
  struct famlist stickittome;
468
469
  gchar *family_name;
  int i;
470

471
472
  /* Use a temporary hashtable to uniqueify the family names. */
  
473
474
  for(i = 0; i < fbfm->all_descs->len; i++)
    {
475
      PangoFBFontListing *fl = g_ptr_array_index (fbfm->all_descs, i);
476
477
      family_name = fl->desc.family_name;
      g_hash_table_insert (thash, family_name, family_name);
478
    }
479
480
481
482
483
484
485
  
  for(i = 0; i < ALIAS_TABLE_LEN; i++)
    {
      family_name = alias_table[i].alias.family_name;
      g_hash_table_insert (thash, family_name, family_name);
    }
  
486
487
  *n_families = g_hash_table_size (thash);
  *families = g_new (gchar *, *n_families);
488
489
490

  stickittome.families = *families;
  stickittome.last_added = 0;
491
492
  g_hash_table_foreach (thash, add_entry, &stickittome);
  g_hash_table_destroy (thash);
493
494
495
}

static PangoFontMap *
496
pango_fb_font_map (void)
497
{
498
  return g_object_new (pango_fb_font_map_get_type (), NULL);
499
500
501
502
503
504
505
506
507
508
}

PangoMap *
pango_fb_get_shaper_map (const char *lang)
{
  static guint engine_type_id = 0;
  static guint render_type_id = 0;
  
  if (engine_type_id == 0)
    {
509
510
      engine_type_id = g_quark_try_string (PANGO_ENGINE_TYPE_SHAPE);
      render_type_id = g_quark_try_string (PANGO_RENDER_TYPE_FB);
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
    }

  if (engine_type_id == 0)
    {
      engine_type_id = g_quark_from_static_string (PANGO_ENGINE_TYPE_SHAPE);
      render_type_id = g_quark_from_static_string (PANGO_RENDER_TYPE_FB);
    }
  
  return pango_find_map (lang, engine_type_id, render_type_id);
}


/*************** Font impl *****************/
#define PANGO_FB_FONT_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST ((klass), PANGO_TYPE_FB_FONT, PangoFBFontClass))
#define PANGO_FB_IS_FONT(object)        (G_TYPE_CHECK_INSTANCE_TYPE ((object), PANGO_TYPE_FB_FONT))
#define PANGO_FB_IS_FONT_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE ((klass), PANGO_TYPE_FB_FONT))
#define PANGO_FB_FONT_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS ((obj), PANGO_TYPE_FB_FONT, PangoFBFontClass))

typedef struct _PangoFBFontClass   PangoFBFontClass;
typedef struct _PangoFBMetricsInfo PangoFBMetricsInfo;
typedef struct _PangoFBContextInfo PangoFBContextInfo;

struct _PangoFBMetricsInfo
{
  const char *lang;
  PangoFontMetrics metrics;
};

struct _PangoFBContextInfo
{
};

struct _PangoFBFontClass
{
  PangoFontClass parent_class;
};

548
549
550
static void                  pango_fb_font_class_init        (PangoFBFontClass *class);
static void                  pango_fb_font_init              (PangoFBFont      *font);
static void                  pango_fb_font_finalize          (GObject          *object);
551
552
553

static PangoFontDescription *pango_fb_font_describe          (PangoFont        *font);
static PangoCoverage *       pango_fb_font_get_coverage      (PangoFont        *font,
554
							      const char       *lang);
555
static PangoEngineShape *    pango_fb_font_find_shaper       (PangoFont        *font,
556
557
							      const char       *lang,
							      guint32           ch);
558
static void                  pango_fb_font_get_glyph_extents (PangoFont        *font,
559
560
561
							      PangoGlyph        glyph,
							      PangoRectangle   *ink_rect,
							      PangoRectangle   *logical_rect);
562
static void                  pango_fb_font_get_metrics       (PangoFont        *font,
563
564
							      const gchar      *lang,
							      PangoFontMetrics *metrics);
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587

GType
pango_fb_font_get_type (void)
{
  static GType object_type = 0;

  if (!object_type)
    {
      static const GTypeInfo object_info =
      {
        sizeof (PangoFBFontClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) pango_fb_font_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (PangoFBFont),
        0,              /* n_preallocs */
        (GInstanceInitFunc) pango_fb_font_init,
      };
      
      object_type = g_type_register_static (PANGO_TYPE_FONT,
                                            "PangoFBFont",
588
589
                                            &object_info,
					    0);
590
591
592
593
594
595
596
597
598
    }
  
  return object_type;
}

static void 
pango_fb_font_init (PangoFBFont *font)
{
  font->desc.size = -1;
599
  font->glyph_info = g_hash_table_new (NULL, NULL);
600
601
602
603
604
}

static gboolean
g_free_2(gpointer key, gpointer value, gpointer data)
{
605
  PangoFBGlyphInfo *pgi = value;
606
607
  g_free (pgi->fbd.drawable_data.mem);
  g_free (value);
608
609
610
611
612
613
  return TRUE;
}

static void
pango_fb_font_clear_extent_cache(PangoFBFont *fbf)
{
614
  g_hash_table_foreach_remove (fbf->glyph_info, g_free_2, NULL);
615
616
617
}

PangoFBGlyphInfo *
618
pango_fb_font_get_glyph_info (PangoFont *font, PangoGlyph glyph)
619
620
{
  PangoFBGlyphInfo *pgi;
621
  PangoFBFont *fbf = PANGO_FB_FONT (font);
622
623
624
625
  FT_Bitmap *renderme;
  FT_GlyphSlot g;
  PangoRectangle *my_logical_rect, *my_ink_rect;
  FT_Face ftf;
626
  FT_Error ec;
627
  
628
629
  ftf = fbf->ftf;

630
  pango_fb_font_set_size (font);
631

632
633
  pgi = g_hash_table_lookup (fbf->glyph_info, GUINT_TO_POINTER (glyph));
  if (pgi)
634
635
    return pgi;

636
  pgi = g_new0 (PangoFBGlyphInfo, 1);
637

638
  ec = FT_Load_Glyph (ftf, glyph, FT_LOAD_DEFAULT);
639
640
641

  g = ftf->glyph;

642
  if (g->format != ft_glyph_format_bitmap)
643
644
645
646
647
648
    if ((ec = FT_Render_Glyph(g, ft_render_mode_normal)))
      g_warning ("Glyph render failed: %d", ec);
  
  renderme = &g->bitmap;
  pgi->top = g->bitmap_top;
  pgi->left = g->bitmap_left;
649

650
  pgi->fbd.drawable_data.mem = g_memdup (renderme->buffer, renderme->pitch * renderme->rows);
651
652
653
654
  pgi->fbd.drawable_data.rowstride = renderme->pitch;
  pgi->fbd.drawable_data.width = pgi->fbd.drawable_data.lim_x = renderme->width;
  pgi->fbd.drawable_data.height = pgi->fbd.drawable_data.lim_y = renderme->rows;

655
  switch (renderme->pixel_mode)
656
657
658
659
660
661
662
663
    {
    case ft_pixel_mode_mono:
      pgi->fbd.drawable_data.depth = 1;
      break;
    case ft_pixel_mode_grays:
      pgi->fbd.drawable_data.depth = 78;
      break;
    default:
664
      g_assert_not_reached ();
665
666
667
668
669
670
671
      break;
    }

  my_ink_rect = &pgi->extents[0];
  my_logical_rect = &pgi->extents[1];

  {
672
673
674
675
    my_ink_rect->width = (PANGO_SCALE * g->metrics.width + 32) >> 6;
    my_ink_rect->height = (PANGO_SCALE * g->metrics.height + 32) >> 6;
    my_ink_rect->x = - ((PANGO_SCALE * g->metrics.horiBearingX + 32) >> 6);
    my_ink_rect->y = - ((PANGO_SCALE * g->metrics.horiBearingY + 32) >> 6);
676
677
678
  }

  {
679
680
681
682
    my_logical_rect->width = (PANGO_SCALE * g->metrics.horiAdvance + 32) >> 6;
    my_logical_rect->height = (PANGO_SCALE * ftf->size->metrics.height + 32) >> 6;
    my_logical_rect->x = - ((PANGO_SCALE * g->metrics.horiBearingX + 32) >> 6);
    my_logical_rect->y = - ((PANGO_SCALE * ftf->size->metrics.ascender + 32) >> 6);
683
684
  }

685
  g_hash_table_insert (fbf->glyph_info, GUINT_TO_POINTER(glyph), pgi);
686
687

  return pgi;
688
689
}

690
static void pango_fb_font_finalize (GObject         *object)
691
{
692
  PangoFBFont *fbf = PANGO_FB_FONT (object);
693
694

  /* XXX FIXME g_hash_table_remove(ourfontmap, &fbf->desc); */
695
696
697
698
  pango_coverage_unref (fbf->coverage);
  g_free (fbf->desc.family_name);
  pango_fb_font_clear_extent_cache (fbf);
  g_hash_table_destroy (fbf->glyph_info);
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
}

static void
pango_fb_font_class_init (PangoFBFontClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  PangoFontClass *font_class = PANGO_FONT_CLASS (class);
  
  object_class->finalize = pango_fb_font_finalize;
  font_class->describe = pango_fb_font_describe;
  font_class->get_coverage = pango_fb_font_get_coverage;
  font_class->find_shaper = pango_fb_font_find_shaper;
  font_class->get_glyph_extents = pango_fb_font_get_glyph_extents;
  font_class->get_metrics = pango_fb_font_get_metrics;
}

static PangoFontDescription *
716
pango_fb_font_describe (PangoFont *font)
717
{
718
  return pango_font_description_copy (&PANGO_FB_FONT(font)->desc);
719
720
721
722
723
724
725
726
727
728
729
}

static void
free_coverages_foreach (gpointer key,
			gpointer value,
			gpointer data)
{
  pango_coverage_unref (value);
}

static PangoCoverage *
730
731
pango_fb_font_get_coverage (PangoFont        *font,
			    const char       *lang)
732
733
734
735
736
737
{
  int i, n;
  PangoCoverage *retval;
  PangoMap *shape_map;
  GHashTable *coverage_hash;

738
739
  if (PANGO_FB_FONT (font)->coverage)
    return pango_coverage_ref (PANGO_FB_FONT (font)->coverage);
740

741
  shape_map = pango_fb_get_shaper_map (lang);
742
743
744

  coverage_hash = g_hash_table_new (g_str_hash, g_str_equal);

745
746
  retval = pango_coverage_new ();
  n = MIN( 65536, PANGO_FB_FONT (font)->ftf->num_glyphs);
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
  for(i = 0; i < n; i++)
    {
      PangoCoverageLevel font_level;
      PangoMapEntry *map_entry;

      map_entry = pango_map_get_entry (shape_map, i);

      if (map_entry->info)
	{
	  PangoCoverage *coverage;
	  coverage = g_hash_table_lookup (coverage_hash, map_entry->info->id);
	  if (!coverage)
	    {
	      PangoEngineShape *engine = (PangoEngineShape *)pango_map_get_engine (shape_map, i);

	      coverage = engine->get_coverage (font, lang);
	      g_hash_table_insert (coverage_hash, map_entry->info->id, coverage);
	    }
	  
	  font_level = pango_coverage_get (coverage, i);
	  if (font_level == PANGO_COVERAGE_EXACT && !map_entry->is_exact)
	    font_level = PANGO_COVERAGE_APPROXIMATE;

	  if (font_level != PANGO_COVERAGE_NONE)
	    pango_coverage_set (retval, i, font_level);
	}
    }

  g_hash_table_foreach (coverage_hash, free_coverages_foreach, NULL);
  g_hash_table_destroy (coverage_hash);

778
  PANGO_FB_FONT (font)->coverage = pango_coverage_ref (retval);
779
780
781
782
783

  return retval;
}

static PangoEngineShape *
784
785
786
pango_fb_font_find_shaper (PangoFont        *font,
			   const char       *lang,
			   guint32           ch)
787
788
789
790
791
792
793
794
{
  PangoMap *shape_map = NULL;

  shape_map = pango_fb_get_shaper_map (lang);
  return (PangoEngineShape *)pango_map_get_engine (shape_map, ch);
}

void
795
pango_fb_font_set_size (PangoFont *font)
796
797
798
{
  PangoFBFont *fbf = (PangoFBFont *)font;

799
  if (PANGO_FB_FONT (font)->desc.size != GPOINTER_TO_UINT (fbf->ftf->generic.data))
800
    {
801
      fbf->ftf->generic.data = GUINT_TO_POINTER (PANGO_FB_FONT (font)->desc.size);
802
      FT_Set_Char_Size (fbf->ftf, 0, PANGO_PIXELS (PANGO_FB_FONT (font)->desc.size << 6), 72, 72);
803
804
805
806
807
808
809
810
811
812
    }
}

static void
pango_fb_font_get_glyph_extents (PangoFont        *font,
				 PangoGlyph        glyph,
				 PangoRectangle   *ink_rect,
				 PangoRectangle   *logical_rect)
{
  PangoFBFont *fbf;
813
814
  PangoRectangle *my_extents;
  PangoFBGlyphInfo *gi;
815

816
  fbf = PANGO_FB_FONT (font);
817

818
  pango_fb_font_set_size (font);
819

820
  gi = pango_fb_font_get_glyph_info (font, glyph);
821
  my_extents = gi->extents;
822

823
  if (ink_rect)
824
825
    *ink_rect = my_extents[0];

826
  if (logical_rect)
827
828
829
830
    *logical_rect = my_extents[1];
}

static void
831
832
833
pango_fb_font_get_metrics (PangoFont        *font,
			   const gchar      *lang,
			   PangoFontMetrics *metrics)
834
835
836
{
  FT_Face ftf;

837
  ftf = PANGO_FB_FONT (font)->ftf;
838

839
840
  pango_fb_font_set_size (font);

841
  if (metrics)
842
    {
843
      metrics->ascent = (ftf->size->metrics.ascender * PANGO_SCALE + 32) >> 6;
Alexander Larsson's avatar
Alexander Larsson committed
844
      metrics->descent = ((-ftf->size->metrics.descender) * PANGO_SCALE + 32) >> 6;
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
    }
}

/* The following array is supposed to contain enough text to tickle all necessary fonts for each
 * of the languages in the following. Yes, it's pretty lame. Not all of the languages
 * in the following have sufficient text to excercise all the accents for the language, and
 * there are obviously many more languages to include as well.
 */
#if 0
LangInfo lang_texts[] = {
  { "ar", "Arabic  السلام عليكم" },
  { "cs", "Czech (česky)  Dobrý den" },
  { "da", "Danish (Dansk)  Hej, Goddag" },
  { "el", "Greek (Ελληνικά) Γειά σας" },
  { "en", "English Hello" },
  { "eo", "Esperanto Saluton" },
  { "es", "Spanish (Español) ¡Hola!" },
  { "et", "Estonian  Tere, Tervist" },
  { "fi", "Finnish (Suomi)  Hei" },
  { "fr", "French (Français)" },
  { "de", "German Grüß Gott" },
  { "iw", "Hebrew   שלום" },
  { "il", "Italiano  Ciao, Buon giorno" },
  { "ja", "Japanese (日本語) こんにちは, コンニチハ" },
  { "ko", "Korean (한글)   안녕하세요, 안녕하십니까" },
  { "mt", "Maltese   Ċaw, Saħħa" },
  { "nl", "Nederlands, Vlaams Hallo, Dag" },
  { "no", "Norwegian (Norsk) Hei, God dag" },
  { "pl", "Polish   Dzień dobry, Hej" },
  { "ru", "Russian (Русский)" },
  { "sk", "Slovak   Dobrý deň" },
  { "sv", "Swedish (Svenska) Hej, Goddag" },
  { "tr", "Turkish (Türkçe) Merhaba" },
  { "zh", "Chinese (中文,普通话,汉语)" }
};
#endif

/******** misc gdk tie-ins **********/
PangoContext *
gdk_pango_context_get (void)
{
  PangoContext *retval;
  static PangoFontMap *font_map = NULL;

889
  if (!font_map)
890
891
    font_map = pango_fb_font_map ();

892
  retval = pango_context_new ();
893
894
895
896
897

  pango_context_add_font_map (retval, font_map);

  return retval;
}