summaryrefslogtreecommitdiff
path: root/pango/pango-context.c
blob: 7f5e078f9d925d5b29304bd2ba56abb9df6535f5 (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
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
/* Pango
 * pango-context.c: Contexts for itemization and shaping
 *
 * Copyright (C) 2000 Red Hat Software
 *
 * 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 <string.h>
#include <stdlib.h>

#include "pango/pango-context.h"
#include "pango/pango-utils.h"

#include "pango-modules.h"

struct _PangoContext
{
  GObject parent_instance;

  PangoLanguage *language;
  PangoDirection base_dir;
  PangoFontDescription *font_desc;

  GSList *font_maps;
};

struct _PangoContextClass
{
  GObjectClass parent_class;
  
};

static void add_engines (PangoContext      *context,
			 const gchar       *text,
                         gint               start_index,
			 gint               length,
			 PangoAttrList     *attrs,
                         PangoAttrIterator *cached_iter,
                         gint               n_chars,
			 PangoAnalysis     *analyses);

static void pango_context_init        (PangoContext      *context);
static void pango_context_class_init  (PangoContextClass *klass);
static void pango_context_finalize    (GObject           *object);

static gpointer parent_class;

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

  if (!object_type)
    {
      static const GTypeInfo object_info =
      {
        sizeof (PangoContextClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) pango_context_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (PangoContext),
        0,              /* n_preallocs */
        (GInstanceInitFunc) pango_context_init,
      };
      
      object_type = g_type_register_static (G_TYPE_OBJECT,
                                            "PangoContext",
                                            &object_info, 0);
    }
  
  return object_type;
}

static void
pango_context_init (PangoContext *context)
{
  context->base_dir = PANGO_DIRECTION_LTR;
  context->language = NULL;
  context->font_maps = NULL;

  context->font_desc = pango_font_description_new ();
  pango_font_description_set_family (context->font_desc, "serif");
  pango_font_description_set_style (context->font_desc, PANGO_STYLE_NORMAL);
  pango_font_description_set_variant (context->font_desc, PANGO_VARIANT_NORMAL);
  pango_font_description_set_weight (context->font_desc, PANGO_WEIGHT_NORMAL);
  pango_font_description_set_stretch (context->font_desc, PANGO_STRETCH_NORMAL);
  pango_font_description_set_size (context->font_desc, 12 * PANGO_SCALE);
}

static void
pango_context_class_init (PangoContextClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  
  parent_class = g_type_class_peek_parent (klass);
  
  object_class->finalize = pango_context_finalize;
}

static void
pango_context_finalize (GObject *object)
{
  PangoContext *context;

  context = PANGO_CONTEXT (object);

  g_slist_foreach (context->font_maps, (GFunc)g_object_unref, NULL);
  g_slist_free (context->font_maps);

  pango_font_description_free (context->font_desc);
  
  G_OBJECT_CLASS (parent_class)->finalize (object);
}


/**
 * pango_context_new:
 * 
 * Creates a new #PangoContext initialized to default value.
 * 
 * Return value: the new #PangoContext
 **/
PangoContext *
pango_context_new (void)
{
  PangoContext *context;

  context = PANGO_CONTEXT (g_type_create_instance (pango_context_get_type ()));
  
  return context;
}

/**
 * pango_context_add_font_map:
 * @context: a #PangoContext
 * @font_map: the #PangoFontMap to add.
 * 
 * Add a font map to the list of font maps that are searched for fonts
 * when fonts are looked-up in this context.
 **/
void
pango_context_add_font_map (PangoContext *context,
			    PangoFontMap *font_map)
{
  g_return_if_fail (context != NULL);
  g_return_if_fail (font_map != NULL);
  
  g_object_ref (G_OBJECT (font_map));
  context->font_maps =  g_slist_append (context->font_maps, font_map);
}

typedef struct
{
  int n_found;
  PangoFontFamily **families;
} ListFamiliesInfo;

