]> asedeno.scripts.mit.edu Git - PuTTY.git/blobdiff - unix/gtkfont.c
Key rollover: rewrite the PGP keys manual appendix.
[PuTTY.git] / unix / gtkfont.c
index 0f2dc58151380856af7219e69b65f309bdc66255..b152f2bbf8e9436b593841d1326074221a3a67b8 100644 (file)
  *    I haven't the energy.
  */
 
+#if !GLIB_CHECK_VERSION(1,3,7)
+#define g_ascii_strcasecmp g_strcasecmp
+#define g_ascii_strncasecmp g_strncasecmp
+#endif
+
 /*
  * Ad-hoc vtable mechanism to allow font structures to be
  * polymorphic.
@@ -186,8 +191,10 @@ static char *x11_guess_derived_font_name(XFontStruct *xfs, int bold, int wide)
                p++;
            }
 
-           if (nstr < lenof(strings))
+           if (nstr < lenof(strings)) {
+                sfree(dupname);
                return NULL;           /* XLFD was malformed */
+            }
 
            if (bold)
                strings[2] = "bold";
@@ -425,7 +432,7 @@ static int x11font_has_glyph(unifont *font, wchar_t glyph)
         char sbstring[2];
         int sblen = wc_to_mb(xfont->real_charset, 0, &glyph, 1,
                              sbstring, 2, "", NULL, NULL);
