Skip to content

Commit

Permalink
Merge 10b4641 into 55624eb
Browse files Browse the repository at this point in the history
  • Loading branch information
dnfield committed Jan 19, 2020
2 parents 55624eb + 10b4641 commit ac7aea2
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 75 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -12,6 +12,7 @@ packages
.flutter-plugins
doc/api/
coverage/
.project

# Flutter crash logs
/flutter_*.log
8 changes: 8 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,13 @@
# CHANGES

## 0.16.0

- Move `transform` out of `DrawableStyle` and onto `DrawableStyleable`. Shapes
already worked this way, and the transform logic was handled in a confusingly
different way than all the other style attributes.
- Support `<use/>` elements having `id`s.
- Properly apply transforms to referenced use eleemnts.

## 0.15.0

- Respect transformations on `<image/>` tags.
Expand Down
59 changes: 59 additions & 0 deletions example/assets/simple/equation.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions example/assets/wikimedia/Flag_of_the_United_States.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added golden/simple/equation.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added golden/wikimedia/Flag_of_the_United_States.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion lib/src/avd_parser.dart
Expand Up @@ -70,10 +70,10 @@ Drawable parseAvdGroup(XmlElement el, Rect bounds) {
return DrawableGroup(
children,
DrawableStyle(
transform: transform?.storage,
stroke: stroke,
fill: fill,
groupOpacity: 1.0,
),
transform: transform?.storage,
);
}
60 changes: 41 additions & 19 deletions lib/src/svg/parser_state.dart
Expand Up @@ -64,12 +64,14 @@ class _TextInfo {
const _TextInfo(
this.style,
this.offset,
this.transform,
);
final DrawableStyle style;
final Offset offset;
final Matrix4 transform;

@override
String toString() => '$runtimeType{$offset, $style}';
String toString() => '$runtimeType{$offset, $style, $transform}';
}

class _Elements {
Expand Down Expand Up @@ -100,8 +102,8 @@ class _Elements {
parserState._definitions,
parserState.rootBounds,
parent.style,
needsTransform: true,
),
transform: parseTransform(parserState.attribute('transform'))?.storage,
);
if (!parserState._inDefs) {
parent.children.add(group);
Expand All @@ -119,8 +121,8 @@ class _Elements {
parserState._definitions,
null,
parent.style,
needsTransform: true,
),
transform: parseTransform(parserState.attribute('transform'))?.storage,
);
parserState.addGroup(parserState._currentStartElement, group);
return null;
Expand All @@ -139,18 +141,27 @@ class _Elements {
parserState.rootBounds,
parent.style,
);
final Matrix4 transform = Matrix4.identity()
..translate(
parseDouble(parserState.attribute('x', def: '0')),
parseDouble(parserState.attribute('y', def: '0')),
);

final Matrix4 transform =
parseTransform(parserState.attribute('transform')) ??
Matrix4.identity();
transform.translate(
parseDouble(parserState.attribute('x', def: '0')),
parseDouble(parserState.attribute('y', def: '0')),
);

final DrawableStyleable ref =
parserState._definitions.getDrawable('url($xlinkHref)');
final DrawableGroup group = DrawableGroup(
<Drawable>[ref.mergeStyle(style)],
DrawableStyle(transform: transform.storage),
style,
transform: transform.storage,
);
parent.children.add(group);

final bool isIri = parserState.checkForIri(group);
if (!parserState._inDefs || !isIri) {
parent.children.add(group);
}
return null;
}

Expand Down Expand Up @@ -483,13 +494,16 @@ class _Elements {
? transparentStroke
: lastTextInfo.style.stroke,
);
parserState.currentGroup.children.add(DrawableText(
fill,
stroke,
lastTextInfo.offset,
lastTextInfo.style.textStyle.anchor ?? DrawableTextAnchorPosition.start,
transform: lastTextInfo.style.transform,
));
parserState.currentGroup.children.add(
DrawableText(
fill,
stroke,
lastTextInfo.offset,
lastTextInfo.style.textStyle.anchor ??
DrawableTextAnchorPosition.start,
transform: lastTextInfo.transform?.storage,
),
);
lastTextWidth = fill.maxIntrinsicWidth;
}

Expand All @@ -502,16 +516,24 @@ class _Elements {
parserState,
lastTextInfo?.offset?.translate(lastTextWidth, 0),
);
Matrix4 transform = parseTransform(parserState.attribute('transform'));
if (lastTextInfo?.transform != null) {
if (transform == null) {
transform = lastTextInfo.transform;
} else {
transform = lastTextInfo.transform.multiplied(transform);
}
}

textInfos.add(_TextInfo(
parseStyle(
parserState.attributes,
parserState._definitions,
parserState.rootBounds,
lastTextInfo?.style ?? parserState.currentGroup.style,
needsTransform: true,
multiplyTransformByParent: lastTextInfo != null,
),
currentOffset,
transform,
));
if (event.isSelfClosing) {
textInfos.removeLast();
Expand Down
25 changes: 2 additions & 23 deletions lib/src/svg/xml_parsers.dart
@@ -1,4 +1,3 @@
import 'dart:typed_data';
import 'dart:ui';

import 'package:path_drawing/path_drawing.dart';
Expand Down Expand Up @@ -374,30 +373,10 @@ DrawableStyle parseStyle(
List<XmlElementAttribute> attributes,
DrawableDefinitionServer definitions,
Rect bounds,
DrawableStyle parentStyle, {
bool needsTransform = false,
bool multiplyTransformByParent = false,
}) {
Float64List rawTransform;
if (needsTransform) {
final Matrix4 transform = parseTransform(
getAttribute(attributes, 'transform'),
);
if (multiplyTransformByParent && parentStyle?.transform != null) {
if (transform == null) {
rawTransform = parentStyle.transform;
} else {
rawTransform = Matrix4.fromFloat64List(parentStyle.transform)
.multiplied(transform)
.storage;
}
} else {
rawTransform = transform?.storage;
}
}
DrawableStyle parentStyle,
) {
return DrawableStyle.mergeAndBlend(
parentStyle,
transform: rawTransform,
stroke: parseStroke(attributes, bounds, definitions, parentStyle?.stroke),
dashArray: parseDashArray(attributes),
dashOffset: parseDashOffset(attributes),
Expand Down

0 comments on commit ac7aea2

Please sign in to comment.