📄 xpsgraph.c
字号:
{ CurrentLineStyle = LineOnOffDash; } } if (Output == PSOnly || Output == XAndPS) { Win2User(LastWinX, LastWinY, &X, &Y); UserPSWin(X, Y, &WinX, &WinY); fprintf (PSFile, "%d %d M\n", WinX, WinY); }} voidSetLineWidth(Width)int Width;{ if (InitOK == FALSE || FramOK == FALSE) error ("Sorry, you must call Initplot and Framing before anything else"); if (Output == XOnly || Output == XAndPS) { XSetLineAttributes(display, GraphContext, Width, CurrentLineStyle, CapButt, JoinMiter); XSetLineAttributes(display, BackContext, Width, CurrentLineStyle, CapButt, JoinMiter); CurrentLineWidth = Width; } if (Output == PSOnly || Output == XAndPS) fprintf (PSFile, "stroke %d setlinewidth\n", Width*4-3);} void WriteFloat(Number, TotChar, DecChar)double Number;int TotChar, DecChar;{ int Xwin, Ywin, TextWidth; unsigned long returnval, CharHeight; Atom atom; char *format, *NewWord; if (InitOK == FALSE || FramOK == FALSE) error ("Sorry, you must call Initplot and Framing before anything else"); format = (char *) calloc (1, 20); NewWord = (char *) calloc (1, 64); strcat (format,"%"); sprintf (NewWord, "%d.%dlf", TotChar, DecChar); strcat (format, NewWord); sprintf (NewWord, format, Number); if (Output == XOnly || Output == XAndPS) { CheckEvent(); XGetFontProperty (CurrentFontStruct, XA_CAP_HEIGHT, &returnval); CharHeight = returnval; Ywin = LastWinY + CharHeight/2; Xwin = LastWinX; XDrawString (display, GraphWin, GraphContext, Xwin, Ywin, NewWord, strlen(NewWord)); XDrawString (display, Backup, GraphContext, Xwin, Ywin, NewWord, strlen(NewWord)); TextWidth = XTextWidth (CurrentFontStruct, NewWord,strlen(NewWord)); LastWinX = LastWinX+TextWidth; } if (Output == PSOnly || Output == XAndPS) fprintf (PSFile, "(%s) Lshow\n", NewWord); cfree (format); cfree(NewWord);}void WriteInt(Number, CharNum)int Number, CharNum;{ int Xwin, Ywin, TextWidth; unsigned long returnval, CharHeight; Atom atom; char *format, *NewWord; if (InitOK == FALSE || FramOK == FALSE) error ("Sorry, you must call Initplot and Framing before anything else"); format = (char *) calloc (1, 20); NewWord = (char *) calloc (1, 64); strcat (format, "%"); sprintf (NewWord, "%dd", CharNum); strcat (format, NewWord); sprintf (NewWord, format, Number); if (Output == XOnly || Output == XAndPS) { CheckEvent(); XGetFontProperty (CurrentFontStruct, XA_CAP_HEIGHT, &returnval); CharHeight = returnval; Ywin = LastWinY + CharHeight/2; Xwin = LastWinX; XDrawString (display, GraphWin, GraphContext, Xwin, Ywin, NewWord, strlen(NewWord)); XDrawString (display, Backup, GraphContext, Xwin, Ywin, NewWord, strlen(NewWord)); TextWidth = XTextWidth (CurrentFontStruct, NewWord,strlen(NewWord)); LastWinX = LastWinX+TextWidth; } if (Output == PSOnly || Output == XAndPS) fprintf (PSFile, "(%s) Lshow\n", NewWord); cfree (format); cfree(NewWord);}char *GetDirName (){ char *DirName, *HomeName, LocalName; int i, Len; i = 0; HomeName = NULL; while ((environ[i] != NULL) && (strncmp("XPSDIR=", environ[i],7) != 0)) { if (strncmp("HOME=", environ[i],5) == 0) HomeName = environ[i]+5; i++; } if (environ[i] == NULL) { if (HomeName != NULL) { Len = strlen(HomeName)+4; DirName = (char *) calloc(1, Len); DirName = strcpy(DirName, HomeName); strcat(DirName, "/XPS"); } else DirName = "."; } else DirName = environ[i]+7; return (DirName);} #define BUFSIZE 1024void InitPSFile(PSFileName, width,height)unsigned int width, height;char *PSFileName;{ double XSize, YSize; char *HdrFileName, buf[BUFSIZE], *DirName; int HdrFile, n, PSInt; WinWidth = width; WinHeight = height; PSInt = creat (PSFileName, 0644); DirName= GetDirName (); HdrFileName = (char *) calloc(1, 100); HdrFileName = strcpy (HdrFileName, DirName); strcat (HdrFileName , "/prelude"); if ((HdrFile = open (HdrFileName, 0)) == -1) { printf ("Error: file %s not found\n", HdrFileName); printf ("Use setenv XPSDIR to specify directory\n"); printf ("containing file \'prelude\'.\n"); exit(1); } while ((n=read(HdrFile, buf, BUFSIZE))>0) if (write (PSInt, buf, n) != n) { printf ("Error in copying prelude to psfile!\n"); exit(1); } close (PSInt); PSFile = fopen (PSFileName, "a"); XSize = width/9000.0; YSize = height/9000.0; if (width>height) { PSWidth = 6400; PSHeight = 6400*height/width; Scale = 6400.0/width; fprintf(PSFile, "%lf %lf scale\n", XSize, XSize); } else { PSWidth = 6400*width/height; PSHeight = 6400; Scale = 6400.0/height; fprintf(PSFile, "%lf %lf scale\n", YSize, YSize); } fprintf(PSFile, "90 rotate\n0 %d translate\n0 setgray\n", -PSHeight); fprintf(PSFile, "/Helvetica findfont 140 scalefont setfont\n"); fprintf(PSFile, "0 0 M\n 0 %d L\n %d %d L\n %d 0 L\n CLP\n clip\n", PSHeight, PSWidth, PSHeight, PSWidth); fprintf(PSFile, "1 setlinewidth\n"); fprintf(PSFile, "newpath\nLT0\n"); PathLength = 0; cfree (HdrFileName);}voidPutStartButton(){ if (Output == XOnly || Output == XAndPS) { StartOK = TRUE; StartButton = XCreateSimpleWindow (display, GraphWin, (int) (WinWidth/60), (int) (WinHeight/14), 40, 20, 2, BlackPixel (display, screen), WhitePixel (display, screen)); StartContext = XCreateGC(display, StartButton, 0, NULL); XSetForeground (display, StartContext, BlackPixel (display, screen)); XDrawString (display, StartButton, GraphContext, 5,12,"start",5); XGrabButton (display, AnyButton, AnyModifier, StartButton, 0, ButtonPressMask, GrabModeAsync, GrabModeAsync, StartButton, None); while (report.type != ButtonPress) { XNextEvent (display, &report); if (report.type == Expose) { MapStartButton(); MapStopButton(); } if ((report.xbutton.window)!=StartButton) report.type = Expose; } }}voidPutStopButton(){ if (Output == XOnly || Output == XAndPS) { StopOK = TRUE; StopButton = XCreateSimpleWindow (display, GraphWin, (int) (WinWidth/60), (int) (WinHeight/28), 40, 20, 2, BlackPixel (display, screen), WhitePixel (display, screen)); StopContext = XCreateGC(display, StopButton, 0, NULL); XSetForeground (display, StopContext, BlackPixel (display, screen)); XDrawString (display, StopButton, GraphContext, 5,12,"stop",5); XGrabButton (display, AnyButton, AnyModifier, StopButton, 0, ButtonPressMask|ButtonReleaseMask, GrabModeAsync, GrabModeAsync, StopButton, None); MapStopButton(); }}voidPutContButton(){ if (Output == XOnly || Output == XAndPS) { StopOK = TRUE; ContButton = XCreateSimpleWindow (display, GraphWin, (int) (WinWidth/60), (int) (WinHeight/7), 40, 20, 2, BlackPixel (display, screen), WhitePixel (display, screen)); ContContext = XCreateGC(display, ContButton, 0, NULL); XSetForeground (display, ContContext, BlackPixel (display, screen)); XDrawString (display, StopButton, GraphContext, 5,12,"cont",5); XGrabButton (display, AnyButton, AnyModifier, ContButton, 0, ButtonPressMask|ButtonReleaseMask, GrabModeAsync, GrabModeAsync, ContButton, None);/* MapContButton(); */ }}voidGetInfo(redmax, greenmax, bluemax, redmul, greenmul, bluemul, base)unsigned long *redmax, *greenmax, *bluemax, *redmul, *greenmul, *bluemul, *base;{ mapinfo = (XStandardColormap *) malloc(sizeof(XStandardColormap)); mapinfo->red_max = 7; mapinfo->green_max = 7; mapinfo->blue_max = 3; mapinfo->red_mult = 32; mapinfo->green_mult = 4; mapinfo->blue_mult = 1; mapinfo->base_pixel = 0; *redmax = mapinfo->red_max; *greenmax = mapinfo->green_max; *bluemax = mapinfo->blue_max; *redmul = mapinfo->red_mult; *greenmul = mapinfo->green_mult; *bluemul = mapinfo->blue_mult; *base = mapinfo->base_pixel;}voidcreate_rgb_colormap(disp) Display *disp;{ int i, j, k, maps, base; XColor *exact; Visual *viz; Window win;/*** Check if the RGB_BEST_MAP resource has already been defined*/ disp = XOpenDisplay(NULL); win = RootWindow(disp, 0); viz = DefaultVisual(disp, 0);/*** If XGetRGBColormaps returns non-zero then the colormap** is returned in mapinfo->colormap.** This can then be used as detailed in the previous example.*/ if (XGetRGBColormaps(disp, win, &mapinfo, &maps, XA_RGB_BEST_MAP)) { printf("XA_RGB_BEST_MAP is already defined \n"); XCloseDisplay(disp); return; }/*** Allocate the XStandardColormap structure and create the Colormap*/ mapinfo = (XStandardColormap *) malloc(sizeof(XStandardColormap)); mapinfo->colormap = XCreateColormap(disp, win, viz, AllocNone); /*** The following values are specific to the user's needs*/ mapinfo->red_max = 7; mapinfo->green_max = 7; mapinfo->blue_max = 3; mapinfo->red_mult = 32; mapinfo->green_mult = 4; mapinfo->blue_mult = 1; mapinfo->base_pixel = 0;/*** These are necessary.*/ mapinfo->visualid = XVisualIDFromVisual(viz); mapinfo->killid = (XID) mapinfo->colormap;/*** Now we create the RGB colour cube.*/ exact = (XColor *) calloc(sizeof(XColor), 256); base = mapinfo->base_pixel; for (i = 0; i < mapinfo->red_max + 1; i++) { for (j = 0; j < mapinfo->green_max + 1; j++) { for (k = 0; k < mapinfo->blue_max + 1; k++) { exact[base].blue = 65535 * k / mapinfo->blue_max; exact[base].green = 65535 * j/mapinfo->green_max; exact[base].red = 65535 * i / mapinfo->red_max; exact[base].flags = DoRed | DoGreen | DoBlue; exact[base].pixel = base++; } } }/*** Transfer the RGB values to the colormap*/ XStoreColors(disp, mapinfo->colormap, exact, 256);/*** Tell the server to use this colormap as the RGB BEST MAP*/ XSetRGBColormaps(disp, win, mapinfo, 1, XA_RGB_BEST_MAP);/*** Mark this clients resources as permanent, then return.*/ XSetCloseDownMode(disp, RetainPermanent); XCloseDisplay(disp); return;}void InitPlot(ColorName, width, height, PSFileName, OutPar)char *ColorName, *PSFileName;unsigned int width, height, OutPar;{ char *disp_name, *FirstName; XColor Kleur, KleurExact; unsigned long ColorPix; BackColName = (char *) calloc(1, strlen(ColorName)); BackColName = ColorName; Output = OutPar; FramOK = StopOK = StartOK = FALSE; InitOK = TRUE; if (Output != PSOnly && Output != XOnly && Output != XAndPS) error("Sorry, Output as argument to Initplot must be 0, 1 or 2"); if (Output == PSOnly || Output == XAndPS) InitPSFile(PSFileName, width, height); CurrentLineWidth = 1; CurrentLineStyle = LineSolid; CurrentDashLength = 5; if (Output == XOnly || Output == XAndPS) { disp_name = NULL;/* create_rgb_colormap(display); */ display = XOpenDisplay (disp_name); if (display == NULL) { printf ("Environment variable DISPLAY is not set. \n"); printf ("Set this variable to hostname:display_number \n"); printf ("where hostname is the name of the machine running \n"); printf ("the X-server, and the display-number is usually equal \n"); printf ("to 0.\n"); printf ("Do not forget to add the current machine to the xhost-list!\n"); exit(1); } screen = DefaultScreen (display); root = RootWindow (display, screen); InstallFastColors(); XAllocNamedColor (display,XDefaultColormap(display,screen),ColorName, &Kleur, &KleurExact); ColorPix = Kleur.pixel; GraphWin = XCreateSimpleWindow (display, root, 0, 0, width, height, 2, BlackPixel (display, screen), ColorPix); XGetGeometry (display, GraphWin, &root, &WinULX, &WinULY, &WinWidth, &WinHeight, &BorderWidth, &Dep); Backup = XCreatePixmap (display, GraphWin, width, height,Dep); XStoreName (display, GraphWin, "Graphics Application"); GraphContext = XCreateGC(display, GraphWin, 0, NULL); BackContext = XCreateGC(display, Backup, 0, NULL); XSetForeground (display, GraphContext, BlackPixel (display, screen)); XSetForeground (display, BackContext, BlackPixel (display, screen)); XSetBackground (display, BackContext, ColorPix); XMapRaised (display, GraphWin); XUngrabButton (display, AnyButton, AnyModifier, GraphWin); XSelectInput (display, GraphWin, ExposureMask|ButtonPressMask|ButtonReleaseMask); SetNamedBackground(ColorName); XGetGeometry (display, GraphWin, &root, &WinULX, &WinULY, &WinWidth, &WinHeight, &BorderWidth, &Dep); Kleurtjes = XDefaultColormap(display, screen); CurrentFontStruct = NULL; FirstName = "-adobe-times-medium-r-normal--24-240-75-75-p-124-iso8859-1"; CurrentFontStruct=XLoadQueryFont(display, FirstName); }}voidEndPlot(){ if (Output == PSOnly || Output == XAndPS) { fprintf (PSFile, "stroke\ngrestore\nend\nshowpage\n"); fclose (PSFile); } if (Output == PSOnly) exit(1); if (Output == XOnly || Output == XAndPS) { if (StopOK == FALSE) exit(1); while(1) { XNextEvent (display, &report); switch (report.type) { case (Expose) : StartRedraw(); XGetGeometry (display, GraphWin, &root, &WinULX, &WinULY, &WinWidth, &WinHeight, &BorderWidth, &Dep); XClearWindow (display, GraphWin); Redraw (); break; case ButtonPress : if (report.xbutton.window == StopButton) WaitRelease(); } } }}voidNextPage(){ if (Output == PSOnly || Output == XAndPS) { fprintf (PSFile, "stroke\ngsave\nshowpage\ngrestore\n"); /* fclose (PSFile); */ }/* if (Output == PSOnly) exit(1); */ if (Output == XOnly || Output == XAndPS) { SetNamedBackground("white"); }}#include <fortint.h>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -