Copyright | (c) 2011-2015 diagrams-core team (see LICENSE) |
---|---|
License | BSD-style (see LICENSE) |
Maintainer | diagrams-discuss@googlegroups.com |
Safe Haskell | None |
Language | Haskell2010 |
A definition of styles for diagrams as extensible, heterogeneous collections of attributes.
- class (Typeable a, Semigroup a) => AttributeClass a
- data Attribute v n :: * where
- Attribute :: AttributeClass a => a -> Attribute v n
- MAttribute :: AttributeClass a => Measured n a -> Attribute v n
- TAttribute :: (AttributeClass a, Transformable a, V a ~ v, N a ~ n) => a -> Attribute v n
- _Attribute :: AttributeClass a => Prism' (Attribute v n) a
- _MAttribute :: (AttributeClass a, Typeable n) => Prism' (Attribute v n) (Measured n a)
- _TAttribute :: (V a ~ v, N a ~ n, AttributeClass a, Transformable a) => Prism' (Attribute v n) a
- unwrapAttribute :: AttributeClass a => Attribute v n -> Maybe a
- unmeasureAttribute :: (Num n, Typeable n) => n -> n -> Attribute v n -> Attribute v n
- attributeType :: Attribute v n -> TypeRep
- newtype Style v n = Style (HashMap TypeRep (Attribute v n))
- attributeToStyle :: Attribute v n -> Style v n
- getAttr :: forall a v n. AttributeClass a => Style v n -> Maybe a
- unmeasureAttrs :: (Num n, Typeable n) => n -> n -> Style v n -> Style v n
- atAttr :: AttributeClass a => Lens' (Style v n) (Maybe a)
- atMAttr :: (AttributeClass a, Typeable n) => Lens' (Style v n) (Maybe (Measured n a))
- atTAttr :: (V a ~ v, N a ~ n, AttributeClass a, Transformable a) => Lens' (Style v n) (Maybe a)
- applyAttr :: (AttributeClass a, HasStyle d) => a -> d -> d
- applyMAttr :: (AttributeClass a, N d ~ n, HasStyle d, Typeable n) => Measured n a -> d -> d
- applyTAttr :: (AttributeClass a, Transformable a, V a ~ V d, N a ~ N d, HasStyle d) => a -> d -> d
- class HasStyle a where
- applyStyle :: Style (V a) (N a) -> 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, Semigroup a) => AttributeClass a Source
data Attribute v n :: * where Source
An existential wrapper type to hold attributes. Some attributes are simply inert/static; some are affected by transformations; and some are affected by transformations and can be modified generically.
Attribute :: AttributeClass a => a -> Attribute v n | |
MAttribute :: AttributeClass a => Measured n a -> Attribute v n | |
TAttribute :: (AttributeClass a, Transformable a, V a ~ v, N a ~ n) => a -> Attribute v n |
Typeable * n => Show (Attribute v n) Source | Shows the kind of attribute and the type contained in the attribute. |
Typeable * n => Semigroup (Attribute v n) Source | Attributes form a semigroup, where the semigroup operation simply returns the right-hand attribute when the types do not match, and otherwise uses the semigroup operation specific to the (matching) types. |
(Additive v, Traversable v, Floating n) => Transformable (Attribute v n) Source |
|
Each (Style v n) (Style v' n') (Attribute v n) (Attribute v' n') Source | |
type N (Attribute v n) = n Source | |
type V (Attribute v n) = v Source |
Attributes prisms
_Attribute :: AttributeClass a => Prism' (Attribute v n) a Source
Prism onto an Attribute
.
_MAttribute :: (AttributeClass a, Typeable n) => Prism' (Attribute v n) (Measured n a) Source
Prism onto an MAttribute
.
_TAttribute :: (V a ~ v, N a ~ n, AttributeClass a, Transformable a) => Prism' (Attribute v n) a Source
Prism onto a TAttribute
.
Attributes utilities
unwrapAttribute :: AttributeClass a => Attribute v n -> Maybe a Source
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.
Measured attributes cannot be extrated from this function until
they have been unmeasured with unmeasureAttribute
. If you want a
measured attibute use the _MAttribute
prism.
unmeasureAttribute :: (Num n, Typeable n) => n -> n -> Attribute v n -> Attribute v n Source
Turn an MAttribute
into an Attribute
using the given global
and normalized
scale.
attributeType :: Attribute v n -> TypeRep Source
Type of an attribute that is stored with a style. Measured attributes return the type as if it where unmeasured.
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.
Typeable * n => Show (Style v n) Source | Show the attributes in the style. |
Typeable * n => Monoid (Style v n) Source | The empty style contains no attributes. |
Typeable * n => Semigroup (Style v n) Source | Combine a style by combining the attributes; if the two styles have attributes of the same type they are combined according to their semigroup structure. |
Ixed (Style v n) Source | |
At (Style v n) Source | |
Wrapped (Style v n) Source | |
(Additive v, Traversable v, Floating n) => Transformable (Style v n) Source | |
Typeable * n => HasStyle (Style v n) Source | |
Action (Style v n) m Source | Styles have no action on other monoids. |
Rewrapped (Style v n) (Style v' n') Source | |
Each (Style v n) (Style v' n') (Attribute v n) (Attribute v' n') Source | |
type Index (Style v n) = TypeRep Source | |
type IxValue (Style v n) = Attribute v n Source | |
type Unwrapped (Style v n) = HashMap TypeRep (Attribute v n) Source | |
type N (Style v n) = n Source | |
type V (Style v n) = v Source |
Making styles
attributeToStyle :: Attribute v n -> Style v n Source
Turn an attribute into a style. An easier way to make a style is to use the monoid instance and apply library functions for applying that attribute:
myStyle = mempty # fc blue :: Style V2 Double
Extracting attibutes from styles
getAttr :: forall a v n. AttributeClass a => Style v n -> Maybe a Source
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.
Trying to extract a measured attibute will fail. It either has to
be unmeasured with unmeasureAttrs
or use the atMAttr
lens.
unmeasureAttrs :: (Num n, Typeable n) => n -> n -> Style v n -> Style v n Source
Replace all MAttribute
s with Attribute
s using the global
and
normalized
scales.
Attibute lenses
atAttr :: AttributeClass a => Lens' (Style v n) (Maybe a) Source
Lens onto a plain attribute of a style.
atMAttr :: (AttributeClass a, Typeable n) => Lens' (Style v n) (Maybe (Measured n a)) Source
Lens onto a measured attribute of a style.
atTAttr :: (V a ~ v, N a ~ n, AttributeClass a, Transformable a) => Lens' (Style v n) (Maybe a) Source
Lens onto a transformable attribute of a style.
Applying styles
applyAttr :: (AttributeClass a, HasStyle d) => a -> d -> d Source
Apply an attribute to an instance of HasStyle
(such as a
diagram or a style). If the object already has an attribute of
the same type, the new attribute is combined on the left with the
existing attribute, according to their semigroup structure.
applyMAttr :: (AttributeClass a, N d ~ n, HasStyle d, Typeable n) => Measured n a -> d -> d Source
Apply a measured attribute to an instance of HasStyle
(such as a
diagram or a style). If the object already has an attribute of
the same type, the new attribute is combined on the left with the
existing attribute, according to their semigroup structure.
applyTAttr :: (AttributeClass a, Transformable a, V a ~ V d, N a ~ N d, HasStyle d) => a -> d -> d Source
Apply a transformable attribute to an instance of HasStyle
(such as a diagram or a style). If the object already has an
attribute of the same type, the new attribute is combined on the
left with the existing attribute, according to their semigroup
structure.
Type class for things which have a style.
applyStyle :: Style (V a) (N a) -> a -> a Source
Apply a style by combining it (on the left) with the existing style.
HasStyle a => HasStyle [a] Source | |
(HasStyle a, Ord a) => HasStyle (Set a) Source | |
HasStyle b => HasStyle (a -> b) Source | |
(HasStyle a, HasStyle b, (~) (* -> *) (V a) (V b), (~) * (N a) (N b)) => HasStyle (a, b) Source | |
HasStyle a => HasStyle (Map k a) Source | |
HasStyle b => HasStyle (Measured n b) Source | |
Typeable * n => HasStyle (Style v n) Source | |
(Metric v, OrderedField n, Semigroup m) => HasStyle (QDiagram b v n m) Source |