-- | Function used to draw text

module Graphics.PDF.Text
 (-- * Text 
  drawText, clipText,fillAndDrawText,fillText,charSpacing,wordSpacing,textLeading
  , pdfString
 )
 where
 
import Graphics.PDF.LowLevel
import Text.Regex

leftPar = mkRegex "[(]"
rightPar = mkRegex "[)]"
backslash = mkRegex "[\\]"

-- | Create a pdf string
pdfString :: String -> [PdfString]
pdfString s = map (S . escape) . lines $ s
              where
                escape s =  replace rightPar "\\)" . replace leftPar "\\(" . replace backslash "\\\\\\\\" $ s
                replace reg repl s = subRegex reg s repl
   
-- | Stroke text at a given position and use the current font settings            
drawText :: Float -> Float -> String -> PdfCmd
drawText px py s  = (PdfText TextStroke px py (pdfString s),[])

-- | Fill and stroke text at a given position and use the current font settings            
fillAndDrawText :: Float -> Float -> String -> PdfCmd
fillAndDrawText px py s  = (PdfText TextFillStroke px py (pdfString s),[])

-- | Fill text at a given position and use the current font settings            
fillText :: Float -> Float -> String -> PdfCmd
fillText px py s  = (PdfText TextFill px py (pdfString s),[])
    
-- | Intersect clip region with text shape        
clipText :: Float -> Float -> String -> PdfCmd
clipText px py s  = (PdfText TextClip px py (pdfString s),[])
    
-- | Set char spacing value       
charSpacing :: Float -> PdfCmd
charSpacing value  = (PdfCharSpacing value,[])

-- | Set word spacing value       
wordSpacing :: Float -> PdfCmd
wordSpacing value  = (PdfWordSpacing value,[])

-- | Set text leading value (used for line separation when text is containing a \\n)     
textLeading :: Float -> PdfCmd
textLeading value  = (PdfLeading value,[])