text_word.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include "base/basic_types.h"
  9. #include <private/qfixed_p.h>
  10. namespace Ui::Text {
  11. class Word final {
  12. public:
  13. Word() = default;
  14. Word( // !newline
  15. uint16 position,
  16. bool unfinished,
  17. QFixed width,
  18. QFixed rbearing)
  19. : _position(position)
  20. , _rbearing_modulus(std::min(std::abs(rbearing.value()), 0x7FFF))
  21. , _rbearing_positive(rbearing.value() > 0 ? 1 : 0)
  22. , _unfinished(unfinished ? 1 : 0)
  23. , _qfixedwidth(width.value()) {
  24. }
  25. Word(uint16 position, int newlineBlockIndex)
  26. : _position(position)
  27. , _newline(1)
  28. , _newlineBlockIndex(newlineBlockIndex) {
  29. }
  30. [[nodiscard]] bool newline() const {
  31. return _newline != 0;
  32. }
  33. [[nodiscard]] int newlineBlockIndex() const {
  34. return _newline ? _newlineBlockIndex : 0;
  35. }
  36. [[nodiscard]] bool unfinished() const {
  37. return _unfinished != 0;
  38. }
  39. [[nodiscard]] uint16 position() const {
  40. return _position;
  41. }
  42. [[nodiscard]] QFixed f_rbearing() const {
  43. return QFixed::fromFixed(
  44. int(_rbearing_modulus) * (_rbearing_positive ? 1 : -1));
  45. }
  46. [[nodiscard]] QFixed f_width() const {
  47. return _newline ? 0 : QFixed::fromFixed(_qfixedwidth);
  48. }
  49. [[nodiscard]] QFixed f_rpadding() const {
  50. return _rpadding;
  51. }
  52. void add_rpadding(QFixed padding) {
  53. _rpadding += padding;
  54. }
  55. private:
  56. uint16 _position = 0;
  57. uint16 _rbearing_modulus : 13 = 0;
  58. uint16 _rbearing_positive : 1 = 0;
  59. uint16 _unfinished : 1 = 0;
  60. uint16 _newline : 1 = 0;
  61. // Right padding: spaces after the last content of the block (like a word).
  62. // This holds spaces after the end of the block, for example a text ending
  63. // with a space before a link has started. If text block has a leading spaces
  64. // (for example a text block after a link block) it is prepended with an empty
  65. // word that holds those spaces as a right padding.
  66. QFixed _rpadding;
  67. union {
  68. int _qfixedwidth;
  69. int _newlineBlockIndex;
  70. };
  71. };
  72. using Words = std::vector<Word>;
  73. [[nodiscard]] inline uint16 CountPosition(Words::const_iterator i) {
  74. return i->position();
  75. }
  76. } // namespace Ui::Text