Wiki copymirror

This is a mirror of the wiki at pascal.chat. Editing, history and sign-in are there.

Lazarus with FPC3.0 without UTF-8 mode

This page explains the problems and their possible solutions when using Lazarus with FPC 3.0+ but without the new UTF-8 mode. It may be needed when big parts of program code depend on system codepage. The UTF-8 mode is explained here:

To disable the new UTF-8 mode, -dDisableUTF8RTL must be defined in project options, "Additions and Overrides" page. There is a button "Use system encoding" to help adding it with one click. This way it affects both the project and all its dependent packages, including LazUtils which contains the relevant code. If you use strings with codepoints > 127 in your project, you have to change the file encoding to the system encoding too.

Bart has collected some reported issues under a meta-issue. Fortunately most of them are solved and Lazarus libraries should be robust also without the UTF-8 mode. There is an unsolved FPC issue about RawByteString though.

Simple Example

Now you are able to build a project without UTF8 dependences.

Write something in your source editor:

pascal
program project1;

{$mode objfpc}{$H+}
{$codepage cp1252}

var
  test: String;

begin
  test := 'ÄÖÜ';
  WriteLn(test);
  ReadLn;
end.

Start your program.

Problem System encoding and Console encoding (Windows)

If you start the simple example, you will maybe see a wrong content for that writeln('ÄÖÜ'). This is because you can have different codepages for the console and the file system. To check which codepage you have you can make a simple test:

pascal
  Writeln('Console output codepage: ', GetTextCodePage(Output));
  Writeln('System codepage: ', DefaultSystemCodePage);

If your console codepage is different from your system codepage, you can change it in that way (original code from forum)

pascal
unit setdefaultcodepages;

interface

uses
  Windows;

implementation

Const
  LF_FACESIZE = 32;

Type
  CONSOLE_FONT_INFOEX = record
    cbSize     : ULONG;
    nFont      : DWORD;
    dwFontSize : COORD;
    FontFamily : UINT;
    FontWeight : UINT;
    FaceName   : array [0..LF_FACESIZE-1] of WCHAR;
  end;

{ Only supported in Vista and onwards!}

function SetCurrentConsoleFontEx(hConsoleOutput: HANDLE; bMaximumWindow: BOOL; var CONSOLE_FONT_INFOEX): BOOL; stdcall; external kernel32;

var
  New_CONSOLE_FONT_INFOEX: CONSOLE_FONT_INFOEX;

initialization
  SetConsoleOutputCP(DefaultSystemCodePage);
  SetTextCodePage(Output, DefaultSystemCodePage);

  FillChar(New_CONSOLE_FONT_INFOEX, SizeOf(CONSOLE_FONT_INFOEX), 0);
  New_CONSOLE_FONT_INFOEX.cbSize := SizeOf(CONSOLE_FONT_INFOEX);
  New_CONSOLE_FONT_INFOEX.FaceName := 'Consolas';
//  New_CONSOLE_FONT_INFOEX.FaceName := 'Lucida Console';  //use Lucida Console for Win XP
  New_CONSOLE_FONT_INFOEX.FontWeight := 400;
  New_CONSOLE_FONT_INFOEX.dwFontSize.Y := 16;

  SetCurrentConsoleFontEx(StdOutputHandle, False, New_CONSOLE_FONT_INFOEX);
end.

If you now build a project and use that unit, all strings are correctly shown (Strings also Shortstrings). For example:

pascal
program project1;

{$mode objfpc}{$H+}
{$codepage cp1252}

uses
  setdefaultcodepages;

const
  StrCP1252 = #$80#$C4#$D6#$8C#$A5;

var
  AStr : String;
  AShortString : ShortString;
begin
  AStr := StrCP1252;
  AShortString := StrCP1252;
  WriteLn(StrCP1252);
  WriteLn('€ÄÖŒ¥');
  WriteLn(AStr);
  WriteLn(AShortString);
  ReadLn;
end.

Problem applications with ACP strings and LCL controls

If you want to use some LCL controls like TLabel, TEdit, TMemo etc., you have to convert the strings, cause the LCL controls use the UTF8 strings further. You can use the method SysToUTF8 for the conversion of a string to a LCL control string and UTF8ToSys for the conversion back.

Example

pascal
procedure TForm1.Button1Click(Sender: TObject);
var
  s: String;
begin
  s := 'Läuse und Flöhe';
  Edit1.Text := SysToUTF8(s);
end;

From the Free Pascal wiki, page "Lazarus with FPC3.0 without UTF-8 mode", as it stood on 2020-02-18 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .