makeshot.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. #include <math.h>
  2. #include <cairo/cairo.h>
  3. #include <pango/pangocairo.h>
  4. #define NICKNAME_TEXT_SIZE 23
  5. #define MESSAGE_TEXT_SIZE 24
  6. #define HPADDING 18
  7. #define VPADDING 8
  8. #define AVATAR_OFFSET 3
  9. #define AVATAR_PADDING 12
  10. #define AVATAR_DIM 75
  11. #define AVATAR_WIDTH (AVATAR_DIM + AVATAR_PADDING)
  12. #define TAIL_WIDTH 18
  13. #define TAIL_HEIGHT 30
  14. #define MESSAGE_BOX_RADIUS 8
  15. #define MESSAGE_BOX_MIN 160
  16. #define WRAP_WIDTH 400
  17. #define TAIL_PATH "./resources/tail.png"
  18. #define MAX(x, y) ((x) >= (y)? (x): (y))
  19. #define MIN(x, y) ((x) <= (y)? (x): (y))
  20. typedef unsigned int uint_t;
  21. typedef struct {
  22. int r, g, b;
  23. } color_t;
  24. color_t COLORS_TABLE[] = {
  25. /* Nickname colors */
  26. {0xee, 0x49, 0x28}, /* 0 */
  27. {0x41, 0xa9, 0x03}, /* 1 */
  28. {0xe0, 0x96, 0x02}, /* 2 */
  29. {0x0f, 0x94, 0xed}, /* 3 */
  30. {0x8f, 0x3b, 0xf7}, /* 4 */
  31. {0xfc, 0x43, 0x80}, /* 5 */
  32. {0x00, 0xa1, 0xc4}, /* 6 */
  33. {0xeb, 0x70, 0x02}, /* 7 */
  34. /* Message text color */
  35. {0xff, 0xff, 0xff}, /* 8 */
  36. /* Message box color */
  37. {0x2b, 0x2b, 0x2b} /* 9 */
  38. };
  39. #define COLOR(i) (&COLORS_TABLE[i])
  40. #define C_MESSAGE_TEXT COLOR(8)
  41. #define C_MESSAGE_BOX COLOR(9)
  42. typedef struct {
  43. PangoLayout *layout;
  44. uint_t w, h;
  45. } text_t;
  46. typedef struct {
  47. unsigned char *data;
  48. size_t size, allocated;
  49. } buffer_t;
  50. void *malloc_protected(size_t z) {
  51. void *p;
  52. if (!(p = malloc(z))) {
  53. fputs("out of memory.\n", stderr);
  54. exit(1);
  55. }
  56. return p;
  57. }
  58. void *realloc_protected(void *old_p, size_t z) {
  59. void *p;
  60. if (!(p = realloc(old_p, z))) {
  61. fputs("out of memory.\n", stderr);
  62. exit(1);
  63. }
  64. return p;
  65. }
  66. buffer_t *new_buffer() {
  67. buffer_t *buffer;
  68. buffer = malloc_protected(sizeof(buffer_t));
  69. buffer->data = NULL;
  70. buffer->size = 0;
  71. buffer->allocated = 0;
  72. return buffer;
  73. }
  74. void free_buffer(buffer_t *buffer) {
  75. free(buffer->data);
  76. free(buffer);
  77. }
  78. void buffer_expand(buffer_t *buffer, size_t size) {
  79. buffer->allocated += size;
  80. buffer->data = realloc_protected(buffer->data, buffer->allocated);
  81. }
  82. void buffer_append_data(buffer_t *buffer, unsigned char *data, size_t data_length) {
  83. size_t tail = buffer->size;
  84. buffer->size += data_length;
  85. if (buffer->size > buffer->allocated)
  86. buffer_expand(buffer, data_length);
  87. for (size_t i = 0; i < data_length; i++)
  88. buffer->data[tail + i] = data[i];
  89. }
  90. #define BUFFER_APPEND(b, s) buffer_append_data(b, s, strlen(s))
  91. #define BUFFER_CONCAT(b1, b2) buffer_append_data(b1, b2->data, b2->size)
  92. #define BUFFER_PREPROCESS(b1, b2) do {\
  93. buffer_append_data(b2, "\0", 1);\
  94. preprocess_text(b1, b2->data);\
  95. } while(0)
  96. void buffer_append_escaped(buffer_t *buffer, char c) {
  97. switch (c) {
  98. case '<':
  99. BUFFER_APPEND(buffer, "&lt;");
  100. return;
  101. case '>':
  102. BUFFER_APPEND(buffer, "&gt;");
  103. return;
  104. case '&':
  105. BUFFER_APPEND(buffer, "&amp;");
  106. return;
  107. case '"':
  108. BUFFER_APPEND(buffer, "&quot;");
  109. return;
  110. case '\'':
  111. BUFFER_APPEND(buffer, "&#39;");
  112. return;
  113. }
  114. buffer_append_data(buffer, &c, 1);
  115. }
  116. #define SET(s,tag,pr) do {\
  117. buffer_t *temp_buffer;\
  118. uint_t k;\
  119. temp_buffer = new_buffer();\
  120. k = strlen(s);\
  121. i += k;\
  122. while (text[i]) {\
  123. if (strncmp(&text[i], s, k) == 0) {\
  124. i += k-1;\
  125. buffer_append_data(buffer, "<", 1);\
  126. BUFFER_APPEND(buffer, tag);\
  127. buffer_append_data(buffer, ">", 1);\
  128. if (pr)\
  129. BUFFER_PREPROCESS(buffer, temp_buffer);\
  130. else\
  131. BUFFER_CONCAT(buffer, temp_buffer);\
  132. buffer_append_data(buffer, "</", 2);\
  133. BUFFER_APPEND(buffer, tag);\
  134. buffer_append_data(buffer, ">", 1);\
  135. free_buffer(temp_buffer);\
  136. goto escape;\
  137. }\
  138. buffer_append_escaped(temp_buffer, text[i]);\
  139. i++;\
  140. }\
  141. i--;\
  142. BUFFER_APPEND(buffer, s);\
  143. BUFFER_PREPROCESS(buffer, temp_buffer);\
  144. free_buffer(temp_buffer);\
  145. } while(0)
  146. void preprocess_text(buffer_t *buffer, char *text) {
  147. for (size_t i = 0; text[i]; i++) {
  148. unsigned char c, nc, fc;
  149. c = text[i];
  150. nc = text[i+1];
  151. fc = text[i+2];
  152. if (c == '`')
  153. SET("`", "tt", 0);
  154. else if (c == '*' && nc == '*')
  155. SET("**", "b", 1);
  156. else if (c == '_' && nc == '_')
  157. SET("__", "i", 1);
  158. else if (c == '~' && nc == '~')
  159. SET("~~", "s", 1);
  160. else if (c == 0xee && nc == 0x80 && fc == 0x80)
  161. SET("\xee\x80\x80", "u", 1);
  162. else
  163. buffer_append_escaped(buffer, c);
  164. escape:;
  165. }
  166. }
  167. text_t *new_text(cairo_t *cr, char *s, int size, int weight, int preprocess) {
  168. text_t *text;
  169. PangoFontDescription *font_description;
  170. buffer_t *buffer;
  171. text = malloc_protected(sizeof(text_t));
  172. font_description = pango_font_description_from_string("Apple Color Emoji 16,Open Sans 16");
  173. pango_font_description_set_absolute_size(font_description, size * PANGO_SCALE);
  174. pango_font_description_set_weight(font_description, weight);
  175. text->layout = pango_cairo_create_layout(cr);
  176. pango_layout_set_font_description(text->layout, font_description);
  177. pango_layout_set_wrap(text->layout, PANGO_WRAP_WORD);
  178. pango_layout_set_width(text->layout, WRAP_WIDTH * PANGO_SCALE);
  179. pango_font_description_free(font_description);
  180. if (preprocess) {
  181. buffer = new_buffer();
  182. preprocess_text(buffer, s);
  183. pango_layout_set_markup(text->layout, buffer->data, buffer->size);
  184. free_buffer(buffer);
  185. } else
  186. pango_layout_set_text(text->layout, s, -1);
  187. pango_layout_get_pixel_size(text->layout, &text->w, &text->h);
  188. return text;
  189. }
  190. void free_text(text_t *text) {
  191. g_object_unref(text->layout);
  192. free(text);
  193. }
  194. /* TO-DO: do this better */
  195. void get_text_size(char *s, uint_t size, uint_t weight, uint_t *w, uint_t *h, int preprocess) {
  196. cairo_surface_t *surface;
  197. cairo_t *cr;
  198. surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1);
  199. cr = cairo_create(surface);
  200. text_t *text = new_text(cr, s, size, weight, preprocess);
  201. *w = text->w;
  202. *h = text->h;
  203. free_text(text);
  204. cairo_destroy(cr);
  205. cairo_surface_destroy(surface);
  206. }
  207. void select_color(cairo_t *cr, color_t *color) {
  208. cairo_set_source_rgb(cr, color->r / 255.0F, color->g / 255.0F, color->b / 255.0F);
  209. }
  210. void paste_clipped_image(cairo_t *cr, uint_t x, uint_t y, char *path) {
  211. cairo_surface_t *surface, *surface2;
  212. cairo_t *ic;
  213. surface = cairo_image_surface_create_from_png(path);
  214. ic = cairo_create(surface);
  215. cairo_scale(ic, ((double)AVATAR_DIM) / cairo_image_surface_get_width(surface), ((double)AVATAR_DIM) / cairo_image_surface_get_height(surface));
  216. cairo_set_source_surface(ic, surface, 0, 0);
  217. cairo_paint(ic);
  218. cairo_destroy(ic);
  219. surface2 = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, AVATAR_DIM, AVATAR_DIM);
  220. ic = cairo_create(surface2);
  221. cairo_arc(ic, AVATAR_DIM/2, AVATAR_DIM/2, AVATAR_DIM/2, 0, 2*M_PI);
  222. cairo_clip(ic);
  223. cairo_new_path(ic);
  224. cairo_set_source_surface(ic, surface, 0, 0);
  225. cairo_paint(ic);
  226. cairo_set_source_surface(cr, surface2, x, y);
  227. cairo_paint(cr);
  228. cairo_destroy(ic);
  229. cairo_surface_destroy(surface);
  230. cairo_surface_destroy(surface2);
  231. }
  232. void rounded_rectangle(cairo_t *cr, uint_t x, uint_t y, uint_t w, uint_t h, uint_t r) {
  233. cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
  234. cairo_new_sub_path(cr);
  235. cairo_arc(cr, x + r, y + r, r, M_PI, 3 * M_PI / 2);
  236. cairo_arc(cr, x + w - r, y + r, r, 3 *M_PI / 2, 2 * M_PI);
  237. cairo_arc(cr, x + w - r, y + h - r, r, 0, M_PI / 2);
  238. cairo_arc(cr, x + r, y + h - r, r, M_PI / 2, M_PI);
  239. cairo_close_path(cr);
  240. cairo_fill(cr);
  241. }
  242. void text(cairo_t *cr, uint_t x, uint_t y, text_t *text) {
  243. cairo_move_to(cr, x, y);
  244. pango_cairo_update_layout(cr, text->layout);
  245. pango_cairo_show_layout(cr, text->layout);
  246. }
  247. void paste_image(cairo_t *cr, uint_t x, uint_t y, char *path) {
  248. cairo_surface_t *surface;
  249. surface = cairo_image_surface_create_from_png(path);
  250. cairo_set_source_surface(cr, surface, x, y);
  251. cairo_paint(cr);
  252. cairo_surface_destroy(surface);
  253. }
  254. void contain(cairo_surface_t **surface, uint_t width, uint_t height) {
  255. uint_t image_width, image_height;
  256. uint_t new_width, new_height;
  257. double image_ratio, new_ratio;
  258. cairo_surface_t *surface2;
  259. cairo_t *cr;
  260. image_width = cairo_image_surface_get_width(*surface);
  261. image_height = cairo_image_surface_get_height(*surface);
  262. new_width = width;
  263. new_height = height;
  264. image_ratio = ((double)image_width) / ((double)image_height);
  265. new_ratio = ((double)width) / ((double)height);
  266. if (image_ratio > new_ratio)
  267. new_height = ((double)image_height) / ((double)image_width) * ((double)width);
  268. else
  269. new_width = ((double)image_width) / ((double)image_height) * ((double)height);
  270. surface2 = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, new_width, new_height);
  271. cr = cairo_create(surface2);
  272. cairo_scale(cr, ((double)new_width) / ((double)image_width), ((double)new_height) / ((double)image_height));
  273. cairo_set_source_surface(cr, *surface, 0, 0);
  274. cairo_paint(cr);
  275. cairo_destroy(cr);
  276. cairo_surface_destroy(*surface);
  277. *surface = surface2;
  278. }
  279. void read_uint(uint_t *r) {
  280. *r = 0;
  281. for (uint_t i = 0; i < sizeof(uint_t); i++)
  282. *r |= ((uint_t)fgetc(stdin)) << (i*8);
  283. }
  284. void read_string(buffer_t *buffer) {
  285. uint_t length;
  286. read_uint(&length);
  287. buffer_expand(buffer, length);
  288. buffer->size += fread(buffer->data + buffer->size, 1, length, stdin);
  289. buffer_append_data(buffer, &"\0", 1);
  290. }
  291. main() {
  292. uint_t avatar_width;
  293. uint_t image_width;
  294. uint_t image_height;
  295. uint_t x, y;
  296. uint_t w, h;
  297. cairo_surface_t *surface;
  298. cairo_t *cr;
  299. text_t *nickname;
  300. text_t *message;
  301. buffer_t *output_path;
  302. buffer_t *avatar_path;
  303. buffer_t *username_text;
  304. uint_t username_color;
  305. buffer_t *message_text;
  306. uint_t nickname_w, nickname_h;
  307. uint_t message_w, message_h;
  308. output_path = new_buffer();
  309. avatar_path = new_buffer();
  310. username_text = new_buffer();
  311. message_text = new_buffer();
  312. read_string(output_path);
  313. read_string(avatar_path);
  314. read_string(username_text);
  315. read_uint(&username_color);
  316. read_string(message_text);
  317. username_color = MIN(username_color, 6);
  318. get_text_size(username_text->data, NICKNAME_TEXT_SIZE, PANGO_WEIGHT_BOLD, &nickname_w, &nickname_h, 0);
  319. get_text_size(message_text->data, MESSAGE_TEXT_SIZE, PANGO_WEIGHT_NORMAL, &message_w, &message_h, 1);
  320. image_width = AVATAR_WIDTH + MAX(message_w, nickname_w) + HPADDING*2;
  321. image_height = message_h + nickname_h + VPADDING*2;
  322. if (image_width - AVATAR_WIDTH < MESSAGE_BOX_MIN)
  323. image_width = AVATAR_WIDTH + MESSAGE_BOX_MIN;
  324. surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, image_width, image_height+AVATAR_OFFSET);
  325. cr = cairo_create(surface);
  326. nickname = new_text(cr, username_text->data, NICKNAME_TEXT_SIZE, PANGO_WEIGHT_BOLD, 0);
  327. message = new_text(cr, message_text->data, MESSAGE_TEXT_SIZE, PANGO_WEIGHT_NORMAL, 1);
  328. free_buffer(username_text);
  329. free_buffer(message_text);
  330. x = 0;
  331. y = image_height - AVATAR_DIM + AVATAR_OFFSET;
  332. paste_clipped_image(cr, x, y, avatar_path->data);
  333. free_buffer(avatar_path);
  334. x = AVATAR_WIDTH;
  335. y = 0;
  336. w = MAX(message->w, nickname->w) + HPADDING*2;
  337. h = message->h + nickname->h + VPADDING*2;
  338. if (w < MESSAGE_BOX_MIN)
  339. w = MESSAGE_BOX_MIN;
  340. select_color(cr, C_MESSAGE_BOX);
  341. rounded_rectangle(cr, x, y, w, h, MESSAGE_BOX_RADIUS);
  342. x = AVATAR_WIDTH - TAIL_WIDTH + 9;
  343. y = image_height - TAIL_HEIGHT;
  344. paste_image(cr, x, y, TAIL_PATH);
  345. x = AVATAR_WIDTH + HPADDING;
  346. y = VPADDING / 2;
  347. select_color(cr, COLOR(username_color));
  348. text(cr, x, y, nickname);
  349. x = AVATAR_WIDTH + HPADDING;
  350. y = nickname->h + VPADDING - 3;
  351. select_color(cr, C_MESSAGE_TEXT);
  352. text(cr, x, y, message);
  353. cairo_destroy(cr);
  354. contain(&surface, 512, 512);
  355. cairo_surface_write_to_png(surface, output_path->data);
  356. free_buffer(output_path);
  357. cairo_surface_destroy(surface);
  358. free_text(nickname);
  359. free_text(message);
  360. }