Skip to content

Commit

Permalink
make text background box width configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
bcamper committed Jul 11, 2020
1 parent 894c8ed commit 6cf2090
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/styles/text/text_canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export default class TextCanvas {

// Computes width and height of text based on current font style
// Includes word wrapping, returns size info for whole text block and individual lines
textSize(style, text, { transform, text_wrap, max_lines, stroke_width = 0, background_color, background_stroke_width = 0, underline_width = 0, supersample }) {
textSize(style, text, { transform, text_wrap, max_lines, stroke_width = 0, background_color, background_stroke_width = 0, background_width, underline_width = 0, supersample }) {
// Check cache first
TextCanvas.cache.text[style] = TextCanvas.cache.text[style] || {};
if (TextCanvas.cache.text[style][text]) {
Expand All @@ -147,7 +147,10 @@ export default class TextCanvas {
const ctx = this.context;
const vertical_buffer = this.vertical_text_buffer * dpr;
const horizontal_buffer = (stroke_width + this.horizontal_text_buffer) * dpr;
const background_size = (background_color || background_stroke_width) ? (this.background_size + background_stroke_width) * dpr : 0;

background_width = background_width != null ? background_width : this.background_size; // apply default background width
const background_size = (background_color || background_stroke_width) ? (background_width + background_stroke_width) * dpr : 0;

const leading = (2 + underline_width + (underline_width ? (stroke_width + 1) : 0)) * dpr; // adjust for underline and text stroke
const line_height = this.px_size + leading; // px_size already in device pixels

Expand Down
1 change: 1 addition & 0 deletions src/styles/text/text_labels.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ export const TextLabels = {
if (draw.font.background) {
draw.font.background.color = StyleParser.createPropertyCache(draw.font.background.color);
draw.font.background.alpha = StyleParser.createPropertyCache(draw.font.background.alpha);
draw.font.background.width = StyleParser.createPropertyCache(draw.font.background.width, StyleParser.parsePositiveNumber);
if (draw.font.background.stroke) {
draw.font.background.stroke.color = StyleParser.createPropertyCache(draw.font.background.stroke.color);
draw.font.background.stroke.alpha = StyleParser.createPropertyCache(draw.font.background.stroke.alpha);
Expand Down
4 changes: 4 additions & 0 deletions src/styles/text/text_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const TextSettings = {
settings.underline_color,
settings.underline_width,
settings.background_color,
settings.background_width,
settings.background_stroke_color,
settings.background_stroke_width,
settings.transform,
Expand Down Expand Up @@ -75,6 +76,9 @@ const TextSettings = {
// Background fill
style.background_color = StyleParser.evalCachedColorPropertyWithAlpha(draw.font.background.color, draw.font.background.alpha, context);
style.background_color = Utils.toCSSColor(style.background_color); // convert to CSS for Canvas
if (style.background_color) {
style.background_width = StyleParser.evalCachedProperty(draw.font.background.width, context);
}

// Background stroke
style.background_stroke_color =
Expand Down

0 comments on commit 6cf2090

Please sign in to comment.