Skip to content

Commit

Permalink
timesignote timesignature migrated
Browse files Browse the repository at this point in the history
  • Loading branch information
rvilarl committed May 9, 2021
1 parent 60664cc commit 08d78e0
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 103 deletions.
14 changes: 7 additions & 7 deletions src/glyph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ export class Glyph extends Element {
bbox: BoundingBox = new BoundingBox(0, 0, 0, 0);
code: string;
metrics?: GlyphMetrics;
topGlyphs?: Glyph[];
botGlyphs?: Glyph[];
topGlyphs: Glyph[] = [];
botGlyphs: Glyph[] = [];

protected options: GlyphOptions;
protected originShift: { x: number; y: number };
protected x_shift: number;
protected y_shift: number;
protected scale: number = 1;
scale: number = 1;
protected point: number;
protected stave?: Stave;

Expand Down Expand Up @@ -371,10 +371,10 @@ export class Glyph extends Element {
x_max: this.metrics.x_max * this.scale * this.metrics.scale,
width: this.bbox.getW(),
height: this.bbox.getH(),
scale: 1,
x_shift: 0,
y_shift: 0,
outline: [],
scale: this.metrics.scale,
x_shift: this.metrics.x_shift,
y_shift: this.metrics.y_shift,
outline: this.metrics.outline,
};
}

Expand Down
85 changes: 85 additions & 0 deletions src/timesigglyph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Glyph, GlyphMetrics } from './glyph';
import { TimeSignature } from './timesignature';

export class TimeSignatureGlyph extends Glyph {
timeSignature: TimeSignature;
topStartX: number;
botStartX: number;
width: number;
xMin: number;

constructor(
timeSignature: TimeSignature,
topDigits: string[],
botDigits: string[],
code: string,
point: number,
options?: { category: string }
) {
super(code, point, options);
this.timeSignature = timeSignature;
this.topGlyphs = [];
this.botGlyphs = [];

let topWidth = 0;
for (let i = 0; i < topDigits.length; ++i) {
const num = topDigits[i];
const topGlyph = new Glyph('timeSig' + num, this.timeSignature.point!);

this.topGlyphs.push(topGlyph);
topWidth += topGlyph.getMetrics().width!;
}

let botWidth = 0;
for (let i = 0; i < botDigits.length; ++i) {
const num = botDigits[i];
const botGlyph = new Glyph('timeSig' + num, this.timeSignature.point!);

this.botGlyphs.push(botGlyph);
botWidth += botGlyph.getMetrics().width!;
}

this.width = topWidth > botWidth ? topWidth : botWidth;
this.xMin = this.getMetrics().x_min;
this.topStartX = (this.width - topWidth) / 2.0;
this.botStartX = (this.width - botWidth) / 2.0;
this.reset();
}

getMetrics(): GlyphMetrics {
return {
x_min: this.xMin,
x_max: this.xMin + this.width,
width: this.width,
} as GlyphMetrics;
}

renderToStave(x: number): void {
let start_x = x + this.topStartX;
for (let i = 0; i < this.topGlyphs.length; ++i) {
const glyph = this.topGlyphs[i];
Glyph.renderOutline(
this.checkContext(),
glyph.getMetrics().outline,
this.scale,
start_x + this.x_shift,
this.stave!.getYForLine(this.timeSignature.topLine!)
);
start_x += glyph.getMetrics().width!;
}

start_x = x + this.botStartX;
for (let i = 0; i < this.botGlyphs.length; ++i) {
const glyph = this.botGlyphs[i];
this.timeSignature.placeGlyphOnLine(glyph, this.stave!, 0);
Glyph.renderOutline(
this.checkContext(),
glyph.getMetrics().outline,
this.scale,
start_x + glyph.getMetrics().x_shift,
this.stave!.getYForLine(this.timeSignature.bottomLine!)
);
start_x += glyph.getMetrics().width!;
}
}
}
122 changes: 39 additions & 83 deletions src/timesignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,20 @@
import { Vex } from './vex';
import { Glyph } from './glyph';
import { StaveModifier } from './stavemodifier';
import { TimeSignatureGlyph } from './timesigglyph';
export interface TimeSignatureInfo {
glyph: Glyph;
line?: number;
num: boolean;
}

