Giter Club home page Giter Club logo

Comments (18)

wch avatar wch commented on September 23, 2024

Hm, that is strange. The first thing to try is to delete your existing font database by reinstalling the extrafontdb package:

install.packages('extrafontdb')

# Restart R, then re-import fonts
library(extrafont)
font_import()
loadfonts()

If you still have a problem, then my next theory is that it has something to do with Asian font paths.... I wonder if there's something odd in your Fontmap file. Please run this to get the path to that file:

file.path(extrafont:::fontmap_path(), "Fontmap")
# "/Users/winston/R/extrafontdb/fontmap/Fontmap"

If you look inside that file, do you see any strange paths or filenames? For example, here are some lines from my version of that file:

/CMRoman-Bold (/Users/winston/R/fontcm/fonts/outlines/fcmb8a.pfb) ;
/CMRoman-BoldItalic (/Users/winston/R/fontcm/fonts/outlines/fcmbi8a.pfb) ;
/CMRoman-Italic (/Users/winston/R/fontcm/fonts/outlines/fcmri8a.pfb) ;
/CMRoman-Regular (/Users/winston/R/fontcm/fonts/outlines/fcmr8a.pfb) ;
...
/-Keyboard (/System/Library/Fonts/Keyboard.ttf) ;
...
/Georgia-Bold (/Library/Fonts/Georgia Bold.ttf) ;
/Georgia-BoldItalic (/Library/Fonts/Georgia Bold Italic.ttf) ;
/Georgia-Italic (/Library/Fonts/Georgia Italic.ttf) ;
/Impact (/Library/Fonts/Impact.ttf) ;

Also, you may want to check the output of fonttable() to see if anything looks weird there.

from extrafont.

kohske avatar kohske commented on September 23, 2024

Thanks.

The Fontmap file looks fine, and here is the output of fonttable()

> subset(fonttable(), FontName == "Impact")
   package       afmfile                  fontfile FullName FamilyName FontName  Bold Italic Symbol afmsymfile
72      NA Impact.afm.gz /Library/Fonts/Impact.ttf   Impact     Impact   Impact FALSE  FALSE  FALSE         NA

Here is the trace of the cmd in embedFonts:

