indie-raster 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. data/.gitignore +5 -0
  2. data/LICENSE.txt +23 -0
  3. data/README.md +98 -0
  4. data/Rakefile +6 -0
  5. data/build-c/indie-raster +0 -0
  6. data/docs/PRF.md +41 -0
  7. data/fonts/README-fonts.md +26 -0
  8. data/fonts/fonts_prf1/dsm-22-italic.prf1 +0 -0
  9. data/fonts/fonts_prf1/dsm-22.prf1 +0 -0
  10. data/fonts/fonts_prf1/dsm-24.prf1 +0 -0
  11. data/fonts/fonts_prf1/dsm-56-bold-inverted.prf1 +0 -0
  12. data/fonts/fonts_vector/dsm/DroidSansMono.ttf +0 -0
  13. data/fonts/fonts_vector/dsm/DroidSansMono.webloc +8 -0
  14. data/fonts/fonts_vector/dsm/LICENSE.txt +202 -0
  15. data/fonts/fonts_vector/dsm/chars-all.json +201 -0
  16. data/fonts/fonts_vector/dsm/chars.json +5 -0
  17. data/fonts/fonts_vector/dsm/chars.txt +22 -0
  18. data/fonts/glyphs/dsm-22-italic.png +0 -0
  19. data/fonts/glyphs/dsm-22.png +0 -0
  20. data/fonts/glyphs/dsm-24.png +0 -0
  21. data/fonts/glyphs/dsm-56-bold-inverted.png +0 -0
  22. data/fonts/make-fonts.sh +20 -0
  23. data/fonts/scripts/html-entities.json +255 -0
  24. data/fonts/scripts/pack-font.coffee +55 -0
  25. data/fonts/scripts/splitter.c +55 -0
  26. data/fonts/scripts/splitter.sh +5 -0
  27. data/fonts/scripts/text-from-json.coffee +20 -0
  28. data/fonts/scripts/util.coffee +39 -0
  29. data/indie-raster.gemspec +19 -0
  30. data/make.sh +13 -0
  31. data/src-c/canvas.c +93 -0
  32. data/src-c/canvas.h +29 -0
  33. data/src-c/commands.c +148 -0
  34. data/src-c/commands.h +26 -0
  35. data/src-c/fonts.c +66 -0
  36. data/src-c/fonts.h +26 -0
  37. data/src-c/main.c +16 -0
  38. data/src-c/make.sh +11 -0
  39. data/src-c/pnm.c +109 -0
  40. data/src-c/pnm.h +20 -0
  41. data/src-c/session.c +36 -0
  42. data/src-c/session.h +24 -0
  43. data/src-c/upc.c +83 -0
  44. data/src-c/upc.h +9 -0
  45. data/src-ruby/indie-raster.rb +95 -0
  46. metadata +99 -0