static void
list_families_foreach (gpointer key, gpointer value, gpointer user_data)
{
  ListFamiliesInfo *info = user_data;

  if (info->families)
    info->families[info->n_found++] = value;

  g_free (value);
}

/**
 * pango_context_list_families:
 * @context: a #PangoContext
 * @families: location to store a pointer to an array of strings.
 *            This array should be freed with pango_font_map_free_families().
 * @n_families: location to store the number of elements in @descs
 * 
 * List all families for a context.
 **/
void
pango_context_list_families (PangoContext          *context,
			     PangoFontFamily     ***families,
			     int                   *n_families)
{
  int n_maps;
  
  g_return_if_fail (context != NULL);
  g_return_if_fail (families == NULL || n_families != NULL);

  if (n_families == NULL)
    return;
  
  n_maps = g_slist_length (context->font_maps);
  
  if (n_maps == 0)
    {
      *n_families = 0;
      if (families)
	*families = NULL;
      
      return;
    }
  else if (n_maps == 1)

    pango_font_map_list_families (context->font_maps->data, families, n_families);
  else
    {
      GHashTable *family_hash;
      GSList *tmp_list;
      ListFamiliesInfo info;

      *n_families = 0;

      family_hash = g_hash_table_new (g_str_hash, g_str_equal);

      tmp_list = context->font_maps;
      while (tmp_list)
	{
	  PangoFontFamily **tmp_families;
	  int tmp_n_families;
	  int i;
	  
	  pango_font_map_list_families (tmp_list->data, &tmp_families, &tmp_n_families);

	  for (i=0; i<*n_families; i++)
	    {
	      const char *family = pango_font_family_get_name (tmp_families[i]);
		  
	      if (!g_hash_table_lookup (family_hash, tmp_families[i]))
		{
		  g_hash_table_insert (family_hash, (char *)family, tmp_families[i]);
		  (*n_families)++;
		}
	    }
	  
	  g_free (tmp_families);

	  tmp_list = tmp_list->next;
	}

      info.n_found = 0;

      if (families)
	{
	  *families = g_new (PangoFontFamily *, *n_families);
	  info.families = *families;
	}
      else
	info.families = NULL;
	  
      g_hash_table_foreach (family_hash, list_families_foreach, &info);
      g_hash_table_destroy (family_hash);
    }
}

/**
 * pango_context_load_font:
 * @context: a #PangoContext
 * @desc: a #PangoFontDescription describing the font to load
 * 
 * Loads the font in one of the fontmaps in the context
 * that is the closest match for @desc.
 *
 * Returns the font loaded, or %NULL if no font matched.
 **/
PangoFont *
pango_context_load_font (PangoContext               *context,
			 const PangoFontDescription *desc)
{
  GSList *tmp_list;

  g_return_val_if_fail (context != NULL, NULL);

  tmp_list = context->font_maps;
  while (tmp_list)
    {
      PangoFont *font;
      
      font = pango_font_map_load_font (tmp_list->data, desc);
      if (font)
	return font;
      
      tmp_list = tmp_list->next;
    }

  return NULL;
}

/**
 * pango_context_set_font_description:
 * @context: a #PangoContext
 * @desc: the new pango font description
 * 
 * Set the default font description for the context
 **/
void
pango_context_set_font_description (PangoContext               *context,
				    const PangoFontDescription *desc)
{
  g_return_if_fail (context != NULL);
  g_return_if_fail (desc != NULL);

  pango_font_description_free (context->font_desc);
  context->font_desc = pango_font_description_copy (desc);
}

/**
 * pango_context_get_font_description:
 * @context: a #PangoContext
 * 
 * Retrieve the default font description for the context.
 * 
 * Return value: a pointer to the context's default font description.
 *               This value must not be modified or freed.
 **/
PangoFontDescription *
pango_context_get_font_description (PangoContext *context)
{
  g_return_val_if_fail (context != NULL, NULL);
  
  return context->font_desc;
}

