ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* [NTG-context] Is there any way to automatically control the spacing between Chinese and non-Chinese characters?
@ 2024-10-01  6:02 ai2472206007
  2024-10-02  4:56 ` [NTG-context] " 黄复雄
  0 siblings, 1 reply; 3+ messages in thread
From: ai2472206007 @ 2024-10-01  6:02 UTC (permalink / raw)
  To: ntg-context

hi, 
Is there any way to automatically control the spacing between Chinese and non-Chinese characters?
I noticed that there is a tracker that tracks character types and annotates them. 
But I don't know much about lua and the underlying tex. 
Any suggestions would be greatly appreciated.

For example, glue is automatically added between Chinese characters and English or numbers.

input: 今天出去买菜花了5000元。 
will get: 今天出去买菜花了 5000 元。
---------------------------------- 
input: 新MacBook Pro有15%的CPU性能提升。 
will get: 新 MacBook Pro 有 15% 的 CPU 性能提升。

Muyik
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [NTG-context] Re: Is there any way to automatically control the spacing between Chinese and non-Chinese characters?
  2024-10-01  6:02 [NTG-context] Is there any way to automatically control the spacing between Chinese and non-Chinese characters? ai2472206007
@ 2024-10-02  4:56 ` 黄复雄
  2024-10-02 11:36   ` ai2472206007
  0 siblings, 1 reply; 3+ messages in thread
From: 黄复雄 @ 2024-10-02  4:56 UTC (permalink / raw)
  To: mailing list for ConTeXt users

This is a preliminary solution for inserting spaces between Chinese
characters and non-Chinese characters

```ConTeXt
\mainlanguage[cn]
\language[cn]
\setscript[hanzi]
\usetypescriptfile[mscore]
\usebodyfont   [mschinese,12pt]

\showglyphs

\startluacode
Thirddata = Thirddata or {}

local glyph_id = nodes.nodecodes.glyph --node.id("glyph")
local node_insertbefore = node.insertbefore
local node_insertafter = node.insertafter
local node_new = node.new
local tex_sp = tex.sp

local function ischinesechar(c)
    -- for more ranges:
    -- https://wiki.contextgarden.net/List_of_Unicode_blocks
    return (c >= 0x04E00 and c <= 0x09FFF)
        or (c >= 0x03400 and c <= 0x04DBF)
        or (c >= 0x20000 and c <= 0x2A6DF)
end

function Thirddata.processmystuff(head)
    local n = head
    while n do
        if n.id == glyph_id and ischinesechar(n.char) then

            local n_prev = n.prev
            if n_prev
                and n_prev.id == glyph_id
                and not ischinesechar(n_prev.char)
                then
            local glue = node_new("glue")
            glue.width = tex_sp("0.25em")
            glue.stretch = tex_sp("0.25em")
            print("insert space before:", utf8.char(n.char))
            head, glue = node_insertbefore(head, n, glue)
            end

            local n_next = n.next
            if n_next
                and n_next.id == glyph_id
                and not ischinesechar(n_next.char)
                then
            local glue = node_new("glue")
            glue.width = tex_sp("0.25em")
            glue.stretch = tex_sp("0.25em")
            print("insert space after:", utf8.char(n.char))
            head, glue = node_insertafter(head, n, glue)
            n = glue.next
            end
        end
        n = n.next
    end
    return head, done
end

nodes.tasks.appendaction("processors", "after", "Thirddata.processmystuff")


\stopluacode
\starttext
今天出去买菜花了5000元。

新MacBook Pro有15\%的CPU性能提升。
\stoptext
```

On Tue, Oct 1, 2024 at 2:07 PM <ai2472206007@yeah.net> wrote:
>
> hi,
> Is there any way to automatically control the spacing between Chinese and non-Chinese characters?
> I noticed that there is a tracker that tracks character types and annotates them.
> But I don't know much about lua and the underlying tex.
> Any suggestions would be greatly appreciated.
>
> For example, glue is automatically added between Chinese characters and English or numbers.
>
> input: 今天出去买菜花了5000元。
> will get: 今天出去买菜花了 5000 元。
> ----------------------------------
> input: 新MacBook Pro有15%的CPU性能提升。
> will get: 新 MacBook Pro 有 15% 的 CPU 性能提升。
>
> Muyik
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to the Wiki!
>
> maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
> webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
> archive  : https://github.com/contextgarden/context
> wiki     : https://wiki.contextgarden.net
> ___________________________________________________________________________________
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [NTG-context] Re: Is there any way to automatically control the spacing between Chinese and non-Chinese characters?
  2024-10-02  4:56 ` [NTG-context] " 黄复雄
@ 2024-10-02 11:36   ` ai2472206007
  0 siblings, 0 replies; 3+ messages in thread
From: ai2472206007 @ 2024-10-02 11:36 UTC (permalink / raw)
  To: ntg-context

Thank you very much. it works well.
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-10-02 11:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-01  6:02 [NTG-context] Is there any way to automatically control the spacing between Chinese and non-Chinese characters? ai2472206007
2024-10-02  4:56 ` [NTG-context] " 黄复雄
2024-10-02 11:36   ` ai2472206007

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 NNTP newsgroup(s).