UNPKG

@stringsync/vexml

Version:

MusicXML to Vexflow

110 lines (109 loc) 4.42 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.DebugRect = void 0; const util = __importStar(require("../util")); const spatial_1 = require("../spatial"); const label_1 = require("./label"); const DEFAULT_STYLE_FONT_COLOR = 'rgb(230, 0, 0)'; const DEFAULT_STYLE_FILL = 'rgba(255, 0, 0, 0.2)'; const DEFAULT_STYLE_STROKE = 'rgba(255, 0, 0, 0.5)'; class DebugRect { config; log; label; rect; style; ctx = null; constructor(config, log, label, rect, style) { this.config = config; this.log = log; this.label = label; this.rect = rect; this.style = style; } setContext(ctx) { this.ctx = ctx; return this; } draw() { const ctx = this.ctx; util.assertNotNull(ctx); ctx.save(); const padding = {}; const font = { color: DEFAULT_STYLE_FONT_COLOR, size: '8px', family: 'monospace', }; // Draw the main label in the bottom left corner. if (this.label) { const bottomLeft = this.rect.bottomLeft(); const position = new spatial_1.Point(bottomLeft.x + 1, bottomLeft.y - 1); label_1.Label.singleLine(this.config, this.log, this.label, position, padding, font).setContext(ctx).draw(); } // Draw the height label on the middle of the left side. const heightLabelPosition = new spatial_1.Point(this.rect.x + 1, this.rect.center().y + 3); const heightLabel = `${Math.round(this.rect.h)}`; label_1.Label.singleLine(this.config, this.log, heightLabel, heightLabelPosition, padding, font).setContext(ctx).draw(); // Draw the width label on the middle of the bottom side. const widthLabelPosition = new spatial_1.Point(this.rect.center().x, this.rect.y + this.rect.h - 1); const widthLabel = `${Math.round(this.rect.w)}`; label_1.Label.singleLine(this.config, this.log, widthLabel, widthLabelPosition, padding, font).setContext(ctx).draw(); // Draw the position label on the top left corner. const positionLabelPosition = new spatial_1.Point(this.rect.x + 1, this.rect.y + 8); const positionLabel = `${Math.round(this.rect.x)},${Math.round(this.rect.y)}`; label_1.Label.singleLine(this.config, this.log, positionLabel, positionLabelPosition, padding, font).setContext(ctx).draw(); this.drawRect(this.rect); ctx.restore(); return this; } drawRect(rect) { const ctx = this.ctx; util.assertNotNull(ctx); ctx.save(); const stroke = this.style?.stroke ?? DEFAULT_STYLE_STROKE; ctx.setStrokeStyle(stroke); ctx.beginPath(); ctx.rect(rect.x, rect.y, rect.w, rect.h); ctx.stroke(); ctx.closePath(); const fill = this.style?.fill ?? DEFAULT_STYLE_FILL; ctx.setFillStyle(fill); ctx.fillRect(rect.x, rect.y, rect.w, rect.h); ctx.restore(); } } exports.DebugRect = DebugRect;