Skip to content

Commit

Permalink
feat: improve page style for chinese fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin committed Jul 19, 2024
1 parent 4a507b2 commit 84028c2
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
35 changes: 30 additions & 5 deletions typ/templates/page.typ
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// and multiple targets.
#import "@preview/shiroa:0.1.0": get-page-width, target, is-web-target, is-pdf-target, plain-text, templates
#import templates: *
#import "template-link.typ": *
#import "/typ/templates/side-notes.typ": side-attrs

// Metadata
Expand All @@ -25,8 +26,32 @@
} else {
10.5pt
}
#let heading-sizes = (26pt, 22pt, 14pt, 12pt, main-size)

#let heading-sizes = if is-web-target {
(36pt, 26pt, 22pt, 18pt, main-size)
} else {
(26pt, 22pt, 14pt, 12pt, main-size)
}
#assert(
heading-sizes.at(-1) < heading-sizes.at(-2),
message: "The second smallest heading size should be larger than the paragraph size (main-size).",
)

#let list-indent = 0.5em
#let par-leading = if is-web-target {
0.8em
} else {
// typst's default
0.65em
}
// 1.2, 1.5 * this parameter
#let block-spacing = if is-web-target {
par-leading * 1.5
} else {
0.7em
}
#let heading-below = block-spacing * 1.
#let heading-spacing = heading-below * 1.5

// Fonts
#let use-fandol-fonts = false
Expand Down Expand Up @@ -102,7 +127,6 @@
#let project(title: "Typst中文教程", authors: (), kind: "page", body) = {
let is-ref-page = kind == "reference-page"
let is-page = kind == "page"
let heading-sizes = (26pt, 22pt, 14pt, 12pt, main-size)

// set basic document metadata
set document(
Expand Down Expand Up @@ -145,6 +169,7 @@
lang: "zh",
region: "cn",
)
set block(spacing: block-spacing)

let ld = state("label-disambiguator", (:))
let update-ld(k) = ld.update(it => {
Expand Down Expand Up @@ -176,8 +201,8 @@
}

block(
spacing: 0.7em * 1.5 * 1.2,
below: 0.7em * 1.2,
spacing: heading-spacing,
below: heading-below,
it,
)
}
Expand Down Expand Up @@ -229,7 +254,7 @@
}

// Main body.
set par(justify: true)
set par(leading: par-leading, justify: true)

if is-ref-page {
let side-space = 4 * main-size
Expand Down
44 changes: 44 additions & 0 deletions typ/templates/template-link.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

// #import "supports-text.typ": plain-text
#import "@preview/shiroa:0.1.0": plain-text

#let make-unique-label(it, disambiguator: 1) = label({
let k = plain-text(it).trim()
if disambiguator > 1 {
k + "_d" + str(disambiguator)
} else {
k
}
})

#let label-disambiguator = state("label-disambiguator", (:))
#let update-label-disambiguator(k) = label-disambiguator.update(it => {
it.insert(k, it.at(k, default: 0) + 1)
it
})
#let get-label-disambiguator(loc, k) = make-unique-label(k, disambiguator: label-disambiguator.at(loc).at(k))

#let heading-reference(it, d: 1) = make-unique-label(it.body, disambiguator: d)

#let heading-hash(it, hash-color: blue) = {
let title = plain-text(it.body)
if title != none {
let title = title.trim()
update-label-disambiguator(title)
context (
{
let loc = here()
let dest = get-label-disambiguator(loc, title)
let h = measure(it.body).height
place(
left,
dx: -16pt,
[
#set text(fill: hash-color)
#link(loc)[\#] #dest
],
)
}
)
}
}

0 comments on commit 84028c2

Please sign in to comment.