diff --git a/main_test.go b/main_test.go index 4ab6657..3d8470d 100644 --- a/main_test.go +++ b/main_test.go @@ -7,27 +7,23 @@ import ( "path/filepath" "testing" + "github.com/benoitkugler/go-weasyprint/pdf/test" "github.com/benoitkugler/pdf/reader/file" - fc "github.com/benoitkugler/textprocessing/fontconfig" - "github.com/benoitkugler/textprocessing/pango/fcfonts" "github.com/benoitkugler/webrender/logger" "github.com/benoitkugler/webrender/text" "github.com/benoitkugler/webrender/utils" ) -// see pdf/test/draw_test.go -const fontmapCache = "pdf/test/cache.fc" - var fontconfig text.FontConfiguration func init() { logger.ProgressLogger.SetOutput(io.Discard) - fs, err := fc.LoadFontsetFile(fontmapCache) + var err error + fontconfig, err = test.LoadTestFontConfig("pdf/test/") if err != nil { - log.Fatal(err) + log.Fatalf("creating font configuration: %s", err) } - fontconfig = text.NewFontConfigurationPango(fcfonts.NewFontMap(fc.Standard.Copy(), fs)) } func tempFile(s string) string { diff --git a/pdf/draw_test.go b/pdf/draw_test.go index 97b38df..643b29c 100644 --- a/pdf/draw_test.go +++ b/pdf/draw_test.go @@ -15,9 +15,8 @@ import ( "testing" "time" + "github.com/benoitkugler/go-weasyprint/pdf/test" "github.com/benoitkugler/pdf/model" - fc "github.com/benoitkugler/textprocessing/fontconfig" - "github.com/benoitkugler/textprocessing/pango/fcfonts" "github.com/benoitkugler/webrender/backend" "github.com/benoitkugler/webrender/html/document" "github.com/benoitkugler/webrender/html/tree" @@ -27,8 +26,6 @@ import ( tu "github.com/benoitkugler/webrender/utils/testutils" ) -const fontmapCache = "test/cache.fc" - var fontconfig text.FontConfiguration var joker = color.RGBA{} @@ -53,18 +50,11 @@ var colorByName = map[byte]color.RGBA{ func init() { logger.ProgressLogger.SetOutput(io.Discard) - // this command has to run once - // fmt.Println("Scanning fonts...") - // _, err := fc.ScanAndCache(fontmapCache) - // if err != nil { - // log.Fatal(err) - // } - - fs, err := fc.LoadFontsetFile(fontmapCache) + var err error + fontconfig, err = test.LoadTestFontConfig("test/") if err != nil { - log.Fatal(err) + log.Fatalf("creating font configuration: %s", err) } - fontconfig = text.NewFontConfigurationPango(fcfonts.NewFontMap(fc.Standard.Copy(), fs)) } // convert a PDF file to an image using Ghostscript, and extract the pixels, diff --git a/pdf/test/utils.go b/pdf/test/utils.go new file mode 100644 index 0000000..c268e06 --- /dev/null +++ b/pdf/test/utils.go @@ -0,0 +1,36 @@ +package test + +import ( + "fmt" + "os" + "path/filepath" + + fc "github.com/benoitkugler/textprocessing/fontconfig" + "github.com/benoitkugler/textprocessing/pango/fcfonts" + + "github.com/benoitkugler/webrender/text" +) + +// LoadTestFontConfig loads the font index in [cacheDir], +// creating it if needed. +func LoadTestFontConfig(cacheDir string) (text.FontConfiguration, error) { + const cacheFile = "cache.fc" + cachePath := filepath.Join(cacheDir, cacheFile) + + _, err := os.Stat(cachePath) + if err != nil { + // build the index + fmt.Println("Scanning fonts...") + _, err := fc.ScanAndCache(cachePath) + if err != nil { + return nil, err + } + } + + fs, err := fc.LoadFontsetFile(cachePath) + if err != nil { + return nil, err + } + + return text.NewFontConfigurationPango(fcfonts.NewFontMap(fc.Standard.Copy(), fs)), nil +}