本文整理汇总了Golang中github.com/golang/freetype.NewContext函数的典型用法代码示例。如果您正苦于以下问题:Golang NewContext函数的具体用法?Golang NewContext怎么用?Golang NewContext使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewContext函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: drawStringImage
func (this *Signer) drawStringImage(text string) (image.Image, error) {
fontBytes, err := ioutil.ReadFile(this.fontPath)
if err != nil {
fmt.Println(err)
}
font, err := freetype.ParseFont(fontBytes)
if err != nil {
fmt.Println(err)
}
rgba := image.NewRGBA(image.Rect(0, 0, 900, 900))
draw.Draw(rgba, rgba.Bounds(), image.Black, image.ZP, draw.Src)
c := freetype.NewContext()
c.SetDPI(this.dpi)
c.SetFontSize(this.fontSize)
c.SetClip(rgba.Bounds())
c.SetDst(rgba)
c.SetSrc(image.White)
c.SetFont(font)
pt := freetype.Pt(100, 100+int(c.PointToFixed(this.fontSize)>>8))
for _, s := range strings.Split(text, "\r\n") {
_, err = c.DrawString(s, pt)
pt.Y += c.PointToFixed(12 * 1.5)
}
return rgba, nil
}
开发者ID:madong-fun,项目名称:golang-study,代码行数:32,代码来源:drawImage.go
示例2: NewFontFace
func NewFontFace(path string, pixels float32, fg, bg color.Color) (fontface *FontFace, err error) {
var (
font *truetype.Font
fontbytes []byte
bounds fixed.Rectangle26_6
context *freetype.Context
points float32
dpi float32 = 96
)
if fontbytes, err = ioutil.ReadFile(path); err != nil {
return
}
if font, err = freetype.ParseFont(fontbytes); err != nil {
return
}
points = pixels * 72 / dpi
bounds = font.Bounds(fixed.I(int(pixels)))
context = freetype.NewContext()
context.SetFont(font)
context.SetFontSize(float64(points))
context.SetDPI(float64(dpi))
fontface = &FontFace{
font: font,
charw: float32(bounds.Max.X-bounds.Min.X) / 64,
charh: float32(bounds.Max.Y-bounds.Min.Y) / 64,
offy: float32(bounds.Min.Y) / 64,
fg: fg,
bg: bg,
context: context,
}
return
}
开发者ID:kurrik,项目名称:opengl-benchmarks,代码行数:32,代码来源:fontface.go
示例3: init
func (a *Annotator) init() error {
// load the font
fontBytes, err := resources.Asset(fontfile)
if err != nil {
return err
}
luxisr, err := freetype.ParseFont(fontBytes)
if err != nil {
return err
}
// Initialize the context.
fg := image.White
a.context = freetype.NewContext()
a.context.SetDPI(dpi)
a.context.SetFont(luxisr)
a.context.SetFontSize(size)
a.context.SetClip(a.image.Bounds())
a.context.SetDst(a.image)
a.context.SetSrc(fg)
switch hinting {
default:
a.context.SetHinting(font.HintingNone)
case "full":
a.context.SetHinting(font.HintingFull)
}
return nil
}
开发者ID:dhogborg,项目名称:rtl-gopow,代码行数:34,代码来源:annotater.go
示例4: updateTexture
func (font *Font) updateTexture(texture uint32, text string, width int, height int, size float64, dpi float64, rgba color.Color) (int, int) {
context := freetype.NewContext()
context.SetFont(font.ttf)
img := image.NewRGBA(image.Rect(0, 0, width, height))
r, g, b, _ := rgba.RGBA()
draw.Draw(img, img.Bounds(), image.NewUniform(color.RGBA{uint8(r), uint8(g), uint8(b), 0}), image.ZP, draw.Src)
context.SetDst(img)
context.SetClip(img.Bounds())
context.SetSrc(image.NewUniform(rgba))
context.SetFontSize(size)
context.SetDPI(dpi)
pixelBounds, _ := context.DrawString(text, freetype.Pt(0, height/2))
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, texture)
gl.TexSubImage2D(
gl.TEXTURE_2D,
0,
0,
0,
int32(img.Rect.Size().X),
int32(img.Rect.Size().Y),
gl.RGBA,
gl.UNSIGNED_BYTE,
gl.Ptr(img.Pix))
return int26_6Ceiling(pixelBounds.X + 0x3f), int26_6Ceiling(pixelBounds.Y + 0x3f)
}
开发者ID:anthonyrego,项目名称:gosmf,代码行数:31,代码来源:font.go
示例5: NewFontFace
func NewFontFace(path string, size float64, fg, bg color.Color) (fontface *FontFace, err error) {
var (
font *truetype.Font
fontbytes []byte
bounds truetype.Bounds
context *freetype.Context
scale float32
)
if fontbytes, err = ioutil.ReadFile(path); err != nil {
return
}
if font, err = freetype.ParseFont(fontbytes); err != nil {
return
}
bounds = font.Bounds(1)
fmt.Printf("bounds %v\n", bounds)
context = freetype.NewContext()
context.SetFont(font)
context.SetFontSize(size)
context.SetDPI(72)
scale = float32(context.PointToFixed(size) / 64)
fontface = &FontFace{
font: font,
charw: scale * float32(bounds.XMax-bounds.XMin),
charh: scale * float32(bounds.YMax-bounds.YMin),
fg: fg,
bg: bg,
context: context,
}
return
}
开发者ID:pikkpoiss,项目名称:twodee,代码行数:31,代码来源:text.go
示例6: Render
func (f *Font) Render(text string) *Texture {
width, height, yBearing := f.TextDimensions(text)
font := f.ttf
size := f.Size
// Colors
fg := image.NewUniform(color.NRGBA{f.FG.R, f.FG.G, f.FG.B, f.FG.A})
bg := image.NewUniform(color.NRGBA{f.BG.R, f.BG.G, f.BG.B, f.BG.A})
// Create the font context
c := freetype.NewContext()
nrgba := image.NewNRGBA(image.Rect(0, 0, width, height))
draw.Draw(nrgba, nrgba.Bounds(), bg, image.ZP, draw.Src)
c.SetDPI(dpi)
c.SetFont(font)
c.SetFontSize(size)
c.SetClip(nrgba.Bounds())
c.SetDst(nrgba)
c.SetSrc(fg)
// Draw the text.
pt := freetype.Pt(0, int(yBearing))
_, err := c.DrawString(text, pt)
if err != nil {
log.Println(err)
return nil
}
// Create texture
imObj := &ImageObject{nrgba}
return NewTexture(imObj)
}
开发者ID:Kunde21,项目名称:engi,代码行数:35,代码来源:font.go
示例7: Example
func Example() {
// As usual in examples, this ignores all errors. Don't do this in your program.
// setup and find start point for centering
s := "Hello, World!"
size := image.Rect(0, 0, 120, 20)
dst := image.NewRGBA(size)
c := freetype.NewContext()
c.SetFont(font)
c.SetFontSize(14.0)
c.SetSrc(image.NewUniform(color.Black))
c.SetDst(dst)
start, _ := fontutil.CenterX(c, s, size) // CenterX calls c.SetClip(size)
// perform draw at start.X + y 16
c.DrawString(s, start.Add(freetype.Pt(0, 16)))
// write the image out to a file
// out, _ := os.Create("helloworld.png")
// defer out.Close()
// write image to hash for testing purposes
out := fnv.New64()
_ = png.Encode(out, dst)
fmt.Printf("Hash of compressed image: %x", out.Sum64())
// Output: Hash of compressed image: fa83a1b8d8abf5f2
}
开发者ID:infogulch,项目名称:fontutil,代码行数:27,代码来源:example_test.go
示例8: makeImage
func makeImage(req *http.Request, caption, font string, pt, size, border, scale int, f func(x, y int) uint32) *image.RGBA {
d := (size + 2*border) * scale
csize := 0
if caption != "" {
if pt == 0 {
pt = 11
}
csize = pt * 2
}
c := image.NewRGBA(image.Rect(0, 0, d, d+csize))
// white
u := &image.Uniform{C: color.White}
draw.Draw(c, c.Bounds(), u, image.ZP, draw.Src)
for y := 0; y < size; y++ {
for x := 0; x < size; x++ {
r := image.Rect((x+border)*scale, (y+border)*scale, (x+border+1)*scale, (y+border+1)*scale)
rgba := f(x, y)
u.C = color.RGBA{byte(rgba >> 24), byte(rgba >> 16), byte(rgba >> 8), byte(rgba)}
draw.Draw(c, r, u, image.ZP, draw.Src)
}
}
if csize != 0 {
if font == "" {
font = "data/luxisr.ttf"
}
ctxt := fs.NewContext(req)
dat, _, err := ctxt.Read(font)
if err != nil {
panic(err)
}
tfont, err := freetype.ParseFont(dat)
if err != nil {
panic(err)
}
ft := freetype.NewContext()
ft.SetDst(c)
ft.SetDPI(100)
ft.SetFont(tfont)
ft.SetFontSize(float64(pt))
ft.SetSrc(image.NewUniform(color.Black))
ft.SetClip(image.Rect(0, 0, 0, 0))
wid, err := ft.DrawString(caption, freetype.Pt(0, 0))
if err != nil {
panic(err)
}
p := freetype.Pt(d, d+3*pt/2)
p.X -= wid.X
p.X /= 2
ft.SetClip(c.Bounds())
ft.DrawString(caption, p)
}
return c
}
开发者ID:lucmichalski,项目名称:qr-server,代码行数:57,代码来源:pic.go
示例9: initLayout
// initLayout constructs two masks for drawing the battery and the remaining
// energy as well as sets the pixel bounds for drawing energy capacity. the
// masks allow for simplified space-fills and reduced chance of pixel gaps.
func (app *App) initLayout() {
var zeropt image.Point
rectOutTop := image.Rectangle{Min: app.Layout.battRect.Min, Max: app.Layout.battRect.Min.Add(image.Point{2, 2})}
rectOutBottom := rectOutTop.Add(image.Point{Y: app.Layout.battRect.Size().Y - rectOutTop.Size().Y})
capRect := image.Rectangle{
Min: image.Point{X: rectOutTop.Min.X, Y: rectOutTop.Max.Y},
Max: image.Point{X: rectOutBottom.Max.X, Y: rectOutBottom.Min.Y},
}
bodyRect := app.Layout.battRect
bodyRect.Min.X = capRect.Max.X
// energy will be drawn under the battery shell. The only place where it
// is not safe to draw energy is outside the battery on the positive end.
energyMask := image.NewAlpha(app.Layout.battRect)
draw.Draw(energyMask, app.Layout.battRect, opaque, zeropt, draw.Over)
draw.Draw(energyMask, rectOutTop, transparent, zeropt, draw.Src)
draw.Draw(energyMask, rectOutBottom, transparent, zeropt, draw.Src)
app.maskEnergy = energyMask
// the body uses the same mask as the energy with additional transparency
// inside the battery's shell. the mask construction is complex because
// area inside the cap may be exposed.
bodyMask := image.NewAlpha(app.Layout.battRect)
draw.Draw(bodyMask, app.Layout.battRect, energyMask, app.Layout.battRect.Min, draw.Over)
bodyMaskRect := shrinkRect(bodyRect, app.Layout.thickness)
draw.Draw(bodyMask, bodyMaskRect, transparent, zeropt, draw.Src)
capMaskRect := shrinkRect(capRect, app.Layout.thickness)
capMaskRect.Max.X += 2 * app.Layout.thickness
draw.Draw(bodyMask, capMaskRect, transparent, zeropt, draw.Src)
app.maskBattery = bodyMask
// create a freetype.Context to render text. each time the context is used
// it must have its SetDst method called.
app.tt = freetype.NewContext()
app.tt.SetSrc(black)
app.tt.SetClip(app.Layout.textRect)
app.tt.SetDPI(app.Layout.DPI)
app.tt.SetFont(app.Layout.font)
app.tt.SetFontSize(app.Layout.fontSize)
ttopt := &truetype.Options{
Size: app.Layout.fontSize,
DPI: app.Layout.DPI,
}
ttface := truetype.NewFace(app.Layout.font, ttopt)
app.font = &font.Drawer{
Src: black,
Face: ttface,
}
// the rectangle in which energy is drawn needs to account for thickness to
// make the visible percentage more accurate. after adjustment reduce the
// energy rect to account for the account of energy drained. the energy
// mask makes computing Y bounds largely irrelevant.
app.minEnergy = capMaskRect.Min.X
app.maxEnergy = bodyMaskRect.Max.X
}
开发者ID:bmatsuo,项目名称:dockapp-go,代码行数:60,代码来源:main.go
示例10: main
func main() {
fontBytes, err := ioutil.ReadFile("luxisr.ttf")
if err != nil {
log.Fatal(err)
}
font, err := freetype.ParseFont(fontBytes)
if err != nil {
log.Fatal(err)
}
fg, bg := image.White, image.Black
rgba := image.NewRGBA(image.Rect(0, 0, width, height))
draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
c := freetype.NewContext()
c.SetDPI(dpi)
c.SetFont(font)
c.SetFontSize(size)
c.SetClip(rgba.Bounds())
c.SetDst(rgba)
c.SetSrc(fg)
c.SetHinting(freetype.FullHinting)
count := 0
l := NewLife(width, height)
for {
if count%10 == 0 {
count = 0
pt := freetype.Pt(rand.Intn(width-width/4), height/4+rand.Intn(height-height/4))
c.DrawString("NOPE", pt)
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
c := rgba.RGBAAt(x, y)
l.a.Set(x, y, c.R > 0 || c.B > 0 || c.G > 0)
}
}
}
count++
for x := 0; x < width; x++ {
for y := 0; y < height; y++ {
var c color.Color = color.Black
if l.a.Alive(x, y) {
c = rainbow(y)
}
rgba.Set(x, y, c)
}
}
//fmt.Println(l)
sendImage(rgba)
l.Step()
time.Sleep(time.Second / 8)
}
}
开发者ID:nf,项目名称:nope,代码行数:55,代码来源:nope.go
示例11: WriteText
// Write a text inside the image
func WriteText(text string, f *truetype.Font, img *image.Paletted) {
// Initialize the context.
fg := image.Black
c := freetype.NewContext()
c.SetDPI(72)
c.SetFont(f)
c.SetFontSize(64)
c.SetClip(img.Bounds())
c.SetDst(img)
c.SetSrc(fg)
// Draw the text.
pt := freetype.Pt(40, 120)
c.DrawString(text, pt)
}
开发者ID:maxzerbini,项目名称:packagemain,代码行数:15,代码来源:animatedgif.go
示例12: TextLen
func (ig *ImageGraphics) TextLen(s string, font chart.Font) int {
c := freetype.NewContext()
c.SetDPI(dpi)
c.SetFont(ig.font)
fontsize := ig.relFontsizeToPixel(font.Size)
c.SetFontSize(fontsize)
// really draw it
width, err := c.DrawString(s, freetype.Pt(0, 0))
if err != nil {
return 10 * len(s) // BUG
}
return int(width.X+32)>>6 + 1
}
开发者ID:RobinTec,项目名称:chart,代码行数:14,代码来源:image.go
示例13: MakeTextLine
func MakeTextLine(font_name, text string, width int, r, g, b, a float64) *TextLine {
var w TextLine
w.EmbeddedWidget = &BasicWidget{CoreWidget: &w}
font, ok := basic_fonts[font_name]
if !ok {
panic(fmt.Sprintf("Unable to find a font registered as '%s'.", font_name))
}
w.font = font
w.glyph_buf = &truetype.GlyphBuf{}
w.next_text = text
w.context = freetype.NewContext()
w.context.SetDPI(132)
w.context.SetFontSize(12)
w.SetColor(r, g, b, a)
w.Request_dims = Dims{width, 35}
return &w
}
开发者ID:losinggeneration,项目名称:glop,代码行数:17,代码来源:text_line.go
示例14: RenderNRGBA
func (f *Font) RenderNRGBA(text string) *image.NRGBA {
width, height, yBearing := f.TextDimensions(text)
font := f.TTF
size := f.Size
if size <= 0 {
panic("Font size cannot be <= 0")
}
// Default colors
if f.FG == nil {
f.FG = color.NRGBA{0, 0, 0, 0}
}
if f.BG == nil {
f.BG = color.NRGBA{0, 0, 0, 0}
}
// Colors
fg := image.NewUniform(f.FG)
bg := image.NewUniform(f.BG)
// Create the font context
c := freetype.NewContext()
nrgba := image.NewNRGBA(image.Rect(0, 0, width, height))
draw.Draw(nrgba, nrgba.Bounds(), bg, image.ZP, draw.Src)
c.SetDPI(dpi)
c.SetFont(font)
c.SetFontSize(size)
c.SetClip(nrgba.Bounds())
c.SetDst(nrgba)
c.SetSrc(fg)
// Draw the text.
pt := fixed.P(0, int(yBearing))
_, err := c.DrawString(text, pt)
if err != nil {
log.Println(err)
return nil
}
return nrgba
}
开发者ID:matiwinnetou,项目名称:engi,代码行数:44,代码来源:font.go
示例15: main
func main() {
imgcounter := 123
imgfile, _ := os.Create(fmt.Sprintf("%03d.png", imgcounter))
defer imgfile.Close()
img := image.NewNRGBA(image.Rect(0, 0, dx, dy))
for y := 0; y < dy; y++ {
for x := 0; x < dx; x++ {
img.Set(x, y, color.RGBA{uint8(x), uint8(y), 0, 255})
}
}
fontBytes, err := ioutil.ReadFile(fontFile) //读取字体数据
if err != nil {
fmt.Println(err)
return
}
font, err := freetype.ParseFont(fontBytes)
if err != nil {
fmt.Print(err)
return
}
c := freetype.NewContext()
c.SetDPI(fontDPI)
c.SetFont(font)
c.SetFontSize(fontSize)
c.SetClip(img.Bounds())
c.SetDst(img)
c.SetSrc(image.White)
pt := freetype.Pt(10, 10+int(c.PointToFixed(fontSize)>>8)) // 字出现的位置
_, err = c.DrawString("ABCDE", pt)
if err != nil {
fmt.Println(err)
return
}
err = png.Encode(imgfile, img)
if err != nil {
fmt.Println(err)
}
}
开发者ID:madong-fun,项目名称:golang-study,代码行数:44,代码来源:imageSign.go
示例16: getMaxHeight
func (t *Text) getMaxHeight() int {
w, h := t.GetStringSize("|")
bounds := image.Rect(0, 0, int(w>>6), int(h>>6))
img := image.NewRGBA(bounds)
ctx := freetype.NewContext()
ctx.SetFont(t.font)
ctx.SetFontSize(t.fontSize)
ctx.SetSrc(&image.Uniform{t.fillColor})
ctx.SetDst(img)
ctx.SetClip(bounds)
ctx.DrawString("|", freetype.Pt(0, int(t.fontSize)))
var i = len(img.Pix) - 1
for ; img.Pix[i] == 0; i-- {
}
return (i / img.Stride) + 1
}
开发者ID:stephenwithav,项目名称:ssvgc,代码行数:20,代码来源:text.go
示例17: createTextImage
func createTextImage(text string, fontSize int, color color.Color, f *truetype.Font) (*image.RGBA, float32, float32, error) {
// 1 pt = 1/72 in, 72 dpi = 1 in
const dpi = 72
fg, bg := image.NewUniform(color), image.Transparent
c := freetype.NewContext()
c.SetFont(f)
c.SetDPI(dpi)
c.SetFontSize(float64(fontSize)) // points
c.SetSrc(fg)
c.SetHinting(font.HintingFull)
// 1. Figure out maximum height so all text lines are the same height.
// 2. Draw within small bounds to figure out bounds.
// 3. Draw within final bounds.
scale := c.PointToFixed(float64(fontSize)) // point to pixels
rect := f.Bounds(scale) // scale is pixels of 1 em
maxHeight := int(rect.Max.Y>>6) - int(rect.Min.Y>>6)
var rgba *image.RGBA
w, h := 10, maxHeight
for i := 0; i < 2; i++ {
rgba = image.NewRGBA(image.Rect(0, 0, w, h))
draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
c.SetClip(rgba.Bounds())
c.SetDst(rgba)
pt := freetype.Pt(0, int(scale>>6))
end, err := c.DrawString(text, pt)
if err != nil {
return nil, 0, 0, err
}
w = int(end.X >> 6)
}
return rgba, float32(w), float32(h), nil
}
开发者ID:btmura,项目名称:blockcillin,代码行数:41,代码来源:text.go
示例18: mark
func (this *Signer) mark(text string) (image.Image, error) {
fontBytes, err := ioutil.ReadFile(this.fontPath)
if err != nil {
fmt.Println(err)
}
font, err := freetype.ParseFont(fontBytes)
if err != nil {
fmt.Println(err)
}
//读取图片
file, err := os.Open(this.srcImage)
if err != nil {
fmt.Println(err)
}
defer file.Close()
img, err := jpeg.Decode(file)
if err != nil {
fmt.Println(err)
}
bounds := img.Bounds()
nrgba := image.NewNRGBA(bounds)
draw.Draw(nrgba, img.Bounds(), img, image.ZP, draw.Over)
c := freetype.NewContext()
c.SetDPI(this.dpi)
c.SetFont(font)
c.SetDst(nrgba)
c.SetFontSize(this.fontSize)
c.SetClip(nrgba.Bounds())
c.SetSrc(image.White)
pt := freetype.Pt(100, 100+int(c.PointToFixed(this.fontSize)>>8))
for _, s := range strings.Split(text, "\r\n") {
_, err = c.DrawString(s, pt)
pt.Y += c.PointToFixed(12 * 1.5)
}
return nrgba, nil
}
开发者ID:madong-fun,项目名称:golang-study,代码行数:41,代码来源:markImage.go
示例19: drawText
func drawText(img *image.RGBA, font *truetype.Font, color color.Color, x, y int, s string) error {
var ptSize float64 = 12
ctx := freetype.NewContext()
ctx.SetDPI(72)
ctx.SetFont(font)
ctx.SetFontSize(ptSize)
ctx.SetClip(img.Bounds())
ctx.SetDst(img)
ctx.SetSrc(image.NewUniform(color))
ctx.SetHinting(img_font.HintingFull)
width := int(widthOfString(font, ptSize, s))
pt := freetype.Pt(x-width/2, y+int(int32(ctx.PointToFixed(ptSize))>>8)/2)
_, err := ctx.DrawString(s, pt)
if err != nil {
return err
}
return nil
}
开发者ID:Ganners,项目名称:go-tile-server,代码行数:21,代码来源:draw.go
示例20: TextToImage
func TextToImage(text string, fontSize float64, fontFile string, fontColor color.Color) (image.Image, error) {
lines := len(strings.Split(text, "\n"))
maxLen := getMaxLen(text)
margin := 10
width := maxLen*int(fontSize) + margin*2
height := int(fontSize)*lines + margin*2
var fontDPI float64 = 72
fontBytes, err := ioutil.ReadFile(fontFile)
if err != nil {
return nil, err
}
font, err := freetype.ParseFont(fontBytes)
if err != nil {
return nil, err
}
bg := image.Transparent
newImg := image.NewRGBA(image.Rect(0, 0, width, height))
draw.Draw(newImg, newImg.Bounds(), bg, image.ZP, draw.Src)
c := freetype.NewContext()
c.SetDPI(fontDPI)
c.SetFont(font)
c.SetFontSize(fontSize)
c.SetClip(newImg.Bounds())
c.SetDst(newImg)
c.SetSrc(image.NewUniform(fontColor))
// Draw the text.
for i, s := range strings.Split(text, "\n") {
Y := int(fontSize) + i*int(fontSize)
pt := freetype.Pt(margin, Y)
_, err := c.DrawString(s, pt)
if err != nil {
return nil, err
}
}
return newImg, nil
}
开发者ID:mabetle,项目名称:mcore,代码行数:40,代码来源:img_text.go
注:本文中的github.com/golang/freetype.NewContext函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论