/**
 * pango_context_set_language:
 * @context: a #PangoContext
 * @language: the new language tag.
 * 
 * Sets the global language tag for the context.
 **/
void
pango_context_set_language (PangoContext *context,
			    PangoLanguage    *language)
{
  g_return_if_fail (context != NULL);

  context->language = language;
}

/**
 * pango_context_get_language:
 * @context: a #PangoContext
 * 
 * Retrieves the global language tag for the context.
 * 
 * Return value: the global language tag.
 **/
PangoLanguage *
pango_context_get_language (PangoContext *context)
{
  g_return_val_if_fail (context != NULL, NULL);

  return context->language;
}

/**
 * pango_context_set_base_dir:
 * @context: a #PangoContext
 * @direction: the new base direction
 * 
 * Sets the base direction for the context.
 **/
void
pango_context_set_base_dir (PangoContext  *context,
			    PangoDirection direction)
{
  g_return_if_fail (context != NULL);

  context->base_dir = direction;
}

/**
 * pango_context_get_base_dir:
 * @context: 
 * 
 * Retrieves the base direction for the context.
 * 
 * Return value: the base direction for the context.
 **/
PangoDirection
pango_context_get_base_dir (PangoContext *context)
{
  g_return_val_if_fail (context != NULL, PANGO_DIRECTION_LTR);

  return context->base_dir;
}

/**
 * pango_itemize:
 * @context:   a structure holding information that affects
               the itemization process.
 * @text:      the text to itemize.
 * @start_index: first byte in @text to process
 * @length:    the number of bytes (not characters) to process
 *             after @start_index.
 *             This must be >= 0.
 * @attrs:     the set of attributes that apply to @text.
 * @cached_iter:      Cached attribute iterator, or NULL
 *
 * Breaks a piece of text into segments with consistent
 * directional level and shaping engine. Each byte of @text will
 * be contained in exactly one of the items in the returned list;
 * the generated list of items will be in logical order (the start
 * offsets of the items are ascending).
 *
 * @cached_iter should be an iterator over @attrs currently positioned at a
 * range before or containing @start_index; @cached_iter will be advanced to
 * the range covering the position just after @start_index + @length.
 * (i.e. if itemizing in a loop, just keep passing in the same @cached_iter).
 *
 * Return value: a GList of PangoItem structures.
 */
