~jaro/balkon

034a323deb9c3858d2396406d7e22410984ba4e6 — Jaro 1 year, 8 months ago 65d952e
Make SpanOptions a separate type.
M lib/Data/Text/ParagraphLayout.hs => lib/Data/Text/ParagraphLayout.hs +2 -1
@@ 23,8 23,9 @@ module Data.Text.ParagraphLayout
        ,paragraphLineHeight
        ,paragraphMaxWidth
        )
    ,Span(Span, spanLanguage, spanLength)
    ,Span(Span, spanLength, spanOptions)
    ,SpanLayout(SpanLayout)
    ,SpanOptions(SpanOptions, spanLanguage)
    ,layoutPlain
    )
where

M lib/Data/Text/ParagraphLayout/ParagraphConstruction.hs => lib/Data/Text/ParagraphLayout/ParagraphConstruction.hs +1 -1
@@ 1,7 1,7 @@
-- | Infix operators for construction of paragraphs with readable code.
--
-- Example construction:
-- @"ignored prefix" |\< "en"~"one two " \>|\< "ja"~"三四" \>| "ignored suffix"@
-- @"ignored prefix" |\< en~"one two " \>|\< ja~"三四" \>| "ignored suffix"@
--
-- Special syntax for paragraphs with no contents:
-- @"ignored prefix" |<>| "ignored suffix"@

M src/Data/Text/ParagraphLayout/Internal/ParagraphConstruction.hs => src/Data/Text/ParagraphLayout/Internal/ParagraphConstruction.hs +5 -5
@@ 17,11 17,11 @@ import Data.Text.ParagraphLayout.Internal.Plain
    (Paragraph(Paragraph)
    ,ParagraphOptions
    )
import Data.Text.ParagraphLayout.Internal.Span (Span(Span))
import Data.Text.ParagraphLayout.Internal.Span (Span(Span), SpanOptions)

-- | Create first span with optional ignored suffix.
infixr 5 >|
(>|) :: (String, String) -> String -> (Lazy.Text, [Span])
(>|) :: (SpanOptions, String) -> String -> (Lazy.Text, [Span])
(spanLanguage, spanText) >| suffix = (newText, newSpans)
    where
        newSpans = [newSpan]


@@ 32,7 32,7 @@ infixr 5 >|

-- | Create next span.
infixr 5 >|<
(>|<) :: (String, String) -> (Lazy.Text, [Span]) -> (Lazy.Text, [Span])
(>|<) :: (SpanOptions, String) -> (Lazy.Text, [Span]) -> (Lazy.Text, [Span])
(spanLanguage, spanText) >|< (oldText, oldSpans) = (newText, newSpans)
    where
        newSpans = newSpan:oldSpans


@@ 56,5 56,5 @@ prefix |<>| suffix = prefix |< (chunk (pack suffix) empty, [])

-- | Combine language with text.
infix 6 ~
(~) :: String -> String -> (String, String)
lang ~ txt = (lang, txt)
(~) :: SpanOptions -> String -> (SpanOptions, String)
opts ~ txt = (opts, txt)

M src/Data/Text/ParagraphLayout/Internal/Plain.hs => src/Data/Text/ParagraphLayout/Internal/Plain.hs +4 -4
@@ 214,7 214,7 @@ shapeRun (WithSpan rs run) = shape font buffer features
        features = []

resolveSpans :: Paragraph -> [RS.ResolvedSpan]
resolveSpans p@(Paragraph arr pStart spans opts) = do
resolveSpans p@(Paragraph arr pStart spans pOpts) = do
    let sBounds = paragraphSpanBounds p
    let pEnd = NonEmpty.last sBounds
    let sStarts = NonEmpty.init sBounds


@@ 225,15 225,15 @@ resolveSpans p@(Paragraph arr pStart spans opts) = do
        <*> ZipList spans
        <*> ZipList sStarts
        <*> ZipList sLengths
    let lang = spanLanguage s
    let lang = spanLanguage $ spanOptions s
    let breaks = paragraphLineBreaks p pEnd lang
    return RS.ResolvedSpan
        { RS.spanIndex = i
        , RS.spanOffsetInParagraph = sStart - pStart
        -- TODO: Consider adding checks for array bounds.
        , RS.spanText = Text arr sStart sLen
        , RS.spanFont = paragraphFont opts
        , RS.spanLineHeight = paragraphLineHeight opts
        , RS.spanFont = paragraphFont pOpts
        , RS.spanLineHeight = paragraphLineHeight pOpts
        , RS.spanLanguage = lang
        , RS.spanLineBreaks = subOffsetsDesc (sStart - pStart) breaks
        }

M src/Data/Text/ParagraphLayout/Internal/Span.hs => src/Data/Text/ParagraphLayout/Internal/Span.hs +11 -2
@@ 1,6 1,7 @@
module Data.Text.ParagraphLayout.Internal.Span
    (Span(..)
    ,SpanLayout(..)
    ,SpanOptions(..)
    ,spanRects
    )
where


