Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,15 @@ The `config` argument is an object that accepts the following optional fields:
| `canvasKitForceCpuOnly` | Forces CPU-only rendering in CanvasKit without WebGL. | `boolean` |
| `canvasKitMaximumSurfaces` | Maximum number of overlay surfaces that the CanvasKit renderer can allocate. | `number` |
| `canvasKitVariant` | CanvasKit build to download: `'auto'` (default, selects the optimal build), `'full'` (all browsers), or `'chromium'` (smaller build for Chromium-based browsers only). | `string` |
| `preferWebParagraph` | **Experimental.** Uses the browser's text layout APIs through [WebParagraph][web-paragraph] instead of the text stack bundled in CanvasKit. Has no effect when the browser or renderer doesn't support it. Defaults to `false`. | `boolean` |

{:.table .table-striped}

[embedding-flutter-web]: /platform-integration/web/embedding-flutter-web
[multi-asset-cdn]: /deployment/web#multi-asset-and-cdn-loading
[nonce-mdn]: https://developer.mozilla.org/docs/Web/HTML/Global_attributes/nonce
[wasm-support]: /platform-integration/web/wasm
[web-paragraph]: /platform-integration/web/web-paragraph

## Example usage

Expand Down
157 changes: 157 additions & 0 deletions sites/docs/src/content/platform-integration/web/web-paragraph.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
---
title: Browser-native text layout with WebParagraph
description: >-
Try WebParagraph, Flutter's experimental text layout engine for the web
that uses the browser's own text APIs instead of a bundled copy of them.
shortTitle: WebParagraph
---

WebParagraph is an experimental implementation of Flutter's text layout system
(`ParagraphBuilder`, `Paragraph`, and related `dart:ui` APIs) for the web.
Instead of shipping a text stack inside the CanvasKit WebAssembly bundle,
WebParagraph delegates text shaping, measurement, and font fallback to
the browser through the [Enhanced TextMetrics API][enhanced-textmetrics].

:::experimental
WebParagraph is experimental and off by default.
It works only in Chromium-based browsers that expose the `TextCluster` API,
which is behind a flag.
Text layout and rendering details can change between Flutter releases.
Don't ship production apps with WebParagraph enabled.
:::

[enhanced-textmetrics]: https://chromestatus.com/feature/5075532483657728

## Why WebParagraph

Browsers already contain a complete text layout system,
but it isn't exposed to web apps in a way that Flutter can use.
As a result, every Flutter web app bundles a copy of
HarfBuzz, FreeType, a WOFF2 decoder, and font management code,
and downloads fallback fonts at runtime to cover glyphs
that the bundled fonts don't have.

WebParagraph removes that duplication:

- **Smaller download**: The `webparagraph` CanvasKit variant omits
SkParagraph, SkShaper, HarfBuzz, FreeType, and the WOFF2 decoder.
- **No fallback font downloads**: The browser resolves missing glyphs
from fonts already installed on the user's device,
so Flutter doesn't fetch fallback fonts from the network.
- **No tofu**: Characters that the bundled fonts don't cover
render with the browser's own fallback instead of as empty boxes.
- **Platform emoji**: Emoji render with the platform's emoji font,
such as Apple Color Emoji on macOS.

For the list of issues WebParagraph is designed to resolve,
visit the [WebParagraph umbrella issue][umbrella].

[umbrella]: {{site.repo.flutter}}/issues/172561

## Requirements

Flutter enables WebParagraph only when all of the following conditions are met.
If any condition isn't met, the `preferWebParagraph` flag has no effect
and Flutter uses its default text layout.

Flutter version
: Use Flutter 3.50 or later.

Renderer
: WebParagraph runs on the CanvasKit renderer, which is used by
JavaScript builds of your app.
The Skwasm renderer used by WebAssembly builds doesn't support WebParagraph.
If you build with `--wasm`, set `renderer: 'canvaskit'` in your
Flutter configuration so the loader picks the JavaScript build.
Comment on lines +60 to +65

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Setting renderer: 'canvaskit' forces the loader to use the CanvasKit renderer, but it does not force the loader to fall back to the JavaScript build (dart2js). WebAssembly builds (dart2wasm) can also run using the CanvasKit renderer.

