Created
July 2, 2019 10:49
-
-
Save ctrlcctrlv/29c228bb5d05e93e3abf35d1615a76ed to your computer and use it in GitHub Desktop.
Combining class name functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| char * CombiningClassName(uint32 cc) { | |
| const int COMBININGCLASS_COUNT = 14; | |
| char * comb_classes[COMBININGCLASS_COUNT]; | |
| int i = 0; | |
| if (cc & FF_UNICODE_COMBININGCLASS) { | |
| comb_classes[i] = "COMBININGCLASS"; | |
| ++i; | |
| } | |
| if (cc & FF_UNICODE_ABOVE) { | |
| comb_classes[i] = "ABOVE"; | |
| ++i; | |
| } | |
| if (cc & FF_UNICODE_BELOW) { | |
| comb_classes[i] = "BELOW"; | |
| ++i; | |
| } | |
| if (cc & FF_UNICODE_OVERSTRIKE) { | |
| comb_classes[i] = "OVERSTRIKE"; | |
| ++i; | |
| } | |
| if (cc & FF_UNICODE_LEFT) { | |
| comb_classes[i] = "LEFT"; | |
| ++i; | |
| } | |
| if (cc & FF_UNICODE_RIGHT) { | |
| comb_classes[i] = "RIGHT"; | |
| ++i; | |
| } | |
| if (cc & FF_UNICODE_JOINS2) { | |
| comb_classes[i] = "JOINS2"; | |
| ++i; | |
| } | |
| if (cc & FF_UNICODE_CENTERLEFT) { | |
| comb_classes[i] = "CENTERLEFT"; | |
| ++i; | |
| } | |
| if (cc & FF_UNICODE_CENTERRIGHT) { | |
| comb_classes[i] = "CENTERRIGHT"; | |
| ++i; | |
| } | |
| if (cc & FF_UNICODE_CENTEREDOUTSIDE) { | |
| comb_classes[i] = "CENTEREDOUTSIDE"; | |
| ++i; | |
| } | |
| if (cc & FF_UNICODE_OUTSIDE) { | |
| comb_classes[i] = "OUTSIDE"; | |
| ++i; | |
| } | |
| if (cc & FF_UNICODE_LEFTEDGE) { | |
| comb_classes[i] = "LEFTEDGE"; | |
| ++i; | |
| } | |
| if (cc & FF_UNICODE_RIGHTEDGE) { | |
| comb_classes[i] = "RIGHTEDGE"; | |
| ++i; | |
| } | |
| if (cc & FF_UNICODE_TOUCHING) { | |
| comb_classes[i] = "TOUCHING"; | |
| ++i; | |
| } | |
| if (cc == 0) { | |
| // Even though this is the last member, the `++i` is still needed because otherwise | |
| // loop below won't know there's anything in the array. | |
| comb_classes[i] = "BASE"; | |
| ++i; | |
| } | |
| const int MAX_COMBCLASSNAME_LEN = 15; | |
| char * ret = calloc((MAX_COMBCLASSNAME_LEN * COMBININGCLASS_COUNT) + i + 1, sizeof(char)); | |
| for (int j = 0; j < i; j++) { | |
| strcat(ret, comb_classes[j]); | |
| if (j < i - 1) { | |
| strcat(ret, "&"); | |
| } | |
| } | |
| return ret; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| inp = """#define FF_UNICODE_COMBININGCLASS 0xff | |
| #define FF_UNICODE_ABOVE 0x100 | |
| #define FF_UNICODE_BELOW 0x200 | |
| #define FF_UNICODE_OVERSTRIKE 0x400 | |
| #define FF_UNICODE_LEFT 0x800 | |
| #define FF_UNICODE_RIGHT 0x1000 | |
| #define FF_UNICODE_JOINS2 0x2000 | |
| #define FF_UNICODE_CENTERLEFT 0x4000 | |
| #define FF_UNICODE_CENTERRIGHT 0x8000 | |
| #define FF_UNICODE_CENTEREDOUTSIDE 0x10000 | |
| #define FF_UNICODE_OUTSIDE 0x20000 | |
| #define FF_UNICODE_LEFTEDGE 0x80000 | |
| #define FF_UNICODE_RIGHTEDGE 0x40000 | |
| #define FF_UNICODE_TOUCHING 0x100000""" | |
| inp = inp.replace("#define ", "") | |
| inp = inp.splitlines() | |
| inp = [tuple(filter(None, i.split())) for i in inp] | |
| HEAD = """const int COMBININGCLASS_COUNT = {}; | |
| char* comb_classes[COMBININGCLASS_COUNT]; | |
| int i = 0;""".format(len(inp)) | |
| OUTFORMAT = """ | |
| if (cc & {}) {{ | |
| comb_classes[i] = "{}"; ++i; | |
| }}""" | |
| m = max([len(name.split('_')[-1]) for name, val in inp]) | |
| FOOT = """ | |
| const int MAX_COMBCLASSNAME_LEN = {}; | |
| char* ret = calloc((MAX_COMBCLASSNAME_LEN * COMBININGCLASS_COUNT) + i + 1, sizeof(char)); | |
| for (int j=0; j < i; j++) {{ | |
| strcat(ret, comb_classes[j]); | |
| if (j < i-1) {{ | |
| strcat(ret, "&"); | |
| }} | |
| }} | |
| return ret;""".format(m) | |
| print(HEAD) | |
| for name, val in inp: | |
| print( | |
| OUTFORMAT.format(name, name.split('_')[-1]) | |
| ) | |
| print(FOOT) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment