~jaro/balkon

ref: 3e36005fb819e46423c4afe02d3f77d7571e9b2b balkon/src/Data/Text/ParagraphLayout/Internal/Layout.hs -rw-r--r-- 14.6 KiB
3e36005fJaro Update changelog on numbers and neutrals. 1 year, 5 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
-- | Implementation of paragraph layout, decoupled from external interfaces.
module Data.Text.ParagraphLayout.Internal.Layout
    ( FragmentWithSpan
    , layoutAndAlignLines
    )
where

import Data.Foldable (toList)
import Data.Int (Int32)
import Data.List (mapAccumL)
import Data.List.NonEmpty (NonEmpty ((:|)), nonEmpty, (<|))
import qualified Data.List.NonEmpty as NonEmpty
import Data.Maybe (catMaybes, fromMaybe)
import Data.Text.Foreign (lengthWord8)
import Data.Text.Glyphize
    ( Buffer (..)
    , ContentType (ContentTypeUnicode)
    , Direction (DirLTR, DirRTL)
    , FontExtents (..)
    , GlyphInfo
    , GlyphPos
    , defaultBuffer
    , fontExtentsForDir
    , shape
    )
import qualified Data.Text.ICU as BreakStatus (Line (Hard))
import qualified Data.Text.Lazy as Lazy

import Data.Text.ParagraphLayout.Internal.AncestorBox
import Data.Text.ParagraphLayout.Internal.ApplyBoxes
import Data.Text.ParagraphLayout.Internal.BiDiReorder
import Data.Text.ParagraphLayout.Internal.Break
import Data.Text.ParagraphLayout.Internal.Fragment
import Data.Text.ParagraphLayout.Internal.LineHeight
import Data.Text.ParagraphLayout.Internal.ParagraphExtents
import qualified Data.Text.ParagraphLayout.Internal.ProtoFragment as PF
import qualified Data.Text.ParagraphLayout.Internal.ProtoLine as PL
import Data.Text.ParagraphLayout.Internal.Rect
import qualified Data.Text.ParagraphLayout.Internal.ResolvedBox as RB
import qualified Data.Text.ParagraphLayout.Internal.ResolvedSpan as RS
import Data.Text.ParagraphLayout.Internal.Run
import Data.Text.ParagraphLayout.Internal.TextContainer
import Data.Text.ParagraphLayout.Internal.TextOptions
import Data.Text.ParagraphLayout.Internal.WithSpan

-- This is redundant.
-- TODO: Consider using `ResolvedSpan` as `fragmentUserData`, then swapping it
--       for the actual `spanUserData` before returning it to the user.
type ProtoFragmentWithSpan d = WithSpan d PF.ProtoFragment
type FragmentWithSpan d = WithSpan d (Fragment d)
type ProtoFragmentWithBoxes d = WithBoxes d (ProtoFragmentWithSpan d)

-- | Create a multi-line layout from the given runs, splitting them as
-- necessary to fit within the requested line width.
--
-- The output is a flat list of fragments positioned in both dimensions.
layoutAndAlignLines :: Int32 -> NonEmpty (WithSpan d Run) ->
    [FragmentWithSpan d]
layoutAndAlignLines maxWidth runs = frags
    where
        frags = concatMap NonEmpty.toList fragsInLines
        (_, fragsInLines) = mapAccumL positionLineH originY numberedLines
        numberedLines = zip [1 ..] canonicalLines
        canonicalLines = fmap reorderProtoFragments logicalLines
        logicalLines = nonEmptyItems $ layoutLines maxWidth [] runs
        originY = paragraphOriginY

reorderProtoFragments :: PL.ProtoLine NonEmpty d -> PL.ProtoLine NonEmpty d
reorderProtoFragments pl@(PL.ProtoLine { PL.protoFragments = pfs }) =
    pl { PL.protoFragments = reorder pfs }

nonEmptyItems :: Foldable t =>
    t (PL.ProtoLine [] d) -> [PL.ProtoLine NonEmpty d]
nonEmptyItems = catMaybes . map PL.nonEmpty . toList

