Skip to content

Commit

Permalink
working - no arrows
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreyrosenbluth committed Feb 13, 2014
1 parent 9082fa7 commit 8962a52
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 74 deletions.
1 change: 1 addition & 0 deletions diagrams-lib.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Library
Diagrams.TwoD.Align,
-- Diagrams.TwoD.Arrow,
-- Diagrams.TwoD.Arrowheads,
Diagrams.TwoD.Attributes,
Diagrams.TwoD.Combinators,
Diagrams.TwoD.Deform,
Diagrams.TwoD.Transform,
Expand Down
61 changes: 2 additions & 59 deletions src/Diagrams/Attributes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ module Diagrams.Attributes (
-- ** Converting colors
, colorToSRGBA, colorToRGBA

-- * Lines
-- ** Width
, LineWidth, getLineWidth, lineWidth, lineWidthA, lw

-- * Line stuff
-- ** Cap style
, LineCap(..), LineCapA, getLineCap, lineCap

Expand All @@ -55,8 +52,6 @@ module Diagrams.Attributes (
-- ** Dashing
, Dashing(..), DashingA, getDashing, dashing

-- * Measure conversion
, toOutput

) where

Expand All @@ -69,12 +64,9 @@ import Data.Maybe (fromMaybe)
import Data.Monoid.Recommend
import Data.Semigroup
import Data.Typeable
import Data.VectorSpace (magnitude)

import Diagrams.Core
import Diagrams.Core.Compile (mapRTreeStyle)
import Diagrams.Core.Style (setAttr)
import Diagrams.Core.Types (RTree)

------------------------------------------------------------
-- Color -------------------------------------------------
Expand Down Expand Up @@ -273,58 +265,9 @@ opacity :: HasStyle a => Double -> a -> a
opacity = applyAttr . Opacity . Product

------------------------------------------------------------
-- Lines and stuff -------------------------------------
-- Line stuff -------------------------------------
------------------------------------------------------------

-- | Line widths specified on child nodes always override line widths
-- specified at parent nodes.
newtype LineWidth = LineWidth (Last (Measure Double))
deriving (Typeable, Semigroup)
instance AttributeClass LineWidth

geometricScale :: Transformation t -> Double
geometricScale t = sqrt (w * h)
where
w = magnitude $ transform t unitX
h = magnitude $ transform t unitY

--instance Transformable LineWidth where
-- transform t l@(LineWidth (Last (Output w))) = l
-- transform t LineWidth (Last (Normalized w)) =

instance Default LineWidth where
def = LineWidth (Last (Output 1))

getLineWidth :: LineWidth -> (Measure Double)
getLineWidth (LineWidth (Last w)) = w

-- | Set the line (stroke) width.
lineWidth :: HasStyle a => (Measure Double) -> a -> a
lineWidth = applyAttr . LineWidth . Last

-- | Apply a 'LineWidth' attribute.
lineWidthA :: HasStyle a => LineWidth -> a -> a
lineWidthA = applyAttr

-- | A convenient synonym for 'lineWidth'.
lw :: HasStyle a => (Measure Double) -> a -> a
lw = lineWidth

-- | Convert all of the @LineWidth@ attributes in an @RTree@ to output
-- units. `w` and `h` are the width and height of the final diagram.
-- The scaling factor is the geometric mean of `h` and `w`.
toOutput :: Double -> Double -> RTree b v () -> RTree b v ()
toOutput w h tr = mapRTreeStyle f tr
where
f sty = case getAttr sty of
Just (LineWidth (Last (Output t))) -> out t sty
Just (LineWidth (Last (Normalized t))) -> out (s*t) sty
Just (LineWidth (Last (Local t))) -> out (s*t) sty
Just (LineWidth (Last (Global t))) -> out t sty
Nothing -> sty
out z st = setAttr (LineWidth (Last (Output z))) st
s = sqrt (w * h)

-- | What sort of shape should be placed at the endpoints of lines?
data LineCap = LineCapButt -- ^ Lines end precisely at their endpoints.
| LineCapRound -- ^ Lines are capped with semicircles
Expand Down
7 changes: 7 additions & 0 deletions src/Diagrams/TwoD.hs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ module Diagrams.TwoD
-- ** Adjusting size
, sized, sizedAs

-- ** Width
, LineWidth, getLineWidth, lineWidth, lineWidthA, lw

-- * Measure conversion
, toOutput

-- * Visual aids for understanding the internal model
, showOrigin
, showOrigin'
Expand All @@ -243,6 +249,7 @@ module Diagrams.TwoD

import Diagrams.TwoD.Align
import Diagrams.TwoD.Arc
import Diagrams.TwoD.Attributes
--import Diagrams.TwoD.Arrow
--import Diagrams.TwoD.Arrowheads
import Diagrams.TwoD.Combinators
Expand Down
21 changes: 11 additions & 10 deletions src/Diagrams/TwoD/Adjust.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@ module Diagrams.TwoD.Adjust

import Diagrams.Core

import Diagrams.Attributes (lineWidthA, lineColorA, lineCap
, lineJoin, lineMiterLimitA
)
import Diagrams.Util ((#))
import Diagrams.Attributes (lineColorA, lineCap
, lineJoin, lineMiterLimitA
)
import Diagrams.Util ((#))

import Diagrams.TwoD.Types (R2, p2)
import Diagrams.TwoD.Size ( size2D, center2D, SizeSpec2D(..)
, requiredScaleT, requiredScale
)
import Diagrams.TwoD.Text (fontSizeA)
import Diagrams.TwoD.Attributes (lineWidthA)
import Diagrams.TwoD.Types (R2, p2)
import Diagrams.TwoD.Size ( size2D, center2D, SizeSpec2D(..)
, requiredScaleT, requiredScale
)
import Diagrams.TwoD.Text (fontSizeA)

import Data.AffineSpace ((.-.))
import Data.AffineSpace ((.-.))
import Data.Semigroup

import Data.Default.Class
Expand Down
98 changes: 98 additions & 0 deletions src/Diagrams/TwoD/Attributes.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE TypeFamilies #-}

-----------------------------------------------------------------------------
-- |
-- Module : Diagrams.TwoD.Attributes
-- Copyright : (c) 2013 diagrams-lib team (see LICENSE)
-- License : BSD-style (see LICENSE)
-- Maintainer : diagrams-discuss@googlegroups.com
--
-- Diagrams may have /attributes/ which affect the way they are
-- rendered. This module defines /Textures/ (Gradients and Colors) in two
-- dimensions. Like the attriubtes defined in the Diagrams.Attributes module,
-- all attributes defined here use the 'Last' or 'Recommend' /semigroup/ structure.
-- 'FillColor' and 'LineColor' attributes are provided so that backends that
-- don't support gradients need not be concerned with using textures. Backends
-- should only implement color attributes or textures attributes, not both.
--
-----------------------------------------------------------------------------

module Diagrams.TwoD.Attributes (
-- ** Width
LineWidth, getLineWidth, lineWidth, lineWidthA, lw

-- * Measure conversion
, toOutput

) where

import Data.Default.Class
import Data.Semigroup
import Data.Typeable
import Data.VectorSpace (magnitude)

import Diagrams.Core
import Diagrams.Core.Compile (mapRTreeStyle)
import Diagrams.Core.Style (setAttr)
import Diagrams.Core.Types (RTree)
import Diagrams.TwoD.Types (R2)
import Diagrams.TwoD.Vector (unitX, unitY)

------------------------------------------------------------
-- Line Width -------------------------------------------------
------------------------------------------------------------

-- | Line widths specified on child nodes always override line widths
-- specified at parent nodes.
newtype LineWidth = LineWidth (Last (Measure Double))
deriving (Typeable, Semigroup)
instance AttributeClass LineWidth

type instance V LineWidth = R2

-- Estimate the line width scaling based on the geometric mean of the x and y
-- scaling of the transformation
geometricScale :: Transformation R2 -> Double -> Double

This comment has been minimized.

Copy link
@bergey

bergey Feb 13, 2014

Member

Is this meant for the same use as TwoD.Transform.avgScale?

This comment has been minimized.

Copy link
@jeffreyrosenbluth

jeffreyrosenbluth Feb 14, 2014

Author Member

Oops, I forgot we had that, I'll use avgScale instead

geometricScale t w = w * sqrt (x*y)
where
x = magnitude $ transform t unitX
y = magnitude $ transform t unitY

instance Transformable LineWidth where
transform t (LineWidth (Last (Local w))) =
LineWidth (Last (Local (geometricScale t w)))
transform _ l = l

instance Default LineWidth where
def = LineWidth (Last (Output 1))

getLineWidth :: LineWidth -> (Measure Double)
getLineWidth (LineWidth (Last w)) = w

-- | Set the line (stroke) width.
lineWidth :: (HasStyle a, V a ~ R2) => (Measure Double) -> a -> a
lineWidth = applyTAttr . LineWidth . Last

-- | Apply a 'LineWidth' attribute.
lineWidthA :: (HasStyle a, V a ~ R2) => LineWidth -> a -> a
lineWidthA = applyTAttr

-- | A convenient synonym for 'lineWidth'.
lw :: (HasStyle a, V a ~ R2) => (Measure Double) -> a -> a
lw = lineWidth

-- | Convert all of the @LineWidth@ attributes in an @RTree@ to output
-- units. `w` and `h` are the width and height of the final diagram.
-- The scaling factor is the geometric mean of `h` and `w`.
toOutput :: Double -> Double -> RTree b v () -> RTree b v ()
toOutput w h tr = mapRTreeStyle f tr
where
f sty = case getAttr sty of
Just (LineWidth (Last (Output t))) -> out t sty
Just (LineWidth (Last (Normalized t))) -> out (s*t) sty
Just (LineWidth (Last (Local t))) -> out t sty

This comment has been minimized.

Copy link
@bergey

bergey Feb 13, 2014

Member

Is toOutput meant to be called on an RTree after Local and Global Measures have been eliminated? Or is it just not fully implemented yet?

This comment has been minimized.

Copy link
@jeffreyrosenbluth

jeffreyrosenbluth Feb 14, 2014

Author Member

It's meant to be applied to an RTree before Local and Global measures are eliminated. I think Local does not need any adjustment since it is transformable and I was hoping we could loose Global. I just think that having a measure where line width decreases as the diagram size increases does not make much sense. I completely understand if you want to include Global for backward compatibility though. How would you write the toOutput case for Global?

This comment has been minimized.

Copy link
@bergey

bergey Feb 14, 2014

Member

I think Global is the Measure I'll be using for line width, if it's available. I usually want to interpret the Global size, after all Transformations, as some real-world unit like inches or mm, and use a fixed scaling factor to find the Output size. This gives SVGs that have the right dimensions when opened in other software. When I care about line width at all, I usually want to use the same real-world units.

The case for Global is analogous to adjustDiaSize2D -- check the overall size of the RTree, then find a scaling factor to fit it in Dims w h. Of course you're right about Local, I was confused.

This comment has been minimized.

Copy link
@jeffreyrosenbluth

jeffreyrosenbluth via email Feb 14, 2014

Author Member
Nothing -> sty
out z st = setAttr (LineWidth (Last (Output z))) st
s = sqrt (w * h)
11 changes: 6 additions & 5 deletions src/Diagrams/TwoD/Combinators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,21 @@ import Data.VectorSpace

import Diagrams.Core

import Diagrams.Attributes (fc, lw)
import Diagrams.Attributes (fc)
import Diagrams.BoundingBox
import Diagrams.Combinators
import Diagrams.Coordinates
import Diagrams.Path
import Diagrams.Segment
import Diagrams.TrailLike
import Diagrams.TwoD.Align
import Diagrams.TwoD.Path ()
import Diagrams.TwoD.Attributes (lw)
import Diagrams.TwoD.Path ()
import Diagrams.TwoD.Shapes
import Diagrams.TwoD.Transform (scaleX, scaleY)
import Diagrams.TwoD.Transform (scaleX, scaleY)
import Diagrams.TwoD.Types
import Diagrams.TwoD.Vector (fromDirection, unitX, unitY)
import Diagrams.Util (( # ))
import Diagrams.TwoD.Vector (fromDirection, unitX, unitY)
import Diagrams.Util (( # ))


infixl 6 ===
Expand Down
1 change: 1 addition & 0 deletions src/Diagrams/TwoD/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import Control.Lens (makeLenses, (^.))
import Diagrams.Core
import Diagrams.Attributes
import Diagrams.Path
import Diagrams.TwoD.Attributes
import Diagrams.TwoD.Ellipse
import Diagrams.TwoD.Path
import Diagrams.TwoD.Size (size2D)
Expand Down

0 comments on commit 8962a52

Please sign in to comment.