Recent

Author Topic: Garbled characters printed by fpImageCanvas.TextOut method  (Read 1120 times)

PeterHu

  • Jr. Member
  • **
  • Posts: 62
Garbled characters printed by fpImageCanvas.TextOut method
« on: July 30, 2025, 04:05:10 pm »
following https://wiki.freepascal.org/fcl-image the fontdraw example works as expected.When I changed the text string to

Code: Pascal  [Select][+][-]
  1. const
  2.   text_str='Hello world!'+
  3.                     'こんにちは世界!'+
  4.                     '你好世界!' +
  5.                     'नमस्ते दुनिया!'+
  6.                     '👋 🌎 !';  
  7.  
Remarks: the last three character of the above string is 👋🌎! but I don't know why the webpage shows like that.

the output png file shows garbled characters after "Hello world!".Please see below screen shot.

【Edit】I have already set {$codepage utf8} and also add lazutf8 unit to the project.

But when I tried TPicture,the saving png file shows proper characters.
Code: [Select]
Image := TPicture.Create();
  Image.PNG.SetSize(1024, 256);

  with Image.PNG.Canvas do begin

    Brush.Color := TColor($F7F4F0);
    Brush.Style := bsSolid;
    FillRect(0, 0, Image.Width, Image.Height);

    Font.Color := clTeal;
    Font.Name := 'DejaVuSans';
    Font.Height :=20;
    TextOut(16, 96, Text);
  end;

  // Save & destroy image
  Image.SaveToFile('test.png');
  Image.Free();       

So was I missing somthing?
« Last Edit: July 31, 2025, 02:43:18 am by PeterHu »

Thaddy

  • Hero Member
  • *****
  • Posts: 19407
  • Glad to be alive.
Re: Garbled characters printed by fpImageCanvas.TextOut method
« Reply #1 on: July 30, 2025, 06:03:40 pm »
Needs utf8 enabled console or terminal:
Code: Pascal  [Select][+][-]
  1. {$codepage utf8}
  2. const
  3.   text_str='Hello world!'+
  4.                     'こんにちは世界!'+
  5.                     '你好世界!' +
  6.                     'नमस्ते दुनिया!'+
  7.                     #128075+#127758+'!';  
  8. begin
  9.   writeln(text_str)
  10. end.
Is that what you expected??
Note the little changes I made in your code.
BTW the last two are neat! I store that in memory.. 8-) 8-)
« Last Edit: July 30, 2025, 06:06:49 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

PeterHu

  • Jr. Member
  • **
  • Posts: 62
Re: Garbled characters printed by fpImageCanvas.TextOut method
« Reply #2 on: July 31, 2025, 02:39:30 am »
Sorry I forgot to mention I have set {$codepage utf8} and also add lazutf8 unit in the project.Normally when I finished these two steps freepascal and lazarus have done a great job on asian character diplay (than many other languages do I once tried on this aspect  :P ),but not this time with fpImageCanvas.TextOut.
« Last Edit: July 31, 2025, 03:26:51 am by PeterHu »

PeterHu

  • Jr. Member
  • **
  • Posts: 62
Re: Garbled characters printed by fpImageCanvas.TextOut method
« Reply #3 on: August 02, 2025, 10:29:39 am »
Just found TBGRABitmap works great with this issue.Need to mention,from the wikihttps://wiki.freepascal.org/BGRABitmap:
Quote
The main class is TBGRABitmap, which is derived from TFPCustomImage.

Code: Pascal  [Select][+][-]
  1. program project1;
  2. uses
  3.   sysutils,bgraBitmap,bgraBitmapTypes,interfaces;
  4.  
  5. const
  6.   text_str='Hello, world!¡Hola Mundo!Γειά σου Κόσμε!Привет, мир!こんにちは世界!नमस्ते दुनिया!👋🌎!';
  7. procedure test;
  8. var
  9.   bmp: TBGRABitmap;
  10. begin
  11.   bmp := TBGRABitmap.Create(1024, 330, BGRAWhite);
  12.  
  13.   bmp.FontHeight := 16;
  14.  
  15.   bmp.textout(30,30,text_str,BgraBlack);
  16.   bmp.SaveToFile('DrawText.png');
  17.   bmp.Free;
  18. end;
  19.  
  20. begin
  21.  
  22.   test;
  23.   readln;
  24.  
  25. end.    
  26.  

 

TinyPortal © 2005-2018