Made a decision on const correctness in uiOpenTypeFeatures.

This commit is contained in:
Pietro Gagliardi 2017-06-19 14:50:03 -04:00
parent e6ee2b0dbd
commit 3e20e4670c
5 changed files with 22 additions and 17 deletions

View File

@ -51,7 +51,8 @@ void uiOpenTypeFeaturesRemove(uiOpenTypeFeatures *otf, char a, char b, char c, c
[otf->tags removeObjectForKey:tn]; [otf->tags removeObjectForKey:tn];
} }
int uiOpenTypeFeaturesGet(uiOpenTypeFeatures *otf, char a, char b, char c, char d, uint32_t *value) // TODO will the const wreck stuff up?
int uiOpenTypeFeaturesGet(const uiOpenTypeFeatures *otf, char a, char b, char c, char d, uint32_t *value)
{ {
NSNumber *tn, *vn; NSNumber *tn, *vn;
@ -78,7 +79,7 @@ void uiOpenTypeFeaturesForEach(const uiOpenTypeFeatures *otf, uiOpenTypeFeatures
b = (uint8_t) ((tag >> 16) & 0xFF); b = (uint8_t) ((tag >> 16) & 0xFF);
c = (uint8_t) ((tag >> 8) & 0xFF); c = (uint8_t) ((tag >> 8) & 0xFF);
d = (uint8_t) (tag & 0xFF); d = (uint8_t) (tag & 0xFF);
ret = (*f)((char) a, (char) b, (char) c, (char) d, ret = (*f)(otf, (char) a, (char) b, (char) c, (char) d,
mapObjectValue(vn), data); mapObjectValue(vn), data);
// TODO for all: require exact match? // TODO for all: require exact match?
if (ret == uiForEachStop) if (ret == uiForEachStop)
@ -98,7 +99,7 @@ int uiOpenTypeFeaturesEqual(const uiOpenTypeFeatures *a, const uiOpenTypeFeature
// TODO explain all this // TODO explain all this
// TODO rename outerArray and innerDict (the names made sense when this was part of fontdescAppendFeatures(), but not here) // TODO rename outerArray and innerDict (the names made sense when this was part of fontdescAppendFeatures(), but not here)
// TODO make all this use enumerateKeysAndObjects (which requires duplicating code)? // TODO make all this use enumerateKeysAndObjects (which requires duplicating code)?
static uiForEach otfArrayForEachAAT(char a, char b, char c, char d, uint32_t value, void *data) static uiForEach otfArrayForEachAAT(const uiOpenTypeFeatures *otf, char a, char b, char c, char d, uint32_t value, void *data)
{ {
CFMutableArrayRef outerArray = (CFMutableArrayRef) data; CFMutableArrayRef outerArray = (CFMutableArrayRef) data;
@ -133,7 +134,7 @@ static uiForEach otfArrayForEachAAT(char a, char b, char c, char d, uint32_t val
} }
// TODO find out which fonts differ in AAT small caps and test them with this // TODO find out which fonts differ in AAT small caps and test them with this
static uiForEach otfArrayForEachOT(char a, char b, char c, char d, uint32_t value, void *data) static uiForEach otfArrayForEachOT(const uiOpenTypeFeatures *otf, char a, char b, char c, char d, uint32_t value, void *data)
{ {
CFMutableArrayRef outerArray = (CFMutableArrayRef) data; CFMutableArrayRef outerArray = (CFMutableArrayRef) data;
CFDictionaryRef innerDict; CFDictionaryRef innerDict;

1
ui.h
View File

@ -4,6 +4,7 @@
// TODOs // TODOs
// - make getters that return whether something exists accept a NULL pointer to discard the value (and thus only return that the thing exists?) // - make getters that return whether something exists accept a NULL pointer to discard the value (and thus only return that the thing exists?)
// - const-correct everything
#ifndef __LIBUI_UI_H__ #ifndef __LIBUI_UI_H__
#define __LIBUI_UI_H__ #define __LIBUI_UI_H__

View File

@ -104,8 +104,10 @@ _UI_ENUM(uiDrawUnderlineColor) {
// TODO invalid features // TODO invalid features
typedef struct uiOpenTypeFeatures uiOpenTypeFeatures; typedef struct uiOpenTypeFeatures uiOpenTypeFeatures;
// TODO pass the feature set? (resolve const struct issue below first) // uiOpenTypeFeaturesForEachFunc is the type of the function
typedef uiForEach (*uiOpenTypeFeaturesForEachFunc)(char a, char b, char c, char d, uint32_t value, void *data); // invoked by uiOpenTypeFeaturesForEach() for every OpenType
// feature in otf.
typedef uiForEach (*uiOpenTypeFeaturesForEachFunc)(const uiOpenTypeFeatures *otf, char a, char b, char c, char d, uint32_t value, void *data);
// @role uiOpenTypeFeatures constructor // @role uiOpenTypeFeatures constructor
// uiNewOpenTypeFeatures() returns a new uiOpenTypeFeatures // uiNewOpenTypeFeatures() returns a new uiOpenTypeFeatures
@ -143,12 +145,11 @@ _UI_EXTERN void uiOpenTypeFeaturesRemove(uiOpenTypeFeatures *otf, char a, char b
// for a feature. You should likewise not treat a missing feature as // for a feature. You should likewise not treat a missing feature as
// having a value of zero either. Instead, a missing feature should // having a value of zero either. Instead, a missing feature should
// be treated as having some unspecified default value. // be treated as having some unspecified default value.
// TODO const-correct this function (can we do that given the members of the struct on some platforms being full blown objects that may or may not themselves be const-correct?) _UI_EXTERN int uiOpenTypeFeaturesGet(const uiOpenTypeFeatures *otf, char a, char b, char c, char d, uint32_t *value);
_UI_EXTERN int uiOpenTypeFeaturesGet(uiOpenTypeFeatures *otf, char a, char b, char c, char d, uint32_t *value);
// uiOpenTypeFeaturesForEach() executes f for every tag-value // uiOpenTypeFeaturesForEach() executes f for every tag-value
// pair in otf. The enumeration order is unspecified. // pair in otf. The enumeration order is unspecified. You cannot
// TODO make other enumerators const (and in general const-correct everything) (but see the const struct TODO below and the const struct object member TODO above) // modify otf while uiOpenTypeFeaturesForEach() is running.
_UI_EXTERN void uiOpenTypeFeaturesForEach(const uiOpenTypeFeatures *otf, uiOpenTypeFeaturesForEachFunc f, void *data); _UI_EXTERN void uiOpenTypeFeaturesForEach(const uiOpenTypeFeatures *otf, uiOpenTypeFeaturesForEachFunc f, void *data);
// uiOpenTypeFeaturesEqual() returns nonzero if a is equal to b. // uiOpenTypeFeaturesEqual() returns nonzero if a is equal to b.

View File

@ -60,9 +60,8 @@ void uiOpenTypeFeaturesRemove(uiOpenTypeFeatures *otf, char a, char b, char c, c
g_hash_table_remove(otf->tags, mkTag(a, b, c, d)); g_hash_table_remove(otf->tags, mkTag(a, b, c, d));
} }
// TODO should this be before Add and Remove? // TODO will the const wreck stuff up?
// TODO better name than Get? int uiOpenTypeFeaturesGet(const uiOpenTypeFeatures *otf, char a, char b, char c, char d, uint32_t *value)
int uiOpenTypeFeaturesGet(uiOpenTypeFeatures *otf, char a, char b, char c, char d, uint32_t *value)
{ {
gboolean found; gboolean found;
gpointer gv; gpointer gv;
@ -77,6 +76,7 @@ int uiOpenTypeFeaturesGet(uiOpenTypeFeatures *otf, char a, char b, char c, char
} }
struct otfForEach { struct otfForEach {
const uiOpenTypeFeatures *otf;
uiOpenTypeFeaturesForEachFunc f; uiOpenTypeFeaturesForEachFunc f;
void *data; void *data;
uiForEach ret; uiForEach ret;
@ -96,7 +96,7 @@ static void foreach(gpointer key, gpointer value, gpointer data)
b = (uint8_t) ((tag >> 16) & 0xFF); b = (uint8_t) ((tag >> 16) & 0xFF);
c = (uint8_t) ((tag >> 8) & 0xFF); c = (uint8_t) ((tag >> 8) & 0xFF);
d = (uint8_t) (tag & 0xFF); d = (uint8_t) (tag & 0xFF);
ofe->ret = (*(ofe->f))((char) a, (char) b, (char) c, (char) d, GPOINTER_TO_INT(value), ofe->data); ofe->ret = (*(ofe->f))(ofe->otf, (char) a, (char) b, (char) c, (char) d, GPOINTER_TO_INT(value), ofe->data);
} }
void uiOpenTypeFeaturesForEach(const uiOpenTypeFeatures *otf, uiOpenTypeFeaturesForEachFunc f, void *data) void uiOpenTypeFeaturesForEach(const uiOpenTypeFeatures *otf, uiOpenTypeFeaturesForEachFunc f, void *data)
@ -104,6 +104,7 @@ void uiOpenTypeFeaturesForEach(const uiOpenTypeFeatures *otf, uiOpenTypeFeatures
struct otfForEach ofe; struct otfForEach ofe;
memset(&ofe, 0, sizeof (struct otfForEach)); memset(&ofe, 0, sizeof (struct otfForEach));
ofe.otf = otf;
ofe.f = f; ofe.f = f;
ofe.data = data; ofe.data = data;
g_hash_table_foreach(otf->tags, foreach, &ofe); g_hash_table_foreach(otf->tags, foreach, &ofe);
@ -183,7 +184,7 @@ out:
// see https://developer.mozilla.org/en/docs/Web/CSS/font-feature-settings // see https://developer.mozilla.org/en/docs/Web/CSS/font-feature-settings
// TODO make this a g_hash_table_foreach() function (which requires duplicating code)? // TODO make this a g_hash_table_foreach() function (which requires duplicating code)?
static uiForEach toCSS(char a, char b, char c, char d, uint32_t value, void *data) static uiForEach toCSS(const uiOpenTypeFeatures *otf, char a, char b, char c, char d, uint32_t value, void *data)
{ {
// TODO is there a G_STRING()? // TODO is there a G_STRING()?
GString *s = (GString *) data; GString *s = (GString *) data;

View File

@ -45,7 +45,8 @@ void uiOpenTypeFeaturesRemove(uiOpenTypeFeatures *otf, char a, char b, char c, c
otf->tags->erase(mktag(a, b, c, d)); otf->tags->erase(mktag(a, b, c, d));
} }
int uiOpenTypeFeaturesGet(uiOpenTypeFeatures *otf, char a, char b, char c, char d, uint32_t *value) // TODO will the const wreck stuff up?
int uiOpenTypeFeaturesGet(const uiOpenTypeFeatures *otf, char a, char b, char c, char d, uint32_t *value)
{ {
tagmap::const_iterator iter; tagmap::const_iterator iter;
@ -69,7 +70,7 @@ void uiOpenTypeFeaturesForEach(const uiOpenTypeFeatures *otf, uiOpenTypeFeatures
b = (uint8_t) ((iter->first >> 8) & 0xFF); b = (uint8_t) ((iter->first >> 8) & 0xFF);
c = (uint8_t) ((iter->first >> 16) & 0xFF); c = (uint8_t) ((iter->first >> 16) & 0xFF);
d = (uint8_t) ((iter->first >> 24) & 0xFF); d = (uint8_t) ((iter->first >> 24) & 0xFF);
ret = (*f)((char) a, (char) b, (char) c, (char) d, ret = (*f)(otf, (char) a, (char) b, (char) c, (char) d,
iter->second, data); iter->second, data);
if (ret == uiForEachStop) if (ret == uiForEachStop)
return; return;