Skip to content

Commit

Permalink
Merge branch 'feature/region-percentage' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandercerutti committed Feb 4, 2024
2 parents 5fdac32 + d4d9ffe commit fc94c49
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
6 changes: 3 additions & 3 deletions packages/captions-renderer/src/TreeOrchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ export default class TreeOrchestrator {
const [originX, originY] = trackRegionSettings?.getOrigin(
parent.offsetWidth,
parent.offsetHeight,
) ?? [0, 70];
) ?? ["0%", "70%"];

const rootStyles: Partial<CSSStyleDeclaration> = {
width: `${trackRegionSettings?.width ?? 100}%`,
height: `${this.settings.lines * 1.5}em`,
left: `${originX}%`,
top: `${originY}%`,
left: originX,
top: originY,
};

Object.assign(root.style, rootStyles);
Expand Down
10 changes: 5 additions & 5 deletions packages/server/src/Region.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ export interface Region {
/**
* Allows each parser how to express
* the position of the region.
*
* @returns {[x: string, y: string]} coordinates with measure unit
*/

getOrigin(): [x: number | string, y: number | string];
getOrigin(): [x: string, y: string];

/**
* Allows each parser how to express
* the position of the region based on runtime data
*
* @param viewportWidth
* @param viewportHeight
* @returns {[x: string, y: string]} coordinates with measure unit
*/

getOrigin(
viewportWidth: number,
viewportHeight: number,
): [x: number | string, y: number | string];
getOrigin(viewportWidth: number, viewportHeight: number): [x: string, y: string];
}
23 changes: 19 additions & 4 deletions packages/webvtt-adapter/src/Parser/parseRegion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,22 @@ export function parseRegion(rawRegionData: string): Region {
case "regionanchor":
case "viewportanchor": {
const [x = "0%", y = "0%"] = value.split(",");
region[key] = [parseInt(x), parseInt(y)];

if (!x.endsWith("%") || !y.endsWith("%")) {
break;
}

const xInteger = parseInt(x);
const yInteger = parseInt(y);

if (Number.isNaN(xInteger) || Number.isNaN(yInteger)) {
break;
}

const clampedX = Math.max(0, Math.min(xInteger, 100));
const clampedY = Math.max(0, Math.min(yInteger, 100));

region[key] = [clampedX, clampedY];
break;
}

Expand Down Expand Up @@ -92,7 +107,7 @@ class WebVTTRegion implements Region {
*/
public regionanchor?: [number, number];

public getOrigin(): [x: number, y: number] {
public getOrigin(): [x: string, y: string] {
const height = VH_LINE_HEIGHT * this.lines;

const [regionAnchorWidth = 0, regionAnchorHeight = 0] = this.regionanchor || [];
Expand All @@ -106,8 +121,8 @@ class WebVTTRegion implements Region {
const leftOffset = (regionAnchorWidth * this.width) / 100;
const topOffset = (regionAnchorHeight * height) / 100;

const originX = viewportAnchorWidth - leftOffset;
const originY = viewportAnchorHeight - topOffset;
const originX = `${viewportAnchorWidth - leftOffset}%`;
const originY = `${viewportAnchorHeight - topOffset}%`;

return [originX, originY];
}
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
"moduleResolution": "node",
"noImplicitReturns": true,
"lib": ["DOM", "ES2018", "ES2020"]
}
},
"include": ["packages/**/src/**/*"]
}

0 comments on commit fc94c49

Please sign in to comment.