Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KaTeX (2/n): Support horizontal and vertical offsets for spans #1452

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Binary file added assets/KaTeX/KaTeX_AMS-Regular.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Caligraphic-Bold.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Caligraphic-Regular.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Fraktur-Bold.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Fraktur-Regular.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Main-Bold.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Main-BoldItalic.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Main-Italic.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Main-Regular.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Math-BoldItalic.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Math-Italic.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_SansSerif-Bold.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_SansSerif-Italic.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_SansSerif-Regular.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Script-Regular.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Size1-Regular.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Size2-Regular.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Size3-Regular.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Size4-Regular.ttf
Binary file not shown.
Binary file added assets/KaTeX/KaTeX_Typewriter-Regular.ttf
Binary file not shown.
21 changes: 21 additions & 0 deletions assets/KaTeX/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2013-2020 Khan Academy and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions lib/licenses.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import 'package:flutter/services.dart';
Stream<LicenseEntry> additionalLicenses() async* {
// Alphabetic by path.

yield LicenseEntryWithLineBreaks(
['KaTeX'],
await rootBundle.loadString('assets/KaTeX/LICENSE'));
yield LicenseEntryWithLineBreaks(
['Noto Color Emoji'],
await rootBundle.loadString('assets/Noto_Color_Emoji/LICENSE'));
Expand Down
232 changes: 173 additions & 59 deletions lib/model/content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import 'package:html/parser.dart';

import '../api/model/model.dart';
import '../api/model/submessage.dart';
import '../log.dart';
import 'code_block.dart';
import 'katex.dart';

/// A node in a parse tree for Zulip message-style content.
///
Expand Down Expand Up @@ -341,22 +343,110 @@ class CodeBlockSpanNode extends ContentNode {
}

class MathBlockNode extends BlockContentNode {
const MathBlockNode({super.debugHtmlNode, required this.texSource});
const MathBlockNode({
super.debugHtmlNode,
required this.texSource,
required this.nodes,
});

final String texSource;
final List<KatexNode>? nodes;

@override
bool operator ==(Object other) {
return other is MathBlockNode && other.texSource == texSource;
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(StringProperty('texSource', texSource));
}

@override
int get hashCode => Object.hash('MathBlockNode', texSource);
List<DiagnosticsNode> debugDescribeChildren() {
return nodes?.map((node) => node.toDiagnosticsNode()).toList() ?? const [];
}
}

sealed class KatexNode extends ContentNode {
const KatexNode({super.debugHtmlNode});
}

class KatexSpanNode extends KatexNode {
const KatexSpanNode({
required this.styles,
required this.text,
required this.nodes,
super.debugHtmlNode,
});

final KatexSpanStyles styles;
final String? text;
final List<KatexNode> nodes;

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(StringProperty('texSource', texSource));
properties.add(KatexSpanStylesProperty('styles', styles));
properties.add(StringProperty('text', text));
}

@override
List<DiagnosticsNode> debugDescribeChildren() {
return nodes.map((node) => node.toDiagnosticsNode()).toList();
}
}

class KatexNegativeMarginNode extends KatexNode {
const KatexNegativeMarginNode({
this.marginRightEm,
this.nodes = const [],
super.debugHtmlNode,
});

final double? marginRightEm;
final List<KatexNode> nodes;

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(StringProperty('marginRightEm', '$marginRightEm'));
}

@override
List<DiagnosticsNode> debugDescribeChildren() {
return nodes.map((node) => node.toDiagnosticsNode()).toList();
}
}

class KatexVlistNode extends KatexNode {
const KatexVlistNode({
required this.rows,
super.debugHtmlNode,
});

final List<KatexVlistRowNode> rows;

@override
List<DiagnosticsNode> debugDescribeChildren() {
return rows.map((row) => row.toDiagnosticsNode()).toList();
}
}

class KatexVlistRowNode extends ContentNode {
const KatexVlistRowNode({
required this.verticalOffsetEm,
this.nodes = const [],
});

final double verticalOffsetEm;
final List<KatexNode> nodes;

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(StringProperty('verticalOffsetEm', '$verticalOffsetEm'));
}

@override
List<DiagnosticsNode> debugDescribeChildren() {
return nodes.map((node) => node.toDiagnosticsNode()).toList();
}
}

Expand Down Expand Up @@ -822,23 +912,25 @@ class ImageEmojiNode extends EmojiNode {
}

class MathInlineNode extends InlineContentNode {
const MathInlineNode({super.debugHtmlNode, required this.texSource});
const MathInlineNode({
super.debugHtmlNode,
required this.texSource,
required this.nodes,
});

final String texSource;

@override
bool operator ==(Object other) {
return other is MathInlineNode && other.texSource == texSource;
}

@override
int get hashCode => Object.hash('MathInlineNode', texSource);
final List<KatexNode>? nodes;

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(StringProperty('texSource', texSource));
}

@override
List<DiagnosticsNode> debugDescribeChildren() {
return nodes?.map((node) => node.toDiagnosticsNode()).toList() ?? const [];
}
}

