File: plotGremln.c

package info (click to toggle)
magic 8.3.105%2Bds.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 17,128 kB
  • sloc: ansic: 175,615; sh: 7,634; tcl: 4,536; lisp: 2,554; makefile: 946; cpp: 587; python: 389; csh: 148; awk: 140
file content (834 lines) | stat: -rw-r--r-- 23,390 bytes parent folder | download | duplicates (2)
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
/*
 * plotGremlin.c --
 *
 * This file contains procedures that generate Gremlin-format files
 * to describe a section of layout.
 *
 *     *********************************************************************
 *     * Copyright (C) 1985, 1990 Regents of the University of California. *
 *     * Permission to use, copy, modify, and distribute this              *
 *     * software and its documentation for any purpose and without        *
 *     * fee is hereby granted, provided that the above copyright          *
 *     * notice appear in all copies.  The University of California        *
 *     * makes no representations about the suitability of this            *
 *     * software for any purpose.  It is provided "as is" without         *
 *     * express or implied warranty.  Export of this software outside     *
 *     * of the United States of America may require an export license.    *
 *     *********************************************************************
 */

#ifndef lint
static char rcsid[] __attribute__ ((unused)) = "$Header: /usr/cvsroot/magic-8.0/plot/plotGremln.c,v 1.2 2008/06/01 18:37:45 tim Exp $";
#endif  /* not lint */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "utils/magic.h"
#include "utils/geometry.h"
#include "tiles/tile.h"
#include "utils/hash.h"
#include "database/database.h"
#include "utils/tech.h"
#include "utils/malloc.h"
#include "utils/utils.h"
#include "windows/windows.h"
#include "commands/commands.h"
#include "dbwind/dbwind.h"
#include "textio/textio.h"

/* Records of the following type are used to describe how to generate
 * Gremlin output for a particular set of mask layers.  Each style
 * describes the Gremlin figures to draw for a particular set of
 * layers.  A single layer may participate in several gremlin styles.
 */

#ifdef GREMLIN

typedef struct gremlinstyle
{
    TileTypeBitMask grs_layers;		/* Layers to plot in this style. */
    int grs_stipple;			/* Type of fill to use.  See below. */
    struct gremlinstyle *grs_next;	/* Next style in chain. */
} GremlinStyle;

static GremlinStyle *plotGremlinStyles;

/* Most of the grs_stipple values are Gremlin stipple numbers.  However,
 * if a grs_stipple value is less than zero, it means something special.
 * The definitions below give the possible alternatives:
 *
 * CROSS:		Draw a thick outline around the tile with
 *			a cross through it (used for contacts).
 * BORDER:		Same as CROSS, except draw the outline with
 *			no cross through it.
 */

#define CROSS -1
#define BORDER -2

/* The definitions below give the integers used for various Gremlin
 * line drawing styles (brushes).
 */

#define GREMLIN_DOTTED	1
#define GREMLIN_DASHED	4
#define GREMLIN_DOTDASH	2
#define GREMLIN_THIN	5
#define GREMLIN_MEDIUM	6
#define GREMLIN_THICK	3

/* The variables below are used to pass information from the top-level
 * procedure PlotGremlin down to the lower-level search functions
 * that are invoked for pieces of the layout.
 */

static FILE *file;		/* File to use for output. */
static float scale;		/* Multiply this by Magic units to get
				 * Gremlin units.
				 */
static GremlinStyle *curStyle;	/* Current style being output. */
static TileTypeBitMask curMask;	/* Layers currently being searched:  this
				 * is the AND of the mask from curStyle and
				 * the layers that the user specified.
				 */
static Rect bbox;		/* Bounding box, in root coordinates, of
				 * area being plotted.
				 */
#endif /* GREMLIN */


/*
 * ----------------------------------------------------------------------------
 *	PlotGremlinTechInit --
 *
 * 	Called once at beginning of technology file read-in to initialize
 *	data structures.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Clears out the list of things to plot.
 * ----------------------------------------------------------------------------
 */

#ifdef GREMLIN

