Recent

Author Topic: UTF8 problems with carbon (CFStringToStr)  (Read 6943 times)

Fesch

  • Newbie
  • Posts: 3
    • http://www.fesch.lu
UTF8 problems with carbon (CFStringToStr)
« on: July 10, 2007, 05:52:59 pm »
Hi everybody

I got a new laptop, and as you may image, i bought a MAC. As I wanted to port different of my Delphi/Lazarus applications, everything worked out fine, expect that the carbon interface did not return correct strings.

After a while, I found that the "CFStringToStr" function in the file "CarbonProc.pp" did not work as it should do. Being very frustrated, I replaced it with this code, which now works fine for my system (I didn't test yet on other ones ...).

Code: [Select]
function CFStringToStr(AString: CFStringRef): String;
var
  Str: Pointer;
  StrSize: CFIndex;
  i:integer;
  u:UniChar;
begin
  if AString = nil then
  begin
    Result := '';
    Exit;
  end;
 
  // init emtpy string
  Result:='';
  // get characters ...
  for i:=0 to CFStringGetLength(AString)-1 do
  begin
    //... byte by byte ...
    u:=CFStringGetCharacterAtIndex(AString,i);
    // ... and at them to the result
    Result:=Result+chr(u);
   end;
  // convert the entire result to ANSI
  Result:=ansitoutf8(Result);
end;


I don't know if my system has a problem, or if I did any coding mistake. I did not find much about others having the same problem nor any solution. This is why I decided to post this message.

have a nice day

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: UTF8 problems with carbon (CFStringToStr)
« Reply #1 on: July 11, 2007, 12:00:06 am »
Quote from: "Fesch"
After a while, I found that the "CFStringToStr" function in the file "CarbonProc.pp" did not work as it should do. Being very frustrated, I replaced it with this code, which now works fine for my system (I didn't test yet on other ones ...).


Could you be more specific about what aspect of the CFStringToStr function didn't work. I think this was one of the earliest functions added to the Carbon widgetset.

Thanks.

-Phil

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2584
RE: Re: UTF8 problems with carbon (CFStringToStr)
« Reply #2 on: July 11, 2007, 12:07:13 am »
The LCL (and widgetsets) should handle all text as UTF8 internally, the CFStringRef passed to CFStringToStr should already been converted to utf8. (or done later by the calling proc)
This function should not mess with the encoding.
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

Fesch

  • Newbie
  • Posts: 3
    • http://www.fesch.lu
Re: UTF8 problems with carbon (CFStringToStr)
« Reply #3 on: July 11, 2007, 06:50:35 am »
Quote from: "Phil"

Could you be more specific about what aspect of the CFStringToStr function didn't work.


=> I wrote a very simple program with on edit box (edit1) an one label (label1), adding the following code to the "onChange" event of the edit box: (both elements display UTF8)
Code: [Select]
label1.caption:=edit1.text;

This works well, except if I enter a character with an ASCII code above 127 into the edit box. In that case, the label stays empty!

As already said, I traced it back to the "CFStringToStr" function ...

I am on a PowerBook Pro (Intel) with Mac OS 10.4.10 and the nightly snapshot of Lazarus & FPC from last friday (07/07/07).

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2584
RE: Re: UTF8 problems with carbon (CFStringToStr)
« Reply #4 on: July 12, 2007, 11:27:37 am »
then we either don't get the string in UTF8, or we don't set the caption in UTF8.

what happens if you set the text of a 2nd edit ? (edit2.text := edit1.text;)
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
RE: Re: UTF8 problems with carbon (CFStringToStr)
« Reply #5 on: July 12, 2007, 07:47:01 pm »
Please submit a bug report with a small test program:

http://wiki.lazarus.freepascal.org/How_do_I_create_a_bug_report

Fesch

  • Newbie
  • Posts: 3
    • http://www.fesch.lu
Re: RE: Re: UTF8 problems with carbon (CFStringToStr)
« Reply #6 on: July 14, 2007, 03:29:06 pm »
Quote from: "Marc"

what happens if you set the text of a 2nd edit ? (edit2.text := edit1.text;)


=> The same. "edit2" will contain an emtpy string as soon as a character with ASCII code greater than 128 is entered in "edit1" .... :-/


I submitted a bug-report ...

 

TinyPortal © 2005-2018