summaryrefslogtreecommitdiff
path: root/pango/pangoatsui-fontmap.c
blob: ee153074b3aae2ef39d864d1899a8023d82c6311 (plain)
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
/* Pango
 * pangoatsui-fontmap.c
 *
 * Copyright (C) 2000-2003 Red Hat, Inc.
 * Copyright (C) 2005-2007 Imendio AB
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>

#include "pango-fontmap.h"
#include "pangoatsui-private.h"
#include "pango-impl-utils.h"
#include "modules.h"

#import <Cocoa/Cocoa.h>

typedef struct _FontHashKey      FontHashKey;

struct _PangoATSUIFamily
{
  PangoFontFamily parent_instance;

  char *family_name;

  PangoFontFace **faces;
  gint n_faces;
};

#define PANGO_TYPE_ATSUI_FAMILY              (pango_atsui_family_get_type ())
#define PANGO_ATSUI_FAMILY(object)           (G_TYPE_CHECK_INSTANCE_CAST ((object), PANGO_TYPE_ATSUI_FAMILY, PangoATSUIFamily))
#define PANGO_ATSUI_IS_FAMILY(object)        (G_TYPE_CHECK_INSTANCE_TYPE ((object), PANGO_TYPE_ATSUI_FAMILY))

#define PANGO_TYPE_ATSUI_FACE              (pango_atsui_face_get_type ())
#define PANGO_ATSUI_FACE(object)           (G_TYPE_CHECK_INSTANCE_CAST ((object), PANGO_TYPE_ATSUI_FACE, PangoATSUIFace))
#define PANGO_ATSUI_IS_FACE(object)        (G_TYPE_CHECK_INSTANCE_TYPE ((object), PANGO_TYPE_ATSUI_FACE))

struct _PangoATSUIFace
{
  PangoFontFace parent_instance;

  PangoATSUIFamily *family;

  PangoCoverage *coverage;

  char *postscript_name;
  char *style_name;

  int weight;
  int traits;
  guint synthetic_italic : 1;
};

static GType pango_atsui_family_get_type (void);
static GType pango_atsui_face_get_type (void);

static gpointer pango_atsui_family_parent_class;
static gpointer pango_atsui_face_parent_class;

static const char *
get_real_family (const char *family_name)
{
  switch (family_name[0])
    {
    case 'm':
    case 'M':
      if (g_ascii_strcasecmp (family_name, "monospace") == 0)
	return "Courier";
      break;
    case 's':
    case 'S':
      if (g_ascii_strcasecmp (family_name, "sans") == 0)
	return "Helvetica";
      else if (g_ascii_strcasecmp (family_name, "serif") == 0)
	return "Times";
      break;
    }

  return family_name;
}

static void
pango_atsui_family_list_faces (PangoFontFamily  *family,
			       PangoFontFace  ***faces,
			       int              *n_faces)
{
  PangoATSUIFamily *atsuifamily = PANGO_ATSUI_FAMILY (family);

  if (atsuifamily->n_faces < 0)
    {
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
      const char *real_family = get_real_family (atsuifamily->family_name);
      NSFontManager *manager = [NSFontManager sharedFontManager];
      NSArray *members = [manager availableMembersOfFontFamily:[NSString stringWithUTF8String:real_family]];
      int i, count;
      GHashTable *hash_table;
      GList *faces = NULL, *l;
      GList *synthetic_faces = NULL;

      /* The NSFontManager API returns italic faces for some families
       * even if they don't exist. When using Cocoa to create
       * instances of those fonts, Cocoa synthesizes italic versions
       * by applying a shear transformation. We do that manually for
       * those fonts in pangocairo-atsuifont.c. For many other fonts,
       * there is no italic face at all, so we create synthesized
       * versions of those like in the win32 and fontconfig backends.
       */
      hash_table = g_hash_table_new (g_direct_hash, g_direct_equal);

      count = [members count];
      for (i = 0; i < count; i++)
	{
	  PangoATSUIFace *face = g_object_new (PANGO_TYPE_ATSUI_FACE, NULL);
	  NSArray *font_array = [members objectAtIndex:i];

	  face->family = atsuifamily;
	  face->postscript_name = g_strdup ([[font_array objectAtIndex:0] UTF8String]);
	  face->style_name = g_strdup ([[font_array objectAtIndex:1] UTF8String]);
	  face->weight = [[font_array objectAtIndex:2] intValue];
	  face->traits = [[font_array objectAtIndex:3] intValue];

	  faces = g_list_prepend (faces, face);

	  if (face->traits & NSItalicFontMask)
	    g_hash_table_insert (hash_table, GINT_TO_POINTER (face->weight), face);
	}

      for (l = faces; l; l = l->next)
	{
	  PangoATSUIFace *face = l->data;

	  if (!g_hash_table_lookup (hash_table, GINT_TO_POINTER (face->weight)))
	    {
	      PangoATSUIFace *italic_face = g_object_new (PANGO_TYPE_ATSUI_FACE, NULL);

	      italic_face->family = atsuifamily;
	      italic_face->postscript_name = g_strdup (face->postscript_name);
	      italic_face->weight = face->weight;
	      italic_face->traits = face->traits | NSItalicFontMask;
	      italic_face->synthetic_italic = TRUE;

	      /* Try to create a sensible face name. */
	      if (strcasecmp (face->style_name, "regular") == 0)
		italic_face->style_name = g_strdup ("Oblique");
	      else 
		italic_face->style_name = g_strdup_printf ("%s Oblique", face->style_name);

	      synthetic_faces = g_list_prepend (synthetic_faces, italic_face);
	    }
	}

      faces = g_list_concat (faces, synthetic_faces);

      atsuifamily->n_faces = g_list_length (faces);
      atsuifamily->faces = g_new (PangoFontFace *, atsuifamily->n_faces);

      for (l = faces, i = 0; l; l = l->next, i++)
	atsuifamily->faces[i] = l->data;

      g_list_free (faces);
      g_hash_table_destroy (hash_table);

      [pool release];
    }

  if (n_faces)
    *n_faces = atsuifamily->n_faces;

  if (faces)
    *faces = g_memdup (atsuifamily->faces, atsuifamily->n_faces * sizeof (PangoFontFace *));
}

