potracer 1.0.5 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -163,6 +163,50 @@ trace_trace (VALUE obj, VALUE bitmap, VALUE params)
163
163
  return obj;
164
164
  }
165
165
 
166
+ static VALUE
167
+ trace_as_svg (VALUE klass)
168
+ {
169
+ FILE *out = tmpfile();
170
+ char *out_buffer;
171
+ long size;
172
+ potrace_path_t *path;
173
+ int i, num_segments, *tag;
174
+ potrace_dpoint_t (*c)[3];
175
+ potrace_state_t *trace = NULL;
176
+ Data_Get_Struct(klass, potrace_state_t, trace);
177
+
178
+ fprintf(out, "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">");
179
+ fprintf(out, "<path fill-rule=\"evenodd\" fill=\"rgb(0,0,0)\" d=\"");
180
+ if (trace->status == POTRACE_STATUS_OK) {
181
+ path = trace->plist;
182
+ while (path != NULL) {
183
+ num_segments = path->curve.n;
184
+ tag = path->curve.tag;
185
+ c = path->curve.c;
186
+ SVG_MOVE_TO(out, c[num_segments-1][2]);
187
+ for (i = 0; i < num_segments; i++) {
188
+ switch (tag[i]) {
189
+ case POTRACE_CORNER:
190
+ SVG_LINE_TO(out, c[i][1]);
191
+ SVG_LINE_TO(out, c[i][2]);
192
+ break;
193
+ case POTRACE_CURVETO:
194
+ SVG_CURVE_TO(out, c[i]);
195
+ break;
196
+ }
197
+ }
198
+ path = path->next;
199
+ }
200
+ }
201
+ fprintf(out, "\" /></svg>");
202
+
203
+ size = ftell(out);
204
+ out_buffer = ALLOC_N(char, size);
205
+ rewind(out);
206
+ fread(out_buffer, 1, size, out);
207
+ return rb_str_new2(out_buffer);
208
+ }
209
+
166
210
  static VALUE
167
211
  trace_as_array (VALUE klass)
168
212
  {
@@ -307,6 +351,7 @@ Init_potracer () {
307
351
  rb_define_alloc_func(rb_cPotracerTrace, trace_alloc);
308
352
  rb_define_protected_method(rb_cPotracerTrace, "do_trace", trace_trace, 2);
309
353
  rb_define_method(rb_cPotracerTrace, "to_a", trace_as_array, 0);
354
+ rb_define_method(rb_cPotracerTrace, "to_svg", trace_as_svg, 0);
310
355
 
311
356
  // Define the Params class inside the Potracer module
312
357
  rb_cPotracerParams = rb_define_class_under(rb_mPotracer, "Params", rb_cObject);
@@ -20,6 +20,10 @@
20
20
  #define MOVE_TO(ar, c) PUSH_TR(ar, 3, "moveto", RB_PAIR(c))
21
21
  #define LINE_TO(ar, c) PUSH_TR(ar, 3, "lineto", RB_PAIR(c))
22
22
  #define CURVE_TO(ar, c) PUSH_TR(ar, 7, "curveto", RB_PAIR(c[0]), RB_PAIR(c[1]), RB_PAIR(c[2]))
23
+ #define SVG_PAIR(p) p.x, p.y
24
+ #define SVG_MOVE_TO(f, c) fprintf(f, "M %f %f ", SVG_PAIR(c))
25
+ #define SVG_LINE_TO(f, c) fprintf(f, "L %f %f ", SVG_PAIR(c))
26
+ #define SVG_CURVE_TO(f, c) fprintf(f, "C %f %f %f %f %f %f", SVG_PAIR(c[0]), SVG_PAIR(c[1]), SVG_PAIR(c[2]))
23
27
 
24
28
  static VALUE rb_mPotracer;
25
29
  static VALUE rb_mTurnpolicy;
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: potracer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: