fromHtml static method
Implementation
static List<ImageMapRegion> fromHtml(
String data, [
Color? color,
]) {
final regions = <ImageMapRegion>[];
final areaMatches = RegExp(
r'<area .+>',
caseSensitive: false,
multiLine: true,
).allMatches(data);
final shapeRegex = RegExp(
r'shape="([^"]+)"',
caseSensitive: false,
);
final coordsRegex = RegExp(
r'coords="([^"]+)"',
caseSensitive: false,
);
final titleRegex = RegExp(
r'title="([^"]+)"',
caseSensitive: false,
);
final linkRegex = RegExp(
r'href="([^"]+)"',
caseSensitive: false,
);
final unescape = HtmlUnescape();
for (final areaMatch in areaMatches) {
final areaValue = areaMatch.group(0);
if (areaValue == null) {
continue;
}
final shapeMatch = shapeRegex.firstMatch(areaValue);
final coordsMatch = coordsRegex.firstMatch(areaValue);
if (shapeMatch == null || coordsMatch == null) {
continue;
}
final titleMatch = titleRegex.firstMatch(areaValue);
final titleText = titleMatch?.group(1);
final title = titleText != null ? unescape.convert(titleText) : null;
final linkMatch = linkRegex.firstMatch(areaValue);
final link = linkMatch?.group(1);
final shape = getImageMapShapeFromString(shapeMatch.group(1)!);
final coords = coordsMatch
.group(1)!
.split(',')
.map(double.parse)
.toList(growable: false);
switch (shape) {
case ImageMapShape.rect:
regions.add(
ImageMapRegion.fromRect(
rect: Rect.fromLTRB(
coords[0],
coords[1],
coords[2],
coords[3],
),
color: color,
title: title,
link: link,
),
);
break;
case ImageMapShape.poly:
final offsets = <Offset>[];
for (var i = 0; i < coords.length; i += 2) {
final x = coords[i];
final y = coords[i + 1];
offsets.add(Offset(x, y));
}
regions.add(
ImageMapRegion.fromPoly(
points: offsets,
color: color,
title: title,
link: link,
),
);
break;
case ImageMapShape.circle:
regions.add(
ImageMapRegion.fromCircle(
center: Offset(
coords[0],
coords[1],
),
radius: coords[2],
color: color,
title: title,
link: link,
),
);
break;
}
}
return regions;
}