Debian Codegraph @

texk/ps2pk/spaces.c

raw
1 /* $XConsortium: spaces.c,v 1.4 91/10/10 11:19:16 rws Exp $ */
2 /* Copyright International Business Machines, Corp. 1991
3 * All Rights Reserved
4 * Copyright Lexmark International, Inc. 1991
5 * All Rights Reserved
6 *
7 * License to use, copy, modify, and distribute this software and its
8 * documentation for any purpose and without fee is hereby granted,
9 * provided that the above copyright notice appear in all copies and that
10 * both that copyright notice and this permission notice appear in
11 * supporting documentation, and that the name of IBM or Lexmark not be
12 * used in advertising or publicity pertaining to distribution of the
13 * software without specific, written prior permission.
14 *
15 * IBM AND LEXMARK PROVIDE THIS SOFTWARE "AS IS", WITHOUT ANY WARRANTIES OF
16 * ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO ANY
17 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
18 * AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. THE ENTIRE RISK AS TO THE
19 * QUALITY AND PERFORMANCE OF THE SOFTWARE, INCLUDING ANY DUTY TO SUPPORT
20 * OR MAINTAIN, BELONGS TO THE LICENSEE. SHOULD ANY PORTION OF THE
21 * SOFTWARE PROVE DEFECTIVE, THE LICENSEE (NOT IBM OR LEXMARK) ASSUMES THE
22 * ENTIRE COST OF ALL SERVICING, REPAIR AND CORRECTION. IN NO EVENT SHALL
23 * IBM OR LEXMARK BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
24 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
25 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
26 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
27 * THIS SOFTWARE.
28 */
29 /* SPACES CWEB V0021 ******** */
30 /*
31 :h1 id=spaces.SPACES Module - Handles Coordinate Spaces
32
33 This module is responsible for handling the TYPE1IMAGER "XYspace" object.
34
35 &author. Jeffrey B. Lotspiech ([email protected])
36
37
38 :h3.Include Files
39 */
40 #include "types.h"
41 #include "objects.h"
42 #include "spaces.h"
43 #include "paths.h"
44 #include "pictures.h"
45 #include "fonts.h"
46 #include "arith.h"
47
48 static void FindFfcn(DOUBLE, DOUBLE,
49 fractpel (**)(DOUBLE,DOUBLE,DOUBLE,DOUBLE));
50 static void FindIfcn(DOUBLE, DOUBLE, fractpel *, fractpel *,
51 fractpel (**)(int32_t,int32_t,int32_t,int32_t));
52 /*
53 :h3.Entry Points Provided to the TYPE1IMAGER User
54 */
55
56 /*SHARED LINE(S) ORIGINATED HERE*/
57
58 /*
59 :h3.Entry Points Provided to Other Modules
60 */
61
62 /*
63 In addition, other modules call the SPACES module through function
64 vectors in the "XYspace" structure. The entry points accessed that
65 way are "FConvert()", "IConvert()", and "ForceFloat()".
66 */
67
68 /*SHARED LINE(S) ORIGINATED HERE*/
69 /*
70 :h3.Macros and Typedefs Provided to Other Modules
71
72 :h4.Duplicating and Killing Spaces
73
74 Destroying XYspaces is so simple we can do it with a
75 macro:
76 */
77
78 /*SHARED LINE(S) ORIGINATED HERE*/
79 /*
80 On the other hand, duplicating XYspaces is slightly more difficult
81 because of the need to keep a unique ID in the space, see
82 :hdref refid=dupspace..
83
84 :h4.Fixed Point Pel Representation
85
86 We represent pel positions with fixed point numbers. This does NOT
87 mean integer, but truly means fixed point, with a certain number
88 of binary digits (FRACTBITS) representing the fractional part of the
89 pel.
90 */
91
92 /*SHARED LINE(S) ORIGINATED HERE*/
93 /*
94 :h2.Data Structures for Coordinate Spaces and Points
95 */
96 /*
97 :h3 id=matrix.Matrices
98
99 TYPE1IMAGER uses 2x2 transformation matrices. We'll use C notation for
100 such a matrix (M[2][2]), the first index being rows, the second columns.
101 */
102
103 /*
104 :h3.The "doublematrix" Structure
105
106 We frequently find it desirable to store both a matrix and its
107 inverse. We store these in a "doublematrix" structure.
108 */
109
110 /*SHARED LINE(S) ORIGINATED HERE*/
111
112 /*
113 :h3.The "XYspace" Structure
114
115 The XYspace structure represents the XYspace object.
116 */
117
118 /*SHARED LINE(S) ORIGINATED HERE*/
119 #define RESERVED 10 /* 'n' IDs are reserved for invalid & immortal spaces */
120 /*
121 */
122 #define NEXTID ((SpaceID < RESERVED) ? (SpaceID = RESERVED) : ++SpaceID)
123
124 static unsigned int SpaceID = 1;
125
126 struct XYspace *CopySpace(register struct XYspace *S)
127 {
128 S = (struct XYspace *)Allocate(sizeof(struct XYspace), S, 0);
129 S->ID = NEXTID;
130 return(S);
131 }
132 /*
133 :h3.The "fractpoint" Structure
134
135 A fractional point is just a "fractpel" x and y:
136 */
137
138 /*SHARED LINE(S) ORIGINATED HERE*/
139
140 /*
141 :h3.Lazy Evaluation of Matrix Inverses
142
143 Calculating the inverse of a matrix is somewhat involved, and we usually
144 do not need them. So, we flag whether or not the space has the inverse
145 already calculated:
146 */
147
148 #define HASINVERSE(flag) ((flag)&0x80)
149
150 /*
151 The following macro forces a space to have an inverse:
152 */
153
154 #define CoerceInverse(S) if (!HASINVERSE((S)->flag)) { \
155 MatrixInvert((S)->tofract.normal, (S)->tofract.inverse); (S)->flag |= HASINVERSE(ON); }
156 /*
157 :h3.IDENTITY Space
158
159 IDENTITY space is (logically) the space corresponding to the identity
160 transformation matrix. However, since all our transformation matrices
161 have a common FRACTFLOAT scale factor to convert to 'fractpel's, that
162 is actually what we store in 'tofract' matrix of IDENTITY:
163 */
164
165 static struct XYspace identity = { SPACETYPE, ISPERMANENT(ON) + ISIMMORTAL(ON)
166 + HASINVERSE(ON), 2, /* added 3-26-91 PNM */
167 NULL, NULL,
168 NULL, NULL, NULL, NULL,
169 INVALIDID + 1, 0,
170 { { { FRACTFLOAT, 0.0 }, { 0.0, FRACTFLOAT } },
171 { { 1.0/FRACTFLOAT, 0.0 }, { 0.0, 1.0/FRACTFLOAT } } },
172 { { 0, 0 }, { 0, 0 } } };
173 struct XYspace *IDENTITY = &identity;
174
175 /*
176 */
177 #define MAXCONTEXTS 16
178
179 static struct doublematrix contexts[MAXCONTEXTS];
180
181 #if 0
182
183 static int nextcontext = 1;
184
185 /*SHARED LINE(S) ORIGINATED HERE*/
186
187 /*
188 :h3.FindDeviceContext() - Find the Context Given a Device
189
190 This routine, given a device, returns the index of the device's
191 transformation matrix in the context array. If it cannot find it,
192 it will allocate a new array entry and fill it out.
193 */
194
195 static int FindDeviceContext(
196 void *device) /* device token */
197 {
198 DOUBLE M[2][2]; /* temporary matrix */
199 float Xres,Yres; /* device resolution */
200 int orient = -1; /* device orientation */
201 int rc = -1; /* return code for QueryDeviceState */
202
203 if (rc != 0) /* we only bother with this check once */
204 t1_abort("Context: QueryDeviceState didn't work");
205
206 M[0][0] = M[1][0] = M[0][1] = M[1][1] = 0.0;
207
208 switch (orient) {
209 case 0:
210 M[0][0] = Xres; M[1][1] = -Yres;
211 break;
212 case 1:
213 M[1][0] = Yres; M[0][1] = Xres;
214 break;
215 case 2:
216 M[0][0] = -Xres; M[1][1] = Yres;
217 break;
218 case 3:
219 M[1][0] = -Yres; M[0][1] = -Xres;
220 break;
221 default:
222 t1_abort("QueryDeviceState returned invalid orientation");
223 }
224 return(FindContext(M));
225 }
226
227 /*
228 :h3.FindContext() - Find the Context Given a Matrix
229
230 This routine, given a matrix, returns the index of that matrix matrix in
231 the context array. If it cannot find it, it will allocate a new array
232 entry and fill it out.
233 */
234
235 int FindContext(
236 DOUBLE M[2][2]) /* array to search for */
237 {
238 register int i; /* loop variable for search */
239 for (i=0; i < nextcontext; i++)
240 if (M[0][0] == contexts[i].normal[0][0] && M[1][0] == contexts[i].normal[1][0]
241 && M[0][1] == contexts[i].normal[0][1] && M[1][1] == contexts[i].normal[1][1])
242 break;
243
244 if (i >= nextcontext) {
245 if (i >= MAXCONTEXTS)
246 t1_abort("Context: out of them");
247 INT32COPY(contexts[i].normal, M, sizeof(contexts[i].normal));
248 MatrixInvert(M, contexts[i].inverse);
249 nextcontext++;
250 }
251
252 return(i);
253 }
254
255 /*
256 :h3.Context() - Create a Coordinate Space for a Device
257
258 This user operator is implemented by first finding the device context
259 array index, then transforming IDENTITY space to create an appropriate
260 cooridnate space.
261 */
262
263 struct XYspace *Context(
264 void *device, /* device token */
265 DOUBLE units) /* multiples of one inch */
266 {
267 DOUBLE M[2][2]; /* device transformation matrix */
268 register int n; /* will hold device context number */
269 register struct XYspace *S; /* XYspace constructed */
270
271 IfTrace2((MustTraceCalls),"Context(%p, %f)\n", device, units);
272
273 ARGCHECK((device == NULL), "Context of NULLDEVICE not allowed",
274 NULL, IDENTITY, (0), struct XYspace *);
275 ARGCHECK((units == 0.0), "Context: bad units", NULL, IDENTITY, (0), struct XYspace *);
276
277 n = FindDeviceContext(device);
278
279 INT32COPY(M, contexts[n].normal, sizeof(M));
280
281 M[0][0] *= units;
282 M[0][1] *= units;
283 M[1][0] *= units;
284 M[1][1] *= units;
285
286 S = (struct XYspace *)Xform(IDENTITY, M);
287
288 S->context = n;
289 return(S);
290 }
291 #endif
292
293 /*
294 :h3.ConsiderContext() - Adjust a Matrix to Take Out Device Transform
295
296 Remember, we have :f/x times U times D/ and :f/M/ and and we want :f/x
297 times U times M times D/. An easy way to do this is to calculate
298 :f/D sup <-1> times M times D/, because:
299 :formula.
300 x times U times D times D sup <-1> times M times D = x times U times M times D
301 :formula.
302 So this subroutine, given an :f/M/and an object, finds the :f/D/ for that
303 object and modifies :f/M/ so it is :f/D sup <-1> times M times D/.
304 */
305
306 static void ConsiderContext(
307 register struct xobject *obj, /* object to be transformed */
308 register DOUBLE M[2][2]) /* matrix (may be changed) */
309 {
310 register int context; /* index in contexts array */
311
312 if (obj == NULL) return;
313
314 if (ISPATHTYPE(obj->type)) {
315 struct segment *path = (struct segment *) obj;
316
317 context = path->context;
318 }
319 else if (obj->type == SPACETYPE) {
320 struct XYspace *S = (struct XYspace *) obj;
321
322 context = S->context;
323 }
324 else if (obj->type == PICTURETYPE) {
325 context = NULLCONTEXT;
326 }
327 else
328 context = NULLCONTEXT;
329
330 if (context != NULLCONTEXT) {
331 MatrixMultiply(contexts[context].inverse, M, M);
332 MatrixMultiply(M, contexts[context].normal, M);
333 }
334 }
335
336 /*
337 :h2.Conversion from User's X,Y to "fractpel" X,Y
338
339 When the user is building paths (lines, moves, curves, etc.) he passes
340 the control points (x,y) for the paths together with an XYspace. We
341 must convert from the user's (x,y) to our internal representation
342 which is in pels (fractpels, actually). This involves transforming
343 the user's (x,y) under the coordinate space transformation. It is
344 important that we do this quickly. So, we store pointers to different
345 conversion functions right in the XYspace structure. This allows us
346 to have simpler special case functions for the more commonly
347 encountered types of transformations.
348
349 :h3.Convert(), IConvert(), and ForceFloat() - Called Through "XYspace" Structure
350
351 These are functions that fit in the "convert" and "iconvert" function
352 pointers in the XYspace structure. They call the "xconvert", "yconvert",
353 "ixconvert", and "iyconvert" as appropriate to actually do the work.
354 These secondary routines come in many flavors to handle different
355 special cases as quickly as possible.
356 */
357
358 static void FXYConvert(
359 register struct fractpoint *pt, /* point to set */
360 register struct XYspace *S, /* relevant coordinate space */
361 register DOUBLE x, register DOUBLE y) /* user's coordinates of point */
362 {
363 pt->x = (*S->xconvert)(S->tofract.normal[0][0], S->tofract.normal[1][0], x, y);
364 pt->y = (*S->yconvert)(S->tofract.normal[0][1], S->tofract.normal[1][1], x, y);
365 }
366
367 static void IXYConvert(
368 register struct fractpoint *pt, /* point to set */
369 register struct XYspace *S, /* relevant coordinate space */
370 register int32_t x, register int32_t y)
371 /* user's coordinates of point */
372 {
373 pt->x = (*S->ixconvert)(S->itofract[0][0], S->itofract[1][0], x, y);
374 pt->y = (*S->iyconvert)(S->itofract[0][1], S->itofract[1][1], x, y);
375 }
376
377 /*
378 ForceFloat is a substitute for IConvert(), when we just do not have
379 enough significant digits in the coefficients to get high enough
380 precision in the answer with fixed point arithmetic. So, we force the
381 integers to floats, and do the arithmetic all with floats:
382 */
383
384 static void ForceFloat(
385 register struct fractpoint *pt, /* point to set */
386 register struct XYspace *S, /* relevant coordinate space */
387 register int32_t x, register int32_t y)
388 /* user's coordinates of point */
389 {
390 (*S->convert)(pt, S, (DOUBLE) x, (DOUBLE) y);
391 }
392
393 /*
394 :h3.FXYboth(), FXonly(), FYonly() - Floating Point Conversion
395
396 These are the routines we use when the user has given us floating
397 point numbers for x and y. FXYboth() is the general purpose routine;
398 FXonly() and FYonly() are special cases when one of the coefficients
399 is 0.0.
400 */
401
402 static fractpel FXYboth(
403 register DOUBLE cx, register DOUBLE cy, /* x and y coefficients */
404 register DOUBLE x, register DOUBLE y) /* user x,y */
405 {
406 register DOUBLE r; /* temporary float */
407
408 r = x * cx + y * cy;
409 return((fractpel) r);
410 }
411
412 /*ARGSUSED*/
413 static fractpel FXonly(
414 register DOUBLE cx, register DOUBLE cy, /* x and y coefficients */
415 register DOUBLE x, register DOUBLE y) /* user x,y */
416 {
417 register DOUBLE r; /* temporary float */
418
419 r = x * cx;
420 return((fractpel) r);
421 }
422
423 /*ARGSUSED*/
424 static fractpel FYonly(
425 register DOUBLE cx, register DOUBLE cy, /* x and y coefficients */
426 register DOUBLE x, register DOUBLE y) /* user x,y */
427 {
428 register DOUBLE r; /* temporary float */
429
430 r = y * cy;
431 return((fractpel) r);
432 }
433
434 /*
435 :h3.IXYboth(), IXonly(), IYonly() - Simple Integer Conversion
436
437 These are the routines we use when the user has given us integers for
438 x and y, and the coefficients have enough significant digits to
439 provide precise answers with only "long" (32 bit?) multiplication.
440 IXYboth() is the general purpose routine; IXonly() and IYonly() are
441 special cases when one of the coefficients is 0.
442 */
443
444 static fractpel IXYboth(
445 register fractpel cx, register fractpel cy, /* x and y coefficients */
446 register int32_t x, register int32_t y) /* user x,y */
447 {
448 return(x * cx + y * cy);
449 }
450
451 /*ARGSUSED*/
452 static fractpel IXonly(
453 register fractpel cx, register fractpel cy, /* x and y coefficients */
454 register int32_t x, register int32_t y) /* user x,y */
455 {
456 return(x * cx);
457 }
458
459 /*ARGSUSED*/
460 static fractpel IYonly(
461 register fractpel cx, register fractpel cy, /* x and y coefficients */
462 register int32_t x, register int32_t y) /* user x,y */
463 {
464 return(y * cy);
465 }
466
467
468 /*
469 :h3.FPXYboth(), FPXonly(), FPYonly() - More Involved Integer Conversion
470
471 These are the routines we use when the user has given us integers for
472 x and y, but the coefficients do not have enough significant digits to
473 provide precise answers with only "long" (32 bit?) multiplication.
474 We have increased the number of significant bits in the coefficients
475 by FRACTBITS; therefore we must use "double long" (64 bit?)
476 multiplication by calling FPmult(). FPXYboth() is the general purpose
477 routine; FPXonly() and FPYonly() are special cases when one of the
478 coefficients is 0.
479
480 Note that it is perfectly possible for us to calculate X with the
481 "FP" method and Y with the "I" method, or vice versa. It all depends
482 on how the functions in the XYspace structure are filled out.
483 */
484
485 static fractpel FPXYboth(
486 register fractpel cx, register fractpel cy, /* x and y coefficients */
487 register int32_t x, register int32_t y) /* user x,y */
488 {
489 return( FPmult(x, cx) + FPmult(y, cy) );
490 }
491
492 /*ARGSUSED*/
493 static fractpel FPXonly(
494 register fractpel cx, register fractpel cy, /* x and y coefficients */
495 register int32_t x, register int32_t y) /* user x,y */
496 {
497 return( FPmult(x, cx) );
498 }
499
500 /*ARGSUSED*/
501 static fractpel FPYonly(
502 register fractpel cx, register fractpel cy, /* x and y coefficients */
503 register int32_t x, register int32_t y) /* user x,y */
504 {
505 return( FPmult(y, cy) );
506 }
507
508
509
510 /*
511 :h3.FillOutFcns() - Determine the Appropriate Functions to Use for Conversion
512
513 This function fills out the "convert" and "iconvert" function pointers
514 in an XYspace structure, and also fills the "helper"
515 functions that actually do the work.
516 */
517
518 static void FillOutFcns(
519 register struct XYspace *S) /* functions will be set in this structure */
520 {
521 S->convert = FXYConvert;
522 S->iconvert = IXYConvert;
523
524 FindFfcn(S->tofract.normal[0][0], S->tofract.normal[1][0], &S->xconvert);
525 FindFfcn(S->tofract.normal[0][1], S->tofract.normal[1][1], &S->yconvert);
526 FindIfcn(S->tofract.normal[0][0], S->tofract.normal[1][0],
527 &S->itofract[0][0], &S->itofract[1][0], &S->ixconvert);
528 FindIfcn(S->tofract.normal[0][1], S->tofract.normal[1][1],
529 &S->itofract[0][1], &S->itofract[1][1], &S->iyconvert);
530
531 if (S->ixconvert == NULL || S->iyconvert == NULL)
532 S->iconvert = ForceFloat;
533 }
534
535 /*
536 :h4.FindFfcn() - Subroutine of FillOutFcns() to Fill Out Floating Functions
537
538 This function tests for the special case of one of the coefficients
539 being zero:
540 */
541
542 static void FindFfcn(
543 register DOUBLE cx, register DOUBLE cy, /* x and y coefficients */
544 register fractpel (**fcnP)(DOUBLE,DOUBLE,DOUBLE,DOUBLE))
545 /* pointer to function to set */
546 {
547 if (cx == 0.0)
548 *fcnP = FYonly;
549 else if (cy == 0.0)
550 *fcnP = FXonly;
551 else
552 *fcnP = FXYboth;
553 }
554
555 /*
556 :h4.FindIfcn() - Subroutine of FillOutFcns() to Fill Out Integer Functions
557
558 There are two types of integer functions, the 'I' type and the 'FP' type.
559 We use the I type functions when we are satisfied with simple integer
560 arithmetic. We used the FP functions when we feel we need higher
561 precision (but still fixed point) arithmetic. If all else fails,
562 we store a NULL indicating that this we should do the conversion in
563 floating point.
564 */
565
566 static void FindIfcn(
567 register DOUBLE cx, DOUBLE cy, /* x and y coefficients */
568 register fractpel *icxP, /* fixed point ... */
569 register fractpel *icyP, /* ... coefficients to set */
570 register fractpel (**fcnP)(int32_t,int32_t,int32_t,int32_t))
571 /* pointer to function to set */
572 {
573 register fractpel imax; /* maximum of cx and cy */
574
575 *icxP = cx;
576 *icyP = cy;
577
578 if (cx != (float) (*icxP) || cy != (float) (*icyP)) {
579 /*
580 At this point we know our integer approximations of the coefficients
581 are not exact. However, we will still use them if the maximum
582 coefficient will not fit in a 'fractpel'. Of course, we have little
583 choice at that point, but we haven't lost that much precision by
584 staying with integer arithmetic. We have enough significant digits
585 so that
586 any error we introduce is less than one part in 2:sup/16/.
587 */
588
589 imax = MAX(ABS(*icxP), ABS(*icyP));
590 if (imax < (fractpel) (1<<(FRACTBITS-1)) ) {
591 /*
592 At this point we know our integer approximations just do not have
593 enough significant digits for accuracy. We will add FRACTBITS
594 significant digits to the coefficients (by multiplying them by
595 1<<FRACTBITS) and go to the "FP" form of the functions. First, we
596 check to see if we have ANY significant digits at all (that is, if
597 imax == 0). If we don't, we suspect that adding FRACTBITS digits
598 won't help, so we punt the whole thing.
599 */
600 if (imax == 0) {
601 *fcnP = NULL;
602 return;
603 }
604 cx *= FRACTFLOAT;
605 cy *= FRACTFLOAT;
606 *icxP = cx;
607 *icyP = cy;
608 *fcnP = FPXYboth;
609 }
610 else
611 *fcnP = IXYboth;
612 }
613 else
614 *fcnP = IXYboth;
615 /*
616 Now we check for special cases where one coefficient is zero (after
617 integer conversion):
618 */
619 if (*icxP == 0)
620 *fcnP = (*fcnP == FPXYboth) ? FPYonly : IYonly;
621 else if (*icyP == 0)
622 *fcnP = (*fcnP == FPXYboth) ? FPXonly : IXonly;
623 }
624 /*
625 :h3.UnConvert() - Find User Coordinates From FractPoints
626
627 The interesting thing with this routine is that we avoid calculating
628 the matrix inverse of the device transformation until we really need
629 it, which is to say, until this routine is called for the first time
630 with a given coordinate space.
631
632 We also only calculate it only once. If the inverted matrix is valid,
633 we don't calculate it; if not, we do. We never expect matrices with
634 zero determinants, so by convention, we mark the matrix is invalid by
635 marking both X terms zero.
636 */
637
638 void UnConvert(
639 register struct XYspace *S, /* relevant coordinate space */
640 register struct fractpoint *pt, /* device coordinates */
641 DOUBLE *xp, DOUBLE *yp) /* where to store resulting x,y */
642 {
643 DOUBLE x,y;
644
645 CoerceInverse(S);
646 x = pt->x;
647 y = pt->y;
648 *xp = S->tofract.inverse[0][0] * x + S->tofract.inverse[1][0] * y;
649 *yp = S->tofract.inverse[0][1] * x + S->tofract.inverse[1][1] * y;
650 }
651
652 /*
653 :h2.Transformations
654 */
655 /*
656 :h3 id=xform.Xform() - Transform Object in X and Y
657
658 TYPE1IMAGER wants transformations of objects like paths to be identical
659 to transformations of spaces. For example, if you scale a line(1,1)
660 by 10 it should yield the same result as generating the line(1,1) in
661 a coordinate space that has been scaled by 10.
662
663 We handle fonts by storing the accumulated transform, for example, SR
664 (accumulating on the right). Then when we map the font through space TD,
665 for example, we multiply the accumulated font transform on the left by
666 the space transform on the right, yielding SRTD in this case. We will
667 get the same result if we did S, then R, then T on the space and mapping
668 an unmodified font through that space.
669 */
670 static void PseudoSpace(struct XYspace *, DOUBLE[2][2]);
671
672 struct xobject *t1_Xform(
673 register struct xobject *obj, /* object to transform */
674 register DOUBLE M[2][2]) /* transformation matrix */
675 {
676 if (obj == NULL)
677 return(NULL);
678
679 if (obj->type == FONTTYPE) {
680 register struct font *F = (struct font *) obj;
681
682 F = UniqueFont(F);
683 return((struct xobject*)F);
684 }
685 if (obj->type == PICTURETYPE) {
686 /*
687 In the case of a picture, we choose both to update the picture's
688 transformation matrix and keep the handles up to date.
689 */
690 register struct picture *P = (struct picture *) obj;
691 register struct segment *handles; /* temporary path to transform handles */
692
693 P = UniquePicture(P);
694 handles = PathSegment(LINETYPE, P->origin.x, P->origin.y);
695 handles = Join(handles,
696 PathSegment(LINETYPE, P->ending.x, P->ending.y) );
697 handles = (struct segment *)Xform((struct xobject *) handles, M);
698 P->origin = handles->dest;
699 P->ending = handles->link->dest;
700 KillPath(handles);
701 return((struct xobject *)P);
702 }
703
704 if (ISPATHTYPE(obj->type)) {
705 struct XYspace pseudo; /* local temporary space */
706 PseudoSpace(&pseudo, M);
707 return((struct xobject *) PathTransform((struct segment *)obj, &pseudo));
708 }
709
710
711 if (obj->type == SPACETYPE) {
712 register struct XYspace *S = (struct XYspace *) obj;
713
714 /* replaced ISPERMANENT(S->flag) with S->references > 1 3-26-91 PNM */
715 if (S->references > 1)
716 S = CopySpace(S);
717 else
718 S->ID = NEXTID;
719
720 MatrixMultiply(S->tofract.normal, M, S->tofract.normal);
721 /*
722 * mark inverted matrix invalid:
723 */
724 S->flag &= ~HASINVERSE(ON);
725
726 FillOutFcns(S);
727 return((struct xobject *) S);
728 }
729
730 return(ArgErr("Untransformable object", obj, obj));
731 }
732
733 /*
734 :h3.Transform() - Transform an Object
735
736 This is the external user's entry point.
737 */
738 struct xobject *t1_Transform(
739 struct xobject *obj,
740 DOUBLE cxx, DOUBLE cyx, /* 2x2 transform matrix ... */
741 DOUBLE cxy, DOUBLE cyy) /* ... elements in row order */
742 {
743 DOUBLE M[2][2];
744
745 IfTrace1((MustTraceCalls),"Transform(%p,", obj);
746 IfTrace4((MustTraceCalls)," %f %f %f %f)\n", cxx, cyx, cxy, cyy);
747
748 M[0][0] = cxx;
749 M[0][1] = cyx;
750 M[1][0] = cxy;
751 M[1][1] = cyy;
752 ConsiderContext(obj, M);
753 return(Xform(obj, M));
754 }
755 /*
756 :h3.Scale() - Special Case of Transform()
757
758 This is a user operator.
759 */
760
761 struct xobject *t1_Scale(
762 struct xobject *obj, /* object to scale */
763 DOUBLE sx, DOUBLE sy) /* scale factors in x and y */
764 {
765 DOUBLE M[2][2];
766 IfTrace3((MustTraceCalls),"Scale(%p, %f, %f)\n", obj, sx, sy);
767 M[0][0] = sx;
768 M[1][1] = sy;
769 M[1][0] = M[0][1] = 0.0;
770 ConsiderContext(obj, M);
771 return(Xform(obj, M));
772 }
773
774 #if 0
775 /*
776 :h3 id=rotate.Rotate() - Special Case of Transform()
777
778 We special-case different settings of 'degrees' for performance
779 and accuracy within the DegreeSin() and DegreeCos() routines themselves.
780 */
781
782 struct xobject *xiRotate(
783 struct xobject *obj, /* object to be transformed */
784 DOUBLE degrees) /* degrees of COUNTER-clockwise rotation */
785 {
786 DOUBLE M[2][2];
787
788
789 IfTrace2((MustTraceCalls),"Rotate(%p, %f)\n", obj, degrees);
790
791 M[0][0] = M[1][1] = DegreeCos(degrees);
792 M[1][0] = - (M[0][1] = DegreeSin(degrees));
793 ConsiderContext(obj, M);
794 return(Xform(obj, M));
795 }
796 #endif
797
798 /*
799 :h3.PseudoSpace() - Build a Coordinate Space from a Matrix
800
801 Since we have built all this optimized code that, given an (x,y) and
802 a coordinate space, yield transformed (x,y), it seems a shame not to
803 use the same logic when we need to multiply an (x,y) by an arbitrary
804 matrix that is not (initially) part of a coordinate space. This
805 subroutine takes the arbitrary matrix and builds a coordinate
806 space, with all its nifty function pointers.
807 */
808
809 static void PseudoSpace(
810 struct XYspace *S, /* coordinate space structure to fill out */
811 DOUBLE M[2][2]) /* matrix that will become 'tofract.normal' */
812 {
813 S->type = SPACETYPE;
814 S->flag = ISPERMANENT(ON) + ISIMMORTAL(ON);
815 S->references = 2; /* 3-26-91 added PNM */
816 S->tofract.normal[0][0] = M[0][0];
817 S->tofract.normal[1][0] = M[1][0];
818 S->tofract.normal[0][1] = M[0][1];
819 S->tofract.normal[1][1] = M[1][1];
820
821 FillOutFcns(S);
822 }
823
824 /*
825 :h2 id=matrixa.Matrix Arithmetic
826
827 Following the convention in Newman and Sproull, :hp1/Interactive
828 Computer Graphics/,
829 matrices are organized:
830 :xmp.
831 | cxx cyx |
832 | cxy cyy |
833 :exmp.
834 A point is horizontal, for example:
835 :xmp.
836 [ x y ]
837 :exmp.
838 This means that:
839 :formula/x prime = cxx times x + cxy times y/
840 :formula/y prime = cyx times x + cyy times y/
841 I've seen the other convention, where transform matrices are
842 transposed, equally often in the literature.
843 */
844
845 /*
846 :h3.MatrixMultiply() - Implements Multiplication of Two Matrices
847
848 Implements matrix multiplication, A * B = C.
849
850 To remind myself, matrix multiplication goes rows of A times columns
851 of B.
852 The output matrix may be the same as one of the input matrices.
853 */
854 void MatrixMultiply(
855 register DOUBLE A[2][2], /* input ... */
856 register DOUBLE B[2][2], /* ... matrices */
857 register DOUBLE C[2][2]) /* output matrix */
858 {
859 register DOUBLE txx,txy,tyx,tyy;
860
861 txx = A[0][0] * B[0][0] + A[0][1] * B[1][0];
862 txy = A[1][0] * B[0][0] + A[1][1] * B[1][0];
863 tyx = A[0][0] * B[0][1] + A[0][1] * B[1][1];
864 tyy = A[1][0] * B[0][1] + A[1][1] * B[1][1];
865
866 C[0][0] = txx;
867 C[1][0] = txy;
868 C[0][1] = tyx;
869 C[1][1] = tyy;
870 }
871 /*
872 :h3.MatrixInvert() - Invert a Matrix
873
874 My reference for matrix inversion was :hp1/Elementary Linear Algebra/
875 by Paul C. Shields, Worth Publishers, Inc., 1968.
876 */
877 void MatrixInvert(
878 DOUBLE M[2][2], /* input matrix */
879 DOUBLE Mprime[2][2]) /* output inverted matrix */
880 {
881 register DOUBLE D; /* determinant of matrix M */
882 register DOUBLE txx,txy,tyx,tyy;
883
884 txx = M[0][0];
885 txy = M[1][0];
886 tyx = M[0][1];
887 tyy = M[1][1];
888
889 D = M[1][1] * M[0][0] - M[1][0] * M[0][1];
890 if (D == 0.0)
891 t1_abort("MatrixInvert: can't");
892
893 Mprime[0][0] = tyy / D;
894 Mprime[1][0] = -txy / D;
895 Mprime[0][1] = -tyx / D;
896 Mprime[1][1] = txx / D;
897 }
898 /*
899 :h2.Initialization, Queries, and Debug
900 */
901 /*
902 :h3.InitSpaces() - Initialize Constant Spaces
903
904 For compatibility, we initialize a coordinate space called USER which
905 maps 72nds of an inch to pels on the default device.
906 */
907
908 struct XYspace *USER = &identity;
909
910 void InitSpaces(void)
911 {
912 IDENTITY->type = SPACETYPE;
913 FillOutFcns(IDENTITY);
914
915 contexts[NULLCONTEXT].normal[1][0]
916 = contexts[NULLCONTEXT].normal[0][1]
917 = contexts[NULLCONTEXT].inverse[1][0]
918 = contexts[NULLCONTEXT].inverse[0][1] = 0.0;
919 contexts[NULLCONTEXT].normal[0][0]
920 = contexts[NULLCONTEXT].normal[1][1]
921 = contexts[NULLCONTEXT].inverse[0][0]
922 = contexts[NULLCONTEXT].inverse[1][1] = 1.0;
923
924 USER->flag |= ISIMMORTAL(ON);
925 CoerceInverse(USER);
926 }
927 /*
928 :h3.QuerySpace() - Returns the Transformation Matrix of a Space
929
930 Since the tofract matrix of an XYspace includes the scale factor
931 necessary to produce fractpel results (i.e., FRACTFLOAT), this
932 must be taken out before we return the matrix to the user. Fortunately,
933 this is simple: just multiply by the inverse of IDENTITY!
934 */
935
936 void QuerySpace(
937 register struct XYspace *S, /* space asked about */
938 register DOUBLE *cxxP, register DOUBLE *cyxP, /* where to ... */
939 register DOUBLE *cxyP, register DOUBLE *cyyP) /* ... put answer */
940 {
941 DOUBLE M[2][2]; /* temp matrix to build user's answer */
942
943 if (S->type != SPACETYPE) {
944 ArgErr("QuerySpace: not a space", S, NULL);
945 return;
946 }
947 MatrixMultiply(S->tofract.normal, IDENTITY->tofract.inverse, M);
948 *cxxP = M[0][0];
949 *cxyP = M[1][0];
950 *cyxP = M[0][1];
951 *cyyP = M[1][1];
952 }
953
954 #if 0
955 /*
956 :h3.FormatFP() - Format a Fixed Point Pel
957
958 We format the pel as "dddd.XXXX", where XX's are hexidecimal digits,
959 and the dd's are decimal digits. This might be a little confusing
960 mixing hexidecimal and decimal like that, but it is convenient
961 to use for debug.
962
963 We make sure we have N (FRACTBITS/4) digits past the decimal point.
964 */
965 #define FRACTMASK ((1<<FRACTBITS)-1) /* mask for fractional part */
966
967 static void FormatFP(
968 register char *str, /* output str */
969 register fractpel fpel) /* fractional pel input */
970 {
971 char temp[8];
972 register char *s;
973 register const char *sign;
974
975 if (fpel < 0) {
976 sign = "-";
977 fpel = -fpel;
978 }
979 else
980 sign = "";
981
982 sprintf(temp, "000%x", fpel & FRACTMASK);
983 s = temp + strlen(temp) - (FRACTBITS/4);
984
985 sprintf(str, "%s%d.%sx", sign, fpel >> FRACTBITS, s);
986 }
987
988 /*
989 :h3.DumpSpace() - Display a Coordinate Space
990 */
991 /*ARGSUSED*/
992 void DumpSpace(register struct XYspace *S)
993 {
994 IfTrace4(TRUE,"--Coordinate space at %p,ID=%d,convert=%p,iconvert=%p\n",
995 S, S->ID, S->convert, S->iconvert);
996 IfTrace2(TRUE," | %12.3f %12.3f |",
997 S->tofract.normal[0][0], S->tofract.normal[0][1]);
998 IfTrace2(TRUE," [ %d %d ]\n", S->itofract[0][0], S->itofract[0][1]);
999 IfTrace2(TRUE," | %12.3f %12.3f |",
1000 S->tofract.normal[1][0], S->tofract.normal[1][1]);
1001 IfTrace2(TRUE," [ %d %d ]\n", S->itofract[1][0], S->itofract[1][1]);
1002 }
1003 #endif