all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: tumashu  <tumashu@163.com>
To: "emacs-devel@gnu.org" <emacs-devel@gnu.org>
Subject: Re:Re:Any new method to deal with Emacs Fonts settings puzzles nearly every Chinese emacser?
Date: Sat, 13 Nov 2021 23:03:10 +0800 (CST)	[thread overview]
Message-ID: <7b6b298a.222b.17d19d2e110.Coremail.tumashu@163.com> (raw)
In-Reply-To: <593d0379.21f8.17d19c624db.Coremail.tumashu@163.com>

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

I have found a hack patch to deal with this problem: it is created by cjacker in 2012,

After this patch can not apply, I created cnfonts as a Chinese new emacser.






--- emacs-23.4/src/xftfont.c    2012-01-15 10:53:31.000000000 +0800
+++ emacs-23.4n/src/xftfont.c    2012-04-18 08:59:40.218009268 +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  */
@@ -141,6 +143,86 @@
     }
 }
 
+static int xftfont_is_cjk_font(struct xftfont_info *);
+static int xftfont_get_cjk_padding(int, int, int*);
+static int xftfont_get_default_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
+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;
+  FRAME_PTR 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_SPACE_WIDTH(frame);
+  /* 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_SPACE_WIDTH(frame);
+  /* otherwise return the current font width */
+  return face->font->space_width;
+}
 
 static Lisp_Object xftfont_list P_ ((Lisp_Object, Lisp_Object));
 static Lisp_Object xftfont_match P_ ((Lisp_Object, Lisp_Object));
@@ -450,6 +532,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;
@@ -625,20 +715,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), 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 *
@@ -699,9 +796,27 @@
     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;



















At 2021-11-13 22:49:15, "tumashu" <tumashu@163.com> wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>At 2021-11-13 22:36:11, "tumashu" <tumashu@163.com> wrote:
>>
>>Hello everyone:
>>
>>
>>
>>   (1)    How to let A Chinese char width = 2 * Ascii char width is an eternal topic for Chinese new emacser.
>>
>>
>>it let all Chinese new users and many old users puzzle a lot, that is the reason my cnfonts package (a Chinese fonts setup utils) get >600 github star.
>>recent day, we talk about Emacs fonts, so I want to know, is it possible provide a feature in emacs core to solve this problem?
>>The way cnfonts provide just a hack way I think. https://github.com/tumashu/cnfonts
>>
>>
>>
>>The two question is that: How to force set min line-width to a value?  Chinese emaser use will use two fonts generally, a Ascii fonts and A Chinese fonts.
>
>Sorry, is min line-height, not min line-width.
>
>>Two fonts will use different height to let 1 Chinese Char width = 2 * Ascii char width,  but this set will get other problem:
>>
>>
>>     (2)                     Chinese char Height > Ascii char height,
>>
>>
>>The results is that head-line, mode-line and minibuffer's height will shark (change frequently depend Chinese char exist or not).
>>it looks very very ugly.
>>
>>
>>These two questions make Chinese Emacs new users feel that Emacs is particularly low !!!
>>
>>
>>Thanks!
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>

[-- Attachment #2: Type: text/html, Size: 12924 bytes --]

  reply	other threads:[~2021-11-13 15:03 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-13 14:36 Any new method to deal with Emacs Fonts settings puzzles nearly every Chinese emacser? tumashu
2021-11-13 14:49 ` tumashu
2021-11-13 15:03   ` tumashu [this message]
2021-11-13 15:32     ` Any " Eli Zaretskii
2021-11-13 16:19       ` Feng Shu
2021-11-13 16:42         ` Eli Zaretskii
2021-11-13 19:35           ` Feng Shu
2021-11-13 20:03             ` Eli Zaretskii
2021-11-13 23:38               ` Feng Shu
2021-11-14  0:28                 ` Eric Abrahamsen
2021-11-14  7:37                   ` Eli Zaretskii
2021-11-14  7:51                     ` Po Lu
2021-11-14  9:07                       ` Werner LEMBERG
2021-11-14  9:38                         ` Po Lu
2021-11-15  7:20                           ` Byung-Hee HWANG
2021-11-14  7:31                 ` Eli Zaretskii
2021-11-14  7:36                   ` Po Lu
2021-11-14  8:09                     ` Eli Zaretskii
2021-11-14  8:24                       ` Feng Shu
2021-11-14  9:41                       ` Po Lu
2021-11-14 11:33                         ` Eli Zaretskii
2021-11-14 11:57                           ` Po Lu
2021-11-15  0:36                       ` tumashu
2021-11-15  0:48                       ` tumashu
2021-11-15 12:43                         ` Eli Zaretskii
2021-11-15 12:47                           ` Po Lu
2021-11-15 13:55                             ` Eli Zaretskii
2021-11-13 15:21 ` Eli Zaretskii
2021-11-13 16:04   ` Feng Shu
2021-11-13 16:12   ` Feng Shu
2021-11-13 16:36     ` Eli Zaretskii
2021-11-13 19:33       ` Feng Shu
2021-11-13 19:54         ` Feng Shu
2021-11-13 20:06           ` Eli Zaretskii
2021-11-13 23:31             ` Feng Shu
2021-11-14  6:48               ` Eli Zaretskii
2021-11-14  7:03                 ` Feng Shu
2021-11-13 20:02         ` Eli Zaretskii
2021-11-13 23:29           ` Feng Shu
2021-11-13 23:14   ` Phil Sainty
2021-11-14  0:02     ` Feng Shu
2021-11-14  0:18       ` Feng Shu
2021-11-14  7:36         ` Eli Zaretskii
2021-11-14  7:54           ` Po Lu
2021-11-14  8:14             ` Eli Zaretskii
2021-11-14  9:43               ` Po Lu
2021-11-14  6:45     ` Eli Zaretskii
2021-11-14  7:17       ` Feng Shu
2021-11-14  8:00         ` Eli Zaretskii
2021-11-14  1:12   ` Po Lu
2021-11-14  1:52     ` Feng Shu
2021-11-14  7:55     ` Eli Zaretskii

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=7b6b298a.222b.17d19d2e110.Coremail.tumashu@163.com \
    --to=tumashu@163.com \
    --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 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.