vyuh_diagram_codec_svg 3.5.1
vyuh_diagram_codec_svg: ^3.5.1 copied to clipboard
Export Vyuh Diagram drawings as SVG and import SVG paths and primitives as editable shapes. Pure Dart, headless on servers or inside Flutter.
example/main.dart
import 'package:vyuh_diagram_codec_svg/input.dart';
import 'package:vyuh_diagram_codec_svg/vyuh_diagram_codec_svg.dart';
import 'package:vyuh_diagram_core/vyuh_diagram_core.dart';
/// Imports SVG shapes into a diagram, then exports the diagram as SVG.
///
/// Runs on the Dart VM. In Flutter, mount a `DiagramCanvas` before exporting
/// text so the canvas supplies text layout.
Future<void> main() async {
const source = '''
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 100">
<rect x="10" y="10" width="80" height="60" fill="#d9f99d" stroke="#365314"/>
<circle cx="150" cy="40" r="30" fill="#bfdbfe"/>
</svg>''';
final imported = const DiagramSvgInput().decode(source);
final controller = DiagramController(
configuration: DiagramConfiguration(extensions: [SvgExtension()]),
);
try {
// One batch is one undo step.
controller.edits.batch('Import SVG', () {
for (final element in imported.elements) {
controller.edits.add(element, select: false).requireAllowed();
}
}).requireAllowed();
final svg = await controller.exportSvg(
SvgOptions(bounds: controller.scene.bounds),
);
// ignore: avoid_print
print(
'${imported.elements.length} shapes, ${imported.warnings.length} '
'warnings; exported ${svg.svg.length} characters',
);
} finally {
controller.dispose();
}
}