--- emacs-24.1.50/src/xftfont.c 2012-04-13 00:47:16.703598929 +0800 +++ emacs-24.1.50.cjk/src/xftfont.c 2012-04-13 00:50:02.909608551 +0800 @@ -61,6 +61,8 @@ Display *display; int screen; XftFont *xftfont; + FRAME_PTR 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 */ @@ -137,6 +139,62 @@ } +static int is_cjk_font(struct xftfont_info *); +static int calc_cjk_padding(int, int); +static int frame_default_font_width(FRAME_PTR); + +/* 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 +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; +} + +/* Caculate the padding according to default font width */ +static int +calc_cjk_padding(int default_font_width, int char_width) +{ + int padding = 0; + if( default_font_width == 0 || /* default font is not monospace */ + char_width < default_font_width || /* almost impossible */ + char_width == default_font_width) /* already good */ + return 0; + /* get the padding, all cjk symbols is DOUBLE width */ + padding = default_font_width * 2 - char_width; + /* 1, Some old CJK pcf fonts may bigger than 2*default_font_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 : 0; +} + +/* + Get the width of default font from FRAME_PTR. + If it is monospace font, return the space_width(as same as font width) + else return 0. + font remap can be supported now. + Thanks to Eli. */ +static int +frame_default_font_width(FRAME_PTR f) +{ + int id = lookup_basic_face (f, DEFAULT_FACE_ID); + struct face *face = FACE_FROM_ID (f, id); + if(face && face->font) { + Lisp_Object font_object; + XSETFONT (font_object, face->font); + if(XINT(AREF (font_object, FONT_SPACING_INDEX)) != FONT_SPACING_MONO) + return 0; + return face->font->space_width; + } + return 0; +} + static Lisp_Object xftfont_list (Lisp_Object, Lisp_Object); static Lisp_Object xftfont_match (Lisp_Object, Lisp_Object); static Lisp_Object xftfont_open (FRAME_PTR, Lisp_Object, int); @@ -434,6 +492,14 @@ 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 "is_cjk_font" many times. */ + xftfont_info->is_cjk = is_cjk_font(xftfont_info); UNBLOCK_INPUT; font->ascent = xftfont->ascent; @@ -593,20 +659,27 @@ { 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 = calc_cjk_padding(frame_default_font_width(xftfont_info->frame), extents.xOff); + /* 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 * @@ -664,9 +737,28 @@ 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 { + if(!xftfont_info->is_cjk) + XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont, x, y, code, len); + else { + /* draw CJK glyphs one by one and adjust the offset */ + int default_font_width = frame_default_font_width(xftfont_info->frame); + for (i = 0; i < len; i++) { + int cjk_padding = 0; + int offset = 0; + XGlyphInfo extents; + XftGlyphExtents (xftfont_info->display, xftfont_info->xftfont, code+i, 1, + &extents); + cjk_padding = calc_cjk_padding(default_font_width,extents.xOff); + if(cjk_padding) + offset = default_font_width * i * 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;