unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: 黄建忠 <jianzhong.huang@i-soft.com.cn>
To: emacs-devel@gnu.org
Subject: Re: A patch for enforcing double-width CJK character display
Date: Mon, 16 Apr 2012 17:21:52 +0800	[thread overview]
Message-ID: <4F8BE4B0.7020303@i-soft.com.cn> (raw)
In-Reply-To: <4F8BBE1F.9080701@i-soft.com.cn>


[-- Attachment #1.1: Type: text/plain, Size: 4624 bytes --]

Hi, All,
Patch updated, add support for CJK half-width letters.

1, There are some forms in Japanese:
Full-width Hirakana/katakana
Half-width katakana
The width of Half-Width katakana equals to 1/2 of Full-width Katakana 
letters.

2, There are also some forms for Korean basic letters. Full-Width 
letters and Half-Width letters.

The half-width letters for J/K is ignored in previous patch, since the 
width is even smaller than English letters from monospace font.

The updated patch add support for such half-width letters and align it 
according to single English glyph width,
Normal CJK glyphs still use 2 * English glyph width as reference.

NOTE the second line in attachments, It shows the result of half-width 
katakana before/after the patch applied.

The link below provides a full list of such half-width letters.
http://svn.jacekowski.org/chromium/trunk/third_party/icu/source/data/translit/Fullwidth_Halfwidth.txt

于 2012年04月16日 14:37, 黄建忠 写道:
> [mail rejected again and again , resend to list]
>
> Hi, Miles.
>
> I had downloaded the font and try it with such settings:
>
> 1, LANG=ja_JP.utf8
> 2, remove all zh/ja fonts existed on my Linux, and just let it match 
> "kiloji" font.
> 3, set emacs font size 10 or 13.
>
> And got the result as attachments, It is readable and just looks like 
> before, but the alignment issue is fixed. (NOTE the alignment of 
> VERTICAL BAR)
>
> I think you have some misunderstanding about the fix and character 
> width and font width.
>
> 1, although the "kiloji" character width looks as small as monospace 
> font width, but the real pixel width of the glyph is already wide enough,
>    It's the font choose to leave too much space left and right of the 
> character.
>
> 2, for Monospace 10, the pixelsize is 14, then it matches a "koliji 
> 10" with pixelsize 12. that's to say,
> *the fix only added ONE pixel left and ONE pixel right.* Can  anybody 
> feel such a little alignment change?
>
> The patch should be good enough for you and I am sure you will never 
> fell the change of the alignment after the fix.
>
> Do not guess the result and just have a try :-D
>
>
> 于 2012年04月16日 13:40, 黄建忠 写道:
>> Got it, I will try this font.
>>
>> by the way, You can add a line to your .emacs.
>> (set-fontset-font "fontset-default" 'han "FONTFAMILY FONTSIZE" )
>> replace "FONTFAMILY" and FONTSIZE according to your environment.
>> And FONTSIZE can be ignored if you had no need to specify a size for 
>> this font.
>>
>> 于 2012年04月16日 13:27, Miles Bader 写道:
>>> 黄建忠<jianzhong.huang@i-soft.com.cn>  writes:
>>>> Would you please provide some example characters and such a font to
>>>> help us make it better?
>>> Here's an example from my (Debian) system; the font I chose in Emacs
>>> is "Droid Sans Mono"; the "x11 size" is 13 [which isn't exactly the
>>> size chosen in the UI; fontconfig sizes and X font sizes seem to be
>>> only loosely related... :( ]
>>>
>>> I set the font to "Droid Sans Mono", and the Japanese font Emacs
>>> automatically chose was "きろ字".  I don't know _why_ Emacs chose that
>>> font, as other apps don't seem to -- If I select Droid S M in GTK
>>> apps, for instance, they use something much better looking, probably
>>> "Droid Fallback" (which is a matching font for Droid S M).
>>>
>>> I've attached an image file showing what this looks like on my
>>> computer.
>>>
>>> The things I notice:
>>>
>>>    (1) The font chosen by Emacs for Japanese might be a bit odd, and
>>>    doesn't seem to match what other apps choose.
>>>
>>>    (2) The "きろ字" font is already pretty widely spaced, maybe near
>>>    the limit of readability IMO.
>>>
>>>    (3) It looks like forcing CJK alignment to 2*ASCII will increase the
>>>    width of characters in this font by about 30%.  Given the already
>>>    very wide spacing, I think the result might look funny.
>>>
>>>    [ (4) If I grow or shrink the font-size, the ASCII and Japanese grow
>>>    by different, and varying amounts (that is, there are obvious
>>>    "jumps" in the size increases, and the jumps occur at different
>>>    places for the ASCII font and the Japanese font); my guess is that
>>>    this is probably due to rounding by the font renderer.  So there
>>>    will be. ]
>>>
>>> Now that I think about it, I'd say that the problem seems to lie more
>>> with Emacs' choice of fonts for Japanese (both the funny automatic
>>> choice, and the lack of good methods for users to tweak it).
>>>
>>> Thanks,
>>>
>>> -Miles
>>>
>>>
>>>
>>
>>
>> -- 
>> Huang JianZhong
>
>
> -- 
> Huang JianZhong


-- 
Huang JianZhong


[-- Attachment #1.2: Type: text/html, Size: 6248 bytes --]

[-- Attachment #2: 2012-04-16-161636_205x195_scrot.png --]
[-- Type: image/png, Size: 30559 bytes --]

[-- Attachment #3: 2012-04-16-161751_197x212_scrot.png --]
[-- Type: image/png, Size: 29810 bytes --]

[-- Attachment #4: emacs-cjk-monospace-v10.patch --]
[-- Type: text/plain, Size: 5814 bytes --]

--- emacs/src/xftfont.c	2012-04-16 16:02:23.621041065 +0800
+++ emacs.cjk/src/xftfont.c	2012-04-16 16:01:33.707037366 +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,68 @@
 }
 
 
+static int xftfont_is_cjk_font(struct xftfont_info *);
+static int xftfont_get_cjk_padding(int, int, int*);
+static int xftfont_get_default_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
+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 || /* 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 : 0;                   
+}
+
+/* 
+   Get the width of default monospace font.
+   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 
+xftfont_get_default_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 +498,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 "xftfont_is_cjk_font" many times. */
+  xftfont_info->is_cjk = xftfont_is_cjk_font(xftfont_info);
   UNBLOCK_INPUT;
 
   font->ascent = xftfont->ascent;
@@ -593,20 +665,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 = xftfont_get_cjk_padding(xftfont_get_default_width(xftfont_info->frame), 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 *
@@ -664,9 +743,29 @@
     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_width = xftfont_get_default_width(xftfont_info->frame);
+         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;

  reply	other threads:[~2012-04-16  9:21 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     ` 黄建忠
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                                   ` 黄建忠 [this message]
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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=4F8BE4B0.7020303@i-soft.com.cn \
    --to=jianzhong.huang@i-soft.com.cn \
    --cc=emacs-devel@gnu.org \
    /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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).