📄 tkcanvps.c
字号:
int result; Tk_Item *itemPtr;#define STRING_LENGTH 400 char string[STRING_LENGTH+1], *p; time_t now; size_t length; int deltaX = 0, deltaY = 0; /* Offset of lower-left corner of * area to be marked up, measured * in canvas units from the positioning * point on the page (reflects * anchor position). Initial values * needed only to stop compiler * warnings. */ Tcl_HashSearch search; Tcl_HashEntry *hPtr; Tcl_DString buffer; CONST char * CONST *chunk; /* *---------------------------------------------------------------- * Initialize the data structure describing Postscript generation, * then process all the arguments to fill the data structure in. *---------------------------------------------------------------- */ oldInfoPtr = canvasPtr->psInfoPtr; canvasPtr->psInfoPtr = &psInfo; psInfo.x = canvasPtr->xOrigin; psInfo.y = canvasPtr->yOrigin; psInfo.width = -1; psInfo.height = -1; psInfo.pageXString = NULL; psInfo.pageYString = NULL; psInfo.pageX = 72*4.25; psInfo.pageY = 72*5.5; psInfo.pageWidthString = NULL; psInfo.pageHeightString = NULL; psInfo.scale = 1.0; psInfo.pageAnchor = TK_ANCHOR_CENTER; psInfo.rotate = 0; psInfo.fontVar = NULL; psInfo.colorVar = NULL; psInfo.colorMode = NULL; psInfo.colorLevel = 0; psInfo.fileName = NULL; psInfo.channelName = NULL; psInfo.chan = NULL; psInfo.prepass = 0; Tcl_InitHashTable(&psInfo.fontTable, TCL_STRING_KEYS); result = Tk_ConfigureWidget(canvasPtr->interp, canvasPtr->tkwin, configSpecs, argc-2, argv+2, (char *) &psInfo, TK_CONFIG_ARGV_ONLY); if (result != TCL_OK) { goto cleanup; } if (psInfo.width == -1) { psInfo.width = Tk_Width(canvasPtr->tkwin); } if (psInfo.height == -1) { psInfo.height = Tk_Height(canvasPtr->tkwin); } psInfo.x2 = psInfo.x + psInfo.width; psInfo.y2 = psInfo.y + psInfo.height; if (psInfo.pageXString != NULL) { if (GetPostscriptPoints(canvasPtr->interp, psInfo.pageXString, &psInfo.pageX) != TCL_OK) { goto cleanup; } } if (psInfo.pageYString != NULL) { if (GetPostscriptPoints(canvasPtr->interp, psInfo.pageYString, &psInfo.pageY) != TCL_OK) { goto cleanup; } } if (psInfo.pageWidthString != NULL) { if (GetPostscriptPoints(canvasPtr->interp, psInfo.pageWidthString, &psInfo.scale) != TCL_OK) { goto cleanup; } psInfo.scale /= psInfo.width; } else if (psInfo.pageHeightString != NULL) { if (GetPostscriptPoints(canvasPtr->interp, psInfo.pageHeightString, &psInfo.scale) != TCL_OK) { goto cleanup; } psInfo.scale /= psInfo.height; } else { psInfo.scale = (72.0/25.4)*WidthMMOfScreen(Tk_Screen(canvasPtr->tkwin)); psInfo.scale /= WidthOfScreen(Tk_Screen(canvasPtr->tkwin)); } switch (psInfo.pageAnchor) { case TK_ANCHOR_NW: case TK_ANCHOR_W: case TK_ANCHOR_SW: deltaX = 0; break; case TK_ANCHOR_N: case TK_ANCHOR_CENTER: case TK_ANCHOR_S: deltaX = -psInfo.width/2; break; case TK_ANCHOR_NE: case TK_ANCHOR_E: case TK_ANCHOR_SE: deltaX = -psInfo.width; break; } switch (psInfo.pageAnchor) { case TK_ANCHOR_NW: case TK_ANCHOR_N: case TK_ANCHOR_NE: deltaY = - psInfo.height; break; case TK_ANCHOR_W: case TK_ANCHOR_CENTER: case TK_ANCHOR_E: deltaY = -psInfo.height/2; break; case TK_ANCHOR_SW: case TK_ANCHOR_S: case TK_ANCHOR_SE: deltaY = 0; break; } if (psInfo.colorMode == NULL) { psInfo.colorLevel = 2; } else { length = strlen(psInfo.colorMode); if (strncmp(psInfo.colorMode, "monochrome", length) == 0) { psInfo.colorLevel = 0; } else if (strncmp(psInfo.colorMode, "gray", length) == 0) { psInfo.colorLevel = 1; } else if (strncmp(psInfo.colorMode, "color", length) == 0) { psInfo.colorLevel = 2; } else { Tcl_AppendResult(canvasPtr->interp, "bad color mode \"", psInfo.colorMode, "\": must be monochrome, ", "gray, or color", (char *) NULL); goto cleanup; } } if (psInfo.fileName != NULL) { /* * Check that -file and -channel are not both specified. */ if (psInfo.channelName != NULL) { Tcl_AppendResult(canvasPtr->interp, "can't specify both -file", " and -channel", (char *) NULL); result = TCL_ERROR; goto cleanup; } /* * Check that we are not in a safe interpreter. If we are, disallow * the -file specification. */ if (Tcl_IsSafe(canvasPtr->interp)) { Tcl_AppendResult(canvasPtr->interp, "can't specify -file in a", " safe interpreter", (char *) NULL); result = TCL_ERROR; goto cleanup; } p = Tcl_TranslateFileName(canvasPtr->interp, psInfo.fileName, &buffer); if (p == NULL) { goto cleanup; } psInfo.chan = Tcl_OpenFileChannel(canvasPtr->interp, p, "w", 0666); Tcl_DStringFree(&buffer); if (psInfo.chan == NULL) { goto cleanup; } } if (psInfo.channelName != NULL) { int mode; /* * Check that the channel is found in this interpreter and that it * is open for writing. */ psInfo.chan = Tcl_GetChannel(canvasPtr->interp, psInfo.channelName, &mode); if (psInfo.chan == (Tcl_Channel) NULL) { result = TCL_ERROR; goto cleanup; } if ((mode & TCL_WRITABLE) == 0) { Tcl_AppendResult(canvasPtr->interp, "channel \"", psInfo.channelName, "\" wasn't opened for writing", (char *) NULL); result = TCL_ERROR; goto cleanup; } } /* *-------------------------------------------------------- * Make a pre-pass over all of the items, generating Postscript * and then throwing it away. The purpose of this pass is just * to collect information about all the fonts in use, so that * we can output font information in the proper form required * by the Document Structuring Conventions. *-------------------------------------------------------- */ psInfo.prepass = 1; for (itemPtr = canvasPtr->firstItemPtr; itemPtr != NULL; itemPtr = itemPtr->nextPtr) { if ((itemPtr->x1 >= psInfo.x2) || (itemPtr->x2 < psInfo.x) || (itemPtr->y1 >= psInfo.y2) || (itemPtr->y2 < psInfo.y)) { continue; } if (itemPtr->typePtr->postscriptProc == NULL) { continue; } result = (*itemPtr->typePtr->postscriptProc)(canvasPtr->interp, (Tk_Canvas) canvasPtr, itemPtr, 1); Tcl_ResetResult(canvasPtr->interp); if (result != TCL_OK) { /* * An error just occurred. Just skip out of this loop. * There's no need to report the error now; it can be * reported later (errors can happen later that don't * happen now, so we still have to check for errors later * anyway). */ break; } } psInfo.prepass = 0; /* *-------------------------------------------------------- * Generate the header and prolog for the Postscript. *-------------------------------------------------------- */ Tcl_AppendResult(canvasPtr->interp, "%!PS-Adobe-3.0 EPSF-3.0\n", "%%Creator: Tk Canvas Widget\n", (char *) NULL);#if !(defined(__WIN32__) || defined(MAC_TCL)) if (!Tcl_IsSafe(interp)) { struct passwd *pwPtr = getpwuid(getuid()); Tcl_AppendResult(canvasPtr->interp, "%%For: ", (pwPtr != NULL) ? pwPtr->pw_gecos : "Unknown", "\n", (char *) NULL); endpwent(); }#endif /* __WIN32__ || MAC_TCL */ Tcl_AppendResult(canvasPtr->interp, "%%Title: Window ", Tk_PathName(canvasPtr->tkwin), "\n", (char *) NULL); time(&now); Tcl_AppendResult(canvasPtr->interp, "%%CreationDate: ", ctime(&now), (char *) NULL); if (!psInfo.rotate) { sprintf(string, "%d %d %d %d", (int) (psInfo.pageX + psInfo.scale*deltaX), (int) (psInfo.pageY + psInfo.scale*deltaY), (int) (psInfo.pageX + psInfo.scale*(deltaX + psInfo.width) + 1.0), (int) (psInfo.pageY + psInfo.scale*(deltaY + psInfo.height) + 1.0)); } else { sprintf(string, "%d %d %d %d", (int) (psInfo.pageX - psInfo.scale*(deltaY + psInfo.height)), (int) (psInfo.pageY + psInfo.scale*deltaX), (int) (psInfo.pageX - psInfo.scale*deltaY + 1.0), (int) (psInfo.pageY + psInfo.scale*(deltaX + psInfo.width) + 1.0)); } Tcl_AppendResult(canvasPtr->interp, "%%BoundingBox: ", string, "\n", (char *) NULL); Tcl_AppendResult(canvasPtr->interp, "%%Pages: 1\n", "%%DocumentData: Clean7Bit\n", (char *) NULL); Tcl_AppendResult(canvasPtr->interp, "%%Orientation: ", psInfo.rotate ? "Landscape\n" : "Portrait\n", (char *) NULL); p = "%%DocumentNeededResources: font "; for (hPtr = Tcl_FirstHashEntry(&psInfo.fontTable, &search); hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) { Tcl_AppendResult(canvasPtr->interp, p, Tcl_GetHashKey(&psInfo.fontTable, hPtr), "\n", (char *) NULL); p = "%%+ font "; } Tcl_AppendResult(canvasPtr->interp, "%%EndComments\n\n", (char *) NULL); /* * Insert the prolog */ for (chunk=prolog; *chunk; chunk++) { Tcl_AppendResult(interp, *chunk, (char *) NULL); } if (psInfo.chan != NULL) { Tcl_Write(psInfo.chan, canvasPtr->interp->result, -1); Tcl_ResetResult(canvasPtr->interp); } /* *----------------------------------------------------------- * Document setup: set the color level and include fonts. *----------------------------------------------------------- */ sprintf(string, "/CL %d def\n", psInfo.colorLevel); Tcl_AppendResult(canvasPtr->interp, "%%BeginSetup\n", string, (char *) NULL); for (hPtr = Tcl_FirstHashEntry(&psInfo.fontTable, &search); hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) { Tcl_AppendResult(canvasPtr->interp, "%%IncludeResource: font ", Tcl_GetHashKey(&psInfo.fontTable, hPtr), "\n", (char *) NULL); } Tcl_AppendResult(canvasPtr->interp, "%%EndSetup\n\n", (char *) NULL); /* *----------------------------------------------------------- * Page setup: move to page positioning point, rotate if * needed, set scale factor, offset for proper anchor position, * and set clip region. *----------------------------------------------------------- */ Tcl_AppendResult(canvasPtr->interp, "%%Page: 1 1\n", "save\n", (char *) NULL); sprintf(string, "%.1f %.1f translate\n", psInfo.pageX, psInfo.pageY); Tcl_AppendResult(canvasPtr->interp, string, (char *) NULL); if (psInfo.rotate) { Tcl_AppendResult(canvasPtr->interp, "90 rotate\n", (char *) NULL); } sprintf(string, "%.4g %.4g scale\n", psInfo.scale, psInfo.scale); Tcl_AppendResult(canvasPtr->interp, string, (char *) NULL); sprintf(string, "%d %d translate\n", deltaX - psInfo.x, deltaY); Tcl_AppendResult(canvasPtr->interp, string, (char *) NULL); sprintf(string, "%d %.15g moveto %d %.15g lineto %d %.15g lineto %d %.15g", psInfo.x, Tk_CanvasPsY((Tk_Canvas) canvasPtr, (double) psInfo.y), psInfo.x2, Tk_CanvasPsY((Tk_Canvas) canvasPtr, (double) psInfo.y), psInfo.x2, Tk_CanvasPsY((Tk_Canvas) canvasPtr, (double) psInfo.y2), psInfo.x, Tk_CanvasPsY((Tk_Canvas) canvasPtr, (double) psInfo.y2)); Tcl_AppendResult(canvasPtr->interp, string, " lineto closepath clip newpath\n", (char *) NULL); if (psInfo.chan != NULL) { Tcl_Write(psInfo.chan, canvasPtr->interp->result, -1); Tcl_ResetResult(canvasPtr->interp); } /* *--------------------------------------------------------------------- * Iterate through all the items, having each relevant one draw itself. * Quit if any of the items returns an error. *--------------------------------------------------------------------- */ result = TCL_OK; for (itemPtr = canvasPtr->firstItemPtr; itemPtr != NULL; itemPtr = itemPtr->nextPtr) { if ((itemPtr->x1 >= psInfo.x2) || (itemPtr->x2 < psInfo.x) || (itemPtr->y1 >= psInfo.y2) || (itemPtr->y2 < psInfo.y)) { continue; } if (itemPtr->typePtr->postscriptProc == NULL) { continue; } Tcl_AppendResult(canvasPtr->interp, "gsave\n", (char *) NULL); result = (*itemPtr->typePtr->postscriptProc)(canvasPtr->interp, (Tk_Canvas) canvasPtr, itemPtr, 0); if (result != TCL_OK) { char msg[100]; sprintf(msg, "\n (generating Postscript for item %d)", itemPtr->id); Tcl_AddErrorInfo(canvasPtr->interp, msg); goto cleanup; } Tcl_AppendResult(canvasPtr->interp, "grestore\n", (char *) NULL); if (psInfo.chan != NULL) { Tcl_Write(psInfo.chan, canvasPtr->interp->result, -1); Tcl_ResetResult(canvasPtr->interp); } } /* *--------------------------------------------------------------------- * Output page-end information, such as commands to print the page * and document trailer stuff. *--------------------------------------------------------------------- */ Tcl_AppendResult(canvasPtr->interp, "restore showpage\n\n", "%%Trailer\nend\n%%EOF\n", (char *) NULL); if (psInfo.chan != NULL) { Tcl_Write(psInfo.chan, canvasPtr->interp->result, -1); Tcl_ResetResult(canvasPtr->interp); } /* * Clean up psInfo to release malloc'ed stuff. */ cleanup: if (psInfo.pageXString != NULL) { ckfree(psInfo.pageXString); } if (psInfo.pageYString != NULL) { ckfree(psInfo.pageYString); } if (psInfo.pageWidthString != NULL) { ckfree(psInfo.pageWidthString); } if (psInfo.pageHeightString != NULL) { ckfree(psInfo.pageHeightString); } if (psInfo.fontVar != NULL) { ckfree(psInfo.fontVar); } if (psInfo.colorVar != NULL) { ckfree(psInfo.colorVar); } if (psInfo.colorMode != NULL) { ckfree(psInfo.colorMode); } if (psInfo.fileName != NULL) { ckfree(psInfo.fileName); } if ((psInfo.chan != NULL) && (psInfo.channelName == NULL)) { Tcl_Close(canvasPtr->interp, psInfo.chan); } if (psInfo.channelName != NULL) { ckfree(psInfo.channelName); } Tcl_DeleteHashTable(&psInfo.fontTable); canvasPtr->psInfoPtr = oldInfoPtr; return result;}/* *-------------------------------------------------------------- * * Tk_CanvasPsColor -- * * This procedure is called by individual canvas items when * they want to set a color value for output. Given information * about an X color, this procedure will generate Postscript * commands to set up an appropriate color in Postscript. * * Results: * Returns a standard Tcl return value. If an error occurs * then an error message will be left in interp->result. * If no error occurs, then additional Postscript will be * appended to interp->result. * * Side effects: * None. * *--------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -