How to convert old data from TRichEdit to TRichView

General TRichView support forum. Please post your questions here
Post Reply
armagnac
Posts: 12
Joined: Tue Mar 05, 2024 8:35 am

How to convert old data from TRichEdit to TRichView

Post by armagnac »

Hi,

I have a huge database containing text (mix of Times New Roman and Symbol fonts). When I load one of these texts, I can see the style numbers. And when I load another text, I see same font names, but not with the same style number. In example : in the first text, I see that the style number 8 is Symbol in size 15, in the second the same style is now numbered 10.
My goal is to convert texts from this database in the same numbered styles, maybe 6 always Times New Roman size 14, 7 Symbol color navy, 8 Symbol color red, 9 Symbol color orange and 10 Symbol color green. As you can guess, text is composed with "normal text" and cards symbols (spade, heart, diamond and club).
I'm too newbie to get a good idea in coding that, and after many hours looking up for a way, reading hundred of this forum's items, I realize I need some help.

How I proceed :

I first load the text :
theBS := TABSBlobStream(TableDonnes.CreateBlobStream(FieldByName('COMMENT'), bmRead));
rve.Clear;
rve.LoadFromStream(theBS, rvynaAuto);
rve.Format;
theBS.Free;

Then I do :
rvs.ResetTextStyles;
rve.Format;
AjouteStyles;
rve.SelectAll;
rve.CurTextStyleNo := NormalStyle;
fd.Font.Assign(rvs.TextStyles[rve.CurTextStyleNo]);
rve.ApplyStyleConversion(TEXT_APPLYFONT);
rve.Deselect;

The procedure AjouteStyles :

var TextStyle: TFontInfo;
StyleNo: Integer;
begin
IgnoreChanges := True;

TextStyle := TFontInfo.Create(nil);
TextStyle.Assign(rve.Style.TextStyles[rve.CurTextStyleNo]);
TextStyle.Color := clBlack;
TextStyle.BackColor := clInfoBk;
TextStyle.Size := 14;
TextStyle.FontName := 'Times New Roman';
TextStyle.StyleName := 'Normal Text';
rve.Style.TextStyles.Add;
StyleNo := rve.Style.TextStyles.Count - 1;
// CouleursStyles[pique] := StyleNo;
rve.Style.TextStyles[StyleNo].Assign(TextStyle);
rve.Style.TextStyles[StyleNo].Standard := False;
TextStyle.Free;

TextStyle := TFontInfo.Create(nil);
TextStyle.Assign(rve.Style.TextStyles[rve.CurTextStyleNo]);
TextStyle.Color := clNavy;
TextStyle.BackColor := clInfoBk;
TextStyle.Size := 15;
TextStyle.FontName := 'Symbol';
TextStyle.StyleName := 'Pique';
rve.Style.TextStyles.Add;
StyleNo := rve.Style.TextStyles.Count - 1;
CouleursStyles[pique] := StyleNo;
rve.Style.TextStyles[StyleNo].Assign(TextStyle);
rve.Style.TextStyles[StyleNo].Standard := False;
TextStyle.Free;

TextStyle := TFontInfo.Create(nil);
TextStyle.Assign(rve.Style.TextStyles[rve.CurTextStyleNo]);
TextStyle.Color := clRed;
TextStyle.BackColor := clInfoBk;
TextStyle.Size := 15;
TextStyle.FontName := 'Symbol';
TextStyle.StyleName := 'Coeur';
rve.Style.TextStyles.Add;
StyleNo := rve.Style.TextStyles.Count - 1;
CouleursStyles[coeur] := StyleNo;
rve.Style.TextStyles[StyleNo].Assign(TextStyle);
rve.Style.TextStyles[StyleNo].Standard := False;
TextStyle.Free;

TextStyle := TFontInfo.Create(nil);
TextStyle.Assign(rve.Style.TextStyles[rve.CurTextStyleNo]);
TextStyle.Color := $0099FF;
TextStyle.BackColor := clInfoBk;
TextStyle.Size := 15;
TextStyle.FontName := 'Symbol';
TextStyle.StyleName := 'Carreau';
rve.Style.TextStyles.Add;
StyleNo := rve.Style.TextStyles.Count - 1;
CouleursStyles[carreau] := StyleNo;
rve.Style.TextStyles[StyleNo].Assign(TextStyle);
rve.Style.TextStyles[StyleNo].Standard := False;
TextStyle.Free;

TextStyle := TFontInfo.Create(nil);
TextStyle.Assign(rve.Style.TextStyles[rve.CurTextStyleNo]);
TextStyle.Color := clGreen;
TextStyle.BackColor := clInfoBk;
TextStyle.Size := 15;
TextStyle.FontName := 'Symbol';
TextStyle.StyleName := 'Trefle';
rve.Style.TextStyles.Add;
StyleNo := rve.Style.TextStyles.Count - 1;
CouleursStyles[trefle] := StyleNo;
rve.Style.TextStyles[StyleNo].Assign(TextStyle);
rve.Style.TextStyles[StyleNo].Standard := False;
TextStyle.Free;

IgnoreChanges := False;
end;

Image

As you can see, I get a text on good font and size (Times New Roman 14) but on bad BackColor (red and I want clInfoBk) because the style number is 11 and not 6 as I wanted it to be.
The styles from 6 to 10 are right, but what are these styles 11 and 12 ?


Thank you
Attachments
saisie.png
saisie.png (32.32 KiB) Viewed 20730 times
Last edited by armagnac on Wed Mar 06, 2024 2:39 pm, edited 1 time in total.
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: How to convert old data from TRichEdit to TRichView

Post by Sergey Tkachenko »

What is the format of documents in the database?
If it was created using TRichEdit, it must be RTF. However, RTF does not contain numbered styles. Well, in documents created by MS Word or TRichView named styles have internal numbering in RTF file. However, TRichEdit does not support named styles.
armagnac
Posts: 12
Joined: Tue Mar 05, 2024 8:35 am

Re: How to convert old data from TRichEdit to TRichView

Post by armagnac »

The Database is ABSDatabase, and Table are TABSDatabase (Component Aces). The content of the text is in a TABSBlobField (derived from Blob Field).

I see the styles by clicking a button to execute this procedure :

st := '';
for i := 6 to rvs.TextStyles.Count // 6 because 0 to 5 are defined styles
do begin
fi := rvs.TextStyles;
st := st + IntToStr(i) + ' > ' + fi.FontName + ' sz:' + IntToStr(fi.Size) +
' name: ' + fi.StyleName + #13;
end;
ShowMessage(st);

Thank you
armagnac
Posts: 12
Joined: Tue Mar 05, 2024 8:35 am

Re: How to convert old data from TRichEdit to TRichView

Post by armagnac »

If I don't add new styles (= new fonts), the text is in Arial 10 with clRed BackColor.
How to transform text in the RichViewEdit into Times New Roman 14, and more difficult to put caracters 167 to 170 (card suit symbols) in Symbol 15 with the right color (spade = navy, heart = red, etc.).

text example : 2¨ = Quatrième couleur forcing, 11 H et plus. Pour en savoir plus...
The ¨caracter is supposed to be a diamond in Symbol character set (#168).

Thank you
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: How to convert old data from TRichEdit to TRichView

Post by Sergey Tkachenko »

To understand the problem better, I need a sample of document from this DB.
You can save it to a file. After creating theBS, call
theBS.SaveToFile(FileName);
and send this file to me to email richviewgmailcom.


Symbol font is not necessary to add special symbols such as card suits. You can use Unicode symbols instead.
For example, to insert black spade suit, use RichView.InsertTextW(#$2660) or AddNLW(#$2660, ...) (in Delphi 2009 and newer, you can use InsertText and AddNL too).
If Unicode character does not exist in the specified font, a character from another font will be displayed (font substitution).
You can find a list of playing cards Unicode characters here: https://en.wikipedia.org/wiki/Playing_cards_in_Unicode
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: How to convert old data from TRichEdit to TRichView

Post by Sergey Tkachenko »

About styles and loading

I received your file.
This is a document in RVF format. And it contains all styles inside it.
With the default property settings, all styles that you added before loading will be removed, and styles from RVF are used.
There are some things that you can try.
For example, you can assign RVFTextStylesReadMode = rvf_sIgnore. In this mode, styles from RVF will be ignored, but styles that you added before loading will be used. But documents will look ok only if the have compatible styles, i.e. indexes of styles in document must point to the proper styles that were created before loading.
These in more option. Styles in document can be mapped to the most similar existing styles. Assign RVFTextStylesReadMode = rvf_sInsertMap and load documents using InsertRVFromStream.
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: How to convert old data from TRichEdit to TRichView

Post by Sergey Tkachenko »

About characters from "Symbol" font

Fonts having Charset = SYMBOL_CHARSET (including "Symbol" font) are special fonts.

In all other fonts, each character code has special meaning, described in Unicode standard. Because of this, when you apply a non-symbol font to text of another non-symbol font, you can see the same characters. And if you save document that was created using non-symbol font in a text file (in Unicode encoding, such as UTF-8 or UTF-16), you will see all correct characters after opening this file in Notepad.

But meaning of characters in symbol fonts are not defined. They are different from font to font.
For example, character with code $A9 (I'll use hexadecimal numbers instead of decimal) is hearts (card suit) in "Symbol". But in another symbol font, "Wingdings", this character is a three-pointed star. But in all non-symbol font this is a copyright symbol.
So, if you save document containing this charater as a plain text, you will see a copyright character when opening in Notepad (but TRichView solves this problem, see below).
So, this is the problem number one: if you use characters of "Symbol" font, you must be sure that this font is applied.

(to be continued later)
armagnac
Posts: 12
Joined: Tue Mar 05, 2024 8:35 am

Re: How to convert old data from TRichEdit to TRichView

Post by armagnac »

I cannot use rve.LoadRVFFromStream(theBS) with the existing database because rve is empty after that : no text.
So I use rve.LoadFromStream(theBS, rvynaAuto), and I get text with cards symbol, all correct but not in "my" styles. It seems rve makes styles that it recognizes in the flow, so if a card symbol is missing in the text, the style is not created.
My styles are numbered from 6 to 10 (6 is Times New Roman, size 14, 7 is Symbol color clNavy size 15, and so on), but when the data are read, they have different (new) styles, as I showed in my first message. These new style are numbered 11 to x, and I see that some are regular text (Arial - that I don't need - or Times New Roman) and others are Symbol 15 with appropriate colors.
rve.RVFTextStylesReadMode := rvf_sIgnore doesn't work (I guess it only works with .LoadRVFFromStream).

Is a way to search in the text characters #167 to #170, and to set them in the correct style (7 for #170, 8 for #169,...) and to give all other letters the style 6 (Times New Roman, 14, black) or in inverse way, first all text in style number 6, then search special characters, maybe easier ?
And then save the content in RVF format.
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: How to convert old data from TRichEdit to TRichView

Post by Sergey Tkachenko »

But your document has RVF format. This means that LoadFromStream, after detecting RVF, calls LoadRVFromStream to load it.
As I said, this RVF document contains styles, and your styles will be ignored and replaced by styles from the file.
Please wait, I'll try to make a demo.

Back to characters, I still want to give more information why using "Symbol" font leads to many problems.
The first problem I described: you cannot save symbol characters to text (and HTML too) as they are, they will be displayed incorrectly.

The second problem: when you load this file, you can see card suit characters, but I cannot see them.
It happens because you have Western locale as default in your Windows, but I have Cyrillic locale, and I need to use different character code to display symbol fonts. The same problem happens with any non-Western locale.
A workaround using not small codes like $A9, but add $F000 to them, for example use $F0A9 code to display hearts suit instead of $A9.

But as I said, there is better solution: to get rid of "Symbol" font at all. Use any non-symbol font and Unicode characters instead:
Character code - character
$2660 - ♠
$2665 - ♥
$2666 - ♦
$2663 - ♣
With these characters, you must have no problems. You can use any font to display them, and save to text and HTML without problems
(but TRichView already implements a workaround for saving characters of "Symbol" font to text files and HTML: it automatically saves them as the most similar Unicode characters)
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: How to convert old data from TRichEdit to TRichView

Post by Sergey Tkachenko »

I attached the demo.
It can:
1. Load your file with default settings (using styles from file)
2. Load your file using styles created in the application (ignoring styles from file)
3. Convert card suit characters to Unicode.

PS: I have no problems loading your file using LoadRVF. But if I change LoadRVF to LoadFromFile, results are exactly the same.
Attachments
DocsWithCardSuits.zip
(92.65 KiB) Downloaded 1021 times
Sergey Tkachenko
Site Admin
Posts: 17559
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: How to convert old data from TRichEdit to TRichView

Post by Sergey Tkachenko »

The file that you sent to me previously as a sample field content was in RVF format (RichView Format). In this format, collections of text and paragraph styles are saved, so you had 3 options:
- using styles from the document
- ignore styles from document and use styles from the application
- map styles from the document to the most similar styles from the application.

However, later you sent me a sample database, and this database contains documents in another format, RTF (Rich Text Format).
Obviously, you cannot load them using LoadRVFFromStream, you need LoadRTFFromStream.
But an RTF document does not have collection of styles (I mean styles representing all used text formatting, an analog of RVStyle.TextStyles). It just has commands like "apply the font name AAA", "make text bold", and so on.
When loading in TRichView (and when rve.RTFReadProperties.TextStylesMode = rvrsAddIfNeeded), TRichView creates a collection of text styles representing formatting from the document. There is no specific order of styles added when reading RTF: each time when a new text formatting (that was not used before) is found, a new style is added to represent it.

You cannot use predefined TextStyles when loading RTF (if rve.RTFReadProperties.TextStylesMode = rvrsAddIfNeeded).
However, you still have option to map RTF formatting to the most similar existing text styles: assign ve.RTFReadProperties.TextStylesMode =
rvrsUseClosest.
Post Reply