aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/cru/platform/GraphicsBase.h37
-rw-r--r--src/platform/graphics/cairo/PangoTextLayout.cpp38
2 files changed, 34 insertions, 41 deletions
diff --git a/include/cru/platform/GraphicsBase.h b/include/cru/platform/GraphicsBase.h
index d5936476..79e539a3 100644
--- a/include/cru/platform/GraphicsBase.h
+++ b/include/cru/platform/GraphicsBase.h
@@ -40,12 +40,8 @@ constexpr bool operator==(const Point& left, const Point& right) {
return left.x == right.x && left.y == right.y;
}
-constexpr bool operator!=(const Point& left, const Point& right) {
- return !(left == right);
-}
-
inline std::string ToUtf8String(const Point& point) {
- return std::format("(x: {}, y: {})", point.x, point.y);
+ return std::format("Point(x: {}, y: {})", point.x, point.y);
}
inline String ToString(const Point& point) {
@@ -85,12 +81,8 @@ constexpr bool operator==(const Size& left, const Size& right) {
return left.width == right.width && left.height == right.height;
}
-constexpr bool operator!=(const Size& left, const Size& right) {
- return !(left == right);
-}
-
inline std::string ToUtf8String(const Size& size) {
- return std::format("(width: {}, height: {})", size.width, size.height);
+ return std::format("Size(width: {}, height: {})", size.width, size.height);
}
inline String ToString(const Size& size) {
@@ -149,10 +141,6 @@ constexpr bool operator==(const Thickness& left, const Thickness& right) {
left.right == right.right && left.bottom == right.bottom;
}
-constexpr bool operator!=(const Thickness& left, const Thickness& right) {
- return !(left == right);
-}
-
struct Rect final {
constexpr Rect() = default;
constexpr Rect(const float left, const float top, const float width,
@@ -253,8 +241,13 @@ constexpr bool operator==(const Rect& left, const Rect& right) {
left.width == right.width && left.height == right.height;
}
-constexpr bool operator!=(const Rect& left, const Rect& right) {
- return !(left == right);
+inline std::string ToUtf8String(const Rect& rect) {
+ return std::format("Rect(left: {}, top: {}, width: {}, height: {})",
+ rect.left, rect.top, rect.width, rect.height);
+}
+
+inline String ToString(const Rect& rect) {
+ return String::FromUtf8(ToUtf8String(rect));
}
struct RoundedRect final {
@@ -273,10 +266,6 @@ constexpr bool operator==(const RoundedRect& left, const RoundedRect& right) {
left.radius_y == right.radius_y;
}
-constexpr bool operator!=(const RoundedRect& left, const RoundedRect& right) {
- return !(left == right);
-}
-
struct Ellipse final {
constexpr Ellipse() = default;
constexpr Ellipse(const Point& center, const float radius_x,
@@ -301,10 +290,6 @@ constexpr bool operator==(const Ellipse& left, const Ellipse& right) {
left.radius_y == right.radius_y;
}
-constexpr bool operator!=(const Ellipse& left, const Ellipse& right) {
- return !(left == right);
-}
-
using TextRange = Range;
} // namespace cru::platform
@@ -315,3 +300,7 @@ struct std::formatter<cru::platform::Point, char>
template <>
struct std::formatter<cru::platform::Size, char>
: cru::ImplementFormatterByToUtf8String<cru::platform::Size> {};
+
+template <>
+struct std::formatter<cru::platform::Rect, char>
+ : cru::ImplementFormatterByToUtf8String<cru::platform::Rect> {};
diff --git a/src/platform/graphics/cairo/PangoTextLayout.cpp b/src/platform/graphics/cairo/PangoTextLayout.cpp
index 0ba7c806..d69ee273 100644
--- a/src/platform/graphics/cairo/PangoTextLayout.cpp
+++ b/src/platform/graphics/cairo/PangoTextLayout.cpp
@@ -103,7 +103,7 @@ Index PangoTextLayout::FromUtf16IndexToUtf8Index(Index index) {
Rect PangoTextLayout::GetTextBounds(bool includingTrailingSpace) {
PangoRectangle rectangle;
- pango_layout_get_extents(pango_layout_, &rectangle, nullptr);
+ pango_layout_get_extents(pango_layout_, nullptr, &rectangle);
return ConvertFromPango(
Rect(rectangle.x, rectangle.y, rectangle.width, rectangle.height));
}
@@ -112,6 +112,7 @@ std::vector<Rect> PangoTextLayout::TextRangeRect(const TextRange& text_range) {
auto tr = text_range.Normalize();
auto utf8_start_index = FromUtf16IndexToUtf8Index(tr.GetStart());
auto utf8_end_index = FromUtf16IndexToUtf8Index(tr.GetEnd());
+ PangoRectangle rectangle;
int start_line_index, end_line_index, start_x_pos, end_x_pos;
pango_layout_index_to_line_x(pango_layout_, utf8_start_index, false,
@@ -119,37 +120,40 @@ std::vector<Rect> PangoTextLayout::TextRangeRect(const TextRange& text_range) {
pango_layout_index_to_line_x(pango_layout_, utf8_end_index, false,
&end_line_index, &end_x_pos);
+ pango_layout_index_to_pos(pango_layout_, utf8_start_index, &rectangle);
+ auto top = rectangle.y;
+
if (start_line_index == end_line_index) {
auto line = pango_layout_get_line(pango_layout_, start_line_index);
- PangoRectangle rectangle;
- pango_layout_line_get_extents(line, &rectangle, nullptr);
- return {ConvertFromPango(Rect(rectangle.x + start_x_pos, rectangle.y,
- end_x_pos - start_x_pos, rectangle.height))};
+ pango_layout_line_get_extents(line, nullptr, &rectangle);
+ auto rect =
+ ConvertFromPango(Rect(rectangle.x + start_x_pos, top,
+ end_x_pos - start_x_pos, rectangle.height));
+ return {rect};
} else {
std::vector<Rect> result;
- PangoRectangle rectangle;
-
auto start_line = pango_layout_get_line(pango_layout_, start_line_index);
- pango_layout_line_get_extents(start_line, &rectangle, nullptr);
- result.push_back(Rect(rectangle.x + start_x_pos, rectangle.y,
+ pango_layout_line_get_extents(start_line, nullptr, &rectangle);
+ result.push_back(Rect(rectangle.x + start_x_pos, top,
rectangle.width - start_x_pos, rectangle.height));
+ top += rectangle.height;
for (int line_index = start_line_index + 1; line_index < end_line_index;
line_index++) {
auto line = pango_layout_get_line(pango_layout_, line_index);
- pango_layout_line_get_extents(line, &rectangle, nullptr);
+ pango_layout_line_get_extents(line, nullptr, &rectangle);
result.push_back(
- Rect(rectangle.x, rectangle.y, rectangle.width, rectangle.height));
+ Rect(rectangle.x, top, rectangle.width, rectangle.height));
+ top += rectangle.height;
}
auto end_line = pango_layout_get_line(pango_layout_, end_line_index);
- pango_layout_line_get_extents(end_line, &rectangle, nullptr);
- result.push_back(
- Rect(rectangle.x, rectangle.y, end_x_pos, rectangle.height));
+ pango_layout_line_get_extents(end_line, nullptr, &rectangle);
+ result.push_back(Rect(rectangle.x, top, end_x_pos, rectangle.height));
- for (auto& r : result) {
- r = ConvertFromPango(r);
+ for (auto& rect : result) {
+ rect = ConvertFromPango(rect);
}
return result;
@@ -164,7 +168,7 @@ Rect PangoTextLayout::TextSinglePoint(Index position, bool trailing) {
auto line = pango_layout_get_line(pango_layout_, line_index);
PangoRectangle rectangle;
- pango_layout_line_get_extents(line, &rectangle, nullptr);
+ pango_layout_line_get_extents(line, nullptr, &rectangle);
return ConvertFromPango(
Rect(rectangle.x + x_pos, rectangle.y, 0, rectangle.height));