Skip to content

Commit 8da7923

Browse files
committed
fix(android): wrong weight for spans
faster string parsing
1 parent a585990 commit 8da7923

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

plugin/platforms/android/java/com/nativescript/label/Font.java

+15-12
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public static String getFontWeightSuffix(String fontWeight) {
105105
return Build.VERSION.SDK_INT >= 16 ? "-light" : "";
106106
case FontWeight.NORMAL:
107107
case "400":
108+
case "":
108109
return "";
109110
case FontWeight.MEDIUM:
110111
case FontWeight.SEMI_BOLD:
@@ -258,8 +259,8 @@ static ArrayList<ArrayList<String>> parseFormattedString(String formattedString)
258259

259260

260261
public static void setSpanModifiers(Context context, String fontFolder, SpannableStringBuilder ssb, ArrayList<String> span, int start, int end) {
261-
boolean bold = span.get(2) == "1";
262-
boolean italic = span.get(3) == "1";
262+
boolean bold = span.get(2).equals("bold") || span.get(2).equals("700");
263+
boolean italic = span.get(3).equals("1");
263264

264265
if (bold && italic) {
265266
ssb.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD_ITALIC), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
@@ -270,8 +271,8 @@ public static void setSpanModifiers(Context context, String fontFolder, Spannabl
270271
}
271272

272273
String fontFamily = span.get(0);
273-
if (!fontFamily.equals("undefined") ) {
274-
Typeface typeface = createTypeface(context, fontFolder, fontFamily, bold ? "bold" : "normal",
274+
if (!fontFamily.equals("0") ) {
275+
Typeface typeface = createTypeface(context, fontFolder, fontFamily, span.get(2),
275276
bold, italic);
276277
// const font = new Font(fontFamily, 0, (italic) ? "italic" : "normal", (bold) ? "bold" : "normal");
277278
// const typeface = font.getAndroidTypeface() || android.graphics.Typeface.create(fontFamily, 0);
@@ -280,30 +281,32 @@ public static void setSpanModifiers(Context context, String fontFolder, Spannabl
280281
}
281282

282283
String fontSize = span.get(1);
283-
if (!fontSize.equals("undefined") ) {
284+
if (!fontSize.equals("-1") ) {
284285
ssb.setSpan(new AbsoluteSizeSpan(Math.round(Float.parseFloat(fontSize) * context.getResources().getDisplayMetrics().density)), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
285286
}
286287

287288
String color = span.get(5);
288-
if (!color.equals("undefined") ) {
289+
if (!color.equals("-1") ) {
289290
ssb.setSpan(new ForegroundColorSpan(Integer.parseInt(color)), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
290291
}
291292

292293

293294
String backgroundColor = span.get(6);
294295

295-
if (!backgroundColor.equals("undefined") ) {
296+
if (!backgroundColor.equals("-1") ) {
296297
ssb.setSpan(new BackgroundColorSpan(Integer.parseInt(backgroundColor)), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
297298
}
298299

299300

300301
String textDecoration = span.get(4);
301-
if (textDecoration.contains("underline")) {
302-
ssb.setSpan(new android.text.style.UnderlineSpan(), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
303-
}
302+
if (!textDecoration.equals("0") ) {
303+
if (textDecoration.contains("underline")) {
304+
ssb.setSpan(new android.text.style.UnderlineSpan(), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
305+
}
304306

305-
if (textDecoration.contains("line-through")) {
306-
ssb.setSpan(new android.text.style.StrikethroughSpan(), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
307+
if (textDecoration.contains("line-through")) {
308+
ssb.setSpan(new android.text.style.StrikethroughSpan(), start, end, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
309+
}
307310
}
308311
long stopTime = System.nanoTime();
309312
// TODO: Implement letterSpacing for Span here.

src/label.android.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ Span.prototype.toNativeString = function() {
9999
text = getTransformedText(text, textTransform);
100100
}
101101
const delimiter = String.fromCharCode(0x1e);
102-
let result = `${this.fontFamily}${delimiter}${this.fontSize}${delimiter}${isBold(this.fontWeight) ? 1 : 0}${delimiter}${
102+
let result = `${this.fontFamily || 0}${delimiter}${this.fontSize !== undefined ? this.fontSize: -1}${delimiter}${this.fontWeight || ''}${delimiter}${
103103
this.fontStyle === 'italic' ? 1 : 0
104-
}${delimiter}${textDecoration}${delimiter}${this.color ? this.color.android : undefined}${delimiter}${backgroundColor ? backgroundColor.android : undefined}${delimiter}${this.text}`;
105-
104+
}${delimiter}${textDecoration || 0}${delimiter}${this.color ? this.color.android : -1}${delimiter}${backgroundColor ? backgroundColor.android : -1}${delimiter}${this.text}`;
105+
// console.log('toNativeString', result, this.fontFamily, this.style.fontFamily, this.style.fontWeight, this.style.fontStyle)
106106
return result;
107107
};
108108

0 commit comments

Comments
 (0)