📄 tkcanvtext.c
字号:
if (textInfoPtr->selectFirst > textInfoPtr->selectLast) { textInfoPtr->selItemPtr = NULL; } if ((textInfoPtr->anchorItemPtr == itemPtr) && (textInfoPtr->selectAnchor > first)) { textInfoPtr->selectAnchor -= count; if (textInfoPtr->selectAnchor < first) { textInfoPtr->selectAnchor = first; } } } if (textPtr->insertPos > first) { textPtr->insertPos -= count; if (textPtr->insertPos < first) { textPtr->insertPos = first; } } ComputeTextBbox(canvas, textPtr); return;}/* *-------------------------------------------------------------- * * TextToPoint -- * * Computes the distance from a given point to a given * text item, in canvas units. * * Results: * The return value is 0 if the point whose x and y coordinates * are pointPtr[0] and pointPtr[1] is inside the text item. If * the point isn't inside the text item then the return value * is the distance from the point to the text item. * * Side effects: * None. * *-------------------------------------------------------------- */static doubleTextToPoint(canvas, itemPtr, pointPtr) Tk_Canvas canvas; /* Canvas containing itemPtr. */ Tk_Item *itemPtr; /* Item to check against point. */ double *pointPtr; /* Pointer to x and y coordinates. */{ TextItem *textPtr; textPtr = (TextItem *) itemPtr; return (double) Tk_DistanceToTextLayout(textPtr->textLayout, (int) pointPtr[0] - textPtr->leftEdge, (int) pointPtr[1] - textPtr->header.y1);}/* *-------------------------------------------------------------- * * TextToArea -- * * This procedure is called to determine whether an item * lies entirely inside, entirely outside, or overlapping * a given rectangle. * * Results: * -1 is returned if the item is entirely outside the area * given by rectPtr, 0 if it overlaps, and 1 if it is entirely * inside the given area. * * Side effects: * None. * *-------------------------------------------------------------- */static intTextToArea(canvas, itemPtr, rectPtr) Tk_Canvas canvas; /* Canvas containing itemPtr. */ Tk_Item *itemPtr; /* Item to check against rectangle. */ double *rectPtr; /* Pointer to array of four coordinates * (x1, y1, x2, y2) describing rectangular * area. */{ TextItem *textPtr; textPtr = (TextItem *) itemPtr; return Tk_IntersectTextLayout(textPtr->textLayout, (int) (rectPtr[0] + 0.5) - textPtr->leftEdge, (int) (rectPtr[1] + 0.5) - textPtr->header.y1, (int) (rectPtr[2] - rectPtr[0] + 0.5), (int) (rectPtr[3] - rectPtr[1] + 0.5));}/* *-------------------------------------------------------------- * * ScaleText -- * * This procedure is invoked to rescale a text item. * * Results: * None. * * Side effects: * Scales the position of the text, but not the size * of the font for the text. * *-------------------------------------------------------------- */ /* ARGSUSED */static voidScaleText(canvas, itemPtr, originX, originY, scaleX, scaleY) Tk_Canvas canvas; /* Canvas containing rectangle. */ Tk_Item *itemPtr; /* Rectangle to be scaled. */ double originX, originY; /* Origin about which to scale rect. */ double scaleX; /* Amount to scale in X direction. */ double scaleY; /* Amount to scale in Y direction. */{ TextItem *textPtr = (TextItem *) itemPtr; textPtr->x = originX + scaleX*(textPtr->x - originX); textPtr->y = originY + scaleY*(textPtr->y - originY); ComputeTextBbox(canvas, textPtr); return;}/* *-------------------------------------------------------------- * * TranslateText -- * * This procedure is called to move a text item by a * given amount. * * Results: * None. * * Side effects: * The position of the text item is offset by (xDelta, yDelta), * and the bounding box is updated in the generic part of the * item structure. * *-------------------------------------------------------------- */static voidTranslateText(canvas, itemPtr, deltaX, deltaY) Tk_Canvas canvas; /* Canvas containing item. */ Tk_Item *itemPtr; /* Item that is being moved. */ double deltaX, deltaY; /* Amount by which item is to be * moved. */{ TextItem *textPtr = (TextItem *) itemPtr; textPtr->x += deltaX; textPtr->y += deltaY; ComputeTextBbox(canvas, textPtr);}/* *-------------------------------------------------------------- * * GetTextIndex -- * * Parse an index into a text item and return either its value * or an error. * * Results: * A standard Tcl result. If all went well, then *indexPtr is * filled in with the index (into itemPtr) corresponding to * string. Otherwise an error message is left in * interp->result. * * Side effects: * None. * *-------------------------------------------------------------- */static intGetTextIndex(interp, canvas, itemPtr, string, indexPtr) Tcl_Interp *interp; /* Used for error reporting. */ Tk_Canvas canvas; /* Canvas containing item. */ Tk_Item *itemPtr; /* Item for which the index is being * specified. */ char *string; /* Specification of a particular character * in itemPtr's text. */ int *indexPtr; /* Where to store converted index. */{ TextItem *textPtr = (TextItem *) itemPtr; size_t length; int c; TkCanvas *canvasPtr = (TkCanvas *) canvas; Tk_CanvasTextInfo *textInfoPtr = textPtr->textInfoPtr; c = string[0]; length = strlen(string); if ((c == 'e') && (strncmp(string, "end", length) == 0)) { *indexPtr = textPtr->numChars; } else if ((c == 'i') && (strncmp(string, "insert", length) == 0)) { *indexPtr = textPtr->insertPos; } else if ((c == 's') && (strncmp(string, "sel.first", length) == 0) && (length >= 5)) { if (textInfoPtr->selItemPtr != itemPtr) { interp->result = "selection isn't in item"; return TCL_ERROR; } *indexPtr = textInfoPtr->selectFirst; } else if ((c == 's') && (strncmp(string, "sel.last", length) == 0) && (length >= 5)) { if (textInfoPtr->selItemPtr != itemPtr) { interp->result = "selection isn't in item"; return TCL_ERROR; } *indexPtr = textInfoPtr->selectLast; } else if (c == '@') { int x, y; double tmp; char *end, *p; p = string+1; tmp = strtod(p, &end); if ((end == p) || (*end != ',')) { goto badIndex; } x = (int) ((tmp < 0) ? tmp - 0.5 : tmp + 0.5); p = end+1; tmp = strtod(p, &end); if ((end == p) || (*end != 0)) { goto badIndex; } y = (int) ((tmp < 0) ? tmp - 0.5 : tmp + 0.5); *indexPtr = Tk_PointToChar(textPtr->textLayout, x + canvasPtr->scrollX1 - textPtr->leftEdge, y + canvasPtr->scrollY1 - textPtr->header.y1); } else if (Tcl_GetInt(interp, string, indexPtr) == TCL_OK) { if (*indexPtr < 0){ *indexPtr = 0; } else if (*indexPtr > textPtr->numChars) { *indexPtr = textPtr->numChars; } } else { /* * Some of the paths here leave messages in interp->result, * so we have to clear it out before storing our own message. */ badIndex: Tcl_SetResult(interp, (char *) NULL, TCL_STATIC); Tcl_AppendResult(interp, "bad index \"", string, "\"", (char *) NULL); return TCL_ERROR; } return TCL_OK;}/* *-------------------------------------------------------------- * * SetTextCursor -- * * Set the position of the insertion cursor in this item. * * Results: * None. * * Side effects: * The cursor position will change. * *-------------------------------------------------------------- */ /* ARGSUSED */static voidSetTextCursor(canvas, itemPtr, index) Tk_Canvas canvas; /* Record describing canvas widget. */ Tk_Item *itemPtr; /* Text item in which cursor position * is to be set. */ int index; /* Index of character just before which * cursor is to be positioned. */{ TextItem *textPtr = (TextItem *) itemPtr; if (index < 0) { textPtr->insertPos = 0; } else if (index > textPtr->numChars) { textPtr->insertPos = textPtr->numChars; } else { textPtr->insertPos = index; }}/* *-------------------------------------------------------------- * * GetSelText -- * * This procedure is invoked to return the selected portion * of a text item. It is only called when this item has * the selection. * * Results: * The return value is the number of non-NULL bytes stored * at buffer. Buffer is filled (or partially filled) with a * NULL-terminated string containing part or all of the selection, * as given by offset and maxBytes. * * Side effects: * None. * *-------------------------------------------------------------- */static intGetSelText(canvas, itemPtr, offset, buffer, maxBytes) Tk_Canvas canvas; /* Canvas containing selection. */ Tk_Item *itemPtr; /* Text item containing selection. */ int offset; /* Offset within selection of first * character to be returned. */ char *buffer; /* Location in which to place * selection. */ int maxBytes; /* Maximum number of bytes to place * at buffer, not including terminating * NULL character. */{ TextItem *textPtr = (TextItem *) itemPtr; int count; Tk_CanvasTextInfo *textInfoPtr = textPtr->textInfoPtr; count = textInfoPtr->selectLast + 1 - textInfoPtr->selectFirst - offset; if (textInfoPtr->selectLast == textPtr->numChars) { count -= 1; } if (count > maxBytes) { count = maxBytes; } if (count <= 0) { return 0; } strncpy(buffer, textPtr->text + textInfoPtr->selectFirst + offset, (size_t) count); buffer[count] = '\0'; return count;}/* *-------------------------------------------------------------- * * TextToPostscript -- * * This procedure is called to generate Postscript for * text items. * * Results: * The return value is a standard Tcl result. If an error * occurs in generating Postscript then an error message is * left in interp->result, replacing whatever used * to be there. If no error occurs, then Postscript for the * item is appended to the result. * * Side effects: * None. * *-------------------------------------------------------------- */static intTextToPostscript(interp, canvas, itemPtr, prepass) Tcl_Interp *interp; /* Leave Postscript or error message * here. */ Tk_Canvas canvas; /* Information about overall canvas. */ Tk_Item *itemPtr; /* Item for which Postscript is * wanted. */ int prepass; /* 1 means this is a prepass to * collect font information; 0 means * final Postscript is being created. */{ TextItem *textPtr = (TextItem *) itemPtr; int x, y; Tk_FontMetrics fm; char *justify; char buffer[500]; if (textPtr->color == NULL) { return TCL_OK; } if (Tk_CanvasPsFont(interp, canvas, textPtr->tkfont) != TCL_OK) { return TCL_ERROR; } if (prepass != 0) { return TCL_OK; } if (Tk_CanvasPsColor(interp, canvas, textPtr->color) != TCL_OK) { return TCL_ERROR; } if (textPtr->stipple != None) { Tcl_AppendResult(interp, "/StippleText {\n ", (char *) NULL); Tk_CanvasPsStipple(interp, canvas, textPtr->stipple); Tcl_AppendResult(interp, "} bind def\n", (char *) NULL); } sprintf(buffer, "%.15g %.15g [\n", textPtr->x, Tk_CanvasPsY(canvas, textPtr->y)); Tcl_AppendResult(interp, buffer, (char *) NULL); Tk_TextLayoutToPostscript(interp, textPtr->textLayout); x = 0; y = 0; justify = NULL; /* lint. */ switch (textPtr->anchor) { case TK_ANCHOR_NW: x = 0; y = 0; break; case TK_ANCHOR_N: x = 1; y = 0; break; case TK_ANCHOR_NE: x = 2; y = 0; break; case TK_ANCHOR_E: x = 2; y = 1; break; case TK_ANCHOR_SE: x = 2; y = 2; break; case TK_ANCHOR_S: x = 1; y = 2; break; case TK_ANCHOR_SW: x = 0; y = 2; break; case TK_ANCHOR_W: x = 0; y = 1; break; case TK_ANCHOR_CENTER: x = 1; y = 1; break; } switch (textPtr->justify) { case TK_JUSTIFY_LEFT: justify = "0"; break; case TK_JUSTIFY_CENTER: justify = "0.5";break; case TK_JUSTIFY_RIGHT: justify = "1"; break; } Tk_GetFontMetrics(textPtr->tkfont, &fm); sprintf(buffer, "] %d %g %g %s %s DrawText\n", fm.linespace, x / -2.0, y / 2.0, justify, ((textPtr->stipple == None) ? "false" : "true")); Tcl_AppendResult(interp, buffer, (char *) NULL); return TCL_OK;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -