Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ coverage
.pub/
build/
pubspec.lock
!example/pubspec.lock
/test/failures/
/test/golden/temp/

# Android related
**/android/**/gradle-wrapper.jar
Expand Down Expand Up @@ -72,6 +75,7 @@ pubspec.lock
**/ios/Flutter/app.zip
**/ios/Flutter/flutter_assets/
**/ios/Flutter/flutter_export_environment.sh
**/ios/Flutter/ephemeral/
**/ios/ServiceDefinitions.json
**/ios/Runner/GeneratedPluginRegistrant.*

Expand Down
8 changes: 8 additions & 0 deletions .pubignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Generated local build and test artifacts.
/build/
/test/failures/
/test/golden/temp/

# Local example artifacts that should not ship with the package.
/example/pubspec.lock
/example/ios/Flutter/ephemeral/
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
## 0.8.0

* Add compatibility updates for Flutter `3.38.x` and Dart `3.10.x`.
* Raise the minimum Flutter version to `3.38.0`.
* Update package constraints for `flutter_svg`, `provider`, `collection`, and `tuple`.
* Remove the direct `meta` dependency because the current Flutter SDK pins `meta` through `flutter_test`.
* Clean up deprecated Flutter API usage across rendering, selection, web, and tests.
* **Breaking:** replace direct `ToolbarOptions` usage with `SelectableMathToolbarOptions` in `SelectableMath`.
* Fix selection toolbar behavior and focus handling on current Flutter releases.
* Fix matrix child updates, font metric fallback lookup, and other null-safety/code-structure issues.
* Update the example app SDK constraints and package resolution for the current Flutter toolchain.
* Refresh golden files and documentation for the current renderer output.
* Add shaped `\text{...}` rendering for multilingual inline text, including Bangla, Arabic, Hindi, Japanese and more mixed with math on the same line.
* Add RTL-aware text-run handling for Arabic inside `\text{...}`.
* Document current Unicode/UTF-8 support behavior and add widget coverage for multilingual text rendering in both `Math.tex` and `SelectableMath.tex`.

## 0.7.4

* Add support for flutter 3.32.0
Expand Down
225 changes: 163 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,121 +1,222 @@
# Flutter Math
# Flutter Math Fork

