Took a different, more Go-like approach to attributes.

This commit is contained in:
Pietro Gagliardi 2018-08-16 11:01:45 -04:00
parent 75e67484a3
commit 9df762b10a
1 changed files with 48 additions and 122 deletions

View File

@ -8,90 +8,49 @@ import "C"
// Attribute stores information about an attribute in an // Attribute stores information about an attribute in an
// AttributedString. // AttributedString.
// //
// You do not create Attributes directly; instead, you create an // The following types can be used as Attributes:
// Attribute of a given type using the specialized constructor
// functions. For every Unicode codepoint in the AttributedString,
// at most one value of each attribute type can be applied.
// //
// Attributes are immutable and the AttributedString takes // - TextFamily
// ownership of the Attribute object once assigned. Therefore, // - TextSize
// you can create attributes, give them to AttributedStrings, // - TextWeight
// and then forget about them, like with other garbage-collected // - TextItalic
// objects in Go. However, Attributes are still objects managed by // - TextStretch
// libui underneath package ui, so if you created an Attribute without // - TextColor
// adding it to an AttributedString, you must call Free on that // - TextBackgroundColor
// Attribute to avoid leaking resources. // - TextUnderline
type Attribute struct { // - TextUnderlineColor
a *C.uiAttribute // - OpenTypeFeatures
//
// For every Unicode codepoint in the AttributedString, at most one
// value of each attribute type can be applied.
type Attribute interface {
toLibui() *C.uiAttribute
} }
// Free frees the Attribute. You almost never need to call this. // TextFamily is an Attribute that changes the font family of the text
// For the specific scenario where this is needed, refer to the // it is applied to. Font family names are case-insensitive.
// top of the Attribute documentation. type TextFamily string
func (a *Attribute) Free() {
C.uiFreeAttribute(a.a)
}
// uiAttributeType holds the possible uiAttribute types that may be func (f TextFamily) toLibui() *C.uiAttribute {
// returned by Attribute.GetType. Refer to the documentation for fstr := C.CString(string(f))
// each type's constructor function for details on each type.
type AttributeType int
const (
AttributeTypeFamily AttributeType = iota
AttributeTypeSize
AttributeTypeWeight
AttributeTypeItalic
AttributeTypeStretch
AttributeTypeColor
AttributeTypeBackground
AttributeTypeUnderline
AttributeTypeUnderlineColor
AttributeTypeFeatures
)
// Type returns the type of a.
func (a *Attribute) Type() AttributeType {
return AttributeType(C.uiAttributeGetType(a.a))
}
// NewFamilyAttribute creates a new Attribute that changes the
// font family of the text it is applied to. Font family names are
// case-insensitive.
func NewFamilyAttribute(family string) *Attribute {
fstr := C.CString(family)
defer freestr(fstr) defer freestr(fstr)
return &Attribute{ return C.uiNewFamilyAttribute(fstr)
a: C.uiNewFamilyAttribute(fstr),
}
} }
// Family returns the font family stored in a. // TextSize is an Attribute that changes the size of the text it is
// It is an error to call this on an Attribute that does not hold a // applied to, in typographical points.
// font family. type TextSize float64
func (a *Attribute) Family() string {
return C.GoString(C.uiAttributeFamily(a.a)) func (s TextSize) toLibui() *C.uiAttribute {
return C.uiNewSizeAttribute(C.double(size))
} }
// NewSizeAttribute() creates a new Attribute that changes the // TextWeight is an Attribute that changes the weight of the text
// size of the text it is applied to, in typographical points. // it is applied to. These roughly map to the OS/2 text weight field
func NewSizeAttribute(size float64) *Attribute { // of TrueType and OpenType fonts, or to CSS weight numbers. The
return &Attribute{ // named constants are nominal values; the actual values may vary
a: C.uiNewSizeAttribute(C.double(size)), // by font and by OS, though this isn't particularly likely. Any value
} // between TextWeightMinimum and TextWeightMaximum,
}
// Size returns the font size stored in a. It is an error to
// call this on a Attribute that does not hold a font size.
func (a *Attribute) Size() float64 {
return float64(C.uiAttributeSize(a.a))
}
// TextWeight represents possible text weights. These roughly
// map to the OS/2 text weight field of TrueType and OpenType
// fonts, or to CSS weight numbers. The named constants are
// nominal values; the actual values may vary by font and by OS,
// though this isn't particularly likely. Any value between
// TextWeightMinimum and TextWeightMaximum,
// inclusive, is allowed. // inclusive, is allowed.
// //
// Note that due to restrictions in early versions of Windows, some // Note that due to restrictions in early versions of Windows, some
@ -118,27 +77,15 @@ const (
TextWeightMaximum = 1000, TextWeightMaximum = 1000,
) )
// NewWeightAttribute creates a new Attribute that changes the func (w TextWeight) toLibui() *C.uiAttribute {
// weight of the text it is applied to. It is an error to specify a weight return C.uiNewWeightAttribute(C.uiTextWeight(w))
// outside the range [TextWeightMinimum,
// TextWeightMaximum].
func NewWeightAttribute(weight TextWeight) *Attribute {
return &Attribute{
a: C.uiNewWeightAttribute(C.uiTextWeight(weight)),
}
} }
// Weight returns the font weight stored in a. It is an error // TextItalic is an Attribute that changes the italic mode of the text
// to call this on a uiAttribute that does not hold a font weight. // it is applied to. Italic represents "true" italics where the slanted
func (a *Attribute) Weight() TextWeight { // glyphs have custom shapes, whereas oblique represents italics
return TextWeight(C.uiAttributeWeight(a.a)) // that are merely slanted versions of the normal glyphs. Most fonts
} // usually have one or the other.
// TextItalic represents possible italic modes for a font. Italic
// represents "true" italics where the slanted glyphs have custom
// shapes, whereas oblique represents italics that are merely slanted
// versions of the normal glyphs. Most fonts usually have one or the
// other.
type TextItalic int type TextItalic int
const ( const (
TextItalicNormal TextItalic = iota TextItalicNormal TextItalic = iota
@ -146,24 +93,14 @@ const (
TextItalicItalic TextItalicItalic
) )
// NewItalicAttribute creates a new Attribute that changes the func (i TextItalic) toLibui() *C.uiAttribute {
// italic mode of the text it is applied to. It is an error to specify an return C.uiNewItalicAttribute(C.uiTextItalic(i))
// italic mode not specified in TextItalic.
func NewItalicAttribute(italic TextItalic) *Attribute {
return &Attribute{
a: C.uiNewItalicAttribute(C.uiTextItalic(italic)),
}
} }
// Italic returns the font italic mode stored in a. It is an ///// TODOTODO
// error to call this on an Attribute that does not hold a font italic
// mode.
func (a *Attribute) Italic() TextItalic {
return TextItalic(C.uiAttributeItalic(a.a))
}
// TextStretch represents possible stretches (also called "widths") // TextStretch is an Attribute that changes the stretch (also called
// of a font. // "width") of the text it is applied to.
// //
// Note that due to restrictions in early versions of Windows, some // Note that due to restrictions in early versions of Windows, some
// fonts have "special" stretches be exposed in many programs as // fonts have "special" stretches be exposed in many programs as
@ -185,19 +122,8 @@ const (
TextStretchUltraExpanded TextStretchUltraExpanded
) )
// NewStretchAttribute creates a new Attribute that changes the func (s TextStretch) toLibui() *C.uiAttribute {
// stretch of the text it is applied to. It is an error to specify a strech return C.uiNewStretchAttribute(C.uiTextStretch(s))
// not specified in TextStretch.
func NewStretchAttribute(stretch TextStretch) *Attribute {
return &Attribute{
a: C.uiNewStretchAttribute(C.uiTextStretch(stretch)),
}
}
// Stretch returns the font stretch stored in a. It is an
// error to call this on an Attribute that does not hold a font stretch.
func (a *Attribute) Stretch() TextStretch {
return TextStretch(C.uiAttributeStretch(a.a))
} }
/////// TODOTODO /////// TODOTODO