github.com/kintar/etxt@v0.0.9/examples/gtxt/debug_glyph/main.go (about)

     1  //go:build gtxt
     2  
     3  package main
     4  
     5  import "os"
     6  import "log"
     7  import "fmt"
     8  
     9  import "github.com/kintar/etxt"
    10  import "github.com/kintar/etxt/emask"
    11  
    12  import "golang.org/x/image/math/fixed"
    13  
    14  // Must be compiled with '-tags gtxt'
    15  
    16  // More than an example, this is something I use when debugging effects
    17  // and rasterizers to print mask glyph data directly and be able to
    18  // see it and analyze it.
    19  
    20  func main() {
    21  	// get font path
    22  	if len(os.Args) != 2 {
    23  		msg := "Usage: expects one argument with the path to the font to be used\n"
    24  		fmt.Fprint(os.Stderr, msg)
    25  		os.Exit(1)
    26  	}
    27  
    28  	// parse font
    29  	font, fontName, err := etxt.ParseFontFrom(os.Args[1])
    30  	if err != nil {
    31  		log.Fatal(err)
    32  	}
    33  	fmt.Printf("Font loaded: %s\n", fontName)
    34  
    35  	// create and configure renderer
    36  	fauxRast := emask.FauxRasterizer{}
    37  	fauxRast.SetSkewFactor(-1.0)
    38  	fauxRast.SetExtraWidth(0)
    39  	renderer := etxt.NewRenderer(&fauxRast)
    40  	renderer.SetSizePx(36)
    41  	renderer.SetFont(font)
    42  	renderer.SetAlign(etxt.YCenter, etxt.XCenter)
    43  	renderer.Traverse("d", fixed.P(0, 0),
    44  		func(dot fixed.Point26_6, _ rune, glyphIndex etxt.GlyphIndex) {
    45  			mask := renderer.LoadGlyphMask(glyphIndex, dot)
    46  			n := 0
    47  			row := 0
    48  			for n < len(mask.Pix) {
    49  				fmt.Printf("%03d: %03v\n", row, mask.Pix[n:n+mask.Stride])
    50  				n += mask.Stride
    51  				row += 1
    52  			}
    53  		})
    54  	fmt.Print("Program exited successfully.\n")
    55  }