Recent

Author Topic: A bug in TRichMemo!  (Read 2803 times)

Ed78z

  • Jr. Member
  • **
  • Posts: 82
A bug in TRichMemo!
« on: July 31, 2025, 02:48:50 am »
Hi, I am using a TRichMemo component, and I wrote this code to make the selected text Bold. (no changes in the other font's parameters)
Code: Pascal  [Select][+][-]
  1. procedure SelTextBoldOnly(RM: TRichMemo; Bold: Boolean);
  2. var
  3.   i, SelStart, SelLength: Integer;
  4.   FP: TFontParams;
  5. begin
  6.   SelStart := RM.SelStart;
  7.   SelLength := RM.SelLength;
  8.   i := SelStart;
  9.   while i < SelStart + SelLength do
  10.   begin
  11.     if not RM.GetTextAttributes(i, FP)then
  12.        Begin Inc(i);Continue;end;
  13.  
  14.     if Bold then
  15.       Include(FP.Style, fsBold)
  16.     else
  17.       Exclude(FP.Style, fsBold);
  18.  
  19.     RM.SetTextAttributes(i, 1, FP);
  20.     Inc(i);
  21.   end;
  22. end;
  23.  

It works very well, unless there is a 4-byte character withing the selected text.
A 4-byte character like "𝒿" (Unicode: 1D4BF)

The TRichMemo acts strangely and randomly if you couple of times. sometimes works, sometime with some edit, doesn't work ( I mean not changing to Bold).



I tried the "Rtf" property of the RichMemo1 in the Object Inspector, it acts same. can't make the selected text Bold or Italic.
see screenshot.

Any idea?

Windows 11 64bit
Lazarus 4.2
« Last Edit: July 31, 2025, 04:28:47 am by Ed78z »

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 724
Re: A bug in TRichMemo!
« Reply #1 on: July 31, 2025, 06:29:06 am »
Suggest you use the "Search" function here on TRichMemo.  It's been quite a while, but as I recall there are a fairly large number of reported problems with it (although I was working on linux not windows).

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: A bug in TRichMemo!
« Reply #2 on: July 31, 2025, 12:18:55 pm »
The TRichMemo acts strangely and randomly if you couple of times. sometimes works, sometime with some edit, doesn't work ( I mean not changing to Bold).
Works fine for me.
But "sometimes it works and sometimes it doesn't" isn't really a helpful problem description.

BTW. If you are on Windows-only you can also use direct API call to the RTF-dll and change only the bold attribute of your entire selection (keeping the other styles). That way you don't need to iterate through all the characters.

Code: Pascal  [Select][+][-]
  1. uses richedit, Windows;
  2.  
  3. procedure AddBoldToSelection(RM: TRichMemo; Bold: Boolean);
  4. var
  5.   cf: CHARFORMAT2;
  6. begin
  7.   FillChar(cf, SizeOf(cf), #0);
  8.   cf.cbSize := SizeOf(cf);
  9.   cf.dwMask := CFM_BOLD;        // We're only changing the bold attribute
  10.   cf.dwEffects := CFE_BOLD;     // Enable bold
  11.   if not Bold then cf.dwEffects := 0; // Clear bold
  12.   SendMessage(RM.Handle, EM_SETCHARFORMAT, SCF_SELECTION, LPARAM(@cf));
  13. end;
« Last Edit: July 31, 2025, 12:39:59 pm by rvk »

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: A bug in TRichMemo!
« Reply #3 on: July 31, 2025, 01:28:10 pm »
@Rvk
So icode is a widow? Seems to me your code also reveals the same as the question?
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: A bug in TRichMemo!
« Reply #4 on: July 31, 2025, 01:53:58 pm »
So icode is a widow? Seems to me your code also reveals the same as the question?
No, in the original question everything after that Unicode character was not turned to bold. In my code (and the original code I tested) the whole selection was turned to bold. So I couldn't reproduce the problem. Can you?

Ed78z

  • Jr. Member
  • **
  • Posts: 82
Re: A bug in TRichMemo!
« Reply #5 on: July 31, 2025, 02:43:25 pm »
So icode is a widow? Seems to me your code also reveals the same as the question?
No, in the original question everything after that Unicode character was not turned to bold. In my code (and the original code I tested) the whole selection was turned to bold. So I couldn't reproduce the problem. Can you?

Thank you for your response and help.

If you look at the original screenshot, you can see the first line changed to bold but the second line did not change fully to bold, even the 4-byte character "𝒿" did not change to bold at all (The font itself has bold feature, and some time the 𝒿 also turns bold: 𝒿)
To reproduce this bug in the RichMemo, just copy and past the 𝒿 between the new characters that you are typing randomly in the RichMemo and try to bold and un-bold the part or all of the line. finally, you will see that any characters after "𝒿" will not change to bold. (visually, but in fact all characters have fsBold style internally, they are just not shown as bold)


rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: A bug in TRichMemo!
« Reply #6 on: July 31, 2025, 02:52:05 pm »
If you look at the original screenshot, you can see the first line changed to bold but the second line did not change fully to bold, even the 4-byte character "𝒿" did not change to bold at all (The font itself has bold feature, and some time the 𝒿 also turns bold: 𝒿)
To reproduce this bug in the RichMemo, just copy and past the 𝒿 between the new characters that you are typing randomly in the RichMemo and try to bold and un-bold the part or all of the line. finally, you will see that any characters after "𝒿" will not change to bold. (visually, but in fact all characters have fsBold style internally, they are just not shown as bold)
I still can't reproduce this.
Can you put your example text in a rtf and attach it (maybe in a zip if rtf isn't allowed).

For me the 𝒿 doesn't switch to bold (but that could be due to the chosen font). The rest of the line (and following line) does switch to bold (and back).

Did you try my procedure yet?
Does that work?

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: A bug in TRichMemo!
« Reply #7 on: July 31, 2025, 03:35:03 pm »
I can also not reproduce it.
Laz trunk , fpc trunk, windows 11 64bit
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

Ed78z

  • Jr. Member
  • **
  • Posts: 82
Re: A bug in TRichMemo!
« Reply #8 on: July 31, 2025, 06:57:05 pm »
If you look at the original screenshot, you can see the first line changed to bold but the second line did not change fully to bold, even the 4-byte character "𝒿" did not change to bold at all (The font itself has bold feature, and some time the 𝒿 also turns bold: 𝒿)
To reproduce this bug in the RichMemo, just copy and past the 𝒿 between the new characters that you are typing randomly in the RichMemo and try to bold and un-bold the part or all of the line. finally, you will see that any characters after "𝒿" will not change to bold. (visually, but in fact all characters have fsBold style internally, they are just not shown as bold)
I still can't reproduce this.
Can you put your example text in a rtf and attach it (maybe in a zip if rtf isn't allowed).

For me the 𝒿 doesn't switch to bold (but that could be due to the chosen font). The rest of the line (and following line) does switch to bold (and back).

Did you try my procedure yet?
Does that work?

Yes, I tried your version, same issue, no bold for the 𝒿 (like 𝒿), and no bold for the rest of the characters. this is happening just for Bold and Italic styles, other font parameters works fine.

Please see the screenshot and the source code.

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: A bug in TRichMemo!
« Reply #9 on: July 31, 2025, 07:06:38 pm »
Please see the screenshot and the source code.
Can you also provide the RTF.
Because that's not the standard font.

Did you try it with the font which is default when you drop a trichmemo on the form?
Does it work then?

(That's why I asked for the RTF)

Ed78z

  • Jr. Member
  • **
  • Posts: 82
Re: A bug in TRichMemo!
« Reply #10 on: July 31, 2025, 07:14:59 pm »
Please see the screenshot and the source code.
Can you also provide the RTF.
Because that's not the standard font.

Did you try it with the font which is default when you drop a trichmemo on the form?
Does it work then?

(That's why I asked for the RTF)


I can provide, however you can see the "Rtf" property of the RichMemo1 in the Object Inspector, and play with font parameters there.

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: A bug in TRichMemo!
« Reply #11 on: July 31, 2025, 07:40:15 pm »
It's because the RTF is really weird (maybe not valid).
If you put the RTF in a rtf file and open it in Wordpad you see it behaves the same.

That's why I also said you should try typing new text and paste only that character in.
Then it works fine.

Quote
{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil Segoe UI;}{\f1\fnil\fcharset0 Cambria Math;}}
{\colortbl ;\red0\green0\blue0;}
{\*\generator Riched20 10.0.22621}{\*\mmathPr\mmathFont1\mwrapIndent1440 }\viewkind4\uc1
\pard\f0\fs18 RichMemo1\par
\cf1\b\f1\fs24 asdvhas-\u-10187?\u-9025?sghdhasj\par
\cf0\b0\f0\fs18\par
\par
}

You see that the complete text is Cambria Math, including your text. Maybe there it goes wrong.

When making a separate RTF where only that character is Cambria Math, it works.

BTW. You also see when selecting the text, it behaves weird after reaching that character (the rest of the text switches from bold to normal, when you only select the text, which is not correct).

So this is a combination of mixing fonts and riched20.dll from Windows not knowing how to deal with it.

Normal:
Quote
{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Segoe UI;}{\f1\fnil Cambria Math;}{\f2\fnil\fcharset0 Calibri;}}
{\colortbl ;\red0\green0\blue0;}
{\*\generator Riched20 10.0.19041}{\*\mmathPr\mmathFont1\mwrapIndent1440 }\viewkind4\uc1
\pard\sa200\sl276\slmult1\cf1\f0\fs40 A 4-byte character like "\f1\u-10187?\u-9025?\f0 " (Unicode: 1D4BF)\cf0\f2\fs22\lang9\par
}

And when doing this: (note the \f0 to select normal text is changed in \f1 so everything is in Cambria Math): Here is where the problems begin.
Quote
{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Segoe UI;}{\f1\fnil Cambria Math;}{\f2\fnil\fcharset0 Calibri;}}
{\colortbl ;\red0\green0\blue0;}
{\*\generator Riched20 10.0.19041}{\*\mmathPr\mmathFont1\mwrapIndent1440 }\viewkind4\uc1
\pard\sa200\sl276\slmult1\cf1\f1\fs40 A 4-byte character like "\u-10187?\u-9025? " (Unicode: 1D4BF)\cf0\f2\fs22\lang9\par
}

