canvist_layout 0.2.2

Incremental text and block layout for the canvist editor: line breaking, bidi, hit testing, display lists, and SVG/HTML renderers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
//! The default theme, matching the look of TipTap's StarterKit templates.

use std::sync::Arc;

use super::AttrBinding;
use super::BlockPatch;
use super::BlockRule;
use super::Border;
use super::Chrome;
use super::Decorations;
use super::EmbedRule;
use super::EmbedStyle;
use super::FontSize;
use super::LeafStyle;
use super::MarkRule;
use super::MarkerKind;
use super::MarkerStyle;
use super::Motion;
use super::TableRole;
use super::TextPatch;
use super::TextStyle;
use super::Theme;
use super::ThemeBuilder;
use crate::Color;
use crate::FontSpec;
use crate::Insets;

/// The sans-serif stack used for body text.
pub const SANS: &str = "Inter, ui-sans-serif, system-ui, sans-serif";
/// The monospace stack used for code. It names fonts rather than
/// `ui-monospace`: after a missing family, WebKit takes tab stops from the
/// `ui-monospace` face but draws glyphs from the `monospace` fallback, which
/// a canvas cannot reproduce.
pub const MONO: &str =
	"\"JetBrains Mono\", SFMono-Regular, Menlo, Consolas, \"Liberation Mono\", monospace";

/// Light or dark colours.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ColorScheme {
	/// Dark text on white, TipTap's look.
	#[default]
	Light,
	/// Light text on a warm near-black.
	Dark,
}

/// The colours one scheme uses.
struct Palette {
	page: Color,
	ink: Color,
	accent: Color,
	accent_wash: Color,
	rule: Color,
	quote_bar: Color,
	highlight: Color,
	code_page: Color,
	code_ink: Color,
	selection: Color,
	/// Callout backgrounds and bars: info, success, warning, and error.
	callouts: [(Color, Color); 4],
	/// Table header cells' background.
	table_header: Color,
	/// The tint under commented text.
	comment: Color,
	/// The tint under conditional text, which only some versions keep.
	conditional: Color,
	/// The paper behind an ink drawing.
	ink_paper: Color,
}

const LIGHT: Palette = Palette {
	page: Color::rgb(0xff, 0xff, 0xff),
	ink: Color::rgb(0x2e, 0x2b, 0x29),
	accent: Color::rgb(0x6a, 0x00, 0xf5),
	accent_wash: Color::rgb(0xee, 0xee, 0xff),
	rule: Color::rgb(0xe5, 0xe3, 0xe1),
	quote_bar: Color::rgb(0xd6, 0xd3, 0xd1),
	highlight: Color::rgb(0xfa, 0xf5, 0x94),
	code_page: Color::rgb(0x2e, 0x2b, 0x29),
	code_ink: Color::rgb(0xff, 0xff, 0xff),
	selection: Color::rgba(0x6a, 0x00, 0xf5, 0x33),
	callouts: [
		(Color::rgb(0xee, 0xee, 0xff), Color::rgb(0x6a, 0x00, 0xf5)),
		(Color::rgb(0xe6, 0xf6, 0xea), Color::rgb(0x2b, 0x8a, 0x3e)),
		(Color::rgb(0xff, 0xf4, 0xdb), Color::rgb(0xe8, 0x59, 0x0c)),
		(Color::rgb(0xff, 0xe9, 0xe9), Color::rgb(0xc9, 0x2a, 0x2a)),
	],
	table_header: Color::rgb(0xf7, 0xf6, 0xf5),
	comment: Color::rgba(0xe8, 0x59, 0x0c, 0x1f),
	conditional: Color::rgba(0x1c, 0x7e, 0xd6, 0x1f),
	ink_paper: Color::rgb(0xfa, 0xf9, 0xf7),
};

const DARK: Palette = Palette {
	page: Color::rgb(0x1a, 0x18, 0x17),
	ink: Color::rgb(0xe9, 0xe6, 0xe3),
	accent: Color::rgb(0xb8, 0x96, 0xff),
	accent_wash: Color::rgb(0x33, 0x2a, 0x4d),
	rule: Color::rgb(0x3d, 0x39, 0x36),
	quote_bar: Color::rgb(0x57, 0x52, 0x4e),
	highlight: Color::rgb(0x5c, 0x55, 0x1a),
	code_page: Color::rgb(0x0d, 0x0c, 0x0b),
	code_ink: Color::rgb(0xe9, 0xe6, 0xe3),
	selection: Color::rgba(0xb8, 0x96, 0xff, 0x4d),
	callouts: [
		(Color::rgb(0x33, 0x2a, 0x4d), Color::rgb(0xb8, 0x96, 0xff)),
		(Color::rgb(0x1f, 0x3a, 0x29), Color::rgb(0x69, 0xdb, 0x7c)),
		(Color::rgb(0x45, 0x34, 0x18), Color::rgb(0xff, 0xa9, 0x4d)),
		(Color::rgb(0x4a, 0x21, 0x21), Color::rgb(0xff, 0x87, 0x87)),
	],
	table_header: Color::rgb(0x24, 0x21, 0x1f),
	comment: Color::rgba(0xff, 0xa9, 0x4d, 0x2e),
	conditional: Color::rgba(0x74, 0xc0, 0xfc, 0x2e),
	ink_paper: Color::rgb(0x21, 0x1f, 0x1d),
};

/// What [`Theme::starter_kit_with`] varies: the colour scheme, the font
/// stacks, and the body size. Spacing stays in the StarterKit's rem units.
#[derive(Clone, Debug, PartialEq)]
pub struct StarterKitOptions {
	/// Light or dark colours.
	pub scheme: ColorScheme,
	/// The CSS `font-family` list for body text and headings.
	pub body_family: Arc<str>,
	/// The CSS `font-family` list for inline code and code blocks.
	pub code_family: Arc<str>,
	/// The body font size in pixels.
	pub size: f32,
	/// The body line height, as a multiple of the font size.
	pub line_height: f32,
	/// Extra space after every grapheme of body text, in pixels, like CSS
	/// `letter-spacing`.
	pub letter_spacing: f32,
}

impl Default for StarterKitOptions {
	fn default() -> Self {
		Self {
			scheme: ColorScheme::Light,
			body_family: Arc::from(SANS),
			code_family: Arc::from(MONO),
			size: REM,
			line_height: BODY_LINE_HEIGHT,
			letter_spacing: 0.0,
		}
	}
}

/// One root em in pixels.
const REM: f32 = 16.0;
/// The body line height, as a multiple of the font size.
const BODY_LINE_HEIGHT: f32 = 1.5;

/// The weight of headings and bold text.
const BOLD: u16 = 700;
/// The weight of a details summary.
const SEMIBOLD: u16 = 600;
/// Headings set tighter than body text, as a multiple of their font size.
const HEADING_LINE_HEIGHT: f32 = 1.1;

/// Space between a list marker and the item content.
const MARKER_GAP: f32 = 0.4 * REM;
/// The side length of a task checkbox.
const CHECKBOX_SIZE: f32 = 0.875 * REM;

/// Space above and below a top-level paragraph.
const PARAGRAPH_SPACE: f32 = 0.75 * REM;
/// Space above and below block quotes and code blocks.
const FIGURE_SPACE: f32 = 1.5 * REM;
/// Space above and below a top-level list.
const LIST_SPACE: f32 = 1.25 * REM;
/// The left padding a list keeps for its markers.
const LIST_INDENT: f32 = 1.4 * REM;
/// Space between list items, and around the paragraphs and lists inside
/// them, so items read as one block.
const ITEM_SPACE: f32 = 0.25 * REM;