data/src-c/commands.c ADDED
@@ -0,0 +1,148 @@
1
+
2
+ #include "commands.h"
3
+
4
+
5
+ #define LINEMAX 1000
6
+
7
+ void run_commands(Session *s, FILE *f, FILE *outfile) {
8
+
9
+ char line[LINEMAX + 1];
10
+ char *tok;
11
+
12
+ while (fgets(line, LINEMAX, f)) {
13
+ if ((line[0] == '#') || (line[0] == 0)) {
14
+ continue;
15
+ }
16
+ tok = strtok(line, " \t\n");
17
+ if (!(tok && tok[0])) {
18
+ continue;
19
+ }
20
+
21
+ if (strcmp(tok, "createBlank") == 0) {
22
+ long destId = atol(strtok(NULL, " \t\n"));
23
+ long w = atol(strtok(NULL, " \t\n"));
24
+ long h = atol(strtok(NULL, " \t\n"));
25
+ createBlank(s, destId, w, h);
26
+ }
27
+ else if (strcmp(tok, "exportP4") == 0) {
28
+ long srcId = atol(strtok(NULL, " \t\n"));
29
+ exportP4(s, srcId, outfile);
30
+ }
31
+ else if (strcmp(tok, "loadImage") == 0) {
32
+ long destId = atol(strtok(NULL, " \t\n"));
33
+ long size = atol(strtok(NULL, " \t\n"));
34
+ loadImage(s, destId, size, f);
35
+ }
36
+ else if (strcmp(tok, "loadFont") == 0) {
37
+ long destId = atol(strtok(NULL, " \t\n"));
38
+ long size = atol(strtok(NULL, " \t\n"));
39
+ loadFont(s, destId, size, f);
40
+ }
41
+ else if (strcmp(tok, "drawImage") == 0) {
42
+ long destId = atol(strtok(NULL, " \t\n"));
43
+ long x = atol(strtok(NULL, " \t\n"));
44
+ long y = atol(strtok(NULL, " \t\n"));
45
+ long srcId = atol(strtok(NULL, " \t\n"));
46
+ drawImage(s, destId, x, y, srcId);
47
+ }
48
+ else if (strcmp(tok, "drawUPC") == 0) {
49
+ long destId = atol(strtok(NULL, " \t\n"));
50
+ long x = atol(strtok(NULL, " \t\n"));
51
+ long y = atol(strtok(NULL, " \t\n"));
52
+ char *digits = strtok(NULL, " \t\n");
53
+ long w = atol(strtok(NULL, " \t\n"));
54
+ long h = atol(strtok(NULL, " \t\n"));
55
+ long extraHeight = atol(strtok(NULL, " \t\n"));
56
+ drawUPC(s, destId, x, y, digits, w, h, extraHeight);
57
+ }
58
+ else if (strcmp(tok, "drawText") == 0) {
59
+ long destId = atol(strtok(NULL, " \t\n"));
60
+ long x0 = atol(strtok(NULL, " \t\n"));
61
+ long y0 = atol(strtok(NULL, " \t\n"));
62
+ long fontId = atol(strtok(NULL, " \t\n"));
63
+ long numChars = atol(strtok(NULL, " \t\n"));
64
+ drawText(s, destId, x0, y0, fontId, numChars, f);
65
+ }
66
+ }
67
+ }
68
+
69
+
70
+ void createBlank(Session *s, long destId, long w, long h) {
71
+ Canvas *c = Canvas_new(w, h);
72
+ Session_setCanvas(s, destId, c);
73
+ }
74
+
75
+
76
+ void exportP4(Session *s, long srcId, FILE *outfile) {
77
+ Canvas *c = Session_getCanvas(s, srcId);
78
+ Canvas_writeP4(c, outfile);
79
+ }
80
+
81
+
82
+ void loadImage(Session *s, long destId, long size, FILE *f) {
83
+ Canvas *c = Canvas_readImage(f);
84
+ Session_setCanvas(s, destId, c);
85
+ }
86
+
87
+
88
+ void loadFont(Session *s, long destId, long size, FILE *f) {
89
+
90
+ Font *font = fonts_readFont(f);
91
+ Session_setItem(s, destId, (void *)font);
92
+ }
93
+
94
+ void drawText(Session *s, long destId, long x0, long y0, long fontId, long numChars, FILE *f) {
95
+
96
+ long size = numChars * 4;
97
+ void *text = malloc(size);
98
+ fread(text, 1, size, f);
99
+
100
+ Canvas *c = Session_getCanvas(s, destId);
101
+ Font *font = (Font *)Session_getItem(s, fontId);
102
+
103
+ fonts_drawText(c, font, numChars, (uint32_t*)text, x0, y0);
104
+
105
+ free(text);
106
+ }
107
+
108
+
109
+ void drawImage(Session *s, long destId, long x, long y, long srcId) {
110
+ Canvas *src = Session_getCanvas(s, srcId);
111
+ Canvas *dest = Session_getCanvas(s, destId);
112
+
113
+ Canvas_drawOnto(src, dest, x, y);
114
+ }
115
+
116
+
117
+ void drawUPC(Session *s, long destId, long x0, long y0, char *digits, long w, long h, long extraHeight) {
118
+
119
+ Canvas *dest = Session_getCanvas(s, destId);
120
+ long y, bar, dest_x, dest_y;
121
+ int barWidth = w / 95;
122
+
123
+ char *bitstring = upc_bitstring(digits);
124
+
125
+ // This could be optimized if needed.
126
+ for (y = 0; y < (h + extraHeight); y++) {
127
+ dest_y = y + y0;
128
+ if (dest_y < dest->h) {
129
+ for (bar = 0; bar < 95; bar++) {
130
+ if ((y < h) || upc_bar_is_guard_bar(bar)) {
131
+ for (
132
+ dest_x = (x0 + (barWidth * bar));
133
+ dest_x < (x0 + (barWidth * (bar + 1)));
134
+ dest_x++) {
135
+ if (dest_x < dest->w) {
136
+ dest->pixels[(dest_y * dest->w) + dest_x] = ((bitstring[bar] == '1')) ? 0 : 255;
137
+ }
138
+ }
139
+ }
140
+ }
141
+ }
142
+ }
143
+ free(bitstring);
144
+ }
145
+
146
+
147
+
148
+
data/src-c/commands.h ADDED
@@ -0,0 +1,26 @@
1
+
2
+ #ifndef COMMANDS_INCLUDED
3
+ #define COMMANDS_INCLUDED
4
+
5
+ #include <stdio.h>
6
+ #include <string.h>
7
+ #include <stdint.h>
8
+ #include <stdlib.h>
9
+
10
+ #include "upc.h"
11
+ #include "session.h"
12
+ #include "canvas.h"
13
+ #include "fonts.h"
14
+
15
+
16
+
17
+ void run_commands(Session *s, FILE *f, FILE *outfile);
18
+ void createBlank(Session *s, long destId, long w, long h);
19
+ void exportP4(Session *s, long srcId, FILE *outfile);
20
+ void loadImage(Session *s, long destId, long size, FILE *f);
21
+ void loadFont(Session *s, long destId, long size, FILE *f);
22
+ void drawText(Session *s, long destId, long x, long y, long fontId, long numChars, FILE *f);
23
+ void drawImage(Session *s, long destId, long x, long y, long srcId);
24
+ void drawUPC(Session *s, long destId, long x, long y, char *digits, long w, long h, long extraHeight);
25
+
26
+ #endif
data/src-c/fonts.c ADDED
@@ -0,0 +1,66 @@
1
+
2
+ #include "fonts.h"
3
+
4
+
5
+
6
+ Font *fonts_readFont(FILE *f) {
7
+
8
+ Font *font = (Font *)(malloc(sizeof(Font)));
9
+
10
+ uint32_t u32, size, i;
11
+
12
+ // "PRF1"
13
+ char magic[4];
14
+ fread(magic, 1, 4, f);
15
+
16
+ // uint32 num_chars
17
+ fread(&u32, 1, 4, f);
18
+ font->numChars = u32;
19
+
20
+ // uint32 image_width
21
+ fread(&u32, 1, 4, f);
22
+ font->w = u32;
23
+
24
+ // uint32 image_height
25
+ fread(&u32, 1, 4, f);
26
+ font->h = u32;
27
+
28
+
29
+ // uint32+ char_codes
30
+ size = 4 * font->numChars;
31
+ font->codepoints = (uint32_t *)malloc(size);
32
+ fread(font->codepoints, 1, size, f);
33
+
34
+ // uint32+ image_byte_offets
35
+ // (ignore them)
36
+ size = 4 * font->numChars;
37
+ void *temp = malloc(size);
38
+ fread(temp, 1, size, f);
39
+ free(temp);
40
+
41
+ // data+ images
42
+ font->glyphs = (Canvas **)(malloc(sizeof(Canvas *) * font->numChars));
43
+ for (i = 0; i < font->numChars; i++) {
44
+ font->glyphs[i] = Canvas_readImage(f);
45
+ }
46
+
47
+ return font;
48
+ }
49
+
50
+
51
+ void fonts_drawText(Canvas *c, Font *font, long numChars, uint32_t *text, long x0, long y0) {
52
+
53
+ uint32_t i, j, codepoint;
54
+ long x = x0;
55
+
56
+ for(i = 0; i < numChars; i++) {
57
+ codepoint = text[i];
58
+ for(j = 0; j < font->numChars; j++) {
59
+ if (font->codepoints[j] == codepoint) {
60
+ Canvas_drawOnto(font->glyphs[j], c, x, y0);
61
+ x += font->w;
62
+ }
63
+ }
64
+ }
65
+ }
66
+
data/src-c/fonts.h ADDED
@@ -0,0 +1,26 @@
1
+
2
+ #ifndef FONTS_INCLUDED
3
+ #define FONTS_INCLUDED
4
+
5
+ #include <stdint.h>
6
+ #include <stdlib.h>
7
+ #include "canvas.h"
8
+
9
+
10
+ typedef struct {
11
+
12
+ uint32_t numChars;
13
+ uint32_t w;
14
+ uint32_t h;
15
+
16
+ uint32_t *codepoints;
17
+ Canvas **glyphs;
18
+
19
+ } Font;
20
+
21
+
22
+ Font *fonts_readFont(FILE *f);
23
+ void fonts_drawText(Canvas *c, Font *font, long numChars, uint32_t *text, long x0, long y0);
24
+
25
+
26
+ #endif
data/src-c/main.c ADDED
@@ -0,0 +1,16 @@
1
+
2
+ #include <stdlib.h>
3
+ #include <stdio.h>
4
+
5
+ #include "commands.h"
6
+ #include "session.h"
7
+
8
+
9
+ int main(int argc, char *argv[]) {
10
+
11
+ Session *s = Session_new();
12
+ run_commands(s, stdin, stdout);
13
+
14
+ return 0;
15
+ }
16
+
data/src-c/make.sh ADDED
@@ -0,0 +1,11 @@
1
+ #!/bin/bash
2
+
3
+ gcc \
4
+ -o ../build-c/indie-raster \
5
+ canvas.c commands.c fonts.c main.c session.c upc.c pnm.c
6
+
7
+ # -O2 \
8
+
9
+
10
+
11
+ # bash make.sh && cat ../example/commands2 | ../build-c/indie-raster > temp.pbm && convert temp.pbm temp.png && open temp.png
data/src-c/pnm.c ADDED
@@ -0,0 +1,109 @@
1
+
2
+ #include <stdio.h>
3
+ #include <string.h>
4
+ #include <stdlib.h>
5
+ #include "pnm.h"
6
+
7
+ #define UTIL_PNM_LINE_MAX 100
8
+
9
+
10
+ void util_read_pnm_header(FILE *f, int *p, long *w, long *h, int *maxValue) {
11
+ // Note: standard whitespace assumed
12
+
13
+ char line[UTIL_PNM_LINE_MAX + 1];
14
+ char *tok;
15
+
16
+ // "P4\n"
17
+ fgets(line, UTIL_PNM_LINE_MAX, f);
18
+ *p = line[1] - 0x30;
19
+
20
+ // "300 200\n"
21
+ fgets(line, UTIL_PNM_LINE_MAX, f);
22
+ tok = strtok(line, " \n");
23
+ *w = atol(tok);
24
+ tok = strtok(NULL, " \n");
25
+ *h = atol(tok);
26
+
27
+ if ((*p == 4) || (*p == 1)) {
28
+ *maxValue = 1;
29
+ }
30
+ else {
31
+ fgets(line, UTIL_PNM_LINE_MAX, f);
32
+ *maxValue = atoi(line);
33
+ }
34
+ }
35
+
36
+
37
+ //### P6
38
+
39
+ void util_write_p6(FILE *f, long w, long h, unsigned char *pixels) {
40
+ fprintf(f, "P6\n%ld %ld\n255\n", w, h);
41
+ //fwrite(TODO);
42
+ }
43
+
44
+ //### P5
45
+
46
+ void util_write_p5(FILE *f, long w, long h, unsigned char *pixels) {
47
+ fprintf(f, "P5\n%ld %ld\n255\n", w, h);
48
+ //fwrite(TODO);
49
+ }
50
+
51
+ //### P4
52
+
53
+ void util_write_p4(FILE *f, long w, long h, unsigned char *pixels) {
54
+ fprintf(f, "P4\n%ld %ld\n", w, h);
55
+ util_write_p4_body(f, w, h, pixels);
56
+ }
57
+
58
+
59
+ void util_write_p4_body(FILE *f, long w, long h, unsigned char *pixels) {
60
+ util_write_p4_body_from_subrect(f, 0, 0, w, h, w, pixels);
61
+ }
62
+
63
+ void util_write_p4_body_from_subrect(FILE *f, long x0, long y0, long w, long h, long pixels_w, unsigned char *pixels) {
64
+ long pos, x, y;
65
+ int c, i, value;
66
+ c = 0;
67
+ for (y = y0; y < (h + y0); y++) {
68
+ i = 0;
69
+ for (x = x0; x < (w + x0); x++) {
70
+ pos = (y * (pixels_w)) + x;
71
+ value = ((pixels[pos] > 127) ? 0 : 1); // 1 is black
72
+ c |= (value << (7 - i));
73
+
74
+ // Write byte?
75
+ if ((i == 7) || (x == ((w + x0) - 1))) {
76
+ fputc(c, f);
77
+ c = 0;
78
+ }
79
+
80
+ i = (i + 1) % 8;
81
+ }
82
+ }
83
+ }
84
+
85
+
86
+ void util_read_p4_body(FILE *f, long w, long h, unsigned char **pixels_) {
87
+
88
+ int i, c;
89
+ long x, y, pos;
90
+ unsigned char *pixels;
91
+
92
+ pixels = (unsigned char *)malloc(w * h);
93
+
94
+ pos = 0;
95
+ for (y = 0; y < h; y++) {
96
+ for (x = 0; x < w; x++) {
97
+ i = x % 8;
98
+ if (i == 0) {
99
+ c = fgetc(f);
100
+ //ASSERT c > 0
101
+ }
102
+ pixels[pos] = ((c >> (7 - i)) & 1) ? 0 : 255;
103
+ pos++;
104
+ }
105
+ }
106
+
107
+ *pixels_ = pixels;
108
+ }
109
+
data/src-c/pnm.h ADDED
@@ -0,0 +1,20 @@
1
+
2
+ #ifndef UTIL_PNM_INCLUDED
3
+ #define UTIL_PNM_INCLUDED
4
+
5
+ #include <stdio.h>
6
+
7
+
8
+ void util_read_pnm_header(FILE *f, int *p, long *w, long *h, int *maxValue);
9
+
10
+ void util_write_p6(FILE *f, long w, long h, unsigned char *pixels);
11
+
12
+ void util_write_p5(FILE *f, long w, long h, unsigned char *pixels);
13
+
14
+ void util_write_p4(FILE *f, long w, long h, unsigned char *pixels);
15
+ void util_write_p4_body(FILE *f, long w, long h, unsigned char *pixels);
16
+ void util_write_p4_body_from_subrect(FILE *f, long x0, long y0, long w, long h, long pixels_w, unsigned char *pixels);
17
+ void util_read_p4_body(FILE *f, long w, long h, unsigned char **pixels_);
18
+
19
+
20
+ #endif
data/src-c/session.c ADDED
@@ -0,0 +1,36 @@
1
+
2
+ #include <stdlib.h>
3
+ #include "session.h"
4
+
5
+
6
+ Session* Session_new() {
7
+ Session *s = (Session*)calloc(1, sizeof(Session));
8
+ return s;
9
+ }
10
+
11
+
12
+ void Session_free(Session **s) {
13
+ free(*s);
14
+ *s = NULL;
15
+ }
16
+
17
+
18
+ void Session_setCanvas(Session *s, long id, Canvas *canvas) {
19
+ Session_setItem(s, id, (void*)canvas);
20
+ }
21
+
22
+
23
+ Canvas * Session_getCanvas(Session *s, long id) {
24
+ return (Canvas *)Session_getItem(s, id);
25
+ }
26
+
27
+
28
+ void Session_setItem(Session *s, long id, void *data) {
29
+ s->items[id] = data;
30
+ }
31
+
32
+
33
+ void * Session_getItem(Session *s, long id) {
34
+ return s->items[id];
35
+ }
36
+
data/src-c/session.h ADDED
@@ -0,0 +1,24 @@
1
+
2
+ #ifndef SESSION_INCLUDED
3
+ #define SESSION_INCLUDED
4
+
5
+ #include "canvas.h"
6
+
7
+
8
+ typedef struct {
9
+
10
+ long numBins;
11
+
12
+ void *items[1000];
13
+
14
+ } Session;
15
+
16
+ Session* Session_new();
17
+ void Session_free(Session **s);
18
+ void Session_setCanvas(Session *s, long id, Canvas *canvas);
19
+ Canvas * Session_getCanvas(Session *s, long id);
20
+ void Session_setItem(Session *s, long id, void *data);
21
+ void * Session_getItem(Session *s, long id);
22
+
23
+ #endif
24
+
data/src-c/upc.c ADDED
@@ -0,0 +1,83 @@
1
+
2
+ #include <stdlib.h>
3
+ #include <stdio.h>
4
+ #include <string.h>
5
+
6
+ #include "upc.h"
7
+
8
+ /*
9
+ Note: 1 is black
10
+
11
+ 101 0 1 2
12
+ 0101111 3 4 5 6 7 8 9
13
+ 0101111 10
14
+ 0101111 17
15
+ 0101111 24
16
+ 0101111 31
17
+ 0101111 38 ... 44
18
+ 01010 45 ... 49
19
+ 1100110 50 ...
20
+ 1100110 57
21
+ 1100110 64
22
+ 1100110 71
23
+ 1100110 78
24
+ 1100110 85 ... 91
25
+ 101 92 93 94
26
+ */
27
+
28
+ char *LEFT_DIGITS[] = {
29
+ (char*)"0001101",
30
+ (char*)"0011001",
31
+ (char*)"0010011",
32
+ (char*)"0111101",
33
+ (char*)"0100011",
34
+ (char*)"0110001",
35
+ (char*)"0101111",
36
+ (char*)"0111011",
37
+ (char*)"0110111",
38
+ (char*)"0001011"
39
+ };
40
+
41
+ char *RIGHT_DIGITS[] = {
42
+ (char*)"1110010",
43
+ (char*)"1100110",
44
+ (char*)"1101100",
45
+ (char*)"1000010",
46
+ (char*)"1011100",
47
+ (char*)"1001110",
48
+ (char*)"1010000",
49
+ (char*)"1000100",
50
+ (char*)"1001000",
51
+ (char*)"1110100"
52
+ };
53
+
54
+
55
+ int upc_bar_is_guard_bar(int bar) {
56
+ return (bar < (0 + 3)) || (bar > (94 - 3)) || ((bar >= 45) && (bar < (45 + 5)));
57
+ }
58
+
59
+
60
+ char* upc_bitstring(char *digits) {
61
+
62
+ int i;
63
+
64
+ // Blank string
65
+ char *bs = (char*)malloc(95 + 1);
66
+ bs[95] = 0;
67
+
68
+ // Guard bars
69
+ strncpy(&(bs[0]), "101", 3);
70
+ strncpy(&(bs[45]), "01010", 5);
71
+ strncpy(&(bs[92]), "101", 3);
72
+
73
+ // Digits
74
+ for (i = 0; i < 6; i++) {
75
+ strncpy(&(bs[3 + 7 * i]), LEFT_DIGITS[digits[i] - 0x30], 7);
76
+ }
77
+ for (i = 0; i < 6; i++) {
78
+ strncpy(&(bs[50 + 7 * i]), RIGHT_DIGITS[digits[i + 6] - 0x30], 7);
79
+ }
80
+
81
+ return bs;
82
+ }
83
+
data/src-c/upc.h ADDED
@@ -0,0 +1,9 @@
1
+
2
+ #ifndef UPC_INCLUDED
3
+ #define UPC_INCLUDED
4
+
5
+ int upc_bar_is_guard_bar(int bar);
6
+ char* upc_bitstring(char *digits);
7
+
8
+
9
+ #endif
@@ -0,0 +1,95 @@
1
+
2
+ require 'Open3'
3
+
4
+
5
+ def ascii_to_utf32(str)
6
+ arr = []
7
+ str.split('').each do |c|
8
+ arr.push c, "\000\000\000"
9
+ end
10
+ return arr.join('')
11
+ end
12
+
13
+ def ith_byte(i, str)
14
+ x = str[i]
15
+ x = x.ord if x.class == String # Ruby 1.9+
16
+ return x
17
+ end
18
+
19
+ def dataFromOpt(opt)
20
+ if opt[:data]
21
+ return opt[:data]
22
+ elsif opt[:path]
23
+ File.open(opt[:path], "r") do |f|
24
+ return f.read
25
+ end
26
+ end
27
+ end
28
+
29
+
30
+ class IndieRasterSession
31
+
32
+ def initialize
33
+ @c = []
34
+ @fonts = {}
35
+ end
36
+
37
+ def createBlank(opt)
38
+ @c.push "createBlank #{opt[:id] or 1} #{opt[:w]} #{opt[:h]}\n"
39
+ end
40
+
41
+ def loadImage(opt)
42
+ data = dataFromOpt opt
43
+ @c.push "loadImage #{opt[:id]} #{data.length}\n"
44
+ @c.push data
45
+ end
46
+
47
+ def loadFont(opt)
48
+ data = dataFromOpt opt
49
+ @fonts[opt[:id]] = data
50
+ @c.push "loadFont #{opt[:id]} #{data.length}\n"
51
+ @c.push data
52
+ end
53
+
54
+ def drawImage(opt)
55
+ @c.push "drawImage #{opt[:on] or 1} #{opt[:x]} #{opt[:y]} #{opt[:id]}\n"
56
+ end
57
+
58
+ def widthOfText(opt)
59
+ glyphWidth = ith_byte 8, @fonts[opt[:font]] #ASSUMPTION: glyph width < 256px
60
+ return glyphWidth * opt[:text].length
61
+ end
62
+
63
+ def drawText(opt)
64
+ data = ascii_to_utf32 opt[:text]
65
+ if opt[:toLeftOf]
66
+ opt[:x] = opt[:toLeftOf] - self.widthOfText(opt)
67
+ end
68
+ @c.push "drawText #{opt[:on] or 1} #{opt[:x]} #{opt[:y]} #{opt[:font]} #{data.length / 4}\n"
69
+ @c.push data
70
+ end
71
+
72
+ def drawUPC(opt)
73
+ @c.push "drawUPC #{opt[:on] or 1} #{opt[:x]} #{opt[:y]} #{opt[:digits]} #{opt[:w]} #{opt[:h]} #{opt[:guard_bar_extra_h] or 0} #{opt[:middle_font] or 0} #{opt[:side_font] or 0}\n"
74
+ end
75
+
76
+ def exportP4(opt = {})
77
+
78
+ @c.push "exportP4 #{opt[:id] or 1}\n"
79
+
80
+ cmd = File.expand_path(File.dirname(__FILE__) + "/../build-c/indie-raster")
81
+ input = @c.join ''
82
+
83
+ stdin, stdout, stderr = Open3.popen3 cmd
84
+ stdin.puts input
85
+ stdin.close
86
+ code = $?.to_i
87
+ out = stdout.read
88
+ err = stderr.read
89
+ #TODO handle error
90
+
91
+ return out
92
+ end
93
+
94
+ end
95
+