Skip to content

Commit

Permalink
get halo size from label size
Browse files Browse the repository at this point in the history
  • Loading branch information
nreese committed Jan 7, 2020
1 parent 6119f23 commit 0cb97cc
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function getVectorStyleLabel(styleName) {
return i18n.translate('xpack.maps.styles.vector.labelBorderColorLabel', {
defaultMessage: 'Label border color',
});
case VECTOR_STYLES.LABEL_BORDER_WIDTH:
case VECTOR_STYLES.LABEL_BORDER_SIZE:
return i18n.translate('xpack.maps.styles.vector.labelBorderWidthLabel', {
defaultMessage: 'Label border width',
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';

import { EuiFormRow, EuiSelect } from '@elastic/eui';
import { LABEL_BORDER_SIZES, VECTOR_STYLES } from '../../vector_style_defaults';
import { getVectorStyleLabel } from '../get_vector_style_label';
import { i18n } from '@kbn/i18n';

const options = [
{
value: LABEL_BORDER_SIZES.NONE,
text: i18n.translate('xpack.maps.styles.labelBorderSize.noneLabel', {
defaultMessage: 'None',
}),
},
{
value: LABEL_BORDER_SIZES.SMALL,
text: i18n.translate('xpack.maps.styles.labelBorderSize.smallLabel', {
defaultMessage: 'Small',
}),
},
{
value: LABEL_BORDER_SIZES.MEDIUM,
text: i18n.translate('xpack.maps.styles.labelBorderSize.mediumLabel', {
defaultMessage: 'Medium',
}),
},
{
value: LABEL_BORDER_SIZES.LARGE,
text: i18n.translate('xpack.maps.styles.labelBorderSize.largeLabel', {
defaultMessage: 'Large',
}),
},
];

export function VectorStyleLabelHaloSizeEditor({ handlePropertyChange, styleProperty }) {
function onChange(e) {
handlePropertyChange(styleProperty.getStyleName(), { size: e.target.value });
}

return (
<EuiFormRow
label={getVectorStyleLabel(VECTOR_STYLES.LABEL_BORDER_SIZE)}
display="columnCompressed"
>
<EuiSelect
options={options}
value={styleProperty.getOptions().size}
onChange={onChange}
aria-label={i18n.translate('xpack.maps.styles.labelBorderSizeSelect.ariaLabel', {
defaultMessage: 'Select label border size',
})}
compressed
/>
</EuiFormRow>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { VectorStyleColorEditor } from './color/vector_style_color_editor';
import { VectorStyleSizeEditor } from './size/vector_style_size_editor';
import { VectorStyleSymbolEditor } from './vector_style_symbol_editor';
import { VectorStyleLabelEditor } from './label/vector_style_label_editor';
import { VectorStyleLabelHaloSizeEditor } from './label/vector_style_label_border_size_editor';
import { VectorStyle } from '../vector_style';
import { OrientationEditor } from './orientation/orientation_editor';
import {
Expand Down Expand Up @@ -280,17 +281,9 @@ export class VectorStyleEditor extends Component {
/>
<EuiSpacer size="m" />

<VectorStyleSizeEditor
onStaticStyleChange={this._onStaticStyleChange}
onDynamicStyleChange={this._onDynamicStyleChange}
styleProperty={this.props.styleProperties[VECTOR_STYLES.LABEL_BORDER_WIDTH]}
fields={this._getOrdinalFields()}
defaultStaticStyleOptions={
this.state.defaultStaticProperties[VECTOR_STYLES.LABEL_BORDER_WIDTH].options
}
defaultDynamicStyleOptions={
this.state.defaultDynamicProperties[VECTOR_STYLES.LABEL_BORDER_WIDTH].options
}
<VectorStyleLabelHaloSizeEditor
handlePropertyChange={this.props.handlePropertyChange}
styleProperty={this.props.styleProperties[VECTOR_STYLES.LABEL_BORDER_SIZE]}
/>
<EuiSpacer size="m" />
</Fragment>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { DynamicStyleProperty } from './dynamic_style_property';
import { getComputedFieldName } from '../style_util';

import {
HALF_LARGE_MAKI_ICON_SIZE,
LARGE_MAKI_ICON_SIZE,
Expand Down Expand Up @@ -76,7 +76,7 @@ export class DynamicSizeProperty extends DynamicStyleProperty {
mbMap.setLayoutProperty(symbolLayerId, 'icon-image', `${symbolId}-${iconPixels}`);

const halfIconPixels = iconPixels / 2;
const targetName = getComputedFieldName(VECTOR_STYLES.ICON_SIZE, this._options.field.name);
const targetName = this.getComputedFieldName();
// Using property state instead of feature-state because layout properties do not support feature-state
mbMap.setLayoutProperty(symbolLayerId, 'icon-size', [
'interpolate',
Expand Down Expand Up @@ -113,15 +113,10 @@ export class DynamicSizeProperty extends DynamicStyleProperty {
mbMap.setLayoutProperty(mbLayerId, 'text-size', lineWidth);
}

syncLabelBorderWidthWithMb(mbLayerId, mbMap) {
const lineWidth = this._getMbSize();
mbMap.setPaintProperty(mbLayerId, 'text-halo-width', lineWidth);
}

_getMbSize() {
if (this._isSizeDynamicConfigComplete(this._options)) {
return this._getMbDataDrivenSize({
targetName: getComputedFieldName(this._styleName, this._options.field.name),
targetName: this.getComputedFieldName(),
minSize: this._options.minSize,
maxSize: this._options.maxSize,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import _ from 'lodash';
import { AbstractStyleProperty } from './style_property';
import { DEFAULT_SIGMA } from '../vector_style_defaults';
import { STYLE_TYPE } from '../../../../../common/constants';
import { scaleValue } from '../style_util';
import { scaleValue, getComputedFieldName } from '../style_util';
import React from 'react';
import { OrdinalLegend } from './components/ordinal_legend';
import { CategoricalLegend } from './components/categorical_legend';
Expand All @@ -31,6 +31,13 @@ export class DynamicStyleProperty extends AbstractStyleProperty {
return this._field;
}

getComputedFieldName() {
if (!this.isComplete()) {
return null;
}
return getComputedFieldName(this._styleName, this.getField().getName());
}

isDynamic() {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import _ from 'lodash';
import { AbstractStyleProperty } from './style_property';
import { DEFAULT_LABEL_SIZE, LABEL_BORDER_SIZES } from '../vector_style_defaults';

const SMALL_SIZE = 1 / 16;
const MEDIUM_SIZE = 1 / 8;
const LARGE_SIZE = 1 / 5; // halo of 1/4 is just a square. Use smaller ratio to preserve contour on letters

function getWidthRatio(size) {
switch (size) {
case LABEL_BORDER_SIZES.LARGE:
return LARGE_SIZE;
case LABEL_BORDER_SIZES.MEDIUM:
return MEDIUM_SIZE;
default:
return SMALL_SIZE;
}
}

export class LabelBorderSizeProperty extends AbstractStyleProperty {
constructor(options, styleName, labelSizeProperty) {
super(options, styleName);
this._labelSizeProperty = labelSizeProperty;
}

syncLabelBorderSizeWithMb(mbLayerId, mbMap) {
const widthRatio = getWidthRatio(this.getOptions().size);

if (this.getOptions().size === LABEL_BORDER_SIZES.NONE) {
mbMap.setPaintProperty(mbLayerId, 'text-halo-width', 0);
} else if (this._labelSizeProperty.isDynamic() && this._labelSizeProperty.isComplete()) {
const labelSizeKey = this._labelSizeProperty.getComputedFieldName();
mbMap.setPaintProperty(mbLayerId, 'text-halo-width', [
'max',
['*', ['coalesce', ['get', labelSizeKey], 0], widthRatio],
1,
]);
} else {
const labelSize = _.get(this._labelSizeProperty.getOptions(), 'size', DEFAULT_LABEL_SIZE);
const labelBorderSize = Math.max(labelSize * widthRatio, 1);
mbMap.setPaintProperty(mbLayerId, 'text-halo-width', labelBorderSize);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,4 @@ export class StaticSizeProperty extends StaticStyleProperty {
syncLabelSizeWithMb(mbLayerId, mbMap) {
mbMap.setLayoutProperty(mbLayerId, 'text-size', this._options.size);
}

syncLabelBorderWidthWithMb(mbLayerId, mbMap) {
mbMap.setPaintProperty(mbLayerId, 'text-halo-width', this._options.size);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { StaticOrientationProperty } from './properties/static_orientation_prope
import { DynamicOrientationProperty } from './properties/dynamic_orientation_property';
import { StaticTextProperty } from './properties/static_text_property';
import { DynamicTextProperty } from './properties/dynamic_text_property';
import { LabelBorderSizeProperty } from './properties/label_border_size_property';
import { extractColorFromStyleProperty } from './components/legend/extract_color_from_style_property';

const POINTS = [GEO_JSON_TYPE.POINT, GEO_JSON_TYPE.MULTI_POINT];
Expand Down Expand Up @@ -100,14 +101,15 @@ export class VectorStyle extends AbstractStyle {
this._descriptor.properties[VECTOR_STYLES.LABEL_COLOR],
VECTOR_STYLES.LABEL_COLOR
);
this._labelBorderWidthStyleProperty = this._makeSizeProperty(
this._descriptor.properties[VECTOR_STYLES.LABEL_BORDER_WIDTH],
VECTOR_STYLES.LABEL_BORDER_WIDTH
);
this._labelBorderColorStyleProperty = this._makeColorProperty(
this._descriptor.properties[VECTOR_STYLES.LABEL_BORDER_COLOR],
VECTOR_STYLES.LABEL_BORDER_COLOR
);
this._labelBorderSizeStyleProperty = new LabelBorderSizeProperty(
this._descriptor.properties[VECTOR_STYLES.LABEL_BORDER_SIZE],
VECTOR_STYLES.LABEL_BORDER_SIZE,
this._labelSizeStyleProperty
);
}

_getAllStyleProperties() {
Expand All @@ -120,8 +122,8 @@ export class VectorStyle extends AbstractStyle {
this._labelStyleProperty,
this._labelSizeStyleProperty,
this._labelColorStyleProperty,
this._labelBorderWidthStyleProperty,
this._labelBorderColorStyleProperty,
this._labelBorderSizeStyleProperty,
];
}

Expand Down Expand Up @@ -548,7 +550,7 @@ export class VectorStyle extends AbstractStyle {
this._labelStyleProperty.syncTextFieldWithMb(textLayerId, mbMap);
this._labelColorStyleProperty.syncLabelColorWithMb(textLayerId, mbMap, alpha);
this._labelSizeStyleProperty.syncLabelSizeWithMb(textLayerId, mbMap);
this._labelBorderWidthStyleProperty.syncLabelBorderWidthWithMb(textLayerId, mbMap);
this._labelBorderSizeStyleProperty.syncLabelBorderSizeWithMb(textLayerId, mbMap);
this._labelBorderColorStyleProperty.syncLabelBorderColorWithMb(textLayerId, mbMap);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ export const MAX_SIZE = 64;
export const DEFAULT_MIN_SIZE = 4;
export const DEFAULT_MAX_SIZE = 32;
export const DEFAULT_SIGMA = 3;
export const DEFAULT_LABEL_SIZE = 14;

export const LABEL_BORDER_SIZES = {
NONE: 'NONE',
SMALL: 'SMALL',
MEDIUM: 'MEDIUM',
LARGE: 'LARGE',
};

export const VECTOR_STYLES = {
SYMBOL: 'symbol',
Expand All @@ -28,7 +36,7 @@ export const VECTOR_STYLES = {
LABEL_COLOR: 'labelColor',
LABEL_SIZE: 'labelSize',
LABEL_BORDER_COLOR: 'labelBorderColor',
LABEL_BORDER_WIDTH: 'labelBorderWidth',
LABEL_BORDER_SIZE: 'labelBorderSize',
};

export const LINE_STYLES = [VECTOR_STYLES.LINE_COLOR, VECTOR_STYLES.LINE_WIDTH];
Expand All @@ -47,6 +55,11 @@ export function getDefaultProperties(mapColors = []) {
symbolId: DEFAULT_ICON,
},
},
[VECTOR_STYLES.LABEL_BORDER_SIZE]: {
options: {
size: LABEL_BORDER_SIZES.SMALL,
},
},
};
}

Expand Down Expand Up @@ -105,7 +118,7 @@ export function getDefaultStaticProperties(mapColors = []) {
[VECTOR_STYLES.LABEL_SIZE]: {
type: VectorStyle.STYLE_TYPE.STATIC,
options: {
size: 14,
size: DEFAULT_LABEL_SIZE,
},
},
[VECTOR_STYLES.LABEL_BORDER_COLOR]: {
Expand All @@ -114,12 +127,6 @@ export function getDefaultStaticProperties(mapColors = []) {
color: isDarkMode ? '#000000' : '#FFFFFF',
},
},
[VECTOR_STYLES.LABEL_BORDER_WIDTH]: {
type: VectorStyle.STYLE_TYPE.STATIC,
options: {
size: 2,
},
},
};
}

Expand Down Expand Up @@ -221,17 +228,5 @@ export function getDefaultDynamicProperties() {
},
},
},
[VECTOR_STYLES.LABEL_BORDER_WIDTH]: {
type: VectorStyle.STYLE_TYPE.DYNAMIC,
options: {
minSize: 1,
maxSize: 10,
field: undefined,
fieldMetaOptions: {
isEnabled: true,
sigma: DEFAULT_SIGMA,
},
},
},
};
}

0 comments on commit 0cb97cc

Please sign in to comment.