diff --git a/assets/fonts/README.md b/assets/fonts/README.md index c2be5cc..820715a 100644 --- a/assets/fonts/README.md +++ b/assets/fonts/README.md @@ -53,6 +53,16 @@ monospace face (`AppFonts.monoFallback` in `lib/core/theme.dart`). Licenses: [`Figtree-OFL.txt`](Figtree-OFL.txt), [`GeistMono-OFL.txt`](GeistMono-OFL.txt) +## Formula fonts + +Formulas written in TeX are drawn in KaTeX's fonts, which come with +flutter_math_fork rather than living here: twenty files, about 660 KB in +every install. They are MIT licensed by Khan Academy, and that notice ships +as `packages/flutter_math_fork/katex_fonts/LICENSE`, listed in +`pubspec.yaml`, since the package's own LICENSE does not carry it. + +Source: + ## Brand font The `Kapy Notes` wordmark uses Odin Rounded Bold by Frank Hemmekam. diff --git a/benchmark/markdown_rendering.dart b/benchmark/markdown_rendering.dart new file mode 100644 index 0000000..7f29e54 --- /dev/null +++ b/benchmark/markdown_rendering.dart @@ -0,0 +1,387 @@ +// How long does a long note with formulas and code take to open, and how fast +// does typing in it stay? +// +// Five thousand lines: 30 code blocks in Python, TypeScript and SQL, 300 +// formulas among words and 50 set apart, and prose and sums between them, +// with markdown on unless KAPY_BENCH_MARKDOWN=off is in the environment. +// KAPY_BENCH_FORMULAS=off writes words where the formulas were, so the cost +// of drawing them can be told apart from the rest. +// The note is opened three times, then typed into the way a keyboard types, +// one editing-value update a key about thirty times a second for a second at +// a time: in prose, in a code block and right after a formula. The semantics +// tree is on throughout, as it always is in the macOS app, which switches it +// on at launch so that dictation tools can write into the editor (see +// AccessibilityTree.swift). Prints how long each opening took to its first +// frame, and how long the typing frames took to build and to draw, and +// quits. KAPY_BENCH_TYPING=off stops after the openings. +// +// A plain app rather than an integration test, because only profile mode +// says anything about frame times. It keeps the app's `main` reachable without +// calling it, so that it is compiled together with the app's own code. Build +// it under a bundle id of its own, never the real app's, and with sync off, +// then launch it with what to measure: +// +// FLUTTER_XCODE_PRODUCT_BUNDLE_IDENTIFIER=com.kapybara.kapynotes.renderperf \ +// flutter build macos --profile --dart-define=KAPYNOTES_SYNC=false \ +// -t benchmark/markdown_rendering.dart +// open -n --env KAPY_BENCH_MARKDOWN=off \ +// "build/macos/Build/Products/Profile/Kapy Notes.app" +// +// The figures are the line starting MARKDOWN_RENDERING_PERF in the log, also +// written to markdown_rendering_perf.json in the app's temporary directory. +import 'dart:async'; +import 'dart:convert'; +import 'dart:io'; + +import 'package:flutter/rendering.dart'; +import 'package:flutter/scheduler.dart'; +import 'package:kapy_notes/calc/engine.dart'; +import 'package:kapy_notes/calc/highlight.dart'; +import 'package:kapy_notes/core/editor_font.dart'; +import 'package:kapy_notes/core/theme.dart'; +import 'package:kapy_notes/data/local_store.dart'; +import 'package:kapy_notes/data/shortcut_prefs.dart'; +import 'package:kapy_notes/main.dart' as app; +import 'package:kapy_notes/ui/editor/note_editor.dart'; +import 'package:material_ui/material_ui.dart'; +import 'package:window_manager/window_manager.dart'; + +class _MemoryStore extends LocalStore { + _MemoryStore() : super(fileName: 'markdown-rendering-benchmark.json'); + + @override + Future load() async {} + + @override + Future flush() async {} + + @override + void put(String key, Object? value) => data[key] = value; +} + +const _python = [ + 'import math', + 'from dataclasses import dataclass', + '', + '@dataclass', + 'class Point:', + ' x: float', + ' y: float', + '', + ' def norm(self) -> float:', + ' # Euclidean length of the vector', + ' return math.sqrt(self.x ** 2 + self.y ** 2)', + '', + 'def mean(values: list[float]) -> float:', + ' """The arithmetic mean, or zero for nothing."""', + ' if not values:', + ' return 0.0', + ' return sum(values) / len(values)', + '', + 'for i in range(10):', + ' print(f"step {i}: {mean([i, i * 2, 3.5])}")', +]; + +const _typescript = [ + "import { readFile } from 'node:fs/promises';", + '', + 'interface Invoice {', + ' id: string;', + ' total: number;', + ' paid?: boolean;', + '}', + '', + 'export async function load(path: string): Promise {', + " const raw = await readFile(path, 'utf8');", + ' // One invoice per line, as JSON.', + ' return raw.split("\\n").filter(Boolean).map((line) => JSON.parse(line));', + '}', + '', + 'const unpaid = (all: Invoice[]) => all.filter((i) => !i.paid);', + 'let sum = 0;', + 'for (const invoice of unpaid([])) {', + ' sum += invoice.total * 1.2;', + '}', + r'console.log(`owed: ${sum.toFixed(2)}`);', +]; + +const _sql = [ + '-- Monthly revenue by customer', + 'SELECT c.name,', + " date_trunc('month', o.created_at) AS month,", + ' SUM(o.total) AS revenue,', + ' COUNT(*) AS orders', + 'FROM customers c', + 'JOIN orders o ON o.customer_id = c.id', + "WHERE o.status = 'paid'", + ' AND o.created_at >= NOW() - INTERVAL 1 YEAR', + 'GROUP BY c.name, month', + 'HAVING SUM(o.total) > 1000', + 'ORDER BY revenue DESC', + 'LIMIT 50;', + '', + 'UPDATE orders SET status = NULL WHERE id IN (1, 2, 3);', + 'CREATE INDEX orders_customer ON orders (customer_id);', + "INSERT INTO audit (event, at) VALUES ('refresh', NOW());", + 'DELETE FROM sessions WHERE expires_at < NOW();', + "SELECT * FROM customers WHERE email LIKE '%@example.com';", + 'DROP TABLE IF EXISTS scratch;', +]; + +/// The note: 5,000 lines in 30 sections, each with five lines of two +/// formulas among words, one or two formulas set apart, a sum and a code +/// block, and prose after them. +String longNote() { + final lines = []; + const languages = ['python', 'typescript', 'sql']; + const bodies = [_python, _typescript, _sql]; + for (var section = 0; section < 30; section++) { + final start = lines.length; + lines + ..add('## Section $section') + ..add(''); + for (var i = 0; i < 5; i++) { + lines.add( + _formulas + ? 'Line $i of $section says \$x_{$i}^{2} + \\frac{a}{b}\$ grows ' + 'faster than \$\\sqrt{n}\$ here.' + : 'Line $i of $section says x to the $i squared plus a over b ' + 'grows faster than root n here.', + ); + } + lines.add(''); + final displays = section < 20 ? 2 : 1; + for (var d = 0; d < displays; d++) { + lines + ..add(_formulas ? '\$\$' : 'Set apart:') + ..add( + _formulas + ? '\\sum_{k=1}^{$section} k^$d = \\int_0^1 f(t)\\,dt' + : 'the sum of k to the $d for k up to $section is an integral', + ) + ..add(_formulas ? '\$\$' : 'of f over 0 to 1.') + ..add(''); + } + lines + ..add('price = ${section + 3} usd') + ..add('price * 3') + ..add('') + ..add('```${languages[section % 3]}'); + final body = bodies[section % 3]; + lines + ..addAll(body) + ..addAll(body) + ..add('```') + ..add(''); + while (lines.length - start < 166) { + lines.add( + 'Plain prose for section $section, line ${lines.length - start}, ' + 'with a total of 4 + 5 in it.', + ); + } + } + while (lines.length < 5000) { + lines.add('closing line ${lines.length}'); + } + return lines.take(5000).join('\n'); +} + +/// [values] at the [p]th percentile, by nearest rank. +double percentile(List values, double p) { + final sorted = [...values]..sort(); + final rank = (p / 100 * sorted.length).ceil().clamp(1, sorted.length); + return sorted[rank - 1]; +} + +double _ms(Duration duration) => duration.inMicroseconds / 1000; + +Map summary(List timings) { + const budget = 1000 / 60; + final build = [for (final t in timings) _ms(t.buildDuration)]; + final raster = [for (final t in timings) _ms(t.rasterDuration)]; + Map stats(List values) => { + 'p50': percentile(values, 50), + 'p90': percentile(values, 90), + 'max': percentile(values, 100), + }; + return { + 'frames': timings.length, + 'build_ms': stats(build), + 'raster_ms': stats(raster), + 'frames_over_16_7ms': [ + for (var i = 0; i < timings.length; i++) + if (build[i] > budget || raster[i] > budget) i, + ].length, + }; +} + +void main() { + WidgetsFlutterBinding.ensureInitialized(); + unawaited(_runAll()); +} + +Future _runAll() async { + // The app's window waits hidden for the app's main to show it, and a + // hidden window draws no frames. + await windowManager.ensureInitialized(); + await windowManager.setSize(const Size(1280, 900)); + await windowManager.show(); + final report = await _run(); + final line = jsonEncode(report); + File( + '${Directory.systemTemp.path}/markdown_rendering_perf.json', + ).writeAsStringSync(line); + _say('MARKDOWN_RENDERING_PERF $line'); + if (report.isEmpty) await app.main(); + exit(0); +} + +/// Prints [line] to the log, which is where a run that is taking its time +/// shows how far it has got. +void _say(String line) { + // ignore: avoid_print + print(line); +} + +void _show(Widget home) { + runApp(MaterialApp(key: UniqueKey(), theme: KapyTheme.dark(), home: home)); +} + +Widget _editor(String body) { + final engine = CalcEngine(); + return Scaffold( + body: NoteEditor( + noteId: 'benchmark', + initialBody: body, + engine: engine, + highlighter: Highlighter(engine.registry), + resultsVisible: true, + markdownEnabled: _markdown, + autofocus: true, + onDocumentChanged: (_, _, _) {}, + onSettingsPressed: () {}, + writingFont: WritingFont.handwritten, + shortcuts: ShortcutPrefs(_MemoryStore())..load(), + ), + ); +} + +EditableTextState _field() { + EditableTextState? found; + void visit(Element element) { + if (found != null) return; + if (element is StatefulElement && element.state is EditableTextState) { + found = element.state as EditableTextState; + return; + } + element.visitChildElements(visit); + } + + WidgetsBinding.instance.rootElement!.visitChildElements(visit); + return found!; +} + +double _slowest(Iterable values) => + values.fold(0, (a, b) => a > b ? a : b); + +/// How long [body] takes to open in an empty window: from showing the +/// editor to the end of its first frame, which builds the note, lays it out +/// and paints it; and the slowest build and raster of the frames it takes. +Future> _open(String body) async { + _show(const SizedBox.shrink()); + await WidgetsBinding.instance.endOfFrame; + await Future.delayed(const Duration(seconds: 1)); + final timings = []; + void record(List batch) => timings.addAll(batch); + SchedulerBinding.instance.addTimingsCallback(record); + final stopwatch = Stopwatch()..start(); + _show(_editor(body)); + await WidgetsBinding.instance.endOfFrame; + final firstFrame = stopwatch.elapsedMicroseconds / 1000; + await Future.delayed(const Duration(seconds: 2)); + SchedulerBinding.instance.removeTimingsCallback(record); + return { + 'first_frame_ms': firstFrame, + 'slowest_build_ms': _slowest([ + for (final t in timings) _ms(t.buildDuration), + ]), + 'slowest_raster_ms': _slowest([ + for (final t in timings) _ms(t.rasterDuration), + ]), + }; +} + +final _markdown = Platform.environment['KAPY_BENCH_MARKDOWN'] != 'off'; +final _formulas = Platform.environment['KAPY_BENCH_FORMULAS'] != 'off'; + +Future> _run() async { + final body = longNote(); + final opens = >[]; + for (var i = 1; i <= 3; i++) { + opens.add(await _open(body)); + _say( + 'opened $i of 3: ${opens.last['first_frame_ms']} ms to the first frame', + ); + } + if (Platform.environment['KAPY_BENCH_TYPING'] == 'off') { + return { + 'semantics': SemanticsBinding.instance.semanticsEnabled, + 'markdown': _markdown, + 'characters': body.length, + 'open': opens, + }; + } + await Future.delayed(const Duration(seconds: 1)); + final field = _field(); + const keys = 30; + + Future> typeAt(int caret) async { + field.userUpdateTextEditingValue( + field.textEditingValue.copyWith( + selection: TextSelection.collapsed(offset: caret), + ), + SelectionChangedCause.tap, + ); + await Future.delayed(const Duration(seconds: 1)); + final timings = []; + void record(List batch) => timings.addAll(batch); + SchedulerBinding.instance.addTimingsCallback(record); + for (var i = 0; i < keys; i++) { + final value = field.textEditingValue; + final at = value.selection.start; + // What the platform's text input sends for a key. + field.updateEditingValue( + TextEditingValue( + text: value.text.replaceRange(at, at, 'a'), + selection: TextSelection.collapsed(offset: at + 1), + ), + ); + await Future.delayed(const Duration(milliseconds: 33)); + } + // Timings arrive in batches, about once a second in profile mode. + await Future.delayed(const Duration(seconds: 2)); + SchedulerBinding.instance.removeTimingsCallback(record); + _say('typed $keys keys at $caret'); + return summary(timings); + } + + int after(String needle, String from) { + final text = field.textEditingValue.text; + return text.indexOf(needle, text.indexOf(from)); + } + + final prose = await typeAt( + after('Plain prose for section 15, line 100', '## Section 15'), + ); + final code = await typeAt(after('return sum(values)', '## Section 15')); + final formula = await typeAt(after(' grows', 'Line 2 of 15')); + return { + 'semantics': SemanticsBinding.instance.semanticsEnabled, + 'markdown': _markdown, + 'characters': body.length, + 'open': opens, + 'typing_in_prose': prose, + 'typing_in_code': code, + 'typing_after_a_formula': formula, + }; +} diff --git a/lib/ui/editor/code_highlighting.dart b/lib/ui/editor/code_highlighting.dart new file mode 100644 index 0000000..b2fc3d1 --- /dev/null +++ b/lib/ui/editor/code_highlighting.dart @@ -0,0 +1,237 @@ +import 'package:flutter/foundation.dart'; +import 'package:re_highlight/languages/bash.dart'; +import 'package:re_highlight/languages/c.dart'; +import 'package:re_highlight/languages/cpp.dart'; +import 'package:re_highlight/languages/csharp.dart'; +import 'package:re_highlight/languages/css.dart'; +import 'package:re_highlight/languages/dart.dart'; +import 'package:re_highlight/languages/diff.dart'; +import 'package:re_highlight/languages/dockerfile.dart'; +import 'package:re_highlight/languages/go.dart'; +import 'package:re_highlight/languages/graphql.dart'; +import 'package:re_highlight/languages/ini.dart'; +import 'package:re_highlight/languages/java.dart'; +import 'package:re_highlight/languages/javascript.dart'; +import 'package:re_highlight/languages/json.dart'; +import 'package:re_highlight/languages/kotlin.dart'; +import 'package:re_highlight/languages/latex.dart'; +import 'package:re_highlight/languages/lua.dart'; +import 'package:re_highlight/languages/makefile.dart'; +import 'package:re_highlight/languages/markdown.dart'; +import 'package:re_highlight/languages/objectivec.dart'; +import 'package:re_highlight/languages/perl.dart'; +import 'package:re_highlight/languages/php.dart'; +import 'package:re_highlight/languages/plaintext.dart'; +import 'package:re_highlight/languages/powershell.dart'; +import 'package:re_highlight/languages/python.dart'; +import 'package:re_highlight/languages/r.dart'; +import 'package:re_highlight/languages/ruby.dart'; +import 'package:re_highlight/languages/rust.dart'; +import 'package:re_highlight/languages/scala.dart'; +import 'package:re_highlight/languages/scss.dart'; +import 'package:re_highlight/languages/sql.dart'; +import 'package:re_highlight/languages/swift.dart'; +import 'package:re_highlight/languages/typescript.dart'; +import 'package:re_highlight/languages/xml.dart'; +import 'package:re_highlight/languages/yaml.dart'; +import 'package:re_highlight/re_highlight.dart'; + +import 'markdown_syntax.dart'; + +/// What a stretch of code is, as far as colouring it goes. +enum CodeTokenKind { + keyword, + string, + number, + comment, + title, + type, + attribute, + variable, + punctuation, +} + +/// [start, end) of the note, coloured as [kind]. +typedef CodeToken = ({int start, int end, CodeTokenKind kind}); + +/// Fenced code coloured the way the language its info string names reads, +/// by highlight.js's grammars. +/// +/// The languages a note is likely to hold, under the names and aliases +/// highlight.js gives them. A block in any other language, or in none, stays +/// plain code. Each block is coloured once for as long as its language and +/// its code stay the same, so typing in one block leaves the rest alone. +class CodeHighlighter { + static final Highlight _grammars = Highlight() + ..registerLanguage('bash', langBash) + ..registerLanguage('c', langC) + ..registerLanguage('cpp', langCpp) + ..registerLanguage('csharp', langCsharp) + ..registerLanguage('css', langCss) + ..registerLanguage('dart', langDart) + ..registerLanguage('diff', langDiff) + ..registerLanguage('dockerfile', langDockerfile) + ..registerLanguage('go', langGo) + ..registerLanguage('graphql', langGraphql) + ..registerLanguage('ini', langIni) + ..registerLanguage('java', langJava) + ..registerLanguage('javascript', langJavascript) + ..registerLanguage('json', langJson) + ..registerLanguage('kotlin', langKotlin) + ..registerLanguage('latex', langLatex) + ..registerLanguage('lua', langLua) + ..registerLanguage('makefile', langMakefile) + ..registerLanguage('markdown', langMarkdown) + ..registerLanguage('objectivec', langObjectivec) + ..registerLanguage('perl', langPerl) + ..registerLanguage('php', langPhp) + ..registerLanguage('plaintext', langPlaintext) + ..registerLanguage('powershell', langPowershell) + ..registerLanguage('python', langPython) + ..registerLanguage('r', langR) + ..registerLanguage('ruby', langRuby) + ..registerLanguage('rust', langRust) + ..registerLanguage('scala', langScala) + ..registerLanguage('scss', langScss) + ..registerLanguage('sql', langSql) + ..registerLanguage('swift', langSwift) + ..registerLanguage('typescript', langTypescript) + ..registerLanguage('xml', langXml) + ..registerLanguage('yaml', langYaml) + ..registerAliases(['zsh', 'shell'], 'bash'); + + Map<(String, String), List> _coloured = const {}; + + /// Every token of every block in [blocks], in order, in [text]'s offsets. + List tokensIn(String text, List blocks) { + final coloured = <(String, String), List>{}; + final tokens = []; + for (final block in blocks) { + final language = block.language.toLowerCase(); + if (_grammars.getLanguage(language) == null) continue; + final code = block.code + .map((range) => text.substring(range.start, range.end)) + .join(); + final key = (language, code); + final relative = coloured[key] ??= + _coloured[key] ?? _highlight(language, code); + // The code of a quoted or indented block is in pieces, and a token can + // run from one into the next. + var piece = 0; + var pieceStart = 0; + for (final token in relative) { + var from = token.start; + while (from < token.end && piece < block.code.length) { + final range = block.code[piece]; + final pieceEnd = pieceStart + range.end - range.start; + if (from >= pieceEnd) { + piece++; + pieceStart = pieceEnd; + continue; + } + final to = token.end < pieceEnd ? token.end : pieceEnd; + tokens.add(( + start: range.start + from - pieceStart, + end: range.start + to - pieceStart, + kind: token.kind, + )); + from = to; + } + } + } + _coloured = coloured; + return tokens; + } + + static List _highlight(String language, String code) { + final tokens = _Tokens(); + try { + _grammars.highlight(code: code, language: language).render(tokens); + } catch (error, stack) { + // A grammar that trips over some code costs that block its colours, + // never the note its text. + FlutterError.reportError( + FlutterErrorDetails( + exception: error, + stack: stack, + library: 'Kapy Notes editor', + context: ErrorDescription('while colouring $language code'), + ), + ); + return const []; + } + return tokens.length == code.length ? tokens.tokens : const []; + } +} + +/// Reads highlight.js's tree of scopes into flat tokens, each coloured by the +/// innermost scope around it that has a colour. +class _Tokens implements HighlightRenderer { + final tokens = []; + final _open = []; + var length = 0; + + @override + void addText(String text) { + final kind = _open.lastWhere((kind) => kind != null, orElse: () => null); + if (kind != null && text.isNotEmpty) { + final last = tokens.lastOrNull; + if (last != null && last.end == length && last.kind == kind) { + tokens.last = ( + start: last.start, + end: length + text.length, + kind: kind, + ); + } else { + tokens.add((start: length, end: length + text.length, kind: kind)); + } + } + length += text.length; + } + + @override + void openNode(DataNode node) => _open.add(_kindOf(node.scope)); + + @override + void closeNode(DataNode node) => _open.removeLast(); + + static CodeTokenKind? _kindOf(String? scope) { + if (scope == null) return null; + if (scope.startsWith('title.class')) return CodeTokenKind.type; + return switch (scope.split('.').first) { + 'keyword' || + 'built_in' || + 'literal' || + 'name' || + 'selector-tag' || + 'section' || + 'doctag' || + 'deletion' => CodeTokenKind.keyword, + 'string' || + 'regexp' || + 'symbol' || + 'char' || + 'addition' || + 'code' || + 'link' => CodeTokenKind.string, + 'number' => CodeTokenKind.number, + 'comment' || 'quote' => CodeTokenKind.comment, + 'title' || 'function' => CodeTokenKind.title, + 'type' || 'class' => CodeTokenKind.type, + 'attr' || + 'attribute' || + 'property' || + 'selector-attr' || + 'selector-class' || + 'selector-id' || + 'selector-pseudo' => CodeTokenKind.attribute, + 'variable' || 'template-variable' => CodeTokenKind.variable, + 'operator' || + 'punctuation' || + 'meta' || + 'tag' || + 'bullet' => CodeTokenKind.punctuation, + _ => null, + }; + } +} diff --git a/lib/ui/editor/highlighting_controller.dart b/lib/ui/editor/highlighting_controller.dart index e9e074a..2f73af8 100644 --- a/lib/ui/editor/highlighting_controller.dart +++ b/lib/ui/editor/highlighting_controller.dart @@ -9,8 +9,10 @@ import '../../core/editor_font.dart'; import '../../core/note_link.dart'; import '../../core/theme.dart'; import '../../data/note_format.dart'; +import 'code_highlighting.dart'; import 'editor_formatting.dart'; import 'markdown_syntax.dart'; +import 'note_math.dart'; /// One image, already sized and built, ready to be dropped into the span tree /// at its placeholder. @@ -47,6 +49,10 @@ class HighlightingController extends TextEditingController { bool _markdown; final MarkdownAnalyzer _markdownAnalyzer = MarkdownAnalyzer(); Map _imageSpans = const {}; + final NoteMath _math = NoteMath(); + final CodeHighlighter _codeHighlighter = CodeHighlighter(); + double _mathWidth = 0; + List<(int, PlaceholderDimensions)> _formulaPlaceholders = const []; /// Native spelling results currently painted by this rich text controller. /// @@ -139,6 +145,7 @@ class HighlightingController extends TextEditingController { selection, revealBlocks: markdownReveal.blocks, revealEdges: markdownReveal.edges, + drawable: (math) => _math.draws(math.tex), ); _concealment = concealment; _concealedFor = analysis; @@ -163,17 +170,30 @@ class HighlightingController extends TextEditingController { _imageSpans = value; } + /// Deliberately silent, for the same reason as [setImageSpansDuringLayout]. + /// + /// The width of the writing column a formula is drawn in: display math is + /// centred across it, and any formula wider than it is drawn smaller. + void setMathWidthDuringLayout(double value) { + _mathWidth = value; + } + /// The placeholder boxes [TextPainter] must be told about before it can lay - /// out a span containing [WidgetSpan]s, in the order they appear. + /// out a span containing [WidgetSpan]s, in the order they appear: pictures, + /// table rows, and the formulas the last [buildTextSpan] drew. List placeholderDimensions() { - final offsets = _imageSpans.keys.toList()..sort(); - return [ - for (final offset in offsets) - PlaceholderDimensions( - size: Size(_imageSpans[offset]!.width, _imageSpans[offset]!.height), - alignment: PlaceholderAlignment.top, + final placeholders = [ + for (final MapEntry(key: offset, value: image) in _imageSpans.entries) + ( + offset, + PlaceholderDimensions( + size: Size(image.width, image.height), + alignment: PlaceholderAlignment.top, + ), ), - ]; + ..._formulaPlaceholders, + ]..sort((a, b) => a.$1.compareTo(b.$1)); + return [for (final (_, dimensions) in placeholders) dimensions]; } void _invalidate() { @@ -235,11 +255,11 @@ class HighlightingController extends TextEditingController { final concealment = markdownConcealment(); final hidden = concealment.hidden; final transparent = concealment.transparent; - final stretches = _listStretches( - analysis, - base, - MediaQuery.maybeTextScalerOf(context) ?? TextScaler.noScaling, - ); + final scaler = + MediaQuery.maybeTextScalerOf(context) ?? TextScaler.noScaling; + final stretches = _listStretches(analysis, base, scaler); + final formulas = concealment.formulas; + final code = _codeTokens(analysis); final composing = withComposing && value.isComposingRangeValid ? value.composing : null; @@ -285,6 +305,13 @@ class HighlightingController extends TextEditingController { boundaries.add(stretch.start); boundaries.add(stretch.end); } + for (final formula in formulas) { + boundaries.add(formula.start + 1); + } + for (final token in code) { + boundaries.add(token.start); + boundaries.add(token.end); + } if (composing != null) { boundaries.add(composing.start.clamp(0, source.length)); boundaries.add(composing.end.clamp(0, source.length)); @@ -306,6 +333,10 @@ class HighlightingController extends TextEditingController { var hiddenIndex = 0; var transparentIndex = 0; var stretchIndex = 0; + var formulaIndex = 0; + final formulaCopies = {}; + var codeIndex = 0; + final placeholders = <(int, PlaceholderDimensions)>[]; for (var i = 0; i < cuts.length - 1; i++) { final start = cuts[i]; @@ -356,6 +387,41 @@ class HighlightingController extends TextEditingController { continue; } + // A formula drawn as itself stands in the place of its first character, + // the way a picture does, with the rest of its TeX hidden after it. + while (formulaIndex < formulas.length && + formulas[formulaIndex].end <= start) { + formulaIndex++; + } + final formula = + formulaIndex < formulas.length && + formulas[formulaIndex].start <= start + ? formulas[formulaIndex] + : null; + if (formula != null && start == formula.start) { + final (span, placeholder) = _formulaSpan( + formula, + copy: formulaCopies.update( + formula.tex, + (copy) => copy + 1, + ifAbsent: () => 0, + ), + words: _markdownBlockStyle( + base, + base, + (style) => style != MarkdownStyle.math && marked(style), + ), + base: base, + scaler: scaler, + indent: formula.display + ? _indent(formula.start, hidden, stretches, base, scaler) + : 0, + ); + children.add(span); + placeholders.add((formula.start, placeholder)); + continue; + } + while (spanIndex < spans.length && spans[spanIndex].end <= start) { spanIndex++; } @@ -399,6 +465,14 @@ class HighlightingController extends TextEditingController { if (markdown != 0) { segmentStyle = _markdownBlockStyle(segmentStyle, base, marked); } + while (codeIndex < code.length && code[codeIndex].end <= start) { + codeIndex++; + } + if (codeIndex < code.length && code[codeIndex].start <= start) { + segmentStyle = segmentStyle.merge( + _codeTokenStyle(code[codeIndex].kind), + ); + } final matchingFormats = _formats.where( (format) => format.start <= start && format.end >= end, ); @@ -427,10 +501,18 @@ class HighlightingController extends TextEditingController { }; } if (markdown != 0) { + final script = openMarkdown + .where( + (span) => + span.style == MarkdownStyle.superscript || + span.style == MarkdownStyle.subscript, + ) + .lastOrNull; segmentStyle = _markdownInlineStyle( segmentStyle, marked, segmentLinkColor, + script == null ? '' : source.substring(script.start, script.end), ); } final done = checked || marked(MarkdownStyle.doneTask); @@ -497,12 +579,169 @@ class HighlightingController extends TextEditingController { } } + // A line break inside a formula drawn as itself would still break the + // line, hidden or not, and leave an empty line under the formula for + // each one; the span the field draws is not the note's text, so here it + // is a space. + final words = source.substring(start, end); children.add( - TextSpan(text: source.substring(start, end), style: segmentStyle), + TextSpan( + text: formula == null ? words : words.replaceAll(_lineEnd, ' '), + style: segmentStyle, + ), ); } - return TextSpan(style: base, children: children); + _formulaPlaceholders = placeholders; + return _NoteSpan(style: base, children: children); + } + + static final _lineEnd = RegExp('[\r\n]'); + + /// How much bigger a formula is set than the words around it. KaTeX's own + /// face runs small beside a sans serif at the same size. + static const double _mathScale = 1.1; + + /// [formula] as a placeholder, and the box [placeholderDimensions] reports + /// for it. + /// + /// Among words a formula sits on their baseline. Set apart, it is centred + /// across what is left of the writing column after [indent], in the middle + /// of a line of its own. Both are drawn in the size and colour of the words + /// they stand among, in [words]. + /// + /// A placeholder's widget is scaled with the text by the field, so it is + /// built at the size the text is before scaling, and the box is reported + /// at the size it comes out at. [copy] is which of the formulas drawn with + /// the same TeX this one is: see [NoteMath.formula]. + (WidgetSpan, PlaceholderDimensions) _formulaSpan( + MarkdownMath formula, { + required int copy, + required TextStyle words, + required TextStyle base, + required TextScaler scaler, + required double indent, + }) { + final size = base.fontSize ?? kDefaultFontSize; + final scale = scaler.scale(size) / size; + final room = math.max(0.0, _mathWidth - indent) / scale; + final drawn = _math.formula( + formula.tex, + display: formula.display, + fontSize: (words.fontSize ?? size) * _mathScale, + color: words.color ?? _palette.textPrimary, + maxWidth: room, + copy: copy, + ); + if (!formula.display) { + return ( + WidgetSpan( + alignment: PlaceholderAlignment.baseline, + baseline: TextBaseline.alphabetic, + child: drawn.child, + ), + PlaceholderDimensions( + size: Size(drawn.width * scale, drawn.height * scale), + alignment: PlaceholderAlignment.baseline, + baseline: TextBaseline.alphabetic, + baselineOffset: drawn.baseline * scale, + ), + ); + } + final width = math.max(room, drawn.width); + return ( + WidgetSpan( + alignment: PlaceholderAlignment.middle, + child: SizedBox( + width: width, + height: drawn.height, + child: Center(child: drawn.child), + ), + ), + PlaceholderDimensions( + size: Size(width * scale, drawn.height * scale), + alignment: PlaceholderAlignment.middle, + ), + ); + } + + /// How far into its line [offset] is drawn: the advance of what is shown + /// before it, with the room any list marker there is spaced out to. + double _indent( + int offset, + List hidden, + List<_Stretch> stretches, + TextStyle base, + TextScaler scaler, + ) { + final source = text; + final lineStart = offset == 0 + ? 0 + : source.lastIndexOf('\n', offset - 1) + 1; + if (lineStart >= offset) return 0; + final painter = TextPainter( + text: TextSpan( + style: base, + children: [ + for (var i = lineStart; i < offset; i++) + if (!hidden.any((range) => range.start <= i && i < range.end)) + TextSpan( + text: source[i], + style: TextStyle( + letterSpacing: stretches + .where((stretch) => stretch.start <= i && i < stretch.end) + .firstOrNull + ?.spacing, + ), + ), + ], + ), + textDirection: TextDirection.ltr, + textScaler: scaler, + )..layout(); + final width = painter + .getOffsetForCaret( + TextPosition(offset: painter.plainText.length), + Rect.zero, + ) + .dx; + painter.dispose(); + return width; + } + + MarkdownAnalysis? _colouredFor; + List _colouredCode = const []; + + /// The colours of [analysis]'s fenced code, worked out once per analysis. + List _codeTokens(MarkdownAnalysis analysis) { + if (identical(analysis, _colouredFor)) return _colouredCode; + _colouredFor = analysis; + return _colouredCode = analysis.fencedCode.isEmpty + ? const [] + : _codeHighlighter.tokensIn(analysis.text, analysis.fencedCode); + } + + /// Code coloured from the note's own palette, so that it sits in the note + /// in light and in dark alike rather than in some editor's theme. + TextStyle _codeTokenStyle(CodeTokenKind kind) => switch (kind) { + CodeTokenKind.keyword => TextStyle(color: _palette.keyword), + CodeTokenKind.string => TextStyle(color: _palette.chipCurrency), + CodeTokenKind.number => TextStyle(color: _palette.number), + CodeTokenKind.comment => TextStyle( + color: _palette.comment, + fontStyle: FontStyle.italic, + ), + CodeTokenKind.title => TextStyle(color: _palette.function), + CodeTokenKind.type || + CodeTokenKind.attribute => TextStyle(color: _palette.unit), + CodeTokenKind.variable => TextStyle(color: _palette.variable), + CodeTokenKind.punctuation => TextStyle(color: _palette.operator), + }; + + @override + void dispose() { + _math.dispose(); + super.dispose(); } /// The room a list marker takes — a bullet, a box or a number, with the @@ -649,9 +888,12 @@ class HighlightingController extends TextEditingController { if (marked(MarkdownStyle.quote)) { result = result.copyWith(color: _palette.textSecondary); } - if (marked(MarkdownStyle.codeBlock) || marked(MarkdownStyle.code)) { + if (marked(MarkdownStyle.codeBlock) || + marked(MarkdownStyle.code) || + marked(MarkdownStyle.math)) { // The panel under a code block and the pill under inline code are drawn // behind the field, where the selection highlight still shows over them. + // A formula's TeX, shown as written, is code too. result = _codeStyle(result); } return result; @@ -682,26 +924,30 @@ class HighlightingController extends TextEditingController { ); } - /// How words under [styles] are drawn somewhere other than the field — in - /// a table drawn as a grid — in the faces and colours they have in it. + /// How [words] under [styles] are drawn somewhere other than the field — + /// in a table drawn as a grid — in the faces and colours they have in it. TextStyle markdownRunStyle( TextStyle base, Set styles, - Color linkColor, - ) { + Color linkColor, { + String words = '', + }) { bool marked(MarkdownStyle style) => styles.contains(style); return _markdownInlineStyle( _markdownBlockStyle(base, base, marked), marked, linkColor, + words, ); } - /// The markdown that changes a word rather than a block. + /// The markdown that changes a word rather than a block. [script] is all + /// of the superscript or subscript the words are part of, if they are. TextStyle _markdownInlineStyle( TextStyle style, bool Function(MarkdownStyle) marked, Color linkColor, + String script, ) { var result = style; if (marked(MarkdownStyle.strong) || marked(MarkdownStyle.tableHeader)) { @@ -710,12 +956,23 @@ class HighlightingController extends TextEditingController { if (marked(MarkdownStyle.emphasis)) { result = result.copyWith(fontStyle: FontStyle.italic); } - if (marked(MarkdownStyle.strikethrough)) { + final lines = [ + if (marked(MarkdownStyle.underline)) TextDecoration.underline, + if (marked(MarkdownStyle.strikethrough)) TextDecoration.lineThrough, + ]; + if (lines.isNotEmpty) { result = result.copyWith( - decoration: TextDecoration.lineThrough, + decoration: TextDecoration.combine(lines), decorationColor: result.color, ); } + if (marked(MarkdownStyle.superscript) || marked(MarkdownStyle.subscript)) { + result = _scripted( + result, + script, + superscript: marked(MarkdownStyle.superscript), + ); + } if (marked(MarkdownStyle.link)) { result = result.copyWith(color: linkColor); } @@ -728,6 +985,49 @@ class HighlightingController extends TextEditingController { return result; } + /// Superscript or subscript: the face's own raised or lowered glyphs where + /// it has one for every character of [script], and otherwise the same words + /// smaller. A text engine cannot move words off their baseline, and the + /// line keeps its height either way. + /// + /// Which characters a face has these for is known for the faces a note is + /// written in: Shantell Sans for digits, the system face on Apple's + /// platforms for digits, small letters and `+=()`. Anywhere else the words + /// are drawn smaller. + TextStyle _scripted( + TextStyle style, + String script, { + required bool superscript, + }) { + final (feature, drawn) = switch (style.fontFamily) { + 'Shantell Sans' => (superscript ? 'sups' : 'sinf', _digits), + '.AppleSystemUIFont' => ( + superscript ? 'sups' : 'subs', + _appleScriptCharacters, + ), + _ => (null, const {}), + }; + if (feature != null && + script.trim().isNotEmpty && + script.runes.every((rune) => rune == 0x20 || drawn.contains(rune))) { + return style.copyWith( + fontFeatures: [...?style.fontFeatures, FontFeature.enable(feature)], + ); + } + final size = style.fontSize; + final height = style.height; + if (size == null) return style; + return style.copyWith( + fontSize: size * _scriptScale, + height: height == null ? null : height / _scriptScale, + ); + } + + static const double _scriptScale = 0.75; + static final _digits = '0123456789'.runes.toSet(); + static final _appleScriptCharacters = + '0123456789abcdefghijklmnopqrstuvwxyz+=()'.runes.toSet(); + static List _checkedTextRanges(String source) { final ranges = []; var lineStart = 0; @@ -783,3 +1083,21 @@ class HighlightingController extends TextEditingController { /// Letter spacing over [start, end): see /// [HighlightingController._listStretches]. typedef _Stretch = ({int start, int end, double spacing}); + +/// The text the field draws, which works out its semantics information once +/// and keeps it. +/// +/// The field works that information out afresh, over every run in the note, +/// each time its semantics are marked dirty, and taking in a widget marks +/// them. So without this, opening a note with hundreds of formulas walked the +/// whole note that many times over whenever the semantics tree was on, and on +/// macOS it always is, for dictation: see `AccessibilityTree.swift`. +class _NoteSpan extends TextSpan { + _NoteSpan({super.style, super.children}); + + late final List _semantics = + List.unmodifiable(super.getSemanticsInformation()); + + @override + List getSemanticsInformation() => _semantics; +} diff --git a/lib/ui/editor/markdown_syntax.dart b/lib/ui/editor/markdown_syntax.dart index cd848cb..5f91754 100644 --- a/lib/ui/editor/markdown_syntax.dart +++ b/lib/ui/editor/markdown_syntax.dart @@ -19,12 +19,26 @@ enum MarkdownStyle { emphasis, strikethrough, + /// Words between `` and ``, or `` and ``. + underline, + + /// Words between `` and ``. + superscript, + + /// Words between `` and ``. + subscript, + /// Inline code, its backticks included. code, /// A fenced or indented code block, fences included. codeBlock, + /// A formula in TeX, its dollar signs included. Drawn as the formula it + /// writes, and as its TeX in code's face while the caret is at it; see + /// [MarkdownMath]. + math, + /// Everything inside a block quote. quote, @@ -133,6 +147,95 @@ class MarkdownTask { int get hashCode => Object.hash(box, checked); } +/// A formula written in TeX between dollar signs: `$…$` among words, or +/// `$$…$$` set apart on lines of its own. +/// +/// [start] to [end] takes the dollar signs in, and [tex] is what they hold, +/// trimmed. A formula is literal, the way code is: the calculator does not read +/// it and spelling leaves it alone. +@immutable +class MarkdownMath { + const MarkdownMath(this.start, this.end, this.tex, {required this.display}); + + final int start; + final int end; + final String tex; + + /// Set apart on lines of its own rather than written among words. + final bool display; + + MarkdownMath _shifted(int by) => + MarkdownMath(start + by, end + by, tex, display: display); + + /// Whether the formula shows its TeX rather than itself, with the selection + /// at [selection]. + /// + /// Only in a note being edited, which is what [revealBlocks] says. A caret + /// or a selection's end anywhere inside always shows it: that is where the + /// TeX is typed. Against its edges, display math shows, as a block's syntax + /// does; inline math as inline markers do, so only with [revealEdges] — and + /// a formula whose closing `$` has just been typed is drawn at once. A + /// selection reaching over the whole of one leaves it drawn. + bool revealedAt( + TextSelection? selection, { + required bool revealBlocks, + required bool revealEdges, + }) { + if (!revealBlocks || selection == null || !selection.isValid) return false; + bool inside(int offset) => offset > start && offset < end; + if (inside(selection.baseOffset) || inside(selection.extentOffset)) { + return true; + } + if (!selection.isCollapsed) return false; + final caret = selection.baseOffset; + if (caret != start && caret != end) return false; + return display || revealEdges; + } + + @override + bool operator ==(Object other) => + other is MarkdownMath && + other.start == start && + other.end == end && + other.tex == tex && + other.display == display; + + @override + int get hashCode => Object.hash(start, end, tex, display); + + @override + String toString() => + 'MarkdownMath($start, $end, ${display ? 'display' : 'inline'}: $tex)'; +} + +/// A fenced code block whose info string names a language, for colouring the +/// code the way that language reads. +@immutable +class MarkdownCodeBlock { + const MarkdownCodeBlock(this.language, this.code); + + /// The info string's first word, as written: `python`, `ts`, `C++`. + final String language; + + /// Where the code sits, fences left out. In more than one piece when the + /// block is indented or quoted, since a line's indentation or `>` is not + /// code. + final List code; + + MarkdownCodeBlock _shifted(int by) => MarkdownCodeBlock(language, [ + for (final range in code) _shift(range, by), + ]); + + @override + bool operator ==(Object other) => + other is MarkdownCodeBlock && + other.language == language && + listEquals(other.code, code); + + @override + int get hashCode => Object.hash(language, Object.hashAll(code)); +} + enum MarkdownBlockKind { code, quote, rule } /// A block that is drawn behind the text as well as in it: the panel under a @@ -504,12 +607,14 @@ class MarkdownConcealment { const MarkdownConcealment._({ required this.hidden, required this.transparent, + required this.formulas, required this.key, }); static const none = MarkdownConcealment._( hidden: [], transparent: [], + formulas: [], key: 0, ); @@ -520,6 +625,11 @@ class MarkdownConcealment { /// place: a bullet, a checkbox, a table's grid. Sorted, never overlapping. final List transparent; + /// The formulas drawn as formulas rather than as their TeX. Each is in + /// [hidden] whole; the editor draws the formula in the place of its first + /// character. Sorted. + final List formulas; + /// Changes whenever what is shown does, for anything that caches a layout. final int key; } @@ -564,6 +674,17 @@ class MarkdownAnalysis { /// Fenced and indented code blocks, which the calculator does not read. List get codeBlocks => _parts.codeBlocks; + /// The fenced code blocks that name a language, for colouring their code. + List get fencedCode => _parts.fencedCode; + + /// Every formula outside a table, in order. One in a table's cell is drawn + /// as its TeX, in the grid. + List get maths => _parts.maths; + + /// `
` tags, which a table's cell draws as the line break they ask for. + /// Anywhere else a tag is shown as written, as any other inline HTML is. + List get lineBreaks => _parts.lineBreaks; + /// The rest of what the calculator does not read: struck-through text, and /// the `*` and `_` around emphasis — the markers only, not the words. List get unread => _parts.unread; @@ -601,6 +722,9 @@ class MarkdownAnalysis { /// this far: the parser is not shown it as emphasis in the first place. /// * Struck-through text is blanked, words and all: crossed out is not /// counted, and `Total ~~12~~ 14` is 14. + /// * Formulas are blanked, dollar signs and all. `$x^2 + 1$` is written to + /// be read, not worked out, and a price never gets this far: `$5 and $10` + /// is not a formula, by the rules [MarkdownMath] is read by. /// * A table's rows are blanked. `| 2+2 | 4 |` is a row of a grid rather than /// a sum, and a result chip beside a tall wrapped row reads as a mistake. late final String calculatorText = _neutralise(); @@ -635,6 +759,9 @@ class MarkdownAnalysis { /// The words of [start, end) with every marker in it hidden, trimmed, and /// cut wherever their inline markdown changes: what a table's cell shows /// when the table is drawn as a grid rather than as its text. + /// + /// A `
` becomes the line break it asks for, the spaces either side of + /// it dropped, since a cell has nowhere else to put one. List runsIn(int start, int end) { final hidden = [ for (final conceal in conceals) @@ -647,6 +774,10 @@ class MarkdownAnalysis { _runStyles.contains(span.style)) span, ]; + final breaks = [ + for (final tag in lineBreaks) + if (tag.start >= start && tag.end <= end) tag, + ]; final cuts = {start, end}; for (final conceal in hidden) { cuts @@ -658,6 +789,11 @@ class MarkdownAnalysis { ..add(span.start.clamp(start, end)) ..add(span.end.clamp(start, end)); } + for (final tag in breaks) { + cuts + ..add(tag.start) + ..add(tag.end); + } final sorted = cuts.toList()..sort(); final runs = []; @@ -668,11 +804,15 @@ class MarkdownAnalysis { hidden.any((conceal) => conceal.start <= from && to <= conceal.end)) { continue; } + final lineBreak = breaks.any((tag) => tag.start <= from && to <= tag.end); final styles = { for (final span in styled) - if (span.start <= from && to <= span.end) span.style, + if (span.start <= from && + to <= span.end && + !(lineBreak && span.style == MarkdownStyle.html)) + span.style, }; - final words = text.substring(from, to); + final words = lineBreak ? '\n' : text.substring(from, to); if (runs.isNotEmpty && setEquals(runs.last.styles, styles)) { final last = runs.removeLast(); runs.add((text: last.text + words, styles: styles)); @@ -680,6 +820,15 @@ class MarkdownAnalysis { runs.add((text: words, styles: styles)); } } + if (breaks.isNotEmpty) { + for (var i = 0; i < runs.length; i++) { + final run = runs[i]; + runs[i] = ( + text: run.text.replaceAll(_spacedBreak, '\n'), + styles: run.styles, + ); + } + } while (runs.isNotEmpty && runs.first.text.trimLeft().isEmpty) { runs.removeAt(0); @@ -695,12 +844,18 @@ class MarkdownAnalysis { return runs; } + static final _spacedBreak = RegExp(r'[ \t]*\n[ \t]*'); + /// The markdown that styles words wherever they are drawn. static const _runStyles = { MarkdownStyle.strong, MarkdownStyle.emphasis, MarkdownStyle.strikethrough, + MarkdownStyle.underline, + MarkdownStyle.superscript, + MarkdownStyle.subscript, MarkdownStyle.code, + MarkdownStyle.math, MarkdownStyle.link, MarkdownStyle.html, }; @@ -722,10 +877,15 @@ class MarkdownAnalysis { /// [revealEdges] is false in the same cases and also while the writer is /// typing, so markers they have just completed disappear as they complete /// them rather than lingering beside the caret. + /// + /// A formula is drawn as itself unless [MarkdownMath.revealedAt] says + /// otherwise, or [drawable] says it cannot be: TeX the editor cannot draw + /// stays in sight as written. MarkdownConcealment concealFor( TextSelection? selection, { required bool revealBlocks, required bool revealEdges, + bool Function(MarkdownMath math)? drawable, }) { final valid = selection != null && selection.isValid; final caret = valid && selection.isCollapsed ? selection.baseOffset : null; @@ -782,6 +942,24 @@ class MarkdownAnalysis { } } + final formulas = []; + for (var i = 0; i < maths.length; i++) { + final math = maths[i]; + final drawn = + (drawable?.call(math) ?? true) && + !math.revealedAt( + selection, + revealBlocks: revealBlocks, + revealEdges: revealEdges, + ); + if (drawn) { + formulas.add(math); + hidden.add(TextRange(start: math.start, end: math.end)); + } else { + key = Object.hash(key, conceals.length + i); + } + } + final transparent = [ for (final ornament in ornaments) if (ornament.kind != MarkdownOrnamentKind.codeSpan) @@ -808,6 +986,7 @@ class MarkdownAnalysis { return MarkdownConcealment._( hidden: _merged(hidden), transparent: _merged(transparent), + formulas: List.unmodifiable(formulas), key: key, ); } @@ -848,6 +1027,7 @@ class MarkdownAnalysis { String _neutralise() { if (codeBlocks.isEmpty && unread.isEmpty && + maths.isEmpty && containerMarkers.isEmpty && tables.isEmpty) { return text; @@ -861,7 +1041,11 @@ class MarkdownAnalysis { if (unit != 0x0A && unit != 0x0D) units[i] = 0x20; } } - for (final range in [...codeBlocks, ...unread]) { + for (final range in [ + ...codeBlocks, + ...unread, + for (final math in maths) TextRange(start: math.start, end: math.end), + ]) { for (var i = range.start; i < range.end && i < units.length; i++) { final unit = units[i]; if (unit != 0x0A && unit != 0x0D) units[i] = 0x20; @@ -1044,6 +1228,9 @@ class _Parts { required this.listItems, required this.atomicPrefixes, required this.codeBlocks, + required this.fencedCode, + required this.maths, + required this.lineBreaks, required this.unread, required this.literals, required this.addresses, @@ -1064,6 +1251,9 @@ class _Parts { listItems: [], atomicPrefixes: [], codeBlocks: [], + fencedCode: [], + maths: [], + lineBreaks: [], unread: [], literals: [], addresses: [], @@ -1081,6 +1271,9 @@ class _Parts { final List listItems; final List atomicPrefixes; final List codeBlocks; + final List fencedCode; + final List maths; + final List lineBreaks; final List unread; final List literals; final List addresses; @@ -1107,6 +1300,9 @@ class _Parts { listItems: all((p) => p.listItems, (i, by) => i._shifted(by)), atomicPrefixes: all((p) => p.atomicPrefixes, _shift), codeBlocks: all((p) => p.codeBlocks, _shift), + fencedCode: all((p) => p.fencedCode, (c, by) => c._shifted(by)), + maths: all((p) => p.maths, (m, by) => m._shifted(by)), + lineBreaks: all((p) => p.lineBreaks, _shift), unread: all((p) => p.unread, _shift), literals: all((p) => p.literals, _shift), addresses: all((p) => p.addresses, _shift), @@ -1116,7 +1312,8 @@ class _Parts { } /// Just the tables, and only what [MarkdownAnalysis.runsIn] needs to draw - /// their cells: the inline styles inside them, and the markers those hide. + /// their cells: the inline styles inside them, the markers those hide, and + /// the `
` tags that break their lines. /// /// Everything else is dropped rather than emptied by its reader, so that one /// answer covers every part of the editor at once — nothing can quietly read @@ -1146,6 +1343,9 @@ class _Parts { listItems: const [], atomicPrefixes: const [], codeBlocks: const [], + fencedCode: const [], + maths: const [], + lineBreaks: lineBreaks, unread: const [], literals: const [], addresses: const [], @@ -1332,6 +1532,7 @@ _Parts _parse(String source) { // `>>>` quote fence nothing else reads. enableEmoji: false, enableFencedBlockquote: false, + extensions: [const _DisplayMathSyntax(), _InlineMathSyntax(), _WordRun()], ); final List nodes; try { @@ -1372,6 +1573,199 @@ _Tail _tailOf(List nodes) { }; } +const _dollar = 0x24; +const _backslash = 0x5C; + +bool _isWhitespace(int unit) => + unit == 0x20 || unit == 0x09 || unit == 0x0A || unit == 0x0D; + +/// Math among words, by pandoc's rules: `$…$`, and `$$…$$`. +/// +/// The opening `$` of `$…$` has something other than a space after it, and +/// the closing one something other than a space before it and no digit after +/// it, which is what keeps `$5 and $10` two prices rather than one formula. +/// A backslash keeps the character after it, so `\$` inside a formula is a +/// dollar sign rather than its end, and `\text{…}` may hold dollar signs of +/// its own. `$$…$$` ends at the next `$$`. +class _InlineMathSyntax extends md.InlineSyntax { + _InlineMathSyntax() : super(RegExp(r'\$'), startCharacter: _dollar); + + @override + md.InlineObject? parse(md.InlineParser parser, Match match) { + final start = parser.position; + final doubled = + start + 1 < parser.length && parser.charAt(start + 1) == _dollar; + final marker = doubled ? 2 : 1; + final close = doubled + ? _doubledClose(parser, start + 2) + : _close(parser, start + 1); + // Left where it was, the parser goes on to read the `$` as text. + if (close == null) return null; + final markers = parser.consumeBy(marker); + final content = parser.consumeBy(close - start - marker); + markers.addAll(parser.consumeBy(marker)); + return md.InlineElement( + 'math', + markers: markers, + children: [for (final span in content) md.Text.fromSpan(span)], + start: markers.first.start, + end: markers.last.end, + ); + } + + static int? _close(md.InlineParser parser, int from) { + final length = parser.length; + if (from >= length || _isWhitespace(parser.charAt(from))) return null; + var i = from; + while (i < length) { + final unit = parser.charAt(i); + if (unit == _dollar) { + if (i + 1 < length && _isDigit(parser.charAt(i + 1))) return null; + return i; + } + if (unit == _backslash) { + i = _afterEscape(parser, i); + } else if (_isWhitespace(unit)) { + while (i < length && _isWhitespace(parser.charAt(i))) { + i++; + } + if (i < length && parser.charAt(i) == _dollar) return null; + } else { + i++; + } + } + return null; + } + + static int? _doubledClose(md.InlineParser parser, int from) { + final length = parser.length; + var blank = true; + var i = from; + while (i + 1 < length) { + final unit = parser.charAt(i); + if (unit == _dollar && parser.charAt(i + 1) == _dollar) { + return blank ? null : i; + } + if (!_isWhitespace(unit)) blank = false; + i = unit == _backslash ? i + 2 : i + 1; + } + return null; + } + + /// Past the escape at [at]: the backslash and the character after it, or + /// a whole `\text{…}` with its braces balanced. + static int _afterEscape(md.InlineParser parser, int at) { + const text = r'\text{'; + final length = parser.length; + if (at + text.length <= length && + parser.substring(at, at + text.length) == text) { + var depth = 0; + for (var i = at + text.length - 1; i < length; i++) { + final unit = parser.charAt(i); + if (unit == _backslash) { + i++; + } else if (unit == 0x7B) { + depth++; + } else if (unit == 0x7D && --depth == 0) { + return i + 1; + } + } + } + return at + 2 < length ? at + 2 : length; + } + + static bool _isDigit(int unit) => unit >= 0x30 && unit <= 0x39; +} + +/// Words and the spaces between them, passed over as one run of text. +/// +/// The parser does this itself until it is given a syntax of its own, and +/// then goes a word at a time, in case that syntax starts at a space or a +/// letter, which [_InlineMathSyntax] never does. A word at a time, each word +/// of a note is cut out as text of its own, placed by counting from the start +/// of its line, and reading a long note takes more than twice as long. +class _WordRun extends md.InlineSyntax { + _WordRun() : super(RegExp(r'[ \tA-Za-z0-9]*[A-Za-z0-9](?=\s)')); + + @override + md.InlineObject? parse(md.InlineParser parser, Match match) { + parser.advanceBy(match[0]!.length); + return null; + } +} + +/// Display math: `$$` at the start of a line, and the TeX up to the `$$` +/// that closes it at the end of one, the same line or a later one. +/// +/// Pandoc reads `$$…$$` wherever it is written. Read as a block, the lines +/// between are TeX rather than markdown, so the `- b` of an aligned equation +/// stays a line of it rather than starting a list. As in pandoc there is no +/// blank line inside one. A `$$` closed anywhere but the end of a line, as in +/// `$$x$$ and more`, is math among words, and left to [_InlineMathSyntax]. +class _DisplayMathSyntax extends md.BlockSyntax { + const _DisplayMathSyntax(); + + static final _opening = RegExp(r'^ {0,3}\$\$'); + + @override + RegExp get pattern => _opening; + + @override + bool canParse(md.BlockParser parser) => _close(parser) != null; + + @override + md.BlockElement? parse(md.BlockParser parser) { + final close = _close(parser); + if (close == null) return null; + final (last, column) = close; + final open = _opening.matchAsPrefix(parser.current.content.text)!; + final markers = [ + md.Text.fromSpan(parser.current.content).subText(open.end - 2, open.end), + ]; + final children = []; + for (var line = 0; line <= last; line++) { + final content = md.Text.fromSpan(parser.current.content); + final from = line == 0 ? open.end : 0; + final to = line == last ? column : content.length; + if (to > from) children.add(content.subText(from, to)); + if (line == last) markers.add(content.subText(column, column + 2)); + parser.advance(); + } + return md.BlockElement( + 'displayMath', + markers: markers, + children: children, + start: markers.first.start, + end: markers.last.end, + ); + } + + /// The line, counted from the current one, and the column of the `$$` that + /// closes the formula the current line opens, if it opens one. + static (int, int)? _close(md.BlockParser parser) { + final open = _opening.matchAsPrefix(parser.current.content.text); + if (open == null) return null; + var blank = true; + for (var ahead = 0; ; ahead++) { + final line = parser.peek(ahead); + if (line == null || (ahead > 0 && line.isBlankLine)) return null; + final text = line.content.text; + var i = ahead == 0 ? open.end : 0; + while (i < text.length) { + final unit = text.codeUnitAt(i); + if (unit == _dollar && + i + 1 < text.length && + text.codeUnitAt(i + 1) == _dollar) { + if (blank || text.substring(i + 2).trim().isNotEmpty) return null; + return (ahead, i); + } + if (!_isWhitespace(unit)) blank = false; + i = unit == _backslash ? i + 2 : i + 1; + } + } + } +} + /// Walks the parser's tree and flattens it into what the editor draws. class _Collector { _Collector(this.source); @@ -1386,6 +1780,9 @@ class _Collector { final _tables = []; final _listItems = []; final _codeBlocks = []; + final _fencedCode = []; + final _maths = []; + final _lineBreaks = []; final _unread = []; final _literals = []; final _addresses = []; @@ -1396,6 +1793,9 @@ class _Collector { /// over like any other line prefix. final _numbers = []; + /// Where each ``, `` and the like that is one of a pair starts. + final _pairedTags = {}; + _Parts finish({required _Tail tail, required bool hasReferences}) { _spans.sort((a, b) => a.start.compareTo(b.start)); _links.sort((a, b) => a.start.compareTo(b.start)); @@ -1405,8 +1805,10 @@ class _Collector { _ornaments.sort((a, b) => a.start.compareTo(b.start)); _tables.sort((a, b) => a.start.compareTo(b.start)); _listItems.sort((a, b) => a.markerStart.compareTo(b.markerStart)); + _maths.sort((a, b) => a.start.compareTo(b.start)); for (final ranges in [ _codeBlocks, + _lineBreaks, _unread, _literals, _addresses, @@ -1426,6 +1828,9 @@ class _Collector { listItems: List.unmodifiable(_listItems), atomicPrefixes: List.unmodifiable(_atomicPrefixes()), codeBlocks: List.unmodifiable(_codeBlocks), + fencedCode: List.unmodifiable(_fencedCode), + maths: List.unmodifiable(_maths), + lineBreaks: List.unmodifiable(_lineBreaks), unread: List.unmodifiable(_unread), literals: List.unmodifiable(_literals), addresses: List.unmodifiable(_addresses), @@ -1677,6 +2082,18 @@ class _Collector { _style(start, end, MarkdownStyle.codeBlock); _record(_codeBlocks, start, end); _record(_literals, start, end); + final language = node.attributes['language']; + if (language != null && language.isNotEmpty) { + final code = [ + for (final line in node.children) + ?_range(line.start.offset, line.end.offset), + ]; + if (code.isNotEmpty) { + _fencedCode.add( + MarkdownCodeBlock(_unmask(language), List.unmodifiable(code)), + ); + } + } _blocks.add( MarkdownBlock(MarkdownBlockKind.code, _clamp(start), _clamp(end)), ); @@ -1711,8 +2128,22 @@ class _Collector { case 'tableHeadCell': _style(start, end, MarkdownStyle.tableHeader); case 'htmlBlock' || 'rawHtml': - _style(start, end, MarkdownStyle.html); + _style( + start, + end, + _pairedTags.contains(start) + ? MarkdownStyle.syntax + : MarkdownStyle.html, + ); _record(_literals, start, end); + if (_inTable(start) && + _lineBreakTag.hasMatch( + source.substring(_clamp(start), _clamp(end)), + )) { + _record(_lineBreaks, start, end); + } + case 'math' || 'displayMath': + _math(node, claimed); case 'linkReferenceDefinition': // Bookkeeping, not writing: GitHub shows none of it, and neither does // this until the caret is on it. @@ -1844,11 +2275,94 @@ class _Collector { ); } } + _pairTags(node.children); for (final child in node.children) { visit(child, quoteDepth: childQuoteDepth, listDepth: childListDepth); } } + /// Whether [offset] is in the table being read: a cell's words are drawn by + /// its grid, which shows a formula as its TeX and a `
` as a line break. + bool _inTable(int offset) => + _tables.isNotEmpty && + offset >= _tables.last.start && + offset < _tables.last.end; + + /// A formula: its TeX shown as code with its dollar signs quiet, and + /// outside a table the formula itself, which the editor draws in place of + /// all of it. + void _math(md.Element node, Set claimed) { + final start = node.start.offset; + final end = node.end.offset; + _style(start, end, MarkdownStyle.math); + _record(_literals, start, end); + for (final marker in node.markers) { + claimed.add(marker.start.offset); + _style(marker.start.offset, marker.end.offset, MarkdownStyle.syntax); + } + final range = _range(start, end); + if (range == null || _inTable(start)) return; + final display = node.type == 'displayMath'; + final tex = _unmask( + node.children.map((piece) => piece.textContent).join(display ? '\n' : ''), + ).trim(); + if (tex.isEmpty) return; + _maths.add(MarkdownMath(range.start, range.end, tex, display: display)); + } + + /// ``, ``, `` and ``, opening and closing among the + /// same words. Their words are drawn the way the tags ask, and the tags are + /// hidden like any other inline marker. One that crosses another, or is + /// never closed, stays the HTML it is written as. + void _pairTags(List nodes) { + final open = <(String, md.Element)>[]; + for (final node in nodes) { + if (node is! md.Element || node.type != 'rawHtml') continue; + final tag = _pairedTag.firstMatch( + source.substring(_clamp(node.start.offset), _clamp(node.end.offset)), + ); + if (tag == null) continue; + final name = tag[2]!.toLowerCase(); + if (tag[1]!.isEmpty) { + open.add((name, node)); + continue; + } + final at = open.lastIndexWhere((opening) => opening.$1 == name); + if (at < 0) continue; + final opening = open[at].$2; + open.removeRange(at, open.length); + final scopeStart = opening.start.offset; + final scopeEnd = node.end.offset; + final innerStart = opening.end.offset; + final innerEnd = node.start.offset; + _style(innerStart, innerEnd, switch (name) { + 'sup' => MarkdownStyle.superscript, + 'sub' => MarkdownStyle.subscript, + _ => MarkdownStyle.underline, + }); + for (final marker in [opening, node]) { + _pairedTags.add(marker.start.offset); + _record(_unread, marker.start.offset, marker.end.offset); + _conceal( + marker.start.offset, + marker.end.offset, + MarkdownReveal.edges, + scopeStart: scopeStart, + scopeEnd: scopeEnd, + innerStart: innerStart, + innerEnd: innerEnd, + ); + } + } + } + + static final _pairedTag = RegExp( + r'^<(/?)(u|ins|sup|sub)>$', + caseSensitive: false, + ); + + static final _lineBreakTag = RegExp(r'^$', caseSensitive: false); + void _listItem( md.Element node, Set claimed, { diff --git a/lib/ui/editor/note_editor.dart b/lib/ui/editor/note_editor.dart index 8e1d976..28e77c1 100644 --- a/lib/ui/editor/note_editor.dart +++ b/lib/ui/editor/note_editor.dart @@ -5410,15 +5410,16 @@ class NoteEditorState extends State with WidgetsBindingObserver { editorScale: widget.editorTextScale, ); // Only a note that needs a row taller than one line gives up the forced - // one: a picture, or a table whose cells wrap onto more lines than that. - final holdsTable = _controller - .markdownFor(_controller.text) - .tables - .isNotEmpty; + // one: a picture, a table whose cells wrap onto more lines than that, or a + // formula, which is as tall as what it writes. Any formula, drawn or shown + // as its TeX, so that moving the caret onto one never moves every line. + final analysis = _controller.markdownFor(_controller.text); final strut = EditorMetrics.strut( widget.writingFont, allowTallRows: - (_attachments.isNotEmpty && widget.images != null) || holdsTable, + (_attachments.isNotEmpty && widget.images != null) || + analysis.tables.isNotEmpty || + analysis.maths.isNotEmpty, editorScale: widget.editorTextScale, ); final textScaler = MediaQuery.textScalerOf(context); @@ -5500,16 +5501,19 @@ class NoteEditorState extends State with WidgetsBindingObserver { scaler: textScaler, fitToWidth: attachmentWidth, minRowHeight: rowHeight, - runStyle: (base, styles) { + runStyle: (base, styles, words) { final drawn = _controller.markdownRunStyle( base, styles, Theme.of(context).colorScheme.primary, + words: words, ); // The pill inline code has in the text can be a plain // background in a grid painted behind it: there is no - // selection to show over it there. - return styles.contains(MarkdownStyle.code) + // selection to show over it there. A formula in a cell + // is drawn as its TeX, which is code too. + return styles.contains(MarkdownStyle.code) || + styles.contains(MarkdownStyle.math) ? drawn.copyWith( backgroundColor: palette.controlBackground, ) @@ -5518,6 +5522,7 @@ class NoteEditorState extends State with WidgetsBindingObserver { ), }; + _controller.setMathWidthDuringLayout(attachmentWidth); // A table's rows reserve their room the same way a picture // does, through a placeholder apiece. A real attachment wins any // offset they might share. diff --git a/lib/ui/editor/note_math.dart b/lib/ui/editor/note_math.dart new file mode 100644 index 0000000..5a8b823 --- /dev/null +++ b/lib/ui/editor/note_math.dart @@ -0,0 +1,234 @@ +import 'package:flutter/rendering.dart'; +import 'package:flutter/widgets.dart'; +import 'package:flutter_math_fork/flutter_math.dart'; +import 'package:flutter_math_fork/tex.dart'; + +/// One formula, ready for a line of the note: the widget that draws it and +/// the room it takes, with how far down that room its baseline sits. +typedef NoteFormula = ({ + Widget child, + double width, + double height, + double baseline, +}); + +/// One formula as it is drawn: its TeX, set apart or not, at a size, in a +/// colour. +typedef _Drawn = (String tex, bool display, double fontSize, Color color); + +/// Formulas in TeX, drawn by flutter_math_fork and sized before the text +/// around them is laid out. +/// +/// A formula stands in its line as a placeholder, and a text engine has to be +/// told how big every placeholder is before it can break lines: the field asks +/// the widget, and everything that measures the note apart from the field has +/// only the numbers. So each formula is laid out once, in a widget tree of its +/// own that is never shown, and its size kept. +/// +/// The widgets handed to the field are kept as well, so that a formula nobody +/// touched is never built twice — one for every formula in the note, never +/// one for two. flutter_math_fork keys each line it builds with a [GlobalKey] +/// kept in the formula's parsed TeX, so a widget shown in two places is one +/// key in two places, and the second takes the line away from the first. +class NoteMath { + final Map _drawable = {}; + final Map<_Drawn, ({double width, double height, double baseline})> _boxes = + {}; + final Map<(_Drawn, int), Widget> _widgets = {}; + _Offscreen? _offscreen; + + /// Whether [tex] is TeX flutter_math_fork reads. One it cannot is shown as + /// written instead. + bool draws(String tex) { + final known = _drawable[tex]; + if (known != null) return known; + if (_drawable.length > 512) _drawable.clear(); + return _drawable[tex] = + Math.tex(_readable(tex), settings: _settings).parseError == null; + } + + /// [tex] with what pandoc and Wikipedia write and flutter_math_fork does + /// not read, as what it does. The environments of a LaTeX document become + /// the ones it has: `align` is drawn as `aligned` is, `alignat` as + /// `alignedat`, a `split` as `aligned`, and an `equation` is only the + /// number a note has no use for. A phantom of a sized delimiter written + /// without braces, `\vphantom\bigg)`, which Wikipedia's TeX reads as one + /// argument, gets its braces. + static String _readable(String tex) => tex + .replaceAllMapped( + _environment, + (match) => switch (match[2]) { + 'equation' => '', + 'alignat' => '\\${match[1]}{alignedat}', + _ => '\\${match[1]}{aligned}', + }, + ) + .replaceAllMapped( + _phantomDelimiter, + (match) => '\\${match[1]}{${match[2]}}', + ); + + static final _environment = RegExp( + r'\\(begin|end)\{(align|alignat|equation|split)\*?\}', + ); + + static final _phantomDelimiter = RegExp( + r'\\([vh]?phantom)\s*(\\[Bb]igg?[lmr]?\s*(?:\\[A-Za-z]+|\\.|[^\s\\{}]))', + ); + + /// The commands Wikipedia's TeX has that flutter_math_fork lacks: `\sgn`, + /// the sign function. + static final _settings = TexParserSettings( + macros: {r'\sgn': MacroDefinition.fromString(r'\operatorname{sgn}')}, + ); + + /// [tex] drawn at [fontSize] in [color], set apart from the words around it + /// when [display], and no wider than [maxWidth]: a formula wider than the + /// writing column is drawn smaller rather than cut off. + /// + /// [copy] says which of the note's formulas with this TeX it is, counting + /// from 0 in the order they are written, and each gets a widget of its own. + NoteFormula formula( + String tex, { + required bool display, + required double fontSize, + required Color color, + required double maxWidth, + required int copy, + }) { + var drawn = (tex, display, fontSize, color); + var box = _box(drawn); + if (box.width > maxWidth && maxWidth > 0) { + // Rules and spacing scale with the size, so this is close to exact; what + // little is not is taken up by asking for a touch less. + final fitted = + (fontSize * maxWidth / box.width * 0.98 * 10).floorToDouble() / 10; + drawn = (tex, display, fitted, color); + box = _box(drawn); + } + if (_widgets.length > 1024) _widgets.clear(); + return ( + child: _widgets[(drawn, copy)] ??= _math(drawn), + width: box.width, + height: box.height, + baseline: box.baseline, + ); + } + + /// How big [drawn] is, measured with a widget of its own that is never + /// shown. + ({double width, double height, double baseline}) _box(_Drawn drawn) { + final known = _boxes[drawn]; + if (known != null) return known; + if (_boxes.length > 512) _boxes.clear(); + return _boxes[drawn] = (_offscreen ??= _Offscreen()).measure(_math(drawn)); + } + + /// [drawn] as a widget, with nothing of it in the semantics tree. A screen + /// reader reads the note's text, where a formula is its TeX, and + /// flutter_math_fork gives every glyph a node of its own, which in a note + /// with hundreds of formulas is most of the tree. + Widget _math(_Drawn drawn) { + final (tex, display, fontSize, color) = drawn; + return ExcludeSemantics( + child: Math.tex( + _readable(tex), + settings: _settings, + options: MathOptions( + style: display ? MathStyle.display : MathStyle.text, + fontSize: fontSize, + color: color, + ), + // Only what TeX parses and still cannot build gets here: shown as its + // TeX, which is what a formula that does not parse is shown as too. + onErrorFallback: (_) => Text( + tex, + style: TextStyle(fontSize: fontSize, color: color), + textDirection: TextDirection.ltr, + ), + ), + ); + } + + void dispose() { + _offscreen?.dispose(); + _offscreen = null; + _boxes.clear(); + _widgets.clear(); + } +} + +/// A widget tree of its own, laid out and never painted, for knowing how big +/// a widget is before it is shown. +class _Offscreen { + _Offscreen() { + _pipeline.rootNode = _root; + _root.scheduleInitialLayout(); + _element = RenderObjectToWidgetAdapter( + container: _root, + child: const SizedBox.shrink(), + ).attachToRenderTree(_owner); + } + + final _pipeline = PipelineOwner(); + final _owner = BuildOwner(focusManager: FocusManager()); + final _root = _Measure(); + late final RenderObjectToWidgetElement _element; + + ({double width, double height, double baseline}) measure(Widget widget) { + RenderObjectToWidgetAdapter( + container: _root, + child: Directionality(textDirection: TextDirection.ltr, child: widget), + ).attachToRenderTree(_owner, _element); + _owner.buildScope(_element); + _pipeline.flushLayout(); + // The formula measured before this one is done with. + _owner.finalizeTree(); + return ( + width: _root.size.width, + height: _root.size.height, + baseline: _root.baseline, + ); + } + + void dispose() { + RenderObjectToWidgetAdapter( + container: _root, + ).attachToRenderTree(_owner, _element); + _owner.buildScope(_element); + _owner.finalizeTree(); + _owner.focusManager.dispose(); + _pipeline.rootNode = null; + _pipeline.dispose(); + } +} + +/// The root of [_Offscreen]: lays its one child out at the size it wants, and +/// asks it for its baseline, which only a parent laying it out may do. +class _Measure extends RenderObject with RenderObjectWithChildMixin { + Size size = Size.zero; + double baseline = 0; + + @override + void performLayout() { + final child = this.child; + if (child == null) return; + child.layout(const BoxConstraints(), parentUsesSize: true); + size = child.size; + baseline = + child.getDistanceToBaseline(TextBaseline.alphabetic, onlyReal: true) ?? + size.height; + } + + @override + void performResize() {} + + @override + void debugAssertDoesMeetConstraints() {} + + @override + Rect get paintBounds => Offset.zero & size; + + @override + Rect get semanticBounds => paintBounds; +} diff --git a/lib/ui/editor/table_geometry.dart b/lib/ui/editor/table_geometry.dart index 71b3203..0ab93b1 100644 --- a/lib/ui/editor/table_geometry.dart +++ b/lib/ui/editor/table_geometry.dart @@ -6,7 +6,7 @@ import 'markdown_syntax.dart'; /// How a run of a cell's words is drawn, given the markdown over them. typedef MarkdownRunStyle = - TextStyle Function(TextStyle base, Set styles); + TextStyle Function(TextStyle base, Set styles, String words); /// The grid one table is drawn as: how wide each column is, how tall each row /// is, and the words of each cell laid out ready to paint. @@ -28,6 +28,10 @@ typedef MarkdownRunStyle = /// onto as many lines as they need. A phone's writing column is narrower than /// most tables, and hiding half a cell behind an ellipsis is no way to edit /// one. +/// +/// Either way a `
` in a cell starts a new line of it, so a cell has as +/// many lines as it was written with at the least: one each, cut short one by +/// one, when the table is its natural size. class TableGeometry { TableGeometry({ required this.table, @@ -52,24 +56,28 @@ class TableGeometry { final painters = []; for (var c = 0; c < row.cells.length; c++) { final cell = row.cells[c]; + final words = [ + for (final run in analysis.runsIn(cell.start, cell.end)) + TextSpan( + text: run.text, + style: runStyle(base, { + ...run.styles, + if (row.header) MarkdownStyle.tableHeader, + }, run.text), + ), + ]; + final broken = words.any((span) => span.text!.contains('\n')); final painter = TextPainter( text: TextSpan( style: base, - children: [ - for (final run in analysis.runsIn(cell.start, cell.end)) - TextSpan( - text: run.text, - style: runStyle(base, { - ...run.styles, - if (row.header) MarkdownStyle.tableHeader, - }), - ), - ], + children: fitting || !broken + ? words + : _cutShort(words, base, scaler, maxCellWidth), ), textDirection: TextDirection.ltr, textScaler: scaler, - maxLines: fitting ? null : 1, - ellipsis: fitting ? null : '…', + maxLines: fitting || broken ? null : 1, + ellipsis: fitting || broken ? null : '…', )..layout(maxWidth: maxCellWidth); natural[c] = math.max(natural[c], painter.width); longestWord[c] = math.max(longestWord[c], painter.minIntrinsicWidth); @@ -217,6 +225,81 @@ class TableGeometry { return null; } + /// [words] with each of its lines that is wider than [width] cut short, an + /// ellipsis in place of the rest: what a single-line cell gets from its + /// painter, done line by line, since a painter only shortens the last line + /// it is allowed and wraps every one before it. + static List _cutShort( + List words, + TextStyle base, + TextScaler scaler, + double width, + ) { + final lines = >[[]]; + for (final span in words) { + final pieces = span.text!.split('\n'); + for (var i = 0; i < pieces.length; i++) { + if (i > 0) lines.add([]); + if (pieces[i].isNotEmpty) { + lines.last.add(TextSpan(text: pieces[i], style: span.style)); + } + } + } + double widthOf(List line) { + final painter = TextPainter( + text: TextSpan(style: base, children: line), + textDirection: TextDirection.ltr, + textScaler: scaler, + maxLines: 1, + )..layout(); + final width = painter.width; + painter.dispose(); + return width; + } + + List ellipsized(List line, int length) { + final kept = []; + var left = length; + for (final span in line) { + final text = span.text!; + if (left < text.length) { + var end = left; + if (end > 0 && _isLeadSurrogate(text.codeUnitAt(end - 1))) end--; + kept.add( + TextSpan(text: '${text.substring(0, end)}…', style: span.style), + ); + return kept; + } + kept.add(span); + left -= text.length; + } + return kept; + } + + final result = []; + for (var l = 0; l < lines.length; l++) { + var line = lines[l]; + if (widthOf(line) > width) { + var low = 0; + var high = line.fold(0, (sum, span) => sum + span.text!.length); + while (low < high) { + final middle = (low + high + 1) ~/ 2; + if (widthOf(ellipsized(line, middle)) <= width) { + low = middle; + } else { + high = middle - 1; + } + } + line = ellipsized(line, low); + } + if (l > 0) result.add(const TextSpan(text: '\n')); + result.addAll(line); + } + return result; + } + + static bool _isLeadSurrogate(int unit) => unit >= 0xD800 && unit <= 0xDBFF; + /// [natural] shrunk to fit [available]. /// /// Wide columns give way before narrow ones: the widest are capped at one diff --git a/pubspec.lock b/pubspec.lock index c1a002b..db5ba9f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -416,6 +416,14 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_math_fork: + dependency: "direct main" + description: + name: flutter_math_fork + sha256: "6d5f2f1aa57ae539ffb0a04bb39d2da67af74601d685a161aff7ce5bda5fa407" + url: "https://pub.dev" + source: hosted + version: "0.7.4" flutter_plugin_android_lifecycle: dependency: transitive description: @@ -892,6 +900,14 @@ packages: url: "https://pub.dev" source: hosted version: "3.1.0" + nested: + dependency: transitive + description: + name: nested + sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20" + url: "https://pub.dev" + source: hosted + version: "1.0.0" nm: dependency: transitive description: @@ -1044,6 +1060,14 @@ packages: url: "https://pub.dev" source: hosted version: "5.0.6" + provider: + dependency: transitive + description: + name: provider + sha256: "4e82183fa20e5ca25703ead7e05de9e4cceed1fbd1eadc1ac3cb6f565a09f272" + url: "https://pub.dev" + source: hosted + version: "6.1.5+1" pub_semver: dependency: transitive description: @@ -1068,6 +1092,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.2.3" + re_highlight: + dependency: "direct main" + description: + name: re_highlight + sha256: "6c4ac3f76f939fb7ca9df013df98526634e17d8f7460e028bd23a035870024f2" + url: "https://pub.dev" + source: hosted + version: "0.0.3" record: dependency: "direct main" description: @@ -1423,6 +1455,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.5.3" + tuple: + dependency: transitive + description: + name: tuple + sha256: a97ce2013f240b2f3807bcbaf218765b6f301c3eff91092bcfa23a039e7dd151 + url: "https://pub.dev" + source: hosted + version: "2.0.2" typed_data: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 44a4fb1..727ae05 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -154,6 +154,16 @@ dependencies: # passes the CommonMark and GFM spec suites, and it is pure Dart, so it runs # the same on every platform and inside a widget test. dart_markdown: ^3.1.7 + # Drawing a note's `$…$` and `$$…$$` as the formulas they write. Pure Dart + # and Flutter's own text engine, with KaTeX's fonts bundled (about 660 KB), + # so it lays a formula out offline, the same on every platform and inside a + # widget test, and reports the baseline a formula among words sits on. + # Apache-2.0. + flutter_math_fork: ^0.7.4 + # Colouring a fenced code block in the language its info string names: the + # highlight.js grammars ported to Dart, MIT. Each grammar is a value of its + # own that the app names, so only the languages it registers are compiled in. + re_highlight: ^0.0.3 # One quiet, consistent line-icon language across every app surface. # Hugeicons' free stroke-rounded set is MIT licensed and tree-shaken. hugeicons: ^1.1.7 @@ -213,6 +223,9 @@ flutter: - assets/fonts/ShantellSans-OFL.txt - assets/fonts/Figtree-OFL.txt - assets/fonts/GeistMono-OFL.txt + # The notice KaTeX's fonts come with, which flutter_math_fork bundles + # without: its own LICENSE is the package's, not the fonts'. + - packages/flutter_math_fork/katex_fonts/LICENSE fonts: - family: Shantell Sans fonts: diff --git a/test/goldens/markdown_rendering_dark.png b/test/goldens/markdown_rendering_dark.png new file mode 100644 index 0000000..b632daf Binary files /dev/null and b/test/goldens/markdown_rendering_dark.png differ diff --git a/test/goldens/markdown_rendering_light.png b/test/goldens/markdown_rendering_light.png new file mode 100644 index 0000000..3df8041 Binary files /dev/null and b/test/goldens/markdown_rendering_light.png differ diff --git a/test/goldens/markdown_rendering_revealed_dark.png b/test/goldens/markdown_rendering_revealed_dark.png new file mode 100644 index 0000000..9ad6d52 Binary files /dev/null and b/test/goldens/markdown_rendering_revealed_dark.png differ diff --git a/test/markdown_syntax_test.dart b/test/markdown_syntax_test.dart index c106650..99d72c8 100644 --- a/test/markdown_syntax_test.dart +++ b/test/markdown_syntax_test.dart @@ -482,6 +482,183 @@ void main() { }); }); + group('math', () { + List formulas(String note) => [ + for (final math in read(note).maths) + '${math.display ? 'display' : 'inline'} ${math.start}-${math.end} ' + '${math.tex}', + ]; + + test('is never a price', () { + for (final note in [ + r'$5 and $10', + r'$4.50 + $3', + r'costs $5, total $10', + r'between $5-$10', + r'a $x$5 b', + r'paid \$5 and \$10', + ]) { + expect(formulas(note), isEmpty, reason: note); + } + const prices = r'$5 and $10'; + expect(identical(read(prices).calculatorText, prices), isTrue); + }); + + test('is read among words by pandoc\'s rules', () { + expect(formulas(r'The area is $\pi r^2$ here'), [ + r'inline 12-21 \pi r^2', + ]); + // A backslash keeps a dollar sign inside, and \text{} may hold some. + expect(formulas(r'$a \$ b$'), [r'inline 0-8 a \$ b']); + expect(formulas(r'$\text{costs $5}$ ok'), [ + r'inline 0-17 \text{costs $5}', + ]); + expect(formulas(r'\$5 and \$10 and $y$'), ['inline 17-20 y']); + expect(formulas('a \$b +\nc\$ d'), ['inline 2-9 b +\nc']); + expect(formulas(r'`code $x$` and $y$'), ['inline 15-18 y']); + // Among words, $$ is a formula among words too. + expect(formulas(r'math $$x^2$$ among words'), ['inline 5-12 x^2']); + }); + + test('set apart is TeX up to its closing \$\$', () { + const note = '\$\$\nx^2\n- y\n\$\$\n\nafter'; + expect(formulas(note), ['display 0-13 x^2\n- y']); + expect(read(note).listItems, isEmpty, reason: '- y is TeX'); + expect(formulas(r'$$x = 1$$'), ['display 0-9 x = 1']); + expect(formulas('para\n\$\$\nz\n\$\$\nmore'), ['display 5-12 z']); + expect(formulas('> \$\$\n> a\n> \$\$'), ['display 2-13 a']); + expect(formulas('- item\n\n \$\$\n a + b\n \$\$'), [ + 'display 10-25 a + b', + ]); + }); + + test('set apart needs its closing \$\$ before a blank line', () { + expect(formulas('\$\$\nunclosed\n\nnext'), isEmpty); + expect(formulas(r'$$'), isEmpty); + }); + + test('is literal, and the calculator reads none of it', () { + const note = r'5 + 3 and $1 + 1$'; + final analysis = read(note); + expect(stylesOf(analysis, r'$1 + 1$'), contains(MarkdownStyle.math)); + expect(analysis.isLiteral(10, 17), isTrue); + expect(analysis.calculatorText, '5 + 3 and '.padRight(note.length)); + }); + + test('in a table is left to the grid, which shows its TeX', () { + final analysis = read('| a |\n|---|\n| \$x\$ |'); + expect(analysis.maths, isEmpty); + final cell = analysis.tables.single.rows[1].cells.single; + expect( + [ + for (final run in analysis.runsIn(cell.start, cell.end)) + '${run.text}:${run.styles.map((style) => style.name).join('+')}', + ], + [r'$x$:math'], + ); + }); + + test('drawn, is hidden whole; with the caret in it, shown as written', () { + expect(visible(r'a $x$ b|'), 'a b'); + expect(visible(r'a $x|$ b'), r'a $x$ b'); + expect(visible(r'a $x|$ b', typing: true), r'a $x$ b'); + // Against an edge it shows as any inline marker does: not while typing. + expect(visible(r'a $x$| b'), r'a $x$ b'); + expect(visible(r'a $x$| b', typing: true), 'a b'); + expect(visible(r'a $x|$ b', editing: false), 'a b'); + // Set apart, it shows with the caret against it, as a block's syntax. + expect(visible('\$\$\nx\n\$\$|', typing: true), '\$\$\nx\n\$\$'); + expect(visible('\$\$\nx\n\$\$\n|'), '\n'); + }); + + test('drawn, is named for the editor to draw', () { + const note = r'a $x$ and $$y$$'; + final drawn = read( + note, + ).concealFor(null, revealBlocks: false, revealEdges: false); + expect([for (final math in drawn.formulas) math.tex], ['x', 'y']); + final undrawable = read(note).concealFor( + null, + revealBlocks: false, + revealEdges: false, + drawable: (math) => math.tex != 'x', + ); + expect([for (final math in undrawable.formulas) math.tex], ['y']); + expect(undrawable.hidden, [const TextRange(start: 10, end: 15)]); + }); + }); + + group('HTML among words', () { + test(', , and style what they hold', () { + final analysis = read( + 'under inserted x2 H3O', + ); + expect(stylesOf(analysis, 'under'), contains(MarkdownStyle.underline)); + expect(stylesOf(analysis, 'inserted'), contains(MarkdownStyle.underline)); + expect(stylesOf(analysis, '2'), contains(MarkdownStyle.superscript)); + expect(stylesOf(analysis, '3'), contains(MarkdownStyle.subscript)); + expect(stylesOf(analysis, ''), contains(MarkdownStyle.syntax)); + expect(stylesOf(analysis, ''), isNot(contains(MarkdownStyle.html))); + }); + + test('their tags hide like any inline marker', () { + expect(visible('a word b|'), 'a word b'); + expect(visible('a word| b'), 'a word b'); + expect(visible('a word| b', typing: true), 'a word b'); + }); + + test('the calculator reads their words without them', () { + expect(read('5 + 3').calculatorText, ' 5 + 3 '); + }); + + test('one never closed, or crossing another, stays the HTML it is', () { + final open = read('open'); + expect(stylesOf(open, ''), contains(MarkdownStyle.html)); + expect(stylesOf(open, 'open'), isNot(contains(MarkdownStyle.underline))); + + final crossed = read('x'); + expect(stylesOf(crossed, 'x'), contains(MarkdownStyle.underline)); + expect( + stylesOf(crossed, 'x'), + isNot(contains(MarkdownStyle.superscript)), + ); + expect(stylesOf(crossed, ''), contains(MarkdownStyle.html)); + }); + + test('
breaks the line of a table\'s cell, and nothing else', () { + final analysis = read( + '| a |\n|---|\n| one
two
three
four |', + ); + final cell = analysis.tables.single.rows[1].cells.single; + expect( + [for (final run in analysis.runsIn(cell.start, cell.end)) run.text], + ['one\ntwo\nthree\nfour'], + ); + expect(analysis.lineBreaks, hasLength(3)); + expect(read('one
two').lineBreaks, isEmpty); + }); + }); + + group('fenced code', () { + test('that names a language is kept, with where its code is', () { + const note = + '```python\nprint(1)\n```\n\n' + '- ```ts\n let x = 1;\n ```\n\n' + '```\nplain\n```'; + final blocks = read(note).fencedCode; + expect([for (final block in blocks) block.language], ['python', 'ts']); + String code(MarkdownCodeBlock block) => [ + for (final range in block.code) note.substring(range.start, range.end), + ].join(); + expect(code(blocks.first), 'print(1)\n'); + expect( + code(blocks.last), + 'let x = 1;\n', + reason: 'the item\'s indentation is not code', + ); + }); + }); + group('reading in pieces', () { const vocabulary = [ '', '', '', ' ', '\t', // @@ -540,6 +717,12 @@ void main() { ]}', ], ranges(analysis.atomicPrefixes), + [for (final math in analysis.maths) math.toString()], + [ + for (final block in analysis.fencedCode) + '${block.language}-${ranges(block.code)}', + ], + ranges(analysis.lineBreaks), analysis.calculatorText, ]; } @@ -556,6 +739,28 @@ void main() { } }); + test('gives the same with math and HTML in the note', () { + const markup = [ + r'$$', r'$$x$$', r'$x$', r'$\frac{a}{b}$', r'$5 and $10', // + r'a \$5', r'$ $', r'a $x', r'y$ b', r'> $$', r'- $$', r' $$', + r'$$ $$', r'$\text{$}$', 'u', '2', '', '', + '
', '| a
b |', r'| $x$ | y |', '|---|---|', '```py', + '```', + ]; + final random = Random(41); + for (var i = 0; i < 800; i++) { + final note = List.generate(random.nextInt(24), (_) { + final pool = random.nextBool() ? markup : vocabulary; + return pool[random.nextInt(pool.length)]; + }).join('\n'); + expect( + everything(MarkdownAnalyzer().analyze(note)), + everything(MarkdownAnalyzer.analyzeWhole(note)), + reason: note, + ); + } + }); + test('and keeps doing so edit after edit', () { final random = Random(11); const typed = ['*', '`', '\n', ' ', '#', '-', '>', 'x', '[', ']', '~']; diff --git a/test/note_editor_markdown_test.dart b/test/note_editor_markdown_test.dart index 6513887..da97289 100644 --- a/test/note_editor_markdown_test.dart +++ b/test/note_editor_markdown_test.dart @@ -2,6 +2,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart' show PointerDeviceKind, PointerScrollEvent, kSecondaryButton; import 'package:flutter/services.dart'; +import 'package:flutter_math_fork/flutter_math.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:kapy_notes/calc/engine.dart'; import 'package:kapy_notes/calc/highlight.dart'; @@ -40,6 +41,7 @@ Widget harness( bool markdown = true, bool autofocus = false, bool readOnly = false, + bool light = false, double? width, List initialFormats = const [], ValueChanged? onBodyChanged, @@ -56,7 +58,7 @@ Widget harness( ); return MaterialApp( - theme: KapyTheme.dark(), + theme: light ? KapyTheme.light() : KapyTheme.dark(), home: Scaffold( body: bound( NoteEditor( @@ -1639,4 +1641,267 @@ void main() { expect(controllerOf(tester).text, 'say **hi** x'); }); }); + group('formulas, code and HTML', () { + /// Where the first box [needle] is drawn in sits on screen. + Rect rectOf(WidgetTester tester, String needle) { + final start = controllerOf(tester).text.indexOf(needle); + final editable = tester + .state(find.byType(EditableText)) + .renderEditable; + final box = editable + .getBoxesForSelection( + TextSelection( + baseOffset: start, + extentOffset: start + needle.length, + ), + ) + .first; + return Rect.fromPoints( + editable.localToGlobal(Offset(box.left, box.top)), + editable.localToGlobal(Offset(box.right, box.bottom)), + ); + } + + testWidgets('a formula is drawn in place of its TeX, shown with the caret ' + 'in it', (tester) async { + const body = r'Area $\pi r^2$ here'; + await tester.pumpWidget(harness(body, autofocus: true)); + await tester.pumpAndSettle(); + controllerOf(tester).selection = const TextSelection.collapsed(offset: 0); + await tester.pump(); + expect(find.byType(Math), findsOneWidget); + expect(isHidden(tester, r'\pi'), isTrue); + + // The caret inside: the TeX as written, as code, where the formula was. + controllerOf(tester).selection = TextSelection.collapsed( + offset: body.indexOf('r^2'), + ); + await tester.pump(); + expect(find.byType(Math), findsNothing); + expect(isHidden(tester, r'\pi'), isFalse); + expect( + styleOf(tester, r'\pi').fontFamily, + WritingFont.monospace.fontFamily, + ); + + // Typed in, it stays as written. + await typeAtCaret(tester, '2'); + expect(controllerOf(tester).text, r'Area $\pi 2r^2$ here'); + expect(isHidden(tester, r'\pi'), isFalse); + + // Away from it, drawn again. + controllerOf(tester).selection = const TextSelection.collapsed(offset: 0); + await tester.pump(); + expect(find.byType(Math), findsOneWidget); + }); + + testWidgets('arrow keys walk through a formula\'s TeX, and it is drawn ' + 'again past it', (tester) async { + await tester.pumpWidget(harness(r'a $x$ b', autofocus: true)); + await tester.pumpAndSettle(); + controllerOf(tester).selection = const TextSelection.collapsed(offset: 1); + await tester.pump(); + expect(find.byType(Math), findsOneWidget); + + // Against it and inside it, the caret is never somewhere unseen. + for (var offset = 2; offset <= 5; offset++) { + await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight); + await tester.pump(); + expect(controllerOf(tester).selection.baseOffset, offset); + expect(find.byType(Math), findsNothing, reason: 'at $offset'); + } + await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight); + await tester.pump(); + expect(controllerOf(tester).selection.baseOffset, 6); + expect(find.byType(Math), findsOneWidget); + }); + + testWidgets('the same formula twice is drawn twice', (tester) async { + // flutter_math_fork keys each line it builds with a GlobalKey, so one + // widget shown in two places is a key in two places. + await tester.pumpWidget(harness(r'$x^2$ and again $x^2$')); + await tester.pumpAndSettle(); + expect(tester.takeException(), isNull); + expect(find.byType(Math), findsNWidgets(2)); + }); + + testWidgets('TeX it cannot draw stays as written, as code', (tester) async { + await tester.pumpWidget(harness(r'bad $\frac{a$ here')); + await tester.pumpAndSettle(); + expect(find.byType(Math), findsNothing); + expect(isHidden(tester, r'\frac'), isFalse); + expect( + styleOf(tester, r'\frac').fontFamily, + WritingFont.monospace.fontFamily, + ); + }); + + testWidgets('dollar amounts stay words', (tester) async { + await tester.pumpWidget(harness(r'Lunch $5 and coffee $10')); + await tester.pumpAndSettle(); + expect(find.byType(Math), findsNothing); + expect(isHidden(tester, r'$5'), isFalse); + expect( + styleOf(tester, r'$5').fontFamily, + isNot(WritingFont.monospace.fontFamily), + ); + }); + + testWidgets('set apart, a formula takes a line of its own and results ' + 'keep to theirs', (tester) async { + const body = '1 + 1\n\$\$\n\\frac{a}{b}\n\$\$\n2 + 2'; + await tester.pumpWidget(harness(body)); + await tester.pumpAndSettle(); + final formula = tester.getRect(find.byType(Math)); + final above = rectOf(tester, '1 + 1'); + final below = rectOf(tester, '2 + 2'); + expect(formula.top, greaterThanOrEqualTo(above.bottom)); + expect(formula.bottom, lessThanOrEqualTo(below.top)); + expect(formula.left, greaterThan(above.right), reason: 'it is centred'); + expect( + tester.getCenter(chipWithText('4')).dy, + closeTo(below.center.dy, 2), + reason: 'the result is beside the line it belongs to', + ); + }); + + testWidgets('fenced code is coloured by its language, from the palette', ( + tester, + ) async { + const body = + '```python\ndef total(xs):\n return sum(xs) # all\n```\n\n' + '```nosuchlanguage\ndefine "it" # too\n```'; + await tester.pumpWidget(harness(body)); + await tester.pumpAndSettle(); + const palette = KapyTheme.darkPalette; + expect(styleOf(tester, 'def').color, palette.keyword); + expect(styleOf(tester, 'return').color, palette.keyword); + expect(styleOf(tester, 'total').color, palette.function); + expect(styleOf(tester, '# all').color, palette.comment); + expect(styleOf(tester, '# all').fontStyle, FontStyle.italic); + // A language it does not know is left as it is written. + final plain = styleOf(tester, 'define'); + expect(styleOf(tester, '"it"').color, plain.color); + expect(styleOf(tester, '# too').color, plain.color); + expect(styleOf(tester, '# too').fontStyle, plain.fontStyle); + }); + + testWidgets('underline, superscript and subscript, their tags hidden', ( + tester, + ) async { + const body = + 'a under E = mc2 and H3O xn'; + await tester.pumpWidget(harness(body)); + await tester.pumpAndSettle(); + expect(isHidden(tester, ''), isTrue); + expect(isHidden(tester, '
'), isTrue); + expect(styleOf(tester, 'under').decoration, TextDecoration.underline); + // The handwritten face has raised and lowered figures of its own. + expect( + styleOf(tester, '2<').fontFeatures, + contains(const FontFeature.enable('sups')), + ); + expect( + styleOf(tester, '3<').fontFeatures, + contains(const FontFeature.enable('sinf')), + ); + // But not letters, which are drawn smaller instead. + expect( + styleOf(tester, 'n<').fontSize, + lessThan(styleOf(tester, 'x<').fontSize!), + ); + }); + + testWidgets('a
in a cell gives its row another line', (tester) async { + Future afterWith(String cell) async { + await tester.pumpWidget(const SizedBox.shrink()); + await tester.pumpAndSettle(); + await tester.pumpWidget(harness('| a |\n| --- |\n| $cell |\n\nafter')); + await tester.pumpAndSettle(); + return centerOf(tester, 'after').dy; + } + + final one = await afterWith('one two'); + final two = await afterWith('one
two'); + final three = await afterWith('one
two
three'); + expect(two, greaterThan(one)); + expect(three - two, greaterThan(0)); + expect(three - two, closeTo(two - one, 1), reason: 'a line apiece'); + }); + + group('as a golden', () { + const note = + '# Formulas, code and underline\n' + '\n' + r'The area of a circle is $\pi r^2$, and lunch was $5 or $10.' + '\n' + '\n' + '\$\$\n' + r'x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}' + '\n' + '\$\$\n' + '\n' + r'TeX that does not parse stays as written: $\frac{a$' + '\n' + '\n' + 'Underlined, E = mc2, H2O and ' + '2n\n' + '\n' + '```python\n' + 'def area(r):\n' + ' # a circle\n' + ' return 3.14159 * r ** 2 # "pi r squared"\n' + '```\n' + '\n' + '| Constant | Value |\n' + '| --- | --- |\n' + '| Speed of light | 299 792 458
metres a second |\n' + r'| $c^2$ | squared |' + '\n' + '\n' + '5 + 3'; + + Future pumpNote( + WidgetTester tester, { + bool light = false, + bool focused = false, + }) async { + tester.view.physicalSize = const Size(900, 1000); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + await tester.pumpWidget( + harness(note, light: light, autofocus: focused), + ); + await tester.pumpAndSettle(); + } + + testWidgets('dark', (tester) async { + await pumpNote(tester); + await expectLater( + find.byType(NoteEditor), + matchesGoldenFile('goldens/markdown_rendering_dark.png'), + ); + }); + + testWidgets('light', (tester) async { + await pumpNote(tester, light: true); + await expectLater( + find.byType(NoteEditor), + matchesGoldenFile('goldens/markdown_rendering_light.png'), + ); + }); + + testWidgets('dark, the caret in a formula set apart', (tester) async { + await pumpNote(tester, focused: true); + controllerOf(tester).selection = TextSelection.collapsed( + offset: note.indexOf(r'\frac{-b'), + ); + await tester.pumpAndSettle(); + await expectLater( + find.byType(NoteEditor), + matchesGoldenFile('goldens/markdown_rendering_revealed_dark.png'), + ); + }); + }); + }); } diff --git a/test/note_editor_test.dart b/test/note_editor_test.dart index 89a51f7..7842840 100644 --- a/test/note_editor_test.dart +++ b/test/note_editor_test.dart @@ -251,6 +251,24 @@ void main() { expect(decoration.focusedErrorBorder, InputBorder.none); }); + testWidgets('works out what the note says to the semantics tree once', ( + tester, + ) async { + const body = 'Bread 4\nMilk 2\n\n# Shopping'; + await tester.pumpWidget(harness(body, autofocus: true)); + await tester.pumpAndSettle(); + + // The field asks again whenever its semantics are marked dirty, which it + // does for every widget it takes in, and each answer walks the note. + final text = tester + .state(find.byType(EditableText)) + .renderEditable + .text!; + final semantics = text.getSemanticsInformation(); + expect(semantics.map((info) => info.text).join(), body); + expect(text.getSemanticsInformation(), same(semantics)); + }); + testWidgets('ticking a box celebrates, unticking it does not', ( tester, ) async { diff --git a/test/note_math_test.dart b/test/note_math_test.dart new file mode 100644 index 0000000..79b23b0 --- /dev/null +++ b/test/note_math_test.dart @@ -0,0 +1,99 @@ +import 'package:flutter/widgets.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:kapy_notes/ui/editor/note_math.dart'; + +import 'test_fonts.dart'; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + setUpAll(loadTestFonts); + + late NoteMath math; + setUp(() => math = NoteMath()); + tearDown(() => math.dispose()); + + NoteFormula draw( + String tex, { + bool display = false, + double maxWidth = 1000, + int copy = 0, + }) => math.formula( + tex, + display: display, + fontSize: 16, + color: const Color(0xFF000000), + maxWidth: maxWidth, + copy: copy, + ); + + test('draws the TeX it reads, and says which it cannot', () { + expect(math.draws(r'\frac{a}{b}'), isTrue); + expect(math.draws(r'\frac{a'), isFalse); + expect(math.draws(r'\nosuchcommand'), isFalse); + }); + + test('reads a LaTeX document\'s environments as the ones it has', () { + expect(math.draws(r'\begin{align} a &= b \\ c &= d \end{align}'), isTrue); + expect(math.draws(r'\begin{align*} a &= b \end{align*}'), isTrue); + expect( + math.draws(r'\begin{alignat}{2} a &= b &&= c \end{alignat}'), + isTrue, + ); + expect( + math.draws( + r'\begin{equation}\begin{split} a &= b \\ &= c \end{split}\end{equation}', + ), + isTrue, + ); + }); + + test('measures a formula, with its baseline inside it', () { + final inline = draw(r'\frac{a}{b}'); + expect(inline.width, greaterThan(0)); + expect(inline.baseline, inInclusiveRange(0, inline.height)); + expect( + draw(r'\frac{a}{b}', display: true).height, + greaterThan(inline.height), + reason: 'set apart, a fraction is set larger', + ); + }); + + test('draws a formula too wide for the column smaller', () { + const long = 'a + b + c + d + e + f + g + h + i + j + k + l + m + n + o'; + final natural = draw(long); + final fitted = draw(long, maxWidth: natural.width / 2); + expect(fitted.width, lessThanOrEqualTo(natural.width / 2)); + expect(fitted.width, greaterThan(natural.width / 3)); + }); + + test('gives each copy of a formula a widget of its own, and keeps it', () { + final first = draw('x^2'); + final second = draw('x^2', copy: 1); + expect(identical(first.child, second.child), isFalse); + expect(second.width, first.width); + expect(identical(draw('x^2').child, first.child), isTrue); + }); + + test( + 'reads Wikipedia\'s \\sgn, and a phantom\'s delimiter without braces', + () { + expect(math.draws(r'x = -\sgn(b)\sqrt{\vert c\vert}'), isTrue); + expect( + math.draws(r'x = \dfrac{-b\pm\sqrt{b^2 - 4ac}}{2a}. \vphantom\bigg)'), + isTrue, + ); + }, + ); + + testWidgets('leaves a formula\'s glyphs out of the semantics tree', ( + tester, + ) async { + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Center(child: draw(r'x^2 + y').child), + ), + ); + expect(find.bySemanticsLabel(RegExp('[xy2+]')), findsNothing); + }); +} diff --git a/test/table_geometry_test.dart b/test/table_geometry_test.dart index db7f635..2bf41e3 100644 --- a/test/table_geometry_test.dart +++ b/test/table_geometry_test.dart @@ -21,7 +21,7 @@ TableGeometry geometryFor( analysis: analysis, base: _base, scaler: TextScaler.noScaling, - runStyle: (base, styles) => base, + runStyle: (base, styles, words) => base, fitToWidth: fitToWidth, minRowHeight: minRowHeight, ); @@ -301,7 +301,7 @@ void main() { analysis: analysis, base: base, scaler: TextScaler.noScaling, - runStyle: (base, styles) => base, + runStyle: (base, styles, words) => base, fitToWidth: fitToWidth, ); @@ -396,4 +396,36 @@ void main() { expect(fitted.width, closeTo(120, 0.5)); }); }); + + group('a
in a cell', () { + test('starts a line of its own, at the natural size and fitted alike', () { + for (final fitToWidth in [null, 400.0]) { + final geometry = geometryFor( + '| a |\n| --- |\n| one
two |', + fitToWidth: fitToWidth, + ); + final [header, row] = geometry.table.rows; + final cell = geometry.cell(row, 0)!; + expect(cell.plainText, 'one\ntwo', reason: '$fitToWidth'); + expect(cell.computeLineMetrics(), hasLength(2), reason: '$fitToWidth'); + expect( + geometry.rowHeight(row), + greaterThan(geometry.rowHeight(header)), + reason: '$fitToWidth', + ); + } + }); + + test('at the natural size, cuts each of its lines short on its own', () { + final long = List.filled(40, 'word').join(' '); + final geometry = geometryFor('| a |\n| --- |\n| $long
short |'); + final cell = geometry.cell(geometry.table.rows[1], 0)!; + final lines = cell.plainText.split('\n'); + expect(lines, hasLength(2)); + expect(lines.first, endsWith('…')); + expect(lines.last, 'short'); + expect(cell.computeLineMetrics(), hasLength(2)); + expect(cell.width, lessThanOrEqualTo(TableGeometry.maxCellWidth)); + }); + }); } diff --git a/test/test_fonts.dart b/test/test_fonts.dart index 411ad47..604052f 100644 --- a/test/test_fonts.dart +++ b/test/test_fonts.dart @@ -1,3 +1,4 @@ +import 'dart:convert'; import 'dart:io'; import 'package:flutter/services.dart'; @@ -35,6 +36,21 @@ Future loadTestFonts() async { 'assets/fonts/Figtree-Italic-Variable.ttf', ]); await _loadAll('Geist Mono', ['assets/fonts/GeistMono-Variable.ttf']); + + // KaTeX's faces, which flutter_math_fork draws formulas in. They are in the + // test's asset bundle like every package font, and like every one of them + // are only drawn once they are loaded. + final manifest = + jsonDecode(await rootBundle.loadString('FontManifest.json')) as List; + for (final entry in manifest.cast>()) { + final family = entry['family']! as String; + if (!family.startsWith('packages/flutter_math_fork/')) continue; + final loader = FontLoader(family); + for (final font in (entry['fonts']! as List).cast>()) { + loader.addFont(rootBundle.load(font['asset']! as String)); + } + await loader.load(); + } } Future _load(String family, List candidates) async {