vectory 0.13.3 → 0.13.4
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.
- checksums.yaml +4 -4
- data/.github/workflows/release.yml +1 -0
- data/Gemfile +3 -1
- data/README.adoc +195 -4
- data/docs/reference/api.adoc +110 -0
- data/lib/vectory/cli.rb +42 -4
- data/lib/vectory/configuration.rb +3 -2
- data/lib/vectory/emf.rb +4 -0
- data/lib/vectory/eps.rb +4 -0
- data/lib/vectory/errors.rb +11 -0
- data/lib/vectory/file_magic.rb +7 -2
- data/lib/vectory/image.rb +32 -3
- data/lib/vectory/png.rb +40 -0
- data/lib/vectory/png_options.rb +69 -0
- data/lib/vectory/png_renderer.rb +74 -0
- data/lib/vectory/ps.rb +4 -0
- data/lib/vectory/svg.rb +6 -0
- data/lib/vectory/svg_input_policy.rb +50 -0
- data/lib/vectory/svg_mapping.rb +59 -0
- data/lib/vectory/vector.rb +0 -45
- data/lib/vectory/version.rb +1 -1
- data/lib/vectory.rb +3 -0
- data/script/check_png_shell +235 -0
- data/vectory.gemspec +6 -1
- metadata +39 -3
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Vectory
|
|
4
|
+
# Converts command-line PNG options into the renderer's public values.
|
|
5
|
+
class PngOptions
|
|
6
|
+
NAMES = %w[font fallback_font width height scale background].freeze
|
|
7
|
+
|
|
8
|
+
def self.validate_format!(options)
|
|
9
|
+
return if options[:format] == "png"
|
|
10
|
+
return unless NAMES.any? { |name| options.key?(name) }
|
|
11
|
+
|
|
12
|
+
raise ConversionError, "Font and render-profile options require PNG output"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def initialize(options)
|
|
16
|
+
@options = options
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def keywords
|
|
20
|
+
PngRenderer.with_errors do
|
|
21
|
+
{ profile: profile, fonts: fonts, fallback_fonts: fallback_fonts }
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def profile
|
|
28
|
+
%w[width height scale background].each_with_object({}) do |name, values|
|
|
29
|
+
next unless @options.key?(name)
|
|
30
|
+
|
|
31
|
+
values[name.to_sym] = profile_value(name, @options[name])
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def profile_value(name, value)
|
|
36
|
+
case name
|
|
37
|
+
when "width", "height" then Integer(value, 10)
|
|
38
|
+
else value
|
|
39
|
+
end
|
|
40
|
+
rescue ArgumentError, TypeError
|
|
41
|
+
raise RenderProfileError, "Invalid PNG #{name}: #{value}"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def fonts
|
|
45
|
+
Array(@options[:font]).each_with_object({}) do |assignment, families|
|
|
46
|
+
family, face = font_face(assignment)
|
|
47
|
+
(families[family] ||= []) << face
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def fallback_fonts
|
|
52
|
+
Array(@options[:fallback_font]).map do |assignment|
|
|
53
|
+
_family, face = font_face(assignment)
|
|
54
|
+
face
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def font_face(assignment)
|
|
59
|
+
family, path = assignment.split("=", 2)
|
|
60
|
+
if family.nil? || family.strip.empty? || path.nil? || path.empty?
|
|
61
|
+
raise FontError, "Font option must be FAMILY=PATH"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
[family, Svgpng::FontFace.new(bytes: File.binread(path), source_id: path, family: family)]
|
|
65
|
+
rescue ::SystemCallError, IOError => e
|
|
66
|
+
raise Vectory::SystemCallError, "Cannot read font: #{e.message}"
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Vectory
|
|
4
|
+
# Keep the optional native dependency behind the PNG projection boundary.
|
|
5
|
+
module PngRenderer
|
|
6
|
+
def self.render(content, profile:, fonts:, fallback_fonts:)
|
|
7
|
+
with_errors do
|
|
8
|
+
result = Svgpng.render(content, profile: profile, fonts: fonts,
|
|
9
|
+
fallback_fonts: fallback_fonts)
|
|
10
|
+
Png.new(result.png_bytes, warnings: result.warnings)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.with_errors
|
|
15
|
+
require "svgpng"
|
|
16
|
+
yield
|
|
17
|
+
rescue LoadError => e
|
|
18
|
+
raise RendererLoadError, e.message
|
|
19
|
+
rescue Svgpng::Error => e
|
|
20
|
+
raise mapped_error(e), describe(e)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# The native layer reports only a status number; its typed details say why.
|
|
24
|
+
def self.describe(error)
|
|
25
|
+
return error.message unless error.message.match?(/\(status \d+\)\z/)
|
|
26
|
+
|
|
27
|
+
explain = status_messages.find { |type, _message| error.is_a?(type) }&.last
|
|
28
|
+
explain ? explain.call(error) : error.message
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.status_messages
|
|
32
|
+
{
|
|
33
|
+
Svgpng::FontMetadataError => lambda { |_error|
|
|
34
|
+
"Font face is unreadable, or its own family name differs from the family it was given"
|
|
35
|
+
},
|
|
36
|
+
Svgpng::DuplicateFontError => ->(_error) { "Two font faces share one family, style, weight and stretch" },
|
|
37
|
+
Svgpng::MissingFamilyError => ->(error) { "No font face was given for #{family(error)}" },
|
|
38
|
+
Svgpng::MissingGlyphError => lambda { |error|
|
|
39
|
+
"#{family(error).sub(/\A./, &:upcase)} has no glyph for #{scalars(error)}"
|
|
40
|
+
},
|
|
41
|
+
Svgpng::ForbiddenReferenceError => ->(error) { "External reference forbidden: #{reference(error)}" },
|
|
42
|
+
}
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def self.family(error)
|
|
46
|
+
error.requested_family ? "font family #{error.requested_family.inspect}" : "a font family in the SVG"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def self.scalars(error)
|
|
50
|
+
values = Array(error.scalars).map { |scalar| format("U+%04X", scalar) }
|
|
51
|
+
values.empty? ? "some characters" : values.join(", ")
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def self.reference(error)
|
|
55
|
+
[error.reference_class, error.location].compact.join(" at ").then { |text| text.empty? ? "unknown" : text }
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# An ordered list preserves specific subclasses before their parent category.
|
|
59
|
+
def self.mapped_error(error)
|
|
60
|
+
mappings = {
|
|
61
|
+
Svgpng::ProfileError => RenderProfileError,
|
|
62
|
+
Svgpng::FontError => FontError,
|
|
63
|
+
Svgpng::ForbiddenReferenceError => ForbiddenExternalReferenceError,
|
|
64
|
+
Svgpng::LimitError => RenderLimitError,
|
|
65
|
+
Svgpng::InputError => MalformedSvgError,
|
|
66
|
+
Svgpng::UnsupportedPlatformError => UnsupportedPlatformError,
|
|
67
|
+
Svgpng::EncodingError => PngEncodingError,
|
|
68
|
+
Svgpng::NativeError => NativeRenderError,
|
|
69
|
+
}
|
|
70
|
+
mappings.find { |type, _target| error.is_a?(type) }&.last || NativeRenderError
|
|
71
|
+
end
|
|
72
|
+
private_class_method :mapped_error, :describe, :status_messages, :family, :scalars, :reference
|
|
73
|
+
end
|
|
74
|
+
end
|
data/lib/vectory/ps.rb
CHANGED
data/lib/vectory/svg.rb
CHANGED
|
@@ -32,6 +32,11 @@ module Vectory
|
|
|
32
32
|
self.content = content
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
+
def to_png(fonts:, fallback_fonts:, profile: {})
|
|
36
|
+
PngRenderer.render(content, profile: profile, fonts: fonts,
|
|
37
|
+
fallback_fonts: fallback_fonts)
|
|
38
|
+
end
|
|
39
|
+
|
|
35
40
|
def to_emf
|
|
36
41
|
Emf.from_content(Emfsvg.from_svg(content))
|
|
37
42
|
end
|
|
@@ -75,6 +80,7 @@ module Vectory
|
|
|
75
80
|
end
|
|
76
81
|
|
|
77
82
|
def content=(content)
|
|
83
|
+
SvgInputPolicy.validate!(content)
|
|
78
84
|
# non-root node inserts the xml tag which breaks markup when placed in
|
|
79
85
|
# another xml document
|
|
80
86
|
document = begin
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Vectory
|
|
4
|
+
# Inspect raw bytes before parsing can discard a DTD or expand an entity.
|
|
5
|
+
module SvgInputPolicy
|
|
6
|
+
DOCTYPE = /\A<!DOCTYPE(?:\s|\[|>)/
|
|
7
|
+
|
|
8
|
+
def self.validate!(content)
|
|
9
|
+
return unless content.is_a?(String)
|
|
10
|
+
|
|
11
|
+
# XML permits UTF-16/32 declarations; ASCII token bytes are NUL padded.
|
|
12
|
+
bytes = content.b.delete("\0")
|
|
13
|
+
cursor = 0
|
|
14
|
+
while (opening = bytes.index("<", cursor))
|
|
15
|
+
cursor = token_end(bytes, opening)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Literal text inside these tokens is not markup.
|
|
20
|
+
OPAQUE = { "<!--" => "-->", "<![CDATA[" => "]]>", "<?" => "?>" }.freeze
|
|
21
|
+
|
|
22
|
+
def self.token_end(bytes, opening)
|
|
23
|
+
return doctype_end(bytes, opening + 9) if bytes[opening, 10].match?(DOCTYPE)
|
|
24
|
+
|
|
25
|
+
start, delimiter = OPAQUE.find { |token, _end| bytes[opening, token.bytesize] == token }
|
|
26
|
+
start ? closing(bytes, delimiter, opening + start.bytesize) : opening + 1
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# A DOCTYPE without an internal subset declares no entities, and the XML
|
|
30
|
+
# parser never loads its external identifier, so only `[` is refused.
|
|
31
|
+
def self.doctype_end(bytes, cursor)
|
|
32
|
+
while cursor < bytes.bytesize
|
|
33
|
+
case bytes[cursor]
|
|
34
|
+
when ">" then return cursor + 1
|
|
35
|
+
when "[" then raise ForbiddenExternalReferenceError, "XML DOCTYPE internal subsets are forbidden"
|
|
36
|
+
when '"', "'" then cursor = closing(bytes, bytes[cursor], cursor + 1)
|
|
37
|
+
else cursor += 1
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
cursor
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def self.closing(bytes, delimiter, start)
|
|
44
|
+
index = bytes.index(delimiter, start)
|
|
45
|
+
# An unterminated token consumes the rest; the XML parser diagnoses it.
|
|
46
|
+
index ? index + delimiter.bytesize : bytes.bytesize
|
|
47
|
+
end
|
|
48
|
+
private_class_method :token_end, :doctype_end, :closing
|
|
49
|
+
end
|
|
50
|
+
end
|
data/lib/vectory/svg_mapping.rb
CHANGED
|
@@ -90,6 +90,7 @@ module Vectory
|
|
|
90
90
|
# identity. Applied before the index suffix to provide cross-document
|
|
91
91
|
# uniqueness. Example: "_ISO_17301-1_2016"
|
|
92
92
|
def initialize(doc, local_directory = "", id_suffix: nil)
|
|
93
|
+
@caller_doc = doc unless doc.is_a?(String)
|
|
93
94
|
xml = doc.respond_to?(:to_xml) ? doc.to_xml : doc
|
|
94
95
|
@doc = Leptris::XML.parse(xml)
|
|
95
96
|
@local_directory = local_directory
|
|
@@ -106,6 +107,8 @@ module Vectory
|
|
|
106
107
|
process_svgmap(svgmap, index)
|
|
107
108
|
end
|
|
108
109
|
|
|
110
|
+
write_back_to_caller_doc
|
|
111
|
+
|
|
109
112
|
@doc
|
|
110
113
|
end
|
|
111
114
|
|
|
@@ -118,6 +121,62 @@ module Vectory
|
|
|
118
121
|
|
|
119
122
|
private
|
|
120
123
|
|
|
124
|
+
# Writes the processed document back into a caller-supplied document
|
|
125
|
+
# object, restoring the pre-0.13 contract: metanorma-standoc's
|
|
126
|
+
# svgmap_cleanup passes its live document and relies on SvgMapping to
|
|
127
|
+
# update it in place (its return value is discarded), so without this
|
|
128
|
+
# write-back svgmap hotspots keep their unremapped hrefs (issue #84).
|
|
129
|
+
#
|
|
130
|
+
# The write-back runs through the caller's own XML library, which is
|
|
131
|
+
# already loaded in the caller's process; no dependency is added.
|
|
132
|
+
# Callers on the 0.13 return-value contract are unaffected: #call
|
|
133
|
+
# still returns the processed Leptris document.
|
|
134
|
+
#
|
|
135
|
+
# @return [void]
|
|
136
|
+
def write_back_to_caller_doc
|
|
137
|
+
return unless @caller_doc
|
|
138
|
+
|
|
139
|
+
reparsed = reparse_for_caller_doc
|
|
140
|
+
return unless reparsed
|
|
141
|
+
|
|
142
|
+
write_back_root(reparsed)
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Moves the reparsed document's children under the caller document's
|
|
146
|
+
# root, the same graft metanorma-standoc applies when it adopts the
|
|
147
|
+
# return-value contract.
|
|
148
|
+
#
|
|
149
|
+
# @param reparsed [Object] The processed XML reparsed by the caller's
|
|
150
|
+
# XML library
|
|
151
|
+
# @return [void]
|
|
152
|
+
def write_back_root(reparsed)
|
|
153
|
+
root = caller_doc_root
|
|
154
|
+
return unless root
|
|
155
|
+
|
|
156
|
+
root.children.each(&:remove)
|
|
157
|
+
reparsed.root.children.each { |n| root << n }
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# Returns the caller document's root, if its type exposes one.
|
|
161
|
+
#
|
|
162
|
+
# @return [Object, nil]
|
|
163
|
+
def caller_doc_root
|
|
164
|
+
@caller_doc.respond_to?(:root) ? @caller_doc.root : nil
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# Reparses the processed XML with the caller's own XML library.
|
|
168
|
+
#
|
|
169
|
+
# @return [Object, nil] The reparsed document, or nil if the caller's
|
|
170
|
+
# document object is of an unrecognized type
|
|
171
|
+
def reparse_for_caller_doc
|
|
172
|
+
if defined?(Moxml::Node) && @caller_doc.is_a?(Moxml::Node)
|
|
173
|
+
Moxml.new.parse(@doc.to_xml)
|
|
174
|
+
elsif defined?(Nokogiri::XML::Node) &&
|
|
175
|
+
@caller_doc.is_a?(Nokogiri::XML::Node)
|
|
176
|
+
Nokogiri::XML(@doc.to_xml)
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
|
|
121
180
|
# Processes a single svgmap element.
|
|
122
181
|
#
|
|
123
182
|
# @param svgmap [Leptris::XML::Element] The svgmap element to process
|
data/lib/vectory/vector.rb
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "tempfile"
|
|
4
|
-
|
|
5
3
|
module Vectory
|
|
6
4
|
class Vector < Image
|
|
7
|
-
def self.from_path(path)
|
|
8
|
-
content = File.read(path, mode: "rb")
|
|
9
|
-
new(content, path)
|
|
10
|
-
end
|
|
11
|
-
|
|
12
5
|
def self.from_datauri(uri)
|
|
13
6
|
Datauri.new(uri).to_vector
|
|
14
7
|
end
|
|
@@ -23,28 +16,10 @@ module Vectory
|
|
|
23
16
|
"#mimetype should be implemented in a subclass."
|
|
24
17
|
end
|
|
25
18
|
|
|
26
|
-
attr_reader :initial_path
|
|
27
|
-
|
|
28
|
-
def initialize(content = nil, initial_path = nil)
|
|
29
|
-
super(content)
|
|
30
|
-
|
|
31
|
-
@initial_path = initial_path
|
|
32
|
-
end
|
|
33
|
-
|
|
34
19
|
def mime
|
|
35
20
|
self.class.mimetype
|
|
36
21
|
end
|
|
37
22
|
|
|
38
|
-
def size
|
|
39
|
-
content.bytesize
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
def file_size
|
|
43
|
-
raise(NotWrittenToDiskError) unless @path
|
|
44
|
-
|
|
45
|
-
File.size(@path)
|
|
46
|
-
end
|
|
47
|
-
|
|
48
23
|
def height
|
|
49
24
|
raise Vectory::NotImplementedError,
|
|
50
25
|
"#height should be implemented in a subclass when no intrinsic " \
|
|
@@ -60,25 +35,5 @@ module Vectory
|
|
|
60
35
|
def to_uri
|
|
61
36
|
Datauri.from_vector(self)
|
|
62
37
|
end
|
|
63
|
-
|
|
64
|
-
def write(path = nil)
|
|
65
|
-
target_path = path || @path || tmp_path
|
|
66
|
-
File.binwrite(target_path, content)
|
|
67
|
-
@path = File.expand_path(target_path)
|
|
68
|
-
|
|
69
|
-
self
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
def path
|
|
73
|
-
@path || raise(NotWrittenToDiskError)
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
private
|
|
77
|
-
|
|
78
|
-
def tmp_path
|
|
79
|
-
dir = Dir.mktmpdir
|
|
80
|
-
filename = "image.#{self.class.default_extension}"
|
|
81
|
-
File.join(dir, filename)
|
|
82
|
-
end
|
|
83
38
|
end
|
|
84
39
|
end
|
data/lib/vectory/version.rb
CHANGED
data/lib/vectory.rb
CHANGED
|
@@ -32,6 +32,9 @@ module Vectory
|
|
|
32
32
|
autoload :Eps, "vectory/eps"
|
|
33
33
|
autoload :Ps, "vectory/ps"
|
|
34
34
|
autoload :Emf, "vectory/emf"
|
|
35
|
+
autoload :Png, "vectory/png"
|
|
36
|
+
autoload :PngRenderer, "vectory/png_renderer"
|
|
37
|
+
autoload :SvgInputPolicy, "vectory/svg_input_policy"
|
|
35
38
|
autoload :Svg, "vectory/svg"
|
|
36
39
|
autoload :SvgMapping, "vectory/svg_mapping"
|
|
37
40
|
autoload :SvgDocument, "vectory/svg_document"
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "digest"
|
|
5
|
+
require "fileutils"
|
|
6
|
+
require "json"
|
|
7
|
+
require "open3"
|
|
8
|
+
require "optparse"
|
|
9
|
+
require "rbconfig"
|
|
10
|
+
require "rubygems/package"
|
|
11
|
+
require "tmpdir"
|
|
12
|
+
|
|
13
|
+
# Uses real packaged gems and Bundler. The selection phase simulates an unsupported
|
|
14
|
+
# platform for svgpng alone; the downstream phase runs on the actual host.
|
|
15
|
+
class PngShellCheck
|
|
16
|
+
TARGETS = %w[arm64-darwin x86_64-darwin x86_64-linux aarch64-linux
|
|
17
|
+
x86_64-linux-musl aarch64-linux-musl x64-mingw-ucrt ruby].freeze
|
|
18
|
+
|
|
19
|
+
def initialize(options)
|
|
20
|
+
@options = options
|
|
21
|
+
@root = File.expand_path("..", __dir__)
|
|
22
|
+
@bundler = Gem.bin_path("bundler", "bundle")
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def run
|
|
26
|
+
shell = File.expand_path(@options.fetch(:shell))
|
|
27
|
+
verify_package(shell, "ruby", @options.fetch(:sha256))
|
|
28
|
+
packages = @options[:shell_only] ? [shell] : release_packages(shell)
|
|
29
|
+
Dir.mktmpdir("vectory-png-shell-") do |directory|
|
|
30
|
+
check_selection(directory, packages) unless @options[:shell_only]
|
|
31
|
+
selection_notice if @options[:shell_only]
|
|
32
|
+
check_downstream(directory, shell)
|
|
33
|
+
end
|
|
34
|
+
report_success(shell)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def selection_notice
|
|
40
|
+
puts "SELECTION NOT RUN (--shell-only); unsupported-platform resolution remains unproven"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def report_success(shell)
|
|
44
|
+
puts "PASS real shell downstream; host=#{Gem::Platform.local}; shell_sha256=#{Digest::SHA256.file(shell).hexdigest}"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def verify_digest(path, digest)
|
|
48
|
+
raise "Missing artifact: #{path}" unless File.file?(path)
|
|
49
|
+
raise "SHA256 mismatch: #{path}" unless Digest::SHA256.file(path).hexdigest == digest
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def verify_package(path, target, digest)
|
|
53
|
+
verify_digest(path, digest)
|
|
54
|
+
spec = Gem::Package.new(path).spec
|
|
55
|
+
return if spec.name == "svgpng" && spec.version.to_s == "0.1.0" && spec.platform.to_s == target
|
|
56
|
+
|
|
57
|
+
raise "Wrong package metadata: #{path} (#{spec.full_name})"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def release_packages(shell)
|
|
61
|
+
directory = File.expand_path(@options.fetch(:release_set))
|
|
62
|
+
manifest = JSON.parse(File.read(File.join(directory, "checksums.json")))
|
|
63
|
+
entries = manifest_entries(manifest)
|
|
64
|
+
entries.map { |entry| release_package(directory, entry, shell) }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def manifest_entries(manifest)
|
|
68
|
+
entries = manifest.fetch("artifacts")
|
|
69
|
+
unless manifest["schema_version"] == 1 && manifest["version"] == "0.1.0" &&
|
|
70
|
+
entries.map { |entry| entry.fetch("target") }.sort == TARGETS.sort
|
|
71
|
+
raise "checksums.json must identify exactly the eight 0.1.0 packages"
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
entries
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def release_package(directory, entry, shell)
|
|
78
|
+
target = entry.fetch("target")
|
|
79
|
+
expected = target == "ruby" ? "svgpng-0.1.0.gem" : "svgpng-0.1.0-#{target}.gem"
|
|
80
|
+
raise "Unexpected artifact filename" unless entry.fetch("path") == expected
|
|
81
|
+
|
|
82
|
+
path = File.join(directory, expected)
|
|
83
|
+
verify_package(path, target, entry.fetch("sha256"))
|
|
84
|
+
verify_same_shell(path, shell) if target == "ruby"
|
|
85
|
+
path
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def verify_same_shell(path, shell)
|
|
89
|
+
return if Digest::SHA256.file(path).hexdigest == Digest::SHA256.file(shell).hexdigest
|
|
90
|
+
|
|
91
|
+
raise "Downstream shell differs from full-set shell"
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def bundle_environment(directory)
|
|
95
|
+
environment = ENV.keys.grep(/\ABUNDLE(?:R)?_/).to_h { |key| [key, nil] }
|
|
96
|
+
environment.merge("BUNDLE_GEMFILE" => File.join(directory, "Gemfile"),
|
|
97
|
+
"BUNDLE_IGNORE_CONFIG" => "true", "BUNDLE_PATH__SYSTEM" => "true", "RUBYOPT" => nil,
|
|
98
|
+
"GEM_HOME" => File.join(directory, "gems"),
|
|
99
|
+
"GEM_PATH" => [File.join(directory, "gems"), Gem.default_dir].join(File::PATH_SEPARATOR))
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def prepare_bundle(directory, packages, gemfile)
|
|
103
|
+
FileUtils.mkdir_p(File.join(directory, "vendor", "cache"))
|
|
104
|
+
packages.each { |path| FileUtils.cp(path, File.join(directory, "vendor", "cache")) }
|
|
105
|
+
File.write(File.join(directory, "Gemfile"), gemfile)
|
|
106
|
+
seed_dependencies(File.join(directory, "gems"))
|
|
107
|
+
bundle_environment(directory)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Reuse installed dependencies without builds or writes to the caller's gem home.
|
|
111
|
+
# svgpng is deliberately excluded so Bundler must install the supplied package.
|
|
112
|
+
def seed_dependencies(home)
|
|
113
|
+
Gem::Specification.each do |spec|
|
|
114
|
+
next if spec.name == "svgpng" || spec.name == "vectory" || spec.default_gem?
|
|
115
|
+
|
|
116
|
+
link_dependency(home, spec)
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def link_dependency(home, spec)
|
|
121
|
+
FileUtils.mkdir_p([File.join(home, "specifications"), File.join(home, "gems")])
|
|
122
|
+
File.write(File.join(home, "specifications", "#{spec.full_name}.gemspec"), spec.to_ruby)
|
|
123
|
+
File.symlink(spec.full_gem_path, File.join(home, "gems", spec.full_name))
|
|
124
|
+
link_extension(home, spec) unless spec.extensions.empty?
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def link_extension(home, spec)
|
|
128
|
+
extension = File.join(home, "extensions", Gem::Platform.local.to_s, Gem.extension_api_version, spec.full_name)
|
|
129
|
+
FileUtils.mkdir_p(File.dirname(extension))
|
|
130
|
+
File.symlink(spec.extension_dir, extension)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def execute(environment, directory, *arguments, expected: 0)
|
|
134
|
+
stdout, stderr, result = Open3.capture3(environment, RbConfig.ruby, *arguments, chdir: directory)
|
|
135
|
+
unless result.exitstatus == expected
|
|
136
|
+
raise "Command failed (expected #{expected}, got #{result.exitstatus}): #{arguments.join(' ')}\n#{stdout}#{stderr}"
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
[stdout, stderr]
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def check_selection(directory, packages)
|
|
143
|
+
location = File.join(directory, "selection")
|
|
144
|
+
environment = prepare_bundle(location, packages, <<~GEMFILE)
|
|
145
|
+
source "https://rubygems.org"
|
|
146
|
+
gem "svgpng", "= 0.1.0"
|
|
147
|
+
GEMFILE
|
|
148
|
+
# An isolated Gemfile avoids pretending Vectory's unrelated native dependencies
|
|
149
|
+
# are installable on this synthetic platform. Bundler still resolves real gems.
|
|
150
|
+
launcher = <<~RUBY
|
|
151
|
+
require "rubygems"
|
|
152
|
+
unsupported = Gem::Platform.new("universal-unsupportedos")
|
|
153
|
+
Gem::Platform.define_singleton_method(:local) { unsupported }
|
|
154
|
+
Gem.platforms = [Gem::Platform::RUBY, unsupported]
|
|
155
|
+
load #{@bundler.inspect}
|
|
156
|
+
RUBY
|
|
157
|
+
File.write(File.join(location, "unsupported_bundle.rb"), launcher)
|
|
158
|
+
execute(environment, location, "unsupported_bundle.rb", "install", "--local", "--no-cache", "--quiet")
|
|
159
|
+
assert_shell(environment, location)
|
|
160
|
+
puts "PASS simulated unsupported-platform Bundler selection from eight checksummed packages (not an actual unsupported OS)"
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def assert_shell(environment, directory)
|
|
164
|
+
execute(environment, directory, @bundler, "exec", "ruby", "-e", <<~RUBY)
|
|
165
|
+
require "svgpng"
|
|
166
|
+
spec = Gem.loaded_specs.fetch("svgpng")
|
|
167
|
+
abort "Bundler selected a native svgpng package" unless spec.platform.to_s == "ruby"
|
|
168
|
+
abort "Bundler used an external svgpng installation" unless File.realpath(spec.full_gem_path).start_with?(#{File.realpath(File.join(directory, 'gems', 'gems')).inspect})
|
|
169
|
+
require "digest"
|
|
170
|
+
abort "Installed shell checksum mismatch" unless Digest::SHA256.file(spec.cache_file).hexdigest == #{@options.fetch(:sha256).inspect}
|
|
171
|
+
begin
|
|
172
|
+
Svgpng.render('<svg xmlns="http://www.w3.org/2000/svg" width="2" height="1"/>',
|
|
173
|
+
profile: {}, fonts: {}, fallback_fonts: [])
|
|
174
|
+
abort "Ruby shell unexpectedly rendered"
|
|
175
|
+
rescue Svgpng::UnsupportedPlatformError
|
|
176
|
+
puts "actual shell UnsupportedPlatformError"
|
|
177
|
+
end
|
|
178
|
+
RUBY
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def check_downstream(directory, shell)
|
|
182
|
+
location = File.join(directory, "downstream")
|
|
183
|
+
environment = prepare_bundle(location, [shell], <<~GEMFILE)
|
|
184
|
+
source "https://rubygems.org"
|
|
185
|
+
gem "vectory", path: #{@root.inspect}
|
|
186
|
+
gem "svgpng", "= 0.1.0", force_ruby_platform: true
|
|
187
|
+
GEMFILE
|
|
188
|
+
execute(environment, location, @bundler, "install", "--local", "--no-cache", "--quiet")
|
|
189
|
+
assert_shell(environment, location)
|
|
190
|
+
File.write(File.join(location, "input.eps"), <<~POSTSCRIPT)
|
|
191
|
+
%!PS-Adobe-3.0 EPSF-3.0
|
|
192
|
+
%%BoundingBox: 0 0 2 1
|
|
193
|
+
newpath 0 0 moveto 2 0 lineto 2 1 lineto closepath fill
|
|
194
|
+
showpage
|
|
195
|
+
POSTSCRIPT
|
|
196
|
+
check_conversions(environment, location)
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
def check_conversions(environment, location)
|
|
200
|
+
cli = File.join(@root, "exe", "vectory")
|
|
201
|
+
execute(environment, location, @bundler, "exec", "ruby", cli,
|
|
202
|
+
"convert", "input.eps", "--format", "svg", "--output", "output.svg")
|
|
203
|
+
svg = File.join(location, "output.svg")
|
|
204
|
+
raise "Non-PNG conversion produced no SVG" unless File.read(svg).include?("<svg")
|
|
205
|
+
|
|
206
|
+
stdout, stderr = execute(environment, location, @bundler, "exec", "ruby", cli,
|
|
207
|
+
"convert", "output.svg", "--format", "png", "--output", "output.png", expected: 5)
|
|
208
|
+
raise "PNG failure did not identify unsupported platform: #{stdout}#{stderr}" unless (stdout + stderr).include?("No packaged SVGPNG renderer")
|
|
209
|
+
raise "Failed PNG conversion wrote output" if File.exist?(File.join(location, "output.png"))
|
|
210
|
+
|
|
211
|
+
puts "PASS actual EPS→SVG conversion; actual SVG→PNG shell failure exits 5 without output"
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
options = {}
|
|
216
|
+
parser = OptionParser.new do |opts|
|
|
217
|
+
opts.banner = "Usage: script/check_png_shell --shell GEM --sha256 SHA [--release-set DIR | --shell-only]"
|
|
218
|
+
opts.on("--shell GEM", "Real svgpng 0.1.0 ruby package") { |value| options[:shell] = value }
|
|
219
|
+
opts.on("--sha256 SHA", "Expected shell SHA256") { |value| options[:sha256] = value }
|
|
220
|
+
opts.on("--release-set DIR", "Full eight-package set with checksums.json") { |value| options[:release_set] = value }
|
|
221
|
+
opts.on("--shell-only", "Run downstream behavior only; selection remains unproven") { options[:shell_only] = true }
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
begin
|
|
225
|
+
parser.parse!
|
|
226
|
+
raise OptionParser::MissingArgument, "--shell and --sha256 are required" unless options[:shell] && options[:sha256]
|
|
227
|
+
unless options[:shell_only] || options[:release_set]
|
|
228
|
+
raise OptionParser::MissingArgument, "--release-set required for selection; use --shell-only explicitly for partial evidence"
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
PngShellCheck.new(options).run
|
|
232
|
+
rescue StandardError => e
|
|
233
|
+
warn "FAIL: #{e.message}"
|
|
234
|
+
exit 1
|
|
235
|
+
end
|
data/vectory.gemspec
CHANGED
|
@@ -11,7 +11,10 @@ Gem::Specification.new do |spec|
|
|
|
11
11
|
spec.summary = "Pairwise vector image conversions."
|
|
12
12
|
spec.description = <<~DESCRIPTION
|
|
13
13
|
Vectory performs pairwise vector image conversions for common
|
|
14
|
-
vector image formats, such as SVG, EMF, EPS and PS
|
|
14
|
+
vector image formats, such as SVG, EMF, EPS and PS, with PNG output via
|
|
15
|
+
svgpng prebuilt native packages on arm64-darwin, x86_64-darwin, x86_64-linux,
|
|
16
|
+
aarch64-linux, x86_64-linux-musl, aarch64-linux-musl and x64-mingw-ucrt. Its portable
|
|
17
|
+
shell package preserves installation elsewhere and reports unsupported PNG output.
|
|
15
18
|
DESCRIPTION
|
|
16
19
|
|
|
17
20
|
spec.homepage = "https://github.com/metanorma/vectory"
|
|
@@ -33,5 +36,7 @@ Gem::Specification.new do |spec|
|
|
|
33
36
|
spec.add_dependency "leptris", "~> 1.9.399"
|
|
34
37
|
spec.add_dependency "marcel", "~> 1.0"
|
|
35
38
|
spec.add_dependency "postsvg", "~> 0.3"
|
|
39
|
+
spec.add_dependency "svgpng", "~> 0.1.0"
|
|
36
40
|
spec.add_dependency "thor", "~> 1.0"
|
|
41
|
+
spec.add_dependency "yeptris", "~> 0.6.56.1"
|
|
37
42
|
end
|