|
- /*
- * Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
- *
- * Xuxuxu is free software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation.
- * And when you do redistribute it or modify it, it will use either version 3 of the License, or (at yours truly opinion) any later version.
- * It is distributed in the hope that it will be useful or harmful, it really depends... But no warranty what so ever, seriously. See GNU/GPLv3.
- */
-
- #include <xolatile/xyntax.h>
- #include <xolatile/xyntax.c>
-
- #include <png.h>
-
- #define FOREGROUND (0XFFFFFFFF)
- #define BACKGROUND (0XFF000000)
-
- #define TAB_WIDTH ( 8)
- #define FONT_WIDTH ( 8)
- #define FONT_HEIGHT ( 8)
- #define FONT_COUNT (96)
-
- static unsigned int * render = NULL;
- static unsigned int colour = FOREGROUND;
-
- static int width = 0;
- static int height = 0;
- static int x = 0;
- static int y = 0;
-
- static void export_render_as_png (char * file_path) {
- png_image image = { 0 };
-
- image.version = PNG_IMAGE_VERSION;
- image.format = PNG_FORMAT_RGBA;
- image.width = (unsigned int) width;
- image.height = (unsigned int) height;
-
- fatal_failure (png_image_write_to_file (& image, file_path, 0, render, 0, NULL) == 0, "Failed to export render as PNG image!\n");
-
- png_image_free (& image);
- }
-
- static int fetch_width (char * data) {
- int image_width = 0;
- int count = 0;
-
- do {
- if (* data == '\t') {
- count += TAB_WIDTH;
- } else if (* data == '\n') {
- ++count;
- image_width = (count > image_width)
- ? count
- : image_width;
- count = 0;
- } else {
- ++count;
- }
- } while (* (++data) != '\0');
-
- return (image_width - 1);
- }
-
- static int fetch_height (char * data) {
- int image_height = 0;
-
- do {
- if (* data == '\n') {
- ++image_height;
- }
- } while (* (++data) != '\0');
-
- return (image_height + 1);
- }
-
- static void fetch_font_glyph (unsigned int * glyph, const char character) {
- const unsigned long int font_code_list [FONT_COUNT] = {
- 0X0000000000000000, 0X00180018183C3C18, 0X0000000000363636,
- 0X006C6CFE6CFE6C6C, 0X00187ED07C16FC30, 0X0060660C18306606,
- 0X00DC66B61C36361C, 0X0000000000181818, 0X0030180C0C0C1830,
- 0X000C18303030180C, 0X0000187E3C7E1800, 0X000018187E181800,
- 0X0C18180000000000, 0X000000007E000000, 0X0018180000000000,
- 0X0000060C18306000, 0X003C666E7E76663C, 0X007E181818181C18,
- 0X007E0C183060663C, 0X003C66603860663C, 0X0030307E363C3830,
- 0X003C6660603E067E, 0X003C66663E060C38, 0X000C0C0C1830607E,
- 0X003C66663C66663C, 0X001C30607C66663C, 0X0018180018180000,
- 0X0C18180018180000, 0X0030180C060C1830, 0X0000007E007E0000,
- 0X000C18306030180C, 0X001800181830663C, 0X003C06765676663C,
- 0X006666667E66663C, 0X003E66663E66663E, 0X003C66060606663C,
- 0X001E36666666361E, 0X007E06063E06067E, 0X000606063E06067E,
- 0X003C66667606663C, 0X006666667E666666, 0X007E18181818187E,
- 0X001C36303030307C, 0X0066361E0E1E3666, 0X007E060606060606,
- 0X00C6C6D6D6FEEEC6, 0X006666767E6E6666, 0X003C66666666663C,
- 0X000606063E66663E, 0X006C36566666663C, 0X006666363E66663E,
- 0X003C66603C06663C, 0X001818181818187E, 0X003C666666666666,
- 0X00183C6666666666, 0X00C6EEFED6D6C6C6, 0X0066663C183C6666,
- 0X001818183C666666, 0X007E060C1830607E, 0X003E06060606063E,
- 0X00006030180C0600, 0X007C60606060607C, 0X000000000000663C,
- 0XFFFF000000000000, 0X000000000030180C, 0X007C667C603C0000,
- 0X003E6666663E0606, 0X003C6606663C0000, 0X007C6666667C6060,
- 0X003C067E663C0000, 0X000C0C0C3E0C0C38, 0X3C607C66667C0000,
- 0X00666666663E0606, 0X003C1818181C0018, 0X0E181818181C0018,
- 0X0066361E36660606, 0X003C18181818181C, 0X00C6D6D6FE6C0000,
- 0X00666666663E0000, 0X003C6666663C0000, 0X06063E66663E0000,
- 0XE0607C66667C0000, 0X000606066E360000, 0X003E603C067C0000,
- 0X00380C0C0C3E0C0C, 0X007C666666660000, 0X00183C6666660000,
- 0X006CFED6D6C60000, 0X00663C183C660000, 0X3C607C6666660000,
- 0X007E0C18307E0000, 0X003018180E181830, 0X0018181818181818,
- 0X000C18187018180C, 0X000000000062D68C, 0X0000000000000000
- };
-
- int offset = 0;
- unsigned long int font_code = 0;
-
- font_code = font_code_list [(int) character];
-
- do {
- glyph [offset] = ((font_code >> offset) & 1)
- ? colour
- : BACKGROUND;
- } while (++offset != FONT_WIDTH * FONT_HEIGHT);
- }
-
- static void render_character (char character) {
- unsigned int glyph [FONT_WIDTH * FONT_HEIGHT] = { 0 };
-
- int offset = 0;
-
- fetch_font_glyph (glyph, character);
-
- do {
- int u = offset / FONT_WIDTH + y;
- int v = offset % FONT_WIDTH + x;
-
- render [u * width + v] = glyph [offset];
- } while (++offset != FONT_WIDTH * FONT_HEIGHT);
-
- x += FONT_WIDTH + 1;
- }
-
- static void render_string (char * string, int length) {
- int offset = 0;
-
- do {
- if (string [offset] == '\t') {
- x += FONT_WIDTH * TAB_WIDTH;
- } else if (string [offset] == '\n') {
- y += FONT_HEIGHT + 1;
- x = 1;
- } else {
- render_character (string [offset] - ' ');
- }
- } while (++offset != length);
- }
-
- int main (void) {
- int offset = 0;
- int word = 0;
- int index = 0;
- int length = 0;
- char * buffer = NULL;
-
- int preprocessor = 0;
- int line_comment = 0;
- int multiline_comment = 0;
- int character = 0;
- int string = 0;
- int bracket = 0;
- int operator = 0;
- int keyword = 0;
- int digit = 0;
- int uppercase = 0;
- int lowercase = 0;
- int underscore = 0;
-
- char separator [29] = ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n";
-
- char * c_keywords [32] = {
- "register", "volatile", "auto", "const", "static", "extern", "if", "else",
- "do", "while", "for", "continue", "switch", "case", "default", "break",
- "enum", "union", "struct", "typedef", "goto", "void", "return", "sizeof",
- "char", "short", "int", "long", "signed", "unsigned", "float", "double"
- };
-
- syntax_define (& preprocessor, 0, 0, "#", "\n", '\\', COLOUR_RED, EFFECT_BOLD);
- syntax_define (& line_comment, 0, 0, "//", "\n", '\0', COLOUR_GREY, EFFECT_BOLD);
- syntax_define (& multiline_comment, 0, 0, "/*", "*/", '\0', COLOUR_GREY, EFFECT_BOLD);
- syntax_define (& character, 0, 0, "'", "'", '\\', COLOUR_PINK, EFFECT_BOLD);
- syntax_define (& string, 0, 0, "\"", "\"", '\\', COLOUR_RED, EFFECT_BOLD);
- syntax_define (& bracket, 1, 0, "()[]{}", "", '\0', COLOUR_GREEN, EFFECT_BOLD);
- syntax_define (& operator, 1, 0, ".,:;<=>+-*/%!&~^?", "", '\0', COLOUR_BLUE, EFFECT_BOLD);
-
- for (word = 0; word != 32; ++word) {
- syntax_define (& keyword, 0, 1, c_keywords [word], separator, '\0', COLOUR_YELLOW, EFFECT_BOLD);
- }
-
- syntax_define (& digit, 1, 1, "0123456789", separator, '\0', COLOUR_CYAN, EFFECT_BOLD);
- syntax_define (& uppercase, 1, 1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", separator, '\0', COLOUR_PINK, EFFECT_ITALIC);
- syntax_define (& lowercase, 1, 1, "abcdefghijklmnopqrstuvwxyz", separator, '\0', COLOUR_WHITE, EFFECT_ITALIC);
- syntax_define (& underscore, 0, 1, "_", separator, '\0', COLOUR_YELLOW, EFFECT_ITALIC);
-
- buffer = record ();
-
- width = fetch_width (buffer) * (FONT_WIDTH + 1) + 1;
- height = fetch_height (buffer) * (FONT_HEIGHT + 1) + 1;
-
- render = allocate (4 * width * height);
-
- for (offset = 0; offset != width * height; ++offset) {
- render [offset] = BACKGROUND;
- }
-
- for (offset = 0; buffer [offset] != '\0'; offset += length) {
- syntax_select (& buffer [offset], & index, & length);
-
- switch (syntax_colour [index]) {
- case COLOUR_GREY: colour = 0XFF333333; break;
- case COLOUR_RED: colour = 0XFF3333FF; break;
- case COLOUR_GREEN: colour = 0XFF33FF33; break;
- case COLOUR_YELLOW: colour = 0XFF33FFFF; break;
- case COLOUR_BLUE: colour = 0XFFFF3333; break;
- case COLOUR_PINK: colour = 0XFFFF33FF; break;
- case COLOUR_CYAN: colour = 0XFFFFFF33; break;
- case COLOUR_WHITE: colour = 0XFFFFFFFF; break;
- default: colour = 0XFFFFFFFF; break;
- }
-
- render_string (& buffer [offset], length);
- }
-
- export_render_as_png ("xuxuxu.png");
-
- buffer = deallocate (buffer);
- render = deallocate (render);
-
- syntax_delete ();
-
- return (EXIT_SUCCESS);
- }
|