diff --git a/src/xftfont.c b/src/xftfont.c index 18c180f..2020b4f 100644 --- a/src/xftfont.c +++ b/src/xftfont.c @@ -60,6 +60,8 @@ struct xftfont_info Display *display; XftFont *xftfont; unsigned x_display_id; + struct frame *frame; /* hold frame ptr, cjk double width fix need it */ + int is_cjk; /* Flag to tell if it is CJK font or not. */ }; /* Structure pointed by (struct face *)->extra */ @@ -133,6 +135,83 @@ xftfont_get_colors (struct frame *f, struct face *face, GC gc, } +/* Check whether the font contains CJK Ideograph 'number one', 0x4E00, + It should be ok for Chinese/Japanese font. + Or font contains Korean script syllable 'Ka',0xAC00, + because Korean fonts may not have any Chinese characters at all. + codes from xterm.*/ +static int +xftfont_is_cjk_font(struct xftfont_info *xftfont_info) +{ + if(XftCharExists(xftfont_info->display, xftfont_info->xftfont, 0x4E00) || + XftCharExists(xftfont_info->display, xftfont_info->xftfont, 0xAC00)) + return 1; + return 0; +} + +/* Get the padding according to default monospace font width */ +static int +xftfont_get_cjk_padding(int default_width, int char_width, int *half_width_cjk) +{ + int padding = 0; + if(half_width_cjk) + *half_width_cjk = 0; + + if( default_width == 0 || /* something wrong */ + default_width == -1 || /* default font is not monospace */ + char_width == default_width) /* already good */ + return 0; + if( char_width < default_width) { + /* almost impossible, but we can handle it */ + padding = default_width - char_width; + if(half_width_cjk) + *half_width_cjk = 1; + } else /* get the padding, all cjk symbols is DOUBLE width */ + padding = default_width * 2 - char_width; + /* 1, Some old CJK pcf fonts may bigger than 2*default_width. + 2, User may set a very big font size for script HAN manually. + Keep it unchanged, NOT adjust default font width. */ + return (padding > 0 && padding < default_width) ? padding : 0; +} + +/* Get the font width of monospace font. + Something wrong: return 0; + Default is not monospace: return -1; + Otherwise: return monospace font width; */ +static int +xftfont_get_default_width(struct xftfont_info *xftfont_info) +{ + Lisp_Object font_object; + double request_pixel_size; + struct frame *frame = xftfont_info->frame; + FcPattern * pattern = xftfont_info->xftfont->pattern; + int id = lookup_basic_face (frame, DEFAULT_FACE_ID); + struct face *face = FACE_FROM_ID (frame, id); + if(!face && !face->font) + return 0; + XSETFONT (font_object, face->font); + /* default font is not monospace */ + if (XINT(AREF (font_object, FONT_SPACING_INDEX)) != FONT_SPACING_MONO) + return -1; + if(FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &request_pixel_size) != FcResultMatch) + return 0; + /* the font of minibuf/modeline never changed when rescaling. + it's a little bit difficult to determine where the xftfont draw to. + for example, when use mule/leim to input some CJK text. + it will draw on the selected frame and minibuf(candidate string). + by the way, the 'int' conversion should be ok. */ + if(FRAME_FONT(frame)->pixel_size == (int)request_pixel_size) + return FRAME_FONT(frame)->space_width; + /* User may set a fixed font size for script han manually, + the font will not rescale when issue rescaling, + should not ajust the width offset and avoid to make it too wide. + its alignment will be wrong, but let's respect user settings.*/ + if(face->font->pixel_size != (int)request_pixel_size) + return FRAME_FONT(frame)->space_width; + /* otherwise return the current font width */ + return face->font->space_width; +} + struct font_driver xftfont_driver; static Lisp_Object @@ -420,6 +499,15 @@ xftfont_open (struct frame *f, Lisp_Object entity, int pixel_size) XftTextExtents8 (display, xftfont, ascii_printable + 1, 94, &extents); font->average_width = (font->space_width + extents.xOff) / 95; } + + /* to fix CJK double width alignment issue. + pass FRAME_PTR to every xftfont_info structure, + we can not get it in "xftfont_text_extents". */ + xftfont_info->frame = f; + /* mark it is CJK font or not when font opened, + avoid calling "xftfont_is_cjk_font" many times. */ + xftfont_info->is_cjk = xftfont_is_cjk_font(xftfont_info); + unblock_input (); font->ascent = xftfont->ascent; @@ -588,19 +676,29 @@ xftfont_text_extents (struct font *font, unsigned int *code, int nglyphs, struct struct xftfont_info *xftfont_info = (struct xftfont_info *) font; XGlyphInfo extents; + int cjk_padding = 0; + int l_padding = 0; + int r_padding = 0; + block_input (); XftGlyphExtents (xftfont_info->display, xftfont_info->xftfont, code, nglyphs, &extents); + if(xftfont_info->is_cjk) + cjk_padding = xftfont_get_cjk_padding(xftfont_get_default_width(xftfont_info), extents.xOff, NULL); + /* cjk_padding may equals to 0, then all is zero, still ok */ + l_padding = cjk_padding >> 1; /* get half */ + r_padding = cjk_padding - l_padding; /* may not divided by 2 exactly */ + unblock_input (); if (metrics) { - metrics->lbearing = - extents.x; - metrics->rbearing = - extents.x + extents.width; - metrics->width = extents.xOff; + metrics->lbearing = - extents.x - l_padding; + metrics->rbearing = - extents.x + extents.width + r_padding; + metrics->width = extents.xOff + cjk_padding; metrics->ascent = extents.y; metrics->descent = extents.height - extents.y; } - return extents.xOff; + return extents.xOff + cjk_padding; } static XftDraw * @@ -658,9 +756,28 @@ xftfont_draw (struct glyph_string *s, int from, int to, int x, int y, for (i = 0; i < len; i++) XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont, x + i, y, code + i, 1); - else - XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont, - x, y, code, len); + else { + int default_width = xftfont_get_default_width(xftfont_info); + if(!xftfont_info->is_cjk || default_width == -1) /* not cjk or default not monospace */ + XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont, x, y, code, len); + else /* draw CJK glyphs one by one and adjust the offset */ + for (i = 0; i < len; i++) { + int cjk_padding = 0; + int offset = 0; + int half_width_cjk = 0; + XGlyphInfo extents; + XftGlyphExtents (xftfont_info->display, xftfont_info->xftfont, code+i, 1, + &extents); + cjk_padding = xftfont_get_cjk_padding(default_width,extents.xOff, &half_width_cjk); + if(cjk_padding) + offset = default_width * i * (half_width_cjk ? 1 : 2) + (cjk_padding>>1); + else + offset = extents.xOff * i; + XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont, + x+offset, y, code+i, 1); + } + } + unblock_input (); return len;