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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifdef IBMBIDI
#include "nsBidiPresUtils.h"
#include "nsITextContent.h"
#include "nsTextFragment.h"
#include "nsLayoutAtoms.h"
#include "nsPresContext.h"
#include "nsIRenderingContext.h"
#include "nsIServiceManager.h"
#include "nsFrameManager.h"
#include "nsBidiFrames.h"
#include "nsBidiUtils.h"
static const PRUnichar kSpace = 0x0020;
static const PRUnichar kLineSeparator = 0x2028;
static const PRUnichar kObjectSubstitute = 0xFFFC;
static const PRUnichar kLRE = 0x202A;
static const PRUnichar kRLE = 0x202B;
static const PRUnichar kLRO = 0x202D;
static const PRUnichar kRLO = 0x202E;
static const PRUnichar kPDF = 0x202C;
static const PRUnichar ALEF = 0x05D0;
#define CHAR_IS_HEBREW(c) ((0x0590 <= (c)) && ((c)<= 0x05FF))
// Note: The above code are moved from gfx/src/windows/nsRenderingContextWin.cpp
nsresult
NS_NewContinuingTextFrame(nsIPresShell* aPresShell, nsIFrame** aResult);
nsresult
NS_NewDirectionalFrame(nsIFrame** aNewFrame, PRUnichar aChar);
nsBidiPresUtils::nsBidiPresUtils() : mArraySize(8),
mIndexMap(nsnull),
mLevels(nsnull),
mSuccess(NS_ERROR_FAILURE),
mBidiEngine(nsnull)
{
mBidiEngine = new nsBidi();
if (mBidiEngine && mContentToFrameIndex.Init()) {
mSuccess = NS_OK;
}
}
nsBidiPresUtils::~nsBidiPresUtils()
{
if (mLevels) {
delete[] mLevels;
}
if (mIndexMap) {
delete[] mIndexMap;
}
delete mBidiEngine;
}
PRBool
nsBidiPresUtils::IsSuccessful() const
{
return NS_SUCCEEDED(mSuccess);
}
/* Some helper methods for Resolve() */
static nsresult
CreateBidiContinuation(nsPresContext* aPresContext,
nsIContent* aContent,
nsIFrame* aFrame,
nsIFrame** aNewFrame)
{
NS_PRECONDITION(aNewFrame, "null OUT ptr");
*aNewFrame = nsnull;
NS_PRECONDITION(aFrame, "null ptr");
nsIPresShell *presShell = aPresContext->PresShell();
NS_ASSERTION(presShell, "PresShell must be set on PresContext before calling nsBidiPresUtils::CreateBidiContinuation");
NS_NewContinuingTextFrame(presShell, aNewFrame);
if (!(*aNewFrame) ) {
return NS_ERROR_OUT_OF_MEMORY;
}
nsStyleContext* styleContext = aFrame->GetStyleContext();
NS_ASSERTION(styleContext, "Frame has no styleContext in nsBidiPresUtils::CreateBidiContinuation");
nsIFrame* parent = aFrame->GetParent();
NS_ASSERTION(parent, "Couldn't get frame parent in nsBidiPresUtils::CreateBidiContinuation");
(*aNewFrame)->Init(aPresContext, aContent, parent, styleContext, nsnull);
// XXX: TODO: Instead, create and insert entire frame list
(*aNewFrame)->SetNextSibling(nsnull);
// The list name nsLayoutAtoms::nextBidi would indicate we don't want reflow
parent->InsertFrames(nsLayoutAtoms::nextBidi, aFrame, *aNewFrame);
return NS_OK;
}
/*
* Overview of the implementation of Resolve():
*
* Walk through the descendants of aBlockFrame and build:
* * mLogicalArray: an nsVoidArray of nsIFrame* pointers in logical order
* * mBuffer: an nsAutoString 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 mLogicalArray, setting the
* baseLevel, embeddingLevel, and charType properties according to the results
* returned by the Bidi engine and CalculateCharType().
*
* The rendering layer requires each text frame to contain text in only one
* direction and of only one character type, so we may need to call
* EnsureBidiContinuation() to split frames. We may also need to call
* RemoveBidiContinuation() to delete frames created by
* EnsureBidiContinuation() in previous reflows.
*/
nsresult
nsBidiPresUtils::Resolve(nsPresContext* aPresContext,
nsIFrame* aBlockFrame,
nsIFrame* aFirstChild,
PRBool& aForceReflow,
PRBool aIsVisualFormControl)
{
aForceReflow = PR_FALSE;
mLogicalFrames.Clear();
mContentToFrameIndex.Clear();
// handle bidi-override being set on the block itself before calling
// InitLogicalArray.
const nsStyleVisibility* vis = aBlockFrame->GetStyleVisibility();
const nsStyleTextReset* text = aBlockFrame->GetStyleTextReset();
if (text->mUnicodeBidi == NS_STYLE_UNICODE_BIDI_OVERRIDE) {
nsresult rv = NS_OK;
nsIFrame *directionalFrame = nsnull;
if (NS_STYLE_DIRECTION_RTL == vis->mDirection) {
rv = NS_NewDirectionalFrame(&directionalFrame, kRLO);
}
else if (NS_STYLE_DIRECTION_LTR == vis->mDirection) {
rv = NS_NewDirectionalFrame(&directionalFrame, kLRO);
}
if (directionalFrame && NS_SUCCEEDED(rv)) {
mLogicalFrames.AppendElement(directionalFrame);
}
}
mSuccess = InitLogicalArray(aPresContext, aFirstChild, nsnull, PR_TRUE);
if (text->mUnicodeBidi == NS_STYLE_UNICODE_BIDI_OVERRIDE) {
nsIFrame *directionalFrame = nsnull;
nsresult rv = NS_NewDirectionalFrame(&directionalFrame, kPDF);
if (directionalFrame && NS_SUCCEEDED(rv)) {
mLogicalFrames.AppendElement(directionalFrame);
}
}
if (NS_FAILED(mSuccess) ) {
return mSuccess;
}
CreateBlockBuffer(aPresContext);
PRInt32 bufferLength = mBuffer.Length();
if (bufferLength < 1) {
mSuccess = NS_OK;
return mSuccess;
}
PRInt32 runCount;
PRUint8 embeddingLevel;
nsBidiLevel paraLevel = embeddingLevel =
(NS_STYLE_DIRECTION_RTL == vis->mDirection)
? NSBIDI_RTL : NSBIDI_LTR;
mSuccess = mBidiEngine->SetPara(mBuffer.get(), bufferLength, paraLevel, nsnull);
if (NS_FAILED(mSuccess) ) {
return mSuccess;
}
PRBool isVisual;
if (aIsVisualFormControl) {
isVisual = PR_FALSE;
} else {
isVisual = aPresContext->IsVisualMode();
}
mSuccess = mBidiEngine->CountRuns(&runCount);
if (NS_FAILED(mSuccess) ) {
return mSuccess;
}
PRInt32 runLength = 0;
PRInt32 fragmentLength = 0;
PRInt32 temp;
PRInt32 frameIndex = -1;
PRInt32 frameCount = mLogicalFrames.Count();
PRInt32 contentOffset = 0; // offset within current frame
PRInt32 lineOffset = 0; // offset within mBuffer
PRInt32 logicalLimit = 0;
PRInt32 numRun = -1;
PRUint8 charType;
PRUint8 prevType = eCharType_LeftToRight;
PRBool isTextFrame = PR_FALSE;
nsIFrame* frame = nsnull;
nsIFrame* nextBidi;
nsIContent* content = nsnull;
nsCOMPtr<nsITextContent> textContent;
const nsTextFragment* fragment;
nsIAtom* frameType = nsnull;
nsPropertyTable *propTable = aPresContext->PropertyTable();
for (; ;) {
if (fragmentLength <= 0) {
if (++frameIndex >= frameCount) {
break;
}
contentOffset = 0;
frame = (nsIFrame*) (mLogicalFrames[frameIndex]);
frameType = frame->GetType();
if (nsLayoutAtoms::textFrame == frameType) {
content = frame->GetContent();
if (!content) {
mSuccess = NS_OK;
break;
}
textContent = do_QueryInterface(content, &mSuccess);
if (NS_FAILED(mSuccess) || (!textContent) ) {
break;
}
fragment = textContent->Text();
if (!fragment) {
mSuccess = NS_ERROR_FAILURE;
break;
}
fragmentLength = fragment->GetLength();
isTextFrame = PR_TRUE;
} // if text frame
else {
isTextFrame = PR_FALSE;
fragmentLength = 1;
}
} // if (fragmentLength <= 0)
if (runLength <= 0) {
if (++numRun >= runCount) {
break;
}
lineOffset = logicalLimit;
if (NS_FAILED(mBidiEngine->GetLogicalRun(
lineOffset, &logicalLimit, &embeddingLevel) ) ) {
break;
}
runLength = logicalLimit - lineOffset;
if (isVisual) {
embeddingLevel = paraLevel;
}
} // if (runLength <= 0)
if (nsLayoutAtoms::directionalFrame == frameType) {
delete frame;
++lineOffset;
}
else {
propTable->SetProperty(frame, nsLayoutAtoms::embeddingLevel,
NS_INT32_TO_PTR(embeddingLevel), nsnull, nsnull);
propTable->SetProperty(frame, nsLayoutAtoms::baseLevel,
NS_INT32_TO_PTR(paraLevel), nsnull, nsnull);
if (isTextFrame) {
PRInt32 typeLimit = PR_MIN(logicalLimit, lineOffset + fragmentLength);
CalculateCharType(lineOffset, typeLimit, logicalLimit, runLength,
runCount, charType, prevType);
// IBMBIDI - Egypt - Start
propTable->SetProperty(frame, nsLayoutAtoms::charType,
NS_INT32_TO_PTR(charType), nsnull, nsnull);
// IBMBIDI - Egypt - End
if ( (runLength > 0) && (runLength < fragmentLength) ) {
if (!EnsureBidiContinuation(aPresContext, content, frame,
&nextBidi, frameIndex) ) {
break;
}
frame->AdjustOffsetsForBidi(contentOffset, contentOffset + runLength);
frame = nextBidi;
contentOffset += runLength;
} // if (runLength < fragmentLength)
else {
frame->AdjustOffsetsForBidi(contentOffset, contentOffset + fragmentLength);
PRInt32 newIndex = 0;
mContentToFrameIndex.Get(content, &newIndex);
if (newIndex > frameIndex) {
RemoveBidiContinuation(aPresContext, frame,
frameIndex, newIndex, temp);
aForceReflow = PR_TRUE;
runLength -= temp;
fragmentLength -= temp;
lineOffset += temp;
frameIndex = newIndex;
}
}
} // isTextFrame
else {
++lineOffset;
}
} // not directionalFrame
temp = runLength;
runLength -= fragmentLength;
fragmentLength -= temp;
} // for
return mSuccess;
}
nsresult
nsBidiPresUtils::InitLogicalArray(nsPresContext* aPresContext,
nsIFrame* aCurrentFrame,
nsIFrame* aNextInFlow,
PRBool aAddMarkers)
{
nsIFrame* frame;
nsIFrame* directionalFrame;
nsresult rv;
nsresult res = NS_OK;
for (frame = aCurrentFrame;
frame && frame != aNextInFlow;
frame = frame->GetNextSibling()) {
rv = NS_ERROR_FAILURE;
const nsStyleDisplay* display = frame->GetStyleDisplay();
if (aAddMarkers && !display->IsBlockLevel() ) {
const nsStyleVisibility* vis = frame->GetStyleVisibility();
const nsStyleTextReset* text = frame->GetStyleTextReset();
switch (text->mUnicodeBidi) {
case NS_STYLE_UNICODE_BIDI_NORMAL:
break;
case NS_STYLE_UNICODE_BIDI_EMBED:
if (NS_STYLE_DIRECTION_RTL == vis->mDirection) {
rv = NS_NewDirectionalFrame(&directionalFrame, kRLE);
}
else if (NS_STYLE_DIRECTION_LTR == vis->mDirection) {
rv = NS_NewDirectionalFrame(&directionalFrame, kLRE);
}
break;
case NS_STYLE_UNICODE_BIDI_OVERRIDE:
if (NS_STYLE_DIRECTION_RTL == vis->mDirection) {
rv = NS_NewDirectionalFrame(&directionalFrame, kRLO);
}
else if (NS_STYLE_DIRECTION_LTR == vis->mDirection) {
rv = NS_NewDirectionalFrame(&directionalFrame, kLRO);
}
break;
}
if (NS_SUCCEEDED(rv) ) {
mLogicalFrames.AppendElement(directionalFrame);
}
}
nsIAtom* frameType = frame->GetType();
if ( (!display->IsBlockLevel() )
&& ( (nsLayoutAtoms::inlineFrame == frameType)
|| (nsLayoutAtoms::positionedInlineFrame == frameType)
|| (nsLayoutAtoms::letterFrame == frameType)
|| (nsLayoutAtoms::blockFrame == frameType) ) ) {
nsIFrame* kid = frame->GetFirstChild(nsnull);
res = InitLogicalArray(aPresContext, kid, aNextInFlow, aAddMarkers);
}
else {
/* Bidi leaf frame: add the frame to the mLogicalFrames array,
* and add its index to the mContentToFrameIndex hashtable. This
* will be used in RemoveBidiContinuation() to identify the last
* frame in the array with a given content.
*/
nsIContent* content = frame->GetContent();
if (content) {
mContentToFrameIndex.Put(content, mLogicalFrames.Count());
}
mLogicalFrames.AppendElement(frame);
}
// If the element is attributed by dir, indicate direction pop (add PDF frame)
if (NS_SUCCEEDED(rv) ) {
rv = NS_NewDirectionalFrame(&directionalFrame, kPDF);
if (NS_SUCCEEDED(rv) ) {
mLogicalFrames.AppendElement(directionalFrame);
}
}
} // for
return res;
}
void
nsBidiPresUtils::CreateBlockBuffer(nsPresContext* aPresContext)
{
mBuffer.SetLength(0);
nsIFrame* frame;
nsIContent* prevContent = nsnull;
nsCOMPtr<nsITextContent> textContent;
PRUint32 i;
PRUint32 count = mLogicalFrames.Count();
for (i = 0; i < count; i++) {
frame = (nsIFrame*) (mLogicalFrames[i]);
nsIAtom* frameType = frame->GetType();
if (nsLayoutAtoms::textFrame == frameType) {
nsIContent* content = frame->GetContent();
if (!content) {
mSuccess = NS_OK;
break;
}
if (content == prevContent) {
continue;
}
prevContent = content;
textContent = do_QueryInterface(content, &mSuccess);
if ( (NS_FAILED(mSuccess) ) || (!textContent) ) {
break;
}
textContent->Text()->AppendTo(mBuffer);
}
else if (nsLayoutAtoms::brFrame == frameType) { // break frame
// Append line separator
mBuffer.Append( (PRUnichar) kLineSeparator);
}
else if (nsLayoutAtoms::directionalFrame == frameType) {
nsDirectionalFrame* dirFrame;
frame->QueryInterface(NS_GET_IID(nsDirectionalFrame),
(void**) &dirFrame);
mBuffer.Append(dirFrame->GetChar() );
}
else { // not text frame
// See the Unicode Bidi Algorithm:
// "...inline objects (such as graphics) are treated as if they are ... U+FFFC"
mBuffer.Append( (PRUnichar) kObjectSubstitute);
}
}
// XXX: TODO: Handle preformatted text ('\n')
mBuffer.ReplaceChar("\t\r\n", kSpace);
}
void
nsBidiPresUtils::ReorderFrames(nsPresContext* aPresContext,
nsIRenderingContext* aRendContext,
nsIFrame* aFirstChild,
nsIFrame* aNextInFlow,
PRInt32 aChildCount)
{
mLogicalFrames.Clear();
if (NS_SUCCEEDED(InitLogicalArray(aPresContext, aFirstChild, aNextInFlow))
&& (mLogicalFrames.Count() > 1)) {
PRBool bidiEnabled;
// Set bidiEnabled to true if the line is reordered
Reorder(aPresContext, bidiEnabled);
if (bidiEnabled) {
RepositionInlineFrames(aPresContext, aRendContext, aFirstChild, aChildCount);
}
}
}
nsresult
nsBidiPresUtils::Reorder(nsPresContext* aPresContext,
PRBool& aBidiEnabled)
{
aBidiEnabled = PR_FALSE;
PRInt32 count = mLogicalFrames.Count();
if (mArraySize < count) {
mArraySize = count << 1;
if (mLevels) {
delete[] mLevels;
mLevels = nsnull;
}
if (mIndexMap) {
delete[] mIndexMap;
mIndexMap = nsnull;
}
}
if (!mLevels) {
mLevels = new PRUint8[mArraySize];
if (!mLevels) {
return NS_ERROR_OUT_OF_MEMORY;
}
}
memset(mLevels, 0, sizeof(PRUint8) * mArraySize);
nsIFrame* frame;
PRInt32 i;
for (i = 0; i < count; i++) {
frame = (nsIFrame*) (mLogicalFrames[i]);
mLevels[i] = NS_GET_EMBEDDING_LEVEL(frame);
}
if (!mIndexMap) {
mIndexMap = new PRInt32[mArraySize];
}
if (!mIndexMap) {
mSuccess = NS_ERROR_OUT_OF_MEMORY;
}
else {
memset(mIndexMap, 0, sizeof(PRUint32) * mArraySize);
mSuccess = mBidiEngine->ReorderVisual(mLevels, count, mIndexMap);
if (NS_SUCCEEDED(mSuccess) ) {
mVisualFrames.Clear();
for (i = 0; i < count; i++) {
mVisualFrames.AppendElement(mLogicalFrames[mIndexMap[i]]);
if (i != mIndexMap[i]) {
aBidiEnabled = PR_TRUE;
}
}
} // NS_SUCCEEDED(mSuccess)
} // indexMap
if (NS_FAILED(mSuccess) ) {
aBidiEnabled = PR_FALSE;
}
return mSuccess;
}
void
nsBidiPresUtils::RepositionInlineFrames(nsPresContext* aPresContext,
nsIRenderingContext* aRendContext,
nsIFrame* aFirstChild,
PRInt32 aChildCount) const
{
PRInt32 count = mVisualFrames.Count();
if (count < 2) {
return;
}
nsIFrame* frame = (nsIFrame*) (mVisualFrames[0]);
PRInt32 i;
PRInt32 ch;
PRInt32 charType;
nscoord width, dWidth, alefWidth, dx;
PRUnichar buf[2] = {ALEF, 0x0000};
PRBool isBidiSystem;
PRUint32 hints = 0;
dWidth = alefWidth = dx = 0;
aRendContext->GetHints(hints);
isBidiSystem = (hints & NS_RENDERING_HINT_BIDI_REORDERING);
nsRect rect = frame->GetRect();
if (frame != aFirstChild) {
rect.x = aFirstChild->GetPosition().x;
frame->SetPosition(nsPoint(rect.x, rect.y));
}
nsPropertyTable *propTable = aPresContext->PropertyTable();
for (i = 1; i < count; i++) {
ch = 0;
charType = NS_PTR_TO_INT32(propTable->GetProperty((nsIFrame*)mVisualFrames[i], nsLayoutAtoms::charType));
if (CHARTYPE_IS_RTL(charType) ) {
ch = NS_PTR_TO_INT32(propTable->GetProperty(frame,
nsLayoutAtoms::endsInDiacritic));
if (ch) {
if (!alefWidth) {
aRendContext->GetWidth(buf, 1, alefWidth, nsnull);
}
dWidth = 0;
if (isBidiSystem) {
buf[1] = (PRUnichar) ch;
aRendContext->GetWidth(buf, 2, width, nsnull);
dWidth = width - alefWidth;
}
if (dWidth <= 0) {
frame->SetPosition(nsPoint(rect.x + (nscoord)((float)width/8), rect.y));
}
}
}
frame = (nsIFrame*) (mVisualFrames[i]);
if (ch) {
dx += (rect.width - dWidth);
frame->SetPosition(nsPoint(rect.x + dWidth, frame->GetPosition().y));
} else
frame->SetPosition(nsPoint(rect.XMost(), frame->GetPosition().y));
rect = frame->GetRect();
} // for
if (dx > 0) {
PRInt32 alignRight = NS_GET_BASE_LEVEL(frame);
if (0 == (alignRight & 1) ) {
const nsStyleText* styleText = frame->GetStyleText();
if (NS_STYLE_TEXT_ALIGN_RIGHT == styleText->mTextAlign
|| NS_STYLE_TEXT_ALIGN_MOZ_RIGHT == styleText->mTextAlign) {
alignRight = 1;
}
}
if (alignRight & 1) {
for (i = 0; i < count; i++) {
frame = (nsIFrame*) (mVisualFrames[i]);
frame->SetPosition(frame->GetPosition() + nsPoint(dx, 0));
}
}
}
// Now adjust inline container frames.
// Example: LTR paragraph
// <p><b>english HEBREW</b> 123</p>
// should be displayed as
// <p><b>english </b>123 <b>WERBEH</b></p>
// We assume that <b></b> rectangle takes all the room from "english" left edge to
// "WERBEH" right edge.
frame = aFirstChild;
for (i = 0; i < aChildCount; i++) {
nsIAtom* frameType = frame->GetType();
if ( (nsLayoutAtoms::inlineFrame == frameType)
|| (nsLayoutAtoms::positionedInlineFrame == frameType)
|| (nsLayoutAtoms::letterFrame == frameType)
|| (nsLayoutAtoms::blockFrame == frameType) ) {
PRInt32 minX = 0x7FFFFFFF;
PRInt32 maxX = 0;
RepositionContainerFrame(aPresContext, frame, minX, maxX);
}
frame = frame->GetNextSibling();
} // for
}
void
nsBidiPresUtils::RepositionContainerFrame(nsPresContext* aPresContext,
nsIFrame* aContainer,
PRInt32& aMinX,
PRInt32& aMaxX) const
{
nsIFrame* frame;
PRInt32 minX = 0x7FFFFFFF;
PRInt32 maxX = 0;
nsIFrame* firstChild = aContainer->GetFirstChild(nsnull);
for (frame = firstChild; frame; frame = frame->GetNextSibling()) {
nsIAtom* frameType = frame->GetType();
if ( (nsLayoutAtoms::inlineFrame == frameType)
|| (nsLayoutAtoms::positionedInlineFrame == frameType)
|| (nsLayoutAtoms::letterFrame == frameType)
|| (nsLayoutAtoms::blockFrame == frameType) ) {
RepositionContainerFrame(aPresContext, frame, minX, maxX);
}
else {
nsRect rect = frame->GetRect();
minX = PR_MIN(minX, rect.x);
maxX = PR_MAX(maxX, rect.XMost());
}
}
aMinX = PR_MIN(minX, aMinX);
aMaxX = PR_MAX(maxX, aMaxX);
if (minX < maxX) {
nsRect rect = aContainer->GetRect();
rect.x = minX;
rect.width = maxX - minX;
aContainer->SetRect(rect);
}
// Now adjust all the kids (kid's coordinates are relative to the parent's)
for (frame = firstChild; frame; frame = frame->GetNextSibling()) {
frame->SetPosition(frame->GetPosition() - nsPoint(minX, 0));
}
}
PRBool
nsBidiPresUtils::EnsureBidiContinuation(nsPresContext* aPresContext,
nsIContent* aContent,
nsIFrame* aFrame,
nsIFrame** aNewFrame,
PRInt32& aFrameIndex)
{
NS_PRECONDITION(aNewFrame, "null OUT ptr");
if (!aNewFrame) {
return PR_FALSE;
}
*aNewFrame = nsnull;
if (!aFrame) {
return PR_FALSE;
}
if (aFrameIndex + 1 < mLogicalFrames.Count() ) {
nsIFrame* frame = (nsIFrame*)mLogicalFrames[aFrameIndex + 1];
if (frame->GetContent() == aContent) {
*aNewFrame = frame;
++aFrameIndex;
aFrame->SetNextInFlow(nsnull);
frame->SetPrevInFlow(nsnull);
}
}
if (nsnull == *aNewFrame) {
mSuccess = CreateBidiContinuation(aPresContext, aContent, aFrame, aNewFrame);
if (NS_FAILED(mSuccess) ) {
return PR_FALSE;
}
}
aPresContext->PropertyTable()->SetProperty(aFrame, nsLayoutAtoms::nextBidi,
(void*) *aNewFrame,
nsnull, nsnull);
return PR_TRUE;
}
void
nsBidiPresUtils::RemoveBidiContinuation(nsPresContext* aPresContext,
nsIFrame* aFrame,
PRInt32 aFirstIndex,
PRInt32 aLastIndex,
PRInt32& aOffset) const
{
nsIFrame* frame;
PRInt32 index;
nsIFrame* parent = aFrame->GetParent();
aOffset = 0;
for (index = aLastIndex; index > aFirstIndex; index--) {
frame = (nsIFrame*) mLogicalFrames[index];
if (nsLayoutAtoms::directionalFrame == frame->GetType()) {
delete frame;
++aOffset;
}
else {
if (frame->GetStateBits() & NS_FRAME_IS_BIDI) {
// only delete Bidi frames
if (parent) {
parent->RemoveFrame(nsLayoutAtoms::nextBidi, frame);
}
else {
frame->Destroy(aPresContext);
}
}
}
}
nsIFrame* thisFramesNextBidiFrame;
nsIFrame* previousFramesNextBidiFrame;
nsPropertyTable *propTable = aPresContext->PropertyTable();
thisFramesNextBidiFrame = NS_STATIC_CAST(nsIFrame*,
propTable->GetProperty(aFrame, nsLayoutAtoms::nextBidi));
if (thisFramesNextBidiFrame) {
// Remove nextBidi property, associated with the current frame
// and with all of its prev-in-flow.
frame = aFrame;
do {
propTable->DeleteProperty(frame, nsLayoutAtoms::nextBidi);
frame = frame->GetPrevInFlow();
if (!frame) {
break;
}
previousFramesNextBidiFrame =
NS_STATIC_CAST(nsIFrame*, propTable->GetProperty(frame,
nsLayoutAtoms::nextBidi));
} while (thisFramesNextBidiFrame == previousFramesNextBidiFrame);
} // if (thisFramesNextBidiFrame)
}
nsresult
nsBidiPresUtils::FormatUnicodeText(nsPresContext* aPresContext,
PRUnichar* aText,
PRInt32& aTextLength,
nsCharType aCharType,
PRBool aIsOddLevel,
PRBool aIsBidiSystem)
{
NS_ASSERTION(aIsOddLevel == 0 || aIsOddLevel == 1, "aIsOddLevel should be 0 or 1");
nsresult rv = NS_OK;
// ahmed
//adjusted for correct numeral shaping
PRUint32 bidiOptions = aPresContext->GetBidi();
switch (GET_BIDI_OPTION_NUMERAL(bidiOptions)) {
case IBMBIDI_NUMERAL_HINDI:
HandleNumbers(aText,aTextLength,IBMBIDI_NUMERAL_HINDI);
break;
case IBMBIDI_NUMERAL_ARABIC:
HandleNumbers(aText,aTextLength,IBMBIDI_NUMERAL_ARABIC);
break;
case IBMBIDI_NUMERAL_REGULAR:
switch (aCharType) {
case eCharType_EuropeanNumber:
HandleNumbers(aText,aTextLength,IBMBIDI_NUMERAL_ARABIC);
break;
case eCharType_ArabicNumber:
HandleNumbers(aText,aTextLength,IBMBIDI_NUMERAL_HINDI);
break;
default:
break;
}
break;
case IBMBIDI_NUMERAL_HINDICONTEXT:
if ( ( (GET_BIDI_OPTION_DIRECTION(bidiOptions)==IBMBIDI_TEXTDIRECTION_RTL) && (IS_ARABIC_DIGIT (aText[0])) ) || (eCharType_ArabicNumber == aCharType) )
HandleNumbers(aText,aTextLength,IBMBIDI_NUMERAL_HINDI);
else if (eCharType_EuropeanNumber == aCharType)
HandleNumbers(aText,aTextLength,IBMBIDI_NUMERAL_ARABIC);
break;
case IBMBIDI_NUMERAL_NOMINAL:
default:
break;
}
PRBool doReverse = PR_FALSE;
PRBool doShape = PR_FALSE;
if (aIsBidiSystem) {
if ( (CHARTYPE_IS_RTL(aCharType)) ^ (aIsOddLevel) )
doReverse = PR_TRUE;
}
else {
if (aIsOddLevel)
doReverse = PR_TRUE;
if (eCharType_RightToLeftArabic == aCharType)
doShape = PR_TRUE;
}
if (doReverse || doShape) {
PRInt32 newLen;
if (mBuffer.Length() < aTextLength) {
mBuffer.SetLength(aTextLength);
if (mBuffer.Length() < aTextLength)
return NS_ERROR_OUT_OF_MEMORY;
}
PRUnichar* buffer = mBuffer.BeginWriting();
if (doReverse) {
rv = mBidiEngine->WriteReverse(aText, aTextLength, buffer,
NSBIDI_DO_MIRRORING, &newLen);
if (NS_SUCCEEDED(rv) ) {
aTextLength = newLen;
memcpy(aText, buffer, aTextLength * sizeof(PRUnichar) );
}
}
if (doShape) {
rv = ArabicShaping(aText, aTextLength, buffer, (PRUint32 *)&newLen,
PR_FALSE, PR_FALSE);
if (NS_SUCCEEDED(rv) ) {
aTextLength = newLen;
memcpy(aText, buffer, aTextLength * sizeof(PRUnichar) );
}
}
}
StripBidiControlCharacters(aText, aTextLength);
return rv;
}
void
nsBidiPresUtils::StripBidiControlCharacters(PRUnichar* aText,
PRInt32& aTextLength) const
{
if ( (nsnull == aText) || (aTextLength < 1) ) {
return;
}
PRInt32 stripLen = 0;
for (PRInt32 i = 0; i < aTextLength; i++) {
// XXX: This silently ignores surrogate characters.
// As of Unicode 4.0, all Bidi control characters are within the BMP.
if (mBidiEngine->IsBidiControl((PRUint32)aText[i])) {
++stripLen;
}
else {
aText[i - stripLen] = aText[i];
}
}
aTextLength -= stripLen;
}
#if 0 // XXX: for the future use ???
void
RemoveDiacritics(PRUnichar* aText,
PRInt32& aTextLength)
{
if (aText && (aTextLength > 0) ) {
PRInt32 offset = 0;
for (PRInt32 i = 0; i < aTextLength && aText[i]; i++) {
if (IS_BIDI_DIACRITIC(aText[i]) ) {
++offset;
continue;
}
aText[i - offset] = aText[i];
}
aTextLength = i - offset;
aText[aTextLength] = 0;
}
}
#endif
void
nsBidiPresUtils::CalculateCharType(PRInt32& aOffset,
PRInt32 aCharTypeLimit,
PRInt32& aRunLimit,
PRInt32& aRunLength,
PRInt32& aRunCount,
PRUint8& aCharType,
PRUint8& aPrevCharType) const
{
PRBool strongTypeFound = PR_FALSE;
PRInt32 offset;
nsCharType charType;
aCharType = eCharType_OtherNeutral;
for (offset = aOffset; offset < aCharTypeLimit; offset++) {
// Make sure we give RTL chartype to all characters that would be classified
// as Right-To-Left by a bidi platform.
// (May differ from the UnicodeData, eg we set RTL chartype to some NSMs.)
if (IS_HEBREW_CHAR(mBuffer[offset]) ) {
charType = eCharType_RightToLeft;
}
else if (IS_ARABIC_ALPHABETIC(mBuffer[offset]) ) {
charType = eCharType_RightToLeftArabic;
}
else {
mBidiEngine->GetCharTypeAt(offset, &charType);
}
if (!CHARTYPE_IS_WEAK(charType) ) {
if (strongTypeFound
&& (charType != aPrevCharType)
&& (CHARTYPE_IS_RTL(charType) || CHARTYPE_IS_RTL(aPrevCharType) ) ) {
// Stop at this point to ensure uni-directionality of the text
// (from platform's point of view).
// Also, don't mix Arabic and Hebrew content (since platform may
// provide BIDI support to one of them only).
aRunLength = offset - aOffset;
aRunLimit = offset;
++aRunCount;
break;
}
if ( (eCharType_RightToLeftArabic == aPrevCharType
|| eCharType_ArabicNumber == aPrevCharType)
&& eCharType_EuropeanNumber == charType) {
charType = eCharType_ArabicNumber;
}
// Set PrevCharType to the last strong type in this frame
// (for correct numeric shaping)
aPrevCharType = charType;
strongTypeFound = PR_TRUE;
aCharType = charType;
}
}
aOffset = offset;
}
nsresult nsBidiPresUtils::GetBidiEngine(nsBidi** aBidiEngine)
{
nsresult rv = NS_ERROR_FAILURE;
if (mBidiEngine) {
*aBidiEngine = mBidiEngine;
rv = NS_OK;
}
return rv;
}
nsresult nsBidiPresUtils::RenderText(const PRUnichar* aText,
PRInt32 aLength,
nsBidiDirection aBaseDirection,
nsPresContext* aPresContext,
nsIRenderingContext& aRenderingContext,
nscoord aX,
nscoord aY,
nsBidiPositionResolve* aPosResolve,
PRInt32 aPosResolveCount)
{
NS_ASSERTION((aPosResolve == nsnull) != (aPosResolveCount > 0), "Incorrect aPosResolve / aPosResolveCount arguments");
PRInt32 runCount;
mBuffer.Assign(aText, aLength);
nsresult rv = mBidiEngine->SetPara(mBuffer.get(), aLength, aBaseDirection, nsnull);
if (NS_FAILED(rv))
return rv;
rv = mBidiEngine->CountRuns(&runCount);
if (NS_FAILED(rv))
return rv;
nscoord width, xEndRun, xStartText = aX;
PRBool isRTL = PR_FALSE;
PRInt32 i, start, limit, length;
PRUint32 visualStart = 0;
PRUint8 charType;
PRUint8 prevType = eCharType_LeftToRight;
nsBidiLevel level;
PRUint32 hints = 0;
aRenderingContext.GetHints(hints);
PRBool isBidiSystem = (hints & NS_RENDERING_HINT_BIDI_REORDERING);
for(int nPosResolve=0; nPosResolve < aPosResolveCount; ++nPosResolve)
{
aPosResolve[nPosResolve].visualIndex = kNotFound;
aPosResolve[nPosResolve].visualLeftTwips = kNotFound;
}
for (i = 0; i < runCount; i++) {
rv = mBidiEngine->GetVisualRun(i, &start, &length, &aBaseDirection);
if (NS_FAILED(rv))
return rv;
rv = mBidiEngine->GetLogicalRun(start, &limit, &level);
if (NS_FAILED(rv))
return rv;
PRInt32 subRunLength = limit - start;
PRInt32 lineOffset = start;
PRInt32 typeLimit = PR_MIN(limit, aLength);
PRInt32 subRunCount = 1;
PRInt32 subRunLimit = typeLimit;
/*
* If |level| is even, i.e. the direction of the run is left-to-right, we
* render the subruns from left to right and increment the x-coordinate
* |aX| by the width of each subrun after rendering.
*
* If |level| is odd, i.e. the direction of the run is right-to-left, we
* render the subruns from right to left. We begin by incrementing |aX| by
* the width of the whole run, and then decrement it by the width of each
* subrun before rendering. After rendering all the subruns, we restore the
* x-coordinate of the end of the run for the start of the next run.
*/
if (level & 1) {
aRenderingContext.GetWidth(aText + start, subRunLength, width, nsnull);
aX += width;
xEndRun = aX;
}
while (subRunCount > 0) {
// CalculateCharType can increment subRunCount if the run
// contains mixed character types
CalculateCharType(lineOffset, typeLimit, subRunLimit, subRunLength, subRunCount, charType, prevType);
if (eCharType_RightToLeftArabic == charType) {
isBidiSystem = (hints & NS_RENDERING_HINT_ARABIC_SHAPING);
}
if (isBidiSystem && (CHARTYPE_IS_RTL(charType) ^ isRTL) ) {
// set reading order into DC
isRTL = !isRTL;
aRenderingContext.SetRightToLeftText(isRTL);
}
nsAutoString runVisualText;
runVisualText.Assign(aText + start, subRunLength);
if (runVisualText.Length() < subRunLength)
return NS_ERROR_OUT_OF_MEMORY;
FormatUnicodeText(aPresContext, runVisualText.BeginWriting(), subRunLength,
(nsCharType)charType, level & 1,
isBidiSystem);
aRenderingContext.GetWidth(runVisualText.get(), subRunLength, width, nsnull);
if (level & 1) {
aX -= width;
}
aRenderingContext.DrawString(runVisualText.get(), subRunLength, aX, aY, width);
/*
* The caller may request to calculate the visual position of one
* or more characters.
*/
for(int nPosResolve=0; nPosResolve<aPosResolveCount; ++nPosResolve)
{
nsBidiPositionResolve* posResolve = &aPosResolve[nPosResolve];
/*
* Did we already resolve this position's visual metric? If so, skip.
*/
if (posResolve->visualLeftTwips != kNotFound)
continue;
/*
* First find out if the logical position is within this run.
*/
if (start <= posResolve->logicalIndex &&
start + subRunLength > posResolve->logicalIndex) {
/*
* If this run is only one character long, we have an easy case:
* the visual position is the x-coord of the start of the run
* less the x-coord of the start of the whole text (saved in xStartText).
*/
if (subRunLength == 1) {
posResolve->visualIndex = visualStart;
posResolve->visualLeftTwips = aX - xStartText;
}
/*
* Otherwise, we need to measure the width of the run's part
* which is to the visual left of the index.
* In other words, the run is broken in two, around the logical index,
* and we measure the part which is visually left.
* If the run is right-to-left, this part will span from after the index
* up to the end of the run; if it is left-to-right, this part will span
* from the start of the run up to (and inclduing) the character before the index.
*/
else {
nscoord subWidth;
// The position in the text where this run's "left part" begins.
const PRUnichar* visualLeftPart;
if (level & 1) {
// One day, son, this could all be replaced with mBidiEngine.GetVisualIndex ...
posResolve->visualIndex = visualStart + (subRunLength - (posResolve->logicalIndex + 1 - start));
// Skipping to the "left part".
visualLeftPart = aText + posResolve->logicalIndex + 1;
}
else {
posResolve->visualIndex = visualStart + (posResolve->logicalIndex - start);
// Skipping to the "left part".
visualLeftPart = aText + start;
}
// The delta between the start of the run and the left part's end.
PRInt32 visualLeftLength = posResolve->visualIndex - visualStart;
aRenderingContext.GetWidth(visualLeftPart,
visualLeftLength,
subWidth, nsnull);
posResolve->visualLeftTwips = aX + subWidth - xStartText;
}
}
}
if (!(level & 1)) {
aX += width;
}
--subRunCount;
start = lineOffset;
subRunLimit = typeLimit;
subRunLength = typeLimit - lineOffset;
} // while
if (level & 1) {
aX = xEndRun;
}
visualStart += length;
} // for
// Restore original reading order
if (isRTL) {
aRenderingContext.SetRightToLeftText(PR_FALSE);
}
return NS_OK;
}
nsresult
nsBidiPresUtils::ReorderUnicodeText(PRUnichar* aText,
PRInt32& aTextLength,
nsCharType aCharType,
PRBool aIsOddLevel,
PRBool aIsBidiSystem)
{
NS_ASSERTION(aIsOddLevel == 0 || aIsOddLevel == 1, "aIsOddLevel should be 0 or 1");
nsresult rv = NS_OK;
PRBool doReverse = PR_FALSE;
if (aIsBidiSystem) {
if ( (CHARTYPE_IS_RTL(aCharType)) ^ (aIsOddLevel) )
doReverse = PR_TRUE;
}
else {
if (aIsOddLevel)
doReverse = PR_TRUE;
}
if (doReverse) {
PRInt32 newLen;
if (mBuffer.Length() < aTextLength) {
mBuffer.SetLength(aTextLength);
if (mBuffer.Length() < aTextLength)
return NS_ERROR_OUT_OF_MEMORY;
}
PRUnichar* buffer = mBuffer.BeginWriting();
if (doReverse) {
rv = mBidiEngine->WriteReverse(aText, aTextLength, buffer,
NSBIDI_DO_MIRRORING, &newLen);
if (NS_SUCCEEDED(rv) ) {
aTextLength = newLen;
memcpy(aText, buffer, aTextLength * sizeof(PRUnichar) );
}
}
}
return rv;
}
#endif // IBMBIDI
|