Recent

Author Topic: $CODEPAGE + LCL = Incorrect Display?  (Read 404 times)

gcscaglia

  • Newbie
  • Posts: 6
$CODEPAGE + LCL = Incorrect Display?
« on: April 17, 2025, 09:36:23 pm »
Hello.

I've being trying to solve some problems related to wrong encoding in displayed text on my applications, and I've found some very confusing behavior. I think it might be a bug, but do not know where it might be, if it is a bug at all.

I've attached a MRE of the problem, and also a .gif of the MRE running. The gif is from a Windows 11 system, but the behavior also happens in a Debian 12 system.

I have a source file encoded as ISO 8859-1. This file has string literals with non-ascii characters. If I try to assign these literals directly to e.g. a Label caption, they're not displayed correctly, even including a {$CODEPAGE 8859-1} directive in the source. The confusing bits are two:

  • If I pass that same literal through a simple Format('%s', [...]) call, then it is displayed correctly!
  • If I ask the compiler if the two strings (before and after the Format) are equal, it says they are!

Expressing the above as code:
Code: Pascal  [Select][+][-]
  1. {$CODEPAGE 8859-1}
  2. unit Unit1;
  3.  
  4. // ...
  5.  
  6. begin
  7.   Label1.Caption := 'Acentuação'; // Displays as 'Acentua?' (incorrect)
  8.   Label1.Caption := Format('%s', ['Acentuação']); // Displays as 'Acentuação' (correct)
  9.   if 'Acentuação' = Format('%s', ['Acentuação']) then
  10.     ShowMessage('But they are the same!'); // This statement executes!
  11. end;
  12.  

P.S. Some background on why I must use {$CODEPAGE 8859-1} and cannot just convert my sources to UTF8: This same source files must also be compilable via Delphi 7 and Kylix 3. We're doing a gradual migration from those two to FPC, and since we cannot afford to make the migration all at once (hundreds of millions LOC) we need the same sources to (at least for a while) compile and execute on all 3 systems at once. In the worst case scenario I'm ready to wrap all calls to UI elements in a function to deal with encoding differences between the 3, but I want to avoid that if at all possible.

gcscaglia

  • Newbie
  • Posts: 6
Re: $CODEPAGE + LCL = Incorrect Display?
« Reply #1 on: April 29, 2025, 04:08:38 pm »
Just an update to anyone from the future that finds this post:

After a more careful reading of https://wiki.freepascal.org/Unicode_Support_in_Lazarus and finding https://wiki.freepascal.org/Lazarus_with_FPC3.0_without_UTF-8_mode, I've understood that this behavior is expected.

As initially said, I'll be resorting to using a conversion function, conditionally compiled to a NOP in D7/K3 and a conversion in FPC.

Thaddy

  • Hero Member
  • *****
  • Posts: 16928
  • Ceterum censeo Trump esse delendam
Re: $CODEPAGE + LCL = Incorrect Display?
« Reply #2 on: April 29, 2025, 06:47:53 pm »
D7/K3 is old and not unicode but Ansi + codepage strict.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018