github.com/kintar/etxt@v0.0.9/emask/bold_bench_test.go (about)

     1  //go:build gtxt
     2  
     3  package emask
     4  
     5  import "os"
     6  import "log"
     7  import "io/fs"
     8  import "errors"
     9  import "strings"
    10  
    11  import "testing"
    12  
    13  import "golang.org/x/image/font/sfnt"
    14  import "golang.org/x/image/math/fixed"
    15  
    16  var testFont *sfnt.Font
    17  
    18  func init() { // parse test font
    19  	workDir, err := os.Getwd()
    20  	if err != nil {
    21  		log.Fatal(err)
    22  	}
    23  
    24  	fontPath := "test_font.ttf"
    25  	_, err = os.Stat(fontPath)
    26  	if errors.Is(err, fs.ErrNotExist) && strings.HasSuffix(workDir, "/etxt/emask") {
    27  		fontPath = "../test_font.ttf" // search on etxt/ instead
    28  	}
    29  
    30  	fontBytes, err := os.ReadFile(fontPath)
    31  	if err != nil {
    32  		if !errors.Is(err, fs.ErrNotExist) {
    33  			log.Fatal(err)
    34  		}
    35  		log.Fatal("etxt requires '" + fontPath + "' file to run benchmarks")
    36  	}
    37  	testFont, err = sfnt.Parse(fontBytes)
    38  	if err != nil {
    39  		log.Fatal(err)
    40  	}
    41  }
    42  
    43  func BenchmarkFauxBoldWhole(b *testing.B) {
    44  	contours, rasterizer, dot := getBenchMinData()
    45  	rasterizer.SetExtraWidth(1)
    46  
    47  	b.ResetTimer()
    48  	for n := 0; n < b.N; n++ {
    49  		Rasterize(contours, rasterizer, dot)
    50  	}
    51  }
    52  
    53  func BenchmarkFauxBoldWhole7(b *testing.B) {
    54  	contours, rasterizer, dot := getBenchMinData()
    55  	rasterizer.SetExtraWidth(7)
    56  
    57  	b.ResetTimer()
    58  	for n := 0; n < b.N; n++ {
    59  		Rasterize(contours, rasterizer, dot)
    60  	}
    61  }
    62  
    63  func BenchmarkFauxBoldFract(b *testing.B) {
    64  	contours, rasterizer, dot := getBenchMinData()
    65  	rasterizer.SetExtraWidth(0.7)
    66  
    67  	b.ResetTimer()
    68  	for n := 0; n < b.N; n++ {
    69  		Rasterize(contours, rasterizer, dot)
    70  	}
    71  }
    72  
    73  func BenchmarkFauxBoldFract7(b *testing.B) {
    74  	contours, rasterizer, dot := getBenchMinData()
    75  	rasterizer.SetExtraWidth(7.4)
    76  
    77  	b.ResetTimer()
    78  	for n := 0; n < b.N; n++ {
    79  		Rasterize(contours, rasterizer, dot)
    80  	}
    81  }
    82  
    83  func BenchmarkFauxOblique(b *testing.B) {
    84  	contours, rasterizer, dot := getBenchMinData()
    85  	rasterizer.SetSkewFactor(0.8) // (this is a rather extreme value)
    86  
    87  	b.ResetTimer()
    88  	for n := 0; n < b.N; n++ {
    89  		Rasterize(contours, rasterizer, dot)
    90  	}
    91  }
    92  
    93  func BenchmarkFauxNone(b *testing.B) {
    94  	contours, rasterizer, dot := getBenchMinData()
    95  
    96  	b.ResetTimer()
    97  	for n := 0; n < b.N; n++ {
    98  		Rasterize(contours, rasterizer, dot)
    99  	}
   100  }
   101  
   102  func BenchmarkDefault(b *testing.B) {
   103  	var buffer sfnt.Buffer
   104  	size := fixed.Int26_6(72 << 6) // in pixels
   105  	rasterizer := DefaultRasterizer{}
   106  	_ = rasterizer.CacheSignature()
   107  	index, err := testFont.GlyphIndex(&buffer, 'g')
   108  	if err != nil {
   109  		log.Fatal(err)
   110  	}
   111  	contours, err := testFont.LoadGlyph(&buffer, index, size, nil)
   112  	if err != nil {
   113  		log.Fatal(err)
   114  	}
   115  	dot := fixed.P(0, 0)
   116  
   117  	b.ResetTimer()
   118  	for n := 0; n < b.N; n++ {
   119  		Rasterize(contours, &rasterizer, dot)
   120  	}
   121  }
   122  
   123  func getBenchMinData() (sfnt.Segments, *FauxRasterizer, fixed.Point26_6) {
   124  	var buffer sfnt.Buffer
   125  	size := fixed.Int26_6(72 << 6) // in pixels
   126  	rasterizer := FauxRasterizer{}
   127  	_ = rasterizer.CacheSignature()
   128  	index, err := testFont.GlyphIndex(&buffer, 'g')
   129  	if err != nil {
   130  		log.Fatal(err)
   131  	}
   132  	contours, err := testFont.LoadGlyph(&buffer, index, size, nil)
   133  	if err != nil {
   134  		log.Fatal(err)
   135  	}
   136  	return contours, &rasterizer, fixed.P(0, 0)
   137  }