all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: 黄建忠 <jianzhong.huang@i-soft.com.cn>
To: Kan-Ru Chen <kanru@kanru.info>
Cc: emacs-devel@gnu.org
Subject: Re: A patch for enforcing double-width CJK character display
Date: Thu, 12 Apr 2012 16:56:16 +0800	[thread overview]
Message-ID: <4F8698B0.2030703@i-soft.com.cn> (raw)
In-Reply-To: <4F85AE69.9050002@i-soft.com.cn>

[-- Attachment #1: Type: text/plain, Size: 2040 bytes --]

FRAME_COLUMN_WIDTH and FRAME_SPACE_WIDTH never changed, it only be 
initialized when first font loaded.

Here is a try to fix "text-scale-increase/decrease" CJK double width issue.

I did not want to change external codes, So introduce a dirty workaround 
and a global static variable "default_font_width", The comments in this 
patch explained the idea and necessity.
"get_default_font_width" function is called by "xftfont_text_extents" 
and "xftfont_draw" to get default font width correctly after scale.

Can anybody provide a clue how to catch the new width of default font 
via FRAME_PTR when scale happened?

于 2012年04月12日 00:16, 黄建忠 写道:
> Thanks for reply.
> I checked with "text-scale-increase" and it did not work.
> When scale happened, the space_width and column_width of frame did not 
> changed, I need find another way to get monospace font width after scale.
> But I think we can store the default font face to every xftfont_info 
> and when scale happened, re-caculate the space width of default face.
> Let me try, thanks for the feedback.
>
>
> 于 2012年04月11日 23:48, Kan-Ru Chen 写道:
>> [ CC:ed emacs-devel ]
>>
>> Hi!
>>
>> 黄建忠<jianzhong.huang@i-soft.com.cn>  writes:
>>
>>> Here is a patch to fix double-width issue of CJK fonts when use Xft
>>> backend , just like some terminal did.
>> I like you idea to draw glyphs individually when the font is a CJK font.
>> I didn't realized that when the font changes, XftDrawGlyphs is called
>> again so we have a chance to examine the font!
>>
>> Your patch probably needs some adjust to fit the coding style, but the
>> approach is great! Did you check that it also works with scaled fonts?
>> Like (text-scale-increase) multiple times.
>>
>>    Kanru
>>
>>
>
>


-- 
黄建忠
技术总监
普华基础软件股份有限公司 基础软件事业部

电话:010-82664919-8201
传真:010-82664407
手机:13501162460
网址:http://www.i-soft.com.cn

地址:中国北京市海淀区海淀东三街欧美汇大厦9层
邮编:100080


[-- Attachment #2: emacs-cjk-monospace-v5.patch --]
[-- Type: text/plain, Size: 6793 bytes --]

--- xftfont.c.orig	2012-04-12 15:18:25.108011233 +0800
+++ xftfont.c	2012-04-12 16:35:50.993433916 +0800
@@ -61,6 +61,7 @@
   Display *display;
   int screen;
   XftFont *xftfont;
+  FRAME_PTR frame; /* hold frame ptr, cjk double width fix need it */
 };
 
 /* Structure pointed by (struct face *)->extra  */
@@ -137,6 +138,70 @@
 }
 
 
+static int default_font_width;
+
+static int is_cjk_font(struct xftfont_info *);
+static int calc_cjk_padding(int char_width);
+static void get_default_font_width(struct xftfont_info *);
+
+/* 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 width padding according to default font width */
+static int
+calc_cjk_padding(int char_width)
+{
+    int padding = 0;
+    if( default_font_width == 0 || /* the default font is not a monospace font */
+        char_width < default_font_width || /* almost impossible */
+        char_width == default_font_width) /* ascii glyph from real monospace CJK font */
+      return 0;
+    /* get the padding, all cjk symbols is double width */
+    padding = default_font_width * 2 - char_width;
+    /* 1, auto matched CJK pcf/bdf fonts may bigger than 2 * default_font_width.
+       2, User may set a very big font size for script HAN.
+       Keep it unchanged, should NOT adjust default monospace font width. */
+    return padding > 0 ? padding : 0;                   
+}
+
+/* Hmm, This function is dirty....
+   Anyway, it fix text-scale-increase/decrease CJK double width issue.
+   Emacs should provide a way to get the default font width of frame when scale happened, but how?
+   After I tried FRAME_* macros, I give up, It is initialized when first font loaded and never changed.
+
+   This is the root cause why we need this function and "default_font_width" global variable.
+   here we get the default font family and compare with every xftfont_info structure,
+   If they are same, we catch the default font, then set "default_font_width" to new value. 
+*/
+static void 
+get_default_font_width(struct xftfont_info *xftfont_info)
+{
+  struct font *defaultfont = NULL;
+  if(defaultfont = FRAME_FONT(xftfont_info->frame)){ /* default font of frame */
+     Lisp_Object default_font_object;
+     Lisp_Object xftfont_object;
+     XSETFONT (default_font_object, defaultfont);
+     XSETFONT (xftfont_object, &xftfont_info->font);
+     if(!NILP(AREF (default_font_object, FONT_FAMILY_INDEX)) &&
+        !NILP(Fstring_equal (AREF (default_font_object, FONT_FAMILY_INDEX),
+                                    AREF (xftfont_object, FONT_FAMILY_INDEX) )) && 
+        INTEGERP (AREF (default_font_object, FONT_SPACING_INDEX)) &&
+        XINT(AREF (default_font_object, FONT_SPACING_INDEX)) == FONT_SPACING_MONO) /* ensure monospace */
+           default_font_width = xftfont_info->font.space_width;
+  }
+}
+
 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);
