Article View: comp.os.ms-windows.programmer.misc
Article #2188how do I do subscripts & superscripts?
From: John Grant
Date: Sun, 22 Nov 1992 02:29
Date: Sun, 22 Nov 1992 02:29
63 lines
2714 bytes
2714 bytes
Can anyone supply me with some sample code that will allow me to draw a
text string which may or may not contain subscripts and/or superscripts?
It is driving me nuts.
I thought that the best way would be to recognize special characters
in the text string which would mean the next character was a subscript
or superscript:
238
U "U^2^3^8"
H SO "H~2SO~4"
2 4
To draw these sub/superscripts, I need a font 1/2 the height of the
current font. Then I have to loop through the text string, switching
fonts as the subscripts & superscripts are encountered. The Y
co-ordinate also has to be adjusted for superscript or subscript:
void DrawSubSuperText(HDC hdc,POINT *p,char *text)
{
BOOL sub;
for(i=0;i<strlen(text);i++){
if(text[i]=='^' || text[i]=='~'){
switch to smaller font
if(text[i]=='^'){
p->y+=fontheight*0.5;
}else{
p->y-=fontheight*0.5;
}
i++;
sub=TRUE;
}else{
sub=FALSE;
}
TextOut(hdc,p->x,p->y,&text[i],1);
if(sub) switch back to main font
}
Creating a second font with the same characteristics (but smaller)
as the current font is relatively easy. The problem is how to
track the (x,y) positions. I know the initial (x,y) of the string,
so I plot the first character at (x,y). However I don't know the (x,y)
of each of the subsequent characters, so after I plot the first character,
I do:
SetTextAlign(hdc,GetTextAlign(hdc) | TA_UPDATECP);
so that Windows will keep track of the (x,y) after it draws each character
and I won't have to know about it. However, I need to know about it,
because I have to adjust the y value up or down for superscript/subscript.
What if the text is plotted at an arbitrary angle? I have to rotate the
(x,y) values, I guess.
What if the alignment is TA_RIGHT or TA_CENTER? Then the original (x,y)
of the text string does not refer to the beginning of the text string.
I can't believe how disgustingly messy this is! It really should be done
internally in the GDI text functions. I will need a week and 10
pages of code to do this. Is there an easier way to do this? There must
be something simpler because superscripts would be quite common in
scientific literature (or even footnote references).
--
John A. Grant jagrant@emr1.emr.ca
Airborne Geophysics
Geological Survey of Canada, Ottawa
Technical Details
Message-ID:
<1992Nov22.022918.1436@emr1.emr.ca>Path:
arc1-ca.pugleaf.net!sparky!uunet!cs.utexas.edu!torn!nott!emr1!jagrantShow Full Headers
Complete Article Headers
Newsgroups: comp.os.ms-windows.programmer.misc Path: arc1-ca.pugleaf.net!sparky!uunet!cs.utexas.edu!torn!nott!emr1!jagrant From: jagrant@emr1.emr.ca (John Grant) Subject: how do I do subscripts & superscripts? Message-ID: <1992Nov22.022918.1436@emr1.emr.ca> Organization: Energy, Mines, and Resources, Ottawa Date: Sun, 22 Nov 1992 02:29:18 GMT Lines: 63