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. #define FLAG_BOLD (1 << 0)
  51. #define FLAG_STRIKE (1 << 1)
  52. #define FLAG_ITALIC (1 << 2)
  53. #define FLAG_TT (1 << 3)
  54. typedef struct _node_t {
  55. uint_t flags;
  56. buffer_t *buffer;
  57. struct _node_t *next;
  58. } node_t;
  59. void *malloc_protected(size_t z) {
  60. void *p;
  61. if (!(p = malloc(z))) {
  62. fputs("out of memory.\n", stderr);
  63. exit(1);
  64. }
  65. return p;
  66. }
  67. void *realloc_protected(void *old_p, size_t z) {
  68. void *p;
  69. if (!(p = realloc(old_p, z))) {
  70. fputs("out of memory.\n", stderr);
  71. exit(1);
  72. }
  73. return p;
  74. }
  75. buffer_t *new_buffer() {
  76. buffer_t *buffer;
  77. buffer = malloc_protected(sizeof(buffer_t));
  78. buffer->data = NULL;
  79. buffer->size = 0;
  80. buffer->allocated = 0;
  81. return buffer;
  82. }
  83. void free_buffer(buffer_t *buffer) {
  84. free(buffer->data);
  85. free(buffer);
  86. }
  87. void buffer_expand(buffer_t *buffer, size_t size) {
  88. buffer->allocated += size;
  89. buffer->data = realloc_protected(buffer->data, buffer->allocated);
  90. }
  91. void buffer_append_data(buffer_t *buffer, unsigned char *data, size_t data_length) {
  92. size_t tail = buffer->size;
  93. buffer->size += data_length;
  94. if (buffer->size > buffer->allocated)
  95. buffer_expand(buffer, data_length);
  96. for (size_t i = 0; i < data_length; i++)
  97. buffer->data[tail + i] = data[i];
  98. }
  99. #define BUFFER_APPEND(b, s) buffer_append_data(b, s, strlen(s))
  100. #define BUFFER_CONCAT(b1, b2) buffer_append_data(b1, (b2)->data, (b2)->size);
  101. node_t *new_node() {
  102. node_t *node;
  103. node = malloc_protected(sizeof(node_t));
  104. node->flags = 0;
  105. node->buffer = new_buffer();
  106. node->next = NULL;
  107. return node;
  108. }
  109. void free_node(node_t *node) {
  110. if (node->next)
  111. free_node(node->next);
  112. free_buffer(node->buffer);
  113. free(node);
  114. }
  115. #define SET(flag) {\
  116. if (tail->flags & FLAG_##flag) {\
  117. tail = tail->next = new_node();\
  118. continue;\
  119. }\
  120. uint_t flags = tail->flags;\
  121. tail = tail->next = new_node();\
  122. tail->flags = flags | FLAG_##flag;\
  123. }
  124. void preprocess_text(buffer_t *buffer, char *text) {
  125. uint_t state;
  126. node_t *head;
  127. node_t *tail;
  128. head = tail = new_node();
  129. for (size_t i = 0; i < strlen(text); i++) {
  130. unsigned char c, nc;
  131. c = text[i];
  132. nc = text[i+1];
  133. if (c == '`') {
  134. SET(TT);
  135. } else if (c == '*' && nc == '*') {
  136. i++;
  137. SET(BOLD);
  138. } else if (c == '~' && nc == '~') {
  139. i++;
  140. SET(STRIKE);
  141. } else if (c == '_' && nc == '_') {
  142. i++;
  143. SET(ITALIC);
  144. } else {
  145. buffer_append_data(tail->buffer, &c, 1);
  146. }
  147. }
  148. if (tail->flags) {
  149. tail->flags = 0;
  150. }
  151. node_t *start = head;
  152. while (head) {
  153. if (head->flags & FLAG_BOLD)
  154. BUFFER_APPEND(buffer, "<b>");
  155. if (head->flags & FLAG_STRIKE)
  156. BUFFER_APPEND(buffer, "<s>");
  157. if (head->flags & FLAG_ITALIC)
  158. BUFFER_APPEND(buffer, "<i>");
  159. if (head->flags & FLAG_TT)
  160. BUFFER_APPEND(buffer, "<tt>");
  161. BUFFER_CONCAT(buffer, head->buffer);
  162. if (head->flags & FLAG_TT)
  163. BUFFER_APPEND(buffer, "</tt>");
  164. if (head->flags & FLAG_ITALIC)
  165. BUFFER_APPEND(buffer, "</i>");
  166. if (head->flags & FLAG_STRIKE)
  167. BUFFER_APPEND(buffer, "</s>");
  168. if (head->flags & FLAG_BOLD)
  169. BUFFER_APPEND(buffer, "</b>");
  170. head = head->next;
  171. }
  172. free_node(start);
  173. }
  174. text_t *new_text(cairo_t *cr, char *s, int size, int weight, int preprocess) {
  175. text_t *text;
  176. PangoFontDescription *font_description;
  177. buffer_t *buffer;
  178. text = malloc_protected(sizeof(text_t));
  179. font_description = pango_font_description_from_string("Apple Color Emoji 16,Open Sans 16");
  180. pango_font_description_set_absolute_size(font_description, size * PANGO_SCALE);
  181. pango_font_description_set_weight(font_description, weight);
  182. text->layout = pango_cairo_create_layout(cr);
  183. pango_layout_set_font_description(text->layout, font_description);
  184. pango_layout_set_wrap(text->layout, PANGO_WRAP_WORD);
  185. pango_layout_set_width(text->layout, WRAP_WIDTH * PANGO_SCALE);
  186. pango_font_description_free(font_description);
  187. if (preprocess) {
  188. buffer = new_buffer();
  189. preprocess_text(buffer, s);
  190. pango_layout_set_markup(text->layout, buffer->data, buffer->size);
  191. free_buffer(buffer);
  192. } else
  193. pango_layout_set_text(text->layout, s, -1);
  194. pango_layout_get_pixel_size(text->layout, &text->w, &text->h);
  195. return text;
  196. }
  197. void free_text(text_t *text) {
  198. g_object_unref(text->layout);
  199. free(text);
  200. }
  201. /* TO-DO: do this better */
  202. void get_text_size(char *s, uint_t size, uint_t weight, uint_t *w, uint_t *h, int preprocess) {
  203. cairo_surface_t *surface;
  204. cairo_t *cr;
  205. surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1);
  206. cr = cairo_create(surface);
  207. text_t *text = new_text(cr, s, size, weight, preprocess);
  208. *w = text->w;
  209. *h = text->h;
  210. free_text(text);
  211. cairo_destroy(cr);
  212. cairo_surface_destroy(surface);
  213. }
  214. void select_color(cairo_t *cr, color_t *color) {
  215. cairo_set_source_rgb(cr, color->r / 255.0F, color->g / 255.0F, color->b / 255.0F);
  216. }
  217. void paste_clipped_image(cairo_t *cr, uint_t x, uint_t y, char *path) {
  218. cairo_surface_t *surface, *surface2;
  219. cairo_t *ic;
  220. surface = cairo_image_surface_create_from_png(path);
  221. ic = cairo_create(surface);
  222. cairo_scale(ic, ((double)AVATAR_DIM) / cairo_image_surface_get_width(surface), ((double)AVATAR_DIM) / cairo_image_surface_get_height(surface));
  223. cairo_set_source_surface(ic, surface, 0, 0);
  224. cairo_paint(ic);
  225. cairo_destroy(ic);
  226. surface2 = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, AVATAR_DIM, AVATAR_DIM);
  227. ic = cairo_create(surface2);
  228. cairo_arc(ic, AVATAR_DIM/2, AVATAR_DIM/2, AVATAR_DIM/2, 0, 2*M_PI);
  229. cairo_clip(ic);
  230. cairo_new_path(ic);
  231. cairo_set_source_surface(ic, surface, 0, 0);
  232. cairo_paint(ic);
  233. cairo_set_source_surface(cr, surface2, x, y);
  234. cairo_paint(cr);
  235. cairo_destroy(ic);
  236. cairo_surface_destroy(surface);
  237. cairo_surface_destroy(surface2);
  238. }
  239. void rounded_rectangle(cairo_t *cr, uint_t x, uint_t y, uint_t w, uint_t h, uint_t r) {
  240. cairo_set_fill_rule(cr, CAIRO_FILL_RULE_EVEN_ODD);
  241. cairo_new_sub_path(cr);
  242. cairo_arc(cr, x + r, y + r, r, M_PI, 3 * M_PI / 2);
  243. cairo_arc(cr, x + w - r, y + r, r, 3 *M_PI / 2, 2 * M_PI);
  244. cairo_arc(cr, x + w - r, y + h - r, r, 0, M_PI / 2);
  245. cairo_arc(cr, x + r, y + h - r, r, M_PI / 2, M_PI);
  246. cairo_close_path(cr);
  247. cairo_fill(cr);
  248. }
  249. void text(cairo_t *cr, uint_t x, uint_t y, text_t *text) {
  250. cairo_move_to(cr, x, y);
  251. pango_cairo_update_layout(cr, text->layout);
  252. pango_cairo_show_layout(cr, text->layout);
  253. }
  254. void paste_image(cairo_t *cr, uint_t x, uint_t y, char *path) {
  255. cairo_surface_t *surface;
  256. surface = cairo_image_surface_create_from_png(path);
  257. cairo_set_source_surface(cr, surface, x, y);
  258. cairo_paint(cr);
  259. cairo_surface_destroy(surface);
  260. }
  261. void fit(cairo_surface_t **surface, uint_t width, uint_t height) {
  262. uint_t image_width, image_height;
  263. uint_t new_width, new_height;
  264. double image_ratio, new_ratio;
  265. cairo_surface_t *surface2;
  266. cairo_t *cr;
  267. image_width = cairo_image_surface_get_width(*surface);
  268. image_height = cairo_image_surface_get_height(*surface);
  269. new_width = width;
  270. new_height = height;
  271. image_ratio = ((double)image_width) / ((double)image_height);
  272. new_ratio = ((double)width) / ((double)height);
  273. if (image_ratio > new_ratio)
  274. new_height = ((double)image_height) / ((double)image_width) * ((double)width);
  275. else
  276. new_width = ((double)image_width) / ((double)image_height) * ((double)height);
  277. surface2 = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, new_width, new_height);
  278. cr = cairo_create(surface2);
  279. cairo_scale(cr, ((double)new_width) / ((double)image_width), ((double)new_height) / ((double)image_height));
  280. cairo_set_source_surface(cr, *surface, 0, 0);
  281. cairo_paint(cr);
  282. cairo_destroy(cr);
  283. cairo_surface_destroy(*surface);
  284. *surface = surface2;
  285. }
  286. void read_uint(uint_t *r) {
  287. *r = 0;
  288. for (uint_t i = 0; i < sizeof(uint_t); i++)
  289. *r |= ((uint_t)fgetc(stdin)) << (i*8);
  290. }
  291. void read_string(buffer_t *buffer) {
  292. uint_t length;
  293. read_uint(&length);
  294. buffer_expand(buffer, length);
  295. buffer->size += fread(buffer->data + buffer->size, 1, length, stdin);
  296. buffer_append_data(buffer, &"\0", 1);
  297. }
  298. main() {
  299. uint_t avatar_width;
  300. uint_t image_width;
  301. uint_t image_height;
  302. uint_t x, y;
  303. uint_t w, h;
  304. cairo_surface_t *surface;
  305. cairo_t *cr;
  306. text_t *nickname;
  307. text_t *message;
  308. buffer_t *output_path;
  309. buffer_t *avatar_path;
  310. buffer_t *username_text;
  311. uint_t username_color;
  312. buffer_t *message_text;
  313. uint_t nickname_w, nickname_h;
  314. uint_t message_w, message_h;
  315. output_path = new_buffer();
  316. avatar_path = new_buffer();
  317. username_text = new_buffer();
  318. message_text = new_buffer();
  319. read_string(output_path);
  320. read_string(avatar_path);
  321. read_string(username_text);
  322. read_uint(&username_color);
  323. read_string(message_text);
  324. username_color = MIN(username_color, 6);
  325. get_text_size(username_text->data, NICKNAME_TEXT_SIZE, PANGO_WEIGHT_BOLD, &nickname_w, &nickname_h, 0);
  326. get_text_size(message_text->data, MESSAGE_TEXT_SIZE, PANGO_WEIGHT_NORMAL, &message_w, &message_h, 1);
  327. image_width = AVATAR_WIDTH + MAX(message_w, nickname_w) + HPADDING*2;
  328. image_height = message_h + nickname_h + VPADDING*2;
  329. if (image_width - AVATAR_WIDTH < MESSAGE_BOX_MIN)
  330. image_width = AVATAR_WIDTH + MESSAGE_BOX_MIN;
  331. surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, image_width, image_height+AVATAR_OFFSET);
  332. cr = cairo_create(surface);
  333. nickname = new_text(cr, username_text->data, NICKNAME_TEXT_SIZE, PANGO_WEIGHT_BOLD, 0);
  334. message = new_text(cr, message_text->data, MESSAGE_TEXT_SIZE, PANGO_WEIGHT_NORMAL, 1);
  335. free_buffer(username_text);
  336. free_buffer(message_text);
  337. x = 0;
  338. y = image_height - AVATAR_DIM + AVATAR_OFFSET;
  339. paste_clipped_image(cr, x, y, avatar_path->data);
  340. free_buffer(avatar_path);
  341. x = AVATAR_WIDTH;
  342. y = 0;
  343. w = MAX(message->w, nickname->w) + HPADDING*2;
  344. h = message->h + nickname->h + VPADDING*2;
  345. if (w < MESSAGE_BOX_MIN)
  346. w = MESSAGE_BOX_MIN;
  347. select_color(cr, C_MESSAGE_BOX);
  348. rounded_rectangle(cr, x, y, w, h, MESSAGE_BOX_RADIUS);
  349. x = AVATAR_WIDTH - TAIL_WIDTH + 9;
  350. y = image_height - TAIL_HEIGHT;
  351. paste_image(cr, x, y, TAIL_PATH);
  352. x = AVATAR_WIDTH + HPADDING;
  353. y = VPADDING / 2;
  354. select_color(cr, COLOR(username_color));
  355. text(cr, x, y, nickname);
  356. x = AVATAR_WIDTH + HPADDING;
  357. y = nickname->h + VPADDING - 3;
  358. select_color(cr, C_MESSAGE_TEXT);
  359. text(cr, x, y, message);
  360. cairo_destroy(cr);
  361. fit(&surface, 512, 512);
  362. cairo_surface_write_to_png(surface, output_path->data);
  363. free_buffer(output_path);
  364. cairo_surface_destroy(surface);
  365. free_text(nickname);
  366. free_text(message);
  367. }