Commits

Anonymous committed 9d8ed32

unicode cleanups

Comments (0)

Files changed (9)

 # BREAK = change breaks existing code
 # BUG	= fixed a bug that was (or could have been) crashing
 
+July 17, 2002
+	Michael Hudson's unicode cleanups
+
 June 15, 2002
 	added get_time() and get_rawtime() to Clock
 
 different distributions.
 <p></p>
 There are several binary RPM packages for linux machines. These are
-actually a little bit of work to install, since you will also need
-several RPM packages for the dependencies. There is a good chance
+actually a little bit of work to install, since you will also need several
+RPM packages for the dependencies. There is a good chance
 your linux distribution came with the needed dependencies (like Python
-and SDL). There are binary RPMs available from the
-website for each dependency.
+and SDL). There are binary RPMs available from the website for each dependency.
 </p><p>
 For debian systems, pygame is actively maintained in the debian
-archives. It is available for the "woody" release. Visit the
+archives.  Visit the
 debian pygame page for more information.
 <a href=http://packages.debian.org/cgi-bin/search_packages.pl?keywords=pygame>http://packages.debian.org/cgi-bin/search_packages.pl?keywords=pygame</a>
 </p><p>
 
 <big><b><u>Unix Compiling Info</u></b></big><blockquote><p>
 Compiling from linux shouldn't give you any problems. One thing
-you must keep in mind is that most linux packages separate the
-actual library from the "dev" files needed to compile. To build on
-linux you'll need to make sure the packages like "SDL-dev" are
+you must keep in mind is that most linux RPM packages separate the
+actual library from the "dev" files needed to compile. To build you
+will need to make sure the packages like "SDL-dev" are
 installed.
 <p></p>
 You can check to see if SDL is ready to be built from by running
 the command "sdl-config" and seeing if it is found.
-<p></p>
-Depending on where the SMPEG headers are installed, the pygame
-"config.py" script might not actually find it. If this happens
-you can easily fix the SMPEG line in the "Setup" script to
-use the correct flags. Then also uncomment the line for the
-"movie" module.
-<p></p>
-Several users have run into error compiling when trying to use the
-Numeric Python header files. This is a problem with the autoconfigure
-script. The script foolishly assumes if it can import the Numeric
-module, then the headers will be available for compile. You need
-to either install the correct "python-numeric-dev" package for your
-system, or comment out the line for "surfarray" in the "Setup" file.
 </p></blockquote><br>
 
 <big><b><u>OSX Compiling Info</u></b></big><blockquote><p>
      for each dependency.
 
      For debian systems, pygame is actively maintained in the debian
-     archives. It is available for the "woody" release. Visit the debian
-     pygame page for more information.
+     archives. Visit the debian pygame page for more information.
      http://packages.debian.org/cgi-bin/search_packages.pl?keywords=pyga
      me
 
    Unix Compiling Info
 
      Compiling from linux shouldn't give you any problems. One thing you
-     must keep in mind is that most linux packages separate the actual
-     library from the "dev" files needed to compile. To build on linux
-     you'll need to make sure the packages like "SDL-dev" are installed.
+     must keep in mind is that most linux RPM packages separate the
+     actual library from the "dev" files needed to compile. To build you
+     will need to make sure the packages like "SDL-dev" are installed.
 
      You can check to see if SDL is ready to be built from by running
      the command "sdl-config" and seeing if it is found.
 
-     Depending on where the SMPEG headers are installed, the pygame
-     "config.py" script might not actually find it. If this happens you
-     can easily fix the SMPEG line in the "Setup" script to use the
-     correct flags. Then also uncomment the line for the "movie" module.
-
-     Several users have run into error compiling when trying to use the
-     Numeric Python header files. This is a problem with the
-     autoconfigure script. The script foolishly assumes if it can import
-     the Numeric module, then the headers will be available for compile.
-     You need to either install the correct "python-numeric-dev" package
-     for your system, or comment out the line for "surfarray" in the
-     "Setup" file.
-
    OSX Compiling Info
 
      Building pygame on OSX is not really supported yet. Actually, the
 <li>Thanks to those sending in patches and fixes:
     Niki Spahiev, Gordon Tyler, Nathaniel Pryce, 
     Dave Wallace, John Popplewell, Michael Urman,
-    Andrew Straw</li>
+    Andrew Straw, Michael Hudson</li>
 <li>And our bug hunters above and beyond:
     Angus, Guillaume Proux, Frank Raiser,
     Austin Henry, Kaweh Kazemi, Arturo Aldama,
 
      Thanks to those sending in patches and fixes: Niki Spahiev, Gordon
    Tyler, Nathaniel Pryce, Dave Wallace, John Popplewell, Michael Urman,
-   Andrew Straw
+   Andrew Straw, Michael Hudson
 
      And our bug hunters above and beyond: Angus, Guillaume Proux, Frank
    Raiser, Austin Henry, Kaweh Kazemi, Arturo Aldama, Mike Mulcheck, Rene
 	}
 }
 
+#ifdef Py_USING_UNICODE
+
+static PyObject* our_unichr(long uni)
+{
+	static PyObject* bltin_unichr = NULL;
+
+	if (bltin_unichr == NULL) {
+		PyObject* bltins;
+		
+		bltins = PyImport_ImportModule("__builtin__");
+		bltin_unichr = PyObject_GetAttrString(bltins, "unichr");
+
+		Py_DECREF(bltins);
+	}
+
+	return PyEval_CallFunction(bltin_unichr, "(l)", uni);
+}
+
+static PyObject* our_empty_ustr(void)
+{
+	static PyObject* empty_ustr = NULL;
+
+	if (empty_ustr == NULL) {
+		PyObject* bltins;
+		PyObject* bltin_unicode;
+		
+		bltins = PyImport_ImportModule("__builtin__");
+		bltin_unicode = PyObject_GetAttrString(bltins, "unicode");
+		empty_ustr = PyEval_CallFunction(bltin_unicode, "(s)", "");
+
+		Py_DECREF(bltin_unicode);
+		Py_DECREF(bltins);
+	}
+
+	Py_INCREF(empty_ustr);
+
+	return empty_ustr;
+}
+
+#else
+
+static PyObject* our_unichr(long uni)
+{
+	return PyInt_FromLong(uni);
+}
+
+static PyObject* our_empty_ustr(void)
+{
+	return PyInt_FromLong(0);
+}
+
+#endif /* Py_USING_UNICODE */
 
 static PyObject* dict_from_event(SDL_Event* event)
 {
 			return dict;
 	}
 
-
 	if(!(dict = PyDict_New()))
 		return NULL;
 	switch(event->type)
 		break;
 	case SDL_KEYDOWN:
 		if(event->key.keysym.unicode)
-			insobj(dict, "unicode", PyUnicode_FromUnicode(
-							(Py_UNICODE*)&event->key.keysym.unicode,
-							event->key.keysym.unicode > 0));
+			insobj(dict, "unicode", our_unichr(event->key.keysym.unicode));
 		else
-			insobj(dict, "unicode",
-							PyUnicode_FromObject(PyString_FromString("")));
+			insobj(dict, "unicode", our_empty_ustr());
 	case SDL_KEYUP:
 		insobj(dict, "key", PyInt_FromLong(event->key.keysym.sym));
 		insobj(dict, "mod", PyInt_FromLong(event->key.keysym.mod));
 
 	if(PyUnicode_Check(text))
 	{
-		Py_UNICODE* string = PyUnicode_AsUnicode(text);
+		PyObject* strob = PyEval_CallMethod(text, "encode", "(s)", "utf-8");
+		char *string = PyString_AsString(text);
+
 		if(aa)
 		{
 			if(!bg_rgba_obj)
-				surf = TTF_RenderUNICODE_Blended(font, string, foreg);
+				surf = TTF_RenderUTF8_Blended(font, string, foreg);
 			else
-				surf = TTF_RenderUNICODE_Shaded(font, string, foreg, backg);
+				surf = TTF_RenderUTF8_Shaded(font, string, foreg, backg);
 		}
 		else
-			surf = TTF_RenderUNICODE_Solid(font, string, foreg);
+			surf = TTF_RenderUTF8_Solid(font, string, foreg);
+
+		Py_DECREF(strob);
 	}
 	else if(PyString_Check(text))
 	{
 
 	if(PyUnicode_Check(text))
 	{
-		Py_UNICODE* string = PyUnicode_AsUnicode(text);
-		TTF_SizeUNICODE(font, string, &w, &h);
+		PyObject* strob = PyEval_CallMethod(text, "encode", "(s)", "utf-8");
+		char *string = PyString_AsString(text);
+
+		TTF_SizeUTF8(font, string, &w, &h);
+
+		Py_DECREF(text);
 	}
 	else if(PyString_Check(text))
 	{
 #define PySequence_Size(x) PySequence_Length(x)
 
 #define PyUnicode_Check(text) 0
-#define PyUnicode_AsUnicode(text) text
-#define Py_UNICODE void
-#define PyUnicode_FromUnicode(p,c) PyLong_FromLong(*(short*)(p))
-#define PyUnicode_FromObject(s) s
 #endif
 
-
-
 /* macros used throughout the source */
 #define RAISE(x,y) (PyErr_SetString((x), (y)), (PyObject*)NULL)
 #define RETURN_NONE return (Py_INCREF(Py_None), Py_None);
         /*see if we should handle alpha ourselves*/
         if(dest->format->Amask && dest->flags&SDL_SRCALPHA &&
                     (dest->format->BytesPerPixel == 2 || dest->format->BytesPerPixel==4))
+	{
             result = pygame_AlphaBlit(src, (SDL_Rect*)src_rect, dest, &dest_rect);
+	}
         else
+	{
             result = SDL_BlitSurface(src, (SDL_Rect*)src_rect, dest, &dest_rect);
-        
+        }
+
         
 	if(didconvert)
 		SDL_FreeSurface(src);