/02-development/uni/htk/HTk/Kernel/Font.hs

https://bitbucket.org/jmelo_lyncode/thesis · Haskell · 360 lines · 189 code · 66 blank · 105 comment · 5 complexity · 2850f27039dcea15d7d18e0d2a71b6ba MD5 · raw file

  1. {-# LANGUAGE TypeSynonymInstances #-}
  2. {-# LANGUAGE FlexibleInstances #-}
  3. -- | The @module Font@ export basic types and classes concerning
  4. -- font resources.
  5. module HTk.Kernel.Font (
  6. FontDesignator(..),
  7. Font(..),
  8. XFont(..),
  9. xfont,
  10. FontFamily(..),
  11. FontWeight(..),
  12. FontSlant(..),
  13. FontWidth(..),
  14. FontSpacing(..)
  15. ) where
  16. import HTk.Kernel.GUIValue
  17. import Data.Char
  18. import Util.ExtendedPrelude(simpleSplit)
  19. -- -----------------------------------------------------------------------
  20. -- Font
  21. -- -----------------------------------------------------------------------
  22. -- | The general @Font@ datatype.
  23. newtype Font = Font String
  24. -- | The @XFont@ datatype - representing the elements of an
  25. -- X font string.
  26. data XFont =
  27. XFont { foundry :: String,
  28. family :: Maybe FontFamily,
  29. weight :: Maybe FontWeight,
  30. slant :: Maybe FontSlant,
  31. fontwidth :: Maybe FontWidth,
  32. pixels :: (Maybe Int),
  33. points :: (Maybe Int),
  34. xres :: (Maybe Int),
  35. yres :: (Maybe Int),
  36. spacing :: Maybe FontSpacing,
  37. charwidth :: (Maybe Int),
  38. charset :: Maybe String }
  39. | XFontAlias String
  40. -- -----------------------------------------------------------------------
  41. -- Font
  42. -- -----------------------------------------------------------------------
  43. -- | Datatypes that describe a font instantiate the
  44. -- @class FontDesignator@.
  45. class FontDesignator fh where
  46. toFont :: fh -> Font
  47. -- | A @Font@ object itself represents a font.
  48. instance FontDesignator Font where
  49. -- Internal.
  50. toFont = id
  51. -- | An X font string represents a font.
  52. instance FontDesignator String where
  53. -- Internal.
  54. toFont = Font
  55. -- | An @XFont@ object (see type) represents a font.
  56. instance FontDesignator XFont where
  57. -- Internal.
  58. toFont = Font . show
  59. -- | A @FontFamily@ object describes a font (default values
  60. -- set for other parameters).
  61. instance FontDesignator FontFamily where
  62. -- Internal.
  63. toFont ch = toFont (xfont {family = Just ch})
  64. -- | A tuple of @(FontFamily,Int)@ describes a font with
  65. -- its font family and points.
  66. instance FontDesignator (FontFamily,Int) where
  67. -- Internal.
  68. toFont (ch,s) = toFont (xfont {family = Just ch, points = (Just s)})
  69. -- | A tuple of @(FontFamily,FontWeight,Int)@ describes a font
  70. -- with its font family, font weight and points.
  71. instance FontDesignator (FontFamily,FontWeight,Int) where
  72. -- Internal.
  73. toFont (ch, w, po) =
  74. toFont (xfont {family = Just ch, weight = Just w, points = (Just po)})
  75. -- | A tuple of @(FontFamily,FontSlant,Int)@ describes a font
  76. -- with its font family, font slant and points.
  77. instance FontDesignator (FontFamily,FontSlant,Int) where
  78. -- Internal.
  79. toFont (ch, sl, po) =
  80. toFont (xfont {family = Just ch, slant = Just sl, points = (Just po)})
  81. -- -----------------------------------------------------------------------
  82. -- X Font Construction
  83. -- -----------------------------------------------------------------------
  84. -- | Standard font.
  85. xfont :: XFont
  86. xfont = XFont {
  87. foundry = "Adobe",
  88. family = Just Helvetica,
  89. weight = Just NormalWeight,
  90. slant = Nothing,
  91. fontwidth = Just NormalWidth,
  92. pixels = Nothing,
  93. points = Just 120,
  94. xres = Nothing,
  95. yres = Nothing,
  96. spacing = Nothing,
  97. charwidth = Nothing,
  98. charset = Nothing
  99. }
  100. -- -----------------------------------------------------------------------
  101. -- Font Instantations
  102. -- -----------------------------------------------------------------------
  103. -- | Internal.
  104. instance GUIValue Font where
  105. -- Internal.
  106. cdefault = toFont xfont
  107. -- | Internal.
  108. instance Show Font where
  109. -- Internal.
  110. showsPrec d (Font c) r = c ++ r
  111. -- | Internal.
  112. instance Read Font where
  113. -- Internal.
  114. readsPrec p str = [(Font str,[])]
  115. -- -----------------------------------------------------------------------
  116. -- XFont Instantations
  117. -- -----------------------------------------------------------------------
  118. -- | Internal.
  119. instance GUIValue XFont where
  120. -- Internal.
  121. cdefault = read "-Adobe-Helvetica-Normal-R-Normal-*-*-120-*-*-*-*-*-*"
  122. -- | Internal.
  123. instance Show XFont where
  124. -- Internal.
  125. showsPrec d c r = cshow c ++ r
  126. where
  127. cshow (XFont fo fa we sl sw pi po xr yr sp cw cs) =
  128. hy ++ fo ++ hy ++ mshow fa ++ hy ++ mshow we ++ hy ++
  129. mshow sl ++ hy ++ mshow sw ++ hy ++ mshow pi ++ hy ++
  130. mshow po ++ hy ++ mshow xr ++ hy ++ mshow yr ++ hy ++
  131. mshow sp ++ hy ++ mshow cw ++ hy ++ mshow cs ++ hy ++ "*"
  132. where hy = "-"
  133. cshow (XFontAlias str) = str
  134. -- | Internal.
  135. instance Read XFont where
  136. -- Internal.
  137. readsPrec p str = [(cread (dropWhile isSpace str),[])]
  138. where
  139. cread s@('-':str) = toXFont (simpleSplit (== '-') str)
  140. cread str = XFontAlias str
  141. toXFont (fo : fa : we : sl : sw : pi : po : xr : yr : sp : cw : cs : y : _) =
  142. XFont fo (mread fa) (mread we) (mread sl) (mread sw)
  143. (mread pi) (mread po) (mread xr) (mread yr)
  144. (mread sp) (mread cw) (mread cs)
  145. mshow :: Show a => Maybe a -> String
  146. mshow Nothing = "*"
  147. mshow (Just a) = show a
  148. mread :: Read a => String -> Maybe a
  149. mread "*" = Nothing
  150. mread str = Just (read str)
  151. -- -----------------------------------------------------------------------
  152. -- FontWeight
  153. -- -----------------------------------------------------------------------
  154. -- | The @FontWeight@ datatype.
  155. data FontWeight = NormalWeight | Medium | Bold
  156. -- | Internal.
  157. instance Read FontWeight where
  158. -- Internal.
  159. readsPrec p b =
  160. case dropWhile (isSpace) (map toLower b) of
  161. 'n':'o':'r':'m':'a':'l':xs -> [(NormalWeight,xs)]
  162. 'm':'e':'d':'i':'u':'m':xs -> [(Medium,xs)]
  163. 'b':'o':'l':'d':xs -> [(Bold,xs)]
  164. _ -> []
  165. -- | Internal.
  166. instance Show FontWeight where
  167. -- Internal.
  168. showsPrec d p r =
  169. (case p of
  170. NormalWeight -> "Normal"
  171. Medium -> "Medium"
  172. Bold -> "Bold"
  173. ) ++ r
  174. -- | Internal.
  175. instance GUIValue FontWeight where
  176. -- Internal.
  177. cdefault = NormalWeight
  178. -- -----------------------------------------------------------------------
  179. -- FontFamily
  180. -- -----------------------------------------------------------------------
  181. -- | The @FontFamily@ datatype.
  182. data FontFamily =
  183. Lucida
  184. | Times
  185. | Helvetica
  186. | Courier
  187. | Symbol
  188. | Other String
  189. -- | Internal.
  190. instance Read FontFamily where
  191. -- Internal.
  192. readsPrec p b =
  193. case dropWhile (isSpace) (map toLower b) of
  194. 'l':'u':'c':'i':'d':'a':xs -> [(Lucida,xs)]
  195. 't':'i':'m':'e':'s':xs -> [(Times,xs)]
  196. 'h':'e':'l':'v':'e':'t':'i':'c':'a':xs -> [(Helvetica,xs)]
  197. 'c':'o':'u':'r':'i':'e':'r':xs -> [(Courier,xs)]
  198. 's':'y':'m':'b':'o':'l':xs -> [(Symbol,xs)]
  199. fstr -> [(Other fstr, [])]
  200. -- | Internal.
  201. instance Show FontFamily where
  202. -- Internal.
  203. showsPrec d p r =
  204. (case p of
  205. Lucida -> "Lucida"
  206. Times -> "Times"
  207. Helvetica -> "Helvetica"
  208. Courier -> "Courier"
  209. Symbol -> "Symbol"
  210. Other fstr -> fstr
  211. ) ++ r
  212. -- | Internal.
  213. instance GUIValue FontFamily where
  214. -- Internal.
  215. cdefault = Courier
  216. -- -----------------------------------------------------------------------
  217. -- FontSlant
  218. -- -----------------------------------------------------------------------
  219. -- | The @FontSlant@ datatype.
  220. data FontSlant = Roman | Italic | Oblique
  221. -- | Internal.
  222. instance Read FontSlant where
  223. -- Internal.
  224. readsPrec p b =
  225. case dropWhile (isSpace) (map toLower b) of
  226. 'r':xs -> [(Roman,xs)]
  227. 'i':xs -> [(Italic,xs)]
  228. 'o':xs -> [(Oblique,xs)]
  229. _ -> []
  230. -- | Internal.
  231. instance Show FontSlant where
  232. -- Internal.
  233. showsPrec d p r =
  234. (case p of
  235. Roman -> "R"
  236. Italic -> "I"
  237. Oblique -> "O"
  238. ) ++ r
  239. -- | Internal.
  240. instance GUIValue FontSlant where
  241. -- Internal.
  242. cdefault = Roman
  243. -- -----------------------------------------------------------------------
  244. -- FontWidth
  245. -- -----------------------------------------------------------------------
  246. -- | The @FontWidth@ datatype.
  247. data FontWidth = NormalWidth | Condensed | Narrow
  248. -- | Internal.
  249. instance Read FontWidth where
  250. -- Internal.
  251. readsPrec p b =
  252. case dropWhile (isSpace) (map toLower b) of
  253. 'n':'o':'r':'m':'a':'l':xs -> [(NormalWidth,xs)]
  254. 'c':'o':'n':'d':'e':'n':'s':'e':'d':xs -> [(Condensed,xs)]
  255. 'n':'a':'r':'r':'o':'w':xs -> [(Narrow,xs)]
  256. _ -> []
  257. -- | Internal.
  258. instance Show FontWidth where
  259. -- Internal.
  260. showsPrec d p r =
  261. (case p of
  262. NormalWidth -> "Normal"
  263. Condensed -> "Condensed"
  264. Narrow -> "Narrow"
  265. ) ++ r
  266. -- | Internal.
  267. instance GUIValue FontWidth where
  268. -- Internal.
  269. cdefault = NormalWidth
  270. -- -----------------------------------------------------------------------
  271. -- FontSpacing
  272. -- -----------------------------------------------------------------------
  273. -- | The @FontSpacing@ datatype.
  274. data FontSpacing = MonoSpace | Proportional
  275. -- | Internal.
  276. instance Read FontSpacing where
  277. -- Internal.
  278. readsPrec p b =
  279. case dropWhile (isSpace) (map toLower b) of
  280. 'm':xs -> [(MonoSpace,xs)]
  281. 'p':xs -> [(Proportional,xs)]
  282. _ -> []
  283. -- | Internal.
  284. instance Show FontSpacing where
  285. -- Internal.
  286. showsPrec d p r =
  287. (case p of
  288. MonoSpace -> "M"
  289. Proportional -> "P"
  290. ) ++ r
  291. -- | Internal.
  292. instance GUIValue FontSpacing where
  293. -- Internal.
  294. cdefault = MonoSpace