Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom font stack in symbol options #359

Merged
merged 1 commit into from
Oct 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions android/src/main/java/com/mapbox/mapboxgl/Convert.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ static void interpretSymbolOptions(Object o, SymbolOptionsSink sink) {
if (iconAnchor != null) {
sink.setIconAnchor(toString(iconAnchor));
}
final ArrayList fontNames = (ArrayList) data.get("fontNames");
if (fontNames != null) {
sink.setFontNames((String[]) fontNames.toArray(new String[0]));
}
final Object textField = data.get("textField");
if (textField != null) {
sink.setTextField(toString(textField));
Expand Down
3 changes: 3 additions & 0 deletions android/src/main/java/com/mapbox/mapboxgl/SymbolBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public void setIconAnchor(String iconAnchor) {
symbolOptions.withIconAnchor(iconAnchor);
}

@Override
public void setFontNames(String[] fontNames) { symbolOptions.withTextFont(fontNames); }

@Override
public void setTextField(String textField) {
symbolOptions.withTextField(textField);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public void setIconAnchor(String iconAnchor) {
symbol.setIconAnchor(iconAnchor);
}

@Override
public void setFontNames(String[] fontNames) { symbol.setTextFont(fontNames); }

@Override
public void setTextField(String textField) {
symbol.setTextField(textField);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ interface SymbolOptionsSink {

void setIconAnchor(String iconAnchor);

void setFontNames(String[] fontNames);

void setTextField(String textField);

void setTextSize(float textSize);
Expand Down
31 changes: 25 additions & 6 deletions example/lib/place_symbol.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,28 @@ class PlaceSymbolBodyState extends State<PlaceSymbolBody> {
}

SymbolOptions _getSymbolOptions(String iconImage, int symbolCount){
return SymbolOptions(
geometry: LatLng(
center.latitude + sin(symbolCount * pi / 6.0) / 20.0,
center.longitude + cos(symbolCount * pi / 6.0) / 20.0,
),
iconImage: iconImage,
LatLng geometry = LatLng(
center.latitude + sin(symbolCount * pi / 6.0) / 20.0,
center.longitude + cos(symbolCount * pi / 6.0) / 20.0,
);
return iconImage == 'customFont'
? SymbolOptions(
geometry: geometry,
iconImage: 'airport-15',
fontNames: ['DIN Offc Pro Bold', 'Arial Unicode MS Regular'],
textField: 'Airport',
textSize: 12.5,
textOffset: Offset(0, 0.8),
textAnchor: 'top',
textColor: '#000000',
textHaloBlur: 1,
textHaloColor: '#ffffff',
textHaloWidth: 0.8,
)
: SymbolOptions(
geometry: geometry,
iconImage: iconImage,
);
}

Future<void> _addAll(String iconImage) async {
Expand Down Expand Up @@ -334,6 +349,10 @@ class PlaceSymbolBodyState extends State<PlaceSymbolBody> {
onPressed: () =>
(_symbolCount == 12) ? null : _add("networkImage"), //networkImage added to the style in _onStyleLoaded
),
FlatButton(
child: const Text('add (custom font)'),
onPressed: () => (_symbolCount == 12) ? null : _add("customFont"),
)
],
),
Column(
Expand Down
3 changes: 3 additions & 0 deletions ios/Classes/Convert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ class Convert {
if let iconHaloBlur = options["iconHaloBlur"] as? CGFloat {
delegate.iconHaloBlur = iconHaloBlur
}
if let fontNames = options["fontNames"] as? [String] {
delegate.fontNames = fontNames
}
if let textField = options["textField"] as? String {
delegate.text = textField
}
Expand Down
4 changes: 4 additions & 0 deletions mapbox_gl_platform_interface/lib/src/symbol.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class SymbolOptions {
this.iconRotate,
this.iconOffset,
this.iconAnchor,
this.fontNames,
this.textField,
this.textSize,
this.textMaxWidth,
Expand Down Expand Up @@ -78,6 +79,7 @@ class SymbolOptions {
final double iconRotate;
final Offset iconOffset;
final String iconAnchor;
final List<String> fontNames;
final String textField;
final double textSize;
final double textMaxWidth;
Expand Down Expand Up @@ -113,6 +115,7 @@ class SymbolOptions {
iconRotate: changes.iconRotate ?? iconRotate,
iconOffset: changes.iconOffset ?? iconOffset,
iconAnchor: changes.iconAnchor ?? iconAnchor,
fontNames: changes.fontNames ?? fontNames,
textField: changes.textField ?? textField,
textSize: changes.textSize ?? textSize,
textMaxWidth: changes.textMaxWidth ?? textMaxWidth,
Expand Down Expand Up @@ -152,6 +155,7 @@ class SymbolOptions {
addIfPresent('iconRotate', iconRotate);
addIfPresent('iconOffset', _offsetToJson(iconOffset));
addIfPresent('iconAnchor', iconAnchor);
addIfPresent('fontNames', fontNames);
addIfPresent('textField', textField);
addIfPresent('textSize', textSize);
addIfPresent('textMaxWidth', textMaxWidth);
Expand Down