Class: PSWriter

Inherits:
Object
  • Object
show all
Includes:
PS_Private
Defined in:
lib/geotree/pswriter.rb

Overview

A debugging / demonstration utility class that generates postscript images

Constant Summary

Constants included from PS_Private

PS_Private::FONTHEIGHT_, PS_Private::LINETYPE_, PS_Private::LINEWIDTH_, PS_Private::LINEWIDTH_FACTOR_, PS_Private::LT_DASHED_, PS_Private::LT_DOTTED_, PS_Private::LT_SOLID_, PS_Private::RGB_, PS_Private::SCALE_, PS_Private::S_CLOSED_, PS_Private::S_OPEN_, PS_Private::S_START_, PS_Private::TRANS_

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ PSWriter

Returns a new instance of PSWriter.

Parameters:

  • path

    path of file to write (e.g. xxx.ps)



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
# File 'lib/geotree/pswriter.rb', line 50

def initialize(path)
  @path = path
  @line_width = -1
  @rgb = [-1,0,0]
  @phys_size = [612,792]
  @state = S_START_
  @stack = []
  @buffer_stack = []
  @dict = {}
  @dict_keys = []

  de("A","{arc} bind")
  de("CP","{closepath} bind")
  de('F','{fill} bind')
  de('I','{index} bind')
  de("L","{lineto} bind")
  de("M","{moveto} bind")
  de("NP","{newpath} bind")
  de('SRGB','{setrgbcolor} bind')
  de('SLW','{setlinewidth} bind')
  de('P','{pop} bind')
  de("R","{rmoveto} bind")
  de("S","{stroke} bind")
  de('SCL','{scale} bind')
  de('TR','{translate} bind')
  de("V","{rlineto} bind")
  de("DSH","{setdash} bind")

  set_logical_page_size(1000,1200)

  @line_type = 0
  @scale_ = 0
  @font_height = 0
  @scaled_font_height = 0

  @page_used = false
  @s = ''
end

Instance Method Details

#add_element(key, val) ⇒ Object

Define an element by placing it in the dictionary



390
391
392
# File 'lib/geotree/pswriter.rb', line 390

def add_element(key,val)
  de(key,'{'+val+'}')
end

#closeObject

Close document and write to disk



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/geotree/pswriter.rb', line 104

def close
  if @state < S_CLOSED_
    if @stack.size != 0
      warn("state stack nonempty for #{@path}")
    end

    flush_page

    # Construct file by combining header, dictionary, and
    # the user text

    s = get_doc_header
    s << get_doc_dictionary
    s << @s
    set_state(S_CLOSED_)

    write_text_file(@path, s)
  end
end

#draw_circle(cx, cy, radius) ⇒ Object



193
194
195
196
197
198
199
200
201
# File 'lib/geotree/pswriter.rb', line 193

def draw_circle(cx, cy, radius)
  de("CH", "{NP 0 360 A CP S}")
  a("NP");
  a(cx);
  a(cy);
  a(radius);
  a("CH");
  cr
end

#draw_disc(cx, cy, radius) ⇒ Object



184
185
186
187
188
189
190
191
# File 'lib/geotree/pswriter.rb', line 184

def draw_disc(cx,cy,radius)
  de("CF", "{NP 0 360 A CP F}")
  a(cx);
  a(cy);
  a(radius);
  a("CF")
  cr
end

#draw_element(key) ⇒ Object

Raises:

  • (ArgumentError)


394
395
396
397
398
# File 'lib/geotree/pswriter.rb', line 394

def draw_element(key)
  val = @dict[key]
  raise ArgumentError if !@dict.member?(key)
  a(key)
end

#draw_line(x1, y1, x2, y2) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/geotree/pswriter.rb', line 203

def draw_line(x1,y1,x2,y2)
  de("LN", "{NP 4 2 roll M L CP S}")

  #    a("NP");
  a(x1);
  a(y1);
  #    a("M");
  a(x2);
  a(y2);
  a("LN");
  cr
