makeshot.c 12 KB

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