void
PlotGremlinTechInit()
{
    GremlinStyle *style;

    for (style = plotGremlinStyles; style != NULL; style = style->grs_next)
    {
	freeMagic((char *) style);
    }
    plotGremlinStyles = NULL;
}

#else

void
PlotGremlinTechInit()
{}

#endif /* GREMLIN */

/*
 * ----------------------------------------------------------------------------
 *	PlotGremlinTechLine --
 *
 * 	This procedure is invoked by the technology module once for
 *	each line in the "gremlin" subsection of the "plot" section
 *	of the technology file.
 *
 * Results:
 *	Always returns TRUE (otherwise the technology module would
 *	abort Magic with a fatal error).
 *
 * Side effects:
 *	Builds up the table of Gremlin styles.
 * ----------------------------------------------------------------------------
 */

#ifdef GREMLIN

bool
PlotGremlinTechLine(sectionName, argc, argv)
    char *sectionName;		/* Name of this section (unused). */
    int argc;			/* Number of arguments on line. */
    char *argv[];		/* Pointers to fields of line. */
{
    GremlinStyle *new;
    int stipple, i;

    if (argc != 2)
    {
	TechError("\"gremlin\" lines must have exactly 2 arguments.\n");
	return TRUE;
    }

    if (strcmp(argv[1], "X") == 0)
	stipple = CROSS;
    else if (strcmp(argv[1], "B") == 0)
	stipple = BORDER;
    else
    {
	if (!StrIsInt(argv[1]))
	{
	    TechError("2nd field must be an integer or \"X\" or \"B\".\n");
	    return TRUE;
	}
	stipple = atoi(argv[1]);
    }

    new = (GremlinStyle *) mallocMagic(sizeof(GremlinStyle));

    DBTechNoisyNameMask(argv[0], &new->grs_layers);

    /* Replace non-primary contact images with primary images. */

    for (i = TT_TECHDEPBASE; i < DBNumTypes; i++)
    {
	if TTMaskHasType(&new->grs_layers, i)
	    TTMaskSetMask(&new->grs_layers, &DBLayerTypeMaskTbl[i]);
    }
    TTMaskAndMask(&new->grs_layers, &DBUserLayerBits);
    new->grs_stipple = stipple;
    new->grs_next = plotGremlinStyles;
    plotGremlinStyles = new;

    return TRUE;
}

#else

bool
PlotGremlinTechLine(sectionName, argc, argv)
    char *sectionName;		/* Name of this section (unused). */
    int argc;			/* Number of arguments on line (unused). */
    char *argv[];		/* Pointers to fields of line (unused). */
{
    return TRUE;
}

#endif /* GREMLIN */

#ifdef GREMLIN

/*
 * ----------------------------------------------------------------------------
 *
 * plotGremlinLine --
 *
 * 	Outputs a line into the current Gremlin file.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	I/O.
 *
 * ----------------------------------------------------------------------------
 */

void
plotGremlinLine(p1, p2, lineStyle)
    Point *p1, *p2;		/* Endpoints of line, given in root
				 * coordinates.
				 */
    int lineStyle;		/* Gremlin line style to use for line. */
{
    float x1, x2, y1, y2, limit;

    /* Clip the line to the rectangular area being output.  First,
     * arrange for the first x-coordinate to be the smaller, then
     * clip against vertical lines at the x-boundaries.
     */

    if (p1->p_x <= p2->p_x)
    {
	x1 = p1->p_x - bbox.r_xbot;
	x2 = p2->p_x - bbox.r_xbot;
	y1 = p1->p_y - bbox.r_ybot;
	y2 = p2->p_y - bbox.r_ybot;
    }
    else
    {
	x1 = p2->p_x - bbox.r_xbot;
	x2 = p1->p_x - bbox.r_xbot;
	y1 = p2->p_y - bbox.r_ybot;
	y2 = p1->p_y - bbox.r_ybot;
    }
    limit = bbox.r_xtop - bbox.r_xbot;
    if ((x1 > limit) || (x2 < 0)) return;
    if (x1 < 0)
    {
	y1 += (-x1)*(y2-y1)/(x2-x1);
	x1 = 0;
    }
    if (x2 > limit)
    {
	y2 -= (x2-limit)*(y2-y1)/(x2-x1);
	x2 = limit;
    }

    /* Now clip against horizontal lines at the y-boundaries. */

    if (y2 < y1)
    {
	float tmp;
	tmp = y2; y2 = y1; y1 = tmp;
	tmp = x2; x2 = x1; x1 = tmp;
    }
    limit = bbox.r_ytop - bbox.r_ybot;
    if ((y1 > limit) || (y2 < 0)) return;
    if (y1 < 0)
    {
	x1 += (-y1)*(x2-x1)/(y2-y1);
	y1 = 0;
    }
    if (y2 > limit)
    {
	x2 -= (y2-limit)*(x2-x1)/(y2-y1);
	y2 = limit;
    }

    /* Lastly, scale and generate Gremlin output. */

    x1 *= scale;
    x2 *= scale;
    y1 *= scale;
    y2 *= scale;

    fprintf(file, "VECTOR\n");
    fprintf(file, "%.3f %.3f\n%.3f %.3f\n", x1, y1, x2, y2);
    fprintf(file, "*\n%d 0\n0 \n", lineStyle);
}

