2017-02-12 00:05:27 -06:00
|
|
|
// 12 february 2017
|
|
|
|
#import "uipriv_darwin.h"
|
2018-03-04 19:01:52 -06:00
|
|
|
#import "attrstr.h"
|
2017-02-12 00:05:27 -06:00
|
|
|
|
2017-02-14 13:57:56 -06:00
|
|
|
// this is what AppKit does internally
|
|
|
|
// WebKit does this too; see https://github.com/adobe/webkit/blob/master/Source/WebCore/platform/graphics/mac/GraphicsContextMac.mm
|
|
|
|
static NSColor *spellingColor = nil;
|
|
|
|
static NSColor *grammarColor = nil;
|
|
|
|
static NSColor *auxiliaryColor = nil;
|
|
|
|
|
|
|
|
static NSColor *tryColorNamed(NSString *name)
|
|
|
|
{
|
|
|
|
NSImage *img;
|
|
|
|
|
|
|
|
img = [NSImage imageNamed:name];
|
|
|
|
if (img == nil)
|
|
|
|
return nil;
|
|
|
|
return [NSColor colorWithPatternImage:img];
|
|
|
|
}
|
|
|
|
|
2018-03-04 19:01:52 -06:00
|
|
|
void uiprivInitUnderlineColors(void)
|
2017-02-14 13:57:56 -06:00
|
|
|
{
|
|
|
|
spellingColor = tryColorNamed(@"NSSpellingDot");
|
|
|
|
if (spellingColor == nil) {
|
|
|
|
// WebKit says this is needed for "older systems"; not sure how old, but 10.11 AppKit doesn't look for this
|
|
|
|
spellingColor = tryColorNamed(@"SpellingDot");
|
|
|
|
if (spellingColor == nil)
|
|
|
|
spellingColor = [NSColor redColor];
|
|
|
|
}
|
|
|
|
[spellingColor retain]; // override autoreleasing
|
|
|
|
|
|
|
|
grammarColor = tryColorNamed(@"NSGrammarDot");
|
|
|
|
if (grammarColor == nil) {
|
|
|
|
// WebKit says this is needed for "older systems"; not sure how old, but 10.11 AppKit doesn't look for this
|
|
|
|
grammarColor = tryColorNamed(@"GrammarDot");
|
|
|
|
if (grammarColor == nil)
|
|
|
|
grammarColor = [NSColor greenColor];
|
|
|
|
}
|
|
|
|
[grammarColor retain]; // override autoreleasing
|
|
|
|
|
|
|
|
auxiliaryColor = tryColorNamed(@"NSCorrectionDot");
|
|
|
|
if (auxiliaryColor == nil) {
|
|
|
|
// WebKit says this is needed for "older systems"; not sure how old, but 10.11 AppKit doesn't look for this
|
|
|
|
auxiliaryColor = tryColorNamed(@"CorrectionDot");
|
|
|
|
if (auxiliaryColor == nil)
|
|
|
|
auxiliaryColor = [NSColor blueColor];
|
|
|
|
}
|
|
|
|
[auxiliaryColor retain]; // override autoreleasing
|
|
|
|
}
|
|
|
|
|
2018-03-04 19:01:52 -06:00
|
|
|
void uiprivUninitUnderlineColors(void)
|
2017-02-14 13:57:56 -06:00
|
|
|
{
|
|
|
|
[auxiliaryColor release];
|
2018-03-04 19:01:52 -06:00
|
|
|
auxiliaryColor = nil;
|
2017-02-14 13:57:56 -06:00
|
|
|
[grammarColor release];
|
2018-03-08 22:27:04 -06:00
|
|
|
grammarColor = nil;
|
2017-02-14 13:57:56 -06:00
|
|
|
[spellingColor release];
|
2018-03-04 19:01:52 -06:00
|
|
|
spellingColor = nil;
|
2017-02-14 13:57:56 -06:00
|
|
|
}
|
|
|
|
|
2018-03-08 22:04:45 -06:00
|
|
|
// TODO opentype features are lost when using uiFontDescriptor, so a handful of fonts in the font panel ("Titling" variants of some fonts and possibly others but those are the examples I know about) cannot be represented by uiFontDescriptor; what *can* we do about this since this info is NOT part of the font on other platforms?
|
2017-02-12 00:05:27 -06:00
|
|
|
// TODO see if we could use NSAttributedString?
|
|
|
|
// TODO consider renaming this struct and the fep variable(s)
|
2017-05-30 12:06:58 -05:00
|
|
|
// TODO restructure all this so the important details at the top are below with the combined font attributes type?
|
2017-05-30 12:18:13 -05:00
|
|
|
// TODO in fact I should just write something to explain everything in this file...
|
2017-02-12 00:05:27 -06:00
|
|
|
struct foreachParams {
|
|
|
|
CFMutableAttributedStringRef mas;
|
2018-03-11 01:26:32 -06:00
|
|
|
NSMutableArray *backgroundParams;
|
2017-02-12 00:05:27 -06:00
|
|
|
};
|
|
|
|
|
2018-03-05 22:15:31 -06:00
|
|
|
// unlike the other systems, Core Text rolls family, size, weight, italic, width, AND opentype features into the "font" attribute
|
|
|
|
// instead of incrementally adjusting CTFontRefs (which, judging from NSFontManager, seems finicky and UI-centric), we use a custom class to incrementally store attributes that go into a CTFontRef, and then convert everything to CTFonts en masse later
|
|
|
|
// https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/AttributedStrings/Tasks/ChangingAttrStrings.html#//apple_ref/doc/uid/20000162-BBCBGCDG says we must have -hash and -isEqual: workign properly for this to work, so we must do that too, using a basic xor-based hash and leveraging Cocoa -hash implementations where useful and feasible (if not necessary)
|
|
|
|
// TODO structure and rewrite this part
|
2018-03-05 22:16:43 -06:00
|
|
|
// TODO re-find sources proving support of custom attributes
|
2018-03-04 22:57:18 -06:00
|
|
|
// TODO what if this is NULL?
|
|
|
|
static const CFStringRef combinedFontAttrName = CFSTR("libuiCombinedFontAttribute");
|
|
|
|
|
|
|
|
enum {
|
|
|
|
cFamily,
|
|
|
|
cSize,
|
|
|
|
cWeight,
|
|
|
|
cItalic,
|
|
|
|
cStretch,
|
|
|
|
cFeatures,
|
|
|
|
nc,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const int toc[] = {
|
2018-03-05 07:59:45 -06:00
|
|
|
[uiAttributeTypeFamily] = cFamily,
|
|
|
|
[uiAttributeTypeSize] = cSize,
|
|
|
|
[uiAttributeTypeWeight] = cWeight,
|
|
|
|
[uiAttributeTypeItalic] = cItalic,
|
|
|
|
[uiAttributeTypeStretch] = cStretch,
|
|
|
|
[uiAttributeTypeFeatures] = cFeatures,
|
2018-03-04 22:57:18 -06:00
|
|
|
};
|
|
|
|
|
2018-03-05 07:59:45 -06:00
|
|
|
static uiForEach featuresHash(const uiOpenTypeFeatures *otf, char a, char b, char c, char d, uint32_t value, void *data)
|
|
|
|
{
|
|
|
|
NSUInteger *hash = (NSUInteger *) data;
|
|
|
|
uint32_t tag;
|
|
|
|
|
|
|
|
tag = (((uint32_t) a) & 0xFF) << 24;
|
|
|
|
tag |= (((uint32_t) b) & 0xFF) << 16;
|
|
|
|
tag |= (((uint32_t) c) & 0xFF) << 8;
|
|
|
|
tag |= ((uint32_t) d) & 0xFF;
|
|
|
|
*hash ^= tag;
|
|
|
|
*hash ^= value;
|
|
|
|
return uiForEachContinue;
|
|
|
|
}
|
|
|
|
|
2018-03-04 22:57:18 -06:00
|
|
|
@interface uiprivCombinedFontAttr : NSObject<NSCopying> {
|
|
|
|
uiAttribute *attrs[nc];
|
2018-03-05 07:59:45 -06:00
|
|
|
BOOL hasHash;
|
|
|
|
NSUInteger hash;
|
2018-03-04 22:57:18 -06:00
|
|
|
}
|
2018-03-05 22:15:31 -06:00
|
|
|
- (void)addAttribute:(uiAttribute *)attr;
|
2018-03-08 22:04:45 -06:00
|
|
|
- (CTFontRef)toCTFontWithDefaultFont:(uiFontDescriptor *)defaultFont;
|
2017-05-30 12:06:58 -05:00
|
|
|
@end
|
2017-02-13 09:34:25 -06:00
|
|
|
|
2018-03-04 19:01:52 -06:00
|
|
|
@implementation uiprivCombinedFontAttr
|
2017-05-30 12:06:58 -05:00
|
|
|
|
2018-03-04 22:57:18 -06:00
|
|
|
- (id)init
|
2017-05-30 12:06:58 -05:00
|
|
|
{
|
|
|
|
self = [super init];
|
2018-03-05 07:59:45 -06:00
|
|
|
if (self) {
|
2018-03-04 22:57:18 -06:00
|
|
|
memset(self->attrs, 0, nc * sizeof (uiAttribute *));
|
2018-03-05 07:59:45 -06:00
|
|
|
self->hasHash = NO;
|
|
|
|
}
|
2017-05-30 12:06:58 -05:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-03-04 22:57:18 -06:00
|
|
|
- (void)dealloc
|
2017-05-30 12:06:58 -05:00
|
|
|
{
|
2018-03-04 22:57:18 -06:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nc; i++)
|
|
|
|
if (self->attrs[i] != NULL) {
|
|
|
|
uiprivAttributeRelease(self->attrs[i]);
|
|
|
|
self->attrs[i] = NULL;
|
|
|
|
}
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)copyWithZone:(NSZone *)zone
|
|
|
|
{
|
|
|
|
uiprivCombinedFontAttr *ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ret = [[uiprivCombinedFontAttr allocWithZone:zone] init];
|
|
|
|
for (i = 0; i < nc; i++)
|
|
|
|
if (self->attrs[i] != NULL)
|
|
|
|
ret->attrs[i] = uiprivAttributeRetain(self->attrs[i]);
|
2018-03-05 07:59:45 -06:00
|
|
|
ret->hasHash = self->hasHash;
|
|
|
|
ret->hash = self->hash;
|
2018-03-04 22:57:18 -06:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-03-05 22:15:31 -06:00
|
|
|
- (void)addAttribute:(uiAttribute *)attr
|
2018-03-04 22:57:18 -06:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
|
|
|
index = toc[uiAttributeGetType(attr)];
|
|
|
|
if (self->attrs[index] != NULL)
|
|
|
|
uiprivAttributeRelease(self->attrs[index]);
|
|
|
|
self->attrs[index] = uiprivAttributeRetain(attr);
|
2018-03-05 07:59:45 -06:00
|
|
|
self->hasHash = NO;
|
2018-03-04 22:57:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isEqual:(id)bb
|
|
|
|
{
|
|
|
|
uiprivCombinedFontAttr *b = (uiprivCombinedFontAttr *) bb;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (b == nil)
|
2017-05-30 12:06:58 -05:00
|
|
|
return NO;
|
2018-03-04 22:57:18 -06:00
|
|
|
for (i = 0; i < nc; i++) {
|
|
|
|
if (self->attrs[i] == NULL && b->attrs[i] == NULL)
|
|
|
|
continue;
|
|
|
|
if (self->attrs[i] == NULL || b->attrs[i] == NULL)
|
|
|
|
return NO;
|
|
|
|
if (!uiprivAttributeEqual(self->attrs[i], b->attrs[i]))
|
|
|
|
return NO;
|
|
|
|
}
|
2017-05-30 12:06:58 -05:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2018-03-04 22:57:18 -06:00
|
|
|
- (NSUInteger)hash
|
|
|
|
{
|
2018-03-05 07:59:45 -06:00
|
|
|
if (self->hasHash)
|
|
|
|
return self->hash;
|
|
|
|
@autoreleasepool {
|
|
|
|
NSString *family;
|
|
|
|
NSNumber *size;
|
|
|
|
|
|
|
|
self->hash = 0;
|
|
|
|
if (self->attrs[cFamily] != NULL) {
|
|
|
|
family = [NSString stringWithUTF8String:uiAttributeFamily(self->attrs[cFamily])];
|
|
|
|
// TODO make sure this aligns with case-insensitive compares when those are done in common/attribute.c
|
|
|
|
self->hash ^= [[family uppercaseString] hash];
|
|
|
|
}
|
|
|
|
if (self->attrs[cSize] != NULL) {
|
|
|
|
size = [NSNumber numberWithDouble:uiAttributeSize(self->attrs[cSize])];
|
|
|
|
self->hash ^= [size hash];
|
|
|
|
}
|
|
|
|
if (self->attrs[cWeight] != NULL)
|
|
|
|
self->hash ^= (NSUInteger) uiAttributeWeight(self->attrs[cWeight]);
|
|
|
|
if (self->attrs[cItalic] != NULL)
|
|
|
|
self->hash ^= (NSUInteger) uiAttributeItalic(self->attrs[cItalic]);
|
|
|
|
if (self->attrs[cStretch] != NULL)
|
|
|
|
self->hash ^= (NSUInteger) uiAttributeStretch(self->attrs[cStretch]);
|
|
|
|
if (self->attrs[cFeatures] != NULL)
|
|
|
|
uiOpenTypeFeaturesForEach(uiAttributeFeatures(self->attrs[cFeatures]), featuresHash, &(self->hash));
|
|
|
|
self->hasHash = YES;
|
|
|
|
}
|
|
|
|
return self->hash;
|
2018-03-04 22:57:18 -06:00
|
|
|
}
|
|
|
|
|
2018-03-08 22:04:45 -06:00
|
|
|
- (CTFontRef)toCTFontWithDefaultFont:(uiFontDescriptor *)defaultFont
|
2017-05-30 12:18:13 -05:00
|
|
|
{
|
2018-03-08 22:04:45 -06:00
|
|
|
uiFontDescriptor uidesc;
|
2017-05-30 12:18:13 -05:00
|
|
|
CTFontDescriptorRef desc;
|
|
|
|
CTFontRef font;
|
|
|
|
|
2018-03-04 22:57:18 -06:00
|
|
|
uidesc = *defaultFont;
|
|
|
|
if (self->attrs[cFamily] != NULL)
|
2018-03-08 22:04:45 -06:00
|
|
|
// TODO const-correct uiFontDescriptor or change this function below
|
2018-03-04 22:57:18 -06:00
|
|
|
uidesc.Family = (char *) uiAttributeFamily(self->attrs[cFamily]);
|
|
|
|
if (self->attrs[cSize] != NULL)
|
|
|
|
uidesc.Size = uiAttributeSize(self->attrs[cSize]);
|
|
|
|
if (self->attrs[cWeight] != NULL)
|
|
|
|
uidesc.Weight = uiAttributeWeight(self->attrs[cWeight]);
|
|
|
|
if (self->attrs[cItalic] != NULL)
|
|
|
|
uidesc.Italic = uiAttributeItalic(self->attrs[cItalic]);
|
|
|
|
if (self->attrs[cStretch] != NULL)
|
|
|
|
uidesc.Stretch = uiAttributeStretch(self->attrs[cStretch]);
|
2018-03-08 22:04:45 -06:00
|
|
|
desc = uiprivFontDescriptorToCTFontDescriptor(&uidesc);
|
2018-03-04 22:57:18 -06:00
|
|
|
if (self->attrs[cFeatures] != NULL)
|
|
|
|
desc = uiprivCTFontDescriptorAppendFeatures(desc, uiAttributeFeatures(self->attrs[cFeatures]));
|
|
|
|
font = CTFontCreateWithFontDescriptor(desc, uidesc.Size, NULL);
|
2017-05-30 12:18:13 -05:00
|
|
|
CFRelease(desc); // TODO correct?
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
2017-05-30 12:06:58 -05:00
|
|
|
@end
|
2017-02-13 09:34:25 -06:00
|
|
|
|
2018-03-05 22:15:31 -06:00
|
|
|
static void addFontAttributeToRange(struct foreachParams *p, size_t start, size_t end, uiAttribute *attr)
|
2017-02-12 00:05:27 -06:00
|
|
|
{
|
2018-03-04 19:01:52 -06:00
|
|
|
uiprivCombinedFontAttr *cfa;
|
2018-03-05 22:15:31 -06:00
|
|
|
CFRange range;
|
|
|
|
size_t diff;
|
|
|
|
|
2018-03-10 21:04:07 -06:00
|
|
|
while (start < end) {
|
2018-03-05 22:15:31 -06:00
|
|
|
cfa = (uiprivCombinedFontAttr *) CFAttributedStringGetAttribute(p->mas, start, combinedFontAttrName, &range);
|
|
|
|
if (cfa == nil)
|
|
|
|
cfa = [uiprivCombinedFontAttr new];
|
|
|
|
else
|
|
|
|
cfa = [cfa copy];
|
|
|
|
[cfa addAttribute:attr];
|
2018-03-13 17:43:32 -05:00
|
|
|
// clamp range within [start, end)
|
2018-03-05 22:15:31 -06:00
|
|
|
if (range.location < start) {
|
|
|
|
diff = start - range.location;
|
|
|
|
range.location = start;
|
|
|
|
range.length -= diff;
|
2018-03-04 19:45:05 -06:00
|
|
|
}
|
2018-03-05 22:15:31 -06:00
|
|
|
if ((range.location + range.length) > end)
|
|
|
|
range.length = end - range.location;
|
|
|
|
CFAttributedStringSetAttribute(p->mas, range, combinedFontAttrName, cfa);
|
|
|
|
[cfa release];
|
2018-03-08 22:27:04 -06:00
|
|
|
start += range.length;
|
2017-02-12 00:05:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-08 22:27:04 -06:00
|
|
|
static CGColorRef mkcolor(double r, double g, double b, double a)
|
2017-02-14 13:57:56 -06:00
|
|
|
{
|
|
|
|
CGColorSpaceRef colorspace;
|
|
|
|
CGColorRef color;
|
|
|
|
CGFloat components[4];
|
|
|
|
|
2018-03-04 19:45:05 -06:00
|
|
|
// TODO we should probably just create this once and recycle it throughout program execution...
|
2017-02-14 13:57:56 -06:00
|
|
|
colorspace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
|
|
|
|
if (colorspace == NULL) {
|
|
|
|
// TODO
|
|
|
|
}
|
2018-03-08 22:27:04 -06:00
|
|
|
components[0] = r;
|
|
|
|
components[1] = g;
|
|
|
|
components[2] = b;
|
|
|
|
components[3] = a;
|
2017-02-14 13:57:56 -06:00
|
|
|
color = CGColorCreate(colorspace, components);
|
|
|
|
CFRelease(colorspace);
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
2018-03-11 01:26:32 -06:00
|
|
|
static void addBackgroundAttribute(struct foreachParams *p, size_t start, size_t end, double r, double g, double b, double a)
|
|
|
|
{
|
|
|
|
uiprivDrawTextBackgroundParams *dtb;
|
|
|
|
|
|
|
|
// TODO make sure this works properly with line paragraph spacings (after figuring out what that means, of course)
|
2018-05-05 21:02:25 -05:00
|
|
|
if (uiprivFUTURE_kCTBackgroundColorAttributeName != NULL) {
|
2018-03-11 01:26:32 -06:00
|
|
|
CGColorRef color;
|
|
|
|
CFRange range;
|
|
|
|
|
|
|
|
color = mkcolor(r, g, b, a);
|
|
|
|
range.location = start;
|
|
|
|
range.length = end - start;
|
2018-05-05 21:02:25 -05:00
|
|
|
CFAttributedStringSetAttribute(p->mas, range, *uiprivFUTURE_kCTBackgroundColorAttributeName, color);
|
2018-03-11 01:26:32 -06:00
|
|
|
CFRelease(color);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dtb = [[uiprivDrawTextBackgroundParams alloc] initWithStart:start end:end r:r g:g b:b a:a];
|
|
|
|
[p->backgroundParams addObject:dtb];
|
|
|
|
[dtb release];
|
|
|
|
}
|
|
|
|
|
2018-03-04 19:45:05 -06:00
|
|
|
static uiForEach processAttribute(const uiAttributedString *s, const uiAttribute *attr, size_t start, size_t end, void *data)
|
2017-02-12 00:05:27 -06:00
|
|
|
{
|
|
|
|
struct foreachParams *p = (struct foreachParams *) data;
|
2017-02-12 19:27:47 -06:00
|
|
|
CFRange range;
|
|
|
|
CGColorRef color;
|
2017-02-14 13:57:56 -06:00
|
|
|
int32_t us;
|
|
|
|
CFNumberRef num;
|
2018-03-08 22:27:04 -06:00
|
|
|
double r, g, b, a;
|
|
|
|
uiUnderlineColor colorType;
|
2017-02-12 00:05:27 -06:00
|
|
|
|
2018-03-04 19:45:05 -06:00
|
|
|
start = uiprivAttributedStringUTF8ToUTF16(s, start);
|
|
|
|
end = uiprivAttributedStringUTF8ToUTF16(s, end);
|
2017-02-12 19:27:47 -06:00
|
|
|
range.location = start;
|
|
|
|
range.length = end - start;
|
2018-03-04 19:45:05 -06:00
|
|
|
switch (uiAttributeGetType(attr)) {
|
2018-03-05 22:15:31 -06:00
|
|
|
case uiAttributeTypeFamily:
|
|
|
|
case uiAttributeTypeSize:
|
|
|
|
case uiAttributeTypeWeight:
|
|
|
|
case uiAttributeTypeItalic:
|
|
|
|
case uiAttributeTypeStretch:
|
|
|
|
case uiAttributeTypeFeatures:
|
|
|
|
addFontAttributeToRange(p, start, end, attr);
|
2017-02-12 12:41:52 -06:00
|
|
|
break;
|
2018-03-08 22:27:04 -06:00
|
|
|
case uiAttributeTypeColor:
|
|
|
|
uiAttributeColor(attr, &r, &g, &b, &a);
|
|
|
|
color = mkcolor(r, g, b, a);
|
2017-02-12 19:27:47 -06:00
|
|
|
CFAttributedStringSetAttribute(p->mas, range, kCTForegroundColorAttributeName, color);
|
|
|
|
CFRelease(color);
|
|
|
|
break;
|
2018-03-08 22:27:04 -06:00
|
|
|
case uiAttributeTypeBackground:
|
|
|
|
uiAttributeColor(attr, &r, &g, &b, &a);
|
2018-03-11 01:26:32 -06:00
|
|
|
addBackgroundAttribute(p, start, end, r, g, b, a);
|
2017-02-13 01:10:39 -06:00
|
|
|
break;
|
2018-03-05 22:15:31 -06:00
|
|
|
// TODO turn into a class, like we did with the font attributes, or even integrate *into* the font attributes
|
2018-03-08 22:27:04 -06:00
|
|
|
case uiAttributeTypeUnderline:
|
|
|
|
switch (uiAttributeUnderline(attr)) {
|
|
|
|
case uiUnderlineNone:
|
2017-02-14 13:57:56 -06:00
|
|
|
us = kCTUnderlineStyleNone;
|
|
|
|
break;
|
2018-03-08 22:27:04 -06:00
|
|
|
case uiUnderlineSingle:
|
2017-02-14 13:57:56 -06:00
|
|
|
us = kCTUnderlineStyleSingle;
|
|
|
|
break;
|
2018-03-08 22:27:04 -06:00
|
|
|
case uiUnderlineDouble:
|
2017-02-14 13:57:56 -06:00
|
|
|
us = kCTUnderlineStyleDouble;
|
|
|
|
break;
|
2018-03-08 22:27:04 -06:00
|
|
|
case uiUnderlineSuggestion:
|
2017-02-14 13:57:56 -06:00
|
|
|
// TODO incorrect if a solid color
|
|
|
|
us = kCTUnderlineStyleThick;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
num = CFNumberCreate(NULL, kCFNumberSInt32Type, &us);
|
|
|
|
CFAttributedStringSetAttribute(p->mas, range, kCTUnderlineStyleAttributeName, num);
|
|
|
|
CFRelease(num);
|
|
|
|
break;
|
2018-03-08 22:27:04 -06:00
|
|
|
case uiAttributeTypeUnderlineColor:
|
|
|
|
uiAttributeUnderlineColor(attr, &colorType, &r, &g, &b, &a);
|
|
|
|
switch (colorType) {
|
|
|
|
case uiUnderlineColorCustom:
|
|
|
|
color = mkcolor(r, g, b, a);
|
2017-02-14 13:57:56 -06:00
|
|
|
break;
|
2018-03-08 22:27:04 -06:00
|
|
|
case uiUnderlineColorSpelling:
|
2017-02-14 13:57:56 -06:00
|
|
|
color = [spellingColor CGColor];
|
|
|
|
break;
|
2018-03-08 22:27:04 -06:00
|
|
|
case uiUnderlineColorGrammar:
|
2017-02-14 13:57:56 -06:00
|
|
|
color = [grammarColor CGColor];
|
|
|
|
break;
|
2018-03-08 22:27:04 -06:00
|
|
|
case uiUnderlineColorAuxiliary:
|
2017-02-14 13:57:56 -06:00
|
|
|
color = [auxiliaryColor CGColor];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
CFAttributedStringSetAttribute(p->mas, range, kCTUnderlineColorAttributeName, color);
|
2018-03-08 22:27:04 -06:00
|
|
|
if (colorType == uiUnderlineColorCustom)
|
2017-02-14 13:57:56 -06:00
|
|
|
CFRelease(color);
|
|
|
|
break;
|
2017-02-12 00:05:27 -06:00
|
|
|
}
|
2017-06-06 11:47:07 -05:00
|
|
|
return uiForEachContinue;
|
2017-02-12 00:05:27 -06:00
|
|
|
}
|
|
|
|
|
2018-03-08 22:04:45 -06:00
|
|
|
static void applyFontAttributes(CFMutableAttributedStringRef mas, uiFontDescriptor *defaultFont)
|
2017-02-12 00:05:27 -06:00
|
|
|
{
|
2018-03-05 22:15:31 -06:00
|
|
|
uiprivCombinedFontAttr *cfa;
|
2017-05-30 13:38:25 -05:00
|
|
|
CTFontRef font;
|
|
|
|
CFRange range;
|
2018-03-05 22:15:31 -06:00
|
|
|
CFIndex n;
|
|
|
|
|
|
|
|
n = CFAttributedStringGetLength(mas);
|
2017-05-30 13:38:25 -05:00
|
|
|
|
2018-03-05 22:15:31 -06:00
|
|
|
// first apply the default font to the entire string
|
|
|
|
// TODO is this necessary given the #if 0'd code in uiprivAttributedStringToCFAttributedString()?
|
|
|
|
cfa = [uiprivCombinedFontAttr new];
|
|
|
|
font = [cfa toCTFontWithDefaultFont:defaultFont];
|
2017-05-30 13:38:25 -05:00
|
|
|
[cfa release];
|
2018-03-05 22:15:31 -06:00
|
|
|
range.location = 0;
|
|
|
|
range.length = n;
|
|
|
|
CFAttributedStringSetAttribute(mas, range, kCTFontAttributeName, font);
|
|
|
|
CFRelease(font);
|
2017-05-30 13:38:25 -05:00
|
|
|
|
2018-03-13 21:01:15 -05:00
|
|
|
// now go through, replacing every uiprivCombinedFontAttr with the proper CTFontRef
|
2017-05-30 13:38:25 -05:00
|
|
|
// we are best off treating series of identical fonts as single ranges ourselves for parity across platforms, even if OS X does something similar itself
|
|
|
|
range.location = 0;
|
2018-03-05 22:15:31 -06:00
|
|
|
while (range.location < n) {
|
|
|
|
// TODO consider seeing if CFAttributedStringGetAttributeAndLongestEffectiveRange() can make things faster by reducing the number of potential iterations, either here or above
|
2018-03-08 22:27:04 -06:00
|
|
|
cfa = (uiprivCombinedFontAttr *) CFAttributedStringGetAttribute(mas, range.location, combinedFontAttrName, &range);
|
2018-03-05 22:15:31 -06:00
|
|
|
if (cfa != nil) {
|
|
|
|
font = [cfa toCTFontWithDefaultFont:defaultFont];
|
|
|
|
CFAttributedStringSetAttribute(mas, range, kCTFontAttributeName, font);
|
|
|
|
CFRelease(font);
|
|
|
|
}
|
|
|
|
range.location += range.length;
|
2017-05-30 13:38:25 -05:00
|
|
|
}
|
|
|
|
|
2018-03-05 22:15:31 -06:00
|
|
|
// and finally, get rid of all the uiprivCombinedFontAttrs as we won't need them anymore
|
|
|
|
range.location = 0;
|
|
|
|
range.length = 0;
|
|
|
|
CFAttributedStringRemoveAttribute(mas, range, combinedFontAttrName);
|
2017-02-12 00:05:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static const CTTextAlignment ctaligns[] = {
|
2017-06-06 13:14:33 -05:00
|
|
|
[uiDrawTextAlignLeft] = kCTTextAlignmentLeft,
|
|
|
|
[uiDrawTextAlignCenter] = kCTTextAlignmentCenter,
|
|
|
|
[uiDrawTextAlignRight] = kCTTextAlignmentRight,
|
2017-02-12 00:05:27 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
static CTParagraphStyleRef mkParagraphStyle(uiDrawTextLayoutParams *p)
|
|
|
|
{
|
|
|
|
CTParagraphStyleRef ps;
|
|
|
|
CTParagraphStyleSetting settings[16];
|
|
|
|
size_t nSettings = 0;
|
|
|
|
|
|
|
|
settings[nSettings].spec = kCTParagraphStyleSpecifierAlignment;
|
|
|
|
settings[nSettings].valueSize = sizeof (CTTextAlignment);
|
|
|
|
settings[nSettings].value = ctaligns + p->Align;
|
|
|
|
nSettings++;
|
|
|
|
|
|
|
|
ps = CTParagraphStyleCreate(settings, nSettings);
|
|
|
|
if (ps == NULL) {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
return ps;
|
|
|
|
}
|
|
|
|
|
2018-03-11 16:36:22 -05:00
|
|
|
// TODO either rename this (on all platforms) to uiprivDrawTextLayoutParams... or rename this file or both or split the struct or something else...
|
2018-03-11 01:26:32 -06:00
|
|
|
CFAttributedStringRef uiprivAttributedStringToCFAttributedString(uiDrawTextLayoutParams *p, NSArray **backgroundParams)
|
2017-02-12 00:05:27 -06:00
|
|
|
{
|
|
|
|
CFStringRef cfstr;
|
|
|
|
CFMutableDictionaryRef defaultAttrs;
|
|
|
|
CTParagraphStyleRef ps;
|
|
|
|
CFAttributedStringRef base;
|
|
|
|
CFMutableAttributedStringRef mas;
|
|
|
|
struct foreachParams fep;
|
|
|
|
|
2018-03-10 18:02:10 -06:00
|
|
|
cfstr = CFStringCreateWithCharacters(NULL, uiprivAttributedStringUTF16String(p->String), uiprivAttributedStringUTF16Len(p->String));
|
2017-02-12 00:05:27 -06:00
|
|
|
if (cfstr == NULL) {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
defaultAttrs = CFDictionaryCreateMutable(NULL, 0,
|
|
|
|
&kCFCopyStringDictionaryKeyCallBacks,
|
|
|
|
&kCFTypeDictionaryValueCallBacks);
|
|
|
|
if (defaultAttrs == NULL) {
|
|
|
|
// TODO
|
|
|
|
}
|
2017-05-30 14:24:31 -05:00
|
|
|
#if 0 /* TODO */
|
2017-02-15 15:22:42 -06:00
|
|
|
ffp.desc = *(p->DefaultFont);
|
|
|
|
defaultCTFont = fontdescToCTFont(&ffp);
|
2017-02-12 00:05:27 -06:00
|
|
|
CFDictionaryAddValue(defaultAttrs, kCTFontAttributeName, defaultCTFont);
|
|
|
|
CFRelease(defaultCTFont);
|
2017-05-30 14:24:31 -05:00
|
|
|
#endif
|
2017-02-12 00:05:27 -06:00
|
|
|
ps = mkParagraphStyle(p);
|
|
|
|
CFDictionaryAddValue(defaultAttrs, kCTParagraphStyleAttributeName, ps);
|
|
|
|
CFRelease(ps);
|
|
|
|
|
|
|
|
base = CFAttributedStringCreate(NULL, cfstr, defaultAttrs);
|
|
|
|
if (base == NULL) {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
CFRelease(cfstr);
|
|
|
|
CFRelease(defaultAttrs);
|
|
|
|
mas = CFAttributedStringCreateMutableCopy(NULL, 0, base);
|
|
|
|
CFRelease(base);
|
|
|
|
|
|
|
|
CFAttributedStringBeginEditing(mas);
|
|
|
|
fep.mas = mas;
|
2018-03-11 01:26:32 -06:00
|
|
|
fep.backgroundParams = [NSMutableArray new];
|
2017-02-12 00:05:27 -06:00
|
|
|
uiAttributedStringForEachAttribute(p->String, processAttribute, &fep);
|
2018-03-05 22:15:31 -06:00
|
|
|
applyFontAttributes(mas, p->DefaultFont);
|
2017-02-12 00:05:27 -06:00
|
|
|
CFAttributedStringEndEditing(mas);
|
|
|
|
|
2018-03-11 01:26:32 -06:00
|
|
|
*backgroundParams = fep.backgroundParams;
|
2017-02-12 00:05:27 -06:00
|
|
|
return mas;
|
|
|
|
}
|