Index: pango/pango-context.c
===================================================================
RCS file: /cvs/gnome/pango/pango/pango-context.c,v
retrieving revision 1.49
diff -u -r1.49 pango-context.c
--- pango/pango-context.c	14 Oct 2002 22:50:47 -0000	1.49
+++ pango/pango-context.c	9 Feb 2003 23:26:56 -0000
@@ -36,6 +36,11 @@
   PangoFontDescription *font_desc;
 
   PangoFontMap *font_map;
+
+  struct {
+    PangoFontDescription *current_desc;
+    PangoFontset *current_fonts;
+  } add_engines;
 };
 
 struct _PangoContextClass
@@ -101,6 +106,9 @@
   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);
+
+  context->add_engines.current_desc = NULL;
+  context->add_engines.current_fonts = NULL;
 }
 
 static void
@@ -124,7 +132,12 @@
     g_object_unref (context->font_map);
 
   pango_font_description_free (context->font_desc);
-  
+
+  if (context->add_engines.current_desc)
+    pango_font_description_free (context->add_engines.current_desc);
+  if (context->add_engines.current_fonts)
+    g_object_unref (context->add_engines.current_fonts);
+
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
@@ -160,13 +173,29 @@
   g_return_if_fail (PANGO_IS_CONTEXT (context));
   g_return_if_fail (!font_map || PANGO_IS_FONT_MAP (font_map));
 
-  if (context->font_map)
-    g_object_unref (context->font_map);
+  if (font_map == context->font_map)
+    return;
 
+  /* Ref before unref.  */
   if (font_map)
     g_object_ref (font_map);
 
+  if (context->font_map)
+    g_object_unref (context->font_map);
+
   context->font_map = font_map;
+
+  /* Invalidate the add_engines cache.  */
+  if (context->add_engines.current_desc)
+    {
+      pango_font_description_free (context->add_engines.current_desc);
+      context->add_engines.current_desc = NULL;
+    }
+  if (context->add_engines.current_fonts)
+    {
+      g_object_unref (context->add_engines.current_fonts);
+      context->add_engines.current_fonts = NULL;
+    }
 }
 
 /**
@@ -592,8 +621,6 @@
   int next_index;
   GSList *extra_attrs = NULL;
   PangoMap *lang_map = NULL;
-  PangoFontDescription *current_desc = NULL;
-  PangoFontset *current_fonts = NULL;
   PangoAttrIterator *iterator;
   gboolean first_iteration = TRUE;
   gunichar wc;
@@ -651,28 +678,30 @@
 					 engine_type_id, render_type_id);
 	    }
 
-	  if (i == 0 || !pango_font_description_equal (current_desc, next_desc))
+	  if (context->add_engines.current_desc == NULL ||
+	      !pango_font_description_equal (context->add_engines.current_desc, next_desc))
 	    {
-	      pango_font_description_free (current_desc);
-	      current_desc = next_desc;
+	      pango_font_description_free (context->add_engines.current_desc);
+	      context->add_engines.current_desc = pango_font_description_copy (next_desc);
 
-	      if (current_fonts)
-		g_object_unref (current_fonts);
+	      if (context->add_engines.current_fonts)
+		g_object_unref (context->add_engines.current_fonts);
 	      
-	      current_fonts = pango_font_map_load_fontset (context->font_map,
-							   context,
-							   current_desc,
-							   language);
+	      context->add_engines.current_fonts =
+		pango_font_map_load_fontset (context->font_map,
+					     context,
+					     context->add_engines.current_desc,
+					     language);
 	    }
-	  else
-	    pango_font_description_free (next_desc);
+
+	  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);
-      analysis->font = pango_fontset_get_font (current_fonts, wc);
+      analysis->font = pango_fontset_get_font (context->add_engines.current_fonts, wc);
       analysis->language = language;
       
       /* FIXME: handle reference counting properly on the shapers */
@@ -688,11 +717,6 @@
     }
 
   g_assert (pos - text == start_index + length);
-
-  if (current_desc)
-    pango_font_description_free (current_desc);
-  if (current_fonts)
-    g_object_unref (current_fonts);
 
   if (iterator != cached_iter)
     pango_attr_iterator_destroy (iterator);
