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 5//go:generate go run gen.go 6 7// Package basicfont provides fixed-size font faces. 8package basicfont // import "golang.org/x/image/font/basicfont" 9 10import ( 11 "image" 12 13 "golang.org/x/image/font" 14 "golang.org/x/image/math/fixed" 15) 16 17// Range maps a contiguous range of runes to vertically adjacent sub-images of 18// a Face's Mask image. The rune range is inclusive on the low end and 19// exclusive on the high end. 20// 21// If Low <= r && r < High, then the rune r is mapped to the sub-image of 22// Face.Mask whose bounds are image.Rect(0, y*h, Face.Width, (y+1)*h), 23// where y = (int(r-Low) + Offset) and h = (Face.Ascent + Face.Descent). 24type Range struct { 25 Low, High rune 26 Offset int 27} 28 29// Face7x13 is a Face derived from the public domain X11 misc-fixed font files. 30// 31// At the moment, it holds the printable characters in ASCII starting with 32// space, and the Unicode replacement character U+FFFD. 33// 34// Its data is entirely self-contained and does not require loading from 35// separate files. 36var Face7x13 = &Face{ 37 Advance: 7, 38 Width: 6, 39 Height: 13, 40 Ascent: 11, 41 Descent: 2, 42 Mask: mask7x13, 43 Ranges: []Range{ 44 {'\u0020', '\u007f', 0}, 45 {'\ufffd', '\ufffe', 95}, 46 }, 47} 48 49// Face is a basic font face whose glyphs all have the same metrics. 50// 51// It is safe to use concurrently. 52type Face struct { 53 // Advance is the glyph advance, in pixels. 54 Advance int 55 // Width is the glyph width, in pixels. 56 Width int 57 // Height is the inter-line height, in pixels. 58 Height int 59 // Ascent is the glyph ascent, in pixels. 60 Ascent int 61 // Descent is the glyph descent, in pixels. 62 Descent int 63 // Left is the left side bearing, in pixels. A positive value means that 64 // all of a glyph is to the right of the dot. 65 Left int 66 67 // Mask contains all of the glyph masks. Its width is typically the Face's 68 // Width, and its height a multiple of the Face's Height. 69 Mask image.Image 70 // Ranges map runes to sub-images of Mask. The rune ranges must not 71 // overlap, and must be in increasing rune order. 72 Ranges []Range 73} 74 75func (f *Face) Close() error { return nil } 76func (f *Face) Kern(r0, r1 rune) fixed.Int26_6 { return 0 } 77 78func (f *Face) Metrics() font.Metrics { 79 return font.Metrics{ 80 Height: fixed.I(f.Height), 81 Ascent: fixed.I(f.Ascent), 82 Descent: fixed.I(f.Descent), 83 XHeight: fixed.I(f.Ascent), 84 CapHeight: fixed.I(f.Ascent), 85 CaretSlope: image.Point{X: 0, Y: 1}, 86 } 87} 88 89func (f *Face) Glyph(dot fixed.Point26_6, r rune) ( 90 dr image.Rectangle, mask image.Image, maskp image.Point, advance fixed.Int26_6, ok bool) { 91 92loop: 93 for _, rr := range [2]rune{r, '\ufffd'} { 94 for _, rng := range f.Ranges { 95 if rr < rng.Low || rng.High <= rr { 96 continue 97 } 98 maskp.Y = (int(rr-rng.Low) + rng.Offset) * (f.Ascent + f.Descent) 99 ok = true 100 break loop 101 } 102 } 103 if !ok { 104 return image.Rectangle{}, nil, image.Point{}, 0, false 105 } 106 107 x := int(dot.X+32)>>6 + f.Left 108 y := int(dot.Y+32) >> 6 109 dr = image.Rectangle{ 110 Min: image.Point{ 111 X: x, 112 Y: y - f.Ascent, 113 }, 114 Max: image.Point{ 115 X: x + f.Width, 116 Y: y + f.Descent, 117 }, 118 } 119 120 return dr, f.Mask, maskp, fixed.I(f.Advance), true 121} 122 123func (f *Face) GlyphBounds(r rune) (bounds fixed.Rectangle26_6, advance fixed.Int26_6, ok bool) { 124 return fixed.R(0, -f.Ascent, f.Width, +f.Descent), fixed.I(f.Advance), true 125} 126 127func (f *Face) GlyphAdvance(r rune) (advance fixed.Int26_6, ok bool) { 128 return fixed.I(f.Advance), true 129} 130