2017-01-02 22:53:31 -06:00
|
|
|
// 2 january 2017
|
2016-01-12 00:12:35 -06:00
|
|
|
#import "uipriv_darwin.h"
|
2017-01-06 22:53:23 -06:00
|
|
|
#import "draw.h"
|
2016-01-12 00:12:35 -06:00
|
|
|
|
2017-01-17 11:02:42 -06:00
|
|
|
// TODO what happens if nLines == 0 in any function?
|
|
|
|
|
2017-01-02 22:53:31 -06:00
|
|
|
struct uiDrawTextLayout {
|
|
|
|
CFAttributedStringRef attrstr;
|
2017-01-17 11:02:42 -06:00
|
|
|
|
|
|
|
// the width as passed into uiDrawTextLayout constructors
|
2017-01-02 22:53:31 -06:00
|
|
|
double width;
|
2017-01-17 11:02:42 -06:00
|
|
|
|
2017-01-03 22:59:23 -06:00
|
|
|
CTFramesetterRef framesetter;
|
2017-01-17 11:02:42 -06:00
|
|
|
|
|
|
|
// the *actual* size of the frame
|
2017-01-05 20:36:07 -06:00
|
|
|
// note: technically, metrics returned from frame are relative to CGPathGetPathBoundingBox(tl->path)
|
|
|
|
// however, from what I can gather, for a path created by CGPathCreateWithRect(), like we do (with a NULL transform), CGPathGetPathBoundingBox() seems to just return the standardized form of the rect used to create the path
|
|
|
|
// (this I confirmed through experimentation)
|
|
|
|
// so we can just use tl->size for adjustments
|
|
|
|
// we don't need to adjust coordinates by any origin since our rect origin is (0, 0)
|
2017-01-03 22:59:23 -06:00
|
|
|
CGSize size;
|
2017-01-17 11:02:42 -06:00
|
|
|
|
2017-01-03 22:59:23 -06:00
|
|
|
CGPathRef path;
|
2017-01-04 22:50:08 -06:00
|
|
|
CTFrameRef frame;
|
2017-01-17 11:02:42 -06:00
|
|
|
|
2017-01-04 22:50:08 -06:00
|
|
|
CFArrayRef lines;
|
2017-01-17 11:02:42 -06:00
|
|
|
CFIndex nLines;
|
|
|
|
// we compute this once when first creating the layout
|
|
|
|
uiDrawTextLayoutLineMetrics *lineMetrics;
|
|
|
|
|
|
|
|
// for converting CFAttributedString indices to byte offsets
|
2017-01-07 19:09:44 -06:00
|
|
|
size_t *u16tou8;
|
|
|
|
size_t nu16tou8; // TODO I don't like the casing of this name
|
2016-01-12 00:12:35 -06:00
|
|
|
};
|
|
|
|
|
2017-01-03 11:18:17 -06:00
|
|
|
static CTFontRef fontdescToCTFont(uiDrawFontDescriptor *fd)
|
|
|
|
{
|
|
|
|
CTFontDescriptorRef desc;
|
|
|
|
CTFontRef font;
|
|
|
|
|
2017-01-03 22:59:23 -06:00
|
|
|
desc = fontdescToCTFontDescriptor(fd);
|
|
|
|
font = CTFontCreateWithFontDescriptor(desc, fd->Size, NULL);
|
2017-01-03 11:18:17 -06:00
|
|
|
CFRelease(desc); // TODO correct?
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CFAttributedStringRef attrstrToCoreFoundation(uiAttributedString *s, uiDrawFontDescriptor *defaultFont)
|
2016-01-12 00:12:35 -06:00
|
|
|
{
|
|
|
|
CFStringRef cfstr;
|
2017-01-03 11:18:17 -06:00
|
|
|
CFMutableDictionaryRef defaultAttrs;
|
|
|
|
CTFontRef defaultCTFont;
|
2017-01-02 22:53:31 -06:00
|
|
|
CFAttributedStringRef base;
|
|
|
|
CFMutableAttributedStringRef mas;
|
2016-01-12 00:12:35 -06:00
|
|
|
|
2017-01-02 22:53:31 -06:00
|
|
|
cfstr = CFStringCreateWithCharacters(NULL, attrstrUTF16(s), attrstrUTF16Len(s));
|
|
|
|
if (cfstr == NULL) {
|
|
|
|
// TODO
|
2016-01-12 00:12:35 -06:00
|
|
|
}
|
2017-01-03 11:18:17 -06:00
|
|
|
defaultAttrs = CFDictionaryCreateMutable(NULL, 1,
|
2017-01-02 22:53:31 -06:00
|
|
|
&kCFCopyStringDictionaryKeyCallBacks,
|
|
|
|
&kCFTypeDictionaryValueCallBacks);
|
|
|
|
if (defaultAttrs == NULL) {
|
|
|
|
// TODO
|
2016-01-12 00:12:35 -06:00
|
|
|
}
|
2017-01-03 11:18:17 -06:00
|
|
|
defaultCTFont = fontdescToCTFont(defaultFont);
|
|
|
|
CFDictionaryAddValue(defaultAttrs, kCTFontAttributeName, defaultCTFont);
|
|
|
|
CFRelease(defaultCTFont);
|
2016-01-12 00:12:35 -06:00
|
|
|
|
2017-01-02 22:53:31 -06:00
|
|
|
base = CFAttributedStringCreate(NULL, cfstr, defaultAttrs);
|
|
|
|
if (base == NULL) {
|
|
|
|
// TODO
|
2016-01-12 00:12:35 -06:00
|
|
|
}
|
2017-01-02 22:53:31 -06:00
|
|
|
CFRelease(cfstr);
|
|
|
|
CFRelease(defaultAttrs);
|
|
|
|
mas = CFAttributedStringCreateMutableCopy(NULL, 0, base);
|
|
|
|
CFRelease(base);
|
2016-01-12 00:46:28 -06:00
|
|
|
|
2017-01-02 22:53:31 -06:00
|
|
|
CFAttributedStringBeginEditing(mas);
|
|
|
|
// TODO copy in the attributes
|
|
|
|
CFAttributedStringEndEditing(mas);
|
2016-01-12 00:12:35 -06:00
|
|
|
|
2017-01-02 22:53:31 -06:00
|
|
|
return mas;
|
2016-01-12 00:46:28 -06:00
|
|
|
}
|
|
|
|
|
2017-01-17 11:02:42 -06:00
|
|
|
static uiDrawTextLayoutLineMetrics *computeLineMetrics(CTFrame frame, CGSize size)
|
|
|
|
{
|
|
|
|
uiDrawTextLayoutLineMetrics *metrics;
|
|
|
|
CFArray lines;
|
|
|
|
CTLineRef line;
|
|
|
|
CFIndex i, n;
|
|
|
|
CGFloat ypos;
|
|
|
|
CGRect bounds, boundsNoLeading;
|
|
|
|
CGFloat ascent, descent, leading;
|
|
|
|
CGPoint *origins;
|
|
|
|
|
|
|
|
lines = CTFrameGetLines(frame);
|
|
|
|
n = CFArrayGetCount(lines);
|
|
|
|
metrics = (uiDrawTextLay
|
|
|
|
outLineMetrics *) uiAlloc(n * sizeof (uiDrawTextLayoutLineMetrics), "uiDrawTextLayoutLineMetrics[] (text layout)");
|
|
|
|
|
|
|
|
origins = (CGFloat *) uiAlloc(n * sizeof (CGFloat), "CGFloat[] (text layout)");
|
|
|
|
CTFrameGetLineOrigins(frame, CFRangeMake(0, n), origins);
|
|
|
|
|
|
|
|
ypos = size.height;
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
line = (CTLineRef) CFArrayGetValueAtIndex(lines, i);
|
|
|
|
bounds = CTLineGetBoundsWithOptions(line, 0);
|
|
|
|
boundsNoLeading = CTLineGetBoundsWithOptions(line, kCTLineBoundsExcludeTypographicLeading);
|
|
|
|
|
|
|
|
// this is equivalent to boundsNoLeading.size.height + boundsNoLeading.origin.y (manually verified)
|
|
|
|
ascent = bounds.size.height + bounds.origin.y;
|
|
|
|
descent = -boundsNoLeading.origin.y;
|
|
|
|
// TODO does this preserve leading sign?
|
|
|
|
leading = -bounds.origin.y - descent;
|
|
|
|
|
|
|
|
// Core Text always rounds these up for paragraph style calculations; there is a flag to control it but it's inaccessible (and this behavior is turned off for old versions of iPhoto)
|
|
|
|
ascent = floor(ascent + 0.5);
|
|
|
|
descent = floor(descent + 0.5);
|
|
|
|
if (leading > 0)
|
|
|
|
leading = floor(leading + 0.5);
|
|
|
|
|
|
|
|
metrics[i].X = origins[i].x;
|
|
|
|
metrics[i].Y = origins[i].y - descent - leading;
|
|
|
|
metrics[i].Width = bounds.size.width;
|
|
|
|
metrics[i].Height = ascent + descent + leading;
|
|
|
|
|
|
|
|
metrics[i].BaselineY = origins[i].y;
|
|
|
|
metrics[i].Ascent = ascent;
|
|
|
|
metrics[i].Descent = descent;
|
|
|
|
metrics[i].Leading = leading;
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
metrics[i].ParagraphSpacingBefore = 0;
|
|
|
|
metrics[i].LineHeightSpace = 0;
|
|
|
|
metrics[i].LineSpacing = 0;
|
|
|
|
metrics[i].ParagraphSpacing = 0;
|
|
|
|
|
|
|
|
// and finally advance to the next line
|
|
|
|
ypos += metrics[i].Height;
|
|
|
|
}
|
|
|
|
|
|
|
|
// okay, but now all these metrics are unflipped
|
|
|
|
// we need to flip them
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
metrics[i].Y = size.height - metrics[i].Y;
|
|
|
|
// go from bottom-left corner to top-left
|
|
|
|
metrics[i].Y -= metrics[i].Height;
|
|
|
|
metrics[i].BaselineY = size.height - metrics[i].BaselineY;
|
|
|
|
// TODO also adjust by metrics[i].Height?
|
|
|
|
}
|
|
|
|
|
|
|
|
uiFree(origins);
|
|
|
|
return metrics;
|
|
|
|
}
|
|
|
|
|
2017-01-02 22:53:31 -06:00
|
|
|
uiDrawTextLayout *uiDrawNewTextLayout(uiAttributedString *s, uiDrawFontDescriptor *defaultFont, double width)
|
2016-01-12 00:46:28 -06:00
|
|
|
{
|
2017-01-03 22:59:23 -06:00
|
|
|
uiDrawTextLayout *tl;
|
|
|
|
CGFloat cgwidth;
|
|
|
|
CFRange range, unused;
|
|
|
|
CGRect rect;
|
|
|
|
|
|
|
|
tl = uiNew(uiDrawTextLayout);
|
|
|
|
tl->attrstr = attrstrToCoreFoundation(s, defaultFont);
|
|
|
|
range.location = 0;
|
|
|
|
range.length = CFAttributedStringGetLength(tl->attrstr);
|
|
|
|
tl->width = width;
|
|
|
|
|
|
|
|
// TODO CTFrameProgression for RTL/LTR
|
|
|
|
// TODO kCTParagraphStyleSpecifierMaximumLineSpacing, kCTParagraphStyleSpecifierMinimumLineSpacing, kCTParagraphStyleSpecifierLineSpacingAdjustment for line spacing
|
|
|
|
tl->framesetter = CTFramesetterCreateWithAttributedString(tl->attrstr);
|
|
|
|
if (tl->framesetter == NULL) {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
cgwidth = (CGFloat) width;
|
|
|
|
if (cgwidth < 0)
|
|
|
|
cgwidth = CGFLOAT_MAX;
|
|
|
|
// TODO these seem to be floor()'d or truncated?
|
|
|
|
// TODO double check to make sure this TODO was right
|
|
|
|
tl->size = CTFramesetterSuggestFrameSizeWithConstraints(tl->framesetter,
|
|
|
|
range,
|
|
|
|
// TODO kCTFramePathWidthAttributeName?
|
|
|
|
NULL,
|
|
|
|
CGSizeMake(cgwidth, CGFLOAT_MAX),
|
|
|
|
&unused); // not documented as accepting NULL
|
|
|
|
|
|
|
|
rect.origin = CGZeroPoint;
|
|
|
|
rect.size = tl->size;
|
|
|
|
tl->path = CGPathCreateWithRect(rect, NULL);
|
2017-01-04 22:50:08 -06:00
|
|
|
tl->frame = CTFramesetterCreateFrame(tl->framesetter,
|
|
|
|
range,
|
|
|
|
tl->path,
|
|
|
|
// TODO kCTFramePathWidthAttributeName?
|
|
|
|
NULL);
|
|
|
|
if (tl->frame == NULL) {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
tl->lines = CTFrameGetLines(tl->frame);
|
2017-01-17 11:02:42 -06:00
|
|
|
tl->nLines = CFArrayGetCount(tl->lines);
|
|
|
|
tl->lineMetrics = computeLineMetrics(tl->frame, tl->size);
|
2017-01-04 22:50:08 -06:00
|
|
|
|
2017-01-07 19:09:44 -06:00
|
|
|
// and finally copy the UTF-16 to UTF-8 index conversion table
|
|
|
|
tl->u16tou8 = attrstrCopyUTF16ToUTF8(s, &(tl->nu16tou8));
|
|
|
|
|
2017-01-03 22:59:23 -06:00
|
|
|
return tl;
|
2016-01-12 00:12:35 -06:00
|
|
|
}
|
|
|
|
|
2017-01-02 22:53:31 -06:00
|
|
|
void uiDrawFreeTextLayout(uiDrawTextLayout *tl)
|
2016-01-12 00:12:35 -06:00
|
|
|
{
|
2017-01-07 19:09:44 -06:00
|
|
|
uiFree(tl->u16tou8);
|
2017-01-17 11:02:42 -06:00
|
|
|
uiFree(tl->lineMetrics);
|
2017-01-04 22:50:08 -06:00
|
|
|
// TODO release tl->lines?
|
|
|
|
CFRelease(tl->frame);
|
2017-01-03 22:59:23 -06:00
|
|
|
CFRelease(tl->path);
|
|
|
|
CFRelease(tl->framesetter);
|
2017-01-03 11:18:17 -06:00
|
|
|
CFRelease(tl->attrstr);
|
|
|
|
uiFree(tl);
|
2016-01-12 00:12:35 -06:00
|
|
|
}
|
|
|
|
|
2017-01-06 22:53:23 -06:00
|
|
|
// TODO document that (x,y) is the top-left corner of the *entire frame*
|
2017-01-02 22:53:31 -06:00
|
|
|
void uiDrawText(uiDrawContext *c, uiDrawTextLayout *tl, double x, double y)
|
2016-01-15 19:18:53 -06:00
|
|
|
{
|
2017-01-06 22:53:23 -06:00
|
|
|
CGContextSaveGState(c->c);
|
|
|
|
|
|
|
|
// Core Text doesn't draw onto a flipped view correctly; we have to pretend it was unflipped
|
|
|
|
// see the iOS bits of the first example at https://developer.apple.com/library/mac/documentation/StringsTextFonts/Conceptual/CoreText_Programming/LayoutOperations/LayoutOperations.html#//apple_ref/doc/uid/TP40005533-CH12-SW1 (iOS is naturally flipped)
|
|
|
|
// TODO how is this affected by a non-identity CTM?
|
|
|
|
CGContextTranslateCTM(c->c, 0, cheight);
|
|
|
|
CGContextScaleCTM(c->c, 1.0, -1.0);
|
|
|
|
CGContextSetTextMatrix(c->c, CGAffineTransformIdentity);
|
|
|
|
|
|
|
|
// wait, that's not enough; we need to offset y values to account for our new flipping
|
|
|
|
y = c->height - y;
|
|
|
|
|
|
|
|
// CTFrameDraw() draws in the path we specified when creating the frame
|
|
|
|
// this means that in our usage, CTFrameDraw() will draw at (0,0)
|
|
|
|
// so move the origin to be at (x,y) instead
|
|
|
|
// TODO are the signs correct?
|
|
|
|
CGContextTranslateCTM(c->c, x, y);
|
|
|
|
|
|
|
|
CTFrameDraw(tl->frame, c->c);
|
|
|
|
|
|
|
|
CGContextRestoreGState(c->c);
|
2016-01-15 19:18:53 -06:00
|
|
|
}
|
|
|
|
|
2017-01-05 16:56:47 -06:00
|
|
|
// TODO document that the width and height of a layout is not necessarily the sum of the widths and heights of its constituent lines; this is definitely untrue on OS X, where lines are placed in such a way that the distance between baselines is always integral
|
2017-01-02 22:53:31 -06:00
|
|
|
void uiDrawTextLayoutExtents(uiDrawTextLayout *tl, double *width, double *height)
|
2016-01-16 12:34:22 -06:00
|
|
|
{
|
2017-01-05 16:55:05 -06:00
|
|
|
*width = tl->size.width;
|
|
|
|
*height = tl->size.height;
|
2016-01-16 12:34:22 -06:00
|
|
|
}
|
|
|
|
|
2017-01-02 22:53:31 -06:00
|
|
|
int uiDrawTextLayoutNumLines(uiDrawTextLayout *tl)
|
2016-01-16 12:34:22 -06:00
|
|
|
{
|
2017-01-17 11:02:42 -06:00
|
|
|
return tl->nLines;
|
2016-01-16 12:34:22 -06:00
|
|
|
}
|
2016-01-15 21:48:38 -06:00
|
|
|
|
2017-01-02 22:53:31 -06:00
|
|
|
void uiDrawTextLayoutLineByteRange(uiDrawTextLayout *tl, int line, size_t *start, size_t *end)
|
2016-01-15 19:18:53 -06:00
|
|
|
{
|
2017-01-04 22:50:08 -06:00
|
|
|
CTLineRef lr;
|
|
|
|
CFRange range;
|
|
|
|
|
2017-01-17 11:02:42 -06:00
|
|
|
lr = (CTLineRef) CFArrayGetValueAtIndex(tl->lines, line);
|
2017-01-04 22:50:08 -06:00
|
|
|
range = CTLineGetStringRange(lr);
|
2017-01-07 19:09:44 -06:00
|
|
|
*start = tl->u16tou8[range.location];
|
|
|
|
*end = tl->u16tou8[range.location + range.length];
|
2016-01-15 19:18:53 -06:00
|
|
|
}
|
|
|
|
|
2017-01-02 22:53:31 -06:00
|
|
|
void uiDrawTextLayoutLineGetMetrics(uiDrawTextLayout *tl, int line, uiDrawTextLayoutLineMetrics *m)
|
2016-01-12 00:12:35 -06:00
|
|
|
{
|
2017-01-17 11:02:42 -06:00
|
|
|
*m = tl->lineMetrics[line];
|
2016-01-12 00:12:35 -06:00
|
|
|
}
|
|
|
|
|
2017-01-02 22:53:31 -06:00
|
|
|
void uiDrawTextLayoutByteIndexToGraphemeRect(uiDrawTextLayout *tl, size_t pos, int *line, double *x, double *y, double *width, double *height)
|
2016-01-12 00:12:35 -06:00
|
|
|
{
|
|
|
|
}
|
2016-01-13 14:17:49 -06:00
|
|
|
|
2017-01-07 19:09:44 -06:00
|
|
|
void uiDrawTextLayoutHitTest(uiDrawTextLayout *tl, double x, double y, uiDrawTextLayoutHitTestResult *result)
|
2016-05-23 00:11:43 -05:00
|
|
|
{
|
2017-01-17 11:02:42 -06:00
|
|
|
CFIndex i;
|
|
|
|
CTLine line;
|
|
|
|
CFIndex pos;
|
|
|
|
CGFloat charLeft, charRight;
|
2017-01-07 19:09:44 -06:00
|
|
|
|
2017-01-17 11:02:42 -06:00
|
|
|
if (y >= 0) {
|
|
|
|
for (i = 0; i < tl->nLines; i++) {
|
|
|
|
double ltop, lbottom;
|
2017-01-07 19:09:44 -06:00
|
|
|
|
2017-01-17 11:02:42 -06:00
|
|
|
ltop = tl->lineMetrics[i].Y;
|
|
|
|
lbottom = ltop + tl->lineMetrics[i].Height;
|
|
|
|
if (y >= ltop && y < lbottom)
|
2017-01-07 19:09:44 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
result->YPosition = uiDrawTextLayoutHitTestPositionInside;
|
2017-01-17 11:02:42 -06:00
|
|
|
if (i == tl->nLines) {
|
|
|
|
i--;
|
|
|
|
result->YPosition = uiDrawTextLayoutHitTestPositionAfter;
|
2017-01-07 19:09:44 -06:00
|
|
|
}
|
2017-01-17 11:02:42 -06:00
|
|
|
} else {
|
|
|
|
i = 0;
|
|
|
|
result->YPosition = uiDrawTextLayoutHitTestPositionBefore;
|
2017-01-07 19:09:44 -06:00
|
|
|
}
|
2017-01-17 11:02:42 -06:00
|
|
|
m->Line = i;
|
|
|
|
|
|
|
|
result->XPosition = uiDrawTextLayoutHitTestPositionInside;
|
|
|
|
if (x < tl->lineMetrics[i].X) {
|
|
|
|
result->XPosition = uiDrawTextLay
|
|
|
|
outHitTestPositionBefore;
|
|
|
|
// and forcibly return the first character
|
|
|
|
x = tl->lineMetrics[i].X;
|
|
|
|
} else if (x > (tl->lineMetrics[i].X + tl->lineMetrics[i].Width)) {
|
|
|
|
result->XPosition = uiDrawTextLayoutHitTestP
|
|
|
|
ositionAfter;
|
|
|
|
// and forcibly return the last character
|
|
|
|
x = tl->lineMetrics[i].X + tl->lineMetrics[i].Width;
|
|
|
|
}
|
|
|
|
|
|
|
|
line = (CTLineRef) CFArrayGetValueAtIndex(tl->lines, i);
|
|
|
|
pos = CTLineGetStringIndexForPosition(line, CGP
|
|
|
|
ointMake(x, 0));
|
|
|
|
if (pos == kCFNotFound) {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
m->Pos = tl->u16tou8[pos];
|
2016-05-23 00:11:43 -05:00
|
|
|
}
|
|
|
|
|
2017-01-02 22:53:31 -06:00
|
|
|
void uiDrawTextLayoutByteRangeToRectangle(uiDrawTextLayout *tl, size_t start, size_t end, uiDrawTextLayoutByteRangeRectangle *r)
|
2016-04-19 15:06:50 -05:00
|
|
|
{
|
|
|
|
}
|