Bug 1216427 - part 2 - Ensure mouse selection does not split up a Regional Indicator flag symbol. r=emk
authorJonathan Kew <jkew@mozilla.com>
Mon, 26 Oct 2015 10:47:18 +0000
changeset 304665 9a59037687802067d469167e5eafe5cdf3ce1ecb
parent 304664 019608d8a449322f00784012d18be0bad0b61291
child 304666 ffef339aaa4fc55cc315e9cff3216e45125916ea
push id1001
push userraliiev@mozilla.com
push dateMon, 18 Jan 2016 19:06:03 +0000
treeherdermozilla-release@8b89261f3ac4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersemk
bugs1216427
milestone44.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1216427 - part 2 - Ensure mouse selection does not split up a Regional Indicator flag symbol. r=emk
layout/generic/nsTextFrame.cpp
--- a/layout/generic/nsTextFrame.cpp
+++ b/layout/generic/nsTextFrame.cpp
@@ -6653,20 +6653,43 @@ nsTextFrame::GetCharacterOffsetAtFramePo
 
   int32_t selectedOffset;
   if (charsFit < skippedLength) {
     // charsFit characters fitted, but no more could fit. See if we're
     // more than halfway through the cluster.. If we are, choose the next
     // cluster.
     gfxSkipCharsIterator extraCluster(provider.GetStart());
     extraCluster.AdvanceSkipped(charsFit);
+
+    bool allowSplitLigature = true; // Allow selection of partial ligature...
+
+    // ...but don't let selection/insertion-point split two Regional Indicator
+    // chars that are ligated in the textrun to form a single flag symbol.
+    uint32_t offs = extraCluster.GetOriginalOffset();
+    const nsTextFragment* frag = GetContent()->GetText();
+    if (offs + 1 < frag->GetLength() &&
+        NS_IS_HIGH_SURROGATE(frag->CharAt(offs)) &&
+        NS_IS_LOW_SURROGATE(frag->CharAt(offs + 1)) &&
+        gfxFontUtils::IsRegionalIndicator
+          (SURROGATE_TO_UCS4(frag->CharAt(offs), frag->CharAt(offs + 1)))) {
+      allowSplitLigature = false;
+      if (extraCluster.GetSkippedOffset() > 1 &&
+          !mTextRun->IsLigatureGroupStart(extraCluster.GetSkippedOffset())) {
+        // CountCharsFit() left us in the middle of the flag; back up over the
+        // first character of the ligature, and adjust fitWidth accordingly.
+        extraCluster.AdvanceSkipped(-2); // it's a surrogate pair: 2 code units
+        fitWidth -= mTextRun->GetAdvanceWidth(extraCluster.GetSkippedOffset(),
+                                              2, &provider);
+      }
+    }
+
     gfxSkipCharsIterator extraClusterLastChar(extraCluster);
     FindClusterEnd(mTextRun,
                    provider.GetStart().GetOriginalOffset() + provider.GetOriginalLength(),
-                   &extraClusterLastChar);
+                   &extraClusterLastChar, allowSplitLigature);
     PropertyProvider::Spacing spacing;
     gfxFloat charWidth =
         mTextRun->GetAdvanceWidth(extraCluster.GetSkippedOffset(),
                                   GetSkippedDistance(extraCluster, extraClusterLastChar) + 1,
                                   &provider, &spacing);
     charWidth -= spacing.mBefore + spacing.mAfter;
     selectedOffset = !aForInsertionPoint ||
       width <= fitWidth + spacing.mBefore + charWidth/2