Skip to content
Closed
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/
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
## 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.

## 0.7.4

* Add support for flutter 3.32.0
Expand Down
183 changes: 110 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,121 +1,158 @@
# 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.
* 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).

## Installation

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.
Add the package to your `pubspec.yaml`:

```yaml
dependencies:
flutter_math_fork: ^0.8.0
```

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).

## [Online Demo](https://znjameswu.github.io/flutter_math_demo/)

## Rendering Samples

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

![Example1](https://raw.githubusercontent.com/znjameswu/flutter_math/master/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)`

![Example2](https://raw.githubusercontent.com/znjameswu/flutter_math/master/doc/img/schrodinger.png)

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

![Example3](https://raw.githubusercontent.com/znjameswu/flutter_math/master/doc/img/fourier.png)

The current release targets Flutter `3.38.0` or newer.

## How to use
## Quick start

Add `flutter_math` to your `pubspec.yaml` dependencies
Render a display equation:

### Mobile
Currently only Android platform has been tested. If you encounter any issues with iOS, please file them.
```dart
import 'package:flutter/material.dart';
import 'package:flutter_math_fork/flutter_math.dart';

### 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/)
Math.tex(
r'x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}',
mathStyle: MathStyle.display,
textStyle: const TextStyle(fontSize: 24),
)
```

## 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.
Render inline math:

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

In-line equations
Control sizing explicitly with `MathOptions`:

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

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`
## Selectable math

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

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

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`.
Starting with `0.8.0`, `SelectableMath.toolbarOptions` uses the package-owned
`SelectableMathToolbarOptions` type instead of Flutter's deprecated
`ToolbarOptions`.

```dart
SelectableMath.tex(r'\frac a b', textStyle: TextStyle(fontSize: 42))
```
## Error handling

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

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.
```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.
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