xuxuxu lol
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
6.8KB

  1. /*
  2. * Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
  3. *
  4. * Xuxuxu is free software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation.
  5. * 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.
  6. * 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.
  7. */
  8. #include <xolatile/xtandard.c>
  9. #include <xolatile/xyntax.c>
  10. #include <png.h>
  11. #define FOREGROUND (0XFFCCCCCC)
  12. #define BACKGROUND (0XFF111111)
  13. #define TAB_WIDTH ( 8)
  14. #define FONT_WIDTH ( 8)
  15. #define FONT_HEIGHT ( 8)
  16. #define FONT_COUNT (96)
  17. static unsigned int * render = NULL;
  18. static unsigned int colour = FOREGROUND;
  19. static int width = 0;
  20. static int height = 0;
  21. static int x = 0;
  22. static int y = 0;
  23. static void export_render_as_png (char * file_path) {
  24. png_image image = { 0 };
  25. image.version = PNG_IMAGE_VERSION;
  26. image.format = PNG_FORMAT_RGBA;
  27. image.width = (unsigned int) width;
  28. image.height = (unsigned int) height;
  29. fatal_failure (png_image_write_to_file (& image, file_path, 0, render, 0, NULL) == 0, "Failed to export render as PNG image!\n");
  30. png_image_free (& image);
  31. }
  32. static int fetch_width (char * data) {
  33. int image_width = 0;
  34. int count = 0;
  35. do {
  36. if (* data == '\t') {
  37. count += TAB_WIDTH;
  38. } else if (* data == '\n') {
  39. image_width = (++count > image_width) ? count : image_width;
  40. count = 0;
  41. } else {
  42. ++count;
  43. }
  44. } while (* (++data) != '\0');
  45. return (image_width - 1);
  46. }
  47. static int fetch_height (char * data) {
  48. int image_height = 0;
  49. do {
  50. if (* data == '\n') {
  51. ++image_height;
  52. }
  53. } while (* (++data) != '\0');
  54. return (image_height + 1);
  55. }
  56. static void render_character (char character) {
  57. const unsigned long int font_code [FONT_COUNT] = {
  58. 0X0000000000000000, 0X00180018183C3C18, 0X0000000000363636, 0X006C6CFE6CFE6C6C, 0X00187ED07C16FC30, 0X0060660C18306606, 0X00DC66B61C36361C, 0X0000000000181818,
  59. 0X0030180C0C0C1830, 0X000C18303030180C, 0X0000187E3C7E1800, 0X000018187E181800, 0X0C18180000000000, 0X000000007E000000, 0X0018180000000000, 0X0000060C18306000,
  60. 0X003C666E7E76663C, 0X007E181818181C18, 0X007E0C183060663C, 0X003C66603860663C, 0X0030307E363C3830, 0X003C6660603E067E, 0X003C66663E060C38, 0X000C0C0C1830607E,
  61. 0X003C66663C66663C, 0X001C30607C66663C, 0X0018180018180000, 0X0C18180018180000, 0X0030180C060C1830, 0X0000007E007E0000, 0X000C18306030180C, 0X001800181830663C,
  62. 0X003C06765676663C, 0X006666667E66663C, 0X003E66663E66663E, 0X003C66060606663C, 0X001E36666666361E, 0X007E06063E06067E, 0X000606063E06067E, 0X003C66667606663C,
  63. 0X006666667E666666, 0X007E18181818187E, 0X001C36303030307C, 0X0066361E0E1E3666, 0X007E060606060606, 0X00C6C6D6D6FEEEC6, 0X006666767E6E6666, 0X003C66666666663C,
  64. 0X000606063E66663E, 0X006C36566666663C, 0X006666363E66663E, 0X003C66603C06663C, 0X001818181818187E, 0X003C666666666666, 0X00183C6666666666, 0X00C6EEFED6D6C6C6,
  65. 0X0066663C183C6666, 0X001818183C666666, 0X007E060C1830607E, 0X003E06060606063E, 0X00006030180C0600, 0X007C60606060607C, 0X000000000000663C, 0XFFFF000000000000,
  66. 0X000000000030180C, 0X007C667C603C0000, 0X003E6666663E0606, 0X003C6606663C0000, 0X007C6666667C6060, 0X003C067E663C0000, 0X000C0C0C3E0C0C38, 0X3C607C66667C0000,
  67. 0X00666666663E0606, 0X003C1818181C0018, 0X0E181818181C0018, 0X0066361E36660606, 0X003C18181818181C, 0X00C6D6D6FE6C0000, 0X00666666663E0000, 0X003C6666663C0000,
  68. 0X06063E66663E0000, 0XE0607C66667C0000, 0X000606066E360000, 0X003E603C067C0000, 0X00380C0C0C3E0C0C, 0X007C666666660000, 0X00183C6666660000, 0X006CFED6D6C60000,
  69. 0X00663C183C660000, 0X3C607C6666660000, 0X007E0C18307E0000, 0X003018180E181830, 0X0018181818181818, 0X000C18187018180C, 0X000000000062D68C, 0X0000000000000000
  70. };
  71. int offset;
  72. for (offset = 0; offset != FONT_WIDTH * FONT_HEIGHT; ++offset) {
  73. int u = offset / FONT_WIDTH + y;
  74. int v = offset % FONT_WIDTH + x;
  75. render [u * width + v] = ((font_code [(int) (character - ' ')] >> offset) % 2) ? colour : BACKGROUND;
  76. }
  77. x += FONT_WIDTH + 1;
  78. }
  79. static void render_string (char * string, int length) {
  80. int offset;
  81. for (offset = 0; offset != length; ++offset) {
  82. if (string [offset] == '\t') {
  83. x += FONT_WIDTH * TAB_WIDTH;
  84. } else if (string [offset] == '\n') {
  85. y += FONT_HEIGHT + 1;
  86. x = 1;
  87. } else {
  88. render_character (string [offset]);
  89. }
  90. }
  91. }
  92. int main (void) {
  93. int offset = 0;
  94. int word = 0;
  95. int index = 0;
  96. int length = 0;
  97. char * buffer = NULL;
  98. char separator [29] = ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n";
  99. char * c_keywords [32] = {
  100. "register", "volatile", "auto", "const", "static", "extern", "if", "else",
  101. "do", "while", "for", "continue", "switch", "case", "default", "break",
  102. "enum", "union", "struct", "typedef", "goto", "void", "return", "sizeof",
  103. "char", "short", "int", "long", "signed", "unsigned", "float", "double"
  104. };
  105. (void) syntax_define (0, 0, "#", "\n", '\\', COLOUR_PINK, 0);
  106. (void) syntax_define (0, 0, "//", "\n", '\0', COLOUR_GREY, 0);
  107. (void) syntax_define (0, 0, "/*", "*/", '\0', COLOUR_GREY, 0);
  108. (void) syntax_define (0, 0, "'", "'", '\\', COLOUR_RED, 0);
  109. (void) syntax_define (0, 0, "\"", "\"", '\\', COLOUR_RED, 0);
  110. (void) syntax_define (1, 0, ".,:;<=>+-*/%!&~^?|()[]{}", "", '\0', COLOUR_BLUE, 0);
  111. for (word = 0; word != 32; ++word) {
  112. (void) syntax_define (0, 1, c_keywords [word], separator, '\0', COLOUR_YELLOW, 0);
  113. }
  114. (void) syntax_define (1, 1, "0123456789", separator, '\0', COLOUR_CYAN, 0);
  115. (void) syntax_define (1, 1, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_", separator, '\0', COLOUR_WHITE, 0);
  116. buffer = record ();
  117. width = fetch_width (buffer) * (FONT_WIDTH + 1) + 1;
  118. height = fetch_height (buffer) * (FONT_HEIGHT + 1) + 1;
  119. render = allocate (4 * width * height);
  120. for (offset = 0; offset != width * height; ++offset) {
  121. render [offset] = BACKGROUND;
  122. }
  123. for (offset = 0; buffer [offset] != '\0'; offset += length) {
  124. index = syntax_select (& buffer [offset], & length);
  125. switch (syntax_colour [index]) {
  126. case COLOUR_GREY: colour = 0XFF333333; break;
  127. case COLOUR_RED: colour = 0XFF3311CC; break;
  128. case COLOUR_GREEN: colour = 0XFF33CC11; break;
  129. case COLOUR_YELLOW: colour = 0XFF33CCCC; break;
  130. case COLOUR_BLUE: colour = 0XFFCC3311; break;
  131. case COLOUR_PINK: colour = 0XFFCC33CC; break;
  132. case COLOUR_CYAN: colour = 0XFFCCCC33; break;
  133. case COLOUR_WHITE: colour = 0XFFCCCCCC; break;
  134. default: colour = 0XFFCCCCCC; break;
  135. }
  136. render_string (& buffer [offset], length);
  137. }
  138. export_render_as_png ("xuxuxu.png");
  139. buffer = deallocate (buffer);
  140. render = deallocate (render);
  141. syntax_delete ();
  142. return (EXIT_SUCCESS);
  143. }