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

Allow to get Color from Analysis #168

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gir.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ single_version_file = true
deprecate_by_min_version = true

generate = [
# "Pango.AttrColor",
# "Pango.AttrFloat",
# "Pango.AttrFontDesc",
# "Pango.AttrFontFeatures",
Expand Down Expand Up @@ -72,6 +71,7 @@ manual = [
"GLib.Error",
"Pango.Analysis",
"Pango.AttrClass",
"Pango.AttrColor",
"Pango.Language",
"Pango.Rectangle",
]
Expand Down
7 changes: 4 additions & 3 deletions src/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use glib::translate::*;
use pango_sys;
use Attribute;
use EngineLang;
use EngineShape;
use Font;
Expand Down Expand Up @@ -47,9 +48,9 @@ impl Analysis {
unsafe { from_glib_none(self.0.language) }
}

/*pub fn extra_attrs(&self) -> Vec<LogAttr> {
unsafe { from_glib_none_num_as_vec(self.0.extra_attrs) }
}*/
pub fn extra_attrs(&self) -> Vec<Attribute> {
unsafe { FromGlibPtrContainer::from_glib_none(self.0.extra_attrs) }
}
}

#[doc(hidden)]
Expand Down
9 changes: 9 additions & 0 deletions src/attr_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

use glib::translate::from_glib;
use glib::translate::{FromGlibPtrFull, FromGlibPtrNone, Stash, ToGlibPtr};
use pango_sys;
use AttrType;

#[doc(hidden)]
impl<'a> ToGlibPtr<'a, *mut pango_sys::PangoAttrClass> for &'a AttrClass {
Expand Down Expand Up @@ -46,8 +48,15 @@ impl FromGlibPtrFull<*const pango_sys::PangoAttrClass> for AttrClass {
}
}

#[derive(Debug)]
pub struct AttrClass(*mut pango_sys::PangoAttrClass);

impl AttrClass {
pub fn get_type(&self) -> AttrType {
unsafe { from_glib((*self.0).type_) }
}
}

