Fonts stored in base85 can contain trigraphs. #839

Closed
opened 2016-09-22 18:13:22 +00:00 by tss3000 · 4 comments
tss3000 commented 2016-09-22 18:13:22 +00:00 (Migrated from github.com)

When using

binary_to_compressed_c --base85 Cousine-Regular.ttf

the result ends up containing trigraphs like ??( witch will end up replaced to other characters on some build setups.
It might be worth adjusting or changing the base85 algorithm to something like https://github.com/maksverver/base85

Edit:

char Encode85Byte(unsigned int x) 
{
    x = (x % 85) + 35;
    switch(x)
    {
    case '\\': return 35 + 85;
    case '?':  return 35 + 86;
    case ':':  return 35 + 87;
    case '%':  return 35 + 88;
    default:   return x;
    }
}
unsigned int Decode85Byte(char c)
{
    switch (c)
    {
    case 35 + 85: return '\\' - 35;
    case 35 + 86: return '?' - 35;
    case 35 + 87: return ':' - 35;
    case 35 + 88: return '%' - 35;
    default:      return c - 35;
    }
}

When using > binary_to_compressed_c --base85 Cousine-Regular.ttf the result ends up containing trigraphs like ??( witch will end up replaced to other characters on some build setups. It might be worth adjusting or changing the base85 algorithm to something like https://github.com/maksverver/base85 Edit: ``` char Encode85Byte(unsigned int x) { x = (x % 85) + 35; switch(x) { case '\\': return 35 + 85; case '?': return 35 + 86; case ':': return 35 + 87; case '%': return 35 + 88; default: return x; } } unsigned int Decode85Byte(char c) { switch (c) { case 35 + 85: return '\\' - 35; case 35 + 86: return '?' - 35; case 35 + 87: return ':' - 35; case 35 + 88: return '%' - 35; default: return c - 35; } } ```
ocornut commented 2016-09-23 07:14:32 +00:00 (Migrated from github.com)

Nice catch. Who ever remembers about trigraphs nowadays? I'm surprised they affect string literals to be honest, Wikipedia is unclear:

The only places in a C file where two question marks in a row may be used are in multi-character constants, string literals, and comments

Then immediately following

To safely place two consecutive question marks within a string literal, the programmer can use string concatenation "...?""?..." or an escape sequence "...?\?...".

Changing the encoding isn't desirable as it would break already encoded data. Instead I added escape characters when there are pairs of ?? as suggested by the wikipedia entry:
https://en.wikipedia.org/wiki/Digraphs_and_trigraphs#C.2B.2B

Does that works for you? I didn't test with an old compiler.

Do you have problems with digraphs as well? (<: %> etc.)

Nice catch. Who ever remembers about trigraphs nowadays? I'm surprised they affect string literals to be honest, Wikipedia is unclear: `The only places in a C file where two question marks in a row may be used are in multi-character constants, string literals, and comments` Then immediately following `To safely place two consecutive question marks within a string literal, the programmer can use string concatenation "...?""?..." or an escape sequence "...?\?...".` Changing the encoding isn't desirable as it would break already encoded data. Instead I added escape characters when there are pairs of ?? as suggested by the wikipedia entry: https://en.wikipedia.org/wiki/Digraphs_and_trigraphs#C.2B.2B Does that works for you? I didn't test with an old compiler. Do you have problems with digraphs as well? (`<:` `%>` etc.)
MrSapps commented 2016-09-23 10:41:00 +00:00 (Migrated from github.com)

I think c++11 and newer remove or disable them by default

I think c++11 and newer remove or disable them by default
ocornut commented 2016-09-23 10:44:42 +00:00 (Migrated from github.com)

Yes.

Yes.
tss3000 commented 2016-09-23 20:05:17 +00:00 (Migrated from github.com)

I can confirm that the submitted changes resolve the issue. Surprisingly I stumbled onto it when using the latest https://github.com/kripken/emscripten-fastcomp-clang . I don't plan on using either trigraphs or diagraphs in any of my projects so I'll be a user of "-Werror=trigraphs" from now on.

Thanks for the help and for this amazing library.

I can confirm that the submitted changes resolve the issue. Surprisingly I stumbled onto it when using the latest https://github.com/kripken/emscripten-fastcomp-clang . I don't plan on using either trigraphs or diagraphs in any of my projects so I'll be a user of "-Werror=trigraphs" from now on. Thanks for the help and for this amazing library.
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ocornut/imgui#839
No description provided.