github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/image/font/basicfont/basicfont.go (about)

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