Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.ccil.cowan.tagsoup.HTMLSchema;
import org.ccil.cowan.tagsoup.Parser;
import org.xml.sax.Attributes;
Expand Down Expand Up @@ -157,6 +159,17 @@ private static String getBlockTag(EnrichedParagraphSpan[] spans) {
return "p";
}

private static String getTextAlignStyleValue(Layout.Alignment alignment) {
if (alignment == null || alignment == Layout.Alignment.ALIGN_NORMAL) {
return null;
} else if (alignment == Layout.Alignment.ALIGN_CENTER) {
return "center";
} else if (alignment == Layout.Alignment.ALIGN_OPPOSITE) {
return "right";
}
return null;
}

private static void withinBlock(StringBuilder out, Spanned text, int start, int end) {
boolean isInUlList = false;
boolean isInOlList = false;
Expand Down Expand Up @@ -234,6 +247,18 @@ private static void withinBlock(StringBuilder out, Spanned text, int start, int
}
}

ParagraphStyle[] paragraphStyleSpans = text.getSpans(i, next, ParagraphStyle.class);
for (ParagraphStyle paragraphStyle : paragraphStyleSpans) {
if (paragraphStyle instanceof AlignmentSpan) {
String alignment =
getTextAlignStyleValue(((AlignmentSpan) paragraphStyle).getAlignment());
if (alignment != null) {
out.append(" style=\"text-align: ").append(alignment).append("\"");
}
break;
}
}

out.append(">");
withinParagraph(out, text, i, next);
out.append("</");
Expand Down Expand Up @@ -463,6 +488,10 @@ private void handleStartTag(String tag, Attributes attributes) {
} else if (tag.equalsIgnoreCase("p")) {
isEmptyTag = true;
startBlockElement(mSpannableStringBuilder);
Layout.Alignment alignment = parseTextAlignFromStyle(attributes.getValue("", "style"));
if (alignment != null) {
start(mSpannableStringBuilder, new Alignment(alignment));
}
} else if (tag.equalsIgnoreCase("ul")) {
isInOrderedList = false;
String dataType = attributes.getValue("", "data-type");
Expand Down Expand Up @@ -514,6 +543,33 @@ private void handleStartTag(String tag, Attributes attributes) {
}
}

private static Layout.Alignment parseTextAlignFromStyle(String styleAttr) {
if (styleAttr == null) {
return null;
}

Pattern pattern =
Pattern.compile("text-align\\s*:\\s*(left|center|right|justify)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(styleAttr);
if (!matcher.find()) {
return null;
}

String value = matcher.group(1).toLowerCase();
switch (value) {
case "left":
return Layout.Alignment.ALIGN_NORMAL;
case "center":
return Layout.Alignment.ALIGN_CENTER;
case "right":
return Layout.Alignment.ALIGN_OPPOSITE;
case "justify":
return Layout.Alignment.ALIGN_NORMAL;
default:
return null;
}
}

private void handleEndTag(String tag) {
if (tag.equalsIgnoreCase("br")) {
handleBr(mSpannableStringBuilder);
Expand Down Expand Up @@ -597,6 +653,11 @@ private static void handleBr(Editable text) {
private void startLi(Editable text, Attributes attributes) {
startBlockElement(text);

Layout.Alignment alignment = parseTextAlignFromStyle(attributes.getValue("", "style"));
if (alignment != null) {
start(text, new Alignment(alignment));
}

if (isInOrderedList) {
currentOrderedListItemIndex++;
start(text, new List("ordered", currentOrderedListItemIndex, false));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.swmansion.enriched.textinput.spans

import android.graphics.Canvas
import android.graphics.Paint
import android.text.style.ReplacementSpan

class EnrichedAlignmentPlaceholderSpan : ReplacementSpan() {
override fun getSize(
paint: Paint,
text: CharSequence,
start: Int,
end: Int,
fm: Paint.FontMetricsInt?,
): Int = paint.measureText(" ").toInt()

override fun draw(
canvas: Canvas,
text: CharSequence,
start: Int,
end: Int,
x: Float,
top: Int,
y: Int,
bottom: Int,
paint: Paint,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ open class EnrichedCheckboxListSpan(
val textCenter = baseline + (fm.ascent + fm.descent) / 2f
val drawableTop = textCenter - (enrichedStyle.ulCheckboxBoxSize / 2f)

canvas.withTranslation(x.toFloat() + enrichedStyle.ulCheckboxMarginLeft, drawableTop) {
val markerBaseX = if (dir > 0) 0f else x.toFloat()
canvas.withTranslation(markerBaseX + enrichedStyle.ulCheckboxMarginLeft, drawableTop) {
checkboxDrawable.draw(this)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ open class EnrichedOrderedListSpan(
val width = paint.measureText(text)

val yPosition = baseline.toFloat()
val xPosition = (enrichedStyle.olMarginLeft + x - width / 2) * dir
val markerBaseX = if (dir > 0) 0 else x
val xPosition = (enrichedStyle.olMarginLeft + markerBaseX - width / 2) * dir

val originalColor = paint.color
val originalTypeface = paint.typeface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ open class EnrichedUnorderedListSpan(
paint.style = Paint.Style.FILL

val bulletRadius = enrichedStyle.ulBulletSize / 2f
val fm = paint.fontMetricsInt
val yPosition = baseline + (fm.ascent + fm.descent) / 2f
val xPosition = x + dir * bulletRadius + enrichedStyle.ulMarginLeft
val yPosition = (top + bottom) / 2f
val markerBaseX = if (dir > 0) 0 else x
val xPosition = markerBaseX + dir * bulletRadius + enrichedStyle.ulMarginLeft

canvas.drawCircle(xPosition, yPosition, bulletRadius, paint)

Expand Down
Loading