-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
300877d
commit b445d31
Showing
7 changed files
with
987 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Changelog | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a | ||
Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to | ||
[Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
|
||
## [Unreleased] | ||
|
||
## [0.10.0] - 2023-11-24 | ||
|
||
### Changed | ||
|
||
- Values are now compared with "first-class values" types instead of strings | ||
(see [Typst 0.8.0 release notes](https://github.com/typst/typst/releases/tag/v0.8.0)) | ||
(**breaking change**). | ||
|
||
### Added | ||
|
||
- Added minimal Typst version to `0.8.0` (see the reason above). | ||
|
||
## [0.9.2] - 2023-09-14 | ||
|
||
### Fixed | ||
|
||
- Fixed import in example.typ. | ||
|
||
## [0.9.1] - 2023-09-14 | ||
|
||
### Fixed | ||
|
||
- Fixed example.typ. | ||
|
||
## [0.9.0] - 2023-09-14 | ||
|
||
### Changed | ||
|
||
Now kebab case is used instead of snake case. | ||
|
||
- Renamed function `get_ruby` to `get-ruby` (**breaking change**). | ||
- Renamed `get-ruby`'s argument `auto_spacing` to `auto-spacing` (**breaking | ||
change**). | ||
|
||
## [0.8.0] - 2023-07-03 | ||
|
||
Initial release. | ||
|
||
[0.10.0]: https://github.com/Andrew15-5/rubby/releases/tag/v0.10.0 | ||
|
||
[0.9.2]: https://github.com/Andrew15-5/rubby/releases/tag/v0.9.2 | ||
|
||
[0.9.1]: https://github.com/Andrew15-5/rubby/releases/tag/v0.9.1 | ||
|
||
[0.9.0]: https://github.com/Andrew15-5/rubby/releases/tag/v0.9.0 | ||
|
||
[0.8.0]: https://github.com/Andrew15-5/rubby/releases/tag/v0.8.0 |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Copyright/permission noticies for third party components used by this project | ||
can be found below. | ||
|
||
================================================================================ | ||
Copyright (c) 2023 rinmyo | ||
|
||
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: | ||
================================================================================ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# rubby (Typst package) | ||
|
||
## Usage | ||
|
||
```typ | ||
#import "@preview/rubby:0.10.0": get-ruby | ||
#let ruby = get-ruby( | ||
size: 0.5em, // Ruby font size | ||
dy: 0pt, // Vertical offset of the ruby | ||
pos: top, // Ruby position (top or bottom) | ||
alignment: "center", // Ruby alignment ("center", "start", "between", "around") | ||
delimiter: "|", // The delimiter between words | ||
auto-spacing: true, // Automatically add necessary space around words | ||
) | ||
// Ruby goes first, base text - second. | ||
#ruby[ふりがな][振り仮名] | ||
Treat each kanji as a separate word: | ||
#ruby[とう|きょう|こう|ぎょう|だい|がく][東|京|工|業|大|学] | ||
``` | ||
|
||
If you don't want automatically wrap text with delimiter: | ||
|
||
```typ | ||
#let ruby = get-ruby(auto-spacing: false) | ||
``` | ||
|
||
See also <https://github.com/rinmyo/ruby-typ/blob/main/manual.pdf> and `example.typ`. | ||
|
||
## Notes | ||
|
||
Original project is at <https://github.com/rinmyo/ruby-typ> which itself is | ||
based on [the post](https://zenn.dev/saito_atsushi/articles/ff9490458570e1) | ||
of 齊藤敦志 (Saito Atsushi). This project is a modified version of | ||
[this commit](https://github.com/rinmyo/ruby-typ/commit/23ca86180757cf70f2b9f5851abb5ea5a3b4c6a1). | ||
|
||
`auto-spacing` adds missing delimiter around the `content`/`string` which | ||
then adds space around base text if ruby is wider than the base text. | ||
|
||
Problems appear only if ruby is wider than its base text and `auto-spacing` is | ||
not set to `true` (default is `true`). | ||
|
||
You can always use a one-letter function (variable) name to shorten the | ||
function call length (if you have to use it a lot), e.g., `#let r = get-ruby()` | ||
(or `f` — short for furigana). But be careful as there are functions with names | ||
`v` and `h` and there could be a new built-in function with a name `r` or `f` | ||
which may break your document (Typst right now is in beta, so breaking changes | ||
are possible). | ||
|
||
Although you can open issues or send PRs, I won't be able to always reply | ||
quickly (sometimes I'm very busy). | ||
|
||
## Changelog | ||
|
||
You can view the change log in the CHANGELOG.md file in the root of the project. | ||
|
||
## License | ||
|
||
This Typst package is licensed under AGPL v3.0. You can view the license in the | ||
LICENSE file in the root of the project or at | ||
<https://www.gnu.org/licenses/agpl-3.0.txt>. There is also a NOTICE file for | ||
3rd party copyright notices. | ||
|
||
Copyright (C) 2023 Andrew Voynov |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#set text( | ||
font: ("Liberation Sans", "Noto Sans CJK JP") | ||
) // Optional | ||
|
||
#import "@preview/rubby:0.10.0": get-ruby | ||
#let ruby = get-ruby() | ||
|
||
```typst | ||
#let ruby = get-ruby() // (1) Adds missing delimiter around every content/string | ||
// or | ||
#let ruby = get-ruby(auto-spacing: false) // (2) Logic from original project | ||
``` | ||
|
||
#ruby[ふりがな][振り仮名] | ||
|
||
#ruby[とう|きょう|こう|ぎょう|だい|がく][東|京|工|業|大|学] | ||
|
||
#ruby[とうきょうこうぎょうだいがく][東京工業大学] | ||
|
||
Next 2 lines look the same with (1) (default): | ||
|
||
#let ruby = get-ruby() | ||
|
||
#ruby[|きょうりょく|][|協力|] | ||
|
||
#ruby[きょうりょく][協力] | ||
|
||
But lines are being typeset differently if (2) is used: | ||
|
||
#let ruby = get-ruby(auto-spacing: false) | ||
|
||
#ruby[|きょうりょく|][|協力|] | ||
|
||
#ruby[きょうりょく][協力] // Page boundaries are not honored | ||
|
||
First 3 lines out of 4 look the same way with (1): | ||
|
||
#let ruby = get-ruby() | ||
|
||
#ruby[きゅう][九]#ruby[じゅう][十] | ||
|
||
#ruby[きゅう|][九|]#ruby[|じゅう][|十] | ||
|
||
#ruby[きゅう|じゅう][九|十] | ||
|
||
#ruby[きゅうじゅう][九十] | ||
|
||
Only 2nd and 3rd lines look the same way with (2): | ||
|
||
#let ruby = get-ruby(auto-spacing: false) | ||
|
||
#ruby[きゅう][九]#ruby[じゅう][十] | ||
|
||
#ruby[きゅう|][九|]#ruby[|じゅう][|十] | ||
|
||
#ruby[きゅう|じゅう][九|十] | ||
|
||
#ruby[きゅうじゅう][九十] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright (C) 2023 Andrew Voynov | ||
// AGPL-3.0-only license is in the LICENSE file in the root of the project | ||
// or at https://www.gnu.org/licenses/agpl-3.0.txt | ||
|
||
#let _ruby(rt, rb, size, pos, dy, alignment, delimiter, auto-spacing) = { | ||
if not ("center", "start", "between", "around").contains(alignment) { | ||
panic("'" + repr(alignment) + "' is not a valid ruby alignment") | ||
} | ||
|
||
if not (top, bottom).contains(pos) { | ||
panic("pos can be either top or bottom but '" + repr(pos) + "'") | ||
} | ||
|
||
let extract-content(content, fn: it => it) = { | ||
let func = content.func() | ||
return if func == text or func == raw { | ||
(content.text, fn) | ||
} else { | ||
extract-content(content.body, fn: it => func(fn(it))) | ||
} | ||
} | ||
|
||
let add-spacing-if-enabled(text) = { | ||
if auto-spacing != true { return text } | ||
return ( | ||
if text.first() != delimiter { delimiter } | ||
+ text | ||
+ if text.last() != delimiter { delimiter } | ||
) | ||
} | ||
|
||
let rt-array = if type(rt) == content { | ||
let (inner, func) = extract-content(rt) | ||
add-spacing-if-enabled(inner).split(delimiter).map(func) | ||
} else if type(rt) == str { | ||
add-spacing-if-enabled(rt).split(delimiter) | ||
} else {(rt,)} | ||
assert(type(rt-array) == array) | ||
|
||
let rb-array = if type(rb) == content { | ||
let (inner, func) = extract-content(rb) | ||
add-spacing-if-enabled(inner).split(delimiter).map(func) | ||
} else if type(rb) == str { | ||
add-spacing-if-enabled(rb).split(delimiter) | ||
} else {(rb,)} | ||
assert(type(rb-array) == array) | ||
|
||
if rt-array.len() != rb-array.len() { | ||
rt-array = (rt,) | ||
rb-array = (rb,) | ||
} | ||
|
||
let gutter = if (alignment == "center" or alignment == "start") { | ||
h(0pt) | ||
} else if (alignment == "between" or alignment == "around") { | ||
h(1fr) | ||
} | ||
|
||
box(style(st => { | ||
let sum-body = [] | ||
let sum-width = 0pt | ||
let i = 0 | ||
while i < rb-array.len() { | ||
let (body, ruby) = (rb-array.at(i), rt-array.at(i)) | ||
let bodysize = measure(body, st) | ||
let rt-plain-width = measure(text(size: size, ruby), st).width | ||
let width = if rt-plain-width > bodysize.width { | ||
rt-plain-width | ||
} else { | ||
bodysize.width | ||
} | ||
let chars = if(alignment == "around") { | ||
h(0.5fr) + ruby.clusters().join(gutter) + h(0.5fr) | ||
} else { | ||
ruby.clusters().join(gutter) | ||
} | ||
let rubytext = box( | ||
width: width, | ||
align( | ||
if (alignment == "start") { left } else { center }, | ||
text(size: size, chars) | ||
) | ||
) | ||
let textsize = measure(rubytext, st) | ||
let dx = textsize.width - bodysize.width | ||
let (t-dx, l-dx, r-dx) = if (alignment == "start") { | ||
(0pt, 0pt, dx) | ||
} else { | ||
(-dx/2, dx/2, dx/2) | ||
} | ||
let (l, r) = (i != 0, i != rb-array.len() - 1) | ||
sum-width += if l { 0pt } else { t-dx } | ||
let dy = if pos == top { | ||
-1.5 * textsize.height - dy | ||
} else { | ||
bodysize.height + textsize.height/2 + dy | ||
} | ||
place( | ||
top + left, | ||
dx: sum-width, | ||
dy: dy, | ||
rubytext | ||
) | ||
sum-width += width | ||
sum-body += if l { h(l-dx) } + body + if r { h(r-dx) } | ||
i += 1 | ||
} | ||
sum-body | ||
})) | ||
} | ||
|
||
#let get-ruby( | ||
size: 0.5em, | ||
dy: 0pt, | ||
pos: top, | ||
alignment: "center", | ||
delimiter: "|", | ||
auto-spacing: true | ||
) = (rt, rb, alignment: alignment) => ( | ||
_ruby(rt, rb, size, pos, dy, alignment, delimiter, auto-spacing) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "rubby" | ||
version = "0.10.0" | ||
entrypoint = "rubby.typ" | ||
authors = ["Andrew Voynov"] | ||
license = "AGPL-3.0-only" | ||
description = "Add ruby (furigana) next to base text." | ||
repository = "https://github.com/Andrew15-5/rubby" | ||
keywords = ["ruby", "furigana", "CJK", "Chinese", "Japanese", "Korean"] | ||
compiler = "0.8.0" | ||
exclude = ["example.typ"] |