forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsBidiPresUtils.cpp
2236 lines (1979 loc) · 76.1 KB
/
nsBidiPresUtils.cpp
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsBidiPresUtils.h"
#include "nsGkAtoms.h"
#include "nsPresContext.h"
#include "nsRenderingContext.h"
#include "nsBidiUtils.h"
#include "nsCSSFrameConstructor.h"
#include "nsContainerFrame.h"
#include "nsInlineFrame.h"
#include "nsPlaceholderFrame.h"
#include "nsFirstLetterFrame.h"
#include "nsUnicodeProperties.h"
#include "nsTextFrame.h"
#include "nsBlockFrame.h"
#include "nsIFrameInlines.h"
#include <algorithm>
#undef NOISY_BIDI
#undef REALLY_NOISY_BIDI
using namespace mozilla;
static const char16_t kSpace = 0x0020;
static const char16_t kZWSP = 0x200B;
static const char16_t kLineSeparator = 0x2028;
static const char16_t kObjectSubstitute = 0xFFFC;
static const char16_t kLRE = 0x202A;
static const char16_t kRLE = 0x202B;
static const char16_t kLRO = 0x202D;
static const char16_t kRLO = 0x202E;
static const char16_t kPDF = 0x202C;
static const char16_t kSeparators[] = {
// All characters with Bidi type Segment Separator or Block Separator
char16_t('\t'),
char16_t('\r'),
char16_t('\n'),
char16_t(0xb),
char16_t(0x1c),
char16_t(0x1d),
char16_t(0x1e),
char16_t(0x1f),
char16_t(0x85),
char16_t(0x2029),
char16_t(0)
};
#define NS_BIDI_CONTROL_FRAME ((nsIFrame*)0xfffb1d1)
struct BidiParagraphData {
nsString mBuffer;
nsAutoTArray<char16_t, 16> mEmbeddingStack;
nsTArray<nsIFrame*> mLogicalFrames;
nsTArray<nsLineBox*> mLinePerFrame;
nsDataHashtable<nsISupportsHashKey, int32_t> mContentToFrameIndex;
bool mIsVisual;
bool mReset;
nsBidiLevel mParaLevel;
nsIContent* mPrevContent;
nsAutoPtr<nsBidi> mBidiEngine;
nsIFrame* mPrevFrame;
nsAutoPtr<BidiParagraphData> mSubParagraph;
uint8_t mParagraphDepth;
void Init(nsBlockFrame *aBlockFrame)
{
mBidiEngine = new nsBidi();
mPrevContent = nullptr;
mParagraphDepth = 0;
mParaLevel = nsBidiPresUtils::BidiLevelFromStyle(aBlockFrame->StyleContext());
mIsVisual = aBlockFrame->PresContext()->IsVisualMode();
if (mIsVisual) {
/**
* Drill up in content to detect whether this is an element that needs to
* be rendered with logical order even on visual pages.
*
* We always use logical order on form controls, firstly so that text
* entry will be in logical order, but also because visual pages were
* written with the assumption that even if the browser had no support
* for right-to-left text rendering, it would use native widgets with
* bidi support to display form controls.
*
* We also use logical order in XUL elements, since we expect that if a
* XUL element appears in a visual page, it will be generated by an XBL
* binding and contain localized text which will be in logical order.
*/
for (nsIContent* content = aBlockFrame->GetContent() ; content;
content = content->GetParent()) {
if (content->IsNodeOfType(nsINode::eHTML_FORM_CONTROL) ||
content->IsXUL()) {
mIsVisual = false;
break;
}
}
}
}
BidiParagraphData* GetSubParagraph()
{
if (!mSubParagraph) {
mSubParagraph = new BidiParagraphData();
mSubParagraph->Init(this);
}
return mSubParagraph;
}
// Initialise a sub-paragraph from its containing paragraph
void Init(BidiParagraphData *aBpd)
{
mBidiEngine = new nsBidi();
mPrevContent = nullptr;
mIsVisual = aBpd->mIsVisual;
mReset = false;
}
void Reset(nsIFrame* aBDIFrame, BidiParagraphData *aBpd)
{
mReset = true;
mLogicalFrames.Clear();
mLinePerFrame.Clear();
mContentToFrameIndex.Clear();
mBuffer.SetLength(0);
mPrevFrame = aBpd->mPrevFrame;
mParagraphDepth = aBpd->mParagraphDepth + 1;
const nsStyleTextReset* text = aBDIFrame->StyleTextReset();
bool isRTL = (NS_STYLE_DIRECTION_RTL ==
aBDIFrame->StyleVisibility()->mDirection);
if (text->mUnicodeBidi & NS_STYLE_UNICODE_BIDI_PLAINTEXT) {
mParaLevel = NSBIDI_DEFAULT_LTR;
} else {
mParaLevel = mParagraphDepth * 2;
if (isRTL) ++mParaLevel;
}
if (text->mUnicodeBidi & NS_STYLE_UNICODE_BIDI_OVERRIDE) {
PushBidiControl(isRTL ? kRLO : kLRO);
}
}
void EmptyBuffer()
{
mBuffer.SetLength(0);
}
nsresult SetPara()
{
return mBidiEngine->SetPara(mBuffer.get(), BufferLength(),
mParaLevel, nullptr);
}
/**
* mParaLevel can be NSBIDI_DEFAULT_LTR as well as NSBIDI_LTR or NSBIDI_RTL.
* GetParaLevel() returns the actual (resolved) paragraph level which is
* always either NSBIDI_LTR or NSBIDI_RTL
*/
nsBidiLevel GetParaLevel()
{
nsBidiLevel paraLevel = mParaLevel;
if (IS_DEFAULT_LEVEL(paraLevel)) {
mBidiEngine->GetParaLevel(¶Level);
}
return paraLevel;
}
nsBidiDirection GetDirection()
{
nsBidiDirection dir;
mBidiEngine->GetDirection(&dir);
return dir;
}
nsresult CountRuns(int32_t *runCount){ return mBidiEngine->CountRuns(runCount); }
nsresult GetLogicalRun(int32_t aLogicalStart,
int32_t* aLogicalLimit,
nsBidiLevel* aLevel)
{
nsresult rv = mBidiEngine->GetLogicalRun(aLogicalStart,
aLogicalLimit, aLevel);
if (mIsVisual || NS_FAILED(rv))
*aLevel = GetParaLevel();
return rv;
}
void ResetData()
{
mLogicalFrames.Clear();
mLinePerFrame.Clear();
mContentToFrameIndex.Clear();
mBuffer.SetLength(0);
mPrevContent = nullptr;
for (uint32_t i = 0; i < mEmbeddingStack.Length(); ++i) {
mBuffer.Append(mEmbeddingStack[i]);
mLogicalFrames.AppendElement(NS_BIDI_CONTROL_FRAME);
mLinePerFrame.AppendElement((nsLineBox*)nullptr);
}
}
void ResetForNewBlock()
{
for (BidiParagraphData* bpd = this; bpd; bpd = bpd->mSubParagraph) {
bpd->mPrevFrame = nullptr;
}
}
void AppendFrame(nsIFrame* aFrame,
nsBlockInFlowLineIterator* aLineIter,
nsIContent* aContent = nullptr)
{
if (aContent) {
mContentToFrameIndex.Put(aContent, FrameCount());
}
mLogicalFrames.AppendElement(aFrame);
AdvanceLineIteratorToFrame(aFrame, aLineIter, mPrevFrame);
mLinePerFrame.AppendElement(aLineIter->GetLine().get());
}
void AdvanceAndAppendFrame(nsIFrame** aFrame,
nsBlockInFlowLineIterator* aLineIter,
nsIFrame** aNextSibling)
{
nsIFrame* frame = *aFrame;
nsIFrame* nextSibling = *aNextSibling;
frame = frame->GetNextContinuation();
if (frame) {
AppendFrame(frame, aLineIter, nullptr);
/*
* If we have already overshot the saved next-sibling while
* scanning the frame's continuations, advance it.
*/
if (frame == nextSibling) {
nextSibling = frame->GetNextSibling();
}
}
*aFrame = frame;
*aNextSibling = nextSibling;
}
int32_t GetLastFrameForContent(nsIContent *aContent)
{
int32_t index = 0;
mContentToFrameIndex.Get(aContent, &index);
return index;
}
int32_t FrameCount(){ return mLogicalFrames.Length(); }
int32_t BufferLength(){ return mBuffer.Length(); }
nsIFrame* FrameAt(int32_t aIndex){ return mLogicalFrames[aIndex]; }
nsLineBox* GetLineForFrameAt(int32_t aIndex){ return mLinePerFrame[aIndex]; }
void AppendUnichar(char16_t aCh){ mBuffer.Append(aCh); }
void AppendString(const nsDependentSubstring& aString){ mBuffer.Append(aString); }
void AppendControlChar(char16_t aCh)
{
mLogicalFrames.AppendElement(NS_BIDI_CONTROL_FRAME);
mLinePerFrame.AppendElement((nsLineBox*)nullptr);
AppendUnichar(aCh);
}
void PushBidiControl(char16_t aCh)
{
AppendControlChar(aCh);
mEmbeddingStack.AppendElement(aCh);
}
void PopBidiControl()
{
AppendControlChar(kPDF);
NS_ASSERTION(mEmbeddingStack.Length(), "embedding/override underflow");
mEmbeddingStack.TruncateLength(mEmbeddingStack.Length() - 1);
}
void ClearBidiControls()
{
for (uint32_t i = 0; i < mEmbeddingStack.Length(); ++i) {
AppendControlChar(kPDF);
}
}
static bool
IsFrameInCurrentLine(nsBlockInFlowLineIterator* aLineIter,
nsIFrame* aPrevFrame, nsIFrame* aFrame)
{
nsIFrame* endFrame = aLineIter->IsLastLineInList() ? nullptr :
aLineIter->GetLine().next()->mFirstChild;
nsIFrame* startFrame = aPrevFrame ? aPrevFrame : aLineIter->GetLine()->mFirstChild;
for (nsIFrame* frame = startFrame; frame && frame != endFrame;
frame = frame->GetNextSibling()) {
if (frame == aFrame)
return true;
}
return false;
}
static void
AdvanceLineIteratorToFrame(nsIFrame* aFrame,
nsBlockInFlowLineIterator* aLineIter,
nsIFrame*& aPrevFrame)
{
// Advance aLine to the line containing aFrame
nsIFrame* child = aFrame;
nsIFrame* parent = nsLayoutUtils::GetParentOrPlaceholderFor(child);
while (parent && !nsLayoutUtils::GetAsBlock(parent)) {
child = parent;
parent = nsLayoutUtils::GetParentOrPlaceholderFor(child);
}
NS_ASSERTION (parent, "aFrame is not a descendent of aBlockFrame");
while (!IsFrameInCurrentLine(aLineIter, aPrevFrame, child)) {
#ifdef DEBUG
bool hasNext =
#endif
aLineIter->Next();
NS_ASSERTION(hasNext, "Can't find frame in lines!");
aPrevFrame = nullptr;
}
aPrevFrame = child;
}
};
struct BidiLineData {
nsTArray<nsIFrame*> mLogicalFrames;
nsTArray<nsIFrame*> mVisualFrames;
nsTArray<int32_t> mIndexMap;
nsAutoTArray<uint8_t, 18> mLevels;
bool mIsReordered;
BidiLineData(nsIFrame* aFirstFrameOnLine, int32_t aNumFramesOnLine)
{
/**
* Initialize the logically-ordered array of frames using the top-level
* frames of a single line
*/
mLogicalFrames.Clear();
bool isReordered = false;
bool hasRTLFrames = false;
for (nsIFrame* frame = aFirstFrameOnLine;
frame && aNumFramesOnLine--;
frame = frame->GetNextSibling()) {
AppendFrame(frame);
uint8_t level = nsBidiPresUtils::GetFrameEmbeddingLevel(frame);
mLevels.AppendElement(level);
mIndexMap.AppendElement(0);
if (level & 1) {
hasRTLFrames = true;
}
}
// Reorder the line
nsBidi::ReorderVisual(mLevels.Elements(), FrameCount(),
mIndexMap.Elements());
for (int32_t i = 0; i < FrameCount(); i++) {
mVisualFrames.AppendElement(LogicalFrameAt(mIndexMap[i]));
if (i != mIndexMap[i]) {
isReordered = true;
}
}
// If there's an RTL frame, assume the line is reordered
mIsReordered = isReordered || hasRTLFrames;
}
void AppendFrame(nsIFrame* aFrame)
{
mLogicalFrames.AppendElement(aFrame);
}
int32_t FrameCount(){ return mLogicalFrames.Length(); }
nsIFrame* LogicalFrameAt(int32_t aIndex){ return mLogicalFrames[aIndex]; }
nsIFrame* VisualFrameAt(int32_t aIndex){ return mVisualFrames[aIndex]; }
};
/* Some helper methods for Resolve() */
// Should this frame be split between text runs?
static bool
IsBidiSplittable(nsIFrame* aFrame)
{
// Bidi inline containers should be split, unless they're line frames.
nsIAtom* frameType = aFrame->GetType();
return (aFrame->IsFrameOfType(nsIFrame::eBidiInlineContainer) &&
frameType != nsGkAtoms::lineFrame) ||
frameType == nsGkAtoms::textFrame;
}
// Should this frame be treated as a leaf (e.g. when building mLogicalFrames)?
static bool
IsBidiLeaf(nsIFrame* aFrame)
{
nsIFrame* kid = aFrame->GetFirstPrincipalChild();
return !kid || !aFrame->IsFrameOfType(nsIFrame::eBidiInlineContainer);
}
/**
* Create non-fluid continuations for the ancestors of a given frame all the way
* up the frame tree until we hit a non-splittable frame (a line or a block).
*
* @param aParent the first parent frame to be split
* @param aFrame the child frames after this frame are reparented to the
* newly-created continuation of aParent.
* If aFrame is null, all the children of aParent are reparented.
*/
static nsresult
SplitInlineAncestors(nsContainerFrame* aParent,
nsIFrame* aFrame)
{
nsPresContext* presContext = aParent->PresContext();
nsIPresShell* presShell = presContext->PresShell();
nsIFrame* frame = aFrame;
nsContainerFrame* parent = aParent;
nsContainerFrame* newParent;
while (IsBidiSplittable(parent)) {
nsContainerFrame* grandparent = parent->GetParent();
NS_ASSERTION(grandparent, "Couldn't get parent's parent in nsBidiPresUtils::SplitInlineAncestors");
// Split the child list after |frame|, unless it is the last child.
if (!frame || frame->GetNextSibling()) {
newParent = static_cast<nsContainerFrame*>(presShell->FrameConstructor()->
CreateContinuingFrame(presContext, parent, grandparent, false));
nsFrameList tail = parent->StealFramesAfter(frame);
// Reparent views as necessary
nsresult rv;
rv = nsContainerFrame::ReparentFrameViewList(tail, parent, newParent);
if (NS_FAILED(rv)) {
return rv;
}
// The parent's continuation adopts the siblings after the split.
newParent->InsertFrames(nsIFrame::kNoReflowPrincipalList, nullptr, tail);
// The list name kNoReflowPrincipalList would indicate we don't want reflow
nsFrameList temp(newParent, newParent);
grandparent->InsertFrames(nsIFrame::kNoReflowPrincipalList, parent, temp);
}
frame = parent;
parent = grandparent;
}
return NS_OK;
}
static void
MakeContinuationFluid(nsIFrame* aFrame, nsIFrame* aNext)
{
NS_ASSERTION (!aFrame->GetNextInFlow() || aFrame->GetNextInFlow() == aNext,
"next-in-flow is not next continuation!");
aFrame->SetNextInFlow(aNext);
NS_ASSERTION (!aNext->GetPrevInFlow() || aNext->GetPrevInFlow() == aFrame,
"prev-in-flow is not prev continuation!");
aNext->SetPrevInFlow(aFrame);
}
static void
MakeContinuationsNonFluidUpParentChain(nsIFrame* aFrame, nsIFrame* aNext)
{
nsIFrame* frame;
nsIFrame* next;
for (frame = aFrame, next = aNext;
frame && next &&
next != frame && next == frame->GetNextInFlow() &&
IsBidiSplittable(frame);
frame = frame->GetParent(), next = next->GetParent()) {
frame->SetNextContinuation(next);
next->SetPrevContinuation(frame);
}
}
// If aFrame is the last child of its parent, convert bidi continuations to
// fluid continuations for all of its inline ancestors.
// If it isn't the last child, make sure that its continuation is fluid.
static void
JoinInlineAncestors(nsIFrame* aFrame)
{
nsIFrame* frame = aFrame;
do {
nsIFrame* next = frame->GetNextContinuation();
if (next) {
// Don't join frames if they come from different paragraph depths (i.e.
// one is bidi isolated relative to the other
if (nsBidiPresUtils::GetParagraphDepth(frame) ==
nsBidiPresUtils::GetParagraphDepth(next)) {
MakeContinuationFluid(frame, next);
}
}
// Join the parent only as long as we're its last child.
if (frame->GetNextSibling())
break;
frame = frame->GetParent();
} while (frame && IsBidiSplittable(frame));
}
static nsresult
CreateContinuation(nsIFrame* aFrame,
nsIFrame** aNewFrame,
bool aIsFluid)
{
NS_PRECONDITION(aNewFrame, "null OUT ptr");
NS_PRECONDITION(aFrame, "null ptr");
*aNewFrame = nullptr;
nsPresContext *presContext = aFrame->PresContext();
nsIPresShell *presShell = presContext->PresShell();
NS_ASSERTION(presShell, "PresShell must be set on PresContext before calling nsBidiPresUtils::CreateContinuation");
nsContainerFrame* parent = aFrame->GetParent();
NS_ASSERTION(parent, "Couldn't get frame parent in nsBidiPresUtils::CreateContinuation");
nsresult rv = NS_OK;
// Have to special case floating first letter frames because the continuation
// doesn't go in the first letter frame. The continuation goes with the rest
// of the text that the first letter frame was made out of.
if (parent->GetType() == nsGkAtoms::letterFrame &&
parent->IsFloating()) {
nsFirstLetterFrame* letterFrame = do_QueryFrame(parent);
rv = letterFrame->CreateContinuationForFloatingParent(presContext, aFrame,
aNewFrame, aIsFluid);
return rv;
}
*aNewFrame = presShell->FrameConstructor()->
CreateContinuingFrame(presContext, aFrame, parent, aIsFluid);
// The list name kNoReflowPrincipalList would indicate we don't want reflow
// XXXbz this needs higher-level framelist love
nsFrameList temp(*aNewFrame, *aNewFrame);
parent->InsertFrames(nsIFrame::kNoReflowPrincipalList, aFrame, temp);
if (!aIsFluid) {
// Split inline ancestor frames
rv = SplitInlineAncestors(parent, aFrame);
if (NS_FAILED(rv)) {
return rv;
}
}
return NS_OK;
}
/*
* Overview of the implementation of Resolve():
*
* Walk through the descendants of aBlockFrame and build:
* * mLogicalFrames: an nsTArray of nsIFrame* pointers in logical order
* * mBuffer: an nsString containing a representation of
* the content of the frames.
* In the case of text frames, this is the actual text context of the
* frames, but some other elements are represented in a symbolic form which
* will make the Unicode Bidi Algorithm give the correct results.
* Bidi embeddings and overrides set by CSS or <bdo> elements are
* represented by the corresponding Unicode control characters.
* <br> elements are represented by U+2028 LINE SEPARATOR
* Other inline elements are represented by U+FFFC OBJECT REPLACEMENT
* CHARACTER
*
* Then pass mBuffer to the Bidi engine for resolving of embedding levels
* by nsBidi::SetPara() and division into directional runs by
* nsBidi::CountRuns().
*
* Finally, walk these runs in logical order using nsBidi::GetLogicalRun() and
* correlate them with the frames indexed in mLogicalFrames, setting the
* baseLevel and embeddingLevel properties according to the results returned
* by the Bidi engine.
*
* The rendering layer requires each text frame to contain text in only one
* direction, so we may need to call EnsureBidiContinuation() to split frames.
* We may also need to call RemoveBidiContinuation() to convert frames created
* by EnsureBidiContinuation() in previous reflows into fluid continuations.
*/
nsresult
nsBidiPresUtils::Resolve(nsBlockFrame* aBlockFrame)
{
BidiParagraphData bpd;
bpd.Init(aBlockFrame);
// Handle bidi-override being set on the block itself before calling
// TraverseFrames.
const nsStyleTextReset* text = aBlockFrame->StyleTextReset();
char16_t ch = 0;
if (text->mUnicodeBidi & NS_STYLE_UNICODE_BIDI_OVERRIDE) {
const nsStyleVisibility* vis = aBlockFrame->StyleVisibility();
if (NS_STYLE_DIRECTION_RTL == vis->mDirection) {
ch = kRLO;
}
else if (NS_STYLE_DIRECTION_LTR == vis->mDirection) {
ch = kLRO;
}
if (ch != 0) {
bpd.PushBidiControl(ch);
}
}
for (nsBlockFrame* block = aBlockFrame; block;
block = static_cast<nsBlockFrame*>(block->GetNextContinuation())) {
block->RemoveStateBits(NS_BLOCK_NEEDS_BIDI_RESOLUTION);
nsBlockInFlowLineIterator lineIter(block, block->begin_lines());
bpd.ResetForNewBlock();
TraverseFrames(aBlockFrame, &lineIter, block->GetFirstPrincipalChild(), &bpd);
// XXX what about overflow lines?
}
if (ch != 0) {
bpd.PopBidiControl();
}
BidiParagraphData* subParagraph = bpd.GetSubParagraph();
if (subParagraph->BufferLength()) {
ResolveParagraph(aBlockFrame, subParagraph);
subParagraph->EmptyBuffer();
}
return ResolveParagraph(aBlockFrame, &bpd);
}
nsresult
nsBidiPresUtils::ResolveParagraph(nsBlockFrame* aBlockFrame,
BidiParagraphData* aBpd)
{
nsPresContext *presContext = aBlockFrame->PresContext();
if (aBpd->BufferLength() < 1) {
return NS_OK;
}
aBpd->mBuffer.ReplaceChar(kSeparators, kSpace);
int32_t runCount;
nsresult rv = aBpd->SetPara();
NS_ENSURE_SUCCESS(rv, rv);
uint8_t embeddingLevel = aBpd->GetParaLevel();
rv = aBpd->CountRuns(&runCount);
NS_ENSURE_SUCCESS(rv, rv);
int32_t runLength = 0; // the length of the current run of text
int32_t lineOffset = 0; // the start of the current run
int32_t logicalLimit = 0; // the end of the current run + 1
int32_t numRun = -1;
int32_t fragmentLength = 0; // the length of the current text frame
int32_t frameIndex = -1; // index to the frames in mLogicalFrames
int32_t frameCount = aBpd->FrameCount();
int32_t contentOffset = 0; // offset of current frame in its content node
bool isTextFrame = false;
nsIFrame* frame = nullptr;
nsIContent* content = nullptr;
int32_t contentTextLength = 0;
FramePropertyTable *propTable = presContext->PropertyTable();
nsLineBox* currentLine = nullptr;
#ifdef DEBUG
#ifdef NOISY_BIDI
printf("Before Resolve(), aBlockFrame=0x%p, mBuffer='%s', frameCount=%d, runCount=%d\n",
(void*)aBlockFrame, NS_ConvertUTF16toUTF8(aBpd->mBuffer).get(), frameCount, runCount);
#ifdef REALLY_NOISY_BIDI
printf(" block frame tree=:\n");
aBlockFrame->List(stdout, 0);
#endif
#endif
#endif
if (runCount == 1 && frameCount == 1 &&
aBpd->mParagraphDepth == 0 && aBpd->GetDirection() == NSBIDI_LTR &&
aBpd->GetParaLevel() == 0) {
// We have a single left-to-right frame in a left-to-right paragraph,
// without bidi isolation from the surrounding text.
// Make sure that the embedding level and base level frame properties aren't
// set (because if they are this frame used to have some other direction,
// so we can't do this optimization), and we're done.
nsIFrame* frame = aBpd->FrameAt(0);
if (frame != NS_BIDI_CONTROL_FRAME &&
!frame->Properties().Get(nsIFrame::EmbeddingLevelProperty()) &&
!frame->Properties().Get(nsIFrame::BaseLevelProperty())) {
#ifdef DEBUG
#ifdef NOISY_BIDI
printf("early return for single direction frame %p\n", (void*)frame);
#endif
#endif
frame->AddStateBits(NS_FRAME_IS_BIDI);
return NS_OK;
}
}
nsIFrame* firstFrame = nullptr;
nsIFrame* lastFrame = nullptr;
for (; ;) {
if (fragmentLength <= 0) {
// Get the next frame from mLogicalFrames
if (++frameIndex >= frameCount) {
break;
}
frame = aBpd->FrameAt(frameIndex);
if (frame == NS_BIDI_CONTROL_FRAME ||
nsGkAtoms::textFrame != frame->GetType()) {
/*
* Any non-text frame corresponds to a single character in the text buffer
* (a bidi control character, LINE SEPARATOR, or OBJECT SUBSTITUTE)
*/
isTextFrame = false;
fragmentLength = 1;
}
else {
if (!firstFrame) {
firstFrame = frame;
}
lastFrame = frame;
currentLine = aBpd->GetLineForFrameAt(frameIndex);
content = frame->GetContent();
if (!content) {
rv = NS_OK;
break;
}
contentTextLength = content->TextLength();
if (contentTextLength == 0) {
frame->AdjustOffsetsForBidi(0, 0);
// Set the base level and embedding level of the current run even
// on an empty frame. Otherwise frame reordering will not be correct.
propTable->Set(frame, nsIFrame::EmbeddingLevelProperty(),
NS_INT32_TO_PTR(embeddingLevel));
propTable->Set(frame, nsIFrame::BaseLevelProperty(),
NS_INT32_TO_PTR(aBpd->GetParaLevel()));
propTable->Set(frame, nsIFrame::ParagraphDepthProperty(),
NS_INT32_TO_PTR(aBpd->mParagraphDepth));
continue;
}
int32_t start, end;
frame->GetOffsets(start, end);
NS_ASSERTION(!(contentTextLength < end - start),
"Frame offsets don't fit in content");
fragmentLength = std::min(contentTextLength, end - start);
contentOffset = start;
isTextFrame = true;
}
} // if (fragmentLength <= 0)
if (runLength <= 0) {
// Get the next run of text from the Bidi engine
if (++numRun >= runCount) {
break;
}
lineOffset = logicalLimit;
if (NS_FAILED(aBpd->GetLogicalRun(
lineOffset, &logicalLimit, &embeddingLevel) ) ) {
break;
}
runLength = logicalLimit - lineOffset;
} // if (runLength <= 0)
if (frame == NS_BIDI_CONTROL_FRAME) {
frame = nullptr;
++lineOffset;
}
else {
propTable->Set(frame, nsIFrame::EmbeddingLevelProperty(),
NS_INT32_TO_PTR(embeddingLevel));
propTable->Set(frame, nsIFrame::BaseLevelProperty(),
NS_INT32_TO_PTR(aBpd->GetParaLevel()));
propTable->Set(frame, nsIFrame::ParagraphDepthProperty(),
NS_INT32_TO_PTR(aBpd->mParagraphDepth));
if (isTextFrame) {
if ( (runLength > 0) && (runLength < fragmentLength) ) {
/*
* The text in this frame continues beyond the end of this directional run.
* Create a non-fluid continuation frame for the next directional run.
*/
currentLine->MarkDirty();
nsIFrame* nextBidi;
int32_t runEnd = contentOffset + runLength;
rv = EnsureBidiContinuation(frame, &nextBidi, frameIndex,
contentOffset,
runEnd);
if (NS_FAILED(rv)) {
break;
}
nextBidi->AdjustOffsetsForBidi(runEnd,
contentOffset + fragmentLength);
lastFrame = frame = nextBidi;
contentOffset = runEnd;
} // if (runLength < fragmentLength)
else {
if (contentOffset + fragmentLength == contentTextLength) {
/*
* We have finished all the text in this content node. Convert any
* further non-fluid continuations to fluid continuations and advance
* frameIndex to the last frame in the content node
*/
int32_t newIndex = aBpd->GetLastFrameForContent(content);
if (newIndex > frameIndex) {
currentLine->MarkDirty();
RemoveBidiContinuation(aBpd, frame,
frameIndex, newIndex, lineOffset);
frameIndex = newIndex;
lastFrame = frame = aBpd->FrameAt(frameIndex);
}
} else if (fragmentLength > 0 && runLength > fragmentLength) {
/*
* There is more text that belongs to this directional run in the next
* text frame: make sure it is a fluid continuation of the current frame.
* Do not advance frameIndex, because the next frame may contain
* multi-directional text and need to be split
*/
int32_t newIndex = frameIndex;
do {
} while (++newIndex < frameCount &&
aBpd->FrameAt(newIndex) == NS_BIDI_CONTROL_FRAME);
if (newIndex < frameCount) {
currentLine->MarkDirty();
RemoveBidiContinuation(aBpd, frame,
frameIndex, newIndex, lineOffset);
}
} else if (runLength == fragmentLength) {
/*
* If the directional run ends at the end of the frame, make sure
* that any continuation is non-fluid, and do the same up the
* parent chain
*/
nsIFrame* next = frame->GetNextInFlow();
if (next) {
currentLine->MarkDirty();
MakeContinuationsNonFluidUpParentChain(frame, next);
}
}
frame->AdjustOffsetsForBidi(contentOffset, contentOffset + fragmentLength);
}
} // isTextFrame
else {
++lineOffset;
}
} // not bidi control frame
int32_t temp = runLength;
runLength -= fragmentLength;
fragmentLength -= temp;
if (frame && fragmentLength <= 0) {
// If the frame is at the end of a run, and this is not the end of our
// paragrah, split all ancestor inlines that need splitting.
// To determine whether we're at the end of the run, we check that we've
// finished processing the current run, and that the current frame
// doesn't have a fluid continuation (it could have a fluid continuation
// of zero length, so testing runLength alone is not sufficient).
if (runLength <= 0 && !frame->GetNextInFlow()) {
if (numRun + 1 < runCount) {
nsIFrame* child = frame;
nsContainerFrame* parent = frame->GetParent();
// As long as we're on the last sibling, the parent doesn't have to
// be split.
// However, if the parent has a fluid continuation, we do have to make
// it non-fluid. This can happen e.g. when we have a first-letter
// frame and the end of the first-letter coincides with the end of a
// directional run.
while (parent &&
IsBidiSplittable(parent) &&
!child->GetNextSibling()) {
nsIFrame* next = parent->GetNextInFlow();
if (next) {
parent->SetNextContinuation(next);
next->SetPrevContinuation(parent);
}
child = parent;
parent = child->GetParent();
}
if (parent && IsBidiSplittable(parent)) {
SplitInlineAncestors(parent, child);
}
}
}
else {
// We're not at an end of a run. If |frame| is the last child of its
// parent, and its ancestors happen to have bidi continuations, convert
// them into fluid continuations.
JoinInlineAncestors(frame);
}
}
} // for
if (aBpd->mParagraphDepth > 0) {
if (firstFrame) {
nsContainerFrame* child = firstFrame->GetParent();
if (child) {
nsContainerFrame* parent = child->GetParent();
if (parent && IsBidiSplittable(parent)) {
nsIFrame* prev = child->GetPrevSibling();
if (prev) {
SplitInlineAncestors(parent, prev);
}
}
}
}
if (lastFrame) {
nsContainerFrame* child = lastFrame->GetParent();
if (child) {
nsContainerFrame* parent = child->GetParent();
if (parent && IsBidiSplittable(parent)) {
SplitInlineAncestors(parent, child);
}
}
}
}
#ifdef DEBUG
#ifdef REALLY_NOISY_BIDI
printf("---\nAfter Resolve(), frameTree =:\n");
aBlockFrame->List(stdout, 0);
printf("===\n");
#endif
#endif
return rv;
}
void
nsBidiPresUtils::TraverseFrames(nsBlockFrame* aBlockFrame,
nsBlockInFlowLineIterator* aLineIter,
nsIFrame* aCurrentFrame,
BidiParagraphData* aBpd)
{
if (!aCurrentFrame)
return;
#ifdef DEBUG
nsBlockFrame* initialLineContainer = aLineIter->GetContainer();
#endif
nsIFrame* childFrame = aCurrentFrame;
do {
/*
* It's important to get the next sibling and next continuation *before*
* handling the frame: If we encounter a forced paragraph break and call
* ResolveParagraph within this loop, doing GetNextSibling and
* GetNextContinuation after that could return a bidi continuation that had
* just been split from the original childFrame and we would process it
* twice.
*/
nsIFrame* nextSibling = childFrame->GetNextSibling();
bool isLastFrame = !childFrame->GetNextContinuation();
bool isFirstFrame = !childFrame->GetPrevContinuation();
// If the real frame for a placeholder is a first letter frame, we need to
// drill down into it and include its contents in Bidi resolution.
// If not, we just use the placeholder.
nsIFrame* frame = childFrame;
if (nsGkAtoms::placeholderFrame == childFrame->GetType()) {
nsIFrame* realFrame =
nsPlaceholderFrame::GetRealFrameForPlaceholder(childFrame);
if (realFrame->GetType() == nsGkAtoms::letterFrame) {
frame = realFrame;
}
}
char16_t ch = 0;
if (frame->IsFrameOfType(nsIFrame::eBidiInlineContainer)) {
if (!(frame->GetStateBits() & NS_FRAME_FIRST_REFLOW)) {
nsContainerFrame* c = static_cast<nsContainerFrame*>(frame);
MOZ_ASSERT(c = do_QueryFrame(frame),
"eBidiInlineContainer must be a nsContainerFrame subclass");
c->DrainSelfOverflowList();
}
const nsStyleVisibility* vis = frame->StyleVisibility();
const nsStyleTextReset* text = frame->StyleTextReset();
if (text->mUnicodeBidi & NS_STYLE_UNICODE_BIDI_OVERRIDE) {
if (NS_STYLE_DIRECTION_RTL == vis->mDirection) {
ch = kRLO;
}
else if (NS_STYLE_DIRECTION_LTR == vis->mDirection) {
ch = kLRO;
}
} else if (text->mUnicodeBidi & NS_STYLE_UNICODE_BIDI_EMBED) {
if (NS_STYLE_DIRECTION_RTL == vis->mDirection) {