And migrated the rest of drawtext.go. Phew!

This commit is contained in:
Pietro Gagliardi 2018-08-19 12:20:59 -04:00
parent e8dd3305ef
commit 6739b438a8
1 changed files with 90 additions and 60 deletions

View File

@ -29,6 +29,22 @@ package ui
// { // {
// free(c); // free(c);
// } // }
// static inline uiFontDescriptor *pkguiNewFontDescriptor(void)
// {
// return (uiFontDescriptor *) pkguiAlloc(sizeof (uiFontDescriptor));
// }
// static inline void pkguiFreeFontDescriptor(uiFontDescriptor *fd)
// {
// free(fd);
// }
// static inline uiDrawTextLayoutParams *pkguiNewDrawTextLayoutParams(void)
// {
// return (uiDrawTextLayoutParams *) pkguiAlloc(sizeof (uiDrawTextLayoutParams));
// }
// static inline void pkguiFreeDrawTextLayoutParams(uiDrawTextLayoutParams *fd)
// {
// free(fd);
// }
import "C" import "C"
// Attribute stores information about an attribute in an // Attribute stores information about an attribute in an
@ -440,85 +456,99 @@ func (s *AttributedString) SetAttribute(a Attribute, start, end int) {
// TODO uiAttributedStringByteIndexToGrapheme // TODO uiAttributedStringByteIndexToGrapheme
// TODO uiAttributedStringGraphemeToByteIndex // TODO uiAttributedStringGraphemeToByteIndex
//////// TODOTODO // FontDescriptor 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
// uiDrawTextLayout and as the data returned by uiFontButton. // DrawTextLayout and as the data returned by FontButton.
// All the members operate like the respective uiAttributes. type FontDescriptor struct {
typedef struct uiFontDescriptor uiFontDescriptor; Family TextFamily
Size TextSize
Weight TextWeight
Italic TextItalic
Stretch TextStretch
}
struct uiFontDescriptor { func (d *FontDescriptor) fromLibui(fd *C.uiFontDescriptor) {
// TODO const-correct this or figure out how to deal with this when getting a value d.Family = TextFamily(C.GoString(fd.Family))
char *Family; d.Size = TextSize(fd.Size)
double Size; d.Weight = TextWeight(fd.Weight)
uiTextWeight Weight; d.Italic = TextItalic(fd.Italic)
uiTextItalic Italic; d.Stretch = TextStretch(fd.Stretch)
uiTextStretch Stretch; }
};
// uiDrawTextLayout is a concrete representation of a func (d *FontDescriptor) toLibui() *C.uiFontDescriptor {
// uiAttributedString that can be displayed in a uiDrawContext. fd := C.pkguiNewFontDescriptor()
fd.Family = C.CString(d.Family)
fd.Size = C.double(d.Size)
fd.Weight = C.uiTextWeight(d.Weight)
fd.Italic = C.uiTextItalic(d.Italic)
fd.Stretch = C.uiTextStretch(d.Stretch)
return fd
}
func freeLibuiFontDescriptor(fd *C.uiFontDescriptor) {
freestr(fd.Family)
C.pkguiFreeFontDescriptor(fd)
}
// DrawTextLayout is a concrete representation of an
// AttributedString that can be displayed in a DrawContext.
// It includes information important for the drawing of a block of // It includes information important for the drawing of a block of
// text, including the bounding box to wrap the text within, the // text, including the bounding box to wrap the text within, the
// alignment of lines of text within that box, areas to mark as // alignment of lines of text within that box, areas to mark as
// being selected, and other things. // being selected, and other things.
// //
// Unlike uiAttributedString, the content of a uiDrawTextLayout is // Unlike AttributedString, the content of a DrawTextLayout is
// immutable once it has been created. // immutable once it has been created.
// //
// TODO talk about OS-specific differences with text drawing that libui can't account for... // TODO talk about OS-specific differences with text drawing that libui can't account for...
typedef struct uiDrawTextLayout uiDrawTextLayout; type DrawTextLayout struct {
tl *C.uiDrawTextLayout
}
// uiDrawTextAlign specifies the alignment of lines of text in a // DrawTextAlign specifies the alignment of lines of text in a
// uiDrawTextLayout. // DrawTextLayout.
// TODO should this really have Draw in the name? // TODO should this really have Draw in the name?
_UI_ENUM(uiDrawTextAlign) { type DrawTextAlign int
uiDrawTextAlignLeft, const (
uiDrawTextAlignCenter, DrawTextAlignLeft DrawTextAlign = iota
uiDrawTextAlignRight, DrawTextAlignCenter
}; DrawTextAlignRight
)
// uiDrawTextLayoutParams describes a uiDrawTextLayout. // DrawTextLayoutParams describes a DrawTextLayout.
// DefaultFont is used to render any text that is not attributed // DefaultFont is used to render any text that is not attributed
// sufficiently in String. Width determines the width of the bounding // sufficiently in String. Width determines the width of the bounding
// box of the text; the height is determined automatically. // box of the text; the height is determined automatically.
typedef struct uiDrawTextLayoutParams uiDrawTextLayoutParams; type DrawTextLayoutParams struct {
String *AttributedString
DefaultFont *FontDescriptor
Width float64
Align DrawTextAlign
}
// TODO const-correct this somehow // DrawNewTextLayout() creates a new DrawTextLayout from
struct uiDrawTextLayoutParams {
uiAttributedString *String;
uiFontDescriptor *DefaultFont;
double Width;
uiDrawTextAlign Align;
};
// @role uiDrawTextLayout constructor
// uiDrawNewTextLayout() creates a new uiDrawTextLayout from
// the given parameters. // the given parameters.
// func DrawNewTextLayout(p *DrawTextLayoutParams) *DrawTextLayout {
// TODO dp := C.pkguiNewDrawTextLayoutParams()
// - allow creating a layout out of a substring defer C.pkguiFreeDrawTextLayoutParams(dp)
// - allow marking compositon strings dp.String = p.String.s
// - allow marking selections, even after creation dp.DefaultFont = p.DefaultFont.toLibui()
// - add the following functions: defer freeLibuiFontDescriptor(dp.DefaultFont)
// - uiDrawTextLayoutHeightForWidth() (returns the height that a layout would need to be to display the entire string at a given width) dp.Width = C.double(p.Width)
// - uiDrawTextLayoutRangeForSize() (returns what substring would fit in a given size) dp.Align = C.uiDrawTextAlign(p.Align)
// - uiDrawTextLayoutNewWithHeight() (limits amount of string used by the height) return DrawTextLayout{
// - some function to fix up a range (for text editing) tl: C.uiDrawNewTextLayout(dp),
_UI_EXTERN uiDrawTextLayout *uiDrawNewTextLayout(uiDrawTextLayoutParams *params); }
}
// @role uiDrawFreeTextLayout destructor // Free frees tl. The underlying AttributedString is not freed.
// uiDrawFreeTextLayout() frees tl. The underlying func (tl *DrawTextLayout) Free() {
// uiAttributedString is not freed. C.uiDrawFreeTextLayout(tl.tl)
_UI_EXTERN void uiDrawFreeTextLayout(uiDrawTextLayout *tl); }
// uiDrawText() draws tl in c with the top-left point of tl at (x, y). // Text draws tl in c with the top-left point of tl at (x, y).
_UI_EXTERN void uiDrawText(uiDrawContext *c, uiDrawTextLayout *tl, double x, double y); func (c *DrawContext) Text(tl *DrawTextLayout, x, y float64) {
C.uiDrawText(c.c, tl.tl, C.double(x), C.double(y))
}
// uiDrawTextLayoutExtents() returns the width and height of tl // TODO uiDrawTextLayoutExtents
// in width and height. The returned width may be smaller than
// the width passed into uiDrawNewTextLayout() depending on
// how the text in tl is wrapped. Therefore, you can use this
// function to get the actual size of the text layout.
_UI_EXTERN void uiDrawTextLayoutExtents(uiDrawTextLayout *tl, double *width, double *height);