forked from sandflow/imscJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html.js
1680 lines (1063 loc) · 50.3 KB
/
html.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
/*
* Copyright (c) 2016, Pierre-Anthony Lemieux <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @module imscHTML
*/
;
(function (imscHTML, imscNames, imscStyles) {
/**
* Function that maps <pre>smpte:background</pre> URIs to URLs resolving to image resource
* @callback IMGResolver
* @param {string} <pre>smpte:background</pre> URI
* @return {string} PNG resource URL
*/
/**
* Renders an ISD object (returned by <pre>generateISD()</pre>) into a
* parent element, that must be attached to the DOM. The ISD will be rendered
* into a child <pre>div</pre>
* with heigh and width equal to the clientHeight and clientWidth of the element,
* unless explicitly specified otherwise by the caller. Images URIs specified
* by <pre>smpte:background</pre> attributes are mapped to image resource URLs
* by an <pre>imgResolver</pre> function. The latter takes the value of <code>smpte:background</code>
* attribute and an <code>img</code> DOM element as input, and is expected to
* set the <code>src</code> attribute of the <code>img</code> to the absolute URI of the image.
* <pre>displayForcedOnlyMode</pre> sets the (boolean)
* value of the IMSC1 displayForcedOnlyMode parameter. The function returns
* an opaque object that should passed in <code>previousISDState</code> when this function
* is called for the next ISD, otherwise <code>previousISDState</code> should be set to
* <code>null</code>.
*
* @param {Object} isd ISD to be rendered
* @param {Object} element Element into which the ISD is rendered
* @param {?IMGResolver} imgResolver Resolve <pre>smpte:background</pre> URIs into URLs.
* @param {?number} eheight Height (in pixel) of the child <div>div</div> or null
* to use clientHeight of the parent element
* @param {?number} ewidth Width (in pixel) of the child <div>div</div> or null
* to use clientWidth of the parent element
* @param {?boolean} displayForcedOnlyMode Value of the IMSC1 displayForcedOnlyMode parameter,
* or false if null
* @param {?module:imscUtils.ErrorHandler} errorHandler Error callback
* @param {Object} previousISDState State saved during processing of the previous ISD, or null if initial call
* @param {?boolean} enableRollUp Enables roll-up animations (see CEA 708)
* @return {Object} ISD state to be provided when this funtion is called for the next ISD
*/
imscHTML.render = function (isd,
element,
imgResolver,
eheight,
ewidth,
displayForcedOnlyMode,
errorHandler,
previousISDState,
enableRollUp
) {
/* maintain aspect ratio if specified */
var height = eheight || element.clientHeight;
var width = ewidth || element.clientWidth;
if (isd.aspectRatio !== null) {
var twidth = height * isd.aspectRatio;
if (twidth > width) {
height = Math.round(width / isd.aspectRatio);
} else {
width = twidth;
}
}
var rootcontainer = document.createElement("div");
rootcontainer.style.position = "relative";
rootcontainer.style.width = width + "px";
rootcontainer.style.height = height + "px";
rootcontainer.style.margin = "auto";
rootcontainer.style.top = 0;
rootcontainer.style.bottom = 0;
rootcontainer.style.left = 0;
rootcontainer.style.right = 0;
rootcontainer.style.zIndex = 0;
var context = {
h: height,
w: width,
regionH: null,
regionW: null,
imgResolver: imgResolver,
displayForcedOnlyMode: displayForcedOnlyMode || false,
isd: isd,
errorHandler: errorHandler,
previousISDState: previousISDState,
enableRollUp: enableRollUp || false,
currentISDState: {},
flg: null, /* current fillLineGap value if active, null otherwise */
lp: null, /* current linePadding value if active, null otherwise */
mra: null, /* current multiRowAlign value if active, null otherwise */
ipd: null, /* inline progression direction (lr, rl, tb) */
bpd: null, /* block progression direction (lr, rl, tb) */
ruby: null, /* is ruby present in a <p> */
textEmphasis: null, /* is textEmphasis present in a <p> */
rubyReserve: null /* is rubyReserve applicable to a <p> */
};
element.appendChild(rootcontainer);
for (var i in isd.contents) {
processElement(context, rootcontainer, isd.contents[i]);
}
return context.currentISDState;
};
function processElement(context, dom_parent, isd_element) {
var e;
if (isd_element.kind === 'region') {
e = document.createElement("div");
e.style.position = "absolute";
} else if (isd_element.kind === 'body') {
e = document.createElement("div");
} else if (isd_element.kind === 'div') {
e = document.createElement("div");
} else if (isd_element.kind === 'image') {
e = document.createElement("img");
if (context.imgResolver !== null && isd_element.src !== null) {
var uri = context.imgResolver(isd_element.src, e);
if (uri)
e.src = uri;
e.height = context.regionH;
e.width = context.regionW;
}
} else if (isd_element.kind === 'p') {
e = document.createElement("p");
} else if (isd_element.kind === 'span') {
if (isd_element.styleAttrs[imscStyles.byName.ruby.qname] === "container") {
e = document.createElement("ruby");
context.ruby = true;
} else if (isd_element.styleAttrs[imscStyles.byName.ruby.qname] === "base") {
e = document.createElement("rb");
} else if (isd_element.styleAttrs[imscStyles.byName.ruby.qname] === "text") {
e = document.createElement("rt");
} else if (isd_element.styleAttrs[imscStyles.byName.ruby.qname] === "baseContainer") {
e = document.createElement("rbc");
} else if (isd_element.styleAttrs[imscStyles.byName.ruby.qname] === "textContainer") {
e = document.createElement("rtc");
} else if (isd_element.styleAttrs[imscStyles.byName.ruby.qname] === "delimiter") {
/* ignore rp */
return;
} else {
e = document.createElement("span");
}
var te = isd_element.styleAttrs[imscStyles.byName.textEmphasis.qname];
if (te && te.style !== "none") {
context.textEmphasis = true;
}
//e.textContent = isd_element.text;
} else if (isd_element.kind === 'br') {
e = document.createElement("br");
}
if (!e) {
reportError(context.errorHandler, "Error processing ISD element kind: " + isd_element.kind);
return;
}
/* add to parent */
dom_parent.appendChild(e);
/* override UA default margin */
/* TODO: should apply to <p> only */
e.style.margin = "0";
/* tranform TTML styles to CSS styles */
for (var i in STYLING_MAP_DEFS) {
var sm = STYLING_MAP_DEFS[i];
var attr = isd_element.styleAttrs[sm.qname];
if (attr !== undefined && sm.map !== null) {
sm.map(context, e, isd_element, attr);
}
}
var proc_e = e;
/* remember writing direction */
if (isd_element.kind === "region") {
var wdir = isd_element.styleAttrs[imscStyles.byName.writingMode.qname];
if (wdir === "lrtb" || wdir === "lr") {
context.ipd = "lr";
context.bpd = "tb";
} else if (wdir === "rltb" || wdir === "rl") {
context.ipd = "rl";
context.bpd = "tb";
} else if (wdir === "tblr") {
context.ipd = "tb";
context.bpd = "lr";
} else if (wdir === "tbrl" || wdir === "tb") {
context.ipd = "tb";
context.bpd = "rl";
}
}
/* do we have linePadding ? */
var lp = isd_element.styleAttrs[imscStyles.byName.linePadding.qname];
if (lp && (! lp.isZero())) {
var plength = lp.toUsedLength(context.w, context.h);
if (plength > 0) {
/* apply padding to the <p> so that line padding does not cause line wraps */
var padmeasure = Math.ceil(plength) + "px";
if (context.bpd === "tb") {
proc_e.style.paddingLeft = padmeasure;
proc_e.style.paddingRight = padmeasure;
} else {
proc_e.style.paddingTop = padmeasure;
proc_e.style.paddingBottom = padmeasure;
}
context.lp = lp;
}
}
// do we have multiRowAlign?
var mra = isd_element.styleAttrs[imscStyles.byName.multiRowAlign.qname];
if (mra && mra !== "auto") {
/* create inline block to handle multirowAlign */
var s = document.createElement("span");
s.style.display = "inline-block";
s.style.textAlign = mra;
e.appendChild(s);
proc_e = s;
context.mra = mra;
}
/* do we have rubyReserve? */
var rr = isd_element.styleAttrs[imscStyles.byName.rubyReserve.qname];
if (rr && rr[0] !== "none") {
context.rubyReserve = rr;
}
/* remember we are filling line gaps */
if (isd_element.styleAttrs[imscStyles.byName.fillLineGap.qname]) {
context.flg = true;
}
if (isd_element.kind === "span" && isd_element.text) {
if (imscStyles.byName.textCombine.qname in isd_element.styleAttrs &&
isd_element.styleAttrs[imscStyles.byName.textCombine.qname][0] === "all") {
/* ignore tate-chu-yoku since line break cannot happen within */
e.textContent = isd_element.text;
} else {
// wrap characters in spans to find the line wrap locations
var cbuf = '';
for (var j = 0; j < isd_element.text.length; j++) {
cbuf += isd_element.text.charAt(j);
var cc = isd_element.text.charCodeAt(j);
if (cc < 0xD800 || cc > 0xDBFF || j === isd_element.text.length) {
/* wrap the character(s) in a span unless it is a high surrogate */
var span = document.createElement("span");
span.textContent = cbuf;
e.appendChild(span);
cbuf = '';
}
}
}
}
/* process the children of the ISD element */
for (var k in isd_element.contents) {
processElement(context, proc_e, isd_element.contents[k]);
}
/* list of lines */
var linelist = [];
/* paragraph processing */
/* TODO: linePadding only supported for horizontal scripts */
if ((context.lp || context.mra || context.flg || context.ruby || context.textEmphasis || context.rubyReserve) &&
isd_element.kind === "p") {
constructLineList(context, proc_e, linelist, null);
/* apply rubyReserve */
if (context.rubyReserve) {
applyRubyReserve(linelist, context);
context.rubyReserve = null;
}
/* apply tts:rubyPosition="outside" */
if (context.ruby || context.rubyReserve) {
applyRubyPosition(linelist, context);
context.ruby = null;
}
/* apply text emphasis "outside" position */
if (context.textEmphasis) {
applyTextEmphasis(linelist, context);
context.textEmphasis = null;
}
/* insert line breaks for multirowalign */
if (context.mra) {
applyMultiRowAlign(linelist);
context.mra = null;
}
/* add linepadding */
if (context.lp) {
applyLinePadding(linelist, context.lp.toUsedLength(context.w, context.h), context);
context.lp = null;
}
/* fill line gaps linepadding */
if (context.flg) {
var par_edges = rect2edges(proc_e.getBoundingClientRect(), context);
applyFillLineGap(linelist, par_edges.before, par_edges.after, context);
context.flg = null;
}
}
/* region processing */
if (isd_element.kind === "region") {
/* build line list */
constructLineList(context, proc_e, linelist);
/* perform roll up if needed */
if ((context.bpd === "tb") &&
context.enableRollUp &&
isd_element.contents.length > 0 &&
isd_element.styleAttrs[imscStyles.byName.displayAlign.qname] === 'after') {
/* horrible hack, perhaps default region id should be underscore everywhere? */
var rid = isd_element.id === '' ? '_' : isd_element.id;
var rb = new RegionPBuffer(rid, linelist);
context.currentISDState[rb.id] = rb;
if (context.previousISDState &&
rb.id in context.previousISDState &&
context.previousISDState[rb.id].plist.length > 0 &&
rb.plist.length > 1 &&
rb.plist[rb.plist.length - 2].text ===
context.previousISDState[rb.id].plist[context.previousISDState[rb.id].plist.length - 1].text) {
var body_elem = e.firstElementChild;
var h = rb.plist[rb.plist.length - 1].after - rb.plist[rb.plist.length - 1].before;
body_elem.style.bottom = "-" + h + "px";
body_elem.style.transition = "transform 0.4s";
body_elem.style.position = "relative";
body_elem.style.transform = "translateY(-" + h + "px)";
}
}
/* TODO: clean-up the spans ? */
}
}
function applyLinePadding(lineList, lp, context) {
for (var i in lineList) {
var l = lineList[i].elements.length;
var se = lineList[i].elements[lineList[i].start_elem];
var ee = lineList[i].elements[lineList[i].end_elem];
var pospadpxlen = Math.ceil(lp) + "px";
var negpadpxlen = "-" + Math.ceil(lp) + "px";
if (l !== 0) {
if (context.ipd === "lr") {
se.node.style.borderLeftColor = se.bgcolor || "#00000000";
se.node.style.borderLeftStyle = "solid";
se.node.style.borderLeftWidth = pospadpxlen;
se.node.style.marginLeft = negpadpxlen;
} else if (context.ipd === "rl") {
se.node.style.borderRightColor = se.bgcolor || "#00000000";
se.node.style.borderRightStyle = "solid";
se.node.style.borderRightWidth = pospadpxlen;
se.node.style.marginRight = negpadpxlen;
} else if (context.ipd === "tb") {
se.node.style.borderTopColor = se.bgcolor || "#00000000";
se.node.style.borderTopStyle = "solid";
se.node.style.borderTopWidth = pospadpxlen;
se.node.style.marginTop = negpadpxlen;
}
if (context.ipd === "lr") {
ee.node.style.borderRightColor = ee.bgcolor || "#00000000";
ee.node.style.borderRightStyle = "solid";
ee.node.style.borderRightWidth = pospadpxlen;
ee.node.style.marginRight = negpadpxlen;
} else if (context.ipd === "rl") {
ee.node.style.borderLeftColor = ee.bgcolor || "#00000000";
ee.node.style.borderLeftStyle = "solid";
ee.node.style.borderLeftWidth = pospadpxlen;
ee.node.style.marginLeft = negpadpxlen;
} else if (context.ipd === "tb") {
ee.node.style.borderBottomColor = ee.bgcolor || "#00000000";
ee.node.style.borderBottomStyle = "solid";
ee.node.style.borderBottomWidth = pospadpxlen;
ee.node.style.marginBottom = negpadpxlen;
}
}
}
}
function applyMultiRowAlign(lineList) {
/* apply an explicit br to all but the last line */
for (var i = 0; i < lineList.length - 1; i++) {
var l = lineList[i].elements.length;
if (l !== 0 && lineList[i].br === false) {
var br = document.createElement("br");
var lastnode = lineList[i].elements[l - 1].node;
lastnode.parentElement.insertBefore(br, lastnode.nextSibling);
}
}
}
function applyTextEmphasis(lineList, context) {
/* supports "outside" only */
for (var i = 0; i < lineList.length; i++) {
for (var j = 0; j < lineList[i].te.length; j++) {
/* skip if position already set */
if (lineList[i].te[j].style.textEmphasisPosition &&
lineList[i].te[j].style.textEmphasisPosition !== "none")
continue;
var pos;
if (context.bpd === "tb") {
pos = (i === 0) ? "left over" : "left under";
} else {
if (context.bpd === "rl") {
pos = (i === 0) ? "right under" : "left under";
} else {
pos = (i === 0) ? "left under" : "right under";
}
}
lineList[i].te[j].style.textEmphasisPosition = pos;
}
}
}
function applyRubyPosition(lineList, context) {
for (var i = 0; i < lineList.length; i++) {
for (var j = 0; j < lineList[i].rbc.length; j++) {
/* skip if ruby-position already set */
if (lineList[i].rbc[j].style.rubyPosition)
continue;
var pos;
if (context.bpd === "tb") {
pos = (i === 0) ? "over" : "under";
} else {
if (context.bpd === "rl") {
pos = (i === 0) ? "over" : "under";
} else {
pos = (i === 0) ? "under" : "over";
}
}
lineList[i].rbc[j].style.rubyPosition = pos;
}
}
}
function applyRubyReserve(lineList, context) {
for (var i = 0; i < lineList.length; i++) {
var ruby = document.createElement("ruby");
var rb = document.createElement("rb");
rb.textContent = "\u200B";
ruby.appendChild(rb);
var rt1;
var rt2;
var fs = context.rubyReserve[1].toUsedLength(context.w, context.h) + "px";
if (context.rubyReserve[0] === "both") {
rt1 = document.createElement("rtc");
rt1.style.rubyPosition = "under";
rt1.textContent = "\u200B";
rt1.style.fontSize = fs;
rt2 = document.createElement("rtc");
rt2.style.rubyPosition = "over";
rt2.textContent = "\u200B";
rt2.style.fontSize = fs;
ruby.appendChild(rt1);
ruby.appendChild(rt2);
} else {
rt1 = document.createElement("rtc");
rt1.textContent = "\u200B";
rt1.style.fontSize = fs;
if (context.rubyReserve[0] === "after" || (context.rubyReserve[0] === "outside" && i > 0)) {
rt1.style.rubyPosition = (context.bpd === "tb" || context.bpd === "rl") ? "under" : "over";
} else {
rt1.style.rubyPosition = (context.bpd === "tb" || context.bpd === "rl") ? "over" : "under";
}
ruby.appendChild(rt1);
}
var e = lineList[i].elements[0].node.parentElement.insertBefore(
ruby,
lineList[i].elements[0].node
);
}
}
function applyFillLineGap(lineList, par_before, par_after, context) {
/* positive for BPD = lr and tb, negative for BPD = rl */
var s = Math.sign(par_after - par_before);
for (var i = 0; i <= lineList.length; i++) {
/* compute frontier between lines */
var frontier;
if (i === 0) {
frontier = par_before;
} else if (i === lineList.length) {
frontier = par_after;
} else {
frontier = (lineList[i].before + lineList[i - 1].after) / 2;
}
/* padding amount */
var pad;
/* current element */
var e;
/* before line */
if (i > 0) {
for (var j = 0; j < lineList[i - 1].elements.length; j++) {
if (lineList[i - 1].elements[j].bgcolor === null)
continue;
e = lineList[i - 1].elements[j];
if (s * (e.after - frontier) < 0) {
pad = Math.ceil(Math.abs(frontier - e.after)) + "px";
e.node.style.backgroundColor = e.bgcolor;
if (context.bpd === "lr") {
e.node.style.paddingRight = pad;
} else if (context.bpd === "rl") {
e.node.style.paddingLeft = pad;
} else if (context.bpd === "tb") {
e.node.style.paddingBottom = pad;
}
}
}
}
/* after line */
if (i < lineList.length) {
for (var k = 0; k < lineList[i].elements.length; k++) {
e = lineList[i].elements[k];
if (e.bgcolor === null)
continue;
if (s * (e.before - frontier) > 0) {
pad = Math.ceil(Math.abs(e.before - frontier)) + "px";
e.node.style.backgroundColor = e.bgcolor;
if (context.bpd === "lr") {
e.node.style.paddingLeft = pad;
} else if (context.bpd === "rl") {
e.node.style.paddingRight = pad;
} else if (context.bpd === "tb") {
e.node.style.paddingTop = pad;
}
}
}
}
}
}
function RegionPBuffer(id, lineList) {
this.id = id;
this.plist = lineList;
}
function pruneEmptySpans(element) {
var child = element.firstChild;
while (child) {
var nchild = child.nextSibling;
if (child.nodeType === Node.ELEMENT_NODE &&
child.localName === 'span') {
pruneEmptySpans(child);
if (child.childElementCount === 0 &&
child.textContent.length === 0) {
element.removeChild(child);
}
}
child = nchild;
}
}
function rect2edges(rect, context) {
var edges = {before: null, after: null, start: null, end: null};
if (context.bpd === "tb") {
edges.before = rect.top;
edges.after = rect.bottom;
if (context.ipd === "lr") {
edges.start = rect.left;
edges.end = rect.right;
} else {
edges.start = rect.right;
edges.end = rect.left;
}
} else if (context.bpd === "lr") {
edges.before = rect.left;
edges.after = rect.right;
edges.start = rect.top;
edges.end = rect.bottom;
} else if (context.bpd === "rl") {
edges.before = rect.right;
edges.after = rect.left;
edges.start = rect.top;
edges.end = rect.bottom;
}
return edges;
}
function constructLineList(context, element, llist, bgcolor) {
if (element.localName === "rt" || element.localName === "rtc") {
/* skip ruby annotations */
return;
}
var curbgcolor = element.style.backgroundColor || bgcolor;
if (element.childElementCount === 0) {
if (element.localName === 'span' || element.localName === 'rb') {
var r = element.getBoundingClientRect();
/* skip if span is not displayed */
if (r.height === 0 || r.width === 0)
return;
var edges = rect2edges(r, context);
if (llist.length === 0 ||
(!isSameLine(edges.before, edges.after, llist[llist.length - 1].before, llist[llist.length - 1].after))
) {
llist.push({
before: edges.before,
after: edges.after,