GList *
pango_itemize (PangoContext      *context, 
	       const char        *text,
               int                start_index,
	       int                length,
	       PangoAttrList     *attrs,
               PangoAttrIterator *cached_iter)
{
  gunichar *text_ucs4;
  long n_chars, i;
  guint8 *embedding_levels;
  PangoDirection base_dir;
  PangoItem *item;
  const char *p;
  const char *next;
  GList *result = NULL;

  PangoAnalysis *analyses;

  g_return_val_if_fail (context != NULL, NULL);
  g_return_val_if_fail (start_index >= 0, NULL);
  g_return_val_if_fail (length >= 0, NULL);
  g_return_val_if_fail (length == 0 || text != NULL, NULL);

  if (length == 0)
    return NULL;

  base_dir = context->base_dir;

  if (length == 0)
    return NULL;

  /* First, apply the bidirectional algorithm to break
   * the text into directional runs.
   */
  text_ucs4 = g_utf8_to_ucs4_fast (text + start_index, length, &n_chars);

  embedding_levels = g_new (guint8, n_chars);

  pango_log2vis_get_embedding_levels (text_ucs4, n_chars, &base_dir,
                                      embedding_levels);

  /* Storing ranges would be more efficient, but also more
   * complicated... we take the simple approach for now.
   */

  analyses = g_new0 (PangoAnalysis, n_chars);

  /* Now, fill in the appropriate shapers, language engines and fonts for
   * each character.
   */

  add_engines (context, text, start_index, length, attrs,
               cached_iter,
               n_chars,
	       analyses);

  /* Make a GList of PangoItems out of the above results
   */

  item = NULL;
  p = text + start_index;
  for (i=0; i<n_chars; i++)
    {
      PangoAnalysis *analysis = &analyses[i];
      PangoAnalysis *last_analysis = i > 0 ? &analyses[i-1] : 0;
      
      next = g_utf8_next_char (p);
      
      if (i == 0 ||
	  text_ucs4[i] == '\t' || text_ucs4[i-1] == '\t' ||
	  embedding_levels[i] != embedding_levels[i-1] ||
	  analysis->shape_engine != last_analysis->shape_engine ||
	  analysis->lang_engine != last_analysis->lang_engine ||
	  analysis->font != last_analysis->font ||
	  analysis->language != last_analysis->language ||
	  analysis->extra_attrs != last_analysis->extra_attrs)
	{
          /* assert that previous item got at least one char */
          g_assert (item == NULL || item->length > 0);
          g_assert (item == NULL || item->num_chars > 0);
          
	  item = pango_item_new ();
	  item->offset = p - text;
	  item->num_chars = 0;
	  item->analysis.level = embedding_levels[i];
	  
	  item->analysis.shape_engine = analysis->shape_engine;
	  item->analysis.lang_engine = analysis->lang_engine;

	  item->analysis.font = analysis->font;
	  item->analysis.language = analysis->language;

	  /* Copy the extra attribute list if necessary */
	  if (analysis->extra_attrs && i != 0 && analysis->extra_attrs == last_analysis->extra_attrs)
	    {
	      GSList *tmp_list = analysis->extra_attrs;
	      GSList *new_list = NULL;
	      while (tmp_list)
		{
		  new_list = g_slist_prepend (new_list,
					      pango_attribute_copy (tmp_list->data));
		  tmp_list = tmp_list->next;
		}
	      item->analysis.extra_attrs = g_slist_reverse (new_list);
	    }
	  else
	    item->analysis.extra_attrs = analysis->extra_attrs;

	  result = g_list_prepend (result, item);
	}
      else
	g_object_unref (analysis->font);

      item->length = (next - text) - item->offset;
      item->num_chars++;
      p = next;
    }  

  g_free (analyses);
  g_free (embedding_levels);
  g_free (text_ucs4);
  
  return g_list_reverse (result);
}

static void 
fallback_engine_shape (PangoFont        *font,
                       const char       *text,
                       gint              length,
                       PangoAnalysis    *analysis,
                       PangoGlyphString *glyphs)
{
  int n_chars;
  int i;
  const char *p;
  
  g_return_if_fail (font != NULL);
  g_return_if_fail (text != NULL);
  g_return_if_fail (length >= 0);
  g_return_if_fail (analysis != NULL);

  n_chars = g_utf8_strlen (text, length);
  pango_glyph_string_set_size (glyphs, n_chars);
  
  p = text;
  i = 0;
  while (i < n_chars)
    {
      glyphs->glyphs[i].glyph = 0;
      
      glyphs->glyphs[i].geometry.x_offset = 0;
      glyphs->glyphs[i].geometry.y_offset = 0;
      glyphs->glyphs[i].geometry.width = 0;

      glyphs->log_clusters[i] = p - text;
      
      ++i;
      p = g_utf8_next_char (p);
    }
}

static PangoCoverage*
fallback_engine_get_coverage (PangoFont      *font,
                              PangoLanguage  *lang)
{
  PangoCoverage *result = pango_coverage_new ();

  /* We return an empty coverage (this function won't get
   * called, but if it is, empty coverage will keep
   * it from being used).
   */
  
  return result;
}

static PangoEngineShape fallback_shaper = {
  {
    "FallbackScriptEngine",
    PANGO_ENGINE_TYPE_SHAPE,
    sizeof (PangoEngineShape)
  },
  fallback_engine_shape,
  fallback_engine_get_coverage
};

