Link to home
Start Free TrialLog in
Avatar of Mariusz Jędrzejowski
Mariusz Jędrzejowski

asked on

various fonts in TRichEdit

I need to use various fonts for various parts of text in TRichEdit, Delphi Tokyo. For part of the text I need 'Times New Roman' and for the other 'Symbol'. So far does not work what I tried. Either font is set the samo for the whole text or my selected character does not show up.
Avatar of Manuel Lopez-Michelone
Manuel Lopez-Michelone
Flag of Mexico image

I wrote a chess editor, to make easy to write chess diagrams. There are a lot of public domain chess fonts to use. I dont have Tokio´s version of Delphi, but I think the way the richedit component is still the old richedit since Delphi 7 at least. In my editor I simple add a button to change the font:

procedure TForm2.Fuente1Click(Sender: TObject);
begin
  if FontDialog1.Execute then
    with RichEdit1, FontDialog1 do
    begin
      SelAttributes.Name := Font.Name;
      SelAttributes.Size := Font.Size;
      SelAttributes.Color := Font.Color;
      SelAttributes.Pitch := Font.Pitch;
      SelAttributes.Style := Font.Style;
      SelAttributes.Height := Font.Height;
    end;
end;

Remember to add a fontDialog component as well. if still have problems, I can send all my code so you can figure out if is Tokio´s version or some simple error in your approach.
regards
Lopem
Avatar of Mariusz Jędrzejowski
Mariusz Jędrzejowski

ASKER

So far almost everything seems to work, but when I set the "pitch" to fpfixed the characters appear as having variable width for each font I tried.
I am not a truetype expert but I guess you can't set pitch to fpfixed to any font you like. In computer fonts you have proportional and non-proportional fonts. The non-proportional fonts are the ones with pitch fixed. The proportional fonts use different spaces points between letters (glyphs)

For instance, Courier New is a non-proportional font, but Times Roman its a proportional one. Is a property of a font and I dont think you can change them. Please, check https://en.wikipedia.org/wiki/Monospaced_font

regards,
Lopem
I Tried "Courier New"and also other monospaced fonts, in vain.
Bez-tytu-u1.png
Bez-tytu-u.png
how do you add it ?
User generated image
procedure TForm1.AddText(msg: string; aFontName: string = ''; aFontSize: integer = 0; aFontColor: TColor = clBlack);
var re: TRichEdit;
begin
  re := RichEdit1;
  re.SelStart := Length(re.Text);
  re.SelLength := 0;
  if aFontName <> '' then
    re.SelAttributes.Name := aFontName;
  if aFontSize <> 0 then
    re.SelAttributes.Size := aFontSize;
  if aFontColor <> clBlack then
    re.SelAttributes.Color := aFontColor;
  re.SelText := msg;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  AddText('This is a piece of text in Courier new'#$D#$A, 'Courier New', 10, clRed);
  AddText('This is a piece of text in Times New Roman'#$D#$A, 'Times New Roman', 8, clGreen);
  AddText('This is a piece of text in Harlow Solid Italic'#$D#$A, 'Harlow Solid Italic', 14, clBlue);
end;

Open in new window

I used the same code as yours, only changed characters. Characters (arrows) at the bottom line are more or less arranged correctly, but they are not centered. This happens with many narrow characters, aligned to the left. I need them to be centered in their rectangle to be in vertical line with other characters.
 AddText(#9671+#9675+#9675+#9675+#9675+'Courier new'+#$D, 'Courier New', 18, clRed);
  AddText(#9671+#8609+#8609+#8609+#8609+'Courier new'+#$D, 'Courier New', 18, clRed);

Situation is even worse while using unicode components. Since the regular RichEdit allows only a very small number of characters, I tried DSUnicodeRichEdit from
https://delphi-sucks.com/2018/01/20/make-richedit-unicode-aware/
and it discards monospaced fonts. It shows all fonts as having variable character width. So I got out of the frying pan into the fire.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.