aboutsummaryrefslogtreecommitdiff
path: root/xuxuxu.c
blob: 13c036faa40a4f7a883a1d3c6833c991ddcabbd6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/// __  ___   ___  ___   ___  ___   _
/// \ \/ / | | \ \/ / | | \ \/ / | | |
///  >  <| |_| |>  <| |_| |>  <| |_| |
/// /_/\_\\__,_/_/\_\\__,_/_/\_\\__,_|
///
/// Copyright (c) 1997 - Ognjen 'xolatile' Milan Robovic
///
/// xolatile@chud.cyou - xuxuxu - Braindead simple C source code renderer to PNG image in TempleOS style.
///
/// This program is free software, free as in freedom and as in free beer, you can redistribute it and/or modify it under the terms of the GNU
/// General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version if you wish...
///
/// This program is distributed in the hope that it will be useful, but it is probably not, and without any warranty, without even the implied
/// warranty of merchantability or fitness for a particular purpose, because it is pointless. Please see the GNU (Geenoo) General Public License
/// for more details, if you dare, it is a lot of text that nobody wants to read...

#define use_png_library

#include "xtandard.h"
#include "xyntax.h"
#include "xormat.h"

#define foreground (0xffffffffu)
#define background (0xff000000u)

#define font_indent ( 8)
#define font_width  ( 8)
#define font_height ( 8)
#define font_count  (96)

static natural * render = null;
static natural   colour = foreground;

static natural width  = 0;
static natural height = 0;
static natural x      = 0;
static natural y      = 0;

static procedure render_character (character code) {
	natural_64 font_code [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
	};

	for (natural offset = 0; offset < font_width * font_height; ++offset) {
		natural u = offset / font_width + y;
		natural v = offset % font_width + x;

		render [u * width + v] = ((font_code [(natural) (code - ' ')] >> offset) % 2) ? colour : background;
	}

	x += font_width + 1;
}

static procedure render_string (character * string, natural length) {
	for (natural offset = 0; offset < length; ++offset) {
		if (string [offset] == '\t') {
			x += font_width * font_indent;
		} else if (string [offset] == '\n') {
			y += font_height + 1;
			x  = 1;
		} else {
			render_character (string [offset]);
		}
	}
}

integer main (none) {
	natural     index  = 0;
	natural     length = 0;
	character * buffer = null;

	character separator [] = ".,:;<=>+-*/%!&~^()[]{}'\" \t\r\n";

	character * keywords [] = {
		"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_structure * syntax = syntax_initialize (0);

	syntax_define (syntax, false, false, "#",                  "\n", '\\', 0xff00ff00, 0);
	syntax_define (syntax, false, false, "//",                 "\n", '\0', 0xff777777, 0);
	syntax_define (syntax, false, false, "/*",                 "*/", '\0', 0xff777777, 0);
	syntax_define (syntax, false, false, "'",                  "'",  '\\', 0xff0000ff, 0);
	syntax_define (syntax, false, false, "\"",                 "\"", '\\', 0xff0000ff, 0);
	syntax_define (syntax, true,  false, ".,:;<=>+-*/%!&~^?|", "",   '\0', 0xffffff00, 0);
	syntax_define (syntax, true,  false, "(){}[]",             "",   '\0', 0xffff0000, 0);

	for (natural_64 word = 0; word < array_length (keywords); ++word) {
		syntax_define (syntax, false, true, keywords [word], separator, '\0', 0xff00ffff, 0);
	}

	syntax_define (syntax, true, true, "0123456789",                                            separator, '\0', 0xffff00ff, 0);
	syntax_define (syntax, true, true, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_", separator, '\0', 0xffffffff, 0);

	buffer = record ();

	width  = string_full_width  (buffer, font_indent) * (font_width  + 1) + 1;
	height = string_full_height (buffer)              * (font_height + 1) + 1;

	render = allocate (4 * width * height);

	for (natural offset = 0; offset < width * height; ++offset) {
		render [offset] = background;
	}

	for (natural offset = 0; buffer [offset] != '\0'; offset += length) {
		index = syntax_select (syntax, & buffer [offset], & length);

		colour = syntax->colour [index];

		render_string (& buffer [offset], length);
	}

	png_image_export ("xuxuxu.png", render, width, height);

	syntax = syntax_deinitialize (syntax);

	buffer = deallocate (buffer);
	render = deallocate (render);

	return (log_success);
}