Updated the test and fixed compiler errors in darwin/drawtext.m. It seems to work now! Width handling is still not working right, but width handling needs major rewrites because width on Core Text has major sensibility issues...

This commit is contained in:
Pietro Gagliardi 2016-01-12 01:58:45 -05:00
parent 426b133e58
commit 73867119ef
2 changed files with 36 additions and 28 deletions

View File

@ -380,35 +380,35 @@ CFMutableDictionaryRef extractAttributes(CTFontDescriptorRef desc)
uiDrawTextFont *uiDrawLoadClosestFont(const uiDrawTextFontDescriptor *desc)
{
uiDrawTextFont *f;
uiDrawTextFont *font;
CFMutableDictionaryRef attr;
CTFontDescriptorRef desc;
CTFontDescriptorRef cfdesc;
font = uiNew(uiDrawTextFont);
attr = newAttrList();
addFontFamilyAttr(attr, initialStyle->Family);
addFontSizeAttr(attr, initialStyle->Size);
addFontFamilyAttr(attr, desc->Family);
addFontSizeAttr(attr, desc->Size);
// now we have to do the traits matching, so create a descriptor, match the traits, and then get the attributes back
desc = CTFontDescriptorCreateWithAttributes(attr);
cfdesc = CTFontDescriptorCreateWithAttributes(attr);
// TODO release attr?
desc = matchTraits(desc, initialStyle->Weight, initialStyle->Italic, initialStyle->Stretch);
attr = extractAttributes(desc);
CFRelease(desc);
cfdesc = matchTraits(cfdesc, desc->Weight, desc->Italic, desc->Stretch);
attr = extractAttributes(cfdesc);
CFRelease(cfdesc);
// and finally add the other attributes
if (initialStyle->SmallCaps)
if (desc->SmallCaps)
addFontSmallCapsAttr(attr);
addFontGravityAttr(attr, initialStyle->Gravity);
addFontGravityAttr(attr, desc->Gravity);
// and NOW create the final descriptor
desc = CTFontDescriptorCreateWithAttributes(attr);
cfdesc = CTFontDescriptorCreateWithAttributes(attr);
// TODO release attr?
// specify the initial size again just to be safe
font->f = CTFontCreateWithFontDescriptor(desc, initialStyle->Size, NULL);
// TODO release desc?
font->f = CTFontCreateWithFontDescriptor(cfdesc, desc->Size, NULL);
// TODO release cfdesc?
return font;
}
@ -446,7 +446,7 @@ static intmax_t *strToCFStrOffsetList(const char *str, CFMutableStringRef *cfstr
*cfstr = CFStringCreateMutable(NULL, 0);
if (*cfstr == NULL)
complain("error creating CFMutableStringRef for storing string in uiDrawNewTextLayout()");
complain("error creating CFMutableStringRef for storing string in strToCFStrOffset()");
i = 0;
while (i < len) {
@ -465,7 +465,7 @@ static intmax_t *strToCFStrOffsetList(const char *str, CFMutableStringRef *cfstr
// - reached the end of the string without a successful conversion (invalid string)
// - ran into allocation issues
if (substr == NULL)
complain("something bad happened when trying to prepare string in uiDrawNewTextLayout()");
complain("something bad happened when trying to prepare string in strToCFStrOffset()");
// now save the character offsets for those bytes
pos = CFStringGetLength(*cfstr);
@ -483,19 +483,20 @@ static intmax_t *strToCFStrOffsetList(const char *str, CFMutableStringRef *cfstr
return bytesToCharacters;
}
uiDrawTextLayout *uiDrawNewTextLayout(const char *str, uiDrawTextFont *font)
uiDrawTextLayout *uiDrawNewTextLayout(const char *str, uiDrawTextFont *defaultFont)
{
uiDrawTextLayout *layout;
CFMutableStringRef cfstr;
CFAttributedStringRef immutable;
CFMutableDictionaryRef attr;
layout = uiNew(uiDrawTextLayout);
layout->bytesToCharacters = strToCFStrOffsetList(str, &cfstr);
attr = newAttrList();
// this will retain font->f; no need to worry
CFDictionaryAddValue(attr, kCTFontAttributeName, font->f);
// this will retain defaultFont->f; no need to worry
CFDictionaryAddValue(attr, kCTFontAttributeName, defaultFont->f);
immutable = CFAttributedStringCreate(NULL, cfstr, attr);
if (immutable == NULL)

View File

@ -26,24 +26,31 @@ static double entryDouble(uiEntry *e)
static void handlerDraw(uiAreaHandler *a, uiArea *area, uiAreaDrawParams *dp)
{
uiDrawInitialTextStyle style;
uiDrawTextFontDescriptor desc;
uiDrawTextFont *font;
char *s;
char *family; // make compiler happy
uiDrawTextLayout *layout;
memset(&style, 0, sizeof (uiDrawInitialTextStyle));
memset(&desc, 0, sizeof (uiDrawTextFontDescriptor));
family = uiEntryText(textFont);
style.Family = family;
style.Size = entryDouble(textSize);
style.Weight = uiComboboxSelected(textWeight);
style.Italic = uiComboboxSelected(textItalic);
style.SmallCaps = uiCheckboxChecked(textSmallCaps);
style.Stretch = uiComboboxSelected(textStretch);
style.Gravity = uiComboboxSelected(textGravity);
desc.Family = family;
desc.Size = entryDouble(textSize);
desc.Weight = uiComboboxSelected(textWeight);
desc.Italic = uiComboboxSelected(textItalic);
desc.SmallCaps = uiCheckboxChecked(textSmallCaps);
desc.Stretch = uiComboboxSelected(textStretch);
desc.Gravity = uiComboboxSelected(textGravity);
font = uiDrawLoadClosestFont(&desc);
s = uiEntryText(textString);
layout = uiDrawNewTextLayout(s, &style);
layout = uiDrawNewTextLayout(s, font);
uiDrawText(dp->Context, 10, 10, layout);
uiDrawFreeTextLayout(layout);
uiDrawFreeTextFont(font);
uiFreeText(s);
uiFreeText(family);
}