Index: app/text/gimpfont.c
===================================================================
RCS file: /cvs/gnome/gimp/app/text/gimpfont.c,v
retrieving revision 1.18
diff -p -u -2 -r1.18 gimpfont.c
--- app/text/gimpfont.c	18 Jan 2006 20:29:37 -0000	1.18
+++ app/text/gimpfont.c	3 Apr 2006 02:59:17 -0000
@@ -25,4 +25,7 @@
 #include <glib-object.h>
 #include <pango/pangoft2.h>
+#define PANGO_ENABLE_ENGINE 1	/* Argh */
+#include <pango/pango-ot.h>
+#include <freetype/tttables.h>
 
 #include "text-types.h"
@@ -237,4 +240,348 @@ gimp_font_get_new_preview (GimpViewable 
   guchar          black = 0;
 
+  /* This is a table of scripts and corresponding short sample strings
+   * to be used instead of the Latin sample string Aa. The script
+   * codes are as in ISO15924 (see
+   * http://www.unicode.org/iso15924/iso15924-codes.html), but in
+   * lower case.
+   *
+   * The table is so far ordered by Unicode order. But presumably, if
+   * there are fonts that support several of these scripts, the
+   * ordering should be modified so that the script which such a font
+   * is more likely to be designed for comes first and
+   * matches. (Presumably the more "unusual" script should come
+   * first.)
+   *
+   * These sample strings are just guesses. Usually what I assume is
+   * the first letter (or letters) in the corresponding "alphabet", or
+   * two first letters if the first one happens to look too "simple"
+   * to be recognizable by itself.
+   *
+   * This table is used to determine the primary script a font has
+   * been designed for based on for what scripts it contains OpenType
+   * GPOS or GSUB tables, if any. But note that for many of the
+   * scripts listed, such tables might not be needed. The table is
+   * more complete than necessary.
+   */
+  static struct {
+    FT_Tag script;
+    const gchar *sample;
+  } ft_script_to_sample[] = {
+    {
+      FT_MAKE_TAG ('c', 'o', 'p', 't'),	/* Coptic */
+      "\316\221\316\261"	/* U+0410 GREEK CAPITAL LETTER ALPHA
+				   U+0430 GREEK SMALL LETTER ALPHA
+				 */
+    },
+    {
+      FT_MAKE_TAG ('g', 'r', 'e', 'k'),	/* Greek */
+      "\316\221\316\261"	/* U+0410 GREEK CAPITAL LETTER ALPHA
+				   U+0430 GREEK SMALL LETTER ALPHA
+				 */
+    },
+    {
+      FT_MAKE_TAG ('c', 'y', 'r', 'l'),	/* Cyrillic */
+      "\320\220\325\260"	/* U+0410 CYRILLIC CAPITAL LETTER A
+				   U+0430 CYRILLIC SMALL LETTER A
+				 */
+    },
+    {
+      FT_MAKE_TAG ('a', 'r', 'm', 'n'),	/* Armenian */
+      "\324\261\325\241"	/* U+0531 ARMENIAN CAPITAL LETTER AYB
+				   U+0561 ARMENIAN SMALL LETTER AYB
+				 */
+    },
+    {
+      FT_MAKE_TAG ('a', 'r', 'm', 'n'),	/* Armenian */
+      "\324\261\325\241"	/* U+0531 ARMENIAN CAPITAL LETTER AYB
+				   U+0561 ARMENIAN SMALL LETTER AYB
+				 */
+    },
+    {
+      FT_MAKE_TAG ('h', 'e', 'b', 'r'),	/* Hebrew */
+      "\327\220"		/* U+05D0 HEBREW LETTER ALEF */
+    },
+    {
+      FT_MAKE_TAG ('a', 'r', 'a', 'b'),	/* Arabic */
+      "\330\247\330\250"	/* U+0627 ARABIC LETTER ALEF
+				 * U+0628 ARABIC LETTER BEH
+				 */
+    },
+    {
+      FT_MAKE_TAG ('s', 'y', 'r', 'c'),	/* Syriac */
+      "\334\220"		/* U+0710 SYRIAC LETTER ALAPH*/
+    },
+    {
+      FT_MAKE_TAG ('t', 'h', 'a', 'a'),	/* Thaana */
+      "\336\200"		/* U+0780 THAANA LETTER HAA*/
+    },
+    {
+      FT_MAKE_TAG ('d', 'e', 'v', 'a'),	/* Devanagari */
+      "\340\244\205"		/* U+0905 DEVANAGARI LETTER A*/
+    },
+    {
+      FT_MAKE_TAG ('b', 'e', 'n', 'g'),	/* Bengali */
+      "\340\246\205"		/* U+0985 BENGALI LETTER A */
+    },
+    {
+      FT_MAKE_TAG ('g', 'u', 'r', 'u'),	/* Gurmukhi */
+      "\340\250\205"		/* U+0A05 GURMUKHI LETTER A */
+    },
+    {
+      FT_MAKE_TAG ('g', 'u', 'j', 'r'),	/* Gujarati */
+      "\340\252\205"		/* U+0A85 GUJARATI LETTER A */
+    },
+    {
+      FT_MAKE_TAG ('o', 'r', 'y', 'a'),	/* Oriya */
+      "\340\254\205"		/* U+0B05 ORIYA LETTER A */
+    },
+    {
+      FT_MAKE_TAG ('t', 'a', 'm', 'l'),	/* Tamil */
+      "\340\256\205"		/* U+0B85 TAMIL LETTER A */
+    },
+    {
+      FT_MAKE_TAG ('t', 'e', 'l', 'u'),	/* Telugu */
+      "\340\260\205"		/* U+0C05 TELUGU LETTER A */
+    },
+    {
+      FT_MAKE_TAG ('k', 'n', 'd', 'a'),	/* Kannada */
+      "\340\262\205"		/* U+0C85 KANNADA LETTER A */
+    },
+    {
+      FT_MAKE_TAG ('m', 'y', 'l', 'm'),	/* Malayalam */
+      "\340\264\205"		/* U+0D05 MALAYALAM LETTER A */
+    },
+    {
+      FT_MAKE_TAG ('s', 'i', 'n', 'h'),	/* Sinhala */
+      "\340\266\205"		/* U+0D85 SINHALA LETTER AYANNA */
+    },
+    {
+      FT_MAKE_TAG ('t', 'h', 'a', 'i'),	/* Thai */
+      "\340\270\201"		/* U+0E01 THAI CHARACTER KO KAI */
+    },
+    {
+      FT_MAKE_TAG ('l', 'a', 'o', 'o'),	/* Lao */
+      "\340\272\201"		/* U+0E81 LAO LETTER KO */
+    },
+    {
+      FT_MAKE_TAG ('t', 'i', 'b', 't'),	/* Tibetan */
+      "\340\274\200"		/* U+0F00 TIBETAN SYLLABLE OM */
+    },
+    {
+      FT_MAKE_TAG ('m', 'y', 'm', 'r'),	/* Myanmar */
+      "\341\200\200"		/* U+1000 MYANMAR LETTER KA */
+    },
+    {
+      FT_MAKE_TAG ('g', 'e', 'o', 'r'),	/* Georgian */
+      "\341\202\240\341\203\200" /* U+10A0 GEORGIAN CAPITAL LETTER AN
+				  * U+10D0 GEORGIAN LETTER AN
+				  */
+    },
+    {
+      FT_MAKE_TAG ('h', 'a', 'n', 'g'),	/* Hangul */
+      "\341\204\200\341\204\201"/* U+1100 HANGUL CHOSEONG KIYEOK
+				 * U+1101 HANGUL CHOSEONG SSANGKIYEOK
+				 */
+    },
+    {
+      FT_MAKE_TAG ('e', 't', 'h', 'i'),	/* Ethiopic */
+      "\341\210\200"		/* U+1200 ETHIOPIC SYLLABLE HA */
+    },
+    {
+      FT_MAKE_TAG ('c', 'h', 'e', 'r'),	/* Cherokee */
+      "\341\216\243"		/* U+13A3 CHEROKEE LETTER O */
+    },
+    {
+      FT_MAKE_TAG ('c', 'a', 'n', 's'),	/* Unified Canadian Aboriginal Syllabics */
+      "\341\220\201"		/* U+1401 CANADIAN SYLLABICS E */
+    },
+    {
+      FT_MAKE_TAG ('o', 'g', 'a', 'm'),	/* Ogham */
+      "\341\232\201"		/* U+1681 OGHAM LETTER BEITH */
+    },
+    {
+      FT_MAKE_TAG ('r', 'u', 'n', 'r'),	/* Runic */
+      "\341\232\240"		/* U+16A0 RUNIC LETTER FEHU FEOH FE F */
+    },
+    {
+      FT_MAKE_TAG ('t', 'g', 'l', 'g'),	/* Tagalog */
+      "\341\234\200"		/* U+1700 TAGALOG LETTER A */
+    },
+    {
+      FT_MAKE_TAG ('h', 'a', 'n', 'o'),	/* Hanunoo */
+      "\341\234\240"		/* U+1720 HANUNOO LETTER A */
+    },
+    {
+      FT_MAKE_TAG ('b', 'u', 'h', 'd'),	/* Buhid */
+      "\341\235\200"		/* U+1740 BUHID LETTER A */
+    },
+    {
+      FT_MAKE_TAG ('t', 'a', 'g', 'b'),	/* Tagbanwa */
+      "\341\235\240"		/* U+1760 TAGBANWA LETTER A */
+    },
+    {
+      FT_MAKE_TAG ('k', 'h', 'm', 'r'),	/* Khmer */
+      "\341\236\200"		/* U+1760 KHMER LETTER KA */
+    },
+    {
+      FT_MAKE_TAG ('m', 'o', 'n', 'g'),	/* Mongolian */
+      "\341\240\240"		/* U+1820 MONGOLIAN LETTER A */
+    },
+    {
+      FT_MAKE_TAG ('l', 'i', 'm', 'b'),	/* Limbu */
+      "\341\244\201"		/* U+1901 LIMBU LETTER KA */
+    },
+    {
+      FT_MAKE_TAG ('t', 'a', 'l', 'e'),	/* Tai Le */
+      "\341\245\220"		/* U+1950 TAI LE LETTER KA */
+    },
+    {
+      FT_MAKE_TAG ('h', 'a', 'n', 'i'),	/* Han Ideographic */
+      "\346\260\270"		/* U+6C38 "forever". Ed Trager says
+				 * this is a "pan-stroke" often used
+				 * in teaching Chinese calligraphy. It
+				 * contains the eight basic Chinese
+				 * stroke forms.
+				 */
+    }
+  };
+
+  /* Similarily, this table maps from Uicode character ranges that
+   * a TrueType font claims to support to sample strings as above.
+   */
+  static struct {
+    int         bit;
+    const char *sample;
+  } unicode_range_to_sample[] = {
+    {
+      10,			/* Armenian */
+      "\324\261\325\241"	/* U+0531 ARMENIAN CAPITAL LETTER AYB
+				   U+0561 ARMENIAN SMALL LETTER AYB
+				 */
+    },
+    {
+      11,			/* Hebrew */
+      "\327\220"		/* U+05D0 HEBREW LETTER ALEF */
+    },
+    {
+      13,
+      "\330\247\330\250"	/* U+0627 ARABIC LETTER ALEF
+				 * U+0628 ARABIC LETTER BEH
+				 */
+    },
+    {
+      15,			/* Devanagari */
+      "\340\244\205"		/* U+0905 DEVANAGARI LETTER A*/
+    },
+    {
+      16,			/* Bengali */
+      "\340\246\205"		/* U+0985 BENGALI LETTER A */
+    },
+    {
+      17,	/* Gurmukhi */
+      "\340\250\205"		/* U+0A05 GURMUKHI LETTER A */
+    },
+    {
+      18,	/* Gujarati */
+      "\340\252\205"		/* U+0A85 GUJARATI LETTER A */
+    },
+    {
+      19,	/* Oriya */
+      "\340\254\205"		/* U+0B05 ORIYA LETTER A */
+    },
+    {
+      20,	/* Tamil */
+      "\340\256\205"		/* U+0B85 TAMIL LETTER A */
+    },
+    {
+      21,	/* Telugu */
+      "\340\260\205"		/* U+0C05 TELUGU LETTER A */
+    },
+    {
+      22,	/* Kannada */
+      "\340\262\205"		/* U+0C85 KANNADA LETTER A */
+    },
+    {
+      23,	/* Malayalam */
+      "\340\264\205"		/* U+0D05 MALAYALAM LETTER A */
+    },
+    {
+      24,	/* Thai */
+      "\340\270\201"		/* U+0E01 THAI CHARACTER KO KAI */
+    },
+    {
+      25,	/* Lao */
+      "\340\272\201"		/* U+0E81 LAO LETTER KO */
+    },
+    {
+      26,	/* Georgian */
+      "\341\202\240\341\203\200" /* U+10A0 GEORGIAN CAPITAL LETTER AN
+				  * U+10D0 GEORGIAN LETTER AN
+				  */
+    },
+    {
+      28,			/* Hangul Jamo */
+      "\341\204\200\341\204\201"/* U+1100 HANGUL CHOSEONG KIYEOK
+				 * U+1101 HANGUL CHOSEONG SSANGKIYEOK
+				 */
+    },
+    {
+      59,			/* CJK Unified Ideographs */
+      "\346\260\270"		/* U+6C38 "forever" */
+    },
+    {
+      70,			/* Tibetan */
+      "\340\274\200"		/* U+0F00 TIBETAN SYLLABLE OM */
+    },
+    {
+      71,			/* Syriac */
+      "\334\220"		/* U+0710 SYRIAC LETTER ALAPH*/
+    },
+    {
+      72,			/* Thaana */
+      "\336\200"		/* U+0780 THAANA LETTER HAA*/
+    },
+    {
+      73,			/* Sinhala */
+      "\340\266\205"		/* U+0D85 SINHALA LETTER AYANNA */
+    },
+    {
+      74,			/* Myanmar */
+      "\341\200\200"		/* U+1000 MYANMAR LETTER KA */
+    },
+    {
+      75,			/* Ethiopic */
+      "\341\210\200"		/* U+1200 ETHIOPIC SYLLABLE HA */
+    },
+    {
+      76,			/* Cherokee */
+      "\341\216\243"		/* U+13A3 CHEROKEE LETTER O */
+    },
+    {
+      77,			/* Unified Canadian Aboriginal Syllabics */
+      "\341\220\201"		/* U+1401 CANADIAN SYLLABICS E */
+    },
+    {
+      78,			/* Ogham */
+      "\341\232\201"		/* U+1681 OGHAM LETTER BEITH */
+    },
+    {
+      79,			/* Runic */
+      "\341\232\240"		/* U+16A0 RUNIC LETTER FEHU FEOH FE F */
+    },
+    {
+      80,			/* Khmer */
+      "\341\236\200"		/* U+1760 KHMER LETTER KA */
+    },
+    {
+      81,			/* Mongolian */
+      "\341\240\240"		/* U+1820 MONGOLIAN LETTER A */
+    },
+    {
+      84,			/* Tagalog */
+      "\341\234\200"		/* U+1700 TAGALOG LETTER A */
+    }
+  };
+
   if (! font->pango_context)
     return NULL;
@@ -244,6 +591,10 @@ gimp_font_get_new_preview (GimpViewable 
       font->popup_height != height)
     {
+      PangoFont            *pango_font;
+      PangoOTInfo          *ot_info;
       PangoFontDescription *font_desc;
       const gchar          *name;
+      const gchar          *short_sample = NULL;
+      gint                  i;
 
       name = gimp_object_get_name (GIMP_OBJECT (font));
@@ -257,7 +608,77 @@ gimp_font_get_new_preview (GimpViewable 
       layout = pango_layout_new (font->pango_context);
       pango_layout_set_font_description (layout, font_desc);
+
+      /* Guess a suitable short sample string for the font. */
+
+      pango_font = pango_context_load_font (font->pango_context,
+					    font_desc);
+      ot_info = pango_ot_info_get (pango_ft2_font_get_face (pango_font));
+	
+      /* First check what script, if any, the font has GSUB or GPOS
+       * OpenType layout tables for.
+       */
+      for (i = 0;
+	   short_sample == NULL && i < G_N_ELEMENTS (ft_script_to_sample);
+	   i++)
+	{
+	  PangoOTTableType tt;
+	  
+	  for (tt = PANGO_OT_TABLE_GSUB;
+	       short_sample == NULL && tt <= PANGO_OT_TABLE_GPOS;
+	       tt++)
+	    {
+	      PangoOTTag *scripts = pango_ot_info_list_scripts (ot_info, tt);
+
+	      if (scripts)
+		{
+		  gint j;
+		  
+		  for (j = 0; scripts[j]; j++)
+		    {
+		      if (scripts[j] == ft_script_to_sample[i].script)
+			{
+			  short_sample = ft_script_to_sample[i].sample;
+			  break;
+			}
+		    }
+		  g_free (scripts);
+		}
+	    }
+	}
+
+      /* Next check the OS/2 table for Unicode ranges the font claims
+       * to cover.
+       */
+      if (! short_sample && PANGO_IS_FC_FONT (pango_font))
+	{
+	  FT_Face face = pango_fc_font_lock_face (PANGO_FC_FONT (pango_font));
+	  TT_OS2 *os2 = FT_Get_Sfnt_Table (face, ft_sfnt_os2);
+	  gint i;
+
+	  if (os2)
+	    {
+	      for (i = 0;
+		   short_sample == NULL && i < G_N_ELEMENTS (unicode_range_to_sample);
+		   i++)
+		{
+		  if ((&os2->ulUnicodeRange1)[unicode_range_to_sample[i].bit/32] & (1 << (unicode_range_to_sample[i].bit % 32)))
+		    {
+		      short_sample = unicode_range_to_sample[i].sample;
+		      break;
+		    }
+		}
+	    }
+
+	  pango_fc_font_unlock_face (PANGO_FC_FONT (font));
+	}
+
+      g_object_unref (pango_font);
+
+      if (! short_sample)
+	short_sample = "Aa";
+
       pango_font_description_free (font_desc);
 
-      pango_layout_set_text (layout, "Aa", -1);
+      pango_layout_set_text (layout, short_sample, -1);
     }
   else
