Skip to content

Commit

Permalink
almost working
Browse files Browse the repository at this point in the history
  • Loading branch information
jaytaph committed Jan 6, 2025
1 parent 24b7cdf commit fd7ea50
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
27 changes: 27 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
font-kit = "0.14.2"
cairo-rs = { version = "0.19.4", features = ["freetype"] }
pangocairo = "0.20.7"
34 changes: 24 additions & 10 deletions src/bin/gtk-test.rs
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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();
Expand All @@ -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()
Expand Down Expand Up @@ -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();
Expand All @@ -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();
}
}
}

0 comments on commit fd7ea50

Please sign in to comment.