...

Source file src/github.com/golang/freetype/example/drawer/main.go

Documentation: github.com/golang/freetype/example/drawer

     1  // Copyright 2015 The Freetype-Go Authors. All rights reserved.
     2  // Use of this source code is governed by your choice of either the
     3  // FreeType License or the GNU General Public License version 2 (or
     4  // any later version), both of which can be found in the LICENSE file.
     5  
     6  // +build example
     7  //
     8  // This build tag means that "go install github.com/golang/freetype/..."
     9  // doesn't install this example program. Use "go run main.go" to run it or "go
    10  // install -tags=example" to install it.
    11  
    12  package main
    13  
    14  import (
    15  	"bufio"
    16  	"flag"
    17  	"fmt"
    18  	"image"
    19  	"image/color"
    20  	"image/draw"
    21  	"image/png"
    22  	"io/ioutil"
    23  	"log"
    24  	"math"
    25  	"os"
    26  
    27  	"github.com/golang/freetype/truetype"
    28  	"golang.org/x/image/font"
    29  	"golang.org/x/image/math/fixed"
    30  )
    31  
    32  var (
    33  	dpi      = flag.Float64("dpi", 72, "screen resolution in Dots Per Inch")
    34  	fontfile = flag.String("fontfile", "../../testdata/luxisr.ttf", "filename of the ttf font")
    35  	hinting  = flag.String("hinting", "none", "none | full")
    36  	size     = flag.Float64("size", 12, "font size in points")
    37  	spacing  = flag.Float64("spacing", 1.5, "line spacing (e.g. 2 means double spaced)")
    38  	wonb     = flag.Bool("whiteonblack", false, "white text on a black background")
    39  )
    40  
    41  const title = "Jabberwocky"
    42  
    43  var text = []string{
    44  	"’Twas brillig, and the slithy toves",
    45  	"Did gyre and gimble in the wabe;",
    46  	"All mimsy were the borogoves,",
    47  	"And the mome raths outgrabe.",
    48  	"",
    49  	"“Beware the Jabberwock, my son!",
    50  	"The jaws that bite, the claws that catch!",
    51  	"Beware the Jubjub bird, and shun",
    52  	"The frumious Bandersnatch!”",
    53  	"",
    54  	"He took his vorpal sword in hand:",
    55  	"Long time the manxome foe he sought—",
    56  	"So rested he by the Tumtum tree,",
    57  	"And stood awhile in thought.",
    58  	"",
    59  	"And as in uffish thought he stood,",
    60  	"The Jabberwock, with eyes of flame,",
    61  	"Came whiffling through the tulgey wood,",
    62  	"And burbled as it came!",
    63  	"",
    64  	"One, two! One, two! and through and through",
    65  	"The vorpal blade went snicker-snack!",
    66  	"He left it dead, and with its head",
    67  	"He went galumphing back.",
    68  	"",
    69  	"“And hast thou slain the Jabberwock?",
    70  	"Come to my arms, my beamish boy!",
    71  	"O frabjous day! Callooh! Callay!”",
    72  	"He chortled in his joy.",
    73  	"",
    74  	"’Twas brillig, and the slithy toves",
    75  	"Did gyre and gimble in the wabe;",
    76  	"All mimsy were the borogoves,",
    77  	"And the mome raths outgrabe.",
    78  }
    79  
    80  func main() {
    81  	flag.Parse()
    82  
    83  	// Read the font data.
    84  	fontBytes, err := ioutil.ReadFile(*fontfile)
    85  	if err != nil {
    86  		log.Println(err)
    87  		return
    88  	}
    89  	f, err := truetype.Parse(fontBytes)
    90  	if err != nil {
    91  		log.Println(err)
    92  		return
    93  	}
    94  
    95  	// Draw the background and the guidelines.
    96  	fg, bg := image.Black, image.White
    97  	ruler := color.RGBA{0xdd, 0xdd, 0xdd, 0xff}
    98  	if *wonb {
    99  		fg, bg = image.White, image.Black
   100  		ruler = color.RGBA{0x22, 0x22, 0x22, 0xff}
   101  	}
   102  	const imgW, imgH = 640, 480
   103  	rgba := image.NewRGBA(image.Rect(0, 0, imgW, imgH))
   104  	draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
   105  	for i := 0; i < 200; i++ {
   106  		rgba.Set(10, 10+i, ruler)
   107  		rgba.Set(10+i, 10, ruler)
   108  	}
   109  
   110  	// Draw the text.
   111  	h := font.HintingNone
   112  	switch *hinting {
   113  	case "full":
   114  		h = font.HintingFull
   115  	}
   116  	d := &font.Drawer{
   117  		Dst: rgba,
   118  		Src: fg,
   119  		Face: truetype.NewFace(f, &truetype.Options{
   120  			Size:    *size,
   121  			DPI:     *dpi,
   122  			Hinting: h,
   123  		}),
   124  	}
   125  	y := 10 + int(math.Ceil(*size**dpi/72))
   126  	dy := int(math.Ceil(*size * *spacing * *dpi / 72))
   127  	d.Dot = fixed.Point26_6{
   128  		X: (fixed.I(imgW) - d.MeasureString(title)) / 2,
   129  		Y: fixed.I(y),
   130  	}
   131  	d.DrawString(title)
   132  	y += dy
   133  	for _, s := range text {
   134  		d.Dot = fixed.P(10, y)
   135  		d.DrawString(s)
   136  		y += dy
   137  	}
   138  
   139  	// Save that RGBA image to disk.
   140  	outFile, err := os.Create("out.png")
   141  	if err != nil {
   142  		log.Println(err)
   143  		os.Exit(1)
   144  	}
   145  	defer outFile.Close()
   146  	b := bufio.NewWriter(outFile)
   147  	err = png.Encode(b, rgba)
   148  	if err != nil {
   149  		log.Println(err)
   150  		os.Exit(1)
   151  	}
   152  	err = b.Flush()
   153  	if err != nil {
   154  		log.Println(err)
   155  		os.Exit(1)
   156  	}
   157  	fmt.Println("Wrote out.png OK.")
   158  }
   159  

View as plain text