Maintainer | diagrams-discuss@googlegroups.com |
---|
A definition of styles for diagrams as extensible, heterogeneous collections of attributes.
- class Typeable a => AttributeClass a
- data Attribute where
- Attribute :: AttributeClass a => a -> Attribute
- mkAttr :: AttributeClass a => a -> Attribute
- unwrapAttr :: AttributeClass a => Attribute -> Maybe a
- applyAttr :: (AttributeClass a, HasStyle d) => a -> d -> d
- newtype Style = Style (Map String Attribute)
- attrToStyle :: forall a. AttributeClass a => a -> Style
- getAttr :: forall a. AttributeClass a => Style -> Maybe a
- setAttr :: forall a. AttributeClass a => a -> Style -> Style
- addAttr :: AttributeClass a => a -> Style -> Style
- class HasStyle a where
- applyStyle :: Style -> a -> a
Attributes
An attribute is anything that determines some aspect of a diagram's rendering. The standard diagrams library defines several standard attributes (line color, line width, fill color, etc.) but additional attributes may easily be created. Additionally, a given backend need not handle (or even know about) attributes used in diagrams it renders.
The attribute code is inspired by xmonad's Message
type, which
was in turn based on ideas in:
Simon Marlow. An Extensible Dynamically-Typed Hierarchy of Exceptions. Proceedings of the 2006 ACM SIGPLAN workshop on Haskell. http://research.microsoft.com/apps/pubs/default.aspx?id=67968.
class Typeable a => AttributeClass a Source
Every attribute must be an instance of AttributeClass
, which
simply guarantees a Typeable
constraint.
An existential wrapper type to hold attributes.
Attribute :: AttributeClass a => a -> Attribute |
mkAttr :: AttributeClass a => a -> AttributeSource
Wrap up an attribute.
unwrapAttr :: AttributeClass a => Attribute -> Maybe aSource
Unwrap an unknown Attribute
type, performing a dynamic (but
safe) check on the type of the result. If the required type
matches the type of the attribute, the attribute value is
returned wrapped in Just
; if the types do not match, Nothing
is returned.
applyAttr :: (AttributeClass a, HasStyle d) => a -> d -> dSource
Apply an attribute to an instance of HasStyle
(such as a
diagram or a style). applyAttr
has no effect if an attribute of
the same type already exists.
Styles
A Style
is a heterogeneous collection of attributes, containing
at most one attribute of any given type. This is also based on
ideas stolen from xmonad, specifically xmonad's implementation of
user-extensible state.
A Style
is a heterogeneous collection of attributes, containing
at most one attribute of any given type.
attrToStyle :: forall a. AttributeClass a => a -> StyleSource
Create a style from a single attribute.
getAttr :: forall a. AttributeClass a => Style -> Maybe aSource
Extract an attribute from a style of a particular type. If the
style contains an attribute of the requested type, it will be
returned wrapped in Just
; otherwise, Nothing
is returned.
setAttr :: forall a. AttributeClass a => a -> Style -> StyleSource
Add a new attribute to a style, or replace the old attribute of the same type if one exists.
addAttr :: AttributeClass a => a -> Style -> StyleSource
Attempt to add a new attribute to a style, but if an attribute of the same type already exists, do not replace it.
Type class for things which have a style.
applyStyle :: Style -> a -> aSource
Apply a style by combining it (on the left) with the existing style.
HasStyle Style | |
(HasLinearMap v, InnerSpace v, OrderedField (Scalar v), Monoid m) => HasStyle (AnnDiagram b v m) |