diff --git a/internal/providers/gofpdf/builder.go b/internal/providers/gofpdf/builder.go index 065a5d4a..46964607 100644 --- a/internal/providers/gofpdf/builder.go +++ b/internal/providers/gofpdf/builder.go @@ -1,6 +1,8 @@ package gofpdf import ( + "slices" + "github.com/phpdave11/gofpdf" "github.com/johnfercher/maroto/v2/internal/cache" @@ -51,7 +53,7 @@ func (b *builder) Build(cfg *entity.Config, cache cache.Cache) *Dependencies { }) for _, font := range cfg.CustomFonts { - fpdf.AddUTF8FontFromBytes(font.GetFamily(), string(font.GetStyle()), font.GetBytes()) + fpdf.AddUTF8FontFromBytes(font.GetFamily(), string(font.GetStyle()), slices.Clone(font.GetBytes())) } if cfg.DisableAutoPageBreak { diff --git a/maroto_test.go b/maroto_test.go index 259c8804..89e894b9 100644 --- a/maroto_test.go +++ b/maroto_test.go @@ -2,18 +2,21 @@ package maroto_test import ( "fmt" + "os" "runtime" "testing" "time" "github.com/johnfercher/maroto/v2/pkg/components/code" - "github.com/johnfercher/maroto/v2/pkg/components/text" - "github.com/johnfercher/maroto/v2/pkg/components/col" "github.com/johnfercher/maroto/v2/pkg/components/page" "github.com/johnfercher/maroto/v2/pkg/components/row" + "github.com/johnfercher/maroto/v2/pkg/components/text" "github.com/johnfercher/maroto/v2/pkg/config" + "github.com/johnfercher/maroto/v2/pkg/consts/fontstyle" "github.com/johnfercher/maroto/v2/pkg/core" + "github.com/johnfercher/maroto/v2/pkg/fontrepository" + "github.com/johnfercher/maroto/v2/pkg/props" "github.com/johnfercher/maroto/v2/pkg/test" "github.com/johnfercher/maroto/v2" @@ -597,4 +600,30 @@ func TestMaroto_RegisterFooter(t *testing.T) { assert.Nil(t, err) test.New(t).Assert(sut.GetStructure()).Equals("footer_auto_row.json") }) + + t.Run("when running with concurrent mode, no race condition while accessing font", func(t *testing.T) { + fontData, err := os.ReadFile("docs/assets/fonts/arial-unicode-ms.ttf") + assert.NoError(t, err) + customFonts, err := fontrepository.New(). + AddUTF8FontFromBytes("customFontName", fontstyle.Normal, fontData). + Load() + + assert.NoError(t, err) + + row := row.New(10).Add(col.New()) + cfg := config.NewBuilder(). + WithCustomFonts(customFonts). + WithDefaultFont(&props.Font{Family: "customFontName", Size: 16}). + WithConcurrentMode(4). + Build() + + m := maroto.New(cfg) + for range 150 { + // Generate multiple pages + m.AddRows(row) + } + + _, err = m.Generate() + assert.NoError(t, err) + }) }