module Data.Text.ParagraphLayout.Internal.Plain (Paragraph(..) ,ParagraphLayout(..) ,ParagraphOptions(..) ,SpanLayout(..) ,layoutPlain ) where import Data.Int (Int32) import Data.List (mapAccumL) import Data.List.NonEmpty (NonEmpty((:|))) import Data.Maybe (catMaybes, fromMaybe, listToMaybe) import qualified Data.Text as Text import Data.Text.Array (Array) import Data.Text.Foreign (lengthWord8) import Data.Text.Glyphize (Buffer(..) ,ContentType(ContentTypeUnicode) ,Font ,FontExtents(..) ,GlyphInfo ,GlyphPos(x_advance) ,defaultBuffer ,fontExtentsForDir ,shape ) import Data.Text.ICU (LocaleName(Locale), breakLine) import qualified Data.Text.ICU as BreakStatus (Line) import Data.Text.Internal (Text(Text)) import qualified Data.Text.Lazy as Lazy import Data.Text.ParagraphLayout.Internal.Break import Data.Text.ParagraphLayout.Internal.Fragment import Data.Text.ParagraphLayout.Internal.LineHeight import qualified Data.Text.ParagraphLayout.Internal.ProtoFragment as PF import Data.Text.ParagraphLayout.Internal.Rect import qualified Data.Text.ParagraphLayout.Internal.ResolvedSpan as RS import Data.Text.ParagraphLayout.Internal.Run import Data.Text.ParagraphLayout.Internal.Span import Data.Text.ParagraphLayout.Internal.TextContainer -- | Text to be laid out as a single paragraph. -- -- May be divided into any number of neighbouring spans, each of which will -- be represented as a separate `SpanLayout` in the resulting layout. -- -- The input text must be encoded as UTF-8 in a contiguous byte array. -- -- You may need to use "Data.Text.Internal" in order to determine the byte -- array and the necessary offsets to construct the paragraph without copying -- data. -- -- For simple use cases, it may be sufficient to construct paragraphs using -- [ParagraphConstruction]("Data.Text.ParagraphLayout.ParagraphConstruction"). data Paragraph = Paragraph Array -- ^ A byte array containing the whole text to be laid out, in UTF-8. Int -- ^ Byte offset of the first span from the start of the byte array. -- Any characters preceding this offset will not be shaped, but may still -- be used to influence the shape of neighbouring characters. [Span] -- ^ Parts of the text to be laid out, in logical order. -- The initial offset plus total length of all spans must not exceed -- the bounds of the byte array. -- Any characters following the last span will not be shaped, but may still -- be used to influence the shape of neighbouring characters. ParagraphOptions -- ^ Options applying to the paragraph as a whole. data ParagraphOptions = ParagraphOptions { paragraphFont :: Font -- ^ Font to be used for shaping and measurement. -- Make sure to set its scale (see `Data.Text.Glyphize.optionScale`) using -- the same units that you want in the output. , paragraphLineHeight :: LineHeight -- ^ Preferred line height of the resulting box fragments. , paragraphMaxWidth :: Int32 -- ^ Line width at which line breaking should occur. -- Lines will be broken at language-appropriate boundaries. -- If a line still exceeds this limit then, it will be broken at character -- boundaries, and if it already consists of a single cluster that cannot -- be further broken down, it will overflow. } deriving (Eq, Show) -- | The resulting layout of the whole paragraph. data ParagraphLayout = ParagraphLayout { paragraphRect :: Rect Int32 -- ^ The containing block (CSS3). , spanLayouts :: [SpanLayout] } deriving (Eq, Read, Show) -- | The resulting layout of each span, which may include multiple fragments if -- broken over multiple lines. data SpanLayout = SpanLayout [Fragment] -- TODO: Consider merging. fragments created by script changes. deriving (Eq, Read, Show) -- | Wrapper for temporarily mapping the relationship to a `Span`. data WithSpan a = WithSpan RS.ResolvedSpan a instance Functor WithSpan where fmap f (WithSpan s a) = WithSpan s (f a) instance TextContainer a => TextContainer (WithSpan a) where getText (WithSpan _ c) = getText c instance SeparableTextContainer a => SeparableTextContainer (WithSpan a) where splitTextAt8 n (WithSpan rs c) = (WithSpan rs c1, WithSpan rs c2) where (c1, c2) = splitTextAt8 n c splitBySpanIndex :: [WithSpan a] -> [[a]] splitBySpanIndex xs = [getBySpanIndex i xs | i <- [0..]] getBySpanIndex :: Int -> [WithSpan a] -> [a] getBySpanIndex idx xs = map contents $ filter matchingIndex $ xs where matchingIndex (WithSpan rs _) = (RS.spanIndex rs) == idx contents (WithSpan _ x) = x spanRects :: SpanLayout -> [Rect Int32] spanRects (SpanLayout frags) = map fragmentRect frags base :: (Num a) => Rect a base = Rect 0 0 0 0 containRects :: (Ord a, Num a) => [Rect a] -> Rect a containRects = foldr union base -- | Lay out a paragraph of plain, unidirectional text using a single font. layoutPlain :: Paragraph -> ParagraphLayout layoutPlain p@(Paragraph _ _ _ opts) = ParagraphLayout pRect sls where pRect = containRects $ concat $ map spanRects sls sls = map SpanLayout fragsBySpan fragsBySpan = take (length spans) $ splitBySpanIndex frags frags = layoutAndAlignLines maxWidth $ spansToRunsWrapped spans maxWidth = paragraphMaxWidth opts spans = resolveSpans p -- | Split a number of spans into a flat array of runs and add a wrapper -- so that each run can be traced back to its originating span. spansToRunsWrapped :: [RS.ResolvedSpan] -> [WithSpan Run] spansToRunsWrapped ss = concat $ map spanToRunsWrapped ss -- | Split a span into runs and add a wrapper -- so that each run can be traced back to its originating span. spanToRunsWrapped :: RS.ResolvedSpan -> [WithSpan Run] spanToRunsWrapped s = map (WithSpan s) (spanToRuns s) -- | 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 -> [WithSpan Run] -> [WithSpan Fragment] layoutAndAlignLines maxWidth runs = frags where frags = concat fragsInLines (_, fragsInLines) = mapAccumL alignLineH originY protoFragsInLines protoFragsInLines = layoutLines maxWidth runs originY = 0 -- | 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 -> [WithSpan Run] -> [[WithSpan PF.ProtoFragment]] layoutLines maxWidth runs | null rest -- Everything fits. We are done. = fitting : [] | null fitting -- Nothing fits. We must resolve this by overflowing. = overflowing : [] | otherwise -- Something fits, the rest goes on the next line. = fitting : layoutLines maxWidth rest where (fitting, rest) = tryAddRunsH maxWidth originX runs overflowing = addRunsH originX runs originX = 0 -- TODO: Allow a run across multiple spans (e.g. if they only differ by colour). -- | Align all the given horizontal fragments vertically 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. alignLineH :: Int32 -> [WithSpan PF.ProtoFragment] -> (Int32, [WithSpan Fragment]) alignLineH originY pfs = (nextY, frags) where nextY = maximum $ map y_min rects rects = map (\(WithSpan _ r) -> fragmentRect r) frags frags = map (alignFragmentH originY) pfs -- | Align the given horizontal fragment vertically on a line, -- using `originY` as its top edge. alignFragmentH :: Int32 -> WithSpan PF.ProtoFragment -> WithSpan Fragment alignFragmentH originY (WithSpan rs pf) = WithSpan rs (Fragment rect (penX, penY) (PF.glyphs pf)) where rect = Rect (PF.offset pf) originY (PF.advance pf) (-lineHeight) penX = 0 penY = descent + leading `div` 2 - lineHeight lineHeight = case RS.spanLineHeight rs of Normal -> normalLineHeight Absolute h -> h leading = lineHeight - normalLineHeight normalLineHeight = ascent + descent ascent = ascender extents descent = - descender extents extents = fontExtentsForDir font (PF.direction pf) font = RS.spanFont rs -- | Like `addRunsH`, but break the input runs as necessary to prevent -- overflowing the maximum line width, -- and return the remaining runs to be placed on other lines. tryAddRunsH :: Int32 -> Int32 -> [WithSpan Run] -> ([WithSpan PF.ProtoFragment], [WithSpan Run]) tryAddRunsH maxWidth currentX runs = fromMaybe lastResortSplit $ listToMaybe validSplits where lastResortSplit = do let (runs1, runs2) = splitTextsAt8 1 runs let (_, pfs) = mapAccumL addRunH currentX runs1 (pfs, runs2) applySplit (runs1, runs2) = do let (nextX, pfs) = mapAccumL addRunH currentX runs1 if abs nextX <= maxWidth then Just (pfs, runs2) else Nothing validSplits = catMaybes $ map applySplit splits splits = noSplit : (filter hasContent $ breakSplits [] (reverse runs)) noSplit = (runs, []) hasContent = not . null . fst -- | Recursive function for finding all possible ways to split a list of runs -- into two on a valid line-breaking boundary, including the start of the first -- run and excluding the end of the last run. -- -- The first input list is the suffix consisting of runs that have already been -- considered for breaking. These will be appended to the output suffix as they -- are. -- -- The second input list is the prefix consisting of runs to be considered for -- breaking, in reverse order. -- -- The results in the form (prefix, suffix) will be ordered from the longest -- prefix to shortest. breakSplits :: [WithSpan Run] -> [WithSpan Run] -> [([WithSpan Run], [WithSpan Run])] breakSplits _ [] = [] breakSplits closed (x:xs) = splits ++ breakSplits (x:closed) xs where splits = map mapFunc $ runLineSplits x mapFunc ((x1, x2), _) = (reverse $ collapse $ x1 :| xs, collapse $ x2 :| closed) -- | If the first run is empty, remove it. collapse :: NonEmpty (WithSpan Run) -> [WithSpan Run] collapse (x :| xs) | Text.null (getText x) = xs | otherwise = x:xs -- | Calculate layout for multiple runs on the same line and -- arrange them in one horizontal direction starting from the given x_offset. addRunsH :: Int32 -> [WithSpan Run] -> [WithSpan PF.ProtoFragment] addRunsH currentX runs = snd $ mapAccumL addRunH currentX runs -- | Calculate layout for the given run, -- place the generated fragment horizontally at the given x_offset in a line, -- and return the final x_offset for continuation. addRunH :: Int32 -> WithSpan Run -> (Int32, WithSpan PF.ProtoFragment) addRunH currentX run = (nextX, WithSpan rs pf) where WithSpan rs pf = layoutRun currentX run nextX = currentX + PF.advance pf -- | Calculate layout for the given run and position it in a line. layoutRun :: Int32 -> WithSpan Run -> WithSpan PF.ProtoFragment layoutRun originX (WithSpan rs run) = WithSpan rs pf where pf = PF.ProtoFragment dir originX totalX glyphs glyphs = shapeRun (WithSpan rs run) positions = map snd glyphs totalX = sum $ map x_advance positions dir = runDirection run -- | Calculate layout for the given run independently of its position. shapeRun :: WithSpan Run -> [(GlyphInfo, GlyphPos)] shapeRun (WithSpan rs run) = shape font buffer features where font = RS.spanFont rs -- TODO: Set beginsText / endsText. buffer = defaultBuffer { text = Lazy.fromStrict $ runText run , contentType = Just ContentTypeUnicode , direction = runDirection run , script = runScript run , language = Just $ RS.spanLanguage rs } features = [] resolveSpans :: Paragraph -> [RS.ResolvedSpan] resolveSpans p@(Paragraph arr off spans opts) = do let (end, textsAndMarks) = cutsAndMarks arr off spans let indexes = [0..] (s, (o, t), i) <- zip3 spans textsAndMarks indexes let lang = spanLanguage s let breaks = paragraphLineBreaks p end lang return RS.ResolvedSpan { RS.spanIndex = i , RS.spanOffsetInParagraph = o - off , RS.spanText = t , RS.spanFont = paragraphFont opts , RS.spanLineHeight = paragraphLineHeight opts , RS.spanLanguage = lang , RS.spanLineBreaks = subOffsetsDesc (o - off) breaks } paragraphLineBreaks :: Paragraph -> Int -> String -> [(Int, BreakStatus.Line)] paragraphLineBreaks (Paragraph arr off _ _) end lang = breaksDesc (breakLine (localeFromLanguage lang)) paragraphText where paragraphText = Text arr off (end - off) -- | Split the given run at every valid line break position. runLineSplits :: WithSpan Run -> [((WithSpan Run, WithSpan Run), BreakStatus.Line)] runLineSplits r = map split $ runLineBreaks r where split (i, status) = (splitTextAt8 i r, status) runLineBreaks :: WithSpan Run -> [(Int, BreakStatus.Line)] runLineBreaks (WithSpan rs run) = dropWhile (not . valid) $ subOffsetsDesc (runOffsetInSpan run) $ RS.spanLineBreaks rs where valid (off, _) = off < runLength runLength = lengthWord8 $ getText run -- TODO: Identify and correct for differences between the two. localeFromLanguage :: String -> LocaleName localeFromLanguage x = Locale x -- | Given an underlying `Array`, an initial offset, and a list of consecutive -- `Span`s, produce a list of `Text`s corresponding to the given spans, as well -- as the offset of the start of each `Text` and the end of the last `Text`. -- -- TODO: Consider adding checks for array bounds. cutsAndMarks :: Array -> Int -> [Span] -> (Int, [(Int, Text)]) cutsAndMarks arr initialOffset spans = mapAccumL (cutAndMark arr) initialOffset spans -- | Like `cut`, but also include the starting offset in the output. cutAndMark :: Array -> Int -> Span -> (Int, (Int, Text)) cutAndMark arr off s = (end, (off, t)) where (end, t) = cut arr off s -- | Produce a `Text`, defined by an initial offset and a `Span`, out of the -- underlying `Array`. cut :: Array -> Int -> Span -> (Int, Text) cut arr off s = (end, t) where len = spanLength s end = off + len t = Text arr off len