Class: Origami::ContentStream

Inherits:
Stream
  • Object
show all
Defined in:
lib/origami/page.rb,
lib/origami/graphics/xobject.rb

Overview

A class representing a Stream containing the contents of a Page.

Constant Summary collapse

DEFAULT_SIZE =
12
DEFAULT_FONT =
:F1
DEFAULT_LEADING =
20
DEFAULT_STROKE_COLOR =
Graphics::Color::GrayScale.new(0.0)
DEFAULT_FILL_COLOR =
Graphics::Color::GrayScale.new(1.0)
DEFAULT_LINECAP =
Graphics::LineCapStyle::BUTT_CAP
DEFAULT_LINEJOIN =
Graphics::LineJoinStyle::MITER_JOIN
DEFAULT_DASHPATTERN =
Graphics::DashPattern.new([], 0)
DEFAULT_LINEWIDTH =
1.0

Constants inherited from Stream

Stream::DEFINED_FILTERS, Stream::TOKENS

Constants included from StandardObject

StandardObject::DEFAULT_ATTRIBUTES

Constants included from Object

Object::TOKENS

Instance Attribute Summary collapse

Attributes inherited from Stream

#dictionary

Attributes included from Object

#file_offset, #generation, #no, #objstm_offset, #parent

Instance Method Summary collapse

Methods inherited from Stream

#[], #[]=, #cast_to, #data, #data=, #decode!, #each_filter, #each_key, #each_pair, #encode!, #encoded_data, #encoded_data=, #filters, #key?, #keys, parse, #post_build, #set_predictor, #to_obfuscated_str, #to_s, #value

Methods included from TypeGuessing

#guess_type

Methods included from FieldAccessor

#method_missing, #respond_to_missing?

Methods included from StandardObject

included, #version_required

Methods included from Object

#cast_to, #copy, #document, #export, included, #indirect?, #indirect_parent, #logicalize, #logicalize!, #native_type, #numbered?, parse, #post_build, #reference, #set_document, #set_indirect, skip_until_next_obj, #solve, #to_o, #to_s, #type, typeof, #version_required, #xrefs

Constructor Details

#initialize(data = "", dictionary = {}) ⇒ ContentStream

Returns a new instance of ContentStream.



38
39
40
41
42
43
# File 'lib/origami/graphics/xobject.rb', line 38

def initialize(data = "", dictionary = {})
  super

  @instructions = nil
  @canvas = Graphics::DummyCanvas.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Origami::FieldAccessor

Instance Attribute Details

#canvasObject

Returns the value of attribute canvas.



36
37
38
# File 'lib/origami/graphics/xobject.rb', line 36

def canvas
  @canvas
end

Instance Method Details

#draw_image(name, attr = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/origami/graphics/xobject.rb', line 73

def draw_image(name, attr = {})
  load!

  x, y = attr[:x], attr[:y]

  @instructions << PDF::Instruction.new('q')
  @instructions << PDF::Instruction.new('cm', (attr[:w] || 300), 0, 0, (attr[:h] || 300), x, y)
  @instructions << PDF::Instruction.new('Do', name)
  @instructions << PDF::Instruction.new('Q')
end

#draw_line(from, to, attr = {}) ⇒ Object

Draw a straight line from the point at coord from, to the point at coord to.



87
88
89
# File 'lib/origami/graphics/xobject.rb', line 87

def draw_line(from, to, attr = {})
  draw_polygon([from, to], attr)
end

#draw_polygon(coords = [], attr = {}) ⇒ Object

Draw a polygon from a array of coordinates.



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
# File 'lib/origami/graphics/xobject.rb', line 94