> trace(embedFonts, quote(print(cmd)), at = 12)
Tracing function "embedFonts" in package "grDevices"
[1] "embedFonts"
> unload(inst("extrafont"))
> library(extrafont)
> embed_fonts("font_plot.pdf", outfile="font_plot_embed.pdf")
Tracing embedFonts(file = file, format = format, outfile = outfile, options = paste(paste("-I",  .... step 12 
[1] "gs -dNOPAUSE -dBATCH -q -dAutoRotatePages=/None -sDEVICE=pdfwrite -sOutputFile=/var/folders/na/na81MsJUHRmVyCHG4xD0X++++TI/-Tmp-//RtmpByAuFz/Rembed53e685a1258 -sFONTPATH= -I'/Library/Frameworks/R.framework/Versions/2.15/Resources/library/extrafontdb/fontmap'  'font_plot.pdf'"

The cmd looks wired.

[1] "gs -dNOPAUSE -dBATCH -q -dAutoRotatePages=/None -sDEVICE=pdfwrite -sOutputFile=/var/folders/na/na81MsJUHRmVyCHG4xD0X++++TI/-Tmp-//RtmpByAuFz/Rembed53e685a1258 -sFONTPATH= -I'/Library/Frameworks/R.framework/Versions/2.15/Resources/library/extrafontdb/fontmap'  'font_plot.pdf'"

especially for -sFONTPATH=

maybe this is the problem by using different version of gs?

> system("gs --version")
9.05
> system("gs --help")
GPL Ghostscript 9.05 (2012-02-08)
Copyright (C) 2010 Artifex Software, Inc.  All rights reserved.

from extrafont.

kohske avatar kohske commented on September 23, 2024

And this command (from shell) can generate the embedded pdf.

gs -dNOPAUSE -dBATCH -q -dAutoRotatePages=/None -sDEVICE=pdfwrite -sOutputFile=/var/folders/na/na81MsJUHRmVyCHG4xD0X++++TI/-Tmp-//RtmpukJoUm/Rembed64d5d8cefdf -sFONTPATH=/Library/Fonts  'font_plot.pdf'

then, this induces the same error:

gs -dNOPAUSE -dBATCH -q -dAutoRotatePages=/None -sDEVICE=pdfwrite -sOutputFile=/var/folders/na/na81MsJUHRmVyCHG4xD0X++++TI/-Tmp-//RtmpukJoUm/Rembed64df005022 -sFONTPATH=/Library/Fonts -I'/Library/Frameworks/R.framework/Versions/2.15/Resources/library/extrafontdb/fontmap'  'font_plot.pdf'

So, -I'/Library/Frameworks/R.framework/Versions/2.15/Resources/library/extrafontdb/fontmap' is doing something wired.

from extrafont.

wch avatar wch commented on September 23, 2024

My cmd looks the same:

> embed_fonts("font_plot.pdf", outfile="font_plot_embed.pdf")
Tracing embedFonts(file = file, format = format, outfile = outfile, options = paste(paste("-I",  .... step 12 
[1] "gs -dNOPAUSE -dBATCH -q -dAutoRotatePages=/None -sDEVICE=pdfwrite -sOutputFile=/var/folders/26/df_1sr1d2w1_x179whkc7xxw0000gn/T//Rtmp4E8FlX/Rembedc0f0441dd951 -sFONTPATH= -I'/Users/winston/R/extrafontdb/fontmap'  'font_plot.pdf'"

The -I option points it to the Fontmap file, and that file contains the font path for each font. I still wonder if your Fontmap file has something strange in it. Here is another experiment to try (two options):

  • Delete every line in Fontmap except the line for Impact.
  • Re-install extrafontdb, and then run font_import(pattern="Impact").

from extrafont.

kohske avatar kohske commented on September 23, 2024

Still not fixed. Could you please show the output of gs -h?

from extrafont.

kohske avatar kohske commented on September 23, 2024

Just in case this is the content of Fontmap:

/Impact (/Library/Fonts/Impact.ttf) ;

now there is only 1 line.

from extrafont.

kohske avatar kohske commented on September 23, 2024

Finally I may figure out the reason. It may be the format of path.

[takahashi@haruna extrafont]$ gs -dNOPAUSE -dBATCH -q -dAutoRotatePages=/None -I/Users/takahashi/tmp/fontmap -sDEVICE=pdfwrite -sOutputFile=f2.pdf 'font_plot.pdf'
[takahashi@haruna extrafont]$ gs -dNOPAUSE -dBATCH -q -dAutoRotatePages=/None -I/Library/Frameworks/R.framework/Versions/2.15/Resources/library/extrafontdb/fontmap -sDEVICE=pdfwrite -sOutputFile=f2.pdf 'font_plot.pdf'

*** Warning: GenericResourceDir doesn't point to a valid resource directory.
               the -sGenericResourceDir=... option can be used to set this.

The contents of the fontmap folder is same.
I couldn't find any reference of the format of the path for gs resource dir, but only difference is the name of path...

Probably this is less related to the extrafont itself.

from extrafont.

kohske avatar kohske commented on September 23, 2024

Finally I figured out. The -I options cannot correctly handle the path including the string "Resources"

[takahashi@haruna extrafont]$ gs -dNOPAUSE -dBATCH -q -dAutoRotatePages=/None -I/Users/takahashi/tmp/Resources/x -sDEVICE=pdfwrite -sOutputFile=f2.pdf 'font_plot.pdf'

*** Warning: GenericResourceDir doesn't point to a valid resource directory.
               the -sGenericResourceDir=... option can be used to set this.

gs -dNOPAUSE -dBATCH -q -dAutoRotatePages=/None -I/Users/takahashi/tmp/zesources/x -sDEVICE=pdfwrite -sOutputFile=f2.pdf 'font_plot.pdf'
[takahashi@haruna extrafont]$ gs -dNOPAUSE -dBATCH -q -dAutoRotatePages=/None -I/Users/takahashi/tmp/zesources/x -sDEVICE=pdfwrite -sOutputFile=f2.pdf 'font_plot.pdf'
[takahashi@haruna extrafont]$ 

Could you please test this?
This is definitely the bug of gs..., but the R library path in OSX includes "Resources", some dirty hack may be necessary.

from extrafont.

wch avatar wch commented on September 23, 2024

Wow, I think you're right:

$ gs -dNOPAUSE -dBATCH -q -dAutoRotatePages=/None -sDEVICE=pdfwrite -sOutputFile=out.pdf -sFONTPATH= -I'/Users/winston/temp/Resources/fontmap'  'font_plot.pdf'

*** Warning: GenericResourceDir doesn't point to a valid resource directory.
               the -sGenericResourceDir=... option can be used to set this.

   **** Error reading a content stream. The page may be incomplete.
   **** File did not complete the page properly and may be damaged.
   **** Warning: File has unbalanced q/Q operators (too many q's)

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

$ gs -dNOPAUSE -dBATCH -q -dAutoRotatePages=/None -sDEVICE=pdfwrite -sOutputFile=out.pdf -sFONTPATH= -I'/Users/winston/temp/esources/fontmap'  'font_plot.pdf'

Here's a hack that seems to work for me: use resources instead of Resources:

gs -dNOPAUSE -dBATCH -q -dAutoRotatePages=/None -sDEVICE=pdfwrite -sOutputFile=out.pdf -sFONTPATH= -I'/Users/winston/temp/resources/fontmap'  'font_plot.pdf'

Yuck. But it works. (Assuming a case-insensitive HFS filesystem)

from extrafont.

kohske avatar kohske commented on September 23, 2024

Yes, resource works also for me. Thanks.

Then, should the fixpath_os like this?

function (path) 
{
    if (grepl("^mingw", sessionInfo()$R.version$os)) {
        gsub("/", "\\\\", path)
    } elseif (grepl("darwin", R.version$os) {
        gsub("Resources", "resources")
    }
    else {
        path
    }
}

from extrafont.

wch avatar wch commented on September 23, 2024

I think something like that should work, but it should be done outside of that function, and inside embed_fonts instead. This is because fixpath_os should be usable in other places where the Resources hack is unnecessary.

from extrafont.

wch avatar wch commented on September 23, 2024

Hm, now that I think about it a bit more, it might be better to create a symlink to the fontmap directory in the temp dir. This is because the resources hack won't work for computers with case-sensitive HFS+ filesystems.

from extrafont.

kohske avatar kohske commented on September 23, 2024

Hm, now that I think about it a bit more, it might be better to create a symlink to the fontmap directory in the temp dir.

agreed. Then, in embed_fonts,

  1. copy the Fontmap into a temp dir.
  2. run embedding with fontmap of the temp dir
  3. remove the Fontmap in the temp dir.

make sense.

from extrafont.

wch avatar wch commented on September 23, 2024

OK, I pushed a fix in the fix-fontmap branch, but I haven't tested it. Can you try it and tell me if it works?

from extrafont.

kohske avatar kohske commented on September 23, 2024

Excellent, works perfectly!!

Thanks.

from extrafont.

wch avatar wch commented on September 23, 2024

Thanks for your help figuring out the problem and a fix!

I made a small change and merged it into master. Can you test the master branch?

from extrafont.

kohske avatar kohske commented on September 23, 2024

The test on the master branch is ok!!
Thanks for your excellent work!

from extrafont.

wch avatar wch commented on September 23, 2024

Great, thank you!

from extrafont.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.