600void TextTrackCue::determineTextDirection()
601{
602 DEFINE_STATIC_LOCAL(const String, rtTag, (ASCIILiteral("rt")));
603 createWebVTTNodeTree();
604
605 // Apply the Unicode Bidirectional Algorithm's Paragraph Level steps to the
606 // concatenation of the values of each WebVTT Text Object in nodes, in a
607 // pre-order, depth-first traversal, excluding WebVTT Ruby Text Objects and
608 // their descendants...
609 StringBuilder paragraphBuilder;
610 for (Node* node = m_webVTTNodeTree->firstChild(); node; node = NodeTraversal::next(node, m_webVTTNodeTree.get())) {
611 if (!node->isTextNode() || node->localName() == rtTag)
612 continue;
613
614 paragraphBuilder.append(node->nodeValue());
615 }
616
617 String paragraph = paragraphBuilder.toString();
618 if (!paragraph.length())
619 return;
620
621 TextRun textRun(paragraph, paragraph.length());
622
623 BidiResolver<TextRunIterator, BidiCharacterRun> bidiResolver;
624 bidiResolver.setStatus(BidiStatus(textRun.direction(), textRun.directionalOverride()));
625 bidiResolver.setPositionIgnoringNestedIsolates(TextRunIterator(&textRun, 0));
626
627 BidiRunList<BidiCharacterRun>& bidiRuns = bidiResolver.runs();
628 bidiResolver.createBidiRunsForLine(TextRunIterator(&textRun, textRun.length()));
629
630 ASSERT(bidiRuns.runCount());
631
632 // ... to determine the paragraph embedding level of the first Unicode paragraph of the cue.
633
634 // If the paragraph embedding level determined in the previous step is even
635 // (the paragraph direction is left-to-right), let direction be 'ltr', otherwise, let it be 'rtl'.
636 m_displayDirection = bidiRuns.firstRun()->level() % 2 ? CSSValueRtl : CSSValueLtr;
637}
638