def draw_polygon(coords = [], attr = {})
  load!

  stroke_color = attr.fetch(:stroke_color, DEFAULT_STROKE_COLOR)
  fill_color = attr.fetch(:fill_color, DEFAULT_FILL_COLOR)
  line_cap = attr.fetch(:line_cap, DEFAULT_LINECAP)
  line_join = attr.fetch(:line_join, DEFAULT_LINEJOIN)
  line_width = attr.fetch(:line_width, DEFAULT_LINEWIDTH)
  dash_pattern = attr.fetch(:dash, DEFAULT_DASHPATTERN)

  stroke = attr[:stroke].nil? || attr[:stroke]
  fill = attr[:fill].nil? ? false : attr[:fill]

  stroke = true if (fill == false) && (stroke == false)

  set_fill_color(fill_color) if fill
  set_stroke_color(stroke_color) if stroke
  set_line_width(line_width)
  set_line_cap(line_cap)
  set_line_join(line_join)
  set_dash_pattern(dash_pattern)

  if @canvas.gs.text_state.is_in_text_object?
    @instructions << PDF::Instruction.new('ET').render(@canvas)
  end

  unless coords.size < 1
    x, y = coords.slice!(0)
    @instructions << PDF::Instruction.new('m', x, y).render(@canvas)

    coords.each do |px, py|
      @instructions << PDF::Instruction.new('l', px, py).render(@canvas)
    end

    @instructions << (i =
                        if stroke && !fill
                          PDF::Instruction.new('s')
                        elsif fill && !stroke
                          PDF::Instruction.new('f')
                        elsif fill && stroke
                          PDF::Instruction.new('b')
                        end
                     )

    i.render(@canvas)
  end

  self
end

#draw_rectangle(x, y, width, height, attr = {}) ⇒ Object

Draw a rectangle at position (x,y) with defined width and height.



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
# File 'lib/origami/graphics/xobject.rb', line 147

def draw_rectangle(x, y, width, height, attr = {})
  load!

  stroke_color = attr.fetch(:stroke_color, DEFAULT_STROKE_COLOR)
  fill_color = attr.fetch(:fill_color, DEFAULT_FILL_COLOR)
  line_cap = attr.fetch(:line_cap, DEFAULT_LINECAP)
  line_join = attr.fetch(:line_join, DEFAULT_LINEJOIN)
  line_width = attr.fetch(:line_width, DEFAULT_LINEWIDTH)
  dash_pattern = attr.fetch(:dash, DEFAULT_DASHPATTERN)

  stroke = attr[:stroke].nil? || attr[:stroke]
  fill = attr[:fill].nil? ? false : attr[:fill]

  stroke = true if (fill == false) && (stroke == false)

  set_fill_color(fill_color) if fill
  set_stroke_color(stroke_color) if stroke
  set_line_width(line_width)
  set_line_cap(line_cap)
  set_line_join(line_join)
  set_dash_pattern(dash_pattern)

  if @canvas.gs.text_state.is_in_text_object?
    @instructions << PDF::Instruction.new('ET').render(@canvas)
  end

  @instructions << PDF::Instruction.new('re', x, y, width, height).render(@canvas)

  @instructions << (i =
                      if stroke && !fill
                        PDF::Instruction.new('S')
                      elsif fill && !stroke
                        PDF::Instruction.new('f')
                      elsif fill && stroke
                        PDF::Instruction.new('B')
                      end
                   )

  i.render(@canvas)

  self
end

#instructionsObject



67
68
69
70
71
# File 'lib/origami/graphics/xobject.rb', line 67

def instructions
  load!

  @instructions
end

#paint_shading(shade) ⇒ Object



234
235
236
237
238
239
240
# File 'lib/origami/graphics/xobject.rb', line 234

def paint_shading(shade)
  load!

  @instructions << PDF::Instruction.new('sh', shade).render(@canvas)

  self
end

#pre_buildObject

:nodoc:



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/origami/graphics/xobject.rb', line 55

def pre_build # :nodoc:
  unless @instructions.nil?
    if @canvas.gs.text_state.is_in_text_object?
      @instructions << PDF::Instruction.new('ET').render(@canvas)
    end

    @data = @instructions.join
  end

  super
end

#render(engine) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/origami/graphics/xobject.rb', line 45

def render(engine)
  load!

  @instructions.each do |instruction|
    instruction.render(engine)
  end

  nil
end

#set_dash_pattern(pattern) ⇒ Object



380
381
382
383
384
385
386
387
388
# File 'lib/origami/graphics/xobject.rb', line 380

def set_dash_pattern(pattern)
  load!

  unless @canvas.gs.dash_pattern.eql? pattern
    @instructions << PDF::Instruction.new('d', pattern.array, pattern.phase).render(@canvas)
  end

  self
end

#set_fill_color(color) ⇒ Object



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
# File 'lib/origami/graphics/xobject.rb', line 320

