This repository has been archived by the owner on Jul 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
vips.js
3467 lines (3467 loc) · 138 KB
/
vips.js
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
/**
* Based on CSSBox' "Box" class with minimal set of features to support VIPS.
*/
var Box = /** @class */ (function () {
function Box(node) {
this.node = node;
this.rootelem = false;
if (this.node.nodeType === Node.ELEMENT_NODE) {
this.isblock = window.getComputedStyle(this.node).getPropertyValue("display") === "block";
}
else {
this.isblock = false;
}
this.displayed = true;
this.visible = true;
this.splitted = false;
}
Box.prototype.getNode = function () {
return this.node;
};
Box.prototype.isRootElement = function () {
return this.rootelem;
};
Box.prototype.isBlock = function () {
return this.isblock;
};
Box.prototype.isDisplayed = function () {
return this.displayed;
};
Box.prototype.isVisible = function () {
return this.visible;
};
Box.prototype.makeRoot = function () {
this.rootelem = true;
};
return Box;
}());
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
///<reference path="Box.ts"/>
/**
* Based on CSSBox' "ElementBox" class with minimal set of features to support VIPS.
*/
var ElementBox = /** @class */ (function (_super) {
__extends(ElementBox, _super);
function ElementBox(n) {
var _this = _super.call(this, n) || this;
if (n != null) {
_this.el = n;
_this.nested = _this.getVisibleElementBoxNested(n);
// the body contains all text nodes as children, but we want to keep the hierarchy, so only parse text boxes for non-body elements
if (n.nodeName.toLowerCase() != "body") {
_this.getTextBoxNested(n);
}
var elem = _this.el;
// https://stackoverflow.com/a/33456469
_this.visible = !!(elem.offsetWidth || elem.offsetHeight || elem.getClientRects().length);
_this.startChild = 0;
_this.endChild = _this.nested.length;
}
return _this;
}
ElementBox.prototype.getVisibleElementBoxNested = function (n) {
var ret = new Array();
var rec;
for (var i = 0; i < n.childNodes.length; i++) {
var child = n.childNodes.item(i);
if (child.nodeType === Node.ELEMENT_NODE) {
var el = child;
// https://stackoverflow.com/a/33456469
if (!!(el.offsetWidth || el.offsetHeight || el.getClientRects().length)) {
ret.push(new ElementBox(child));
}
}
}
if (ret.length === 0) { // only go deeper if we didn't find a visible box yet
for (var j = 0; j < n.childNodes.length; j++) {
var child = n.childNodes.item(j);
rec = this.getVisibleElementBoxNested(child);
while (rec.length > 0) {
ret.push(rec.pop());
}
}
}
return ret;
};
ElementBox.prototype.getTextBoxNested = function (n) {
for (var i = 0; i < n.childNodes.length; i++) {
var element = n.childNodes.item(i);
if (element.nodeType === Node.TEXT_NODE) {
this.nested.push(new TextBox(element));
}
}
};
ElementBox.prototype.getText = function () {
var ret = "";
for (var i = this.startChild; i < this.endChild; ++i) {
ret = ret.concat(this.getSubBox(i).getText());
}
return ret;
};
ElementBox.prototype.getElement = function () {
return this.el;
};
ElementBox.prototype.getSubBox = function (index) {
return this.nested[index];
};
ElementBox.prototype.getSubBoxList = function () {
return this.nested;
};
return ElementBox;
}(Box));
/**
* Simple Point class.
*/
var Point = /** @class */ (function () {
function Point(x, y) {
this.x = 0;
this.y = 0;
this.x = x;
this.y = y;
}
return Point;
}());
///<reference path="Point.ts"/>
/**
* Class that represents visual separator.
*
* @author Tomas Popela
* @author Lars Meyer
*/
var Separator = /** @class */ (function () {
function Separator(start, end, weight, leftUpX, leftUpY, rightDownX, rightDownY) {
this.startPoint = 0;
this.endPoint = 0;
this.weight = 3;
this.normalizedWeight = 0;
if (typeof start === 'undefined' || typeof end === 'undefined') {
if (!(typeof leftUpX === 'undefined' || typeof leftUpY === 'undefined' || typeof rightDownX === 'undefined' || typeof rightDownY === 'undefined')) {
this.leftUp = new Point(leftUpX, leftUpY);
this.rightDown = new Point(rightDownX, rightDownY);
this.startPoint = leftUpX;
this.endPoint = rightDownY;
}
}
else {
this.startPoint = start;
this.endPoint = end;
}
if (!(typeof weight === 'undefined')) {
this.weight = weight;
}
}
Separator.prototype.setLeftUp = function (leftUpX, leftUpY) {
this.leftUp = new Point(leftUpX, leftUpY);
};
Separator.prototype.setRightDown = function (rightDownX, rightDownY) {
this.rightDown = new Point(rightDownX, rightDownY);
};
Separator.prototype.compareTo = function (otherSeparator) {
return this.weight - otherSeparator.weight;
};
return Separator;
}());
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
///<reference path="Box.ts"/>
/**
* Based on CSSBox' "TextBox" class with minimal set of features to support VIPS.
*/
var TextBox = /** @class */ (function (_super) {
__extends(TextBox, _super);
function TextBox(n) {
var _this = _super.call(this, n) || this;
_this.textNode = n;
_this.transform = "none";
_this.setWhiteSpace("normal");
_this.collapsews = true;
return _this;
}
TextBox.prototype.getText = function () {
return this.text != null ? this.text.substring(this.textStart, this.textEnd) : "";
};
TextBox.prototype.setWhiteSpace = function (value) {
this.splitws = value === "normal" || value === "pre-wrap" || value === "pre-line";
this.collapsews = value === "normal" || value === "nowrap" || value === "pre-line";
this.linews = value === "normal" || value === "nowrap";
if (!this.splitted) {
this.applyWhiteSpace();
}
// TODO: implement computeLineLengths() and computeMinimalWidth() as well as computeMaximalWidth()
};
TextBox.prototype.applyWhiteSpace = function () {
this.text = this.applyTransformations(this.collapseWhitespaces(this.node.nodeValue));
this.textStart = 0;
this.textEnd = this.text.length;
this.isempty = this.textEnd === 0;
};
TextBox.prototype.collapseWhitespaces = function (src) {
var ret = "";
var inws = false;
for (var i = 0; i < src.length; ++i) {
var ch = src.charAt(i);
if (this.collapsews && /\s/.test(ch)) {
if (!inws) {
ret = ret.concat(' ');
inws = true;
}
}
else if (this.isLineBreak(ch)) {
ret = ret.concat('\r');
if (ch === '\r' && i + 1 < src.length && src.charAt(i + 1) === '\n') {
++i;
}
}
else {
inws = false;
ret = ret.concat(ch);
}
}
return ret;
};
TextBox.prototype.applyTransformations = function (src) {
switch (this.transform) {
case "lowercase":
return src.toLowerCase();
case "uppercase":
return src.toUpperCase();
case "capitalize":
var ret = "";
var ws = true;
for (var i = 0; i < src.length; ++i) {
var ch = src.charAt(i);
if (/\s/.test(ch)) {
ws = true;
}
else {
if (ws) {
ch = ch.toUpperCase();
}
ws = false;
}
ret.concat(ch);
}
return ret;
default:
return src;
}
};
TextBox.prototype.isWhitespace = function (ch) {
if (this.linews) {
return /\s/.test(ch) && (!(ch === '\r' || ch === '\n'));
}
else {
return /\s/.test(ch);
}
};
TextBox.prototype.isLineBreak = function (ch) {
if (this.linews) {
return false;
}
else {
return ch === '\r' || ch === '\n';
}
};
return TextBox;
}(Box));
///<reference path="VipsParser.ts"/>
///<reference path="VisualStructureConstructor.ts"/>
///<reference path="VipsOutput.ts"/>
/**
* Vision-based Page Segmentation algorithm
*
* @author Tomas Popela
* @author Lars Meyer
*/
var Vips = /** @class */ (function () {
function Vips(filename) {
this._pDoC = 11;
this._filename = "";
this.sizeThresholdWidth = 350;
this.sizeThresholdHeight = 400;
this._filename = filename;
}
/**
* Sets permitted degree of coherence (pDoC) value.
* @param value pDoC value.
*/
Vips.prototype.setPredefinedDoC = function (value) {
if (value <= 0 || value > 11) {
console.error("pDoC value must be between 1 and 11! Not " + value + "!");
return;
}
else {
this._pDoC = value;
}
};
Vips.prototype.setOutputFileName = function (filename) {
if (!(filename === "")) {
this._filename = filename;
}
else {
console.log("Invalid filename!");
}
};
/**
* Performs page segmentation.
*/
Vips.prototype.performSegmentation = function () {
try {
var numberOfIterations = 10;
var pageWidth = window.innerWidth;
var pageHeight = window.innerHeight;
var vipsParser = new VipsParser();
var constructor = new VisualStructureConstructor(undefined, this._pDoC);
for (var iterationNumber = 1; iterationNumber < numberOfIterations + 1; iterationNumber++) {
//visual blocks detection
vipsParser.setSizeThresholdHeight(this.sizeThresholdHeight);
vipsParser.setSizeThresholdWidth(this.sizeThresholdWidth);
vipsParser.parse();
var vipsBlocks = vipsParser.getVipsBlocks();
if (iterationNumber === 1) {
// visual structure construction
constructor.setVipsBlocks(vipsBlocks);
constructor.setPageSize(pageWidth, pageHeight);
}
else {
vipsBlocks = vipsParser.getVipsBlocks();
constructor.updateVipsBlocks(vipsBlocks);
}
// visual structure construction
constructor.constructVisualStructure();
// prepare thresholds for next iteration
if (iterationNumber <= 5) {
this.sizeThresholdHeight -= 50;
this.sizeThresholdWidth -= 50;
}
if (iterationNumber == 6) {
this.sizeThresholdHeight = 100;
this.sizeThresholdWidth = 100;
}
if (iterationNumber == 7) {
this.sizeThresholdHeight = 80;
this.sizeThresholdWidth = 80;
}
if (iterationNumber == 8) {
this.sizeThresholdHeight = 40;
this.sizeThresholdWidth = 10;
}
if (iterationNumber == 9) {
this.sizeThresholdHeight = 1;
this.sizeThresholdWidth = 1;
}
}
constructor.normalizeSeparatorsMinMax();
var vipsOutput = new VipsOutput(this._filename, this._pDoC);
return vipsOutput.writeJSON(constructor.getVisualStructure());
}
catch (Error) {
console.error("Something's wrong!");
console.error(Error.message);
console.error(Error.stack);
}
};
return Vips;
}());
///<reference path="Box.ts"/>
///<reference path="ElementBox.ts"/>
///<reference path="TextBox.ts"/>
/**
* Class that represents block on page.
*
* @author Tomas Popela
* @author Lars Meyer
*/
var VipsBlock = /** @class */ (function () {
function VipsBlock(id, node) {
// rendered Box that corresponds to DOM element
this._box = null;
// children of this node
this._children = null;
// node id
this._id = 0;
// node's Degree of Coherence
this._DoC = 0;
// number of images in node
this._containImg = 0;
// if node is image
this._isImg = false;
// if node is visual block
this._isVisualBlock = false;
// if node contains table
this._containTable = false;
// number of paragraphs in node
this._containP = 0;
// if node was already divided
this._alreadyDivided = false;
// if node can be divided
this._isDividable = true;
this._bgColor = null;
this._sourceIndex = 0;
this._tmpSrcIndex = 0;
// length of text in node
this._textLen = 0;
// length of text in links in node
this._linkTextLen = 0;
this._children = new Array();
if (!(typeof id === 'undefined')) {
this.setId(id);
}
if (!(typeof node === 'undefined')) {
this.addChild(node);
}
}
/**
* Sets block as visual block
* @param isVisualBlock Value
*/
VipsBlock.prototype.setIsVisualBlock = function (isVisualBlock) {
this._isVisualBlock = isVisualBlock;
this.checkProperties();
};
/**
* Checks if block is visual block
* @return True if block is visual block, otherwise false
*/
VipsBlock.prototype.isVisualBlock = function () {
return this._isVisualBlock;
};
/**
* Checks the properties of visual block
*/
VipsBlock.prototype.checkProperties = function () {
this.checkIsImg();
this.checkContainImg(this);
this.checkContainTable(this);
this.checkContainP(this);
this._linkTextLen = 0;
this._textLen = 0;
this.countTextLength(this);
this.countLinkTextLength(this);
this.setSourceIndex(this.getBox().getNode().ownerDocument);
};
/**
* Checks if visual block is an image.
*/
VipsBlock.prototype.checkIsImg = function () {
this._isImg = (this._box.getNode().nodeName.toLowerCase() === "img");
};
/**
* Checks if visual block contains image.
* @param vipsBlock Visual block
*/
VipsBlock.prototype.checkContainImg = function (vipsBlock) {
if (vipsBlock.getBox().getNode().nodeName.toLowerCase() === "img") {
vipsBlock._isImg = true;
this._containImg++;
}
for (var _i = 0, _a = vipsBlock.getChildren(); _i < _a.length; _i++) {
var childVipsBlock = _a[_i];
this.checkContainImg(childVipsBlock);
}
};
/**
* Checks if visual block contains table.
* @param vipsBlock Visual block
*/
VipsBlock.prototype.checkContainTable = function (vipsBlock) {
if (vipsBlock.getBox().getNode().nodeName.toLowerCase() === "table") {
this._containTable = true;
}
for (var _i = 0, _a = vipsBlock.getChildren(); _i < _a.length; _i++) {
var childVipsBlock = _a[_i];
this.checkContainTable(childVipsBlock);
}
};
/**
* Checks number of paragraphs in visual block.
* @param vipsBlock Visual block
*/
VipsBlock.prototype.checkContainP = function (vipsBlock) {
if (vipsBlock.getBox().getNode().nodeName.toLowerCase() === "p") {
this._containP++;
}
for (var _i = 0, _a = vipsBlock.getChildren(); _i < _a.length; _i++) {
var childVipsBlock = _a[_i];
this.checkContainP(childVipsBlock);
}
};
/**
* Counts length of text in links in visual block
* @param vipsBlock Visual block
*/
VipsBlock.prototype.countLinkTextLength = function (vipsBlock) {
if (vipsBlock.getBox().getNode().nodeName.toLowerCase() === "a") {
this._linkTextLen += vipsBlock.getBox().getText().length;
}
for (var _i = 0, _a = vipsBlock.getChildren(); _i < _a.length; _i++) {
var childVipsBlock = _a[_i];
this.countLinkTextLength(childVipsBlock);
}
};
/**
* Counts length of text in visual block
* @param vipsBlock Visual block
*/
VipsBlock.prototype.countTextLength = function (vipsBlock) {
this._textLen = vipsBlock.getBox().getText().replace("\n", "").length;
};
/**
* Adds new child to blocks' children
* @param child New child
*/
VipsBlock.prototype.addChild = function (child) {
this._children.push(child);
};
/**
* Gets all blocks' children
* @return List of children
*/
VipsBlock.prototype.getChildren = function () {
return this._children;
};
/**
* Sets block's corresponding Box
* @param box Box
*/
VipsBlock.prototype.setBox = function (box) {
this._box = box;
};
/**
* Gets Box corresponding to the block
* @return Box
*/
VipsBlock.prototype.getBox = function () {
return this._box;
};
/**
* Gets ElementBox corresponding to the block
* @return ElementBox
*/
VipsBlock.prototype.getElementBox = function () {
if (this._box instanceof ElementBox) {
return this._box;
}
else {
return null;
}
};
/**
* Sets block's id
* @param id Id
*/
VipsBlock.prototype.setId = function (id) {
this._id = id;
};
/**
* Returns block's degree of coherence DoC
* @return Degree of coherence
*/
VipsBlock.prototype.getDoC = function () {
return this._DoC;
};
/**
* Sets block's degree of coherence
* @param doC Degree of coherence
*/
VipsBlock.prototype.setDoC = function (doC) {
this._DoC = doC;
};
/**
* Checks if block is dividable
* @return True if dividable, otherwise false
*/
VipsBlock.prototype.isDividable = function () {
return this._isDividable;
};
/**
* Sets dividability of block
* @param isDividable True if dividable, otherwise false
*/
VipsBlock.prototype.setIsDividable = function (isDividable) {
this._isDividable = isDividable;
};
/**
* Checks if node was already divided
* @return True if block was divided, otherwise false
*/
VipsBlock.prototype.isAlreadyDivided = function () {
return this._alreadyDivided;
};
/**
* Sets if block was divided
* @param alreadyDivided True if block was divided, otherwise false
*/
VipsBlock.prototype.setAlreadyDivided = function (alreadyDivided) {
this._alreadyDivided = alreadyDivided;
};
/**
* Finds background color of element
* @param element Element
*/
VipsBlock.prototype.findBgColor = function (element) {
var backgroundColor = element.getAttribute("background-color");
if (backgroundColor.length === 0) {
if (element.parentNode != null && !(element.tagName === "body")) {
this.findBgColor(element.parentNode);
}
else {
this._bgColor = "#ffffff";
return;
}
}
else {
this._bgColor = backgroundColor;
return;
}
};
/**
* Gets background color of element
* @return Background color
*/
VipsBlock.prototype.getBgColor = function () {
if (this._bgColor != null) {
return this._bgColor;
}
if (this.getBox() instanceof TextBox) {
this._bgColor = "#ffffff";
}
else {
this._bgColor = window.getComputedStyle(this.getElementBox().getElement()).getPropertyValue("background-color");
}
if (this._bgColor.length === 0) {
this.findBgColor(this.getElementBox().getElement());
}
return this._bgColor;
};
/**
* Sets source index of block
* @param node Node
*/
VipsBlock.prototype.setSourceIndex = function (node) {
if (!(this.getBox().getNode() == node)) {
this._tmpSrcIndex++;
}
else {
this._sourceIndex = this._tmpSrcIndex;
}
for (var i = 0; i < node.childNodes.length; i++) {
this.setSourceIndex(node.childNodes[i]);
}
};
return VipsBlock;
}());
///<reference path="VisualStructure.ts"/>
/**
* Class that handles JSON output of VIPS algorithm
*
* @author Lars Meyer
* @author Tomas Popela
*/
var VipsOutput = /** @class */ (function () {
function VipsOutput(id, pDoC) {
this._pDoC = 0;
this._id = id;
this.setPDoC(pDoC);
}
VipsOutput.prototype.writeVisualJSONBlocks = function (segmentations, visualStructure) {
var multiPolygonSet = new Array();
var multiPolygon = new Array();
var polygon = new Array();
var start = new Array(Math.round(visualStructure.getX()), Math.round(visualStructure.getY()));
polygon.push(start);
polygon.push(new Array(Math.round(visualStructure.getX()), Math.round(visualStructure.getY() + visualStructure.getHeight())));
polygon.push(new Array(Math.round(visualStructure.getX() + visualStructure.getWidth()), Math.round(visualStructure.getY() + visualStructure.getHeight())));
polygon.push(new Array(Math.round(visualStructure.getX() + visualStructure.getWidth()), Math.round(visualStructure.getY())));
polygon.push(start);
multiPolygon.push(polygon);
multiPolygonSet.push(multiPolygon);
segmentations.push(multiPolygonSet);
if (this._pDoC >= visualStructure.getDoC()) {
// continue segmenting
for (var _i = 0, _a = visualStructure.getChildrenVisualStructures(); _i < _a.length; _i++) {
var child = _a[_i];
this.writeVisualJSONBlocks(segmentations, child);
}
} // else "stop" segmentation
};
VipsOutput.prototype.writeJSON = function (visualStructure) {
var boxes = new Array();
for (var _i = 0, _a = visualStructure.getChildrenVisualStructures(); _i < _a.length; _i++) {
var child = _a[_i];
this.writeVisualJSONBlocks(boxes, child);
}
return JSON.stringify({ "id": this._id, "height": window.innerHeight, "width": window.innerWidth, "segmentations": { "vips": boxes } });
};
/**
* Sets permitted degree of coherence pDoC
* @param pDoC pDoC value
*/
VipsOutput.prototype.setPDoC = function (pDoC) {
if (pDoC <= 0 || pDoC > 11) {
console.error("pDoC value must be between 1 and 11! Not " + pDoC + "!");
return;
}
else {
this._pDoC = pDoC;
}
};
return VipsOutput;
}());
///<reference path="VipsBlock.ts"/>
/**
* Class that parses blocks on page and finds visual blocks.
* @author Tomas Popela
* @author Lars Meyer
*/
var VipsParser = /** @class */ (function () {
/**
* Default constructor
*/
function VipsParser() {
this._vipsBlocks = null;
this._currentVipsBlock = null;
this._tempVipsBlock = null;
this._sizeThresholdWidth = 0;
this._sizeThresholdHeight = 0;
this._visualBlocksCount = 0;
this._pageWidth = 0;
this._pageHeight = 0;
this._cnt = 0;
this._vipsBlocks = new VipsBlock();
this._sizeThresholdHeight = 80;
this._sizeThresholdWidth = 80;
this._pageWidth = window.innerWidth;
this._pageHeight = window.innerHeight;
}
/**
* Starts visual page segmentation on given page
*/
VipsParser.prototype.parse = function () {
this._vipsBlocks = new VipsBlock();
this._visualBlocksCount = 0;
this.constructVipsBlockTree(new ElementBox(document.getElementsByTagName("body").item(0)), this._vipsBlocks);
this.divideVipsBlockTree(this._vipsBlocks);
this.getVisualBlocksCount(this._vipsBlocks);
};
/**
* Counts number of visual blocks in visual structure
* @param vipsBlock Visual structure
*/
VipsParser.prototype.getVisualBlocksCount = function (vipsBlock) {
if (vipsBlock.isVisualBlock()) {
this._visualBlocksCount++;
}
for (var _i = 0, _a = vipsBlock.getChildren(); _i < _a.length; _i++) {
var vipsBlockChild = _a[_i];
if (!(vipsBlockChild.getBox() instanceof TextBox)) {
this.getVisualBlocksCount(vipsBlockChild);
}
}
};
/**
* Construct VIPS block tree from viewport.
*
* Starts from <body> element.
* @param element Box that represents element
* @param node Visual structure tree node
*/
VipsParser.prototype.constructVipsBlockTree = function (element, node) {
node.setBox(element);
if (!(element instanceof TextBox)) {
for (var _i = 0, _a = element.getSubBoxList(); _i < _a.length; _i++) {
var box = _a[_i];
node.addChild(new VipsBlock());
this.constructVipsBlockTree(box, node.getChildren()[node.getChildren().length - 1]);
}
}
};
/**
* Tries to divide DOM elements and finds visual blocks.
* @param vipsBlock Visual structure
*/
VipsParser.prototype.divideVipsBlockTree = function (vipsBlock) {
this._currentVipsBlock = vipsBlock;
var elementBox = vipsBlock.getBox();
// With VIPS rules it tries to determine if element is dividable
if (this.applyVipsRules(elementBox) && vipsBlock.isDividable() && !vipsBlock.isVisualBlock()) {
// if element is dividable, let's divide it
this._currentVipsBlock.setAlreadyDivided(true);
for (var _i = 0, _a = vipsBlock.getChildren(); _i < _a.length; _i++) {
var vipsBlockChild = _a[_i];
if (!(vipsBlockChild.getBox() instanceof TextBox)) {
this.divideVipsBlockTree(vipsBlockChild);
}
}
}
else {
if (vipsBlock.isDividable()) {
vipsBlock.setIsVisualBlock(true);
vipsBlock.setDoC(11);
}
if (!this.verifyValidity(elementBox)) {
this._currentVipsBlock.setIsVisualBlock(false);
}
}
};
VipsParser.prototype.getAllTextLength = function (node) {
var childrenTextNodes = new Array();
this.findTextChildrenNodes(node, childrenTextNodes);
var textLength = 0;
for (var _i = 0, childrenTextNodes_1 = childrenTextNodes; _i < childrenTextNodes_1.length; _i++) {
var child = childrenTextNodes_1[_i];
var childText = child.getText();
if (!(childText === "") && !(childText === " ") && !(childText === "\n")) {
textLength += childText.length;
}
}
return textLength;
};
VipsParser.prototype.getAllChildren = function (node, children) {
children.push(node);
if (node instanceof TextBox) {
return;
}
for (var _i = 0, _a = node.getSubBoxList(); _i < _a.length; _i++) {
var child = _a[_i];
this.getAllChildren(child, children);
}
};
VipsParser.prototype.verifyValidity = function (node) {
if (node.getElement().getBoundingClientRect().left < 0 || node.getElement().getBoundingClientRect().top < 0) {
return false;
}
if (node.getElement().getBoundingClientRect().left + node.getElement().getBoundingClientRect().width > this._pageWidth) {
return false;
}
if (node.getElement().getBoundingClientRect().top + node.getElement().getBoundingClientRect().height > this._pageHeight) {
return false;
}
if (node.getElement().getBoundingClientRect().width <= 0 || node.getElement().getBoundingClientRect().height <= 0) {
return false;
}
if (!node.isDisplayed()) {
return false;
}
if (!node.isVisible()) {
return false;
}
if (this.getAllTextLength(node) === 0) {
var children = new Array();
this.getAllChildren(node, children);
for (var _i = 0, children_1 = children; _i < children_1.length; _i++) {
var child = children_1[_i];
var childNodeName = child.getNode().nodeName;
if (!child.isVisible()) {
continue;
}
if (childNodeName.toLowerCase() === "img") {
return true;
}
if (childNodeName.toLowerCase() === "input") {
return true;
}
}
return false;
}
return true;
};
/**
* Checks if node is a valid node.
*
* Node is valid if it's visible in the browser. This means that the node's
* width and height are not zero.
*
* @param node
* Input node
*
* @return True if node is valid, otherwise false.
*/
VipsParser.prototype.isValidNode = function (node) {
if (node.getElement().clientHeight > 0 && node.getElement().clientWidth > 0) {
return true;
}
else {
return false;
}
};
/**
* Checks if node is a text node.
*
* @param box
* Input node
*
* @return True if node is a text node, otherwise false.
*/
VipsParser.prototype.isTextNode = function (box) {
return box.getNode().nodeName.toLowerCase() === "text";
};
/**
* Checks if node is a virtual text node.
*
* Inline nodes with only text node children are virtual text nodes.
*
* @param node
* Input node
*
* @return True if node is virtual text node, otherwise false.
*/
VipsParser.prototype.isVirtualTextNode1 = function (node) {
if (node.isBlock()) {
return false;
}
for (var _i = 0, _a = node.getSubBoxList(); _i < _a.length; _i++) {
var childNode = _a[_i];
if (!(childNode instanceof TextBox)) {
if (!this.isTextNode(childNode)) {
return false;
}
}
}
return true;
};
/**
* Checks if node is a virtual text node.
*
* Inline nodes with only text node and virtual text node children are
* virtual text nodes.
*
* @param node
* Input node
*
* @return True if node is virtual text node, otherwise false.
*/
VipsParser.prototype.isVirtualTextNode2 = function (node) {
if (node.isBlock()) {
return false;
}
for (var _i = 0, _a = node.getSubBoxList(); _i < _a.length; _i++) {
var childNode = _a[_i];
if (!this.isTextNode(childNode) ||
this.isVirtualTextNode1(childNode)) {
return false;
}
}
return true;
};
/**
* Checks if node is virtual text node.
*
* @param node
* Input node
*
* @return True if node is virtual text node, otherwise false.
*/
VipsParser.prototype.isVirtualTextNode = function (node) {
if (this.isVirtualTextNode1(node)) {
return true;
}
if (this.isVirtualTextNode2(node)) {
return true;
}
return false;
};
VipsParser.prototype.checkValidChildrenNodes = function (node) {
if (node instanceof TextBox) {
if (!(node.getText() === " ")) {
this._cnt++;
}
return;
}
else {
if (this.isValidNode(node)) {
this._cnt++;
}
}
for (var _i = 0, _a = node.getSubBoxList(); _i < _a.length; _i++) {
var childNode = _a[_i];