Skip to content

Commit

Permalink
TextLayer supports backgroundPadding as vec4
Browse files Browse the repository at this point in the history
  • Loading branch information
Pessimistress committed Jul 26, 2021
1 parent 8e2167c commit 0de85c7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
7 changes: 5 additions & 2 deletions docs/api-reference/layers/text-layer.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,12 @@ Whether to render background for the text blocks.

##### `backgroundPadding` (Array, optional)

- Default `[0, 0]`
- Default `[0, 0, 0, 0]`

The padding of the background, `[padding_x, padding_y]` in pixels.
The padding of the background, an array of either 2 or 4 numbers.

+ If an array of 2 is supplied, it is interpreted as `[padding_x, padding_y]` in pixels.
+ If an array of 4 is supplied, it is interpreted as `[padding_left, padding_top, padding_right, padding_bottom]` in pixels.

##### `fontFamily` (String, optional)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ uniform float opacity;
uniform float sizeScale;
uniform float sizeMinPixels;
uniform float sizeMaxPixels;
uniform vec2 padding;
uniform vec4 padding;
varying vec4 vFillColor;
varying vec4 vLineColor;
Expand Down Expand Up @@ -50,9 +50,9 @@ void main(void) {
sizeMinPixels, sizeMaxPixels
);
dimensions = instanceRects.zw * sizePixels + padding;
dimensions = instanceRects.zw * sizePixels + padding.xy + padding.zw;
vec2 pixelOffset = (positions * instanceRects.zw + instanceRects.xy) * sizePixels + padding * (positions * 2.0 - 1.0);
vec2 pixelOffset = (positions * instanceRects.zw + instanceRects.xy) * sizePixels + mix(-padding.xy, padding.zw, positions);
pixelOffset = rotate_by_angle(pixelOffset, instanceAngles);
pixelOffset += instancePixelOffsets;
pixelOffset.y *= -1.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const defaultProps = {
sizeMinPixels: 0,
sizeMaxPixels: Number.MAX_SAFE_INTEGER,

padding: {type: 'array', value: [0, 0]},
padding: {type: 'array', value: [0, 0, 0, 0]},

getPosition: {type: 'accessor', value: x => x.position},
getSize: {type: 'accessor', value: 1},
Expand Down Expand Up @@ -101,11 +101,14 @@ export default class TextBackgroundLayer extends Layer {
sizeUnits,
sizeMinPixels,
sizeMaxPixels,
padding,
getLineWidth
} = this.props;
let {padding} = this.props;

const sizeScaleMultiplier = sizeUnits === 'pixels' ? viewport.metersPerPixel : 1;
if (padding.length < 4) {
padding = [padding[0], padding[1], padding[0], padding[1]];
}

this.state.model
.setUniforms(uniforms)
Expand Down
2 changes: 1 addition & 1 deletion modules/layers/src/text-layer/text-layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const defaultProps = {
getBackgroundColor: {type: 'accessor', value: [255, 255, 255, 255]},
getBorderColor: {type: 'accessor', value: DEFAULT_COLOR},
getBorderWidth: {type: 'accessor', value: 0},
backgroundPadding: {type: 'array', value: [0, 0]},
backgroundPadding: {type: 'array', value: [0, 0, 0, 0]},

characterSet: {type: 'object', value: DEFAULT_CHAR_SET},
fontFamily: DEFAULT_FONT_FAMILY,
Expand Down

0 comments on commit 0de85c7

Please sign in to comment.