@@ -233,18 +233,6 @@ getfont(PyObject *self_, PyObject *args, PyObject *kw) {
233233 return (PyObject * )self ;
234234}
235235
236- static int
237- font_getchar (PyObject * string , int index , FT_ULong * char_out ) {
238- if (PyUnicode_Check (string )) {
239- if (index >= PyUnicode_GET_LENGTH (string )) {
240- return 0 ;
241- }
242- * char_out = PyUnicode_READ_CHAR (string , index );
243- return 1 ;
244- }
245- return 0 ;
246- }
247-
248236#ifdef HAVE_RAQM
249237
250238static size_t
@@ -266,28 +254,34 @@ text_layout_raqm(
266254 goto failed ;
267255 }
268256
257+ Py_ssize_t size ;
258+ int set_text ;
269259 if (PyUnicode_Check (string )) {
270260 Py_UCS4 * text = PyUnicode_AsUCS4Copy (string );
271- Py_ssize_t size = PyUnicode_GET_LENGTH (string );
261+ size = PyUnicode_GET_LENGTH (string );
272262 if (!text || !size ) {
273263 /* return 0 and clean up, no glyphs==no size,
274264 and raqm fails with empty strings */
275265 goto failed ;
276266 }
277- int set_text = raqm_set_text (rq , text , size );
267+ set_text = raqm_set_text (rq , text , size );
278268 PyMem_Free (text );
279- if (!set_text ) {
280- PyErr_SetString (PyExc_ValueError , "raqm_set_text() failed" );
269+ } else {
270+ char * buffer ;
271+ PyBytes_AsStringAndSize (string , & buffer , & size );
272+ if (!buffer || !size ) {
273+ /* return 0 and clean up, no glyphs==no size,
274+ and raqm fails with empty strings */
281275 goto failed ;
282276 }
283- if ( lang ) {
284- if (! raqm_set_language ( rq , lang , start , size )) {
285- PyErr_SetString ( PyExc_ValueError , "raqm_set_language() failed" );
286- goto failed ;
287- }
288- }
289- } else {
290- PyErr_SetString (PyExc_TypeError , "expected string " );
277+ set_text = raqm_set_text_utf8 ( rq , buffer , size );
278+ }
279+ if (! set_text ) {
280+ PyErr_SetString ( PyExc_ValueError , "raqm_set_text() failed" ) ;
281+ goto failed ;
282+ }
283+ if ( lang && ! raqm_set_language ( rq , lang , start , size )) {
284+ PyErr_SetString (PyExc_ValueError , "raqm_set_language() failed " );
291285 goto failed ;
292286 }
293287
@@ -405,28 +399,25 @@ text_layout_fallback(
405399 GlyphInfo * * glyph_info ,
406400 int mask ,
407401 int color ) {
408- int error , load_flags ;
402+ int error , load_flags , i ;
403+ char * buffer = NULL ;
409404 FT_ULong ch ;
410405 Py_ssize_t count ;
411406 FT_GlyphSlot glyph ;
412407 FT_Bool kerning = FT_HAS_KERNING (self -> face );
413408 FT_UInt last_index = 0 ;
414- int i ;
415409
416410 if (features != Py_None || dir != NULL || lang != NULL ) {
417411 PyErr_SetString (
418412 PyExc_KeyError ,
419413 "setting text direction, language or font features is not supported "
420414 "without libraqm" );
421415 }
422- if (!PyUnicode_Check (string )) {
423- PyErr_SetString (PyExc_TypeError , "expected string" );
424- return 0 ;
425- }
426416
427- count = 0 ;
428- while (font_getchar (string , count , & ch )) {
429- count ++ ;
417+ if (PyUnicode_Check (string )) {
418+ count = PyUnicode_GET_LENGTH (string );
419+ } else {
420+ PyBytes_AsStringAndSize (string , & buffer , & count );
430421 }
431422 if (count == 0 ) {
432423 return 0 ;
@@ -445,7 +436,12 @@ text_layout_fallback(
445436 if (color ) {
446437 load_flags |= FT_LOAD_COLOR ;
447438 }
448- for (i = 0 ; font_getchar (string , i , & ch ); i ++ ) {
439+ for (i = 0 ; i < count ; i ++ ) {
440+ if (buffer ) {
441+ ch = buffer [i ];
442+ } else {
443+ ch = PyUnicode_READ_CHAR (string , i );
444+ }
449445 (* glyph_info )[i ].index = FT_Get_Char_Index (self -> face , ch );
450446 error = FT_Load_Glyph (self -> face , (* glyph_info )[i ].index , load_flags );
451447 if (error ) {
0 commit comments