Repository navigation
Expand file tree
/
Copy pathJavaScriptPluginUnit.pas
More file actions
2089 lines (1791 loc) · 65.9 KB
/
Copy pathJavaScriptPluginUnit.pas
File metadata and controls
2089 lines (1791 loc) · 65.9 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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -----------------------------------------------------------------------------
// VisualSubSync
// -----------------------------------------------------------------------------
// Copyright (C) 2003-2008 Christophe Paris
// -----------------------------------------------------------------------------
// This Program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.
//
// This Program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with GNU Make; see the file COPYING. If not, write to
// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
// http://www.gnu.org/copyleft/gpl.html
// -----------------------------------------------------------------------------
unit JavaScriptPluginUnit;
interface
uses Classes, js15decl, jsintf, SubStructUnit, TntClasses, TntSysUtils, Graphics, Types, Contnrs, TntActnList;
type
{$TYPEINFO ON}
TSubtitleRangeJSWrapper = class; // Forward declaration
TSubtitleRangeJSWrapperChangeStartEvent = procedure (Sender : TSubtitleRangeJSWrapper;
SubtitleRange : TSubtitleRange; NewValue : Integer) of object;
TSubtitleRangeJSWrapperChangeStopEvent = procedure (Sender : TSubtitleRangeJSWrapper;
SubtitleRange : TSubtitleRange; NewValue : Integer) of object;
TSubtitleRangeJSWrapperChangeTextEvent = procedure (Sender : TSubtitleRangeJSWrapper;
SubtitleRange : TSubtitleRange; NewValue : WideString) of object;
TSubtitleRangeJSWrapper = class(TObject)
private
FStrippedText : WideString;
FStrippedTextProcessed : Boolean;
FOnChangeStart : TSubtitleRangeJSWrapperChangeStartEvent;
FOnChangeStop : TSubtitleRangeJSWrapperChangeStopEvent;
FOnChangeText : TSubtitleRangeJSWrapperChangeTextEvent;
FSubtitleRange : TSubtitleRange;
function GetIndex : Integer;
function GetStart : Integer;
function GetStop : Integer;
function GetText : WideString;
function GetStrippedText : WideString;
procedure SetStart(Value : Integer);
procedure SetStop(Value : Integer);
procedure SetText(Value : WideString);
procedure SetSubtitle(Value : TSubtitleRange);
public
constructor Create;
property OnChangeStart : TSubtitleRangeJSWrapperChangeStartEvent read FOnChangeStart write FOnChangeStart;
property OnChangeStop : TSubtitleRangeJSWrapperChangeStopEvent read FOnChangeStop write FOnChangeStop;
property OnChangeText : TSubtitleRangeJSWrapperChangeTextEvent read FOnChangeText write FOnChangeText;
published
property Index : Integer read GetIndex;
property Start : Integer read GetStart write SetStart;
property Stop : Integer read GetStop write SetStop;
property Text : WideString read GetText write SetText;
property StrippedText : WideString read GetStrippedText write SetText;
end;
TSceneChangeWrapper = class(TObject)
private
FSceneChangeList : TIntegerDynArray;
FStartOffset, FStopOffset, FFilterOffset : Integer;
FVisible : Boolean;
function GetNextIndex(TimeMs : Integer; Backward : Boolean = False) : Integer;
public
procedure RegisterSceneChange(JSObject : TJSObject);
procedure SetSceneChangeList(SceneChangeList : TIntegerDynArray);
procedure SetOffsets(StartOffset, StopOffset, FilterOffset : Integer);
procedure SetVisible(Value : Boolean);
function Delete(StartTimeMs, StopTimeMs : Integer) : TIntegerDynArray;
procedure Insert(Src : TIntegerDynArray);
function GetSCArray : TIntegerDynArray;
published
function Contains(Start, Stop : Integer) : Boolean;
function GetCount : Integer;
function GetAt(Index : Integer) : Integer;
function GetNext(TimeMs : Integer) : Integer;
function GetPrevious(TimeMs : Integer) : Integer;
property StartOffset : Integer read FStartOffset;
property StopOffset : Integer read FStopOffset;
property FilterOffset : Integer read FFilterOffset;
property Visible : Boolean read FVisible;
end;
TVSSCoreWrapper = class(TObject)
private
FINDEX_COL_IDX : Integer;
FSTART_COL_IDX : Integer;
FSTOP_COL_IDX : Integer;
FSTYLE_COL_IDX : Integer;
FTEXT_COL_IDX : Integer;
FVO_COL_IDX : Integer;
FLAST_CORE_COL_IDX : Integer;
FMinimumBlank : Integer;
FCpsTarget : Integer;
FMinimumDuration : Integer;
FMaximumDuration : Integer;
FVideoWidth : Integer;
FVideoHeight : Integer;
FSubRangeWrapperPool : array[0..9] of TSubtitleRangeJSWrapper;
FSubRangeWrapperPoolIndex : Integer;
FOnSubtitleChangeStart : TSubtitleRangeJSWrapperChangeStartEvent;
FOnSubtitleChangeStop : TSubtitleRangeJSWrapperChangeStopEvent;
FOnSubtitleChangeText : TSubtitleRangeJSWrapperChangeTextEvent;
FVSSCoreEngine : TVSSCoreEngineIntf;
function MakeWrappedSub(SubtitleRange : TSubtitleRange) : TSubtitleRangeJSWrapper;
public
constructor Create;
destructor Destroy; override;
procedure Set_INDEX_COL_IDX(Value : Integer);
procedure Set_START_COL_IDX(Value : Integer);
procedure Set_STOP_COL_IDX(Value : Integer);
procedure Set_STYLE_COL_IDX(Value : Integer);
procedure Set_TEXT_COL_IDX(Value : Integer);
procedure Set_VO_COL_IDX(Value : Integer);
procedure Set_LAST_CORE_COL_IDX(Value : Integer);
procedure SetCpsTarget(Value : Integer);
procedure SetMinimumDuration(Value : Integer);
procedure SetMaximumDuration(Value : Integer);
procedure SetMinimumBlank(Value : Integer);
procedure SetVideoWidth(Value : Integer);
procedure SetVideoHeight(Value : Integer);
procedure RegisterJS(JSParent : TJSObject);
procedure SetOnSubtitleChangeStartEvent(Value : TSubtitleRangeJSWrapperChangeStartEvent);
procedure SetOnSubtitleChangeStopEvent(Value : TSubtitleRangeJSWrapperChangeStopEvent);
procedure SetOnSubtitleChangeTextEvent(Value : TSubtitleRangeJSWrapperChangeTextEvent);
procedure SetVSSCoreEngine(VSSCoreEngine : TVSSCoreEngineIntf);
property OnSubtitleChangeStart : TSubtitleRangeJSWrapperChangeStartEvent read FOnSubtitleChangeStart write SetOnSubtitleChangeStartEvent;
property OnSubtitleChangeStop : TSubtitleRangeJSWrapperChangeStopEvent read FOnSubtitleChangeStop write SetOnSubtitleChangeStopEvent;
property OnSubtitleChangeText : TSubtitleRangeJSWrapperChangeTextEvent read FOnSubtitleChangeText write SetOnSubtitleChangeTextEvent;
published
procedure RegisterJavascriptAction(AName : WideString;
ACaption : WideString; ADefaultShortcut : WideString);
function GetSubCount : Integer;
function GetSubAt(Index : Integer) : TSubtitleRangeJSWrapper;
function GetFirst : TSubtitleRangeJSWrapper;
function GetNext(Sub : TSubtitleRangeJSWrapper) : TSubtitleRangeJSWrapper;
function GetPrevious(Sub : TSubtitleRangeJSWrapper) : TSubtitleRangeJSWrapper;
function GetSelectedCount : Integer;
function GetFirstSelected : TSubtitleRangeJSWrapper;
function GetNextSelected(Sub : TSubtitleRangeJSWrapper) : TSubtitleRangeJSWrapper;
function DeleteSubtitle(Index : Integer) : Boolean;
function GetPluginParamValue(JsSection, JsParameter : WideString) : Integer;
procedure DisableJavascriptItemMenu(ACaption : WideString);
procedure EnableJavascriptItemMenu(ACaption : WideString);
procedure InsertBreakBeforeJavascriptMenuItem(ACaption : WideString);
procedure InsertBreakAfterJavascriptMenuItem(ACaption : WideString);
procedure SetPluginParamValue(JsSection, JsParameter : WideString; Value : Integer);
function GetCursorPosition : Integer;
procedure SetSubColor(StartIndex,LastIndex : Integer; Color : WideString);
procedure ResetSubColor;
procedure SetSubIcon(Index : Integer; UpdateIconIndex : Integer);
function MeasureStringWidth(FontName : WideString; FontSize : Integer;
Bold : Boolean; Text : WideString) : Integer;
function GetTextSelectionStart : Integer;
function GetTextSelectionLength : Integer;
function GetAudioCursorPosition : Integer;
property INDEX_COL_IDX : Integer read FINDEX_COL_IDX;
property START_COL_IDX : Integer read FSTART_COL_IDX;
property STOP_COL_IDX : Integer read FSTOP_COL_IDX;
property STYLE_COL_IDX : Integer read FSTYLE_COL_IDX;
property TEXT_COL_IDX : Integer read FTEXT_COL_IDX;
property VO_COL_IDX : Integer read FVO_COL_IDX;
property LAST_CORE_COL_IDX : Integer read FLAST_CORE_COL_IDX;
property CpsTarget : Integer read FCpsTarget;
property MinimumDuration : Integer read FMinimumDuration;
property MaximumDuration : Integer read FMaximumDuration;
property MinimumBlank : Integer read FMinimumBlank;
property VideoWidth : Integer read FVideoWidth;
property VideoHeight : Integer read FVideoHeight;
end;
{$TYPEINFO OFF}
TJSPluginParamType = (jsptUnknown, jsptBoolean, jsptInteger, jsptDouble, jsptWideString);
PJSPluginParam = ^TJSPluginParam;
TJSPluginParam = record
Name : WideString;
ParamType : TJSPluginParamType;
// Maybe use variant for the value
BooleanValue : Boolean;
IntegerValue : Integer;
DoubleValue : Double;
WideStringValue : WideString;
UnitStr : WideString;
Description : WideString;
end;
TJSPluginNotifyEvent = procedure (const Msg : WideString) of object;
TBaseJavascriptPlugin = class
protected
FEngine : TJSEngine;
FLastErrorMsg : WideString;
FFatalError : Boolean;
FVSSPluginObject : TJSObject;
FFilename : WideString;
FOnJSPluginError : TJSPluginNotifyEvent;
FLoadingStack : TStack; // Needed for nested include stuff
FPreInitDone : Boolean;
FFilenames : TTntStringList; // To keep unicode filenames
procedure OnJsError(eng : TJSEngine; cx: PJSContext; Msg: PChar;
report: PJSErrorReport);
// function called during the script loading just after compilation and before execution
procedure PreInitScript; virtual;
function InternalLoadScript(Filename : WideString) : Boolean;
function InternalLoadScripts(Filename : WideString) : Boolean;
function GetCurrentLoadingFile : WideString;
public
constructor Create;
destructor Destroy; override;
function LoadScript(Filename : WideString) : Boolean;
procedure CallJSFunction(JSFunctionName : WideString);
procedure Eval(Code : WideString);
property Filename : WideString read FFilename;
property LastErrorMsg : WideString read FLastErrorMsg;
property FatalError : Boolean read FFatalError;
property OnJSPluginError : TJSPluginNotifyEvent read FOnJSPluginError write FOnJSPluginError;
end;
// Error plugin
TJavaScriptPlugin = class(TBaseJavascriptPlugin)
private
FHasErrorFunc, FFixErrorFunc : TJSFunction;
FVSSPluginObject : TJSObject;
FCurrentSub, FPreviousSub, FNextSub : TSubtitleRangeJSWrapper;
FCurrentSubJS, FPreviousSubJS, FNextSubJS : TJSObject;
FParamCount : Integer;
FOnSubtitleChangeStart : TSubtitleRangeJSWrapperChangeStartEvent;
FOnSubtitleChangeStop : TSubtitleRangeJSWrapperChangeStopEvent;
FOnSubtitleChangeText : TSubtitleRangeJSWrapperChangeTextEvent;
FParamArray : array[0..2] of TJSBase; // used when calling a JS function
// ----- Plugin constant -----
FName : WideString;
FDescription : WideString;
FColor : Integer;
FMessage : WideString;
procedure FillParamArray(CurrentSub, PreviousSub, NextSub : TSubtitleRange);
procedure SetOnSubtitleChangeStartEvent(Value : TSubtitleRangeJSWrapperChangeStartEvent);
procedure SetOnSubtitleChangeStopEvent(Value : TSubtitleRangeJSWrapperChangeStopEvent);
procedure SetOnSubtitleChangeTextEvent(Value : TSubtitleRangeJSWrapperChangeTextEvent);
public
constructor Create;
destructor Destroy; override;
function LoadScript(Filename : WideString) : Boolean;
procedure FillParamList(ParamList : TList);
procedure SetParamValue(Name, Value : WideString);
function HasError(CurrentSub, PreviousSub, NextSub : TSubtitleRange) : WideString;
procedure FixError(CurrentSub, PreviousSub, NextSub : TSubtitleRange);
function CanFixError : Boolean;
property Filename;
property OnJSPluginError;
property LastErrorMsg;
property FatalError;
property Name : WideString read FName;
property Description : WideString read FDescription;
property Color : Integer read FColor;
property Msg : WideString read FMessage;
property OnSubtitleChangeStart : TSubtitleRangeJSWrapperChangeStartEvent read FOnSubtitleChangeStart write SetOnSubtitleChangeStartEvent;
property OnSubtitleChangeStop : TSubtitleRangeJSWrapperChangeStopEvent read FOnSubtitleChangeStop write SetOnSubtitleChangeStopEvent;
property OnSubtitleChangeText : TSubtitleRangeJSWrapperChangeTextEvent read FOnSubtitleChangeText write SetOnSubtitleChangeTextEvent;
end;
// General plugin
TSimpleJavascriptWrapper = class(TBaseJavascriptPlugin)
private
FNotifySubtitleModificationFunc : TJSFunction;
FNotifySelectionModificationFunc : TJSFunction;
FNotifyRangeStartDblClickFunc : TJSFunction;
FNotifyRangeStopDblClickFunc : TJSFunction;
FCurrentSub, FPreviousSub, FNextSub : TSubtitleRangeJSWrapper;
FCurrentSubJS, FPreviousSubJS, FNextSubJS : TJSObject;
FVSSPluginObject : TJSObject;
FParamArray : array[0..2] of TJSBase; // used when calling a JS function
FOnJSPluginSetStatusBarText : TJSPluginNotifyEvent;
FOnSubtitleChangeStart : TSubtitleRangeJSWrapperChangeStartEvent;
FOnSubtitleChangeStop : TSubtitleRangeJSWrapperChangeStopEvent;
FOnSubtitleChangeText : TSubtitleRangeJSWrapperChangeTextEvent;
// Columns
FGetColumnBGColor : TJSFunction;
FGetColumnText : TJSFunction;
FCoreColumnsCount : Integer;
FExtraColumnsCount : Integer;
FColumnsTitle : TWideStringDynArray;
FColumnsSize : TIntegerDynArray;
FColumnsBGColorized : TBooleanDynArray;
FColumnsCustomText : TBooleanDynArray;
FIndexParam : TJSInteger;
FIndexParamArray : array[0..0]of TJSBase;
FICPNParamArray : array[0..3]of TJSBase; // Index, CurrentSub, PreviousSub, NextSub
procedure FillParamArray(CurrentSub, PreviousSub, NextSub : TSubtitleRange);
procedure FillICPNParamArray(Index : Integer; CurrentSub, PreviousSub,
NextSub : TSubtitleRange);
procedure SetOnSubtitleChangeStartEvent(Value : TSubtitleRangeJSWrapperChangeStartEvent);
procedure SetOnSubtitleChangeStopEvent(Value : TSubtitleRangeJSWrapperChangeStopEvent);
procedure SetOnSubtitleChangeTextEvent(Value : TSubtitleRangeJSWrapperChangeTextEvent);
protected
// nothing for now
public
constructor Create;
destructor Destroy; override;
function LoadScript(Filename : WideString) : Boolean;
function NotifySubtitleModification(CurrentSub, PreviousSub, NextSub : TSubtitleRange) : WideString;
function NotifySelectionModification(CurrentSub, PreviousSub, NextSub : TSubtitleRange) : WideString;
function NotifyRangeStartDblClick(CurrentSub, PreviousSub, NextSub : TSubtitleRange) : WideString;
function NotifyRangeStopDblClick(CurrentSub, PreviousSub, NextSub : TSubtitleRange) : WideString;
function GetColumnTitle(Index : Integer) : WideString;
function GetColumnSize(Index : Integer) : Integer;
function IsColumnBGColorized(Index : Integer) : Boolean;
function HasColumnCustomText(Index : Integer) : Boolean;
function GetColumnBgColor(Index : Integer; CurrentSub, PreviousSub, NextSub : TSubtitleRange) : TColor;
function GetColumnText(Index : Integer; CurrentSub, PreviousSub, NextSub : TSubtitleRange) : WideString;
property CoreColumnsCount : Integer read FCoreColumnsCount write FCoreColumnsCount;
property ExtraColumnsCount : Integer read FExtraColumnsCount;
property OnJSPluginSetStatusBarText : TJSPluginNotifyEvent read FOnJSPluginSetStatusBarText write FOnJSPluginSetStatusBarText;
property OnSubtitleChangeStart : TSubtitleRangeJSWrapperChangeStartEvent read FOnSubtitleChangeStart write SetOnSubtitleChangeStartEvent;
property OnSubtitleChangeStop : TSubtitleRangeJSWrapperChangeStopEvent read FOnSubtitleChangeStop write SetOnSubtitleChangeStopEvent;
property OnSubtitleChangeText : TSubtitleRangeJSWrapperChangeTextEvent read FOnSubtitleChangeText write SetOnSubtitleChangeTextEvent;
end;
TAbstractJavaScriptPluginEnumerator = class
protected
FOnJSPluginError : TJSPluginNotifyEvent;
public
procedure Reset; virtual; abstract;
function GetNext(var JSP : TJavaScriptPlugin) : Boolean; virtual; abstract;
property OnJSPluginError : TJSPluginNotifyEvent read FOnJSPluginError write FOnJSPluginError;
end;
TJavaScriptPluginEnumerator = class(TAbstractJavaScriptPluginEnumerator)
private
FPluginPath : WideString;
FFindFileAttrs : Integer;
FSearchRec : TSearchRecW;
public
constructor Create(PluginPath : WideString);
destructor Destroy; override;
procedure Reset; override;
function GetNext(var JSP : TJavaScriptPlugin) : Boolean; override;
function GetPluginByName(Name : WideString) : TJavaScriptPlugin;
function GetPluginByFilename(Filename : WideString) : TJavaScriptPlugin;
end;
TJavascriptAction = class(TTntAction)
private
FJSObjectName : WideString;
public
property JSObjectName : WideString read FJSObjectName write FJSObjectName;
end;
function GetParamValueAsWideString(pParam : PJSPluginParam) : WideString;
procedure SetParamValueAsWideString(pParam : PJSPluginParam; Value : WideString);
function JSColorToTColor(RGBColor : Integer) : TColor;
var
g_SceneChangeWrapper : TSceneChangeWrapper;
g_VSSCoreWrapper : TVSSCoreWrapper;
implementation
uses SysUtils, Windows, WAVDisplayerUnit, MiscToolsUnit, GlobalUnit;
function IsValidSubRange(Sub : TSubtitleRange) : Boolean;
begin
Result := g_GlobalContext.SubList.IndexOf(Sub) <> -1;
end;
//------------------------------------------------------------------------------
function GetParamValueAsWideString(pParam : PJSPluginParam) : WideString;
begin
case pParam.ParamType of
jsptBoolean: Result := BoolToStr(pParam.BooleanValue);
jsptInteger: Result := IntToStr(pParam.IntegerValue);
jsptDouble: Result := FloatToStr(pParam.DoubleValue);
jsptWideString: Result := pParam.WideStringValue;
else Result := '';
end;
end;
//------------------------------------------------------------------------------
procedure SetParamValueAsWideString(pParam : PJSPluginParam; Value : WideString);
begin
case pParam.ParamType of
jsptBoolean: pParam.BooleanValue := StrToBool(Value);
jsptInteger: pParam.IntegerValue := StrToInt(Value);
jsptDouble: pParam.DoubleValue := StrToFloat(Value);
jsptWideString: pParam.WideStringValue := Value;
end;
end;
//------------------------------------------------------------------------------
function JSColorToTColor(RGBColor : Integer) : TColor;
begin
Result := RGBColor and $0000FF00;
Result := Result or ((RGBColor and $000000FF) shl 16);
Result := Result or ((RGBColor and $00FF0000) shr 16);
end;
//------------------------------------------------------------------------------
function _JS_ScriptLog(cx: PJSContext; obj: PJSObject; argc: uintN; argv, rval: pjsval): JSBool; cdecl;
var
jseng: TJSEngine;
jsplugin : TBaseJavascriptPlugin;
pstr : PJSString;
begin
if (argc = 1) then
begin
jseng := TJSEngine(PEngineData(JS_GetContextPrivate(cx))^);
jsplugin := TBaseJavascriptPlugin(jseng.UserData);
if Assigned(jsplugin.OnJSPluginError) then
begin
pstr := JS_ValueToString(cx, argv^);
jsplugin.OnJSPluginError(JSStringToString(pstr));
end;
end;
Result := JS_TRUE;
end;
//------------------------------------------------------------------------------
function _JS_SetStatusBarText(cx: PJSContext; obj: PJSObject; argc: uintN; argv, rval: pjsval): JSBool; cdecl;
var
jseng: TJSEngine;
jsplugin : TSimpleJavascriptWrapper;
Func : TJSPluginNotifyEvent;
pstr : PJSString;
begin
if (argc = 1) then
begin
jseng := TJSEngine(PEngineData(JS_GetContextPrivate(cx))^);
jsplugin := TSimpleJavascriptWrapper(jseng.UserData);
Func := jsplugin.OnJSPluginSetStatusBarText;
if Assigned(Func) then
begin
pstr := JS_ValueToString(cx, argv^);
jsplugin.OnJSPluginSetStatusBarText(JSStringToString(pstr));
end;
end;
Result := JS_TRUE;
end;
function _JS_LoadScript(cx: PJSContext; obj: PJSObject; argc: uintN; argv, rval: pjsval): JSBool; cdecl;
var
jseng: TJSEngine;
jsplugin : TBaseJavascriptPlugin;
pstr : PJSString;
begin
if (argc = 1) then
begin
jseng := TJSEngine(PEngineData(JS_GetContextPrivate(cx))^);
jsplugin := TBaseJavascriptPlugin(jseng.UserData);
pstr := JS_ValueToString(cx, argv^);
jsplugin.InternalLoadScripts(JSStringToString(pstr));
end;
Result := JS_TRUE;
end;
// =============================================================================
constructor TSubtitleRangeJSWrapper.Create;
begin
FStrippedText := '';
FStrippedTextProcessed := False;
FSubtitleRange := nil;
FOnChangeStart := nil;
FOnChangeStop := nil;
FOnChangeText := nil;
end;
function TSubtitleRangeJSWrapper.GetIndex : Integer;
begin
if Assigned(FSubtitleRange.Node) then
Result := FSubtitleRange.Node.Index + 1
else
Result := -1
end;
function TSubtitleRangeJSWrapper.GetStart : Integer;
begin
Result := FSubtitleRange.StartTime;
end;
function TSubtitleRangeJSWrapper.GetStop : Integer;
begin
Result := FSubtitleRange.StopTime;
end;
function TSubtitleRangeJSWrapper.GetText : WideString;
begin
Result := FSubtitleRange.Text;
end;
function TSubtitleRangeJSWrapper.GetStrippedText : WideString;
begin
if (FStrippedTextProcessed = False) then
begin
FStrippedText := StripTags(FSubtitleRange.Text);
FStrippedTextProcessed := True;
end;
Result := FStrippedText;
end;
procedure TSubtitleRangeJSWrapper.SetStart(Value : Integer);
begin
if (Value <> FSubtitleRange.StartTime) then
begin
if Assigned(FOnChangeStart) then
FOnChangeStart(Self, FSubtitleRange, Value);
FSubtitleRange.StartTime := Value;
end;
end;
procedure TSubtitleRangeJSWrapper.SetStop(Value : Integer);
begin
if (Value <> FSubtitleRange.StopTime) then
begin
if Assigned(FOnChangeStop) then
FOnChangeStop(Self, FSubtitleRange, Value);
FSubtitleRange.StopTime := Value;
end;
end;
procedure TSubtitleRangeJSWrapper.SetText(Value : WideString);
begin
if (Value <> FSubtitleRange.Text) then
begin
if Assigned(FOnChangeText) then
FOnChangeText(Self, FSubtitleRange, Value);
FSubtitleRange.Text := Value;
FStrippedTextProcessed := False;
end;
end;
procedure TSubtitleRangeJSWrapper.SetSubtitle(Value : TSubtitleRange);
begin
FSubtitleRange := Value;
FStrippedTextProcessed := False;
FStrippedText := '';
end;
// =============================================================================
constructor TBaseJavascriptPlugin.Create;
begin
inherited Create;
FLoadingStack := TStack.Create;
FFilenames := TTntStringList.Create;
FPreInitDone := False;
FEngine := TJSEngine.Create(256*1024);
FEngine.OnJSError := OnJsError;
FEngine.UserData := Self;
FEngine.Global.AddMethod('ScriptLog', _JS_ScriptLog, 1);
FEngine.Global.AddMethod('LoadScript', _JS_LoadScript, 1);
end;
//------------------------------------------------------------------------------
destructor TBaseJavascriptPlugin.Destroy;
begin
FEngine.Free;
FFilenames.Free;
FLoadingStack.Free;
inherited;
end;
//------------------------------------------------------------------------------
procedure TBaseJavascriptPlugin.OnJsError(eng : TJSEngine; cx: PJSContext;
Msg: PChar; report: PJSErrorReport);
var
ErrorTypeStr: WideString;
CurrentFile : WideString;
FileNameIndex : Integer;
begin
if JSReportIsException(report) then
begin
ErrorTypeStr := 'Exception';
FFatalError := True;
end
else if JSReportIsWarning(report) then
begin
ErrorTypeStr := 'Warning';
FFatalError := False;
end
else
begin
ErrorTypeStr := 'Error';
FFatalError := True;
end;
FileNameIndex := StrToIntDef(report^.filename, -1);
if (FileNameIndex >= 0) and (FileNameIndex < FFilenames.Count) then
begin
CurrentFile := FFilenames[FileNameIndex];
end
else if (FLoadingStack.Count > 0) then
begin
CurrentFile := PWideString(FLoadingStack.Peek)^;
end
else
begin
CurrentFile := FFilename;
end;
FLastErrorMsg := Format('%s in %s at line %d', [Msg, WideExtractFileName(CurrentFile), report^.lineno]);
if (report^.uclinebuf <> '') then
begin
FLastErrorMsg := FLastErrorMsg + CRLF + '-> ' + report^.uclinebuf;
end;
if Assigned(FOnJSPluginError) then
FOnJSPluginError(FLastErrorMsg);
end;
//------------------------------------------------------------------------------
procedure TBaseJavascriptPlugin.PreInitScript;
begin
// VSSCore
g_VSSCoreWrapper.RegisterJS(FEngine.Global);
end;
//------------------------------------------------------------------------------
function TBaseJavascriptPlugin.GetCurrentLoadingFile : WideString;
begin
if (FLoadingStack.Count > 0) then
begin
Result := PWideString(FLoadingStack.Peek)^;
end
else
begin
Result := FFilename;
end;
end;
//------------------------------------------------------------------------------
function TBaseJavascriptPlugin.InternalLoadScripts(Filename : WideString) : Boolean;
var CurrentFilePath : WideString;
FSearchPath : WideString;
FFindFileAttrs : Integer;
FSearchRec : TSearchRecW;
begin
if (Pos('*', Filename) > 0) then
begin
// Check if Filename is an absolute path
if not WideIsAbsolutePath(Filename) then
begin
// Resolve path to be absolute
CurrentFilePath := WideExtractFilePath(GetCurrentLoadingFile);
Filename := WideResolveRelativePath(CurrentFilePath, Filename);
end;
FSearchPath := WideIncludeTrailingBackslash(WideExtractFilePath(Filename));
FFindFileAttrs := faAnyFile;
FSearchRec.FindHandle := INVALID_HANDLE_VALUE;
if (WideFindFirst(Filename, FFindFileAttrs, FSearchRec) <> 0) then
WideFindClose(FSearchRec)
else
begin
while (FSearchRec.FindHandle <> INVALID_HANDLE_VALUE) do
begin
InternalLoadScript(FSearchPath + FSearchRec.Name);
if (WideFindNext(FSearchRec) <> 0) then
WideFindClose(FSearchRec);
end;
end;
Result := True;
end
else
begin
Result := InternalLoadScript(Filename);
end;
end;
function TBaseJavascriptPlugin.InternalLoadScript(Filename : WideString) : Boolean;
var Script : TJSScript;
PFilename : PWideString;
CurrentFilePath : WideString;
begin
Result := False;
// Check if Filename is an absolute path
if not WideIsAbsolutePath(Filename) then
begin
// Resolve path to be absolute
CurrentFilePath := WideExtractFilePath(GetCurrentLoadingFile);
Filename := WideResolveRelativePath(CurrentFilePath, Filename);
end;
if not WideFileExists(Filename) then
begin
if Assigned(FOnJSPluginError) then
begin
FOnJSPluginError('Can''t find ' + Filename);
end;
Exit;
end;
// Save the file name in a stack because spydermonkey doesn't support unicode filename
New(PFilename);
PFilename ^:= Filename;
FLoadingStack.Push(PFilename);
FFilenames.Add(Filename);
Script := TJSScript.Create;
Script.LoadRaw(Filename);
// try to compile the script, we pass the index of the filename to avoid unicode problems
Script.Compile(FEngine, IntToStr(FFilenames.Count-1));
if Script.Compiled then
begin
if not FPreInitDone then
begin
PreInitScript;
FPreInitDone := True;
end;
Result := Script.Execute(FEngine);
end
else
begin
// There is probably a pending exception, try to report it now
if JS_IsExceptionPending(FEngine.Context) = JS_TRUE then
begin
// We need an updated DLL to do that
JS_ReportPendingException(FEngine.Context);
end
end;
Script.Free;
FLoadingStack.Pop;
Dispose(PFilename);
end;
//------------------------------------------------------------------------------
function TBaseJavascriptPlugin.LoadScript(Filename : WideString) : Boolean;
begin
FFilename := Filename;
Result := InternalLoadScript(Filename);
end;
//------------------------------------------------------------------------------
procedure TBaseJavascriptPlugin.CallJSFunction(JSFunctionName : WideString);
var JSFunction : TJSFunction;
Dummy : Integer;
begin
JSFunction := FEngine.Global.GetFunction(JSFunctionName);
if Assigned(JSFunction) then
begin
JSFunction.Call([], Dummy);
FreeAndNil(JSFunction);
end;
end;
//------------------------------------------------------------------------------
procedure TBaseJavascriptPlugin.Eval(Code : WideString);
begin
FEngine.Global.Evaluate(Code);
end;
// =============================================================================
constructor TJavaScriptPlugin.Create;
begin
inherited Create;
FCurrentSub := TSubtitleRangeJSWrapper.Create;
FPreviousSub := TSubtitleRangeJSWrapper.Create;
FNextSub := TSubtitleRangeJSWrapper.Create;
FCurrentSubJS := nil; FPreviousSubJS := nil; FNextSubJS := nil;
FHasErrorFunc := nil; FFixErrorFunc := nil;
FVSSPluginObject := nil;
end;
//------------------------------------------------------------------------------
destructor TJavaScriptPlugin.Destroy;
begin
if Assigned(FCurrentSubJS) then
FreeAndNil(FCurrentSubJS);
if Assigned(FPreviousSubJS) then
FreeAndNil(FPreviousSubJS);
if Assigned(FNextSubJS) then
FreeAndNil(FNextSubJS);
if Assigned(FHasErrorFunc) then
FreeAndNil(FHasErrorFunc);
if Assigned(FFixErrorFunc) then
FreeAndNil(FFixErrorFunc);
if Assigned(FVSSPluginObject) then
FreeAndNil(FVSSPluginObject);
FNextSub.Free;
FPreviousSub.Free;
FCurrentSub.Free;
inherited;
end;
//------------------------------------------------------------------------------
procedure TJavaScriptPlugin.SetOnSubtitleChangeStartEvent(Value : TSubtitleRangeJSWrapperChangeStartEvent);
begin
if (@Value <> @FOnSubtitleChangeStart) then
begin
FOnSubtitleChangeStart := Value;
FCurrentSub.FOnChangeStart := Value;
FPreviousSub.FOnChangeStart := Value;
FNextSub.FOnChangeStart := Value;
end;
end;
//------------------------------------------------------------------------------
procedure TJavaScriptPlugin.SetOnSubtitleChangeStopEvent(Value : TSubtitleRangeJSWrapperChangeStopEvent);
begin
if (@Value <> @FOnSubtitleChangeStop) then
begin
FOnSubtitleChangeStop := Value;
FCurrentSub.FOnChangeStop := Value;
FPreviousSub.FOnChangeStop := Value;
FNextSub.FOnChangeStop := Value;
end;
end;
//------------------------------------------------------------------------------
procedure TJavaScriptPlugin.SetOnSubtitleChangeTextEvent(Value : TSubtitleRangeJSWrapperChangeTextEvent);
begin
if (@Value <> @FOnSubtitleChangeText) then
begin
FOnSubtitleChangeText := Value;
FCurrentSub.FOnChangeText := Value;
FPreviousSub.FOnChangeText := Value;
FNextSub.FOnChangeText := Value;
end;
end;
//------------------------------------------------------------------------------
function TJavaScriptPlugin.LoadScript(Filename : WideString) : Boolean;
begin
Result := inherited LoadScript(Filename);
if not Result then
Exit;
if FEngine.Global.GetProperty('VSSPlugin', FVSSPluginObject) then
begin
FCurrentSubJS := FVSSPluginObject.AddNativeObject(FCurrentSub, '__FCurrentSub');
FPreviousSubJS := FVSSPluginObject.AddNativeObject(FPreviousSub, '__FPreviousSub');
FNextSubJS := FVSSPluginObject.AddNativeObject(FNextSub, '__FNextSub');
FHasErrorFunc := FVSSPluginObject.GetFunction('HasError');
if FVSSPluginObject.IsFunction('FixError') then
FFixErrorFunc := FVSSPluginObject.GetFunction('FixError');
// ----- Plugin constant -----
FVSSPluginObject.GetProperty('Name', FName);
FVSSPluginObject.GetProperty('Description', FDescription);
FVSSPluginObject.GetProperty('Color', FColor);
FVSSPluginObject.GetProperty('Message', FMessage);
// Scene change
g_SceneChangeWrapper.RegisterSceneChange(FEngine.Global);
// We need at least one function
Result := (FHasErrorFunc <> nil);
end;
end;
//------------------------------------------------------------------------------
procedure TJavaScriptPlugin.FillParamList(ParamList : TList);
var ListEnum : TStringArray;
i : Integer;
pPluginParam : PJSPluginParam;
ParamObject : TJSObject;
Desc : WideString;
begin
ParamList.Clear;
FParamCount := 0;
ListEnum := FVSSPluginObject.Enumerate;
for i := 0 to Length(ListEnum)-1 do
begin
if Pos('Param', ListEnum[i]) = 1 then
begin
FVSSPluginObject.GetProperty(ListEnum[i], ParamObject);
if not Assigned(ParamObject) then
Continue;
pPluginParam := New(PJSPluginParam);
ZeroMemory(pPluginParam, SizeOf(TJSPluginParam));
pPluginParam.Name := ListEnum[i];
ParamObject.GetProperty('Unit', pPluginParam.UnitStr);
ParamObject.GetProperty('Description', Desc);
if (Desc <> 'undefined') then
begin
pPluginParam.Description := Desc;
end;
case ParamObject.TypeOf('Value') of
JSTYPE_NUMBER:
if (ParamObject.IsInteger('Value')) then
begin
pPluginParam.ParamType := jsptInteger;
ParamObject.GetProperty('Value', pPluginParam.IntegerValue);
end else begin
pPluginParam.ParamType := jsptDouble;
ParamObject.GetProperty('Value', pPluginParam.DoubleValue);
end;
JSTYPE_STRING:
begin
pPluginParam.ParamType := jsptWideString;
ParamObject.GetProperty('Value', pPluginParam.WideStringValue);
end;
JSTYPE_BOOLEAN:
begin
pPluginParam.ParamType := jsptBoolean;
ParamObject.GetProperty('Value', pPluginParam.BooleanValue);
end;
else
pPluginParam.ParamType := jsptUnknown;
end;
if (pPluginParam.ParamType <> jsptUnknown) then
begin
ParamList.Add(pPluginParam);
Inc(FParamCount);
end
else
Dispose(pPluginParam);
//FreeAndNil(ParamObject);
end;
end;
SetLength(ListEnum, 0);
end;
//------------------------------------------------------------------------------