Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsTextFrameUtils.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "nsUnicharUtils.h" |
michael@0 | 9 | #include "nsBidiUtils.h" |
michael@0 | 10 | #include "nsIContent.h" |
michael@0 | 11 | #include "nsStyleStruct.h" |
michael@0 | 12 | #include "nsTextFragment.h" |
michael@0 | 13 | #include <algorithm> |
michael@0 | 14 | |
michael@0 | 15 | static bool IsDiscardable(char16_t ch, uint32_t* aFlags) |
michael@0 | 16 | { |
michael@0 | 17 | // Unlike IS_DISCARDABLE, we don't discard \r. \r will be ignored by gfxTextRun |
michael@0 | 18 | // and discarding it would force us to copy text in many cases of preformatted |
michael@0 | 19 | // text containing \r\n. |
michael@0 | 20 | if (ch == CH_SHY) { |
michael@0 | 21 | *aFlags |= nsTextFrameUtils::TEXT_HAS_SHY; |
michael@0 | 22 | return true; |
michael@0 | 23 | } |
michael@0 | 24 | return IsBidiControl(ch); |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | static bool IsDiscardable(uint8_t ch, uint32_t* aFlags) |
michael@0 | 28 | { |
michael@0 | 29 | if (ch == CH_SHY) { |
michael@0 | 30 | *aFlags |= nsTextFrameUtils::TEXT_HAS_SHY; |
michael@0 | 31 | return true; |
michael@0 | 32 | } |
michael@0 | 33 | return false; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | char16_t* |
michael@0 | 37 | nsTextFrameUtils::TransformText(const char16_t* aText, uint32_t aLength, |
michael@0 | 38 | char16_t* aOutput, |
michael@0 | 39 | CompressionMode aCompression, |
michael@0 | 40 | uint8_t* aIncomingFlags, |
michael@0 | 41 | gfxSkipChars* aSkipChars, |
michael@0 | 42 | uint32_t* aAnalysisFlags) |
michael@0 | 43 | { |
michael@0 | 44 | uint32_t flags = 0; |
michael@0 | 45 | char16_t* outputStart = aOutput; |
michael@0 | 46 | |
michael@0 | 47 | bool lastCharArabic = false; |
michael@0 | 48 | |
michael@0 | 49 | if (aCompression == COMPRESS_NONE || |
michael@0 | 50 | aCompression == DISCARD_NEWLINE) { |
michael@0 | 51 | // Skip discardables. |
michael@0 | 52 | uint32_t i; |
michael@0 | 53 | for (i = 0; i < aLength; ++i) { |
michael@0 | 54 | char16_t ch = *aText++; |
michael@0 | 55 | if (IsDiscardable(ch, &flags) || |
michael@0 | 56 | (ch == '\n' && aCompression == DISCARD_NEWLINE)) { |
michael@0 | 57 | aSkipChars->SkipChar(); |
michael@0 | 58 | } else { |
michael@0 | 59 | aSkipChars->KeepChar(); |
michael@0 | 60 | if (ch > ' ') { |
michael@0 | 61 | lastCharArabic = IS_ARABIC_CHAR(ch); |
michael@0 | 62 | } else if (ch == '\t') { |
michael@0 | 63 | flags |= TEXT_HAS_TAB; |
michael@0 | 64 | } |
michael@0 | 65 | *aOutput++ = ch; |
michael@0 | 66 | } |
michael@0 | 67 | } |
michael@0 | 68 | if (lastCharArabic) { |
michael@0 | 69 | *aIncomingFlags |= INCOMING_ARABICCHAR; |
michael@0 | 70 | } else { |
michael@0 | 71 | *aIncomingFlags &= ~INCOMING_ARABICCHAR; |
michael@0 | 72 | } |
michael@0 | 73 | *aIncomingFlags &= ~INCOMING_WHITESPACE; |
michael@0 | 74 | } else { |
michael@0 | 75 | bool inWhitespace = (*aIncomingFlags & INCOMING_WHITESPACE) != 0; |
michael@0 | 76 | uint32_t i; |
michael@0 | 77 | for (i = 0; i < aLength; ++i) { |
michael@0 | 78 | char16_t ch = *aText++; |
michael@0 | 79 | bool nowInWhitespace; |
michael@0 | 80 | if (ch == ' ' && |
michael@0 | 81 | (i + 1 >= aLength || |
michael@0 | 82 | !IsSpaceCombiningSequenceTail(aText, aLength - (i + 1)))) { |
michael@0 | 83 | nowInWhitespace = true; |
michael@0 | 84 | } else if (ch == '\n' && aCompression == COMPRESS_WHITESPACE_NEWLINE) { |
michael@0 | 85 | if (i > 0 && IS_CJ_CHAR(aText[-1]) && |
michael@0 | 86 | i + 1 < aLength && IS_CJ_CHAR(aText[1])) { |
michael@0 | 87 | // Discard newlines between CJK chars. |
michael@0 | 88 | // XXX this really requires more context to get right! |
michael@0 | 89 | aSkipChars->SkipChar(); |
michael@0 | 90 | continue; |
michael@0 | 91 | } |
michael@0 | 92 | nowInWhitespace = true; |
michael@0 | 93 | } else { |
michael@0 | 94 | nowInWhitespace = ch == '\t'; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | if (!nowInWhitespace) { |
michael@0 | 98 | if (IsDiscardable(ch, &flags)) { |
michael@0 | 99 | aSkipChars->SkipChar(); |
michael@0 | 100 | nowInWhitespace = inWhitespace; |
michael@0 | 101 | } else { |
michael@0 | 102 | *aOutput++ = ch; |
michael@0 | 103 | aSkipChars->KeepChar(); |
michael@0 | 104 | lastCharArabic = IS_ARABIC_CHAR(ch); |
michael@0 | 105 | } |
michael@0 | 106 | } else { |
michael@0 | 107 | if (inWhitespace) { |
michael@0 | 108 | aSkipChars->SkipChar(); |
michael@0 | 109 | } else { |
michael@0 | 110 | if (ch != ' ') { |
michael@0 | 111 | flags |= TEXT_WAS_TRANSFORMED; |
michael@0 | 112 | } |
michael@0 | 113 | *aOutput++ = ' '; |
michael@0 | 114 | aSkipChars->KeepChar(); |
michael@0 | 115 | } |
michael@0 | 116 | } |
michael@0 | 117 | inWhitespace = nowInWhitespace; |
michael@0 | 118 | } |
michael@0 | 119 | if (lastCharArabic) { |
michael@0 | 120 | *aIncomingFlags |= INCOMING_ARABICCHAR; |
michael@0 | 121 | } else { |
michael@0 | 122 | *aIncomingFlags &= ~INCOMING_ARABICCHAR; |
michael@0 | 123 | } |
michael@0 | 124 | if (inWhitespace) { |
michael@0 | 125 | *aIncomingFlags |= INCOMING_WHITESPACE; |
michael@0 | 126 | } else { |
michael@0 | 127 | *aIncomingFlags &= ~INCOMING_WHITESPACE; |
michael@0 | 128 | } |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | if (outputStart + aLength != aOutput) { |
michael@0 | 132 | flags |= TEXT_WAS_TRANSFORMED; |
michael@0 | 133 | } |
michael@0 | 134 | *aAnalysisFlags = flags; |
michael@0 | 135 | return aOutput; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | uint8_t* |
michael@0 | 139 | nsTextFrameUtils::TransformText(const uint8_t* aText, uint32_t aLength, |
michael@0 | 140 | uint8_t* aOutput, |
michael@0 | 141 | CompressionMode aCompression, |
michael@0 | 142 | uint8_t* aIncomingFlags, |
michael@0 | 143 | gfxSkipChars* aSkipChars, |
michael@0 | 144 | uint32_t* aAnalysisFlags) |
michael@0 | 145 | { |
michael@0 | 146 | uint32_t flags = 0; |
michael@0 | 147 | uint8_t* outputStart = aOutput; |
michael@0 | 148 | |
michael@0 | 149 | if (aCompression == COMPRESS_NONE || |
michael@0 | 150 | aCompression == DISCARD_NEWLINE) { |
michael@0 | 151 | // Skip discardables. |
michael@0 | 152 | uint32_t i; |
michael@0 | 153 | for (i = 0; i < aLength; ++i) { |
michael@0 | 154 | uint8_t ch = *aText++; |
michael@0 | 155 | if (IsDiscardable(ch, &flags) || |
michael@0 | 156 | (ch == '\n' && aCompression == DISCARD_NEWLINE)) { |
michael@0 | 157 | aSkipChars->SkipChar(); |
michael@0 | 158 | } else { |
michael@0 | 159 | aSkipChars->KeepChar(); |
michael@0 | 160 | if (ch == '\t') { |
michael@0 | 161 | flags |= TEXT_HAS_TAB; |
michael@0 | 162 | } |
michael@0 | 163 | *aOutput++ = ch; |
michael@0 | 164 | } |
michael@0 | 165 | } |
michael@0 | 166 | *aIncomingFlags &= ~(INCOMING_ARABICCHAR | INCOMING_WHITESPACE); |
michael@0 | 167 | } else { |
michael@0 | 168 | bool inWhitespace = (*aIncomingFlags & INCOMING_WHITESPACE) != 0; |
michael@0 | 169 | uint32_t i; |
michael@0 | 170 | for (i = 0; i < aLength; ++i) { |
michael@0 | 171 | uint8_t ch = *aText++; |
michael@0 | 172 | bool nowInWhitespace = ch == ' ' || ch == '\t' || |
michael@0 | 173 | (ch == '\n' && aCompression == COMPRESS_WHITESPACE_NEWLINE); |
michael@0 | 174 | if (!nowInWhitespace) { |
michael@0 | 175 | if (IsDiscardable(ch, &flags)) { |
michael@0 | 176 | aSkipChars->SkipChar(); |
michael@0 | 177 | nowInWhitespace = inWhitespace; |
michael@0 | 178 | } else { |
michael@0 | 179 | *aOutput++ = ch; |
michael@0 | 180 | aSkipChars->KeepChar(); |
michael@0 | 181 | } |
michael@0 | 182 | } else { |
michael@0 | 183 | if (inWhitespace) { |
michael@0 | 184 | aSkipChars->SkipChar(); |
michael@0 | 185 | } else { |
michael@0 | 186 | if (ch != ' ') { |
michael@0 | 187 | flags |= TEXT_WAS_TRANSFORMED; |
michael@0 | 188 | } |
michael@0 | 189 | *aOutput++ = ' '; |
michael@0 | 190 | aSkipChars->KeepChar(); |
michael@0 | 191 | } |
michael@0 | 192 | } |
michael@0 | 193 | inWhitespace = nowInWhitespace; |
michael@0 | 194 | } |
michael@0 | 195 | *aIncomingFlags &= ~INCOMING_ARABICCHAR; |
michael@0 | 196 | if (inWhitespace) { |
michael@0 | 197 | *aIncomingFlags |= INCOMING_WHITESPACE; |
michael@0 | 198 | } else { |
michael@0 | 199 | *aIncomingFlags &= ~INCOMING_WHITESPACE; |
michael@0 | 200 | } |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | if (outputStart + aLength != aOutput) { |
michael@0 | 204 | flags |= TEXT_WAS_TRANSFORMED; |
michael@0 | 205 | } |
michael@0 | 206 | *aAnalysisFlags = flags; |
michael@0 | 207 | return aOutput; |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | uint32_t |
michael@0 | 211 | nsTextFrameUtils::ComputeApproximateLengthWithWhitespaceCompression( |
michael@0 | 212 | nsIContent *aContent, const nsStyleText *aStyleText) |
michael@0 | 213 | { |
michael@0 | 214 | const nsTextFragment *frag = aContent->GetText(); |
michael@0 | 215 | // This is an approximation so we don't really need anything |
michael@0 | 216 | // too fancy here. |
michael@0 | 217 | uint32_t len; |
michael@0 | 218 | if (aStyleText->WhiteSpaceIsSignificant()) { |
michael@0 | 219 | len = frag->GetLength(); |
michael@0 | 220 | } else { |
michael@0 | 221 | bool is2b = frag->Is2b(); |
michael@0 | 222 | union { |
michael@0 | 223 | const char *s1b; |
michael@0 | 224 | const char16_t *s2b; |
michael@0 | 225 | } u; |
michael@0 | 226 | if (is2b) { |
michael@0 | 227 | u.s2b = frag->Get2b(); |
michael@0 | 228 | } else { |
michael@0 | 229 | u.s1b = frag->Get1b(); |
michael@0 | 230 | } |
michael@0 | 231 | bool prevWS = true; // more important to ignore blocks with |
michael@0 | 232 | // only whitespace than get inline boundaries |
michael@0 | 233 | // exactly right |
michael@0 | 234 | len = 0; |
michael@0 | 235 | for (uint32_t i = 0, i_end = frag->GetLength(); i < i_end; ++i) { |
michael@0 | 236 | char16_t c = is2b ? u.s2b[i] : u.s1b[i]; |
michael@0 | 237 | if (c == ' ' || c == '\n' || c == '\t' || c == '\r') { |
michael@0 | 238 | if (!prevWS) { |
michael@0 | 239 | ++len; |
michael@0 | 240 | } |
michael@0 | 241 | prevWS = true; |
michael@0 | 242 | } else { |
michael@0 | 243 | ++len; |
michael@0 | 244 | prevWS = false; |
michael@0 | 245 | } |
michael@0 | 246 | } |
michael@0 | 247 | } |
michael@0 | 248 | return len; |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | bool nsSkipCharsRunIterator::NextRun() { |
michael@0 | 252 | do { |
michael@0 | 253 | if (mRunLength) { |
michael@0 | 254 | mIterator.AdvanceOriginal(mRunLength); |
michael@0 | 255 | NS_ASSERTION(mRunLength > 0, "No characters in run (initial length too large?)"); |
michael@0 | 256 | if (!mSkipped || mLengthIncludesSkipped) { |
michael@0 | 257 | mRemainingLength -= mRunLength; |
michael@0 | 258 | } |
michael@0 | 259 | } |
michael@0 | 260 | if (!mRemainingLength) |
michael@0 | 261 | return false; |
michael@0 | 262 | int32_t length; |
michael@0 | 263 | mSkipped = mIterator.IsOriginalCharSkipped(&length); |
michael@0 | 264 | mRunLength = std::min(length, mRemainingLength); |
michael@0 | 265 | } while (!mVisitSkipped && mSkipped); |
michael@0 | 266 | |
michael@0 | 267 | return true; |
michael@0 | 268 | } |