Report problems to ATLAS LXR Team (with time and IP address indicated)

The LXR Cross Referencer

source navigation ]
diff markup ]
identifier search ]
general search ]
 
 
Architecture: linux ]
Version: head ] [ nightly ] [ GaudiDev ]
  Links to LXR source navigation pages for stable releases [ 12.*.* ]   [ 13.*.* ]   [ 14.*.* ] 

001 /*
002  * CGMIN.C
003  *
004  * $Id: cgmin.c,v 1.1 2008/04/08 17:40:13 binet Exp $
005  *
006  * Define the CGM reader/echoer for GIST.
007  *
008  */
009 /*    Copyright (c) 1994.  The Regents of the University of California.
010                     All rights reserved.  */
011 
012 #include "play.h"
013 #include "cgmin.h"
014 #include "engine.h"
015 
016 #include <stdio.h>
017 #include "pstdio.h"
018 
019 #include <string.h>
020 #include "pstdlib.h"
021 
022 extern void CGMinfo(void);  /* defined here */
023 
024 /* function that returns the number of pages in a CGM file family */
025 extern int CGM_pages(void);
026 
027 /* Some commercial GKS packages (e.g.- ATC) set the palette to
028    0==black, 1==white, then use color 1 as the foreground color, but
029    print it as black (sigh).  This flag tries to accomodate such
030    deviant behavior.  */
031 extern int bg0fg1;
032 int bg0fg1= 0;
033 
034 typedef struct CGM CGM;
035 typedef unsigned char Octet;
036 
037 Engine *outEngines[8];
038 int outTypes[8];
039 
040 static int EchoPage(int number, int inc);
041 
042 static CGM *NewCGM(char *name, p_file *file, int maxPages);
043 static CGM *DeleteCGM(CGM *cgm);
044 static void NextName(char *filename);
045 static int BeginCatalog(CGM *cgm);
046 
047 static long ReadParameters(long nOctets, GpColor *colors, long maxColors);
048 static int SkipParameters(long nOctets);
049 static long ReadElement(int *klass, int *id);
050 
051 static long DePascalify(char *text, Octet *pasctxt, long nMax);
052 static int Snarf16(Octet *param);
053 static long SnarfInteger(Octet *param);
054 static GpReal SnarfReal(Octet *param);
055 static void RememberPoint(GpReal x0, GpReal y0);
056 static char *SnarfMoreText(Octet *param);
057 static char *SnarfText(Octet *param);
058 static char *PathifyText(char *text, int path);
059 static GpReal SnarfSA(Octet *param, int md, GpReal dflt);
060 static int SnarfColor(Octet *param, long nOctets);
061 static void ConvertPoints(long n, short *vdc, GpReal *ndcx, GpReal *ndcy);
062 static int CheckScratch(long nPoints);
063 
064 static void SetVDC(int *vdc);
065 static void GSetClip(int on);
066 static void ResetPalette(int forPage);
067 
068 static int DoClass0(int id, long nOctets);
069 static int DoClass1(int id, long nOctets);
070 static int DoClass2(int id, long nOctets);
071 static int DoClass3(int id, long nOctets);
072 static int DoClass4(int id, long nOctets);
073 static int DoClass5(int id, long nOctets);
074 static int DoClass6(int id, long nOctets);
075 static int DoClass7(int id, long nOctets);
076 
077 #define NCAR_RECORD_SIZE 1440L
078 #define N_BUFSIZE 4096L
079 static unsigned long n_fread(void *ptr, unsigned long nbytes, CGM *cgm);
080 static int n_fseek(CGM *cgm, long offset, int relative);
081 static long n_ftell(CGM *cgm);
082 static long n_fskip(void *ptr, long nbytes, CGM *cgm);
083 
084 /* ------------------------------------------------------------------------ */
085 
086 static int currentPage= 0;
087 
088 static char metafileName[256];                    /* from BEGIN METAFILE */
089 static char pictureName[256];          /* from most recent BEGIN PICTURE */
090 static char metafileDescription[256];       /* from METAFILE DESCRIPTION */
091 static int isGistCGM= 0;  /* set if metafileDescription begins "Gist;  " */
092 
093 static int nBytesInt= 2;
094 static int vdcIllegible, realIllegible, indexIllegible,
095   cvalIllegible, cndxIllegible, cselIllegible;
096 
097 #define N_GIST_FONTS 20
098 static char *gistFonts[]= {
099   "Courier", "Courier-Bold", "Courier-Oblique", "Courier-BoldOblique",
100   "Times-Roman", "Times-Bold", "Times-Italic", "Times-BoldItalic",
101   "Helvetica", "Helvetica-Bold", "Helvetica-Oblique", "Helvetica-BoldOblique",
102   "Symbol", "Symbol", "Symbol", "Symbol",
103   "NewCenturySchlbk-Roman", "NewCenturySchlbk-Bold",
104   "NewCenturySchlbk-Italic", "NewCenturySchlbk-BoldItalic" };
105 static int defaultNumbers[]= {
106   0, 8, 4, 12, 1, 9, 5, 13, 2, 10, 6, 14, 3, 11, 7, 15, 16, 17, 18, 19 };
107 
108 /* Transformation from vdc->ndc is known on a per-page basis */
109 #define DEFAULT_METRIC (1.0/(25545.24*(ONE_INCH/25.4)))
110 static GpReal cgmScale= (1.0/25545.24), cgmScaleX= (1.0/25545.24),
111   cgmScaleY= (1.0/25545.24), cgmOffsetX= 0.0, cgmOffsetY= 0.0;
112 int cgmLandscape= 0;
113 extern int cgmScaleToFit;
114 int cgmScaleToFit= 0;
115 
116 /* May as well have a permanently allocated palette of maximal size */
117 static GpColorCell cgmPalette[256], defaultPalette[256];
118 static int cgmNColors= 0, defaultNColors=  0, cgmNColMax;
119 static int cgmColorChange= 0, alteredPalette= 0, setXPalette= 0,
120   needPalette= 0;
121 
122 struct CGMDesc {
123   /* metafile descriptor elements */
124   int colorMaxI, colorMaxV[6];
125   int *fonts, nfonts, maxFonts;
126 
127   /* picture descriptor elements */
128   int scaling;
129   GpReal metric;
130   int lwidth, msize, ewidth;
131   int vdc[4];
132   struct unused_color { unsigned char red, green, blue, gray; } background;
133 
134 #define ABSTRACT 0
135 #define METRIC 1
136 #define INDEXED 0
137 #define DIRECT 1
138 #define ABSOLUTE 0
139 #define SCALED 1
140 };
141 
142 /* cgmDesc represents default metafile and picture descriptor settings as
143    defined in the ANSI X3.122 - 1986 (Part 3) standard */
144 struct CGMDesc cgmDesc= {
145   63, {0, 0, 0, 255, 255, 255}, defaultNumbers, N_GIST_FONTS, 0,
146   0, DEFAULT_METRIC, 1, 1, 1, {0, 0, 32767, 32767}, {255, 255, 255, 255} };
147 
148 /* pageDesc represents cgmDesc, as modified by the defaults
149    replacement section of the metafile header */
150 struct CGMDesc pageDesc= {
151   63, {0, 0, 0, 255, 255, 255}, defaultNumbers, N_GIST_FONTS, 0,
152   0, DEFAULT_METRIC, 1, 1, 1, {0, 0, 32767, 32767}, {255, 255, 255, 255} };
153 
154 /* pictDesc represents pageDesc, as modified
155    by any picture descriptor elements */
156 struct CGMDesc pictDesc= {
157   63, {0, 0, 0, 255, 255, 255}, defaultNumbers, N_GIST_FONTS, 0,
158   0, DEFAULT_METRIC, 1, 1, 1, {0, 0, 32767, 32767}, {255, 255, 255, 255} };
159 
160 struct CGMControls {
161   int auxColor, transparency;
162   short clipBox[4];
163   int clipOn, clipDefault;
164 };
165 
166 /* cgmControls represent default control settings as defined in the
167    ANSI X3.122 - 1986 (Part 3) standard */
168 struct CGMControls cgmControls= {
169   0, 1, {0, 0, 32767, 32767}, 1, 1 };
170 
171 /* pageControls represent cgmControls, as modified by the defaults
172    replacement section of the metafile header */
173 struct CGMControls pageControls= {
174   0, 1, {0, 0, 32767, 32767}, 1, 1 };
175 
176 /* pictControls represents pageControls, as modified
177    by any picture descriptor elements */
178 struct CGMControls pictControls= {
179   0, 1, {0, 0, 32767, 32767}, 1, 1 };
180 
181 struct NonGistAttribs {
182   /* non-Gist constructs */
183   int eVisible;         /* edge visibility separate from gistA.e.type */
184   int hindex, pindex;   /* separate hatch and pattern indices */
185   GpReal upX, upY, baseX, baseY;  /* character up and base vectors */
186   GpReal expand, spacing;         /* other text attributes */
187   int prec, path;                 /* text precision, path */
188 };
189 
190 struct CGMAttributes {
191 
192   /* line attributes (GpLines, GpDisjoint) */
193   GpLineAttribs l;
194 
195   /* marker attributes (GpMarkers) */
196   GpMarkerAttribs m;
197 
198   /* filled area attributes (GpFill) */
199   GpFillAttribs f;
200 
201   /* text attributes (GpText) */
202   GpTextAttribs t;
203 
204   /* non-Gist constructs */
205   struct NonGistAttribs ng;
206 };
207 
208 /* cgmDefaults represent default attribute settings as defined in the
209    ANSI X3.122 - 1986 (Part 3) standard */
210 static struct CGMAttributes cgmDefaults= {
211   { FG_COLOR, L_SOLID, 1.0 },                     /* line attributes */
212   { FG_COLOR, M_ASTERISK, 1.0 },                  /* marker attributes */
213   { FG_COLOR, F_SOLID },                          /* fill attributes */
214   { FG_COLOR, 0, 0.0156,
215       TX_RIGHT, TH_NORMAL, TV_NORMAL },           /* text attributes */
216 
217   {0, 0, 0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 2, 0}   /* non-Gist attributes,
218                                                      except pattern table */
219   };
220 
221 /* pageDefaults represent cgmDefaults, as modified by the defaults
222    replacement section of the metafile header */
223 static struct CGMAttributes pageDefaults= {
224   { FG_COLOR, L_SOLID, 1.0 },                     /* line attributes */
225   { FG_COLOR, M_ASTERISK, 1.0 },                /* marker attributes */
226   { FG_COLOR, F_SOLID },                          /* fill attributes */
227   { FG_COLOR, 0, 0.0156,
228       TX_RIGHT, TH_NORMAL, TV_NORMAL },           /* text attributes */
229 
230   {0, 0, 0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 2, 0}   /* non-Gist attributes,
231                                                      except pattern table */
232   };
233 
234 /* current values of non-Gist attributes
235    (current values of others in gistA) */
236 static struct NonGistAttribs nonGistA= {
237   0, 0, 0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 2, 0};
238 
239 /* ------------------------------------------------------------------------ */
240 
241 struct CGM {
242   char *name;
243   p_file *file;   /* non-zero only if currently open */
244   CGM *prev, *next;
245   int firstPage, nPages, maxPages;
246   long *pageAddress, catalogEnd;
247 
248   /* additional stuff to read NCAR CGM files */
249   unsigned char *buffer;
250   long pos, valid;
251   int ncar;
252   long ncar_rec, ncar_len;
253 };
254 
255 static CGM *currentCGM= 0, *firstCGM= 0, *catalogCGM= 0;
256 
257 static int cgmCmdLen;  /* 2 or 4, length of previous ReadElement command */
258 static Octet *cmdString= 0;
259 static Octet *currentCmd;
260 static long cmdLength;
261 
262 /* Name incrementer copied from cgm.c */
263 static void NextName(char *filename)
264 {
265   int i, len= filename? strlen(filename) : 0;
266   if (len>4 && (strcmp(filename+len-4, ".cgm")==0 ||
267                 strcmp(filename+len-4, ".CGM")==0)) i= len-4;
268   else if (len>5 && (strcmp(filename+len-5, ".ncgm")==0 ||
269                      strcmp(filename+len-5, ".NCGM")==0)) i= len-5;
270   else i= len;
271   while (i-- > 0) {
272     if (filename[i]=='9') {
273       filename[i]= '0';
274     } else {
275       if (filename[i]=='Z' || filename[i]>='z') filename[i]= '0';
276       else filename[i]++;
277       break;
278     }
279   }
280 }
281 
282 static CGM *NewCGM(char *name, p_file *file, int maxPages)
283 {
284   CGM *cgm= (CGM *)p_malloc(sizeof(CGM)+strlen(name)+1);
285   long *pageAddress= (long *)p_malloc(sizeof(long)*maxPages);
286 
287   if (!cgm || !pageAddress) {
288     if (cgm) p_free(cgm);
289     if (pageAddress) p_free(pageAddress);
290     Warning("memory manager failed trying to open ", name);
291     return 0;
292   }
293 
294   cgm->name= ((char *)cgm)+sizeof(CGM);
295   strcpy(cgm->name, name);
296   if (!file) {
297     NextName(cgm->name);
298     file= p_fopen(cgm->name, "rb");
299     if (!file) {
300       p_free(cgm);
301       p_free(pageAddress);
302       return 0;
303     }
304   }
305   cgm->file= file;
306   cgm->prev= cgm->next= 0;
307   cgm->firstPage= 1;
308   cgm->nPages= 0;
309   cgm->maxPages= maxPages;
310   cgm->catalogEnd= -1;
311   cgm->pageAddress= pageAddress;
312 
313   cgm->buffer = p_malloc(4096);
314   cgm->pos = cgm->valid = 0;
315   cgm->ncar = 0;
316   cgm->ncar_rec = cgm->ncar_len = -1L;
317 
318   return cgm;
319 }
320 
321 static CGM *DeleteCGM(CGM *cgm)
322 {
323   CGM *next= cgm->next;
324   if (cgm->prev) cgm->prev->next= next;
325   if (cgm->pageAddress) p_free(cgm->pageAddress);
326   if (cgm->buffer) p_free(cgm->buffer);
327   p_free(cgm);
328   return next;
329 }
330 
331 static int BeginCatalog(CGM *cgm)
332 {
333   CGM *oldCGM= currentCGM;
334   int klass, id, bad;
335   long nOctets;
336 
337   do {
338     currentCGM= cgm;
339 
340     nOctets= ReadElement(&klass, &id);
341 
342     if (klass==0 && id==1) {
343       bad= 0;
344       for (;;) {
345         SkipParameters(nOctets);
346         nOctets= ReadElement(&klass, &id);
347         if (nOctets>=0) {
348           if (klass==0) {
349             if (id==3 || id==2) break;  /* begin picture or end metafile */
350             else if (id!=0) {
351               if (cgm->ncar)
352                 Warning("bad class 0 element in metafile descriptor", "");
353               bad= 1;
354               break;
355             }
356           } else if (klass!=1 && klass!=6 && klass!=7) {
357             if (cgm->ncar)
358               Warning("bad element class in metafile descriptor", "");
359             bad= 1;
360             break;
361           }
362         } else {
363           bad= 1;
364           break;
365         }
366       }
367     } else {
368       if (cgm->ncar) Warning("BEGIN METAFILE element missing", "");
369       bad= 1;
370     }
371 
372     if (bad) {
373       int ncar = cgm->ncar;
374       cgm->ncar = 1;
375       if (ncar || n_fseek(cgm, 0L, 0)) {
376         currentCGM= oldCGM;
377         Warning(cgm->name, " is not a binary CGM, cannot open");
378         return 1;
379       }
380     }
381   } while (bad);
382 
383   if (id==3) {
384     cgm->pageAddress[0]= n_ftell(cgm)-cgmCmdLen;
385     SkipParameters(nOctets);
386     cgm->catalogEnd= n_ftell(cgm);
387     cgm->nPages= 1;
388   } else {
389     cgm->catalogEnd= -1;
390   }
391 
392   currentCGM= oldCGM;
393 
394   return 0;
395 }
396 
397 int CatalogCGM(void)
398 {
399   int klass, id, nPage;
400   long nOctets;
401   CGM *tmp, *cgm= currentCGM;
402 
403   if (!catalogCGM) return 0;
404 
405   while (catalogCGM->catalogEnd<0) {
406     if (catalogCGM!=currentCGM) {
407       p_fclose(catalogCGM->file);
408       catalogCGM->file= 0;
409     }
410     nPage= catalogCGM->firstPage+catalogCGM->nPages;
411     catalogCGM= NewCGM(catalogCGM->name, (void *)0, 256);
412     if (!catalogCGM || BeginCatalog(catalogCGM)) {
413       if (catalogCGM) {
414         p_fclose(catalogCGM->file);
415         DeleteCGM(catalogCGM);
416         catalogCGM= 0;
417       }
418       return 0;
419     }
420     catalogCGM->firstPage= nPage;
421   }
422 
423   n_fseek(catalogCGM, catalogCGM->catalogEnd, 0);
424 
425   for (;;) {
426     currentCGM= catalogCGM;
427     for (;;) {
428       nOctets= ReadElement(&klass, &id);
429       if (nOctets>=0) {
430         if (klass==0) {
431           if (id==3 || id==2) break;  /* begin picture or end metafile */
432         }
433         SkipParameters(nOctets);
434       } else {
435         Warning("missing END METAFILE element in ", catalogCGM->name);
436         klass= 0;
437         id= 2;
438         break;
439       }
440     }
441     currentCGM= cgm;
442 
443     if (id==3) {
444       if (catalogCGM->maxPages==catalogCGM->nPages) {
445         long *pa= (long *)p_malloc(sizeof(long)*(catalogCGM->maxPages+256));
446         int i;
447         if (!pa) {
448           catalogCGM->catalogEnd= -1;
449           if (catalogCGM!=currentCGM) p_fclose(catalogCGM->file);
450           catalogCGM= 0;
451           Warning("memory manager failed cataloging ", catalogCGM->name);
452           break;
453         }
454         for (i=0 ; i<catalogCGM->nPages ; i++)
455           pa[i]= catalogCGM->pageAddress[i];
456         p_free(catalogCGM->pageAddress);
457         catalogCGM->pageAddress= pa;
458         catalogCGM->maxPages+= 256;
459       }
460       catalogCGM->pageAddress[catalogCGM->nPages++]=
461         n_ftell(catalogCGM)-cgmCmdLen;
462       currentCGM= catalogCGM;
463       SkipParameters(nOctets);
464       currentCGM= cgm;
465       catalogCGM->catalogEnd= n_ftell(catalogCGM);
466       break;
467 
468     } else { /* found END METAFILE */
469       tmp= catalogCGM;
470       tmp->catalogEnd= -1;
471       if (tmp!=currentCGM) {
472         p_fclose(tmp->file);
473         tmp->file= 0;
474       }
475       tmp= tmp->next= NewCGM(catalogCGM->name, (void *)0, 256);
476       if (tmp) {
477         if (BeginCatalog(tmp)) {
478           p_fclose(tmp->file);
479           DeleteCGM(tmp);
480           tmp= 0;
481         } else {
482           tmp->prev= catalogCGM;
483           tmp->firstPage= catalogCGM->firstPage+catalogCGM->nPages;
484         }
485       }
486       catalogCGM= tmp;
487       if (!tmp) return 0;
488     }
489   }
490 
491   return 1;
492 }
493 
494 /* ------------------------------------------------------------------------ */