module Codec.Xlsx.Types.RichText (
RichTextRun(..)
, RunProperties(..)
, applyRunProperties
, richTextRunProperties
, richTextRunText
, runPropertiesBold
, runPropertiesCharset
, runPropertiesColor
, runPropertiesCondense
, runPropertiesExtend
, runPropertiesFontFamily
, runPropertiesItalic
, runPropertiesOutline
, runPropertiesFont
, runPropertiesScheme
, runPropertiesShadow
, runPropertiesStrikeThrough
, runPropertiesSize
, runPropertiesUnderline
, runPropertiesVertAlign
) where
import Control.Lens hiding (element)
import Control.Monad
import Data.Default
import Data.Maybe (catMaybes)
import Data.Text (Text)
import Text.XML
import Text.XML.Cursor
import qualified Data.Map as Map
import Codec.Xlsx.Parser.Internal
import Codec.Xlsx.Types.StyleSheet
import Codec.Xlsx.Writer.Internal
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative
import Data.Monoid
#endif
data RichTextRun = RichTextRun {
_richTextRunProperties :: Maybe RunProperties
, _richTextRunText :: Text
}
deriving (Show, Eq, Ord)
data RunProperties = RunProperties {
_runPropertiesBold :: Maybe Bool
, _runPropertiesCharset :: Maybe Int
, _runPropertiesColor :: Maybe Color
, _runPropertiesCondense :: Maybe Bool
, _runPropertiesExtend :: Maybe Bool
, _runPropertiesFontFamily :: Maybe FontFamily
, _runPropertiesItalic :: Maybe Bool
, _runPropertiesOutline :: Maybe Bool
, _runPropertiesFont :: Maybe Text
, _runPropertiesScheme :: Maybe FontScheme
, _runPropertiesShadow :: Maybe Bool
, _runPropertiesStrikeThrough :: Maybe Bool
, _runPropertiesSize :: Maybe Double
, _runPropertiesUnderline :: Maybe FontUnderline
, _runPropertiesVertAlign :: Maybe FontVerticalAlignment
}
deriving (Show, Eq, Ord)
makeLenses ''RichTextRun
makeLenses ''RunProperties
instance Default RichTextRun where
def = RichTextRun {
_richTextRunProperties = Nothing
, _richTextRunText = ""
}
instance Default RunProperties where
def = RunProperties {
_runPropertiesBold = Nothing
, _runPropertiesCharset = Nothing
, _runPropertiesColor = Nothing
, _runPropertiesCondense = Nothing
, _runPropertiesExtend = Nothing
, _runPropertiesFontFamily = Nothing
, _runPropertiesItalic = Nothing
, _runPropertiesOutline = Nothing
, _runPropertiesFont = Nothing
, _runPropertiesScheme = Nothing
, _runPropertiesShadow = Nothing
, _runPropertiesStrikeThrough = Nothing
, _runPropertiesSize = Nothing
, _runPropertiesUnderline = Nothing
, _runPropertiesVertAlign = Nothing
}
instance ToElement RichTextRun where
toElement nm RichTextRun{..} = Element {
elementName = nm
, elementAttributes = Map.empty
, elementNodes = map NodeElement . catMaybes $ [
toElement "rPr" <$> _richTextRunProperties
, Just $ elementContentPreserved "t" _richTextRunText
]
}
instance ToElement RunProperties where
toElement nm RunProperties{..} = Element {
elementName = nm
, elementAttributes = Map.empty
, elementNodes = map NodeElement . catMaybes $ [
elementValue "rFont" <$> _runPropertiesFont
, elementValue "charset" <$> _runPropertiesCharset
, elementValue "family" <$> _runPropertiesFontFamily
, elementValue "b" <$> _runPropertiesBold
, elementValue "i" <$> _runPropertiesItalic
, elementValue "strike" <$> _runPropertiesStrikeThrough
, elementValue "outline" <$> _runPropertiesOutline
, elementValue "shadow" <$> _runPropertiesShadow
, elementValue "condense" <$> _runPropertiesCondense
, elementValue "extend" <$> _runPropertiesExtend
, toElement "color" <$> _runPropertiesColor
, elementValue "sz" <$> _runPropertiesSize
, elementValue "u" <$> _runPropertiesUnderline
, elementValue "vertAlign" <$> _runPropertiesVertAlign
, elementValue "scheme" <$> _runPropertiesScheme
]
}
instance FromCursor RichTextRun where
fromCursor cur = do
_richTextRunText <- cur $/ element (n"t") &/ content
_richTextRunProperties <- maybeFromElement (n"rPr") cur
return RichTextRun{..}
instance FromCursor RunProperties where
fromCursor cur = do
_runPropertiesFont <- maybeElementValue (n"rFont") cur
_runPropertiesCharset <- maybeElementValue (n"charset") cur
_runPropertiesFontFamily <- maybeElementValue (n"family") cur
_runPropertiesBold <- maybeElementValue (n"b") cur
_runPropertiesItalic <- maybeElementValue (n"i") cur
_runPropertiesStrikeThrough <- maybeElementValue (n"strike") cur
_runPropertiesOutline <- maybeElementValue (n"outline") cur
_runPropertiesShadow <- maybeElementValue (n"shadow") cur
_runPropertiesCondense <- maybeElementValue (n"condense") cur
_runPropertiesExtend <- maybeElementValue (n"extend") cur
_runPropertiesColor <- maybeFromElement (n"color") cur
_runPropertiesSize <- maybeElementValue (n"sz") cur
_runPropertiesUnderline <- maybeElementValue (n"u") cur
_runPropertiesVertAlign <- maybeElementValue (n"vertAlign") cur
_runPropertiesScheme <- maybeElementValue (n"scheme") cur
return RunProperties{..}
instance Monoid RunProperties where
mempty = def
a `mappend` b = RunProperties {
_runPropertiesBold = override _runPropertiesBold
, _runPropertiesCharset = override _runPropertiesCharset
, _runPropertiesColor = override _runPropertiesColor
, _runPropertiesCondense = override _runPropertiesCondense
, _runPropertiesExtend = override _runPropertiesExtend
, _runPropertiesFontFamily = override _runPropertiesFontFamily
, _runPropertiesItalic = override _runPropertiesItalic
, _runPropertiesOutline = override _runPropertiesOutline
, _runPropertiesFont = override _runPropertiesFont
, _runPropertiesScheme = override _runPropertiesScheme
, _runPropertiesShadow = override _runPropertiesShadow
, _runPropertiesStrikeThrough = override _runPropertiesStrikeThrough
, _runPropertiesSize = override _runPropertiesSize
, _runPropertiesUnderline = override _runPropertiesUnderline
, _runPropertiesVertAlign = override _runPropertiesVertAlign
}
where
override :: (RunProperties -> Maybe x) -> Maybe x
override f = f b `mplus` f a
applyRunProperties :: RunProperties -> RichTextRun -> RichTextRun
applyRunProperties p (RichTextRun Nothing t) = RichTextRun (Just p) t
applyRunProperties p (RichTextRun (Just p') t) = RichTextRun (Just (p `mappend` p')) t