Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify encoding when loading type1 font #17

Merged
merged 3 commits into from
Jan 24, 2025

Conversation

unhammer
Copy link

@unhammer unhammer commented Jan 19, 2025

c.f. #9 this lets us do things like

main = do
   
    Right azsubscriptStructure <- readType1FontWithEncoding (OtherEncoding subscriptEncoding) 
                                                           "./azsubscripts.pfb" "./azsubscripts.afm"
    run (standardDocInfo { author = "me", compressed = False }) docRect $ do
        azsubscriptFont <- mkType1Font azsubscriptStructure
        makeDocument azsubscriptFont 

subscriptEncoding :: M.Map String Char
subscriptEncoding = M.fromList
  [ ("uni2080",'₀')
  , ("uni2081",'₁')
  , ("uni2082",'₂')
  , ("uni2083",'₃')
  , ("uni2084",'₄')
  , ("uni2085",'₅')
  , ("uni2086",'₆')
  , ("uni2087",'₇')
  , ("uni2088",'₈')
  , ("uni2089",'₉')
  ]

I use these helpers to pick fonts:

         txtWithFont fontPicker textThatMightHaveSubscripts
         forceNewLine
  where
    fontPicker c
        | c `elem` subscriptEncoding = Normal azsubscriptFont
        | otherwise              = Normal timesRoman

txtWithFont :: (Char -> MyParaStyles) -> Text -> Para MyParaStyles ()
txtWithFont pickFont text = for_ broken styleChar
  where
    broken = groupWithKey pickFont (T.unpack text)
    styleChar :: (MyParaStyles, String) -> Para MyParaStyles ()
    styleChar (style, substring) = do 
                        setStyle style
                        txt (T.pack substring)

groupWithKey :: Eq key => (elt -> key) -> [elt] -> [(key, [elt])]
groupWithKey classify lst = fmap decorate lst |> Data.List.Extra.groupOnKey fst |> map (second (map snd))
  where
    decorate elt = (classify elt, elt)

Note: there are some functions like getEncoding that I don't understand why are in IO, but I haven't changed them (perhaps the idea was to keep the api general enough to add encodings that need IO at a later point?).

And like I said in the comments of #9 I have no idea what the best API here is, but at least this PR exposes the functions necessary to get something working, without exposing that much of the Encodings module.

@unhammer
Copy link
Author

unhammer commented Jan 19, 2025

However, there's some issue with the embedded fonts, they show fine in emacs and zathura, but Atril/Firefox/Chrome don't see the symbols.

If I do

$ gs -o output.pdf -sDEVICE=pdfwrite -dEmbedAllFonts=true fromHPDF.pdf 
The following warnings were encountered at least once while processing this file:
        A FontDescriptor has a missing or bad /FontName

   **** This file had errors that were repaired or ignored.
   **** The file was produced by:
   **** >>>>  <<<<
   **** Please notify the author of the software that produced this
   **** file that it does not conform to Adobe's published PDF
   **** specification.

then the output.pdf is fixed (but gs also outputs PDF-1.7 instead of 1.5 and changes the font from type 1 to type 1C according to pdffonts), but I don't understand where the error was – the font itself or something HPDF did.


EDIT: I tried just overwriting glyphs of letters a-j in an existing font encoded as AdobeStandard according to fontforge, and supplying the encoding

[("a",'₀'),("b",'₁'),("c",'₂'),("d",'₃'),("e",'₄'),("f",'₅'),("g",'₆'),("h",'₇'),("i",'₈'),("j",'₉')]

to HPDF, and then it works fine. Basically, it seems like if Encoding is AdobeStandard in fontforge, then firefox etc. understand it, but not if it's "Glyph Order" or "Symbol" or anything like that. So it seems there's something about the way HPDF inserts the font definition that leads pdf.js to assume a certain order of glyphs in the pfb (while for some reason zathura was able to read it), or maybe it can't find the ordering, I don't know. I'm guessing the error is in HPDF and not fontforge though.

In any case, it's good enough for me now, hopefully someone else can pick up the baton wrt. that glyph order; I've shaved enough of yak for one weekend :)

@unhammer unhammer changed the title Specify encoding Specify encoding when loading type1 font Jan 20, 2025
So one can e.g. add to AdobeStandardEncoding instead of specifying it
all over again
@hsyl20
Copy link
Owner

hsyl20 commented Jan 24, 2025

Thanks for your contribution! It looks good to me. Thanks for the documentation.

Note: there are some functions like getEncoding that I don't understand why are in IO, but I haven't changed them (perhaps the idea was to keep the api general enough to add encodings that need IO at a later point?).

Sorry but I have no idea. I'm not the package author, I'm not using it, and I'm barely maintaining it. :-)

@hsyl20 hsyl20 merged commit ffcf524 into hsyl20:master Jan 24, 2025
@hsyl20
Copy link
Owner

hsyl20 commented Jan 24, 2025

Please tell me if/when you want a release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants