StringMetrics.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (c) 2020-2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Types.h>
  8. #include <AK/Vector.h>
  9. namespace Line {
  10. struct StringMetrics {
  11. struct MaskedChar {
  12. size_t position { 0 };
  13. size_t original_length { 0 };
  14. size_t masked_length { 0 };
  15. };
  16. struct LineMetrics {
  17. Vector<MaskedChar> masked_chars;
  18. size_t length { 0 };
  19. size_t total_length(ssize_t offset = -1) const
  20. {
  21. size_t length = this->length;
  22. for (auto& mask : masked_chars) {
  23. if (offset < 0 || mask.position <= (size_t)offset) {
  24. length -= mask.original_length;
  25. length += mask.masked_length;
  26. }
  27. }
  28. return length;
  29. }
  30. };
  31. Vector<LineMetrics> line_metrics;
  32. size_t total_length { 0 };
  33. size_t max_line_length { 0 };
  34. size_t lines_with_addition(const StringMetrics& offset, size_t column_width) const;
  35. size_t offset_with_addition(const StringMetrics& offset, size_t column_width) const;
  36. void reset()
  37. {
  38. line_metrics.clear();
  39. total_length = 0;
  40. max_line_length = 0;
  41. line_metrics.append({ {}, 0 });
  42. }
  43. };
  44. }