Made the OS X code and the example program use the new layout stuff.

This commit is contained in:
Pietro Gagliardi 2017-02-11 19:47:20 -05:00
parent 491ec3ae49
commit 8a64a1dfb0
4 changed files with 22 additions and 16 deletions

View File

@ -155,7 +155,7 @@ static uiDrawTextLayoutLineMetrics *computeLineMetrics(CTFrameRef frame, CGSize
return metrics;
}
uiDrawTextLayout *uiDrawNewTextLayout(uiAttributedString *s, uiDrawFontDescriptor *defaultFont, double width)
uiDrawTextLayout *uiDrawNewTextLayout(uiDrawTextLayoutParams *p)
{
uiDrawTextLayout *tl;
CGFloat cgwidth;
@ -163,10 +163,10 @@ uiDrawTextLayout *uiDrawNewTextLayout(uiAttributedString *s, uiDrawFontDescripto
CGRect rect;
tl = uiNew(uiDrawTextLayout);
tl->attrstr = attrstrToCoreFoundation(s, defaultFont);
tl->attrstr = attrstrToCoreFoundation(p->String, p->DefaultFont);
range.location = 0;
range.length = CFAttributedStringGetLength(tl->attrstr);
tl->width = width;
tl->width = p->Width;
// TODO CTFrameProgression for RTL/LTR
// TODO kCTParagraphStyleSpecifierMaximumLineSpacing, kCTParagraphStyleSpecifierMinimumLineSpacing, kCTParagraphStyleSpecifierLineSpacingAdjustment for line spacing
@ -175,7 +175,7 @@ uiDrawTextLayout *uiDrawNewTextLayout(uiAttributedString *s, uiDrawFontDescripto
// TODO
}
cgwidth = (CGFloat) width;
cgwidth = (CGFloat) (tl->width);
if (cgwidth < 0)
cgwidth = CGFLOAT_MAX;
// TODO these seem to be floor()'d or truncated?
@ -204,8 +204,8 @@ uiDrawTextLayout *uiDrawNewTextLayout(uiAttributedString *s, uiDrawFontDescripto
tl->lineMetrics = computeLineMetrics(tl->frame, tl->size);
// and finally copy the UTF-8/UTF-16 conversion tables
tl->u8tou16 = attrstrCopyUTF8ToUTF16(s, &(tl->nUTF8));
tl->u16tou8 = attrstrCopyUTF16ToUTF8(s, &(tl->nUTF16));
tl->u8tou16 = attrstrCopyUTF8ToUTF16(p->String, &(tl->nUTF8));
tl->u16tou8 = attrstrCopyUTF16ToUTF8(p->String, &(tl->nUTF16));
return tl;
}

View File

@ -18,7 +18,7 @@ static const char *text =
"and important."
"";
static char fontFamily[] = "Palatino";
// TODO should be const; look at constructor function
// TODO should be const; look at constructor function?
static uiDrawFontDescriptor defaultFont = {
.Family = fontFamily,
.Size = 18,
@ -27,6 +27,7 @@ static uiDrawFontDescriptor defaultFont = {
.Stretch = uiDrawTextStretchNormal,
};
static uiAttributedString *attrstr;
static uiDrawTextLayoutParams params;
#define margins 10
@ -141,9 +142,8 @@ static void draw(uiAreaDrawParams *p)
uiDrawFreePath(path);
#endif
layout = uiDrawNewTextLayout(attrstr,
&defaultFont,
p->AreaWidth - 2 * margins);
params.Width = p->AreaWidth - 2 * margins;
layout = uiDrawNewTextLayout(&params);
uiDrawText(p->Context, layout, margins, margins);
uiDrawRestore(p->Context);
@ -257,6 +257,9 @@ struct example *mkBasicExample(void)
basicExample.key = NULL;
attrstr = uiNewAttributedString(text);
params.String = attrstr;
params.DefaultFont = &defaultFont;
params.Align = uiDrawTextLayoutAlignLeft;
return &basicExample;
}

View File

@ -27,6 +27,7 @@ static uiDrawFontDescriptor defaultFont = {
.Stretch = uiDrawTextStretchNormal,
};
static uiAttributedString *attrstr;
static uiDrawTextLayoutParams params;
#define margins 10
@ -88,9 +89,8 @@ static void draw(uiAreaDrawParams *p)
uiDrawClip(p->Context, path);
uiDrawFreePath(path);
layout = uiDrawNewTextLayout(attrstr,
&defaultFont,
p->AreaWidth - 2 * margins);
params.Width = p->AreaWidth - 2 * margins;
layout = uiDrawNewTextLayout(&params);
uiDrawText(p->Context, layout, margins, margins);
uiDrawRestore(p->Context);
@ -130,9 +130,8 @@ static void mouse(uiAreaMouseEvent *e)
if (e->Down != 1)
return;
layout = uiDrawNewTextLayout(attrstr,
&defaultFont,
e->AreaWidth - 2 * margins);
params.Width = e->AreaWidth - 2 * margins;
layout = uiDrawNewTextLayout(&params);
uiDrawTextLayoutHitTest(layout,
e->X - margins, e->Y - margins,
&caretPos, &caretLine);
@ -237,6 +236,9 @@ struct example *mkHitTestExample(void)
hitTestExample.key = key;
attrstr = uiNewAttributedString(text);
params.String = attrstr;
params.DefaultFont = &defaultFont;
params.Align = uiDrawTextLayoutAlignLeft;
return &hitTestExample;
}

View File

@ -89,6 +89,7 @@ typedef struct uiDrawTextLayout uiDrawTextLayout;
typedef struct uiDrawTextLayoutParams uiDrawTextLayoutParams;
typedef struct uiDrawTextLayoutLineMetrics uiDrawTextLayoutLineMetrics;
// TODO drop the Layout from this?
_UI_ENUM(uiDrawTextLayoutAlign) {
uiDrawTextLayoutAlignLeft,
uiDrawTextLayoutAlignCenter,