@@ 15,12 16,20 @@ import Data.Text.ParagraphLayout.Internal.Rect
-- Each span could have a different font family, size, style, text decoration,
-- colour, language, etc.
data Span = Span
-- TODO: Add all relevant attributes.

    { spanLength :: Int
    -- ^ Byte offset to the next span or the end of the paragraph text.

    , spanLanguage :: String
    , spanOptions :: SpanOptions
    -- ^ Options applying to this specific span.

    }
    deriving (Eq, Read, Show)

-- TODO: Add all relevant attributes.
data SpanOptions = SpanOptions

    { spanLanguage :: String
    -- ^ Used for selecting the appropriate glyphs and line breaking rules.

    }

M test/Data/Text/ParagraphLayout/ParagraphData.hs => test/Data/Text/ParagraphLayout/ParagraphData.hs +29 -9
@@ 11,38 11,58 @@ module Data.Text.ParagraphLayout.ParagraphData
    )
where

import Data.Text.ParagraphLayout (Paragraph, ParagraphOptions)
import Data.Text.ParagraphLayout (Paragraph, ParagraphOptions, SpanOptions(..))
import Data.Text.ParagraphLayout.ParagraphConstruction

-- | Span with text in the Czech language.
cs :: SpanOptions
cs = SpanOptions { spanLanguage = "cs" }

-- | Span with text in the English language.
en :: SpanOptions
en = SpanOptions { spanLanguage = "en" }

-- | Span with text in the Japanese language.
ja :: SpanOptions
ja = SpanOptions { spanLanguage = "ja" }

-- | Span with text in the Serbian language.
sr :: SpanOptions
sr = SpanOptions { spanLanguage = "sr" }

-- | Span with text in no language.
zxx :: SpanOptions
zxx = SpanOptions { spanLanguage = "zxx" }

emptyParagraph :: ParagraphOptions -> Paragraph
emptyParagraph = "x" |<>| "zzzzzzz"

emptySpanParagraph :: ParagraphOptions -> Paragraph
emptySpanParagraph = "xx" |< "en"~"" >| "zzzzz"
emptySpanParagraph = "xx" |< en~"" >| "zzzzz"

trivialParagraph :: ParagraphOptions -> Paragraph
trivialParagraph = "xxx" |< "en"~"a" >| "zzz"
trivialParagraph = "xxx" |< en~"a" >| "zzz"

-- | Fillter text using the Arabic script.
-- Source: <https://generator.lorem-ipsum.info/_arabic>
arabicFillerParagraph :: ParagraphOptions -> Paragraph
arabicFillerParagraph = "xxxx" |< "zxx"~"إعلان بأيدي وبغطاء هذه من. عرض غينيا يتمكن واعتلاء في. و فرنسا الثانية وفي, أسر إذ السبب ارتكبها مليارات. فكان الشتاء، ما حتى, غير أن وصغار الأخذ. في الصفحة لهيمنة وتتحمّل وتم, أن أما وبداية الغالي." >| "z"
arabicFillerParagraph = "xxxx" |< zxx~"إعلان بأيدي وبغطاء هذه من. عرض غينيا يتمكن واعتلاء في. و فرنسا الثانية وفي, أسر إذ السبب ارتكبها مليارات. فكان الشتاء، ما حتى, غير أن وصغار الأخذ. في الصفحة لهيمنة وتتحمّل وتم, أن أما وبداية الغالي." >| "z"

-- | Fillter text using the Latin script.
-- | Source: <https://www.lipsum.com/>
loremIpsumParagraph :: ParagraphOptions -> Paragraph
loremIpsumParagraph = "xxxx" |< "zxx"~"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." >| "z"
loremIpsumParagraph = "xxxx" |< zxx~"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." >| "z"

czechHelloParagraph :: ParagraphOptions -> Paragraph
czechHelloParagraph = "xxxxx" |< "cs"~"Ahoj, světe!" >| "zz"
czechHelloParagraph = "xxxxx" |< cs~"Ahoj, světe!" >| "zz"

mixedScriptSerbianParagraph :: ParagraphOptions -> Paragraph
mixedScriptSerbianParagraph = "xxxxxx" |< "sr"~"Vikipedija (Википедија)" >| "zzzz"
mixedScriptSerbianParagraph = "xxxxxx" |< sr~"Vikipedija (Википедија)" >| "zzzz"

-- | For testing line breaking on boundaries that are different from script
-- boundaries.
mixedScriptWordsParagraph :: ParagraphOptions -> Paragraph
mixedScriptWordsParagraph = "xxxxxxx" |< "zxx"~"jjjжжжjjj jjjжжжjjj жжжjjjжжж жжжjjjжжж jжj jжj жjж жjж" >| "zzzzzzzz"
mixedScriptWordsParagraph = "xxxxxxx" |< zxx~"jjjжжжjjj jjjжжжjjj жжжjjjжжж жжжjjjжжж jжj jжj жjж жjж" >| "zzzzzzzz"

mixedLanguageLTRParagraph :: ParagraphOptions -> Paragraph
mixedLanguageLTRParagraph = "Tak " |< "cs"~"jsem tady, " >|< "ja"~"世界!" >| "zzzzzz"
mixedLanguageLTRParagraph = "Tak " |< cs~"jsem tady, " >|< ja~"世界!" >| "zzzzzz"