File indexing completed on 2024-06-23 04:52:41

0001 /*
0002         SPDX-FileCopyrightText: 2007-2009 Sergio Pistone <sergio_pistone@yahoo.com.ar>
0003 
0004         SPDX-License-Identifier: GPL-2.0-or-later
0005 
0006         @category Examples
0007         @name Iterate Selected Lines
0008         @version 1.0
0009         @summary Example script to iterate over selected lines and show their text.
0010         @author SubtitleComposer Team
0011 */
0012 
0013 function RangesIterator(rangeList, forward) {
0014         this.forward = forward === undefined ? true : forward;
0015         this.rangeList = rangeList;
0016         this.rangeIndex = this.forward ? -1 : this.rangeList.rangesCount()
0017 
0018         this.current = function() {
0019                 return this.rangeIndex >= 0 && this.rangeIndex < this.rangeList.rangesCount() ?
0020                         this.rangeList.range(this.rangeIndex) :
0021                         null;
0022         };
0023 
0024         this.hasNext = function() {
0025                 return this.forward ?
0026                         this.rangeIndex < this.rangeList.rangesCount() - 1 :
0027                         this.rangeIndex > 0;
0028         };
0029 
0030         this.next = function() {
0031                 this.rangeIndex += this.forward ? 1 : -1;
0032                 return this.current();
0033         };
0034 }
0035 
0036 function LinesIterator(rangeList, forward) {
0037         this.forward = forward === undefined ? true : forward;
0038         this.rangesIt = new RangesIterator(rangeList, this.forward);
0039         this.lineIndex = -1;
0040         this.subtitle = subtitle.instance();
0041 
0042         this.current = function() {
0043                 let range = this.rangesIt.current();
0044                 return range != null && this.lineIndex >= range.start() && this.lineIndex < range.end()
0045                                 ? this.subtitle.line(this.lineIndex) : null;
0046         };
0047 
0048         this.hasNext = function() {
0049                 return this.rangesIt.hasNext() || (this.rangesIt.current() &&
0050                         (this.forward ? this.lineIndex < this.rangesIt.current().end() : this.lineIndex > this.rangesIt.current().start())
0051                         );
0052         };
0053 
0054         this.next = function() {
0055                 let currentRange = this.rangesIt.current();
0056                 if(currentRange == null || (this.lineIndex == (forward ? currentRange.end() : currentRange.start()))) {
0057                         if(!this.rangesIt.hasNext())
0058                                 return null;
0059                         currentRange = this.rangesIt.next();
0060                         this.lineIndex = this.forward ? currentRange.start() : currentRange.end();
0061                 } else {
0062                         this.lineIndex += this.forward ? 1 : -1;
0063                 }
0064                 return this.subtitle.line(this.lineIndex);
0065         };
0066 }
0067 
0068 let rangesIt = new RangesIterator(ranges.newSelectionRangeList(), true);
0069 while(rangesIt.hasNext()) {
0070         let range = rangesIt.next();
0071         debug.information( range.start().toString() + ":" + range.end().toString() );
0072 }
0073 
0074 let linesIt = new LinesIterator( ranges.newSelectionRangeList(), false );
0075 while(linesIt.hasNext()) {
0076         let line = linesIt.next();
0077         debug.information("Plain text: " + line.plainPrimaryText() + "\n\nRich Text: " + line.richPrimaryText());
0078 }