aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-09-09 17:55:40 +0800
committercrupest <crupest@outlook.com>2021-09-09 17:55:40 +0800
commit74aee8a6308445604b513ab37812ddc99365aa98 (patch)
tree4a0886df82af6baa248e62b97fa585e09be9470d /include
parent700751108257a00ab1a6134fe0ca570acb3269a8 (diff)
downloadcru-74aee8a6308445604b513ab37812ddc99365aa98.tar.gz
cru-74aee8a6308445604b513ab37812ddc99365aa98.tar.bz2
cru-74aee8a6308445604b513ab37812ddc99365aa98.zip
...
Diffstat (limited to 'include')
-rw-r--r--include/cru/common/Exception.hpp5
-rw-r--r--include/cru/common/Range.hpp43
-rw-r--r--include/cru/common/String.hpp14
-rw-r--r--include/cru/common/StringUtil.hpp6
-rw-r--r--include/cru/osx/graphics/quartz/Convert.hpp5
-rw-r--r--include/cru/osx/graphics/quartz/TextLayout.hpp4
-rw-r--r--include/cru/platform/GraphBase.hpp40
7 files changed, 72 insertions, 45 deletions
diff --git a/include/cru/common/Exception.hpp b/include/cru/common/Exception.hpp
index 861bf5e9..8864f4df 100644
--- a/include/cru/common/Exception.hpp
+++ b/include/cru/common/Exception.hpp
@@ -18,4 +18,9 @@ class CRU_BASE_API Exception {
private:
String message_;
};
+
+class CRU_BASE_API TextEncodeException : public Exception {
+ public:
+ using Exception::Exception;
+};
} // namespace cru
diff --git a/include/cru/common/Range.hpp b/include/cru/common/Range.hpp
new file mode 100644
index 00000000..ecc61243
--- /dev/null
+++ b/include/cru/common/Range.hpp
@@ -0,0 +1,43 @@
+#pragma once
+#include "Base.hpp"
+
+namespace cru {
+struct Range final {
+ constexpr static Range FromTwoSides(gsl::index start, gsl::index end) {
+ return Range(start, end - start);
+ }
+
+ constexpr static Range FromTwoSides(gsl::index start, gsl::index end,
+ gsl::index offset) {
+ return Range(start + offset, end - start);
+ }
+
+ constexpr Range() = default;
+ constexpr Range(const gsl::index position, const gsl::index count = 0)
+ : position(position), count(count) {}
+
+ gsl::index GetStart() const { return position; }
+ gsl::index GetEnd() const { return position + count; }
+
+ void ChangeEnd(gsl::index new_end) { count = new_end - position; }
+
+ Range Normalize() const {
+ auto result = *this;
+ if (result.count < 0) {
+ result.position += result.count;
+ result.count = -result.count;
+ }
+ return result;
+ }
+
+ Range CoerceInto(gsl::index min, gsl::index max) const {
+ auto coerce = [min, max](gsl::index index) {
+ return index > max ? max : (index < min ? min : index);
+ };
+ return Range::FromTwoSides(coerce(GetStart()), coerce(GetEnd()));
+ }
+
+ gsl::index position = 0;
+ gsl::index count = 0;
+};
+} // namespace cru
diff --git a/include/cru/common/String.hpp b/include/cru/common/String.hpp
index 563e1606..4211a160 100644
--- a/include/cru/common/String.hpp
+++ b/include/cru/common/String.hpp
@@ -1,7 +1,9 @@
#pragma once
-
#include "Base.hpp"
+#include "StringUtil.hpp"
+#include "Range.hpp"
+
#include <cstdint>
#include <iterator>
@@ -127,6 +129,16 @@ class CRU_BASE_API String {
}
public:
+ Utf16CodePointIterator CodePointIterator() const {
+ return Utf16CodePointIterator(
+ std::u16string_view(reinterpret_cast<char16_t*>(buffer_), size_));
+ }
+
+ Index IndexFromCodeUnitToCodePoint(Index code_unit_index) const;
+ Index IndexFromCodePointToCodeUnit(Index code_point_index) const;
+ Range RangeFromCodeUnitToCodePoint(Range code_unit_range) const;
+ Range RangeFromCodePointToCodeUnit(Range code_point_range) const;
+
const char16_t* Char16CStr() const {
return reinterpret_cast<const char16_t*>(c_str());
}
diff --git a/include/cru/common/StringUtil.hpp b/include/cru/common/StringUtil.hpp
index 985f0032..5c230898 100644
--- a/include/cru/common/StringUtil.hpp
+++ b/include/cru/common/StringUtil.hpp
@@ -1,6 +1,5 @@
#pragma once
#include "Base.hpp"
-#include "Exception.hpp"
#include <functional>
#include <string>
@@ -10,11 +9,6 @@ namespace cru {
using CodePoint = std::int32_t;
constexpr CodePoint k_invalid_code_point = -1;
-class CRU_BASE_API TextEncodeException : public Exception {
- public:
- using Exception::Exception;
-};
-
inline bool IsUtf16SurrogatePairCodeUnit(char16_t c) {
return c >= 0xD800 && c <= 0xDFFF;
}
diff --git a/include/cru/osx/graphics/quartz/Convert.hpp b/include/cru/osx/graphics/quartz/Convert.hpp
index f217e549..f85fc20e 100644
--- a/include/cru/osx/graphics/quartz/Convert.hpp
+++ b/include/cru/osx/graphics/quartz/Convert.hpp
@@ -1,5 +1,7 @@
#pragma once
#include "cru/platform/Matrix.hpp"
+#include "cru/common/String.hpp"
+#include "cru/common/Range.hpp"
#include <CoreGraphics/CoreGraphics.h>
@@ -12,4 +14,7 @@ Matrix Convert(const CGAffineTransform& matrix);
CGRect Convert(const Rect& rect);
Rect Convert(const CGRect& rect);
+
+CFRange Convert(const Range& range);
+Range Convert(const CFRange& range);
} // namespace cru::platform::graphics::osx::quartz
diff --git a/include/cru/osx/graphics/quartz/TextLayout.hpp b/include/cru/osx/graphics/quartz/TextLayout.hpp
index 4a3830db..36d5d25b 100644
--- a/include/cru/osx/graphics/quartz/TextLayout.hpp
+++ b/include/cru/osx/graphics/quartz/TextLayout.hpp
@@ -47,5 +47,9 @@ private:
CTFramesetterRef ct_framesetter_;
CTFrameRef ct_frame_;
+ int line_count_;
+ std::vector<CGPoint> line_origins_;
+ std::vector<CTLineRef> lines_;
+ std::vector<std::vector<float>> line_caret_offsets_;
};
} // namespace cru::platform::graphics::osx::quartz
diff --git a/include/cru/platform/GraphBase.hpp b/include/cru/platform/GraphBase.hpp
index c4dd19f6..cc0ffcc7 100644
--- a/include/cru/platform/GraphBase.hpp
+++ b/include/cru/platform/GraphBase.hpp
@@ -3,6 +3,7 @@
#include "Color.hpp"
#include "cru/common/Format.hpp"
+#include "cru/common/Range.hpp"
#include <fmt/core.h>
#include <cstdint>
@@ -266,42 +267,5 @@ constexpr bool operator!=(const Ellipse& left, const Ellipse& right) {
return !(left == right);
}
-struct TextRange final {
- constexpr static TextRange FromTwoSides(gsl::index start, gsl::index end) {
- return TextRange(start, end - start);
- }
-
- constexpr static TextRange FromTwoSides(gsl::index start, gsl::index end,
- gsl::index offset) {
- return TextRange(start + offset, end - start);
- }
-
- constexpr TextRange() = default;
- constexpr TextRange(const gsl::index position, const gsl::index count = 0)
- : position(position), count(count) {}
-
- gsl::index GetStart() const { return position; }
- gsl::index GetEnd() const { return position + count; }
-
- void ChangeEnd(gsl::index new_end) { count = new_end - position; }
-
- TextRange Normalize() const {
- auto result = *this;
- if (result.count < 0) {
- result.position += result.count;
- result.count = -result.count;
- }
- return result;
- }
-
- TextRange CoerceInto(gsl::index min, gsl::index max) const {
- auto coerce = [min, max](gsl::index index) {
- return index > max ? max : (index < min ? min : index);
- };
- return TextRange::FromTwoSides(coerce(GetStart()), coerce(GetEnd()));
- }
-
- gsl::index position = 0;
- gsl::index count = 0;
-};
+using TextRange = Range;
} // namespace cru::platform