static const char *
pango_atsui_family_get_name (PangoFontFamily *family)
{
  PangoATSUIFamily *atsuifamily = PANGO_ATSUI_FAMILY (family);

  return atsuifamily->family_name;
}

static gboolean
pango_atsui_family_is_monospace (PangoFontFamily *family)
{
  /* Fixme: Implement */
  return FALSE;
}

static void
pango_atsui_family_finalize (GObject *object)
{
  PangoATSUIFamily *family = PANGO_ATSUI_FAMILY (object);
  int i;

  g_free (family->family_name);

  if (family->n_faces != -1)
    {
      for (i = 0; i < family->n_faces; i++)
	g_object_unref (family->faces[i]);

      g_free (family->faces);
    }

  G_OBJECT_CLASS (pango_atsui_family_parent_class)->finalize (object);
}

static void
pango_atsui_family_class_init (PangoFontFamilyClass *class)
{
  GObjectClass *object_class = (GObjectClass *)class;
  int i;

  pango_atsui_family_parent_class = g_type_class_peek_parent (class);

  object_class->finalize = pango_atsui_family_finalize;

  class->list_faces = pango_atsui_family_list_faces;
  class->get_name = pango_atsui_family_get_name;
  class->is_monospace = pango_atsui_family_is_monospace;

  for (i = 0; _pango_included_atsui_modules[i].list; i++)
    pango_module_register (&_pango_included_atsui_modules[i]);
}

static void
pango_atsui_family_init (PangoATSUIFamily *family)
{
  family->n_faces = -1;
}

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

  if (!object_type)
    {
      const GTypeInfo object_info =
      {
	sizeof (PangoFontFamilyClass),
	(GBaseInitFunc) NULL,
	(GBaseFinalizeFunc) NULL,
	(GClassInitFunc) pango_atsui_family_class_init,
	NULL,           /* class_finalize */
	NULL,           /* class_data */
	sizeof (PangoATSUIFamily),
	0,              /* n_preallocs */
	(GInstanceInitFunc) pango_atsui_family_init,
      };

      object_type = g_type_register_static (PANGO_TYPE_FONT_FAMILY,
					    I_("PangoATSUIFamily"),
					    &object_info, 0);
    }

  return object_type;
}

