本文整理匯總了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")
}
}
示例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
}
示例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()}
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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,
}),
}
}
示例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)
}
}
示例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,
}
}
示例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)
}
}
示例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)
}
}
示例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")
}
示例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")
}
示例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")
}