Debian Codegraph @

src/Diagrams/TwoD/Text.hs

raw symbols SCIP
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 -- | A 'Text' primitive consists of the string contents, text alignment
72 -- and the transformation to be applied. The transformation is scale
73 -- invarient, the average scale of the transform should always be 1.
74 -- All text scaling is obtained from the 'FontSize' attribute.
75 --
76 -- This constructor should not be used directly. Use 'text',
77 -- 'alignedText' or 'baselineText'.
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 -- | @TextAlignment@ specifies the alignment of the text's origin.
95 data TextAlignment n = BaselineText | BoxAlignedText n n
96
97 -- | Make a text from a 'TextAlignment', recommending a fill colour of
98 -- 'black' and 'fontSize' of @'local' 1@.
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 -- | Make a text from a 'TextAlignment' without any default size or fill
108 -- colour. This is useful is you want to recommend your own using
109 -- 'recommendFillColor' or 'recommendFontSize'.
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 -- | Create a primitive text diagram from the given string, with center
146 -- alignment, equivalent to @'alignedText' 0.5 0.5@.
147 --
148 -- Note that it /takes up no space/, as text size information is not
149 -- available.
150 text :: (TypeableFloat n, Renderable (Text n) b) => String -> QDiagram b V2 n Any
151 text = alignedText 0.5 0.5
152
153 -- | Create a primitive text diagram from the given string, origin at
154 -- the top left corner of the text's bounding box, equivalent to
155 -- @'alignedText' 0 1@.
156 --
157 -- Note that it /takes up no space/.
158 topLeftText :: (TypeableFloat n, Renderable (Text n) b) => String -> QDiagram b V2 n Any
159 topLeftText = alignedText 0 1
160
161 -- | Create a primitive text diagram from the given string, with the
162 -- origin set to a point interpolated within the bounding box. The
163 -- first parameter varies from 0 (left) to 1 (right), and the second
164 -- parameter from 0 (bottom) to 1 (top). Some backends do not
165 -- implement this and instead snap to closest corner or the center.
166 --
167 -- The height of this box is determined by the font's potential ascent
168 -- and descent, rather than the height of the particular string.
169 --
170 -- Note that it /takes up no space/.
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 -- | Create a primitive text diagram from the given string, with the
176 -- origin set to be on the baseline, at the beginning (although not
177 -- bounding). This is the reference point of showText in the Cairo
178 -- graphics library.
179 --
180 -- Note that it /takes up no space/.
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 -- | The @Font@ attribute specifies the name of a font family. Inner
193 -- @Font@ attributes override outer ones.
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 -- | Extract the font family name from a @Font@ attribute.
203 getFont :: Font -> String
204 getFont (Font (Last f)) = f
205
206 -- | Specify a font family to be used for all text within a diagram.
207 font :: HasStyle a => String -> a -> a
208 font = applyAttr . Font . Last
209
210 -- | Lens onto the font name of a style.
211 _font :: Lens' (Style v n) (Maybe String)
212 _font = atAttr . mapping _Font
213
214 --------------------------------------------------
215 -- Font size
216
217 -- | The @FontSize@ attribute specifies the size of a font's
218 -- em-square. Inner @FontSize@ attributes override outer ones.
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 -- | Extract the size from a @FontSize@ attribute.
247 getFontSize :: FontSize n -> n
248 getFontSize (FontSize (Recommend (Last s))) = s
249 getFontSize (FontSize (Commit (Last s))) = s
250
251 -- | Set the font size, that is, the size of the font's em-square as
252 -- measured within the current local vector space. The default size
253 -- is @local 1@ (which is applied by 'recommendFontSize').
254 fontSize :: (N a ~ n, Typeable n, HasStyle a) => Measure n -> a -> a
255 fontSize = applyMAttr . fmap (FontSize . Commit . Last)
256
257 -- | 'Recommend' a font size. Any use of 'fontSize' above this will
258 -- overwrite any recommended size. This should only be used with
259 -- 'mkText'', other text functions already has a recommended font
260 -- size so this will be ignored.
261 recommendFontSize :: (N a ~ n, Typeable n, HasStyle a) => Measure n -> a -> a
262 recommendFontSize = applyMAttr . fmap (FontSize . Recommend . Last)
263
264 -- | A convenient synonym for 'fontSize (Global w)'.
265 fontSizeG :: (N a ~ n, Typeable n, Num n, HasStyle a) => n -> a -> a
266 fontSizeG = fontSize . global
267
268 -- | A convenient synonym for 'fontSize (Normalized w)'.
269 fontSizeN :: (N a ~ n, Typeable n, Num n, HasStyle a) => n -> a -> a
270 fontSizeN = fontSize . normalized
271
272 -- | A convenient synonym for 'fontSize (Output w)'.
273 fontSizeO :: (N a ~ n, Typeable n, HasStyle a) => n -> a -> a
274 fontSizeO = fontSize . output
275
276 -- | A convenient sysnonym for 'fontSize (Local w)'.
277 fontSizeL :: (N a ~ n, Typeable n, Num n, HasStyle a) => n -> a -> a
278 fontSizeL = fontSize . local
279
280 -- | Apply a 'FontSize' attribute.
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 -- | Lens to commit a font size. This is *not* a valid lens (see
288 -- 'commited'.
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 -- | The @FontSlantA@ attribute specifies the slant (normal, italic,
299 -- or oblique) that should be used for all text within a diagram.
300 -- Inner @FontSlantA@ attributes override outer ones.
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 -- | Extract the font slant from a 'FontSlantA' attribute.
314 getFontSlant :: FontSlant -> FontSlant
315 getFontSlant = id
316
317 -- | Specify the slant (normal, italic, or oblique) that should be
318 -- used for all text within a diagram. See also 'italic' and
319 -- 'oblique' for useful special cases.
320 fontSlant :: HasStyle a => FontSlant -> a -> a
321 fontSlant = applyAttr
322
323 -- | Lens onto the font slant in a style.
324 _fontSlant :: Lens' (Style v n) FontSlant
325 _fontSlant = atAttr . non def
326
327 -- | Set all text in italics.
328 italic :: HasStyle a => a -> a
329 italic = fontSlant FontSlantItalic
330
331 -- | Set all text using an oblique slant.
332 oblique :: HasStyle a => a -> a
333 oblique = fontSlant FontSlantOblique
334
335 --------------------------------------------------
336 -- Font weight
337
338 -- | The @FontWeightA@ attribute specifies the weight (normal or bold)
339 -- that should be used for all text within a diagram. Inner
340 -- @FontWeightA@ attributes override outer ones.
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 -- | Last semigroup structure
358 instance Semigroup FontWeight where
359 _ <> b = b
360
361 instance Default FontWeight where
362 def = FontWeightNormal
363
364 -- | Extract the font weight.
365 getFontWeight :: FontWeight -> FontWeight
366 getFontWeight = id
367
368 -- | Specify the weight (normal, bolder, lighter or bold) that should be
369 -- used for all text within a diagram. See also 'bold'
370 -- for a useful special case.
371 fontWeight :: HasStyle a => FontWeight -> a -> a
372 fontWeight = applyAttr
373
374 -- | Set all text using a bold font weight.
375 bold :: HasStyle a => a -> a
376 bold = fontWeight FontWeightBold
377
378 -- | Set all text using a thin font weight.
379 thinWeight :: HasStyle a => a -> a
380 thinWeight = fontWeight FontWeightThin
381
382 -- | Set all text using a extra light font weight.
383 ultraLight :: HasStyle a => a -> a
384 ultraLight = fontWeight FontWeightUltraLight
385
386 -- | Set all text using a light font weight.
387 light :: HasStyle a => a -> a
388 light = fontWeight FontWeightLight
389
390 -- | Set all text using a medium font weight.
391 mediumWeight :: HasStyle a => a -> a
392 mediumWeight = fontWeight FontWeightMedium
393
394 -- | Set all text using a semi-bold font weight.
395 semiBold :: HasStyle a => a -> a
396 semiBold = fontWeight FontWeightSemiBold
397
398 -- | Set all text using an ultra-bold font weight.
399 ultraBold :: HasStyle a => a -> a
400 ultraBold = fontWeight FontWeightUltraBold
401
402 -- | Set all text using a heavy/black font weight.
403 heavy :: HasStyle a => a -> a
404 heavy = fontWeight FontWeightHeavy
405
406 -- | Set all text to be bolder than the inherited font weight.
407 bolder :: HasStyle a => a -> a
408 bolder = fontWeight FontWeightBolder
409
410 -- | Set all text to be lighter than the inherited font weight.
411 lighter :: HasStyle a => a -> a
412 lighter = fontWeight FontWeightLighter
413
414 -- | Lens onto the font weight in a style.
415 _fontWeight :: Lens' (Style v n) FontWeight
416 _fontWeight = atAttr . non def