-        if (!sbstring[0])
+        if (sblen == 0 || !sbstring[0])
             return FALSE;              /* not even in the charset */
 
         return x11_font_has_glyph(xfont->fonts[0], 0,
@@ -661,21 +668,21 @@ static void x11font_enum_fonts(GtkWidget *widget,
            style = p;
            p += sprintf(p, "%s", components[2][0] ? components[2] :
                         "regular");
-           if (!g_strcasecmp(components[3], "i"))
+           if (!g_ascii_strcasecmp(components[3], "i"))
                p += sprintf(p, " italic");
-           else if (!g_strcasecmp(components[3], "o"))
+           else if (!g_ascii_strcasecmp(components[3], "o"))
                p += sprintf(p, " oblique");
-           else if (!g_strcasecmp(components[3], "ri"))
+           else if (!g_ascii_strcasecmp(components[3], "ri"))
                p += sprintf(p, " reverse italic");
-           else if (!g_strcasecmp(components[3], "ro"))
+           else if (!g_ascii_strcasecmp(components[3], "ro"))
                p += sprintf(p, " reverse oblique");
-           else if (!g_strcasecmp(components[3], "ot"))
+           else if (!g_ascii_strcasecmp(components[3], "ot"))
                p += sprintf(p, " other-slant");
-           if (components[4][0] && g_strcasecmp(components[4], "normal"))
+           if (components[4][0] && g_ascii_strcasecmp(components[4], "normal"))
                p += sprintf(p, " %s", components[4]);
-           if (!g_strcasecmp(components[10], "m"))
+           if (!g_ascii_strcasecmp(components[10], "m"))
                p += sprintf(p, " [M]");
-           if (!g_strcasecmp(components[10], "c"))
+           if (!g_ascii_strcasecmp(components[10], "c"))
                p += sprintf(p, " [C]");
            if (components[5][0])
                p += sprintf(p, " %s", components[5]);
@@ -687,23 +694,23 @@ static void x11font_enum_fonts(GtkWidget *widget,
             */
            p++;
            stylekey = p;
-           if (!g_strcasecmp(components[2], "medium") ||
-               !g_strcasecmp(components[2], "regular") ||
-               !g_strcasecmp(components[2], "normal") ||
-               !g_strcasecmp(components[2], "book"))
+           if (!g_ascii_strcasecmp(components[2], "medium") ||
+               !g_ascii_strcasecmp(components[2], "regular") ||
+               !g_ascii_strcasecmp(components[2], "normal") ||
+               !g_ascii_strcasecmp(components[2], "book"))
                weightkey = 0;
-           else if (!g_strncasecmp(components[2], "demi", 4) ||
-                    !g_strncasecmp(components[2], "semi", 4))
+           else if (!g_ascii_strncasecmp(components[2], "demi", 4) ||
+                    !g_ascii_strncasecmp(components[2], "semi", 4))
                weightkey = 1;
            else
                weightkey = 2;
-           if (!g_strcasecmp(components[3], "r"))
+           if (!g_ascii_strcasecmp(components[3], "r"))
                slantkey = 0;
-           else if (!g_strncasecmp(components[3], "r", 1))
+           else if (!g_ascii_strncasecmp(components[3], "r", 1))
                slantkey = 2;
            else
                slantkey = 1;
-           if (!g_strcasecmp(components[4], "normal"))
+           if (!g_ascii_strcasecmp(components[4], "normal"))
                setwidthkey = 0;
            else
                setwidthkey = 1;
@@ -863,6 +870,13 @@ struct pangofont {
      * Data passed in to unifont_create().
      */
     int bold, shadowoffset, shadowalways;
+    /*
+     * Cache of character widths, indexed by Unicode code point. In
+     * pixels; -1 means we haven't asked Pango about this character
+     * before.
+     */
+    int *widthcache;
+    unsigned nwidthcache;
 };
 
 static const struct unifont_vtable pangofont_vtable = {
@@ -915,8 +929,8 @@ static int pangofont_check_desc_makes_sense(PangoContext *ctx,
 
     matched = FALSE;
     for (i = 0; i < nfamilies; i++) {
-       if (!g_strcasecmp(pango_font_family_get_name(families[i]),
-                         pango_font_description_get_family(desc))) {
+       if (!g_ascii_strcasecmp(pango_font_family_get_name(families[i]),
+                               pango_font_description_get_family(desc))) {
            matched = TRUE;
            break;
        }
@@ -979,6 +993,8 @@ static unifont *pangofont_create_internal(GtkWidget *widget,
     pfont->bold = bold;
     pfont->shadowoffset = shadowoffset;
     pfont->shadowalways = shadowalways;
+    pfont->widthcache = NULL;
+    pfont->nwidthcache = 0;
 
     pango_font_metrics_unref(metrics);
 
@@ -1032,10 +1048,40 @@ static void pangofont_destroy(unifont *font)
 {
     struct pangofont *pfont = (struct pangofont *)font;
     pango_font_description_free(pfont->desc);
+    sfree(pfont->widthcache);
     g_object_unref(pfont->fset);
     sfree(font);
 }
 
+static int pangofont_char_width(PangoLayout *layout, struct pangofont *pfont,
+                                wchar_t uchr, const char *utfchr, int utflen)
+{
+    /*
+     * Here we check whether a character has the same width as the
+     * character cell it'll be drawn in. Because profiling showed that
+     * pango_layout_get_pixel_extents() was a huge bottleneck when we
+     * were calling it every time we needed to know this, we instead
+     * call it only on characters we don't already know about, and
+     * cache the results.
+     */
+
+    if ((unsigned)uchr >= pfont->nwidthcache) {
+        unsigned newsize = ((int)uchr + 0x100) & ~0xFF;
+        pfont->widthcache = sresize(pfont->widthcache, newsize, int);
+        while (pfont->nwidthcache < newsize)
+            pfont->widthcache[pfont->nwidthcache++] = -1;
+    }
+
+    if (pfont->widthcache[uchr] < 0) {
+        PangoRectangle rect;
+        pango_layout_set_text(layout, utfchr, utflen);
+        pango_layout_get_pixel_extents(layout, NULL, &rect);
+        pfont->widthcache[uchr] = rect.width;
+    }
+
+    return pfont->widthcache[uchr];
+}
+
 static int pangofont_has_glyph(unifont *font, wchar_t glyph)
 {
     /* Pango implements font fallback, so assume it has everything */
@@ -1118,39 +1164,34 @@ static void pangofont_draw_text(GdkDrawable *target, GdkGC *gc, unifont *font,
            clen++;
        n = 1;
 
-        /*
-         * If it's a right-to-left character, we must display it on
-         * its own, to stop Pango helpfully re-reversing our already
-         * reversed text.
-         */
-        if (!is_rtl(string[0])) {
-
+        if (is_rtl(string[0]) ||
+            pangofont_char_width(layout, pfont, string[n-1],
+                                 utfptr, clen) != cellwidth) {
             /*
-             * See if that character has the width we expect.
+             * If this character is a right-to-left one, or has an
+             * unusual width, then we must display it on its own.
              */
-            pango_layout_set_text(layout, utfptr, clen);
-            pango_layout_get_pixel_extents(layout, NULL, &rect);
-
-            if (rect.width == cellwidth) {
-                /*
-                 * Try extracting more characters, for as long as they
-                 * stay well-behaved.
-                 */
-                while (clen < utflen) {
-                    int oldclen = clen;
-                    clen++;                   /* skip UTF-8 introducer byte */
-                    while (clen < utflen &&
-                           (unsigned char)utfptr[clen] >= 0x80 &&
-                           (unsigned char)utfptr[clen] < 0xC0)
-                        clen++;
-                    n++;
-                    pango_layout_set_text(layout, utfptr, clen);
-                    pango_layout_get_pixel_extents(layout, NULL, &rect);
-                    if (rect.width != n * cellwidth) {
-                        clen = oldclen;
-                        n--;
-                        break;
-                    }
+        } else {
+            /*
+             * Try to amalgamate a contiguous string of characters
+             * with the expected sensible width, for the common case
+             * in which we're using a monospaced font and everything
+             * works as expected.
+             */
+            while (clen < utflen) {
+                int oldclen = clen;
+                clen++;                       /* skip UTF-8 introducer byte */
+                while (clen < utflen &&
+                       (unsigned char)utfptr[clen] >= 0x80 &&
+                       (unsigned char)utfptr[clen] < 0xC0)
+                    clen++;
+                n++;
+                if (pangofont_char_width(layout, pfont,
+                                         string[n-1], utfptr + oldclen,
+                                         clen - oldclen) != cellwidth) {
+                    clen = oldclen;
+                    n--;
+                    break;
                 }
             }
         }
@@ -1567,6 +1608,7 @@ unifont *multifont_create(GtkWidget *widget, const char *name,
     if (!font)
         return NULL;
 
+    fallback = NULL;
     if (font->want_fallback) {
        for (i = 0; i < lenof(unifont_types); i++) {
             if (unifont_types[i]->create_fallback) {
@@ -1712,7 +1754,7 @@ static int strnullcasecmp(const char *a, const char *b)
     /*
      * Otherwise, ordinary strcasecmp.
      */
-    return g_strcasecmp(a, b);
+    return g_ascii_strcasecmp(a, b);
 }
 
 static int fontinfo_realname_compare(void *av, void *bv)
@@ -2282,6 +2324,8 @@ static fontinfo *update_for_intended_size(unifontsel_internal *fs,
      */
     below = findrelpos234(fs->fonts_by_selorder, &info2, NULL,
                          REL234_LE, &pos);
+    if (!below)
+        pos = -1;
     above = index234(fs->fonts_by_selorder, pos+1);
 
     /*
@@ -2289,7 +2333,7 @@ static fontinfo *update_for_intended_size(unifontsel_internal *fs,
      * case. If we have, it'll be in `below' and not `above',
      * because we did a REL234_LE rather than REL234_LT search.
      */
-    if (!fontinfo_selorder_compare(&info2, below))
+    if (below && !fontinfo_selorder_compare(&info2, below))
        return below;
 
     /*