-
Notifications
You must be signed in to change notification settings - Fork 31
/
html.js
executable file
·1865 lines (1173 loc) · 56.2 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 <pal@sandflow.com>
* 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
*/
var browserIsFirefox = /firefox/i.test(navigator.userAgent);
;
(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);
if ("contents" in isd) {
for (var i = 0; i < isd.contents.length; i++) {
processElement(context, rootcontainer, isd.contents[i], isd);
}
}
return context.currentISDState;
};
function processElement(context, dom_parent, isd_element, isd_parent) {
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("span"); // rb element is deprecated in HTML
} 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");
}
//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;
}
/* set language */
if (isd_element.lang) {
if (isd_element.kind === 'region' || isd_element.lang !== isd_parent.lang) {
e.lang = isd_element.lang;
}
}
/* add to parent */
dom_parent.appendChild(e);
/* override UA default margin */
/* TODO: should apply to <p> only */
e.style.margin = "0";
/* determine ipd and bpd */
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";
}
} else if (isd_element.kind === "p" && context.bpd === "tb") {
var pdir = isd_element.styleAttrs[imscStyles.byName.direction.qname];
context.ipd = pdir === "ltr" ? "lr" : "rl";
}
/* tranform TTML styles to CSS styles */
for (var i = 0; i < STYLING_MAP_DEFS.length; i++) {
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;
/* 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) {
var te = isd_element.styleAttrs[imscStyles.byName.textEmphasis.qname];
if (te && te.style !== "none") {
context.textEmphasis = true;
}
if (imscStyles.byName.textCombine.qname in isd_element.styleAttrs &&
isd_element.styleAttrs[imscStyles.byName.textCombine.qname] === "all") {
/* ignore tate-chu-yoku since line break cannot happen within */
e.textContent = isd_element.text;
e._isd_element = isd_element;
if (te) {
applyTextEmphasis(context, e, isd_element, te);
};
} 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 - 1) {
/* wrap the character(s) in a span unless it is a high surrogate */
var span = document.createElement("span");
span.textContent = cbuf;
/* apply textEmphasis */
if (te) {
applyTextEmphasis(context, span, isd_element, te);
};
e.appendChild(span);
cbuf = '';
//For the sake of merging these back together, record what isd element generated it.
span._isd_element = isd_element;
}
}
}
}
/* process the children of the ISD element */
if ("contents" in isd_element) {
for (var k = 0; k < isd_element.contents.length; k++) {
processElement(context, proc_e, isd_element.contents[k], isd_element);
}
}
/* list of lines */
var linelist = [];
/* paragraph processing */
/* TODO: linePadding only supported for horizontal scripts */
if (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) {
applyTextEmphasisOutside(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;
}
mergeSpans(linelist, context); // The earlier we can do this the less processing there will be.
/* fill line gaps linepadding */
if (context.flg) {
var par_edges = rect2edges(proc_e.getBoundingClientRect(), context);
applyFillLineGap(linelist, par_edges.before, par_edges.after, context, proc_e);
context.flg = null;
}
}
/* region processing */
if (isd_element.kind === "region") {
/* perform roll up if needed */
if ((context.bpd === "tb") &&
context.enableRollUp &&
isd_element.contents.length > 0 &&
isd_element.styleAttrs[imscStyles.byName.displayAlign.qname] === 'after') {
/* build line list */
constructLineList(context, proc_e, linelist, null);
/* 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)";
}
}
}
}
function mergeSpans(lineList, context) {
for (var i = 0; i < lineList.length; i++) {
var line = lineList[i];
for (var j = 1; j < line.elements.length;) {
var previous = line.elements[j - 1];
var span = line.elements[j];
if (spanMerge(previous.node, span.node, context)) {
//removed from DOM by spanMerge(), remove from the list too.
line.elements.splice(j, 1);
continue;
} else {
j++;
}
}
}
// Copy backgroundColor to each span so that fillLineGap will apply padding to elements with the right background
var thisNode, ancestorBackgroundColor;
var clearTheseBackgrounds = [];
for (var l = 0; l < lineList.length; l++) {
for (var el = 0; el < lineList[l].elements.length; el++) {
thisNode = lineList[l].elements[el].node;
ancestorBackgroundColor = getSpanAncestorColor(thisNode, clearTheseBackgrounds, false);
if (ancestorBackgroundColor) {
thisNode.style.backgroundColor = ancestorBackgroundColor;
}
}
}
for (var bi = 0; bi < clearTheseBackgrounds.length; bi++) {
clearTheseBackgrounds[bi].style.backgroundColor = "";
}
}
function getSpanAncestorColor(element, ancestorList, isAncestor) {
if (element.style.backgroundColor) {
if (isAncestor && !ancestorList.includes(element)) {
ancestorList.push(element);
}
return element.style.backgroundColor;
} else {
if (element.parentElement.nodeName === "SPAN" ||
element.parentElement.nodeName === "RUBY" ||
element.parentElement.nodeName === "RBC" ||
element.parentElement.nodeName === "RTC" ||
element.parentElement.nodeName === "RT") {
return getSpanAncestorColor(element.parentElement, ancestorList, true);
}
}
return undefined;
}
function spanMerge(first, second, context) {
if (first.tagName === "SPAN" &&
second.tagName === "SPAN" &&
first._isd_element === second._isd_element) {
if (! first._isd_element) {
/* we should never get here since every span should have a source ISD element */
reportError(context.errorHandler, "Internal error: HTML span is not linked to a source element; cannot merge spans.");
return false;
}
first.textContent += second.textContent;
for (var i = 0; i < second.style.length; i++) {
var styleName = second.style[i];
if (styleName.indexOf("border") >= 0 ||
styleName.indexOf("padding") >= 0 ||
styleName.indexOf("margin") >= 0) {
first.style[styleName] = second.style[styleName];
}
}
second.parentElement.removeChild(second);
return true;
}
return false;
}
function applyLinePadding(lineList, lp, context) {
if (lineList === null) return;
for (var i = 0; i < lineList.length; i++) {
var l = lineList[i].elements.length;
var pospadpxlen = Math.ceil(lp) + "px";
var negpadpxlen = "-" + Math.ceil(lp) + "px";
if (l !== 0) {
var se = lineList[i].elements[lineList[i].start_elem];
var ee = lineList[i].elements[lineList[i].end_elem];
if (se === ee) {
// Check to see if there's any background at all
var elementBoundingRect = se.node.getBoundingClientRect();
if (elementBoundingRect.width == 0 || elementBoundingRect.height == 0) {
// There's no background on this line, move on.
continue;
}
}
// Start element
if (context.ipd === "lr") {
se.node.style.marginLeft = negpadpxlen;
se.node.style.paddingLeft = pospadpxlen;
} else if (context.ipd === "rl") {
se.node.style.paddingRight = pospadpxlen;
se.node.style.marginRight = negpadpxlen;
} else if (context.ipd === "tb") {
se.node.style.paddingTop = pospadpxlen;
se.node.style.marginTop = negpadpxlen;
}
// End element
if (context.ipd === "lr") {
// Firefox has a problem with line-breaking when a negative margin is applied.
// The positioning will be wrong but don't apply when on firefox.
// https://bugzilla.mozilla.org/show_bug.cgi?id=1502610
if (!browserIsFirefox) {
ee.node.style.marginRight = negpadpxlen;
}
ee.node.style.paddingRight = pospadpxlen;
} else if (context.ipd === "rl") {
ee.node.style.paddingLeft = pospadpxlen;
if (!browserIsFirefox) {
ee.node.style.marginLeft = negpadpxlen;
}
} else if (context.ipd === "tb") {
ee.node.style.paddingBottom = 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 applyTextEmphasisOutside(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_PROP] &&
lineList[i].te[j].style[TEXTEMPHASISPOSITION_PROP] !== "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_PROP] = 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_PROP])
continue;
var pos;
if (RUBYPOSITION_ISWK) {
/* WebKit exception */
pos = (i === 0) ? "before" : "after";
} else 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_PROP] = pos;
}
}
}
function applyRubyReserve(lineList, context) {
for (var i = 0; i < lineList.length; i++) {
var ruby = document.createElement("ruby");
var rb = document.createElement("span"); // rb element is deprecated in HTML
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" || (context.rubyReserve[0] === "outside" && lineList.length == 1)) {
rt1 = document.createElement("rtc");
rt1.style[RUBYPOSITION_PROP] = RUBYPOSITION_ISWK ? "after" : "under";
rt1.textContent = "\u200B";
rt1.style.fontSize = fs;
rt2 = document.createElement("rtc");
rt2.style[RUBYPOSITION_PROP] = RUBYPOSITION_ISWK ? "before" : "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;
var pos;
if (context.rubyReserve[0] === "after" || (context.rubyReserve[0] === "outside" && i > 0)) {
pos = RUBYPOSITION_ISWK ? "after" : ((context.bpd === "tb" || context.bpd === "rl") ? "under" : "over");
} else {
pos = RUBYPOSITION_ISWK ? "before" : ((context.bpd === "tb" || context.bpd === "rl") ? "over" : "under");
}
rt1.style[RUBYPOSITION_PROP] = pos;
ruby.appendChild(rt1);
}
/* add in front of the first ruby element of the line, if it exists */
var sib = null;
for (var j = 0; j < lineList[i].rbc.length; j++) {
if (lineList[i].rbc[j].localName === 'ruby') {
sib = lineList[i].rbc[j];
/* copy specified style properties from the sibling ruby container */
for (var k = 0; k < sib.style.length; k++) {
ruby.style.setProperty(sib.style.item(k), sib.style.getPropertyValue(sib.style.item(k)));
}
break;
}
}
/* otherwise add before first span */
sib = sib || lineList[i].elements[0].node;
sib.parentElement.insertBefore(ruby, sib);
}
}
function applyFillLineGap(lineList, par_before, par_after, context, element) {
/* 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 = Math.round(par_before);
} else if (i === lineList.length) {
frontier = Math.round(par_after);
} else {