/// The font size of code blocks.
const CODE_BLOCK_SIZE: f32 = 0.8 * REM;
/// The vertical padding of code blocks.
const CODE_BLOCK_PADDING: f32 = 0.75 * REM;
/// The font size of inline code, relative to the surrounding text.
const INLINE_CODE_SCALE: f32 = 0.85;
/// The font size of subscripts and superscripts, relative to the text.
const SCRIPT_SCALE: f32 = 0.75;
/// How far a subscript's baseline drops, in ems of its own size.
const SUBSCRIPT_SHIFT: f32 = -0.25;
/// How far a superscript's baseline rises, in ems of its own size.
const SUPERSCRIPT_SHIFT: f32 = 0.5;
/// The default height of an image block, and of a video without a height.
const IMAGE_HEIGHT: f32 = 12.5 * REM;
/// The height of an audio player.
const AUDIO_HEIGHT: f32 = 3.5 * REM;
/// The height of a file attachment card.
const FILE_HEIGHT: f32 = 4.0 * REM;
/// The height reserved for a block formula until the host measures it.
const MATH_HEIGHT: f32 = 3.0 * REM;

/// How one heading level looks. Sizes and spacing are in rem.
struct HeadingStyle {
	level: i64,
	size: f32,
	space_before: f32,
	space_after: f32,
}

impl HeadingStyle {
	/// The rule for this level, refining the base heading rule.
	fn rule(&self) -> BlockRule {
		BlockRule::new(
			"heading",
			BlockPatch::new()
				.text(TextPatch::new().size(FontSize::Px(self.size * REM)))
				.space(self.space_before * REM, self.space_after * REM)
				.tag(&format!("h{}", self.level)),
		)
		.when("level", self.level)
	}
}

/// Heading font sizes and spacing, from level 1 to 6. The top two levels
/// open major sections, so they get more room above and below.
const HEADINGS: [HeadingStyle; 6] = [
	HeadingStyle {
		level: 1,
		size: 1.4,
		space_before: 3.5,
		space_after: 1.5,
	},
	HeadingStyle {
		level: 2,
		size: 1.2,
		space_before: 3.5,
		space_after: 1.5,
	},
	HeadingStyle {
		level: 3,
		size: 1.1,
		space_before: 2.5,
		space_after: 0.75,
	},
	HeadingStyle {
		level: 4,
		size: 1.0,
		space_before: 2.5,
		space_after: 0.75,
	},
	HeadingStyle {
		level: 5,
		size: 1.0,
		space_before: 2.5,
		space_after: 0.75,
	},
	HeadingStyle {
		level: 6,
		size: 1.0,
		space_before: 2.5,
		space_after: 0.75,
	},
];

impl Theme {
	/// The default theme. Block, mark, and embed names follow TipTap's
	/// StarterKit (`paragraph`, `bulletList`, `codeBlock`, `hardBreak`, …),
	/// so documents imported from TipTap JSON style correctly.
	#[must_use]
	pub fn starter_kit() -> Self {
		Self::starter_kit_with(&StarterKitOptions::default())
	}

	/// The StarterKit theme in another colour scheme, font stacks, or body
	/// size. Server rendering and the canvas must build the theme from the
	/// same options, or the HTML will not match the canvas.
	#[must_use]
	pub fn starter_kit_with(options: &StarterKitOptions) -> Self {
		let palette = match options.scheme {
			ColorScheme::Light => &LIGHT,
			ColorScheme::Dark => &DARK,
		};
		let builder = Theme::builder(base_style(options, palette))
			.chrome(Chrome {
				background: palette.page,
				selection: palette.selection,
				caret: palette.ink,
				caret_width: 2.0,
			})
			.markers(MarkerStyle {
				gap: MARKER_GAP,
				bullets: vec![Arc::from("•"), Arc::from("◦"), Arc::from("▪")],
				checkbox: CHECKBOX_SIZE,
				color: None,
			});
		let builder = block_rules(options, palette)
			.into_iter()
			.fold(builder, ThemeBuilder::block);
		let builder = mark_rules(options, palette)
			.into_iter()
			.fold(builder, ThemeBuilder::mark);

		embed_rules(palette)
			.into_iter()
			.fold(builder, ThemeBuilder::embed)
			.build()
	}
}

/// The root text style: body text in the body stack.
fn base_style(options: &StarterKitOptions, palette: &Palette) -> TextStyle {
	TextStyle {
		font: FontSpec {
			family: options.body_family.clone(),
			size: options.size,
			weight: 400,
			italic: false,
		},
		line_height: options.line_height,
		color: palette.ink,
		background: None,
		decorations: Decorations::default(),
		letter_spacing: options.letter_spacing,
		baseline_shift: 0.0,
		motion: Motion::None,
	}
}

