aboutsummaryrefslogtreecommitdiff
path: root/src/platform/graphics/cairo
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-10-17 12:06:14 +0800
committerYuqian Yang <crupest@crupest.life>2025-10-17 12:06:14 +0800
commit32aa6f116acc6e3e20a1ec76cef45b29f7005ad7 (patch)
tree892b71060a88b58d9293d78033000b05818783df /src/platform/graphics/cairo
parentfaf77949e19dc0d01f75bf8abb783eda70328048 (diff)
downloadcru-32aa6f116acc6e3e20a1ec76cef45b29f7005ad7.tar.gz
cru-32aa6f116acc6e3e20a1ec76cef45b29f7005ad7.tar.bz2
cru-32aa6f116acc6e3e20a1ec76cef45b29f7005ad7.zip
Remove String stage 1.
Diffstat (limited to 'src/platform/graphics/cairo')
-rw-r--r--src/platform/graphics/cairo/CairoGraphicsFactory.cpp4
-rw-r--r--src/platform/graphics/cairo/CairoImageFactory.cpp2
-rw-r--r--src/platform/graphics/cairo/CairoPainter.cpp24
-rw-r--r--src/platform/graphics/cairo/CairoResource.cpp6
-rw-r--r--src/platform/graphics/cairo/PangoFont.cpp9
-rw-r--r--src/platform/graphics/cairo/PangoTextLayout.cpp56
6 files changed, 39 insertions, 62 deletions
diff --git a/src/platform/graphics/cairo/CairoGraphicsFactory.cpp b/src/platform/graphics/cairo/CairoGraphicsFactory.cpp
index 11d2e734..7414e14f 100644
--- a/src/platform/graphics/cairo/CairoGraphicsFactory.cpp
+++ b/src/platform/graphics/cairo/CairoGraphicsFactory.cpp
@@ -32,13 +32,13 @@ CairoGraphicsFactory::CreateGeometryBuilder() {
return std::make_unique<CairoGeometryBuilder>(this);
}
-std::unique_ptr<IFont> CairoGraphicsFactory::CreateFont(String font_family,
+std::unique_ptr<IFont> CairoGraphicsFactory::CreateFont(std::string font_family,
float font_size) {
return std::make_unique<PangoFont>(this, std::move(font_family), font_size);
}
std::unique_ptr<ITextLayout> CairoGraphicsFactory::CreateTextLayout(
- std::shared_ptr<IFont> font, String text) {
+ std::shared_ptr<IFont> font, std::string text) {
auto text_layout = std::make_unique<PangoTextLayout>(this, std::move(font));
text_layout->SetText(std::move(text));
return text_layout;
diff --git a/src/platform/graphics/cairo/CairoImageFactory.cpp b/src/platform/graphics/cairo/CairoImageFactory.cpp
index ff922e77..6225247a 100644
--- a/src/platform/graphics/cairo/CairoImageFactory.cpp
+++ b/src/platform/graphics/cairo/CairoImageFactory.cpp
@@ -212,7 +212,7 @@ std::unique_ptr<IImage> CairoImageFactory::DecodeFromStream(
void CairoImageFactory::EncodeToStream(IImage* image, io::Stream* stream,
ImageFormat format, float quality) {
- auto cairo_image = CheckPlatform<CairoImage>(image, GetPlatformIdUtf8());
+ auto cairo_image = CheckPlatform<CairoImage>(image, GetPlatformId());
if (format == ImageFormat::Png) {
EncodePng(cairo_image->GetCairoSurface(), stream);
diff --git a/src/platform/graphics/cairo/CairoPainter.cpp b/src/platform/graphics/cairo/CairoPainter.cpp
index 94111098..d043e686 100644
--- a/src/platform/graphics/cairo/CairoPainter.cpp
+++ b/src/platform/graphics/cairo/CairoPainter.cpp
@@ -56,7 +56,7 @@ void CairoPainter::Clear(const Color& color) {
void CairoPainter::DrawLine(const Point& start, const Point& end, IBrush* brush,
float width) {
CheckValidation();
- auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformIdUtf8());
+ auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformId());
auto cairo_pattern = cairo_brush->GetCairoPattern();
cairo_save(cairo_);
cairo_set_source(cairo_, cairo_pattern);
@@ -71,7 +71,7 @@ void CairoPainter::DrawLine(const Point& start, const Point& end, IBrush* brush,
void CairoPainter::StrokeRectangle(const Rect& rectangle, IBrush* brush,
float width) {
CheckValidation();
- auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformIdUtf8());
+ auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformId());
auto cairo_pattern = cairo_brush->GetCairoPattern();
cairo_save(cairo_);
cairo_set_source(cairo_, cairo_pattern);
@@ -85,7 +85,7 @@ void CairoPainter::StrokeRectangle(const Rect& rectangle, IBrush* brush,
void CairoPainter::FillRectangle(const Rect& rectangle, IBrush* brush) {
CheckValidation();
- auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformIdUtf8());
+ auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformId());
auto cairo_pattern = cairo_brush->GetCairoPattern();
cairo_save(cairo_);
cairo_set_source(cairo_, cairo_pattern);
@@ -99,7 +99,7 @@ void CairoPainter::FillRectangle(const Rect& rectangle, IBrush* brush) {
void CairoPainter::StrokeEllipse(const Rect& outline_rect, IBrush* brush,
float width) {
CheckValidation();
- auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformIdUtf8());
+ auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformId());
auto cairo_pattern = cairo_brush->GetCairoPattern();
cairo_save(cairo_);
cairo_set_source(cairo_, cairo_pattern);
@@ -122,7 +122,7 @@ void CairoPainter::StrokeEllipse(const Rect& outline_rect, IBrush* brush,
void CairoPainter::FillEllipse(const Rect& outline_rect, IBrush* brush) {
CheckValidation();
- auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformIdUtf8());
+ auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformId());
auto cairo_pattern = cairo_brush->GetCairoPattern();
cairo_save(cairo_);
cairo_set_source(cairo_, cairo_pattern);
@@ -145,8 +145,8 @@ void CairoPainter::FillEllipse(const Rect& outline_rect, IBrush* brush) {
void CairoPainter::StrokeGeometry(IGeometry* geometry, IBrush* brush,
float width) {
CheckValidation();
- auto cairo_geometry = CheckPlatform<CairoGeometry>(geometry, GetPlatformIdUtf8());
- auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformIdUtf8());
+ auto cairo_geometry = CheckPlatform<CairoGeometry>(geometry, GetPlatformId());
+ auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformId());
auto cairo_path = cairo_geometry->GetCairoPath();
auto cairo_pattern = cairo_brush->GetCairoPattern();
@@ -162,8 +162,8 @@ void CairoPainter::StrokeGeometry(IGeometry* geometry, IBrush* brush,
void CairoPainter::FillGeometry(IGeometry* geometry, IBrush* brush) {
CheckValidation();
- auto cairo_geometry = CheckPlatform<CairoGeometry>(geometry, GetPlatformIdUtf8());
- auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformIdUtf8());
+ auto cairo_geometry = CheckPlatform<CairoGeometry>(geometry, GetPlatformId());
+ auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformId());
auto cairo_path = cairo_geometry->GetCairoPath();
auto cairo_pattern = cairo_brush->GetCairoPattern();
@@ -181,9 +181,9 @@ void CairoPainter::DrawText(const Point& offset, ITextLayout* text_layout,
CheckValidation();
auto pango_text_layout =
- CheckPlatform<PangoTextLayout>(text_layout, GetPlatformIdUtf8());
+ CheckPlatform<PangoTextLayout>(text_layout, GetPlatformId());
- auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformIdUtf8());
+ auto cairo_brush = CheckPlatform<CairoBrush>(brush, GetPlatformId());
auto cairo_pattern = cairo_brush->GetCairoPattern();
cairo_save(cairo_);
@@ -196,7 +196,7 @@ void CairoPainter::DrawText(const Point& offset, ITextLayout* text_layout,
void CairoPainter::DrawImage(const Point& offset, IImage* image) {
CheckValidation();
- auto cairo_image = CheckPlatform<CairoImage>(image, GetPlatformIdUtf8());
+ auto cairo_image = CheckPlatform<CairoImage>(image, GetPlatformId());
cairo_save(cairo_);
cairo_set_source_surface(cairo_, cairo_image->GetCairoSurface(), 0, 0);
cairo_new_path(cairo_);
diff --git a/src/platform/graphics/cairo/CairoResource.cpp b/src/platform/graphics/cairo/CairoResource.cpp
index ee371efc..f8ce9666 100644
--- a/src/platform/graphics/cairo/CairoResource.cpp
+++ b/src/platform/graphics/cairo/CairoResource.cpp
@@ -2,14 +2,16 @@
#include "cru/platform/graphics/cairo/CairoGraphicsFactory.h"
namespace cru::platform::graphics::cairo {
-static const String kCairoGraphicsPlatformId(u"cairo");
+static const std::string kCairoGraphicsPlatformId("cairo");
CairoResource::CairoResource(CairoGraphicsFactory* factory)
: factory_(factory) {}
CairoResource::~CairoResource() {}
-String CairoResource::GetPlatformId() const { return kCairoGraphicsPlatformId; }
+std::string CairoResource::GetPlatformId() const {
+ return kCairoGraphicsPlatformId;
+}
IGraphicsFactory* CairoResource::GetGraphicsFactory() { return factory_; }
} // namespace cru::platform::graphics::cairo
diff --git a/src/platform/graphics/cairo/PangoFont.cpp b/src/platform/graphics/cairo/PangoFont.cpp
index d5c1ad0b..e375d674 100644
--- a/src/platform/graphics/cairo/PangoFont.cpp
+++ b/src/platform/graphics/cairo/PangoFont.cpp
@@ -1,23 +1,24 @@
#include "cru/platform/graphics/cairo/PangoFont.h"
namespace cru::platform::graphics::cairo {
-PangoFont::PangoFont(CairoGraphicsFactory* factory, String font_family,
+PangoFont::PangoFont(CairoGraphicsFactory* factory, std::string font_family,
float font_size)
: CairoResource(factory),
font_family_(std::move(font_family)),
font_size_(font_size) {
pango_font_description_ = pango_font_description_new();
- auto font_family_str = font_family_.ToUtf8();
+ auto font_family_str = font_family_;
pango_font_description_set_family(pango_font_description_,
font_family_str.c_str());
- pango_font_description_set_size(pango_font_description_, font_size * PANGO_SCALE);
+ pango_font_description_set_size(pango_font_description_,
+ font_size * PANGO_SCALE);
}
PangoFont::~PangoFont() {
pango_font_description_free(pango_font_description_);
}
-String PangoFont::GetFontName() { return font_family_; }
+std::string PangoFont::GetFontName() { return font_family_; }
float PangoFont::GetFontSize() { return font_size_; }
} // namespace cru::platform::graphics::cairo
diff --git a/src/platform/graphics/cairo/PangoTextLayout.cpp b/src/platform/graphics/cairo/PangoTextLayout.cpp
index 746056e2..f8ed20c7 100644
--- a/src/platform/graphics/cairo/PangoTextLayout.cpp
+++ b/src/platform/graphics/cairo/PangoTextLayout.cpp
@@ -1,5 +1,4 @@
#include "cru/platform/graphics/cairo/PangoTextLayout.h"
-#include "cru/base/StringUtil.h"
#include "cru/platform/Check.h"
#include "cru/platform/GraphicsBase.h"
#include "cru/platform/graphics/Base.h"
@@ -24,7 +23,7 @@ PangoTextLayout::PangoTextLayout(CairoGraphicsFactory* factory,
std::shared_ptr<IFont> font)
: CairoResource(factory) {
Expects(font);
- font_ = CheckPlatform<PangoFont>(font, GetPlatformIdUtf8());
+ font_ = CheckPlatform<PangoFont>(font, GetPlatformId());
pango_layout_ = pango_cairo_create_layout(factory->GetDefaultCairo());
pango_layout_set_font_description(pango_layout_,
font_->GetPangoFontDescription());
@@ -32,19 +31,18 @@ PangoTextLayout::PangoTextLayout(CairoGraphicsFactory* factory,
PangoTextLayout::~PangoTextLayout() { g_object_unref(pango_layout_); }
-String PangoTextLayout::GetText() { return text_; }
+std::string PangoTextLayout::GetText() { return text_; }
-void PangoTextLayout::SetText(String new_text) {
+void PangoTextLayout::SetText(std::string new_text) {
text_ = std::move(new_text);
- utf8_text_ = text_.ToUtf8();
- pango_layout_set_text(pango_layout_, utf8_text_.c_str(), utf8_text_.size());
+ pango_layout_set_text(pango_layout_, text_.c_str(), text_.size());
}
std::shared_ptr<IFont> PangoTextLayout::GetFont() { return font_; }
void PangoTextLayout::SetFont(std::shared_ptr<IFont> font) {
Expects(font);
- font_ = CheckPlatform<PangoFont>(font, GetPlatformIdUtf8());
+ font_ = CheckPlatform<PangoFont>(font, GetPlatformId());
pango_layout_set_font_description(pango_layout_,
font_->GetPangoFontDescription());
}
@@ -63,9 +61,8 @@ void PangoTextLayout::SetEditMode(bool enable) { edit_mode_ = enable; }
Index PangoTextLayout::GetLineIndexFromCharIndex(Index char_index) {
int line;
- pango_layout_index_to_line_x(pango_layout_,
- FromUtf16IndexToUtf8Index(char_index), false,
- &line, nullptr);
+ pango_layout_index_to_line_x(pango_layout_, char_index, false, &line,
+ nullptr);
return line;
}
@@ -80,27 +77,6 @@ float PangoTextLayout::GetLineHeight(Index line_index) {
return static_cast<float>(height) / PANGO_SCALE;
}
-Index PangoTextLayout::FromUtf8IndexToUtf16Index(Index index) {
- Utf8CodePointIterator iter(utf8_text_.data(), utf8_text_.size());
- int cp_count = 0;
- while ((!iter.IsPastEnd()) && iter.GetPosition() < index) {
- ++iter;
- cp_count++;
- }
- return text_.IndexFromCodePointToCodeUnit(cp_count);
-}
-
-Index PangoTextLayout::FromUtf16IndexToUtf8Index(Index index) {
- Index cp_index = text_.IndexFromCodeUnitToCodePoint(index);
- Utf8CodePointIterator iter(utf8_text_.data(), utf8_text_.size());
-
- for (Index i = 0; i < cp_index; ++i) {
- ++iter;
- }
-
- return iter.GetPosition();
-}
-
Rect PangoTextLayout::GetTextBounds(bool includingTrailingSpace) {
PangoRectangle rectangle;
pango_layout_get_extents(pango_layout_, nullptr, &rectangle);
@@ -110,17 +86,17 @@ Rect PangoTextLayout::GetTextBounds(bool includingTrailingSpace) {
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());
+ auto start_index = tr.GetStart();
+ auto end_index = 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,
+ pango_layout_index_to_line_x(pango_layout_, start_index, false,
&start_line_index, &start_x_pos);
- pango_layout_index_to_line_x(pango_layout_, utf8_end_index, false,
- &end_line_index, &end_x_pos);
+ pango_layout_index_to_line_x(pango_layout_, end_index, false, &end_line_index,
+ &end_x_pos);
- pango_layout_index_to_pos(pango_layout_, utf8_start_index, &rectangle);
+ pango_layout_index_to_pos(pango_layout_, start_index, &rectangle);
auto top = rectangle.y;
if (start_line_index == end_line_index) {
@@ -161,9 +137,8 @@ std::vector<Rect> PangoTextLayout::TextRangeRect(const TextRange& text_range) {
}
Rect PangoTextLayout::TextSinglePoint(Index position, bool trailing) {
- auto utf8_index = FromUtf16IndexToUtf8Index(position);
int line_index, x_pos, y_pos = 0;
- pango_layout_index_to_line_x(pango_layout_, utf8_index, trailing, &line_index,
+ pango_layout_index_to_line_x(pango_layout_, position, trailing, &line_index,
&x_pos);
for (int i = 0; i < line_index; i++) {
@@ -186,8 +161,7 @@ TextHitTestResult PangoTextLayout::HitTest(const Point& point) {
auto inside_text =
pango_layout_xy_to_index(pango_layout_, point.x * PANGO_SCALE,
point.y * PANGO_SCALE, &index, &trailing);
- return TextHitTestResult{FromUtf8IndexToUtf16Index(index), trailing != 0,
- inside_text != 0};
+ return TextHitTestResult{index, trailing != 0, inside_text != 0};
}
} // namespace cru::platform::graphics::cairo