static PangoFontDescription *
pango_atsui_face_describe (PangoFontFace *face)
{
  PangoATSUIFace *atsuiface = PANGO_ATSUI_FACE (face);
  PangoFontDescription *description;
  PangoWeight pango_weight;
  PangoStyle pango_style;
  PangoVariant pango_variant;
  int weight;

  description = pango_font_description_new ();

  pango_font_description_set_family (description, atsuiface->family->family_name);

  weight = atsuiface->weight;

  if (weight == 1 || weight == 2)
    pango_weight = PANGO_WEIGHT_ULTRALIGHT;
  else if (weight == 3 || weight == 4)
    pango_weight = PANGO_WEIGHT_LIGHT;
  else if (weight == 5 || weight == 6)
    pango_weight = PANGO_WEIGHT_NORMAL;
  else if (weight == 7 || weight == 8)
    pango_weight = PANGO_WEIGHT_SEMIBOLD;
  else if (weight == 9 || weight == 10)
    pango_weight = PANGO_WEIGHT_BOLD;
  else if (weight == 11 || weight == 12)
    pango_weight = PANGO_WEIGHT_ULTRABOLD;
  else if (weight == 13 || weight == 14)
    pango_weight = PANGO_WEIGHT_HEAVY;
  else
    g_assert_not_reached ();

  if (atsuiface->traits & NSItalicFontMask)
    pango_style = PANGO_STYLE_ITALIC;
  else
    pango_style = PANGO_STYLE_NORMAL;

  if (atsuiface->traits & NSSmallCapsFontMask)
    pango_variant = PANGO_VARIANT_SMALL_CAPS;
  else
    pango_variant = PANGO_VARIANT_NORMAL;

  pango_font_description_set_weight (description, pango_weight);
  pango_font_description_set_style (description, pango_style);
  pango_font_description_set_variant (description, pango_variant);

  return description;
}

static const char *
pango_atsui_face_get_face_name (PangoFontFace *face)
{
  PangoATSUIFace *atsuiface = PANGO_ATSUI_FACE (face);

  return atsuiface->style_name;
}

static void
pango_atsui_face_list_sizes (PangoFontFace  *face,
			     int           **sizes,
			     int            *n_sizes)
{
  *n_sizes = 0;
  *sizes = NULL;
}

static void
pango_atsui_face_finalize (GObject *object)
{
  PangoATSUIFace *atsuiface = PANGO_ATSUI_FACE (object);

  if (atsuiface->coverage)
    pango_coverage_unref (atsuiface->coverage);

  g_free (atsuiface->postscript_name);
  g_free (atsuiface->style_name);

  G_OBJECT_CLASS (pango_atsui_face_parent_class)->finalize (object);
}

static void
pango_atsui_face_class_init (PangoFontFaceClass *class)
{
  GObjectClass *object_class = (GObjectClass *)class;

  pango_atsui_face_parent_class = g_type_class_peek_parent (class);

  object_class->finalize = pango_atsui_face_finalize;

  class->describe = pango_atsui_face_describe;
  class->get_face_name = pango_atsui_face_get_face_name;
  class->list_sizes = pango_atsui_face_list_sizes;
}

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

  if (!object_type)
    {
      const GTypeInfo object_info =
      {
	sizeof (PangoFontFaceClass),
	(GBaseInitFunc) NULL,
	(GBaseFinalizeFunc) NULL,
	(GClassInitFunc) pango_atsui_face_class_init,
	NULL,           /* class_finalize */
	NULL,           /* class_data */
	sizeof (PangoATSUIFace),
	0,              /* n_preallocs */
	(GInstanceInitFunc) NULL,
      };

      object_type = g_type_register_static (PANGO_TYPE_FONT_FACE,
					    I_("PangoATSUIFace"),
					    &object_info, 0);
    }

  return object_type;
}

const char *
_pango_atsui_face_get_postscript_name (PangoATSUIFace *face)
{
  return face->postscript_name;
}

gboolean
_pango_atsui_face_get_synthetic_italic (PangoATSUIFace *face)
{
  return face->synthetic_italic;
}

PangoCoverage *
_pango_atsui_face_get_coverage (PangoATSUIFace *face,
				PangoLanguage  *language)
{
  int i;

  /* Note: We currently fake the coverage by reporting that the 255 first 
   * glyphs exist in all fonts, which is not true. It's unclear how to get
   * this done correctly.
   */

  if (face->coverage)
    return face->coverage;

  face->coverage = pango_coverage_new ();

  for (i = 0; i < 256; i++)
    pango_coverage_set (face->coverage, i, PANGO_COVERAGE_EXACT);

  return face->coverage;
}

static void pango_atsui_font_map_class_init (PangoATSUIFontMapClass *class);
static void pango_atsui_font_map_init (PangoATSUIFontMap *atsuifontmap);

static guint    font_hash_key_hash  (const FontHashKey *key);
static gboolean font_hash_key_equal (const FontHashKey *key_a,
				     const FontHashKey *key_b);
static void     font_hash_key_free  (FontHashKey       *key);

G_DEFINE_TYPE (PangoATSUIFontMap, pango_atsui_font_map, PANGO_TYPE_FONT_MAP);

static void
pango_atsui_font_map_finalize (GObject *object)
{
  PangoATSUIFontMap *fontmap = PANGO_ATSUI_FONT_MAP (object);

  g_hash_table_destroy (fontmap->font_hash);
  g_hash_table_destroy (fontmap->families);

  G_OBJECT_CLASS (pango_atsui_font_map_parent_class)->finalize (object);
}

struct _FontHashKey {
  PangoATSUIFontMap *fontmap;
  PangoMatrix matrix;
  PangoFontDescription *desc;
  char *postscript_name;
  gpointer context_key;
};

/* Fowler / Noll / Vo (FNV) Hash (http://www.isthe.com/chongo/tech/comp/fnv/)
 *
 * Not necessarily better than a lot of other hashes, but should be OK, and
 * well tested with binary data.
 */

#define FNV_32_PRIME ((guint32)0x01000193)
#define FNV1_32_INIT ((guint32)0x811c9dc5)

static guint32
hash_bytes_fnv (unsigned char *buffer,
		int            len,
		guint32        hval)
{
  while (len--)
    {
      hval *= FNV_32_PRIME;
      hval ^= *buffer++;
    }

  return hval;
}

static gboolean
font_hash_key_equal (const FontHashKey *key_a,
		     const FontHashKey *key_b)
{
  if (key_a->matrix.xx == key_b->matrix.xx &&
      key_a->matrix.xy == key_b->matrix.xy &&
      key_a->matrix.yx == key_b->matrix.yx &&
      key_a->matrix.yy == key_b->matrix.yy &&
      pango_font_description_equal (key_a->desc, key_b->desc) &&
      strcmp (key_a->postscript_name, key_b->postscript_name) == 0)
    {
      if (key_a->context_key)
	return PANGO_ATSUI_FONT_MAP_GET_CLASS (key_a->fontmap)->context_key_equal (key_a->fontmap,
										key_a->context_key,
										key_b->context_key);
      else
	return TRUE;
    }
  else
    return FALSE;
}

static guint
font_hash_key_hash (const FontHashKey *key)
{
    guint32 hash = FNV1_32_INIT;

    /* We do a bytewise hash on the context matrix */
    hash = hash_bytes_fnv ((unsigned char *)(&key->matrix),
			   sizeof(double) * 4,
			   hash);

    if (key->context_key)
      hash ^= PANGO_ATSUI_FONT_MAP_GET_CLASS (key->fontmap)->context_key_hash (key->fontmap,
									       key->context_key);

    hash ^= g_str_hash (key->postscript_name);

    return (hash ^ pango_font_description_hash (key->desc));
}

static void
font_hash_key_free (FontHashKey *key)
{
  if (key->context_key)
    PANGO_ATSUI_FONT_MAP_GET_CLASS (key->fontmap)->context_key_free (key->fontmap,
								     key->context_key);

  g_slice_free (FontHashKey, key);
}

static FontHashKey *
font_hash_key_copy (FontHashKey *old)
{
  FontHashKey *key = g_slice_new (FontHashKey);

  key->fontmap = old->fontmap;
  key->matrix = old->matrix;
  key->desc = pango_font_description_copy (old->desc);
  key->postscript_name = g_strdup (old->postscript_name);
  if (old->context_key)
    key->context_key = PANGO_ATSUI_FONT_MAP_GET_CLASS (key->fontmap)->context_key_copy (key->fontmap,
											old->context_key);
  else
    key->context_key = NULL;

  return key;
}