/// Every block rule, in the order later rules override earlier ones.
fn block_rules(options: &StarterKitOptions, palette: &Palette) -> Vec<BlockRule> {
	let mut rules = text_block_rules();

	rules.extend(container_rules(palette));
	rules.extend(code_and_leaf_rules(options, palette));
	rules.extend(nested_list_rules());

	if cfg!(feature = "theme-extensions") {
		rules.extend(media_rules());
		rules.extend(details_rules());
		rules.extend(callout_rules(palette));
	}

	if cfg!(feature = "tables") {
		rules.extend(table_rules(palette));
	}

	if cfg!(feature = "ink") {
		rules.push(BlockRule::new(
			"ink",
			BlockPatch::new()
				.leaf(LeafStyle::Ink {
					height: INK_HEIGHT,
					stroke: INK_STROKE,
				})
				.background(palette.ink_paper, 0.5 * REM)
				.space(FIGURE_SPACE, FIGURE_SPACE)
				.tag("figure"),
		));
	}

	rules
}

/// Host-drawn media blocks beyond the StarterKit's images.
fn media_rules() -> [BlockRule; 5] {
	[
		media_rule("youtube", IMAGE_HEIGHT),
		media_rule("twitch", IMAGE_HEIGHT),
		media_rule("audio", AUDIO_HEIGHT),
		media_rule("file", FILE_HEIGHT),
		media_rule("blockMath", MATH_HEIGHT),
	]
}

/// An ink drawing's default height and stroke width, in drawing units: a
/// 5:2 sketch area drawn with a fine pen.
const INK_HEIGHT: f32 = 400.0;
const INK_STROKE: f32 = 4.0;

/// Tables: a grid of hairlines, padded cells, and tinted header cells.
fn table_rules(palette: &Palette) -> Vec<BlockRule> {
	let cell = || {
		BlockPatch::new()
			.table(TableRole::Cell)
			.padding(Insets::symmetric(0.375 * REM, 0.5 * REM))
	};
	let rule = Border {
		width: 1.0,
		color: palette.rule,
	};

	vec![
		BlockRule::new(
			"table",
			BlockPatch::new()
				.table(TableRole::Table { rule })
				.space(FIGURE_SPACE, FIGURE_SPACE)
				.tag("table"),
		),
		BlockRule::new(
			"tableRow",
			BlockPatch::new().table(TableRole::Row).tag("tr"),
		),
		BlockRule::new("tableCell", cell().tag("td")),
		BlockRule::new(
			"tableHeader",
			cell()
				.text(TextPatch::new().weight(BOLD))
				.background(palette.table_header, 0.0)
				.tag("th"),
		),
		BlockRule::new("paragraph", BlockPatch::new().space(ITEM_SPACE, ITEM_SPACE))
			.within("tableCell"),
		BlockRule::new("paragraph", BlockPatch::new().space(ITEM_SPACE, ITEM_SPACE))
			.within("tableHeader"),
	]
}

/// Collapsible details: a disclosure triangle beside the summary, which is
/// all a closed one shows.
fn details_rules() -> Vec<BlockRule> {
	vec![
		BlockRule::new(
			"details",
			BlockPatch::new()
				.padding(Insets::new(0.0, 0.0, 0.0, LIST_INDENT))
				.space(PARAGRAPH_SPACE, PARAGRAPH_SPACE)
				.marker(MarkerKind::Disclosure)
				.tag("div"),
		),
		BlockRule::new(
			"detailsSummary",
			BlockPatch::new()
				.text(TextPatch::new().weight(SEMIBOLD))
				.tag("div"),
		),
		BlockRule::new("detailsContent", BlockPatch::new().tag("div")),
	]
}

