font.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package font defines an interface for font faces, for drawing text on an
  5. // image.
  6. //
  7. // Other packages provide font face implementations. For example, a truetype
  8. // package would provide one based on .ttf font files.
  9. package font // import "golang.org/x/image/font"
  10. import (
  11. "image"
  12. "image/draw"
  13. "io"
  14. "unicode/utf8"
  15. "golang.org/x/image/math/fixed"
  16. )
  17. // TODO: who is responsible for caches (glyph images, glyph indices, kerns)?
  18. // The Drawer or the Face?
  19. // Face is a font face. Its glyphs are often derived from a font file, such as
  20. // "Comic_Sans_MS.ttf", but a face has a specific size, style, weight and
  21. // hinting. For example, the 12pt and 18pt versions of Comic Sans are two
  22. // different faces, even if derived from the same font file.
  23. //
  24. // A Face is not safe for concurrent use by multiple goroutines, as its methods
  25. // may re-use implementation-specific caches and mask image buffers.
  26. //
  27. // To create a Face, look to other packages that implement specific font file
  28. // formats.
  29. type Face interface {
  30. io.Closer
  31. // Glyph returns the draw.DrawMask parameters (dr, mask, maskp) to draw r's
  32. // glyph at the sub-pixel destination location dot, and that glyph's
  33. // advance width.
  34. //
  35. // It returns !ok if the face does not contain a glyph for r.
  36. //
  37. // The contents of the mask image returned by one Glyph call may change
  38. // after the next Glyph call. Callers that want to cache the mask must make
  39. // a copy.
  40. Glyph(dot fixed.Point26_6, r rune) (
  41. dr image.Rectangle, mask image.Image, maskp image.Point, advance fixed.Int26_6, ok bool)
  42. // GlyphBounds returns the bounding box of r's glyph, drawn at a dot equal
  43. // to the origin, and that glyph's advance width.
  44. //
  45. // It returns !ok if the face does not contain a glyph for r.
  46. //
  47. // The glyph's ascent and descent equal -bounds.Min.Y and +bounds.Max.Y. A
  48. // visual depiction of what these metrics are is at
  49. // https://developer.apple.com/library/mac/documentation/TextFonts/Conceptual/CocoaTextArchitecture/Art/glyph_metrics_2x.png
  50. GlyphBounds(r rune) (bounds fixed.Rectangle26_6, advance fixed.Int26_6, ok bool)
  51. // GlyphAdvance returns the advance width of r's glyph.
  52. //
  53. // It returns !ok if the face does not contain a glyph for r.
  54. GlyphAdvance(r rune) (advance fixed.Int26_6, ok bool)
  55. // Kern returns the horizontal adjustment for the kerning pair (r0, r1). A
  56. // positive kern means to move the glyphs further apart.
  57. Kern(r0, r1 rune) fixed.Int26_6
  58. // Metrics returns the metrics for this Face.
  59. Metrics() Metrics
  60. // TODO: ColoredGlyph for various emoji?
  61. // TODO: Ligatures? Shaping?
  62. }
  63. // Metrics holds the metrics for a Face. A visual depiction is at
  64. // https://developer.apple.com/library/mac/documentation/TextFonts/Conceptual/CocoaTextArchitecture/Art/glyph_metrics_2x.png
  65. type Metrics struct {
  66. // Height is the recommended amount of vertical space between two lines of
  67. // text.
  68. Height fixed.Int26_6
  69. // Ascent is the distance from the top of a line to its baseline.
  70. Ascent fixed.Int26_6
  71. // Descent is the distance from the bottom of a line to its baseline. The
  72. // value is typically positive, even though a descender goes below the
  73. // baseline.
  74. Descent fixed.Int26_6
  75. }
  76. // Drawer draws text on a destination image.
  77. //
  78. // A Drawer is not safe for concurrent use by multiple goroutines, since its
  79. // Face is not.
  80. type Drawer struct {
  81. // Dst is the destination image.
  82. Dst draw.Image
  83. // Src is the source image.
  84. Src image.Image
  85. // Face provides the glyph mask images.
  86. Face Face
  87. // Dot is the baseline location to draw the next glyph. The majority of the
  88. // affected pixels will be above and to the right of the dot, but some may
  89. // be below or to the left. For example, drawing a 'j' in an italic face
  90. // may affect pixels below and to the left of the dot.
  91. Dot fixed.Point26_6
  92. // TODO: Clip image.Image?
  93. // TODO: SrcP image.Point for Src images other than *image.Uniform? How
  94. // does it get updated during DrawString?
  95. }
  96. // TODO: should DrawString return the last rune drawn, so the next DrawString
  97. // call can kern beforehand? Or should that be the responsibility of the caller
  98. // if they really want to do that, since they have to explicitly shift d.Dot
  99. // anyway? What if ligatures span more than two runes? What if grapheme
  100. // clusters span multiple runes?
  101. //
  102. // TODO: do we assume that the input is in any particular Unicode Normalization
  103. // Form?
  104. //
  105. // TODO: have DrawRunes(s []rune)? DrawRuneReader(io.RuneReader)?? If we take
  106. // io.RuneReader, we can't assume that we can rewind the stream.
  107. //
  108. // TODO: how does this work with line breaking: drawing text up until a
  109. // vertical line? Should DrawString return the number of runes drawn?
  110. // DrawBytes draws s at the dot and advances the dot's location.
  111. //
  112. // It is equivalent to DrawString(string(s)) but may be more efficient.
  113. func (d *Drawer) DrawBytes(s []byte) {
  114. prevC := rune(-1)
  115. for len(s) > 0 {
  116. c, size := utf8.DecodeRune(s)
  117. s = s[size:]
  118. if prevC >= 0 {
  119. d.Dot.X += d.Face.Kern(prevC, c)
  120. }
  121. dr, mask, maskp, advance, ok := d.Face.Glyph(d.Dot, c)
  122. if !ok {
  123. // TODO: is falling back on the U+FFFD glyph the responsibility of
  124. // the Drawer or the Face?
  125. // TODO: set prevC = '\ufffd'?
  126. continue
  127. }
  128. draw.DrawMask(d.Dst, dr, d.Src, image.Point{}, mask, maskp, draw.Over)
  129. d.Dot.X += advance
  130. prevC = c
  131. }
  132. }
  133. // DrawString draws s at the dot and advances the dot's location.
  134. func (d *Drawer) DrawString(s string) {
  135. prevC := rune(-1)
  136. for _, c := range s {
  137. if prevC >= 0 {
  138. d.Dot.X += d.Face.Kern(prevC, c)
  139. }
  140. dr, mask, maskp, advance, ok := d.Face.Glyph(d.Dot, c)
  141. if !ok {
  142. // TODO: is falling back on the U+FFFD glyph the responsibility of
  143. // the Drawer or the Face?
  144. // TODO: set prevC = '\ufffd'?
  145. continue
  146. }
  147. draw.DrawMask(d.Dst, dr, d.Src, image.Point{}, mask, maskp, draw.Over)
  148. d.Dot.X += advance
  149. prevC = c
  150. }
  151. }
  152. // BoundBytes returns the bounding box of s, drawn at the drawer dot, as well as
  153. // the advance.
  154. //
  155. // It is equivalent to BoundBytes(string(s)) but may be more efficient.
  156. func (d *Drawer) BoundBytes(s []byte) (bounds fixed.Rectangle26_6, advance fixed.Int26_6) {
  157. bounds, advance = BoundBytes(d.Face, s)
  158. bounds.Min = bounds.Min.Add(d.Dot)
  159. bounds.Max = bounds.Max.Add(d.Dot)
  160. return
  161. }
  162. // BoundString returns the bounding box of s, drawn at the drawer dot, as well
  163. // as the advance.
  164. func (d *Drawer) BoundString(s string) (bounds fixed.Rectangle26_6, advance fixed.Int26_6) {
  165. bounds, advance = BoundString(d.Face, s)
  166. bounds.Min = bounds.Min.Add(d.Dot)
  167. bounds.Max = bounds.Max.Add(d.Dot)
  168. return
  169. }
  170. // MeasureBytes returns how far dot would advance by drawing s.
  171. //
  172. // It is equivalent to MeasureString(string(s)) but may be more efficient.
  173. func (d *Drawer) MeasureBytes(s []byte) (advance fixed.Int26_6) {
  174. return MeasureBytes(d.Face, s)
  175. }
  176. // MeasureString returns how far dot would advance by drawing s.
  177. func (d *Drawer) MeasureString(s string) (advance fixed.Int26_6) {
  178. return MeasureString(d.Face, s)
  179. }
  180. // BoundBytes returns the bounding box of s with f, drawn at a dot equal to the
  181. // origin, as well as the advance.
  182. //
  183. // It is equivalent to BoundString(string(s)) but may be more efficient.
  184. func BoundBytes(f Face, s []byte) (bounds fixed.Rectangle26_6, advance fixed.Int26_6) {
  185. prevC := rune(-1)
  186. for len(s) > 0 {
  187. c, size := utf8.DecodeRune(s)
  188. s = s[size:]
  189. if prevC >= 0 {
  190. advance += f.Kern(prevC, c)
  191. }
  192. b, a, ok := f.GlyphBounds(c)
  193. if !ok {
  194. // TODO: is falling back on the U+FFFD glyph the responsibility of
  195. // the Drawer or the Face?
  196. // TODO: set prevC = '\ufffd'?
  197. continue
  198. }
  199. b.Min.X += advance
  200. b.Max.X += advance
  201. bounds = bounds.Union(b)
  202. advance += a
  203. prevC = c
  204. }
  205. return
  206. }
  207. // BoundString returns the bounding box of s with f, drawn at a dot equal to the
  208. // origin, as well as the advance.
  209. func BoundString(f Face, s string) (bounds fixed.Rectangle26_6, advance fixed.Int26_6) {
  210. prevC := rune(-1)
  211. for _, c := range s {
  212. if prevC >= 0 {
  213. advance += f.Kern(prevC, c)
  214. }
  215. b, a, ok := f.GlyphBounds(c)
  216. if !ok {
  217. // TODO: is falling back on the U+FFFD glyph the responsibility of
  218. // the Drawer or the Face?
  219. // TODO: set prevC = '\ufffd'?
  220. continue
  221. }
  222. b.Min.X += advance
  223. b.Max.X += advance
  224. bounds = bounds.Union(b)
  225. advance += a
  226. prevC = c
  227. }
  228. return
  229. }
  230. // MeasureBytes returns how far dot would advance by drawing s with f.
  231. //
  232. // It is equivalent to MeasureString(string(s)) but may be more efficient.
  233. func MeasureBytes(f Face, s []byte) (advance fixed.Int26_6) {
  234. prevC := rune(-1)
  235. for len(s) > 0 {
  236. c, size := utf8.DecodeRune(s)
  237. s = s[size:]
  238. if prevC >= 0 {
  239. advance += f.Kern(prevC, c)
  240. }
  241. a, ok := f.GlyphAdvance(c)
  242. if !ok {
  243. // TODO: is falling back on the U+FFFD glyph the responsibility of
  244. // the Drawer or the Face?
  245. // TODO: set prevC = '\ufffd'?
  246. continue
  247. }
  248. advance += a
  249. prevC = c
  250. }
  251. return advance
  252. }
  253. // MeasureString returns how far dot would advance by drawing s with f.
  254. func MeasureString(f Face, s string) (advance fixed.Int26_6) {
  255. prevC := rune(-1)
  256. for _, c := range s {
  257. if prevC >= 0 {
  258. advance += f.Kern(prevC, c)
  259. }
  260. a, ok := f.GlyphAdvance(c)
  261. if !ok {
  262. // TODO: is falling back on the U+FFFD glyph the responsibility of
  263. // the Drawer or the Face?
  264. // TODO: set prevC = '\ufffd'?
  265. continue
  266. }
  267. advance += a
  268. prevC = c
  269. }
  270. return advance
  271. }
  272. // Hinting selects how to quantize a vector font's glyph nodes.
  273. //
  274. // Not all fonts support hinting.
  275. type Hinting int
  276. const (
  277. HintingNone Hinting = iota
  278. HintingVertical
  279. HintingFull
  280. )
  281. // Stretch selects a normal, condensed, or expanded face.
  282. //
  283. // Not all fonts support stretches.
  284. type Stretch int
  285. const (
  286. StretchUltraCondensed Stretch = -4
  287. StretchExtraCondensed Stretch = -3
  288. StretchCondensed Stretch = -2
  289. StretchSemiCondensed Stretch = -1
  290. StretchNormal Stretch = +0
  291. StretchSemiExpanded Stretch = +1
  292. StretchExpanded Stretch = +2
  293. StretchExtraExpanded Stretch = +3
  294. StretchUltraExpanded Stretch = +4
  295. )
  296. // Style selects a normal, italic, or oblique face.
  297. //
  298. // Not all fonts support styles.
  299. type Style int
  300. const (
  301. StyleNormal Style = iota
  302. StyleItalic
  303. StyleOblique
  304. )
  305. // Weight selects a normal, light or bold face.
  306. //
  307. // Not all fonts support weights.
  308. //
  309. // The named Weight constants (e.g. WeightBold) correspond to CSS' common
  310. // weight names (e.g. "Bold"), but the numerical values differ, so that in Go,
  311. // the zero value means to use a normal weight. For the CSS names and values,
  312. // see https://developer.mozilla.org/en/docs/Web/CSS/font-weight
  313. type Weight int
  314. const (
  315. WeightThin Weight = -3 // CSS font-weight value 100.
  316. WeightExtraLight Weight = -2 // CSS font-weight value 200.
  317. WeightLight Weight = -1 // CSS font-weight value 300.
  318. WeightNormal Weight = +0 // CSS font-weight value 400.
  319. WeightMedium Weight = +1 // CSS font-weight value 500.
  320. WeightSemiBold Weight = +2 // CSS font-weight value 600.
  321. WeightBold Weight = +3 // CSS font-weight value 700.
  322. WeightExtraBold Weight = +4 // CSS font-weight value 800.
  323. WeightBlack Weight = +5 // CSS font-weight value 900.
  324. )