本文整理汇总了Golang中golang.org/x/image/math/fixed.P函数的典型用法代码示例。如果您正苦于以下问题:Golang P函数的具体用法?Golang P怎么用?Golang P使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了P函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: drawPerimeterNumber
func drawPerimeterNumber(dst draw.Image, pt image.Point, number string) {
dot := fixed.P(pt.X, pt.Y)
d := font.Drawer{
Dst: dst,
Src: image.NewUniform(color.RGBA{B: 0xFF, A: 0xFF}),
Face: basicfont.Face7x13,
Dot: dot,
}
d.DrawString(number)
}
示例2: Render
func (c *Context) Render(txt string, size float64, col color.Color) (*image.RGBA, error) {
bnd := c.fnt.Bounds(fixed.I(int(size + 0.5)))
lh := int26_6ToFloat64(bnd.Max.Y) - int26_6ToFloat64(bnd.Min.Y) - 0.5
c.ft.SetSrc(image.NewUniform(col))
c.ft.SetFontSize(size)
/* Render image to temporary buffer to determine final size */
tmp := nullImage{}
c.ft.SetDst(tmp)
c.ft.SetClip(tmp.Bounds())
p, err := c.ft.DrawString(txt, fixed.P(0, int(lh)))
if err != nil {
return nil, err
}
dst := image.NewRGBA(image.Rect(0, 0, int(int26_6ToFloat64(p.X)+0.5), int(lh)))
draw.Draw(dst, dst.Bounds(), image.NewUniform(color.RGBA{}), image.ZP, draw.Src)
c.ft.SetDst(dst)
c.ft.SetClip(dst.Bounds())
p, err = c.ft.DrawString(txt, fixed.P(0, int(size)))
if err != nil {
return nil, err
}
return dst, nil
}
示例3: main
func main() {
const (
w = 400
h = 400
)
r := raster.NewRasterizer(w, h)
r.UseNonZeroWinding = true
cjs := []struct {
c raster.Capper
j raster.Joiner
}{
{raster.RoundCapper, raster.RoundJoiner},
{raster.ButtCapper, raster.BevelJoiner},
{raster.SquareCapper, raster.BevelJoiner},
}
for i, cj := range cjs {
var path raster.Path
path.Start(fixed.P(30+100*i, 30+120*i))
path.Add1(fixed.P(180+100*i, 80+120*i))
path.Add1(fixed.P(50+100*i, 130+120*i))
raster.Stroke(r, path, fixed.I(20), cj.c, cj.j)
}
rgba := image.NewRGBA(image.Rect(0, 0, w, h))
draw.Draw(rgba, rgba.Bounds(), image.Black, image.Point{}, draw.Src)
p := raster.NewRGBAPainter(rgba)
p.SetColor(color.RGBA{0x7f, 0x7f, 0x7f, 0xff})
r.Rasterize(p)
white := color.RGBA{0xff, 0xff, 0xff, 0xff}
for i := range cjs {
rgba.SetRGBA(30+100*i, 30+120*i, white)
rgba.SetRGBA(180+100*i, 80+120*i, white)
rgba.SetRGBA(50+100*i, 130+120*i, white)
}
// 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.")
}
示例4: TestHorizontalLayout
func TestHorizontalLayout(t *testing.T) {
layout := NewHorizontalLayout(fixed.P(10, 10), fixed.P(100, 100))
if r, ok := layout.NextBounds(); !ok {
t.Error("no bounds returned by the horizontal layout")
} else if r != fixed.R(10, 10, 110, 110) {
t.Error("invalid bounds returned by the horizontal layout:", r)
}
if r, ok := layout.NextBounds(); !ok {
t.Error("no bounds returned by the horizontal layout")
} else if r != fixed.R(110, 10, 210, 110) {
t.Error("invalid bounds returned by the horizontal layout:", r)
}
}
示例5: BenchmarkDrawString
func BenchmarkDrawString(b *testing.B) {
data, err := ioutil.ReadFile("../licenses/gpl.txt")
if err != nil {
b.Fatal(err)
}
lines := strings.Split(string(data), "\n")
data, err = ioutil.ReadFile("../testdata/luxisr.ttf")
if err != nil {
b.Fatal(err)
}
f, err := Parse(data)
if err != nil {
b.Fatal(err)
}
dst := image.NewRGBA(image.Rect(0, 0, 800, 600))
draw.Draw(dst, dst.Bounds(), image.White, image.ZP, draw.Src)
d := &font.Drawer{
Dst: dst,
Src: image.Black,
Face: NewFace(f, nil),
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j, line := range lines {
d.Dot = fixed.P(0, (j*16)%600)
d.DrawString(line)
}
}
}
示例6: TestDrawView
func TestDrawView(t *testing.T) {
const str = "Hello World!\nHow are you doing?"
f1 := truetype.NewFace(clearSans, &truetype.Options{DPI: 144})
f2 := truetype.NewFace(clearSansBoldItalic, &truetype.Options{DPI: 144})
red := image.NewUniform(color.RGBA{255, 0, 0, 255})
yellow := image.NewUniform(color.RGBA{255, 255, 0, 255})
view, _ := Render(
NewReader(
strings.NewReader("Hello World!\nHow are you doing?"),
Style{Offset: 0, Face: f1, Foreground: image.Black, Background: yellow},
Style{Offset: 10, Face: f2, Foreground: red, Background: yellow},
Style{Offset: 20, Face: f1, Foreground: image.Black, Background: image.White},
),
NewNaturalLayout(fixed.P(0, 0)),
)
size := view.Bounds.Max.Sub(view.Bounds.Min)
for _, a := range []Alignment{Left, Right, Center, Justify} {
dst := image.NewRGBA(image.Rect(0, 0, int(size.X>>6)+1, int(size.Y>>6)+1))
view.Align(a)
view.Draw(dst, LeftToRight)
saveTest(t, dst, "text.View.Draw_"+a.(fmt.Stringer).String()+".png")
}
}
示例7: newTextTexture
func (l *Label) newTextTexture(eng sprite.Engine) sprite.SubTex {
fg, bg := image.Black, image.White
draw.Draw(l.rgba, l.rgba.Bounds(), bg, image.ZP, draw.Src)
d := &sfont.Drawer{
Dst: l.rgba,
Src: fg,
Face: truetype.NewFace(l.font, truetype.Options{
Size: l.fontSize,
DPI: 72,
Hinting: sfont.HintingFull,
}),
}
spacing := 1.5
dy := int(math.Ceil(l.fontSize * spacing))
for i, s := range strings.Split(l.Text, "\n") {
d.Dot = fixed.P(0, int(l.fontSize*0.8)+dy*i)
d.DrawString(s)
}
t, err := eng.LoadTexture(l.rgba)
if err != nil {
log.Fatal(err)
}
return sprite.SubTex{t, l.rgba.Bounds()}
}
示例8: drawLayerNumber
func drawLayerNumber(dst draw.Image, n int) {
d := font.Drawer{
Dst: dst,
Src: image.Black,
Face: basicfont.Face7x13,
Dot: fixed.P(2, 13),
}
d.DrawString(fmt.Sprintf("Layer %03d", n))
}
示例9: TestVerticalLayout
func TestVerticalLayout(t *testing.T) {
layout := NewVerticalLayout(fixed.P(10, 10), fixed.I(100))
if r, ok := layout.NextBounds(); !ok {
t.Error("no bounds returned by the vertical layout")
} else if r != fixed.R(10, 10, 110, 33554431) {
t.Error("invalid bounds returned by the vertical layout:", r)
}
if _, ok := layout.NextBounds(); ok {
t.Error("incomplete vertical layout")
}
}
示例10: 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
}
示例11: TestCharAtSuccess
func TestCharAtSuccess(t *testing.T) {
f := truetype.NewFace(clearSans, nil)
defer f.Close()
c := NewReader(strings.NewReader("Hello World!\n"),
Style{Offset: 0, Face: f, Foreground: image.Black, Background: image.White},
)
view, err := Render(c, NewNaturalLayout(fixed.P(1, 1)))
if err != nil {
t.Error(err)
return
}
char, bounds, ok := view.CharAt(fixed.Point26_6{
X: int26_6(31, 0),
Y: int26_6(5, 0),
}, LeftToRight)
if !ok {
t.Error("no char found")
return
}
if char != (Char{
Offset: 5,
Rune: ' ',
Face: f,
Foreground: image.Black,
Background: image.White,
}) {
t.Error("invalid char found:", char)
}
if bounds != (fixed.Rectangle26_6{
Min: fixed.Point26_6{X: int26_6(28, 37), Y: int26_6(1, 0)},
Max: fixed.Point26_6{X: int26_6(31, 35), Y: int26_6(15, 5)},
}) {
t.Error("invalid char bounds:", bounds)
}
}
示例12: Create
func (t *TextTexture) Create(eng sprite.Engine, text string) (sprite.SubTex, error) {
draw.Draw(t.rgba, t.rgba.Bounds(), t.bg, image.ZP, draw.Src)
d := &sfont.Drawer{
Dst: t.rgba,
Src: t.fg,
Face: t.Face,
}
dy := int(math.Ceil(t.Face.Size * t.Spacing))
for i, s := range strings.Split(text, "\n") {
d.Dot = fixed.P(0, int(t.Face.Size*0.8)+dy*i)
d.DrawString(s)
}
tex, err := eng.LoadTexture(t.rgba)
if err != nil {
return sprite.SubTex{}, err
}
return sprite.SubTex{tex, t.rgba.Bounds()}, nil
}
示例13: Render
func (f *Font) Render(text string, width, height float32, color Color) *Texture {
/* Set color */
f.drawer.Src = image.NewUniform(color.RGBA())
line := math.Ceil(f.Size * f.DPI / 72)
//height := int(f.Spacing * line)
//width := int(float64(f.drawer.MeasureString(text)) / f.Size)
/* Create and attach destination image */
rgba := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
f.drawer.Dst = rgba
/* Draw text */
f.drawer.Dot = fixed.P(0, int(line))
f.drawer.DrawString(text)
fmt.Println("Font texture size W:", width, "H:", height)
tx := CreateTexture(int32(width), int32(height))
tx.Buffer(rgba)
return tx
}
示例14: TestCharAtFailure
func TestCharAtFailure(t *testing.T) {
f := truetype.NewFace(clearSans, nil)
defer f.Close()
c := NewReader(strings.NewReader("Hello World!\n"),
Style{Offset: 0, Face: f, Foreground: image.Black, Background: image.White},
)
view, err := Render(c, NewNaturalLayout(fixed.P(1, 1)))
if err != nil {
t.Error(err)
return
}
char, bounds, ok := view.CharAt(fixed.Point26_6{}, LeftToRight)
if char != (Char{}) || bounds != (fixed.Rectangle26_6{}) || ok {
t.Error("not char should have been found at (0, 0) but we got", char, bounds, ok)
}
}
示例15: drawText
func (app *App) drawText(img draw.Image, metrics *battery.Metrics, f battery.MetricFormatter) error {
// measure the text so that it can be centered within the text area. if f
// is a MaxMetricFormatter use it's MaxFormattedWidth method to determine
// the appropriate centering position so that a change in metric values
// (but not formatter) will have a smooth transition in the ui.
app.font.Dst = img
text := f.Format(metrics)
measuretext := text
if fmax, ok := f.(battery.MaxMetricFormatter); ok {
measuretext = fmax.MaxFormattedWidth()
}
xoffset := app.font.MeasureString(measuretext)
ttwidth := int(xoffset >> 6)
ttheight := int(app.tt.PointToFixed(app.Layout.fontSize) >> 6)
padleft := (app.Layout.textRect.Size().X - ttwidth) / 2
padtop := (app.Layout.textRect.Size().Y - ttheight) / 2
x := app.Layout.textRect.Min.X + padleft
y := app.Layout.textRect.Max.Y - padtop
app.font.Dot = fixed.P(x, y)
app.font.DrawString(text)
return nil
}