1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
/*
* Copyright (c) 1990, 1991 Stanford University
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided
* that the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of Stanford not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. Stanford makes no representations about
* the suitability of this software for any purpose. It is provided "as is"
* without express or implied warranty.
*
* STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
* IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* IdrawComp implementation.
*/
#include "idarrows.h"
#include "idcomp.h"
#include "idcatalog.h"
#include "idclasses.h"
#include <Unidraw/grid.h>
#include <Unidraw/viewer.h>
#include <OS/math.h>
#include <stream.h>
/*****************************************************************************/
static const float GRID_XINCR = 8; // default grid spacing
static const float GRID_YINCR = 8;
/*****************************************************************************/
IdrawComp::IdrawComp () { SetGridSpacing(GRID_XINCR, GRID_YINCR); }
ClassId IdrawComp::GetClassId () { return IDRAW_COMP; }
ClassId IdrawComp::GetSubstId (const char*& delim) {
delim = "%END_IDRAW_COMP%";
return GraphicComps::GetClassId();
}
boolean IdrawComp::IsA (ClassId id) {
return IDRAW_COMP == id || GraphicComps::IsA(id);
}
void IdrawComp::SetGridSpacing (float xincr, float yincr) {
_xincr = xincr;
_yincr = yincr;
}
void IdrawComp::GetGridSpacing (float& xincr, float& yincr) {
xincr = _xincr;
yincr = _yincr;
}
/*****************************************************************************/
IdrawView::IdrawView (IdrawComp* subj) : GraphicViews(subj) { }
ClassId IdrawView::GetClassId () { return IDRAW_VIEW; }
boolean IdrawView::IsA (ClassId id) {
return IDRAW_VIEW == id || GraphicViews::IsA(id);
}
/*****************************************************************************/
PSIdraw::PSIdraw (IdrawComp* subj) : PostScriptViews(subj) { }
ClassId PSIdraw::GetClassId () { return PS_IDRAW; }
boolean PSIdraw::IsA (ClassId id) {
return PS_IDRAW == id || PostScriptViews::IsA(id);
}
void PSIdraw::Creator (ostream& out) {
out << "%%Creator: idraw\n";
}
void PSIdraw::Prologue (ostream& out) {
out << "%%BeginIdrawPrologue\n";
ArrowHeader(out);
out << "%%EndIdrawPrologue\n\n";
PostScriptView::Prologue(out);
}
void PSIdraw::GridSpacing (ostream& out) {
float xincr, yincr;
IdrawComp* comp = (IdrawComp*) GetSubject();
comp->GetGridSpacing(xincr, yincr);
out << "Grid " << xincr << " " << yincr << " ";
}
void PSIdraw::ConstProcs (ostream& out) {
int arrowWidth = Math::round(ARROWWIDTH*ivpoints);
int arrowHeight = Math::round(ARROWHEIGHT*ivpoints);
out << "/arrowHeight " << arrowHeight << " def\n";
out << "/arrowWidth " << arrowWidth << " def\n\n";
PostScriptViews::ConstProcs(out);
}
void PSIdraw::LineProc (ostream& out) {
out << "/Line {\n";
out << "0 begin\n";
out << "2 storexyn\n";
out << "newpath\n";
out << "x 0 get y 0 get moveto\n";
out << "x 1 get y 1 get lineto\n";
out << "brushNone not { istroke } if\n";
out << "0 0 1 1 leftarrow\n";
out << "0 0 1 1 rightarrow\n";
out << "end\n";
out << "} dup 0 4 dict put def\n\n";
}
void PSIdraw::MultiLineProc (ostream& out) {
out << "/MLine {\n";
out << "0 begin\n";
out << "storexyn\n";
out << "newpath\n";
out << "n 1 gt {\n";
out << "x 0 get y 0 get moveto\n";
out << "1 1 n 1 sub {\n";
out << "/i exch def\n";
out << "x i get y i get lineto\n";
out << "} for\n";
out << "patternNone not brushLeftArrow not brushRightArrow not and and ";
out << "{ ifill } if\n";
out << "brushNone not { istroke } if\n";
out << "0 0 1 1 leftarrow\n";
out << "n 2 sub dup n 1 sub dup rightarrow\n";
out << "} if\n";
out << "end\n";
out << "} dup 0 4 dict put def\n\n";
}
void PSIdraw::BSplineProc (ostream& out) {
out << "/BSpl {\n";
out << "0 begin\n";
out << "storexyn\n";
out << "newpath\n";
out << "n 1 gt {\n";
out << "0 0 0 0 0 0 1 1 true subspline\n";
out << "n 2 gt {\n";
out << "0 0 0 0 1 1 2 2 false subspline\n";
out << "1 1 n 3 sub {\n";
out << "/i exch def\n";
out << "i 1 sub dup i dup i 1 add dup i 2 add dup false subspline\n";
out << "} for\n";
out << "n 3 sub dup n 2 sub dup n 1 sub dup 2 copy false subspline\n";
out << "} if\n";
out << "n 2 sub dup n 1 sub dup 2 copy 2 copy false subspline\n";
out << "patternNone not brushLeftArrow not brushRightArrow not and and ";
out << "{ ifill } if\n";
out << "brushNone not { istroke } if\n";
out << "0 0 1 1 leftarrow\n";
out << "n 2 sub dup n 1 sub dup rightarrow\n";
out << "} if\n";
out << "end\n";
out << "} dup 0 4 dict put def\n\n";
}
void PSIdraw::SetBrushProc (ostream& out) {
out << "/SetB {\n";
out << "dup type /nulltype eq {\n";
out << "pop\n";
out << "false /brushRightArrow idef\n";
out << "false /brushLeftArrow idef\n";
out << "true /brushNone idef\n";
out << "} {\n";
out << "/brushDashOffset idef\n";
out << "/brushDashArray idef\n";
out << "0 ne /brushRightArrow idef\n";
out << "0 ne /brushLeftArrow idef\n";
out << "/brushWidth idef\n";
out << "false /brushNone idef\n";
out << "} ifelse\n";
out << "} def\n\n";
}
void PSIdraw::ArrowHeader (ostream& out) {
out << "/arrowhead {\n";
out << "0 begin\n";
out << "transform originalCTM itransform\n";
out << "/taily exch def\n";
out << "/tailx exch def\n";
out << "transform originalCTM itransform\n";
out << "/tipy exch def\n";
out << "/tipx exch def\n";
out << "/dy tipy taily sub def\n";
out << "/dx tipx tailx sub def\n";
out << "/angle dx 0 ne dy 0 ne or { dy dx atan } { 90 } ifelse def\n";
out << "gsave\n";
out << "originalCTM setmatrix\n";
out << "tipx tipy translate\n";
out << "angle rotate\n";
out << "newpath\n";
out << "arrowHeight neg arrowWidth 2 div moveto\n";
out << "0 0 lineto\n";
out << "arrowHeight neg arrowWidth 2 div neg lineto\n";
out << "patternNone not {\n";
out << "originalCTM setmatrix\n";
out << "/padtip arrowHeight 2 exp 0.25 arrowWidth 2 exp mul add sqrt ";
out << "brushWidth mul\n";
out << "arrowWidth div def\n";
out << "/padtail brushWidth 2 div def\n";
out << "tipx tipy translate\n";
out << "angle rotate\n";
out << "padtip 0 translate\n";
out << "arrowHeight padtip add padtail add arrowHeight div dup scale\n";
out << "arrowheadpath\n";
out << "ifill\n";
out << "} if\n";
out << "brushNone not {\n";
out << "originalCTM setmatrix\n";
out << "tipx tipy translate\n";
out << "angle rotate\n";
out << "arrowheadpath\n";
out << "istroke\n";
out << "} if\n";
out << "grestore\n";
out << "end\n";
out << "} dup 0 9 dict put def\n\n";
out << "/arrowheadpath {\n";
out << "newpath\n";
out << "arrowHeight neg arrowWidth 2 div moveto\n";
out << "0 0 lineto\n";
out << "arrowHeight neg arrowWidth 2 div neg lineto\n";
out << "} def\n\n";
out << "/leftarrow {\n";
out << "0 begin\n";
out << "y exch get /taily exch def\n";
out << "x exch get /tailx exch def\n";
out << "y exch get /tipy exch def\n";
out << "x exch get /tipx exch def\n";
out << "brushLeftArrow { tipx tipy tailx taily arrowhead } if\n";
out << "end\n";
out << "} dup 0 4 dict put def\n\n";
out << "/rightarrow {\n";
out << "0 begin\n";
out << "y exch get /tipy exch def\n";
out << "x exch get /tipx exch def\n";
out << "y exch get /taily exch def\n";
out << "x exch get /tailx exch def\n";
out << "brushRightArrow { tipx tipy tailx taily arrowhead } if\n";
out << "end\n";
out << "} dup 0 4 dict put def\n\n";
}
|