/*
 * ----------------------------------------------------------------------------
 *
 * plotGremlinRect --
 *
 * 	Outputs Gremlin statements to draw a rectangular area as
 *	an outline with a given line style.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Adds information to the current Gremlin file.
 *
 * ----------------------------------------------------------------------------
 */

void
plotGremlinRect(rect, lineStyle)
    Rect *rect;	/* Rectangle to be drawn, in root coords. */
    int lineStyle;		/* Gremlin line style to use for outline. */
{
    Point p;

    p.p_x = rect->r_xbot;
    p.p_y = rect->r_ytop;
    plotGremlinLine(&rect->r_ll, &p, lineStyle);
    plotGremlinLine(&p, &rect->r_ur, lineStyle);
    p.p_x = rect->r_xtop;
    p.p_y = rect->r_ybot;
    plotGremlinLine(&rect->r_ur, &p, lineStyle);
    plotGremlinLine(&p, &rect->r_ll, lineStyle);
}

/*
 * ----------------------------------------------------------------------------
 *
 * plotGremlinPaint --
 *
 * 	This procedure is invoked once for each paint rectangle in
 *	the area being plotted.
 *
 * Results:
 *	Always returns 0 to keep the search alive.
 *
 * Side effects:
 *	Outputs information for the tile, including stipple for its
 *	interior, and a solid line for any portion of the boundary
 *	of the tile that is adjacent to a tile NOT in this style.
 *
 * ----------------------------------------------------------------------------
 */

int
plotGremlinPaint(tile, cxp)
    Tile *tile;			/* Tile that's of type to be output. */
    TreeContext *cxp;		/* Describes search in progress. */
{
    Rect tileArea, edge, rootArea;
    float xbot, xtop, ybot, ytop;
    Tile *neighbor;

    /* First transform tile coords to root coords */

    TiToRect(tile, &tileArea);
    GeoTransRect(&cxp->tc_scx->scx_trans, &tileArea, &rootArea);

    /* See if this tile gets special handling. */

    if ((curStyle->grs_stipple == CROSS) || (curStyle->grs_stipple == BORDER))
    {
	/* Draw tile as a thick outline with a cross from corner
	 * to corner, and skip the rest of this procedure.
	 */

	Point ul, lr;

	plotGremlinRect(&rootArea, GREMLIN_MEDIUM);
	if (curStyle->grs_stipple == CROSS)
	{
	    ul.p_x = rootArea.r_xbot;
	    ul.p_y = rootArea.r_ytop;
	    lr.p_x = rootArea.r_xtop;
	    lr.p_y = rootArea.r_ybot;
	    plotGremlinLine(&rootArea.r_ll, &rootArea.r_ur, GREMLIN_MEDIUM);
	    plotGremlinLine(&ul, &lr, GREMLIN_MEDIUM);
	}
	return 0;
    }

    /* This tile gets "normal" processing (i.e. stippling and outlining).
     * Clip it to the plotting area and translate to Gremlin coords.
     * Then output Gremlin information for stippled interior.
     */

    GeoClip(&rootArea, &bbox);
    xbot = (rootArea.r_xbot - bbox.r_xbot) * scale;
    xtop = (rootArea.r_xtop - bbox.r_xbot) * scale;
    ybot = (rootArea.r_ybot - bbox.r_ybot) * scale;
    ytop = (rootArea.r_ytop - bbox.r_ybot) * scale;
    fprintf(file, "POLYGON\n");
    fprintf(file, "%.3f %.3f\n%.3f %.3f\n%.3f %.3f\n%.3f %.3f\n%.3f %.3f\n",
	    xbot, ybot, xtop, ybot, xtop, ytop, xbot, ytop, xbot, ybot);
    fprintf(file, "*\n0 %d\n0 \n", curStyle->grs_stipple);

    /* Now output lines for any edges between material of the type
     * currently being drawn and material of other types.  This is
     * done by searching along the tile's borders for neighbors that
     * have the wrong types.  First, search the tile's bottom border
     * (unless it is at infinity).
     */

    if (tileArea.r_ybot > TiPlaneRect.r_ybot)
    {
	edge.r_ybot = edge.r_ytop = tileArea.r_ybot;
	for (neighbor = LB(tile); LEFT(neighbor) < tileArea.r_xtop;
		neighbor = TR(neighbor))
	{
	    if (TTMaskHasType(&curMask, TiGetType(neighbor))) continue;
	    edge.r_xbot = LEFT(neighbor);
	    edge.r_xtop = RIGHT(neighbor);
	    if (edge.r_xbot < tileArea.r_xbot) edge.r_xbot = tileArea.r_xbot;
	    if (edge.r_xtop > tileArea.r_xtop) edge.r_xtop = tileArea.r_xtop;
	    GeoTransRect(&cxp->tc_scx->scx_trans, &edge, &rootArea);
	    plotGremlinLine(&rootArea.r_ll, &rootArea.r_ur, GREMLIN_THIN);
	}
    }

    /* Now go along the tile's left border, doing the same thing.   Ignore
     * edges that are at infinity.
     */

    if (tileArea.r_xbot > TiPlaneRect.r_xbot)
    {
	edge.r_xbot = edge.r_xtop = tileArea.r_xbot;
	for (neighbor = BL(tile); BOTTOM(neighbor) < tileArea.r_ytop;
		neighbor = RT(neighbor))
	{
	    if (TTMaskHasType(&curMask, TiGetType(neighbor))) continue;
	    edge.r_ybot = BOTTOM(neighbor);
	    edge.r_ytop = TOP(neighbor);
	    if (edge.r_ybot < tileArea.r_ybot) edge.r_ybot = tileArea.r_ybot;
	    if (edge.r_ytop < tileArea.r_ytop) edge.r_ytop = tileArea.r_ytop;
	    GeoTransRect(&cxp->tc_scx->scx_trans, &edge, &rootArea);
	    plotGremlinLine(&rootArea.r_ll, &rootArea.r_ur, GREMLIN_THIN);
	}
    }

    /* Same thing for the tile's top border. */

    if (tileArea.r_ytop < TiPlaneRect.r_ytop)
    {
	edge.r_ybot = edge.r_ytop = tileArea.r_ytop;
	for (neighbor = RT(tile); RIGHT(neighbor) > tileArea.r_xbot;
		neighbor = BL(neighbor))
	{
	    if (TTMaskHasType(&curMask, TiGetType(neighbor))) continue;
	    edge.r_xbot = LEFT(neighbor);
	    edge.r_xtop = RIGHT(neighbor);
	    if (edge.r_xbot < tileArea.r_xbot) edge.r_xbot = tileArea.r_xbot;
	    if (edge.r_xtop > tileArea.r_xtop) edge.r_xtop = tileArea.r_xtop;
	    GeoTransRect(&cxp->tc_scx->scx_trans, &edge, &rootArea);
	    plotGremlinLine(&rootArea.r_ll, &rootArea.r_ur, GREMLIN_THIN);
	}
    }

    /* Finally, the right border. */

    if (tileArea.r_xtop < TiPlaneRect.r_xtop)
    {
	edge.r_xbot = edge.r_xtop = tileArea.r_xtop;
	for (neighbor = TR(tile); TOP(neighbor) > tileArea.r_ybot;
		neighbor = LB(neighbor))
	{
	    if (TTMaskHasType(&curMask, TiGetType(neighbor))) continue;
	    edge.r_ybot = BOTTOM(neighbor);
	    edge.r_ytop = TOP(neighbor);
	    if (edge.r_ybot < tileArea.r_ybot) edge.r_ybot = tileArea.r_ybot;
	    if (edge.r_ytop < tileArea.r_ytop) edge.r_ytop = tileArea.r_ytop;
	    GeoTransRect(&cxp->tc_scx->scx_trans, &edge, &rootArea);
	    plotGremlinLine(&rootArea.r_ll, &rootArea.r_ur, GREMLIN_THIN);
	}
    }
    return 0;
}

