home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / i / iv26_w_3.zip / EXAMPLES / IDRAW / IPAINT.C < prev    next >
C/C++ Source or Header  |  1980-01-05  |  6KB  |  193 lines

  1. /*
  2.  * Copyright (c) 1987, 1988, 1989 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Stanford not be used in advertising or
  9.  * publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Stanford makes no representations about
  11.  * the suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  20.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. // $Header: ipaint.c,v 1.10 89/10/09 14:48:18 linton Exp $
  24. // implements classes IBrush, IFont, and IPattern.
  25.  
  26. #include "ipaint.h"
  27. #include "istring.h"
  28. #include <stdlib.h>
  29.  
  30. // IBrush creates the brush.  Calling IBrush with no arguments creates
  31. // the "none" brush.
  32.  
  33. IBrush::IBrush () : () {
  34.     leftarrow = false;
  35.     rightarrow = false;
  36.     CalcDashPat(0xffff);
  37. }
  38.  
  39. IBrush::IBrush (int p, int w, boolean l, boolean r) : (p, w) {
  40.     leftarrow = l;
  41.     rightarrow = r;
  42.     CalcDashPat(p);
  43. }
  44.  
  45. // Width overrides PBrush::Width when PBrush's value is the predefined
  46. // brush single because single cannot be trusted to return 1.  It
  47. // might return 0 because the X implementation might support only a
  48. // fast, device-dependent brush, not the standard single-width brush.
  49.  
  50. int IBrush::Width () {
  51.     int width = PBrush::Width();
  52.     if (value == single) {
  53.     width = 1;
  54.     }
  55.     return width;
  56. }
  57.  
  58. // CalcDashPat calculates and stores the Postscript dash pattern
  59. // corresponding to the brush's line pattern.
  60.  
  61. void IBrush::CalcDashPat (int linepat) {
  62.     linepat &= 0xffff;        // mask should always match patternWidth
  63.  
  64.     if (linepat == 0x0000) {        // clear brush
  65.     dashpatsize = 0;
  66.     dashoffset = -1;
  67.     } else if (linepat == 0xffff) { // solid brush
  68.     dashpatsize = 0;
  69.     dashoffset = 0;
  70.     } else if (linepat == 0x5555) { // dotted brush, store 1 element not 16
  71.     dashpat[0] = 1;
  72.     dashpatsize = 1;
  73.     dashoffset = 1;
  74.     } else if (linepat == 0xaaaa) { // dotted brush, store 1 element not 16
  75.     dashpat[0] = 1;
  76.     dashpatsize = 1;
  77.     dashoffset = 0;
  78.     } else {
  79.     int i = 0;
  80.     while (!((linepat << i) & 0x8000)) {
  81.         ++i;
  82.     }
  83.     dashoffset = patternWidth - i + 1;
  84.  
  85.     int j = 0;
  86.     boolean currentrun = true;
  87.     int length = 0;
  88.     for (int k = 0; k < patternWidth; k++) {
  89.         if ((((linepat << i) & 0x8000) != 0) == currentrun) {
  90.         ++length;
  91.         } else {
  92.         dashpat[j++] = length;
  93.         currentrun = !currentrun;
  94.         length = 1;
  95.         }
  96.         i = (i == patternWidth) ? 0 : i + 1;
  97.     }
  98.     if (length > 0) {
  99.         dashpat[j] = length;
  100.     }
  101.     dashpatsize = j + 1;
  102.     }
  103.  
  104.     const int Postscriptdashlimit = 11;
  105.     if (dashpatsize > Postscriptdashlimit) {
  106.     fprintf(stderr, "Brush dash pattern 0x%x exceeds maximum ", linepat);
  107.     fprintf(stderr, "length of Postscript dash pattern with ");
  108.     fprintf(stderr, "%d elements, truncated to ", dashpatsize);
  109.     fprintf(stderr, "%d elements\n", Postscriptdashlimit);
  110.     dashpatsize = Postscriptdashlimit;
  111.     }
  112. }
  113.  
  114. // IColor creates the named color and stores its name.
  115.  
  116. IColor::IColor (const char* n) : (n) {
  117.     name = strdup(n);
  118. }
  119.  
  120. // IColor creates the color using the given intensities and stores its
  121. // "name".
  122.  
  123. IColor::IColor (int r, int g, int b, const char* n) : (r, g, b) {
  124.     name = strdup(n);
  125. }
  126.  
  127. // IColor stores the given color and its "name".
  128.  
  129. IColor::IColor (Color* color, const char* n) {
  130.     value = color;
  131.     value->Reference();
  132.     name = strdup(n);
  133. }
  134.  
  135. // Free storage allocated for the name.
  136.  
  137. IColor::~IColor () {
  138.     delete name;
  139. }
  140.  
  141. // IFont creates the named font and stores the print font and size.
  142.  
  143. IFont::IFont (const char* name, const char* pf, const char* ps)
  144. : (FilterName(name)) {
  145.     printfont = strdup(pf);
  146.     printsize = strdup(ps);
  147.     printfontandsize = new char[strlen(pf) + 1 + strlen(ps) + 1];
  148.     strcpy(printfontandsize, pf);
  149.     strcat(printfontandsize, " ");
  150.     strcat(printfontandsize, ps);
  151.     lineHt = atoi(ps);
  152. }
  153.  
  154. // Free storage allocated for the print font and size.
  155.  
  156. IFont::~IFont () {
  157.     delete printfont;
  158.     delete printsize;
  159.     delete printfontandsize;
  160. }
  161.  
  162. // FilterName filters the name to ensure "stdfont" does not pass
  163. // through to PFont without being converted to nil.
  164.  
  165. const char* IFont::FilterName (const char* name) {
  166.     if (strcmp(name, "stdfont") == 0) {
  167.     name = nil;
  168.     }
  169.     return name;
  170. }
  171.  
  172. // IPattern creates the pattern.  Calling IPattern with no arguments
  173. // creates the "none" pattern, calling IPattern with an integer
  174. // creates a pattern from a 4x4 bitmap, calling IPattern with an array
  175. // and the actual size creates a pattern from a 16x16 bitmap that may
  176. // have originally been 8x8, and calling IPattern with an integer and
  177. // a float creates a grayscale pattern from a 4x4 bitmap.
  178.  
  179. IPattern::IPattern () : () {
  180.     graylevel = -1;
  181.     size = 0;
  182. }
  183.  
  184. IPattern::IPattern (int dither, float g) : (dither) {
  185.     graylevel = g;
  186.     size = 0;
  187. }
  188.  
  189. IPattern::IPattern (int data[patternHeight], int s) : (data) {
  190.     graylevel = -1;
  191.     size = s;
  192. }
  193.