Bug 1316482 - use the refactored TransformText as a template function for both char16_t and uint8_t text. draft
authorJeremy Chen <jeremychen@mozilla.com>
Thu, 12 Jan 2017 00:39:47 +0800
changeset 459265 ae055d1a7a52efe6a6a85c4dec94f4fc22488472
parent 459264 c3db30c19b5d37e394f3a07aabb8fd8204df72cf
child 459266 ac44a736e435fc0d885d9756f3717904b0e136d6
push id41162
push userjichen@mozilla.com
push dateWed, 11 Jan 2017 16:40:21 +0000
bugs1316482
milestone53.0a1
Bug 1316482 - use the refactored TransformText as a template function for both char16_t and uint8_t text. With this patch, we shall only maintain one version of the TransformText logic. MozReview-Commit-ID: JAIksFVqvqf
layout/generic/nsTextFrameUtils.cpp
layout/generic/nsTextFrameUtils.h
--- a/layout/generic/nsTextFrameUtils.cpp
+++ b/layout/generic/nsTextFrameUtils.cpp
@@ -51,34 +51,39 @@ IsSpaceOrTab(char16_t aCh)
 }
 
 static bool
 IsSpaceOrTabOrSegmentBreak(char16_t aCh)
 {
   return IsSpaceOrTab(aCh) || IsSegmentBreak(aCh);
 }
 