BTW. When pasting in this character in normal text... only that character has that Cambria Math font. The other character stay the normal font.

Ed78z

  • Jr. Member
  • **
  • Posts: 82
Re: A bug in TRichMemo!
« Reply #12 on: July 31, 2025, 10:02:55 pm »
Thank you for your explanation,
So, basically are you confirming this a bug in the RichMemo?

A RichMemo suppose to handle any fonts and styles of each individual character. Why not to be able to add fsBold to each character? when there are different font and styles within the text in the RichMemo?

Do you have any solution?

rvk

  • Hero Member
  • *****
  • Posts: 6944
Re: A bug in TRichMemo!
« Reply #13 on: July 31, 2025, 10:08:24 pm »
Thank you for your explanation,
So, basically are you confirming this a bug in the RichMemo?
No, it's a bug in the underlying Windows component (i.e. riched20.dll).
WordPad suffers the same problem so it had nothing to do with the TRichMemo component itself. That just uses the Windows dll (which contains the problem).

The solution is not using normal characters in the funny Cambria Math.
I'm also not sure how you typed those normal characters in that font.

Ed78z

  • Jr. Member
  • **
  • Posts: 82
Re: A bug in TRichMemo!
« Reply #14 on: August 01, 2025, 12:57:06 am »
Thank you rvk for your help.
After digging more, I found out that's not just because of the font (here: Cambria Math), but because of limitation in the Windows RichText core limitations.
It just supports the unicode plane-0 correctly! (Plane 0=BMP=Basic Multilingual Plane = 0000–​FFFF)!
So, the characters that are in SMP(10000–​1FFFF), SIP(20000–​2FFFF), TIP(30000–​3FFFF) and SSP(E0000–​EFFFF) may not work correctly in the Windows RichText component and should be avoided!!

It seems we need a "Complex Text" component in Lazarus to support beyond the BMP! :)
« Last Edit: August 02, 2025, 06:03:06 am by Ed78z »

 

TinyPortal © 2005-2018