module Wumpus.Core.PictureInternal
(
Picture(..)
, DPicture
, Primitive(..)
, DPrimitive
, Path(..)
, DPath
, PathSegment(..)
, DPathSegment
, Label(..)
, DLabel
, PrimEllipse(..)
, DPrimEllipse
, PrimCTM
, PathProps
, LabelProps
, EllipseProps
, DrawPath(..)
, DrawEllipse(..)
, Locale
, PSUnit(..)
, translatePrimitive
, rotatePrimitive
, scalePrimitive
, uniformScalePrimitive
, identityCTM
, scaleCTM
, matrixRepCTM
, translMatrixRepCTM
, mapLocale
, movePic
, moveLocale
, extractFrame
, repositionProperties
, ellipseControlPoints
) where
import Wumpus.Core.AffineTrans
import Wumpus.Core.BoundingBox
import Wumpus.Core.FontSize
import Wumpus.Core.Geometry
import Wumpus.Core.GraphicsState
import Wumpus.Core.OneList
import Wumpus.Core.TextEncodingInternal
import Wumpus.Core.Utils
import Data.AffineSpace
import Data.Semigroup
import Text.PrettyPrint.Leijen
data Picture u = PicBlank (Locale u)
| Single (Locale u) (Primitive u)
| Picture (Locale u) (OneList (Picture u))
| Clip (Locale u) (Path u) (Picture u)
deriving (Eq,Show)
type DPicture = Picture Double
data Primitive u = PPath PathProps (Path u)
| PLabel LabelProps (Label u)
| PEllipse EllipseProps (PrimEllipse u)
deriving (Eq,Show)
type DPrimitive = Primitive Double
data Path u = Path (Point2 u) [PathSegment u]
deriving (Eq,Show)
type DPath = Path Double
data PathSegment u = PCurveTo (Point2 u) (Point2 u) (Point2 u)
| PLineTo (Point2 u)
deriving (Eq,Show)
type DPathSegment = PathSegment Double
data Label u = Label
{ label_bottom_left :: Point2 u
, label_text :: EncodedText
, label_ctm :: PrimCTM u
}
deriving (Eq,Show)
type DLabel = Label Double
data PrimEllipse u = PrimEllipse
{ ellipse_center :: Point2 u
, ellipse_half_width :: u
, ellipse_half_height :: u
, ellipse_ctm :: PrimCTM u
}
deriving (Eq,Show)
type DPrimEllipse = PrimEllipse Double
data PrimCTM u = PrimCTM
{ ctm_scale_x :: u
, ctm_scale_y :: u
, ctm_rotation :: Radian
}
deriving (Eq,Show)
data DrawPath = CFill | CStroke [StrokeAttr] | OStroke [StrokeAttr]
deriving (Eq,Show)
data DrawEllipse = EFill | EStroke [StrokeAttr]
deriving (Eq,Show)
type PathProps = (PSRgb, DrawPath)
type LabelProps = (PSRgb, FontAttr)
type EllipseProps = (PSRgb, DrawEllipse)
type Locale u = (Frame2 u, BoundingBox u)
instance (Num u, Pretty u) => Pretty (Picture u) where
pretty (PicBlank m) = text "*BLANK*" <+> ppLocale m
pretty (Single m prim) = ppLocale m <$> indent 2 (pretty prim)
pretty (Picture m ones) =
ppLocale m <$> indent 2 (list $ toListF pretty ones)
pretty (Clip m cpath p) =
text "Clip:" <+> ppLocale m <$> indent 2 (pretty cpath)
<$> indent 2 (pretty p)
ppLocale :: (Num u, Pretty u) => Locale u -> Doc
ppLocale (fr,bb) = align (ppfr <$> pretty bb) where
ppfr = if standardFrame fr then text "*std-frame*" else pretty fr
instance Pretty u => Pretty (Primitive u) where
pretty (PPath _ p) = pretty "path:" <+> pretty p
pretty (PLabel _ lbl) = pretty lbl
pretty (PEllipse _ e) = pretty e
instance Pretty u => Pretty (Path u) where
pretty (Path pt ps) = pretty pt <> hcat (map pretty ps)
instance Pretty u => Pretty (PathSegment u) where
pretty (PCurveTo p1 p2 p3) = text ".*" <> pretty p1 <> text ",," <> pretty p2
<> text "*." <> pretty p3
pretty (PLineTo pt) = text "--" <> pretty pt
instance Pretty u => Pretty (Label u) where
pretty (Label pt s ctm) = dquotes (pretty s) <> char '@' <> pretty pt
<+> pretty ctm
instance Pretty u => Pretty (PrimEllipse u) where
pretty (PrimEllipse ctr w h ctm) = pretty "ellipse" <+> pretty ctr
<+> text "w:" <> pretty w
<+> text "h:" <> pretty h
<+> pretty ctm
instance Pretty u => Pretty (PrimCTM u) where
pretty (PrimCTM x y ang) =
braces (pretty x <> comma <+> pretty y <> comma <+> pretty ang)
instance Semigroup (Path u) where
Path st xs `append` Path st' xs' = Path st (xs ++ (PLineTo st' : xs'))
instance Pointwise (Path u) where
type Pt (Path u) = Point2 u
pointwise f (Path st xs) = Path (f st) (map (pointwise f) xs)
instance Pointwise (PathSegment u) where
type Pt (PathSegment u) = Point2 u
pointwise f (PLineTo p) = PLineTo (f p)
pointwise f (PCurveTo p1 p2 p3) = PCurveTo (f p1) (f p2) (f p3)
type instance DUnit (Picture u) = u
type instance DUnit (Primitive u) = u
type instance DUnit (Path u) = u
type instance DUnit (PrimEllipse u) = u
instance (Num u, Ord u) => Transform (Picture u) where
transform ctm pic = trafoPicture (transform ctm) (transform ctm) pic
instance (Floating u, Real u) => Rotate (Picture u) where
rotate = rotatePicture
instance (Floating u, Real u) => RotateAbout (Picture u) where
rotateAbout = rotatePictureAbout
instance (Num u, Ord u) => Scale (Picture u) where
scale = scalePicture
instance (Num u, Ord u) => Translate (Picture u) where
translate = translatePicture
rotatePrimitive :: (Real u, Floating u)
=> Radian -> Primitive u -> Primitive u
rotatePrimitive ang (PPath a path) = PPath a $ rotatePath ang path
rotatePrimitive ang (PLabel a lbl) = PLabel a $ rotateLabel ang lbl
rotatePrimitive ang (PEllipse a ell) = PEllipse a $ rotateEllipse ang ell
scalePrimitive :: Num u => u -> u -> Primitive u -> Primitive u
scalePrimitive x y (PPath a path) = PPath a $ scalePath x y path
scalePrimitive x y (PLabel a lbl) = PLabel a $ scaleLabel x y lbl
scalePrimitive x y (PEllipse a ell) = PEllipse a $ scaleEllipse x y ell
uniformScalePrimitive :: Num u => u -> Primitive u -> Primitive u
uniformScalePrimitive d = scalePrimitive d d
translatePrimitive :: Num u => u -> u -> Primitive u -> Primitive u
translatePrimitive x y (PPath a path) = PPath a $ translatePath x y path
translatePrimitive x y (PLabel a lbl) = PLabel a $ translateLabel x y lbl
translatePrimitive x y (PEllipse a ell) = PEllipse a $ translateEllipse x y ell
rotatePicture :: (Real u, Floating u) => Radian -> Picture u -> Picture u
rotatePicture ang = trafoPicture (rotate ang) (rotate ang)
rotatePictureAbout :: (Real u, Floating u)
=> Radian -> Point2 u -> Picture u -> Picture u
rotatePictureAbout ang pt =
trafoPicture (rotateAbout ang pt) (rotateAbout ang pt)
scalePicture :: (Num u, Ord u) => u -> u -> Picture u -> Picture u
scalePicture x y = trafoPicture (scale x y) (scale x y)
translatePicture :: (Num u, Ord u) => u -> u -> Picture u -> Picture u
translatePicture x y = trafoPicture (translate x y) (translate x y)
trafoPicture :: (Num u, Ord u)
=> (Point2 u -> Point2 u)
-> (Vec2 u -> Vec2 u)
-> Picture u
-> Picture u
trafoPicture fp fv =
mapLocale $ \(frm,bb) -> (trafoFrame fp fv frm, trafoBBox fp bb)
trafoFrame :: Num u
=> (Point2 u -> Point2 u)
-> (Vec2 u -> Vec2 u)
-> Frame2 u
-> Frame2 u
trafoFrame fp fv (Frame2 e0 e1 o) = Frame2 (fv e0) (fv e1) (fp o)
trafoBBox :: (Num u, Ord u)
=> (Point2 u -> Point2 u) -> BoundingBox u -> BoundingBox u
trafoBBox fp bb = traceBoundary $ map fp $ [bl,br,tl,tr]
where
(bl,br,tr,tl) = corners bb
rotatePath :: (Real u, Floating u) => Radian -> Path u -> Path u
rotatePath ang (Path start xs) =
Path start $ map (pointwise (rotateAbout ang start)) xs
scalePath :: Num u => u -> u -> Path u -> Path u
scalePath x y (Path pt xs) = Path pt (map (pointwise fn) xs)
where
fn p1 = let dif = p1 .-. pt in pt .+^ (scale x y $ dif)
translatePath :: Num u => u -> u -> Path u -> Path u
translatePath x y = pointwise (translate x y)
identityCTM :: Num u => PrimCTM u
identityCTM = PrimCTM { ctm_scale_x = 1, ctm_scale_y = 1, ctm_rotation = 0 }
scaleCTM :: Num u => u -> u -> PrimCTM u -> PrimCTM u
scaleCTM x1 y1 (PrimCTM x y ang) = PrimCTM (x1*x) (y1*y) ang
rotateCTM :: Radian -> PrimCTM u -> PrimCTM u
rotateCTM ang1 (PrimCTM x y ang) = PrimCTM x y (circularModulo $ ang1+ang)
matrixRepCTM :: (Floating u, Real u) => PrimCTM u -> Matrix3'3 u
matrixRepCTM (PrimCTM x y ang) =
rotationMatrix (circularModulo ang) * scalingMatrix x y
translMatrixRepCTM :: (Floating u, Real u)
=> u -> u -> PrimCTM u -> Matrix3'3 u
translMatrixRepCTM x y ctm = translationMatrix x y * matrixRepCTM ctm
rotateLabel :: Radian -> Label u -> Label u
rotateLabel ang (Label pt txt ctm) = Label pt txt (rotateCTM ang ctm)
scaleLabel :: Num u => u -> u -> Label u -> Label u
scaleLabel x y (Label pt txt ctm) = Label pt txt (scaleCTM x y ctm)
translateLabel :: Num u => u -> u -> Label u -> Label u
translateLabel x y (Label pt txt ctm) = Label (translate x y pt) txt ctm
rotateEllipse :: Radian -> PrimEllipse u -> PrimEllipse u
rotateEllipse ang (PrimEllipse pt hw hh ctm) =
PrimEllipse pt hw hh (rotateCTM ang ctm)
scaleEllipse :: Num u => u -> u -> PrimEllipse u -> PrimEllipse u
scaleEllipse x y (PrimEllipse pt hw hh ctm) =
PrimEllipse (translate x y pt) hw hh (scaleCTM x y ctm)
translateEllipse :: Num u => u -> u -> PrimEllipse u -> PrimEllipse u
translateEllipse x y (PrimEllipse pt hw hh ctm) =
PrimEllipse (translate x y pt) hw hh ctm
instance Boundary (Picture u) where
boundary (PicBlank (_,bb)) = bb
boundary (Single (_,bb) _) = bb
boundary (Picture (_,bb) _) = bb
boundary (Clip (_,bb) _ _) = bb
instance (Num u, Ord u) => Boundary (Path u) where
boundary (Path st xs) = traceBoundary $ st : foldr f [] xs where
f (PLineTo p1) acc = p1 : acc
f (PCurveTo p1 p2 p3) acc = p1 : p2 : p3 : acc
instance (Real u, Floating u) => Boundary (Primitive u) where
boundary (PPath _ p) = boundary p
boundary (PLabel (_,a) l) = primLabelBoundary a l
boundary (PEllipse _ e) = boundary e
primLabelBoundary :: (Floating u, Real u)
=> FontAttr -> Label u -> BoundingBox u
primLabelBoundary attr (Label (P2 x y) xs ctm) =
retraceBoundary (disp . (m33 *#)) untraf_bbox
where
disp = (.+^ V2 x y)
m33 = matrixRepCTM ctm
untraf_bbox = textBounds (font_size attr) zeroPt char_count
char_count = textLength xs
instance (Real u, Floating u) => Boundary (PrimEllipse u) where
boundary = ellipseBoundary
ellipseBoundary :: (Real u, Floating u) => PrimEllipse u -> BoundingBox u
ellipseBoundary = traceBoundary . ellipseControlPoints
mapLocale :: (Locale u -> Locale u) -> Picture u -> Picture u
mapLocale f (PicBlank m) = PicBlank (f m)
mapLocale f (Single m prim) = Single (f m) prim
mapLocale f (Picture m ones) = Picture (f m) ones
mapLocale f (Clip m x p) = Clip (f m) x p
movePic :: Num u => Vec2 u -> Picture u -> Picture u
movePic v = mapLocale (moveLocale v)
moveLocale :: Num u => Vec2 u -> Locale u -> Locale u
moveLocale v (fr,bb) = (displaceOrigin v fr, pointwise (.+^ v) bb)
extractFrame :: Num u => Picture u -> Frame2 u
extractFrame (PicBlank (fr,_)) = fr
extractFrame (Single (fr,_) _) = fr
extractFrame (Picture (fr,_) _) = fr
extractFrame (Clip (fr,_) _ _) = fr
repositionProperties :: (Num u, Ord u)
=> Picture u -> (BoundingBox u, Maybe (Vec2 u))
repositionProperties = fn . boundary where
fn bb@(BBox (P2 llx lly) (P2 urx ury))
| llx < 4 || lly < 4 = (BBox ll ur, Just $ V2 x y)
| otherwise = (bb, Nothing)
where
x = 4 llx
y = 4 lly
ll = P2 (llx+x) (lly+y)
ur = P2 (urx+x) (ury+y)
ellipseControlPoints :: (Floating u, Real u)
=> PrimEllipse u -> [Point2 u]
ellipseControlPoints (PrimEllipse (P2 x y) hw hh ctm) =
map (disp . (new_mtrx *#)) circ
where
disp = (.+^ V2 x y)
(radius,(dx,dy)) = circleScalingProps hw hh
new_mtrx = matrixRepCTM $ scaleCTM dx dy ctm
circ = bezierCircle 1 radius (P2 0 0)
circleScalingProps :: (Fractional u, Ord u) => u -> u -> (u,(u,u))
circleScalingProps hw hh = (radius, (dx,dy))
where
radius = max hw hh
(dx,dy) = if radius == hw then (1, rescale (0,hw) (0,1) hh)
else (rescale (0,hh) (0,1) hw, 1)