#include #include /* Global buffer of characters */ typedef struct { int x, y; /* Character position */ int code; /* Character code point */ } Char; static Char *characters = NULL; static int character_count = 0; static int character_capacity = 1; /* MergeSort all characters by position */ static void MergeSort(int x, int y) { int m = (x + y) / 2; int i, j, r, w; if( m == x ) return; MergeSort(x, m); MergeSort(m, y); for(i = x, j = m, w = character_count + x; w < character_count + y; w++) { if( i < m ) { if( j < y ) { if( characters[i].y < characters[j].y || (characters[i].y == characters[j].y && characters[i].x < characters[j].x) ) { r = i++; } else { r = j++; } } else { r = i++; } } else { r = j++; } characters[w].x = characters[r].x; characters[w].y = characters[r].y; characters[w].code = characters[r].code; } for(w = x, r = character_count + x; w < y; r++, w++) { characters[w].x = characters[r].x; characters[w].y = characters[r].y; characters[w].code = characters[r].code; } } /* Add a single character to global buffer */ static void AddChar(int x, int y, int code) { /* Since we always double the amount of memory allocated when the next character reaches capacity, we always have at least twice as much memory as number of characters loaded. This means we already have the spare space needed for MergeSort. */ if( character_count + 1 >= character_capacity ) { character_capacity *= 2; characters = (Char*)realloc(characters, character_capacity * sizeof(Char)); if( characters == NULL ) { fputs("Out of memory\n", stderr); exit(EXIT_FAILURE); } } characters[character_count].x = x; characters[character_count].y = y; characters[character_count].code = code; character_count++; } /* Read UTF-8 bytes */ static int ReadUTF8(FILE *infile, int count) { int c = 0; for(; count > 0; count--) c = (c << 6) | (fgetc(infile) & 0x3f); return c; } /* Write UTF-8 bytes */ static void WriteUTF8(int code, FILE *outfile) { if( code < 0x80 ) { fputc(code, outfile); } else if( code < (1 << (5 + 6)) ) { fputc(0xc0 | ((code >> 6) & 0x1f), outfile); fputc(0x80 | (code & 0x3f), outfile); } else if( code < (1 << (4 + 6 + 6)) ) { fputc(0xe0 | ((code >> 12) & 0x0f), outfile); fputc(0x80 | ((code >> 6) & 0x3f), outfile); fputc(0x80 | (code & 0x3f), outfile); } else { fputc(0xf0 | ((code >> 18) & 0x07), outfile); fputc(0x80 | ((code >> 12) & 0x3f), outfile); fputc(0x80 | ((code >> 6) & 0x3f), outfile); fputc(0x80 | (code & 0x3f), outfile); } } /* Get width of character, returns 1 for half width and 2 for full width */ static int GetCharWidth(int c) { static const int ranges[11][2] = { {0x1100, 0x115f}, {0x2329, 0x232a}, {0x2e80, 0x303e}, {0x3040, 0xa4cf}, {0xac00, 0xd7a3}, {0xf900, 0xfaff}, {0xfe10, 0xfe19}, {0xfe30, 0xfe6f}, {0xff00, 0xff60}, {0xffe0, 0xffe6}, {0x20000, 0x3fffd} }; int i; for(i = 0; i < 11; i++) { if( c >= ranges[i][0] && c <= ranges[i][1] ) return 2; } return 1; } /* Update cursor based on escape sequence */ static void UpdateCursor(FILE *infile, int *x, int *y) { int param = 0; int c; while( (c = fgetc(infile)) != EOF ) { if( c >= '0' && c <= '9' ) { param = param * 10 + c - '0'; } else if( c == ';' ) { /* Delimiter used for multi-param sequences (such as colors), but we don't care about those. */ } else if( c == 'A' ) { /* Cursor up */ *y -= param; return; } else if( c == 'B' ) { /* Cursor down */ *y += param; return; } else if( c == 'C' ) { /* Cursor forward */ *x += param; return; } else if( c == 'D' ) { /* Cursor backward */ *x -= param; if( *x < 0 ) *x = 0; return; } else { /* Some escape sequence that we don't care about, silently ignored */ return; } } } /* Load input characters to global buffer */ static void LoadInput(FILE *infile) { int c, x = 0, y = 0; int state = 0; /* 0 = normal, 1 = seen ESC */ while( (c = fgetc(infile)) != EOF ) { if( state != 0 ) { if( c == '[' ) { /* Escape sequence */ UpdateCursor(infile, &x, &y); continue; } state = 0; } if( c == 0x1b ) { /* Start of escape sequence */ state = 1; continue; } else if( (c & 0xe0) == 0xc0 ) { /* 2-byte UTF-8 sequence */ c = ((c & 0x1f) << 6) | ReadUTF8(infile, 1); AddChar(x, y, c); x += GetCharWidth(c); } else if( (c & 0xf0) == 0xe0 ) { /* 3-byte UTF-8 sequence */ c = ((c & 0x0f) << 12) | ReadUTF8(infile, 2); AddChar(x, y, c); x += GetCharWidth(c); } else if( (c & 0xf8) == 0xf0 ) { /* 4-byte UTF-8 sequence */ c = ((c & 0x07) << 18) | ReadUTF8(infile, 3); AddChar(x, y, c); x += GetCharWidth(c); } else if( c == '\n' ) { /* Newline: update cursor but don't buffer character */ y++; x = 0; } else if( c == ' ' ) { /* Space: update cursor but don't buffer character */ x++; } else if( c == '\t' ) { /* Tab: update cursor but don't buffer character */ for(x++; x % 8; x++); } else if( c < 32 ) { /* Other control characters (ignored) */ } else { /* Single byte character */ AddChar(x++, y, c); } } } /* Output characters */ static void Output(FILE *outfile) { int i, x, y, min_y; /* Do nothing if we didn't read any characters at all */ if( characters == NULL ) return; x = y = 0; /* Determine Y offset, in case if input tries to scroll past upper edge */ for(i = min_y = 0; i < character_count; i++) min_y = min_y < characters[i].y ? min_y : characters[i].y; /* Write one sentinel character to mark the end. This for seeking one character ahead when checking duplicates. We don't have to worry about out of bounds writes since the buffer is always at least twice as large as number of characters. */ characters[character_count].x = -1; for(i = 0; i < character_count; i++) { while( y < characters[i].y ) { fputc('\n', outfile); y++; x = 0; } while( x < characters[i].x ) { fputc(' ', outfile); x++; } /* Seek ahead in case if there are overlaps. We want to take the last character that is drawn at this position to simulate overlaps. */ while( characters[i + 1].y == characters[i].y && characters[i + 1].x == characters[i].x ) { i++; } WriteUTF8(characters[i].code, outfile); x += GetCharWidth(characters[i].code); } /* Always end with newline */ if( x > 0 ) fputc('\n', outfile); } int main(int argc, char **argv) { LoadInput(stdin); MergeSort(0, character_count); Output(stdout); return 0; }