当前位置: 首页>>代码示例>>Golang>>正文


Golang truetype.NewFace函数代码示例

本文整理汇总了Golang中github.com/golang/freetype/truetype.NewFace函数的典型用法代码示例。如果您正苦于以下问题:Golang NewFace函数的具体用法?Golang NewFace怎么用?Golang NewFace使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了NewFace函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: 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")
	}
}
开发者ID:achille-roussel,项目名称:go-vu,代码行数:28,代码来源:draw_test.go

示例2: newDrawer

func newDrawer(fontFile string) (*drawer, error) {
	if fontFile == "" {
		return nil, errFontRequired
	}
	g := new(drawer)
	g.fontSize = 75.0
	g.dpi = 72.0
	g.fontHinting = font.HintingNone

	ttf, err := getTTF(fontFile)
	if err != nil {
		return nil, errInvalidTTF
	}
	g.face = truetype.NewFace(ttf, &truetype.Options{
		Size:    g.fontSize,
		DPI:     g.dpi,
		Hinting: g.fontHinting,
	})

	fontBytes, err := ioutil.ReadFile(fontFile)
	if err != nil {
		return nil, errInvalidTTF
	}
	font, err := freetype.ParseFont(fontBytes)
	if err != nil {
		return nil, errInvalidTTF
	}
	g.font = font
	return g, nil
}
开发者ID:rchunping,项目名称:initials-avatar,代码行数:30,代码来源:draw.go

示例3: 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()}
}
开发者ID:tenntenn,项目名称:gomoxy,代码行数:28,代码来源:label.go

示例4: buildWords

// buildWords generates word sprites.
func buildWords(words map[string]int, f *truetype.Font) []Word {
	ch := make(chan Word)
	wg := &sync.WaitGroup{}
	wg.Add(len(words))

	for k, v := range words {
		go func(w string, occ int) {
			face := truetype.NewFace(f, &truetype.Options{
				Size:    float64(occ * 12),
				DPI:     72,
				Hinting: font.HintingFull,
			})

			wd := buildSprite(w, face)
			wd.Weight = occ

			ch <- wd
			wg.Done()
		}(k, v)
	}

	go func() {
		wg.Wait()
		close(ch)
	}()

	var data []Word
	for w := range ch {
		data = append(data, w)
	}

	return data
}
开发者ID:marcusolsson,项目名称:exp,代码行数:34,代码来源:word.go

示例5: ReadFaceFile

// ReadFaceFile parses the contents of path as a truetype font.
func ReadFaceFile(path string, opt *truetype.Options) (font.Face, error) {
	ttf, err := ReadFontFile(path)
	if err != nil {
		return nil, err
	}
	face := truetype.NewFace(ttf, opt)
	return face, nil
}
开发者ID:bmatsuo,项目名称:dockapp-go,代码行数:9,代码来源:font.go

示例6: ReadFace

// ReadFace parses the data read from r as a truetype font.
func ReadFace(r io.Reader, opt *truetype.Options) (font.Face, error) {
	ttf, err := ReadFont(r)
	if err != nil {
		return nil, err
	}
	face := truetype.NewFace(ttf, opt)
	return face, nil
}
开发者ID:bmatsuo,项目名称:dockapp-go,代码行数:9,代码来源:font.go

示例7: 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

示例8: setup

func (f *Font) setup() {
	f.drawer = &font.Drawer{
		Face: truetype.NewFace(f.fnt, &truetype.Options{
			Size:    f.Size,
			DPI:     f.DPI,
			Hinting: font.HintingNone,
		}),
	}
}
开发者ID:johanhenriksson,项目名称:goworld,代码行数:9,代码来源:font.go

示例9: TestRunReaderEndOfLine

func TestRunReaderEndOfLine(t *testing.T) {
	f := truetype.NewFace(clearSans, nil)
	defer f.Close()

	c := NewReader(strings.NewReader("Hello World!\nHow are you doing?"),
		Style{Offset: 0, Face: f, Foreground: image.Black, Background: image.White},
		Style{Offset: 5, Face: f, Foreground: image.White},
	)

	r := NewRunReader(c, nil, fixed.Rectangle26_6{
		Min: fixed.Point26_6{X: fixed.I(1), Y: fixed.I(1)},
		Max: fixed.Point26_6{X: fixed.I(600), Y: fixed.I(600)},
	})

	var run Run
	var err error

	if run, err = r.ReadRun(); err != nil {
		t.Error(run, err)
	}

	if run != (Run{
		Offset:     0,
		Text:       "Hello",
		Face:       f,
		Foreground: image.Black,
		Background: image.White,
		Bounds: fixed.Rectangle26_6{
			Min: fixed.Point26_6{X: int26_6(1, 0), Y: int26_6(1, 0)},
			Max: fixed.Point26_6{X: int26_6(28, 37), Y: int26_6(13, 0)},
		},
	}) {
		t.Error("invalid first run:", run)
	}

	if run, err = r.ReadRun(); err != nil {
		t.Error(run, err)
	}

	if run != (Run{
		Offset:     5,
		Text:       " World!",
		Face:       f,
		Foreground: image.White,
		Background: image.White,
		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(65, 55), Y: int26_6(13, 0)},
		},
	}) {
		t.Error("invalid second run:", run)
	}

	if _, err = r.ReadRun(); err != io.EOF {
		t.Error(err)
	}
}
开发者ID:achille-roussel,项目名称:go-vu,代码行数:57,代码来源:run_test.go

示例10: TTF

func TTF(ttf *truetype.Font, data FaceData) Face {
	return ttfFace{
		Face: truetype.NewFace(ttf, &truetype.Options{
			Size:    data.Size,
			DPI:     data.DPI,
			Hinting: data.Hinting,
		}),
		font: ttf,
		data: data,
	}
}
开发者ID:achille-roussel,项目名称:go-vu,代码行数:11,代码来源:face.go

示例11: TestMeasureEmpty

func TestMeasureEmpty(t *testing.T) {
	f := truetype.NewFace(clearSans, nil)
	defer f.Close()

	s := MeasureBytes(nil, f)

	if s != (fixed.Point26_6{
		X: int26_6(0, 0),
		Y: int26_6(14, 60),
	}) {
		t.Error("invalid result of measuring an empty string:", s)
	}
}
开发者ID:achille-roussel,项目名称:go-vu,代码行数:13,代码来源:measure_test.go

示例12: TestMeasureMultiLine

func TestMeasureMultiLine(t *testing.T) {
	f := truetype.NewFace(clearSans, nil)
	defer f.Close()

	s := MeasureString("Hello World!\nHello World!", f)

	if s != (fixed.Point26_6{
		X: int26_6(64, 55),
		Y: int26_6(26, 60),
	}) {
		t.Error("invalid result of measuring a multi-line string:", s)
	}
}
开发者ID:achille-roussel,项目名称:go-vu,代码行数:13,代码来源:measure_test.go

示例13: renderMin

func (m *Module) renderMin(rgba *image.RGBA, position fixed.Point26_6) {
	d := &font.Drawer{
		Dst: rgba,
		Src: secondaryForeground,
		Face: truetype.NewFace(m.font, &truetype.Options{
			Size:    minsFontSize,
			DPI:     dpi,
			Hinting: font.HintingNone,
		}),
	}
	d.Dot = position
	d.DrawString("min")
}
开发者ID:jbowens,项目名称:muni-display,代码行数:13,代码来源:render.go

示例14: TestReaderPanicNotSorted

func TestReaderPanicNotSorted(t *testing.T) {
	f := truetype.NewFace(clearSans, nil)
	defer f.Close()
	defer func() { recover() }()

	NewReader(nil,
		Style{Offset: 0, Face: f, Foreground: image.Black, Background: image.White},
		Style{Offset: 2, Face: f, Foreground: image.White},
		Style{Offset: 1, Face: f, Foreground: image.Black},
	)

	t.Error("expected panic: styles must be in the right order")
}
开发者ID:achille-roussel,项目名称:go-vu,代码行数:13,代码来源:char_test.go

示例15: TestReaderPanicBadBackground

func TestReaderPanicBadBackground(t *testing.T) {
	f := truetype.NewFace(clearSans, nil)
	defer f.Close()
	defer func() { recover() }()

	NewReader(nil, Style{
		Offset:     0,
		Face:       f,
		Foreground: image.Black,
		Background: nil,
	})

	t.Error("expected panic: no color was given")
}
开发者ID:achille-roussel,项目名称:go-vu,代码行数:14,代码来源:char_test.go


注:本文中的github.com/golang/freetype/truetype.NewFace函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。