Skip to content

Commit

Permalink
[tests] ensure font index is created for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitkugler committed Aug 26, 2024
1 parent 1ec79aa commit aa223d9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
12 changes: 4 additions & 8 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 4 additions & 14 deletions pdf/draw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{}
Expand All @@ -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,
Expand Down
36 changes: 36 additions & 0 deletions pdf/test/utils.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit aa223d9

Please sign in to comment.