Skip to content

Commit

Permalink
Opt-out of reporting width for chord symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronDavidNewman committed Jan 21, 2021
1 parent b0a18fe commit a3cb4fd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 41 deletions.
66 changes: 31 additions & 35 deletions src/chordsymbol.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
import { Vex } from './vex';
import { Flow } from './tables';
import { Glyph } from './glyph';
import { TextFont } from './textfont';
import { Modifier } from './modifier';
import { PetalumaScriptTextMetrics } from './fonts/petalumascript_textmetrics';
import { RobotoSlabTextMetrics } from './fonts/robotoslab_textmetrics';

// To enable logging for this class. Set `Vex.Flow.ChordSymbol.DEBUG` to `true`.
function L(...args) { if (ChordSymbol.DEBUG) Vex.L('Vex.Flow.ChordSymbol', args); }
Expand All @@ -40,7 +39,6 @@ export class ChordSymbol extends Modifier {
};
}


static get verticalJustify() {
return {
TOP: 1,
Expand Down Expand Up @@ -86,30 +84,12 @@ export class ChordSymbol extends Modifier {
return null;
}

static get textMetricsForEngravingFont() {
if (Vex.Flow.DEFAULT_FONT_STACK[0].name === 'Petaluma') {
return PetalumaScriptTextMetrics;
} else {
return RobotoSlabTextMetrics;
}
}

static getMetricForCharacter(c) {
if (ChordSymbol.NOTEXTFORMAT) {
return null;
}
if (ChordSymbol.textMetricsForEngravingFont.glyphs[c]) {
return ChordSymbol.textMetricsForEngravingFont.glyphs[c];
}
return null;
}

static getYOffsetForText(text) {
getYOffsetForText(text) {
let acc = 0;
let ix = 0;
const resolution = ChordSymbol.textMetricsForEngravingFont.resolution;
const resolution = this.textFont.resolution;
for (ix = 0; ix < text.length; ++ix) {
const metric = ChordSymbol.getMetricForCharacter(text[ix]);
const metric = this.textFont.getMetricForCharacter(text[ix]);

if (metric) {
acc = metric.y < acc ? metric.y : acc;
Expand All @@ -127,13 +107,8 @@ export class ChordSymbol extends Modifier {
return ChordSymbol.chordSymbolMetrics.global.spacing / ChordSymbol.engravingFontResolution;
}

static getWidthForCharacter(c) {
const resolution = ChordSymbol.textMetricsForEngravingFont.resolution;
const metric = ChordSymbol.getMetricForCharacter(c);
if (!metric) {
return 0.65;
}
return metric.advanceWidth / resolution;
getWidthForCharacter(c) {
return this.textFont.getMetricForCharacter(c).advanceWidth / this.textFont.resolution;
}

static getWidthForGlyph(glyph) {
Expand Down Expand Up @@ -275,6 +250,7 @@ export class ChordSymbol extends Modifier {

let width = 0;
let nonSuperWidth = 0;
const reportedWidths = [];

for (let i = 0; i < instances.length; ++i) {
const instance = instances[i];
Expand Down Expand Up @@ -304,8 +280,8 @@ export class ChordSymbol extends Modifier {
symbol.glyph.scale = symbol.glyph.scale * adj;
symbol.width = ChordSymbol.getWidthForGlyph(symbol.glyph) * instance.pointsToPixels * subAdj;
} else if (symbol.symbolType === ChordSymbol.symbolTypes.TEXT) {
symbol.width = symbol.width * instance.pointsToPixels * subAdj;
symbol.yShift += ChordSymbol.getYOffsetForText(symbol.text) * adj;
symbol.width = symbol.width * instance.textFont.pointsToPixels * subAdj;
symbol.yShift += instance.getYOffsetForText(symbol.text) * adj;
}

if (symbol.symbolType === ChordSymbol.symbolTypes.GLYPH &&
Expand Down Expand Up @@ -349,8 +325,15 @@ export class ChordSymbol extends Modifier {
instance.setTextLine(state.text_line + 1);
state.text_line += lineSpaces + 1;
}
if (instance.getReportWidth()) {
reportedWidths.push(width);
} else {
reportedWidths.push(0);
}
}

width = reportedWidths.reduce((a, b) => a + b);

state.left_shift += width / 2;
state.right_shift += width / 2;
return true;
Expand All @@ -369,6 +352,7 @@ export class ChordSymbol extends Modifier {
this.horizontal = ChordSymbol.horizontalJustify.LEFT;
this.vertical = ChordSymbol.verticalJustify.TOP;
this.useKerning = true;
this.reportWidth = true;

let fontFamily = 'Arial';
if (this.musicFont.name === 'Petaluma') {
Expand All @@ -381,12 +365,13 @@ export class ChordSymbol extends Modifier {
size: 12,
weight: '',
};
this.textFont = TextFont.getTextFontFromVexFontData(this.font);
}

// ### pointsToPixels
// The font size is specified in points, convert to 'pixels' in the svg space
get pointsToPixels() {
return (this.font.size / 72) / (1 / 96);
return this.textFont.pointsToPixels;
}

get superscriptOffset() {
Expand All @@ -397,6 +382,15 @@ export class ChordSymbol extends Modifier {
return ChordSymbol.subscriptOffset * this.pointsToPixels;
}

setReportWidth(value) {
this.reportWidth = value;
return this;
}

getReportWidth() {
return this.reportWidth;
}

updateOverBarAdjustments() {
let symIx = 0;
const barIx = this.symbolBlocks.findIndex((symbol) =>
Expand Down Expand Up @@ -512,7 +506,7 @@ export class ChordSymbol extends Modifier {
} else if (symbolType === ChordSymbol.symbolTypes.TEXT) {
let twidth = 0;
for (let i = 0; i < rv.text.length; ++i) {
twidth += ChordSymbol.getWidthForCharacter(rv.text[i]);
twidth += this.getWidthForCharacter(rv.text[i]);
}
rv.width = twidth;
} else if (symbolType === ChordSymbol.symbolTypes.LINE) {
Expand Down Expand Up @@ -610,11 +604,13 @@ export class ChordSymbol extends Modifier {
// Set font family, size, and weight. E.g., `Arial`, `10pt`, `Bold`.
setFont(family, size, weight) {
this.font = { family, size, weight };
this.textFont = TextFont.getTextFontFromVexFontData(this.font);
return this;
}

setFontSize(size) {
this.font.size = size;
this.textFont.setFontSize(size);
return this;
}

Expand Down
23 changes: 17 additions & 6 deletions tests/chordsymbol_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,24 @@ VF.Test.ChordSymbol = (function() {
var chords = [];
chords.push(new VF.ChordSymbol()
.addText('A')
.addGlyphSuperscript('dim'));
.addGlyphSuperscript('dim')
.setReportWidth(false));

chords.push(new VF.ChordSymbol()
.addText('A')
.addGlyphSuperscript('dim')
.setEnableKerning(false));
.setEnableKerning(false)
.setReportWidth(false));

chords.push(new VF.ChordSymbol()
.setHorizontal('left').addText('C')
.addGlyph('halfDiminished', { symbolModifier: VF.ChordSymbol.symbolModifiers.SUPERSCRIPT }));
.addGlyph('halfDiminished', { symbolModifier: VF.ChordSymbol.symbolModifiers.SUPERSCRIPT })
.setReportWidth(false));

chords.push(new VF.ChordSymbol()
.setHorizontal('left').addText('D')
.addGlyph('halfDiminished', { symbolModifier: VF.ChordSymbol.symbolModifiers.SUPERSCRIPT }));
.addGlyph('halfDiminished', { symbolModifier: VF.ChordSymbol.symbolModifiers.SUPERSCRIPT })
.setReportWidth(false));

draw(chords, 10);

Expand Down Expand Up @@ -142,10 +146,13 @@ VF.Test.ChordSymbol = (function() {
}

var chord1 = new VF.ChordSymbol().addText('F7').setHorizontal('left')
.addGlyphOrText('(#11b9)', { symbolModifier: VF.ChordSymbol.symbolModifiers.SUPERSCRIPT });
.addGlyphOrText('(#11b9)', { symbolModifier: VF.ChordSymbol.symbolModifiers.SUPERSCRIPT })
.setReportWidth(false);
var chord2 = new VF.ChordSymbol()
.addText('C').setHorizontal('left')
.addGlyphSuperscript('majorSeventh');
.addGlyphSuperscript('majorSeventh')
.setReportWidth(false);

draw(chord1, chord2, 40);

chord1 = new VF.ChordSymbol().addText('F7')
Expand Down Expand Up @@ -196,22 +203,26 @@ VF.Test.ChordSymbol = (function() {
.addGlyphOrText('b9', { symbolModifier: VF.ChordSymbol.symbolModifiers.SUPERSCRIPT })
.addGlyphOrText('#11', { symbolModifier: VF.ChordSymbol.symbolModifiers.SUBSCRIPT })
.addGlyph('rightParenTall')
.setReportWidth(false)
);
chords.push(new VF.ChordSymbol().setFontSize(12).addText('F7')
.addGlyphOrText('b9', { symbolModifier: VF.ChordSymbol.symbolModifiers.SUPERSCRIPT })
.addGlyphOrText('#11', { symbolModifier: VF.ChordSymbol.symbolModifiers.SUBSCRIPT })
.setReportWidth(false)
);
chords.push(new VF.ChordSymbol().setFontSize(14).addText('F7')
.addGlyph('leftParenTall')
.addGlyphOrText('add 3', { symbolModifier: VF.ChordSymbol.symbolModifiers.SUPERSCRIPT })
.addGlyphOrText('omit 9', { symbolModifier: VF.ChordSymbol.symbolModifiers.SUBSCRIPT })
.addGlyph('rightParenTall')
.setReportWidth(false)
);
chords.push(new VF.ChordSymbol().setFontSize(16).addText('F7')
.addGlyph('leftParenTall')
.addGlyphOrText('b9', { symbolModifier: VF.ChordSymbol.symbolModifiers.SUPERSCRIPT })
.addGlyphOrText('#11', { symbolModifier: VF.ChordSymbol.symbolModifiers.SUBSCRIPT })
.addGlyph('rightParenTall')
.setReportWidth(false)
);
draw(chords, 40);

Expand Down

0 comments on commit a3cb4fd

Please sign in to comment.