We help IT Professionals succeed at work.

Adding Subscript/Superscript to a Richedit...

shawn857
shawn857 asked
on
Hi, I don't know if it's possible but I'd like to be able to add a superscript and subscript characters, to a character in a TRichedit. Googling around I've seen some articles outlining some methods that I haven't really tested yet. I'm wondering if the subscript/superscript additions would still be within the "realm" of the one character they were attached to (which is what I'm wanting)... or do the subscript/superscript occupy their own separate character positions within the RichEdit? Ideally I'd like each character of my RichEdit to have both a superscript and a subscript, to which I could assign separate values to (maybe I'm asking for the moon...).
 
I see there is a component "RxRichEdit" ... wondering if anyone has some experience with that. I've never installed anything from the Jvcl/Jedi package before... it seems like a big deal. Would I have to install all the jedi package, or can I just install this RxRichEdit component?

Thanks!
    Shawn

P.S: I use Delphi 7
Comment
Watch Question

Top Expert 2011
Commented:
Hi Shawn,
I don't know if it's possible but I'd like to be able to add a superscript and subscript characters, to a character in a TRichedit

It is possible:
{Source}
//http://www.swissdelphicenter.ch/en/showcode.php?id=1331
uses RichEdit;

type
  TCharacterFormat = (CFM_Superscript, CFM_Subscript, CFM_Normal);

procedure RE_SetCharFormat(RichEdit: TRichEdit; CharacterFormat: TCharacterFormat);
var
  Format: TCharFormat;
begin
  FillChar(Format, SizeOf(Format), 0);
  with Format do
  begin
    cbSize := SizeOf(Format);
    dwMask := CFM_OFFSET;

    case CharacterFormat of
      CFM_Superscript: yOffset := 60;
      CFM_Subscript: yOffset := -60;
      CFM_Normal: yOffset := 0;
    end;
  end;

  Richedit.Perform(EM_SETCHARFORMAT, SCF_SELECTION, Longint(@Format));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  RichEdit1.SelAttributes.Size:= 6; // reduce the font size, but not necessary
  RE_SetCharFormat(RichEdit1, CFM_Subscript);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  RichEdit1.SelAttributes.Size:= 6; // reduce the font size, but not necessary
  RE_SetCharFormat(RichEdit1, CFM_Superscript);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  RichEdit1.SelAttributes.Size:= 8; // the normal font size
  RE_SetCharFormat(RichEdit1, CFM_Normal);
end;

Open in new window


I'm wondering if the subscript/superscript additions would still be within the "realm" of the one character they were attached to (which is what I'm wanting)... or do the subscript/superscript occupy their own separate character positions within the RichEdit?

When using rtf format (.rtf / .doc files) there are extra format tags added which are invisible, but those keep the effects you apply on the text. i.e in sub/superscript example, your character will still be that one normal character but surrounded with sub/superscript tags:

For instance, this is how empty RichEdit looks when saved to file (.rtf):
{\rtf1\fbidis\ansi\ansicpg1256\deff0\deflang1025{\fonttbl{\f0\fnil MS Sans Serif;}}
\viewkind4\uc1\pard\ltrpar\f0\fs16 
\par }
 

Open in new window


When adding 'Shown' as text becomes:
{\rtf1\fbidis\ansi\ansicpg1256\deff0\deflang1025{\fonttbl{\f0\fswiss\fcharset0 Tahoma;}{\f1\fnil MS Sans Serif;}}
\viewkind4\uc1\pard\ltrpar\lang1033\f0\fs16 Shawn\lang1025\f1 
\par }
 

Open in new window


When apply subscript on 'w':
{\rtf1\fbidis\ansi\ansicpg1256\deff0\deflang1025{\fonttbl{\f0\fswiss\fcharset0 Tahoma;}{\f1\fnil MS Sans Serif;}}
\viewkind4\uc1\pard\ltrpar\lang1033\f0\fs16 Sha\dn6\fs12 w\up0\fs16 n\lang1025\f1 
\par }
 

Open in new window


But of course you don't see those formatting tags unless you use notepad to open the .rtf files.

Ideally I'd like each character of my RichEdit to have both a superscript and a subscript, to which I could assign separate values to (maybe I'm asking for the moon...)

What do you mean? one character has both formats at the same time? You need to explain more please.

Author

Commented:
Hi Jimy, thanks for replying. Yes, each character in the RichEdit having BOTH a subscript and a superscript at the same time - like in the image attached. Is that possible?

Thanks!
     Shawn
supersub.PNG
Top Expert 2011

Commented:
> "RxRichEdit" ... wondering if anyone has some experience with that.
it seems like a big deal. Would I have to install all the jedi package, or can I just install this RxRichEdit component?

I haven't used this component.
But with jedi package, you have to install the whole thing.
Top Expert 2011

Commented:
> each character in the RichEdit having BOTH a subscript and a superscript at the same time - like in the image attached. Is that possible?

If that could be done with WordPad it could be done with RichEdit.
Have you tried WordPad?
Let me find out...

PS: I will be away for couple of hours.

Author

Commented:
I found this article on how to make subscript/superscript in Wordpad:

http://www.keynotesupport.com/pc/word-wordpad-keyboard-shortcuts.shtml

It looks like it treats the subscript (or superscript) as a whole new character.

Thanks
   Shawn
Top Expert 2011

Commented:
> It looks like it treats the subscript (or superscript) as a whole new character.

I don't think so, again with 'shawn' added as text in WrodPad, and 'w' is subscript-ed, rtf looks:
{\rtf1\fbidis\ansi\ansicpg1256\deff0\nouicompat\deflang1025{\fonttbl{\f0\fnil\fcharset0 Calibri;}}
{\*\generator Riched20 6.2.9200}\viewkind4\uc1 
\pard\ltrpar\sa200\sl276\slmult1\f0\fs22\lang9 sha\sub w\nosupersub n\par
}
 

Open in new window


So no new character, the 'w' was stressed with tag "sub".

That doesn't seem possible in WordPad nor in RichEdit, not even with basic MS Word, unless using Add-ons and most likely those add-ons use objects to handle such char complexity combination, similar to math equations. At the end you will be working inside framework that accepts certain set of symbols rather than merely adding characters and apply simple styles on them.

Author

Commented:
Yeah that sounds like it would be more trouble than it's worth, wouldn't it? Not to worry anyway Jimy - all I had to do was sleep on it last night and I came up with a much more elegant and clean solution to my problem than using subscripts/superscripts! Sorry for the wild goose chase and thank you for your attention.

Cheers
   Shawn
Top Expert 2011

Commented:
Probably it would have been troublesome.
Glad you sort it out, to the way you like.

Author

Commented:
:-)