Skip to content

Commit b445d31

Browse files
authored
rubby:0.10.0 (#259)
1 parent 300877d commit b445d31

File tree

7 files changed

+987
-0
lines changed

7 files changed

+987
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a
6+
Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to
7+
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
8+
9+
## [Unreleased]
10+
11+
## [0.10.0] - 2023-11-24
12+
13+
### Changed
14+
15+
- Values are now compared with "first-class values" types instead of strings
16+
(see [Typst 0.8.0 release notes](https://github.com/typst/typst/releases/tag/v0.8.0))
17+
(**breaking change**).
18+
19+
### Added
20+
21+
- Added minimal Typst version to `0.8.0` (see the reason above).
22+
23+
## [0.9.2] - 2023-09-14
24+
25+
### Fixed
26+
27+
- Fixed import in example.typ.
28+
29+
## [0.9.1] - 2023-09-14
30+
31+
### Fixed
32+
33+
- Fixed example.typ.
34+
35+
## [0.9.0] - 2023-09-14
36+
37+
### Changed
38+
39+
Now kebab case is used instead of snake case.
40+
41+
- Renamed function `get_ruby` to `get-ruby` (**breaking change**).
42+
- Renamed `get-ruby`'s argument `auto_spacing` to `auto-spacing` (**breaking
43+
change**).
44+
45+
## [0.8.0] - 2023-07-03
46+
47+
Initial release.
48+
49+
[0.10.0]: https://github.com/Andrew15-5/rubby/releases/tag/v0.10.0
50+
51+
[0.9.2]: https://github.com/Andrew15-5/rubby/releases/tag/v0.9.2
52+
53+
[0.9.1]: https://github.com/Andrew15-5/rubby/releases/tag/v0.9.1
54+
55+
[0.9.0]: https://github.com/Andrew15-5/rubby/releases/tag/v0.9.0
56+
57+
[0.8.0]: https://github.com/Andrew15-5/rubby/releases/tag/v0.8.0

packages/preview/rubby/0.10.0/LICENSE

Lines changed: 661 additions & 0 deletions
Large diffs are not rendered by default.

packages/preview/rubby/0.10.0/NOTICE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright/permission noticies for third party components used by this project
2+
can be found below.
3+
4+
================================================================================
5+
Copyright (c) 2023 rinmyo
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
================================================================================
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# rubby (Typst package)
2+
3+
## Usage
4+
5+
```typ
6+
#import "@preview/rubby:0.10.0": get-ruby
7+
8+
#let ruby = get-ruby(
9+
size: 0.5em, // Ruby font size
10+
dy: 0pt, // Vertical offset of the ruby
11+
pos: top, // Ruby position (top or bottom)
12+
alignment: "center", // Ruby alignment ("center", "start", "between", "around")
13+
delimiter: "|", // The delimiter between words
14+
auto-spacing: true, // Automatically add necessary space around words
15+
)
16+
17+
// Ruby goes first, base text - second.
18+
#ruby[ふりがな][振り仮名]
19+
20+
Treat each kanji as a separate word:
21+
#ruby[とう|きょう|こう|ぎょう|だい|がく][東|京|工|業|大|学]
22+
```
23+
24+
If you don't want automatically wrap text with delimiter:
25+
26+
```typ
27+
#let ruby = get-ruby(auto-spacing: false)
28+
```
29+
30+
See also <https://github.com/rinmyo/ruby-typ/blob/main/manual.pdf> and `example.typ`.
31+
32+
## Notes
33+
34+
Original project is at <https://github.com/rinmyo/ruby-typ> which itself is
35+
based on [the post](https://zenn.dev/saito_atsushi/articles/ff9490458570e1)
36+
of 齊藤敦志 (Saito Atsushi). This project is a modified version of
37+
[this commit](https://github.com/rinmyo/ruby-typ/commit/23ca86180757cf70f2b9f5851abb5ea5a3b4c6a1).
38+
39+
`auto-spacing` adds missing delimiter around the `content`/`string` which
40+
then adds space around base text if ruby is wider than the base text.
41+
42+
Problems appear only if ruby is wider than its base text and `auto-spacing` is
43+
not set to `true` (default is `true`).
44+
45+
You can always use a one-letter function (variable) name to shorten the
46+
function call length (if you have to use it a lot), e.g., `#let r = get-ruby()`
47+
(or `f` — short for furigana). But be careful as there are functions with names
48+
`v` and `h` and there could be a new built-in function with a name `r` or `f`
49+
which may break your document (Typst right now is in beta, so breaking changes
50+
are possible).
51+
52+
Although you can open issues or send PRs, I won't be able to always reply
53+
quickly (sometimes I'm very busy).
54+
55+
## Changelog
56+
57+
You can view the change log in the CHANGELOG.md file in the root of the project.
58+
59+
## License
60+
61+
This Typst package is licensed under AGPL v3.0. You can view the license in the
62+
LICENSE file in the root of the project or at
63+
<https://www.gnu.org/licenses/agpl-3.0.txt>. There is also a NOTICE file for
64+
3rd party copyright notices.
65+
66+
Copyright (C) 2023 Andrew Voynov
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#set text(
2+
font: ("Liberation Sans", "Noto Sans CJK JP")
3+
) // Optional
4+
5+
#import "@preview/rubby:0.10.0": get-ruby
6+
#let ruby = get-ruby()
7+
8+
```typst
9+
#let ruby = get-ruby() // (1) Adds missing delimiter around every content/string
10+
// or
11+
#let ruby = get-ruby(auto-spacing: false) // (2) Logic from original project
12+
```
13+
14+
#ruby[ふりがな][振り仮名]
15+
16+
#ruby[とう|きょう|こう|ぎょう|だい|がく][東|京|工|業|大|学]
17+
18+
#ruby[とうきょうこうぎょうだいがく][東京工業大学]
19+
20+
Next 2 lines look the same with (1) (default):
21+
22+
#let ruby = get-ruby()
23+
24+
#ruby[|きょうりょく|][|協力|]
25+
26+
#ruby[きょうりょく][協力]
27+
28+
But lines are being typeset differently if (2) is used:
29+
30+
#let ruby = get-ruby(auto-spacing: false)
31+
32+
#ruby[|きょうりょく|][|協力|]
33+
34+
#ruby[きょうりょく][協力] // Page boundaries are not honored
35+
36+
First 3 lines out of 4 look the same way with (1):
37+
38+
#let ruby = get-ruby()
39+
40+
#ruby[きゅう][九]#ruby[じゅう][十]
41+
42+
#ruby[きゅう|][九|]#ruby[|じゅう][|十]
43+
44+
#ruby[きゅう|じゅう][九|十]
45+
46+
#ruby[きゅうじゅう][九十]
47+
48+
Only 2nd and 3rd lines look the same way with (2):
49+
50+
#let ruby = get-ruby(auto-spacing: false)
51+
52+
#ruby[きゅう][九]#ruby[じゅう][十]
53+
54+
#ruby[きゅう|][九|]#ruby[|じゅう][|十]
55+
56+
#ruby[きゅう|じゅう][九|十]
57+
58+
#ruby[きゅうじゅう][九十]
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Copyright (C) 2023 Andrew Voynov
2+
// AGPL-3.0-only license is in the LICENSE file in the root of the project
3+
// or at https://www.gnu.org/licenses/agpl-3.0.txt
4+
5+
#let _ruby(rt, rb, size, pos, dy, alignment, delimiter, auto-spacing) = {
6+
if not ("center", "start", "between", "around").contains(alignment) {
7+
panic("'" + repr(alignment) + "' is not a valid ruby alignment")
8+
}
9+
10+
if not (top, bottom).contains(pos) {
11+
panic("pos can be either top or bottom but '" + repr(pos) + "'")
12+
}
13+
14+
let extract-content(content, fn: it => it) = {
15+
let func = content.func()
16+
return if func == text or func == raw {
17+
(content.text, fn)
18+
} else {
19+
extract-content(content.body, fn: it => func(fn(it)))
20+
}
21+
}
22+
23+
let add-spacing-if-enabled(text) = {
24+
if auto-spacing != true { return text }
25+
return (
26+
if text.first() != delimiter { delimiter }
27+
+ text
28+
+ if text.last() != delimiter { delimiter }
29+
)
30+
}
31+
32+
let rt-array = if type(rt) == content {
33+
let (inner, func) = extract-content(rt)
34+
add-spacing-if-enabled(inner).split(delimiter).map(func)
35+
} else if type(rt) == str {
36+
add-spacing-if-enabled(rt).split(delimiter)
37+
} else {(rt,)}
38+
assert(type(rt-array) == array)
39+
40+
let rb-array = if type(rb) == content {
41+
let (inner, func) = extract-content(rb)
42+
add-spacing-if-enabled(inner).split(delimiter).map(func)
43+
} else if type(rb) == str {
44+
add-spacing-if-enabled(rb).split(delimiter)
45+
} else {(rb,)}
46+
assert(type(rb-array) == array)
47+
48+
if rt-array.len() != rb-array.len() {
49+
rt-array = (rt,)
50+
rb-array = (rb,)
51+
}
52+
53+
let gutter = if (alignment == "center" or alignment == "start") {
54+
h(0pt)
55+
} else if (alignment == "between" or alignment == "around") {
56+
h(1fr)
57+
}
58+
59+
box(style(st => {
60+
let sum-body = []
61+
let sum-width = 0pt
62+
let i = 0
63+
while i < rb-array.len() {
64+
let (body, ruby) = (rb-array.at(i), rt-array.at(i))
65+
let bodysize = measure(body, st)
66+
let rt-plain-width = measure(text(size: size, ruby), st).width
67+
let width = if rt-plain-width > bodysize.width {
68+
rt-plain-width
69+
} else {
70+
bodysize.width
71+
}
72+
let chars = if(alignment == "around") {
73+
h(0.5fr) + ruby.clusters().join(gutter) + h(0.5fr)
74+
} else {
75+
ruby.clusters().join(gutter)
76+
}
77+
let rubytext = box(
78+
width: width,
79+
align(
80+
if (alignment == "start") { left } else { center },
81+
text(size: size, chars)
82+
)
83+
)
84+
let textsize = measure(rubytext, st)
85+
let dx = textsize.width - bodysize.width
86+
let (t-dx, l-dx, r-dx) = if (alignment == "start") {
87+
(0pt, 0pt, dx)
88+
} else {
89+
(-dx/2, dx/2, dx/2)
90+
}
91+
let (l, r) = (i != 0, i != rb-array.len() - 1)
92+
sum-width += if l { 0pt } else { t-dx }
93+
let dy = if pos == top {
94+
-1.5 * textsize.height - dy
95+
} else {
96+
bodysize.height + textsize.height/2 + dy
97+
}
98+
place(
99+
top + left,
100+
dx: sum-width,
101+
dy: dy,
102+
rubytext
103+
)
104+
sum-width += width
105+
sum-body += if l { h(l-dx) } + body + if r { h(r-dx) }
106+
i += 1
107+
}
108+
sum-body
109+
}))
110+
}
111+
112+
#let get-ruby(
113+
size: 0.5em,
114+
dy: 0pt,
115+
pos: top,
116+
alignment: "center",
117+
delimiter: "|",
118+
auto-spacing: true
119+
) = (rt, rb, alignment: alignment) => (
120+
_ruby(rt, rb, size, pos, dy, alignment, delimiter, auto-spacing)
121+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "rubby"
3+
version = "0.10.0"
4+
entrypoint = "rubby.typ"
5+
authors = ["Andrew Voynov"]
6+
license = "AGPL-3.0-only"
7+
description = "Add ruby (furigana) next to base text."
8+
repository = "https://github.com/Andrew15-5/rubby"
9+
keywords = ["ruby", "furigana", "CJK", "Chinese", "Japanese", "Korean"]
10+
compiler = "0.8.0"
11+
exclude = ["example.typ"]

0 commit comments

Comments
 (0)