-
Notifications
You must be signed in to change notification settings - Fork 766
zh-format:0.1.0 #3455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
zh-format:0.1.0 #3455
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| MIT License | ||
|
|
||
| Copyright (c) 2025 KercyDing | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # zh-format | ||
|
|
||
| [](https://typst.app/universe/package/zh-format) [](https://opensource.org/licenses/MIT) | ||
|
|
||
| zh-format 是一个用于 Typst 的中文格式包,提供更适合中文排版的粗体、斜体和下划线处理方案。 | ||
|
|
||
| ## 用法 | ||
|
|
||
| ```typst | ||
| // 从 Typst Universe 导入 (推荐) | ||
| // #import "@preview/zh-kit:0.1.0": * | ||
|
|
||
| // 如果是本地开发或直接使用仓库代码 | ||
| #import "../lib.typ": * // 假设 lib.typ 在上一级目录 | ||
|
|
||
| #show: zh-format | ||
|
|
||
| 这是*粗体*、_斜体_和#underline[下划线]的效果。 | ||
|
|
||
| 这是#u(width: 8em, offset: 0.4em)[自定义下划线] | ||
| ``` | ||
|
|
||
| ### 主要功能 | ||
|
|
||
| #### 1. 粗体 (Bold) | ||
| - 中文使用描边方式 (`stroke: 0.02857em`) | ||
| - 英文使用原生字体加粗 | ||
| - 自动识别中英文并分别处理 | ||
|
|
||
| #### 2. 斜体 (Italic) | ||
| - 中文使用 `skew(ax: -18deg)` 倾斜变换 | ||
| - 英文使用原生 `style: "italic"` | ||
| - 智能处理中英文混排 | ||
|
|
||
| #### 3. 下划线 (Underline) | ||
| - 基本下划线: `#underline[文本]` | ||
| - 自定义宽度下划线: `#u(width: 8em)[文本]` | ||
|
|
||
| ### 完整示例 | ||
|
|
||
| 查看 `example/example.typ` 获取详细使用示例。 | ||
|
||
|
|
||
| ## 更新日志 | ||
|
|
||
| ### 0.1.0 | ||
| - 初始版本 | ||
| - 实现基础的粗体、斜体、下划线功能 | ||
| - 支持中英文混排智能识别 | ||
| - 添加自定义宽度下划线函数 `#u()` | ||
|
|
||
| ## 许可证 | ||
|
|
||
| [MIT License](LICENSE) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| #import "@preview/zh-kit:0.1.0": * | ||
| // #import "../lib.typ": * | ||
|
|
||
| #show: zh-format | ||
|
|
||
| #set page(paper: "a4", margin: 2cm) | ||
| #set text(font: ("Times New Roman", "SimSun"), size: 12pt) | ||
| #set par(justify: true, first-line-indent: 0em) | ||
| #set heading(numbering: "1.1.1") | ||
|
|
||
| = zh-format 库功能测试文档 | ||
|
|
||
| 本文档用于测试 zh-format 库的各项中文格式化功能。 | ||
|
|
||
| == 粗体效果测试 | ||
|
|
||
| === 纯中文粗体 | ||
| 这是*普通中文粗体文本*的效果展示。通过描边实现更好的视觉效果。 | ||
|
|
||
| *完整段落的中文粗体测试:中文字体的粗体处理采用描边方式,能够在不改变字形结构的前提下,实现更加清晰的加粗效果。这种方法特别适合宋体等衬线字体。* | ||
|
|
||
| === 纯英文粗体 | ||
| This is *bold English text* for testing purposes. | ||
|
|
||
| *Complete paragraph in bold English: The bold formatting for English text uses native font weight, which provides optimal rendering for Latin characters.* | ||
|
|
||
| === 中英文混合粗体 | ||
| *中英文混合 Mixed Chinese and English 粗体效果测试 bold text rendering* | ||
|
|
||
| == 下划线效果测试 | ||
|
|
||
| === 基本下划线 | ||
| 这是#underline[中文下划线]的效果。 | ||
|
|
||
| This is #underline[English underline] effect. | ||
|
|
||
| #underline[中英文混合下划线 Mixed underline test 测试] | ||
|
|
||
| === 自定义下划线函数 #u | ||
| 基本效果(需提供 width 参数):#u(width: 8em)[居中文本] | ||
|
|
||
| 调整偏移量:#u(width: 8em, offset: 0.4em)[较低的下划线] | ||
|
|
||
| + 填空题示例: | ||
|
|
||
| + 中国的首都是 #u(width: 4em)[]。 | ||
|
|
||
| + 1 + 1 = #u(width: 2em)[]。 | ||
|
|
||
| + Typst 是一个现代的 #u(width: 6em)[] 系统。 | ||
|
|
||
| + 签名栏:#h(1fr) #u(width: 8em)[签名] #h(2em) 日期:#u(width: 6em)[] | ||
|
|
||
| == 斜体效果测试 | ||
|
|
||
| === 纯中文斜体 | ||
| 这是_中文斜体文本_的效果,使用倾斜变换实现。 | ||
|
|
||
| _完整段落的中文斜体测试:中文斜体采用 skew 变换,倾斜角度为 -18 度,这样可以在保持字形清晰的同时实现倾斜效果。_ | ||
|
|
||
| === 纯英文斜体 | ||
| This is _italic English text_ using native font style. | ||
|
|
||
| _Complete paragraph in italic English: The italic formatting for English text uses the native italic font variant for optimal typography._ | ||
|
|
||
| === 中英文混合斜体 | ||
| _中文倾斜 and English italic 混合效果 mixed rendering test 测试_ | ||
|
|
||
| == 组合效果测试 | ||
|
|
||
| === 粗体 + 下划线 | ||
| *#underline[中文粗体下划线]* | ||
|
|
||
| *#underline[English bold underline]* | ||
|
|
||
| === 粗体 + 斜体 | ||
| *_中文粗斜体组合_* | ||
|
|
||
| *_English bold italic combination_* | ||
|
|
||
| === 斜体 + 下划线 | ||
| _#underline[中文斜体下划线]_ | ||
|
|
||
| _#underline[English italic underline]_ | ||
|
|
||
| === 三重组合 | ||
| *_#underline[中文粗斜体下划线]_* | ||
|
|
||
| *_#underline[English bold italic underline]_* | ||
|
|
||
| == 实际应用场景 | ||
|
|
||
| === 文章摘要 | ||
| *摘要:*本文介绍了一个用于 Typst 的中文格式化库 _zh-format_,该库提供了#underline[更适合中文排版]的粗体、斜体和下划线处理方案。传统的 LaTeX 或其他排版系统往往*直接套用西文规则*,导致中文显示效果不佳。 | ||
|
|
||
| === 强调重点 | ||
| 在学习过程中,我们需要特别注意以下几点: | ||
| - *重点内容*使用粗体标注 | ||
| - _次要强调_使用斜体表示 | ||
| - #underline[关键术语]使用下划线突出 | ||
|
|
||
| === 表格中的格式化 | ||
|
|
||
| #table( | ||
| columns: (1fr, 2fr), | ||
| [*功能*], [*说明*], | ||
| [粗体], [使用 `*text*` 或 `#strong[]` 实现], | ||
| [斜体], [使用 `_text_` 或 `#emph[]` 实现], | ||
| [下划线], [使用 `#underline[]` 或 `#u[]` 实现], | ||
| [自定义宽度], [使用 `#u(width: )[]` 指定固定宽度], | ||
| ) | ||
|
|
||
| == 特殊符号与标点 | ||
|
|
||
| === 中文标点符号 | ||
| *中文标点:,。!?;:""''《》【】()* | ||
|
|
||
| _中文标点:、…—·「」『』〈〉_ | ||
|
|
||
| === 混合标点 | ||
| *Mixed punctuation test: 中文,English, 混合! What? 测试。* | ||
|
|
||
| == 总结 | ||
|
|
||
| zh-format 库提供了三个主要功能: | ||
| + *setup-bold*:优化中文粗体显示 | ||
| + *setup-underline*:改进下划线渲染 | ||
| + *setup-emph*:智能处理中英文斜体 | ||
|
|
||
| 通过 `#show: zh-format` 可以一次性应用所有格式化规则,让您的中文文档排版更加美观专业。 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| // =========================== | ||
| // 格式化工具库 | ||
| // =========================== | ||
|
|
||
|
|
||
| // 粗体处理逻辑 | ||
| #let setup-bold(body) = { | ||
| show text.where(weight: "bold").or(strong): it => { | ||
| show regex("[\p{script=Han}!-・〇-〰—]+"): cn => { | ||
| set text(weight: "regular") | ||
| context { | ||
| set text(stroke: 0.02857em + text.fill) | ||
| cn | ||
| } | ||
| } | ||
| it | ||
| } | ||
| body | ||
| } | ||
|
|
||
| // 下划线处理逻辑 | ||
| #let setup-underline(body) = { | ||
| show underline: it => context { | ||
| let line_width = measure(it.body).width | ||
| box[ | ||
| #it.body | ||
| #place( | ||
| bottom + left, | ||
| dy: 0.15em, | ||
| line(length: line_width, stroke: 0.05em) | ||
| ) | ||
| ] | ||
| } | ||
| body | ||
| } | ||
|
|
||
| // 自定义下划线函数(#u): | ||
| // width 参数为必填命名参数,指定固定宽度,内容自动居中 | ||
| // 使用: #u(width: )[] | ||
| #let u(width: none, offset: 0.2em, body) = { | ||
| assert(width != none, message: "参数 width 是必填的") | ||
| context { | ||
| let line_width = width | ||
|
|
||
| box(width: line_width)[ | ||
| #align(center, body) | ||
| #place( | ||
| bottom + left, | ||
| dy: offset, | ||
| line(length: line_width, stroke: 0.05em) | ||
| ) | ||
| ] | ||
| } | ||
| } | ||
|
|
||
| // 中文检测 | ||
| #let has-chinese(content) = { | ||
| let text-content = if type(content) == str { | ||
| content | ||
| } else if content.has("text") { | ||
| content.text | ||
| } else if content.has("body") { | ||
| return has-chinese(content.body) | ||
| } else if content.has("children") { | ||
| return content.children.any(has-chinese) | ||
| } else { | ||
| return false | ||
| } | ||
| if type(text-content) == str { | ||
| text-content.codepoints().any(c => { | ||
| let code = str.to-unicode(c) | ||
| code >= 0x4E00 and code <= 0x9FFF | ||
| }) | ||
| } else { | ||
| false | ||
| } | ||
| } | ||
|
|
||
| // 斜体处理逻辑: | ||
| // - 英文:使用原生 italic | ||
| // - 空格:保持原样 | ||
| // - 其他(中文、数字、符号等):使用 skew(-18deg)倾斜 | ||
| #let setup-emph(body) = { | ||
| show emph: it => { | ||
| let process-content(content) = { | ||
| if type(content) == str { | ||
| let pattern = regex("( +)|([a-zA-Z]+)|([^ a-zA-Z]+)") | ||
| let matches = content.matches(pattern) | ||
|
|
||
| matches.map(m => { | ||
| let str = m.text | ||
| let first-char = str.at(0) | ||
| if first-char >= "A" and first-char <= "Z" or first-char >= "a" and first-char <= "z" { | ||
| box(inset: 0pt, outset: 0pt, text(style: "italic")[#str]) | ||
| } else if first-char == " " { | ||
| box(inset: 0pt, outset: 0pt, str) | ||
| } else { | ||
| box(skew(ax: -18deg)[#str]) | ||
| } | ||
| }).join() | ||
| } else if content.has("text") { | ||
| process-content(content.text) | ||
| } else if content.has("body") { | ||
| process-content(content.body) | ||
| } else if content.has("children") { | ||
| content.children.map(process-content).join() | ||
| } else { | ||
| let repr-str = repr(content) | ||
| if repr-str == "[ ]" { | ||
| box(inset: 0pt, outset: 0pt, content) | ||
| } else { | ||
| content | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if has-chinese(it.body) { | ||
| process-content(it.body) | ||
| } else { | ||
| it | ||
| } | ||
| } | ||
| body | ||
| } | ||
|
|
||
| // 统一格式化函数 | ||
| #let zh-format(body) = { | ||
| show: setup-bold | ||
| show: setup-underline | ||
| show: setup-emph | ||
| body | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| [package] | ||
| name = "zh-format" | ||
| version = "0.1.0" | ||
| compiler = "0.13.1" | ||
| repository = "https://github.com/KercyDing/zh-format" | ||
| entrypoint = "lib.typ" | ||
| authors = ["KercyDing <[email protected]>"] | ||
| license = "MIT" | ||
| description = "更好的Typst中文文本格式包(粗体、斜体、下划线)。A Typst package for better Chinese text formatting (bold, italic, underline)" | ||
| keywords = ["chinese", "cn", "format", "中文", "格式", "粗体", "斜体", "下划线"] | ||
| exclude = ["example/"] |
Uh oh!
There was an error while loading. Please reload this page.