module Wumpus.Basic.System.FontLoader.Base.Datatypes
(
AfmUnit
, afmValue
, afmUnitScale
, PSCharCode
, PSEncodingScheme
, AfmBoundingBox
, AfmKey
, GlobalInfo
, AfmFile(..)
, AfmGlyphMetrics(..)
, MonospaceDefaults(..)
, FontProps(..)
, buildMetricsOps
) where
import Wumpus.Basic.Kernel.Base.GlyphMetrics
import Wumpus.Core
import qualified Data.IntMap as IM
import qualified Data.Map as M
newtype AfmUnit = AfmUnit { getAfmUnit :: Double }
deriving (Eq,Ord,Num,Floating,Fractional,Real,RealFrac,RealFloat)
instance Show AfmUnit where
showsPrec p d = showsPrec p (getAfmUnit d)
afmValue :: FromPtSize u => AfmUnit -> PtSize -> u
afmValue u pt = fromPtSize $ (realToFrac $ getAfmUnit u) * (pt / 1000)
afmUnitScale :: AfmUnit -> PtSize
afmUnitScale u = (realToFrac $ getAfmUnit u / 1000)
type PSCharCode = Int
type PSEncodingScheme = String
type AfmBoundingBox = BoundingBox AfmUnit
type AfmKey = String
type GlobalInfo = M.Map AfmKey String
data AfmFile = AfmFile
{ afm_encoding :: Maybe String
, afm_letter_bbox :: Maybe AfmBoundingBox
, afm_cap_height :: Maybe AfmUnit
, afm_descender :: Maybe AfmUnit
, afm_glyph_metrics :: [AfmGlyphMetrics]
}
deriving (Show)
data AfmGlyphMetrics = AfmGlyphMetrics
{ afm_char_code :: !PSCharCode
, afm_width_vector :: !(Vec2 AfmUnit)
, afm_char_name :: !String
}
deriving (Eq,Show)
data MonospaceDefaults cu = MonospaceDefaults
{ default_letter_bbox :: BoundingBox cu
, default_cap_height :: cu
, default_descender :: cu
, default_char_width :: Vec2 cu
}
deriving (Eq,Show)
data FontProps cu = FontProps
{ fp_bounding_box :: BoundingBox cu
, fp_default_adv_vec :: Vec2 cu
, fp_adv_vecs :: IM.IntMap (Vec2 cu)
, fp_cap_height :: cu
, fp_descender :: cu
}
buildMetricsOps :: (cu -> PtSize) -> FontProps cu -> MetricsOps
buildMetricsOps fn font@(FontProps { fp_bounding_box = BBox ll ur
, fp_default_adv_vec = V2 vx vy }) =
MetricsOps
{ get_bounding_box = \sz -> BBox (scalePt sz ll) (scalePt sz ur)
, get_cw_table = \sz i ->
maybe (defaultAV sz) (scaleVec sz) $ IM.lookup i (fp_adv_vecs font)
, get_cap_height = \sz -> upscale sz (fn $ fp_cap_height font)
, get_descender = \sz -> upscale sz (fn $ fp_descender font)
}
where
upscale sz d = fromPtSize $ sz * d
defaultAV sz = V2 (upscale sz $ fn vx) (upscale sz $ fn vy)
scalePt sz (P2 cx cy) = P2 (upscale sz $ fn cx) (upscale sz $ fn cy)
scaleVec sz (V2 cx cy) = V2 (upscale sz $ fn cx) (upscale sz $ fn cy)