/*
 * ----------------------------------------------------------------------------
 *
 * plotGremlinLabel --
 *
 * 	This procedure is invoked once for each label overlapping the
 *	area being plotted.  It generates Gremlin output to describe
 *	the label.
 *
 * Results:
 *	Always returns 0 to keep the search from aborting.
 *
 * Side effects:
 *	Gremlin information is output.
 *
 * ----------------------------------------------------------------------------
 */

int
plotGremlinLabel(scx, label)
    SearchContext *scx;		/* Describes state of search when label
				 * was found.
				 */
    Label *label;		/* Label that was found. */
{
    Rect rootArea;
    float x, y;
    float delta;
    int pos;

    /* Mapping from our GEO_xxx positions to Gremlin object types: */

    static char *gremlinPosition[] =
    {
	"CENTCENT",
	"BOTCENT",
	"BOTLEFT",
	"CENTLEFT",
	"TOPLEFT",
	"TOPCENT",
	"TOPRIGHT",
	"CENTRIGHT",
	"BOTRIGHT"
    };

    /* Compute a distance equal to 1/8th the size of a typical wire
     * (max of thicknesses of routing layers).  This is used to offset
     * text from labels and to compute cross size for point labels.
     */

    if (RtrMetalWidth > RtrPolyWidth)
	delta = RtrMetalWidth*scale/8;
    else delta = RtrPolyWidth*scale/8;

    /* Translate the label's area and relative position to root
     * coordinates, and figure out the point relative to which
     * the label is to be positioned.
     */

    GeoTransRect(&scx->scx_trans, &label->lab_rect, &rootArea);
    pos = GeoTransPos(&scx->scx_trans, label->lab_just);
    switch (pos)
    {
	case GEO_NORTH:
	case GEO_NORTHEAST:
	case GEO_NORTHWEST:
	    y = (rootArea.r_ytop - bbox.r_ybot) * scale;
	    y += delta;
	    break;

	case GEO_CENTER:
	case GEO_WEST:
	case GEO_EAST:
	    y = (rootArea.r_ytop + rootArea.r_ybot - 2*bbox.r_ybot)*scale/2;
	    break;

	case GEO_SOUTH:
	case GEO_SOUTHEAST:
	case GEO_SOUTHWEST:
	    y = (rootArea.r_ybot - bbox.r_ybot) * scale;
	    y -= delta;
	    break;
    }
    switch (pos)
    {
	case GEO_WEST:
	case GEO_NORTHWEST:
	case GEO_SOUTHWEST:
	    x = (rootArea.r_xbot - bbox.r_xbot) * scale;
	    x -= delta;
	    break;

	case GEO_CENTER:
	case GEO_NORTH:
	case GEO_SOUTH:
	    x = (rootArea.r_xtop + rootArea.r_xbot - 2*bbox.r_xbot)*scale/2;
	    break;

	case GEO_EAST:
	case GEO_NORTHEAST:
	case GEO_SOUTHEAST:
	    x = (rootArea.r_xtop - bbox.r_xbot) * scale;
	    x += delta;
	    break;
    }

    /* Output the text for the label, if the label is within delta
     * of the area we're plotting (a large label could overlap a
     * bit of the area but stick out way off-screen too).
     */

    if ((x >= -delta) && (y >= -delta) &&
	    (x <= ((bbox.r_xtop - bbox.r_xbot) * scale) + delta) &&
	    (y <= ((bbox.r_ytop - bbox.r_ybot) * scale) + delta))
    {
	fprintf(file, "%s\n", gremlinPosition[pos]);
	fprintf(file, "%.3f %.3f\n*\n", x, y);
	fprintf(file, "1 1\n");		/* Roman font, small characters */
	fprintf(file, "%d %s\n", strlen(label->lab_text), label->lab_text);
    }

    /* Output lines marking the label's area.  Different things are
     * done depending on whether the label is a point, a line, or an
     * area.
     */

    if ((rootArea.r_xbot == rootArea.r_xtop) &&
	    (rootArea.r_ybot == rootArea.r_ytop))
    {
	/* Point label.  Output a cross. */

	float top, bot;

	x = (rootArea.r_xbot - bbox.r_xbot) * scale;
	y = (rootArea.r_ybot - bbox.r_ybot) * scale;
        top = y + delta;
	bot = y - delta;
	fprintf(file, "VECTOR\n%.3f %.3f\n%.3f %.3f\n*\n", x, bot, x, top);
	fprintf(file, "6 1\n0 \n");		/* Medium thickness */
	top = x + delta;
	bot = x - delta;
	fprintf(file, "VECTOR\n%.3f %.3f\n%.3f %.3f\n*\n", bot, y, top, y);
	fprintf(file, "6 1\n0 \n");		/* Medium thickness */
    }
    else if ((rootArea.r_xbot == rootArea.r_xtop) ||
	     (rootArea.r_ybot == rootArea.r_ytop))
    {
	/* Line label.  Just draw a medium-thickness line. */

	plotGremlinLine(&rootArea.r_ll, &rootArea.r_ur, GREMLIN_MEDIUM);
    }
    else
    {
	/* Rectangular.  Draw lines around the boundary. */

	plotGremlinRect(&rootArea, GREMLIN_MEDIUM);
    }
    return 0;
}

