home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / os / mswindo / programm / misc / 3603 < prev    next >
Encoding:
Text File  |  1992-11-21  |  2.6 KB  |  73 lines

  1. Newsgroups: comp.os.ms-windows.programmer.misc
  2. Path: sparky!uunet!cs.utexas.edu!torn!nott!emr1!jagrant
  3. From: jagrant@emr1.emr.ca (John Grant)
  4. Subject: how do I do subscripts & superscripts?
  5. Message-ID: <1992Nov22.022918.1436@emr1.emr.ca>
  6. Organization: Energy, Mines, and Resources, Ottawa
  7. Date: Sun, 22 Nov 1992 02:29:18 GMT
  8. Lines: 63
  9.  
  10. Can anyone supply me with some sample code that will allow me to draw a
  11. text string which may or may not contain subscripts and/or superscripts?
  12. It is driving me nuts.
  13.  
  14. I thought that the best way would be to recognize special characters
  15. in the text string which would mean the next character was a subscript
  16. or superscript:
  17.          238
  18.         U        "U^2^3^8"
  19.  
  20.         H SO        "H~2SO~4"
  21.          2  4
  22. To draw these sub/superscripts, I need a font 1/2 the height of the
  23. current font.  Then I have to loop through the text string, switching
  24. fonts as the subscripts & superscripts are encountered.  The Y 
  25. co-ordinate also has to be adjusted for superscript or subscript:
  26.  
  27. void DrawSubSuperText(HDC hdc,POINT *p,char *text)
  28. {
  29. BOOL sub;
  30.     for(i=0;i<strlen(text);i++){
  31.       if(text[i]=='^' || text[i]=='~'){
  32.         switch to smaller font
  33.         if(text[i]=='^'){
  34.           p->y+=fontheight*0.5;
  35.         }else{
  36.           p->y-=fontheight*0.5;
  37.         }
  38.         i++;
  39.         sub=TRUE;
  40.       }else{
  41.         sub=FALSE;
  42.       }
  43.       TextOut(hdc,p->x,p->y,&text[i],1);
  44.       if(sub) switch back to main font
  45.     }
  46.  
  47. Creating a second font with the same characteristics (but smaller)
  48. as the current font is relatively easy.  The problem is how to
  49. track the (x,y) positions.  I know the initial (x,y) of the string,
  50. so I plot the first character at (x,y).  However I don't know the (x,y)
  51. of each of the subsequent characters, so after I plot the first character,
  52. I do:
  53.     SetTextAlign(hdc,GetTextAlign(hdc) | TA_UPDATECP);
  54. so that Windows will keep track of the (x,y) after it draws each character
  55. and I won't have to know about it.  However, I need to know about it,
  56. because I have to adjust the y value up or down for superscript/subscript.
  57.  
  58. What if the text is plotted at an arbitrary angle?  I have to rotate the
  59. (x,y) values, I guess.
  60.  
  61. What if the alignment is TA_RIGHT or TA_CENTER?  Then the original (x,y)
  62. of the text string does not refer to the beginning of the text string.
  63.  
  64. I can't believe how disgustingly messy this is!  It really should be done
  65. internally in the GDI text functions.  I will need a week and 10
  66. pages of code to do this.  Is there an easier way to do this?  There must
  67. be something simpler because superscripts would be quite common in
  68. scientific literature (or even footnote references).
  69. -- 
  70. John A. Grant                        jagrant@emr1.emr.ca
  71. Airborne Geophysics
  72. Geological Survey of Canada, Ottawa
  73.