end

#draw_polygon(polygon, x, y) ⇒ Object

Draw a polygon

Parameters:

  • polygon

    array of 2n coordinates, defining closed n-gon

  • x

    translation to apply to coordinates

  • y


269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/geotree/pswriter.rb', line 269

def draw_polygon(polygon, x, y)

  push(translate(x, y))
  a("NP")
  i = 0
  while i < polygon.size
    a(polygon[i])
    a((polygon[i + 1]))
    if (i == 0)
      a("M");
    else
      a("L");
    end
    i += 2
  end
  a("CP S")
  cr
  pop()
end

#draw_rect(x, y, w, h, inset = 0) ⇒ Object

Draw a rectangle

Parameters:

  • inset (defaults to: 0)

    distance to inset rectangle boundary (positive: shrink; negative:expand)



137
138
139
140
141
142
143
144
145
# File 'lib/geotree/pswriter.rb', line 137

def draw_rect(x,y,w,h,inset = 0)
  de("RC","{3 I 3 I M 1 I 0 V 0 1 I V 1 I neg 0 V P P P P CP S }")
  a(x + inset)
  a(y + inset)
  a(w - 2 * inset);
  a(h - 2 * inset);
  a("RC");
  cr
end

#draw_string(string, x, y) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/geotree/pswriter.rb', line 164

def draw_string(string, x, y)
  if @font_height == 0
    set_font_size(28)
  end

  #    /TEXTL { currentpoint S M 0 0 R show } def
  #    % Right-justified text
  #    /TEXTR { currentpoint S M dup stringwidth pop neg 0 R show } def
  #    % Centered text
  de("TX", "{currentpoint S M dup stringwidth pop -2 div 0 R show }")

  a(x)
  a(y - @scaled_font_height / 2);
  a("M")
  work = make_eps_safe(string)
  a(work)
  a("TX")
  cr
end

#new_page(page_title) ⇒ Object

Start a new page

Parameters:

  • page_title

    title of new page



353
354
355
356
357
358
359
# File 'lib/geotree/pswriter.rb', line 353

def new_page(page_title)
  set_state(S_OPEN_);
  flush_page

  print_page_header(page_title);
  @page_used = true;
end

#page_sizeObject



131
132
133
# File 'lib/geotree/pswriter.rb', line 131

def page_size
  return @document_size
end

#pop(count = 1) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/geotree/pswriter.rb', line 368

def pop(count = 1)
  count.times do
    n = @stack.size   - 1
    op = @stack.pop
    case op.type
    when RGB_
      set_rgb(op.arg(0),op.arg(1),op.arg(2))
    when LINETYPE_
      set_line_type(op.arg(0))
    when LINEWIDTH_
      set_line_width(op.arg(0))
    when TRANS_
      translate(op.arg(0),op.arg(1))
    when FONTHEIGHT_
      set_font_size(op.arg(0))
    when SCALE_
      set_scale(op.arg(0))
    end
  end
end

#push(obj) ⇒ Object

Push previous state (returned by an operation) onto a stack to be restored later; call must be balanced by later call to pop()

Parameters:

  • obj

    state object returned by operation such as setGray(), translate()



364
365
366
# File 'lib/geotree/pswriter.rb', line 364

def push(obj)
  @stack << obj
end

#set_font_size(height) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/geotree/pswriter.rb', line 147

def set_font_size(height)
  raise IllegalStateException if @state != S_OPEN_

  ret = SOper.null

  if @font_height != height
    ret = SOper.new(FONTHEIGHT_, @font_height)
    @font_height = height;
    @scaled_font_height = height / @scale;
    a("/Monaco findfont ");
    a(@scaled_font_height);
    a("scalefont setfont\n");
    cr
  end
  ret
end

#set_gray(f) ⇒ Object



333
334
335
# File 'lib/geotree/pswriter.rb', line 333

def set_gray(f)
  set_rgb(f,f,f)
end

#set_line_dashedObject



