Skip to content

Commit

Permalink
Fonsts.XXX is now a function
Browse files Browse the repository at this point in the history
  • Loading branch information
rvilarl committed Jul 17, 2021
1 parent 7f8bf63 commit 49e68b3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
43 changes: 36 additions & 7 deletions src/font.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { PetalumaFont } from './fonts/petaluma_glyphs';
import { PetalumaMetrics } from './fonts/petaluma_metrics';
import { CustomFont } from './fonts/custom_glyphs';
import { CustomMetrics } from './fonts/custom_metrics';
import { RuntimeError } from './util';

export interface FontData {
glyphs: Record<string, FontGlyph>;
Expand Down Expand Up @@ -33,10 +34,30 @@ class Font {
protected readonly fontData: FontData;

// eslint-disable-next-line
constructor(name: string, metrics: Record<string, any>, fontData: FontData) {
constructor(name: string, metrics?: Record<string, any>, fontData?: FontData) {
this.name = name;
this.metrics = metrics;
this.fontData = fontData;
switch (name) {
case 'Bravura':
this.metrics = BravuraMetrics;
this.fontData = BravuraFont;
break;
case 'Gonville':
this.metrics = GonvilleMetrics;
this.fontData = GonvilleFont;
break;
case 'Petaluma':
this.metrics = PetalumaMetrics;
this.fontData = PetalumaFont;
break;
case 'Custom':
this.metrics = CustomMetrics;
this.fontData = CustomFont;
break;
default:
if (!metrics || !fontData) throw new RuntimeError('Missing metrics or font data');
this.metrics = metrics;
this.fontData = fontData;
}
}

getName(): string {
Expand Down Expand Up @@ -78,10 +99,18 @@ class Font {
}

const Fonts = {
Bravura: new Font('Bravura', BravuraMetrics, BravuraFont),
Gonville: new Font('Gonville', GonvilleMetrics, GonvilleFont),
Petaluma: new Font('Petaluma', PetalumaMetrics, PetalumaFont),
Custom: new Font('Custom', CustomMetrics, CustomFont),
Bravura: (): Font => {
return new Font('Bravura');
},
Gonville: (): Font => {
return new Font('Gonville');
},
Petaluma: (): Font => {
return new Font('Petaluma');
},
Custom: (): Font => {
return new Font('Custom');
},
};

export { Fonts, Font };
4 changes: 2 additions & 2 deletions src/fonts/legacy/glyphs.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
var canvas = document.getElementById("glyphs");
var x = 0;

const glyphs = Vex.Flow.Fonts.Gonville.getGlyphs();
const glyphs = Vex.Flow.Fonts.Gonville().getGlyphs();

// Get number of glyphs and rename elements
var counter = Object.keys(glyphs).length;
Expand Down Expand Up @@ -83,7 +83,7 @@
ctx.fillStyle = "green";

if (glyphs[glyph].o) {
g = new Vex.Flow.Glyph(glyph, 38, { font: Vex.Flow.Fonts.Gonville });
g = new Vex.Flow.Glyph(glyph, 38, { font: Vex.Flow.Fonts.Gonville() });
g.render(ctx, x, y);
ctx.save();
ctx.font = "6pt Arial";
Expand Down
4 changes: 2 additions & 2 deletions src/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,9 @@ export const Tables = {

/**
* Customize this to choose a different music font.
* For example: Vex.Flow.DEFAULT_FONT_STACK = [Fonts.Petaluma, Fonts.Custom];
* For example: Vex.Flow.DEFAULT_FONT_STACK = [Fonts.Petaluma(), Fonts.Custom()];
*/
DEFAULT_FONT_STACK: [Fonts.Bravura, Fonts.Gonville, Fonts.Custom],
DEFAULT_FONT_STACK: [Fonts.Bravura(), Fonts.Gonville(), Fonts.Custom()],
DEFAULT_NOTATION_FONT_SCALE: 39,
DEFAULT_TABLATURE_FONT_SCALE: 39,
SLASH_NOTEHEAD_WIDTH: 15,
Expand Down
6 changes: 3 additions & 3 deletions tests/vexflow_test_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ const VexFlowTests = (function () {
FONT_STACKS_TO_TEST: ['Bravura', 'Gonville', 'Petaluma'],

FONT_STACKS: {
Bravura: [VF.Fonts.Bravura, VF.Fonts.Gonville, VF.Fonts.Custom],
Gonville: [VF.Fonts.Gonville, VF.Fonts.Bravura, VF.Fonts.Custom],
Petaluma: [VF.Fonts.Petaluma, VF.Fonts.Gonville, VF.Fonts.Custom],
Bravura: [VF.Fonts.Bravura(), VF.Fonts.Gonville(), VF.Fonts.Custom()],
Gonville: [VF.Fonts.Gonville(), VF.Fonts.Bravura(), VF.Fonts.Custom()],
Petaluma: [VF.Fonts.Petaluma(), VF.Fonts.Gonville(), VF.Fonts.Custom()],
},

// Returns a unique ID for a test.
Expand Down

0 comments on commit 49e68b3

Please sign in to comment.