File: main.c

package info (click to toggle)
gozer 0.7.nofont.1-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 484 kB
  • sloc: ansic: 1,732; sh: 330; makefile: 28
file content (450 lines) | stat: -rw-r--r-- 12,948 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* main.c

Copyright (C) 1999,2000 Tom Gilbert.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies of the Software and its documentation and acknowledgment shall be
given in the documentation and software packages that this Software was
used.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

#include "gozer.h"
#include "options.h"

int
main(int argc, char **argv)
{
   Imlib_Font fn;
   Imlib_Image image, bg_image = NULL;
   Imlib_Load_Error err;
   gib_list *lines, *l, *ll, *words;
   int w = 0, h = 0, ww, hh, x = 0, y = 0, bgw = 0, bgh = 0;
   DATA8 atab[256];
   gib_style *style = NULL;
   char *p, *pp;

   init_parse_options(argc, argv);

   init_imlib();

   if (!opt.output_file)
   {
      weprintf("no output file specified");
      show_mini_usage();
   }
   if (!opt.text && opt.textfile)
      opt.text = gozer_read_file(opt.textfile);

   if (!opt.text && opt.pipe)
   {
      opt.text = gozer_read_pipe();
   }

   if (!opt.text)
   {
      weprintf("no text specified for rendering");
      show_mini_usage();
   }

   fn = imlib_load_font(opt.font);
   if (!fn)
      eprintf("failed to load font %s", opt.font);

   if (opt.stylefile)
   {
      style = gib_style_new_from_ascii(opt.stylefile);
      if (!style)
         weprintf("failed to load style %s", opt.stylefile);
   }

   if (opt.bg_image)
   {
      if (gib_imlib_load_image(&bg_image, opt.bg_image))
      {
         bgw = gib_imlib_image_get_width(bg_image);
         bgh = gib_imlib_image_get_height(bg_image);
      }
      else
      {
         weprintf("failed to load background image \"%s\"", opt.bg_image);
      }
   }

   lines = gozer_calculate_lines(fn, style);

   l = lines;
   while (l)
   {
      p = (char *) l->data;
      gib_imlib_get_text_size(fn, p, style, &ww, &hh, IMLIB_TEXT_TO_RIGHT);
      if (ww > w)
         w = ww;
      h += hh;
      if (l->next)
         h += opt.line_spacing;
      l = l->next;
   }

   /* If we have an offset, increase the size */
   if (opt.x)
      w += opt.x;
   if (opt.y)
      h += opt.y;

   if (bg_image && opt.bg_resize)
      image = imlib_create_image(bgw, bgh);
   else
      image = imlib_create_image(w, h);

   if (!image)
      eprintf("couldn't create imlib image for text area.");

   /* make image transparent (HACK - imlib2 should do this nicely) */
   gib_imlib_image_set_has_alpha(image, 1);
   memset(atab, 0, sizeof(atab));
   gib_imlib_apply_color_modifier_to_rectangle(image, 0, 0, w, h, NULL, NULL,
                                               NULL, atab);

   gib_imlib_image_fill_rectangle(image, 0, 0, w, h, opt.bg_r, opt.bg_g,
                                  opt.bg_b, opt.bg_a);
   if (bg_image)
   {
      if (opt.bg_scale)
      {
         gib_imlib_blend_image_onto_image(image, bg_image, 1, 0, 0, bgw, bgh,
                                          0, 0, w, h, 0, 1, 1);
      }
      else if (opt.bg_tile)
      {
         gib_imlib_image_tile(bg_image);
         gib_imlib_blend_image_onto_image(image, bg_image, 1, 0, 0, bgw, bgh,
                                          0, 0, w, h, 0, 1, 1);
      }
      else
      {
         gib_imlib_blend_image_onto_image(image, bg_image, 1, 0, 0, bgw, bgh,
                                          0, 0, bgw, bgh, 0, 1, 1);
      }
   }
   l = lines;
   x = opt.x;
   y = opt.y;
   while (l)
   {
      p = (char *) l->data;
      gib_imlib_get_text_size(fn, p, style, &ww, &hh, IMLIB_TEXT_TO_RIGHT);
      switch (opt.justification)
      {
        case JUST_LEFT:
           x = opt.x;
           gib_imlib_text_draw(image, fn, style, x, y, p, IMLIB_TEXT_TO_RIGHT,
                               opt.fn_r, opt.fn_g, opt.fn_b, opt.fn_a);
           break;
        case JUST_CENTER:
           if (opt.x) 
           {
              x = (opt.x - ( ww / 2 ));
           }
           else
           {
              x = ((((opt.bg_resize && bgw) ? bgw : w) - ww) / 2);
           }
           /* y = ((((opt.bg_resize && bgw) ? bgh : h) - hh) / 2); */
           gib_imlib_text_draw(image, fn, style, x, y, p, IMLIB_TEXT_TO_RIGHT,
                               opt.fn_r, opt.fn_g, opt.fn_b, opt.fn_a);
           break;
        case JUST_RIGHT:
           if (opt.x) 
           {
              x = opt.x - ww;
           }
           else
           {
           x = (((opt.bg_resize && bgw) ? bgw : w) - ww);
           }
           gib_imlib_text_draw(image, fn, style, x, y, p, IMLIB_TEXT_TO_RIGHT,
                               opt.fn_r, opt.fn_g, opt.fn_b, opt.fn_a);

           break;
        case JUST_BLOCK:
           words = gib_string_split(p, " ");
           if (words)
           {
              int wordcnt, word_spacing, line_w;
              int t_width, m_width, space_width, offset = 0;

              wordcnt = gib_list_length(words);
              gib_imlib_get_text_size(fn, p, style, &line_w, NULL,
                                      IMLIB_TEXT_TO_RIGHT);
              gib_imlib_get_text_size(fn, "M M", style, &t_width, NULL,
                                      IMLIB_TEXT_TO_RIGHT);
              gib_imlib_get_text_size(fn, "M", style, &m_width, NULL,
                                      IMLIB_TEXT_TO_RIGHT);
              space_width = t_width - (2 * m_width);

              if (wordcnt > 1)
                 word_spacing = (w - line_w) / (wordcnt - 1);
              else
                 word_spacing = (w - line_w);

              ll = words;
              while (ll)
              {
                 pp = (char *) ll->data;
                 if (strcmp(pp, " "))
                 {
                    int wordw;

                    gib_imlib_text_draw(image, fn, style, x + offset, y, pp,
                                        IMLIB_TEXT_TO_RIGHT, opt.fn_r,
                                        opt.fn_g, opt.fn_b, opt.fn_a);
                    gib_imlib_get_text_size(fn, pp, style, &wordw, NULL,
                                            IMLIB_TEXT_TO_RIGHT);
                    offset += (wordw + space_width + word_spacing);
                 }
                 ll = ll->next;
              }
              gib_list_free_and_data(words);
           }
           break;

        default:
           break;
      }
      y += hh + opt.line_spacing;
      l = l->next;
   }

   imlib_context_set_image(image);
   imlib_image_attach_data_value("quality", NULL, opt.quality, NULL);

   gib_imlib_save_image_with_error_return(image, opt.output_file, &err);
   if (err)
      eprintf("Saving to file %s failed", opt.output_file);
   if (opt.to_stdout)
   {
      gozer_stream_file(opt.output_file, opt.cgi);
      unlink(opt.output_file);
   }

   return 0;
}


