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

View File

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