def set_fill_color(color)
  load!

  @instructions << (i =
                      if (color.respond_to?(:r) && color.respond_to?(:g) && color.respond_to?(:b)) || (color.is_a?(::Array) && (color.size == 3))
                        r = (color.respond_to?(:r) ? color.r : color[0]).to_f / 255
                        g = (color.respond_to?(:g) ? color.g : color[1]).to_f / 255
                        b = (color.respond_to?(:b) ? color.b : color[2]).to_f / 255
                        PDF::Instruction.new('rg', r, g, b) if @canvas.gs.nonstroking_color != [r, g, b]

                      elsif (color.respond_to?(:c) && color.respond_to?(:m) && color.respond_to?(:y) && color.respond_to?(:k)) || (color.is_a?(::Array) && (color.size == 4))
                        c = (color.respond_to?(:c) ? color.c : color[0]).to_f
                        m = (color.respond_to?(:m) ? color.m : color[1]).to_f
                        y = (color.respond_to?(:y) ? color.y : color[2]).to_f
                        k = (color.respond_to?(:k) ? color.k : color[3]).to_f
                        PDF::Instruction.new('k', c, m, y, k) if @canvas.gs.nonstroking_color != [c, m, y, k]

                      elsif color.respond_to?(:g) || (0.0..1.0).include?(color)
                        g = color.respond_to?(:g) ? color.g : color
                        PDF::Instruction.new('g', g) if @canvas.gs.nonstroking_color != [g]

                      else
                        raise TypeError, "Invalid color : #{color}"
                      end
                   )

  i&.render(@canvas)
  self
end

#set_line_cap(cap) ⇒ Object



400
401
402
403
404
405
406
407
408
# File 'lib/origami/graphics/xobject.rb', line 400

def set_line_cap(cap)
  load!

  if @canvas.gs.line_cap != cap
    @instructions << PDF::Instruction.new('J', cap).render(@canvas)
  end

  self
end

#set_line_join(join) ⇒ Object



410
411
412
413
414
415
416
417
418
# File 'lib/origami/graphics/xobject.rb', line 410

def set_line_join(join)
  load!

  if @canvas.gs.line_join != join
    @instructions << PDF::Instruction.new('j', join).render(@canvas)
  end

  self
end

#set_line_width(width) ⇒ Object



390
391
392
393
394
395
396
397
398
# File 'lib/origami/graphics/xobject.rb', line 390

def set_line_width(width)
  load!

  if @canvas.gs.line_width != width
    @instructions << PDF::Instruction.new('w', width).render(@canvas)
  end

  self
end

#set_stroke_color(color) ⇒ Object



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
# File 'lib/origami/graphics/xobject.rb', line 350

def set_stroke_color(color)
  load!

  @instructions << (i =
                      if (color.respond_to?(:r) && color.respond_to?(:g) && color.respond_to?(:b)) || (color.is_a?(::Array) && (color.size == 3))
                        r = (color.respond_to?(:r) ? color.r : color[0]).to_f / 255
                        g = (color.respond_to?(:g) ? color.g : color[1]).to_f / 255
                        b = (color.respond_to?(:b) ? color.b : color[2]).to_f / 255
                        PDF::Instruction.new('RG', r, g, b) if @canvas.gs.stroking_color != [r, g, b]

                      elsif (color.respond_to?(:c) && color.respond_to?(:m) && color.respond_to?(:y) && color.respond_to?(:k)) || (color.is_a?(::Array) && (color.size == 4))
                        c = (color.respond_to?(:c) ? color.c : color[0]).to_f
                        m = (color.respond_to?(:m) ? color.m : color[1]).to_f
                        y = (color.respond_to?(:y) ? color.y : color[2]).to_f
                        k = (color.respond_to?(:k) ? color.k : color[3]).to_f
                        PDF::Instruction.new('K', c, m, y, k) if @canvas.gs.stroking_color != [c, m, y, k]

                      elsif color.respond_to?(:g) || (0.0..1.0).include?(color)
                        g = color.respond_to?(:g) ? color.g : color
                        PDF::Instruction.new('G', g) if @canvas.gs.stroking_color != [g]

                      else
                        raise TypeError, "Invalid color : #{color}"
                      end
                   )

  i&.render(@canvas)
  self
end

#set_text_char_spacing(char_spacing) ⇒ Object



310
311
312
313
314
315
316
317
318
# File 'lib/origami/graphics/xobject.rb', line 310

def set_text_char_spacing(char_spacing)
  load!

  if char_spacing != @canvas.gs.text_state.char_spacing
    @instructions << PDF::Instruction.new('Tc', char_spacing).render(@canvas)
  end

  self
end

#set_text_font(fontname, size) ⇒ Object



