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 @@ -374,13 +374,21 @@ private static void withinParagraph(StringBuilder out, Spanned text, int start,
}
if (fontFamily != null && !fontFamily.isEmpty()) {
if (cssProps.length() > 0) cssProps.append(" ");
if (fontFamily.indexOf(' ') >= 0) {
if (fontFamily.indexOf(' ') >= 0 || fontFamily.indexOf(',') >= 0) {
cssProps.append("font-family: '").append(fontFamily).append("';");
} else {
cssProps.append("font-family: ").append(fontFamily).append(";");
}
}
out.append("<span style=\"").append(cssProps).append("\">");
out.append("<span style=\"")
.append(
cssProps
.toString()
.replace("&", "&amp;")
.replace("<", "&lt;")
.replace(">", "&gt;")
.replace("\"", "&quot;"))
.append("\">");
} else {
out.append("<span>");
}
Expand Down Expand Up @@ -1032,14 +1040,30 @@ private static void startSpan(Editable text, Attributes attributes) {

private static String parseCssFontFamily(String raw) {
if (raw == null) return null;
String value = raw.trim();
if ((value.startsWith("'") && value.endsWith("'"))
|| (value.startsWith("\"") && value.endsWith("\""))) {
String value = firstInFontFamilyString(raw).trim();
if (value.length() >= 2
&& ((value.startsWith("'") && value.endsWith("'"))
|| (value.startsWith("\"") && value.endsWith("\"")))) {
value = value.substring(1, value.length() - 1);
}
return value.isEmpty() ? null : value;
}

private static String firstInFontFamilyString(String value) {
char quote = 0;
for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i);
if (quote != 0) {
if (c == quote) quote = 0;
} else if (c == '\'' || c == '"') {
quote = c;
} else if (c == ',') {
return value.substring(0, i);
}
}
return value;
}

private static <T> void endSpan(Editable text, T style, EnrichedSpanFactory<T> spanFactory) {
CustomStyleMark mark = getLast(text, CustomStyleMark.class);
if (mark == null) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ open class EnrichedCustomStyleSpan(

protected fun getAssets(): AssetManager = assets

protected fun getAllowFontScaling(): Boolean = allowFontScaling

override fun updateMeasureState(textPaint: TextPaint) {
applyFontState(textPaint)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ data class EnrichedTextStyle(
override val inlineCodeBackgroundColor: Int,
// Mentions
override val mentionsStyle: Map<String, MentionStyle>,
val allowFontScaling: Boolean,
) : EnrichedStyle {
companion object {
fun fromReadableMap(
Expand Down Expand Up @@ -120,6 +121,7 @@ data class EnrichedTextStyle(
inlineCodeColor = parseColor(context, inlineCode, "color"),
inlineCodeBackgroundColor = parseColorWithOpacity(context, inlineCode, "backgroundColor"),
mentionsStyle = parseMentionsStyle(context, mentions),
allowFontScaling = allowFontScaling,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ class EnrichedTextCustomStyleSpan(
getFontSize(),
getFontFamily(),
getAssets(),
getAllowFontScaling(),
style.allowFontScaling,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@ class EnrichedTextInputView :
runAsATransaction {
customStyles?.setStyle(styleJSON)
}
layoutManager.invalidateLayout()

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The custom style can introduce a fontSize, which can changes the line heights, etc

}

fun requestHTML(requestId: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class EnrichedInputCustomStyleSpan(
allowFontScaling,
),
EnrichedInputSpan {
override val dependsOnHtmlStyle: Boolean = false
// font size depends on the htmlStyle's allowFontScaling
override val dependsOnHtmlStyle: Boolean
get() = getFontSize() != null

override fun rebuildWithStyle(htmlStyle: HtmlStyle): EnrichedInputCustomStyleSpan =
EnrichedInputCustomStyleSpan(
Expand All @@ -30,6 +32,6 @@ class EnrichedInputCustomStyleSpan(
getFontSize(),
getFontFamily(),
getAssets(),
getAllowFontScaling(),
htmlStyle.allowFontScaling,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class HtmlStyle : EnrichedStyle {

override var mentionsStyle: MutableMap<String, MentionStyle> = mutableMapOf()

val allowFontScaling: Boolean
get() = view?.allowFontScaling ?: EnrichedConstants.ALLOW_FONT_SCALING_DEFAULT

constructor(view: EnrichedTextInputView?, style: ReadableMap?) {
this.view = view
this.style = style
Expand Down Expand Up @@ -157,7 +160,7 @@ class HtmlStyle : EnrichedStyle {
): Float {
val safeMap = ensureValueIsSet(map, key)
val value = safeMap.getDouble(key)
return ceil(pixelFromSpOrDp(value, view?.allowFontScaling ?: EnrichedConstants.ALLOW_FONT_SCALING_DEFAULT))
return ceil(pixelFromSpOrDp(value, allowFontScaling))
}

private fun parseColorWithOpacity(
Expand Down
Loading