diff --git a/pdf/draw_test.go b/pdf/draw_test.go index 643b29c..2874819 100644 --- a/pdf/draw_test.go +++ b/pdf/draw_test.go @@ -7,20 +7,16 @@ import ( "image/color" "image/draw" "image/png" - "io" - "log" "os" "os/exec" "strings" "testing" "time" - "github.com/benoitkugler/go-weasyprint/pdf/test" "github.com/benoitkugler/pdf/model" "github.com/benoitkugler/webrender/backend" "github.com/benoitkugler/webrender/html/document" "github.com/benoitkugler/webrender/html/tree" - "github.com/benoitkugler/webrender/logger" "github.com/benoitkugler/webrender/text" "github.com/benoitkugler/webrender/utils" tu "github.com/benoitkugler/webrender/utils/testutils" @@ -47,16 +43,6 @@ var colorByName = map[byte]color.RGBA{ 'z': joker, // unspecified, accepts anything } -func init() { - logger.ProgressLogger.SetOutput(io.Discard) - - var err error - fontconfig, err = test.LoadTestFontConfig("test/") - if err != nil { - log.Fatalf("creating font configuration: %s", err) - } -} - // convert a PDF file to an image using Ghostscript, and extract the pixels, // expecting a one color image func pdfToColor(img image.Image) (color.RGBA, error) { diff --git a/pdf/pdf_test.go b/pdf/pdf_test.go index 323a053..bd1f75e 100644 --- a/pdf/pdf_test.go +++ b/pdf/pdf_test.go @@ -5,6 +5,7 @@ import ( "crypto/md5" "fmt" "io" + "log" "math" "os" "path/filepath" @@ -14,16 +15,28 @@ import ( "strings" "testing" + "github.com/benoitkugler/go-weasyprint/pdf/test" "github.com/benoitkugler/pdf/model" "github.com/benoitkugler/pdf/reader" pdfParser "github.com/benoitkugler/pdf/reader/parser" "github.com/benoitkugler/webrender/backend" "github.com/benoitkugler/webrender/css/parser" + "github.com/benoitkugler/webrender/logger" "github.com/benoitkugler/webrender/matrix" "github.com/benoitkugler/webrender/utils" "github.com/benoitkugler/webrender/utils/testutils" ) +func init() { + logger.ProgressLogger.SetOutput(io.Discard) + + var err error + fontconfig, err = test.LoadTestFontConfig("test/") + if err != nil { + log.Fatalf("creating font configuration: %s", err) + } +} + func TestPaint(t *testing.T) { c := NewOutput() page := c.AddPage(0, 200, 100, 0) diff --git a/pdf/test/utils.go b/pdf/test/utils.go index c268e06..ab458c0 100644 --- a/pdf/test/utils.go +++ b/pdf/test/utils.go @@ -2,8 +2,8 @@ package test import ( "fmt" - "os" "path/filepath" + "sync" fc "github.com/benoitkugler/textprocessing/fontconfig" "github.com/benoitkugler/textprocessing/pango/fcfonts" @@ -11,25 +11,31 @@ import ( "github.com/benoitkugler/webrender/text" ) +var lock sync.Mutex + // LoadTestFontConfig loads the font index in [cacheDir], // creating it if needed. func LoadTestFontConfig(cacheDir string) (text.FontConfiguration, error) { + lock.Lock() + defer lock.Unlock() + const cacheFile = "cache.fc" cachePath := filepath.Join(cacheDir, cacheFile) - _, err := os.Stat(cachePath) + _, err := fc.LoadFontsetFile(cachePath) if err != nil { // build the index fmt.Println("Scanning fonts...") _, err := fc.ScanAndCache(cachePath) if err != nil { - return nil, err + return nil, fmt.Errorf("scanning fonts: %s", err) } + fmt.Println("Font index written in", cachePath) } fs, err := fc.LoadFontsetFile(cachePath) if err != nil { - return nil, err + return nil, fmt.Errorf("reading font index: %s", err) } return text.NewFontConfigurationPango(fcfonts.NewFontMap(fc.Standard.Copy(), fs)), nil