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

     1  package esizer
     2  
     3  import "golang.org/x/image/font"
     4  import "golang.org/x/image/font/sfnt"
     5  import "golang.org/x/image/math/fixed"
     6  import "github.com/Kintar/etxt/efixed"
     7  
     8  // A [Sizer] that behaves like the default one, but with a configurable
     9  // horizontal padding factor that's added to the kern between glyphs.
    10  type HorzPaddingSizer struct {
    11  	horzPadding fixed.Int26_6
    12  	buffer      sfnt.Buffer
    13  }
    14  
    15  // Sets the configurable horizontal padding value, in pixels.
    16  func (self *HorzPaddingSizer) SetHorzPadding(value int) {
    17  	self.horzPadding = fixed.Int26_6(value << 6)
    18  }
    19  
    20  // Like [HorzPaddingSizer.SetHorzPadding], but expecting a fixed.Int26_6
    21  // instead of an int.
    22  func (self *HorzPaddingSizer) SetHorzPaddingFract(value fixed.Int26_6) {
    23  	self.horzPadding = value
    24  }
    25  
    26  // Like [HorzPaddingSizer.SetHorzPadding], but expecting a float64 instead
    27  // of an int.
    28  func (self *HorzPaddingSizer) SetHorzPaddingFloat(value float64) {
    29  	self.horzPadding = efixed.FromFloat64RoundToZero(value)
    30  }
    31  
    32  // Satisfies the [Sizer] interface.
    33  func (self *HorzPaddingSizer) Metrics(f *Font, size fixed.Int26_6) font.Metrics {
    34  	return DefaultMetricsFunc(f, size, &self.buffer)
    35  }
    36  
    37  // Satisfies the [Sizer] interface.
    38  func (self *HorzPaddingSizer) Advance(f *Font, glyphIndex GlyphIndex, size fixed.Int26_6) fixed.Int26_6 {
    39  	return DefaultAdvanceFunc(f, glyphIndex, size, &self.buffer)
    40  }
    41  
    42  // Satisfies the [Sizer] interface.
    43  func (self *HorzPaddingSizer) Kern(f *Font, prevGlyphIndex GlyphIndex, currGlyphIndex GlyphIndex, size fixed.Int26_6) fixed.Int26_6 {
    44  	return DefaultKernFunc(f, prevGlyphIndex, currGlyphIndex, size, &self.buffer) + self.horzPadding
    45  }