import TokenLayout from './TokenLayout'; import FrameLayout from './FrameLayout'; import Srl from '../../data/Srl'; import calculateFrameElevation from "./calculateFrameElevation"; import RoleLayout from "./RoleLayout"; import RoleLabel from "./RoleLabel"; import calculateLabelRelocation from './calculateLabelRelocation'; import PredLayout from "./PredLayout"; class SrlLayout { tokens: TokenLayout[]; frames: FrameLayout[]; tokenCountForEachLine: number[]; // the number of elevations for each line elevationsForEachLine: number[]; constructor(srl:Srl, tokenRects:ClientRect[], roleRects:Map, hostRect:ClientRect) { this.tokens = []; this.frames = []; this.tokenCountForEachLine = []; this.elevationsForEachLine = []; // calculate line index for tokens // also set token count for each line let currRow = -1; let currX = Infinity; // Current X value. We can know whether a line break has been encountered. let firstTokenOnCurrLine = 0; for(let i=0; i 0) this.tokenCountForEachLine.push(i-firstTokenOnCurrLine); firstTokenOnCurrLine = i; } currX = x; this.tokens.push(new TokenLayout(srl.tokens[i], tokenRect.width, tokenRect.height, tokenRect.left - hostRect.left, currRow)); } this.tokenCountForEachLine.push(srl.tokens.length-firstTokenOnCurrLine); const {elevationForEachFrame, elevationsForEachLine} = calculateFrameElevation(srl.frames, this.tokenCountForEachLine); this.elevationsForEachLine = elevationsForEachLine; for(let i=0; iArray(elevations).fill(null).map(()=>[])); // gather labels into the previous 3D array for(let frame of layout.frames) { for(let role of frame.roles) { for(let label of role.labels) { allLabels[label.lineIndex][label.elevation].push(label); } } } // relocate for each line each elevation for(let line of allLabels) { for(let labelsThatMayClash of line) { labelsThatMayClash.sort((l1:RoleLabel, l2:RoleLabel)=>l1.cx-l2.cx); const widths = labelsThatMayClash.map(label=>label.w); const desiredCxs = labelsThatMayClash.map(label=>label.cx); const MIN_SPACE_BETWEEN = 4; const newCxs = calculateLabelRelocation(widths, desiredCxs, hostWidth, MIN_SPACE_BETWEEN); labelsThatMayClash.forEach((label, i)=>label.cx=newCxs[i]); } } } export default SrlLayout;