Use Pango for room with no avatars

Cairo text rendering is really basic and not recommended for use

Fixes #156
This commit is contained in:
Alexandre Franke 2018-04-28 19:14:52 +02:00
parent 4f7a2c87ca
commit 8bbc06d793
3 changed files with 52 additions and 5 deletions

34
Cargo.lock generated
View file

@ -370,6 +370,8 @@ dependencies = [
"gdk-pixbuf 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"glib 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"mime 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"pango 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"pangocairo 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
"reqwest 0.8.5 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.43 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1088,6 +1090,36 @@ dependencies = [
"pkg-config 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "pangocairo"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
"cairo-rs 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"cairo-sys-rs 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"glib 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"glib-sys 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"gobject-sys 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
"pango 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"pango-sys 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"pangocairo-sys 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "pangocairo-sys"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"cairo-sys-rs 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"glib-sys 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
"pango-sys 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"pkg-config 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "percent-encoding"
version = "1.0.1"
@ -2014,6 +2046,8 @@ dependencies = [
"checksum ordermap 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a86ed3f5f244b372d6b1a00b72ef7f8876d0bc6a78a4c9985c53614041512063"
"checksum pango 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "45374801e224373c3c0393cd48073c81093494c8735721e81d1dbaa4096b2767"
"checksum pango-sys 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94039b3921a4af4058a3e4335e5d15099101f298a92f5afc40bab3a3027594a1"
"checksum pangocairo 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "db4130d0e0567b177178c9568466277ff167cb43a245b2881e203509ea5fbd84"
"checksum pangocairo-sys 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "decc9523cd5bbba49a67ed8d2ef7dfca957f4760bf420a1ea4a82634da26381c"
"checksum percent-encoding 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831"
"checksum pest 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0fce5d8b5cc33983fc74f78ad552b5522ab41442c4ca91606e4236eb4b5ceefc"
"checksum pest_derive 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "ab94faafeb93f4c5e3ce81ca0e5a779529a602ad5d09ae6d21996bfb8b6a52bf"

View file

@ -10,6 +10,8 @@ gdk-pixbuf = "0.4.0"
#gdk-pixbuf-sys = "0.5.0"
glib = "0.5.0"
mime = "0.3.5"
pango = "0.4.0"
pangocairo = "0.5.0"
regex = "0.2.10"
reqwest = "0.8.5"
serde = "1.0.43"

View file

@ -6,6 +6,8 @@ extern crate serde_json;
extern crate chrono;
extern crate time;
extern crate cairo;
extern crate pango;
extern crate pangocairo;
extern crate gdk;
extern crate gdk_pixbuf;
extern crate mime;
@ -14,6 +16,8 @@ extern crate unicode_segmentation;
use self::unicode_segmentation::UnicodeSegmentation;
use self::pango::LayoutExt;
use self::gdk_pixbuf::Pixbuf;
use self::gdk_pixbuf::PixbufExt;
use self::gdk::ContextExt;
@ -648,7 +652,6 @@ pub fn draw_identicon(fname: &str, name: String, mode: AvatarMode) -> Result<Str
}
};
g.set_font_size(24.);
g.set_source_rgb(1.0, 1.0, 1.0);
let name = name.to_uppercase();
@ -661,10 +664,18 @@ pub fn draw_identicon(fname: &str, name: String, mode: AvatarMode) -> Result<Str
None => String::from("X"),
};
let te = g.text_extents(&first);
g.move_to(20.0 - te.x_bearing - te.width / 2.0,
20.0 + te.height / 2.0);
g.show_text(&first);
let layout = pangocairo::functions::create_layout(&g).unwrap();
let fontdesc = pango::FontDescription::from_string("Cantarell 20");
layout.set_font_description(&fontdesc);
layout.set_text(&first);
// Move to center of the background shape we drew,
// offset by half the size of the glyph
let bx = image.get_width();
let by = image.get_height();
let (ox, oy) = layout.get_pixel_size();
g.translate((bx - ox) as f64/2., (by - oy) as f64/2.);
// Finally draw the glyph
pangocairo::functions::show_layout(&g, &layout);
let mut buffer = File::create(&fname)?;
image.write_to_png(&mut buffer)?;