@@ -391,6 +456,10 @@
   xftfont_info->display = display;
   xftfont_info->screen = FRAME_X_SCREEN_NUMBER (f);
   xftfont_info->xftfont = xftfont;
+  /* to fix CJK double width alignment issue.
+     pass FRAME_PTR to every xftfont_info structure
+     Later, we can get default font of the frame via the ptr */
+  xftfont_info->frame = f;
   /* This means that there's no need of transformation.  */
   xftfont_info->matrix.xx = 0;
   if (FcPatternGetMatrix (xftfont->pattern, FC_MATRIX, 0, &matrix)
@@ -593,20 +662,30 @@
 {
   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);
+  /* get the default font width of the frame
+     and set default_font_width global variable used later by calc_cjk_padding. */
+  get_default_font_width(xftfont_info);
+  if(is_cjk_font(xftfont_info))
+    cjk_padding = calc_cjk_padding(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; /* cjk_padding maybe 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 *
@@ -660,13 +739,33 @@
     code[i] = ((XCHAR2B_BYTE1 (s->char2b + from + i) << 8)
 	       | XCHAR2B_BYTE2 (s->char2b + from + i));
 
+  /* get the default font width of the frame
+     and set default_font_width global variable used later by calc_cjk_padding. */
+  get_default_font_width(xftfont_info);
+
   if (s->padding_p)
     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(!is_cjk_font(xftfont_info)) /*not CJK, draw glyphs like before*/
+        XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont, x, y, code, len);
+      else /* draw glyphs one by one and adjust the offset */
+         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(extents.xOff);
+           if(cjk_padding) /* CJK symbol and have padding */
+             offset = default_font_width * i * 2 + (cjk_padding>>1);
+           else /* No padding, just use extents.xOff directly, real monospace CJK font works */ 
+             offset = extents.xOff * i; 
+           XftDrawGlyphs (xft_draw, &fg, xftfont_info->xftfont,
+                            x+offset, y, code+i, 1);
+         }
+  }
   UNBLOCK_INPUT;
 
   return len;

  reply	other threads:[~2012-04-12  8:56 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <4F85A138.6090900@i-soft.com.cn>
2012-04-11 15:48 ` A patch for enforcing double-width CJK character display Kan-Ru Chen
2012-04-11 16:16   ` 黄建忠
2012-04-12  8:56     ` 黄建忠 [this message]
2012-04-12  9:53       ` Eli Zaretskii
2012-04-12 11:18         ` 黄建忠
2012-04-12 14:27           ` Eli Zaretskii
2012-04-12 17:56             ` 黄建忠
2012-04-12 20:33               ` Stefan Monnier
     [not found]                 ` <4F8782C8.2030005@i-soft.com.cn>
2012-04-13 11:42                   ` 黄建忠
2012-04-13 12:03                     ` 黄建忠
2012-04-13 13:27                   ` Stefan Monnier
2012-04-15  5:10                     ` Miles Bader
2012-04-15 13:27                       ` 黄建忠
2012-04-15 16:08                       ` William Xu
2012-04-15 22:19                         ` Miles Bader
2012-04-16  0:51                           ` 黄建忠
2012-04-16  5:27                             ` Miles Bader
2012-04-16  5:40                               ` 黄建忠
2012-04-16  6:37                                 ` 黄建忠
2012-04-16  9:21                                   ` 黄建忠
2012-04-17  2:16                                     ` 黄建忠
2012-04-17  0:13                                   ` Miles Bader
2012-04-17  0:39                                     ` Miles Bader
2012-04-17  2:00                                       ` 黄建忠
2012-04-17  2:30                                         ` Miles Bader
2012-04-17  3:00                                           ` 黄建忠
2012-04-17  4:08                                             ` Miles Bader
2012-04-17  4:56                                               ` Werner LEMBERG
2012-04-17  5:02                                                 ` 黄建忠
2012-04-17  6:33                                                   ` Miles Bader
2012-04-17  7:03                                                   ` Werner LEMBERG
2012-04-17  5:52                                                 ` Miles Bader
2012-04-17  6:10                                                   ` 黄建忠
2012-04-17  7:02                                                     ` Miles Bader
2012-04-17  8:06                                                       ` Werner LEMBERG
2012-04-17  8:25                                                         ` Miles Bader
2012-04-17  9:06                                                           ` Werner LEMBERG
2012-04-17  8:51                                                       ` 黄建忠
2012-04-17  6:45                                                   ` Werner LEMBERG
2012-04-17  9:07                                       ` James Cloos
2012-04-17  9:27                                         ` 黄建忠
2012-04-17  1:47                                     ` 黄建忠
2012-04-18  6:54                               ` Kenichi Handa
2012-04-18  8:13                                 ` 黄建忠
2012-04-18 13:58                                 ` Miles Bader
2014-04-28  5:35 JunJie Nan
2014-04-29  5:39 ` Stefan Monnier
2014-04-29  6:36   ` Jan D.
2014-04-29  8:16   ` Thien-Thi Nguyen
2014-04-29 20:41 ` Liang Wang
  -- strict thread matches above, loose matches on Subject: below --
2014-04-30  2:00 Hui Liu
2014-04-30 17:08 ` Liang Wang
2014-10-04  3:26 Feng Shu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4F8698B0.2030703@i-soft.com.cn \
    --to=jianzhong.huang@i-soft.com.cn \
    --cc=emacs-devel@gnu.org \
    --cc=kanru@kanru.info \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.