/*
 * ----------------------------------------------------------------------------
 *
 * plotGremlinCell --
 *
 * 	This procedure is invoked once for each unexpanded cell that
 *	overlaps the area being plotted.
 *
 * Results:
 *	Always returns 0 to keep the search from aborting.
 *
 * Side effects:
 *	Gremlin information is output to describe the cell.
 *
 * ----------------------------------------------------------------------------
 */

int
plotGremlinCell(scx)
    SearchContext *scx;		/* Describes cell whose bbox is to
				 * be plotted.
				 */
{
    extern bool PlotShowCellNames;
    char idName[100];
    Rect rootArea;
    CellDef *def;
    float x, y;

    /* Convert the cell's bounding box to root coordinates and then
     * draw as a thick outline.
     */

    def = scx->scx_use->cu_def;
    GeoTransRect(&scx->scx_trans, &def->cd_bbox, &rootArea);
    plotGremlinRect(&rootArea, GREMLIN_THICK);

    if (!PlotShowCellNames)
	return 0;

    /* Output the cell definition's name in the top of the bounding box.
     * Use a bold font (#3), in a medium size (#2).  Make sure that the
     * name's positioning point is within the area we're plotting.
     */

    x = (rootArea.r_xtop + rootArea.r_xbot - 2*bbox.r_xbot)*scale/2;
    y = (2*rootArea.r_ytop + rootArea.r_ybot - 3*bbox.r_ybot)*scale/3;
    if ((x >= 0) && (y >= 0) &&
	    (x <= (bbox.r_xtop - bbox.r_xbot)*scale) &&
	    (y <= (bbox.r_ytop - bbox.r_ybot)*scale))
    {
	fprintf(file, "CENTCENT\n%.3f %.3f\n*\n", x, y);
	fprintf(file, "3 2\n%d %s\n", strlen(def->cd_name), def->cd_name);
    }

    /* Output the cell id in the bottom of the bounding box.
     * Use an italic font (#2) in a medium size (#2).
     */

    x = (rootArea.r_xtop + rootArea.r_xbot - 2*bbox.r_xbot)*scale/2;
    y = (rootArea.r_ytop + 2*rootArea.r_ybot - 3*bbox.r_ybot)*scale/3;
    if ((x >= 0) && (y >= 0) &&
	    (x <= (bbox.r_xtop - bbox.r_xbot)*scale) &&
	    (y <= (bbox.r_ytop - bbox.r_ybot)*scale))
    {
	(void) DBPrintUseId(scx, idName, 100, TRUE);
	fprintf(file, "CENTCENT\n%.3f %.3f\n*\n", x, y);
	fprintf(file, "2 2\n%d %s\n", strlen(idName), idName);
    }

    return 0;
}

