Skip to content

Commit 178e411

Browse files
fix: add image width and height image attributes in Android parser
1 parent af723f6 commit 178e411

3 files changed

Lines changed: 31 additions & 13 deletions

File tree

android/src/main/java/com/swmansion/enriched/spans/EnrichedImageSpan.kt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,19 @@ import android.net.Uri
88
import android.text.style.ImageSpan
99
import androidx.core.graphics.withSave
1010
import com.swmansion.enriched.spans.interfaces.EnrichedInlineSpan
11-
import com.swmansion.enriched.styles.HtmlStyle
1211

1312
class EnrichedImageSpan : ImageSpan, EnrichedInlineSpan {
14-
private var width: Float = 0F
15-
private var height: Float = 0F
16-
private var htmlStyle: HtmlStyle? = null
13+
private var width: Int = 0
14+
private var height: Int = 0
1715

18-
constructor(context: Context, uri: Uri, width: Float, height: Float) : super(context, uri, ALIGN_BASELINE) {
16+
constructor(context: Context, uri: Uri, width: Int, height: Int) : super(context, uri, ALIGN_BASELINE) {
1917
this.width = width
2018
this.height = height
2119
}
2220

23-
constructor(drawable: Drawable, source: String, htmlStyle: HtmlStyle) : super(drawable, source, ALIGN_BASELINE) {
24-
this.htmlStyle = htmlStyle
21+
constructor(drawable: Drawable, source: String, width: Int, height: Int) : super(drawable, source, ALIGN_BASELINE) {
22+
this.width = width
23+
this.height = height
2524
}
2625

2726
override fun draw(
@@ -38,7 +37,15 @@ class EnrichedImageSpan : ImageSpan, EnrichedInlineSpan {
3837

3938
override fun getDrawable(): Drawable {
4039
val drawable = super.getDrawable()
41-
drawable.setBounds(0, 0, width.toInt(), height.toInt())
40+
drawable.setBounds(0, 0, width, height)
4241
return drawable
4342
}
43+
44+
fun getWidth(): Int {
45+
return width
46+
}
47+
48+
fun getHeight(): Int {
49+
return height
50+
}
4451
}

android/src/main/java/com/swmansion/enriched/styles/ParametrizedStyles.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class ParametrizedStyles(private val view: EnrichedTextInputView) {
191191
val imageWidth = width ?: view.htmlStyle.imgWidth
192192
val imageHeight = height ?: view.htmlStyle.imgHeight
193193

194-
val span = EnrichedImageSpan(view.context, uri, imageWidth, imageHeight)
194+
val span = EnrichedImageSpan(view.context, uri, imageWidth.toInt(), imageHeight.toInt())
195195
val (safeStart, safeEnd) = spannable.getSafeSpanBoundaries(start, end)
196196
spannable.setSpan(span, safeStart, safeEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
197197
}

android/src/main/java/com/swmansion/enriched/utils/EnrichedParser.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,15 @@ private static void withinParagraph(StringBuilder out, Spanned text, int start,
275275
if (style[j] instanceof EnrichedImageSpan) {
276276
out.append("<img src=\"");
277277
out.append(((EnrichedImageSpan) style[j]).getSource());
278-
out.append("\">");
278+
279+
out.append(" width=\"");
280+
out.append(((EnrichedImageSpan) style[j]).getWidth());
281+
out.append("\"");
282+
283+
out.append(" height=\"");
284+
out.append(((EnrichedImageSpan) style[j]).getHeight());
285+
286+
out.append("\"/>");
279287
// Don't output the placeholder character underlying the image.
280288
i = next;
281289
}
@@ -454,7 +462,7 @@ private void handleStartTag(String tag, Attributes attributes) {
454462
} else if (tag.equalsIgnoreCase("h3")) {
455463
startHeading(mSpannableStringBuilder, 3);
456464
} else if (tag.equalsIgnoreCase("img")) {
457-
startImg(mSpannableStringBuilder, attributes, mImageGetter, mStyle);
465+
startImg(mSpannableStringBuilder, attributes, mImageGetter);
458466
} else if (tag.equalsIgnoreCase("code")) {
459467
start(mSpannableStringBuilder, new Code());
460468
} else if (tag.equalsIgnoreCase("mention")) {
@@ -679,8 +687,11 @@ private static void end(Editable text, Class kind, Object repl) {
679687
}
680688
}
681689

682-
private static void startImg(Editable text, Attributes attributes, EnrichedParser.ImageGetter img, HtmlStyle style) {
690+
private static void startImg(Editable text, Attributes attributes, EnrichedParser.ImageGetter img) {
683691
String src = attributes.getValue("", "src");
692+
String width = attributes.getValue("", "width");
693+
String height = attributes.getValue("", "height");
694+
684695
Drawable d = null;
685696
if (img != null) {
686697
d = img.getDrawable(src);
@@ -692,7 +703,7 @@ private static void startImg(Editable text, Attributes attributes, EnrichedParse
692703

693704
int len = text.length();
694705
text.append("");
695-
text.setSpan(new EnrichedImageSpan(d, src, style), len, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
706+
text.setSpan(new EnrichedImageSpan(d, src, Integer.parseInt(width), Integer.parseInt(height)), len, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
696707
}
697708

698709
private static void startA(Editable text, Attributes attributes) {

0 commit comments

Comments
 (0)