From 364eb3b6ed28fb18f9ccf6bb23225ef3ac169316 Mon Sep 17 00:00:00 2001 From: FS-Frost <30810344+FS-Frost@users.noreply.github.com> Date: Wed, 12 Jun 2024 15:38:08 -0400 Subject: [PATCH] truncateNumberTags, forEachTag --- jsr.json | 2 +- src/asu.test.ts | 15 +++++++ src/asu.ts | 108 +++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 119 insertions(+), 6 deletions(-) diff --git a/jsr.json b/jsr.json index 91ad6207..5e16da07 100644 --- a/jsr.json +++ b/jsr.json @@ -1,5 +1,5 @@ { "name": "@fs-frost/asu", - "version": "1.2.0", + "version": "1.3.0", "exports": "./src/asu.ts" } diff --git a/src/asu.test.ts b/src/asu.test.ts index 633257c8..174d2f16 100644 --- a/src/asu.test.ts +++ b/src/asu.test.ts @@ -3076,6 +3076,21 @@ test("parse tags with bad args", () => { } }); +test("line to string", () => { + const text = "Dialogue: 0,0:00:00.00,0:00:00.00,Default,a,0,0,0,b,{\\pos(182.123489123918322193,421.847593450834985)\\be2.1234}LINE 1"; + const expectedText = "Dialogue: 0,0:00:00.00,0:00:00.00,Default,a,0,0,0,b,{\\pos(182.123,421.847)\\be2.123}LINE 1"; + const line = asu.parseLine(text); + expect(line).not.toBeNull(); + if (line == null) { + throw "null line"; + } + + const items = asu.parseContent(line.content); + asu.truncateNumberTags(items, 3); + line.content = asu.contentsToString(items); + expect(asu.lineToString(line)).toEqual(expectedText); +}); + // others test("parse result equals toString()", () => { const text = "{\\be5\\pos(0.5,-28)}{¡Buenos días, {\\i1}Chitanda-san{\\i0}!"; diff --git a/src/asu.ts b/src/asu.ts index 408ccb8c..9b868f13 100644 --- a/src/asu.ts +++ b/src/asu.ts @@ -1,4 +1,4 @@ -import { hexToNumber, numberToHex } from "./mat"; +import { hexToNumber, numberToHex, truncate } from "./mat"; import * as regex from "./regex"; import { Time, parseTime, timeToSeconds, timeToString } from "./time"; @@ -1295,6 +1295,104 @@ export function mergeNeighboringEffects(items: ContentItem[]): void { } } +export function truncateNumberTags(items: ContentItem[], decimals: number): void { + forEachTag(items, (tag) => { + switch (tag.name) { + case TagName.t: + { + if (tag.accel != null) { + tag.accel = truncate(tag.accel, decimals); + } + + if (tag.t1 != null) { + tag.t1 = truncate(tag.t1, decimals); + } + + if (tag.t2 != null) { + tag.t2 = truncate(tag.t2, decimals); + } + break; + } + + case TagName.pos: + case TagName.org: + tag.x = truncate(tag.x, decimals); + tag.y = truncate(tag.y, decimals); + break; + + case TagName.move: + tag.x1 = truncate(tag.x1, decimals); + tag.y1 = truncate(tag.y1, decimals); + tag.x2 = truncate(tag.x2, decimals); + tag.y2 = truncate(tag.y2, decimals); + + if (tag.t1 != null) { + tag.t1 = truncate(tag.t1, decimals); + } + + if (tag.t2 != null) { + tag.t2 = truncate(tag.t2, decimals); + } + break; + + case TagName.fad: + tag.in = truncate(tag.in, decimals); + tag.out = truncate(tag.out, decimals); + break; + + case TagName.fade: + tag.t1 = truncate(tag.t1, decimals); + tag.t2 = truncate(tag.t2, decimals); + tag.t3 = truncate(tag.t3, decimals); + tag.t4 = truncate(tag.t4, decimals); + tag.alpha1 = truncate(tag.alpha1, decimals); + tag.alpha2 = truncate(tag.alpha2, decimals); + tag.alpha3 = truncate(tag.alpha3, decimals); + break; + + case TagName.fe: + tag.encodingId = Math.floor(tag.encodingId); + break; + + case TagName.color: + case TagName.color1: + case TagName.color2: + case TagName.color3: + case TagName.color4: + tag.blue = Math.floor(tag.blue); + tag.green = Math.floor(tag.green); + tag.red = Math.floor(tag.red); + break; + + case TagName.clip: + case TagName.iclip: + case TagName.fn: + case TagName.r: + case TagName.text: + case TagName.unknown: + break; + + default: + if (typeof tag.value === "number") { + tag.value = truncate(tag.value, decimals); + } + break; + } + }); +} + +export function forEachTag(items: ContentItem[], predicate: (tag: Tags) => void): void { + for (const item of items) { + if (item.name != "effect") { + continue; + } + + for (const tag of item.tags) { + predicate(tag); + } + } +} + export function findA(items: ContentItem[]): TagA | null { const fx = items.find(item => item.name == "effect"); if (fx?.name != "effect") { @@ -2984,7 +3082,7 @@ export function parseLine(text: string): Line | null { export function lineToString(line: Line): string { let s = line.type; s += ": "; - s += line.layer; + s += Math.floor(line.layer); s += ","; s += timeToString(line.start); s += ","; @@ -2994,11 +3092,11 @@ export function lineToString(line: Line): string { s += ","; s += line.actor; s += ","; - s += line.marginLeft; + s += Math.floor(line.marginLeft); s += ","; - s += line.marginRight; + s += Math.floor(line.marginRight); s += ","; - s += line.marginVertical; + s += Math.floor(line.marginVertical); s += ","; s += line.effect; s += ",";