limited.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2023 The Ebitengine Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package text
  15. import (
  16. "github.com/hajimehoshi/ebiten/v2/vector"
  17. )
  18. var _ Face = (*LimitedFace)(nil)
  19. // LimitedFace is a Face with glyph limitations.
  20. type LimitedFace struct {
  21. face Face
  22. unicodeRanges unicodeRanges
  23. }
  24. // NewLimitedFace creates a new LimitedFace from the given face.
  25. // In the default state, glyphs for any runes are limited and not rendered.
  26. // You have to call AddUnicodeRange to add allowed glyphs.
  27. func NewLimitedFace(face Face) *LimitedFace {
  28. return &LimitedFace{
  29. face: face,
  30. }
  31. }
  32. // AddUnicodeRange adds a rune range for rendered glyphs.
  33. // A range is inclusive, which means that a range contains the specified rune end.
  34. func (l *LimitedFace) AddUnicodeRange(start, end rune) {
  35. l.unicodeRanges.add(start, end)
  36. }
  37. // Metrics implements Face.
  38. func (l *LimitedFace) Metrics() Metrics {
  39. return l.face.Metrics()
  40. }
  41. // advance implements Face.
  42. func (l *LimitedFace) advance(text string) float64 {
  43. return l.face.advance(l.unicodeRanges.filter(text))
  44. }
  45. // hasGlyph implements Face.
  46. func (l *LimitedFace) hasGlyph(r rune) bool {
  47. return l.unicodeRanges.contains(r) && l.face.hasGlyph(r)
  48. }
  49. // appendGlyphsForLine implements Face.
  50. func (l *LimitedFace) appendGlyphsForLine(glyphs []Glyph, line string, indexOffset int, originX, originY float64) []Glyph {
  51. return l.face.appendGlyphsForLine(glyphs, l.unicodeRanges.filter(line), indexOffset, originX, originY)
  52. }
  53. // appendVectorPathForLine implements Face.
  54. func (l *LimitedFace) appendVectorPathForLine(path *vector.Path, line string, originX, originY float64) {
  55. l.face.appendVectorPathForLine(path, l.unicodeRanges.filter(line), originX, originY)
  56. }
  57. // direction implements Face.
  58. func (l *LimitedFace) direction() Direction {
  59. return l.face.direction()
  60. }
  61. // private implements Face.
  62. func (l *LimitedFace) private() {
  63. }
  64. type unicodeRange struct {
  65. start rune
  66. end rune
  67. }
  68. type unicodeRanges struct {
  69. ranges []unicodeRange
  70. }
  71. func (u *unicodeRanges) add(start, end rune) {
  72. u.ranges = append(u.ranges, unicodeRange{
  73. start: start,
  74. end: end,
  75. })
  76. }
  77. func (u *unicodeRanges) contains(r rune) bool {
  78. for _, rg := range u.ranges {
  79. if rg.start <= r && r <= rg.end {
  80. return true
  81. }
  82. }
  83. return false
  84. }
  85. func (u *unicodeRanges) filter(str string) string {
  86. var rs []rune
  87. for _, r := range str {
  88. if !u.contains(r) {
  89. // U+FFFD is "REPLACEMENT CHARACTER".
  90. r = '\ufffd'
  91. }
  92. rs = append(rs, r)
  93. }
  94. return string(rs)
  95. }