github.com/kintar/etxt@v0.0.9/helpers.go (about) 1 package etxt 2 3 import "strconv" 4 import "image" 5 6 import "golang.org/x/image/math/fixed" 7 import "golang.org/x/image/font/sfnt" 8 9 import "github.com/kintar/etxt/ecache" 10 11 // This file contains many helper types, wrappers, aliases and 12 // other minor elements required to make this whole package work. 13 14 // An alias for sfnt.Font so you don't need to import sfnt yourself 15 // when working with etxt. 16 type Font = sfnt.Font 17 18 // Glyph indices are used to specify which font glyph are we working 19 // with. Glyph indices are a low level construct that most users of 20 // etxt dont't have to deal with, but they are important as they can 21 // be used to reference font glyphs that don't have any direct mapping 22 // to unicode code points. 23 // 24 // Support for glyph indices (and not only runes), therefore, is important 25 // in order to make renderers usable with [text shapers] and complex scripts. 26 // 27 // [text shapers]: https://github.com/kintar/etxt/blob/main/docs/shaping.md 28 type GlyphIndex = sfnt.GlyphIndex 29 30 // Text alignment types. 31 32 type VertAlign int8 33 type HorzAlign int8 34 35 // Vertical align constants for renderer operations. See 36 // [Renderer.SetAlign]() for additional details. 37 const ( 38 Top VertAlign = 0 39 YCenter VertAlign = 1 40 Baseline VertAlign = 2 41 Bottom VertAlign = 3 42 ) 43 44 // Horizontal align constants for renderer operations. See 45 // [Renderer.SetAlign] for additional details. 46 const ( 47 Left HorzAlign = 0 48 XCenter HorzAlign = 1 49 Right HorzAlign = 2 50 ) 51 52 // Renderers can have their text direction configured as 53 // left-to-right or right-to-left. 54 // 55 // Directions can be casted directly to [unicode/bidi] directions: 56 // 57 // bidi.Direction(etxt.LeftToRight). 58 // 59 // [unicode/bidi]: https://pkg.go.dev/golang.org/x/text/unicode/bidi 60 type Direction int8 61 62 const ( 63 LeftToRight Direction = iota 64 RightToLeft 65 ) 66 67 // Creates a new cache for font glyphs. For more details on how to use 68 // this new cache with renderers, see [Renderer.SetCacheHandler]() . 69 // 70 // This function will panic if maxBytes < 1024 or crypto/rand fails. If 71 // you need to handle those errors, see [ecache.NewDefaultCache]() instead. 72 func NewDefaultCache(maxBytes int) *ecache.DefaultCache { 73 cache, err := ecache.NewDefaultCache(maxBytes) 74 if err != nil { 75 panic(err) 76 } 77 return cache 78 } 79 80 // RectSize objects are used to store the results of text sizing operations. 81 // If you need to use the fixed.Int26_6 values directly and would like more 82 // context on them, read [this document]. Otherwise, you can obtain RectSize 83 // dimensions as int values like this: 84 // 85 // rect := txtRenderer.SelectionRect(text) 86 // width := rect.Width.Ceil() 87 // height := rect.Height.Ceil() 88 // 89 // [this document]: https://github.com/kintar/etxt/blob/main/docs/fixed-26-6.md 90 type RectSize struct { 91 Width fixed.Int26_6 92 Height fixed.Int26_6 93 } 94 95 // Returns the RectSize as an image.Rectangle with origin at (0, 0). 96 // 97 // Ebitengine and other projects often expect image.Rectangle objects 98 // as arguments in their API calls, so this method is offered as a 99 // handy conversion shortcut. 100 func (self RectSize) ImageRect() image.Rectangle { 101 return image.Rect(0, 0, self.Width.Ceil(), self.Height.Ceil()) 102 } 103 104 // --- misc helpers --- 105 106 func runeToUnicodeCode(r rune) string { 107 return "\\u" + strconv.FormatInt(int64(r), 16) 108 }