1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "nsBidi.h"
8 
CountRuns(int32_t * aRunCount)9 nsresult nsBidi::CountRuns(int32_t* aRunCount) {
10   UErrorCode errorCode = U_ZERO_ERROR;
11   *aRunCount = ubidi_countRuns(mBiDi, &errorCode);
12   if (U_SUCCESS(errorCode)) {
13     mLength = ubidi_getProcessedLength(mBiDi);
14     mLevels = mLength > 0 ? ubidi_getLevels(mBiDi, &errorCode) : nullptr;
15   }
16   return ICUUtils::UErrorToNsResult(errorCode);
17 }
18 
GetLogicalRun(int32_t aLogicalStart,int32_t * aLogicalLimit,nsBidiLevel * aLevel)19 void nsBidi::GetLogicalRun(int32_t aLogicalStart, int32_t* aLogicalLimit,
20                            nsBidiLevel* aLevel) {
21   MOZ_ASSERT(mLevels, "CountRuns hasn't been run?");
22   MOZ_RELEASE_ASSERT(aLogicalStart < mLength, "Out of bound");
23   // This function implements an alternative approach to get logical
24   // run that is based on levels of characters, which would avoid O(n^2)
25   // performance issue when used in a loop over runs.
26   // Per comment in ubidi_getLogicalRun, that function doesn't use this
27   // approach because levels have special interpretation when reordering
28   // mode is UBIDI_REORDER_RUNS_ONLY. Since we don't use this mode in
29   // Gecko, it should be safe to just use levels for this function.
30   MOZ_ASSERT(ubidi_getReorderingMode(mBiDi) != UBIDI_REORDER_RUNS_ONLY,
31              "Don't support UBIDI_REORDER_RUNS_ONLY mode");
32 
33   nsBidiLevel level = mLevels[aLogicalStart];
34   int32_t limit;
35   for (limit = aLogicalStart + 1; limit < mLength; limit++) {
36     if (mLevels[limit] != level) {
37       break;
38     }
39   }
40   *aLogicalLimit = limit;
41   *aLevel = level;
42 }
43