idml 0.1.0 → 0.2.0

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.
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Idml
4
- module Render
5
- module PdfrbExt
6
- # PDF `Do` operator: invoke an XObject (image or form) by name.
7
- # pdfrb 0.3.0 doesn't include this operator; we register it here
8
- # so Canvas#emit_op can draw images.
9
- class InvokeXObject < Pdfrb::Content::Operator::Base
10
- class << self
11
- def name
12
- "Do"
13
- end
14
-
15
- def serialize(_serializer, xobject_name)
16
- "/#{xobject_name} Do\n"
17
- end
18
- end
19
-
20
- register
21
- end
22
- end
23
- end
24
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Idml
4
- module Render
5
- # Extensions to the pdfrb gem for operators not yet implemented in
6
- # pdfrb 0.3.0 (e.g., XObject invocation via `Do`).
7
- module PdfrbExt
8
- autoload :InvokeXObject, "#{__dir__}/pdfrb_ext/invoke_xobject"
9
- autoload :Clip, "#{__dir__}/pdfrb_ext/clip"
10
- autoload :EndPath, "#{__dir__}/pdfrb_ext/clip"
11
- end
12
- end
13
- end
@@ -1,91 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # DEPRECATED: This module is superseded by Canvas#text method.
4
- # Text rendering is now handled by pdfrb's Canvas API.
5
-
6
- module Idml
7
- module Render
8
- # Converts positioned glyphs to PDF text-showing operators.
9
- module Text
10
- module_function
11
-
12
- # Begin text block
13
- def begin_text
14
- "BT"
15
- end
16
-
17
- # End text block
18
- def end_text
19
- "ET"
20
- end
21
-
22
- # Set font: `/FontName size Tf`
23
- def set_font(font_name, size)
24
- format("/%<name>s %<size>.1f Tf", name: font_name, size: size)
25
- end
26
-
27
- # Move text position: `x y Td`
28
- def move_to(x, y)
29
- format("%<x>.2f %<y>.2f Td", x: x, y: y)
30
- end
31
-
32
- # Set text matrix: `a b c d e f Tm`
33
- def set_matrix(a:, b:, c:, d:, e:, f:)
34
- format("%<a>.4f %<b>.4f %<c>.4f %<d>.4f %<e>.2f %<f>.2f Tm",
35
- a: a, b: b, c: c, d: d, e: e, f: f)
36
- end
37
-
38
- # Show a text string: `(escaped) Tj`
39
- def show(text_string)
40
- "(#{escape(text_string)}) Tj"
41
- end
42
-
43
- # Build a complete text-showing block for a run of glyphs
44
- # at the same font/size, positioned at (x, y).
45
- def show_run(text_string:, font_name:, size:, x:, y:)
46
- [
47
- begin_text,
48
- set_font(font_name, size),
49
- move_to(x, y),
50
- show(text_string),
51
- end_text,
52
- ].join("\n")
53
- end
54
-
55
- # Emit positioned glyphs. Groups glyphs by y coordinate into
56
- # lines, sets the text matrix per-line for absolute positioning,
57
- # then shows the line text. Multiple lines within one BT/ET block.
58
- def show_positioned(glyphs)
59
- return "" if glyphs.empty?
60
-
61
- lines = group_by_line(glyphs)
62
- lines.map do |line|
63
- [
64
- set_matrix(a: 1, b: 0, c: 0, d: 1,
65
- e: line.first.x, f: line.first.y),
66
- show(line.map { |g| [g.codepoint].pack("U") }.join),
67
- ].join("\n")
68
- end.join("\n")
69
- end
70
-
71
- def group_by_line(glyphs)
72
- result = []
73
- current = nil
74
- glyphs.each do |glyph|
75
- if current.nil? || current.first.y != glyph.y
76
- current = []
77
- result << current
78
- end
79
- current << glyph
80
- end
81
- result
82
- end
83
- private_class_method :group_by_line
84
-
85
- # Escape characters that are special in PDF strings.
86
- def escape(str)
87
- str.to_s.gsub("\\", "\\\\\\\\").gsub("(", "\\(").gsub(")", "\\)")
88
- end
89
- end
90
- end
91
- end