-static char16_t*
-TransformWhiteSpaces(const char16_t* aText, uint32_t aLength,
+template<class CharT>
+static CharT*
+TransformWhiteSpaces(const CharT* aText, uint32_t aLength,
                      uint32_t aBegin, uint32_t aEnd,
                      bool aHasSegmentBreak,
                      bool& aInWhitespace,
-                     char16_t* aOutput,
+                     CharT* aOutput,
                      uint32_t& aFlags,
                      nsTextFrameUtils::CompressionMode aCompression,
                      gfxSkipChars* aSkipChars)
 {
   MOZ_ASSERT(aCompression == nsTextFrameUtils::COMPRESS_WHITESPACE ||
              aCompression == nsTextFrameUtils::COMPRESS_WHITESPACE_NEWLINE,
              "whitespaces should be skippable!!");
   // Get the context preceding/following this white space range.
+  // For 8-bit text (sizeof CharT == 1), the checks here should get optimized
+  // out, and isSegmentBreakSkippable should be initialized to be 'false'.
   bool isSegmentBreakSkippable =
-    (aBegin > 0 && IS_ZERO_WIDTH_SPACE(aText[aBegin - 1])) ||
-    (aEnd < aLength && IS_ZERO_WIDTH_SPACE(aText[aEnd]));
-  if (!isSegmentBreakSkippable && aBegin > 0 && aEnd < aLength) {
+    sizeof(CharT) > 1 &&
+    ((aBegin > 0 && IS_ZERO_WIDTH_SPACE(aText[aBegin - 1])) ||
+     (aEnd < aLength && IS_ZERO_WIDTH_SPACE(aText[aEnd])));
+  if (sizeof(CharT) > 1 && !isSegmentBreakSkippable &&
+      aBegin > 0 && aEnd < aLength) {
     uint32_t ucs4before;
     uint32_t ucs4after;
     if (aBegin > 1 &&
         NS_IS_LOW_SURROGATE(aText[aBegin - 1]) &&
         NS_IS_HIGH_SURROGATE(aText[aBegin - 2])) {
       ucs4before = SURROGATE_TO_UCS4(aText[aBegin - 2], aText[aBegin - 1]);
     } else {
       ucs4before = aText[aBegin - 1];
@@ -92,17 +97,17 @@ TransformWhiteSpaces(const char16_t* aTe
     }
     // Discard newlines between characters that have F, W, or H
     // EastAsianWidth property and neither side is Hangul.
     isSegmentBreakSkippable = IsSegmentBreakSkipChar(ucs4before) &&
                               IsSegmentBreakSkipChar(ucs4after);
   }
 
   for (uint32_t i = aBegin; i < aEnd; ++i) {
-    char16_t ch = aText[i];
+    CharT ch = aText[i];
     bool keepChar = false;
     bool keepTransformedWhiteSpace = false;
     if (IsDiscardable(ch, &aFlags)) {
       aSkipChars->SkipChar();
       continue;
     }
     if (IsSpaceOrTab(ch)) {
       if (aHasSegmentBreak) {
@@ -162,34 +167,35 @@ TransformWhiteSpaces(const char16_t* aTe
       aInWhitespace = true;
     } else {
       MOZ_ASSERT_UNREACHABLE("Should've skipped the character!!");
     }
   }
   return aOutput;
 }
 
-char16_t*
-nsTextFrameUtils::TransformText(const char16_t* aText, uint32_t aLength,
-                                char16_t* aOutput,
+template<class CharT>
+CharT*
+nsTextFrameUtils::TransformText(const CharT* aText, uint32_t aLength,
+                                CharT* aOutput,
                                 CompressionMode aCompression,
                                 uint8_t* aIncomingFlags,
                                 gfxSkipChars* aSkipChars,
                                 uint32_t* aAnalysisFlags)
 {
   uint32_t flags = 0;
-  char16_t* outputStart = aOutput;
+  CharT* outputStart = aOutput;
 
   bool lastCharArabic = false;
   if (aCompression == COMPRESS_NONE ||
       aCompression == COMPRESS_NONE_TRANSFORM_TO_SPACE) {
     // Skip discardables.
     uint32_t i;
     for (i = 0; i < aLength; ++i) {
-      char16_t ch = aText[i];
+      CharT ch = aText[i];
       if (IsDiscardable(ch, &flags)) {
         aSkipChars->SkipChar();
       } else {
         aSkipChars->KeepChar();
         if (ch > ' ') {
           lastCharArabic = IS_ARABIC_CHAR(ch);
         } else if (aCompression == COMPRESS_NONE_TRANSFORM_TO_SPACE) {
           if (ch == '\t' || ch == '\n') {
@@ -210,17 +216,17 @@ nsTextFrameUtils::TransformText(const ch
     } else {
       *aIncomingFlags &= ~INCOMING_ARABICCHAR;
     }
     *aIncomingFlags &= ~INCOMING_WHITESPACE;
   } else {
     bool inWhitespace = (*aIncomingFlags & INCOMING_WHITESPACE) != 0;
     uint32_t i;
     for (i = 0; i < aLength; ++i) {
-      char16_t ch = aText[i];
+      CharT ch = aText[i];
       // CSS Text 3 - 4.1. The White Space Processing Rules
       // White space processing in CSS affects only the document white space
       // characters: spaces (U+0020), tabs (U+0009), and segment breaks.
       // Since we need the context of segment breaks and their surrounding
       // white spaces to proceed the white space processing, a consecutive run
       // of spaces/tabs/segment breaks is collected in a first pass loop, then
       // we apply the collapsing and transformation rules to this run in a
       // second pass loop.
@@ -239,17 +245,17 @@ nsTextFrameUtils::TransformText(const ch
         }
         // Exclude trailing discardables before checking space combining
         // sequence tail.
         for (; IsDiscardable(aText[j - 1], &flags); j--) {
           countTrailingDiscardables++;
         }
         // If the last white space is followed by a combining sequence tail,
         // exclude it from the range of TransformWhiteSpaces.
-        if (aText[j - 1] == ' ' && j < aLength &&
+        if (sizeof(CharT) > 1 && aText[j - 1] == ' ' && j < aLength &&
             IsSpaceCombiningSequenceTail(&aText[j], aLength - j)) {
           keepLastSpace = true;
           j--;
         }
         if (j > i) {
           aOutput = TransformWhiteSpaces(aText, aLength, i, j, hasSegmentBreak,
                                          inWhitespace, aOutput, flags,
                                          aCompression, aSkipChars);
@@ -294,95 +300,38 @@ nsTextFrameUtils::TransformText(const ch
   }
 
   if (outputStart + aLength != aOutput) {
     flags |= TEXT_WAS_TRANSFORMED;
   }
   *aAnalysisFlags = flags;
   return aOutput;
 }
-
-uint8_t*
+/*
+ * NOTE: This template is part of public API of nsTextFrameUtils, while
+ * its function body is not available in the header. It may stop working
+ * (fail to resolve symbol in link time) once its callsites are moved to a
+ * different translation unit (e.g. a different unified source file).
+ * Explicit instantiating this function template with `uint8_t` and `char16_t`
+ * could prevent us from the potential risk.
+ */
+template uint8_t*
 nsTextFrameUtils::TransformText(const uint8_t* aText, uint32_t aLength,
                                 uint8_t* aOutput,
                                 CompressionMode aCompression,
                                 uint8_t* aIncomingFlags,
                                 gfxSkipChars* aSkipChars,
-                                uint32_t* aAnalysisFlags)
-{
-  uint32_t flags = 0;
-  uint8_t* outputStart = aOutput;
-
-  if (aCompression == COMPRESS_NONE ||
-      aCompression == COMPRESS_NONE_TRANSFORM_TO_SPACE) {
-    // Skip discardables.
-    uint32_t i;
-    for (i = 0; i < aLength; ++i) {
-      uint8_t ch = aText[i];
-      if (IsDiscardable(ch, &flags)) {
-        aSkipChars->SkipChar();
-      } else {
-        aSkipChars->KeepChar();
-        if (aCompression == COMPRESS_NONE_TRANSFORM_TO_SPACE) {
-          if (ch == '\t' || ch == '\n') {
-            ch = ' ';
-            flags |= TEXT_WAS_TRANSFORMED;
-          }
-        } else {
-          // aCompression == COMPRESS_NONE
-          if (ch == '\t') {
-            flags |= TEXT_HAS_TAB;
-          }
-        }
-        *aOutput++ = ch;
-      }
-    }
-    *aIncomingFlags &= ~(INCOMING_ARABICCHAR | INCOMING_WHITESPACE);
-  } else {
-    bool inWhitespace = (*aIncomingFlags & INCOMING_WHITESPACE) != 0;
-    uint32_t i;
-    for (i = 0; i < aLength; ++i) {
-      uint8_t ch = aText[i];
-      bool nowInWhitespace = ch == ' ' || ch == '\t' ||
-        (ch == '\n' && aCompression == COMPRESS_WHITESPACE_NEWLINE);
-      if (!nowInWhitespace) {
-        if (IsDiscardable(ch, &flags)) {
-          aSkipChars->SkipChar();
-          nowInWhitespace = inWhitespace;
-        } else {
-          *aOutput++ = ch;
-          aSkipChars->KeepChar();
-        }
-      } else {
-        if (inWhitespace) {
-          aSkipChars->SkipChar();
-        } else {
-          if (ch != ' ') {
-            flags |= TEXT_WAS_TRANSFORMED;
-          }
-          *aOutput++ = ' ';
-          aSkipChars->KeepChar();
-        }
-      }
-      inWhitespace = nowInWhitespace;
-    }
-    *aIncomingFlags &= ~INCOMING_ARABICCHAR;
-    if (inWhitespace) {
-      *aIncomingFlags |= INCOMING_WHITESPACE;
-    } else {
-      *aIncomingFlags &= ~INCOMING_WHITESPACE;
-    }
-  }
-
-  if (outputStart + aLength != aOutput) {
-    flags |= TEXT_WAS_TRANSFORMED;
-  }
-  *aAnalysisFlags = flags;
-  return aOutput;
-}
+                                uint32_t* aAnalysisFlags);
+template char16_t*
+nsTextFrameUtils::TransformText(const char16_t* aText, uint32_t aLength,
+                                char16_t* aOutput,
+                                CompressionMode aCompression,
+                                uint8_t* aIncomingFlags,
+                                gfxSkipChars* aSkipChars,
+                                uint32_t* aAnalysisFlags);
 
 uint32_t
 nsTextFrameUtils::ComputeApproximateLengthWithWhitespaceCompression(
                     nsIContent *aContent, const nsStyleText *aStyleText)
 {
   const nsTextFragment *frag = aContent->GetText();
   // This is an approximation so we don't really need anything
   // too fancy here.
--- a/layout/generic/nsTextFrameUtils.h
+++ b/layout/generic/nsTextFrameUtils.h
@@ -82,16 +82,20 @@ public:
   IsSpaceCombiningSequenceTail(const char16_t* aChars, int32_t aLength) {
     return aLength > 0 &&
       (mozilla::unicode::IsClusterExtender(aChars[0]) ||
        (IsBidiControl(aChars[0]) &&
         IsSpaceCombiningSequenceTail(aChars + 1, aLength - 1)
        )
       );
   }
+  static bool
+  IsSpaceCombiningSequenceTail(const uint8_t* aChars, int32_t aLength) {
+    return false;
+  }
 
   enum CompressionMode {
     COMPRESS_NONE,
     COMPRESS_WHITESPACE,
     COMPRESS_WHITESPACE_NEWLINE,
     COMPRESS_NONE_TRANSFORM_TO_SPACE
   };
 
@@ -104,29 +108,23 @@ public:
    * @param aCompression control what is compressed to a
    * single space character: no compression, compress spaces (not followed
    * by combining mark) and tabs, compress those plus newlines, or
    * no compression except newlines are discarded.
    * @param aIncomingFlags a flag indicating whether there was whitespace
    * or an Arabic character preceding this text. We set it to indicate if
    * there's an Arabic character or whitespace preceding the end of this text.
    */
-  static char16_t* TransformText(const char16_t* aText, uint32_t aLength,
-                                 char16_t* aOutput,
-                                 CompressionMode aCompression,
-                                 uint8_t* aIncomingFlags,
-                                 gfxSkipChars* aSkipChars,
-                                 uint32_t* aAnalysisFlags);
-
-  static uint8_t* TransformText(const uint8_t* aText, uint32_t aLength,
-                                uint8_t* aOutput,
-                                CompressionMode aCompression,
-                                uint8_t* aIncomingFlags,
-                                gfxSkipChars* aSkipChars,
-                                uint32_t* aAnalysisFlags);
+  template<class CharT>
+  static CharT* TransformText(const CharT* aText, uint32_t aLength,
+                              CharT* aOutput,
+                              CompressionMode aCompression,
+                              uint8_t* aIncomingFlags,
+                              gfxSkipChars* aSkipChars,
+                              uint32_t* aAnalysisFlags);
 
   static void
   AppendLineBreakOffset(nsTArray<uint32_t>* aArray, uint32_t aOffset)
   {
     if (aArray->Length() > 0 && (*aArray)[aArray->Length() - 1] == aOffset)
       return;
     aArray->AppendElement(aOffset);
   }