Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Added AttrList #84

Merged
merged 4 commits into from
Jul 8, 2017
Merged

Added AttrList #84

merged 4 commits into from
Jul 8, 2017

Conversation

EPashkin
Copy link
Member

@EPashkin EPashkin commented Jul 7, 2017

Closes #83

@EPashkin
Copy link
Member Author

EPashkin commented Jul 7, 2017

@RazrFalcon can you test if this works?
And can you give snipped that can used as standalone example (I don't know how use pango right way)?

@GuillaumeGomez
Copy link
Member

GuillaumeGomez commented Jul 7, 2017

Damn, you did it before me! You're too efficient!

@RazrFalcon
Copy link
Contributor

RazrFalcon commented Jul 7, 2017

I don't know how use pango right way

Neither do I. I'm still learning.

How can I build this branch?

@EPashkin
Copy link
Member Author

EPashkin commented Jul 7, 2017

@RazrFalcon
Copy link
Contributor

Strange. I've tried this code:

let attr_list = pango::AttrList::new();
let attr = pango::functions::attr_underline_new(pango::Underline::Single).unwrap();
attr_list.insert(&attr);
layout.set_attributes(Some(&attr_list));

and I got:

thread 'main' panicked at 'not yet implemented', /home/razr/.cargo/git/checkouts/glib-928cf7b282977403/f874842/src/translate.rs:262
stack backtrace:
   0: std::sys::imp::backtrace::tracing::imp::unwind_backtrace
             at /checkout/src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1: std::sys_common::backtrace::_print
             at /checkout/src/libstd/sys_common/backtrace.rs:71
   2: std::panicking::default_hook::{{closure}}
             at /checkout/src/libstd/sys_common/backtrace.rs:60
             at /checkout/src/libstd/panicking.rs:355
   3: std::panicking::default_hook
             at /checkout/src/libstd/panicking.rs:371
   4: std::panicking::rust_panic_with_hook
             at /checkout/src/libstd/panicking.rs:549
   5: std::panicking::begin_panic
             at /checkout/src/libstd/panicking.rs:511
   6: glib::translate::ToGlibPtr::to_glib_full
             at ./<panic macros>:3
   7: pango::attr_list::<impl pango::auto::attr_list::AttrList>::insert
             at /media/data/Programming/Sources/pango/src/attr_list.rs:19
...

@EPashkin
Copy link
Member Author

EPashkin commented Jul 7, 2017

I was afraid of this: no to_glib_full for Boxed.
Try to fix it

@EPashkin
Copy link
Member Author

EPashkin commented Jul 7, 2017

Updated

@RazrFalcon
Copy link
Contributor

It works!

@RazrFalcon
Copy link
Contributor

Also a warning:

 --> pango/src/auto/engine_shape.rs:5:5
  |
5 | use glib::translate::*;
  |     ^^^^^^^^^^^^^^^
  |
  = note: #[warn(unused_imports)] on by default
``

@GuillaumeGomez
Copy link
Member

Don't worry about warnings in generated code. ;)

@EPashkin
Copy link
Member Author

EPashkin commented Jul 7, 2017

@RazrFalcon can you link your current code, I try extract example from it.

@EPashkin
Copy link
Member Author

EPashkin commented Jul 7, 2017

@GuillaumeGomez, @sdroege Please, check manual functions in attr_list.rs.
I don't like current version, but can't find any other variants, maybe you have idea.

@RazrFalcon
Copy link
Contributor

@EPashkin I will try to create a minimal example tomorrow. Current code is from my project.

@GuillaumeGomez
Copy link
Member

@EPashkin: Unfortunately, there aren't many ways to avoid rust running the destructor once the variable goes out of scope. For me, it's good enough and the code is really easy to understand so there's no point (from my point of view) in trying to find something else for the moment. Good job.

@RazrFalcon
Copy link
Contributor

Cargo.toml

[package]
name = "pango-test"
version = "0.1.0"

[dependencies.pango]
# git = "https://github.com/gtk-rs/pango"
# custom branch
path = "../pango" 

[dependencies.pangocairo]
# git = "https://github.com/RazrFalcon/pangocairo-rs"
# with updated dependencies 
path = "../pangocairo"

[dependencies.cairo-rs]
git = "https://github.com/gtk-rs/cairo"
features = ["png"]

main.rs

extern crate pango;
extern crate cairo;
extern crate pangocairo;

use std::fs;

use pango::LayoutExt;
use pangocairo::{PangoContextExt, CairoContextExt};

fn main() {
    let s = cairo::ImageSurface::create(cairo::Format::ARgb32, 200, 200);
    let cr = cairo::Context::new(&s);

    cr.set_source_rgba(1.0, 1.0, 1.0, 1.0);
    cr.paint();

    let pc = cr.create_pango_context();
    pc.set_resolution(300.0);
    cr.update_pango_context(&pc);

    let mut font = pango::FontDescription::new();
    font.set_family("Verdana");
    font.set_size(14 * pango::SCALE);

    let layout = pango::Layout::new(&pc);
    layout.set_font_description(Some(&font));
    layout.set_text("Text", -1);

    let attr_list = pango::AttrList::new();
    let attr = pango::functions::attr_underline_new(pango::Underline::Single).unwrap();
    attr_list.insert(attr);
    layout.set_attributes(Some(&attr_list));

    cr.move_to(10.0, 0.0);
    cr.set_source_rgba(1.0, 0.0, 0.0, 1.0);

    cr.update_pango_layout(&layout);
    cr.show_pango_layout(&layout);

    cr.pango_layout_path(&layout);
    cr.stroke();

    let mut buffer = fs::File::create("out.png").unwrap();
    s.write_to_png(&mut buffer).unwrap();
}

Hope it would help.

Result:
out

@EPashkin
Copy link
Member Author

EPashkin commented Jul 8, 2017

@RazrFalcon Thanks, I tested getter/setters on it.
Bad that it don't works without pangocairo.

@RazrFalcon
Copy link
Contributor

@EPashkin You need pangocairo to draw text on something. I don't know is it possible to draw text using only pango. It's just a layout engine after all.

@RazrFalcon
Copy link
Contributor

@EPashkin Also pango::functions::attr_underline_new() looks super ugly. Can we do something about it? Should I create an issue for this?

@EPashkin
Copy link
Member Author

EPashkin commented Jul 8, 2017

You can use pango::attr_underline_new.
Or you want something other?

@RazrFalcon
Copy link
Contributor

RazrFalcon commented Jul 8, 2017

I want something like: Attribute::underline(Underline::Single). Implement this methods as static methods of Attribute.

Or Attribute::new_underline(Underline::Single).

@EPashkin
Copy link
Member Author

EPashkin commented Jul 8, 2017

Yes, I can do it in this PR.
Only problem with names: IMHO better new_underline instead attr_underline_new to be clear.

@RazrFalcon
Copy link
Contributor

Good. new_underline looks more like idiomatic rust.

@EPashkin
Copy link
Member Author

EPashkin commented Jul 8, 2017

Updated

@EPashkin
Copy link
Member Author

EPashkin commented Jul 8, 2017

Seems this ready to merge

@GuillaumeGomez
Copy link
Member

I agree. Thanks!

@GuillaumeGomez GuillaumeGomez merged commit 6ab1281 into gtk-rs:master Jul 8, 2017
@EPashkin EPashkin deleted the attr_list branch July 8, 2017 15:07
impl AttrList {
pub fn change(&self, attr: Attribute) {
unsafe {
ffi::pango_attr_list_change(self.to_glib_none().0, attr.to_glib_none().0 as *mut _);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's exactly why I'd like to have https://github.com/gtk-rs/glib/issues/176 :)

@sdroege
Copy link
Member

sdroege commented Jul 10, 2017

Looks good to me too

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants