Implement FontData Enums

This commit is contained in:
Mikael Sand
2017-07-24 19:35:07 +03:00
parent 7c5a7ceed3
commit e99ce01dfe
7 changed files with 136 additions and 55 deletions
@@ -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;
@@ -0,0 +1,7 @@
package com.horcrux.svg;
enum FontStyle {
normal,
italic,
oblique
}
@@ -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<String, FontWeight> weightToEnum;
static {
final Map<String, FontWeight> tmpMap = new HashMap<>();
for(final FontWeight en : FontWeight.values()) {
tmpMap.put(en.weight, en);
}
weightToEnum = ImmutableMap.copyOf(tmpMap);
}
@Override
public String toString() {
return weight;
}
}
@@ -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;
@@ -0,0 +1,8 @@
package com.horcrux.svg;
enum TextAnchor
{
start,
middle,
end
}
@@ -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<String, TextDecoration> decorationToEnum;
static {
final Map<String, TextDecoration> tmpMap = new HashMap<>();
for(final TextDecoration en : TextDecoration.values()) {
tmpMap.put(en.decoration, en);
}
decorationToEnum = ImmutableMap.copyOf(tmpMap);
}
@Override
public String toString() {
return decoration;
}
}
@@ -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;