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

  • Home
  • SEARCH
  • 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 396685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:34:35+00:00 2026-05-12T16:34:35+00:00

what would be best way to handle simple text styles like bbcode allowing bold

  • 0

what would be best way to handle simple text styles like bbcode allowing bold italic etc inside of the text?
what I did is dividing the text into parts, each part has assigned style and then I textout each piece, starting from Rect.Left + Canvas.TextWidth(Texts[i-1]). This however is probably quite slow, moreover I have no idea how to text it out in case of VirtualStringTree. It has OnBeforeItemPaint but the callback is not aware of column index. The OnBeforeCellPaint however doesn’t expose the variable to say VST I did painted on my own, therefore it paints on my text…

Anyone, please? 🙂

regards,
Michal

  • 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-05-12T16:34:36+00:00Added an answer on May 12, 2026 at 4:34 pm

    I’ve done something similar using a simple subset of HTML tags. Here’s the code to draw the text:

    function TMyVST.DrawHTML(const ARect: TRect; const ACanvas: TCanvas; const Text: String): Integer;
    (*DrawHTML - Draws text on a canvas using tags based on a simple subset of HTML/CSS
    
      <B> - Bold e.g. <B>This is bold</B>
      <I> - Italic e.g. <I>This is italic</I>
      <U> - Underline e.g. <U>This is underlined</U>
      <font-color=x> Font colour e.g.
                    <font-color=clRed>Delphi red</font-color>
                    <font-color=#FFFFFF>Web white</font-color>
                    <font-color=$000000>Hex black</font-color>
      <font-size=x> Font size e.g. <font-size=30>This is some big text</font-size>
      <font-family> Font family e.g. <font-family=Arial>This is arial</font-family>*)
    
      function CloseTag(const ATag: String): String;
      begin
        Result := concat('/', ATag);
      end;
    
      function GetTagValue(const ATag: String): String;
      var
        p: Integer;
      begin
        p := pos('=', ATag);
    
        if p = 0 then
          Result := ''
        else
          Result := copy(ATag, p + 1, MaxInt);
      end;
    
      function ColorCodeToColor(const Value: String): TColor;
      var
        HexValue: String;
      begin
        Result := 0;
    
        if Value <> '' then
        begin
          if (length(Value) >= 2) and (copy(Uppercase(Value), 1, 2) = 'CL') then
          begin
            // Delphi colour
            Result := StringToColor(Value);
          end else
          if Value[1] = '#' then
          begin
            // Web colour
            HexValue := copy(Value, 2, 6);
    
            Result := RGB(StrToInt('$'+Copy(HexValue, 1, 2)),
                          StrToInt('$'+Copy(HexValue, 3, 2)),
                          StrToInt('$'+Copy(HexValue, 5, 2)));
          end
          else
            // Hex or decimal colour
            Result := StrToIntDef(Value, 0);
        end;
      end;
    
    const
      TagBold = 'B';
      TagItalic = 'I';
      TagUnderline = 'U';
      TagBreak = 'BR';
      TagFontSize = 'FONT-SIZE';
      TagFontFamily = 'FONT-FAMILY';
      TagFontColour = 'FONT-COLOR';
      TagColour = 'COLOUR';
    
    var
      x, y, idx, CharWidth, MaxCharHeight: Integer;
      CurrChar: Char;
      Tag, TagValue: String;
      PreviousFontColour: TColor;
      PreviousFontFamily: String;
      PreviousFontSize: Integer;
      PreviousColour: TColor;
    
    begin
      ACanvas.Font.Size := Canvas.Font.Size;
      ACanvas.Font.Name := Canvas.Font.Name;
      ACanvas.Font.Color := Canvas.Font.Color;
      ACanvas.Font.Style := Canvas.Font.Style;
    
      PreviousFontColour := ACanvas.Font.Color;
      PreviousFontFamily := ACanvas.Font.Name;
      PreviousFontSize := ACanvas.Font.Size;
      PreviousColour := ACanvas.Brush.Color;
    
      x := ARect.Left;
      y := ARect.Top + 1;
      idx := 1;
    
      MaxCharHeight := ACanvas.TextHeight('Ag');
    
      While idx <= length(Text) do
      begin
        CurrChar := Text[idx];
    
        // Is this a tag?
        if CurrChar = '<' then
        begin
          Tag := '';
    
          inc(idx);
    
          // Find the end of then tag
          while (Text[idx] <> '>') and (idx <= length(Text)) do
          begin
            Tag := concat(Tag,  UpperCase(Text[idx]));
    
            inc(idx);
          end;
    
          ///////////////////////////////////////////////////
          // Simple tags
          ///////////////////////////////////////////////////
          if Tag = TagBold then
            ACanvas.Font.Style := ACanvas.Font.Style + [fsBold] else
    
          if Tag = TagItalic then
            ACanvas.Font.Style := ACanvas.Font.Style + [fsItalic] else
    
          if Tag = TagUnderline then
            ACanvas.Font.Style := ACanvas.Font.Style + [fsUnderline] else
    
          if Tag = TagBreak then
          begin
            x := ARect.Left;
    
            inc(y, MaxCharHeight);
          end else
    
          ///////////////////////////////////////////////////
          // Closing tags
          ///////////////////////////////////////////////////
          if Tag = CloseTag(TagBold) then
            ACanvas.Font.Style := ACanvas.Font.Style - [fsBold] else
    
          if Tag = CloseTag(TagItalic) then
            ACanvas.Font.Style := ACanvas.Font.Style - [fsItalic] else
    
          if Tag = CloseTag(TagUnderline) then
            ACanvas.Font.Style := ACanvas.Font.Style - [fsUnderline] else
    
          if Tag = CloseTag(TagFontSize) then
            ACanvas.Font.Size := PreviousFontSize else
    
          if Tag = CloseTag(TagFontFamily) then
            ACanvas.Font.Name := PreviousFontFamily else
    
          if Tag = CloseTag(TagFontColour) then
            ACanvas.Font.Color := PreviousFontColour else
    
          if Tag = CloseTag(TagColour) then
            ACanvas.Brush.Color := PreviousColour else
    
          ///////////////////////////////////////////////////
          // Tags with values
          ///////////////////////////////////////////////////
          begin
            // Get the tag value (everything after '=')
            TagValue := GetTagValue(Tag);
    
            if TagValue <> '' then
            begin
              // Remove the value from the tag
              Tag := copy(Tag, 1, pos('=', Tag) - 1);
    
              if Tag = TagFontSize then
              begin
                PreviousFontSize := ACanvas.Font.Size;
                ACanvas.Font.Size := StrToIntDef(TagValue, ACanvas.Font.Size);
              end else
    
              if Tag = TagFontFamily then
              begin
                PreviousFontFamily := ACanvas.Font.Name;
                ACanvas.Font.Name := TagValue;
              end;
    
              if Tag = TagFontColour then
              begin
                PreviousFontColour := ACanvas.Font.Color;
    
                try
                  ACanvas.Font.Color := ColorCodeToColor(TagValue);
                except
                  //Just in case the canvas colour is invalid
                end;
              end else
    
              if Tag = TagColour then
              begin
                PreviousColour := ACanvas.Brush.Color;
    
                try
                  ACanvas.Brush.Color := ColorCodeToColor(TagValue);
                except
                  //Just in case the canvas colour is invalid
                end;
              end;
            end;
          end;
        end
        else
        // Draw the character if it's not a ctrl char
        if CurrChar >= #32 then
        begin
          CharWidth := ACanvas.TextWidth(CurrChar);
    
          if x + CharWidth > ARect.Right then
          begin
            x := ARect.Left;
    
            inc(y, MaxCharHeight);
          end;
    
          if y + MaxCharHeight < ARect.Bottom then
          begin
            ACanvas.Brush.Style := bsClear;
    
            ACanvas.TextOut(x, y, CurrChar);
          end;
    
          x := x + CharWidth;
        end;
    
        inc(idx);
      end;
    
      Result := x;
    end;
    

    … and the DoAfterCellPaint call

    procedure TMyVST.DoAfterCellPaint(Canvas: TCanvas;
      Node: PVirtualNode; Column: TColumnIndex; CellRect: TRect);
    begin
      inherited;
    
      DrawHTML(CellRect, Canvas, 'HTML <B>tagged</B> string');
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 282k
  • Answers 282k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You send it a WM_NULL with SendMessageTimeout(). If that times… May 13, 2026 at 4:08 pm
  • Editorial Team
    Editorial Team added an answer after several days confusion and working on different solutions, and… May 13, 2026 at 4:08 pm
  • Editorial Team
    Editorial Team added an answer Your action is setting the User IPrincipal for the current… May 13, 2026 at 4:08 pm

Related Questions

I have a live search on my help page that searches our help database
C# question here.. I have a UTF-8 string that is being interpreted by a
Background There are a lot of App Store released iPhone apps that require an
We are creating a simple but improved billing solution. Since we have to use

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.