home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************\
- * *
- * File Name: xhpgl.c part of hpgl to Xwindows program *
- * *
- * *
- * Author: Randy L. Yach *
- * *
- * Functional description: *
- * *
- * This program will read a 7470a syntax HPGL file and *
- * display it on an Xwindow display. *
- * *
- \******************************************************************/
-
-
- #include <X/Xlib.h>
- #include <stdio.h>
- #include "black.bitmap"
-
- #define DISCARD 1
- #define MAXX 1000
- #define MAXY 773
-
- double xMin,xMax,yMin,yMax; /* user min/max coordinate boundaries */
- int last_x, last_y; /* last x and y coordinates for line drawing */
- int x,y; /* current x and y coordinates for line drawing */
- int cur_pen,no_pen,line; /* current pen color */
- int pen_down; /* pen status */
- int absolute; /* ploter status */
- Font text_font; /* LB font selection */
- short font_offset; /* distance from lower left to upper left of font */
- Window main_window; /* main window for plotting */
- Color pen[10]; /* all pen colors for plotting */
- int minWidth,minHeight; /* size of X window for plotting */
- Pattern line_type; /* line dash pattern */
- char font_name[32]; /* font name of user supplied font */
-
- /**/
- /******************************************************************\
- * prodedure select_font(x,y) *
- ******************************************************************
- * will pick a font based on the window size *
- \******************************************************************/
-
- void get_text_font(user_font)
- int user_font;
- {
- if (!user_font)
- {
- if ((minWidth < 350) || (minHeight < 350))
- strcpy(font_name,"nil2");
- else if ((minWidth < 700) || (minHeight < 700))
- strcpy(font_name,"6x10");
- else if ((minWidth < 800) || (minHeight < 800))
- strcpy(font_name,"8x13");
- else
- strcpy(font_name,"9x15");
- }
-
- /* get the font and check if it is valid */
- text_font = XGetFont(font_name);
-
- }
-
- /**/
- /******************************************************************\
- * procedure reverse *
- ******************************************************************
- * used to revers a string s *
- \******************************************************************/
- void reverse(s)
- /* reverse string s in place */
- char s[];
- { /* begin reverse */
- int c,i,j;
-
- for (i = 0, j = strlen(s)-1; i < j; i++, j--)
- { /* begin for string */
- c = s[i];
- s[i] = s[j];
- s[j] = c;
- } /* end for string */
- } /* end reverse */
-
- /**/
- /******************************************************************\
- * procedure itoa *
- ******************************************************************
- * converts an integer to a string. *
- \******************************************************************/
- void itoa(s,n)
- /* convert n to characters in s */
- int n;
- char s[];
- { /* begin itoa */
- int i, sign;
-
- if ((sign = n) < 0) /* record sign */
- n = -n;
- i = 0;
- do { /* generate digits in reverse order */
- s[i++] = n % 10 + '0'; /* get next digit */
- } while ((n /= 10) > 0); /* delete it */
- if (sign < 0)
- s[i++] = '-';
- s[i] = '\0';
- reverse(s);
- } /* end itoa */
-
- /**/
- /******************************************************************\
- * procedure initialize_plotter *
- ******************************************************************
- * used to set up the default plotter conditions. *
- \******************************************************************/
- void initialize_plotter()
- { /* begin initialize_plotter */
- x=0; /* set current x plotter coordinate */
- y=0; /* set current y plotter coordinate */
- last_x=0; /* set last x plotter coordinate */
- last_y=0; /* set last y plotter coordinate */
-
- xMin = 0; /* set default x and y plotter scale */
- yMin = 0; /* user coordinates. This assumes */
- xMax = 10300; /* that the 7470a is in US mode */
- yMax = 7650;
-
- pen_down=0; /* pot the pen in up position */
-
- cur_pen=pen[0].pixel; /* select no pen or no color */
- no_pen=pen[0].pixel; /* need a reference background color for text */
-
- line_type = XMakePattern(0xff,8,1); /* set up solid line pattern */
- } /* end initialize_plotter */
- /**/
- /******************************************************************\
- * procedure assign_color *
- ******************************************************************
- * used to assign a color to each plotter pen. *
- \******************************************************************/
- void assign_color(pen_num,color)
- int pen_num;
- char color[];
- { /* begin assign_color */
- if ((pen_num <=0) || (pen_num >= 9))
- { /* begin invalid pen */
- fprintf(stderr,"pen numbers must be 1 to 8.\n");
- exit(-1);
- } /* end invalid pen */
- else
- { /* begin valid pen */
- if (strcmp(color,"black")==0)
- { /* begin black */
- pen[pen_num].red=0;
- pen[pen_num].green=0; pen[pen_num].blue=0;
- } /* end black */
- else if (strcmp(color,"red")==0)
- { /* begin red */
- pen[pen_num].red=65535;
- pen[pen_num].green=0; pen[pen_num].blue=0;
- } /* end red */
- else if (strcmp(color,"gold")==0)
- { /* begin gold */
- pen[pen_num].red=61500;pen[pen_num].green=36750;
- pen[pen_num].blue=25000;
- } /* end gold */
- else if (strcmp(color,"firebrick")==0)
- { /* begin firebrick */
- pen[pen_num].red=42600;pen[pen_num].green=10500;
- pen[pen_num].blue=10500;
- } /* end firebrick */
- else if (strcmp(color,"maroon")==0)
- { /* begin maroon */
- pen[pen_num].red=42600; pen[pen_num].green=10500;
- pen[pen_num].blue=32100;
- } /* end maroon */
- else if (strcmp(color,"orange")==0)
- { /* begin orange */
- pen[pen_num].red=61200; pen[pen_num].green=15000;
- pen[pen_num].blue=15000;
- } /* end orange */
- else if (strcmp(color,"pink")==0)
- { /* begin pink */
- pen[pen_num].red=56400; pen[pen_num].green=42900;
- pen[pen_num].blue=42900;
- } /* end pink */
- else if (strcmp(color,"turquoise")==0)
- { /* begin turquoise */
- pen[pen_num].red=51900; pen[pen_num].green=65535;
- pen[pen_num].blue=65535;
- } /* end turquoise */
- else if (strcmp(color,"violet")==0)
- { /* begin violet */
- pen[pen_num].red=23700; pen[pen_num].green=14100;
- pen[pen_num].blue=23700;
- } /* end violet */
- else if (strcmp(color,"green")==0)
- { /* begin green */
- pen[pen_num].red=0;
- pen[pen_num].green=65535; pen[pen_num].blue=0;
- } /* end green */
- else if (strcmp(color,"blue")==0)
- { /* begin blue */
- pen[pen_num].red=0;
- pen[pen_num].green=0; pen[pen_num].blue=65535;
- } /* end blue */
- else if (strcmp(color,"yellow")==0)
- { /* begin yellow */
- pen[pen_num].red=65535;
- pen[pen_num].green=65535; pen[pen_num].blue=0;
- } /* end yellow */
- else if (strcmp(color,"cyan")==0)
- { /* begin cyan */
- pen[pen_num].red=0;
- pen[pen_num].green=65535; pen[pen_num].blue=65535;
- } /* end cyan */
- else if (strcmp(color,"magenta")==0)
- { /* begin magenta */
- pen[pen_num].red=65535;
- pen[pen_num].green=0; pen[pen_num].blue=65535;
- } /* end magenta */
- else if (strcmp(color,"white")==0)
- { /* begin white */
- pen[pen_num].red=65535;
- pen[pen_num].green=65535; pen[pen_num].blue=65535;
- } /* end white */
- else
- { /* begin default white */
- fprintf(stderr,"Color %s not supported, using white.\n",color);
- pen[pen_num].red=65535;
- pen[pen_num].green=65535; pen[pen_num].blue=65535;
- } /* begin default white */
- } /* end valid pen */
- } /* end assign_color */
-
- /**/
- main (argc, argv)
- int argc;
- char *argv[];
- { /* begin main */
-
- /* variable declerations */
- Pixmap pixmap,bpix; /* Used for frame and background of main window */
- Bitmap blackB; /* All 1's located in a 16 x 16 area for tiles */
- OpaqueFrame frame; /* Used for creating the frame for the window */
- XEvent event; /* used for watching for keys in main window */
- WindowInfo window_info[1]; /* window information from XQueryWindow */
- FontInfo text_font_info[1]; /* font information structure */
-
- char file_name[32]; /* file name of hpgl input file */
- extern FILE *yyin; /* File pointer for hpgl input file */
-
- char size_def[22]; /* is the default size used for creating the window */
- char temp[22]; /* temporary string holder */
- int user_font = 0; /* need to know if the user entered a font */
-
- int i; /* general puropse counter */
- char user_color[10]; /* string for user color name */
- char *color_file_path; /* path to user color file */
- char color_file[65]; /* color file name */
- FILE *color_file_pointer; /* color file pointer */
-
-
- /* set up default font_name and file_name */
- *font_name='\0';
- *file_name='\0';
-
- /* set up the default color file path and name */
- color_file_path = (char *) getenv("HOME");
- if (color_file_path == NULL)
- *color_file_path = '\0';
- strcpy(color_file,color_file_path);
- strcat(color_file,"/.hpcolors");
-
- /* set up the default pixel size of the display window. all plot
- coordinates will be scaled to these coordinates */
- minWidth=MAXX;
- minHeight=MAXY;
- strcpy(size_def,"=");
- itoa(temp,MAXX);
- strcat(size_def,temp);
- strcat(size_def,"x");
- itoa(temp,MAXY);
- strcat(size_def,temp);
- strcat(size_def,"+0+0");
-
- /* initialize all the pen colors to default value */
- pen[1].red=65535; pen[1].green=0; pen[1].blue=0;
- pen[2].red=0; pen[2].green=65535; pen[2].blue=0;
- pen[3].red=0; pen[3].green=0; pen[3].blue=65535;
- pen[4].red=0; pen[4].green=65535; pen[4].blue=65535;
- pen[5].red=65535; pen[5].green=0; pen[5].blue=65535;
- pen[6].red=65535; pen[6].green=65535; pen[6].blue=0;
- pen[7].red=65535; pen[7].green=65535; pen[7].blue=65535;
- pen[8].red=65535; pen[8].green=65535; pen[8].blue=65535;
-
- /* define the window black and white colors. let the user
- defint all the rest. pen 0 is background black and pen 9
- is frame white. pens 1-8 are user definable */
- pen[0].red=0; pen[0].green=0; pen[0].blue=0; /* background black */
- pen[9].red=65535; pen[9].green=65535; pen[9].blue=65535; /* white */
-
- /* check to see if the user did not enter a file name and print
- and error message */
- if(argc == 1)
- { /* begin error print */
- fprintf(stderr,"\nusage: xhpgl -fn [font] file\n\n");
- exit(-1);
- } /* end error print */
-
- /* parse the input arguments and get the file name and font name.
- all arguments passed that are not -fn font or -cf color_file
- are assumed to be files.
- This program will use the last arg sent as the file to plot. */
- for(argc--,argv++;argc;argc--,argv++)
- { /* begin input arg parseing */
- if (!strcmp(*argv,"-fn"))
- { /* begin arg fn */
- argc--;
- argv++;
- strcpy(font_name,*argv);
- user_font = 1;
- } /* end arg fn */
- if (!strcmp(*argv,"-cf"))
- { /* begin color file */
- argc--;
- argv++;
- strcpy(color_file,*argv);
- } /* end color file */
- else
- strcpy(file_name,*argv);
- } /* end input arg parseing */
-
- /* open the hpgl file and print error if not able to */
- if((yyin=fopen(file_name,"r"))==NULL)
- { /* begin open file error */
- fprintf(stderr,"Could not open hpgl file -%s- for read.\n",
- file_name);
- exit(-1);
- } /* end open file error */
-
-
- /* open the color definition file and print message of not found */
- /* assign colors if file is found */
- if((color_file_pointer=fopen(color_file,"r"))==NULL)
- { /* begin no color file */
- fprintf(stderr,"Could not open color file -%s- for read.\n",
- color_file);
- fprintf(stderr,"Using default internal values...\n");
- } /* end no color file */
- else
- { /* begin assign colors */
- while(fscanf(color_file_pointer,"%d %s",&i,user_color) != EOF)
- assign_color(i,user_color);
- fclose(color_file_pointer);
- } /* begin assign colors */
-
-
- /* connect to the Display as a client */
- if (XOpenDisplay(NULL)==NULL)
- {
- fprintf(stderr,"Could not open Display\n");
- exit(-1);
- }
-
- /* allocate and create a hardware color for each of the pens */
- for (i=0;i<10;i++)
- if (!(XGetHardwareColor(&pen[i])))
- {
- fprintf(stderr,"Could not obtain color for pen %d\n",i);
- exit(-1);
- }
-
-
- /* set up the pixmap and bitmaps for the background of the main
- window and its frame. */
- blackB = XStoreBitmap (black_width,black_height,black_bits);
- pixmap = XMakePixmap (blackB,pen[0].pixel,pen[9].pixel);
- bpix = XMakePixmap (blackB,pen[9].pixel,pen[0].pixel);
-
- /* set up the frame info for the default window */
- frame.self=main_window;
- frame.x=0;
- frame.y=0;
- frame.bdrwidth=3;
- frame.border=bpix;
- frame.background=pixmap;
- frame.width=minWidth;
- frame.height=minHeight;
-
- /* create the main window */
- main_window = XCreate ("xhpgl",
- "xhpgl window",
- size_def,
- size_def,
- &frame,
- minWidth/10,minHeight/10);
-
- /* query window for size to see how big the actual window was
- that was created. */
- XQueryWindow (main_window,window_info);
-
- /* set the the window coordinate size to the actual window size */
- minWidth = window_info[0].width;
- minHeight = window_info[0].height;
-
- /* select the proper font for the window size. minWidth and minHeight
- must be assigned to the current window size before this call is made,
- or the font will be the wrong size */
- get_text_font(user_font);
-
- /* check to make sure that the font selected exists on the
- hardware */
- if (text_font == 0)
- {/* begin get font error */
- fprintf(stderr,"Could not read font %s\n",font_name);
- exit(-1);
- }/* end get font error */
-
- /* get size of the font */
- XQueryFont(text_font,text_font_info);
- font_offset = text_font_info[0].height;
-
- /* map the window and flush all events */
- XMapWindow(main_window);
- XFlush();
-
- /* initialize the plotter and set it up to default conditions */
- initialize_plotter();
-
- /* call the yacc parser to parse the hpgl file. All plotting and line
- drawing are done in the yacc file */
- yyparse();
-
- /* close the hpgl file */
- fclose(yyin);
-
- /* set up the window to watch key events */
- XSelectInput(main_window,KeyPressed);
-
- while(1)
- { /* begin event handeling */
- /* get the next pending event in the queue */
- XNextEvent(&event);
-
- /* switch on the event type */
- switch (event.type)
- { /* begin event switch */
- case KeyPressed:
- { /* begin key pressed */
- char *key_char;
- XKeyEvent *key_event = (XKeyEvent *) &event;
-
- key_char = XLookupMapping(key_event,&i);
-
- for (;i>0;i--)
- switch(*key_char++)
- { /* begin key matching */
- case 'Q':
- case 'q':
- { /* begin keys q and Q */
- exit(0);
- break;
- }/* end keys q and Q */
- case 'r':
- case 'R':
- { /* begin keys r and R */
- /* if a redraw occures, open the file again
- and call the yacc parser. then flush and
- discard all other events. this keeps multiple
- events from clogging the queue while a long
- plot is painting. */
- if((yyin=fopen(file_name,"r"))==NULL)
- { /* beign file error */
- fprintf(stderr,
- "Could not open file %s for read access.\n"
- ,file_name);
- exit(-1);
- } /* end file error */
-
- /* clear the display to get rid of any opbjects
- that are not drawn to proper scale if the window
- was resized */
- XClear(main_window);
-
- /* get window size information */
- XQueryWindow(main_window,window_info);
- minWidth = window_info[0].width;
- minHeight = window_info[0].height;
-
- /* select an appropriate font */
- get_text_font(user_font);
-
- if (text_font == 0)
- {/* begin get font error */
- fprintf(stderr,"Could not read font %s\n",
- font_name);
- exit(-1);
- }/* end get font error */
-
- /* get size of the font */
- XQueryFont(text_font,text_font_info);
- font_offset = text_font_info[0].height;
-
- initialize_plotter();
- yyparse();
- fclose(yyin);
- XFlush();
- XSync(DISCARD);
- break;
- } /* end keys r and R */
- default: break;
- } /* end key matching */
- } /* end key pressed */
- default:
- break;
- } /* end event switch */
- } /* end event handeling */
- } /* end main */
-