Пример #1
0
// FromFont initializes ff from the given font. If it is called on an already
// initialized font face, changes the font face according to the given font.
// Returns an error if the initialization fails.
func (ff *FontFace) FromFont(f font.Font) error {
	data, err := f.Contents()
	if err != nil {
		return err
	}
	mimeType := f.MimeType()
	if mimeType == "" {
		// Should not happen.
		return fmt.Errorf("can't determine font MIME type")
	}
	ff.Base64Data = util.Base64(data)
	ff.Family = f.Family
	ff.Format = f.Format.String()
	ff.MimeType = mimeType
	ff.Style = f.Style
	ff.Weight = f.Weight
	return nil
}
Пример #2
0
func TestFontFaceFromFont(t *testing.T) {
	ff := &FontFace{}
	f := font.Font{
		Family: "Amaranth",
		Format: font.WOFF,
		Path:   filepath.Join(amf, "amaranth-regular.woff"),
		Style:  "normal",
		Weight: 400,
	}
	err := ff.FromFont(f)
	test.VerifyFatal(t, 1, 0, true, nil == err)
	data, err := f.Contents()
	test.Verify(t, 2, 0, util.Base64(data), ff.Base64Data)
	test.Verify(t, 3, 0, f.Family, ff.Family)
	test.Verify(t, 4, 0, f.Format.String(), ff.Format)
	wMimeType := f.MimeType()
	test.VerifyFatal(t, 5, 0, false, "" == wMimeType)
	test.Verify(t, 6, 0, wMimeType, ff.MimeType)
	test.Verify(t, 7, 0, f.Style, ff.Style)
	test.Verify(t, 8, 0, f.Weight, ff.Weight)
}