Skip to content

Commit

Permalink
pointless-size:0.1.1 (#1657)
Browse files Browse the repository at this point in the history
  • Loading branch information
YDX-2147483647 authored Feb 3, 2025
1 parent 2fafa4e commit 4443c9e
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/preview/pointless-size/0.1.1/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Y.D.X.

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.
74 changes: 74 additions & 0 deletions packages/preview/pointless-size/0.1.1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Typst Pointless Size——字号 zìhào

中文字号的号数制及字体度量单位。
Chinese size system (hào-system) and type-related measurements units.

```typst
#import "@preview/pointless-size:0.1.1": zh, zihao
#set text(size: zh(5)) // 五号(10.5pt)
// or
#set text(zh(5))
#show: zihao(5)
// 小号用负数表示 use negative numbers for small sizes
#zh(-4) // 小四(12pt)
#zh(1) // 一号(26pt)
#zh(-1) // 小一(24pt)
#zh("-0") // 小初(36pt)
#zh(0) // 初号(42pt)
// 写汉字也可以 Han characters are also acceptable
#zh("五号")
#zh("五")
#zh("小五")
```

![zihao](https://github.com/user-attachments/assets/585d3016-5e7e-46fe-8e16-befcfe1ee6a3)
<!--
#import "@preview/pointless-size:0.1.0": zh
#set page(width: auto, height: auto, margin: 1em)
#table(
columns: 3,
align: left + horizon,
stroke: none,
table.hline(),
[号数], [点数], [意义],
table.hline(stroke: 0.5pt),
..(
(0, "初号"),
("-0", "小初"),
..range(1, 9).map(n => (
(n, numbering("一号", n)),
..if n < 7 {
(-n, numbering("小一", n))
},
)),
).flatten().chunks(2).map(((n, t)) => (
raw("zh(" + repr(n) + ")", lang: "typst"),
[#zh(n)],
text(zh(n), t),
)).flatten(),
table.hline(),
)
-->

## 覆盖定义 Override

字号没有统一规定,本包默认与 [CTeX、MS Word、WPS、Adobe 的中文规则][docs-ref]一致。
Chinese size systems were not standardized. By default, this package is consistent with [Chinese rules of CTeX, MS Word, WPS, Adobe][docs-ref].

如想覆盖定义:If you want to override:

```typst
#import "@preview/pointless-size:0.1.1": zh as _zh
#let zh = _zh.with(overrides: ((7, 5.25pt),))
#assert.eq(_zh(7), 5.5pt)
#assert.eq(zh(7), 5.25pt)
```

[docs-ref]: https://github.com/YDX-2147483647/typst-pointless-size/blob/main/docs/ref.md
1 change: 1 addition & 0 deletions packages/preview/pointless-size/0.1.1/src/lib.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#import "./zihao.typ": zh, zihao
117 changes: 117 additions & 0 deletions packages/preview/pointless-size/0.1.1/src/zihao.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/// Terminology:
///
/// - points: 1pt, 2pt, …
/// - size number: 1, 2, "-0", …
/// - size name: "初号", "一号", "小五", …

/// Map from size numbers to points.
#let size_number_to_pt = (
(8, 5pt),
(7, 5.5pt),
(-6, 6.5pt),
(6, 7.5pt),
(-5, 9pt),
(5, 10.5pt),
(-4, 12pt),
(4, 14pt),
(-3, 15pt),
(3, 16pt),
(-2, 18pt),
(2, 22pt),
(-1, 24pt),
(1, 26pt),
("-0", 36pt),
(0, 42pt),
)


/// Convert a size number to its name.
///
/// Examples: 5 ⇒ "五号", -5 ⇒ "小五", "-0" ⇒ "小初".
///
/// - size(int | "-0"): size number
/// -> str
#let number_to_name(size) = {
if size == "-0" {
"小初"
} else {
assert.eq(type(size), int, message: "expected an integer or \"-0\", found " + repr(size) + ".")

if size == 0 {
"初号"
} else {
numbering(
if size > 0 { "一号" } else { "小一" },
calc.abs(size),
)
}
}
}

/// Convert a size name to its number.
///
/// Examples: "五号" ⇒ 5, "五" ⇒ 5, "小五" ⇒ -5, "小初" ⇒ "-0".
///
/// Known limitation: numbers greater than 10 are not supported.
///
/// Returns none if failed.
///
/// - size (str): size name.
/// -> int | "-0" | none
#let name_to_number(size) = {
if type(size) != str { return none }

size = size.trim("", at: end)

if size == "小初" { return "-0" }

let pos = "初一二三四五六七八九十".clusters().position(c => c == size.trim("", at: start))
if pos == none { return none }

if size.starts-with("") { -pos } else { pos }
}


/// Convert a size to length.
///
/// Example inputs:
/// - 5, "五", "五号"
/// - -5, "小五"
/// - "-0", "小初"
///
/// -> length
#let zh(size, overrides: ()) = {
let rules = (..overrides, ..size_number_to_pt)
let rule = rules.find(((s, p)) => s == size)
if rule != none {
rule.at(1)
} else {
// Try parsing it as a name
let parsed = name_to_number(size)
if parsed != none {
zh(parsed, overrides: overrides)
} else {
panic(
"expected a valid size (zìhào), found "
+ repr(size)
+ ". (All valid sizes: "
+ rules.map(r => repr(r.at(0))).join(", ")
+ ".)",
)
}
}
}

/// A rule that sets text sizes.
///
/// ```example
/// #show: zihao(5)
/// ```
///
/// -> content => content
#let zihao(size, overrides: ()) = {
body => {
set text(size: zh(size, overrides: overrides))
body
}
}
12 changes: 12 additions & 0 deletions packages/preview/pointless-size/0.1.1/typst.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "pointless-size"
version = "0.1.1"
entrypoint = "src/lib.typ"
authors = ["Y.D.X."]
license = "MIT"
description = "中文字号的号数制及字体度量单位 Chinese size system (hào-system) and type-related measurements units"
repository = "https://github.com/YDX-2147483647/typst-pointless-size"
categories = [
"text",
"languages"
]

0 comments on commit 4443c9e

Please sign in to comment.