aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-09-27 00:24:43 +0800
committerYuqian Yang <crupest@crupest.life>2025-09-27 00:24:43 +0800
commit30910505a7a8e36e6642cbe17134c520000bd583 (patch)
tree035cd76e691560fd6c59b3dd49aef2c09a5042cb
parent38aca5bc750b0679dd7f2b7bb5e5a0b6f983dd8b (diff)
downloadcru-30910505a7a8e36e6642cbe17134c520000bd583.tar.gz
cru-30910505a7a8e36e6642cbe17134c520000bd583.tar.bz2
cru-30910505a7a8e36e6642cbe17134c520000bd583.zip
Fix pango font rendering.
-rw-r--r--demos/platform/graphics/CMakeLists.txt3
-rw-r--r--demos/platform/graphics/DrawText.cpp21
-rw-r--r--src/platform/graphics/cairo/CairoPainter.cpp2
-rw-r--r--src/platform/graphics/cairo/PangoFont.cpp2
-rw-r--r--src/platform/graphics/cairo/PangoTextLayout.cpp4
5 files changed, 30 insertions, 2 deletions
diff --git a/demos/platform/graphics/CMakeLists.txt b/demos/platform/graphics/CMakeLists.txt
index ff5f64a5..07758ee6 100644
--- a/demos/platform/graphics/CMakeLists.txt
+++ b/demos/platform/graphics/CMakeLists.txt
@@ -5,5 +5,8 @@ target_sources(CruDemoPlatformGraphicsBase INTERFACE Base.cpp)
add_executable(CruDemoPlatformGraphicsDrawCircle DrawCircle.cpp)
target_link_libraries(CruDemoPlatformGraphicsDrawCircle PRIVATE CruDemoPlatformGraphicsBase)
+add_executable(CruDemoPlatformGraphicsDrawText DrawText.cpp)
+target_link_libraries(CruDemoPlatformGraphicsDrawText PRIVATE CruDemoPlatformGraphicsBase)
+
add_executable(CruDemoPlatformGraphicsSvgPath SvgPath.cpp)
target_link_libraries(CruDemoPlatformGraphicsSvgPath PRIVATE CruDemoPlatformGraphicsBase)
diff --git a/demos/platform/graphics/DrawText.cpp b/demos/platform/graphics/DrawText.cpp
new file mode 100644
index 00000000..5de5e127
--- /dev/null
+++ b/demos/platform/graphics/DrawText.cpp
@@ -0,0 +1,21 @@
+#include "Base.h"
+#include "cru/platform/Color.h"
+#include "cru/platform/graphics/Factory.h"
+#include "cru/platform/graphics/Font.h"
+#include "cru/platform/graphics/Painter.h"
+
+#include <memory>
+
+int main() {
+ CruPlatformGraphicsDemo demo("draw-text-demo.png", 500, 200);
+
+ auto brush =
+ demo.GetFactory()->CreateSolidColorBrush(cru::platform::colors::skyblue);
+
+ std::shared_ptr<cru::platform::graphics::IFont> font(
+ demo.GetFactory()->CreateFont(u"", 24));
+ auto text_layout = demo.GetFactory()->CreateTextLayout(font, u"Hello world!");
+ demo.GetPainter()->DrawText({0, 0}, text_layout.get(), brush.get());
+
+ return 0;
+}
diff --git a/src/platform/graphics/cairo/CairoPainter.cpp b/src/platform/graphics/cairo/CairoPainter.cpp
index b9ab50c4..8dd214cc 100644
--- a/src/platform/graphics/cairo/CairoPainter.cpp
+++ b/src/platform/graphics/cairo/CairoPainter.cpp
@@ -186,6 +186,8 @@ void CairoPainter::DrawText(const Point& offset, ITextLayout* text_layout,
cairo_save(cairo_);
cairo_set_source(cairo_, cairo_pattern);
+ cairo_move_to(cairo_, offset.x, offset.y);
+ pango_cairo_update_layout(cairo_, pango_text_layout->GetPangoLayout());
pango_cairo_show_layout(cairo_, pango_text_layout->GetPangoLayout());
cairo_restore(cairo_);
}
diff --git a/src/platform/graphics/cairo/PangoFont.cpp b/src/platform/graphics/cairo/PangoFont.cpp
index 0de17add..d5c1ad0b 100644
--- a/src/platform/graphics/cairo/PangoFont.cpp
+++ b/src/platform/graphics/cairo/PangoFont.cpp
@@ -10,7 +10,7 @@ PangoFont::PangoFont(CairoGraphicsFactory* factory, String font_family,
auto font_family_str = font_family_.ToUtf8();
pango_font_description_set_family(pango_font_description_,
font_family_str.c_str());
- pango_font_description_set_size(pango_font_description_, font_size);
+ pango_font_description_set_size(pango_font_description_, font_size * PANGO_SCALE);
}
PangoFont::~PangoFont() {
diff --git a/src/platform/graphics/cairo/PangoTextLayout.cpp b/src/platform/graphics/cairo/PangoTextLayout.cpp
index 1033ce9e..234c4154 100644
--- a/src/platform/graphics/cairo/PangoTextLayout.cpp
+++ b/src/platform/graphics/cairo/PangoTextLayout.cpp
@@ -6,13 +6,15 @@
#include "cru/platform/graphics/cairo/CairoGraphicsFactory.h"
#include "cru/platform/graphics/cairo/PangoFont.h"
+#include <pango/pangocairo.h>
+
namespace cru::platform::graphics::cairo {
PangoTextLayout::PangoTextLayout(CairoGraphicsFactory* factory,
std::shared_ptr<IFont> font)
: CairoResource(factory) {
Expects(font);
font_ = CheckPlatform<PangoFont>(font, GetPlatformId());
- pango_layout_ = pango_layout_new(factory->GetDefaultPangoContext());
+ pango_layout_ = pango_cairo_create_layout(factory->GetDefaultCairo());
pango_layout_set_font_description(pango_layout_,
font_->GetPangoFontDescription());
};