Generalize run-time debugging checks.
[bpt/emacs.git] / src / ftfont.c
index ad01149..29732e4 100644 (file)
@@ -1,5 +1,5 @@
 /* ftfont.c -- FreeType font driver.
-   Copyright (C) 2006-2011 Free Software Foundation, Inc.
+   Copyright (C) 2006-2012 Free Software Foundation, Inc.
    Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
      National Institute of Advanced Industrial Science and Technology (AIST)
      Registration Number H13PRO009
@@ -39,7 +39,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "ftfont.h"
 
 /* Symbolic type of this font-driver.  */
-Lisp_Object Qfreetype;
+static Lisp_Object Qfreetype;
 
 /* Fontconfig's generic families and their aliases.  */
 static Lisp_Object Qmonospace, Qsans_serif, Qserif, Qmono, Qsans, Qsans__serif;
@@ -160,11 +160,20 @@ static struct
 static Lisp_Object
 get_adstyle_property (FcPattern *p)
 {
+  FcChar8 *fcstr;
   char *str, *end;
   Lisp_Object adstyle;
 
-  if (FcPatternGetString (p, FC_STYLE, 0, (FcChar8 **) &str) != FcResultMatch)
+#ifdef FC_FONTFORMAT
+  if ((FcPatternGetString (p, FC_FONTFORMAT, 0, &fcstr) == FcResultMatch)
+      && xstrcasecmp ((char *) fcstr, "bdf") != 0
+      && xstrcasecmp ((char *) fcstr, "pcf") != 0)
+    /* Not a BDF nor PCF font.  */
+    return Qnil;
+#endif
+  if (FcPatternGetString (p, FC_STYLE, 0, &fcstr) != FcResultMatch)
     return Qnil;
+  str = (char *) fcstr;
   for (end = str; *end && *end != ' '; end++);
   if (*end)
     {
@@ -189,19 +198,20 @@ static Lisp_Object
 ftfont_pattern_entity (FcPattern *p, Lisp_Object extra)
 {
   Lisp_Object key, cache, entity;
-  char *file, *str;
+  FcChar8 *str;
+  char *file;
   int idx;
   int numeric;
   double dbl;
   FcBool b;
 
-  if (FcPatternGetString (p, FC_FILE, 0, (FcChar8 **) &file) != FcResultMatch)
+  if (FcPatternGetString (p, FC_FILE, 0, &str) != FcResultMatch)
     return Qnil;
   if (FcPatternGetInteger (p, FC_INDEX, 0, &idx) != FcResultMatch)
     return Qnil;
 
-  key = Fcons (make_unibyte_string ((char *) file, strlen ((char *) file)),
-              make_number (idx));
+  file = (char *) str;
+  key = Fcons (make_unibyte_string (file, strlen (file)), make_number (idx));
   cache = ftfont_lookup_cache (key, FTFONT_CACHE_FOR_ENTITY);
   entity = XCAR (cache);
   if (! NILP (entity))
@@ -211,6 +221,10 @@ ftfont_pattern_entity (FcPattern *p, Lisp_Object extra)
 
       for (i = 0; i < FONT_OBJLIST_INDEX; i++)
        ASET (val, i, AREF (entity, i));
+
+      ASET (val, FONT_EXTRA_INDEX, Fcopy_sequence (extra));
+      font_put_extra (val, QCfont_entity, key);
+
       return val;
     }
   entity = font_make_entity ();
@@ -219,10 +233,16 @@ ftfont_pattern_entity (FcPattern *p, Lisp_Object extra)
   ASET (entity, FONT_TYPE_INDEX, Qfreetype);
   ASET (entity, FONT_REGISTRY_INDEX, Qiso10646_1);
 
-  if (FcPatternGetString (p, FC_FOUNDRY, 0, (FcChar8 **) &str) == FcResultMatch)
-    ASET (entity, FONT_FOUNDRY_INDEX, font_intern_prop (str, strlen (str), 1));
-  if (FcPatternGetString (p, FC_FAMILY, 0, (FcChar8 **) &str) == FcResultMatch)
-    ASET (entity, FONT_FAMILY_INDEX, font_intern_prop (str, strlen (str), 1));
+  if (FcPatternGetString (p, FC_FOUNDRY, 0, &str) == FcResultMatch)
+    {
+      char *s = (char *) str;
+      ASET (entity, FONT_FOUNDRY_INDEX, font_intern_prop (s, strlen (s), 1));
+    }
+  if (FcPatternGetString (p, FC_FAMILY, 0, &str) == FcResultMatch)
+    {
+      char *s = (char *) str;
+      ASET (entity, FONT_FAMILY_INDEX, font_intern_prop (s, strlen (s), 1));
+    }
   if (FcPatternGetInteger (p, FC_WEIGHT, 0, &numeric) == FcResultMatch)
     {
       if (numeric >= FC_WEIGHT_REGULAR && numeric < FC_WEIGHT_MEDIUM)
@@ -259,7 +279,7 @@ ftfont_pattern_entity (FcPattern *p, Lisp_Object extra)
     }
   else
     {
-      /* As this font is not scalable, parhaps this is a BDF or PCF
+      /* As this font is not scalable, perhaps this is a BDF or PCF
         font. */
       FT_Face ft_face;
 
@@ -352,7 +372,7 @@ ftfont_lookup_cache (Lisp_Object key, enum ftfont_cache_for cache_for)
     {
       entity = key;
       val = assq_no_quit (QCfont_entity, AREF (entity, FONT_EXTRA_INDEX));
-      xassert (CONSP (val));
+      eassert (CONSP (val));
       key = XCDR (val);
     }
   else
@@ -505,7 +525,7 @@ static int ftfont_variation_glyphs (struct font *, int c,
 
 struct font_driver ftfont_driver =
   {
-    0,                         /* Qfreetype */
+    LISP_INITIALLY_ZERO,       /* Qfreetype */
     0,                         /* case insensitive */
     ftfont_get_cache,
     ftfont_list,
@@ -673,7 +693,10 @@ ftfont_get_open_type_spec (Lisp_Object otf_spec)
       if (NILP (val))
        continue;
       len = Flength (val);
-      spec->features[i] = malloc (sizeof (int) * XINT (len));
+      spec->features[i] =
+       (min (PTRDIFF_MAX, SIZE_MAX) / sizeof (int) < XINT (len)
+        ? 0
+        : malloc (sizeof (int) * XINT (len)));
       if (! spec->features[i])
        {
          if (i > 0 && spec->features[0])
@@ -714,7 +737,7 @@ ftfont_spec_pattern (Lisp_Object spec, char *otlayout, struct OpenTypeSpec **ots
 
   if ((n = FONT_SLANT_NUMERIC (spec)) >= 0
       && n < 100)
-    /* Fontconfig doesn't support reverse-italic/obligue.  */
+    /* Fontconfig doesn't support reverse-italic/oblique.  */
     return NULL;
 
   if (INTEGERP (AREF (spec, FONT_DPI_INDEX)))
@@ -806,7 +829,7 @@ ftfont_spec_pattern (Lisp_Object spec, char *otlayout, struct OpenTypeSpec **ots
            goto err;
          for (chars = XCDR (chars); CONSP (chars); chars = XCDR (chars))
            if (CHARACTERP (XCAR (chars))
-               && ! FcCharSetAddChar (charset, XUINT (XCAR (chars))))
+               && ! FcCharSetAddChar (charset, XFASTINT (XCAR (chars))))
              goto err;
        }
     }
@@ -937,7 +960,7 @@ ftfont_list (Lisp_Object frame, Lisp_Object spec)
   /* Need fix because this finds any fonts.  */
   if (fontset->nfont == 0 && ! NILP (family))
     {
-      /* Try maching with configuration.  For instance, the
+      /* Try matching with configuration.  For instance, the
         configuration may specify "Nimbus Mono L" as an alias of
         "Courier".  */
       FcPattern *pat = FcPatternBuild (0, FC_FAMILY, FcTypeString,
@@ -1010,13 +1033,13 @@ ftfont_list (Lisp_Object frame, Lisp_Object spec)
 #endif /* HAVE_LIBOTF */
       if (VECTORP (chars))
        {
-         int j;
+         ptrdiff_t j;
 
          if (FcPatternGetCharSet (fontset->fonts[i], FC_CHARSET, 0, &charset)
              != FcResultMatch)
            continue;
          for (j = 0; j < ASIZE (chars); j++)
-           if (NATNUMP (AREF (chars, j))
+           if (TYPE_RANGED_INTEGERP (FcChar32, AREF (chars, j))
                && FcCharSetHasChar (charset, XFASTINT (AREF (chars, j))))
              break;
          if (j == ASIZE (chars))
@@ -1302,7 +1325,7 @@ ftfont_close (FRAME_PTR f, struct font *font)
 
   val = Fcons (font->props[FONT_FILE_INDEX], make_number (ftfont_info->index));
   cache = ftfont_lookup_cache (val, FTFONT_CACHE_FOR_FACE);
-  xassert (CONSP (cache));
+  eassert (CONSP (cache));
   val = XCDR (cache);
   (XSAVE_VALUE (val)->integer)--;
   if (XSAVE_VALUE (val)->integer == 0)
@@ -1446,7 +1469,7 @@ ftfont_get_bitmap (struct font *font, unsigned int code, struct font_bitmap *bit
        : ft_face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_LCD_V ? 8
        : -1);
   if (bitmap->bits_per_pixel < 0)
-    /* We don't suport that kind of pixel mode.  */
+    /* We don't support that kind of pixel mode.  */
     return -1;
   bitmap->rows = ft_face->glyph->bitmap.rows;
   bitmap->width = ft_face->glyph->bitmap.width;
@@ -1603,7 +1626,6 @@ ftfont_get_metrics (MFLTFont *font, MFLTGlyphString *gstring,
        if (g->code != FONT_INVALID_CODE)
          {
            FT_Glyph_Metrics *m;
-           int lbearing, rbearing, ascent, descent, xadv;
 
            if (FT_Load_Glyph (ft_face, g->code, FT_LOAD_DEFAULT) != 0)
              abort ();
@@ -1753,15 +1775,10 @@ static OTF_GlyphString otf_gstring;
 static void
 setup_otf_gstring (int size)
 {
-  if (otf_gstring.size == 0)
+  if (otf_gstring.size < size)
     {
-      otf_gstring.glyphs = (OTF_Glyph *) xmalloc (sizeof (OTF_Glyph) * size);
-      otf_gstring.size = size;
-    }
-  else if (otf_gstring.size < size)
-    {
-      otf_gstring.glyphs = xrealloc (otf_gstring.glyphs,
-                                    sizeof (OTF_Glyph) * size);
+      otf_gstring.glyphs = xnrealloc (otf_gstring.glyphs,
+                                     size, sizeof (OTF_Glyph));
       otf_gstring.size = size;
     }
   otf_gstring.used = size;
@@ -1839,7 +1856,7 @@ ftfont_drive_otf (MFLTFont *font,
   setup_otf_gstring (len);
   for (i = 0; i < len; i++)
     {
-      otf_gstring.glyphs[i].c = in->glyphs[from + i].c;
+      otf_gstring.glyphs[i].c = in->glyphs[from + i].c & 0x11FFFF;
       otf_gstring.glyphs[i].glyph_id = in->glyphs[from + i].code;
     }
 
@@ -1858,7 +1875,6 @@ ftfont_drive_otf (MFLTFont *font,
        {
          MFLTGlyph *g;
          int min_from, max_to;
-         int j;
          int feature_idx = otfg->positioning_type >> 4;
 
          g = out->glyphs + out->used;
@@ -2378,8 +2394,8 @@ static Lisp_Object
 ftfont_shape_by_flt (Lisp_Object lgstring, struct font *font,
                     FT_Face ft_face, OTF *otf, FT_Matrix *matrix)
 {
-  EMACS_UINT len = LGSTRING_GLYPH_LEN (lgstring);
-  EMACS_UINT i;
+  ptrdiff_t len = LGSTRING_GLYPH_LEN (lgstring);
+  ptrdiff_t i;
   struct MFLTFontFT flt_font_ft;
   MFLT *flt = NULL;
   int with_variation_selector = 0;
@@ -2405,7 +2421,10 @@ ftfont_shape_by_flt (Lisp_Object lgstring, struct font *font,
       if (CHAR_VARIATION_SELECTOR_P (c))
        with_variation_selector++;
     }
+
   len = i;
+  lint_assume (len <= STRING_BYTES_BOUND);
+
   if (with_variation_selector)
     {
       setup_otf_gstring (len);
@@ -2435,17 +2454,19 @@ ftfont_shape_by_flt (Lisp_Object lgstring, struct font *font,
        }
     }
 
+  if (INT_MAX / 2 < len)
+    memory_full (SIZE_MAX);
+
   if (gstring.allocated == 0)
     {
-      gstring.allocated = len * 2;
       gstring.glyph_size = sizeof (MFLTGlyph);
-      gstring.glyphs = xmalloc (sizeof (MFLTGlyph) * gstring.allocated);
+      gstring.glyphs = xnmalloc (len * 2, sizeof (MFLTGlyph));
+      gstring.allocated = len * 2;
     }
   else if (gstring.allocated < len * 2)
     {
+      gstring.glyphs = xnrealloc (gstring.glyphs, len * 2, sizeof (MFLTGlyph));
       gstring.allocated = len * 2;
-      gstring.glyphs = xrealloc (gstring.glyphs,
-                                sizeof (MFLTGlyph) * gstring.allocated);
     }
   memset (gstring.glyphs, 0, sizeof (MFLTGlyph) * len);
   for (i = 0; i < len; i++)
@@ -2494,9 +2515,11 @@ ftfont_shape_by_flt (Lisp_Object lgstring, struct font *font,
       int result = mflt_run (&gstring, 0, len, &flt_font_ft.flt_font, flt);
       if (result != -2)
        break;
-      gstring.allocated += gstring.allocated;
-      gstring.glyphs = xrealloc (gstring.glyphs,
-                                sizeof (MFLTGlyph) * gstring.allocated);
+      if (INT_MAX / 2 < gstring.allocated)
+       memory_full (SIZE_MAX);
+      gstring.glyphs = xnrealloc (gstring.glyphs,
+                                 gstring.allocated, 2 * sizeof (MFLTGlyph));
+      gstring.allocated *= 2;
     }
   if (gstring.used > LGSTRING_GLYPH_LEN (lgstring))
     return Qnil;