/*
 * ----------------------------------------------------------------------------
 *
 * PlotGremlin --
 *
 * 	This procedure generates a Gremlin file to describe an area of
 *	a layout.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	None.
 *
 * ----------------------------------------------------------------------------
 */

void
PlotGremlin(fileName, scx, layers, xMask)
    char *fileName;			/* Name of Gremlin file to write. */
    SearchContext *scx;			/* The use and area and transformation
					 * in this describe what to plot.
					 */
    TileTypeBitMask *layers;		/* Tells what layers to plot.  Only
					 * paint layers in this mask, and also
					 * expanded according to xMask, are
					 * plotted.  If L_LABELS is set, then
					 * labels on the layers are also
					 * plotted, if expanded according to
					 * xMask.  If L_CELL is set, then
					 * subcells that are unexpanded
					 * according to xMask are plotted as
					 * bounding boxes.
					 */
    int xMask;				/* An expansion mask, used to indicate
					 * the window whose expansion status
					 * will be used to determine
					 * visibility.  Zero means treat
					 * everything as expanded.
					 */
{
    int size, tmp;

    /* Compute a scale factor between our coordinates and Gremlin
     * coordinates.  Pick an even power of two that will make all
     * the Gremlin units fall between 0 and 512.
     */

    GeoTransRect(&scx->scx_trans, &scx->scx_area, &bbox);
    size = bbox.r_xtop - bbox.r_xbot;
    tmp = bbox.r_ytop - bbox.r_ybot;
    if (size < tmp) size = tmp;
    scale = 64.0;
    while (scale*size > 512)
	scale /= 2.0;

    /* Open the Gremlin file and output header information. */

    file = PaOpen(fileName, "w", (char *) NULL, ".", (char *) NULL,
	    (char **) NULL);
    if (file == NULL)
    {
	TxError("Couldn't write Gremlin file \"%s\".\n", fileName);
	return;
    }
    fprintf(file, "sungremlinfile\n");
    fprintf(file, "1 0.00 0.00\n");

    /* For each Gremlin style, find all the paint layers that belong
     * to that style and put plot information into the file.
     */

    for (curStyle = plotGremlinStyles; curStyle != NULL;
	 curStyle = curStyle->grs_next)
    {
	TTMaskAndMask3(&curMask, layers, &curStyle->grs_layers);
	(void) DBTreeSrTiles(scx, &curMask, xMask, plotGremlinPaint,
		(ClientData) NULL);
    }

    /* Output labels, if they are wanted. */

    if (TTMaskHasType(layers, L_LABEL))
    {
	curMask = *layers;
	TTMaskSetType(&curMask, TT_SPACE);
	(void) DBTreeSrLabels(scx, &curMask, xMask, (TerminalPath *) NULL,
		TF_LABEL_ATTACH, plotGremlinLabel, (ClientData) NULL);
    }

    /* Output subcell bounding boxes, if they are wanted. */

    if (TTMaskHasType(layers, L_CELL))
    {
	(void) DBTreeSrCells(scx, xMask, plotGremlinCell, (ClientData) NULL);
    }

    /* Output trailer information into the file, and close it. */

    fprintf(file, "-1\n");
    fclose(file);
    return;
}

#endif /* GREMLIN */