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

     1  package ecache
     2  
     3  import "golang.org/x/image/math/fixed"
     4  
     5  import "github.com/Kintar/etxt/emask"
     6  
     7  // A GlyphCacheHandler acts as an intermediator between a glyph cache
     8  // and another object, typically a Renderer, to give the later a clear
     9  // target interface to conform to while abstracting the details of an
    10  // underlying cache, which might be finickier to deal with directly
    11  // in a performant way.
    12  //
    13  // GlyphCacheHandler's can't be used concurrently unless the concrete
    14  // implementation explicitly says otherwise.
    15  type GlyphCacheHandler interface {
    16  
    17  	// --- configuration notification methods ---
    18  	// Update methods (called only if required so overhead can be low).
    19  	// Passed values must always be non-nil, except for NotifyOtherChange.
    20  
    21  	// Notifies that the font in use has changed.
    22  	NotifyFontChange(*Font)
    23  
    24  	// Notifies that the text size (in pixels) has changed.
    25  	NotifySizeChange(fixed.Int26_6)
    26  
    27  	// Notifies that the rasterizer has changed. Typically, the
    28  	// rasterizer's CacheSignature() will be used to tell them apart.
    29  	NotifyRasterizerChange(emask.Rasterizer) // called on config changes too
    30  
    31  	// Notifies that the fractional drawing position has changed.
    32  	// Only the _6 decimal bits must be considered.
    33  	NotifyFractChange(fixed.Point26_6)
    34  
    35  	// TODO: I have the suspicion that something is not working properly
    36  	//       with NotifyFractChange ^, as if quantization is not being
    37  	//       hanlded properly in caches.
    38  
    39  	//NotifyOtherChange(any) // more methods like this could be added
    40  
    41  	// --- cache access methods ---
    42  
    43  	// Gets the mask image for the given glyph index and current configuration.
    44  	// The bool indicates whether the mask has been found (as it may be nil).
    45  	GetMask(GlyphIndex) (GlyphMask, bool)
    46  
    47  	// Passes a mask image for the given glyph index and current
    48  	// configuration to the underlying cache. PassMask should only
    49  	// be called after GetMask() fails.
    50  	//
    51  	// Given a specific configuration, the contents of the mask image
    52  	// must always be consistent. This implies that passed masks may be
    53  	// ignored if a mask is already cached under that configuration, as
    54  	// it will be considered superfluous. In other words: passing different
    55  	// masks for the same configuration may cause inconsistent results.
    56  	PassMask(GlyphIndex, GlyphMask)
    57  
    58  	// Notice that many more methods could be provided, like Get/Pass
    59  	// for Advance, Kern, Bounds, etc., and other methods like Clear()
    60  	// or ReleaseFont(), but since etxt doesn't need that, the interface
    61  	// is limited to masks. You can expand whatever you want with your
    62  	// own interfaces and type assertions.
    63  	//
    64  	// Hinting is also another interesting topic, but since sfnt doesn't
    65  	// apply hinting instructions, there's not much to do here. Even if sfnt
    66  	// did, managing glyph "variants" would be wiser, as hinting instructions
    67  	// often exist only for a few characters at a few specific sizes only,
    68  	// and you may not want to keep lots of superfluous duplicated masks for
    69  	// hinted and unhinted configs.
    70  }