/// Callouts: a tinted box with a coloured bar, by `type`.
fn callout_rules(palette: &Palette) -> Vec<BlockRule> {
	let callout = |(background, bar): (Color, Color)| {
		BlockPatch::new()
			.background(background, 0.5 * REM)
			.border_left(4.0, bar)
			.padding(Insets::symmetric(0.75 * REM, REM))
			.space(FIGURE_SPACE, FIGURE_SPACE)
			.tag("div")
	};
	let [info, success, warning, error] = palette.callouts;

	vec![
		BlockRule::new("callout", callout(info)),
		BlockRule::new("callout", callout(success)).when("type", "success"),
		BlockRule::new("callout", callout(warning)).when("type", "warning"),
		BlockRule::new("callout", callout(error)).when("type", "error"),
		BlockRule::new("callout", BlockPatch::new().border_left(0.0, palette.rule))
			.when("type", "blank"),
		BlockRule::new("paragraph", BlockPatch::new().space(ITEM_SPACE, ITEM_SPACE))
			.within("callout"),
	]
}

/// Paragraphs and headings.
fn text_block_rules() -> Vec<BlockRule> {
	let heading_text = TextPatch::new()
		.weight(BOLD)
		.line_height(HEADING_LINE_HEIGHT);
	let mut rules = vec![
		BlockRule::new(
			"paragraph",
			BlockPatch::new()
				.space(PARAGRAPH_SPACE, PARAGRAPH_SPACE)
				.tag("p"),
		),
		BlockRule::new("heading", BlockPatch::new().text(heading_text).tag("h1")),
	];

	rules.extend(HEADINGS.iter().map(HeadingStyle::rule));
	rules
}

/// Block quotes, lists, and list items with their paragraphs.
fn container_rules(palette: &Palette) -> Vec<BlockRule> {
	let list = |marker| {
		BlockPatch::new()
			.padding(Insets::new(0.0, REM, 0.0, LIST_INDENT))
			.space(LIST_SPACE, LIST_SPACE)
			.marker(marker)
	};
	let item = || BlockPatch::new().space(ITEM_SPACE, ITEM_SPACE);

	vec![
		BlockRule::new(
			"blockquote",
			BlockPatch::new()
				.border_left(3.0, palette.quote_bar)
				.padding(Insets::new(0.0, 0.0, 0.0, REM))
				.space(FIGURE_SPACE, FIGURE_SPACE)
				.tag("blockquote"),
		),
		BlockRule::new("bulletList", list(MarkerKind::Bullet).tag("ul")),
		BlockRule::new("orderedList", list(MarkerKind::Ordered).tag("ol")),
		BlockRule::new("taskList", list(MarkerKind::Task).tag("ul")),
		BlockRule::new("listItem", item().tag("li")),
		BlockRule::new("taskItem", item().tag("li")),
		BlockRule::new("paragraph", item()).within("listItem"),
		BlockRule::new("paragraph", item()).within("taskItem"),
	]
}

/// Code blocks, horizontal rules, and images.
fn code_and_leaf_rules(options: &StarterKitOptions, palette: &Palette) -> Vec<BlockRule> {
	let code_text = TextPatch::new()
		.family(&options.code_family)
		.size(FontSize::Px(CODE_BLOCK_SIZE))
		.color(palette.code_ink);

	vec![
		BlockRule::new(
			"codeBlock",
			BlockPatch::new()
				.text(code_text)
				.padding(Insets::symmetric(CODE_BLOCK_PADDING, REM))
				.background(palette.code_page, 0.5 * REM)
				.space(FIGURE_SPACE, FIGURE_SPACE)
				.tag("pre"),
		),
		BlockRule::new(
			"horizontalRule",
			BlockPatch::new()
				.leaf(LeafStyle::Rule {
					thickness: 1.0,
					color: palette.rule,
				})
				.space(2.0 * REM, 2.0 * REM)
				.tag("hr"),
		),
		BlockRule::new(
			"image",
			BlockPatch::new()
				.leaf(LeafStyle::Slot {
					height: IMAGE_HEIGHT,
				})
				.space(REM, REM)
				.tag("div"),
		),
	]
}

/// A host-drawn media block: a slot `height` tall unless its `height`
/// attribute says otherwise.
fn media_rule(name: &str, height: f32) -> BlockRule {
	BlockRule::new(
		name,
		BlockPatch::new()
			.leaf(LeafStyle::Slot { height })
			.space(REM, REM)
			.tag("div"),
	)
}

