From f05881ece7d924202437aa5e85d551479f40af93 Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Fri, 2 Dec 2022 14:12:15 +1300 Subject: [PATCH] Parse src property for @font-face into custom FontConfig property. --- Graphics/Text/Font/Choose/FontSet.hs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Graphics/Text/Font/Choose/FontSet.hs b/Graphics/Text/Font/Choose/FontSet.hs index 85ca81c..9d2bad2 100644 --- a/Graphics/Text/Font/Choose/FontSet.hs +++ b/Graphics/Text/Font/Choose/FontSet.hs @@ -71,6 +71,26 @@ thawFontSet_ cb = bracket (throwNull <$> cb) fcFontSetDestroy thawFontSet data FontFaceParser a = FontFaceParser { cssFonts :: FontSet, cssInner :: a} +parseFontFaceSrc (Function "local":Ident name:RightParen:Comma:rest) = + ("local:" ++ unpack name):parseFontFaceSrc rest +parseFontFaceSrc (Function "local":String name:RightParen:Comma:rest) = + ("local:" ++ unpack name):parseFontFaceSrc rest +parseFontFaceSrc (Function "local":Ident name:RightParen:[]) = ["local:" ++ unpack name] +parseFontFaceSrc (Function "local":String name:RightParen:[]) = ["local:" ++ unpack name] + +parseFontFaceSrc (Url link:toks) + | Comma:rest <- skipMeta toks = unpack link:parseFontFaceSrc rest + | [] <- skipMeta toks = [unpack link] + | otherwise = [""] -- Error indicator! + where + skipMeta (Function "format":Ident _:RightParen:rest) = skipMeta rest + skipMeta (Function "format":String _:RightParen:rest) = skipMeta rest + skipMeta (Function "tech":Ident _:RightParen:rest) = skipMeta rest + skipMeta (Function "tech":String _:RightParen:rest) = skipMeta rest + skipMeta toks = toks + +parseFontFaceSrc _ = [""] + properties2font :: [(Text, [Token])] -> Pattern properties2font (("font-family", [String font]):props) = setValue "family" Strong (unpack font) $ properties2font props @@ -102,6 +122,11 @@ properties2font (("unicode-range", toks):props) | Just chars <- parseCharSet $ unpack $ serialize toks = setValue "charset" Strong chars $ properties2font props +-- Ignoring metadata & trusting in FreeType's broad support for fonts. +properties2font (("src", toks):props) + | fonts@(_:_) <- parseFontFaceSrc toks, "" `notElem` fonts = + setValue "web-src" Strong (intercalate "\t" fonts) $ properties2font props + properties2font (_:props) = properties2font props properties2font [] = [] -- 2.30.2