Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8595311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:24:16+00:00 2026-06-12T00:24:16+00:00

I try to display formatted text on the screen. At first the very simple

  • 0

I try to display formatted text on the screen. At first the very simple HTML text is parsed (there are tags like b,u,i) and then each character is rendered using Canvas.TextOut function in appropriate position and font.

The first thing I noticed is, that rendering of every separate character on the canvas is rather slow. The rendering of whole sentence is much faster. It is obvious, when the canvas is forced to repaint, when form is moved around the screen.

One solution would be to cluster the characters with even fonts and render them at once. But it won’t help too much, when the formatting is rich. In addition I need the characters to be the discrete entities, which could be rendered in any way. For example, there is no WinAPI to support text alignment taJustify or in block writing…

Another approach is to render on bitmap, or to use wisely ClipRect property of TCanvas (I haven’t tried yet).

Anyway, when the same formatted text is displayed in TRichEdit, there is no time penalty by repaint operation. Another quick example are all major browsers, which has no problem to display tons of formated text… do they render each character like I do, but they do it more efficiently ??? I do not know.

So do you know some recipe to speeding up the application (formatted text rendering?).

Thanx for your ideas…

Sample code: (make TForm as big as possible, grab it with mouse and drag it down under screen. When you move it up, you will see “jumpy” movement)

procedure TForm1.FormPaint(Sender: TObject);
var i, w, h, j:integer;
    s:string;
    switch:Boolean;
begin
   w:=0;
   h:=0;
   s:='';
   for j:=0 to 5 do
       for i:=65 to 90 do s:=s + Char(i);

   switch:=False; // set true to see the difference

   if switch then
     begin
     for j:=0 to 70 do begin
         for i := 1 to Length(s) do
         begin
         Form1.Canvas.TextOut(50+ w,h +70 , s[i]);
         w:=w +  Form1.Canvas.TextWidth(s[i]);
         end;
         w:=0;
         h:=h+15;
         end;
     end
    else
      begin
      for j:=0 to 70 do begin
       Form1.Canvas.TextOut(50+ w,h +70 , s);
       w:=w +  Form1.Canvas.TextWidth(s);  // not optimalized just for comparison
       w:=0;                               // not optimalized just for comparison
       h:=h+15;
       end;
      end;
end;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:24:18+00:00Added an answer on June 12, 2026 at 12:24 am

    On my pc, it’s about twice as fast when you render to a bitmap, and then draw that to the canvas. Well, the slow version becomes twice as fast. The fast version stays the same.

    Another optimization that might work.
    You can also pre-calculate character widths into an array, so you don’t have to call canvas.TextWidth() often.

    Keep a variable like this

    widths:array[char] of byte;
    

    Fill it like this:

    for c := low(widths) to high(widths) do
      widths[c] := Canvas.TextWidth(char(c));
    

    Filling this 65536 element array is slow, so perhaps it’s better to just create a 65..90 element-array, and drop unicode-support.

    Another thing..
    Calling Winapi.Windows.TextOut() is faster than canvas.TextOut().

    You can actually win a lot with that.

        Winapi.Windows.TextOut(bmp.Canvas.Handle, w, h, @s[i], 1);
    

    Modified version of your code:

    // set up of off-screen bitmap.. needs to be resized when the form resizes. 
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      bmp := TBitmap.Create;
      bmp.SetSize(width,height);
    end;
    

    This is

    procedure TForm36.PaintIt2;
    var h,i,j,w: Integer; s: string;
    begin
      w := 0;  h := 0;  s := '';
    
      for j := 0 to 5 do
        for i := 65 to 90 do
          s := s + Char(i);
    
      bmp.Canvas.Brush.Color := Color;
      bmp.Canvas.FillRect(bmp.Canvas.ClipRect);
      if Checkbox1.Checked then
      begin
        for j := 0 to 70 do
        begin
          for i := 1 to Length(s) do
          begin
            Winapi.Windows.TextOut(bmp.Canvas.Handle, w, h, @s[i], 1);
            w := w + widths[s[i]];
          end;
          w := 0; h := h + 15;
        end;
      end
      else
        for j := 0 to 70 do
        begin
          bmp.Canvas.TextOut(w, h, s);
          w := 0; h := h + 15;
        end;
      canvas.Draw(0,0,bmp);
    end;
    

    I timed the performance with this procedure:

    procedure TForm1.Button2Click(Sender: TObject);
    var i : Integer; const iterations=300;
    begin
      with TStopwatch.StartNew do
      begin
        for I := 1 to iterations do
          PaintIt2;
        Caption := IntToStr(Elapsed.Ticks div iterations);
      end;
    end;
    

    Last note:

    I’ve tried disabling cleartype/anti-aliasing, but strangely enough that makes rendering twice as slow! This is how I turned anti-aliasing off:

      tagLOGFONT: TLogFont;
    
      GetObject(
        bmp.Canvas.Font.Handle,
        SizeOf(TLogFont),
        @tagLOGFONT);
      tagLOGFONT.lfQuality  := NONANTIALIASED_QUALITY;
      bmp.Canvas.Font.Handle := CreateFontIndirect(tagLOGFONT);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To display HTML formatted text in an AdvancedDataColumn, I'm using a custom renderer which
I would like to display an ajax loader icon when user try to submit
I'm reading an html file like this: try { BufferedReader bufferReader = new BufferedReader(new
I have a text file with hundreds of entries formatted like this, I need
I'm try display data from column points , SELECT * from table ORDER by
I am using the following code to try and display in a time in
I am a total beginner to Objective C. Currently I try to display a
I try to get one data column from a MS-Access table and display it
Is it possible to display images next to items in FlyoutAnchor submenu? I try
So I have a 1920x1080 display, and when I try to run two xcode

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.