Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] text-color option for formatted sections
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasPaczos committed Mar 15, 2019
1 parent 69e1115 commit f1ac3f2
Show file tree
Hide file tree
Showing 8 changed files with 376 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ public void addImage(@NonNull String name, @NonNull Drawable drawable) {
*/
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));
}

/**
Expand All @@ -346,7 +346,7 @@ public void addImages(@NonNull HashMap<String, Bitmap> images) {
*/
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));
}

/**
Expand Down Expand Up @@ -905,11 +905,9 @@ class LayerAtWrapper extends LayerWrapper {
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
Expand All @@ -919,10 +917,12 @@ protected List<Image> doInBackground(Builder.ImageWrapper... params) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2843,7 +2843,8 @@ public static Expression floor(@NonNull Number number) {
* 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))))
* );
* }
Expand Down Expand Up @@ -3626,7 +3627,8 @@ public static Expression step(@NonNull Number input, @NonNull Expression default
* @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));
}

Expand Down Expand Up @@ -4411,7 +4413,7 @@ public static FormatOption formatFontScale(double scale) {
/**
* 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>.
Expand All @@ -4427,7 +4429,7 @@ public static FormatOption formatTextFont(@NonNull Expression 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>.
Expand All @@ -4439,6 +4441,28 @@ public static FormatOption formatTextFont(@NonNull Expression 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));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,41 +16,71 @@
*/
@Keep
public class FormattedSection {
private final String text;
private final Number fontScale;
private final String[] fontStack;
private String text;
private Number fontScale;
private String[] fontStack;
private String textColor;

/**
* Creates a formatted section.
*
* @param text displayed string
*/
public FormattedSection(@NonNull String text) {
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
* 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) {
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
*/
public FormattedSection(@NonNull String text, @Nullable Number fontScale) {
this(text, fontScale, null);
@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 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
*/
public FormattedSection(@NonNull String text) {
this(text, null, null);
@Deprecated
public FormattedSection(@NonNull String text, @Nullable Number fontScale) {
this(text, fontScale, null, null);
}

/**
Expand All @@ -56,9 +89,12 @@ public FormattedSection(@NonNull String text) {
* @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);
}

/**
Expand Down Expand Up @@ -91,6 +127,67 @@ public String[] getFontStack() {
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) {
Expand All @@ -109,21 +206,26 @@ public boolean equals(Object o) {
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
public int hashCode() {
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;
}

Object[] toArray() {
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};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Map;

import static com.mapbox.mapboxsdk.style.expressions.Expression.FormatOption.formatFontScale;
import static com.mapbox.mapboxsdk.style.expressions.Expression.FormatOption.formatTextColor;
import static com.mapbox.mapboxsdk.style.expressions.Expression.FormatOption.formatTextFont;
import static com.mapbox.mapboxsdk.style.expressions.Expression.abs;
import static com.mapbox.mapboxsdk.style.expressions.Expression.acos;
Expand Down Expand Up @@ -1416,12 +1417,17 @@ public void testFormatSingleArgument() {
{
put("font-scale", 1.5f);
put("text-font", new Object[] {"literal", new String[] {"awesome"}});
put("text-color", new Object[] {"rgb", 255f, 0f, 0f});
}
}
};
Object[] actual = format(
formatEntry(
literal("test"), formatFontScale(literal(1.5)), formatTextFont(literal(new String[] {"awesome"})))
literal("test"),
formatFontScale(literal(1.5)),
formatTextFont(literal(new String[] {"awesome"})),
formatTextColor(rgb(255, 0, 0))
)
).toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}
Expand All @@ -1448,19 +1454,31 @@ public void testFormatMultipleArgument() {
}
},
"test4",
new TestableExpressionHashMap() {
{
put("text-color", new Object[] {"rgb", 255f, 0f, 0f});
}
},
"test5",
new TestableExpressionHashMap() {
{
put("font-scale", 1.5f);
put("text-font", new Object[] {"literal", new String[] {"awesome"}});
put("text-color", new Object[] {"rgb", 255f, 0f, 0f});
}
}
};
Object[] actual = format(
formatEntry(literal("test"), formatTextFont(new String[] {"awesome"})),
formatEntry("test2", formatFontScale(1.5)),
formatEntry(literal("test3")),
formatEntry(literal("test4"), formatTextColor(rgb(255, 0, 0))),
formatEntry(
literal("test4"), formatFontScale(literal(1.5)), formatTextFont(new String[] {"awesome"}))
literal("test5"),
formatFontScale(literal(1.5)),
formatTextFont(new String[] {"awesome"}),
formatTextColor(rgb(255, 0, 0))
)
).toArray();
assertTrue("expression should match", Arrays.deepEquals(expected, actual));
}
Expand Down
Loading

0 comments on commit f1ac3f2

Please sign in to comment.