| 1 |
{-# LANGUAGE ConstraintKinds #-} |
| 2 |
{-# LANGUAGE DeriveDataTypeable #-} |
| 3 |
{-# LANGUAGE FlexibleContexts #-} |
| 4 |
{-# LANGUAGE FlexibleInstances #-} |
| 5 |
{-# LANGUAGE GeneralizedNewtypeDeriving #-} |
| 6 |
{-# LANGUAGE MultiParamTypeClasses #-} |
| 7 |
{-# LANGUAGE TypeFamilies #-} |
| 8 |
{-# LANGUAGE TypeOperators #-} |
| 9 |
{-# LANGUAGE UndecidableInstances #-} |
| 10 |
|
| 11 |
----------------------------------------------------------------------------- |
| 12 |
-- | |
| 13 |
-- Module : Diagrams.TwoD.Text |
| 14 |
-- Copyright : (c) 2011-2015 diagrams-lib team (see LICENSE) |
| 15 |
-- License : BSD-style (see LICENSE) |
| 16 |
-- Maintainer : [email protected] |
| 17 |
-- |
| 18 |
-- Very basic text primitives along with associated attributes. |
| 19 |
-- |
| 20 |
----------------------------------------------------------------------------- |
| 21 |
|
| 22 |
module Diagrams.TwoD.Text ( |
| 23 |
-- * Creating text diagrams |
| 24 |
Text(..), TextAlignment(..) |
| 25 |
, text, topLeftText, alignedText, baselineText |
| 26 |
, mkText, mkText' |
| 27 |
|
| 28 |
-- * Text attributes |
| 29 |
|
| 30 |
-- ** Font family |
| 31 |
, Font(..), _Font |
| 32 |
, getFont, font, _font |
| 33 |
|
| 34 |
-- ** Font size |
| 35 |
, FontSize(..), _FontSize |
| 36 |
, fontSize, recommendFontSize |
| 37 |
, fontSizeN, fontSizeO, fontSizeL, fontSizeG |
| 38 |
, getFontSize, fontSizeM |
| 39 |
, _fontSizeR, _fontSize, _fontSizeU |
| 40 |
|
| 41 |
-- ** Font slant |
| 42 |
, FontSlant(..) |
| 43 |
, getFontSlant, fontSlant, italic, oblique, _fontSlant |
| 44 |
|
| 45 |
-- ** Font weight |
| 46 |
, FontWeight(..) |
| 47 |
, getFontWeight, fontWeight, bold, bolder, lighter, _fontWeight |
| 48 |
, thinWeight, ultraLight, light, mediumWeight, heavy, semiBold, ultraBold |
| 49 |
|
| 50 |
) where |
| 51 |
|
| 52 |
import Control.Lens hiding (transform) |
| 53 |
import Diagrams.Attributes (committed) |
| 54 |
import Diagrams.Core |
| 55 |
import Diagrams.Core.Envelope (pointEnvelope) |
| 56 |
import Diagrams.TwoD.Attributes (recommendFillColor) |
| 57 |
import Diagrams.TwoD.Types |
| 58 |
|
| 59 |
import Data.Colour hiding (over) |
| 60 |
import Data.Default |
| 61 |
import Data.Monoid.Recommend |
| 62 |
import Data.Semigroup |
| 63 |
import Data.Typeable |
| 64 |
|
| 65 |
import Linear.Affine |
| 66 |
|
| 67 |
------------------------------------------------------------ |
| 68 |
-- Text diagrams |
| 69 |
------------------------------------------------------------ |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
data Text n = Text (T2 n) (TextAlignment n) String |
| 79 |
deriving Typeable |
| 80 |
|
| 81 |
type instance V (Text n) = V2 |
| 82 |
type instance N (Text n) = n |
| 83 |
|
| 84 |
instance Floating n => Transformable (Text n) where |
| 85 |
transform t (Text tt a s) = Text (t <> tt <> t') a s |
| 86 |
where t' = scaling (1 / avgScale t) |
| 87 |
|
| 88 |
instance Floating n => HasOrigin (Text n) where |
| 89 |
moveOriginTo p = translate (origin .-. p) |
| 90 |
|
| 91 |
instance Floating n => Renderable (Text n) NullBackend where |
| 92 |
render _ _ = mempty |
| 93 |
|
| 94 |
|
| 95 |
data TextAlignment n = BaselineText | BoxAlignedText n n |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
mkText :: (TypeableFloat n, Renderable (Text n) b) |
| 100 |
=> TextAlignment n -> String -> QDiagram b V2 n Any |
| 101 |
mkText a = recommendFillColor black |
| 102 |
-- See Note [recommendFillColor] |
| 103 |
. recommendFontSize (local 1) |
| 104 |
-- See Note [recommendFontSize] |
| 105 |
. mkText' a |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
mkText' :: (TypeableFloat n, Renderable (Text n) b) |
| 111 |
=> TextAlignment n -> String -> QDiagram b V2 n Any |
| 112 |
mkText' a t = mkQD (Prim $ Text mempty a t) |
| 113 |
(pointEnvelope origin) |
| 114 |
mempty |
| 115 |
mempty |
| 116 |
mempty |
| 117 |
|
| 118 |
|
| 119 |
-- ~~~~ Note [recommendFillColor] |
| 120 |
|
| 121 |
-- The reason we "recommend" a fill color of black instead of setting |
| 122 |
-- it directly (or instead of simply not specifying a fill color at |
| 123 |
-- all) was originally to support the SVG backend, though it is |
| 124 |
-- actually in some sense the "right thing" to do, and other backends |
| 125 |
-- we add later may conceivably need it as well. The cairo backend |
| 126 |
-- defaults happen to be to use a transparent fill for paths and a |
| 127 |
-- black fill for text. The SVG standard, however, specifies a |
| 128 |
-- default fill of black for everything (both text and paths). In |
| 129 |
-- order to correctly render paths with no fill set, the SVG backend |
| 130 |
-- must therefore explicitly set the fill to transparent -- but this |
| 131 |
-- meant that it was also drawing text with a transparent fill. The |
| 132 |
-- solution is that we now explicitly inform all backends that the |
| 133 |
-- *default* ("recommended") fill color for text should be black; an |
| 134 |
-- absence of fill specification now consistently means to use a |
| 135 |
-- "transparent" fill no matter what the primitive. The reason we |
| 136 |
-- need the special recommend/commit distinction is because if the |
| 137 |
-- user explicitly sets a fill color later it should override this |
| 138 |
-- recommendation; normally, the innermost occurrence of an attribute |
| 139 |
-- would override all outer occurrences. |
| 140 |
|
| 141 |
-- ~~~~ Note [recommendFontSize] |
| 142 |
-- The reason we "recommend" a font size is so any local scales get |
| 143 |
-- recorded. |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
text :: (TypeableFloat n, Renderable (Text n) b) => String -> QDiagram b V2 n Any |
| 151 |
text = alignedText 0.5 0.5 |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
topLeftText :: (TypeableFloat n, Renderable (Text n) b) => String -> QDiagram b V2 n Any |
| 159 |
topLeftText = alignedText 0 1 |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
alignedText :: (TypeableFloat n, Renderable (Text n) b) |
| 172 |
=> n -> n -> String -> QDiagram b V2 n Any |
| 173 |
alignedText w h = mkText (BoxAlignedText w h) |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
baselineText :: (TypeableFloat n, Renderable (Text n) b) |
| 182 |
=> String -> QDiagram b V2 n Any |
| 183 |
baselineText = mkText BaselineText |
| 184 |
|
| 185 |
------------------------------------------------------------ |
| 186 |
-- Text attributes |
| 187 |
------------------------------------------------------------ |
| 188 |
|
| 189 |
-------------------------------------------------- |
| 190 |
-- Font family |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
newtype Font = Font (Last String) |
| 195 |
deriving (Typeable, Semigroup, Eq) |
| 196 |
|
| 197 |
_Font :: Iso' Font String |
| 198 |
_Font = iso getFont (Font . Last) |
| 199 |
|
| 200 |
instance AttributeClass Font |
| 201 |
|
| 202 |
|
| 203 |
getFont :: Font -> String |
| 204 |
getFont (Font (Last f)) = f |
| 205 |
|
| 206 |
|
| 207 |
font :: HasStyle a => String -> a -> a |
| 208 |
font = applyAttr . Font . Last |
| 209 |
|
| 210 |
|
| 211 |
_font :: Lens' (Style v n) (Maybe String) |
| 212 |
_font = atAttr . mapping _Font |
| 213 |
|
| 214 |
-------------------------------------------------- |
| 215 |
-- Font size |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
newtype FontSize n = FontSize (Recommend (Last n)) |
| 220 |
deriving (Typeable, Semigroup) |
| 221 |
|
| 222 |
-- not sure why this can't be derived |
| 223 |
instance Functor FontSize where |
| 224 |
fmap f (FontSize (Recommend (Last a))) = FontSize (Recommend (Last (f a))) |
| 225 |
fmap f (FontSize (Commit (Last a))) = FontSize (Commit (Last (f a))) |
| 226 |
|
| 227 |
_FontSize :: Iso' (FontSize n) (Recommend n) |
| 228 |
_FontSize = iso getter setter |
| 229 |
where getter (FontSize (Recommend (Last a))) = Recommend a |
| 230 |
getter (FontSize (Commit (Last a))) = Commit a |
| 231 |
setter (Recommend a) = FontSize $ Recommend (Last a) |
| 232 |
setter (Commit a) = FontSize $ Commit (Last a) |
| 233 |
-- = iso (\(FontSize a) -> a) FontSize . mapping _Wrapped |
| 234 |
-- once we depend on monoid-extras-0.4 |
| 235 |
|
| 236 |
_FontSizeM :: Iso' (FontSizeM n) (Measured n (Recommend n)) |
| 237 |
_FontSizeM = mapping _FontSize |
| 238 |
|
| 239 |
type FontSizeM n = Measured n (FontSize n) |
| 240 |
|
| 241 |
instance Typeable n => AttributeClass (FontSize n) |
| 242 |
|
| 243 |
instance Num n => Default (FontSizeM n) where |
| 244 |
def = FontSize . Recommend . Last <$> local 1 |
| 245 |
|
| 246 |
|
| 247 |
getFontSize :: FontSize n -> n |
| 248 |
getFontSize (FontSize (Recommend (Last s))) = s |
| 249 |
getFontSize (FontSize (Commit (Last s))) = s |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
fontSize :: (N a ~ n, Typeable n, HasStyle a) => Measure n -> a -> a |
| 255 |
fontSize = applyMAttr . fmap (FontSize . Commit . Last) |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
recommendFontSize :: (N a ~ n, Typeable n, HasStyle a) => Measure n -> a -> a |
| 262 |
recommendFontSize = applyMAttr . fmap (FontSize . Recommend . Last) |
| 263 |
|
| 264 |
|
| 265 |
fontSizeG :: (N a ~ n, Typeable n, Num n, HasStyle a) => n -> a -> a |
| 266 |
fontSizeG = fontSize . global |
| 267 |
|
| 268 |
|
| 269 |
fontSizeN :: (N a ~ n, Typeable n, Num n, HasStyle a) => n -> a -> a |
| 270 |
fontSizeN = fontSize . normalized |
| 271 |
|
| 272 |
|
| 273 |
fontSizeO :: (N a ~ n, Typeable n, HasStyle a) => n -> a -> a |
| 274 |
fontSizeO = fontSize . output |
| 275 |
|
| 276 |
|
| 277 |
fontSizeL :: (N a ~ n, Typeable n, Num n, HasStyle a) => n -> a -> a |
| 278 |
fontSizeL = fontSize . local |
| 279 |
|
| 280 |
|
| 281 |
fontSizeM :: (N a ~ n, Typeable n, HasStyle a) => FontSizeM n -> a -> a |
| 282 |
fontSizeM = applyMAttr |
| 283 |
|
| 284 |
_fontSizeR :: (Typeable n, OrderedField n) => Lens' (Style v n) (Measured n (Recommend n)) |
| 285 |
_fontSizeR = atMAttr . anon def (const False) . _FontSizeM |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
_fontSize :: (Typeable n, OrderedField n) => Lens' (Style v n) (Measure n) |
| 290 |
_fontSize = _fontSizeR . mapping committed |
| 291 |
|
| 292 |
_fontSizeU :: (Typeable n) => Lens' (Style v n) (Maybe n) |
| 293 |
_fontSizeU = atAttr . mapping (_FontSize . committed) |
| 294 |
|
| 295 |
-------------------------------------------------- |
| 296 |
-- Font slant |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
data FontSlant = FontSlantNormal |
| 302 |
| FontSlantItalic |
| 303 |
| FontSlantOblique |
| 304 |
deriving (Eq, Show, Typeable, Ord) |
| 305 |
|
| 306 |
instance AttributeClass FontSlant where |
| 307 |
instance Semigroup FontSlant where |
| 308 |
_ <> b = b |
| 309 |
|
| 310 |
instance Default FontSlant where |
| 311 |
def = FontSlantNormal |
| 312 |
|
| 313 |
|
| 314 |
getFontSlant :: FontSlant -> FontSlant |
| 315 |
getFontSlant = id |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
fontSlant :: HasStyle a => FontSlant -> a -> a |
| 321 |
fontSlant = applyAttr |
| 322 |
|
| 323 |
|
| 324 |
_fontSlant :: Lens' (Style v n) FontSlant |
| 325 |
_fontSlant = atAttr . non def |
| 326 |
|
| 327 |
|
| 328 |
italic :: HasStyle a => a -> a |
| 329 |
italic = fontSlant FontSlantItalic |
| 330 |
|
| 331 |
|
| 332 |
oblique :: HasStyle a => a -> a |
| 333 |
oblique = fontSlant FontSlantOblique |
| 334 |
|
| 335 |
-------------------------------------------------- |
| 336 |
-- Font weight |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
data FontWeight = FontWeightNormal |
| 342 |
| FontWeightBold |
| 343 |
| FontWeightBolder |
| 344 |
| FontWeightLighter |
| 345 |
| FontWeightThin |
| 346 |
| FontWeightUltraLight |
| 347 |
| FontWeightLight |
| 348 |
| FontWeightMedium |
| 349 |
| FontWeightSemiBold |
| 350 |
| FontWeightUltraBold |
| 351 |
| FontWeightHeavy |
| 352 |
deriving (Eq, |
| 353 |
Ord, Show, Typeable) |
| 354 |
|
| 355 |
instance AttributeClass FontWeight |
| 356 |
|
| 357 |
|
| 358 |
instance Semigroup FontWeight where |
| 359 |
_ <> b = b |
| 360 |
|
| 361 |
instance Default FontWeight where |
| 362 |
def = FontWeightNormal |
| 363 |
|
| 364 |
|
| 365 |
getFontWeight :: FontWeight -> FontWeight |
| 366 |
getFontWeight = id |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
fontWeight :: HasStyle a => FontWeight -> a -> a |
| 372 |
fontWeight = applyAttr |
| 373 |
|
| 374 |
|
| 375 |
bold :: HasStyle a => a -> a |
| 376 |
bold = fontWeight FontWeightBold |
| 377 |
|
| 378 |
|
| 379 |
thinWeight :: HasStyle a => a -> a |
| 380 |
thinWeight = fontWeight FontWeightThin |
| 381 |
|
| 382 |
|
| 383 |
ultraLight :: HasStyle a => a -> a |
| 384 |
ultraLight = fontWeight FontWeightUltraLight |
| 385 |
|
| 386 |
|
| 387 |
light :: HasStyle a => a -> a |
| 388 |
light = fontWeight FontWeightLight |
| 389 |
|
| 390 |
|
| 391 |
mediumWeight :: HasStyle a => a -> a |
| 392 |
mediumWeight = fontWeight FontWeightMedium |
| 393 |
|
| 394 |
|
| 395 |
semiBold :: HasStyle a => a -> a |
| 396 |
semiBold = fontWeight FontWeightSemiBold |
| 397 |
|
| 398 |
|
| 399 |
ultraBold :: HasStyle a => a -> a |
| 400 |
ultraBold = fontWeight FontWeightUltraBold |
| 401 |
|
| 402 |
|
| 403 |
heavy :: HasStyle a => a -> a |
| 404 |
heavy = fontWeight FontWeightHeavy |
| 405 |
|
| 406 |
|
| 407 |
bolder :: HasStyle a => a -> a |
| 408 |
bolder = fontWeight FontWeightBolder |
| 409 |
|
| 410 |
|
| 411 |
lighter :: HasStyle a => a -> a |
| 412 |
lighter = fontWeight FontWeightLighter |
| 413 |
|
| 414 |
|
| 415 |
_fontWeight :: Lens' (Style v n) FontWeight |
| 416 |
_fontWeight = atAttr . non def |