From e99ce01dfe74383d6fa3b224ef6910aad00fb504 Mon Sep 17 00:00:00 2001 From: Mikael Sand Date: Mon, 24 Jul 2017 19:35:07 +0300 Subject: [PATCH] Implement FontData Enums --- .../main/java/com/horcrux/svg/FontData.java | 25 +++++----- .../main/java/com/horcrux/svg/FontStyle.java | 7 +++ .../main/java/com/horcrux/svg/FontWeight.java | 48 ++++++++++++++++++ .../java/com/horcrux/svg/TSpanShadowNode.java | 49 +++++++------------ .../main/java/com/horcrux/svg/TextAnchor.java | 8 +++ .../java/com/horcrux/svg/TextDecoration.java | 41 ++++++++++++++++ .../java/com/horcrux/svg/TextShadowNode.java | 13 ----- 7 files changed, 136 insertions(+), 55 deletions(-) create mode 100644 android/src/main/java/com/horcrux/svg/FontStyle.java create mode 100644 android/src/main/java/com/horcrux/svg/FontWeight.java create mode 100644 android/src/main/java/com/horcrux/svg/TextAnchor.java create mode 100644 android/src/main/java/com/horcrux/svg/TextDecoration.java diff --git a/android/src/main/java/com/horcrux/svg/FontData.java b/android/src/main/java/com/horcrux/svg/FontData.java index 2f190b96..7c10bb11 100644 --- a/android/src/main/java/com/horcrux/svg/FontData.java +++ b/android/src/main/java/com/horcrux/svg/FontData.java @@ -4,6 +4,7 @@ import com.facebook.react.bridge.ReadableMap; import static com.facebook.react.uimanager.ViewProps.FONT_FAMILY; import static com.facebook.react.uimanager.ViewProps.FONT_SIZE; +import static com.facebook.react.uimanager.ViewProps.FONT_STYLE; import static com.facebook.react.uimanager.ViewProps.FONT_WEIGHT; class FontData { @@ -21,12 +22,12 @@ class FontData { final double fontSize; - final String fontStyle; final String fontFamily; - final String fontWeight; + final FontStyle fontStyle; + final FontWeight fontWeight; - final String textAnchor; - final String textDecoration; + final TextAnchor textAnchor; + final TextDecoration textDecoration; final double kerning; final double wordSpacing; @@ -37,12 +38,12 @@ class FontData { static final FontData Defaults = new FontData(); private FontData() { - fontStyle = ""; fontFamily = ""; - fontWeight = ""; + fontStyle = FontStyle.normal; + fontWeight = FontWeight.Normal; - textAnchor = "start"; - textDecoration = "none"; + textAnchor = TextAnchor.start; + textDecoration = TextDecoration.None; manualKerning = false; kerning = DEFAULT_KERNING; @@ -77,12 +78,12 @@ class FontData { fontSize = parentFontSize; } - fontStyle = font.hasKey(FONT_SIZE) ? font.getString(FONT_SIZE) : parent.fontStyle; fontFamily = font.hasKey(FONT_FAMILY) ? font.getString(FONT_FAMILY) : parent.fontFamily; - fontWeight = font.hasKey(FONT_WEIGHT) ? font.getString(FONT_WEIGHT) : parent.fontWeight; + fontStyle = font.hasKey(FONT_STYLE) ? FontStyle.valueOf(font.getString(FONT_STYLE)) : parent.fontStyle; + fontWeight = font.hasKey(FONT_WEIGHT) ? FontWeight.getEnum(font.getString(FONT_WEIGHT)) : parent.fontWeight; - textAnchor = font.hasKey(TEXT_ANCHOR) ? font.getString(TEXT_ANCHOR) : parent.textAnchor; - textDecoration = font.hasKey(TEXT_DECORATION) ? font.getString(TEXT_DECORATION) : parent.textDecoration; + textAnchor = font.hasKey(TEXT_ANCHOR) ? TextAnchor.valueOf(font.getString(TEXT_ANCHOR)) : parent.textAnchor; + textDecoration = font.hasKey(TEXT_DECORATION) ? TextDecoration.getEnum(font.getString(TEXT_DECORATION)) : parent.textDecoration; final boolean hasKerning = font.hasKey(KERNING); manualKerning = hasKerning || parent.manualKerning; diff --git a/android/src/main/java/com/horcrux/svg/FontStyle.java b/android/src/main/java/com/horcrux/svg/FontStyle.java new file mode 100644 index 00000000..d1792933 --- /dev/null +++ b/android/src/main/java/com/horcrux/svg/FontStyle.java @@ -0,0 +1,7 @@ +package com.horcrux.svg; + +enum FontStyle { + normal, + italic, + oblique +} diff --git a/android/src/main/java/com/horcrux/svg/FontWeight.java b/android/src/main/java/com/horcrux/svg/FontWeight.java new file mode 100644 index 00000000..11462043 --- /dev/null +++ b/android/src/main/java/com/horcrux/svg/FontWeight.java @@ -0,0 +1,48 @@ +package com.horcrux.svg; + +import com.facebook.common.internal.ImmutableMap; + +import java.util.HashMap; +import java.util.Map; + +enum FontWeight { + Normal ("normal"), + Bold ("bold"), + Bolder ("bolder"), + Lighter ("lighter"), + w100 ("100"), + w200 ("200"), + w300 ("300"), + w400 ("400"), + w500 ("500"), + w600 ("600"), + w700 ("700"), + w800 ("800"), + w900 ("900"); + + private final String weight; + FontWeight(String weight) { + this.weight = weight; + } + + public static FontWeight getEnum(String strVal) { + if(!weightToEnum.containsKey(strVal)) { + throw new IllegalArgumentException("Unknown String Value: " + strVal); + } + return weightToEnum.get(strVal); + } + + private static final Map weightToEnum; + static { + final Map tmpMap = new HashMap<>(); + for(final FontWeight en : FontWeight.values()) { + tmpMap.put(en.weight, en); + } + weightToEnum = ImmutableMap.copyOf(tmpMap); + } + + @Override + public String toString() { + return weight; + } +} diff --git a/android/src/main/java/com/horcrux/svg/TSpanShadowNode.java b/android/src/main/java/com/horcrux/svg/TSpanShadowNode.java index ae9c48e7..bc73bc2e 100644 --- a/android/src/main/java/com/horcrux/svg/TSpanShadowNode.java +++ b/android/src/main/java/com/horcrux/svg/TSpanShadowNode.java @@ -31,11 +31,8 @@ import static android.graphics.PathMeasure.TANGENT_MATRIX_FLAG; * Shadow node for virtual TSpan view */ class TSpanShadowNode extends TextShadowNode { - private static final String STRETCH = "stretch"; - private static final String ITALIC = "italic"; private static final String FONTS = "fonts/"; - private static final String BOLD = "bold"; private static final String OTF = ".otf"; private static final String TTF = ".ttf"; @@ -85,21 +82,6 @@ class TSpanShadowNode extends TextShadowNode { return mCache; } - private double getTextAnchorShift(double width, String textAnchor) { - double x = 0; - - switch (textAnchor) { - case TEXT_ANCHOR_MIDDLE: - x = -width / 2; - break; - case TEXT_ANCHOR_END: - x = -width; - break; - } - - return x; - } - private Path getLinePath(String line, Paint paint) { final int length = line.length(); final Path path = new Path(); @@ -115,7 +97,18 @@ class TSpanShadowNode extends TextShadowNode { double distance = 0; double renderMethodScaling = 1; final double textMeasure = paint.measureText(line); - double offset = getTextAnchorShift(textMeasure, font.textAnchor); + + final TextAnchor textAnchor = font.textAnchor; + double offset; + if (textAnchor == TextAnchor.start) { + offset = 0; + } else { + if (textAnchor == TextAnchor.middle) { + offset = -textMeasure / 2; + } else { + offset = -textMeasure; + } + } PathMeasure pm = null; if (textPath != null) { @@ -238,22 +231,18 @@ class TSpanShadowNode extends TextShadowNode { double fontSize = font.fontSize * mScale; - boolean isBold = BOLD.equals(font.fontWeight); + boolean isBold = font.fontWeight == FontWeight.Bold; - boolean isItalic = ITALIC.equals(font.fontStyle); + boolean isItalic = font.fontStyle == FontStyle.italic; boolean underlineText = false; boolean strikeThruText = false; - String decoration = font.textDecoration; - switch (decoration) { - case TEXT_DECORATION_UNDERLINE: - underlineText = true; - break; - - case TEXT_DECORATION_LINE_THROUGH: - strikeThruText = true; - break; + TextDecoration decoration = font.textDecoration; + if (decoration == TextDecoration.Underline) { + underlineText = true; + } else if (decoration == TextDecoration.LineThrough) { + strikeThruText = true; } int fontStyle; diff --git a/android/src/main/java/com/horcrux/svg/TextAnchor.java b/android/src/main/java/com/horcrux/svg/TextAnchor.java new file mode 100644 index 00000000..b2aa7627 --- /dev/null +++ b/android/src/main/java/com/horcrux/svg/TextAnchor.java @@ -0,0 +1,8 @@ +package com.horcrux.svg; + +enum TextAnchor +{ + start, + middle, + end +} diff --git a/android/src/main/java/com/horcrux/svg/TextDecoration.java b/android/src/main/java/com/horcrux/svg/TextDecoration.java new file mode 100644 index 00000000..9669e1e9 --- /dev/null +++ b/android/src/main/java/com/horcrux/svg/TextDecoration.java @@ -0,0 +1,41 @@ +package com.horcrux.svg; + +import com.facebook.common.internal.ImmutableMap; + +import java.util.HashMap; +import java.util.Map; + +enum TextDecoration +{ + None("none"), + Underline("underline"), + Overline("overline"), + LineThrough("line-through"), + Blink("blink"); + + private final String decoration; + TextDecoration(String decoration) { + this.decoration = decoration; + } + + public static TextDecoration getEnum(String strVal) { + if(!decorationToEnum.containsKey(strVal)) { + throw new IllegalArgumentException("Unknown String Value: " + strVal); + } + return decorationToEnum.get(strVal); + } + + private static final Map decorationToEnum; + static { + final Map tmpMap = new HashMap<>(); + for(final TextDecoration en : TextDecoration.values()) { + tmpMap.put(en.decoration, en); + } + decorationToEnum = ImmutableMap.copyOf(tmpMap); + } + + @Override + public String toString() { + return decoration; + } +} diff --git a/android/src/main/java/com/horcrux/svg/TextShadowNode.java b/android/src/main/java/com/horcrux/svg/TextShadowNode.java index 443bb2ee..01d45f6d 100644 --- a/android/src/main/java/com/horcrux/svg/TextShadowNode.java +++ b/android/src/main/java/com/horcrux/svg/TextShadowNode.java @@ -24,19 +24,6 @@ import javax.annotation.Nullable; */ class TextShadowNode extends GroupShadowNode { - // static final String INHERIT = "inherit"; - - // static final String TEXT_ANCHOR_AUTO = "auto"; - // static final String TEXT_ANCHOR_START = "start"; - static final String TEXT_ANCHOR_MIDDLE = "middle"; - static final String TEXT_ANCHOR_END = "end"; - - // static final String TEXT_DECORATION_NONE = "none"; - static final String TEXT_DECORATION_UNDERLINE = "underline"; - // static final String TEXT_DECORATION_OVERLINE = "overline"; - static final String TEXT_DECORATION_LINE_THROUGH = "line-through"; - // static final String TEXT_DECORATION_BLINK = "blink"; - private @Nullable ReadableArray mPositionX; private @Nullable ReadableArray mPositionY; private @Nullable ReadableArray mRotate;