home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / msysjour / vol04 / 02b / project / classes / textprin.cls < prev   
Text File  |  1988-09-01  |  3KB  |  143 lines

  1. /* General Text Print Class to output text to a printer.
  2.    Mark Solinski, The Whitewater Group
  3.  */!!
  4.  
  5. inherit(Printer, #TextPrinter, #(xMax    /* Max columns */ 
  6. xPos    /* Current x position */
  7. yMax    /* Max Rows */
  8. yPos    /* Current y position */
  9. textMetrics
  10. chStr
  11. ), 2, nil)!!
  12.  
  13. now(TextPrinterClass)!!
  14.  
  15. now(TextPrinter)!!
  16.  
  17. /* closes down the print document */
  18. Def finish(self)
  19. { ^eof(self)
  20. }!!
  21.  
  22. /* create a new document with the passed name */
  23. Def start(self, docName | xtemp, ytemp, lpPt, aPt)
  24. { Call GlobalCompact(-1);
  25.   if getPrinterParms(self) cand createDC(self)
  26.   then setDocName(self, docName);
  27.     Call GetTextMetrics(hPrintDC, textMetrics);
  28.     aPt := getPhysPageSize(self);
  29.     xtemp := x(aPt);
  30.     ytemp := y(aPt);
  31.     aPt := getPrintingOffset(self);
  32.     xtemp := xtemp - x(aPt);
  33.     ytemp := ytemp - y(aPt);
  34.     xMax := xtemp - 1L;
  35.     yMax := ytemp - 1L;
  36.     if not(startDoc(self))
  37.     then deleteContext(self);
  38.       ^nil;
  39.     endif;
  40.     ^self
  41.   endif;
  42.   ^nil
  43. }!!
  44.  
  45. /* returns the width in pixels of the passed string
  46.    in the current font */
  47. Def textPixelWidth(self, aStr | aPt)
  48. { aPt := asPoint(Call GetTextExtent(errorIfNil(hPrintDC, #printerError), lP(aStr), size(aStr)));
  49.   freeHandle(aStr);
  50.   ^x(aPt)
  51. }!!
  52.  
  53. /* print the object string out to printer, with CR_LF at end */
  54. Def printLine(self, object)
  55. { print(self, object);
  56.   eol(self);
  57. }!!
  58.  
  59. /* print the object string out to printer */
  60. Def print(self, object | aStrm, aStr, extent)
  61. { aStrm := streamOver("");
  62.   printOn(object, aStrm);
  63.   aStr := aStrm.collection;
  64.   if xPos + (extent := textPixelWidth(self, aStr)) > xMax
  65.   then eol(self);
  66.   endif;
  67.   drawString(self, aStr);
  68.   xPos := xPos + extent;
  69. }!!
  70.  
  71. Def testForNewPage(self)
  72. { if yPos >= yMax
  73.   then newPage(self);
  74.   endif;
  75. }!!
  76.  
  77. /* return the tmWidth */
  78. Def  width(self)
  79. { ^asInt(wordAt(textMetrics, 10));
  80. }!!
  81.  
  82. /* return the tmHeight */
  83. Def  height(self)
  84. {  ^asInt(wordAt(textMetrics, 8))
  85.   + asInt(wordAt(textMetrics, 0));
  86. }!!
  87.  
  88. Def eol(self)
  89. { xPos := 0;
  90.   yPos := yPos + height(self);
  91.   testForNewPage(self);
  92. }!!
  93.  
  94. /* Reset the x and y position variables  */
  95. Def home(self)
  96. { xPos := yPos := 0
  97. }!!
  98.  
  99. /* Draw a string to the printer. */
  100. Def drawString(self, aStr)
  101. { Call TextOut(errorIfNil(hPrintDC, #printerError), x(self), y(self), lP(aStr),
  102.   size(aStr));
  103.   freeHandle(aStr);
  104.   ^aStr
  105. }!!
  106.  
  107. /* Home the cursor position and perform a new frame */
  108. Def newPage(self)
  109. { home(self);
  110.   ^newFrame(self);
  111. }!!
  112.  
  113. /* Translate yPos and return current y coordinate in
  114.   pixels.  */
  115. Def y(self)
  116. { ^yPos + 2
  117. }!!
  118.  
  119. /* Translate xPos and return current x coordinate in
  120.   pixels.  */
  121. Def x(self)
  122. { ^xPos + 2
  123. }!!
  124.  
  125. /* Draw a character in the window at current position.
  126.   Go the next line if character is a CR. */
  127. Def drawChar(self, aChar)
  128. { if aChar == CR
  129.   then ^eol(self)
  130.   endif;
  131.   chStr[0] := max(aChar, ' ');
  132.   Call TextOut(errorIfNil(hPrintDC, #printerError), x(self), y(self), lP(chStr), 1);
  133.   freeHandle(chStr);
  134.   ^aChar
  135. }!!
  136.  
  137. /* Initialize the text printer class */
  138. Def init(self | xtemp, ytemp, lpPt, aPt)
  139. { chStr := " ";
  140.   home(self);
  141.   textMetrics := new(Struct, 32);
  142. }!!
  143.