impl PartialEq for AttrClass {
fn eq(&self, other: &AttrClass) -> bool {
self.0 == other.0
Expand Down
106 changes: 85 additions & 21 deletions src/attribute.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
// Copyright 2017, The Gtk-rs Project Developers.
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

use glib::translate::*;
use pango_sys;
use AttrClass;
use Attribute;
use FontDescription;
use Gravity;
use GravityHint;
use Language;
use Rectangle;
use Stretch;
use Style;
use Underline;
use Variant;
use Weight;

use glib::translate::from_glib_full;
use glib::translate::ToGlib;
use glib::translate::ToGlibPtr;

impl Attribute {
#[cfg(any(feature = "v1_38", feature = "dox"))]
pub fn new_background_alpha(alpha: u16) -> Option<Attribute> {
Copy link
Member

Choose a reason for hiding this comment

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

This (and all the others too) should probably return an AttributeBackgrondAlpha?

Copy link
Member

Choose a reason for hiding this comment

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

No, it's all casted as Attribute in the source code. Also, it's not really supposed to be "used" so better not give access to the fields.

Copy link
Member

Choose a reason for hiding this comment

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

Ack

Expand All @@ -32,10 +36,54 @@ impl Attribute {
}
}

// TODO: available at 1.44
// pub fn new_allow_breaks(allow_breaks: bool) -> Option<Attribute> {
// unsafe {
// from_glib_full(pango_sys::pango_attr_allow_breaks_new(
// allow_breaks.to_glib(),
// ))
// }
// }

// TODO: available at 1.44
// pub fn new_insert_hyphens(insert_hyphens: bool) -> Option<Attribute> {
// unsafe {
// from_glib_full(pango_sys::pango_attr_insert_hyphens_new(
// insert_hyphens.to_glib(),
// ))
// }
// }

// TODO: available at 1.44, needs PangoShowFlags
// pub fn new_show(flags: PangoShowFlags) -> Option<Attribute> {
// unsafe {
// from_glib_full(pango_sys::pango_attr_show_new(
// flags.to_glib(),
// ))
// }
// }

pub fn new_language(language: &Language) -> Option<Attribute> {
unsafe {
from_glib_full(pango_sys::pango_attr_language_new(
language.to_glib_none().0,
))
}
}

pub fn new_family(family: &str) -> Option<Attribute> {
unsafe { from_glib_full(pango_sys::pango_attr_family_new(family.to_glib_none().0)) }
}

#[cfg(any(feature = "v1_38", feature = "dox"))]
pub fn new_font_features(features: &str) -> Option<Attribute> {
unsafe {
from_glib_full(pango_sys::pango_attr_font_features_new(
features.to_glib_none().0,
))
}
}

#[cfg(any(feature = "v1_38", feature = "dox"))]
pub fn new_foreground_alpha(alpha: u16) -> Option<Attribute> {
unsafe { from_glib_full(pango_sys::pango_attr_foreground_alpha_new(alpha)) }
Expand All @@ -61,18 +109,34 @@ impl Attribute {
unsafe { from_glib_full(pango_sys::pango_attr_rise_new(rise)) }
}

pub fn new_scale(scale_factor: f64) -> Option<Attribute> {
unsafe { from_glib_full(pango_sys::pango_attr_scale_new(scale_factor)) }
}

pub fn new_size(size: i32) -> Option<Attribute> {
unsafe { from_glib_full(pango_sys::pango_attr_size_new(size)) }
}

pub fn new_size_absolute(size: i32) -> Option<Attribute> {
pub fn new_absolute_size(size: i32) -> Option<Attribute> {
unsafe { from_glib_full(pango_sys::pango_attr_size_new_absolute(size)) }
}

pub fn new_font_desc(desc: &FontDescription) -> Option<Attribute> {
unsafe { from_glib_full(pango_sys::pango_attr_font_desc_new(desc.to_glib_none().0)) }
}

pub fn new_shape(
ink_rect: &Rectangle,
logical_rect: &Rectangle,
) -> Option<Attribute> {
unsafe {
from_glib_full(pango_sys::pango_attr_shape_new(
ink_rect.to_glib_none().0,
logical_rect.to_glib_none().0,
))
}
}

pub fn new_scale(scale_factor: f64) -> Option<Attribute> {
unsafe { from_glib_full(pango_sys::pango_attr_scale_new(scale_factor)) }
}

pub fn new_stretch(stretch: Stretch) -> Option<Attribute> {
unsafe { from_glib_full(pango_sys::pango_attr_stretch_new(stretch.to_glib())) }
}
Expand Down Expand Up @@ -105,9 +169,9 @@ impl Attribute {
unsafe { from_glib_full(pango_sys::pango_attr_underline_new(underline.to_glib())) }
}

pub fn new_variant(variant: Variant) -> Option<Attribute> {
/*pub fn attr_variant_new(variant: Variant) -> Option<Attribute> {
unsafe { from_glib_full(pango_sys::pango_attr_variant_new(variant.to_glib())) }
}
}*/

pub fn new_weight(weight: Weight) -> Option<Attribute> {
unsafe { from_glib_full(pango_sys::pango_attr_weight_new(weight.to_glib())) }
Expand All @@ -117,31 +181,31 @@ impl Attribute {
unsafe { from_glib_full((*self.to_glib_none().0).klass) }
}

pub fn get_start_index(&self) -> u32 {
pub fn get_end_index(&self) -> u32 {
unsafe {
let stash = self.to_glib_none();
(*stash.0).start_index
(*stash.0).end_index
}
}

pub fn get_end_index(&self) -> u32 {
Copy link
Member

Choose a reason for hiding this comment

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

These two getters at least seem useful, the setters maybe not

Copy link
Member

Choose a reason for hiding this comment

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

Indeed. Don't remember why I removed them...

pub fn set_end_index(&mut self, index: u32) {
unsafe {
let stash = self.to_glib_none();
(*stash.0).end_index
let stash = self.to_glib_none_mut();
(*stash.0).end_index = index;
}
}

pub fn set_start_index(&mut self, index: u32) {
pub fn get_start_index(&self) -> u32 {
unsafe {
let stash = self.to_glib_none_mut();
(*stash.0).start_index = index;
let stash = self.to_glib_none();
(*stash.0).start_index
}
}

pub fn set_end_index(&mut self, index: u32) {
pub fn set_start_index(&mut self, index: u32) {
unsafe {
let stash = self.to_glib_none_mut();
(*stash.0).end_index = index;
(*stash.0).start_index = index;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Should implement Clone via pango_attribute_copy(), Drop via pango_attribute_destroy(), PartialEq/Eq via pango_attribute_equal()

Copy link
Member

Choose a reason for hiding this comment

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

Look at the auto impl maybe?

Copy link
Member

Choose a reason for hiding this comment

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

Ah I thought there's only the manual impl of it. Ok then

}
29 changes: 29 additions & 0 deletions src/color.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2018, The Gtk-rs Project Developers.
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

use glib::translate::*;
use Color;

impl Color {
pub fn red(&self) -> u16 {
unsafe {
let stash = self.to_glib_none();
(*stash.0).red
}
}

pub fn green(&self) -> u16 {
unsafe {
let stash = self.to_glib_none();
(*stash.0).green
}
}

pub fn blue(&self) -> u16 {
unsafe {
let stash = self.to_glib_none();
(*stash.0).blue
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub use attr_class::AttrClass;
pub mod attr_iterator;
pub mod attr_list;
pub mod attribute;
pub mod color;
pub mod font_description;
mod functions;
pub mod gravity;
Expand Down