Skip to content
Snippets Groups Projects
Commit a162f6e4 authored by Alexander Rose's avatar Alexander Rose
Browse files

fixed text background positioning

parent f64e297e
No related branches found
No related tags found
No related merge requests found
...@@ -40,38 +40,40 @@ export namespace TextBuilder { ...@@ -40,38 +40,40 @@ export namespace TextBuilder {
return { return {
add: (str: string, x: number, y: number, z: number, depth: number, group: number) => { add: (str: string, x: number, y: number, z: number, depth: number, group: number) => {
let xadvance = 0 let bWidth = 0
const nChar = str.length const nChar = str.length
// calculate width // calculate width
for (let iChar = 0; iChar < nChar; ++iChar) { for (let iChar = 0; iChar < nChar; ++iChar) {
const c = fontAtlas.get(str[iChar]) const c = fontAtlas.get(str[iChar])
xadvance += c.nw - 2 * outline bWidth += c.nw - 2 * outline
} }
const bHeight = 1 / 1.25
// attachment // attachment
let yShift: number, xShift: number let yShift: number, xShift: number
if (attachment.startsWith('top')) { if (attachment.startsWith('top')) {
yShift = 1 / 1.25 yShift = bHeight
} else if (attachment.startsWith('middle')) { } else if (attachment.startsWith('middle')) {
yShift = 1 / 2.5 yShift = bHeight / 2
} else { } else {
yShift = 0 // "bottom" yShift = 0 // "bottom"
} }
if (attachment.endsWith('right')) { if (attachment.endsWith('right')) {
xShift = xadvance xShift = bWidth
} else if (attachment.endsWith('center')) { } else if (attachment.endsWith('center')) {
xShift = xadvance / 2 xShift = bWidth / 2
} else { } else {
xShift = 0 // "left" xShift = 0 // "left"
} }
// background // background
if (background) { if (background) {
ChunkedArray.add2(mappings, -xadvance + xShift - margin - 0.1, yShift + margin) // top left ChunkedArray.add2(mappings, -xShift - margin - 0.1, bHeight - yShift + margin) // top left
ChunkedArray.add2(mappings, -xadvance + xShift - margin - 0.1, -yShift - margin) // bottom left ChunkedArray.add2(mappings, -xShift - margin - 0.1, -yShift - margin) // bottom left
ChunkedArray.add2(mappings, xadvance - xShift + margin + 0.1, yShift + margin) // top right ChunkedArray.add2(mappings, bWidth - xShift + margin + 0.1, bHeight - yShift + margin) // top right
ChunkedArray.add2(mappings, xadvance - xShift + margin + 0.1, -yShift - margin) // bottom right ChunkedArray.add2(mappings, bWidth - xShift + margin + 0.1, -yShift - margin) // bottom right
const offset = centers.elementCount const offset = centers.elementCount
for (let i = 0; i < 4; ++i) { for (let i = 0; i < 4; ++i) {
...@@ -86,7 +88,7 @@ export namespace TextBuilder { ...@@ -86,7 +88,7 @@ export namespace TextBuilder {
xShift += outline xShift += outline
yShift += outline yShift += outline
xadvance = 0 let xadvance = 0
for (let iChar = 0; iChar < nChar; ++iChar) { for (let iChar = 0; iChar < nChar; ++iChar) {
const c = fontAtlas.get(str[iChar]) const c = fontAtlas.get(str[iChar])
......
...@@ -33,7 +33,8 @@ function textRepr() { ...@@ -33,7 +33,8 @@ function textRepr() {
attachment: 'middle-center', attachment: 'middle-center',
fontQuality: 3, fontQuality: 3,
fontWeight: 'normal', fontWeight: 'normal',
borderWidth: 0.3 borderWidth: 0.3,
background: true
} }
const textBuilder = TextBuilder.create(props, 1, 1) const textBuilder = TextBuilder.create(props, 1, 1)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment