Repository navigation
Expand file tree
/
Copy pathSubtitleEditor.cpp
More file actions
741 lines (617 loc) · 19.5 KB
/
Copy pathSubtitleEditor.cpp
File metadata and controls
741 lines (617 loc) · 19.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
#include "SubtitleEditor.h"
SubtitleEditor::SubtitleEditor()
{
this->head = new Node();
this->head->titlePointer = nullptr;
this->head->top = new MyStack();
this->head->next = nullptr;
this->listSelected = false;
}
SubtitleEditor::~SubtitleEditor()
{
//prvo brisem moj stek stanja pozivajuci njegov destruktor, a onda se krecem kroz listu i brisem jedan po jedan element liste
delete this->head->top;
Node* tmp = this->head;
while (this->head != nullptr) {
this->head = this->head->next;
delete tmp->titlePointer;
delete tmp;
tmp = this->head;
}
}
int SubtitleEditor::loadSubtitles(string subtitles)
{
int pos = 0;
this->head->titlePointer = new Title(subtitles, pos);
if (this->head->titlePointer->getError())
return this->head->titlePointer->getSerial();
while (subtitles[pos] != '\0') {
Node* element = new Node();
element->titlePointer = new Title(subtitles, pos);
element->next = nullptr;
// ako postoji greska u titlu, vrati njegov redni broj, ako nema, ubaci ga u listu
if (element->titlePointer->getError())
return element->titlePointer->getSerial();
else
{ if (this->head->next == nullptr)
this->head->next = element;
else
{
Node* tail = this->head;
while (tail->next) tail = tail->next;
tail->next = element;
}
};
}
// procitaj celu listu, smesti u string, a onda dodaj u stek
string str = loadString();
this->head->top->addToStack(str);
return -1;
}
string SubtitleEditor::saveSubtitles()
{
return this->head->top->getLastState();
}
void SubtitleEditor::addToSelection(int start, int end)
{
Node* last = this->head;
while (last->next) last = last->next;
//ispitaj pocetak i start selekcije
start = start < 1 ? 1 : start;
end = end > last->titlePointer->getSerial() ? last->titlePointer->getSerial() : end;
Node* current = this->head;
while (current->next) {
if (current->titlePointer->getSerial() >= start && current->titlePointer->getSerial() <= end)
current->titlePointer->selectTitle();
current = current->next;
}
string str = loadString();
this->head->top->addToStack(str);
this->listSelected = true;
}
void SubtitleEditor::deselect()
{
Node* current = this->head;
while (current->next) {
current->titlePointer->deselectTitle();
current = current->next;
}
string str = loadString();
this->head->top->addToStack(str);
this->listSelected = false;
}
void SubtitleEditor::boldText(int start, int end)
{
//ako nije selektovana lista, prodji kroz sve titlove, ako jeste, samo kroz selektovane titlove
if (!listSelected) {
Node* current = this->head;
while (current != nullptr) {
string text = current->titlePointer->getText();
int posStart = start < 0 ? 0 : start;
int posEnd = end >= text.length() ? text.length() : end; // ispitivanje pozicija pocetnih i krajnjih karaktera u svakom titlu
if (!isOverlapped(text, posStart, posEnd)) { //ako nema preklapanja stilova, udji u if i podesi stilizaciju
text.insert(posStart, "<b>");
text.insert(posEnd + 3, "</b>");
current->titlePointer->setText(text);
}
current = current->next;
}
string str = loadString();
this->head->top->addToStack(str);
}
else {
Node* current = this->head;
while (current != nullptr) {
if (current->titlePointer->getSelection() == true) {
string text = current->titlePointer->getText();
int posStart = start < 0 ? 0 : start;
int posEnd = end >= text.length() ? text.length() : end;// ispitivanje pozicija pocetnih i krajnjih karaktera u svakom titlu
if (!isOverlapped(text, posStart, posEnd)) {//ako nema preklapanja stilova, udji u if i podesi stilizaciju
text.insert(posStart, "<b>");
text.insert(posEnd + 3, "</b>");
current->titlePointer->setText(text);
}
}
current = current->next;
}
string str = loadString();
this->head->top->addToStack(str);
}
return;
}
void SubtitleEditor::italicText(int start, int end)
{
//ako nije selektovana lista, prodji kroz sve titlove, ako jeste, samo kroz selektovane titlove
if (!listSelected) {
Node* current = this->head;
while (current != nullptr) {
string text = current->titlePointer->getText();
int posStart = start < 0 ? 0 : start;
int posEnd = end >= text.length() ? text.length() : end;// ispitivanje pozicija pocetnih i krajnjih karaktera u svakom titlu
if (!isOverlapped(text, posStart, posEnd)) {//ako nema preklapanja stilova, udji u if i podesi stilizaciju
text.insert(posStart, "<i>");
text.insert(posEnd + 3, "</i>");
current->titlePointer->setText(text);
}
current = current->next;
}
string str = loadString();
this->head->top->addToStack(str);
}
else {
Node* current = this->head;
while (current != nullptr) {
if (current->titlePointer->getSelection() == true) {
string text = current->titlePointer->getText();
int posStart = start < 0 ? 0 : start;
int posEnd = end >= text.length() ? text.length() : end;// ispitivanje pozicija pocetnih i krajnjih karaktera u svakom titlu
if (!isOverlapped(text, posStart, posEnd)) {//ako nema preklapanja stilova, udji u if i podesi stilizaciju
text.insert(posStart, "<i>");
text.insert(posEnd + 3, "</i>");
current->titlePointer->setText(text);
}
}
current = current->next;
}
string str = loadString();
this->head->top->addToStack(str);
}
return;
}
void SubtitleEditor::underlineText(int start, int end)
{
//ako nije selektovana lista, prodji kroz sve titlove, ako jeste, samo kroz selektovane titlove
if (!listSelected) {
Node* current = this->head;
while (current != nullptr) {
string text = current->titlePointer->getText();
int posStart = start < 0 ? 0 : start;
int posEnd = end >= text.length() ? text.length() : end;// ispitivanje pozicija pocetnih i krajnjih karaktera u svakom titlu
if (!isOverlapped(text, posStart, posEnd)) {//ako nema preklapanja stilova, udji u if i podesi stilizaciju
text.insert(posStart, "<u>");
text.insert(posEnd + 3, "</u>");
current->titlePointer->setText(text);
}
current = current->next;
}
string str = loadString();
this->head->top->addToStack(str);
}
else {
Node* current = this->head;
while (current != nullptr) {
if (current->titlePointer->getSelection() == true) {
string text = current->titlePointer->getText();
int posStart = start < 0 ? 0 : start;
int posEnd = end >= text.length() ? text.length() : end;// ispitivanje pozicija pocetnih i krajnjih karaktera u svakom titlu
if (!isOverlapped(text, posStart, posEnd)) {//ako nema preklapanja stilova, udji u if i podesi stilizaciju
text.insert(posStart, "<u>");
text.insert(posEnd + 3, "</u>");
current->titlePointer->setText(text);
}
}
current = current->next;
}
string str = loadString();
this->head->top->addToStack(str);
}
return;
}
void SubtitleEditor::removeStyle()
{
Node* current = this->head;
while (current != nullptr) {
if (current->titlePointer->getSelection() == true) { //ako je titl selektovan, potrazi stilizacije, inace nemoj
string text = current->titlePointer->getText();
while (1) { // sve dok pronalazis stilove, izbaci ih iz tekstova titla
int pos = text.find('<');
if (pos == string::npos)
break;
text.erase(pos, 3);
pos = text.find('<');
text.erase(pos, 4);
}
current->titlePointer->setText(text);
}
current = current->next;
}
string str = loadString();
this->head->top->addToStack(str);
return;
}
void SubtitleEditor::fixLongLines(int max)
{
if (listSelected) {
Node* current = this->head;
while (current) {
if (current->titlePointer->getSelection()) {
string text = current->titlePointer->getText();
if (NeedsToBeFixed(text, max)) { //ako ima barem jednu liniju koja mora da se popravi, prepravi
int i = 0;
while (i < text.length()) { //prvo brisem sve nove redove
if (text[i] == '\n') text.erase(i, 1);
i++;
}
int cntChar = 0;
int cntBlanco = 0;
for (int i = 0; i < text.length(); i++) { //ako naidjem na oznake stilizacije, preskacem ih i ne racunam kao nebele znakove
if (text[i] == '<' && text[i + 2] == '>') {
i += 3;
continue;
}
if (text[i] == '<' && text[i + 3] == '>') {
i += 4;
continue;
}
if (text[i] != ' ') cntChar++; //brojim koliko ima nebelih karaktera
else cntBlanco++;//brojim koliko ima blanko znakova
if (cntChar == max) {//ako je broj nebelih karaktera jednak maksimumu, podesim checkpoint na onu poziciju do koje sam stigao, i onda idem unazad sve dok ne dodjem do ' ' i tu postavim '\n'
int checkpoint = i;
while (1) {
if (text[checkpoint] == ' ') {
text[checkpoint] = '\n';
i = checkpoint;
break;
}
checkpoint--;
}
cntChar = cntBlanco = 0;//broj blanko znakova u redu podesim na nulu
}
}
current->titlePointer->setText(text);//postavljam izmenjeni tekst u element liste
}
}
current = current->next;
}
string str = loadString();
this->head->top->addToStack(str);
}
else {
Node* current = this->head;
while (current) {
string text = current->titlePointer->getText();
if (NeedsToBeFixed(text, max)) {
int i = 0;
while (i < text.length()) {
if (text[i] == '\n') text.erase(i, 1);
i++;
}
int cntChar = 0;
int cntBlanco = 0;
for (int i = 0; i < text.length(); i++) {
if (text[i] == '<' && text[i + 2] == '>') {
i += 3;
continue;
}
if (text[i] == '<' && text[i + 3] == '>') {
i += 4;
continue;
}
if (text[i] != ' ') cntChar++;
else cntBlanco++;
if (cntChar == max) {
int checkpoint = i;
while (1) {
if (text[checkpoint] == ' ') {
text[checkpoint] = '\n';
i = checkpoint;
break;
}
checkpoint--;
}
cntChar = cntBlanco = 0;
}
}
current->titlePointer->setText(text);
}
current = current->next;
}
string str = loadString();
this->head->top->addToStack(str);
}
}
void SubtitleEditor::fixLongTime(int max)
{
if (listSelected) {
Node* current = this->head;
while (current) {
if (current->titlePointer->getSelection()) {
if (current->titlePointer->getTitleTimeLength() >= max) {//ako je dati titl u listi selektovan i prebacuje duzinu vremena, obradi ga
int n = current->titlePointer->getTitleTimeLength() / max;
while (n * max <= current->titlePointer->getTitleTimeLength()) n++;
// izracunat broj novih titlova
int length = current->titlePointer->getTitleTimeLength() / n; // duzina jednog novog titla
int beginTime = current->titlePointer->getTitleBeginningTime(); // pocetak u ms prvog titla
string text = current->titlePointer->getText();
while (1)//izbacivanje '\n' iz teksta i menjanje sa ' '
{
int pos = text.find('\n');
if (pos == string::npos)
break;
text[pos] = ' ';
}
int i = 0;
int words = 0; // izracunavanje ukupnog broja reci u titlu
while (text[i] != '\0') {
if (text[i] == '<' && text[i + 2] == '>') i += 3;
if (text[i] == '<' && text[i + 3 == '>']) i += 4;
if (text[i] == ' ') words++;
i++;
}
words++;
int m = words / n; // broj reci po titlu
if (m * n <= words) m++;
i = 0;
// azuriranje trenutnog titla (podesavanje novog krajnjeg vremena i nov tekst)
string newText = readWords(text, m, i);
current->titlePointer->setText(newText);
current->titlePointer->setEndingTime(length);
beginTime += length;
//obrada
for (int j = 0; j < n-1; j++) {
string titleText = readWords(text, m, i);
insertSubtitle(beginTime, length, titleText);
Node* tmp = this->head;
while (tmp) { //prelomljeni titl zadrzava selekciju, tmp-om trazim svaki novi titl
if (tmp->titlePointer->getTitleBeginningTime() == beginTime) {
tmp->titlePointer->selectTitle();
break;
}
tmp = tmp->next;
}
beginTime += length;
}
}
}
current = current->next;
}
string str = loadString();
this->head->top->addToStack(str);
}
else {
Node* current = this->head;
while (current) {
if (current->titlePointer->getTitleTimeLength() >= max) {
int n = current->titlePointer->getTitleTimeLength() / max;
while (n * max <= current->titlePointer->getTitleTimeLength()) n++;
int length = current->titlePointer->getTitleTimeLength() / n;
int beginTime = current->titlePointer->getTitleBeginningTime();
string text = current->titlePointer->getText();
while (1)
{
int pos = text.find('\n');
if (pos == string::npos)
break;
text[pos] = ' ';
}
int i = 0;
int words = 0;
while (text[i] != '\0') {
if (text[i] == '<' && text[i + 2] == '>') i += 3;
if (text[i] == '<' && text[i + 3 == '>']) i += 4;
if (text[i] == ' ') words++;
i++;
}
words++;
int m = words / n;
if (m * n <= words) m++;
i = 0;
string newText = readWords(text, m, i);
current->titlePointer->setText(newText);
current->titlePointer->setEndingTime(length);
beginTime += length;
for (int j = 0; j < n-1; j++) {
string titleText = readWords(text, m, i);
insertSubtitle(beginTime, length, titleText);
beginTime += length;
}
}
}
string str = loadString();
this->head->top->addToStack(str);
}
}
void SubtitleEditor::undo()
{
this->head->top->removeTop();
}
void SubtitleEditor::shiftForward(int ms)
{
Node* current = this->head;
if (this->listSelected) {
while (current) {
if (current->titlePointer->getSelection() == true) {
current->titlePointer->changeTitleTime(ms);
}
current = current->next;
}
string str = loadString();
this->head->top->addToStack(str);
}
else {
while (current) {
current->titlePointer->changeTitleTime(ms);
current = current->next;
}
string str = loadString();
this->head->top->addToStack(str);
}
}
void SubtitleEditor::shiftBackward(int ms)
{
int nms = -ms;
Node* current = this->head;
if (this->listSelected) {
while (current) {
if (current->titlePointer->getSelection() == true) {
current->titlePointer->changeTitleTime(nms);
}
current = current->next;
}
string str = loadString();
this->head->top->addToStack(str);
}
else {
while (current) {
current->titlePointer->changeTitleTime(nms);
current = current->next;
}
string str = loadString();
this->head->top->addToStack(str);
}
}
void SubtitleEditor::insertSubtitle(int start, int length, string text)
{
//kreiranje titla koji treba da se umetne
Node* insertion = new Node();
insertion->titlePointer = new Title(start, length, text);
insertion->next = nullptr;
Node* current = this->head;
while (current) {
//ako moze da se ubaci, isprevezuj pokazivace i azuriraj redne brojeve titlova
if (current->titlePointer->getTitleEndingTime() <= start && current->next->titlePointer->getTitleBeginningTime() >= start + length) {
insertion->next = current->next;
current->next = insertion;
//azuriranje rednih brojeva ostalih titlova iza umetnutog
int nextSerialNumber = current->titlePointer->getSerial() + 1;
Node* currentCopy = current->next;
while (currentCopy) {
currentCopy->titlePointer->setTitleSerial(nextSerialNumber);
nextSerialNumber++;
currentCopy = currentCopy->next;
}
break;
}
current = current->next;
};
string str = loadString();
this->head->top->addToStack(str);
}
void SubtitleEditor::deleteSubtitles()
{
if (this->head == nullptr)
return;
if (listSelected == false)
return;
int serial = this->head->titlePointer->getSerial();
//izbacivanje titlova od pocetka pa sve dok postoji selekcija
while (this->head && this->head->titlePointer->getSelection()) {
Node* tmp = this->head;
this->head = this->head->next;
delete tmp->titlePointer;
delete tmp;
}
if (this->head == nullptr)
return;
//namestanje novog head-a
this->head->titlePointer->setTitleSerial(serial);
Node* current = this->head;
//izbacivanje svih ostalih selektovanih titlova
while (current != nullptr && current->next != nullptr) {
if (current->next->titlePointer->getSelection() == true) {
Node* tmp = current->next;
current->next = tmp->next;
delete tmp->titlePointer;
delete tmp;
}
else {
current = current->next;
}
}
//azuriranje rednih brojeva preostalih titlova
current = this->head->next;
serial++;
while (current != nullptr) {
current->titlePointer->setTitleSerial(serial);
serial++;
current = current->next;
}
this->listSelected = false; //ne postoji selekcija, jer su svi selektovani titlovi uklonjeni
string str = loadString();
this->head->top->addToStack(str);
return;
}
void SubtitleEditor::merge(string subtitles)
{
SubtitleEditor* headTwo = new SubtitleEditor();
headTwo->loadSubtitles(subtitles);
//pronalazenje poslednjeg titla u prvom head-u i nadovezivanje novog na kraj prvog
Node* last = this->head;
while (last->next) last = last->next;
last->next = headTwo->head;
//azuriranje rednih brojeva titlova (nije receno da moraju i vremena da se azuriraju)
int nextSerialNumber = last->titlePointer->getSerial() + 1;
Node* currentCopy = last->next;
while (currentCopy) {
currentCopy->titlePointer->setTitleSerial(nextSerialNumber);
nextSerialNumber++;
currentCopy = currentCopy->next;
}
string str = loadString();
this->head->top->addToStack(str);
return;
}
bool SubtitleEditor::isOverlapped(string text, int start, int end)
{
for (int i = start; i < end - 4; i++) {
string pom1 = "" + text[i] + text[i + 1] + text[i + 2];
string pom2 = "" + text[i] + text[i + 1] + text[i + 2] + text[i + 3];
if (pom1 == "<b>" || pom2 == "</b>" || pom1 == "<i>" || pom2 == "</i>" || pom1 == "<u>" || pom2 == "</u>") {
return true;
}
}
return false;
}
bool SubtitleEditor::NeedsToBeFixed(string str, int max)
{
str.append("\n");
int i = 0;
int cntChar = 0;
while (i < str.length()) {
if (str[i] == '<' && str[i + 2] == '>') {
i += 3;
continue;
}
if (str[i] == '<' && str[i + 3] == '>') {
i += 4;
continue;
}
if (str[i] == '\n') cntChar = 0;
else if (str[i] != ' ') cntChar++;
if (cntChar == max) return true;
else i++;
}
if (cntChar == max) return true;
else return false;
}
string SubtitleEditor::readWords(string text, int number, int& counter)
{
string str = "";
int wordsRead = 0;
while (1) {
if (text[counter] == ' ') wordsRead++;
if (wordsRead == number || text[counter] == '\0') {
str.push_back('\n');
counter++;
break;
}
str.push_back(text[counter]);
counter++;
}
return str;
}
string SubtitleEditor::loadString()
{
string str;
Node* tmp = this->head;
while (tmp != nullptr) {
str.append(to_string(tmp->titlePointer->getSerial()));
str.append("\n");
str.append(tmp->titlePointer->getTitleTimes());
str.append(tmp->titlePointer->getText());
str.append("\n");
str.append("\n");
tmp = tmp->next;
}
return str;
}