File: ovpsview.cc

package info (click to toggle)
ivtools 1.2.11a2-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,364 kB
  • sloc: cpp: 174,988; ansic: 12,717; xml: 5,359; perl: 2,164; makefile: 831; sh: 326
file content (498 lines) | stat: -rw-r--r-- 14,924 bytes parent folder | download | duplicates (9)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
 * Copyright (c) 1994 Vectaport Inc., Cartoactive Systems
 * 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 names of the copyright holders not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.  The copyright holders make
 * no representations about the suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDERS 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.
 * 
 */

/*
 * OverlayView and OverlaysView definitions
 */

#include <OverlayUnidraw/ovclasses.h>
#include <OverlayUnidraw/ovexport.h>
#include <OverlayUnidraw/ovprint.h>
#include <OverlayUnidraw/ovpsview.h>

#include <UniIdraw/idarrows.h>

#include <Unidraw/catalog.h>
#include <Unidraw/creator.h>
#include <Unidraw/iterator.h>
#include <Unidraw/ulist.h>
#include <Unidraw/unidraw.h>

#include <Unidraw/Graphic/graphic.h>

#include <InterViews/transformer.h>

#include <OS/math.h>

#include <stream.h>

/*****************************************************************************/

static const float GRID_XINCR = 8;                 // default grid spacing
static const float GRID_YINCR = 8;

static Transformer* SaveTransformer (Graphic* g) {
    Transformer* orig = g->GetTransformer();
    Ref(orig);
    g->SetTransformer(new Transformer(orig));
    return orig;
}

static void RestoreTransformer (Graphic* g, Transformer* orig) {
    g->SetTransformer(orig);
    Unref(orig);
}

// ScaleToPostscriptCoords scales the picture to Postscript
// coordinates if screen and Postscript inches are different.

static void ScaleToPostScriptCoords (Graphic* g) {
    const double ps_inch = 72.;

    if (ivinch != ps_inch) {
	double factor = ps_inch / ivinch;
	g->Scale(factor, factor);
    }
}

static boolean Uncollected (const char* name, UList* fonts) {
    for (UList* u = fonts->First(); u != fonts->End(); u = u->Next()) {
        PSFont* font = (PSFont*) (*u)();

        if (strcmp(font->GetPrintFont(), name) == 0) {
            return false;
        }
    }
    return true;
}

static void CollectFontsFromGraphic (Graphic* gr, UList* fonts) {
    PSFont* font = gr->GetFont();

    if (font != nil && Uncollected(font->GetPrintFont(), fonts)) {
        fonts->Append(new UList(font));
    }

    Iterator i;

    for (gr->First(i); !gr->Done(i); gr->Next(i)) {
        CollectFontsFromGraphic(gr->GetGraphic(i), fonts);
    }
}

/*****************************************************************************/

boolean OverlayPS::_idraw_format = true;

ClassId OverlayPS::GetClassId () { return OVERLAY_PS; }

boolean OverlayPS::IsA (ClassId id) {
    return OVERLAY_PS == id || PostScriptView::IsA(id);
}

OverlayPS::OverlayPS (OverlayComp* subj) : PostScriptView(subj) {
    _command = nil;
}

void OverlayPS::Creator (ostream& out) {
    out << "%%Creator: " << (idraw_format() ? "idraw" : "unidraw") << "\n";
}

UList* OverlayPS::GetPSFonts () {
    if (_fonts == nil) {
        _fonts = new UList;

        CollectFontsFromGraphic(GetGraphicComp()->GetGraphic(), _fonts);
    }
    return _fonts;
}

void OverlayPS::SetCommand(Command* command) {
    _command = command;
}

Command* OverlayPS::GetCommand() {
    return _command;
}

OverlayPS* OverlayPS::CreateOvPSView (GraphicComp* comp) {
    OverlayPS* ovpsv = (OverlayPS*) comp->Create(POSTSCRIPT_VIEW);

    if (ovpsv != nil) {
        comp->Attach(ovpsv);
	ovpsv->SetCommand(GetCommand());
        ovpsv->Update();
    }
    return ovpsv;
}

OverlayPS* OverlayPS::CreateOvPSViewFromGraphic (Graphic* graphic, boolean comptree) {
    ClassId compid = graphic->CompId();
    ClassId ovpsid = 0;

    if (compid == GRAPHIC_COMPS) {
	if (!comptree) 
	    ovpsid = PICTURE_PS;
	else
	    ovpsid = OVERLAYS_PS;
    }

    else if (compid == ARROWLINE_COMP)      ovpsid = ARROWLINE_PS;
    else if (compid == ARROWMULTILINE_COMP) ovpsid = ARROWMULTILINE_PS;
    else if (compid == ARROWSPLINE_COMP)    ovpsid = ARROWSPLINE_PS;
    else if (compid == CLOSEDSPLINE_COMP)   ovpsid = CLOSEDSPLINE_PS;
    else if (compid == ELLIPSE_COMP)        ovpsid = ELLIPSE_PS;
    else if (compid == LINE_COMP)           ovpsid = LINE_PS;
    else if (compid == MULTILINE_COMP)      ovpsid = MULTILINE_PS;
    else if (compid == POLYGON_COMP)        ovpsid = POLYGON_PS;
    else if (compid == RASTER_COMP)         ovpsid = RASTER_PS;
    else if (compid == RECT_COMP)           ovpsid = RECT_PS;
    else if (compid == SPLINE_COMP)         ovpsid = SPLINE_PS;
    else if (compid == STENCIL_COMP)        ovpsid = STENCIL_PS;
    else if (compid == TEXT_COMP)           ovpsid = TEXT_PS;

    OverlayPS* ovpsv = 
	(OverlayPS*) unidraw->GetCatalog()->GetCreator()->Create(ovpsid);

    if (ovpsv)
	ovpsv->SetCommand(GetCommand());

    return ovpsv;
}

OverlayComp* OverlayPS::GetOverlayComp () {
    return (OverlayComp*) GetSubject();
}

boolean OverlayPS::idraw_format() {
    boolean format = OverlayPS::_idraw_format;
    Command* cmd = GetCommand();
    if (cmd) {
      if (GetCommand()->IsA(OV_EXPORT_CMD))
	format = ((OvExportCmd*)GetCommand())->idraw_format();
      else if (GetCommand() && GetCommand()->IsA(OVPRINT_CMD)) 
	format = ((OvPrintCmd*)GetCommand())->idraw_format();
    }
    return format;
}

void OverlayPS::idraw_format(boolean flag) { _idraw_format = flag; }

/*****************************************************************************/

ClassId OverlaysPS::GetClassId () { return OVERLAYS_PS; }

boolean OverlaysPS::IsA (ClassId id) {
    return OVERLAYS_PS == id || OverlayPS::IsA(id);
}

OverlaysPS::OverlaysPS (OverlayComp* subj) : OverlayPS(subj) {
    _views = new UList;
}

OverlaysPS::~OverlaysPS () {
    DeleteViews();
    delete _views;
}

boolean OverlaysPS::Emit (ostream& out) {
    SetPSFonts();

    Graphic* g = GetGraphicComp()->GetGraphic();
    Transformer* t = SaveTransformer(g);
    ScaleToPostScriptCoords(g);

    Comments(out);
    Prologue(out);
    Version(out);
    GridSpacing(out);

    out << "\n\n%%Page: 1 1\n\n";
    out << "Begin\n";
    FullGS(out);
    out << "/originalCTM matrix currentmatrix def\n\n";

    boolean status = PreorderView::Definition(out);

    out << "End " << MARK << " eop\n\n";
    out << "showpage\n\n";

    Trailer(out);
    RestoreTransformer(g, t);

    return status;
}

boolean OverlaysPS::Definition (ostream& out) {
    out << "Begin " << MARK << " Pict\n";
    FullGS(out);
    out << "\n";

    boolean status = PreorderView::Definition(out);

    out << "End " << MARK << " eop\n\n";

    return status;
}    

void OverlaysPS::Update () {
    DeleteViews();

    GraphicComp* comps = GetGraphicComp();
    Iterator i;

    for (comps->First(i); !comps->Done(i); comps->Next(i)) {
        GraphicComp* comp = comps->GetComp(i);
        OverlayPS* ovpsv = CreateOvPSView(comp);
	if (!ovpsv) {
	    ovpsv = CreateOvPSViewFromGraphic(comp->GetGraphic());
	    if (ovpsv != nil) {
		comp->Attach(ovpsv);
		ovpsv->Update();
	    }
	}

        if (ovpsv != nil) 
            _views->Append(new UList(ovpsv));
    }
}

OverlaysComp* OverlaysPS::GetOverlaysComp () {
    return (OverlaysComp*) GetSubject();
}

UList* OverlaysPS::Elem (Iterator i) { return (UList*) i.GetValue(); }
void OverlaysPS::First (Iterator& i) { i.SetValue(_views->First()); }
void OverlaysPS::Last (Iterator& i) { i.SetValue(_views->Last()); }
void OverlaysPS::Next (Iterator& i) { i.SetValue(Elem(i)->Next()); }
void OverlaysPS::Prev (Iterator& i) { i.SetValue(Elem(i)->Prev()); }
boolean OverlaysPS::Done (Iterator i) { return Elem(i) == _views->End(); }
ExternView* OverlaysPS::GetView (Iterator i) { return View(Elem(i)); }

void OverlaysPS::SetView (ExternView* ev, Iterator& i) {
    i.SetValue(_views->Find(ev));
}

void OverlaysPS::DeleteView (Iterator& i) {
    UList* doomed = Elem(i);
    ExternView* view = GetView(i);

    Next(i);
    _views->Remove(doomed);
    SetParent(view, nil);
    delete doomed;
    delete view;
}

void OverlaysPS::DeleteViews () {
    Iterator i;

    First(i);
    while (!Done(i)) {
        DeleteView(i);
    }
}    

/*****************************************************************************/

OverlayIdrawPS::OverlayIdrawPS (OverlayComp* subj) : OverlaysPS(subj) { }
ClassId OverlayIdrawPS::GetClassId () { return OVERLAY_IDRAW_PS; }

boolean OverlayIdrawPS::IsA (ClassId id) { 
    return OVERLAY_IDRAW_PS == id || OverlaysPS::IsA(id);
}

void OverlayIdrawPS::Prologue (ostream& out) {
    out << "%%BeginIdrawPrologue\n";
    ArrowHeader(out);
    out << "%%EndIdrawPrologue\n\n";

    OverlaysPS::Prologue(out);
}

void OverlayIdrawPS::MiscProcs (ostream& out) {
    PostScriptView::MiscProcs(out);
}

void OverlayIdrawPS::GridSpacing (ostream& out) {
    out << "Grid " << GRID_XINCR << " " << GRID_YINCR << " ";
}

void OverlayIdrawPS::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";

    OverlaysPS::ConstProcs(out);
}

void OverlayIdrawPS::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 OverlayIdrawPS::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 OverlayIdrawPS::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 OverlayIdrawPS::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 OverlayIdrawPS::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";
}