@@ -198,11 +198,32 @@ export class SubtitleUtils {
198198 * @return {string } Formatted subtitle text.
199199 */
200200 static convertSubtitleFormatting ( text ) {
201- return text
202- . replace ( / \{ \\ ( [ i b u ] ) 1 \} / g, '<$1>' ) // convert {\b1}, {\i1}, {\u1} to <b>, <i>, <u>
203- . replace ( / \{ \\ ( [ i b u ] ) \} / g, '</$1>' ) // convert {\b}, {\i}, {\u} to </b>, </i>, </u>
204- . replace ( / \{ ( [ i b u ] ) \} / g, '<$1>' ) // convert {b}, {i}, {u} to <b>, <i>, <u>
205- . replace ( / \{ \/ ( [ i b u ] ) \} / g, '</$1>' ) // convert {/b}, {/i}, {/u} to </b>, </i>, </u>
206- . replace ( / ( \r \n | \n ) \{ \\ a n 8 \} / g, ' line:5%\n' ) ; // handle top positioning
201+ const alignmentSettings = {
202+ 1 : 'line:95% position:0% align:start' ,
203+ 2 : 'line:95% position:50% align:center' ,
204+ 3 : 'line:95% position:100% align:end' ,
205+ 4 : 'line:50% position:0% align:start' ,
206+ 5 : 'line:50% position:50% align:center' ,
207+ 6 : 'line:50% position:100% align:end' ,
208+ 7 : 'line:5% position:0% align:start' ,
209+ 8 : 'line:5% position:50% align:center' ,
210+ 9 : 'line:5% position:100% align:end' ,
211+ } ;
212+
213+ const withAlignment = text . replace ( / ( \r \n | \n ) \{ \\ ? a n ( \d ) \} / gi, ( match , _newline , alignment ) => {
214+ const settings = alignmentSettings [ alignment ] ;
215+ if ( settings ) {
216+ return ` ${ settings } \n` ;
217+ }
218+ return '\n' ;
219+ } ) ;
220+
221+ return withAlignment
222+ . replace ( / \{ \\ ( [ i b u ] ) 1 \} / gi, '<$1>' ) // convert {\b1}, {\i1}, {\u1} to <b>, <i>, <u>
223+ . replace ( / \{ \\ ( [ i b u ] ) \} / gi, '</$1>' ) // convert {\b}, {\i}, {\u} to </b>, </i>, </u>
224+ . replace ( / \{ ( [ i b u ] ) \} / gi, '<$1>' ) // convert {b}, {i}, {u} to <b>, <i>, <u>
225+ . replace ( / \{ \/ ( [ i b u ] ) \} / gi, '</$1>' ) // convert {/b}, {/i}, {/u} to </b>, </i>, </u>
226+ . replace ( / \{ \\ ? a n \d \} / gi, '' ) // strip any remaining alignment tags
227+ . replace ( / \\ h / gi, ' ' ) ; // convert hard spaces to regular spaces
207228 }
208229}
0 commit comments