Skip to content

Commit

Permalink
Merge pull request #1095 from ronyeh/tabnoteposition
Browse files Browse the repository at this point in the history
Small fixes to TabNoteStruct and Unicode
  • Loading branch information
0xfe authored Aug 2, 2021
2 parents 4349c80 + a4a528d commit 37e70ad
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 40 deletions.
36 changes: 19 additions & 17 deletions src/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

/* eslint-disable key-spacing */

import { RuntimeError } from './util';
import { Fraction } from './fraction';
import { Glyph } from './glyph';
import { ArticulationStruct } from './articulation';

import { Fonts } from './font';
import { Fraction } from './fraction';
import { Glyph } from './glyph';
import { RuntimeError } from './util';

// Custom note heads
const customNoteHeads: Record<string, { code: string }> = {
Expand Down Expand Up @@ -539,15 +538,15 @@ export const Tables = {
let width = 0;
let shift_y = 0;

if (fret.toString().toUpperCase() === 'X') {
if (fret.toUpperCase() === 'X') {
const glyphMetrics = new Glyph('accidentalDoubleSharp', Tables.DEFAULT_TABLATURE_FONT_SCALE).getMetrics();
glyph = 'accidentalDoubleSharp';
if (glyphMetrics.width == undefined || glyphMetrics.height == undefined)
throw new RuntimeError('InvalidMetrics', 'Width and height required');
width = glyphMetrics.width;
shift_y = -glyphMetrics.height / 2;
} else {
width = Tables.textWidth(fret.toString());
width = Tables.textWidth(fret);
}

return {
Expand Down Expand Up @@ -730,17 +729,20 @@ export const Tables = {
},

unicode: {
// Unicode accidentals
sharp: String.fromCharCode(parseInt('266F', 16)),
flat: String.fromCharCode(parseInt('266D', 16)),
natural: String.fromCharCode(parseInt('266E', 16)),
// Major Chord
triangle: String.fromCharCode(parseInt('25B3', 16)),
// half-diminished
'o-with-slash': String.fromCharCode(parseInt('00F8', 16)),
// Diminished
degrees: String.fromCharCode(parseInt('00B0', 16)),
circle: String.fromCharCode(parseInt('25CB', 16)),
// ♯ accidental sharp
sharp: String.fromCharCode(0x266f),
// ♭ accidental flat
flat: String.fromCharCode(0x266d),
// ♮ accidental natural
natural: String.fromCharCode(0x266e),
// △ major seventh
triangle: String.fromCharCode(0x25b3),
// ø half-diminished
'o-with-slash': String.fromCharCode(0x00f8),
// ° diminished
degrees: String.fromCharCode(0x00b0),
// ○ diminished
circle: String.fromCharCode(0x25cb),
},

// Used to convert duration aliases to the number based duration.
Expand Down
31 changes: 20 additions & 11 deletions src/tabnote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,28 @@
//
// See `tests/tabnote_tests.js` for usage examples

import { RuntimeError } from './util';
import { Flow } from './flow';
import { Modifier } from './modifier';
import { Stem } from './stem';
import { StemmableNote } from './stemmablenote';
import { Dot } from './dot';
import { Flow } from './flow';
import { Glyph, GlyphProps } from './glyph';
import { Modifier } from './modifier';
import { ModifierContext } from './modifiercontext';
import { Stave } from './stave';
import { StaveNoteStruct } from './stavenote';
import { ModifierContext } from './modifiercontext';
import { Stem } from './stem';
import { StemmableNote } from './stemmablenote';
import { RuntimeError } from './util';

export interface TabNotePosition {
// For example, on a six stringed instrument, `str` ranges from 1 to 6.
str: number;

// fret: 'X' indicates an unused/muted string.
// fret: 3 indicates the third fret.
fret: number | string;
}

export interface TabNoteStruct extends StaveNoteStruct {
positions: { str: string; fret: string }[];
positions: TabNotePosition[];
}
// Gets the unused strings grouped together if consecutive.
//
Expand Down Expand Up @@ -116,7 +125,7 @@ function getPartialStemLines(stem_y: number, unused_strings: number[][], stave:
export class TabNote extends StemmableNote {
protected ghost: boolean;
protected glyphs: GlyphProps[] = [];
protected positions: { str: string; fret: string }[];
protected positions: TabNotePosition[];

static get CATEGORY(): string {
return 'tabnotes';
Expand All @@ -129,8 +138,8 @@ export class TabNote extends StemmableNote {
this.setAttribute('type', 'TabNote');

this.ghost = false; // Renders parenthesis around notes

// Note properties
//
// The fret positions in the note. An array of `{ str: X, fret: X }`
this.positions = tab_struct.positions;

Expand Down Expand Up @@ -230,7 +239,7 @@ export class TabNote extends StemmableNote {
for (let i = 0; i < this.positions.length; ++i) {
let fret = this.positions[i].fret;
if (this.ghost) fret = '(' + fret + ')';
const glyph = Flow.tabToGlyph(fret, this.render_options.scale);
const glyph = Flow.tabToGlyph(fret.toString(), this.render_options.scale);
this.glyphs.push(glyph as GlyphProps);
this.width = Math.max(glyph.getWidth(), this.width);
}
Expand Down Expand Up @@ -280,7 +289,7 @@ export class TabNote extends StemmableNote {
}

// Get the fret positions for the note
getPositions(): { str: string; fret: string }[] {
getPositions(): TabNotePosition[] {
return this.positions;
}

Expand Down
32 changes: 24 additions & 8 deletions src/tabslide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
// This class implements varies types of ties between contiguous notes. The
// ties include: regular ties, hammer ons, pull offs, and slides.

import { RuntimeError } from './util';
import { TabTie } from './tabtie';
import { TabNote } from './tabnote';
import { TabTie } from './tabtie';
import { TieNotes } from './types/common';
import { RuntimeError } from './util';

export class TabSlide extends TabTie {
static get SLIDE_UP(): number {
Expand All @@ -27,26 +27,42 @@ export class TabSlide extends TabTie {
}

/**
* @param notes is a struct that has:
*
* @param notes is a struct of the form:
* {
* first_note: Note,
* last_note: Note,
* first_indices: [n1, n2, n3],
* last_indices: [n1, n2, n3]
* }
* @param notes.first_note the starting note of the slide
* @param notes.last_note the ending note of the slide
* @param notes.first_indices specifies which string + fret positions of the TabNote are used in this slide. zero indexed.
* @param notes.last_indices currently unused. we assume it's the same as first_indices.
*
* @param direction
* @param direction TabSlide.SLIDE_UP or TabSlide.SLIDE_DOWN
*/
constructor(notes: TieNotes, direction?: number) {
super(notes, 'sl.');
this.setAttribute('type', 'TabSlide');

// Determine the direction automatically if it is not provided.
if (!direction) {
const first_fret = (notes.first_note as TabNote).getPositions()[0].fret;
const last_fret = (notes.last_note as TabNote).getPositions()[0].fret;
let first_fret = (notes.first_note as TabNote).getPositions()[0].fret;
if (typeof first_fret === 'string') {
first_fret = parseInt(first_fret, 10);
}
let last_fret = (notes.last_note as TabNote).getPositions()[0].fret;
if (typeof last_fret === 'string') {
last_fret = parseInt(last_fret, 10);
}

direction = parseInt(first_fret, 10) > parseInt(last_fret, 10) ? TabSlide.SLIDE_DOWN : TabSlide.SLIDE_UP;
// If either of the frets are 'X', parseInt() above will return NaN.
// Choose TabSlide.SLIDE_UP by default.
if (isNaN(first_fret) || isNaN(last_fret)) {
direction = TabSlide.SLIDE_UP;
} else {
direction = first_fret > last_fret ? TabSlide.SLIDE_DOWN : TabSlide.SLIDE_UP;
}
}

this.direction = direction;
Expand Down
9 changes: 5 additions & 4 deletions tests/textnote_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
/* eslint-disable */
// @ts-nocheck

import { TestOptions, VexFlowTests } from './vexflow_test_helpers';
import { QUnit, equal, ok, test } from './declarations';
import { Flow } from 'flow';
import { Crescendo } from 'crescendo';
import { TextNote } from 'textnote';
import { Flow } from 'flow';
import { Note } from 'note';
import { TextNote } from 'textnote';

import { equal, ok, QUnit, test } from './declarations';
import { TestOptions, VexFlowTests } from './vexflow_test_helpers';

const TextNoteTests = {
Start(): void {
Expand Down

0 comments on commit 37e70ad

Please sign in to comment.