module Language.LaTeX.Builder.Graphics
(
includegraphics
,IncludeGraphicsOpts(..)
,Loc, c, center, t, top, l, left, r, right, b, bottom, ba, baseline, tr, topRight
,tl, topLeft, bl, bottomLeft, br, bottomRight, baselineLeft, baselineRight
, pkg)
where
import Language.LaTeX.Types hiding (Loc)
import qualified Language.LaTeX.Builder.Internal as BI
import Language.LaTeX.Builder.MonoidUtils ((⊕))
import Control.Arrow ((***))
import Data.Maybe
import Data.String
data Loc = C
| T
| L
| R
| B
| Ba
| TR
| TL
| BL
| BR
| BaL
| BaR
deriving (Eq)
c, center, t, top, l, left, r, right, b, bottom, ba, baseline, tr, topRight,
tl, topLeft, bl, bottomLeft, br, bottomRight, baselineLeft, baselineRight :: Loc
c = C
center = C
t = T
top = T
l = L
left = L
r = R
right = R
b = B
bottom = B
ba = Ba
baseline = Ba
tr = TR
topRight = TR
tl = TL
topLeft = TL
bl = BL
bottomLeft = BL
br = BR
bottomRight = BR
baselineLeft = BaL
baselineRight = BaR
showGrLoc :: Loc -> String
showGrLoc loc = case loc of
C -> "c"
T -> "t"
L -> "l"
R -> "r"
B -> "b"
Ba -> "B"
TR -> "tr"
TL -> "tl"
BL -> "bl"
BR -> "br"
BaL -> "Bl"
BaR -> "Br"
data IncludeGraphicsOpts = IncludeGraphicsOpts
{ scale :: Rational
, width :: Maybe LatexLength
, height :: Maybe LatexLength
, totalheight :: Maybe LatexLength
, keepaspectratio :: Bool
, angle :: Rational
, origin :: Loc
, draft :: Bool
, clip :: Bool
, bb :: Maybe (Coord, Coord)
, viewport :: Maybe (Coord, Coord)
, trim :: Maybe (Coord, Coord)
, hiresbb :: Bool
}
pkg :: PackageName
pkg = PkgName "graphicx"
includegraphics :: (IncludeGraphicsOpts -> IncludeGraphicsOpts) -> FilePath -> ParItem
includegraphics f fp =
BI.parCmdArgs "includegraphics" $ opt ++ [BI.packageDependency pkg, BI.mandatoryLatexItem $ fromString fp]
where opts = includeGraphicsOpts $ f defaultOpts
opt | null opts = []
| otherwise = [BI.namedOpts opts]
defaultOpts :: IncludeGraphicsOpts
defaultOpts = IncludeGraphicsOpts
{ scale = 1
, width = Nothing
, height = Nothing
, totalheight = Nothing
, keepaspectratio = False
, angle = 0
, origin = baselineLeft
, draft = False
, clip = False
, bb = Nothing
, viewport = Nothing
, trim = Nothing
, hiresbb = False
}
includeGraphicsOpts :: IncludeGraphicsOpts -> [Named AnyItem]
includeGraphicsOpts o =
catMaybes [ f "scale" scale BI.rat
, f "width" width (BI.texLength . fromJust)
, f "height" height (BI.texLength . fromJust)
, f "totalheight" totalheight (BI.texLength . fromJust)
, f "keepaspectratio" keepaspectratio BI.bool
, f "angle" angle BI.rat
, f "origin" origin (BI.rawAnyTex . showGrLoc)
, f "draft" draft BI.bool
, f "clip" clip BI.bool
, f "bb" bb maybeCoords
, f "viewport" viewport maybeCoords
, f "trim" trim maybeCoords
, f "hiresbb" hiresbb BI.bool
]
where f :: Eq a => String -> (IncludeGraphicsOpts -> a) -> (a -> AnyItem) -> Maybe (Named AnyItem)
f name proj toAnyItem
| proj defaultOpts == proj o = Nothing
| otherwise = Just (Named name (toAnyItem $ proj o))
maybeCoords = g . (BI.coord *** BI.coord) . fromJust
g (x, y) = BI.latexItem $ BI.latexCast x ⊕ BI.rawTex " " ⊕ BI.latexCast y