Consolidate redundant definitions in s/bsd-common.h.
[bpt/emacs.git] / src / w32uniscribe.c
index da49036..cfdf629 100644 (file)
@@ -1,5 +1,5 @@
 /* Font backend for the Microsoft W32 Uniscribe API.
-   Copyright (C) 2008 Free Software Foundation, Inc.
+   Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
@@ -22,11 +22,12 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
    Windows 2000, though most users of older systems will have it
    since it installs with Internet Explorer 5.0 and other software.
    We only enable the feature if it is available, so there is no chance
-   of calling non-existant functions.  */
+   of calling non-existent functions.  */
 #undef _WIN32_WINNT
 #define _WIN32_WINNT 0x500
 #include <windows.h>
 #include <usp10.h>
+#include <setjmp.h>
 
 #include "lisp.h"
 #include "w32term.h"
@@ -34,6 +35,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include "dispextern.h"
 #include "character.h"
 #include "charset.h"
+#include "composite.h"
 #include "fontset.h"
 #include "font.h"
 #include "w32font.h"
@@ -77,7 +79,7 @@ uniscribe_list (frame, font_spec)
      Lisp_Object frame, font_spec;
 {
   Lisp_Object fonts = w32font_list_internal (frame, font_spec, 1);
-  font_add_log ("uniscribe-list", font_spec, fonts);
+  FONT_ADD_LOG ("uniscribe-list", font_spec, fonts);
   return fonts;
 }
 
@@ -86,7 +88,7 @@ uniscribe_match (frame, font_spec)
      Lisp_Object frame, font_spec;
 {
   Lisp_Object entity = w32font_match_internal (frame, font_spec, 1);
-  font_add_log ("uniscribe-match", font_spec, entity);
+  FONT_ADD_LOG ("uniscribe-match", font_spec, entity);
   return entity;
 }
 
@@ -135,6 +137,9 @@ uniscribe_open (f, font_entity, pixel_size)
   /* Initialize the cache for this font.  */
   uniscribe_font->cache = NULL;
 
+  /* Uniscribe backend uses glyph indices.  */
+  uniscribe_font->w32_font.glyph_idx = ETO_GLYPH_INDEX;
+
   /* Mark the format as opentype  */
   uniscribe_font->w32_font.font.props[FONT_FORMAT_INDEX] = Qopentype;
   uniscribe_font->w32_font.font.driver = &uniscribe_font_driver;
@@ -220,7 +225,7 @@ uniscribe_shape (lgstring)
   uniscribe_font = (struct uniscribe_font_info *) font;
 
   /* Get the chars from lgstring in a form we can use with uniscribe.  */
-  max_glyphs = nchars = LGSTRING_LENGTH (lgstring);
+  max_glyphs = nchars = LGSTRING_GLYPH_LEN (lgstring);
   done_glyphs = 0;
   chars = (wchar_t *) alloca (nchars * sizeof (wchar_t));
   for (i = 0; i < nchars; i++)
@@ -335,13 +340,18 @@ uniscribe_shape (lgstring)
                  int lglyph_index = j + done_glyphs;
                  Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, lglyph_index);
                  ABC char_metric;
+                 unsigned gl;
 
                  if (NILP (lglyph))
                    {
                      lglyph = Fmake_vector (make_number (LGLYPH_SIZE), Qnil);
                      LGSTRING_SET_GLYPH (lgstring, lglyph_index, lglyph);
                    }
-                 LGLYPH_SET_CODE (lglyph, glyphs[j]);
+                 /* Copy to a 32-bit data type to shut up the
+                    compiler warning in LGLYPH_SET_CODE about
+                    comparison being always false.  */
+                 gl = glyphs[j];
+                 LGLYPH_SET_CODE (lglyph, gl);
 
                  /* Detect clusters, for linking codes back to characters.  */
                  if (attributes[j].fClusterStart)
@@ -481,13 +491,16 @@ uniscribe_encode_char (font, c)
       if (SUCCEEDED (ScriptItemize (ch, len, 2, NULL, NULL, items, &nitems)))
        {
          HRESULT result;
-          /* Some Indic characters result in more than 1 glyph.  */
-          WORD glyphs[1], clusters[1];
-          SCRIPT_VISATTR attrs[1];
+          /* Surrogates seem to need 2 here, even though only one glyph is
+            returned.  Indic characters can also produce 2 or more glyphs for
+            a single code point, but they need to use uniscribe_shape
+            above for correct display.  */
+          WORD glyphs[2], clusters[2];
+          SCRIPT_VISATTR attrs[2];
           int nglyphs;
 
           result = ScriptShape (context, &(uniscribe_font->cache),
-                                ch, len, 1, &(items[0].a),
+                                ch, len, 2, &(items[0].a),
                                 glyphs, clusters, attrs, &nglyphs);
 
           if (result == E_PENDING)
@@ -504,7 +517,10 @@ uniscribe_encode_char (font, c)
 
           if (SUCCEEDED (result) && nglyphs == 1)
             {
-              code = glyphs[0];
+             /* Some fonts return .notdef glyphs instead of failing.
+                (Truetype spec reserves glyph code 0 for .notdef)  */
+             if (glyphs[0])
+               code = glyphs[0];
             }
           else if (SUCCEEDED (result) || result == E_OUTOFMEMORY)
             {
@@ -514,11 +530,8 @@ uniscribe_encode_char (font, c)
                  later.  */
               result = ScriptGetCMap (context, &(uniscribe_font->cache),
                                       ch, len, 0, glyphs);
-              if (SUCCEEDED (result))
-                return glyphs[0];
-              else
-                return 0; /* notdef - enough in some cases to get the script
-                             engine working, but not others... */
+              if (SUCCEEDED (result) && glyphs[0])
+                code = glyphs[0];
             }
        }
     }
@@ -589,8 +602,7 @@ add_opentype_font_name_to_list (logical_font, physical_font, font_type,
       && !(physical_font->ntmFontSig.fsUsb[0] & 0x3fffffff))
     return 1;
 
-  family = font_intern_prop (logical_font->elfLogFont.lfFaceName,
-                            strlen (logical_font->elfLogFont.lfFaceName), 1);
+  family = intern_font_name (logical_font->elfLogFont.lfFaceName);
   if (! memq_no_quit (family, *list))
     *list = Fcons (family, *list);
 
@@ -654,7 +666,7 @@ int uniscribe_check_otf (font, otf_spec)
   struct gcpro gcpro1;
 
   /* Check the spec is in the right format.  */
-  if (!CONSP (otf_spec) || Flength (otf_spec) < 3)
+  if (!CONSP (otf_spec) || XINT (Flength (otf_spec)) < 3)
     return 0;
 
   /* Break otf_spec into its components.  */
@@ -725,10 +737,12 @@ int uniscribe_check_otf (font, otf_spec)
              OTF_INT16_VAL (tbl, scriptlist_table + 6 + j * 6, &script_table);
              break;
            }
+#if 0    /* Causes false positives.  */
          /* If there is a DFLT script defined in the font, use it
             if the specified script is not found.  */
          else if (script_id == default_script)
            OTF_INT16_VAL (tbl, scriptlist_table + 6 + j * 6, &script_table);
+#endif
        }
       /* If no specific or default script table was found, then this font
         does not support the script.  */