export interface TimeSignatureGlyphInfo {
code: string;
point: number;
line: number;
}

const assertIsValidFraction = (timeSpec) => {
const assertIsValidFraction = (timeSpec: string) => {
const numbers = timeSpec.split('/').filter((number) => number !== '');

if (numbers.length !== 2) {
Expand All @@ -27,11 +39,19 @@ const assertIsValidFraction = (timeSpec) => {
};

export class TimeSignature extends StaveModifier {
static get CATEGORY() {
point?: number;
bottomLine?: number;
protected timeSig?: TimeSignatureInfo;

protected validate_args: boolean;

topLine?: number;

static get CATEGORY(): string {
return 'timesignatures';
}

static get glyphs() {
static get glyphs(): Record<string, TimeSignatureGlyphInfo> {
return {
C: {
code: 'timeSigCommon',
Expand All @@ -46,30 +66,29 @@ export class TimeSignature extends StaveModifier {
};
}

constructor(timeSpec = null, customPadding = 15, validate_args = true) {
constructor(timeSpec?: string, customPadding = 15, validate_args = true) {
super();
this.setAttribute('type', 'TimeSignature');
this.validate_args = validate_args;

if (timeSpec === null) return;

if (!timeSpec) return;
const padding = customPadding;

this.point = this.musicFont.lookupMetric('digits.point');
const fontLineShift = this.musicFont.lookupMetric('digits.shiftLine', 0);
this.topLine = 2 + fontLineShift;
this.bottomLine = 4 + fontLineShift;
this.setPosition(StaveModifier.Position.BEGIN);
this.setTimeSig(timeSpec);
this.setWidth(this.timeSig.glyph.getMetrics().width);
this.timeSig = this.parseTimeSpec(timeSpec);
this.setWidth(this.getTimeSig().glyph.getMetrics().width!);
this.setPadding(padding);
}

getCategory() {
getCategory(): string {
return TimeSignature.CATEGORY;
}

parseTimeSpec(timeSpec) {
parseTimeSpec(timeSpec: string): TimeSignatureInfo {
if (timeSpec === 'C' || timeSpec === 'C|') {
const { line, code, point } = TimeSignature.glyphs[timeSpec];
return {
Expand All @@ -91,84 +110,21 @@ export class TimeSignature extends StaveModifier {
};
}

makeTimeSignatureGlyph(topDigits, botDigits) {
const glyph = new Glyph('timeSig0', this.point);
glyph.topGlyphs = [];
glyph.botGlyphs = [];

let topWidth = 0;
for (let i = 0; i < topDigits.length; ++i) {
const num = topDigits[i];
const topGlyph = new Glyph('timeSig' + num, this.point);

glyph.topGlyphs.push(topGlyph);
topWidth += topGlyph.getMetrics().width;
}

let botWidth = 0;
for (let i = 0; i < botDigits.length; ++i) {
const num = botDigits[i];
const botGlyph = new Glyph('timeSig' + num, this.point);

glyph.botGlyphs.push(botGlyph);
botWidth += botGlyph.getMetrics().width;
}

const width = topWidth > botWidth ? topWidth : botWidth;
const xMin = glyph.getMetrics().x_min;

glyph.getMetrics = () => ({
x_min: xMin,
x_max: xMin + width,
width,
});

const topStartX = (width - topWidth) / 2.0;
const botStartX = (width - botWidth) / 2.0;

const that = this;
glyph.renderToStave = function renderToStave(x) {
let start_x = x + topStartX;
for (let i = 0; i < this.topGlyphs.length; ++i) {
const glyph = this.topGlyphs[i];
Glyph.renderOutline(
this.context,
glyph.metrics.outline,
glyph.scale,
start_x + glyph.x_shift,
this.stave.getYForLine(that.topLine)
);
start_x += glyph.getMetrics().width;
}

start_x = x + botStartX;
for (let i = 0; i < this.botGlyphs.length; ++i) {
const glyph = this.botGlyphs[i];
that.placeGlyphOnLine(glyph, this.stave, glyph.line);
Glyph.renderOutline(
this.context,
glyph.metrics.outline,
glyph.scale,
start_x + glyph.x_shift,
this.stave.getYForLine(that.bottomLine)
);
start_x += glyph.getMetrics().width;
}
};

makeTimeSignatureGlyph(topDigits: string[], botDigits: string[]): Glyph {
const glyph = new TimeSignatureGlyph(this, topDigits, botDigits, 'timeSig0', this.point!);
return glyph;
}

getTimeSig() {
return this.timeSig;
getTimeSig(): TimeSignatureInfo {
return this.timeSig!;
}

setTimeSig(timeSpec) {
setTimeSig(timeSpec: string): this {
this.timeSig = this.parseTimeSpec(timeSpec);
return this;
}

draw() {
draw(): void {
if (!this.x) {
throw new Vex.RERR('TimeSignatureError', "Can't draw time signature without x.");
}
Expand All @@ -178,9 +134,9 @@ export class TimeSignature extends StaveModifier {
}

this.setRendered();
this.timeSig.glyph.setStave(this.stave);
this.timeSig.glyph.setContext(this.stave.context);
this.placeGlyphOnLine(this.timeSig.glyph, this.stave, this.timeSig.line);
this.timeSig.glyph.renderToStave(this.x);
this.timeSig!.glyph.setStave(this.stave);
this.timeSig!.glyph.setContext(this.stave.getContext());
this.placeGlyphOnLine(this.timeSig!.glyph, this.stave, this.timeSig!.line!);
this.timeSig!.glyph.renderToStave(this.x);
}
}
26 changes: 13 additions & 13 deletions src/timesignote.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
// [VexFlow](http://vexflow.com) - Copyright (c) Mohit Muthanna 2010.
// Author Taehoon Moon 2014

import { Vex } from './vex';
import { Note } from './note';
import { TimeSignature } from './timesignature';
import { TimeSignature, TimeSignatureInfo } from './timesignature';

export class TimeSigNote extends Note {
constructor(timeSpec, customPadding) {
protected timeSig: TimeSignatureInfo;

constructor(timeSpec: string, customPadding?: number) {
super({ duration: 'b' });
this.setAttribute('type', 'TimeSigNote');

const timeSignature = new TimeSignature(timeSpec, customPadding);
this.timeSig = timeSignature.getTimeSig();
this.setWidth(this.timeSig.glyph.getMetrics().width);
this.setWidth(this.timeSig.glyph.getMetrics().width!);

// Note properties
this.ignore_ticks = true;
}

getBoundingBox() {
return super.getBoundingBox();
}

addToModifierContext() {
addToModifierContext(): this {
/* overridden to ignore */
return this;
}

preFormat() {
preFormat(): this {
this.setPreFormatted(true);
return this;
}

draw() {
this.stave.checkContext();
draw(): void {
if (!this.stave) throw new Vex.RERR('NoStave', 'No stave attached to this note.');
const ctx = this.checkContext();
this.setRendered();

if (!this.timeSig.glyph.getContext()) {
this.timeSig.glyph.setContext(this.context);
this.timeSig.glyph.setContext(ctx);
}

this.timeSig.glyph.setStave(this.stave);
this.timeSig.glyph.setYShift(this.stave.getYForLine(this.timeSig.line) - this.stave.getYForGlyphs());
this.timeSig.glyph.setYShift(this.stave.getYForLine(this.timeSig.line!) - this.stave.getYForGlyphs());
this.timeSig.glyph.renderToStave(this.getAbsoluteX());
}
}

0 comments on commit 08d78e0

Please sign in to comment.