[android] text-color option for formatted sections

This commit is contained in:
Łukasz Paczos
2019-03-14 18:37:54 +01:00
committed by Łukasz Paczos
parent 69e11155c8
commit f1ac3f29c3
8 changed files with 398 additions and 145 deletions

View File

@@ -326,7 +326,7 @@ public class Style {
*/
public void addImage(@NonNull final String name, @NonNull final Bitmap bitmap, boolean sdf) {
validateState("addImage");
new BitmapImageConversionTask(nativeMap, sdf).execute(new Builder.ImageWrapper(name, bitmap, sdf));
new BitmapImageConversionTask(nativeMap).execute(new Builder.ImageWrapper(name, bitmap, sdf));
}
/**
@@ -346,7 +346,7 @@ public class Style {
*/
public void addImages(@NonNull HashMap<String, Bitmap> images, boolean sdf) {
validateState("addImages");
new BitmapImageConversionTask(nativeMap, sdf).execute(Builder.ImageWrapper.convertToImageArray(images, sdf));
new BitmapImageConversionTask(nativeMap).execute(Builder.ImageWrapper.convertToImageArray(images, sdf));
}
/**
@@ -905,11 +905,9 @@ public class Style {
private static class BitmapImageConversionTask extends AsyncTask<Builder.ImageWrapper, Void, List<Image>> {
private WeakReference<NativeMap> nativeMap;
private boolean sdf;
BitmapImageConversionTask(NativeMap nativeMap, boolean sdf) {
BitmapImageConversionTask(NativeMap nativeMap) {
this.nativeMap = new WeakReference<>(nativeMap);
this.sdf = sdf;
}
@NonNull
@@ -919,10 +917,12 @@ public class Style {
ByteBuffer buffer;
String name;
Bitmap bitmap;
boolean sdf;
for (Builder.ImageWrapper param : params) {
name = param.id;
bitmap = param.bitmap;
sdf = param.sdf;
if (bitmap.getConfig() != Bitmap.Config.ARGB_8888) {
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, false);

View File

@@ -2843,7 +2843,8 @@ public class Expression {
* CircleLayer circleLayer = new CircleLayer("layer-id", "source-id");
* circleLayer.setProperties(
* circleColor(switchCase(
* eq(literal("it"), resolvedLocale(collator(true, true, Locale.ITALY))), literal(ColorUtils.colorToRgbaString(Color.GREEN)),
* eq(literal("it"), resolvedLocale(collator(true, true, Locale.ITALY))), literal(ColorUtils.colorToRgbaString
* (Color.GREEN)),
* literal(ColorUtils.colorToRgbaString(Color.RED))))
* );
* }
@@ -3626,7 +3627,8 @@ public class Expression {
* @return expression
* @see <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-step">Style specification</a>
*/
public static Expression step(@NonNull Expression input, @NonNull Expression defaultOutput, @NonNull Expression... stops) {
public static Expression step(@NonNull Expression input, @NonNull Expression defaultOutput,
@NonNull Expression... stops) {
return new Expression("step", join(new Expression[] {input, defaultOutput}, stops));
}
@@ -4411,7 +4413,7 @@ public class Expression {
/**
* If set, the text-font argument overrides the font specified by the root layout properties.
* <p>
* "text-font" is required to a literal array.
* "text-font" is required to be a literal array.
* <p>
* The requested font stack has to be a part of the used style.
* For more information see <a href="https://www.mapbox.com/help/define-font-stack/">the documentation</a>.
@@ -4427,7 +4429,7 @@ public class Expression {
/**
* If set, the text-font argument overrides the font specified by the root layout properties.
* <p>
* "text-font" is required to a literal array.
* "text-font" is required to be a literal array.
* <p>
* The requested font stack has to be a part of the used style.
* For more information see <a href="https://www.mapbox.com/help/define-font-stack/">the documentation</a>.
@@ -4439,6 +4441,28 @@ public class Expression {
public static FormatOption formatTextFont(@NonNull String[] fontStack) {
return new FormatOption("text-font", literal(fontStack));
}
/**
* If set, the text-color argument overrides the color specified by the root paint properties.
*
* @param expression expression
* @return format option
*/
@NonNull
public static FormatOption formatTextColor(@NonNull Expression expression) {
return new FormatOption("text-color", expression);
}
/**
* If set, the text-color argument overrides the color specified by the root paint properties.
*
* @param color value
* @return format option
*/
@NonNull
public static FormatOption formatTextColor(@ColorInt int color) {
return new FormatOption("text-color", color(color));
}
}
/**

View File

@@ -1,9 +1,12 @@
package com.mapbox.mapboxsdk.style.types;
import android.support.annotation.ColorInt;
import android.support.annotation.Keep;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.mapbox.mapboxsdk.utils.ColorUtils;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@@ -13,33 +16,10 @@ import java.util.Map;
*/
@Keep
public class FormattedSection {
private final String text;
private final Number fontScale;
private final String[] fontStack;
/**
* Creates a formatted section.
*
* @param text displayed string
* @param fontScale scale of the font, setting to null will fall back to style's default settings
* @param fontStack main and fallback fonts that are a part of the style,
* setting null will fall back to style's default settings
*/
public FormattedSection(@NonNull String text, @Nullable Number fontScale, @Nullable String[] fontStack) {
this.text = text;
this.fontScale = fontScale;
this.fontStack = fontStack;
}
/**
* Creates a formatted section.
*
* @param text displayed string
* @param fontScale scale of the font, setting to null will fall back to style's default settings
*/
public FormattedSection(@NonNull String text, @Nullable Number fontScale) {
this(text, fontScale, null);
}
private String text;
private Number fontScale;
private String[] fontStack;
private String textColor;
/**
* Creates a formatted section.
@@ -47,7 +27,60 @@ public class FormattedSection {
* @param text displayed string
*/
public FormattedSection(@NonNull String text) {
this(text, null, null);
this(text, null, null, null);
}
/**
* Creates a formatted section.
*
* @param text displayed string
* @param fontScale scale of the font, setting to null will fall back to style's default settings
* @param fontStack main and fallback fonts that are a part of the style,
* setting null will fall back to style's default settings.
* The requested font stack has to be a part of the used style.
* For more information see
* <a href="https://www.mapbox.com/help/define-font-stack/">the documentation</a>.
* @param textColor text color, setting to null will fall back to style's default settings.
* Value of red, green, blue components must range between 0 and 255,
* an alpha component must range between 0 and 1.
* <p>
* For more information see
* <a href="https://docs.mapbox.com/mapbox-gl-js/style-spec/#types-color">the documentation</a>.
*/
public FormattedSection(@NonNull String text, @Nullable Number fontScale, @Nullable String[] fontStack,
@Nullable String textColor) {
this.text = text;
this.fontScale = fontScale;
this.fontStack = fontStack;
this.textColor = textColor;
}
/**
* Creates a formatted section.
*
* @param text displayed string
* @param fontScale scale of the font, setting to null will fall back to style's default settings
* @param fontStack main and fallback fonts that are a part of the style,
* setting null will fall back to style's default settings
* @deprecated use {@link #FormattedSection(String)} and setters
* or {@link #FormattedSection(String, Number, String[], String)} instead
*/
@Deprecated
public FormattedSection(@NonNull String text, @Nullable Number fontScale, @Nullable String[] fontStack) {
this(text, fontScale, fontStack, null);
}
/**
* Creates a formatted section.
*
* @param text displayed string
* @param fontScale scale of the font, setting to null will fall back to style's default settings
* @deprecated use {@link #FormattedSection(String)} and setters
* or {@link #FormattedSection(String, Number, String[], String)} instead
*/
@Deprecated
public FormattedSection(@NonNull String text, @Nullable Number fontScale) {
this(text, fontScale, null, null);
}
/**
@@ -56,9 +89,12 @@ public class FormattedSection {
* @param text displayed string
* @param fontStack main and fallback fonts that are a part of the style,
* setting null will fall back to style's default settings
* @deprecated use {@link #FormattedSection(String)} and setters
* or {@link #FormattedSection(String, Number, String[], String)} instead
*/
@Deprecated
public FormattedSection(@NonNull String text, @Nullable String[] fontStack) {
this(text, null, fontStack);
this(text, null, fontStack, null);
}
/**
@@ -91,6 +127,67 @@ public class FormattedSection {
return fontStack;
}
/**
* Returns the text color.
*
* @return text color
*/
public String getTextColor() {
return textColor;
}
/**
* Set font scale. Setting to null will fall back to style's default settings.
*
* @param fontScale fontScale
*/
public void setFontScale(@Nullable Number fontScale) {
// called from JNI
this.fontScale = fontScale;
}
/**
* Set main and fallback fonts that are a part of the style. Setting null will fall back to style's default settings.
* <p>
* The requested font stack has to be a part of the used style.
* For more information see <a href="https://www.mapbox.com/help/define-font-stack/">the documentation</a>.
*
* @param fontStack fontStack
*/
public void setFontStack(@Nullable String[] fontStack) {
// called from JNI
this.fontStack = fontStack;
}
/**
* Set text color. Setting to null will fall back to style's default settings.
* Value of red, green, blue components must range between 0 and 255,
* an alpha component must range between 0 and 1.
* <p>
* For more information see
* <a href="https://docs.mapbox.com/mapbox-gl-js/style-spec/#types-color">the documentation</a>.
*
* @param textColor text color
*/
public void setTextColor(@Nullable String textColor) {
this.textColor = textColor;
}
/**
* Set the text color.
*
* @param textColor text color.
*/
public void setTextColor(@ColorInt int textColor) {
this.textColor = ColorUtils.colorToRgbaString(textColor);
}
void setTextColor(@NonNull Object textColor) {
// called from JNI
// because core is returning R, G and B components in range of 0 to 1, we need to convert them to be in 0 to 255.
setTextColor(ColorUtils.colorToRgbaString(ColorUtils.rgbaToColor((String) textColor)));
}
@Override
public boolean equals(Object o) {
if (this == o) {
@@ -109,7 +206,10 @@ public class FormattedSection {
return false;
}
// Probably incorrect - comparing Object[] arrays with Arrays.equals
return Arrays.equals(fontStack, that.fontStack);
if (!Arrays.equals(fontStack, that.fontStack)) {
return false;
}
return textColor != null ? textColor.equals(that.textColor) : that.textColor == null;
}
@Override
@@ -117,6 +217,7 @@ public class FormattedSection {
int result = text != null ? text.hashCode() : 0;
result = 31 * result + (fontScale != null ? fontScale.hashCode() : 0);
result = 31 * result + Arrays.hashCode(fontStack);
result = 31 * result + (textColor != null ? textColor.hashCode() : 0);
return result;
}
@@ -124,6 +225,7 @@ public class FormattedSection {
Map<String, Object> params = new HashMap<>();
params.put("font-scale", fontScale);
params.put("text-font", fontStack);
params.put("text-color", textColor);
return new Object[] {text, params};
}
}