From 42a17c3a3df2ee8cd05f998ea7249cf2832540fe Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 23 Jul 2024 23:11:46 +0200 Subject: [PATCH] Added support for SVG data URI images --- .../java/eu/faircode/email/ImageHelper.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/app/src/main/java/eu/faircode/email/ImageHelper.java b/app/src/main/java/eu/faircode/email/ImageHelper.java index 02621460bc..4f5a9b7f46 100644 --- a/app/src/main/java/eu/faircode/email/ImageHelper.java +++ b/app/src/main/java/eu/faircode/email/ImageHelper.java @@ -70,6 +70,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; @@ -662,6 +663,27 @@ class ImageHelper { if (comma < 0) throw new IllegalArgumentException("Comma missing"); + int colon = source.indexOf(':'); + int semi = source.indexOf(';'); + String type = null; + if (colon > 0 && semi > colon) + type = source.substring(colon + 1, semi); + else if (colon > 0 && comma > colon) + type = source.substring(colon + 1, comma); + String enc = (semi > 0 && comma > semi ? source.substring(semi + 1, comma) : null); + + if ("image/svg".equalsIgnoreCase(type) && + (enc == null || "utf8".equalsIgnoreCase(enc))) + try { + InputStream is = new ByteArrayInputStream(source.substring(comma + 1).getBytes(StandardCharsets.UTF_8)); + Bitmap bm = ImageHelper.renderSvg(is, Color.WHITE, 1024); + Helper.ByteArrayInOutStream s = new Helper.ByteArrayInOutStream(); + bm.compress(Bitmap.CompressFormat.PNG, 100, s); + return s.getInputStream(); + } catch (IOException ex) { + throw new IllegalArgumentException("SVG", ex); + } + String base64 = source.substring(comma + 1); byte[] bytes = Base64.decode(base64.getBytes(), 0); return new ByteArrayInputStream(bytes);