{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE FlexibleContexts #-}
module Graphics.PDF.Fonts.Font(
IsFont(..)
, GlyphSize
, FontSize
, PDFFont(..)
, AnyFont(..)
, FontStructure
, EmbeddedFont
, FontData
, emptyFontStructure
, fontSize
, trueSize
, readFontData
) where
import Graphics.PDF.LowLevel.Types
import Graphics.PDF.Resources
import qualified Data.Map.Strict as M
import qualified Data.ByteString as B
import Graphics.PDF.Fonts.FontTypes
emptyFontStructure :: FontStructure
emptyFontStructure :: FontStructure
emptyFontStructure = FS { baseFont :: String
baseFont = String
""
, descent :: GlyphSize
descent = GlyphSize
0
, ascent :: GlyphSize
ascent = GlyphSize
0
, height :: GlyphSize
height = GlyphSize
0
, widthData :: Map GlyphCode GlyphSize
widthData = forall k a. Map k a
M.empty
, kernMetrics :: Map GlyphPair GlyphSize
kernMetrics = forall k a. Map k a
M.empty
, hyphen :: Maybe GlyphCode
hyphen = forall a. Maybe a
Nothing
, space :: GlyphCode
space = GlyphCode
0
, encoding :: Map Char GlyphCode
encoding = forall k a. Map k a
M.empty
, fontBBox :: [PDFFloat]
fontBBox = []
, italicAngle :: PDFFloat
italicAngle = PDFFloat
0
, capHeight :: GlyphSize
capHeight = GlyphSize
0
, fixedPitch :: Bool
fixedPitch = Bool
False
, serif :: Bool
serif = Bool
False
, symbolic :: Bool
symbolic = Bool
False
, script :: Bool
script = Bool
False
, nonSymbolic :: Bool
nonSymbolic = Bool
False
, italic :: Bool
italic = Bool
False
, allCap :: Bool
allCap = Bool
False
, smallCap :: Bool
smallCap = Bool
False
, forceBold :: Bool
forceBold = Bool
False
}
class IsFont f where
name :: f -> String
getDescent :: f -> FontSize -> PDFFloat
getHeight :: f -> FontSize -> PDFFloat
getKern :: f -> FontSize -> GlyphCode -> GlyphCode -> PDFFloat
glyphWidth :: f -> FontSize -> GlyphCode -> PDFFloat
hyphenGlyph :: f -> Maybe GlyphCode
spaceGlyph :: f -> GlyphCode
charGlyph :: f -> Char -> GlyphCode
data AnyFont = forall f. (IsFont f,PdfResourceObject f,Show f) => AnyFont f
deriving instance Show AnyFont
instance PdfResourceObject AnyFont where
toRsrc :: AnyFont -> AnyPdfObject
toRsrc (AnyFont f
f) = forall a. PdfResourceObject a => a -> AnyPdfObject
toRsrc f
f
instance IsFont AnyFont where
name :: AnyFont -> String
name (AnyFont f
f) = forall f. IsFont f => f -> String
name f
f
getDescent :: AnyFont -> Int -> PDFFloat
getDescent (AnyFont f
f) = forall f. IsFont f => f -> Int -> PDFFloat
getDescent f
f
getHeight :: AnyFont -> Int -> PDFFloat
getHeight (AnyFont f
f) = forall f. IsFont f => f -> Int -> PDFFloat
getHeight f
f
getKern :: AnyFont -> Int -> GlyphCode -> GlyphCode -> PDFFloat
getKern (AnyFont f
f) = forall f.
IsFont f =>
f -> Int -> GlyphCode -> GlyphCode -> PDFFloat
getKern f
f
glyphWidth :: AnyFont -> Int -> GlyphCode -> PDFFloat
glyphWidth (AnyFont f
f) = forall f. IsFont f => f -> Int -> GlyphCode -> PDFFloat
glyphWidth f
f
hyphenGlyph :: AnyFont -> Maybe GlyphCode
hyphenGlyph (AnyFont f
f) = forall f. IsFont f => f -> Maybe GlyphCode
hyphenGlyph f
f
spaceGlyph :: AnyFont -> GlyphCode
spaceGlyph (AnyFont f
f) = forall f. IsFont f => f -> GlyphCode
spaceGlyph f
f
charGlyph :: AnyFont -> Char -> GlyphCode
charGlyph (AnyFont f
f) = forall f. IsFont f => f -> Char -> GlyphCode
charGlyph f
f
instance Eq AnyFont where
AnyFont
a == :: AnyFont -> AnyFont -> Bool
== AnyFont
b = forall f. IsFont f => f -> String
name AnyFont
a forall a. Eq a => a -> a -> Bool
== forall f. IsFont f => f -> String
name AnyFont
b
instance Ord AnyFont where
compare :: AnyFont -> AnyFont -> Ordering
compare AnyFont
a AnyFont
b = forall a. Ord a => a -> a -> Ordering
compare (forall f. IsFont f => f -> String
name AnyFont
a) (forall f. IsFont f => f -> String
name AnyFont
b)
data PDFFont = PDFFont AnyFont FontSize deriving(PDFFont -> PDFFont -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PDFFont -> PDFFont -> Bool
$c/= :: PDFFont -> PDFFont -> Bool
== :: PDFFont -> PDFFont -> Bool
$c== :: PDFFont -> PDFFont -> Bool
Eq)
fontSize :: PDFFont -> FontSize
fontSize :: PDFFont -> Int
fontSize (PDFFont AnyFont
_ Int
s) = Int
s
instance Ord PDFFont where
compare :: PDFFont -> PDFFont -> Ordering
compare (PDFFont AnyFont
na Int
sa) (PDFFont AnyFont
nb Int
sb) = if Int
sa forall a. Eq a => a -> a -> Bool
== Int
sb then forall a. Ord a => a -> a -> Ordering
compare AnyFont
na AnyFont
nb else forall a. Ord a => a -> a -> Ordering
compare Int
sa Int
sb
trueSize :: Int -> GlyphSize -> PDFFloat
trueSize :: Int -> GlyphSize -> PDFFloat
trueSize Int
fs GlyphSize
glyphSize = (forall a b. (Integral a, Num b) => a -> b
fromIntegral GlyphSize
glyphSize forall a. Num a => a -> a -> a
* forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
fs) forall a. Fractional a => a -> a -> a
/ PDFFloat
1000.0
readFontData :: FilePath -> IO FontData
readFontData :: String -> IO FontData
readFontData String
f = do
ByteString
r <- String -> IO ByteString
B.readFile String
f
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> FontData
Type1Data ByteString
r)