/* FIXME: Remove this artificial limit */
#define MAX_FAMILIES 16

typedef struct _FontSet FontSet;

struct _FontSet
{
  int n_families;
  PangoFont *fonts[MAX_FAMILIES];
  PangoCoverage *coverages[MAX_FAMILIES];  
};

#define FONT_SET_INITIALIZER { 0, }

static gint
font_set_get_font (FontSet  *font_set,
		   gunichar  wc)
{
  PangoCoverageLevel best_level = PANGO_COVERAGE_NONE;

  int result = -1;
  int i;
  
  for (i=0; i < font_set->n_families; i++)
    {
      if (font_set->fonts[i])
	{
	  PangoCoverageLevel level = pango_coverage_get (font_set->coverages[i], wc);
	  
	  if (result == -1 || level > best_level)
	    {
	      result = i;
	      best_level = level;
	    }
	}
    }

  return result;
}

static void
font_set_free (FontSet *font_set)
{
  int j;
  
  for (j=0; j < font_set->n_families; j++)
    {
      if (font_set->fonts[j])
	{
	  g_object_unref (font_set->fonts[j]);
	  pango_coverage_unref (font_set->coverages[j]);
	}
    }

  font_set->n_families = 0;
}

static void
font_set_load (FontSet                    *font_set,
	       PangoContext               *context,
	       PangoLanguage              *language,
	       const PangoFontDescription *desc)
{
  PangoFontDescription *tmp_desc = pango_font_description_copy_static (desc);
  char **families;
  int j;

  font_set_free (font_set);

  families = g_strsplit (pango_font_description_get_family (desc), ",", -1);

  font_set->n_families = 0;
  for (j=0; families[j] && font_set->n_families < MAX_FAMILIES; j++)
    {
      pango_font_description_set_family_static (tmp_desc, families[j]);
      font_set->fonts[font_set->n_families] = pango_context_load_font (context, tmp_desc);
      
      if (font_set->fonts[font_set->n_families])
	{
	  font_set->coverages[font_set->n_families] = pango_font_get_coverage (font_set->fonts[font_set->n_families], language);
	  (font_set->n_families)++;
	}
    }
  
  g_strfreev (families);
  pango_font_description_set_family_static (tmp_desc, pango_font_description_get_family (desc));

  /* The font description was completely unloadable, try with
   * family == "Sans"
   */
  if (font_set->n_families == 0)
    {
      char *ctmp1, *ctmp2;
      
      ctmp1 = pango_font_description_to_string (desc);
      pango_font_description_set_family_static (tmp_desc, "Sans");
      ctmp2 = pango_font_description_to_string (tmp_desc);
      
      g_warning ("Couldn't load font \"%s\" falling back to \"%s\"", ctmp1, ctmp2);
      g_free (ctmp1);
      g_free (ctmp2);
      
      font_set->fonts[0] = pango_context_load_font (context, tmp_desc);
      if (font_set->fonts[0])
	{
	  font_set->coverages[0] = pango_font_get_coverage (font_set->fonts[0], language);
	  font_set->n_families = 1;
	}
    }
  
  /* We couldn't try with Sans and the specified style. Try Sans Normal
   */
  if (font_set->n_families == 0)
    {
      char *ctmp1, *ctmp2;
      
      ctmp1 = pango_font_description_to_string (tmp_desc);
      pango_font_description_set_style (tmp_desc, PANGO_STYLE_NORMAL);
      pango_font_description_set_weight (tmp_desc, PANGO_WEIGHT_NORMAL);
      pango_font_description_set_variant (tmp_desc, PANGO_VARIANT_NORMAL);
      pango_font_description_set_stretch (tmp_desc, PANGO_STRETCH_NORMAL);
      ctmp2 = pango_font_description_to_string (tmp_desc);
      
      g_warning ("Couldn't load font \"%s\" falling back to \"%s\"", ctmp1, ctmp2);
      g_free (ctmp1);
      g_free (ctmp2);
      
      font_set->fonts[0] = pango_context_load_font (context, tmp_desc);
      if (font_set->fonts[0])
	{
	  font_set->coverages[0] = pango_font_get_coverage (font_set->fonts[0], language);
	  font_set->n_families = 1;
	}
    }

  /* Everything failed, we are screwed, there is no way to continue
   */
  if (font_set->n_families == 0)
    {
      g_warning ("All font failbacks failed!!!!");
      exit (1);
    }

  pango_font_description_free (tmp_desc);
}

