Fonts and image assets
A document refers to images and fonts by name. Your app supplies the bytes.
dart
import 'package:flutter/material.dart';
import 'package:vyuh_document_editor/vyuh_document_editor.dart';
final class AppImageAssets implements ImageAssetResolver {
AppImageAssets(Map<String, ImageProvider> images)
: _images = Map.unmodifiable(images);
final Map<String, ImageProvider> _images;
@override
ImageProvider resolve(String assetId) {
final image = _images[assetId];
if (image == null) throw StateError('Missing image asset: $assetId');
return image;
}
}
Widget buildResourceBoundEditor(
DocumentEditorController controller,
DocumentFontLoader fonts,
ImageAssetResolver images,
) => ListenableBuilder(
listenable: fonts,
builder: (_, _) => DocumentEditor(
controller: controller,
assetResolver: images,
layoutRevision: fonts.layoutRevision,
configuration: DocumentEditorConfiguration(
resources: DocumentEditorResources(fontLoader: fonts),
),
),
);
// The host owns and disposes the font loader. Register source fonts through
// its resolver; reuse stable providers for image IDs. JSON contains IDs,
// not the image bytes or font files.Images
ImageAssetResolver maps an asset ID to an image provider. Fetching, authentication and storage are yours. Reuse providers across rebuilds and keep asset IDs when saving.
Fonts
DocumentFontLoader loads font faces from bundled assets or your resolver. Pass it in DocumentEditorResources, keep it for the session, and pass its layoutRevision to the editor so new fonts re-lay out the text.
- Fonts change line breaks and caret positions, so show a load failure rather than relying on a fallback.
- Retry a failed font with
loadRequiredDocumentFonts(document). - Map missing fonts to bundled ones with
withBundledDocxFontSubstitutions(the playground maps Aptos and Calibri to Carlito). The document keeps the original family name.
PDF
ResolvedPdfPainter fetches the same font and image bytes. Each request waits up to 30 seconds (fontResourceTimeout, imageResourceTimeout). Supply the exact bold and italic faces; the PDF can't synthesize them.