makeshot.c 12 KB

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