From 93bf0d403e2b4c378b6f9b110933944e5bd9b2f9 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Mon, 19 Feb 2018 18:37:50 -0500 Subject: [PATCH] Wrote the new uiAttribute code in attribute.c. --- TODO.md | 3 + common/attribute.c | 235 +++++++++++++++++++++++++++++++++++++++++++++ common/attrstr.h | 4 + ui_attrstr.h | 2 +- 4 files changed, 243 insertions(+), 1 deletion(-) create mode 100644 common/attribute.c create mode 100644 common/attrstr.h diff --git a/TODO.md b/TODO.md index 6389f1f8..528a658d 100644 --- a/TODO.md +++ b/TODO.md @@ -257,3 +257,6 @@ mac os x accessibility uiEntry disabling bugs http://www.cocoabuilder.com/archive/cocoa/215525-nstextfield-bug-can-be.html uiMultilineEntry disabling https://developer.apple.com/library/content/qa/qa1461/_index.html + +more TODOs: +- make no guarantee about buildability of feature branches diff --git a/common/attribute.c b/common/attribute.c new file mode 100644 index 00000000..49dc860d --- /dev/null +++ b/common/attribute.c @@ -0,0 +1,235 @@ +// 19 february 2018 +#include "uipriv.h" +#include "attrstr.h" + +struct uiAttribute { + uiAttributeType type; + union { + const char *family; + double size; + uiTextWeight weight; + uiTextItalic italic; + uiTextStretch stretch; + struct { + double r; + double g; + double b; + double a; + // put this here so we can reuse this structure + uiUnderlineColor underlineColor; + } color; + uiUnderline underline; + uiOpenTypeFeatures *features; + } u; +}; + +static uiAttribute *newAttribute(uiAttributeType type) +{ + uiAttribute *a; + + a = uiprivNew(uiAttribute); + a->type = type; + return a; +} + +void uiFreeAttribute(uiAttribute *a) +[ + switch (a->type) { + case uiAttributeTypeFamily: + uiprivFree(a->u.family); + break; + case uiAttributeTypeFeatures: + uiFreeOpenTypeFeatures(a->u.features); + break; + } + uiprivFree(a); +} + +uiAttributeType uiAttributeGetType(const uiAttribute *a) +{ + return a->type; +} + +uiAttribute *uiNewFamilyAttribute(const char *family) +{ + uiAttribute *a; + + a = newAttribute(uiAttributeTypeFamily); + a->u.family = uiprivStrdup(family); + return a; +} + +const char *uiAttributeFamily(const uiAttribute *a) +{ + return a->u.family; +} + +uiAttribute *uiNewSizeAttribute(double size) +{ + uiAttribute *a; + + a = newAttribute(uiAttributeTypeSize); + a->u.size = size; + return a; +} + +double uiAttributeSize(const uiAttribute *a) +{ + return a->u.size; +} + +uiAttribute *uiNewWeightAttribute(uiTextWeight weight) +{ + uiAttribute *a; + + a = newAttribute(uiAttributeTypeWeight); + a->u.weight = weight; + return a; +} + +uiTextWeight uiAttributeWeight(const uiAttribute *a) +{ + return a->u.weight; +} + +uiAttribute *uiNewItalicAttribute(uiTextItalic italic) +{ + uiAttribute *a; + + a = newAttribute(uiAttributeTypeItalic); + a->u.italic = italic; + return a; +} + +uiTextItalic uiAttributeItalic(const uiAttribute *a) +{ + return a->u.italic; +} + +uiAttribute *uiNewStretchAttribute(uiTextStretch stretch) +{ + uiAttribute *a; + + a = newAttribute(uiAttributeTypeStretch); + a->u.stretch = stretch; + return a; +} + +uiTextStretch uiAttributeStretch(const uiAttribute *a) +{ + return a->u.stretch; +} + +uiAttribute *uiNewColorAttribute(double r, double g, double b, double a) +{ + uiAttribute *at; + + at = newAttribute(uiAttributeTypeColor); + at->u.color.r = r; + at->u.color.g = g; + at->u.color.b = b; + at->u.color.a = a; + return at; +} + +void uiAttributeColor(const uiAttribute *a, double *r, double *g, double *b, double *alpha) +{ + *r = a->u.color.r; + *g = a->u.color.g; + *b = a->u.color.b; + *alpha = a->u.color.a; +} + +uiAttribute *uiNewBackgroundAttribute(double r, double g, double b, double a) +{ + uiAttribute *at; + + at = newAttribute(uiAttributeTypeBackgroundColor); + at->u.color.r = r; + at->u.color.g = g; + at->u.color.b = b; + at->u.color.a = a; + return at; +} + +uiAttribute *uiNewUnderlineAttribute(uiUnderline u) +{ + uiAttribute *a; + + a = newAttribute(uiAttributeTypeUnderline); + a->u.underline = u; + return a; +} + +uiUnderline uiAttributeUnderline(const uiAttribute *a) +{ + return a->u.underline; +} + +uiAttribute *uiNewUnderlineColorAttribute(uiUnderlineColor u, double r, double g, double b, double a) +{ + uiAttribute *at; + + at = uiNewColorAttribute(r, g, b, a); + at->type = uiAttributeTypeUnderlineColor; + at->u.color.underlineColor = u; + return at; +} + +void uiAttributeUnderline(const uiAttribute *a, uiUnderlineColor *u, double *r, double *g, double *b, double *alpha) +{ + *u = a->u.color.underlineColor; + uiAttributeColor(a, r, g, b, alpha); +} + +uiAttribute *uiNewFeaturesAttribute(const uiOpenTypeFeatures *otf) +{ + uiAttribute *a; + + a = newAttribute(uiAttributeTypeFeatures); + a->u.features = uiOpenTypeFeaturesClone(otf); + return a; +} + +const uiOpenTypeFeatures *uiAttributeFeatures(const uiAttribute *a) +{ + return a->u.features; +} + +int uiprivAttributeEqual(const uiAttribute *a, const uiAttribute *b) +{ + if (a == b) + return 1; + if (a->type != b->type) + return 0; + switch (a->type) { + case uiAttributeTypeFamily: + return uiprivStricmp(a->u.family, b->u.family); + case uiAttributeTypeSize: + // TODO is the use of == correct? + return a->u.size == b->u.size; + case uiAttributeTypeWeight: + return a->u.weight == b->u.weight; + case uiAttributeTypeItalic: + return a->u.italic == b->u.italic; + case uiAttributeTypeStretch: + return a->u.stretch == b->u.stretch; + case uiAttributeTypeUnderline: + return a->u.underline == b->u.underline; + case uiAttributeTypeUnderlineColor: + if (a->u.color.underlineColor != b->u.color.underlineColor) + return 0; + // fall through + case uiAttributeTypeColor: + case uiAttributeTypeBackground: + // TODO is the use of == correct? + return (a->u.color.r == b->u.color.r) && + (a->u.color.g == b->u.color.g) && + (a->u.color.b == b->u.color.b) && + (a->u.color.a == b->u.color.a); + case uiAttributeTypeFeatures: + return uiprivOpenTypeFeaturesEqual(a->u.features, b->u.features); + } + // TODO should not be reached + return 0; +} diff --git a/common/attrstr.h b/common/attrstr.h new file mode 100644 index 00000000..297add10 --- /dev/null +++ b/common/attrstr.h @@ -0,0 +1,4 @@ +// 19 february 2018 + +// attribute.c +extern int uiprivAttributeEqual(const uiAttribute *a, const uiAttribute *b); diff --git a/ui_attrstr.h b/ui_attrstr.h index f180ad66..c2da9a4a 100644 --- a/ui_attrstr.h +++ b/ui_attrstr.h @@ -51,7 +51,7 @@ _UI_EXTERN const char *uiAttributeFamily(const uiAttribute *a); // uiNewSizeAttribute() creates a new uiAttribute that changes the // size of the text it is applied to, in typographical points. -_UI_EXTERN uiAttribute *uiNewFamilyAttribute(double size); +_UI_EXTERN uiAttribute *uiNewSizeAttribute(double size); // uiAttributeSize() returns the font size stored in a. It is an error to // call this on a uiAttribute that does not hold a font size.