home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / Apps / DevTools / ManPagesFromHeaders / Source / RTF.m < prev    next >
Encoding:
Text File  |  1993-05-06  |  2.3 KB  |  149 lines

  1. /* 
  2.  * RTF is Based on an object by Simson Garfinkel and Michael Mahoney
  3.  * in "NeXTSTEP Programming" published by Telos ISBN  0-387-97884-4
  4.  */
  5.  
  6. #import "RTF.h"
  7.  
  8. const char *header =
  9.     "{\\rtf0\\ansi{\\fonttbl\\f1\\fnil Times-Roman;\\f0\\fswiss"
  10.     " Helvetica;}\\f0\n";
  11.     
  12. @implementation RTF
  13.  
  14. - init
  15. {
  16.     [super init];
  17.  
  18.     textStream = NXOpenMemory(0, 0, NX_READWRITE);
  19.     NXWrite(textStream, header, strlen(header));
  20.     return self;
  21. }
  22.  
  23.  
  24. - (NXStream *)stream
  25. {
  26.     NXSeek(textStream, 0L, NX_FROMSTART);
  27.     return textStream;
  28. }
  29.  
  30.  
  31. /* appendRTF: appends an arbitrary RTF string
  32.  * to the RTF object
  33.  */
  34. - appendRTF:(const char *)string
  35. {
  36.     NXSeek(textStream, 0L, NX_FROMEND);
  37.     NXWrite(textStream, string, strlen(string));
  38.     return self;
  39. }
  40.  
  41.  
  42. /* append: appends an ASCII text string, "escaping"
  43.  * all of the special characters in the text.
  44.  */
  45. - append:(const char *)string
  46. {
  47.     if (string==0) return self;                                     /* safety */
  48.  
  49.     NXSeek(textStream, 0L, NX_FROMEND);
  50.     while(*string) {
  51.         switch(*string) {
  52.             /* escape special characters */
  53.             case '\n':
  54.             case '{':
  55.             case '}':
  56.             case '\\':
  57.                 NXPutc(textStream, '\\');
  58.             break;
  59.             default:
  60.             break;
  61.         }
  62.         NXPutc(textStream, *string);
  63.         string++;
  64.     }
  65.     return self;
  66. }
  67.  
  68. - bold:(BOOL)flag
  69. {
  70.     [self appendRTF: flag ? "\\b " : "\\b0 "];
  71.     return self;
  72. }
  73.  
  74. - italic:(BOOL)flag
  75. {
  76.     [self appendRTF: flag ? "\\i " : "\\i0 "];
  77.     return self;
  78. }
  79.  
  80. - setJustify:(int)mode
  81. {
  82.     switch(mode) {
  83.         case NX_LEFTALIGNED:
  84.         case NX_JUSTIFIED:
  85.             [self appendRTF: "\\ql "];
  86.             break;
  87.         case NX_CENTERED:
  88.             [self appendRTF: "\\qc "];
  89.             break;
  90.          case NX_RIGHTALIGNED:
  91.             [self appendRTF: "\\qr "];
  92.             break;
  93.     }
  94.     return self;
  95. }
  96.  
  97. - tab
  98. {
  99.     [self appendRTF: "\\tab "];
  100.     return self;
  101. }
  102.  
  103. - setFontSize: (int) size
  104. {
  105.     char    buffer[256];
  106.     
  107.     sprintf(buffer, "\\fs%d", (size * 2) );
  108.     [self appendRTF: buffer];
  109.  
  110.     return self;
  111. }
  112.  
  113. - setLeftMargin: (int) marginInTwips;
  114. {
  115.     /* A twip is a typesetters measure. There are 1440 twips in
  116.      * an inch.
  117.      */
  118.     char    buffer[256];
  119.     
  120.     sprintf(buffer, "\\margl%d", marginInTwips);
  121.     [self appendRTF: buffer];
  122.     
  123.     return self;
  124. }
  125.  
  126. - changeToTimes
  127. {
  128.     /* See the header above for definitions of families */
  129.     [self appendRTF: "\\f1"];
  130.     
  131.     return self;
  132. }
  133.  
  134. - changeToHelvetica
  135. {
  136.     /* See the header above for definitions of families */
  137.     [self appendRTF: "\\f0"];
  138.     
  139.     return self;
  140. }
  141.  
  142. - free
  143. {
  144.     NXCloseMemory(textStream, NX_FREEBUFFER);
  145.     return [super free];
  146. }
  147.  
  148. @end
  149.