Skip to content

Commit

Permalink
add lineHeight in ems and letterSpacing in px to text symbolizer [#53,
Browse files Browse the repository at this point in the history
  • Loading branch information
bdon committed Sep 23, 2021
1 parent ba2c159 commit 17e4a3c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/default_style/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ export const labelRules = (params:DefaultStyleParams,shade:string,language1:stri
symbolizer: languageStack(new CenteredTextSymbolizer({
label_props:nametags,
fill:params.countryLabel,
lineHeight:1.5,
font:(z:number,f:Feature) => {
if (z < 6) return "200 14px sans-serif"
return "200 20px sans-serif"
Expand Down
30 changes: 26 additions & 4 deletions src/symbolizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ export class TextSymbolizer implements LabelSymbolizer {
fill: ColorAttr
stroke:ColorAttr
width: NumberAttr
lineHeight: NumberAttr // in ems
letterSpacing: NumberAttr // in px
maxLineCodeUnits: number

constructor(options:any) {
Expand All @@ -434,6 +436,8 @@ export class TextSymbolizer implements LabelSymbolizer {
this.fill = new ColorAttr(options.fill)
this.stroke = new ColorAttr(options.stroke)
this.width = new NumberAttr(options.width,0)
this.lineHeight = new NumberAttr(options.lineHeight,1)
this.letterSpacing = new NumberAttr(options.letterSpacing,0)
this.maxLineCodeUnits = options.maxLineChars || 15
}

Expand All @@ -443,6 +447,8 @@ export class TextSymbolizer implements LabelSymbolizer {
let font = this.font.get(layout.zoom,feature)
layout.scratch.font = font

let letterSpacing = this.letterSpacing.get(layout.zoom,feature)

// line breaking
let lines = linebreak(property,this.maxLineCodeUnits)
var longestLine
Expand All @@ -455,11 +461,11 @@ export class TextSymbolizer implements LabelSymbolizer {
}

let metrics = layout.scratch.measureText(longestLine)
let width = metrics.width
let width = metrics.width + (letterSpacing * (longestLineLen-1))

let ascent = metrics.actualBoundingBoxAscent
let descent = metrics.actualBoundingBoxDescent
let lineHeight = ascent + descent
let lineHeight = (ascent + descent) * this.lineHeight.get(layout.zoom,feature)

let a = new Point(geom[0][0].x,geom[0][0].y)
let bbox = {
Expand All @@ -482,9 +488,25 @@ export class TextSymbolizer implements LabelSymbolizer {
if (textStrokeWidth) {
ctx.lineWidth = textStrokeWidth * 2 // centered stroke
ctx.strokeStyle = this.stroke.get(layout.zoom,feature)
ctx.strokeText(line,0,y)
if (letterSpacing > 0) {
var xPos = 0
for (var letter of line) {
ctx.strokeText(letter,xPos,y)
xPos += ctx.measureText(letter).width + letterSpacing
}
} else {
ctx.strokeText(line,0,y)
}
}
if (letterSpacing > 0) {
var xPos = 0
for (var letter of line) {
ctx.fillText(letter,xPos,y)
xPos += ctx.measureText(letter).width + letterSpacing
}
} else {
ctx.fillText(line,0,y)
}
ctx.fillText(line,0,y)
y += lineHeight
}
}
Expand Down

0 comments on commit 17e4a3c

Please sign in to comment.