📄 tkwinfont.c
字号:
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;#ifndef USE_CKGRAPH_IMP HFONT hFont;#endif int curX, curIdx; fontPtr = (WinFont *) tkfont; //hdc = CkGetDC(fontPtr->hwnd); hdc = TkWinGetNULLDC();#ifdef USE_CKGRAPH_IMP CkSelectFont(hdc, fontPtr->hFont);#else hFont = CkSelectFont(hdc, fontPtr->hFont);#endif 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; }#ifndef USE_CKGRAPH_IMP CkSelectFont(hdc, hFont);#endif //CkReleaseDC(fontPtr->hwnd, hdc); TkWinReleaseNULLDC(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;#ifndef USE_CKGRAPH_IMP HFONT hFont;#endif TkWinDCState state; WinFont *fontPtr; fontPtr = (WinFont *) gc->font; display->request++; if (drawable == None) { return; } dc = TkWinGetDrawableDC(display, drawable, &state); CkSetROP2(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"); }#ifdef USE_CKGRAPH_IMP CkGraph_ClearDC(dc);#endif /* * Select stipple pattern into destination dc. */ dcMem = CkCreateCompatibleDC(dc); stipple = CkCreatePatternBrush(twdPtr->bitmap.handle); SetBrushOrgEx(dc, gc->ts_x_origin, gc->ts_y_origin, NULL); oldBrush = CkSelectBrush(dc, stipple); SetTextAlign(dcMem, TA_LEFT | TA_TOP); CkSetTextColor(dcMem, gc->foreground); CkSetBkMode(dcMem, TRANSPARENT); CkSetBkColor(dcMem, RGB(0, 0, 0));#ifdef USE_CKGRAPH_IMP CkSelectFont(dcMem, fontPtr->hFont);#else hFont = CkSelectFont(dcMem, fontPtr->hFont);#endif /* * Compute the bounding box and create a compatible bitmap. */ GetTextExtentPoint(dcMem, source, numChars, &size); GetTextMetrics(dcMem, &tm); size.cx -= tm.tmOverhang; bitmap = CkCreateCompatibleBitmap(dc, size.cx, size.cy); oldBitmap = CkSelectBitmap(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); CkTextOut(dcMem, 0, 0, source, numChars); CkBitBlt(dc, x, y - tm.tmAscent, size.cx, size.cy, dcMem, 0, 0, 0xEA02E9); PatBlt(dcMem, 0, 0, size.cx, size.cy, WHITENESS); CkTextOut(dcMem, 0, 0, source, numChars); CkBitBlt(dc, x, y - tm.tmAscent, size.cx, size.cy, dcMem, 0, 0, 0x8A0E06); /* * Destroy the temporary bitmap and restore the device context. */#ifndef USE_CKGRAPH_IMP CkSelectFont(dcMem, hFont);#endif CkSelectBitmap(dcMem, oldBitmap); CkDeleteBitmap(bitmap); CkDeleteDC(dcMem); CkSelectBrush(dc, oldBrush); CkDeleteBrush(stipple); } else { SetTextAlign(dc, TA_LEFT | TA_BASELINE); CkSetTextColor(dc, gc->foreground); CkSetBkMode(dc, TRANSPARENT);#ifdef USE_CKGRAPH_IMP CkSelectFont(dc, fontPtr->hFont);#else hFont = CkSelectFont(dc, fontPtr->hFont);#endif CkTextOut(dc, x, y, source, numChars);#ifndef USE_CKGRAPH_IMP CkSelectFont(dc, hFont);#endif } 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. */{ WinFont *fontPtr; HDC hdc; TEXTMETRIC tm; char buf[LF_FACESIZE]; TkFontAttributes *faPtr; if (tkFontPtr != NULL) { fontPtr = (WinFont *) tkFontPtr; CkDeleteFont(fontPtr->hFont); } else { fontPtr = (WinFont *) ckalloc(sizeof(WinFont)); } hdc = TkWinGetNULLDC();#ifdef USE_CKGRAPH_IMP CkSelectFont(hdc, hFont);#else hFont = CkSelectFont(hdc, hFont);#endif 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);#ifndef USE_CKGRAPH_IMP hFont = CkSelectFont(hdc, hFont);#endif TkWinReleaseNULLDC(hdc); fontPtr->hFont = hFont; fontPtr->hwnd = NULL; return (TkFont *) fontPtr;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -