github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/image/example/font/main.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  // +build ignore
     6  //
     7  // This build tag means that "go install golang.org/x/image/..." doesn't
     8  // install this example program. Use "go run main.go" to run it.
     9  
    10  // Font is a basic example of using fonts.
    11  package main
    12  
    13  import (
    14  	"flag"
    15  	"image"
    16  	"image/color"
    17  	"image/draw"
    18  	"image/png"
    19  	"io/ioutil"
    20  	"log"
    21  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  
    25  	"golang.org/x/image/font"
    26  	"golang.org/x/image/font/plan9font"
    27  	"golang.org/x/image/math/fixed"
    28  )
    29  
    30  var (
    31  	fontFlag = flag.String("font", "",
    32  		`filename of the Plan 9 font or subfont file, such as "lucsans/unicode.8.font" or "lucsans/lsr.14"`)
    33  	firstRuneFlag = flag.Int("firstrune", 0, "the Unicode code point of the first rune in the subfont file")
    34  )
    35  
    36  func pt(p fixed.Point26_6) image.Point {
    37  	return image.Point{
    38  		X: int(p.X+32) >> 6,
    39  		Y: int(p.Y+32) >> 6,
    40  	}
    41  }
    42  
    43  func main() {
    44  	flag.Parse()
    45  
    46  	// TODO: mmap the files.
    47  	if *fontFlag == "" {
    48  		flag.Usage()
    49  		log.Fatal("no font specified")
    50  	}
    51  	var face font.Face
    52  	if strings.HasSuffix(*fontFlag, ".font") {
    53  		fontData, err := ioutil.ReadFile(*fontFlag)
    54  		if err != nil {
    55  			log.Fatal(err)
    56  		}
    57  		dir := filepath.Dir(*fontFlag)
    58  		face, err = plan9font.ParseFont(fontData, func(name string) ([]byte, error) {
    59  			return ioutil.ReadFile(filepath.Join(dir, filepath.FromSlash(name)))
    60  		})
    61  		if err != nil {
    62  			log.Fatal(err)
    63  		}
    64  	} else {
    65  		fontData, err := ioutil.ReadFile(*fontFlag)
    66  		if err != nil {
    67  			log.Fatal(err)
    68  		}
    69  		face, err = plan9font.ParseSubfont(fontData, rune(*firstRuneFlag))
    70  		if err != nil {
    71  			log.Fatal(err)
    72  		}
    73  	}
    74  
    75  	dst := image.NewRGBA(image.Rect(0, 0, 800, 300))
    76  	draw.Draw(dst, dst.Bounds(), image.Black, image.Point{}, draw.Src)
    77  
    78  	d := &font.Drawer{
    79  		Dst:  dst,
    80  		Src:  image.White,
    81  		Face: face,
    82  	}
    83  	ss := []string{
    84  		"The quick brown fox jumps over the lazy dog.",
    85  		"Hello, 世界.",
    86  		"U+FFFD is \ufffd.",
    87  	}
    88  	for i, s := range ss {
    89  		d.Dot = fixed.P(20, 100*i+80)
    90  		dot0 := pt(d.Dot)
    91  		d.DrawString(s)
    92  		dot1 := pt(d.Dot)
    93  		dst.SetRGBA(dot0.X, dot0.Y, color.RGBA{0xff, 0x00, 0x00, 0xff})
    94  		dst.SetRGBA(dot1.X, dot1.Y, color.RGBA{0x00, 0x00, 0xff, 0xff})
    95  	}
    96  
    97  	out, err := os.Create("out.png")
    98  	if err != nil {
    99  		log.Fatal(err)
   100  	}
   101  	defer out.Close()
   102  	if err := png.Encode(out, dst); err != nil {
   103  		log.Fatal(err)
   104  	}
   105  }