220
221
222
# File 'lib/geotree/pswriter.rb', line 220

def set_line_dashed
  set_line_type(LT_DASHED_)
end

#set_line_dottedObject



224
225
226
# File 'lib/geotree/pswriter.rb', line 224

def set_line_dotted
  set_line_type(LT_DOTTED_)
end

#set_line_solidObject



216
217
218
# File 'lib/geotree/pswriter.rb', line 216

def set_line_solid
  set_line_type(LT_SOLID_)
end

#set_line_type(type) ⇒ Object



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
# File 'lib/geotree/pswriter.rb', line 239

def set_line_type(type)
  ret = SOper.null
  if  @line_type != type
    ret = SOper.new(LINETYPE_, @line_type)
    @line_type = type
    case type
    when LT_DASHED_
      n =    (@scale * 30).to_i
      a("[");
      a(n);
      a(n);
      a("] 0 DSH");
    when LT_DOTTED_
      int n =    (@scale * 30).to_i
      n2 = n / 4
      a("[");
      a(n2);
      a(n);
      a("] 0 DSH");
    else # LT_SOLID_
      a("[] 0 DSH");
    end
  end
  ret
end

#set_line_width(w) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
# File 'lib/geotree/pswriter.rb', line 307

def set_line_width(w)
  ret = SOper.null
  if @line_width != w

    ret = SOper.new(LINEWIDTH_, @line_width)
    @line_width = w
    a(LINEWIDTH_FACTOR_ * @scale   * @line_width)
    a("SLW");
  end
  ret
end

#set_logical_page_size(width, height) ⇒ Object

Set logical page size. Subsequent drawing operations will be scaled appropriately. Default logical page size is 1000 x 1200 units.



126
127
128
129
# File 'lib/geotree/pswriter.rb', line 126

def set_logical_page_size( width,  height)
  raise IllegalStateException if @state != S_START_
  @document_size = [width,height]
end

#set_rgb(r, g, b) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/geotree/pswriter.rb', line 319

def set_rgb(r,g,b)
  ret = SOper.null
  if (r != @rgb[0] || g != @rgb[1] || b != @rgb[2])

    ret = SOper.new(RGB_, @rgb[0], @rgb[1], @rgb[2])
    a(r)
    a(g)
    a(b)
    @rgb = [r,g,b]
    a("SRGB");
  end
  ret
end

#set_scale(f) ⇒ Object



228
229
230
231
232
233
234
235
236
237
# File 'lib/geotree/pswriter.rb', line 228

def set_scale(f)
  ret = SOper.null
  if   f != 1
    ret = SOper.new(SCALE_, 1.0 / f)
    a(f);
    a(f);
    a("SCL");
  end
  ret
end

#set_state(s) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/geotree/pswriter.rb', line 337

def set_state(  s)
  if (@state != s)
    case s
    when S_OPEN_
      raise IllegalStateException if @state != S_START_
      @state = s
      #        print_document_header
    when S_START_
      raise IllegalStateException
    end
    @state = s
  end
end

#start_bufferObject



89
90
91
92
93
# File 'lib/geotree/pswriter.rb', line 89

def start_buffer
  raise IllegalStateException if !@buffer_stack.empty?
  @buffer_stack << @s
  @s = ''
end

#stop_bufferObject



95
96
97
98
99
100
# File 'lib/geotree/pswriter.rb', line 95

def stop_buffer
  raise IllegalStateException if @buffer_stack.empty?
  ret = @s
  @s = @buffer_stack.pop
  ret
end

#translate(tx, ty, neg = false) ⇒ Object

Translate subsequent drawing operations



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/geotree/pswriter.rb', line 290

def translate(tx,ty,neg=false)

  ret = SOper.null
  if (neg)
    tx = -tx;
    ty = -ty;
  end
  if (tx != 0 || ty != 0)
    ret = SOper.new(TRANS_, -tx, -ty)

    a(tx);
    a(ty);
    a("TR");
  end
  ret
end