📄 graphic.c
字号:
} if (we > winwidth - xe) we = winwidth - xe; if (he > winheight - ye) he = winheight - ye; if (!readrgb (bitstring + n, &red, &green, &blue)) return;#ifdef UNIX if (xe + we > 0 && ye + he > 0) { XGetGCValues (display, gc, GCPlaneMask, &vals); bits = XGetImage (display, backbit, xe, ye, we, he, vals.plane_mask, XYPixmap); if (!bits) { error (ERROR, "Couldn't get bits from window"); return; } }#else /* */ startdraw (FALSE);#endif /* */ for (y = 0; y < h; y++) { for (x = 0; x < w; x++) { if (!readrgb (NULL, &red, &green, &blue)) { error (ERROR, "Invalud bitmap"); return; } should_pixel = rgb_to_pixel (red, green, blue); if (x + xdest >= xe && x + xdest < xe + we && y + ydest >= ye && y + ydest < ye + he) { if (m == 't' && backpixel == should_pixel) { /* do nothing */ } else {#ifdef UNIX XPutPixel (bits, x - xe + xdest, y - ye + ydest, should_pixel);#else SetPixelV (bitcon, xdest + x, ydest + y, should_pixel);#endif } } } }#ifdef UNIX if (bits) { XPutImage (display, window, gc, bits, 0, 0, xe, ye, we, he); XPutImage (display, backbit, gc, bits, 0, 0, xe, ye, we, he); XFlush (display); XDestroyImage (bits); }#else BitBlt (devcon, xe, ye, we, he, bitcon, xe, ye, SRCCOPY); enddraw ();#endif return;}char *getbit (int x1, int y1, int x2, int y2){ /* get rect from win */ int s, x, y, pixel; int xe1, ye1, xe2, ye2; char *bitstring;#ifdef UNIX XImage *bits = NULL; XGCValues vals; int red, green, blue;#endif if (!winopened) { error (ERROR, "Got no window to draw"); return my_strdup (""); } itransform (&x1, &y1); itransform (&x2, &y2); if (x2 < x1) { s = x1; x1 = x2; x2 = s; } if (y2 < y1) { s = y1; y1 = y2; y2 = s; } xe1 = x1; ye1 = y1; xe2 = x2; ye2 = y2; if (xe1 < 0) xe1 = 0; if (ye1 < 0) ye1 = 0; if (xe2 >= winwidth) xe2 = winwidth - 1; if (ye2 >= winheight) ye2 = winheight - 1;#ifdef UNIX if (xe1 <= xe2 && ye1 <= ye2) { XGetGCValues (display, gc, GCPlaneMask, &vals); bits = XGetImage (display, backbit, xe1, ye1, xe2 - xe1 + 1, ye2 - ye1 + 1, vals.plane_mask, XYPixmap); if (!bits) { error (ERROR, "Couldn't get bits from window"); return my_strdup (""); } } bitstring = newrgb (x2 - x1 + 1, y2 - y1 + 1); for (y = y1; y <= y2; y++) { for (x = x1; x <= x2; x++) { if (x >= xe1 && x <= xe2 && y >= ye1 && y <= ye2) { pixel = XGetPixel (bits, x - xe1, y - ye1); } else { pixel = backpixel; } pixel_to_rgb (&red, &green, &blue, pixel); addrgb (bitstring, red, green, blue); } } if (bits) XDestroyImage (bits);#else /* */ startdraw (FALSE); bitstring = newrgb (x2 - x1 + 1, y2 - y1 + 1); for (y = y1; y <= y2; y++) { for (x = x1; x <= x2; x++) { if (x >= xe1 && x <= xe2 && y >= ye1 && y <= ye2) pixel = GetPixel (bitcon, x, y); else pixel = backpixel; addrgb (bitstring, GetRValue (pixel), GetGValue (pixel), GetBValue (pixel)); } } enddraw ();#endif return bitstring;}static char *newrgb (int w, int h){ /* create a new bitmap string */ char *bits; sprintf (string, "rgb %d,%d:", w, h); bits = my_malloc (strlen (string) + ((w * h) * 6) + 2); strcpy (bits, string); bitcount = 0; return bits;}static voidaddrgb (char *bits, unsigned short red, unsigned short green, unsigned short blue){ /* add one rgb pixel to bitstring */ static char *pbits; if (bitcount == 0) pbits = bits + strlen (bits); bitcount++; sprintf (pbits, "%02x%02x%02x\0", red, green, blue); pbits += 6;}static intreadrgb (char *bits, unsigned short *red, unsigned short *green, unsigned short *blue){ /* read rgb bit from string one after another */ static char *bitpt; int r, g, b; if (bits) { bitpt = bits; return 1; } if (sscanf (bitpt, "%02x%02x%02x", &r, &g, &b) == 3) { *red = r; *green = g; *blue = b; bitpt += 6; return 1; } return 0;}voidcreate_openprinter (int num){ /* create command 'openprinter' */ struct command *cmd; cmd = add_command (cOPENPRN, FALSE); cmd->args = num;}voidopenprinter (struct command *cmd){ /* opens a printer */#ifdef WINDOWS char PrinterName[200]; /* Name of default Printer */ char *p; /* points into PrinterName */ static int first = TRUE; DOCINFO di; float width, height, prnscalex, prnscaley; LOGBRUSH mybrush; RECT interior;#endif /* close file, if already open */#ifdef UNIX if (printerfile) closeprinter ();#endif if (cmd->args == 1) { printerfilename = my_strdup ((char *) (pop (stSTRING)->pointer)); print_to_file = TRUE; } else { printerfilename = my_strdup ("\0"); print_to_file = FALSE; }#ifdef UNIX if (*printerfilename == '\0') {#ifdef HAVE_MKSTEMP int fd; my_free (printerfilename); printerfilename = my_strdup ("/tmp/yabasic-postscript-output-XXXXXX"); fd = mkstemp (printerfilename); if (fd > 0) printerfile = fdopen (fd, "w"); else printerfile = NULL;#else struct stat s; my_free (printerfilename); printerfilename = my_strdup ("/tmp/yabasic.ps"); if (stat (printerfilename, &s) && errno != ENOENT) { sprintf (string, "could not check printerfile '%s': %s", printerfilename, my_strerror (errno)); error (ERROR, string); return; } if (s.st_mode & S_IFLNK) { sprintf (string, "could not print to file '%s'; it is a symbolic link"); error (ERROR, string); return; } printerfile = fopen (printerfilename, "w");#endif deleteprinterfile = TRUE; } else { printerfile = fopen (printerfilename, "w"); deleteprinterfile = FALSE; } if (!printerfile) { sprintf (string, "could not open file '%s' for printing: %s", printerfilename, my_strerror (errno)); error (ERROR, string); }#endif#ifdef WINDOWS if (first) { /* query win.ini for default printer */ GetProfileString ("windows", "device", ",,,", PrinterName, 200); /* truncate printer name */ p = PrinterName; while (*p && *p != ',') p++; *p = '\0'; printer = CreateDC (NULL, PrinterName, NULL, NULL); if (!printer) printer = CreateDC (NULL, "winspool", NULL, NULL); if (!printer) { error (ERROR, "Couldn't get handle for printer"); return; } /* calculate scaling-factors */ width = (float) GetDeviceCaps (printer, PHYSICALWIDTH); prnoffx = (float) GetDeviceCaps (printer, PHYSICALOFFSETX); prnscalex = (float) (width - 4 * prnoffx) / winwidth; height = (float) GetDeviceCaps (printer, PHYSICALHEIGHT); prnoffy = (float) GetDeviceCaps (printer, PHYSICALOFFSETY); prnscaley = (float) (height - 4 * prnoffy) / winheight; prnscale = (prnscalex < prnscaley) ? prnscalex : prnscaley; /* create printerpens */ mybrush.lbStyle = BS_SOLID; mybrush.lbColor = RGB (255, 255, 255); mybrush.lbHatch = HS_DIAGCROSS; revprinterpen = ExtCreatePen (PS_GEOMETRIC, (long) prnscale, &mybrush, 0, NULL); mybrush.lbStyle = BS_SOLID; mybrush.lbColor = RGB (0, 0, 0); mybrush.lbHatch = HS_DIAGCROSS; printerpen = ExtCreatePen (PS_GEOMETRIC, (long) prnscale, &mybrush, 0, NULL); /* set clipping region */ GetClientRect (window, &interior); SelectClipRgn (printer, NULL); IntersectClipRect (printer, (int) (interior.left * prnscalex + prnoffx), (int) (interior.top * prnscaley + prnoffy), (int) (interior.right * prnscalex + prnoffx), (int) (interior.bottom * prnscaley + prnoffy)); /* create printerfont */ logfont.lfHeight = (long) (-fontheight * prnscale); printerfont = CreateFontIndirect (&logfont); if (printerfont == NULL) { sprintf (string, "Could not create font for printer"); error (ERROR, string); return; } if (!SelectObject (printer, printerfont)) error (ERROR, "could not select printerfont"); } di.cbSize = sizeof (DOCINFO); di.lpszDocName = "yabasic grafics"; di.lpszOutput = (print_to_file) ? printerfilename : (LPTSTR) NULL; di.lpszDatatype = (LPTSTR) NULL; di.fwType = 0; if (StartDoc (printer, &di) == SP_ERROR) { error (ERROR, "Couldn't start printing"); return; } StartPage (printer); first = FALSE;#else /* UNIX */ fprintf (printerfile, "%%!PS-Adobe-1.0\n"); fprintf (printerfile, "%%%%Title: %s grafic\n", progname); fprintf (printerfile, "%%%%BoundingBox: 0 0 %i %i\n", (int) (winwidth * psscale), (int) (winheight * psscale)); fprintf (printerfile, "%%%%DocumentFonts: Helvetica\n"); fprintf (printerfile, "%%%%Creator: yabasic\n"); fprintf (printerfile, "%%%%Pages: (atend)\n"); fprintf (printerfile, "%%%%EndComments\n"); fprintf (printerfile, "gsave\n"); fprintf (printerfile, "/txt 4 dict def\n"); fprintf (printerfile, "/rec 6 dict def\n"); fprintf (printerfile, "/tri 8 dict def\n"); fprintf (printerfile, "/cir 5 dict def\n"); fprintf (printerfile, "/rgb 3 dict def\n"); fprintf (printerfile, "rgb /r 0 put\n"); fprintf (printerfile, "rgb /g 0 put\n"); fprintf (printerfile, "rgb /b 0 put\n"); fprintf (printerfile, "0 setgray\n"); fprintf (printerfile, "/M {moveto} def\n"); fprintf (printerfile, "/RL {rlineto} def\n"); fprintf (printerfile, "/L {lineto} def\n"); fprintf (printerfile, "/N {newpath} def\n"); fprintf (printerfile, "/S {stroke} def\n"); fprintf (printerfile, "/CLYN {(y) eq {1 setgray} {rgb /r get rgb /g get rgb /b get setrgbcolor} ifelse} def\n"); fprintf (printerfile, "/DO {CLYN N M 0 %g RL %g 0 RL 0 %g RL" " closepath fill (n) CLYN} def\n", psscale, psscale, -psscale); fprintf (printerfile, "/LI {CLYN N M L stroke (n) CLYN} def\n"); fprintf (printerfile, "/CI {mark 6 1 roll cir /fi 3 -1 roll put\n"); fprintf (printerfile, " cir /cl 3 -1 roll put\n"); fprintf (printerfile, " cir /r 3 -1 roll put\n"); fprintf (printerfile, " cir /x 3 -1 roll put\n"); fprintf (printerfile, " cir /y 3 -1 roll put\n"); fprintf (printerfile, " cir /cl get (y) CLYN\n"); fprintf (printerfile, " N cir /x get cir /y get cir /r get 0 360 arc\n"); fprintf (printerfile, " closepath cir /fi get (y) eq {fill} {stroke} ifelse\n"); fprintf (printerfile, " (n) CLYN cleartomark} def\n"); fprintf (printerfile, "/AT {mark 5 1 roll txt /txt 3 -1 roll put\n"); fprintf (printerfile, " txt /xa 3 -1 roll put\n"); fprintf (printerfile, " txt /y 3 -1 roll put\n"); fprintf (printerfile, " txt /x 3 -1 roll put\n"); fprintf (printerfile, " N txt /x get txt /y get M\n"); fprintf (printerfile, " txt /txt get false charpath flattenpath pathbbox\n"); fprintf (printerfile, " pop exch pop exch sub\n"); fprintf (printerfile, " txt /x get exch\n"); fprintf (printerfile, " txt /xa get (c) eq {2 div sub} if\n"); fprintf (printerfile, " txt /xa get (l) eq {pop} if\n"); fprintf (printerfile, " txt /xa get (r) eq {sub} if\n"); fprintf (printerfile, " txt /y get M\n"); fprintf (printerfile, " txt /txt get show cleartomark\n"); fprintf (printerfile, " } def\n"); fprintf (printerfile, "/RE {mark 7 1 roll rec /fi 3 -1 roll put\n"); fprintf (printerfile, " rec /cl 3 -1 roll put\n"); fprintf (printerfile, " rec /y2 3 -1 roll put\n"); fprintf (printerfile, " rec /x2 3 -1 roll put\n"); fprintf (printerfile, " rec /y1 3 -1 roll put\n"); fprintf (printerfile, " rec /x1 3 -1 roll put\n"); fprintf (printerfile, " rec /cl get CLYN\n"); fprintf (printerfile, " N rec /x1 get rec /y1 get M\n"); fprintf (printerfile, " rec /x1 get rec /y2 get L\n"); fprintf (printerfile, " rec /x2 get rec /y2 get L\n"); fprintf (printerfile, " rec /x2 get rec /y1 get L\n"); fprintf (printerfile, " rec /x1 get rec /y1 get L\n"); fprintf (printerfile, " closepath rec /fi get (y) eq {fill} {stroke} ifelse\n"); fprintf (printerfile, " (n) CLYN cleartomark} def\n"); fprintf (printerfile, "/TRI {mark 9 1 roll tri /fi 3 -1 roll put\n"); fprintf (printerfile, " tri /cl 3 -1 roll put\n"); fprintf (printerfile, " tri /y3 3 -1 roll put\n"); fprintf (printerfile, " tri /x3 3 -1 roll put\n"); fprintf (printerfile, " tri /y2 3 -1 roll put\n"); fprintf (printerfile, " tri /x2 3 -1 roll put\n"); fprintf (printerfile, " tri /y1 3 -1 roll put\n"); fprintf (printerfile, " tri /x1 3 -1 roll put\n"); fprintf (printerfile, " tri /cl get CLYN\n"); fprintf (printerfile, " N tri /x1 get tri /y1 get M\n"); fprintf (printerfile, " tri /x2 get tri /y2 get L\n"); fprintf (printerfile, " tri /x3 get tri /y3 get L\n"); fprintf (printerfile, " closepath tri /fi get (y) eq {fill} {stroke} ifelse\n"); fprintf (printerfile, " (n) CLYN cleartomark} def\n"); fprintf (printerfile, "30 30 translate\n"); fprintf (printerfile, "%g setlinewidth\n", psscale); firsttext = TRUE; /* font will be set in text-command */ fflush (printerfile);#endif}voidcloseprinter (){ /* close printer */#ifdef WINDOWS EndPage (printer); EndDoc (printer);#else /* UNIX */ if (printerfile) { fprintf (printerfile, "showpage\ngrestore\n%%%%Trailer\n"); fclose (printerfile); printerfile = NULL; if (deleteprinterfile) { deleteprinterfile = FALSE; sprintf (string, "lpr %s", printerfilename); if (system (string)) { sprintf (string, "couldn't print '%s'", printerfilename); error (ERROR, string); return; } remove (printerfilename); } }#endif if (printerfilename) { my_free (printerfilename); printerfilename = NULL; } print_to_file = FALSE;}#ifdef UNIXvoidgetwinkey (char *retkey){ /* read a key from grafics window */ static XEvent event; /* what has happened ? */ KeySym sym; /* keysmbol */ int yk, len, x, y; close (ConnectionNumber (display)); display = XOpenDisplay (displayname); XSelectInput (display, window, KeyPressMask | B
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -