~jaro/balkon

ref: 1c79394163b5e6dcca224451d8b2d9a8c5234fa1 balkon/src/Data/Text/ParagraphLayout/Internal/Fragment.hs -rw-r--r-- 2.2 KiB
1c793941Jaro Set release date for v0.2.1.0. 1 year, 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
module Data.Text.ParagraphLayout.Internal.Fragment
    (Fragment(..)
    ,ShapedRun
    ,shapedRun)
where

import Data.Int (Int32)
import Data.Text.Glyphize (GlyphInfo, GlyphPos)

import Data.Text.ParagraphLayout.Internal.Rect

-- | A unit of text laid out in a rectangular area.
--
-- Equivalent to the CSS3 terms /box fragment/ or /fragment/, except that
-- continuous text even within one line can be split into multiple fragments,
-- either because it comes from multiple input spans, or because it contains
-- glyphs from multiple scripts.
data Fragment = Fragment

    { fragmentRect :: Rect Int32
    -- ^ Physical position of the fragment within the paragraph, calculated
    -- using all glyph advances in this fragment and the calculated line height.
    --
    -- This is the space that the glyphs "take up" and is probably what you
    -- want to use for detecting position-based events such as mouse clicks.
    --
    -- Beware that actual glyphs will not be drawn exactly to the borders of
    -- this rectangle – they may be offset inwards and they can also extend
    -- outwards!
    --
    -- These are not the typographic bounding boxes that you use for determining
    -- the area to draw on – you need FreeType or a similar library for that.
    --
    -- The origin coordinates are relative to the paragraph.

    , fragmentPen :: (Int32, Int32)
    -- ^ Coordinates of the initial pen position, from which the first glyph
    -- should be drawn, relative to the origin of the `fragmentRect`.
    -- Each glyph's `x_advance` or `y_advance` are then used to move the pen
    -- position for the next glyph.

    , fragmentGlyphs :: [(GlyphInfo, GlyphPos)]
    -- ^ Glyphs contained in the fragment, as returned from HarfBuzz.

    }
    deriving (Eq, Read, Show)

-- | A simplified representation of a box fragment, suitable for passing to a
-- text drawing library but lacking detailed size information.
type ShapedRun = (Int32, Int32, [(GlyphInfo, GlyphPos)])

-- | Convert a `Fragment` to a `ShapedRun`.
shapedRun :: Fragment -> ShapedRun
shapedRun f = (x, y, g)
    where
        x = x_origin r + px
        y = y_origin r + py
        g = fragmentGlyphs f
        (px, py) = fragmentPen f
        r = fragmentRect f