242
243
244
245
246
247
248
249
250
# File 'lib/origami/graphics/xobject.rb', line 242

def set_text_font(fontname, size)
  load!

  if (fontname != @canvas.gs.text_state.font) || (size != @canvas.gs.text_state.font_size)
    @instructions << PDF::Instruction.new('Tf', fontname, size).render(@canvas)
  end

  self
end

#set_text_leading(leading) ⇒ Object



260
261
262
263
264
265
266
267
268
# File 'lib/origami/graphics/xobject.rb', line 260

def set_text_leading(leading)
  load!

  if leading != @canvas.gs.text_state.leading
    @instructions << PDF::Instruction.new('TL', leading).render(@canvas)
  end

  self
end

#set_text_pos(tx, ty) ⇒ Object



252
253
254
255
256
257
258
# File 'lib/origami/graphics/xobject.rb', line 252

def set_text_pos(tx, ty)
  load!

  @instructions << PDF::Instruction.new('Td', tx, ty).render(@canvas)

  self
end

#set_text_rendering(rendering) ⇒ Object



270
271
272
273
274
275
276
277
278
# File 'lib/origami/graphics/xobject.rb', line 270

def set_text_rendering(rendering)
  load!

  if rendering != @canvas.gs.text_state.rendering_mode
    @instructions << PDF::Instruction.new('Tr', rendering).render(@canvas)
  end

  self
end

#set_text_rise(rise) ⇒ Object



280
281
282
283
284
285
286
287
288
# File 'lib/origami/graphics/xobject.rb', line 280

def set_text_rise(rise)
  load!

  if rise != @canvas.gs.text_state.text_rise
    @instructions << PDF::Instruction.new('Ts', rise).render(@canvas)
  end

  self
end

#set_text_scale(scaling) ⇒ Object



290
291
292
293
294
295
296
297
298
# File 'lib/origami/graphics/xobject.rb', line 290

def set_text_scale(scaling)
  load!

  if scaling != @canvas.gs.text_state.scaling
    @instructions << PDF::Instruction.new('Tz', scaling).render(@canvas)
  end

  self
end

#set_text_word_spacing(word_spacing) ⇒ Object



300
301
302
303
304
305
306
307
308
# File 'lib/origami/graphics/xobject.rb', line 300

def set_text_word_spacing(word_spacing)
  load!

  if word_spacing != @canvas.gs.text_state.word_spacing
    @instructions << PDF::Instruction.new('Tw', word_spacing).render(@canvas)
  end

  self
end

#write(text, attr = {}) ⇒ Object

Adds text to the content stream with custom formatting attributes.

text

Text to write.

attr

Formatting attributes.



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
# File 'lib/origami/graphics/xobject.rb', line 195

def write(text, attr = {})
  load!

  x, y = attr[:x], attr[:y]
  font = attr.fetch(:font, DEFAULT_FONT)
  size = attr.fetch(:size, DEFAULT_SIZE)
  leading = attr.fetch(:leading, DEFAULT_LEADING)
  color = attr.fetch(:color, attr.fetch(:fill_color, DEFAULT_STROKE_COLOR))
  stroke_color = attr.fetch(:stroke_color, DEFAULT_STROKE_COLOR)
  line_width = attr.fetch(:line_width, DEFAULT_LINEWIDTH)
  word_spacing = attr.fetch(:word_spacing, @canvas.gs.text_state.word_spacing)
  char_spacing = attr.fetch(:char_spacing, @canvas.gs.text_state.char_spacing)
  scale = attr.fetch(:scale, @canvas.gs.text_state.scaling)
  rise = attr.fetch(:rise, @canvas.gs.text_state.text_rise)
  rendering = attr.fetch(:rendering, @canvas.gs.text_state.rendering_mode)

  @instructions << PDF::Instruction.new('ET').render(@canvas) if (x || y) && @canvas.gs.text_state.is_in_text_object?

  unless @canvas.gs.text_state.is_in_text_object?
    @instructions << PDF::Instruction.new('BT').render(@canvas)
  end

  set_text_font(font, size)
  set_text_pos(x, y) if x || y
  set_text_leading(leading)
  set_text_rendering(rendering)
  set_text_rise(rise)
  set_text_scale(scale)
  set_text_word_spacing(word_spacing)
  set_text_char_spacing(char_spacing)
  set_fill_color(color)
  set_stroke_color(stroke_color)
  set_line_width(line_width)

  write_text_block(text)

  self
end