import Role from "../../data/Role"; import RoleLabel from './RoleLabel'; import TokenLayout from "./TokenLayout"; class RoleLayout{ role: Role; w: number; // the display width of its labels h: number; // the display height of its labels beginLineIndex: number; // the first line this role appears in elevation: number; // how many levels above to draw this role // the sentence may span multiple lines. following properties are for each line. barAnchors: [number, number][]; // the left and right coordinates of each bar labels: RoleLabel[]; constructor(role: Role, w: number, h: number, elevation:number, tokens:TokenLayout[]) { this.role = role; this.w = w; this.h = h; this.beginLineIndex = tokens[role.beginTokenIndex].lineIndex; this.elevation = elevation; this.barAnchors = getBarAnchors(this,tokens); this.labels = this.barAnchors.map((anchor,i)=>new RoleLabel( role.name, w, h, (anchor[0]+anchor[1])/2, this.beginLineIndex+i, elevation)); } } function getBarAnchors(role:RoleLayout, tokens:TokenLayout[]):[number, number][] { if(!role.role.length) return []; let leftAnchor:null|number = null; let rightAnchor:null|number = null; const barAnchors:[number, number][] = []; let lineIndexAnchor = role.beginLineIndex; const beginTokenIndex = role.role.beginTokenIndex; for(let i=0; i lineIndexAnchor && leftAnchor!=null && rightAnchor!=null) { barAnchors.push([leftAnchor, rightAnchor]); leftAnchor = null; lineIndexAnchor = token.lineIndex; } if(leftAnchor==null) leftAnchor = token.x; rightAnchor = token.x + token.w; } if(leftAnchor!=null && rightAnchor!=null) barAnchors.push([leftAnchor, rightAnchor]); return barAnchors; } export default RoleLayout;