Skip to content

Commit

Permalink
[tests] fix font index loading
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitkugler committed Aug 26, 2024
1 parent a326a51 commit 4a912fb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
14 changes: 0 additions & 14 deletions pdf/draw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions pdf/pdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/md5"
"fmt"
"io"
"log"
"math"
"os"
"path/filepath"
Expand All @@ -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)
Expand Down
14 changes: 10 additions & 4 deletions pdf/test/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,40 @@ package test

import (
"fmt"
"os"
"path/filepath"
"sync"

fc "github.com/benoitkugler/textprocessing/fontconfig"
"github.com/benoitkugler/textprocessing/pango/fcfonts"

"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
Expand Down

0 comments on commit 4a912fb

Please sign in to comment.