static void
get_context_matrix (PangoContext *context,
		    PangoMatrix *matrix)
{
  const PangoMatrix *set_matrix;
  static const PangoMatrix identity = PANGO_MATRIX_INIT;

  if (context)
    set_matrix = pango_context_get_matrix (context);
  else
    set_matrix = NULL;

  if (set_matrix)
    *matrix = *set_matrix;
  else
    *matrix = identity;
}

static void
font_hash_key_for_context (PangoATSUIFontMap *fcfontmap,
			   PangoContext   *context,
			   FontHashKey    *key)
{
  key->fontmap = fcfontmap;
  get_context_matrix (context, &key->matrix);

  if (PANGO_ATSUI_FONT_MAP_GET_CLASS (fcfontmap)->context_key_get)
    key->context_key = (gpointer)PANGO_ATSUI_FONT_MAP_GET_CLASS (fcfontmap)->context_key_get (fcfontmap, context);
  else
    key->context_key = NULL;
}

static void
pango_atsui_font_map_add (PangoATSUIFontMap *atsuifontmap,
			  PangoContext      *context,
			  PangoATSUIFont    *atsuifont)
{
  FontHashKey key;
  FontHashKey *key_copy;

  g_assert (atsuifont->fontmap == NULL);

  atsuifont->fontmap = g_object_ref (atsuifontmap);

  font_hash_key_for_context (atsuifontmap, context, &key);
  key.postscript_name = (char *)_pango_atsui_face_get_postscript_name (atsuifont->face);
  key.desc = atsuifont->desc;

  key_copy = font_hash_key_copy (&key);
  atsuifont->context_key = key_copy->context_key;
  atsuifont->matrix = key.matrix;
  g_hash_table_insert (atsuifontmap->font_hash, key_copy, g_object_ref (atsuifont));
}

static PangoATSUIFont *
pango_atsui_font_map_lookup (PangoATSUIFontMap    *atsuifontmap,
			     PangoContext         *context,
			     PangoFontDescription *desc,
			     PangoATSUIFace       *face)
{
  FontHashKey key;

  font_hash_key_for_context (atsuifontmap, context, &key);
  key.postscript_name = (char *)_pango_atsui_face_get_postscript_name (face);
  key.desc = desc;

  return g_hash_table_lookup (atsuifontmap->font_hash, &key);
}

static gboolean
find_best_match (PangoATSUIFamily            *font_family,
		 const PangoFontDescription  *description,
		 PangoFontDescription       **best_description,
		 PangoATSUIFace             **best_face)
{
  PangoFontDescription *new_desc;
  int i;

  *best_description = NULL;
  *best_face = NULL;

  for (i = 0; i < font_family->n_faces; i++)
    {
      new_desc = pango_font_face_describe (font_family->faces[i]);

      if (pango_font_description_better_match (description, *best_description, new_desc))
	{
	  pango_font_description_free (*best_description);
	  *best_description = new_desc;
	  *best_face = (PangoATSUIFace *)font_family->faces[i];
	}
      else
	pango_font_description_free (new_desc);
    }

  if (*best_description)
    return TRUE;

  return FALSE;
}

static PangoFont *
pango_atsui_font_map_load_font (PangoFontMap               *fontmap,
				PangoContext               *context,
				const PangoFontDescription *description)
{
  PangoATSUIFontMap *atsuifontmap = (PangoATSUIFontMap *)fontmap;
  PangoATSUIFamily *font_family;
  gchar *name;
  gint size;

  size = pango_font_description_get_size (description);
  if (size < 0)
    return NULL;

  name = g_utf8_casefold (pango_font_description_get_family (description), -1);
  font_family = g_hash_table_lookup (atsuifontmap->families, name);
  g_free (name);

  if (font_family)
    {
      PangoFontDescription *best_description;
      PangoATSUIFace *best_face;
      PangoATSUIFont *best_font;

      /* Force a listing of the available faces */
      pango_font_family_list_faces ((PangoFontFamily *)font_family, NULL, NULL);

      if (!find_best_match (font_family, description, &best_description, &best_face))
	return NULL;
      
      pango_font_description_set_size (best_description, size);

      best_font = pango_atsui_font_map_lookup (atsuifontmap, 
					       context, 
					       best_description, 
					       best_face);

      if (best_font)
	g_object_ref (best_font);
      else
	{
	  PangoATSUIFontMapClass *klass;

	  klass = PANGO_ATSUI_FONT_MAP_GET_CLASS (atsuifontmap);
	  best_font = klass->create_font (atsuifontmap, context, 
					  best_face, best_description);
	  
	  if (best_font)
	    pango_atsui_font_map_add (atsuifontmap, context, best_font);
	    /* TODO: Handle the else case here. */
	}

      pango_font_description_free (best_description);

      return (PangoFont *)best_font;
    }

  return NULL;
}

