Coder Social home page Coder Social logo

drawfont's Introduction

第一步:

根据编译命令生成适于自己平台的动态库

第二步:

修改draw.go里面关于freetype的链接路径

第三步:

font := DrawFont.NewDrawFont("./wryhBold.ttf")
//创建一个绘制对象

第四步:

f = font.DrawStringWithOutLine(s,whiteG,outG,220,20.0)
//绘制生成文字图片

最后:

draw.Draw(rgba.(draw.Image), r, f, sr.Min, draw.Over)
package main

import (
	"bufio"
	"flag"
	"fmt"
	"image/png"
	"log"
	"os"

	//"unicode"
	"image/color"
	"github.com/NiuStar/DrawFont"
	"image"
	"image/draw"
)

var (
	dpi      = flag.Float64("dpi", 72, "screen resolution in Dots Per Inch")
	fontfile = flag.String("fontfile", "./wryhBold.ttf", "filename of the ttf font")
	hinting  = flag.String("hinting", "none", "none | full")
	size     = flag.Float64("size", 280, "font size in points")
	outLineSize     = flag.Float64("outLineSize", 10, "font size in points")
	spacing  = flag.Float64("spacing", 1.5, "line spacing (e.g. 2 means double spaced)")
	wonb     = flag.Bool("whiteonblack", false, "white text on a black background")
)

var text = []string{
	"欢迎",
	"四川不知道啥的信息化中心",
	"某一个一行",
	"莅临参观指导",

}



func main() {

	b1,err := os.Open("background.png")

	if err != nil {
		panic(err)
	}

	rgba,_, err := image.Decode(b1)
	if err != nil {
		panic(err)
	}

	font := DrawFont.NewDrawFont("./wryhBold.ttf")

	whiteG := color.RGBA{0xff,0xff,0xff,0xff}
	yellowG := color.RGBA{0xff,241,0,0xff}
	outG := color.RGBA{27,91,97,0xff}


	for index, s := range text {

		var f *image.RGBA
		if index == 0 || index == (len(text) - 1) {
			f = font.DrawStringWithOutLine(s,whiteG,outG,220,20.0)
		} else {
			f = font.DrawStringWithOutLine(s,yellowG,outG,220,20.0)
		}

		pt := image.Pt(int((rgba.Bounds().Dx()- f.Bounds().Dx())/2),
			int((rgba.Bounds().Dy()-(f.Bounds().Dy() / 5 + f.Bounds().Dy())*(len(text)-index*int(2)) )/2))

		sr := f.Bounds()         // 获取要复制图片的尺寸
		r := sr.Sub(sr.Min).Add(pt)   // 目标图的要剪切区域

		draw.Draw(rgba.(draw.Image), r, f, sr.Min, draw.Over)
	}
	font.FreeDraw()

	// Save that RGBA image to disk.
	outFile, err := os.Create("out.png")
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}
	defer outFile.Close()
	b := bufio.NewWriter(outFile)
	err = png.Encode(b, rgba)
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}
	err = b.Flush()
	if err != nil {
		log.Println(err)
		os.Exit(1)
	}
	fmt.Println("Wrote out.png OK.")
}

drawfont's People

Contributors

niustar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.