Visitar URL original
encode text transforms (#50) · flutter/packages@8d9dce1 · GitHub
Skip to content

Commit 8d9dce1

Browse files
authored
encode text transforms (#50)
1 parent 7869abc commit 8d9dce1

7 files changed

Lines changed: 151 additions & 12 deletions

File tree

‎packages/vector_graphics/lib/src/listener.dart‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,18 @@ class FlutterVectorGraphicsListener extends VectorGraphicsCodecListener {
329329
double y,
330330
int fontWeight,
331331
double fontSize,
332+
Float64List? transform,
332333
int id,
333334
) {
334335
_textConfig.add(_TextConfig(
335-
text, fontFamily, x, y, ui.FontWeight.values[fontWeight], fontSize));
336+
text,
337+
fontFamily,
338+
x,
339+
y,
340+
ui.FontWeight.values[fontWeight],
341+
fontSize,
342+
transform,
343+
));
336344
}
337345

338346
@override
@@ -357,8 +365,18 @@ class FlutterVectorGraphicsListener extends VectorGraphicsCodecListener {
357365
paragraph.layout(const ui.ParagraphConstraints(
358366
width: double.infinity,
359367
));
360-
_canvas.drawParagraph(paragraph,
361-
ui.Offset(textConfig.dx, textConfig.dy - paragraph.alphabeticBaseline));
368+
369+
if (textConfig.transform != null) {
370+
_canvas.save();
371+
_canvas.transform(textConfig.transform!);
372+
}
373+
_canvas.drawParagraph(
374+
paragraph,
375+
ui.Offset(textConfig.dx, textConfig.dy - paragraph.alphabeticBaseline),
376+
);
377+
if (textConfig.transform != null) {
378+
_canvas.restore();
379+
}
362380
}
363381
}
364382

@@ -370,6 +388,7 @@ class _TextConfig {
370388
this.dy,
371389
this.fontWeight,
372390
this.fontSize,
391+
this.transform,
373392
);
374393

375394
final String text;
@@ -378,4 +397,5 @@ class _TextConfig {
378397
final double dx;
379398
final double dy;
380399
final ui.FontWeight fontWeight;
400+
final Float64List? transform;
381401
}

