~jaro/balkon

390d2fb8a92bd57a895cd02347c6ee33a9a4e170 — Jaro 1 year, 17 days ago 39ec235
Embed TextOptions in ResolvedSpan.

SpanOptions are relegated to the legacy Plain interface, and
TextOptions are now being used internally.
M src/Data/Text/ParagraphLayout/Internal/Layout.hs => src/Data/Text/ParagraphLayout/Internal/Layout.hs +7 -5
@@ 36,6 36,7 @@ import Data.Text.ParagraphLayout.Internal.ResolvedSpan (WithSpan (WithSpan))
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

-- This is redundant.
-- TODO: Consider using `ResolvedSpan` as `fragmentUserData`, then swapping it


@@ 107,15 108,15 @@ positionFragmentH line originY originX (WithSpan rs pf) =
        rect = Rect originX originY (PF.advance pf) (-lineHeight)
        penX = 0
        penY = descent + leading `div` 2 - lineHeight
        lineHeight = case RS.spanLineHeight rs of
        lineHeight = case textLineHeight opts 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
        extents = fontExtentsForDir (textFont opts) (PF.direction pf)
        opts = RS.spanTextOptions rs

-- | Calculate layout for multiple horizontal runs, breaking them as necessary
-- to fit as much content as possible without exceeding the maximum line width,


@@ 223,13 224,13 @@ layoutRunH (WithSpan rs run) = WithSpan rs pf
shapeRun :: WithSpan d Run -> [(GlyphInfo, GlyphPos)]
shapeRun (WithSpan rs run) = shape font buffer features
    where
        font = RS.spanFont rs
        font = textFont opts
        buffer = defaultBuffer
            { text = Lazy.fromStrict $ runText run
            , contentType = Just ContentTypeUnicode
            , direction = runDirection run
            , script = runScript run
            , language = Just $ RS.spanLanguage rs
            , 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.


@@ 243,6 244,7 @@ shapeRun (WithSpan rs run) = shape font buffer features
            , endsText = True
            }
        features = []
        opts = RS.spanTextOptions rs

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

M src/Data/Text/ParagraphLayout/Internal/Plain.hs => src/Data/Text/ParagraphLayout/Internal/Plain.hs +6 -3
@@ 16,6 16,7 @@ import Data.Text.ParagraphLayout.Internal.ResolvedSpan (WithSpan (WithSpan))
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.TextOptions

-- | Lay out a paragraph of plain, unidirectional text using a single font.
layoutPlain :: Paragraph d -> ParagraphLayout d


@@ 60,9 61,11 @@ resolveSpans p@(Paragraph _ pStart spans pOpts) = do
        , RS.spanIndex = i
        , RS.spanOffsetInParagraph = sStart - pStart
        , RS.spanText = sText
        , RS.spanFont = paragraphFont pOpts
        , RS.spanLineHeight = paragraphLineHeight pOpts
        , RS.spanLanguage = lang
        , RS.spanTextOptions = defaultTextOptions
            { textFont = paragraphFont pOpts
            , textLineHeight = paragraphLineHeight pOpts
            , textLanguage = lang
            }
        , RS.spanLineBreaks = subOffsetsDesc (sStart - pStart) lBreaks
        , RS.spanCharacterBreaks = subOffsetsDesc (sStart - pStart) cBreaks
        }

M src/Data/Text/ParagraphLayout/Internal/ResolvedSpan.hs => src/Data/Text/ParagraphLayout/Internal/ResolvedSpan.hs +2 -6
@@ 6,12 6,11 @@ module Data.Text.ParagraphLayout.Internal.ResolvedSpan
where

import Data.Text (Text)
import Data.Text.Glyphize (Font)
import qualified Data.Text.ICU as BreakStatus (Line)

import Data.Text.ParagraphLayout.Internal.BiDiReorder
import Data.Text.ParagraphLayout.Internal.LineHeight
import Data.Text.ParagraphLayout.Internal.TextContainer
import Data.Text.ParagraphLayout.Internal.TextOptions

-- | Internal structure containing resolved values that may be shared with
-- other spans across the paragraph.


@@ 20,14 19,11 @@ data ResolvedSpan d = ResolvedSpan
    , spanIndex :: Int
    , spanOffsetInParagraph :: Int
    , spanText :: Text
    , spanFont :: Font
    , spanLineHeight :: LineHeight
    , spanLanguage :: String
    , spanTextOptions :: TextOptions
    , spanLineBreaks :: [(Int, BreakStatus.Line)]
    -- TODO: Can be optimised by starting with the shortest line break.
    , spanCharacterBreaks :: [(Int, ())]
    }
    deriving (Show)

instance Eq (ResolvedSpan d) where
    a == b = spanIndex a == spanIndex b

M test/Data/Text/ParagraphLayout/SpanData.hs => test/Data/Text/ParagraphLayout/SpanData.hs +16 -9
@@ 10,6 10,7 @@ import Data.Text.Glyphize (Font)

import Data.Text.ParagraphLayout.Internal.LineHeight
import Data.Text.ParagraphLayout.Internal.ResolvedSpan
import Data.Text.ParagraphLayout.Internal.TextOptions

emptySpan :: Font -> ResolvedSpan ()
emptySpan font = ResolvedSpan


@@ 17,9 18,11 @@ emptySpan font = ResolvedSpan
    , spanIndex = 0
    , spanOffsetInParagraph = 0
    , spanText = pack ""
    , spanFont = font
    , spanLineHeight = Normal
    , spanLanguage = "en"
    , spanTextOptions = defaultTextOptions
        { textFont = font
        , textLineHeight = Normal
        , textLanguage = "en"
        }
    , spanLineBreaks = []
    , spanCharacterBreaks = []
    }


@@ 30,9 33,11 @@ czechHello font = ResolvedSpan
    , spanIndex = 0
    , spanOffsetInParagraph = 0
    , spanText = pack "Ahoj, světe!"
    , spanFont = font
    , spanLineHeight = Normal
    , spanLanguage = "cs"
    , spanTextOptions = defaultTextOptions
        { textFont = font
        , textLineHeight = Normal
        , textLanguage = "cs"
        }
    , spanLineBreaks = []
    , spanCharacterBreaks = []
    }


@@ 43,9 48,11 @@ serbianMixedScript font = ResolvedSpan
    , spanIndex = 0
    , spanOffsetInParagraph = 0
    , spanText = pack "Vikipedija (Википедија)"
    , spanFont = font
    , spanLineHeight = Normal
    , spanLanguage = "sr"
    , spanTextOptions = defaultTextOptions
        { textFont = font
        , textLineHeight = Normal
        , textLanguage = "sr"
        }
    , spanLineBreaks = []
    , spanCharacterBreaks = []
    }