From 0bee8136697d2c892cbb567c2780cfa35cb8d01b Mon Sep 17 00:00:00 2001 From: Jaro Date: Sun, 30 Apr 2023 23:45:40 +0200 Subject: [PATCH] Test Rich layout. --- .../richParagraphLayout/nestedBoxes.golden | 60 +++++++++++++++++++ balkon.cabal | 2 + test/Data/Text/ParagraphLayout/PrettyShow.hs | 28 ++++++++- .../ParagraphLayout/Rich/ParagraphData.hs | 43 +++++++++++++ test/Data/Text/ParagraphLayout/RichSpec.hs | 25 ++++++++ 5 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 .golden/richParagraphLayout/nestedBoxes.golden create mode 100644 test/Data/Text/ParagraphLayout/Rich/ParagraphData.hs create mode 100644 test/Data/Text/ParagraphLayout/RichSpec.hs diff --git a/.golden/richParagraphLayout/nestedBoxes.golden b/.golden/richParagraphLayout/nestedBoxes.golden new file mode 100644 index 0000000..15940d2 --- /dev/null +++ b/.golden/richParagraphLayout/nestedBoxes.golden @@ -0,0 +1,60 @@ +ParagraphLayout + { paragraphRect = Rect {x_origin = 0, y_origin = 0, x_size = 14576, y_size = -1121} + , paragraphFragments = + [ Fragment + { fragmentUserData = "text1" + , fragmentLine = 1 + , fragmentRect = Rect {x_origin = 0, y_origin = 0, x_size = 8569, y_size = -1121} + , fragmentPen = (0, -932) + , fragmentGlyphs = + [ (GlyphInfo {codepoint = 87, cluster = 6, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 402, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 75, cluster = 7, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 571, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 76, cluster = 8, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 253, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 86, cluster = 9, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 446, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 3, cluster = 10, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 231, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 83, cluster = 11, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 589, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 68, cluster = 12, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 522, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 85, cluster = 13, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 386, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 68, cluster = 14, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 522, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 74, cluster = 15, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 578, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 85, cluster = 16, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 386, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 68, cluster = 17, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 522, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 83, cluster = 18, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 589, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 75, cluster = 19, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 571, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 3, cluster = 20, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 231, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 75, cluster = 21, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 571, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 68, cluster = 22, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 522, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 86, cluster = 23, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 446, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 3, cluster = 24, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 231, y_advance = 0, x_offset = 0, y_offset = 0}) + ] + } + , Fragment + { fragmentUserData = "text2" + , fragmentLine = 1 + , fragmentRect = Rect {x_origin = 8569, y_origin = 0, x_size = 3350, y_size = -1121} + , fragmentPen = (0, -932) + , fragmentGlyphs = + [ (GlyphInfo {codepoint = 81, cluster = 25, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 574, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 72, cluster = 26, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 559, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 86, cluster = 27, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 446, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 87, cluster = 28, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 392, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 72, cluster = 29, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 559, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 71, cluster = 30, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 589, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 3, cluster = 31, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 231, y_advance = 0, x_offset = 0, y_offset = 0}) + ] + } + , Fragment + { fragmentUserData = "text3" + , fragmentLine = 1 + , fragmentRect = Rect {x_origin = 11919, y_origin = 0, x_size = 2657, y_size = -1121} + , fragmentPen = (0, -932) + , fragmentGlyphs = + [ (GlyphInfo {codepoint = 69, cluster = 32, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 589, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 82, cluster = 33, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 573, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 91, cluster = 34, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 490, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 72, cluster = 35, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 559, y_advance = 0, x_offset = 0, y_offset = 0}) + , (GlyphInfo {codepoint = 86, cluster = 36, unsafeToBreak = False, unsafeToConcat = False, safeToInsertTatweel = False}, GlyphPos {x_advance = 446, y_advance = 0, x_offset = 0, y_offset = 0}) + ] + } + ] + } diff --git a/balkon.cabal b/balkon.cabal index 4c576bb..c37595f 100644 --- a/balkon.cabal +++ b/balkon.cabal @@ -188,6 +188,8 @@ test-suite balkon-test Data.Text.ParagraphLayout.PrettyShow, Data.Text.ParagraphLayout.PrettyShow.Golden, Data.Text.ParagraphLayout.RectSpec, + Data.Text.ParagraphLayout.Rich.ParagraphData, + Data.Text.ParagraphLayout.RichSpec, Data.Text.ParagraphLayout.SpanData -- Test dependencies. diff --git a/test/Data/Text/ParagraphLayout/PrettyShow.hs b/test/Data/Text/ParagraphLayout/PrettyShow.hs index a024a5e..b739e69 100644 --- a/test/Data/Text/ParagraphLayout/PrettyShow.hs +++ b/test/Data/Text/ParagraphLayout/PrettyShow.hs @@ -9,8 +9,12 @@ where import Data.List (intersperse) import Data.Text.ParagraphLayout -import Data.Text.ParagraphLayout.Internal.Fragment (ShapedRun) +import Data.Text.ParagraphLayout.Internal.Fragment + ( Fragment (Fragment) + , ShapedRun + ) import qualified Data.Text.ParagraphLayout.Plain as Plain +import qualified Data.Text.ParagraphLayout.Rich as Rich class PrettyShow a where prettyShow :: a -> String @@ -82,8 +86,8 @@ instance Show d => PrettyShow (Plain.SpanLayout d) where , concat $ commaFirstList indent2 $ map prettyShow frags ] -instance Show d => PrettyShow (Plain.Fragment d) where - prettyShow (Plain.Fragment d l r pen glyphs) = concat +instance Show d => PrettyShow (Fragment d) where + prettyShow (Fragment d l r pen glyphs) = concat [ "Fragment" , newline , indent3 @@ -111,6 +115,24 @@ instance Show d => PrettyShow (Plain.Fragment d) where , "}" ] +instance Show d => PrettyShow (Rich.ParagraphLayout d) where + prettyShow (Rich.ParagraphLayout pr frags) = concat + [ "ParagraphLayout" + , newline + , indent1 + , "{ paragraphRect = " + , show pr + , newline + , indent1 + , ", paragraphFragments =" + , newline + , concat $ commaFirstList indent2 $ map prettyShow frags + , newline + , indent1 + , "}" + , newline + ] + prettyShowPair :: (Show a, Show b) => (a, b) -> String prettyShowPair (a, b) = "(" ++ show a ++ ", " ++ show b ++ ")" diff --git a/test/Data/Text/ParagraphLayout/Rich/ParagraphData.hs b/test/Data/Text/ParagraphLayout/Rich/ParagraphData.hs new file mode 100644 index 0000000..cce0c34 --- /dev/null +++ b/test/Data/Text/ParagraphLayout/Rich/ParagraphData.hs @@ -0,0 +1,43 @@ +module Data.Text.ParagraphLayout.Rich.ParagraphData (nestedBoxesParagraph) +where + +import Data.Text (pack) +import Data.Text.Glyphize (Font) + +import Data.Text.ParagraphLayout.Internal.BoxOptions +import Data.Text.ParagraphLayout.Internal.ParagraphOptions +import Data.Text.ParagraphLayout.Internal.Rich.Paragraph +import Data.Text.ParagraphLayout.Internal.TextOptions +import Data.Text.ParagraphLayout.Internal.TreeOfTexts + +t_ :: TextOptions +t_ = defaultTextOptions + +b_ :: BoxOptions +b_ = defaultBoxOptions + +rootBox :: TextOptions -> [InnerNode d] -> RootNode d +rootBox opts nodes = RootBox (Box nodes opts) + +box :: d -> BoxOptions -> TextOptions -> [InnerNode d] -> InnerNode d +box label boxOpts textOpts nodes = InlineBox label (Box nodes textOpts) boxOpts + +text :: d -> String -> InnerNode d +text label contents = TextSequence label (pack contents) + +nestedBoxesParagraph :: Font -> ParagraphOptions -> Paragraph String +nestedBoxesParagraph font = constructParagraph + (pack "prefix") + (rootBox t + [ text "text1" "this paragraph has " + , box "box2" b_ t + [ box "box3" b_ t + [ text "text2" "nested " + ] + , text "text3" "boxes" + ] + ] + ) + (pack "suffix") + where + t = t_ { textFont = font, textLanguage = "en" } diff --git a/test/Data/Text/ParagraphLayout/RichSpec.hs b/test/Data/Text/ParagraphLayout/RichSpec.hs new file mode 100644 index 0000000..ddd4f31 --- /dev/null +++ b/test/Data/Text/ParagraphLayout/RichSpec.hs @@ -0,0 +1,25 @@ +module Data.Text.ParagraphLayout.RichSpec (spec) where + +import Test.Hspec +import System.FilePath (()) +import Data.Text.ParagraphLayout.FontLoader +import Data.Text.ParagraphLayout.PrettyShow.Golden +import Data.Text.ParagraphLayout.Rich +import Data.Text.ParagraphLayout.Rich.ParagraphData + +spec :: Spec +spec = do + + describe "layoutRich" $ do + let + goldenDir = ".golden" "richParagraphLayout" + shouldBeGolden = goldenTest goldenDir id id + + describe "with Latin font" $ do + font <- runIO $ loadFont latinFont 0 testingOptions + + it "handles nested boxes" $ do + let opts = defaultParagraphOptions + let input = nestedBoxesParagraph font opts + let result = layoutRich input + result `shouldBeGolden` "nestedBoxes" -- 2.30.2