-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtw-image.c
139 lines (125 loc) · 3.03 KB
/
tw-image.c
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
/*
* Copyright (C) 2023 Christopher Lang
* See LICENSE for license details.
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils.h"
#include "twpdf.h"
#include "document.h"
#include "stralloc.h"
#include "arg.h"
static int read_line(FILE *file, char **line, int *allocated);
static void read_file(struct document *doc, FILE *file);
struct stralloc stralloc;
static int font_size;
static int top_margin;
static int bot_margin;
static int left_margin;
static int right_margin;
static const char *tab_expand;
static int
read_line(FILE *file, char **line, int *allocated)
{
int c, len, i;
len = 0;
while ( (c = fgetc(file)) != EOF) {
if (len + 64 >= *allocated) {
*allocated += 256;
*line = xrealloc(*line, *allocated);
}
switch (c) {
case '\r':
break;
case '\n':
(*line)[len] = '\0';
return 1;
case '\t':
for (i = 0; tab_expand[i] != '\0'; i++)
(*line)[len++] = tab_expand[i];
break;
default:
(*line)[len++] = c;
}
}
(*line)[len] = '\0';
return 0;
}
static void
read_file(struct document *doc, FILE *file)
{
char *line, *str;
int line_allocated, image_size;
image_size = 595 - left_margin - right_margin;
line_allocated = 256;
line = xmalloc(line_allocated);
while (read_line(stdin, &line, &line_allocated)) {
str = stralloc_alloc(&stralloc, line);
if (str[0] == '\0') {
put_glue(doc, font_size * 30, font_size);
} else if (strcmp(str, "---") == 0) {
put_glue(doc, 0, 0);
} else if (strncmp(str, "!IMAGE_SIZE ", strlen("!IMAGE_SIZE ")) == 0) {
str += strlen("!IMAGE_SIZE ");
image_size = atoi(str);
} else if (strncmp(str, "!IMAGE ", strlen("!IMAGE ")) == 0) {
str += strlen("!IMAGE ");
put_glue(doc, font_size * 40, font_size / 2);
put_image(doc, str, image_size);
} else {
put_glue(doc, font_size * 40, font_size / 2);
put_text(doc, str, font_size);
}
}
free(line);
}
int
main(int argc, char **argv)
{
struct document doc;
const char *output_fname;
int c;
font_size = 9;
top_margin = 40;
bot_margin = 40;
left_margin = 80;
right_margin = 80;
tab_expand = " ";
output_fname = "output.pdf";
while ( (c = next_opt(argc, argv, "s#v#h#t*o*")) != -1) {
switch (c) {
case 's':
font_size = opt_arg_int;
break;
case 'v':
top_margin = opt_arg_int;
bot_margin = opt_arg_int;
break;
case 'h':
left_margin = opt_arg_int;
right_margin = opt_arg_int;
break;
case 't':
tab_expand = opt_arg_string;
if (strlen(tab_expand) > 32) {
fprintf(stderr, "Tab expand too long.\n");
exit(1);
}
break;
case 'o':
output_fname = opt_arg_string;
break;
}
}
stralloc_init(&stralloc);
init_document(&doc, top_margin, bot_margin, left_margin);
read_file(&doc, stdin);
optimise_breaks(&doc);
build_document(&doc);
pdf_write(&doc.pdf, output_fname);
free_document(&doc);
stralloc_free(&stralloc);
return 0;
}