The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. namespace AccessibilityTextHelpers
  21. {
  22. enum class BoundaryType
  23. {
  24. character,
  25. word,
  26. line,
  27. document
  28. };
  29. enum class Direction
  30. {
  31. forwards,
  32. backwards
  33. };
  34. static int findTextBoundary (const AccessibilityTextInterface& textInterface,
  35. int currentPosition,
  36. BoundaryType boundary,
  37. Direction direction)
  38. {
  39. const auto numCharacters = textInterface.getTotalNumCharacters();
  40. const auto isForwards = (direction == Direction::forwards);
  41. const auto offsetWithDirection = [isForwards] (auto num) { return isForwards ? num : -num; };
  42. switch (boundary)
  43. {
  44. case BoundaryType::character:
  45. return jlimit (0, numCharacters, currentPosition + offsetWithDirection (1));
  46. case BoundaryType::word:
  47. case BoundaryType::line:
  48. {
  49. const auto text = [&]() -> String
  50. {
  51. if (isForwards)
  52. return textInterface.getText ({ currentPosition, textInterface.getTotalNumCharacters() });
  53. const auto str = textInterface.getText ({ 0, currentPosition });
  54. auto start = str.getCharPointer();
  55. auto end = start.findTerminatingNull();
  56. const auto size = getAddressDifference (end.getAddress(), start.getAddress());
  57. String reversed;
  58. if (size > 0)
  59. {
  60. reversed.preallocateBytes ((size_t) size);
  61. auto destPtr = reversed.getCharPointer();
  62. for (;;)
  63. {
  64. destPtr.write (*--end);
  65. if (end == start)
  66. break;
  67. }
  68. destPtr.writeNull();
  69. }
  70. return reversed;
  71. }();
  72. auto tokens = (boundary == BoundaryType::line ? StringArray::fromLines (text)
  73. : StringArray::fromTokens (text, false));
  74. return currentPosition + offsetWithDirection (tokens[0].length());
  75. }
  76. case BoundaryType::document:
  77. return isForwards ? numCharacters : 0;
  78. }
  79. jassertfalse;
  80. return -1;
  81. }
  82. }
  83. } // namespace juce