gib_list *
gozer_calculate_lines(Imlib_Font fn, gib_style * style)
{
   gib_list *ll, *lines = NULL, *list = NULL, *words;
   gib_list *l = NULL;
   char delim[2] = { '\n', '\0' };
   int w, line_width;
   int tw, th;
   char *p, *pp;
   char *line = NULL;
   char *temp;
   int space_width = 0, m_width = 0, t_width = 0, new_width = 0;

   lines = gib_string_split(opt.text, delim);

   if (opt.wrap_width)
   {
      gib_imlib_get_text_size(fn, "M M", style, &t_width, NULL,
                              IMLIB_TEXT_TO_RIGHT);
      gib_imlib_get_text_size(fn, "M", style, &m_width, NULL,
                              IMLIB_TEXT_TO_RIGHT);
      space_width = t_width - (2 * m_width);
      w = opt.wrap_width;
      l = lines;
      while (l)
      {
         line_width = 0;
         p = (char *) l->data;
         /* quick check to see if whole line fits okay */
         gib_imlib_get_text_size(fn, p, style, &tw, &th, IMLIB_TEXT_TO_RIGHT);
         if (tw <= w)
            list = gib_list_add_end(list, estrdup(p));
         else if (strlen(p) == 0)
            list = gib_list_add_end(list, estrdup(""));
         else if (!strcmp(p, " "))
            list = gib_list_add_end(list, estrdup(" "));
         else
         {
            words = gib_string_split(p, " ");
            if (words)
            {
               ll = words;
               while (ll)
               {
                  pp = (char *) ll->data;
                  if (strcmp(pp, " "))
                  {
                     gib_imlib_get_text_size(fn, pp, style, &tw, &th,
                                             IMLIB_TEXT_TO_RIGHT);
                     if (line_width == 0)
                        new_width = tw;
                     else
                        new_width = line_width + space_width + tw;
                     if (new_width <= w)
                     {
                        /* add word to line */
                        if (line)
                        {
                           int len;

                           len = strlen(line) + strlen(pp) + 2;
                           temp = emalloc(len);
                           snprintf(temp, len, "%s %s", line, pp);
                           free(line);
                           line = temp;
                        }
                        else
                           line = estrdup(pp);
                        line_width = new_width;
                     }
                     else if (line_width == 0)
                     {
                        /* can't fit single word in :/
                           increase width limit to width of word and jam the bastard
                           in anyhow */
                        w = tw;
                        line = estrdup(pp);
                        line_width = new_width;
                     }
                     else
                     {
                        /* finish this line, start next and add word there */
                        if (line)
                        {
                           list = gib_list_add_end(list, estrdup(line));
                           free(line);
                           line = NULL;
                        }
                        line = estrdup(pp);
                        line_width = tw;
                     }
                  }
                  ll = ll->next;
               }
               if (line)
               {
                  /* finish last line */
                  list = gib_list_add_end(list, estrdup(line));
                  free(line);
                  line = NULL;
                  line_width = 0;
               }
               gib_list_free_and_data(words);
            }
         }
         l = l->next;
      }
      gib_list_free_and_data(lines);
      lines = list;
   }
   else
      lines = lines;
   return lines;
}

