From e252b1bcd55a9683c1c903df12490118527b379a Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 24 Jul 2024 07:09:58 +0200 Subject: [PATCH] SVG data URI improvements --- .../java/eu/faircode/email/ImageHelper.java | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ImageHelper.java b/app/src/main/java/eu/faircode/email/ImageHelper.java index 4f5a9b7f46..f1fa6bd0ad 100644 --- a/app/src/main/java/eu/faircode/email/ImageHelper.java +++ b/app/src/main/java/eu/faircode/email/ImageHelper.java @@ -657,37 +657,43 @@ class ImageHelper { // "//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU" + // "5ErkJggg==\" alt=\"Red dot\" />"; + // 0 && semi > colon) - type = source.substring(colon + 1, semi); + type = source.substring(colon + 1, semi).trim(); else if (colon > 0 && comma > colon) - type = source.substring(colon + 1, comma); - String enc = (semi > 0 && comma > semi ? source.substring(semi + 1, comma) : null); + type = source.substring(colon + 1, comma).trim(); + + String enc = (semi > 0 && comma > semi ? source.substring(semi + 1, comma).trim() : 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); - } + (TextUtils.isEmpty(enc) /* ASCII */ || "utf8".equalsIgnoreCase(enc))) { + InputStream is = new ByteArrayInputStream(source.substring(comma + 1).getBytes(StandardCharsets.UTF_8)); + Bitmap bm = ImageHelper.renderSvg(is, Color.WHITE, 768); + Helper.ByteArrayInOutStream s = new Helper.ByteArrayInOutStream(); + bm.compress(Bitmap.CompressFormat.PNG, 100, s); + return s.getInputStream(); + } + + if (!"base64".equalsIgnoreCase(enc)) + throw new IllegalArgumentException("Unknown encoding"); String base64 = source.substring(comma + 1); byte[] bytes = Base64.decode(base64.getBytes(), 0); return new ByteArrayInputStream(bytes); - } catch (IllegalArgumentException ex) { + } catch (Throwable ex) { String excerpt = source.substring(0, Math.min(100, source.length())); throw new IllegalArgumentException(excerpt, ex); }