File: text.c

package info (click to toggle)
xcircuit 2.0a6-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,960 kB
  • ctags: 3,202
  • sloc: ansic: 30,529; makefile: 51; sh: 2
file content (326 lines) | stat: -rw-r--r-- 10,563 bytes parent folder | download
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
/*-----------------------------------------------------------------------*/
/* text.c --- text processing routines for xcircuit		 	 */
/* Copyright (c) 1998  Tim Edwards, Johns Hopkins University        	 */
/*-----------------------------------------------------------------------*/

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>

/*------------------------------------------------------------------------*/
/* Local includes                                                         */
/*------------------------------------------------------------------------*/

#include "colordefs.h"
#include "xcircuit.h"
#include "alpha.h"

/*------------------------------------------------------------------------*/
/* External Variable definitions                                          */
/*------------------------------------------------------------------------*/

extern Display *dpy;
extern Clientdata areastruct;
extern short textpos;

/*------------------------------------------------------------------------*/
/* Declarations of functions defined externally to functions.c            */
/*------------------------------------------------------------------------*/

short ULength();
extern void UDrawLines();
extern float UTopScale();
extern int *appcolors;

#define HSPACE 3

/*---------------------------------------------------------------------*/
/* draw a single character at 0, 0 using current transformation matrix */
/*---------------------------------------------------------------------*/

short UDrawChar(localdata, code, styles)
  objinstptr 	localdata;
  short 	code, styles;
{
   alphaptr drawchar;
   XPoint alphapts[2], *curpoint;
   short  totalpts, i;
   short  ffont = (styles & 6);
   short  localwidth;
   XPoint *pointptr;

   /* set proper font */

   switch (ffont) {
      case 0: drawchar = alphabet[code]; 	break;
      case 2: drawchar = helveticabet[code]; 	break;
      case 4: drawchar = courierbet[code]; 	break;
      case 6: drawchar = symbolbet[code];	break;
   }

   localwidth = (ffont == 4) ? COURIER_WIDTH : drawchar->width;
   totalpts = 1 + drawchar->number[0] + drawchar->number[1] + 
	drawchar->number[2];

   if (styles & 1) USlantCTM(DCTM, 0.25);  /* premultiply by slanting function */
					   /* for italic styles		       */
   if (!(styles & 64)) {
      for (i = 0; i < drawchar->parts; i++)
	 UDrawLines(localdata, drawchar->points[i], drawchar->number[i]);

      /* under- and overlines */
      if (styles & 8)
         alphapts[0].y = alphapts[1].y = -6;
      else if (styles & 16)
         alphapts[0].y = alphapts[1].y = TEXTHEIGHT - 6;
      else if (styles & 32)
         alphapts[0].y = alphapts[1].y = TEXTHEIGHT + 4;
      if (styles & 56) {
         alphapts[0].x = -HSPACE; alphapts[1].x = localwidth;
         UDrawSimpleLine(localdata, &alphapts[0], &alphapts[1]);
      }
   }
   return ((styles & 64) ? -localwidth : localwidth);
}

/* setlineattrib and setscript are just conveniences. . . */

void setlineattrib(style)
   short style;
{
   short tmpwidth;
   float tscale = UTopScale();

   tmpwidth = (short)(((style & 2) ? 4.0 : 2.0) * tscale);
   if (tmpwidth < 2) tmpwidth = 0;
   XSetLineAttributes(dpy, areastruct.gc, tmpwidth, LineSolid, CapRound, 
	JoinBevel);
}

