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.

104 lines
3.2KB

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