char *
gozer_read_file(char *filename)
{
   FILE *fp;
   struct stat st;
   char *text;

   if (stat(filename, &st) == -1)
   {
      weprintf("couldn't stat file %s :", filename);
      return NULL;
   }

   fp = fopen(filename, "r");

   if (!fp)
   {
      weprintf("couldn't open file %s :", filename);
      return NULL;
   }

   text = malloc(st.st_size + 1);
   fread(text, 1, st.st_size, fp);
   text[st.st_size] = '\0';

   return text;
}

char *
gozer_read_pipe()
{
   char buf[PIPE_BUF_MAX] = "", buf2[1023] = "", *text = NULL;
   int len = 0;

   while (fgets(buf2, 1023, stdin))
   {
      len += strlen(buf2);
      if (len < PIPE_BUF_MAX)
      {
         strncat(buf, buf2, 1023);
      }
      else
      {
         weprintf("Truncating oversized pipe buffer at %d bytes.", len);
      }
   }
   text = _estrdup(buf);
   return text;
}

void
gozer_stream_file(char *file, int headers)
{
   FILE *fp;
   int c;

   if (headers)
   {
      /* print headers for user-agents */
      char *extension;

      extension = strrchr(file, '.');
      extension++;
      if (extension)
      {
         if (!strcasecmp(extension, "jpg"))
            printf("Content-type: image/jpeg\n");
         else
            printf("Content-type: image/%s\n", extension);
      }
      else
         weprintf("couldn't determine file extension for mime type setting");
      printf("\n");
   }

   fp = fopen(file, "r");
   if (!fp)
   {
      weprintf("couldn't open file %s for streaming\n", file);
      return;
   }
   while (!feof(fp))
   {
      c = fgetc(fp);
      fputc(c, stdout);
   }
}