void UDrawString(localdata, drawlabel)
  objinstptr 	localdata;
  labelptr	drawlabel;
{
   uchar *strptr = drawlabel->string;
   short  tmplength, fstyle, ffont, fline = 0, backct = 1, tmpwidth;
   short  tmpjust;
   short  oldy, oldfont, oldstyle;
   float  tmpscale = 1.0;
   XPoint newpoint, bboxin[2], bboxout[2];
   Boolean xm, ym;

   /* calculate the transformation matrix for this object */
   /* in natural units of the alphabet vectors		  */
   /* (conversion to window units)			  */

   UPushCTM();
   UPreMultCTM(DCTM, drawlabel->position, drawlabel->scale, drawlabel->rotation);

   /* check for flip invariance; recompute CTM and justification if necessary */

   tmpjust = flipadjust(drawlabel->justify);

   tmplength = ULength(drawlabel->string, 0.0, 0); /* "natural" (unscaled) length */

   newpoint.x = (tmpjust & NOTLEFT ?
       (tmpjust & RIGHT ? -tmplength : -tmplength >> 1) : 0);
   newpoint.y = (tmpjust & NOTBOTTOM ?
       (tmpjust & TOP ? -TEXTHEIGHT : -HALFHEIGHT) : 0);


#ifdef SCHEMA
   /* Pinlabels have an additional offset spacing to pad */
   /* them from the circuit point to which they attach.  */

   if (drawlabel->pin)
      pinadjust(tmpjust, &(newpoint.x), &(newpoint.y));

#endif

   oldy = newpoint.y;

   /* do quick calculation on bounding box; don't draw if off-screen */

   bboxin[0].x = newpoint.x;
   bboxin[0].y = newpoint.y - HALFHEIGHT;
   bboxin[1].x = newpoint.x + tmplength;
   bboxin[1].y = newpoint.y + TEXTHEIGHT + HALFHEIGHT;
   UTransformbyCTM(DCTM, bboxin, bboxout, 2);
   xm = (bboxout[0].x < bboxout[1].x) ? 0 : 1;
   ym = (bboxout[0].y < bboxout[1].y) ? 0 : 1;

   if (bboxout[xm].x < areastruct.width && bboxout[ym].y < areastruct.height &&
       bboxout[!xm].x > 0 && bboxout[!ym].y > 0) {

       for (strptr = drawlabel->string; *strptr != '\0'; strptr++) {

          /* make a character transformation matrix by translating to newpoint */

          UPushCTM();
          UPreMultCTM(DCTM, newpoint, tmpscale, 0);

          /* deal with in-line text format control escape sequences */

          if ((short)(*strptr) == TEXT_ESC) {
	     short control = (short)(*(++strptr));

             if (control >= FONT_START) {
	        if (control < FONT_START + FONTS) {
	           fstyle = (control - FONT_START) % 4;
	           ffont = ((control - FONT_START) >> 1) & 0xfe;
	        }
	        else fstyle = ffont = 0;	/* user-included fonts */
	        if (oldy == newpoint.y) {	/* set top-level font and style */
	           oldfont = ffont;
	           oldstyle = fstyle;
	        }
	        setlineattrib(fstyle);
	        fline = 0;  /* font change disables underline/overline */
             }
             else if (control == SUBSCRIPT) {
	        tmpscale *= SUBSCALE; 
	        newpoint.y -= (short)((TEXTHEIGHT >> 1) * tmpscale);
	        fline = 0;
             }
             else if (control == SUPERSCRIPT) {
	        tmpscale *= SUBSCALE;
	        newpoint.y += (short)(TEXTHEIGHT * tmpscale);
	        fline = 0;
             }
             else if (control == NORMALSCRIPT) {
	        tmpscale = 1.0;
	        ffont = oldfont;	/* revert to top-level font and style */
	        fstyle = oldstyle;
	        newpoint.y = oldy;
	        fline = 0;
             }
             else if (control == UNDERLINE) fline = 8;
             else if (control == OVERLINE) {
	        uchar *quickptr = strptr + 1;
	        static uchar biglittle[7] = {'b', 'd', 'f', 'h', 'k', 'l', 't'};
	        short lowerct = 0, i;

	        /* any control character stops the /overline */ 
	        for (; (*quickptr != TEXT_ESC) && (*quickptr != '\0'); quickptr++)
	           if (islower(*quickptr)) {
	              lowerct++;
	              for (i = 0; i < 7; i++) if (*quickptr == biglittle[i])
		         lowerct--;
	           }
	        fline = ((short)(quickptr - strptr - 1) == lowerct) ? 16 : 32;
             }
             else if (control == NOLINE) fline = 0;
             else if (control == BACKSPACE) {
	        if (strptr + 1 - (backct * 3) >= drawlabel->string) {
	           newpoint.x += UDrawChar(localdata, *(strptr + 1 - (backct * 3)), 
	                  (fstyle & 1) | ffont | 64) * tmpscale;
	        }
	        backct = ((short)(*(strptr + 1)) != TEXT_ESC || (short)(*(strptr + 2))
	    	!= BACKSPACE) ? 1 : backct + 1;
             }
	     else if (control == HALFSPACE || control == QTRSPACE) {
		short addx = UDrawChar(localdata, 32, (fstyle & 1) | ffont | fline);
		newpoint.x += addx >> ((control == HALFSPACE) ? 1 : 2);
	     }
          }
          else
	     newpoint.x += UDrawChar(localdata, *strptr, (fstyle & 1) | ffont | fline)
	    		* tmpscale;
   
          /* pop the character transformation matrix */

          UPopCTM();
      }
   }

   /* pop the string transformation matrix */

   UPopCTM();

#ifdef SCHEMA
   if (drawlabel->pin) {
      UDrawX(drawlabel);
   }
#endif
}

