HPDF 1.5.3 → 1.6.0
raw patch · 11 files changed
+70/−95 lines, 11 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Graphics.PDF.Fonts.Type1: getAfmData :: FilePath -> IO AFMData
+ Graphics.PDF.Fonts.Type1: parseAfmData :: ByteString -> Either ParseError AFMData
+ Graphics.PDF.Fonts.Type1: readAfmData :: FilePath -> IO (Either ParseError AFMData)
- Graphics.PDF: readType1Font :: FilePath -> FilePath -> IO Type1FontStructure
+ Graphics.PDF: readType1Font :: FilePath -> FilePath -> IO (Either ParseError Type1FontStructure)
- Graphics.PDF.Fonts.StandardFont: mkStdFont :: FontName -> IO (Maybe AnyFont)
+ Graphics.PDF.Fonts.StandardFont: mkStdFont :: FontName -> IO (Either ParseError AnyFont)
- Graphics.PDF.Fonts.Type1: mkType1FontStructure :: FontData -> AFMData -> IO (Maybe Type1FontStructure)
+ Graphics.PDF.Fonts.Type1: mkType1FontStructure :: FontData -> AFMData -> IO Type1FontStructure
Files
- Graphics/PDF/Fonts/AFMParser.hs +9/−33
- Graphics/PDF/Fonts/StandardFont.hs +8/−10
- Graphics/PDF/Fonts/Type1.hs +14/−12
- Graphics/PDF/Pages.hs +6/−6
- HPDF.cabal +4/−5
- README.md +2/−2
- Test/test.hs +5/−5
- c/conversion.c +0/−17
- c/conversion.h +0/−5
- cbits/conversion.c +17/−0
- cbits/conversion.h +5/−0
Graphics/PDF/Fonts/AFMParser.hs view
@@ -14,16 +14,15 @@ -- AFM AFMParser --------------------------------------------------------- module Graphics.PDF.Fonts.AFMParser(- getFont- , AFMFont(..)+ AFMFont(..) , EncodingScheme(..) , Metric(..) , KX(..)- , parseFont+ , parseAfm+ , fontToStructure ) where import Data.ByteString (ByteString)-import qualified Data.ByteString as B import Data.ByteString.Char8 (unpack) import Text.ParserCombinators.Parsec hiding(space) import Text.Parsec(modifyState)@@ -343,8 +342,8 @@ -- Otherwise we use the encoding we found in the afm file. -- It is used to force MacRomanEncoding on not symbolic default fonts. fontToStructure :: AFMFont - -> M.Map PostscriptName Char - -> Maybe (M.Map PostscriptName GlyphCode)+ -> M.Map PostscriptName Char -- ^ Glyph name to unicode+ -> Maybe (M.Map PostscriptName GlyphCode) -- ^ Glyph name to glyph code if not standard coding -> FontStructure fontToStructure afm' encoding' maybeMapNameToGlyph = let h = (afmAscent afm' - afmDescent afm') @@ -382,31 +381,8 @@ Nothing -> fs2 Just k -> foldr (addKern nameToGlyph) fs2 k -afmParseFromFile :: AFMParser AFMFont -> FilePath -> ByteString -> IO (Either ParseError AFMFont)-afmParseFromFile p path bs = do - return $ runParser p emptyAFM path (unpack bs)--parseFont :: Either ByteString String -> IO (Maybe AFMFont)-parseFont (Left bs) = do- r <- afmParseFromFile afm "<embedded>" bs- case r of- Left e -> error (show e)- Right r' -> return $ Just r'-parseFont (Right path) = do- bs <- B.readFile path- r <- afmParseFromFile afm path bs- case r of- Left e -> error (show e)- Right r' -> return $ Just r'--getFont :: Either ByteString AFMFont- -> M.Map PostscriptName Char -- ^ Glyph name to unicode- -> Maybe (M.Map PostscriptName GlyphCode) -- ^ Glyph name to glyph code if not standard coding- -> IO (Maybe FontStructure)-getFont (Left s) encoding' nameToGlyph = do - result <- parseFont (Left s)- case result of - Nothing -> return Nothing - Just r -> return (Just $ fontToStructure r encoding' nameToGlyph)-getFont (Right result) encoding' nameToGlyph = return . Just $ fontToStructure result encoding' nameToGlyph+afmParseFromFile :: AFMParser AFMFont -> FilePath -> ByteString -> Either ParseError AFMFont+afmParseFromFile p path bs = runParser p emptyAFM path (unpack bs) +parseAfm :: FilePath -> ByteString -> Either ParseError AFMFont+parseAfm = afmParseFromFile afm
Graphics/PDF/Fonts/StandardFont.hs view
@@ -28,9 +28,10 @@ import Graphics.PDF.Resources import qualified Data.Map.Strict as M import Graphics.PDF.Fonts.Font-import Graphics.PDF.Fonts.AFMParser(getFont)+import Graphics.PDF.Fonts.AFMParser(fontToStructure, parseAfm) import Graphics.PDF.Fonts.Encoding import Graphics.PDF.Fonts.FontTypes+import Text.Parsec.Error(ParseError) data FontName = Helvetica @@ -104,7 +105,7 @@ hyphenGlyph (StdFont fs) = hyphen fs spaceGlyph (StdFont fs) = space fs -mkStdFont :: FontName -> IO (Maybe AnyFont)+mkStdFont :: FontName -> IO (Either ParseError AnyFont) mkStdFont f = do theEncoding <- case f of ZapfDingbats -> getEncoding ZapfDingbatsEncoding @@ -113,11 +114,8 @@ ZapfDingbats -> return Nothing Symbol -> return Nothing _ -> parseMacEncoding >>= return . Just- maybeFs <- getFont (Left $ embeddedFont f) theEncoding theMacEncoding- case maybeFs of - Just theFont -> do- let f' = theFont { baseFont = show f- }- return . Just . AnyFont . StdFont $ f'- Nothing -> return Nothing-+ return $ case parseAfm "<embedded>" $ embeddedFont f of + Left pe -> Left pe + Right r -> Right $ let theFont = fontToStructure r theEncoding theMacEncoding+ f' = theFont { baseFont = show f }+ in AnyFont $ StdFont f'
Graphics/PDF/Fonts/Type1.hs view
@@ -18,7 +18,8 @@ , Type1Font(..) , AFMData , Type1FontStructure(..)- , getAfmData+ , readAfmData+ , parseAfmData , mkType1FontStructure ) where @@ -29,8 +30,11 @@ -- import Graphics.PDF.Fonts.AFMParser import Graphics.PDF.Fonts.Encoding import Graphics.PDF.Fonts.FontTypes-import Graphics.PDF.Fonts.AFMParser (AFMFont, getFont, parseFont)+import Graphics.PDF.Fonts.AFMParser (AFMFont, fontToStructure, parseAfm)+import qualified Data.ByteString as B import Data.List+import Data.Bifunctor (Bifunctor(second))+import Text.Parsec.Error (ParseError) data Type1Font = Type1Font FontStructure (PDFReference EmbeddedFont) deriving Show @@ -47,19 +51,17 @@ data AFMData = AFMData AFMFont deriving Show data Type1FontStructure = Type1FontStructure FontData FontStructure -getAfmData :: FilePath -> IO AFMData -getAfmData path = do - Just r <- parseFont (Right path) - return (AFMData r)+readAfmData :: FilePath -> IO (Either ParseError AFMData)+readAfmData path = second AFMData . parseAfm path <$> B.readFile path -mkType1FontStructure :: FontData -> AFMData -> IO (Maybe Type1FontStructure)+parseAfmData :: B.ByteString -> Either ParseError AFMData+parseAfmData bs = second AFMData $ parseAfm "<bytestring>" bs++mkType1FontStructure :: FontData -> AFMData -> IO Type1FontStructure mkType1FontStructure pdfRef (AFMData f) = do theEncoding <- getEncoding AdobeStandardEncoding- maybeFs <- getFont (Right f) theEncoding Nothing- case maybeFs of - Just theFont -> - return . Just $ Type1FontStructure pdfRef theFont- Nothing -> return Nothing+ let theFont = fontToStructure f theEncoding Nothing+ return $ Type1FontStructure pdfRef theFont
Graphics/PDF/Pages.hs view
@@ -49,6 +49,7 @@ import Data.Binary.Builder(fromByteString) import Graphics.PDF.Fonts.FontTypes(FontData(..)) import Graphics.PDF.Fonts.Type1 +import Text.Parsec.Error (ParseError) -- | Set page annotations setPageAnnotations :: [AnyAnnotation] -> PDFReference PDFPage -> PDF ()@@ -282,16 +283,15 @@ -- | Create a type 1 font readType1Font :: FilePath -> FilePath - -> IO Type1FontStructure + -> IO (Either ParseError Type1FontStructure) readType1Font pfb afmPath = do fd <- readFontData pfb - afm <- getAfmData afmPath - Just fs <- mkType1FontStructure fd afm - return fs+ result <- readAfmData afmPath+ case result of+ Left pe -> pure $ Left pe+ Right afm -> Right <$> mkType1FontStructure fd afm mkType1Font :: Type1FontStructure -> PDF AnyFont mkType1Font (Type1FontStructure fd fs) = do ref <- createEmbeddedFont fd return (AnyFont $ Type1Font fs ref)--
HPDF.cabal view
@@ -1,5 +1,5 @@ Name: HPDF-Version: 1.5.3+Version: 1.6.0 cabal-version: >=1.10 License: BSD3 License-file:LICENSE@@ -12,7 +12,7 @@ homepage: http://www.alpheccar.org description: A PDF library with support for several pages, page transitions, outlines, annotations, compression, colors, shapes, patterns, jpegs, fonts, typesetting ... Have a look at the "Graphics.PDF.Documentation" module to see how to use it. Or, download the package and look at the test.hs file in the Test folder. That file is giving an example of each feature. extra-source-files:- c/conversion.h+ cbits/conversion.h Test/logo.jpg Test/Makefile Test/Penrose.hs@@ -92,8 +92,8 @@ ghc-options: -Wall -funbox-strict-fields -O2 C-Sources: - c/conversion.c- Include-Dirs: c+ cbits/conversion.c+ Include-Dirs: cbits Install-Includes: conversion.h exposed-Modules: @@ -132,4 +132,3 @@ Graphics.PDF.Typesetting.StandardStyle Graphics.PDF.Fonts.AFMParser Graphics.PDF.Fonts.Encoding-
README.md view
@@ -20,5 +20,5 @@ TO TEST THE LIBRARY ===================== -stack run HPDF-demo-open demo.pdf+> stack run HPDF-demo+> open demo.pdf
Test/test.hs view
@@ -689,11 +689,11 @@ main :: IO() main = do let rect = PDFRect 0 0 600 400- Just timesRoman <- mkStdFont Times_Roman - Just timesBold <- mkStdFont Times_Bold- Just helveticaBold <- mkStdFont Helvetica_Bold- Just symbol <- mkStdFont Symbol - Just zapf <- mkStdFont ZapfDingbats+ Right timesRoman <- mkStdFont Times_Roman + Right timesBold <- mkStdFont Times_Bold+ Right helveticaBold <- mkStdFont Helvetica_Bold+ Right symbol <- mkStdFont Symbol + Right zapf <- mkStdFont ZapfDingbats let logoPath = "Test/logo.jpg" Right jpg <- readJpegFile logoPath
− c/conversion.c
@@ -1,17 +0,0 @@-#include <stdlib.h>-#include <stdio.h>-#include <string.h>-#include "conversion.h"---short c_floatToString(double f,char* s)-{- sprintf(s,"%.5f",f);- return(strlen(s));-}--short c_shortToString(short d,char* s)-{- sprintf(s,"%d",d);- return(strlen(s));-}
− c/conversion.h
@@ -1,5 +0,0 @@-#ifndef _CONVERSION_H_-#define _CONVERSION_H_-extern short c_floatToString(double f,char* s);-extern short c_shortToString(short d,char* s);-#endif
+ cbits/conversion.c view
@@ -0,0 +1,17 @@+#include <stdlib.h>+#include <stdio.h>+#include <string.h>+#include "conversion.h"+++short c_floatToString(double f,char* s)+{+ sprintf(s,"%.5f",f);+ return(strlen(s));+}++short c_shortToString(short d,char* s)+{+ sprintf(s,"%d",d);+ return(strlen(s));+}
+ cbits/conversion.h view
@@ -0,0 +1,5 @@+#ifndef _CONVERSION_H_+#define _CONVERSION_H_+extern short c_floatToString(double f,char* s);+extern short c_shortToString(short d,char* s);+#endif