📄 tkwinfont.c
字号:
Tk_MeasureChars(tkfont, source, numChars, maxLength, flags, lengthPtr) Tk_Font tkfont; /* Font in which characters will be drawn. */ CONST char *source; /* Characters to be displayed. Need not be * '\0' terminated. */ int numChars; /* Maximum number of characters to consider * from source string. */ int maxLength; /* If > 0, maxLength specifies the longest * permissible line length; don't consider any * character that would cross this * x-position. If <= 0, then line length is * unbounded and the flags argument is * ignored. */ int flags; /* Various flag bits OR-ed together: * TK_PARTIAL_OK means include the last char * which only partially fit on this line. * TK_WHOLE_WORDS means stop on a word * boundary, if possible. * TK_AT_LEAST_ONE means return at least one * character even if no characters fit. */ int *lengthPtr; /* Filled with x-location just after the * terminating character. */{ WinFont *fontPtr; HDC hdc; HFONT hFont; int curX, curIdx; fontPtr = (WinFont *) tkfont; hdc = GetDC(fontPtr->hwnd); hFont = SelectObject(hdc, fontPtr->hFont); if (numChars == 0) { curX = 0; curIdx = 0; } else if (maxLength <= 0) { SIZE size; GetTextExtentPoint32(hdc, source, numChars, &size); curX = size.cx; curIdx = numChars; } else { int max; int *partials; SIZE size; partials = (int *) ckalloc(numChars * sizeof (int)); GetTextExtentExPoint(hdc, source, numChars, maxLength, &max, partials, &size); if ((flags & TK_WHOLE_WORDS) && max < numChars) { int sawSpace; int i; sawSpace = 0; i = max; while (i >= 0 && !isspace(source[i])) { --i; } while (i >= 0 && isspace(source[i])) { sawSpace = 1; --i; } /* * If a space char was not found, and the flag for forcing * at least on (or more) chars to be drawn is false, then * set MAX to zero so no text is drawn. Otherwise, if a * space was found, set max to be one char past the space. */ if ((i < 0) && !(flags & TK_AT_LEAST_ONE)) { max = 0; } else if (sawSpace) { max = i + 1; } } if (max == 0) { curX = 0; } else { curX = partials[max - 1]; } if (((flags & TK_PARTIAL_OK) && max < numChars && curX < maxLength) || ((flags & TK_AT_LEAST_ONE) && max == 0 && numChars > 0)) { /* * We want to include the first character that didn't * quite fit. Call the function again to include the * width of the extra character. */ GetTextExtentExPoint(hdc, source, max + 1, 0, NULL, partials, &size); curX = partials[max]; ++max; } ckfree((char *) partials); curIdx = max; } SelectObject(hdc, hFont); ReleaseDC(fontPtr->hwnd, hdc); *lengthPtr = curX; return curIdx;}/* *--------------------------------------------------------------------------- * * Tk_DrawChars -- * * Draw a string of characters on the screen. * * Results: * None. * * Side effects: * Information gets drawn on the screen. * *--------------------------------------------------------------------------- */voidTk_DrawChars(display, drawable, gc, tkfont, source, numChars, x, y) Display *display; /* Display on which to draw. */ Drawable drawable; /* Window or pixmap in which to draw. */ GC gc; /* Graphics context for drawing characters. */ Tk_Font tkfont; /* Font in which characters will be drawn; * must be the same as font used in GC. */ CONST char *source; /* Characters to be displayed. Need not be * '\0' terminated. All Tk meta-characters * (tabs, control characters, and newlines) * should be stripped out of the string that * is passed to this function. If they are * not stripped out, they will be displayed as * regular printing characters. */ int numChars; /* Number of characters in string. */ int x, y; /* Coordinates at which to place origin of * string when drawing. */{ HDC dc; HFONT hFont; TkWinDCState state; WinFont *fontPtr; fontPtr = (WinFont *) gc->font; display->request++; if (drawable == None) { return; } dc = TkWinGetDrawableDC(display, drawable, &state); SetROP2(dc, tkpWinRopModes[gc->function]); if ((gc->fill_style == FillStippled || gc->fill_style == FillOpaqueStippled) && gc->stipple != None) { TkWinDrawable *twdPtr = (TkWinDrawable *)gc->stipple; HBRUSH oldBrush, stipple; HBITMAP oldBitmap, bitmap; HDC dcMem; TEXTMETRIC tm; SIZE size; if (twdPtr->type != TWD_BITMAP) { panic("unexpected drawable type in stipple"); } /* * Select stipple pattern into destination dc. */ dcMem = CreateCompatibleDC(dc); stipple = CreatePatternBrush(twdPtr->bitmap.handle); SetBrushOrgEx(dc, gc->ts_x_origin, gc->ts_y_origin, NULL); oldBrush = SelectObject(dc, stipple); SetTextAlign(dcMem, TA_LEFT | TA_TOP); SetTextColor(dcMem, gc->foreground); SetBkMode(dcMem, TRANSPARENT); SetBkColor(dcMem, RGB(0, 0, 0)); hFont = SelectObject(dcMem, fontPtr->hFont); /* * Compute the bounding box and create a compatible bitmap. */ GetTextExtentPoint(dcMem, source, numChars, &size); GetTextMetrics(dcMem, &tm); size.cx -= tm.tmOverhang; bitmap = CreateCompatibleBitmap(dc, size.cx, size.cy); oldBitmap = SelectObject(dcMem, bitmap); /* * The following code is tricky because fonts are rendered in multiple * colors. First we draw onto a black background and copy the white * bits. Then we draw onto a white background and copy the black bits. * Both the foreground and background bits of the font are ANDed with * the stipple pattern as they are copied. */ PatBlt(dcMem, 0, 0, size.cx, size.cy, BLACKNESS); TextOut(dcMem, 0, 0, source, numChars); BitBlt(dc, x, y - tm.tmAscent, size.cx, size.cy, dcMem, 0, 0, 0xEA02E9); PatBlt(dcMem, 0, 0, size.cx, size.cy, WHITENESS); TextOut(dcMem, 0, 0, source, numChars); BitBlt(dc, x, y - tm.tmAscent, size.cx, size.cy, dcMem, 0, 0, 0x8A0E06); /* * Destroy the temporary bitmap and restore the device context. */ SelectObject(dcMem, hFont); SelectObject(dcMem, oldBitmap); DeleteObject(bitmap); DeleteDC(dcMem); SelectObject(dc, oldBrush); DeleteObject(stipple); } else { SetTextAlign(dc, TA_LEFT | TA_BASELINE); SetTextColor(dc, gc->foreground); SetBkMode(dc, TRANSPARENT); hFont = SelectObject(dc, fontPtr->hFont); TextOut(dc, x, y, source, numChars); SelectObject(dc, hFont); } TkWinReleaseDrawableDC(drawable, dc, &state);}/* *--------------------------------------------------------------------------- * * AllocFont -- * * Helper for TkpGetNativeFont() and TkpGetFontFromAttributes(). * Allocates and intializes the memory for a new TkFont that * wraps the platform-specific data. * * Results: * Returns pointer to newly constructed TkFont. * * The caller is responsible for initializing the fields of the * TkFont that are used exclusively by the generic TkFont code, and * for releasing those fields before calling TkpDeleteFont(). * * Side effects: * Memory allocated. * *--------------------------------------------------------------------------- */ static TkFont *AllocFont(tkFontPtr, tkwin, hFont) TkFont *tkFontPtr; /* If non-NULL, store the information in * this existing TkFont structure, rather than * allocating a new structure to hold the * font; the existing contents of the font * will be released. If NULL, a new TkFont * structure is allocated. */ Tk_Window tkwin; /* For display where font will be used. */ HFONT hFont; /* Windows information about font. */{ HWND hwnd; WinFont *fontPtr; HDC hdc; TEXTMETRIC tm; Window window; char buf[LF_FACESIZE]; TkFontAttributes *faPtr; if (tkFontPtr != NULL) { fontPtr = (WinFont *) tkFontPtr; DeleteObject(fontPtr->hFont); } else { fontPtr = (WinFont *) ckalloc(sizeof(WinFont)); } window = Tk_WindowId(((TkWindow *) tkwin)->mainPtr->winPtr); hwnd = (window == None) ? NULL : TkWinGetHWND(window); hdc = GetDC(hwnd); hFont = SelectObject(hdc, hFont); GetTextFace(hdc, sizeof(buf), buf); GetTextMetrics(hdc, &tm); fontPtr->font.fid = (Font) fontPtr; faPtr = &fontPtr->font.fa; faPtr->family = Tk_GetUid(buf); faPtr->pointsize = MulDiv(tm.tmHeight - tm.tmInternalLeading, 720 * WidthMMOfScreen(Tk_Screen(tkwin)), 254 * WidthOfScreen(Tk_Screen(tkwin))); faPtr->weight = (tm.tmWeight > FW_MEDIUM) ? TK_FW_BOLD : TK_FW_NORMAL; faPtr->slant = (tm.tmItalic != 0) ? TK_FS_ITALIC : TK_FS_ROMAN; faPtr->underline = (tm.tmUnderlined != 0) ? 1 : 0; faPtr->overstrike = (tm.tmStruckOut != 0) ? 1 : 0; fontPtr->font.fm.ascent = tm.tmAscent; fontPtr->font.fm.descent = tm.tmDescent; fontPtr->font.fm.maxWidth = tm.tmMaxCharWidth; fontPtr->font.fm.fixed = !(tm.tmPitchAndFamily & TMPF_FIXED_PITCH); hFont = SelectObject(hdc, hFont); ReleaseDC(hwnd, hdc); fontPtr->hFont = hFont; fontPtr->hwnd = hwnd; return (TkFont *) fontPtr;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -