From 44f8409b8c4b5f3b0bc0810da604923df7cc3436 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sat, 11 Feb 2017 21:10:16 -0500 Subject: [PATCH] And implemented the alignment stuff on OS X. --- darwin/drawtext.m | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/darwin/drawtext.m b/darwin/drawtext.m index e7a59164..bae7326d 100644 --- a/darwin/drawtext.m +++ b/darwin/drawtext.m @@ -46,27 +46,55 @@ static CTFontRef fontdescToCTFont(uiDrawFontDescriptor *fd) return font; } -static CFAttributedStringRef attrstrToCoreFoundation(uiAttributedString *s, uiDrawFontDescriptor *defaultFont) +static const CTTextAlignment ctaligns[] = { + [uiDrawTextLayoutAlignLeft] = kCTTextAlignmentLeft, + [uiDrawTextLayoutAlignCenter] = kCTTextAlignmentCenter, + [uiDrawTextLayoutAlignRight] = kCTTextAlignmentRight, +}; + +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; +} + +static CFAttributedStringRef attrstrToCoreFoundation(uiDrawTextLayoutParams *p) { CFStringRef cfstr; CFMutableDictionaryRef defaultAttrs; CTFontRef defaultCTFont; + CTParagraphStyleRef ps; CFAttributedStringRef base; CFMutableAttributedStringRef mas; - cfstr = CFStringCreateWithCharacters(NULL, attrstrUTF16(s), attrstrUTF16Len(s)); + cfstr = CFStringCreateWithCharacters(NULL, attrstrUTF16(p->String), attrstrUTF16Len(p->String)); if (cfstr == NULL) { // TODO } - defaultAttrs = CFDictionaryCreateMutable(NULL, 1, + defaultAttrs = CFDictionaryCreateMutable(NULL, 0, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); if (defaultAttrs == NULL) { // TODO } - defaultCTFont = fontdescToCTFont(defaultFont); + defaultCTFont = fontdescToCTFont(p->DefaultFont); CFDictionaryAddValue(defaultAttrs, kCTFontAttributeName, defaultCTFont); CFRelease(defaultCTFont); + ps = mkParagraphStyle(p); + CFDictionaryAddValue(defaultAttrs, kCTParagraphStyleAttributeName, ps); + CFRelease(ps); base = CFAttributedStringCreate(NULL, cfstr, defaultAttrs); if (base == NULL) { @@ -163,7 +191,7 @@ uiDrawTextLayout *uiDrawNewTextLayout(uiDrawTextLayoutParams *p) CGRect rect; tl = uiNew(uiDrawTextLayout); - tl->attrstr = attrstrToCoreFoundation(p->String, p->DefaultFont); + tl->attrstr = attrstrToCoreFoundation(p); range.location = 0; range.length = CFAttributedStringGetLength(tl->attrstr); tl->width = p->Width;