summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/types/FormattedSection.java
blob: 859fcff378c071522f9875b932e1a5c3bda22fdb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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;

/**
 * A component of the {@link Formatted}.
 */
@Keep
public class FormattedSection {
  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.
   *                  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);
  }

  /**
   * Creates a formatted section.
   *
   * @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, null);
  }

  /**
   * Returns the displayed text.
   *
   * @return text
   */
  @NonNull
  public String getText() {
    return text;
  }

  /**
   * Returns displayed text's font scale.
   *
   * @return font scale
   */
  @Nullable
  public Number getFontScale() {
    return fontScale;
  }

  /**
   * Returns the font stack with main and fallback fonts.
   *
   * @return font stack
   */
  @Nullable
  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) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    FormattedSection that = (FormattedSection) o;

    if (text != null ? !text.equals(that.text) : that.text != null) {
      return false;
    }
    if (fontScale != null ? !fontScale.equals(that.fontScale) : that.fontScale != null) {
      return false;
    }
    // Probably incorrect - comparing Object[] arrays with Arrays.equals
    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};
  }

  @Override
  public String toString() {
    return "FormattedSection{"
      + "text='" + text + '\''
      + ", fontScale=" + fontScale
      + ", fontStack=" + Arrays.toString(fontStack)
      + ", textColor='" + textColor + '\''
      + '}';
  }
}