[cells-gtk-devel] UTF-8 support

Peter Hildebrandt peter.hildebrandt at gmail.com
Thu Jan 17 13:22:01 UTC 2008


Dmitri Hrapof wrote:
> Ingo Bormuth wrote:
>> The following might be better.
>>
>> Code is: (mk-button :label (sb-ext:string-to-octets "foo äöüß bar" :external-format :utf-8))
>>
>>
>> The value of STRING is #(102 111 111 32 195 164 195 182 195 188                                                                                                           
>>                          ...), which is not of type STRING.                                                                                                               
>>    [Condition of type SIMPLE-TYPE-ERROR]                                                                                                                                                                                                                                                                                                            
> Yes, now it's clear.
> MK-BUTTON expects a Lisp string, being decent Lisp function, not some
> FFI abomination ;)
> So the solution is to place (sb-ext:string-to-octets "foo äöüß bar"
> :external-format :utf-8) deeper, between MK-BUTTON and GTK-BUTTON-SET-LABEL.

Nope.  I think the problem is somewhere in cffi (maybe we're using 
different versions?).  I'm doing:

(gtk-button-set-label (id *b*) (sto "s"))

and getting a backtrace similar to Ingo's

some search shows:

cffi/src/strings.lisp

(defun foreign-string-alloc (string)
   "Allocate a foreign string containing Lisp string STRING.
The string must be freed with FOREIGN-STRING-FREE."
   (check-type string string)
   (let* ((length (1+ (length string)))
          (ptr (foreign-alloc :char :count length)))
     (lisp-string-to-foreign string ptr length)
     ptr))

(check-type string string) is what causes the error.

commenting this out pushes the error further down towards

(defun lisp-string-to-foreign (string ptr size)
   "Copy at most SIZE-1 characters from a Lisp STRING to PTR.
The foreign string will be null-terminated."
   (decf size)
   (loop with i = 0 for char across string
         while (< i size)
         do (%mem-set (char-code char) ptr :unsigned-char (post-incf i))
         finally (%mem-set 0 ptr :unsigned-char i)))

where the problem is in

(%mem-set (char-code char) ptr :unsigned-char (post-incf i))

i.e. we don't need char-code if string is already an array.  Therefore 
replace the line with:

do (%mem-set (if (characterp char) (char-code char) char) ptr 
:unsigned-char (post-incf i))

and voila,

(gtk-button-set-label (id *b*) (sto "üüüüüßßßß"))

works.

And so does:

(mk-button :label (sto "üüüüüßßßß")))

I'm working to puyt the conversion deeper into cffi.

Peter



> As I used extensively only FFI part of cells-gtk, I didn't have to do it.
> 
> Good luck,
> Dmitri
> _______________________________________________
> cells-gtk-devel site list
> cells-gtk-devel at common-lisp.net
> http://common-lisp.net/mailman/listinfo/cells-gtk-devel




More information about the cells-gtk-devel mailing list