short ULength(string, newscale, dostop)
  uchar *string;
  float	newscale;
  short dostop;
{
   short backct = 1;
   float oldscale, strscale, xtotal = 0.5;
   uchar *strptr;
   objinstptr *scaleobj;
   alphaptr *somebet;

   if (newscale > 0.0) strscale = UTopScale() * newscale;
   else strscale = 1.0;
     
   oldscale = strscale;

   for (strptr = string; *strptr != '\0'; strptr++) {
      if (dostop & ((short)(strptr - string) == textpos)) break;
      if ((short)(*strptr) == TEXT_ESC) {
	 short control = (short)(*(++strptr));	 

         if (control == SUPERSCRIPT || control == SUBSCRIPT) strscale *= SUBSCALE;
         else if (control == NORMALSCRIPT) strscale = oldscale;
	 else if (control == HALFSPACE) {
	    xtotal += (float)((*(somebet + 32))->width) * strscale / 2;
	 }      
	 else if (control == QTRSPACE) {
	    xtotal += (float)((*(somebet + 32))->width) * strscale / 4;
	 }
	 else if (control == BACKSPACE) {
	    if (strptr + 1 - (backct * 3) >= string) {
	       if (somebet == courierbet)
	          xtotal -= COURIER_WIDTH * strscale;
	       else
	          xtotal -= (float)((*(somebet + *(strptr + 1 - (backct * 3))))->width)
			 * strscale;
	    }
	    backct = (*(strptr + 1) != TEXT_ESC || *(strptr + 2) != BACKSPACE)
		 ? 1 : backct + 1;
         }
         else if (control >= FONT_START) {
	    if (control < FONT_START + FONTS) {
	       switch ((control - FONT_START) >> 2) {
	          case 1:  somebet = helveticabet;	break;
	          case 2:  somebet = courierbet;	break;
	          case 3:  somebet = symbolbet;		break;
	          default: somebet = alphabet;		break;
	       }
	    }
	    else somebet = alphabet;
	 }
      }
      else {
	 if (somebet == courierbet)
	    xtotal += COURIER_WIDTH * strscale;
	 else
	    xtotal += (float)((*(somebet + *strptr))->width) * strscale;
      }
   }
   return ((short)(xtotal - (HSPACE * oldscale)));
}

/*------------------------------------------------------------------------*/
/* simple routines for drawing and erasing labels */

undrawtext(settext)
  labelptr settext;
{
   XSetFunction(dpy, areastruct.gc, GXcopy);
   XSetForeground(dpy, areastruct.gc, BACKGROUND);
   UDrawString(areastruct.topobject, settext);
}

redrawtext(settext)
  labelptr settext;
{
   XTopSetForeground(settext->color);
   UDrawString(areastruct.topobject, settext);
}

/*------------------------------------------------------------------------*/