/* * 显示菜单上面的文字信息,只需要指定窗口,窗口宽度,需要显示文字的上边界,字体,显示的字符串和是否选择(选择后反色) * 其他都固定,如背景和文字反色不反色的颜色,反色框和字的位置等 */ void DisplayText(XlibMenu * menu, int shellindex, int line_y, int fontHeight) { FcitxSkin *sc = &menu->owner->skin; int marginLeft = sc->skinMenu.marginLeft; int marginRight = sc->skinMenu.marginRight; cairo_t * cr; cr = cairo_create(menu->menu_cs); int dpi = sc->skinFont.respectDPI? menu->owner->dpi: 0; FCITX_UNUSED(dpi); SetFontContext(cr, menu->owner->menuFont, sc->skinFont.menuFontSize, dpi); if (GetMenuItem(menu->menushell, shellindex)->isselect == 0) { fcitx_cairo_set_color(cr, &sc->skinFont.menuFontColor[MENU_INACTIVE]); OutputStringWithContext(cr, dpi, GetMenuItem(menu->menushell, shellindex)->tipstr, 15 + marginLeft , line_y); } else { cairo_set_operator(cr, CAIRO_OPERATOR_OVER); fcitx_cairo_set_color(cr, &sc->skinMenu.activeColor); cairo_rectangle(cr, marginLeft , line_y, menu->width - marginRight - marginLeft, fontHeight + 4); cairo_fill(cr); fcitx_cairo_set_color(cr, &sc->skinFont.menuFontColor[MENU_ACTIVE]); OutputStringWithContext(cr, dpi, GetMenuItem(menu->menushell, shellindex)->tipstr , 15 + marginLeft , line_y); } ResetFontContext(); cairo_destroy(cr); }
int FontHeight(const char *font, int fontSize, int dpi) { cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, 10, 10); cairo_t *c = cairo_create(surface); SetFontContext(c, font, fontSize, dpi); int height = FontHeightWithContext(c, dpi); ResetFontContext(); cairo_destroy(c); cairo_surface_destroy(surface); return height; }
/* * 以指定的颜色在窗口的指定位置输出字串 */ void OutputString(cairo_t * c, const char *str, const char *font, int fontSize, int dpi, int x, int y, FcitxConfigColor * color) { if (!str || str[0] == 0) return; cairo_save(c); cairo_set_source_rgb(c, color->r, color->g, color->b); SetFontContext(c, font, fontSize, dpi); OutputStringWithContext(c, dpi, str, x, y); ResetFontContext(); cairo_restore(c); }
void StringSizeStrict(const char* str, const char* font, int fontSize, int dpi, int* w, int* h) { if (!str || str[0] == 0) { if (w) *w = 0; if (h) *h = 0; return; } if (!fcitx_utf8_check_string(str)) { if (w) *w = 0; if (h) *h = 0; return; } cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, 10, 10); cairo_t *c = cairo_create(surface); SetFontContext(c, font, fontSize, dpi); #ifdef _ENABLE_PANGO PangoRectangle rect; PangoContext* pc = pango_cairo_create_context(c); pango_cairo_context_set_resolution(pc, dpi); PangoLayout *layout = pango_layout_new(pc); pango_layout_set_text(layout, str, -1); pango_layout_set_font_description(layout, fontDesc); pango_layout_get_pixel_extents(layout, &rect, NULL); if (w) *w = rect.width; if (h) *h = rect.height; g_object_unref(layout); #else cairo_text_extents_t extents; cairo_text_extents(c, str, &extents); if (w) *w = extents.width; if (h) *h = extents.height; #endif ResetFontContext(); cairo_destroy(c); cairo_surface_destroy(surface); }
int StringWidth(const char *str, const char *font, int fontSize, int dpi) { if (!str || str[0] == 0) return 0; cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, 10, 10); cairo_t *c = cairo_create(surface); SetFontContext(c, font, fontSize, dpi); int width = 0; StringSizeWithContext(c, dpi, str, &width, NULL); ResetFontContext(); cairo_destroy(c); cairo_surface_destroy(surface); return width; }
void DrawInputBar(FcitxSkin* sc, InputWindow* inputWindow, int iCursorPos, FcitxMessages * msgup, FcitxMessages *msgdown , unsigned int * iheight, unsigned int *iwidth) { int i; char *strUp[MAX_MESSAGE_COUNT]; char *strDown[MAX_MESSAGE_COUNT]; int posUpX[MAX_MESSAGE_COUNT], posUpY[MAX_MESSAGE_COUNT]; int posDownX[MAX_MESSAGE_COUNT], posDownY[MAX_MESSAGE_COUNT]; int oldHeight = *iheight, oldWidth = *iwidth; int newHeight = 0, newWidth = 0; int cursor_pos = 0; int inputWidth = 0, outputWidth = 0; int outputHeight = 0; cairo_t *c = NULL; FcitxInputState* input = FcitxInstanceGetInputState(inputWindow->owner->owner); FcitxInstance* instance = inputWindow->owner->owner; FcitxClassicUI* classicui = inputWindow->owner; int iChar = iCursorPos; int strWidth = 0, strHeight = 0; SkinImage *inputimg, *prev, *next; inputimg = LoadImage(sc, sc->skinInputBar.backImg, false); prev = LoadImage(sc, sc->skinInputBar.backArrow, false); next = LoadImage(sc, sc->skinInputBar.forwardArrow, false); if (!FcitxMessagesIsMessageChanged(msgup) && !FcitxMessagesIsMessageChanged(msgdown)) return; inputWidth = 0; int dpi = sc->skinFont.respectDPI? classicui->dpi : 0; FCITX_UNUSED(dpi); #ifdef _ENABLE_PANGO /* special case which only macro unable to handle */ SetFontContext(dummy, inputWindow->owner->font, inputWindow->owner->fontSize > 0 ? inputWindow->owner->fontSize : sc->skinFont.fontSize, dpi); #endif int fontHeight = FontHeightWithContext(inputWindow->c_font[0], dpi); for (i = 0; i < FcitxMessagesGetMessageCount(msgup) ; i++) { char *trans = FcitxInstanceProcessOutputFilter(instance, FcitxMessagesGetMessageString(msgup, i)); if (trans) strUp[i] = trans; else strUp[i] = FcitxMessagesGetMessageString(msgup, i); posUpX[i] = sc->skinInputBar.marginLeft + inputWidth; StringSizeWithContext(inputWindow->c_font[FcitxMessagesGetMessageType(msgup, i)], dpi, strUp[i], &strWidth, &strHeight); if (sc->skinFont.respectDPI) posUpY[i] = sc->skinInputBar.marginTop + sc->skinInputBar.iInputPos; else posUpY[i] = sc->skinInputBar.marginTop + sc->skinInputBar.iInputPos - strHeight; inputWidth += strWidth; if (FcitxInputStateGetShowCursor(input)) { int length = strlen(FcitxMessagesGetMessageString(msgup, i)); if (iChar >= 0) { if (iChar < length) { char strTemp[MESSAGE_MAX_LENGTH]; char *strGBKT = NULL; strncpy(strTemp, strUp[i], iChar); strTemp[iChar] = '\0'; strGBKT = strTemp; StringSizeWithContext(inputWindow->c_font[FcitxMessagesGetMessageType(msgup, i)], dpi, strGBKT, &strWidth, &strHeight); cursor_pos = posUpX[i] + strWidth + 2; } iChar -= length; } } } if (iChar >= 0) cursor_pos = inputWidth + sc->skinInputBar.marginLeft; outputWidth = 0; outputHeight = 0; int currentX = 0; int offsetY; if (sc->skinFont.respectDPI) offsetY = sc->skinInputBar.marginTop + sc->skinInputBar.iInputPos + fontHeight + (FcitxMessagesGetMessageCount(msgdown) ? sc->skinInputBar.iOutputPos : 0); else offsetY = sc->skinInputBar.marginTop + sc->skinInputBar.iOutputPos - fontHeight; for (i = 0; i < FcitxMessagesGetMessageCount(msgdown) ; i++) { char *trans = FcitxInstanceProcessOutputFilter(instance, FcitxMessagesGetMessageString(msgdown, i)); if (trans) strDown[i] = trans; else strDown[i] = FcitxMessagesGetMessageString(msgdown, i); if (inputWindow->owner->bVerticalList) { /* vertical */ if (FcitxMessagesGetMessageType(msgdown, i) == MSG_INDEX) { if (currentX > outputWidth) outputWidth = currentX; if (i != 0) { currentX = 0; } } posDownX[i] = sc->skinInputBar.marginLeft + currentX; StringSizeWithContext(inputWindow->c_font[FcitxMessagesGetMessageType(msgdown, i)], dpi, strDown[i], &strWidth, &strHeight); if (FcitxMessagesGetMessageType(msgdown, i) == MSG_INDEX && i != 0) outputHeight += fontHeight + 2; currentX += strWidth; } else { /* horizontal */ posDownX[i] = sc->skinInputBar.marginLeft + outputWidth; StringSizeWithContext(inputWindow->c_font[FcitxMessagesGetMessageType(msgdown, i)], dpi, strDown[i], &strWidth, &strHeight); outputWidth += strWidth; } posDownY[i] = offsetY + outputHeight; } if (inputWindow->owner->bVerticalList && currentX > outputWidth) outputWidth = currentX; newHeight = offsetY + outputHeight + sc->skinInputBar.marginBottom + (FcitxMessagesGetMessageCount(msgdown) || !sc->skinFont.respectDPI ? fontHeight : 0); newWidth = (inputWidth < outputWidth) ? outputWidth : inputWidth; newWidth += sc->skinInputBar.marginLeft + sc->skinInputBar.marginRight; /* round to ROUND_SIZE in order to decrease resize */ newWidth = (newWidth / ROUND_SIZE) * ROUND_SIZE + ROUND_SIZE; if (inputWindow->owner->bVerticalList) { /* vertical */ newWidth = (newWidth < INPUT_BAR_VMIN_WIDTH) ? INPUT_BAR_VMIN_WIDTH : newWidth; } else { newWidth = (newWidth < INPUT_BAR_HMIN_WIDTH) ? INPUT_BAR_HMIN_WIDTH : newWidth; } *iwidth = newWidth; *iheight = newHeight; EnlargeCairoSurface(&inputWindow->cs_input_back, newWidth, newHeight); if (EnlargeCairoSurface(&inputWindow->cs_input_bar, newWidth, newHeight)) { LoadInputMessage(&classicui->skin, classicui->inputWindow, classicui->font); } if (oldHeight != newHeight || oldWidth != newWidth) { c = cairo_create(inputWindow->cs_input_back); DrawResizableBackground(c, inputimg->image, newHeight, newWidth, sc->skinInputBar.marginLeft, sc->skinInputBar.marginTop, sc->skinInputBar.marginRight, sc->skinInputBar.marginBottom, sc->skinInputBar.fillV, sc->skinInputBar.fillH ); cairo_destroy(c); } c = cairo_create(inputWindow->cs_input_bar); cairo_set_source_surface(c, inputWindow->cs_input_back, 0, 0); cairo_save(c); cairo_rectangle(c, 0, 0, newWidth, newHeight); cairo_set_operator(c, CAIRO_OPERATOR_SOURCE); cairo_clip(c); cairo_paint(c); cairo_restore(c); cairo_set_operator(c, CAIRO_OPERATOR_OVER); if (FcitxInputStateGetShowCursor(input)) { //画向前向后箭头 if (prev && next) { cairo_set_source_surface(inputWindow->c_back, prev->image, newWidth - sc->skinInputBar.iBackArrowX , sc->skinInputBar.iBackArrowY); if (FcitxCandidateWordHasPrev(FcitxInputStateGetCandidateList(input))) cairo_paint(inputWindow->c_back); else cairo_paint_with_alpha(inputWindow->c_back, 0.5); //画向前箭头 cairo_set_source_surface(inputWindow->c_back, next->image, newWidth - sc->skinInputBar.iForwardArrowX , sc->skinInputBar.iForwardArrowY); if (FcitxCandidateWordHasNext(FcitxInputStateGetCandidateList(input))) cairo_paint(inputWindow->c_back); else cairo_paint_with_alpha(inputWindow->c_back, 0.5); } } for (i = 0; i < FcitxMessagesGetMessageCount(msgup) ; i++) { OutputStringWithContext(inputWindow->c_font[FcitxMessagesGetMessageType(msgup, i)], dpi, strUp[i], posUpX[i], posUpY[i]); if (strUp[i] != FcitxMessagesGetMessageString(msgup, i)) free(strUp[i]); } for (i = 0; i < FcitxMessagesGetMessageCount(msgdown) ; i++) { OutputStringWithContext(inputWindow->c_font[FcitxMessagesGetMessageType(msgdown, i)], dpi, strDown[i], posDownX[i], posDownY[i]); if (strDown[i] != FcitxMessagesGetMessageString(msgdown, i)) free(strDown[i]); } int cursorY1, cursorY2; if (sc->skinFont.respectDPI) { cursorY1 = sc->skinInputBar.marginTop + sc->skinInputBar.iInputPos; cursorY2 = sc->skinInputBar.marginTop + sc->skinInputBar.iInputPos + fontHeight; } else { cursorY1 = sc->skinInputBar.marginTop + sc->skinInputBar.iInputPos - fontHeight - 4; cursorY2 = sc->skinInputBar.marginTop + sc->skinInputBar.iInputPos; } //画光标 if (FcitxInputStateGetShowCursor(input)) { cairo_move_to(inputWindow->c_cursor, cursor_pos, cursorY1); cairo_line_to(inputWindow->c_cursor, cursor_pos, cursorY2); cairo_stroke(inputWindow->c_cursor); } ResetFontContext(); cairo_destroy(c); FcitxMessagesSetMessageChanged(msgup, false); FcitxMessagesSetMessageChanged(msgdown, false); }