Converted AttributedString.

This commit is contained in:
Pietro Gagliardi 2018-08-19 12:05:52 -04:00
parent 89353fffd3
commit e8dd3305ef
1 changed files with 64 additions and 74 deletions

View File

@ -343,10 +343,8 @@ func attributeFromLibui(a *C.uiAttribute) Attribute {
panic("unreachable") panic("unreachable")
} }
///////// TODOTODO // AttributedString represents a string of UTF-8 text that can
// optionally be embellished with formatting attributes. Package ui
// uiAttributedString represents a string of UTF-8 text that can
// optionally be embellished with formatting attributes. libui
// provides the list of formatting attributes, which cover common // provides the list of formatting attributes, which cover common
// formatting traits like boldface and color as well as advanced // formatting traits like boldface and color as well as advanced
// typographical features provided by OpenType like superscripts // typographical features provided by OpenType like superscripts
@ -360,97 +358,89 @@ func attributeFromLibui(a *C.uiAttribute) Attribute {
// do not split each other apart, but different values of the same // do not split each other apart, but different values of the same
// attribute type do. // attribute type do.
// //
// The empty string can also be represented by uiAttributedString, // The empty string can also be represented by AttributedString,
// but because of the no-zero-length-attribute rule, it will not have // but because of the no-zero-length-attribute rule, it will not have
// attributes. // attributes.
// //
// A uiAttributedString takes ownership of all attributes given to // Unlike Go strings, AttributedStrings are mutable.
// it, as it may need to duplicate or delete uiAttribute objects at
// any time. By extension, when you free a uiAttributedString,
// all uiAttributes within will also be freed. Each method will
// describe its own rules in more details.
// //
// In addition, uiAttributedString provides facilities for moving // AttributedString allocates resources within libui, which package
// ui sits on top of. As such, when you are finished with an
// AttributedString, you must free it with Free. Like other things in
// package ui, AttributedString must only be used from the main
// goroutine.
//
// In addition, AttributedString provides facilities for moving
// between grapheme clusters, which represent a character // between grapheme clusters, which represent a character
// from the point of view of the end user. The cursor of a text editor // from the point of view of the end user. The cursor of a text editor
// is always placed on a grapheme boundary, so you can use these // is always placed on a grapheme boundary, so you can use these
// features to move the cursor left or right by one "character". // features to move the cursor left or right by one "character".
// TODO does uiAttributedString itself need this // TODO does uiAttributedString itself need this
// //
// uiAttributedString does not provide enough information to be able // AttributedString does not provide enough information to be able
// to draw itself onto a uiDrawContext or respond to user actions. // to draw itself onto a DrawContext or respond to user actions.
// In order to do that, you'll need to use a uiDrawTextLayout, which // In order to do that, you'll need to use a DrawTextLayout, which
// is built from the combination of a uiAttributedString and a set of // is built from the combination of an AttributedString and a set of
// layout-specific properties. // layout-specific properties.
typedef struct uiAttributedString uiAttributedString; type AttributedString struct {
s *C.uiAttributedString
}
// uiAttributedStringForEachAttributeFunc is the type of the function // NewAttributedString creates a new AttributedString from
// invoked by uiAttributedStringForEachAttribute() for every
// attribute in s. Refer to that function's documentation for more
// details.
typedef uiForEach (*uiAttributedStringForEachAttributeFunc)(const uiAttributedString *s, const uiAttribute *a, size_t start, size_t end, void *data);
// @role uiAttributedString constructor
// uiNewAttributedString() creates a new uiAttributedString from
// initialString. The string will be entirely unattributed. // initialString. The string will be entirely unattributed.
_UI_EXTERN uiAttributedString *uiNewAttributedString(const char *initialString); func NewAttributedString(initialString string) *AttributedString {
cs := C.CString(initialString)
defer freestr(cs)
return &AttributedString{
s: C.uiNewAttributedString(cs),
}
}
// @role uiAttributedString destructor // Free destroys s.
// uiFreeAttributedString() destroys the uiAttributedString s. func (s *AttributedString) Free() {
// It will also free all uiAttributes within. C.uiFreeAttributedString(s.s)
_UI_EXTERN void uiFreeAttributedString(uiAttributedString *s); }
// uiAttributedStringString() returns the textual content of s as a // String returns the textual content of s.
// '\0'-terminated UTF-8 string. The returned pointer is valid until func (s *AttributedString) String() string {
// the next change to the textual content of s. return C.GoString(C.uiAttributedStringString(s.s))
_UI_EXTERN const char *uiAttributedStringString(const uiAttributedString *s); }
// uiAttributedStringLength() returns the number of UTF-8 bytes in // AppendUnattributed adds str to the end of s. The new substring
// the textual content of s, excluding the terminating '\0'. // will be unattributed.
_UI_EXTERN size_t uiAttributedStringLen(const uiAttributedString *s); func (s *AttributedString) AppendUnattributed(str string) {
cs := C.CString(str)
defer freestr(cs)
C.uiAttributedStringAppendUnattributed(s.s, cs)
}
// uiAttributedStringAppendUnattributed() adds the '\0'-terminated // InsertAtUnattributed adds str to s at the byte position specified by
// UTF-8 string str to the end of s. The new substring will be // at. The new substring will be unattributed; existing attributes will
// unattributed. // be moved along with their text.
_UI_EXTERN void uiAttributedStringAppendUnattributed(uiAttributedString *s, const char *str); func (s *AttributedString) InsertAtUnattributed(str string, at int) {
cs := C.CString(str)
defer freestr(cs)
C.uiAttributedStringInsertAtUnattributed(s.s, cs, C.size_t(at))
}
// uiAttributedStringInsertAtUnattributed() adds the '\0'-terminated // Delete deletes the characters and attributes of s in the byte range
// UTF-8 string str to s at the byte position specified by at. The new // [start, end).
// substring will be unattributed; existing attributes will be moved func (s *AttributedString) Delete(start, end int) {
// along with their text. C.uiAttributedStringDelete(s.s, C.size_t(start), C.size_t(end))
_UI_EXTERN void uiAttributedStringInsertAtUnattributed(uiAttributedString *s, const char *str, size_t at); }
// TODO add the Append and InsertAtExtendingAttributes functions // SetAttribute sets a in the byte range [start, end) of s. Any existing
// TODO and add functions that take a string + length // attributes in that byte range of the same type are removed.
func (s *AttributedString) SetAttribute(a Attribute, start, end int) {
C.uiAttributedStringSetAttribute(s.s, a.toLibui(), C.size_t(start), C.size_t(end))
}
// uiAttributedStringDelete() deletes the characters and attributes of // TODO uiAttributedStringForEachAttribute
// s in the byte range [start, end). // TODO uiAttributedStringNumGraphemes
_UI_EXTERN void uiAttributedStringDelete(uiAttributedString *s, size_t start, size_t end); // TODO uiAttributedStringByteIndexToGrapheme
// TODO uiAttributedStringGraphemeToByteIndex
// TODO add a function to uiAttributedString to get an attribute's value at a specific index or in a specific range, so we can edit //////// TODOTODO
// uiAttributedStringSetAttribute() sets a in the byte range [start, end)
// of s. Any existing attributes in that byte range of the same type are
// removed. s takes ownership of a; you should not use it after
// uiAttributedStringSetAttribute() returns.
_UI_EXTERN void uiAttributedStringSetAttribute(uiAttributedString *s, uiAttribute *a, size_t start, size_t end);
// uiAttributedStringForEachAttribute() enumerates all the
// uiAttributes in s. It is an error to modify s in f. Within f, s still
// owns the attribute; you can neither free it nor save it for later
// use.
// TODO reword the above for consistency (TODO and find out what I meant by that)
// TODO define an enumeration order (or mark it as undefined); also define how consecutive runs of identical attributes are handled here and sync with the definition of uiAttributedString itself
_UI_EXTERN void uiAttributedStringForEachAttribute(const uiAttributedString *s, uiAttributedStringForEachAttributeFunc f, void *data);
// TODO const correct this somehow (the implementation needs to mutate the structure)
_UI_EXTERN size_t uiAttributedStringNumGraphemes(uiAttributedString *s);
// TODO const correct this somehow (the implementation needs to mutate the structure)
_UI_EXTERN size_t uiAttributedStringByteIndexToGrapheme(uiAttributedString *s, size_t pos);
// TODO const correct this somehow (the implementation needs to mutate the structure)
_UI_EXTERN size_t uiAttributedStringGraphemeToByteIndex(uiAttributedString *s, size_t pos);
// uiFontDescriptor provides a complete description of a font where // uiFontDescriptor provides a complete description of a font where
// one is needed. Currently, this means as the default font of a // one is needed. Currently, this means as the default font of a