intl/lwbrk/src/nsPangoBreaker.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/lwbrk/src/nsPangoBreaker.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,60 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsComplexBreaker.h"
    1.10 +
    1.11 +#include <pango/pango-break.h>
    1.12 +#include "nsUTF8Utils.h"
    1.13 +#include "nsString.h"
    1.14 +#include "nsTArray.h"
    1.15 +
    1.16 +void
    1.17 +NS_GetComplexLineBreaks(const char16_t* aText, uint32_t aLength,
    1.18 +                        uint8_t* aBreakBefore)
    1.19 +{
    1.20 +  NS_ASSERTION(aText, "aText shouldn't be null");
    1.21 +
    1.22 +  memset(aBreakBefore, false, aLength * sizeof(uint8_t));
    1.23 +
    1.24 +  nsAutoTArray<PangoLogAttr, 2000> attrBuffer;
    1.25 +  if (!attrBuffer.AppendElements(aLength + 1))
    1.26 +    return;
    1.27 +
    1.28 +  NS_ConvertUTF16toUTF8 aUTF8(aText, aLength);
    1.29 +
    1.30 +  const gchar* p = aUTF8.Data();
    1.31 +  const gchar* end = p + aUTF8.Length();
    1.32 +  uint32_t     u16Offset = 0;
    1.33 +
    1.34 +  static PangoLanguage* language = pango_language_from_string("en");
    1.35 +
    1.36 +  while (p < end)
    1.37 +  {
    1.38 +    PangoLogAttr* attr = attrBuffer.Elements();
    1.39 +    pango_get_log_attrs(p, end - p, -1, language, attr, attrBuffer.Length());
    1.40 +
    1.41 +    while (p < end)
    1.42 +    {
    1.43 +      aBreakBefore[u16Offset] = attr->is_line_break;
    1.44 +      if (NS_IS_LOW_SURROGATE(aText[u16Offset]))
    1.45 +        aBreakBefore[++u16Offset] = false; // Skip high surrogate
    1.46 +      ++u16Offset;
    1.47 +
    1.48 +      bool err;
    1.49 +      uint32_t ch = UTF8CharEnumerator::NextChar(&p, end, &err);
    1.50 +      ++attr;
    1.51 +
    1.52 +      if (ch == 0 || err) {
    1.53 +        // pango_break (pango 1.16.2) only analyses text before the
    1.54 +        // first NUL (but sets one extra attr). Workaround loop to call
    1.55 +        // pango_break again to analyse after the NUL is done somewhere else
    1.56 +        // (gfx/thebes/gfxPangoFonts.cpp: SetupClusterBoundaries()).
    1.57 +        // So, we do the same here for pango_get_log_attrs.
    1.58 +        break;
    1.59 +      }
    1.60 +    }
    1.61 +  }
    1.62 +}
    1.63 +

mercurial