Skip to content

Commit

Permalink
add coalesce expression, evaluate text anchor, allow simpler expressi…
Browse files Browse the repository at this point in the history
…ons for layer selector
  • Loading branch information
mvarendorff committed Sep 6, 2021
1 parent d2bc8ee commit 55539d5
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 9 deletions.
17 changes: 17 additions & 0 deletions lib/src/expressions/coalesce_expression.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'expression.dart';

class CoalesceExpression<T> extends Expression<T> {
final Iterable<Expression<T>> _delegates;

CoalesceExpression(this._delegates);

@override
T? evaluate(Map<String, dynamic> args) {
for (final delegate in _delegates) {
final result = delegate.evaluate(args);
if (result != null) return result;
}

return null;
}
}
5 changes: 4 additions & 1 deletion lib/src/features/text_renderer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ class TextRenderer {
if (_painter == null) {
return null;
}
final anchor = style.textLayout!.anchor;
final anchorString = style.textLayout!.anchor?.evaluate(
toArgsMap(context, feature),
);
final anchor = LayoutAnchor.fromName(anchorString);
final size = _painter!.size;
switch (anchor) {
case LayoutAnchor.center:
Expand Down
16 changes: 16 additions & 0 deletions lib/src/parsers/coalesce.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:vector_tile_renderer/src/expressions/coalesce_expression.dart';
import 'package:vector_tile_renderer/src/expressions/expression.dart';

import 'parser.dart';
import 'parsers.dart' as Parsers;

class CoalesceParser<T> extends Parser<T> {
@override
Expression<T>? parseSpecial(data) {
if (data is! List) return null;

final delegates =
data.skip(1).map((i) => Parsers.parse<T>(i)).whereType<Expression<T>>();
return CoalesceExpression(delegates);
}
}
6 changes: 4 additions & 2 deletions lib/src/parsers/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:vector_tile_renderer/src/logger.dart';
import 'package:vector_tile_renderer/src/parsers/match.dart';

import 'case.dart';
import 'coalesce.dart';
import 'step.dart';

abstract class Parser<T> {
Expand Down Expand Up @@ -39,11 +40,12 @@ abstract class Parser<T> {
return null;
}

if (data[0] == 'case') return CaseParser<T>().parseSpecial(data);
if (data[0] == 'coalesce') return CoalesceParser<T>().parseSpecial(data);
if (data[0] == 'get') return ArgumentExpression<T>(data[1]);
if (data[0] == 'zoom') return ArgumentExpression<T>('zoom');
if (data[0] == 'match') return MatchParser<T>().parseSpecial(data);
if (data[0] == 'step') return StepParser<T>().parseSpecial(data);
if (data[0] == 'case') return CaseParser<T>().parseSpecial(data);
if (data[0] == 'zoom') return ArgumentExpression<T>('zoom');

return null;
}
Expand Down
11 changes: 7 additions & 4 deletions lib/src/themes/selector_factory.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'selector.dart';

import '../logger.dart';
import 'selector.dart';

class SelectorFactory {
final Logger logger;
Expand Down Expand Up @@ -65,7 +64,9 @@ class SelectorFactory {
if (filter.length != 3) {
throw Exception('unexpected filter $filter');
}
final propertyName = filter[1];
final propertyNameField = filter[1];
final propertyName =
propertyNameField is List ? propertyNameField.last : propertyNameField;
final propertyValue = filter[2];
return LayerSelector.withProperty(propertyName,
values: [propertyValue], negated: negated);
Expand All @@ -85,7 +86,9 @@ class SelectorFactory {

LayerSelector _numericComparisonSelector(List<dynamic> filter, String op) {
ComparisonOperator comparison = _toComparisonOperator(op);
final propertyName = filter[1] as String;
final propertyNameField = filter[1];
final propertyName =
propertyNameField is List ? propertyNameField.last : propertyNameField;
final propertyValue = filter[2] as num;
return LayerSelector.comparingProperty(
propertyName, comparison, propertyValue);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/themes/style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class LayoutAnchor {

class TextLayout {
final LayoutPlacement placement;
final LayoutAnchor anchor;
final Expression<String>? anchor;
final Expression<String> text;
final Expression<bool> keepUpright;
final Expression<double> textSize;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/themes/theme_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class ThemeReader {
final textLetterSpacing = parse<double>(layout?['text-letter-spacing']);
final placement =
LayoutPlacement.fromName(layout?['symbol-placement'] as String?);
final anchor = LayoutAnchor.fromName(layout?['text-anchor'] as String?);
final anchor = parse<String>(layout?['text-anchor']);
final textFunction = parse<String>(layout?['text-field'])!;
final keepUpright =
parse<bool>(layout?['text-keep-upright']) ?? ValueExpression(true);
Expand Down

0 comments on commit 55539d5

Please sign in to comment.