-- | Create a multi-line layout from the given runs, splitting them as
-- necessary to fit within the requested line width.
--
-- The output is a two-dimensional list of fragments positioned along the
-- horizontal axis.
layoutLines :: Int32 -> [RB.ResolvedBox d] -> NonEmpty (WithSpan d Run) ->
    NonEmpty (PL.ProtoLine [] d)
layoutLines maxWidth openBoxes runs = case nonEmpty rest of
        -- Everything fits. We are done.
        Nothing -> fitting :| []
        -- Something fits, the rest goes on the next line.
        Just runs' -> fitting <| layoutLines maxWidth openBoxes' runs'
    where
        (fitting, rest) = layoutAndWrapRunsH maxWidth openBoxes runs
        -- Update the list of open boxes using the logically last run on this
        -- line, if there is one.
        openBoxes' =
            openBoxes `fromMaybe` lastSpanBoxes (PL.protoFragments fitting)

-- TODO: Allow a run across multiple spans (e.g. if they only differ by colour).

-- | Position all the given horizontal fragments on the same line,
-- using @originY@ as its top edge, and return the bottom edge for continuation.
--
-- Glyphs will be aligned by their ascent line, similar to the behaviour of
-- @vertical-align: top@ in CSS.
--
-- TODO: For rich text, allow other types of vertical alignment.
positionLineH :: Int32 -> (Int, PL.ProtoLine NonEmpty d) ->
    (Int32, NonEmpty (FragmentWithSpan d))
positionLineH originY (num, pl) = (nextY, frags)
    where
        nextY = maximum $ fmap y_min rects
        rects = fmap (\ (WithSpan _ r) -> fragmentRect r) frags
        (_, frags) = mapAccumL (positionFragmentH num originY) originX wpfs
        wpfs = PL.applyBoxes pl
        originX = paragraphOriginX

-- | Position the given horizontal fragment on a line,
-- using @originY@ as its top edge and @originX@ as its left edge,
-- returning the X coordinate of its right edge for continuation.
positionFragmentH :: Int -> Int32 -> Int32 -> ProtoFragmentWithBoxes d ->
    (Int32, FragmentWithSpan d)
positionFragmentH line originY originX (WithBoxes lbs (WithSpan rs pf) rbs) =
    (nextX, WithSpan rs frag)
    where
        nextX = contentX + contentWidth + rightSpacing
        contentX = originX + leftSpacing
        contentWidth = PF.advance pf
        leftSpacing = totalLeftSpacing bs
        rightSpacing = totalRightSpacing bs
        frag = Fragment userData line bs rect (penX, penY) (PF.glyphs pf)
        userData = RS.spanUserData rs
        bs = ancestorBoxes lbs rbs rs
        rect = Rect contentX originY contentWidth (-lineHeight)
        penX = 0
        penY = descent + leading `div` 2 - lineHeight
        lineHeight = case textLineHeight opts of
            Normal -> normalLineHeight
            Absolute h -> h
        leading = lineHeight - normalLineHeight
        normalLineHeight = ascent + descent
        ascent = ascender extents
        descent = - descender extents
        extents = fontExtentsForDir (textFont opts) (PF.direction pf)
        opts = RS.spanTextOptions rs

ancestorBoxes
    :: [RB.ResolvedBox d]
    -> [RB.ResolvedBox d]
    -> RS.ResolvedSpan d
    -> [AncestorBox d]
ancestorBoxes leftBoxes rightBoxes rs = map ancestorBox $ RS.spanBoxes rs
    where
        ancestorBox b = case RB.boxDirection b of
            DirLTR -> AncestorBox
                { boxUserData = RB.boxUserData b
                , boxLeftEdge = leftEdge b
                , boxRightEdge = rightEdge b
                , boxStartEdge = leftEdge b
                , boxEndEdge = rightEdge b
                }
            DirRTL -> AncestorBox
                { boxUserData = RB.boxUserData b
                , boxLeftEdge = leftEdge b
                , boxRightEdge = rightEdge b
                , boxStartEdge = rightEdge b
                , boxEndEdge = leftEdge b
                }
            _ -> AncestorBox
                { boxUserData = RB.boxUserData b
                , boxLeftEdge = NoEdge
                , boxRightEdge = NoEdge
                , boxStartEdge = NoEdge
                , boxEndEdge = NoEdge
                }
        leftEdge b = if b `elem` leftBoxes
            then SpacedEdge $ RB.boxLeftSpacing b
            else NoEdge
        rightEdge b = if b `elem` rightBoxes
            then SpacedEdge $ RB.boxRightSpacing b
            else NoEdge