To avoid confusing readers about how the loader selects the compilation target versus the renderer, we should clarify that this flag forces the CanvasKit renderer rather than picking the JavaScript build.

Suggested change
Renderer
: WebParagraph runs on the CanvasKit renderer, which is used by
JavaScript builds of your app.
The Skwasm renderer used by WebAssembly builds doesn't support WebParagraph.
If you build with `--wasm`, set `renderer: 'canvaskit'` in your
Flutter configuration so the loader picks the JavaScript build.
Renderer
: WebParagraph runs on the CanvasKit renderer.
The Skwasm renderer (the default for WebAssembly builds) doesn't support WebParagraph.
If you build with `--wasm`, set `renderer: 'canvaskit'` in your
Flutter configuration to use the CanvasKit renderer instead of Skwasm.


Browser
: A Chromium-based browser (Chrome, Edge, and others) with the
`TextCluster` API available.
In Chrome, the API is behind the
**Experimental Web Platform features** flag:
open `chrome://flags/#enable-experimental-web-platform-features`,
set it to **Enabled**, and relaunch the browser.
To check when Chrome enables the API by default, visit the
[Enhanced Canvas TextMetrics status page][enhanced-textmetrics].

To confirm the browser exposes the API,
run the following in the DevTools console:

```js
typeof TextCluster !== 'undefined'
```

## Enable WebParagraph

Enable WebParagraph by setting `preferWebParagraph: true` in your app's
[Flutter configuration][].
Create a custom `flutter_bootstrap.js` file in your app's `web/` directory:

```js title="web/flutter_bootstrap.js" highlightLines=6
{% raw %}{{flutter_js}}{% endraw %}
{% raw %}{{flutter_build_config}}{% endraw %}

_flutter.loader.load({
config: {
preferWebParagraph: true,
},
});
```

If you build with `--wasm`,
also force the CanvasKit renderer:

```js title="web/flutter_bootstrap.js" highlightLines=6-7
{% raw %}{{flutter_js}}{% endraw %}
{% raw %}{{flutter_build_config}}{% endraw %}

_flutter.loader.load({
config: {
renderer: 'canvaskit',
preferWebParagraph: true,
},
});
```

[Flutter configuration]: /platform-integration/web/initialization#customize-the-flutter-loader

### Run the app

`flutter run -d chrome` launches Chrome with a separate user profile,
so the flag you set in `chrome://flags` doesn't apply to it.
Pass the Chrome command-line switch through `flutter run` instead:

```console
$ flutter run -d chrome --web-browser-flag=--enable-experimental-web-platform-features
```

To test a release build, build the app and serve the output directory:

```console
$ flutter build web
```

Then open the app in a browser where you enabled the
**Experimental Web Platform features** flag.

### Verify that WebParagraph is active

When WebParagraph is active, the loader downloads the
`webparagraph` variant of CanvasKit instead of the regular one.
Open the **Network** panel in DevTools and filter by `canvaskit`.
The list shows requests for `canvaskit/webparagraph/canvaskit.js`
and `canvaskit/webparagraph/canvaskit.wasm`.

If you instead see `canvaskit/chromium/canvaskit.wasm` or
`canvaskit/canvaskit.wasm`, one of the [requirements](#requirements)
isn't met.

## Report issues

If text renders differently with WebParagraph than without it,
file an issue in the [Flutter repository][new-issue]
with a minimal reproduction and the Chrome version you used,
and mention WebParagraph in the title.
Follow the [umbrella issue][umbrella] for progress updates.

[new-issue]: {{site.repo.flutter}}/issues/new/choose
2 changes: 2 additions & 0 deletions sites/docs/src/data/sidenav/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,8 @@
permalink: /platform-integration/web/web-content-in-flutter
- title: Display images on the web
permalink: /platform-integration/web/web-images
- title: WebParagraph
permalink: /platform-integration/web/web-paragraph
- title: Web FAQ
permalink: /platform-integration/web/faq
- title: Windows
Expand Down
Loading