/// Lists nested in list items take item spacing instead of list spacing.
fn nested_list_rules() -> Vec<BlockRule> {
	let mut rules = Vec::new();

	for list in ["bulletList", "orderedList", "taskList"] {
		for item in ["listItem", "taskItem"] {
			rules.push(
				BlockRule::new(list, BlockPatch::new().space(ITEM_SPACE, ITEM_SPACE)).within(item),
			);
		}
	}

	rules
}

fn mark_rules(options: &StarterKitOptions, palette: &Palette) -> Vec<MarkRule> {
	let mut rules = vec![
		MarkRule::new("bold", "strong", TextPatch::new().weight(BOLD)),
		MarkRule::new("italic", "em", TextPatch::new().italic(true)),
		MarkRule::new("underline", "u", TextPatch::new().underline(true)),
		MarkRule::new("strike", "s", TextPatch::new().strike(true)),
		MarkRule::new(
			"code",
			"code",
			TextPatch::new()
				.family(&options.code_family)
				.size(FontSize::Em(INLINE_CODE_SCALE))
				.background(palette.accent_wash),
		),
		MarkRule::new(
			"link",
			"a",
			TextPatch::new().color(palette.accent).underline(true),
		),
		MarkRule::new(
			"highlight",
			"mark",
			TextPatch::new().background(palette.highlight),
		)
		.bind(AttrBinding::Background(Arc::from("color"))),
		MarkRule::new("textStyle", "span", TextPatch::new())
			.bind(AttrBinding::Color(Arc::from("color")))
			.bind(AttrBinding::Background(Arc::from("backgroundColor")))
			.bind(AttrBinding::FontFamily(Arc::from("fontFamily")))
			.bind(AttrBinding::FontSize(Arc::from("fontSize")))
			.bind(AttrBinding::LineHeight(Arc::from("lineHeight"))),
	];

	if cfg!(feature = "theme-extensions") {
		rules.extend([
			MarkRule::new(
				"comment",
				"span",
				TextPatch::new().background(palette.comment).underline(true),
			),
			MarkRule::new(
				"conditional",
				"span",
				TextPatch::new().background(palette.conditional),
			),
			MarkRule::new(
				"subscript",
				"sub",
				TextPatch::new()
					.size(FontSize::Em(SCRIPT_SCALE))
					.baseline_shift(SUBSCRIPT_SHIFT),
			),
			MarkRule::new(
				"superscript",
				"sup",
				TextPatch::new()
					.size(FontSize::Em(SCRIPT_SCALE))
					.baseline_shift(SUPERSCRIPT_SHIFT),
			),
		]);
	}

	if cfg!(feature = "kinetic") {
		rules.push(
			MarkRule::new("kinetic", "span", TextPatch::new())
				.bind(AttrBinding::Motion(Arc::from("effect"))),
		);
	}

	rules
}

fn embed_rules(palette: &Palette) -> Vec<EmbedRule> {
	let mut rules = vec![
		EmbedRule::new("hardBreak", EmbedStyle::LineBreak),
		EmbedRule::new(
			"inlineImage",
			EmbedStyle::Box {
				width: REM,
				height: REM,
			},
		),
	];

	if cfg!(feature = "theme-extensions") {
		rules.extend([
			EmbedRule::new(
				"mention",
				EmbedStyle::Label {
					attr: Arc::from("label"),
					prefix: Arc::from("@"),
					patch: TextPatch::new()
						.color(palette.accent)
						.background(palette.accent_wash),
					fallback: Some(Arc::from("id")),
				},
			),
			EmbedRule::new(
				"inlineMath",
				EmbedStyle::Label {
					attr: Arc::from("latex"),
					prefix: Arc::from(""),
					patch: TextPatch::new().background(palette.accent_wash),
					fallback: None,
				},
			),
			EmbedRule::new(
				"emoji",
				EmbedStyle::Label {
					attr: Arc::from("emoji"),
					prefix: Arc::from(""),
					patch: TextPatch::new(),
					fallback: Some(Arc::from("name")),
				},
			),
		]);
	}

	rules
}