{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE FlexibleInstances #-}
module Graphics.PDF.Resources(
PDFResource(..)
, addResource
, emptyRsrc
, StrokeAlpha(..)
, FillAlpha(..)
, PdfResourceObject(..)
, resourceToDict
, emptyResource
, PDFColoredPattern
, PDFUncoloredPattern
, AnyPdfPattern
, PDFColorSpace(..)
) where
import Graphics.PDF.LowLevel.Types
import qualified Data.Map.Strict as M
newtype StrokeAlpha = StrokeAlpha Double deriving(Eq,Ord)
instance PdfResourceObject StrokeAlpha where
toRsrc (StrokeAlpha a) = AnyPdfObject . PDFDictionary . M.fromList $ [(PDFName "CA",AnyPdfObject a)]
newtype FillAlpha = FillAlpha Double deriving(Eq,Ord)
instance PdfResourceObject FillAlpha where
toRsrc (FillAlpha a) = AnyPdfObject . PDFDictionary . M.fromList $ [(PDFName "ca",AnyPdfObject a)]
class PdfResourceObject a where
toRsrc :: a -> AnyPdfObject
data PDFResource = PDFResource {
procSet :: !PDFArray
, resources :: M.Map PDFName PDFDictionary
}
emptyRsrc :: PDFResource
emptyRsrc = PDFResource [] (M.empty)
getResources :: M.Map PDFName PDFDictionary -> [(PDFName,AnyPdfObject)]
getResources = M.toList . M.map AnyPdfObject
instance PdfObject PDFResource where
toPDF r = toPDF . resourceToDict $ r
instance PdfLengthInfo PDFResource where
addResource :: PDFName
-> PDFName
-> AnyPdfObject
-> PDFResource
-> PDFResource
addResource dict name newValue r = let addValue (Just (PDFDictionary a)) = Just . PDFDictionary $ M.insert name newValue a
addValue (Nothing) = Just . PDFDictionary $ M.insert name newValue M.empty
in
r {resources = M.alter addValue dict (resources r)}
resourceToDict :: PDFResource -> PDFDictionary
resourceToDict r = PDFDictionary . M.fromList $
getResources (resources r)
emptyResource :: PDFResource -> Bool
emptyResource (PDFResource a b) = null a && M.null b
data PDFUncoloredPattern
data PDFColoredPattern
data AnyPdfPattern
data PDFColorSpace = PatternRGB deriving(Eq,Ord)
instance PdfResourceObject PDFColorSpace where
toRsrc PatternRGB = AnyPdfObject . map AnyPdfObject $ [PDFName "Pattern",PDFName "DeviceRGB"]
instance PdfObject PDFColoredPattern where
toPDF _ = noPdfObject
instance PdfLengthInfo PDFColoredPattern where
instance PdfResourceObject (PDFReference PDFColoredPattern) where
toRsrc = AnyPdfObject
instance PdfObject PDFUncoloredPattern where
toPDF _ = noPdfObject
instance PdfLengthInfo PDFUncoloredPattern where
instance PdfResourceObject (PDFReference PDFUncoloredPattern) where
toRsrc = AnyPdfObject
instance PdfObject AnyPdfPattern where
toPDF _ = noPdfObject
instance PdfLengthInfo AnyPdfPattern where
instance PdfResourceObject (PDFReference AnyPdfPattern) where
toRsrc = AnyPdfObject