Skip to content

Commit

Permalink
feat: treat \q as global tag
Browse files Browse the repository at this point in the history
  • Loading branch information
weizhenye committed Aug 11, 2024
1 parent 3f404ec commit d4d7cc1
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function compile(text, options = {}) {
info: tree.info,
width: tree.info.PlayResX * 1 || null,
height: tree.info.PlayResY * 1 || null,
wrapStyle: /^[0-3]$/.test(tree.info.WrapStyle) ? tree.info.WrapStyle * 1 : 2,
collisions: tree.info.Collisions || 'Normal',
styles,
dialogues: compileDialogues({
Expand Down
1 change: 1 addition & 0 deletions src/compiler/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export function compileStyles({ info, style, defaultStyle }) {
xshad: s.Shadow,
yshad: s.Shadow,
fe: s.Encoding,
// TODO: [breaking change] remove `q` from style
q: /^[0-3]$/.test(info.WrapStyle) ? info.WrapStyle * 1 : 2,
};
result[s.Name] = { style: s, tag };
Expand Down
4 changes: 3 additions & 1 deletion src/compiler/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function inheritTag(pTag) {

export function compileText({ styles, style, parsed, start, end }) {
let alignment;
let q = { q: styles[style].tag.q };
let pos;
let org;
let move;
Expand All @@ -43,6 +44,7 @@ export function compileText({ styles, style, parsed, start, end }) {
for (let j = 0; j < tags.length; j++) {
const tag = tags[j];
alignment = alignment || a2an[tag.a || 0] || tag.an;
q = compileTag(tag, 'q') || q;
pos = pos || compileTag(tag, 'pos');
org = org || compileTag(tag, 'org');
move = move || compileTag(tag, 'move');
Expand Down Expand Up @@ -79,5 +81,5 @@ export function compileText({ styles, style, parsed, start, end }) {
}
slices.push(slice);

return Object.assign({ alignment, slices }, pos, org, move, fade, clip);
return Object.assign({ alignment, slices }, q, pos, org, move, fade, clip);
}
1 change: 1 addition & 0 deletions test/compiler/dialogues.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ describe('dialogues compiler', () => {
},
effect: null,
alignment: 2,
q: 0,
slices: [{
style: 'Default',
fragments: [{
Expand Down
6 changes: 6 additions & 0 deletions test/compiler/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ describe('text compiler', () => {
drawing: null,
dots: { x1: 1, y1: 2, x2: 3, y2: 4 },
});
const test1 = compileText({ styles, style, parsed: parseText('bla bla').parsed });
expect(test1.q).to.equal(0);
const test2 = compileText({ styles, style, parsed: parseText('{\\q1}bla bla').parsed });
expect(test2.q).to.equal(1);
const test3 = compileText({ styles, style, parsed: parseText('{\\q1}bla {\\q2}bla').parsed });
expect(test3.q).to.equal(2);
});

it('should compile text with \\r', () => {
Expand Down
3 changes: 3 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export interface CompiledASSStyleTag {
xshad: number;
yshad: number;
fe: number;
// TODO: [breaking change] delete `q`
q: 0 | 1 | 2 | 3;
}

Expand Down Expand Up @@ -201,6 +202,7 @@ export interface Dialogue {
}
effect?: EffectBanner | EffectScroll | EffectUnknown;
alignment: number;
q: 0 | 1 | 2 | 3;
slices: DialogueSlice[];
pos?: {
x: number;
Expand Down Expand Up @@ -250,6 +252,7 @@ export interface CompiledASS {
width: number;
height: number;
collisions: 'Normal' | 'Reverse';
wrapStyle: 0 | 1 | 2 | 3;
styles: { [styleName: string]: CompiledASSStyle };
dialogues: Dialogue[];
}
Expand Down

0 comments on commit d4d7cc1

Please sign in to comment.