Visitar URL original
Cleanup and support web (#59) · flutter/packages@37d336c · GitHub
Skip to content

Commit 37d336c

Browse files
authored
Cleanup and support web (#59)
* Cleanup * delete ImageData * analyzer * jonah review
1 parent a00f990 commit 37d336c

13 files changed

Lines changed: 135 additions & 172 deletions

File tree

‎packages/vector_graphics/example/pubspec.yaml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,11 @@ dependency_overrides:
3333
path: ../../vector_graphics_codec
3434
vector_graphics:
3535
path: ../
36+
37+
platforms:
38+
android:
39+
ios:
40+
linux:
41+
macos:
42+
web:
43+
windows:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import 'dart:io';
2+
import 'dart:typed_data';
3+
4+
import 'package:flutter/foundation.dart';
5+
6+
/// Fetches an HTTP resource from the specified [uri] using the specified [headers].
7+
Future<Uint8List> httpGet(Uri uri, {Map<String, String>? headers}) async {
8+
final HttpClient httpClient = HttpClient();
9+
final HttpClientRequest request = await httpClient.getUrl(uri);
10+
headers?.forEach(request.headers.add);
11+
final HttpClientResponse response = await request.close();
12+
13+
if (response.statusCode != HttpStatus.ok) {
14+
// The network may be only temporarily unavailable, or the file will be
15+
// added on the server later. Avoid having future calls fail to check the
16+
// network again.
17+
await response.drain<List<int>>(<int>[]);
18+
throw HttpException('Could not get network asset', uri: uri);
19+
}
20+
return consolidateHttpClientResponseBytes(response);
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This lint is currently broken for packages that say they support web.
2+
// ignore: avoid_web_libraries_in_flutter
3+
import 'dart:html';
4+
import 'dart:typed_data';
5+
6+
/// Fetches an HTTP resource from the specified [url] using the specified [headers].
7+
Future<Uint8List> httpGet(Uri uri, {Map<String, String>? headers}) async {
8+
final HttpRequest request = await HttpRequest.request(
9+
uri.toString(),
10+
requestHeaders: headers,
11+
);
12+
request.responseType = 'arraybuffer';
13+
if (request.response is Uint8List) {
14+
return request.response as Uint8List;
15+
} else {
16+
return Uint8List.fromList(request.response as List<int>);
17+
}
18+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export '_http_io.dart' if (dart.library.html) '_http_web.dart';

‎packages/vector_graphics/lib/vector_graphics.dart‎

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import 'dart:io';
21
import 'dart:math' as math;
32
import 'dart:typed_data';
43
import 'dart:ui' as ui;
54

6-
import 'package:flutter/foundation.dart';
75
import 'package:flutter/widgets.dart';
86

97
import 'package:vector_graphics_codec/vector_graphics_codec.dart';
108

9+
import 'src/http.dart';
1110
import 'src/listener.dart';
1211

1312
/// A widget that displays a [VectorGraphicsCodec] encoded asset.
@@ -211,7 +210,6 @@ class NetworkBytesLoader extends BytesLoader {
211210
const NetworkBytesLoader(
212211
this.url, {
213212
this.headers,
214-
this.client,
215213
});
216214

217215
/// The HTTP headers to use for the network request.
@@ -220,36 +218,20 @@ class NetworkBytesLoader extends BytesLoader {
220218
/// The [Uri] of the resource to request.
221219
final Uri url;
222220

223-
/// The [HttpClient] to use when making a request. By default, this will
224-
/// create a new [HttpClient] per request.
225-
final HttpClient? client;
226-
227221
@override
228222
Future<ByteData> loadBytes(BuildContext context) async {
229-
final HttpClient currentClient = client ?? HttpClient();
230-
final HttpClientRequest request = await currentClient.getUrl(url);
231-
headers?.forEach(request.headers.add);
232-
233-
final HttpClientResponse response = await request.close();
234-
if (response.statusCode != HttpStatus.ok) {
235-
await response.drain<List<int>>(<int>[]);
236-
throw Exception('Failed to load VectorGraphic: ${response.statusCode}');
237-
}
238-
final Uint8List bytes = await consolidateHttpClientResponseBytes(
239-
response,
240-
);
223+
final Uint8List bytes = await httpGet(url, headers: headers);
241224
return bytes.buffer.asByteData();
242225
}
243226

244227
@override
245-
int get hashCode => Object.hash(url, headers, client);
228+
int get hashCode => Object.hash(url, headers);
246229

247230
@override
248231
bool operator ==(Object other) {
249232
return other is NetworkBytesLoader &&
250233
other.headers == headers &&
251-
other.url == url &&
252-
other.client == client;
234+
other.url == url;
253235
}
254236
}
255237

‎packages/vector_graphics/pubspec.yaml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,11 @@ dev_dependencies:
2121
dependency_overrides:
2222
vector_graphics_codec:
2323
path: ../vector_graphics_codec
24+
25+
platforms:
26+
android:
27+
ios:
28+
linux:
29+
macos:
30+
web:
31+
windows:

‎packages/vector_graphics_codec/lib/vector_graphics_codec.dart‎

Lines changed: 51 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,8 @@ class VectorGraphicsCodec {
168168
Uint16List? indices,
169169
int? paintId,
170170
) {
171-
if (buffer._decodePhase.index > _CurrentSection.commands.index) {
172-
throw StateError('Commands must be encoded together.');
173-
}
174-
buffer._decodePhase = _CurrentSection.commands;
171+
buffer._checkPhase(_CurrentSection.commands);
172+
175173
// Type Tag
176174
// Vertex Length
177175
// Vertex Buffer
@@ -206,10 +204,8 @@ class VectorGraphicsCodec {
206204
int blendMode, [
207205
int? shaderId,
208206
]) {
209-
if (buffer._decodePhase.index > _CurrentSection.paints.index) {
210-
throw StateError('Paints must be encoded together.');
211-
}
212-
buffer._decodePhase = _CurrentSection.paints;
207+
buffer._checkPhase(_CurrentSection.paints);
208+
213209
final int paintId = buffer._nextPaintId++;
214210
assert(paintId < kMaxId);
215211
buffer._putUint8(_fillPaintTag);
@@ -231,10 +227,8 @@ class VectorGraphicsCodec {
231227
required Float32List? offsets,
232228
required int tileMode,
233229
}) {
234-
if (buffer._decodePhase.index > _CurrentSection.shaders.index) {
235-
throw StateError('Shaders must be encoded together.');
236-
}
237-
buffer._decodePhase = _CurrentSection.shaders;
230+
buffer._checkPhase(_CurrentSection.shaders);
231+
238232
final int shaderId = buffer._nextShaderId++;
239233
assert(shaderId < kMaxId);
240234
buffer._putUint8(_linearGradientTag);
@@ -273,10 +267,8 @@ class VectorGraphicsCodec {
273267
assert((focalX == null && focalY == null) ||
274268
(focalX != null && focalY != null));
275269
assert(transform == null || transform.length == 16);
276-
if (buffer._decodePhase.index > _CurrentSection.shaders.index) {
277-
throw StateError('Shaders must be encoded together.');
278-
}
279-
buffer._decodePhase = _CurrentSection.shaders;
270+
buffer._checkPhase(_CurrentSection.shaders);
271+
280272
final int shaderId = buffer._nextShaderId++;
281273
assert(shaderId < kMaxId);
282274
buffer._putUint8(_radialGradientTag);
@@ -300,12 +292,7 @@ class VectorGraphicsCodec {
300292
} else {
301293
buffer._putUint16(0);
302294
}
303-
if (transform != null) {
304-
buffer._putUint8(transform.length);
305-
buffer._putFloat64List(transform);
306-
} else {
307-
buffer._putUint8(0);
308-
}
295+
buffer._writeTransform(transform);
309296
buffer._putUint8(tileMode);
310297
return shaderId;
311298
}
@@ -329,10 +316,7 @@ class VectorGraphicsCodec {
329316
double strokeWidth, [
330317
int? shaderId,
331318
]) {
332-
if (buffer._decodePhase.index > _CurrentSection.paints.index) {
333-
throw StateError('Paints must be encoded together.');
334-
}
335-
buffer._decodePhase = _CurrentSection.paints;
319+
buffer._checkPhase(_CurrentSection.paints);
336320
final int paintId = buffer._nextPaintId++;
337321
assert(paintId < kMaxId);
338322
buffer._putUint8(_strokePaintTag);
@@ -392,9 +376,7 @@ class VectorGraphicsCodec {
392376
final Int32List colors = buffer.getInt32List(colorsLength);
393377
final int offsetsLength = buffer.getUint16();
394378
final Float32List offsets = buffer.getFloat32List(offsetsLength);
395-
final int transformLength = buffer.getUint8();
396-
final Float64List? transform =
397-
transformLength != 0 ? buffer.getFloat64List(transformLength) : null;
379+
final Float64List? transform = buffer.getTransform();
398380
final int tileMode = buffer.getUint8();
399381
listener?.onRadialGradient(
400382
centerX,
@@ -464,10 +446,8 @@ class VectorGraphicsCodec {
464446
if (buffer._currentPathId != -1) {
465447
throw StateError('There is already an active Path.');
466448
}
467-
if (buffer._decodePhase.index > _CurrentSection.paths.index) {
468-
throw StateError('Paths must be encoded together');
469-
}
470-
buffer._decodePhase = _CurrentSection.paths;
449+
buffer._checkPhase(_CurrentSection.paths);
450+
471451
buffer._currentPathId = buffer._nextPathId++;
472452
assert(buffer._nextPathId < kMaxId);
473453
buffer._putUint8(_pathTag);
@@ -573,10 +553,8 @@ class VectorGraphicsCodec {
573553
required double fontSize,
574554
required Float64List? transform,
575555
}) {
576-
if (buffer._decodePhase.index > _CurrentSection.text.index) {
577-
throw StateError('Text config must be encoded together.');
578-
}
579-
buffer._decodePhase = _CurrentSection.text;
556+
buffer._checkPhase(_CurrentSection.text);
557+
580558
final int textId = buffer._nextTextId++;
581559
assert(textId < kMaxId);
582560

@@ -596,12 +574,7 @@ class VectorGraphicsCodec {
596574
buffer._putUint16(0);
597575
}
598576

599-
if (transform != null) {
600-
buffer._putUint8(transform.length);
601-
buffer._putFloat64List(transform);
602-
} else {
603-
buffer._putUint8(0);
604-
}
577+
buffer._writeTransform(transform);
605578

606579
// text-value
607580
final Uint8List encoded = utf8.encode(text) as Uint8List;
@@ -663,14 +636,18 @@ class VectorGraphicsCodec {
663636
}
664637

665638
void _readDrawPath(
666-
_ReadBuffer buffer, VectorGraphicsCodecListener? listener) {
639+
_ReadBuffer buffer,
640+
VectorGraphicsCodecListener? listener,
641+
) {
667642
final int pathId = buffer.getUint16();
668643
final int paintId = buffer.getUint16();
669644
listener?.onDrawPath(pathId, paintId);
670645
}
671646

672647
void _readDrawVertices(
673-
_ReadBuffer buffer, VectorGraphicsCodecListener? listener) {
648+
_ReadBuffer buffer,
649+
VectorGraphicsCodecListener? listener,
650+
) {
674651
final int paintId = buffer.getUint16();
675652
final int verticesLength = buffer.getUint16();
676653
final Float32List vertices = buffer.getFloat32List(verticesLength);
@@ -719,12 +696,7 @@ class VectorGraphicsCodec {
719696
if (fontFamilyLength > 0) {
720697
fontFamily = utf8.decode(buffer.getUint8List(fontFamilyLength));
721698
}
722-
final int transformLength = buffer.getUint8();
723-
Float64List? transform;
724-
if (transformLength > 0) {
725-
assert(transformLength == 16);
726-
transform = buffer.getFloat64List(transformLength);
727-
}
699+
Float64List? transform = buffer.getTransform();
728700
final int textLength = buffer.getUint16();
729701
final String text = utf8.decode(buffer.getUint8List(textLength));
730702

@@ -800,11 +772,7 @@ abstract class VectorGraphicsCodecListener {
800772
/// [indices].
801773
///
802774
/// If the [paintId] is `null`, a default empty paint should be used instead.
803-
void onDrawVertices(
804-
Float32List vertices,
805-
Uint16List? indices,
806-
int? paintId,
807-
);
775+
void onDrawVertices(Float32List vertices, Uint16List? indices, int? paintId);
808776

809777
/// Save a new layer with the given [paintId].
810778
void onSaveLayer(int paintId);
@@ -920,6 +888,24 @@ class VectorGraphicsBuffer {
920888
/// enum order.
921889
_CurrentSection _decodePhase = _CurrentSection.size;
922890

891+
void _checkPhase(_CurrentSection expected) {
892+
if (_decodePhase.index > expected.index) {
893+
final String name = expected.name;
894+
throw StateError('${name[0].toUpperCase()}${name.substring(1)} '
895+
'must be encoded together (current phase is ${_decodePhase.name}).');
896+
}
897+
_decodePhase = expected;
898+
}
899+
900+
void _writeTransform(Float64List? transform) {
901+
if (transform != null) {
902+
_putUint8(transform.length);
903+
_putFloat64List(transform);
904+
} else {
905+
_putUint8(0);
906+
}
907+
}
908+
923909
/// Write a Uint8 into the buffer.
924910
void _putUint8(int byte) {
925911
assert(!_isDone);
@@ -1124,4 +1110,13 @@ class _ReadBuffer {
11241110
final int mod = _position % alignment;
11251111
if (mod != 0) _position += alignment - mod;
11261112
}
1113+
1114+
Float64List? getTransform() {
1115+
final int transformLength = getUint8();
1116+
if (transformLength > 0) {
1117+
assert(transformLength == 16);
1118+
return getFloat64List(transformLength);
1119+
}
1120+
return null;
1121+
}
11271122
}

‎packages/vector_graphics_codec/pubspec.yaml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ environment:
99
dev_dependencies:
1010
flutter_lints: ^1.0.0
1111
test: ^1.20.1
12+
13+
platforms:
14+
android:
15+
ios:
16+
linux:
17+
macos:
18+
web:
19+
windows:

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class DrawCommandBuilder {
77
final Map<Paint, int> _paints = <Paint, int>{};
88
final Map<Path, int> _paths = <Path, int>{};
99
final Map<TextConfig, int> _text = <TextConfig, int>{};
10+
1011
final List<DrawCommand> _commands = <DrawCommand>[];
1112

1213
int _getOrGenerateId<T>(T object, Map<T, int> map) =>

0 commit comments

Comments
 (0)