| Class | Origami::ContentStream |
| In: |
sources/parser/graphics/xobject.rb
|
| Parent: | Stream |
| 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 |
# File sources/parser/graphics/xobject.rb, line 44
44: def initialize(rawdata = "", dictionary = {})
45:
46: @instructions = []
47: @gs = Graphics::State.new
48:
49: super(rawdata, dictionary)
50: end
Draw a straight line from the point at coord from, to the point at coord to.
# File sources/parser/graphics/xobject.rb, line 55
55: def draw_line(from, to, attr = {})
56: draw_polygon([from, to], attr)
57: end
Draw a polygon from a array of coordinates.
# File sources/parser/graphics/xobject.rb, line 62
62: def draw_polygon(coords = [], attr = {})
63:
64: stroke_color = attr[:stroke_color] || DEFAULT_STROKE_COLOR
65: fill_color = attr[:fill_color] || DEFAULT_FILL_COLOR
66: line_cap = attr[:line_cap] || DEFAULT_LINECAP
67: line_join = attr[:line_join] || DEFAULT_LINEJOIN
68: line_width = attr[:line_width] || DEFAULT_LINEWIDTH
69: dash_pattern = attr[:dash] || DEFAULT_DASHPATTERN
70:
71: stroke = attr[:stroke].nil? ? true : attr[:stroke]
72: fill = attr[:fill].nil? ? false : attr[:fill]
73:
74: stroke = true if fill == false and stroke == false
75:
76: set_fill_color(fill_color) if fill
77: set_stroke_color(stroke_color) if stroke
78: set_line_width(line_width)
79: set_line_cap(line_cap)
80: set_line_join(line_join)
81: set_dash_pattern(dash_pattern)
82:
83: if @gs.text_state.is_in_text_object?
84: @instructions << Text::Instruction::ET.new.update_state(@gs)
85: end
86:
87: unless coords.size < 1
88: x,y = coords.slice!(0)
89: @instructions << Graphics::Instruction::M.new(x,y).update_state(@gs)
90:
91: coords.each do |px,py|
92: @instructions << Graphics::Instruction::L.new(px,py).update_state(@gs)
93: end
94:
95: @instructions << (i =
96: if stroke and not fill
97: Graphics::Instruction::CloseS.new
98: elsif fill and not stroke
99: Graphics::Instruction::F.new
100: elsif fill and stroke
101: Graphics::Instruction::CloseB.new
102: end
103: )
104:
105: i.update_state(@gs)
106: end
107:
108: self
109: end
Draw a rectangle at position (x,y) with defined width and height.
# File sources/parser/graphics/xobject.rb, line 114
114: def draw_rectangle(x, y, width, height, attr = {})
115:
116: stroke_color = attr[:stroke_color] || DEFAULT_STROKE_COLOR
117: fill_color = attr[:fill_color] || DEFAULT_FILL_COLOR
118: line_cap = attr[:line_cap] || DEFAULT_LINECAP
119: line_join = attr[:line_join] || DEFAULT_LINEJOIN
120: line_width = attr[:line_width] || DEFAULT_LINEWIDTH
121: dash_pattern = attr[:dash] || DEFAULT_DASHPATTERN
122:
123: stroke = attr[:stroke].nil? ? true : attr[:stroke]
124: fill = attr[:fill].nil? ? false : attr[:fill]
125:
126: stroke = true if fill == false and stroke == false
127:
128: set_fill_color(fill_color) if fill
129: set_stroke_color(stroke_color) if stroke
130: set_line_width(line_width)
131: set_line_cap(line_cap)
132: set_line_join(line_join)
133: set_dash_pattern(dash_pattern)
134:
135: if @gs.text_state.is_in_text_object?
136: @instructions << Text::Instruction::ET.new.update_state(@gs)
137: end
138:
139: @instructions << Graphics::Instruction::RE.new(x,y,width,height).update_state(@gs)
140:
141: @instructions << (i =
142: if stroke and not fill
143: Graphics::Instruction::S.new
144: elsif fill and not stroke
145: Graphics::Instruction::F.new
146: elsif fill and stroke
147: Graphics::Instruction::B.new
148: end
149: )
150:
151: i.update_state(@gs)
152:
153: self
154: end
# File sources/parser/graphics/xobject.rb, line 199
199: def paint_shading(shade)
200: @instructions << Graphics::Instruction::SH.new(shade).update_state(@gs)
201:
202: self
203: end
# File sources/parser/graphics/xobject.rb, line 325
325: def set_dash_pattern(pattern)
326: unless @gs.dash_pattern.eql? pattern
327: @instructions << Graphics::Instruction::D.new(pattern.array, pattern.phase).update_state(@gs)
328: end
329:
330: self
331: end
# File sources/parser/graphics/xobject.rb, line 267
267: def set_fill_color(color)
268:
269: @instructions << ( i =
270: if (color.respond_to? :r and color.respond_to? :g and color.respond_to? :b) or (color.is_a?(::Array) and color.size == 3)
271: r = (color.respond_to?(:r) ? color.r : color[0]).to_f / 255
272: g = (color.respond_to?(:g) ? color.g : color[1]).to_f / 255
273: b = (color.respond_to?(:b) ? color.b : color[2]).to_f / 255
274: Graphics::Instruction::RG.new(r, g, b) if @gs.nonstroking_color != [r,g,b]
275:
276: elsif (color.respond_to? :c and color.respond_to? :m and color.respond_to? :y and color.respond_to? :k) or (color.is_a?(::Array) and color.size == 4)
277: c = (color.respond_to?(:c) ? color.c : color[0]).to_f
278: m = (color.respond_to?(:m) ? color.m : color[1]).to_f
279: y = (color.respond_to?(:y) ? color.y : color[2]).to_f
280: k = (color.respond_to?(:k) ? color.k : color[3]).to_f
281: Graphics::Instruction::K.new(c, m, y, k) if @gs.nonstroking_color != [c,m,y,k]
282:
283: elsif color.respond_to?:g or (0.0..1.0) === color
284: g = color.respond_to?(:g) ? color.g : color
285: Graphics::Instruction::G.new(g) if @gs.nonstroking_color != [ g ]
286:
287: else
288: raise TypeError, "Invalid color : #{color}"
289: end
290: )
291:
292: i.update_state(@gs) if i
293: self
294: end
# File sources/parser/graphics/xobject.rb, line 341
341: def set_line_cap(cap)
342: if @gs.line_cap != cap
343: @instructions << Graphics::Instruction::JCap.new(cap).update_state(@gs)
344: end
345:
346: self
347: end
# File sources/parser/graphics/xobject.rb, line 349
349: def set_line_join(join)
350: if @gs.line_join != join
351: @instructions << Graphics::Instruction::JJoin.new(join).update_state(@gs)
352: end
353:
354: self
355: end
# File sources/parser/graphics/xobject.rb, line 333
333: def set_line_width(width)
334: if @gs.line_width != width
335: @instructions << Graphics::Instruction::W.new(width).update_state(@gs)
336: end
337:
338: self
339: end
# File sources/parser/graphics/xobject.rb, line 296
296: def set_stroke_color(color)
297:
298: @instructions << ( i =
299: if (color.respond_to? :r and color.respond_to? :g and color.respond_to? :b) or (color.is_a?(::Array) and color.size == 3)
300: r = (color.respond_to?(:r) ? color.r : color[0]).to_f / 255
301: g = (color.respond_to?(:g) ? color.g : color[1]).to_f / 255
302: b = (color.respond_to?(:b) ? color.b : color[2]).to_f / 255
303: Graphics::Instruction::StrokeRG.new(r, g, b) if @gs.stroking_color != [r,g,b]
304:
305: elsif (color.respond_to? :c and color.respond_to? :m and color.respond_to? :y and color.respond_to? :k) or (color.is_a?(::Array) and color.size == 4)
306: c = (color.respond_to?(:c) ? color.c : color[0]).to_f
307: m = (color.respond_to?(:m) ? color.m : color[1]).to_f
308: y = (color.respond_to?(:y) ? color.y : color[2]).to_f
309: k = (color.respond_to?(:k) ? color.k : color[3]).to_f
310: Graphics::Instruction::StrokeK.new(c, m, y, k) if @gs.stroking_color != [c,m,y,k]
311:
312: elsif color.respond_to?:g or (0.0..1.0) === color
313: g = color.respond_to?(:g) ? color.g : color
314: Graphics::Instruction::StrokeG.new(g) if @gs.stroking_color != [ g ]
315:
316: else
317: raise TypeError, "Invalid color : #{color}"
318: end
319: )
320:
321: i.update_state(@gs) if i
322: self
323: end
# File sources/parser/graphics/xobject.rb, line 259
259: def set_text_char_spacing(char_spacing)
260: if char_spacing != @gs.text_state.char_spacing
261: @instructions << Text::Instruction::Tc.new(char_spacing).update_state(@gs)
262: end
263:
264: self
265: end
# File sources/parser/graphics/xobject.rb, line 205
205: def set_text_font(fontname, size)
206: if fontname != @gs.text_state.font or size != @gs.text_state.font_size
207: @instructions << Text::Instruction::Tf.new(fontname, size).update_state(@gs)
208: end
209:
210: self
211: end
# File sources/parser/graphics/xobject.rb, line 219
219: def set_text_leading(leading)
220: if leading != @gs.text_state.leading
221: @instructions << Text::Instruction::TL.new(leading).update_state(@gs)
222: end
223:
224: self
225: end
# File sources/parser/graphics/xobject.rb, line 213
213: def set_text_pos(tx,ty)
214: @instructions << Text::Instruction::Td.new(tx, ty).update_state(@gs)
215:
216: self
217: end
# File sources/parser/graphics/xobject.rb, line 227
227: def set_text_rendering(rendering)
228: if rendering != @gs.text_state.rendering_mode
229: @instructions << Text::Instruction::Tr.new(rendering).update_state(@gs)
230: end
231:
232: self
233: end
# File sources/parser/graphics/xobject.rb, line 235
235: def set_text_rise(rise)
236: if rise != @gs.text_state.text_rise
237: @instructions << Text::Instruction::Ts.new(rise).update_state(@gs)
238: end
239:
240: self
241: end
# File sources/parser/graphics/xobject.rb, line 243
243: def set_text_scale(scaling)
244: if scale != @gs.text_state.scaling
245: @instructions << Text::Instruction::Tz.new(scaling).update_state(@gs)
246: end
247:
248: self
249: end
# File sources/parser/graphics/xobject.rb, line 251
251: def set_text_word_spacing(word_spacing)
252: if word_spacing != @gs.text_state.word_spacing
253: @instructions << Text::Instruction::Tw.new(word_spacing).update_state(@gs)
254: end
255:
256: self
257: end
Adds text to the content stream with custom formatting attributes.
| text: | Text to write. |
| attr: | Formatting attributes. |
# File sources/parser/graphics/xobject.rb, line 161
161: def write(text, attr = {})
162:
163: x,y = attr[:x], attr[:y]
164: font = attr[:font] || DEFAULT_FONT
165: size = attr[:size] || DEFAULT_SIZE
166: leading = attr[:leading] || DEFAULT_LEADING
167: color = attr[:color] || attr[:fill_color] || DEFAULT_STROKE_COLOR
168: stroke_color = attr[:stroke_color] || DEFAULT_STROKE_COLOR
169: line_width = attr[:line_width] || DEFAULT_LINEWIDTH
170: word_spacing = attr[:word_spacing]
171: char_spacing = attr[:char_spacing]
172: scale = attr[:scale]
173: rise = attr[:rise]
174: rendering = attr[:rendering]
175:
176: @instructions << Text::Instruction::ET.new.update_state(@gs) if (x or y) and @gs.text_state.is_in_text_object?
177:
178: unless @gs.text_state.is_in_text_object?
179: @instructions << Text::Instruction::BT.new.update_state(@gs)
180: end
181:
182: set_text_font(font, size)
183: set_text_pos(x, y) if x or y
184: set_text_leading(leading) if leading
185: set_text_rendering(rendering) if rendering
186: set_text_rise(rise) if rise
187: set_text_scale(scale) if scale
188: set_text_word_spacing(word_spacing) if word_spacing
189: set_text_char_spacing(char_spacing) if char_spacing
190: set_fill_color(color)
191: set_stroke_color(stroke_color)
192: set_line_width(line_width)
193:
194: write_text_block(text)
195:
196: self
197: end