nsPangoBreaker.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsComplexBreaker.h"
  6. #include <pango/pango-break.h>
  7. #include "nsUTF8Utils.h"
  8. #include "nsString.h"
  9. #include "nsTArray.h"
  10. void
  11. NS_GetComplexLineBreaks(const char16_t* aText, uint32_t aLength,
  12. uint8_t* aBreakBefore)
  13. {
  14. NS_ASSERTION(aText, "aText shouldn't be null");
  15. memset(aBreakBefore, false, aLength * sizeof(uint8_t));
  16. AutoTArray<PangoLogAttr, 2000> attrBuffer;
  17. if (!attrBuffer.AppendElements(aLength + 1))
  18. return;
  19. NS_ConvertUTF16toUTF8 aUTF8(aText, aLength);
  20. const gchar* p = aUTF8.Data();
  21. const gchar* end = p + aUTF8.Length();
  22. uint32_t u16Offset = 0;
  23. static PangoLanguage* language = pango_language_from_string("en");
  24. while (p < end)
  25. {
  26. PangoLogAttr* attr = attrBuffer.Elements();
  27. pango_get_log_attrs(p, end - p, -1, language, attr, attrBuffer.Length());
  28. while (p < end)
  29. {
  30. aBreakBefore[u16Offset] = attr->is_line_break;
  31. if (NS_IS_LOW_SURROGATE(aText[u16Offset]))
  32. aBreakBefore[++u16Offset] = false; // Skip high surrogate
  33. ++u16Offset;
  34. bool err;
  35. uint32_t ch = UTF8CharEnumerator::NextChar(&p, end, &err);
  36. ++attr;
  37. if (ch == 0 || err) {
  38. // pango_break (pango 1.16.2) only analyses text before the
  39. // first NUL (but sets one extra attr). Workaround loop to call
  40. // pango_break again to analyse after the NUL is done somewhere else
  41. // (gfx/thebes/gfxFontconfigFonts.cpp: SetupClusterBoundaries()).
  42. // So, we do the same here for pango_get_log_attrs.
  43. break;
  44. }
  45. }
  46. }
  47. }