‎packages/vector_graphics_codec/lib/vector_graphics_codec.dart‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ class VectorGraphicsCodec {
571571
required double y,
572572
required int fontWeight,
573573
required double fontSize,
574+
required Float64List? transform,
574575
}) {
575576
if (buffer._decodePhase.index > _CurrentSection.text.index) {
576577
throw StateError('Text config must be encoded together.');
@@ -595,6 +596,13 @@ class VectorGraphicsCodec {
595596
buffer._putUint16(0);
596597
}
597598

599+
if (transform != null) {
600+
buffer._putUint8(transform.length);
601+
buffer._putFloat64List(transform);
602+
} else {
603+
buffer._putUint8(0);
604+
}
605+
598606
// text-value
599607
final Uint8List encoded = utf8.encode(text) as Uint8List;
600608
buffer._putUint16(encoded.length);
@@ -711,10 +719,17 @@ class VectorGraphicsCodec {
711719
if (fontFamilyLength > 0) {
712720
fontFamily = utf8.decode(buffer.getUint8List(fontFamilyLength));
713721
}
722+
final int transformLength = buffer.getUint8();
723+
Float64List? transform;
724+
if (transformLength > 0) {
725+
assert(transformLength == 16);
726+
transform = buffer.getFloat64List(transformLength);
727+
}
714728
final int textLength = buffer.getUint16();
715729
final String text = utf8.decode(buffer.getUint8List(textLength));
716730

717-
listener?.onTextConfig(text, fontFamily, dx, dy, fontWeight, fontSize, id);
731+
listener?.onTextConfig(
732+
text, fontFamily, dx, dy, fontWeight, fontSize, transform, id);
718733
}
719734

720735
void _readDrawText(
@@ -839,6 +854,7 @@ abstract class VectorGraphicsCodecListener {
839854
double y,
840855
int fontWeight,
841856
double fontSize,
857+
Float64List? transform,
842858
int id,
843859
);
844860

‎packages/vector_graphics_codec/test/vector_graphics_codec_test.dart‎

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ void main() {
451451
y: 12,
452452
fontWeight: 0,
453453
fontSize: 16,
454+
transform: null,
454455
);
455456
codec.writeDrawText(buffer, textId, paintId);
456457
codec.decode(buffer.done(), listener);
@@ -474,6 +475,53 @@ void main() {
474475
16,
475476
'Roboto',
476477
0,
478+
null,
479+
textId,
480+
),
481+
OnDrawText(textId, paintId),
482+
]);
483+
});
484+
485+
test('Encodes text with transform', () {
486+
final buffer = VectorGraphicsBuffer();
487+
final TestListener listener = TestListener();
488+
489+
final Float64List transform = Float64List(16);
490+
transform[15] = 10;
491+
final int paintId = codec.writeFill(buffer, 0xFFAABBAA, 0);
492+
final int textId = codec.writeTextConfig(
493+
buffer: buffer,
494+
text: 'Hello',
495+
fontFamily: 'Roboto',
496+
x: 10,
497+
y: 12,
498+
fontWeight: 0,
499+
fontSize: 16,
500+
transform: transform,
501+
);
502+
codec.writeDrawText(buffer, textId, paintId);
503+
codec.decode(buffer.done(), listener);
504+
505+
expect(listener.commands, [
506+
OnPaintObject(
507+
color: 0xFFAABBAA,
508+
strokeCap: null,
509+
strokeJoin: null,
510+
blendMode: 0,
511+
strokeMiterLimit: null,
512+
strokeWidth: null,
513+
paintStyle: 0,
514+
id: paintId,
515+
shaderId: null,
516+
),
517+
OnTextConfig(
518+
'Hello',
519+
10,
520+
12,
521+
16,
522+
'Roboto',
523+
0,
524+
transform,
477525
textId,
478526
),
479527
OnDrawText(textId, paintId),
@@ -493,6 +541,7 @@ void main() {
493541
y: 12,
494542
fontWeight: 0,
495543
fontSize: 16,
544+
transform: null,
496545
);
497546
codec.writeDrawText(buffer, textId, paintId);
498547
codec.decode(buffer.done(), listener);
@@ -516,6 +565,7 @@ void main() {
516565
16,
517566
null,
518567
0,
568+
null,
519569
textId,
520570
),
521571
OnDrawText(textId, paintId),
@@ -535,6 +585,7 @@ void main() {
535585
y: 12,
536586
fontWeight: 0,
537587
fontSize: 16,
588+
transform: null,
538589
);
539590
codec.writeDrawText(buffer, textId, paintId);
540591
codec.decode(buffer.done(), listener);
@@ -558,6 +609,7 @@ void main() {
558609
16,
559610
null,
560611
0,
612+
null,
561613
textId,
562614
),
563615
OnDrawText(textId, paintId),
@@ -721,6 +773,7 @@ class TestListener extends VectorGraphicsCodecListener {
721773
double dy,
722774
int fontWeight,
723775
double fontSize,
776+
Float64List? transform,
724777
int id,
725778
) {
726779
commands.add(OnTextConfig(
@@ -730,6 +783,7 @@ class TestListener extends VectorGraphicsCodecListener {
730783
fontSize,
731784
fontFamily,
732785
fontWeight,
786+
transform,
733787
id,
734788
));
735789
}
@@ -1102,6 +1156,7 @@ class OnTextConfig {
11021156
this.fontSize,
11031157
this.fontFamily,
11041158
this.fontWeight,
1159+
this.transform,
11051160
this.id,
11061161
);
11071162

@@ -1112,10 +1167,11 @@ class OnTextConfig {
11121167
final String? fontFamily;
11131168
final int fontWeight;
11141169
final int id;
1170+
final Float64List? transform;
11151171

11161172
@override
1117-
int get hashCode =>
1118-
Object.hash(text, x, y, fontSize, fontFamily, fontWeight, id);
1173+
int get hashCode => Object.hash(text, x, y, fontSize, fontFamily, fontWeight,
1174+
Object.hashAll(transform ?? Float64List(0)), id);
11191175

11201176
@override
11211177
bool operator ==(Object other) =>
@@ -1126,11 +1182,12 @@ class OnTextConfig {
11261182
other.fontSize == fontSize &&
11271183
other.fontFamily == fontFamily &&
11281184
other.fontWeight == fontWeight &&
1185+
_listEquals(other.transform, transform) &&
11291186
other.id == id;
11301187

11311188
@override
11321189
String toString() =>
1133-
'OnTextConfig($text, $x, $y, $fontSize, $fontFamily, $fontWeight, $id)';
1190+
'OnTextConfig($text, $x, $y, $fontSize, $fontFamily, $fontWeight, $transform, $id)';
11341191
}
11351192

11361193
class OnDrawText {

‎packages/vector_graphics_compiler/lib/src/paint.dart‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,26 @@ class TextConfig {
12731273
///
12741274
/// If `null` this implies no transform.
12751275
final AffineMatrix? transform;
1276+
1277+
@override
1278+
int get hashCode => Object.hash(
1279+
text, baselineStart, fontSize, fontFamily, fontWeight, transform);
1280+
1281+
@override
1282+
bool operator ==(Object other) {
1283+
return other is TextConfig &&
1284+
other.text == text &&
1285+
other.baselineStart == baselineStart &&
1286+
other.fontSize == fontSize &&
1287+
other.fontFamily == fontFamily &&
1288+
other.fontWeight == fontWeight &&
1289+
other.transform == transform;
1290+
}
1291+
1292+
@override
1293+
String toString() {
1294+
return 'TextConfig($text, $baselineStart, $fontSize, $fontFamily, $fontWeight, $transform)';
1295+
}
12761296
}
12771297

12781298
/// The value of the font weight.

‎packages/vector_graphics_compiler/lib/vector_graphics_compiler.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ Future<Uint8List> encodeSvg(String input, String filename) async {
167167
y: textConfig.baselineStart.y,
168168
fontWeight: textConfig.fontWeight.index,
169169
fontSize: textConfig.fontSize,
170+
transform: _encodeMatrix(textConfig.transform),
170171
);
171172
}
172173

‎packages/vector_graphics_compiler/test/end_to_end_test.dart‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,15 @@ void main() {
178178
id: 3,
179179
shaderId: null,
180180
),
181-
const OnTextConfig('Plain text Roboto', 100, 50, 55, 'Roboto', 3, 0),
182-
const OnTextConfig('Plain text Verdana', 100, 100, 55, 'Verdana', 3, 1),
183-
const OnTextConfig('Bold text Verdana', 100, 150, 55, 'Verdana', 6, 2),
184-
const OnTextConfig('Stroked bold line', 150, 215, 55, 'Roboto', 8, 3),
185-
const OnTextConfig('Line 3', 150, 50, 55, 'Roboto', 3, 4),
181+
const OnTextConfig(
182+
'Plain text Roboto', 100, 50, 55, 'Roboto', 3, null, 0),
183+
const OnTextConfig(
184+
'Plain text Verdana', 100, 100, 55, 'Verdana', 3, null, 1),
185+
const OnTextConfig(
186+
'Bold text Verdana', 100, 150, 55, 'Verdana', 6, null, 2),
187+
const OnTextConfig(
188+
'Stroked bold line', 150, 215, 55, 'Roboto', 8, null, 3),
189+
const OnTextConfig('Line 3', 150, 50, 55, 'Roboto', 3, null, 4),
186190
const OnDrawText(0, 0),
187191
const OnDrawText(1, 0),
188192
const OnDrawText(2, 0),

‎packages/vector_graphics_compiler/test/parser_test.dart‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,27 @@ import 'package:vector_graphics_compiler/vector_graphics_compiler.dart';
66
import 'test_svg_strings.dart';
77

88
void main() {
9+
test('text with transform', () async {
10+
final VectorInstructions instructions = await parse(
11+
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 160"><text transform="rotate(10 -100 50)">a</text></svg>',
12+
);
13+
expect(instructions.paints.single, const Paint(fill: Fill()));
14+
expect(
15+
instructions.text.single,
16+
TextConfig(
17+
'a',
18+
Point.zero,
19+
null,
20+
normalFontWeight,
21+
16,
22+
AffineMatrix.identity
23+
.translated(-100, 50)
24+
.rotated(radians(10))
25+
.translated(100, -50),
26+
),
27+
);
28+
});
29+
930
test('Missing references', () async {
1031
final VectorInstructions instructions = await parse(missingRefs);
1132
expect(

0 commit comments

Comments
 (0)