-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_basic_usage.dart
More file actions
58 lines (50 loc) · 1.85 KB
/
Copy path01_basic_usage.dart
File metadata and controls
58 lines (50 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import 'dart:io';
import 'package:glyph_path/glyph_path.dart';
const String _text = 'Hello Dart';
const double _fontSize = 48.0;
void main(List<String> args) {
if (args.isEmpty) {
stderr.writeln(
'Usage: dart run example/01_basic_usage.dart <font.ttf>\n'
' <font.ttf> Path to a TrueType/OpenType font file.\n'
' Free fonts: https://fonts.google.com (download *.ttf)\n'
' https://notofonts.github.io',
);
exit(1);
}
final File fontFile = File(args[0]);
if (!fontFile.existsSync()) {
stderr.writeln('Error: font not found at ${args[0]}');
exit(1);
}
final Font font = Font.parse(fontFile.readAsBytesSync());
// --- generateGlyphPaths: iterate PathCommand subtypes ---
final TextPathResult result = font.generateGlyphPaths(
_text,
fontSize: _fontSize,
);
final Map<String, int> counts = <String, int>{};
for (final PathCommand cmd in result.commands) {
final String type = switch (cmd) {
MoveTo() => 'MoveTo',
LineTo() => 'LineTo',
QuadTo() => 'QuadTo',
CubicTo() => 'CubicTo',
ClosePath() => 'ClosePath',
};
counts[type] = (counts[type] ?? 0) + 1;
}
stdout.writeln('generateGlyphPaths("$_text", fontSize: $_fontSize)');
stdout.writeln(' total commands : ${result.commands.length}');
for (final MapEntry<String, int> e in counts.entries) {
stdout.writeln(' ${e.key.padRight(10)}: ${e.value}');
}
stdout.writeln(' unitsPerEm : ${result.unitsPerEm}');
stdout.writeln(' ascender : ${result.ascender}');
stdout.writeln(' descender : ${result.descender}');
// --- measureText: advance width without generating paths ---
final TextMeasurement advance = font.measureText(_text, fontSize: _fontSize);
stdout.writeln(
'\nmeasureText("$_text", fontSize: $_fontSize) → ${advance.width}',
);
}