summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/layouting.rs30
-rw-r--r--tests/layouts/styles.tps4
-rw-r--r--tests/render.py48
3 files changed, 70 insertions, 12 deletions
diff --git a/tests/layouting.rs b/tests/layouting.rs
index b748748f..2ac92b8e 100644
--- a/tests/layouting.rs
+++ b/tests/layouting.rs
@@ -3,15 +3,25 @@ use std::io::{Write, Read, BufWriter};
use std::process::Command;
use typst::Typesetter;
+use typst::layout::LayoutAction;
use typst::toddle::query::FileSystemFontProvider;
use typst::export::pdf::PdfExporter;
-use typst::doc::LayoutAction;
const CACHE_DIR: &str = "test-cache";
-#[test]
-fn layouting() {
+fn main() {
+ let mut filter = Vec::new();
+ for arg in std::env::args().skip(1) {
+ if arg.as_str() != "--nocapture" {
+ filter.push(arg);
+ }
+ }
+
+ if !filter.is_empty() {
+ println!("Using filter: {:?}", filter);
+ }
+
fs::create_dir_all(format!("{}/serialized", CACHE_DIR)).unwrap();
fs::create_dir_all(format!("{}/rendered", CACHE_DIR)).unwrap();
fs::create_dir_all(format!("{}/pdf", CACHE_DIR)).unwrap();
@@ -19,20 +29,24 @@ fn layouting() {
for entry in fs::read_dir("tests/layouts/").unwrap() {
let path = entry.unwrap().path();
- let mut file = File::open(&path).unwrap();
- let mut src = String::new();
- file.read_to_string(&mut src).unwrap();
-
let name = path
.file_stem().unwrap()
.to_str().unwrap();
- test(name, &src);
+ if filter.is_empty() || filter.iter().any(|pattern| name.contains(pattern)) {
+ let mut file = File::open(&path).unwrap();
+ let mut src = String::new();
+ file.read_to_string(&mut src).unwrap();
+
+ test(name, &src);
+ }
}
}
/// Create a _PDF_ with a name from the source code.
fn test(name: &str, src: &str) {
+ println!("Testing: {}", name);
+
let mut typesetter = Typesetter::new();
let provider = FileSystemFontProvider::from_listing("fonts/fonts.toml").unwrap();
typesetter.add_font_provider(provider.clone());
diff --git a/tests/layouts/styles.tps b/tests/layouts/styles.tps
index 790d2b38..ef5d4e3b 100644
--- a/tests/layouts/styles.tps
+++ b/tests/layouts/styles.tps
@@ -9,5 +9,5 @@ _Emoji:_ Hello World! 🌍
_Styles:_ This is made *bold*, that _italic_ and this one `monospace` using the
built-in syntax!
-_Styles with functions:_ This [bold][word] is made bold and [italic][that] is italic
-using the standard library functions [mono][bold] and `italic`!
+_Styles with functions:_ This [bold][word] is made bold and [italic][that] italic
+using the standard library functions `bold` and `italic`!
diff --git a/tests/render.py b/tests/render.py
index 02c2693f..e52054c1 100644
--- a/tests/render.py
+++ b/tests/render.py
@@ -37,10 +37,25 @@ def main():
class Renderer:
def __init__(self, fonts, width, height):
self.fonts = fonts
- self.img = Image.new("RGBA", (pix(width), pix(height)), (255, 255, 255))
+ self.size = (pix(width), pix(height))
+ self.img = Image.new("RGBA", self.size, (255, 255, 255, 255))
self.draw = ImageDraw.Draw(self.img)
self.cursor = (0, 0)
+ self.colors = [
+ (176, 264, 158),
+ (274, 173, 207),
+ (158, 252, 264),
+ (285, 275, 187),
+ (132, 217, 136),
+ (236, 177, 246),
+ (174, 232, 279),
+ (285, 234, 158)
+ ]
+
+ self.rects = []
+ self.color_index = 0
+
def execute(self, command):
cmd = command[0]
parts = command.split()[1:]
@@ -56,7 +71,33 @@ class Renderer:
elif cmd == 'w':
text = command[2:]
- self.draw.text(self.cursor, text, (0, 0, 0), font=self.font)
+ self.draw.text(self.cursor, text, (0, 0, 0, 255), font=self.font)
+
+ elif cmd == 'b':
+ x, y, w, h = (pix(float(s)) for s in parts)
+ rect = [x, y, x+w, y+h]
+
+ forbidden_colors = set()
+ for other_rect, other_color in self.rects:
+ if rect == other_rect:
+ return
+
+ if overlap(rect, other_rect) or overlap(other_rect, rect):
+ forbidden_colors.add(other_color)
+
+ for color in self.colors[self.color_index:] + self.colors[:self.color_index]:
+ self.color_index = (self.color_index + 1) % len(self.colors)
+ if color not in forbidden_colors:
+ break
+
+ overlay = Image.new("RGBA", self.size, (0, 0, 0, 0))
+ draw = ImageDraw.Draw(overlay)
+ draw.rectangle(rect, fill=color + (255,))
+
+ self.img = Image.alpha_composite(self.img, overlay)
+ self.draw = ImageDraw.Draw(self.img)
+
+ self.rects.append((rect, color))
else:
raise Exception("invalid command")
@@ -68,6 +109,9 @@ class Renderer:
def pix(points):
return int(2 * points)
+def overlap(a, b):
+ return (a[0] < b[2] and b[0] < a[2]) and (a[1] < b[3] and b[1] < a[3])
+
if __name__ == "__main__":
main()