-- | Calculate layout for multiple horizontal runs, breaking them as necessary
-- to fit as much content as possible without exceeding the maximum line width,
-- and return the remaining runs to be placed on other lines.
layoutAndWrapRunsH
    :: Int32
    -> [RB.ResolvedBox d]
    -> NonEmpty (WithSpan d Run)
    -> (PL.ProtoLine [] d, [WithSpan d Run])
layoutAndWrapRunsH maxWidth prevOpenBoxes runs = NonEmpty.head $ validProtoLines
    where
        validProtoLines = dropWhile1 tooLong layouts
        tooLong (pl, _) = PL.width pl > maxWidth
        layouts = fmap fstToProtoLine splits
        fstToProtoLine (runs1, runs2) =
            (protoLine prevOpenBoxes (layoutRunsH runs1) runs2, runs2)
        -- TODO: Consider optimising.
        --       We do not need to look for soft breaks further than the
        --       shortest hard break.
        splits = hardSplit runs :| softSplits runs

-- | Construct a `PL.ProtoLine`, peeking at the text run on the following line
-- to determine `PL.nextOpenBoxes`.
protoLine
    :: [RB.ResolvedBox d]
    -> [ProtoFragmentWithSpan d]
    -> [WithSpan d Run]
    -> PL.ProtoLine [] d
protoLine prev pfs rest = PL.ProtoLine pfs prev next
    where
        next = [] `fromMaybe` firstSpanBoxes rest

firstSpanBoxes :: [WithSpan d a] -> Maybe [RB.ResolvedBox d]
firstSpanBoxes xs = case xs of
    [] -> Nothing
    (WithSpan rs _) : _ -> Just $ RS.spanBoxes rs

lastSpanBoxes :: [WithSpan d a] -> Maybe [RB.ResolvedBox d]
lastSpanBoxes xs = case reverse xs of
    [] -> Nothing
    (WithSpan rs _) : _ -> Just $ RS.spanBoxes rs

-- | Treat a list of runs as a contiguous sequence, and split them into two
-- lists so that the first list contains as many non-whitespace characters as
-- possible without crossing a hard line break (typically after a newline
-- character).
--
-- If the input is non-empty and starts with a hard line break, then the first
-- output list will contain a run of zero characters. This can be used to
-- correctly size an empty line.
--
-- If there is no hard line break in the input, the first output list will
-- contain the whole input, and the second output list will be empty.
hardSplit :: NonEmpty (WithSpan d Run) -> ([WithSpan d Run], [WithSpan d Run])
hardSplit runs = allowFstEmpty $ trimFst $ NonEmpty.last $ splits
    where
        trimFst (runs1, runs2) = (trim runs1, runs2)
        trim
            = trimTextsStartPreserve isStartSpace
            . trimTextsEndPreserve isEndSpace
            . trimTextsEndPreserve isNewline
        -- TODO: Consider optimising.
        --       We do not need to look for any line breaks further than the
        --       shortest hard break.
        splits = noSplit :| map allowSndEmpty hSplits
        noSplit = (runs, [])
        hSplits = -- from longest to shortest
            splitTextsBy (map fst . filter isHard . runLineBreaks) runs
        isHard (_, status) = status == BreakStatus.Hard

-- | Treat a list of runs as a contiguous sequence,
-- and find all possible ways to split them into two non-empty lists,
-- using soft line break opportunities (typically after words) and then
-- using character boundaries.
--
-- Runs of zero characters will not be created. If line breaking would result
-- in a line that consists entirely of whitespace, this whitespace will be
-- skipped, so an empty line is not created.
--
-- The results in the form (prefix, suffix) will be ordered so that items
-- closer to the start of the list are preferred for line breaking, but without
-- considering overflows.
softSplits :: NonEmpty (WithSpan d Run) ->
    [([WithSpan d Run], [WithSpan d Run])]
softSplits runs = map (allowSndEmpty . trimFst) splits
    where
        trimFst (runs1, runs2) = (trim runs1, runs2)
        trim = trimTextsStart isStartSpace . trimTextsEnd isEndSpace
        splits = lSplits ++ cSplits
        lSplits = splitTextsBy (map fst . runLineBreaks) runs
        -- TODO: Consider optimising.
        --       We do not need to look for character breaks further than the
        --       shortest line break.
        cSplits = splitTextsBy (map fst . runCharacterBreaks) runs

allowFstEmpty :: (NonEmpty a, b) -> ([a], b)
allowFstEmpty (a, b) = (NonEmpty.toList a, b)

allowSndEmpty :: (a, NonEmpty b) -> (a, [b])
allowSndEmpty (a, b) = (a, NonEmpty.toList b)

-- | The suffix remaining after removing the longest prefix of the list for
-- which the predicate holds, except always including at least the last element
-- of the original list.
dropWhile1 :: (a -> Bool) -> NonEmpty a -> NonEmpty a
dropWhile1 p list = case NonEmpty.uncons list of
    (_, Nothing) -> list
    (x, Just xs) -> if p x
        then dropWhile1 p xs
        else list

-- | Calculate layout for multiple horizontal runs on the same line, without
-- any breaking.
layoutRunsH :: [WithSpan d Run] -> [ProtoFragmentWithSpan d]
layoutRunsH runs = map layoutRunH runs

-- | Calculate layout for the given horizontal run and attach extra information.
layoutRunH :: WithSpan d Run -> ProtoFragmentWithSpan d
layoutRunH (WithSpan rs run) = WithSpan rs pf
    where
        pf = PF.protoFragmentH dir lvl glyphs
        glyphs = shapeRun (WithSpan rs run)
        dir = runDirection run
        lvl = runLevel run

-- | Calculate layout for the given run independently of its position.
shapeRun :: WithSpan d Run -> [(GlyphInfo, GlyphPos)]
shapeRun (WithSpan rs run) = shape font buffer features
    where
        font = textFont opts
        buffer = defaultBuffer
            { text = Lazy.fromStrict $ runText run
            , contentType = Just ContentTypeUnicode
            , direction = runDirection run
            , script = runScript run
            , language = Just $ textLanguage opts
            -- Perhaps counter-intuitively, the `beginsText` and `endsText`
            -- flags refer to everything that "Data.Text.Glyphize" can see,
            -- not just the current run.
            --
            -- Since all runs are cut from a single continuous `Text` that
            -- represents the entire paragraph, and "Data.Text.Glyphize" peeks
            -- at the whole underlying byte array, HarfBuzz will be able to see
            -- both the beginning and the end of the paragraph at all times,
            -- so these flags can always be set.
            , beginsText = True
            , endsText = True
            }
        features = []
        opts = RS.spanTextOptions rs

runLineBreaks :: WithSpan d Run -> [(Int, BreakStatus.Line)]
runLineBreaks (WithSpan rs run) =
    runBreaksFromSpan run $ RS.spanLineBreaks rs

runCharacterBreaks :: WithSpan d Run -> [(Int, ())]
runCharacterBreaks (WithSpan rs run) =
    runBreaksFromSpan run $ RS.spanCharacterBreaks rs

-- | Constrain span breaks to a selected run and adjust offsets.
runBreaksFromSpan :: Run -> [(Int, a)] -> [(Int, a)]
runBreaksFromSpan run spanBreaks =
    dropWhile (not . valid) $ subOffsetsDesc (runOffsetInSpan run) spanBreaks
    where
        valid (off, _) = off < runLength
        runLength = lengthWord8 $ getText run

-- | Predicate for characters that can be potentially removed from the
-- beginning of a line according to the CSS Text Module.
isStartSpace :: Char -> Bool
isStartSpace c = c `elem` [' ', '\t']

-- | Predicate for characters that can be potentially removed from the end of
-- a line according to the CSS Text Module.
isEndSpace :: Char -> Bool
isEndSpace c = c `elem` [' ', '\t', '\x1680']

-- | Predicate for characters that should be removed from the end of a line in
-- the case of a hard line break.
isNewline :: Char -> Bool
isNewline c = c == '\n'