github.com/Kintar/etxt@v0.0.0-20221224033739-2fc69f000137/esizer/default_funcs.go (about)

     1  package esizer
     2  
     3  import "strconv"
     4  
     5  import "golang.org/x/image/font"
     6  import "golang.org/x/image/font/sfnt"
     7  import "golang.org/x/image/math/fixed"
     8  
     9  // A default method to respond to font metrics requests. Can be used
    10  // while implementing your own sizers.
    11  func DefaultMetricsFunc(f *Font, size fixed.Int26_6, buffer *sfnt.Buffer) font.Metrics {
    12  	metrics, err := f.Metrics(buffer, size, font.HintingNone)
    13  	if err != nil {
    14  		panic("font.Metrics error: " + err.Error())
    15  	}
    16  	return metrics
    17  }
    18  
    19  // A default method to respond to glyph advance requests. Can be used
    20  // while implementing your own sizers.
    21  func DefaultAdvanceFunc(f *Font, glyphIndex GlyphIndex, size fixed.Int26_6, buffer *sfnt.Buffer) fixed.Int26_6 {
    22  	advance, err := f.GlyphAdvance(buffer, glyphIndex, size, font.HintingNone)
    23  	if err == nil {
    24  		return advance
    25  	}
    26  
    27  	msg := "font.GlyphAdvance(index = " + strconv.Itoa(int(glyphIndex))
    28  	msg += ") error: " + err.Error()
    29  	panic(msg)
    30  }
    31  
    32  // A default method to respond to glyph kern requests. Can be used
    33  // while implementing your own sizers.
    34  func DefaultKernFunc(f *Font, prevGlyphIndex GlyphIndex, currGlyphIndex GlyphIndex, size fixed.Int26_6, buffer *sfnt.Buffer) fixed.Int26_6 {
    35  	kern, err := f.Kern(buffer, prevGlyphIndex, currGlyphIndex, size, font.HintingNone)
    36  	if err == nil {
    37  		return kern
    38  	}
    39  	if err == sfnt.ErrNotFound {
    40  		return 0
    41  	}
    42  
    43  	msg := "font.Kern failed for glyphs with indices "
    44  	msg += strconv.Itoa(int(prevGlyphIndex)) + " and "
    45  	msg += strconv.Itoa(int(currGlyphIndex)) + ": " + err.Error()
    46  	panic(msg)
    47  }