From fd7ea507b1a5cb5ef41f5ee491e9c83430721bf9 Mon Sep 17 00:00:00 2001 From: Joshua Thijssen Date: Mon, 6 Jan 2025 21:10:54 +0100 Subject: [PATCH] almost working --- Cargo.lock | 27 +++++++++++++++++++++++++++ Cargo.toml | 6 +++--- src/bin/gtk-test.rs | 34 ++++++++++++++++++++++++---------- 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5b9d218..24fc5fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -422,6 +422,7 @@ dependencies = [ "font-kit", "gtk4", "log", + "pangocairo", "prettytable", ] @@ -1011,6 +1012,32 @@ dependencies = [ "system-deps 7.0.3", ] +[[package]] +name = "pangocairo" +version = "0.20.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4690509a2fea2a6552a0ef8aa3e5f790c1365365ee0712afa1aedb39af3997b6" +dependencies = [ + "cairo-rs 0.20.7", + "glib 0.20.7", + "libc", + "pango", + "pangocairo-sys", +] + +[[package]] +name = "pangocairo-sys" +version = "0.20.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5be6ac24147911a6a46783922fc288cf02f67570bc0d360e563b5b26aead6767" +dependencies = [ + "cairo-sys-rs 0.20.7", + "glib-sys 0.20.7", + "libc", + "pango-sys", + "system-deps 7.0.3", +] + [[package]] name = "pathfinder_geometry" version = "0.5.1" diff --git a/Cargo.toml b/Cargo.toml index e3ad7c2..c13a5bf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,10 +17,10 @@ path = "src/bin/gtk-test.rs" [dependencies] colog = "^1.3" -font-kit = "0.14.2" -#freetype-rs = "^0.30" log = "0.4.22" anyhow = "1.0.95" prettytable = "0.10.0" gtk4 = { version = "0.9.5", features = ["v4_6"] } -cairo-rs = { version = "0.19.4", features = ["freetype"] } \ No newline at end of file +font-kit = "0.14.2" +cairo-rs = { version = "0.19.4", features = ["freetype"] } +pangocairo = "0.20.7" \ No newline at end of file diff --git a/src/bin/gtk-test.rs b/src/bin/gtk-test.rs index aafb1af..12a3f57 100644 --- a/src/bin/gtk-test.rs +++ b/src/bin/gtk-test.rs @@ -1,9 +1,11 @@ -use cairo::FontFace; +use std::rc::Rc; use cairo::freetype::{Face, GlyphSlot}; -use gtk4::{glib, Application, ApplicationWindow, DrawingArea}; -use gtk4::cairo::{Context, ImageSurface, Format}; -use gtk4::prelude::{ApplicationExt, ApplicationExtManual, DrawingAreaExtManual, GtkWindowExt}; +use cairo::{Context, ImageSurface, Format, FontFace}; use fontmanager::font_manager::{FontManager, FontStyle}; +use gtk4::{glib, pango, Application, ApplicationWindow, DrawingArea}; +use gtk4::cairo::{ffi, Error}; +use gtk4::prelude::{ApplicationExt, ApplicationExtManual, DrawingAreaExtManual, GtkWindowExt}; +use pangocairo::functions::{create_context, create_layout, show_layout}; const APP_ID: &str = "io.gosub.font-manager.gtk-test"; @@ -29,7 +31,7 @@ fn build_ui(app: &Application) { .build(); let area = DrawingArea::default(); - area.set_draw_func(move |_area, cr, _width, _height| { + area.set_draw_func(move |_area, cr, width, height| { cr.set_source_rgba(1.0, 0.0, 0.0, 1.0); cr.rectangle(0.0, 0.0, 100.0, 100.0); let _ = cr.fill(); @@ -38,8 +40,22 @@ fn build_ui(app: &Application) { let info = font_manager.find(vec!["comic sans ms", "arial", "sans-serif"], FontStyle::Normal).expect("Failed to find font"); let face = font_manager.load(info).expect("Failed to load font"); - cr.set_source_rgba(1.0, 0.0, 1.0, 1.0); - draw_freetype_text(cr, &face, "Hello, world!", 200.0, 200.0); + let cface = freetype_to_cairo_font_face(&face); + cr.set_font_face(&cface); + cr.set_font_size(24.0); + + let pango_context = create_context(cr); + let layout = create_layout(cr); + + layout.set_text("Hello, world!"); + layout.set_width(width * pango::SCALE); + layout.set_alignment(pango::Alignment::Center); + + cr.move_to(0.0, (height as f64) / 2.0); + show_layout(&cr, &layout); + + // cr.set_source_rgba(1.0, 0.0, 1.0, 1.0); + // draw_freetype_text(&cr, &face, "Hello, world!", 200.0, 200.0); }); let scroll = gtk4::ScrolledWindow::builder() @@ -92,8 +108,6 @@ fn render_glyph_to_cairo(cr: &Context, glyph: &GlyphSlot) { let width = bitmap.width(); let height = bitmap.rows(); - dbg!(width, height); - if width > 0 && height > 0 { // Create a Cairo image surface from the bitmap let mut surface = ImageSurface::create(Format::A8, width, height).unwrap(); @@ -116,4 +130,4 @@ fn render_glyph_to_cairo(cr: &Context, glyph: &GlyphSlot) { cr.set_source_surface(&surface, 0.0, 0.0).unwrap(); cr.paint().unwrap(); } -} +} \ No newline at end of file