static gboolean
advance_iterator_to (PangoAttrIterator *iterator,
                     int                start_index)
{
  int start_range, end_range;
  
  pango_attr_iterator_range (iterator, &start_range, &end_range);

  while (start_index >= end_range)
    {
      if (!pango_attr_iterator_next (iterator))
        return FALSE;
      pango_attr_iterator_range (iterator, &start_range, &end_range);
    }

  if (start_range > start_index)
    g_warning ("In pango_itemize(), the cached iterator passed in "
               "had already moved beyond the start_index");

  return TRUE;
}

static void
add_engines (PangoContext      *context,
	     const gchar       *text,
             gint               start_index,
	     gint               length,
	     PangoAttrList     *attrs,
             PangoAttrIterator *cached_iter,
             gint               n_chars,
	     PangoAnalysis     *analyses)
{
  const char *pos;
  PangoLanguage *language = NULL;
  int next_index;
  GSList *extra_attrs = NULL;
  PangoMap *lang_map = NULL;
  PangoFontDescription *current_desc = NULL;
  FontSet current_fonts = FONT_SET_INITIALIZER;
  PangoAttrIterator *iterator;
  gboolean first_iteration = TRUE;
  gunichar wc;
  int i = 0;
  int font_index;

  if (cached_iter)
    iterator = cached_iter;
  else
    iterator = pango_attr_list_get_iterator (attrs);

  advance_iterator_to (iterator, start_index);
  
  pango_attr_iterator_range (iterator, NULL, &next_index);
  
  pos = text + start_index;
  for (i=0; i<n_chars; i++)
    {
      PangoAnalysis *analysis = &analyses[i];
      
      if (first_iteration || pos - text == next_index)
	{
	  PangoLanguage *next_language;
	  PangoFontDescription *next_desc = pango_font_description_copy_static (context->font_desc);

          first_iteration = FALSE;
          
          /* Only advance the iterator if we've exhausted a range,
           * not on the first iteration.
           */
          if (pos - text == next_index)
            {
              pango_attr_iterator_next (iterator);
              pango_attr_iterator_range (iterator, NULL, &next_index);
            }
          
	  pango_attr_iterator_get_font (iterator, next_desc, &next_language, &extra_attrs);

          if (!next_language)
	    next_language = context->language;

	  if (i == 0 || language != next_language)
	    {
	      static guint engine_type_id = 0;
	      static guint render_type_id = 0;
	      
	      language = next_language;

	      if (engine_type_id == 0)
		{
		  engine_type_id = g_quark_from_static_string (PANGO_ENGINE_TYPE_LANG);
		  render_type_id = g_quark_from_static_string (PANGO_RENDER_TYPE_NONE);
		}
	       
	      lang_map = pango_find_map (next_language,
					 engine_type_id, render_type_id);
	    }

	  if (i == 0 || !pango_font_description_equal (current_desc, next_desc))
	    {
	      pango_font_description_free (current_desc);
	      current_desc = next_desc;

	      font_set_load (&current_fonts, context, language, current_desc);
	    }
	  else
	    pango_font_description_free (next_desc);
        }

      wc = g_utf8_get_char (pos);
      pos = g_utf8_next_char (pos);
      
      analysis->lang_engine = (PangoEngineLang *)pango_map_get_engine (lang_map, wc);
      font_index = font_set_get_font (&current_fonts, wc);
      if (font_index != -1)
	{
	  analysis->font = current_fonts.fonts[font_index];
	  g_object_ref (analysis->font);
	}
      else
	analysis->font = NULL;
      analysis->language = language;
      
      /* FIXME: handle reference counting properly on the shapers */
      if (analysis->font)
	analysis->shape_engine = pango_font_find_shaper (analysis->font, language, wc);
      else
	analysis->shape_engine = NULL;
      
      if (analysis->shape_engine == NULL)
        analysis->shape_engine = &fallback_shaper;
      
      analysis->extra_attrs = extra_attrs;
    }

  g_assert (pos - text == start_index + length);

  if (current_desc)
    pango_font_description_free (current_desc);
  font_set_free (&current_fonts);

  if (iterator != cached_iter)
    pango_attr_iterator_destroy (iterator);
}

