summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Haug <mhaug@live.de>2022-04-25 12:58:10 +0200
committerMartin Haug <mhaug@live.de>2022-04-25 14:20:47 +0200
commit649c101f07f6de4791dc9b6091dff4a85112a15c (patch)
treea7abdb603577b0186a801e5728a05d1fc4e373ff
parent7163b4a6c57c99fda085bf9b364c82bdba8ea698 (diff)
Add blind text function
-rw-r--r--Cargo.lock41
-rw-r--r--Cargo.toml1
-rw-r--r--src/library/mod.rs1
-rw-r--r--src/library/utility/blind.rs9
-rw-r--r--src/library/utility/mod.rs2
-rw-r--r--tests/ref/utility/blind.pngbin0 -> 27575 bytes
-rw-r--r--tests/typ/utility/blind.typ32
7 files changed, 86 insertions, 0 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 5a2e079e..e429f467 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -335,6 +335,15 @@ dependencies = [
]
[[package]]
+name = "lipsum"
+version = "0.8.0"
+source = "git+https://github.com/reknih/lipsum#c97ce95ba01ed2cce1d1b0b230b6b78295b0720b"
+dependencies = [
+ "rand",
+ "rand_chacha",
+]
+
+[[package]]
name = "lock_api"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -544,6 +553,12 @@ dependencies = [
]
[[package]]
+name = "ppv-lite86"
+version = "0.2.16"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
+
+[[package]]
name = "proc-macro2"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -562,6 +577,31 @@ dependencies = [
]
[[package]]
+name = "rand"
+version = "0.8.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
+dependencies = [
+ "rand_core",
+]
+
+[[package]]
+name = "rand_chacha"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
+dependencies = [
+ "ppv-lite86",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_core"
+version = "0.6.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
+
+[[package]]
name = "rctree"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -854,6 +894,7 @@ dependencies = [
"iai",
"image",
"kurbo",
+ "lipsum",
"memmap2",
"miniz_oxide 0.4.4",
"once_cell",
diff --git a/Cargo.toml b/Cargo.toml
index 341a96d1..1dbe7450 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -17,6 +17,7 @@ typst-macros = { path = "./macros" }
bytemuck = "1"
either = "1"
fxhash = "0.2"
+lipsum = { git = "https://github.com/reknih/lipsum", default-features = false }
once_cell = "1"
serde = { version = "1", features = ["derive"] }
typed-arena = "2"
diff --git a/src/library/mod.rs b/src/library/mod.rs
index d3ed98da..c68915c8 100644
--- a/src/library/mod.rs
+++ b/src/library/mod.rs
@@ -88,6 +88,7 @@ pub fn new() -> Scope {
std.def_fn("letter", utility::letter);
std.def_fn("roman", utility::roman);
std.def_fn("symbol", utility::symbol);
+ std.def_fn("lipsum", utility::lipsum);
// Predefined colors.
std.def_const("black", Color::BLACK);
diff --git a/src/library/utility/blind.rs b/src/library/utility/blind.rs
new file mode 100644
index 00000000..a4cfec90
--- /dev/null
+++ b/src/library/utility/blind.rs
@@ -0,0 +1,9 @@
+use lipsum::lipsum_from_seed;
+
+use crate::library::prelude::*;
+
+/// Create blind text.
+pub fn lipsum(_: &mut Context, args: &mut Args) -> TypResult<Value> {
+ let words: usize = args.expect("number of words")?;
+ Ok(Value::Str(lipsum_from_seed(words, 97).into()))
+}
diff --git a/src/library/utility/mod.rs b/src/library/utility/mod.rs
index 13220242..4244ccbf 100644
--- a/src/library/utility/mod.rs
+++ b/src/library/utility/mod.rs
@@ -1,9 +1,11 @@
//! Computational utility functions.
+mod blind;
mod color;
mod math;
mod string;
+pub use blind::*;
pub use color::*;
pub use math::*;
pub use string::*;
diff --git a/tests/ref/utility/blind.png b/tests/ref/utility/blind.png
new file mode 100644
index 00000000..e972567e
--- /dev/null
+++ b/tests/ref/utility/blind.png
Binary files differ
diff --git a/tests/typ/utility/blind.typ b/tests/typ/utility/blind.typ
new file mode 100644
index 00000000..7d1cb969
--- /dev/null
+++ b/tests/typ/utility/blind.typ
@@ -0,0 +1,32 @@
+// Test blind text.
+
+---
+// Test basic call.
+#lipsum(19)
+
+---
+// Test custom paragraphs with user code.
+#set text(8pt)
+
+{
+ let sentences = lipsum(59)
+ .split(".")
+ .filter(s => s != "")
+ .map(s => s + ".")
+
+ let used = 0
+ for s in sentences {
+ if used < 2 {
+ used += 1
+ } else {
+ parbreak()
+ used = 0
+ }
+ s.trim()
+ [ ]
+ }
+}
+
+---
+// Error: 8-10 missing argument: number of words
+#lipsum()