class GlobalTimeNode extends InlineContentNode {
Expand All @@ -864,7 +956,10 @@ class GlobalTimeNode extends InlineContentNode {

////////////////////////////////////////////////////////////////

String? _parseMath(dom.Element element, {required bool block}) {
({List<KatexNode>? spans, String texSource})? _parseMath(
dom.Element element, {
required bool block,
}) {
final dom.Element katexElement;
if (!block) {
assert(element.localName == 'span' && element.className == 'katex');
Expand All @@ -873,41 +968,55 @@ String? _parseMath(dom.Element element, {required bool block}) {
} else {
assert(element.localName == 'span' && element.className == 'katex-display');

if (element.nodes.length != 1) return null;
final child = element.nodes.single;
if (child is! dom.Element) return null;
if (child.localName != 'span') return null;
if (child.className != 'katex') return null;
katexElement = child;
}

// Expect two children span.katex-mathml, span.katex-html .
// For now we only care about the .katex-mathml .
if (katexElement.nodes.isEmpty) return null;
final child = katexElement.nodes.first;
if (child is! dom.Element) return null;
if (child.localName != 'span') return null;
if (child.className != 'katex-mathml') return null;

if (child.nodes.length != 1) return null;
final grandchild = child.nodes.single;
if (grandchild is! dom.Element) return null;
if (grandchild.localName != 'math') return null;
if (grandchild.attributes['display'] != (block ? 'block' : null)) return null;
if (grandchild.namespaceUri != 'http://www.w3.org/1998/Math/MathML') return null;

if (grandchild.nodes.length != 1) return null;
final greatgrand = grandchild.nodes.single;
if (greatgrand is! dom.Element) return null;
if (greatgrand.localName != 'semantics') return null;

if (greatgrand.nodes.isEmpty) return null;
final descendant4 = greatgrand.nodes.last;
if (descendant4 is! dom.Element) return null;
if (descendant4.localName != 'annotation') return null;
if (descendant4.attributes['encoding'] != 'application/x-tex') return null;

return descendant4.text.trim();
if (element.nodes case [
dom.Element(localName: 'span', className: 'katex') && final child,
]) {
katexElement = child;
} else {
return null;
}
}

if (katexElement.nodes case [
dom.Element(localName: 'span', className: 'katex-mathml', nodes: [
dom.Element(
localName: 'math',
namespaceUri: 'http://www.w3.org/1998/Math/MathML')
&& final mathElement,
]),
dom.Element(localName: 'span', className: 'katex-html', nodes: [...])
&& final katexHtmlElement,
]) {
if (mathElement.attributes['display'] != (block ? 'block' : null)) {
return null;
}

final String texSource;
if (mathElement.nodes case [
dom.Element(localName: 'semantics', nodes: [
...,
dom.Element(
localName: 'annotation',
attributes: {'encoding': 'application/x-tex'},
:final text),
]),
]) {
texSource = text.trim();
} else {
return null;
}

List<KatexNode>? spans;
try {
spans = KatexParser().parseKatexHTML(katexHtmlElement);
} on KatexHtmlParseError catch (e, st) {
assert(debugLog('$e\n$st'));
}

return (spans: spans, texSource: texSource);
} else {
return null;
}
}

/// Parser for the inline-content subtrees within Zulip content HTML.
Expand All @@ -920,9 +1029,12 @@ String? _parseMath(dom.Element element, {required bool block}) {
class _ZulipInlineContentParser {
InlineContentNode? parseInlineMath(dom.Element element) {
final debugHtmlNode = kDebugMode ? element : null;
final texSource = _parseMath(element, block: false);
if (texSource == null) return null;
return MathInlineNode(texSource: texSource, debugHtmlNode: debugHtmlNode);
final parsed = _parseMath(element, block: false);
if (parsed == null) return null;
return MathInlineNode(
texSource: parsed.texSource,
nodes: parsed.spans,
debugHtmlNode: debugHtmlNode);
}

UserMentionNode? parseUserMention(dom.Element element) {
Expand Down Expand Up @@ -1624,10 +1736,11 @@ class _ZulipContentParser {
})());

final firstChild = nodes.first as dom.Element;
final texSource = _parseMath(firstChild, block: true);
if (texSource != null) {
final parsed = _parseMath(firstChild, block: true);
if (parsed != null) {
result.add(MathBlockNode(
texSource: texSource,
texSource: parsed.texSource,
nodes: parsed.spans,
debugHtmlNode: kDebugMode ? firstChild : null));
} else {
result.add(UnimplementedBlockContentNode(htmlNode: firstChild));
Expand Down Expand Up @@ -1659,10 +1772,11 @@ class _ZulipContentParser {
if (child case dom.Text(text: '\n\n')) continue;

if (child case dom.Element(localName: 'span', className: 'katex-display')) {
final texSource = _parseMath(child, block: true);
if (texSource != null) {
final parsed = _parseMath(child, block: true);
if (parsed != null) {
result.add(MathBlockNode(
texSource: texSource,
texSource: parsed.texSource,
nodes: parsed.spans,
debugHtmlNode: debugHtmlNode));
continue;
}
Expand Down
Loading