[Solved]Printing unicode one character at a time lazarus / 21-General ---------------------------------------------------------------------- bobonwhidbey · 2024-12-08 19:55 I want to display a string variable one character at a time onto a Windows Canvas, using TextOut. One character at a time will enable me to use different font colors and font style on various characters. An approach like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i x := 10;y := 50;for i := 1 to length(str) do begin  if odd(i) then     Canvas.Font.Color := clRed  else     Canvas.Font.Color := clBlack;  Canvas.TextOut(x,y,str[i]);  x := x+Canvas.TextWidth(str[i]);end;  stumbles when if comes across a string that contains characters other than UTF8. For example, when str :='På fjelltur'; So my question is, how to parse a string 1 character at a time. ---------------------------------------------------------------------- cdbc · 2024-12-08 20:03 Hi Use 'Unicode'...Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  ls: string = 'På fjelltur';  uc: unicodechar;  us: unicodestring;begin  us:= UTF8Decode(ls);  for uc in us do ...{your thing with 'uc' here};end; I know it's a conversion, but the easiest, I think, in your situation ...if it works, that is  Regards Benny ---------------------------------------------------------------------- marcov · 2024-12-08 21:26 This will also fail with UTF8. It is code that can't deal with variable sized sequences. ---------------------------------------------------------------------- bytebites · 2024-12-08 21:51 Add Lazutf8 to uses. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for i := 1 to Utf8Length(str) do  begin   s := UTF8Copy (str,i,1);    Canvas.TextOut(x,y,s); ---------------------------------------------------------------------- cdbc · 2024-12-08 23:05 Hi I found a thread that might help, in reply #12 Juha gave this snippet:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses ... LazUTF8, LazUnicode;...procedure TForm1.Button1Click(Sender: TObject);var  ch, Str, Pattern: String;  At: Integer;begin  Str := '一二三四五六七八九十';  Pattern := '三六a一2ä';  for ch in Pattern do begin    At := Pos(ch, Str);    if At > 0 then      Memo1.Lines.Add(Format('%s found at byte position %d, character position %d.',                           [ch, At, UTF8Pos(ch,Str)]));  end;end;You need to work it a little bit to your needs... It's from this thread. HTH Regards Benny ---------------------------------------------------------------------- bobonwhidbey · 2024-12-08 23:19 This code does not work. BUT, if I remove the 4 indicated lines, it displays correctly - of course not with the desired font styles. I don't know why that is? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  s, str: string;  x, i: integer;  uc: unicodechar;begin  x := 30;  str := 'På fjelltur';  for i := 1 to Utf8Length(str) do  begin    s := UTF8Copy(str, i, 1);    if i < 3 then                      // Remove      Font.Style := [fsBold]         // Remove    else                                // Remove    Font.Style := [fsUnderline];      // Remove    Self.Canvas.TextOut(x, 30, s);    x := x + Self.Canvas.TextWidth(s);  end;end; ---------------------------------------------------------------------- wp · 2024-12-08 23:42 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  LazUnicode;  // important! procedure TForm1.PaintBox1Paint(Sender: TObject);var  i, x, y: Integer;  ch: String;begin  x := 10;  y := 50;  i := 0;  for ch in str do  begin    if odd(i) then       Canvas.Font.Color := clRed    else       Canvas.Font.Color := clBlack;    Canvas.TextOut(x,y, ch);    x := x+Canvas.TextWidth(ch);  inc(i);  end;end; ---------------------------------------------------------------------- bobonwhidbey · 2024-12-09 00:19 That coding works perfectly. Yes LazUnicode was crucial. I was curious why this coding displayed correctly when placed in a ButtonClick event, but did not display when placed in FormShow Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls,  LazUnicode; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);    procedure FormShow(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm}  { TForm1 } procedure TForm1.Button1Click(Sender: TObject);var  i, x, y: integer;  str, ch: string;begin  str := 'På fjelltur';  x := 10;  y := 100;  i := 0;  for ch in str do  begin    if odd(i) then      Canvas.Font.Color := clRed    else      Canvas.Font.Color := clBlack;    Canvas.TextOut(x, y, ch);    x := x + Canvas.TextWidth(ch);    Inc(i);  end; end; procedure TForm1.FormShow(Sender: TObject);var  i, x, y: integer;  str, ch: string;begin  str := 'På fjelltur';  x := 10;  y := 50;  i := 0;  for ch in str do  begin    if odd(i) then      Self.Canvas.Font.Color := clRed    else      Self.Canvas.Font.Color := clBlack;    Self.Canvas.TextOut(x, y, ch);    x := x + Self.Canvas.TextWidth(ch);    Inc(i);  end; end; end. ---------------------------------------------------------------------- cdbc · 2024-12-09 01:07 Hi You need to place your drawing code in the form's 'OnPaint' handler, otherwise your drawings keep getting erased by the system's own redrawing... If you then call 'Invalidate' of the form, it will repaint itself and your stuff along with it... Regards Benny ---------------------------------------------------------------------- bobonwhidbey · 2024-12-09 01:11 Thank you everyone. I learned a lot. ---------------------------------------------------------------------- cdbc · 2024-12-09 01:47 Hi Here's quick and dirty test unit:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit view.main; {$mode objfpc}{$H+} interface uses  Classes, StdCtrls,SysUtils, Forms, Controls, Graphics, Dialogs; type  TForm1 = class(TForm)    Button1:TButton;    procedure Button1Click(Sender:TObject);    procedure FormPaint(Sender:TObject);  private    fstr: string;  public    procedure AfterConstruction;override;  end; var  Form1: TForm1; implementationuses LazUnicode;{$R *.lfm} procedure TForm1.FormPaint(Sender:TObject);var  i, x, y: Integer;        uc: UnicodeChar;  ch: String;              us: UnicodeString;begin  Canvas.Brush.Style:= bsClear;  Canvas.Pen.Style:= psSolid;  Canvas.Font.Name:= 'DejaVu Sans Mono';  Canvas.Font.Size:= 14;  Canvas.Font.Quality:= fqAntialiased;  x := 10;  y := 50;  i := 0;  for ch in fstr do  begin    if odd(i) then       Canvas.Font.Color := clRed    else       Canvas.Font.Color := clGreen;    Canvas.TextOut(x,y, ch);    x := x+Canvas.TextWidth(ch)+4;    inc(i);  end;   us:= UTF8Decode(fstr);  x := 10;  y := 100;  i := 0;  for uc in us do begin    if odd(i) then       Canvas.Font.Color := clFuchsia    else       Canvas.Font.Color := clAqua;    Canvas.TextOut(x,y, uc);  x := x+Canvas.TextWidth(uc)+4;    inc(i);  end;end; procedure TForm1.AfterConstruction;begin  inherited AfterConstruction;  fstr:= 'æ ø å Æ Ø Å ñ';end; procedure TForm1.Button1Click(Sender:TObject);begin  fstr:= InputBox('Draw- string','What to draw:','På fjelltur');end; end. ...and screenshot attached... Regards Benny ---------------------------------------------------------------------- bobonwhidbey · 2024-12-09 04:23 Very nice.  Thanks Bennny