Skip to content

add font weight, style & variant to label-fonts and tick-fonts #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 66 additions & 11 deletions axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ function Axes(gl) {

this.tickEnable = [ true, true, true ]
this.tickFont = [ 'sans-serif', 'sans-serif', 'sans-serif' ]
this.tickFontStyle = [ 'normal', 'normal', 'normal' ]
this.tickFontWeight = [ 'normal', 'normal', 'normal' ]
this.tickFontVariant = [ 'normal', 'normal', 'normal' ]
this.tickSize = [ 12, 12, 12 ]
this.tickAngle = [ 0, 0, 0 ]
this.tickAlign = [ 'auto', 'auto', 'auto' ]
Expand All @@ -47,7 +50,10 @@ function Axes(gl) {

this.labels = [ 'x', 'y', 'z' ]
this.labelEnable = [ true, true, true ]
this.labelFont = 'sans-serif'
this.labelFont = [ 'sans-serif', 'sans-serif', 'sans-serif' ]
this.labelFontStyle = [ 'normal', 'normal', 'normal' ]
this.labelFontWeight = [ 'normal', 'normal', 'normal' ]
this.labelFontVariant = [ 'normal', 'normal', 'normal' ]
this.labelSize = [ 20, 20, 20 ]
this.labelAngle = [ 0, 0, 0 ]
this.labelAlign = [ 'auto', 'auto', 'auto' ]
Expand Down Expand Up @@ -184,19 +190,26 @@ i_loop:

//Parse tick properties
BOOLEAN('tickEnable')
if(STRING('tickFont')) {
ticksUpdate = true //If font changes, must rebuild vbo
}

//If font changes, must rebuild vbo
if(STRING('tickFont')) ticksUpdate = true
if(STRING('tickFontStyle')) ticksUpdate = true
if(STRING('tickFontWeight')) ticksUpdate = true
if(STRING('tickFontVariant')) ticksUpdate = true

NUMBER('tickSize')
NUMBER('tickAngle')
NUMBER('tickPad')
COLOR('tickColor')

//Axis labels
var labelUpdate = STRING('labels')
if(STRING('labelFont')) {
labelUpdate = true
}

if(STRING('labelFont')) labelUpdate = true
if(STRING('labelFontStyle')) labelUpdate = true
if(STRING('labelFontWeight')) labelUpdate = true
if(STRING('labelFontVariant')) labelUpdate = true

BOOLEAN('labelEnable')
NUMBER('labelSize')
NUMBER('labelPad')
Expand Down Expand Up @@ -229,22 +242,64 @@ i_loop:
BOOLEAN('backgroundEnable')
COLOR('backgroundColor')

var labelFontOpts = [
{
family: this.labelFont[0],
style: this.labelFontStyle[0],
weight: this.labelFontWeight[0],
variant: this.labelFontVariant[0],
},
{
family: this.labelFont[1],
style: this.labelFontStyle[1],
weight: this.labelFontWeight[1],
variant: this.labelFontVariant[1],
},
{
family: this.labelFont[2],
style: this.labelFontStyle[2],
weight: this.labelFontWeight[2],
variant: this.labelFontVariant[2],
}
]

var tickFontOpts = [
{
family: this.tickFont[0],
style: this.tickFontStyle[0],
weight: this.tickFontWeight[0],
variant: this.tickFontVariant[0],
},
{
family: this.tickFont[1],
style: this.tickFontStyle[1],
weight: this.tickFontWeight[1],
variant: this.tickFontVariant[1],
},
{
family: this.tickFont[2],
style: this.tickFontStyle[2],
weight: this.tickFontWeight[2],
variant: this.tickFontVariant[2],
}
]

//Update text if necessary
if(!this._text) {
this._text = createText(
this.gl,
this.bounds,
this.labels,
this.labelFont,
labelFontOpts,
this.ticks,
this.tickFont)
tickFontOpts)
} else if(this._text && (labelUpdate || ticksUpdate)) {
this._text.update(
this.bounds,
this.labels,
this.labelFont,
labelFontOpts,
this.ticks,
this.tickFont)
tickFontOpts)
}

//Update lines if necessary
Expand Down
26 changes: 22 additions & 4 deletions lib/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,25 @@ proto.update = function(bounds, labels, labelFont, ticks, tickFont) {
var data = []

function addItem(t, text, font, size, lineSpacing, styletags) {
var fontcache = __TEXT_CACHE[font]
var fontKey = [
font.style,
font.weight,
font.variant,
font.family
].join('_')

var fontcache = __TEXT_CACHE[fontKey]
if(!fontcache) {
fontcache = __TEXT_CACHE[font] = {}
fontcache = __TEXT_CACHE[fontKey] = {}
}
var mesh = fontcache[text]
if(!mesh) {
mesh = fontcache[text] = tryVectorizeText(text, {
triangles: true,
font: font,
font: font.family,
fontStyle: font.style,
fontWeight: font.weight,
fontVariant: font.variant,
textAlign: 'center',
textBaseline: 'middle',
lineSpacing: lineSpacing,
Expand Down Expand Up @@ -118,10 +128,18 @@ proto.update = function(bounds, labels, labelFont, ticks, tickFont) {
if(!ticks[d][i].text) {
continue
}

var font = {
family: ticks[d][i].font || tickFont[d].family,
style: tickFont[d].fontStyle || tickFont[d].style,
weight: tickFont[d].fontWeight || tickFont[d].weight,
variant: tickFont[d].fontVariant || tickFont[d].variant,
}

addItem(
ticks[d][i].x,
ticks[d][i].text,
ticks[d][i].font || tickFont,
font,
ticks[d][i].fontSize || 12,
lineSpacing,
styletags
Expand Down