[![Build Status](https://travis-ci.com/znjameswu/flutter_math.svg?branch=master)](https://travis-ci.com/znjameswu/flutter_math) [![codecov](https://codecov.io/gh/znjameswu/flutter_math/branch/master/graph/badge.svg)](https://codecov.io/gh/znjameswu/flutter_math) [![Pub Version](https://img.shields.io/pub/v/flutter_math_fork)](https://pub.dev/packages/flutter_math_fork)
[![Pub Version](https://img.shields.io/pub/v/flutter_math_fork)](https://pub.dev/packages/flutter_math_fork)

Math equation rendering in pure Dart and Flutter, with a parser derived from
[KaTeX](https://github.com/KaTeX/KaTeX).

## ⚠ fork
## Fork status

This is a fork of [flutter_math](https://github.com/znjameswu/flutter_math) addressing compatibility
problems while `flutter_math` is not being maintained.
`flutter_math_fork` is a maintained fork of
[flutter_math](https://github.com/znjameswu/flutter_math), with active updates
in [simpleclub/flutter_math](https://github.com/simpleclub/flutter_math) to keep
the package working on current Flutter stable releases.

---
This release is tested with Flutter `3.38.9` on the stable channel.

## Features

* TeX math parsing and rendering in pure Flutter.
* Selectable math with copy and select-all support.
* Shaped multilingual text inside `\text{...}`, including mixed inline math.
* Manual parser and AST APIs for advanced integrations.
* TeX-style line breaking support.

Math equation rendering in pure Dart & Flutter.
Unsupported or partially supported KaTeX features are documented in
[doc/unsupported.md](doc/unsupported.md).

## Unicode and UTF-8 support

This project aims to achieve maximum compatibility and fidelity with regard to the [KaTeX](https://github.com/KaTeX/KaTeX) project, while maintaining the performance advantage of Dart and Flutter. A further [UnicodeMath](https://www.unicode.org/notes/tn28/UTN28-PlainTextMath-v3.1.pdf)-style equation editing support will be experimented in the future.
Unicode input is supported, but not as unrestricted plain-text input in every
parser mode. For multilingual text mixed with math, the recommended path is
`\text{...}`.

Short version: Unicode/UTF-8 works, but not 100% in every parser mode,
script, or font setup.

The TeX parser is a Dart port of the KaTeX parser. There are only a few unsupported features and parsing differences compared to the original KaTeX parser. List of some unsupported features can be found [here](doc/unsupported.md).
What works:

## [Online Demo](https://znjameswu.github.io/flutter_math_demo/)
* Unicode math symbols supported by the parser, such as Greek letters,
arrows, operators, and many mathematical Unicode code points.
* Unicode text inside `\text{...}`.
* Complex-script shaping inside `\text{...}` for scripts such as Bangla,
Arabic, Hindi, Japanese, and more, mixed with math on the same line.
* The same `\text{...}` shaping path in both `Math.tex` and
`SelectableMath.tex`.
* Top-level Unicode text with the default parser settings or with
`TexParserSettings(strict: Strict.ignore)`.

## Rendering Samples
Important limits:

`x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}`
* If you use `TexParserSettings(strict: Strict.error)`, top-level Unicode text
in math mode is rejected by design.
* Raw Unicode typed directly in math mode is still treated as math content,
not as normal paragraph text.
* Correct shaping still depends on the app or device having a font that
supports the script. This package can shape the text run, but it does not
bundle every script font.
* Font metrics and spacing are primarily tuned for math. Arbitrary Unicode,
especially emoji and unsupported scripts, may render with fallback fonts but
should not be considered fully TeX-equivalent.

![Example1](https://raw.githubusercontent.com/znjameswu/flutter_math/master/doc/img/delta.png)
Examples:

`i\hbar\frac{\partial}{\partial t}\Psi(\vec x,t) = -\frac{\hbar}{2m}\nabla^2\Psi(\vec x,t)+ V(\vec x)\Psi(\vec x,t)`
```dart
Math.tex(r'\text{বাংলা } + x^2 = 25');

![Example2](https://raw.githubusercontent.com/znjameswu/flutter_math/master/doc/img/schrodinger.png)
Math.tex(
r'\text{العربية } + x^2 = 25',
);

`\hat f(\xi) = \int_{-\infty}^\infty f(x)e^{- 2\pi i \xi x}\mathrm{d}x`
SelectableMath.tex(
r'\text{हिन्दी } + \frac{a}{b}',
);

![Example3](https://raw.githubusercontent.com/znjameswu/flutter_math/master/doc/img/fourier.png)
Math.tex(
'বাংলা 試 é',
settings: const TexParserSettings(strict: Strict.ignore),
)
```

Rendered sample:

## How to use
`x = \frac{-b+\sqrt{b^2-4ac}}{2a}\quad \text{বাংলা}, \text{العربية}, \text{हिन्दी}, \text{日本語}, \text{中国人} + x^2 = 25`

Add `flutter_math` to your `pubspec.yaml` dependencies
![Multilingual inline sample](doc/img/unicode-inline.png)

### Mobile
Currently only Android platform has been tested. If you encounter any issues with iOS, please file them.
If you need arbitrary multilingual paragraph text, use Flutter's `Text` or
`RichText` for the prose and use `flutter_math_fork` for the math parts or for
explicit text runs inside `\text{...}`.

### Web
Web support is added in v0.1.6. It is tested for DomCanvas backend. In general it should behave largely the same with mobile. It is expected to break with CanvasKit backend. Check out the [Online Demo](https://znjameswu.github.io/flutter_math_demo/)
## Installation

## API usage (v0.2.0)
The usage is straightforward. Just `Math.tex(r'\frac a b')`. There is also optional arguments of `TexParserSettings settings`, which corresponds to Settings in KaTeX and support a subset of its features.
Add the package to your `pubspec.yaml`:

Display-style equations:
```dart
Math.tex(r'\frac a b', mathStyle: MathStyle.display) // Default
```yaml
dependencies:
flutter_math_fork: ^0.8.0
```

In-line equations
The current release targets Flutter `3.38.0` or newer.

## Quick start

Render a display equation:

```dart
Math.tex(r'\frac a b', mathStyle: MathStyle.text)
import 'package:flutter/material.dart';
import 'package:flutter_math_fork/flutter_math.dart';

Math.tex(
r'x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}',
mathStyle: MathStyle.display,
textStyle: const TextStyle(fontSize: 24),
)
```

The default size of the equation is obtained from the build context. If you wish to specify the size, you can use `textStyle`. Note: this parameter will also change how big 1cm/1pt/1inch is rendered on the screen. If you wish to specify the size of those absolute units, use `logicalPpi`
Render inline math:

```dart
Math.tex(
r'\frac a b',
textStyle: TextStyle(fontSize: 42),
// logicalPpi: MathOptions.defaultLogicalPpiFor(42),
mathStyle: MathStyle.text,
)
```

Control sizing explicitly with `MathOptions`:

```dart
Math.tex(
r'\int_0^\infty e^{-x^2}\,\mathrm{d}x',
options: MathOptions(
style: MathStyle.display,
fontSize: 22,
),
)
```

There is also a selectable variant `SelectableMath` that creates selectable and copy-able equations on both mobile and web. (EXPERIMENTAL) Users can select part of the equation and obtain the encoded TeX strings. The usage is similar to Flutter's `SelectableText`.
## Selectable math

Use `SelectableMath` when the user should be able to select or copy the TeX:

```dart
SelectableMath.tex(r'\frac a b', textStyle: TextStyle(fontSize: 42))
SelectableMath.tex(
r'\frac a b',
textStyle: const TextStyle(fontSize: 24),
toolbarOptions: const SelectableMathToolbarOptions(
copy: true,
selectAll: true,
),
)
```

If you would like to display custom styled error message, you should use `onErrorFallback` parameter. You can also process the errors in this function. But beware this function is called in build function.
Starting with `0.8.0`, `SelectableMath.toolbarOptions` uses the package-owned
`SelectableMathToolbarOptions` type instead of Flutter's deprecated
`ToolbarOptions`.

## Error handling

Provide `onErrorFallback` to render your own widget when parsing or building
fails:

```dart
Math.tex(
r'\garbled $tring',
textStyle: TextStyle(color: Colors.green),
r'\garbled $tring',
onErrorFallback: (err) => Container(
color: Colors.red,
child: Text(err.messageWithType, style: TextStyle(color: Colors.yellow)),
padding: const EdgeInsets.all(8),
child: Text(
err.messageWithType,
style: const TextStyle(color: Colors.white),
),
),
)
```

If you wish to have more granularity dealing with equations, you can manually invoke the parser and supply AST into the widget.
## Advanced usage

For manual parsing and AST handling:

```dart
SyntaxTree ast;
try {
ast = SyntaxTree(greenRoot: TexParser(r'\frac a b', TexParserSettings()).parse());
} on ParseException catch (e) {
// Handle my error here
}

SelectableMath(
import 'package:flutter_math_fork/flutter_math.dart';
import 'package:flutter_math_fork/tex.dart';

final ast = SyntaxTree(
greenRoot: TexParser(
r'\frac a b',
const TexParserSettings(),
).parse(),
);

final widget = SelectableMath(
ast: ast,
mathStyle: MathStyle.text,
textStyle: TextStyle(fontSize: 42),
)
textStyle: const TextStyle(fontSize: 24),
);
```

## [Line Breaking](doc/line_breaking.md)
See also:

* [doc/line_breaking.md](doc/line_breaking.md)
* [doc/design.md](doc/design.md)

## Rendering samples

`x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}`

![Quadratic equation](doc/img/delta.png)

`i\hbar\frac{\partial}{\partial t}\Psi(\vec x,t) = -\frac{\hbar}{2m}\nabla^2\Psi(\vec x,t)+ V(\vec x)\Psi(\vec x,t)`

![Schrodinger equation](doc/img/schrodinger.png)

`\hat f(\xi) = \int_{-\infty}^\infty f(x)e^{- 2\pi i \xi x}\mathrm{d}x`

![Fourier transform](doc/img/fourier.png)

## Credits
This project is possible thanks to the inspirations and resources from [the KaTeX Project](https://katex.org/), [MathJax](www.mathjax.org), [Zefyr](https://github.com/memspace/zefyr), and [CaTeX](https://github.com/simpleclub/CaTeX).

## Goals
- [x] : TeX math parsing (See [design doc](doc/design.md))
- [x] : AST rendering in flutter
- [x] : Selectable widget
- [x] : TeX output (WIP)
- [ ] : UnicodeMath parsing and encoding
- [ ] : [UnicodeMath](https://www.unicode.org/notes/tn28/UTN28-PlainTextMath-v3.1.pdf)-style editing
- [ ] : Breakable equations
- [ ] : MathML parsing and encoding

This project draws heavily from the work of
[KaTeX](https://katex.org/), [MathJax](https://www.mathjax.org/),
[Zefyr](https://github.com/memspace/zefyr), and
[CaTeX](https://github.com/simpleclub/CaTeX).
Binary file modified doc/img/delta.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/img/fourier.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/img/leftright.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/img/matrix.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/img/nary.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/img/schrodinger.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/img/stretchyop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/img/underover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/img/unicode-inline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 13 additions & 10 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# example
# flutter_math_fork example

A new Flutter project.
This directory contains the example application for `flutter_math_fork`.

## Getting Started
## Run the example

This project is a starting point for a Flutter application.
From the repository root:

A few resources to get you started if this is your first Flutter project:
```bash
cd example
flutter pub get
flutter run
```

- [Lab: Write your first Flutter app](https://flutter.dev/docs/get-started/codelab)
- [Cookbook: Useful Flutter samples](https://flutter.dev/docs/cookbook)
## Notes

For help getting started with Flutter, view our
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
* The example depends on the local package via `path: ../`.
* The example targets the same current Flutter baseline as the package.
* It is not intended to be published separately.
Loading