/**
 * pango_context_get_metrics:
 * @context: a #PangoContext
 * @desc: a #PangoFontDescription structure
 * @language: language tag used to determine which script to get the metrics
 *            for, or %NULL to indicate to get the metrics for the entire
 *            font.
 * 
 * Get overall metric information for a font particular font
 * description.  Since the metrics may be substantially different for
 * different scripts, a language tag can be provided to indicate that
 * the metrics should be retrieved that correspond to the script(s)
 * used by that language.
 *
 * The #PangoFontDescription is interpreted in the same way as
 * by pango_itemize(), and the family name may be a comma separated
 * list of figures. If characters from multiple of these families
 * would be used to render the string, then the returned fonts would
 * be a composite of the metrics for the fonts loaded for the
 * individual families.
 *
 * Returns: a #PangoMetrics object. The caller must call pango_font_metrics_unref()
 *   when finished using the object.
 **/
PangoFontMetrics *
pango_context_get_metrics (PangoContext                 *context,
			   const PangoFontDescription   *desc,
			   PangoLanguage                *language)
{
  FontSet current_fonts = FONT_SET_INITIALIZER;
  PangoFontMetrics *raw_metrics[MAX_FAMILIES];
  PangoFontMetrics *metrics;
  const char *sample_str;
  const char *p;
  int i;

  g_return_val_if_fail (PANGO_IS_CONTEXT (context), NULL);
  g_return_val_if_fail (desc != NULL, NULL);

  sample_str = pango_language_get_sample_string (language);

  font_set_load (&current_fonts, context, language, desc);

  if (current_fonts.n_families == 1)
    metrics = pango_font_get_metrics (current_fonts.fonts[0], language);
  else
    {
      int count = 0;

      metrics = pango_font_metrics_new ();

      for (i=0; i < MAX_FAMILIES; i++)
	raw_metrics[i] = NULL;
  
      p = sample_str;
      while (*p)
	{
	  gunichar wc = g_utf8_get_char (p);
	  int index = font_set_get_font (&current_fonts, wc);
	  if (!raw_metrics[index])
	    raw_metrics[index] = pango_font_get_metrics (current_fonts.fonts[index], language);

	  if (count == 0)
	    *metrics = *raw_metrics[index];
	  else
	    {
	      metrics->ascent = MAX (metrics->ascent, raw_metrics[index]->ascent);
	      metrics->descent = MAX (metrics->descent, raw_metrics[index]->descent);
	      metrics->approximate_char_width += raw_metrics[index]->approximate_char_width;
	      metrics->approximate_digit_width += raw_metrics[index]->approximate_digit_width;
	    }
	  
	  p = g_utf8_next_char (p);
	  count++;
	}

      for (i=0; i < MAX_FAMILIES; i++)
	if (raw_metrics[i])
	  pango_font_metrics_unref (raw_metrics[i]);

      metrics->approximate_char_width /= count;
      metrics->approximate_digit_width /= count;
    }
      
  font_set_free (&current_fonts);

  return metrics;
}