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 <ai2472206...@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
___________________________________________________________________________________

Reply via email to