makeshot.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. k = strlen(s);\
  120. i += k;\
  121. if (strncmp(&text[i], s, k) == 0) {\
  122. BUFFER_APPEND(buffer, s);\
  123. BUFFER_APPEND(buffer, s);\
  124. break;
  125. }\
  126. temp_buffer = new_buffer();\
  127. while (text[i]) {\
  128. if (strncmp(&text[i], s, k) == 0) {\
  129. i += k-1;\
  130. buffer_append_data(buffer, "<", 1);\
  131. if (*tag == ' ')\
  132. BUFFER_APPEND(buffer, "span");\
  133. BUFFER_APPEND(buffer, tag);\
  134. buffer_append_data(buffer, ">", 1);\
  135. if (pr)\
  136. BUFFER_PREPROCESS(buffer, temp_buffer);\
  137. else\
  138. BUFFER_CONCAT(buffer, temp_buffer);\
  139. buffer_append_data(buffer, "</", 2);\
  140. if (*tag == ' ')\
  141. BUFFER_APPEND(buffer, "span");\
  142. else\
  143. BUFFER_APPEND(buffer, tag);\
  144. buffer_append_data(buffer, ">", 1);\
  145. free_buffer(temp_buffer);\
  146. goto escape;\
  147. }\
  148. buffer_append_escaped(temp_buffer, text[i]);\
  149. i++;\
  150. }\
  151. i--;\
  152. BUFFER_APPEND(buffer, s);\
  153. BUFFER_PREPROCESS(buffer, temp_buffer);\
  154. free_buffer(temp_buffer);\
  155. } while(0)
  156. void preprocess_text(buffer_t *buffer, char *text) {
  157. for (size_t i = 0; text[i]; i++) {
  158. unsigned char c, nc, fc;
  159. c = text[i];
  160. nc = text[i+1];
  161. fc = text[i+2];
  162. if (c == '`')
  163. SET("`", "tt", 0);
  164. else if (c == '*' && nc == '*')
  165. SET("**", "b", 1);
  166. else if (c == '_' && nc == '_')
  167. SET("__", "i", 1);
  168. else if (c == '~' && nc == '~')
  169. SET("~~", "s", 1);
  170. else if (c == 0xee && nc == 0x80 && fc == 0x80)
  171. SET("\xee\x80\x80", "u", 1);
  172. else if (c == 0xee && nc == 0x80 && fc == 0x81)
  173. SET("\xee\x80\x81", " bgcolor=\"#ffffff\"", 0);
  174. else if (c == 0xee && nc == 0x80 && fc == 0x82)
  175. SET("\xee\x80\x82", " fgcolor=\"#70baf5\" underline=\"single\" underline_color=\"#70baf5\"", 0);
  176. else if (c == 0xee && nc == 0x80 && fc == 0x83)
  177. SET("\xee\x80\x83", " fgcolor=\"#70baf5\"", 0);
  178. else
  179. buffer_append_escaped(buffer, c);
  180. escape:;
  181. }
  182. }
  183. text_t *new_text(cairo_t *cr, char *s, int size, int weight, int preprocess) {
  184. text_t *text;
  185. PangoFontDescription *font_description;
  186. buffer_t *buffer;
  187. text = malloc_protected(sizeof(text_t));
  188. font_description = pango_font_description_from_string("Apple Color Emoji 16,Open Sans 16");
  189. pango_font_description_set_absolute_size(font_description, size * PANGO_SCALE);
  190. pango_font_description_set_weight(font_description, weight);
  191. text->layout = pango_cairo_create_layout(cr);
  192. pango_layout_set_font_description(text->layout, font_description);
  193. pango_layout_set_wrap(text->layout, PANGO_WRAP_WORD);
  194. pango_layout_set_width(text->layout, WRAP_WIDTH * PANGO_SCALE);
  195. pango_font_description_free(font_description);
  196. if (preprocess) {
  197. buffer = new_buffer();
  198. preprocess_text(buffer, s);
  199. pango_layout_set_markup(text->layout, buffer->data, buffer->size);
  200. free_buffer(buffer);
  201. } else
  202. pango_layout_set_text(text->layout, s, -1);
  203. pango_layout_get_pixel_size(text->layout, &text->w, &text->h);
  204. return text;
  205. }
  206. void free_text(text_t *text) {
  207. g_object_unref(text->layout);
  208. free(text);
  209. }
  210. /* TO-DO: do this better */
  211. void get_text_size(char *s, uint_t size, uint_t weight, uint_t *w, uint_t *h, int preprocess) {
  212. cairo_surface_t *surface;
  213. cairo_t *cr;
  214. surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1);
  215. cr = cairo_create(surface);
  216. text_t *text = new_text(cr, s, size, weight, preprocess);
  217. *w = text->w;
  218. *h = text->h;
  219. free_text(text);
  220. cairo_destroy(cr);
  221. cairo_surface_destroy(surface);
  222. }
  223. void select_color(cairo_t *cr, color_t *color) {
  224. cairo_set_source_rgb(cr, color->r / 255.0F, color->g / 255.0F, color->b / 255.0F);
  225. }
  226. void paste_clipped_image(cairo_t *cr, uint_t x, uint_t y, char *path) {
  227. cairo_surface_t *surface, *surface2;
  228. cairo_t *ic;
  229. surface = cairo_image_surface_create_from_png(path);
  230. ic = cairo_create(surface);
  231. cairo_scale(ic, ((double)AVATAR_DIM) / cairo_image_surface_get_width(surface), ((double)AVATAR_DIM) / cairo_image_surface_get_height(surface));
  232. cairo_set_source_surface(ic, surface, 0, 0);
  233. cairo_paint(ic);
  234. cairo_destroy(ic);
  235. surface2 = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, AVATAR_DIM, AVATAR_DIM);
  236. ic = cairo_create(surface2);
  237. cairo_arc(ic, AVATAR_DIM/2, AVATAR_DIM/2, AVATAR_DIM/2, 0, 2*M_PI);
  238. cairo_clip(ic);
  239. cairo_new_path(ic);
  240. cairo_set_source_surface(ic, surface, 0, 0);
  241. cairo_paint(ic);
  242. cairo_set_source_surface(cr, surface2, x, y);
  243. cairo_paint(cr);
  244. cairo_destroy(ic);
  245. cairo_surface_destroy(surface);
  246. cairo_surface_destroy(surface2);
  247. }
  248. void rounded_rectangle(cairo_t *cr, uint_t x, uint_t y, uint_t w, uint_t h, uint_t r) {
  249. cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
  250. cairo_new_sub_path(cr);
  251. cairo_arc(cr, x + r, y + r, r, M_PI, 3 * M_PI / 2);
  252. cairo_arc(cr, x + w - r, y + r, r, 3 *M_PI / 2, 2 * M_PI);
  253. cairo_arc(cr, x + w - r, y + h - r, r, 0, M_PI / 2);
  254. cairo_arc(cr, x + r, y + h - r, r, M_PI / 2, M_PI);
  255. cairo_close_path(cr);
  256. cairo_fill(cr);
  257. }
  258. void text(cairo_t *cr, uint_t x, uint_t y, text_t *text) {
  259. cairo_move_to(cr, x, y);
  260. pango_cairo_update_layout(cr, text->layout);
  261. pango_cairo_show_layout(cr, text->layout);
  262. }
  263. void paste_image(cairo_t *cr, uint_t x, uint_t y, char *path) {
  264. cairo_surface_t *surface;
  265. surface = cairo_image_surface_create_from_png(path);
  266. cairo_set_source_surface(cr, surface, x, y);
  267. cairo_paint(cr);
  268. cairo_surface_destroy(surface);
  269. }
  270. void contain(cairo_surface_t **surface, uint_t width, uint_t height) {
  271. uint_t image_width, image_height;
  272. uint_t new_width, new_height;
  273. double image_ratio, new_ratio;
  274. cairo_surface_t *surface2;
  275. cairo_t *cr;
  276. image_width = cairo_image_surface_get_width(*surface);
  277. image_height = cairo_image_surface_get_height(*surface);
  278. new_width = width;
  279. new_height = height;
  280. image_ratio = ((double)image_width) / ((double)image_height);
  281. new_ratio = ((double)width) / ((double)height);
  282. if (image_ratio > new_ratio)
  283. new_height = ((double)image_height) / ((double)image_width) * ((double)width);
  284. else
  285. new_width = ((double)image_width) / ((double)image_height) * ((double)height);
  286. surface2 = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, new_width, new_height);
  287. cr = cairo_create(surface2);
  288. cairo_scale(cr, ((double)new_width) / ((double)image_width), ((double)new_height) / ((double)image_height));
  289. cairo_set_source_surface(cr, *surface, 0, 0);
  290. cairo_paint(cr);
  291. cairo_destroy(cr);
  292. cairo_surface_destroy(*surface);
  293. *surface = surface2;
  294. }
  295. void read_uint(uint_t *r) {
  296. *r = 0;
  297. for (uint_t i = 0; i < sizeof(uint_t); i++)
  298. *r |= ((uint_t)fgetc(stdin)) << (i*8);
  299. }
  300. void read_string(buffer_t *buffer) {
  301. uint_t length;
  302. read_uint(&length);
  303. buffer_expand(buffer, length);
  304. buffer->size += fread(buffer->data + buffer->size, 1, length, stdin);
  305. buffer_append_data(buffer, &"\0", 1);
  306. }
  307. main() {
  308. uint_t avatar_width;
  309. uint_t image_width;
  310. uint_t image_height;
  311. uint_t x, y;
  312. uint_t w, h;
  313. cairo_surface_t *surface;
  314. cairo_t *cr;
  315. text_t *nickname;
  316. text_t *message;
  317. buffer_t *output_path;
  318. buffer_t *avatar_path;
  319. buffer_t *username_text;
  320. uint_t username_color;
  321. buffer_t *message_text;
  322. uint_t nickname_w, nickname_h;
  323. uint_t message_w, message_h;
  324. output_path = new_buffer();
  325. avatar_path = new_buffer();
  326. username_text = new_buffer();
  327. message_text = new_buffer();
  328. read_string(output_path);
  329. read_string(avatar_path);
  330. read_string(username_text);
  331. read_uint(&username_color);
  332. read_string(message_text);
  333. username_color = MIN(username_color, 6);
  334. get_text_size(username_text->data, NICKNAME_TEXT_SIZE, PANGO_WEIGHT_BOLD, &nickname_w, &nickname_h, 0);
  335. get_text_size(message_text->data, MESSAGE_TEXT_SIZE, PANGO_WEIGHT_NORMAL, &message_w, &message_h, 1);
  336. image_width = AVATAR_WIDTH + MAX(message_w, nickname_w) + HPADDING*2;
  337. image_height = message_h + nickname_h + VPADDING*2;
  338. if (image_width - AVATAR_WIDTH < MESSAGE_BOX_MIN)
  339. image_width = AVATAR_WIDTH + MESSAGE_BOX_MIN;
  340. surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, image_width, image_height+AVATAR_OFFSET);
  341. cr = cairo_create(surface);
  342. nickname = new_text(cr, username_text->data, NICKNAME_TEXT_SIZE, PANGO_WEIGHT_BOLD, 0);
  343. message = new_text(cr, message_text->data, MESSAGE_TEXT_SIZE, PANGO_WEIGHT_NORMAL, 1);
  344. free_buffer(username_text);
  345. free_buffer(message_text);
  346. x = 0;
  347. y = image_height - AVATAR_DIM + AVATAR_OFFSET;
  348. paste_clipped_image(cr, x, y, avatar_path->data);
  349. free_buffer(avatar_path);
  350. x = AVATAR_WIDTH;
  351. y = 0;
  352. w = MAX(message->w, nickname->w) + HPADDING*2;
  353. h = message->h + nickname->h + VPADDING*2;
  354. if (w < MESSAGE_BOX_MIN)
  355. w = MESSAGE_BOX_MIN;
  356. select_color(cr, C_MESSAGE_BOX);
  357. rounded_rectangle(cr, x, y, w, h, MESSAGE_BOX_RADIUS);
  358. x = AVATAR_WIDTH - TAIL_WIDTH + 9;
  359. y = image_height - TAIL_HEIGHT;
  360. paste_image(cr, x, y, TAIL_PATH);
  361. x = AVATAR_WIDTH + HPADDING;
  362. y = VPADDING / 2;
  363. select_color(cr, COLOR(username_color));
  364. text(cr, x, y, nickname);
  365. x = AVATAR_WIDTH + HPADDING;
  366. y = nickname->h + VPADDING - 3;
  367. select_color(cr, C_MESSAGE_TEXT);
  368. text(cr, x, y, message);
  369. cairo_destroy(cr);
  370. contain(&surface, 512, 512);
  371. cairo_surface_write_to_png(surface, output_path->data);
  372. free_buffer(output_path);
  373. cairo_surface_destroy(surface);
  374. free_text(nickname);
  375. free_text(message);
  376. }