~alcinnz/fontconfig-pure

c71ba262a73912d376d731a5bd766a40bc8af9d3 — Adrian Cochrane 5 months ago 484b148
Test font weight conversion & font name parsing.
2 files changed, 39 insertions(+), 4 deletions(-)

M lib/Graphics/Text/Font/Choose/Weight.hs
M test/Main.hs
M lib/Graphics/Text/Font/Choose/Weight.hs => lib/Graphics/Text/Font/Choose/Weight.hs +4 -4
@@ 17,8 17,8 @@ foreign import capi "fontconfig/fontconfig.h FcWeightFromOpenTypeDouble"
foreign import capi "fontconfig/fontconfig.h FcWeightToOpenTypeDouble"
    weightToOpenTypeDouble :: Double -> Double
-- | Like weightFromOpenTypeDouble but with integer arguments. Use the other function instead.
foreign import capi "fontconfig/fontconfig.h FcWeightFromOpenType"
    weightFromOpenType :: Int -> Int
weightFromOpenType :: Int -> Int
weightFromOpenType = fromEnum . weightFromOpenTypeDouble . toEnum
-- | Like weightToOpenTypeDouble but with integer arguments. Use the other function instead.
foreign import capi "fontconfig/fontconfig.h FcWeightToOpenType" 
    weightToOpenType :: Int -> Int
weightToOpenType :: Int -> Int
weightToOpenType = fromEnum . weightToOpenTypeDouble . toEnum

M test/Main.hs => test/Main.hs +35 -0
@@ 11,6 11,7 @@ import qualified Data.Map as M
import qualified Data.Set as S
import qualified Data.Text as Txt
import Data.Maybe (isJust)
import GHC.Real (infinity)

import Graphics.Text.Font.Choose
import Graphics.Text.Font.Choose.Internal.Test


@@ 96,3 97,37 @@ main = hspec $ do
            -- A couple additional ones so we know its not always responding with DifferentTerritory!
            S.singleton "mn-mn" `cmp` S.singleton "mn-mn" `shouldBe` SameLang
            S.singleton "mn-mn" `cmp` S.singleton "pap-an" `shouldBe` DifferentLang
        it "Font weights" $ do
            weightFromOpenTypeDouble (fromRational infinity) `shouldBe` 215
            weightFromOpenType maxBound `shouldBe` 215
        it "Name parsing" $ do
            nameParse "sans\\-serif" `shouldBe` M.fromList [
                ("family", [(Strong, ValueString "sans-serif")])
              ]
            nameParse "Foo-10" `shouldBe` M.fromList [
                ("family", [(Strong, ValueString "Foo")]),
                -- NOTE: Equality derived from the pure-Haskell type is stricter than FontConfig's.
                -- This subtest might fail in the future, not sure what to do about that.
                ("size", [(Strong, ValueDouble 10)])
              ]
            nameParse "Foo,Bar-10" `shouldBe` M.fromList [
                ("family", [(Strong, ValueString "Foo"), (Strong, ValueString "Bar")]),
                ("size", [(Strong, ValueDouble 10)])
              ]
            nameParse "Foo:weight=medium" `shouldBe` M.fromList [
                ("family", [(Strong, ValueString "Foo")]),
                ("weight", [(Strong, ValueDouble 100)])
              ]
            nameParse "Foo:weight_medium" `shouldBe` M.fromList [
                ("family", [(Strong, ValueString "Foo")]),
                ("weight", [(Strong, ValueDouble 100)])
              ]
            nameParse ":medium" `shouldBe` M.fromList [
                ("weight", [(Strong, ValueInt 100)])
              ]
            nameParse ":normal" `shouldBe` M.fromList [
                ("width", [(Strong, ValueInt 100)])
              ]
            nameParse ":weight=[medium bold]" `shouldBe` M.fromList [
                ("weight", [(Strong, ValueRange $ Range 100 200)])
              ]