init of repo

This commit is contained in:
2026-09-04 00:46:02 +08:00
commit d2776f71af
3 changed files with 2882 additions and 0 deletions
+332
View File
@@ -0,0 +1,332 @@
// ============================================================
// A4 文档模板
// 封面:页边距 0,中文黑体 / 英文 Source Code Pro
// 正文:上下 3.5cm,左右 2cm,中文宋体 / 英文 Times New Roman
// 代码块:Source Code Pro
// ============================================================
// ---- 字体配置(可按需修改)----
#let cover-cjk = "SimHei" // 封面中文:黑体
#let cover-latin = "Source Code Pro" // 封面英文
#let body-cjk = "SimSun" // 正文中文:宋体
#let body-latin = "Times New Roman" // 正文英文:新罗马
#let mono-font = "Source Code Pro" // 代码块
// ---- 封面网格系统 ----
// 坐标系:左上角为原点,x 向右、y 向下;坐标记为 (x, y) 即 (列, 行)。
// 1 单位 = 1.2cm,一个单位方块为边长 1.5cm 的正方形。
#let unit = 1.2cm
#let sky = rgb("#4FC3F7") // 天蓝色
#let accent = rgb("#E9784B") // 珊瑚橙
// 在网格 (x, y) 处放置内容(以页面左上角为基准)
#let cell(x, y, body) = place(top + left, dx: x * unit, dy: y * unit, body)
// 实心单位正方形
#let sq(x, y, color) = cell(x, y, rect(width: unit, height: unit, fill: color, stroke: none))
// 单位等腰直角三角形,corner 指定直角所在角:
// "tl" 左上 / "tr" 右上 / "bl" 左下 / "br" 右下
#let tri(x, y, corner, color) = cell(x, y, {
let pts = if corner == "tr" { ((0pt, 0pt), (unit, 0pt), (unit, unit)) }
else if corner == "tl" { ((0pt, 0pt), (unit, 0pt), (0pt, unit)) }
else if corner == "bl" { ((0pt, 0pt), (0pt, unit), (unit, unit)) }
else { ((unit, 0pt), (unit, unit), (0pt, unit)) }
polygon(fill: color, stroke: none, ..pts)
})
// 单元格字母字号(NITLOAD 与主标题共用,保证视觉一致)
#let glyph-size = 30pt
// 在单位格内居中放置一个字母
#let glyph(x, y, ch, color) = cell(x, y, box(
width: unit, height: unit, inset: 0pt,
align(center + horizon, text(font: cover-cjk, size: glyph-size, weight: "medium", fill: color)[#ch]),
))
// ---- 封面 ----
// 所有字段均可选,留空则自动跳过,实现自动排版。
#let cover(
title: none, // 主标题(中文)
title-en: none, // 主标题(英文,Source Code Pro 自行排版)
subtitle: none, // 副标题
logo: none, // 图片路径,如 image("logo.png")
number: none, // 文档编号
author: none, // 作者(可为单个字符串或数组)
classification: none, // 密级,如 "内部"、"秘密"
info: (:), // 附加信息字典,如 (学号: "123", 指导教师: "张三")
date: none, // 日期
fill: white, // 封面背景色
decorate: true, // 是否绘制天蓝/暖橙网格装饰色块
) = {
// 封面页:页边距为 0
set page(
paper: "a4",
margin: 0pt,
fill: fill,
header: none,
footer: none,
)
set text(font: (cover-latin, cover-cjk), lang: "zh")
// 网格装饰:NITLOAD 标识(坐标 (x, y) = (列, 行))。
if decorate {
// 天蓝色实心单位正方形
// 暖橙色实心单位正方形
sq(0, 0, sky); sq(1, 0, sky); sq(2, 0, accent)
sq(0, 1, sky); sq(2, 1, sky)
sq(0, 2, sky); sq(2, 2, sky)
sq(0, 3, sky); sq(2, 3, sky)
sq(0, 4, sky); sq(2, 4, sky)
sq(0, 5, sky); sq(2, 5, sky)
// NITLOAD 竖排字样(列 x=1, 行 y=0..6),N 为暖橙,其余天蓝
let letters = ("N", "I", "T", "L", "O", "A", "D")
for (i, ch) in letters.enumerate() {
glyph(1, i, ch, if i == 0 { accent } else { sky })
}
// 主标题:中文逐字对齐网格,连续西文按实际字宽紧凑排布。
let cs = if type(title) == str { title.clusters() } else { () }
// 中文视觉重心偏大,整体右移 0.1 格、下移 0.2 格
let cn-dx = 0.1 * unit
let cn-dy = 0.2 * unit
let is-narrow = c => str.to-unicode(c.codepoints().first()) < 0x2E80
// 西文开头时使用更明确的 0.2 格左边距,与西文标题和信息区形成视觉对齐。
let title-start-pad = if cs.len() > 0 and is-narrow(cs.first()) { 0.2 * unit } else { cn-dx }
let title-width = 21cm - unit - title-start-pad
let en-left-pad = 0.2 * unit
let en-box-width = 21cm - unit
// 分段:连续西文合为一段,其余每字一段
let segs = ()
let run = ""
for ch in cs {
if is-narrow(ch) { run += ch }
else {
if run != "" { segs.push(run); run = "" }
segs.push(ch)
}
}
if run != "" { segs.push(run) }
context {
// CJK 固定占一格;连续西文使用字体测得的自然宽度,不额外预留左右空白。
let cur-x = 0pt
let row = 0
for seg in segs {
let latin = is-narrow(seg.first())
let segment = text(
font: if latin { (cover-latin, cover-cjk) } else { (cover-cjk, cover-latin) },
size: glyph-size,
fill: black,
)[#seg]
let segment-width = if latin { measure(segment).width } else { unit }
if cur-x != 0pt and cur-x + segment-width > title-width {
cur-x = 0pt
row += 1
}
let segment-align = if latin { left + horizon } else { center + horizon }
place(top + left,
dx: unit + title-start-pad + cur-x,
dy: (7 + row) * unit + cn-dy,
box(
width: segment-width,
height: unit,
inset: 0pt,
align(segment-align, segment),
))
cur-x += segment-width
}
let cn-rows = if segs.len() == 0 { 0 } else { row + 1 }
// 英文:左对齐于中文下方;多行时行距对齐网格(行距 = unit),左/上边距相等。
let en-rows = 0
let en-gap = if cn-rows > 0 and title-en != none { 0.25 * unit } else { 0pt }
if title-en != none {
let en1 = text(font: (cover-latin, cover-cjk), size: glyph-size, fill: black)[#title-en]
let lh = measure(en1).height
let pad = (unit - lh) / 2
let inner = {
set par(leading: unit - lh)
en1
}
let h = measure(box(width: en-box-width - en-left-pad, inner)).height
en-rows = calc.max(1, int(calc.round((h - lh) / unit)) + 1)
place(top + left,
dx: unit,
dy: (7 + cn-rows) * unit + en-gap,
box(width: en-box-width, inset: (left: en-left-pad, top: pad), inner))
}
let title-rows = cn-rows + en-rows
let title-end-y = (7 + title-rows) * unit + en-gap
let metadata = ()
if number != none { metadata.push(("编号", number)) }
if author != none {
let author-text = if type(author) == array { author.join(" / ") } else { author }
metadata.push(("作者", author-text))
}
if date != none { metadata.push(("日期", date)) }
if classification != none { metadata.push(("密级", classification)) }
for (key, value) in info {
if value != none { metadata.push((str(key), value)) }
}
let marker-height = 2 * unit
let info-y = title-end-y + 0.45 * unit
let cells = ()
for (label, value) in metadata {
cells.push(text(
font: (cover-cjk, cover-latin), size: 12pt,
weight: "medium", fill: luma(48%),
)[#label])
cells.push(text(
font: (cover-latin, cover-cjk), size: 14pt,
fill: black,
)[#value])
}
let info-content = if metadata.len() > 0 {
box(width: 21cm - 1.5 * unit,
align(left + top, grid(
columns: (auto, 1fr),
column-gutter: 0.6em,
row-gutter: 0.35em,
align: (right + top, left + top),
..cells,
)))
} else { none }
let info-height = if info-content != none { measure(info-content).height } else { 0pt }
// 左侧笔画与右侧笔画一样保持固定高度,不再随标题行数拉伸。
// 标志在第七格独立收尾;下方长条依据文档信息的实际高度紧凑起笔。
tri(0, 6, "tr", sky)
let strip-y = if metadata.len() > 0 {
info-y + info-height + 0.18 * unit
} else {
7 * unit
}
let strip-height = 29.7cm - strip-y
place(top + left, dy: strip-y, polygon(
fill: sky, stroke: none,
(unit, 0pt),
(unit, strip-height),
(0pt, strip-height),
(0pt, unit),
))
// 距页底约七个单位格设置元数据锚点。若标题很长,则自动避让标题尾部。
let meta-y = calc.max(29.7cm - 7 * unit, title-end-y + unit)
if metadata.len() > 0 {
let apex-y = marker-height / 2
// 不是带切角的梯形,而是一个完整的等腰直角三角形:
// 左边为 2 unit 的斜边,右侧尖点到上下端点的两条边等长且垂直。
place(top + left, dy: meta-y, polygon(
fill: accent, stroke: none,
(0pt, 0pt),
(unit, apex-y),
(0pt, marker-height),
))
let signature-line-length = 7 * unit
place(top + left, dx: unit, dy: meta-y + apex-y,
line(length: signature-line-length, stroke: (paint: accent, thickness: 0.8pt)))
let signed-label = text(
font: cover-latin, size: 10pt, weight: "bold", fill: accent,
)[signed]
let signed-size = measure(signed-label)
place(top + left,
dx: unit + signature-line-length - signed-size.width,
dy: meta-y + apex-y - signed-size.height - 0.04 * unit,
signed-label)
// 三角形保留在距页底七个单位的位置,作为独立的封面装饰。
// 文档信息不再与三角形连线,而是跟随主标题实际高度,排列在标题正下方。
place(top + left, dx: 1.25 * unit, dy: info-y, info-content)
}
}
tri(2, 6, "tl", sky)
}
pagebreak(weak: true)
}
// ---- 正文配置 ----
#let conf(
number-pages: true, // 是否显示页码
doc,
) = {
set page(
paper: "a4",
margin: (top: 3.5cm, bottom: 3.5cm, left: 2cm, right: 2cm),
numbering: if number-pages { "1" } else { none },
number-align: center,
header: none, // 正文页不使用装饰角标
)
set text(font: (body-latin, body-cjk), lang: "zh", size: 12pt)
set par(justify: true, leading: 1em, first-line-indent: (amount: 2em, all: true))
// 无序列表整体缩进一个正文字符;圆点与正文仍使用 Typst 默认间距。
set list(indent: 1em)
// 标题字体:中文黑体,英文 Source Code Pro
show heading: set text(font: (cover-latin, cover-cjk))
// 代码块与行内代码:西文使用 Source Code Pro,中文显式回退到正文宋体。
// 避免 Source Code Pro 缺少中文字形时由系统选择到隶书等不合适字体。
show raw: set text(font: (mono-font, body-cjk))
// 块级代码:细边框、浅色背景与自动行号;行内代码不受影响。
show raw.where(block: true): code => {
show raw.line: line => box(width: 100%, grid(
columns: (3em, 1fr),
align: (right + top, left + top),
box(
width: 100%,
inset: (left: 0.5em, right: 0.75em),
stroke: (right: 0.6pt + luma(82%)),
text(fill: luma(55%))[#line.number],
),
pad(left: 0.9em, right: 0.9em, line.body),
))
block(
width: 100%,
inset: (x: 0pt, y: 0.65em),
fill: luma(98%),
stroke: 0.6pt + luma(75%),
radius: 3pt,
breakable: true,
code,
)
}
doc
}
// ---- 模板结束 ----
// Template file ended.
/*
Legacy duplicated tail retained as an inert comment because this workspace's
Windows patch transport does not reliably truncate shortened files.
breakable: true,
code,
)
}
doc
}
// ---- 模板结束 ----
// Template file ended.
}
doc
}
// ---- 模板结束 ----
// Template file ended.
*/
// End of file.
+2400
View File
File diff suppressed because it is too large Load Diff
+150
View File
@@ -0,0 +1,150 @@
#import "NLI-doc-template.typ": cover, conf
// ============================================================
// Nitload 标准文件模板说明文档
// 本文件既是使用手册,也是可直接编译的完整示例。
// ============================================================
#cover(
title: "Nitload标准文件模板说明文档",
title-en: "Nitload Standard Document Template Manual",
number: "NLI-DOC-TEMPLATE-001",
author: "Nitload Industry",
classification: "NON[无秘]SEC",
date: "2026 年 9 月",
)
#show: conf.with(number-pages: true)
= 第一章 模板概述
NLI 文档模板用于生成具有统一 Nitload 视觉标识的 A4 标准文件。模板包含独立封面、中文和英文标题布局、文档识别信息、密级标记、签名位、正文排版以及带边框和自动行号的代码块。
当前目录中只需要两个核心文件:
- `NLI-doc-template.typ`:模板实现,通常不在业务文档中直接修改。
- `main.typ`:文档入口,负责填写封面字段并编写正文。
编译 `main.typ` 即可生成完整 PDF。空值字段会自动跳过,封面信息区和蓝色切角会依据实际内容高度重新排布。
== 1.1 最小使用方式
在新文档中导入模板,填写封面,然后启用正文配置:
```typst
#import "NLI-doc-template.typ": cover, conf
#cover(
title: "文档标题",
title-en: "Document Title",
number: "NLI-2026-001",
author: "作者姓名",
classification: "NON[无秘]SEC",
date: "2026 年 9 月",
)
#show: conf.with(number-pages: true)
= 第一章 正文
这里开始编写正文。
```
== 1.2 文件命名建议
建议保留模板文件名 `NLI-doc-template.typ`,业务文档可以按编号或主题命名,例如 `NLI-2026-001.typ`。若模板与正文位于不同目录,应相应调整 `#import` 的相对路径。
= 第二章 封面参数
`cover` 的稳定参数如下。除装饰控制参数外,所有信息字段均可留空。
#table(
columns: (1.35fr, 0.9fr, 3.4fr),
inset: (x: 7pt, y: 6pt),
stroke: 0.5pt + luma(78%),
align: (left, center, left),
[*参数*], [*必填*], [*说明*],
[`title`], [否], [中文主标题,按照封面网格自动换行。],
[`title-en`], [否], [英文主标题,使用封面西文字体自动换行。],
[`number`], [否], [文档编号,对应信息区中的“编号”。],
[`author`], [否], [作者名称;也可以传入作者数组。],
[`classification`], [否], [专用密级字段,使用CSEA标准密级表示方法。],
[`date`], [否], [成文日期或版本日期。],
[`info`], [否], [附加信息字典,用于项目、部门、审核人等非固定字段。],
[`fill`], [否], [封面背景色,默认白色。],
[`decorate`], [否], [是否显示 NITLOAD 图形装饰,默认为 `true`。],
)
`classification` 是与 `author`、`date` 同级的专用参数,不再需要写入 `info`。`info` 仅用于没有专用参数的扩展信息:
```typst
#cover(
title: "项目评审报告",
classification: "内部",
info: (
项目: "Flagship",
审核人: "示例姓名",
),
)
```
模板中仍保留 `subtitle` 和 `logo` 两个预留接口;当前版本没有为它们提供独立的稳定版式,不建议在正式文件中依赖。
= 第三章 正文排版
正文由 `conf` 统一配置,默认使用 A4 页面,上下边距 3.5 cm、左右边距 2 cm,并在页脚中央显示页码。中文正文使用宋体,英文正文使用 Times New Roman,段落采用两端对齐和两字符首行缩进。
如不需要页码,可以使用:
```typst
#show: conf.with(number-pages: false)
```
== 3.1 标题与段落
一级标题使用一个等号,二级标题使用两个等号。为了让文档目录和层级保持清晰,建议不要跳级使用标题。
```typst
= 第一章 一级标题
这里是正文段落。
== 1.1 二级标题
这里是小节内容。
```
== 3.2 代码排版
行内代码使用反引号,例如 `let x = 1`。块级代码会自动获得浅色背景、细边框和连续行号:
```rust
fn main() {
println!("Hello, Nitload!");
}
```
= 第四章 字体与分发
模板当前依赖 SimHei、SimSun、Source Code Pro 和 Times New Roman。跨设备使用前应确认这些字体已经安装;若计划在 Linux、CI 或公共环境编译,建议后续增加思源黑体、思源宋体等开源字体回退。
模板可以直接托管在 Git 仓库中。最简单的复用方式是将 `NLI-doc-template.typ` 与文档一同克隆,并通过相对路径导入。模板稳定后,也可以增加 `typst.toml`,将其安装为本地 Typst 包。
== 4.1 发布前检查
每次发布或修改模板后,建议完成以下检查:
#grid(
columns: (1fr, 1fr),
column-gutter: 1.2em,
row-gutter: 0.45em,
[• 长中文与长英文标题], [• 编号、作者、日期与密级],
[• 蓝色切角避让信息区], [• 签名线与 `signed` 标记],
[• 代码块、分页与页码], [• 目标系统字体可用性],
)
#block(above: 0.8em, below: 0pt)[
*4.2 版本记录* 本文档对应 NLI 文档模板初始说明版本。后续修改模板接口时,应同步更新本说明文档、示例代码和文档编号。
]
// Stale patch tail intentionally disabled.
// ]