From 1b5e775f066f1e86601feed07929ef14a9075919 Mon Sep 17 00:00:00 2001 From: Mikael Sand Date: Fri, 19 Jul 2019 14:52:24 +0300 Subject: [PATCH] [android] improve font-weight handling related to #860 --- .../main/java/com/horcrux/svg/FontData.java | 52 ++++++++++++------- .../horcrux/svg/RenderableViewManager.java | 16 ++++++ .../main/java/com/horcrux/svg/TSpanView.java | 18 +------ .../java/com/horcrux/svg/TextProperties.java | 8 +-- 4 files changed, 55 insertions(+), 39 deletions(-) diff --git a/android/src/main/java/com/horcrux/svg/FontData.java b/android/src/main/java/com/horcrux/svg/FontData.java index 20c5f06e..33013278 100644 --- a/android/src/main/java/com/horcrux/svg/FontData.java +++ b/android/src/main/java/com/horcrux/svg/FontData.java @@ -4,8 +4,6 @@ import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.ReadableType; import java.util.EnumMap; -import java.util.HashMap; -import java.util.Map; import static com.facebook.react.uimanager.ViewProps.FONT_FAMILY; import static com.facebook.react.uimanager.ViewProps.FONT_SIZE; @@ -17,22 +15,38 @@ class AbsoluteFontWeight { static int normal = 400; - private static final EnumMap fontWeightToInt; + private static final FontWeight[] WEIGHTS = new FontWeight[]{ + FontWeight.w100, + FontWeight.w100, + FontWeight.w200, + FontWeight.w300, + FontWeight.Normal, + FontWeight.w500, + FontWeight.w600, + FontWeight.Bold, + FontWeight.w800, + FontWeight.w900, + FontWeight.w900, + }; + static FontWeight nearestFontWeight(int absoluteFontWeight) { + return WEIGHTS[Math.round(absoluteFontWeight / 100f)]; + } + + private static final EnumMap fontWeightToInt; static { - final Map integerMap = new HashMap<>(); - integerMap.put(FontWeight.Normal, 400); - integerMap.put(FontWeight.Bold, 700); - integerMap.put(FontWeight.w100, 100); - integerMap.put(FontWeight.w200, 200); - integerMap.put(FontWeight.w300, 300); - integerMap.put(FontWeight.w400, 400); - integerMap.put(FontWeight.w500, 500); - integerMap.put(FontWeight.w600, 600); - integerMap.put(FontWeight.w700, 700); - integerMap.put(FontWeight.w800, 800); - integerMap.put(FontWeight.w900, 900); - fontWeightToInt = new EnumMap<>(integerMap); + fontWeightToInt = new EnumMap<>(FontWeight.class); + fontWeightToInt.put(FontWeight.Normal, 400); + fontWeightToInt.put(FontWeight.Bold, 700); + fontWeightToInt.put(FontWeight.w100, 100); + fontWeightToInt.put(FontWeight.w200, 200); + fontWeightToInt.put(FontWeight.w300, 300); + fontWeightToInt.put(FontWeight.w400, 400); + fontWeightToInt.put(FontWeight.w500, 500); + fontWeightToInt.put(FontWeight.w600, 600); + fontWeightToInt.put(FontWeight.w700, 700); + fontWeightToInt.put(FontWeight.w800, 800); + fontWeightToInt.put(FontWeight.w900, 900); } static int from(FontWeight fontWeight) { @@ -157,8 +171,8 @@ class FontData { private void handleNumericWeight(FontData parent, double number) { long weight = Math.round(number); if (weight >= 1 && weight <= 1000) { - fontWeight = FontWeight.Normal; absoluteFontWeight = (int)weight; + fontWeight = AbsoluteFontWeight.nearestFontWeight(absoluteFontWeight); } else { setInheritedWeight(parent); } @@ -185,8 +199,8 @@ class FontData { } if (font.hasKey(FONT_WEIGHT)) { - ReadableType fontSizeType = font.getType(FONT_WEIGHT); - if (fontSizeType == ReadableType.Number) { + ReadableType fontWeightType = font.getType(FONT_WEIGHT); + if (fontWeightType == ReadableType.Number) { handleNumericWeight(parent, font.getDouble(FONT_WEIGHT)); } else { String string = font.getString(FONT_WEIGHT); diff --git a/android/src/main/java/com/horcrux/svg/RenderableViewManager.java b/android/src/main/java/com/horcrux/svg/RenderableViewManager.java index 5128e1f4..e588ddce 100644 --- a/android/src/main/java/com/horcrux/svg/RenderableViewManager.java +++ b/android/src/main/java/com/horcrux/svg/RenderableViewManager.java @@ -366,6 +366,22 @@ class RenderableViewManager extends ViewGroupManager { } node.setFont(map); } + + @ReactProp(name = "fontWeight") + public void setFontWeight(GroupView node, Dynamic fontWeight) { + JavaOnlyMap map = new JavaOnlyMap(); + switch (fontWeight.getType()) { + case Number: + map.putDouble("fontWeight", fontWeight.asDouble()); + break; + case String: + map.putString("fontWeight", fontWeight.asString()); + break; + default: + return; + } + node.setFont(map); + } } diff --git a/android/src/main/java/com/horcrux/svg/TSpanView.java b/android/src/main/java/com/horcrux/svg/TSpanView.java index 50d31e80..42fe4089 100644 --- a/android/src/main/java/com/horcrux/svg/TSpanView.java +++ b/android/src/main/java/com/horcrux/svg/TSpanView.java @@ -985,21 +985,9 @@ class TSpanView extends TextView { double fontSize = font.fontSize * mScale; - boolean isBold = font.fontWeight == FontWeight.Bold; + boolean isBold = font.fontWeight == FontWeight.Bold || font.absoluteFontWeight >= 550; boolean isItalic = font.fontStyle == FontStyle.italic; - /* - boolean underlineText = false; - boolean strikeThruText = false; - - TextDecoration decoration = font.textDecoration; - if (decoration == TextDecoration.Underline) { - underlineText = true; - } else if (decoration == TextDecoration.LineThrough) { - strikeThruText = true; - } - */ - int fontStyle; if (isBold && isItalic) { fontStyle = Typeface.BOLD_ITALIC; @@ -1035,10 +1023,6 @@ class TSpanView extends TextView { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { paint.setLetterSpacing(0); } - - // Do these have any effect for anyone? Not for me (@msand) at least. - // paint.setUnderlineText(underlineText); - // paint.setStrikeThruText(strikeThruText); } private void setupTextPath() { diff --git a/android/src/main/java/com/horcrux/svg/TextProperties.java b/android/src/main/java/com/horcrux/svg/TextProperties.java index 013aedbe..fceb7427 100644 --- a/android/src/main/java/com/horcrux/svg/TextProperties.java +++ b/android/src/main/java/com/horcrux/svg/TextProperties.java @@ -95,10 +95,9 @@ class TextProperties { } enum FontWeight { + // Absolute Normal ("normal"), Bold ("bold"), - Bolder ("bolder"), - Lighter ("lighter"), w100 ("100"), w200 ("200"), w300 ("300"), @@ -107,7 +106,10 @@ class TextProperties { w600 ("600"), w700 ("700"), w800 ("800"), - w900 ("900"); + w900 ("900"), + // Relative + Bolder ("bolder"), + Lighter ("lighter"); private final String weight; FontWeight(String weight) {