static void
list_families_foreach (gpointer key,
		       gpointer value,
		       gpointer user_data)
{
  GSList **list = user_data;

  *list = g_slist_prepend (*list, value);
}

static void
pango_atsui_font_map_list_families (PangoFontMap      *fontmap,
				    PangoFontFamily ***families,
				    int               *n_families)
{
  GSList *family_list = NULL;
  GSList *tmp_list;
  PangoATSUIFontMap *atsuifontmap = (PangoATSUIFontMap *)fontmap;

  if (!n_families)
    return;

  g_hash_table_foreach (atsuifontmap->families, list_families_foreach, &family_list);

  *n_families = g_slist_length (family_list);

  if (families)
    {
      int i = 0;

      *families = g_new (PangoFontFamily *, *n_families);

      tmp_list = family_list;
      while (tmp_list)
	{
	  (*families)[i] = tmp_list->data;
	  i++;
	  tmp_list = tmp_list->next;
	}
    }

  g_slist_free (family_list);
}

static void
pango_atsui_font_map_init (PangoATSUIFontMap *atsuifontmap)
{
  NSArray *family_array;
  NSAutoreleasePool *pool;
  PangoATSUIFamily *family;

  int size, i;

  atsuifontmap->families = g_hash_table_new_full (g_str_hash, g_str_equal,
						  g_free, g_object_unref);


  atsuifontmap->font_hash = g_hash_table_new_full ((GHashFunc)font_hash_key_hash,
						   (GEqualFunc)font_hash_key_equal,
						   (GDestroyNotify)font_hash_key_free,
						   NULL);

  pool = [[NSAutoreleasePool alloc] init];
  family_array = [[NSFontManager sharedFontManager] availableFontFamilies];
  size = [family_array count];

  for (i = 0; i < size; i++) {
    NSString *family_name = [family_array objectAtIndex:i];

    family = g_object_new (PANGO_TYPE_ATSUI_FAMILY, NULL);
    family->family_name = g_strdup ([family_name UTF8String]);

    g_hash_table_insert (atsuifontmap->families, g_utf8_casefold (family->family_name, -1), family);
  }

  /* Insert aliases */
  family = g_object_new (PANGO_TYPE_ATSUI_FAMILY, NULL);
  family->family_name = g_strdup ("Sans");
  g_hash_table_insert (atsuifontmap->families, g_utf8_casefold (family->family_name, -1), family);

  family = g_object_new (PANGO_TYPE_ATSUI_FAMILY, NULL);
  family->family_name = g_strdup ("Serif");
  g_hash_table_insert (atsuifontmap->families, g_utf8_casefold (family->family_name, -1), family);

  family = g_object_new (PANGO_TYPE_ATSUI_FAMILY, NULL);
  family->family_name = g_strdup ("Monospace");
  g_hash_table_insert (atsuifontmap->families, g_utf8_casefold (family->family_name, -1), family);

  [pool release];
}

static void
pango_atsui_font_map_class_init (PangoATSUIFontMapClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  PangoFontMapClass *fontmap_class = PANGO_FONT_MAP_CLASS (class);

  object_class->finalize = pango_atsui_font_map_finalize;

  fontmap_class->load_font = pango_atsui_font_map_load_font;
  fontmap_class->list_families = pango_atsui_font_map_list_families;
  fontmap_class->shape_engine_type = PANGO_RENDER_TYPE_ATSUI;
}