Visitar URL original
Fix text positioning when transform but no x/y or dx/dy (#201) · flutter/packages@3080b52 · GitHub
Skip to content

Commit 3080b52

Browse files
authored
Fix text positioning when transform but no x/y or dx/dy (#201)
* Fix text positioning when transform but no x/y or dx/dy * format * avoid encoding identity
1 parent a0a25cd commit 3080b52

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎packages/vector_graphics_compiler/lib/src/svg/node.dart‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,20 +250,26 @@ class TextPositionNode extends ParentNode {
250250
/// Computes a [TextPosition] to encode for this node.
251251
TextPosition computeTextPosition(Rect bounds, AffineMatrix transform) {
252252
final AffineMatrix computedTransform = concatTransform(transform);
253-
final bool consumeTransform = computedTransform.encodableInRect;
253+
254254
double? x = attributes.x?.calculate(bounds.width);
255255
double? y = attributes.y?.calculate(bounds.height);
256256
double? dx = attributes.dx?.calculate(bounds.width);
257257
double? dy = attributes.dy?.calculate(bounds.height);
258-
if (x != null && y != null) {
258+
259+
final bool hasXY = x != null && y != null;
260+
final bool hasDxDy = dx != null && dy != null;
261+
final bool consumeTransform = computedTransform == AffineMatrix.identity ||
262+
(computedTransform.encodableInRect && (hasXY || hasDxDy));
263+
264+
if (hasXY) {
259265
final Point baseline = consumeTransform
260266
? transform.transformPoint(Point(x, y))
261267
: Point(x, y);
262268
x = baseline.x;
263269
y = baseline.y;
264270
}
265271

266-
if (dx != null && dy != null) {
272+
if (hasDxDy) {
267273
final Point baseline = consumeTransform
268274
? transform.transformPoint(Point(dx, dy))
269275
: Point(dx, dy);

‎packages/vector_graphics_compiler/test/parser_test.dart‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,47 @@ import 'package:vector_graphics_compiler/vector_graphics_compiler.dart';
44
import 'test_svg_strings.dart';
55

66
void main() {
7+
test('Text transform but no xy', () {
8+
final VectorInstructions instructions = parseWithoutOptimizers('''
9+
<svg viewBox="0 0 450 150" xmlns="http://www.w3.org/2000/svg">
10+
<g fill="red" font-family="Arimo,Liberation Sans,HammersmithOne,Helvetica,Arial,sans-serif"
11+
font-weight="600">
12+
<text font-size="40" transform="translate(60 45)">東急電鉄路線図</text>
13+
<text font-size="22" transform="translate(60 75)">Tōkyū Railways route map</text>
14+
</g>
15+
</svg>
16+
''');
17+
18+
expect(instructions.text, const <TextConfig>[
19+
TextConfig(
20+
'東急電鉄路線図',
21+
0.0,
22+
'Arimo,Liberation Sans,HammersmithOne,Helvetica,Arial,sans-serif',
23+
FontWeight.w600,
24+
40.0,
25+
TextDecoration.none,
26+
TextDecorationStyle.solid,
27+
Color(0xff000000),
28+
),
29+
TextConfig(
30+
'Tōkyū Railways route map',
31+
0.0,
32+
'Arimo,Liberation Sans,HammersmithOne,Helvetica,Arial,sans-serif',
33+
FontWeight.w600,
34+
22.0,
35+
TextDecoration.none,
36+
TextDecorationStyle.solid,
37+
Color(0xff000000),
38+
),
39+
]);
40+
expect(instructions.textPositions, <TextPosition>[
41+
TextPosition(
42+
reset: true, transform: AffineMatrix.identity.translated(60, 45)),
43+
TextPosition(
44+
reset: true, transform: AffineMatrix.identity.translated(60, 75)),
45+
]);
46+
});
47+
748
test('Fill rule inheritence', () {
849
final VectorInstructions instructions =
950
parseWithoutOptimizers(inheritFillRule);

0 commit comments

Comments
 (0)