vectory 0.7.2 → 0.7.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 55fbd240e8b8c5e4409e73d720e752c502a1d0700617aa8889c7dca337be989b
4
- data.tar.gz: 51a18f30f14a783c9f249a1b8664bf5767f10eb8f7a85aed3d87ca06d4d8d00e
3
+ metadata.gz: aef4319c091897a2bc5971c65ef3d87332655aa1d76516ffbcd66fc9541465da
4
+ data.tar.gz: ca405f2071d56b523ec6203b6dcf98c390fa420c2c9a58102fdb766a179d9577
5
5
  SHA512:
6
- metadata.gz: 239f4a3ef419cc72978ee7300867b66978e86f5154918b0a537397ef0ac2a3b1726ac91132c2673c637335c871fb4acecf88a26ff952f26fcd32177208dcb8b8
7
- data.tar.gz: 79c93b9aea448ca971a9d4f531a3de7377601f771ee7e8830258cea8a27b5bccd3b9271a743550e84a4e0682fb77ce84e969129a5c6ff4a17faee3c53a087a17
6
+ metadata.gz: 101b0bbf1881240b6a84516450e8e73670c726339d16d1bb7b15e3efd806422f237a95dcc5b1e7145c951df4326cbda679d7f89c0523909822e0d3a28ecf2c73
7
+ data.tar.gz: 4a5b4495f7a8335e8514c2dd76fc332ce6f46afe070132f9aee427317aaeedcbfd4922c23fb7efff8c429f89a2cff74077cfdb5d3e4134279867556b9e38a614
data/README.adoc CHANGED
@@ -27,7 +27,7 @@ Vectory relies on the following software to be installed:
27
27
  * https://inkscape.org[Inkscape]
28
28
  * https://www.ghostscript.com/[Ghostscript]
29
29
 
30
- NOTE: Inkscape 1.3.1 does not work properly with EPS/PS on Windows. To avoid
30
+ NOTE: Inkscape 1.3.1+ does not work properly with EPS/PS on Windows. To avoid
31
31
  this issue, the 1.3.0 version of Inkscape can be used.
32
32
 
33
33
 
data/lib/vectory/emf.rb CHANGED
@@ -29,7 +29,7 @@ module Vectory
29
29
 
30
30
  def to_svg
31
31
  with_file("emf") do |input_path|
32
- content = Emf2svg.from_file(input_path).sub(/<\?[^>]+>/, "")
32
+ content = Emf2svg.from_file(input_path)
33
33
 
34
34
  Svg.from_content(content)
35
35
  end
@@ -2,10 +2,12 @@ require "image_size"
2
2
 
3
3
  module Vectory
4
4
  class ImageResize
5
+ BORDER_WIDTH = 2
6
+
5
7
  def call(img, path, maxheight, maxwidth)
6
8
  s, realsize = get_image_size(img, path)
7
9
  img.name == "svg" && !img["viewBox"] && s[0] && s[1] and
8
- img["viewBox"] = "0 0 #{s[0]} #{s[1]}"
10
+ img["viewBox"] = viewbox(s)
9
11
  s, skip = image_dont_resize(s, realsize)
10
12
  skip and return s
11
13
  s = image_size_fillin(s, realsize)
@@ -20,6 +22,13 @@ module Vectory
20
22
 
21
23
  private
22
24
 
25
+ def viewbox(dimensions)
26
+ width = dimensions[0] + BORDER_WIDTH
27
+ height = dimensions[1] + BORDER_WIDTH
28
+
29
+ "0 0 #{width} #{height}"
30
+ end
31
+
23
32
  def image_dont_resize(dim, realsize)
24
33
  dim.nil? and return [[nil, nil], true]
25
34
  realsize.nil? and return [dim, true]
data/lib/vectory/svg.rb CHANGED
@@ -25,8 +25,10 @@ module Vectory
25
25
  from_path(uri)
26
26
  end
27
27
 
28
- def content
29
- @document&.to_xml || @content
28
+ def initialize(content = nil, initial_path = nil)
29
+ super
30
+
31
+ self.content = content
30
32
  end
31
33
 
32
34
  def to_emf
@@ -41,81 +43,17 @@ module Vectory
41
43
  convert_with_inkscape("--export-type=ps", Ps)
42
44
  end
43
45
 
44
- def namespace(suffix, links, xpath_to_remove)
45
- remap_links(links)
46
- suffix_ids(suffix)
47
- remove_xpath(xpath_to_remove)
48
- end
49
-
50
- def remap_links(map)
51
- document.xpath(".//m:a", "m" => SVG_NS).each do |a|
52
- href_attrs = ["xlink:href", "href"]
53
- href_attrs.each do |p|
54
- a[p] and x = map[File.expand_path(a[p])] and a[p] = x
55
- end
56
- end
57
-
58
- self
59
- end
60
-
61
- def suffix_ids(suffix)
62
- ids = collect_ids
63
- return if ids.empty?
64
-
65
- update_ids_attrs(ids, suffix)
66
- update_ids_css(ids, suffix)
67
-
68
- self
69
- end
70
-
71
- def remove_xpath(xpath)
72
- document.xpath(xpath).remove
73
-
74
- self
75
- end
76
-
77
46
  private
78
47
 
79
48
  def content=(content)
80
- if @document
81
- @document = Nokogiri::XML(content)
82
- else
83
- @content = content
84
- end
85
- end
86
-
87
- def document
88
- @document ||= begin
89
- doc = Nokogiri::XML(@content)
90
- @content = nil
91
- doc
92
- end
93
- end
94
-
95
- def collect_ids
96
- document.xpath("./@id | .//@id").map(&:value)
97
- end
98
-
99
- def update_ids_attrs(ids, suffix)
100
- document.xpath(". | .//*[@*]").each do |a|
101
- a.attribute_nodes.each do |x|
102
- ids.include?(x.value) and x.value += sprintf("_%09d", suffix)
103
- end
49
+ # non-root node inserts the xml tag which breaks markup when placed in
50
+ # another xml document
51
+ document = Nokogiri::XML(content).root
52
+ unless document
53
+ raise ParsingError, "Could not parse '#{content&.slice(0, 30)}'"
104
54
  end
105
- end
106
-
107
- def update_ids_css(ids, suffix)
108
- document.xpath("//m:style", "m" => SVG_NS).each do |s|
109
- c = s.children.to_xml
110
- ids.each do |i|
111
- c = c.gsub(%r[##{i}\b],
112
- sprintf("#%<id>s_%<suffix>09d", id: i, suffix: suffix))
113
- .gsub(%r(\[id\s*=\s*['"]?#{i}['"]?\]),
114
- sprintf("[id='%<id>s_%<suffix>09d']", id: i, suffix: suffix))
115
- end
116
55
 
117
- s.children = c
118
- end
56
+ @content = document.to_xml
119
57
  end
120
58
  end
121
59
  end
@@ -0,0 +1,76 @@
1
+ require "nokogiri"
2
+
3
+ module Vectory
4
+ class SvgDocument
5
+ SVG_NS = "http://www.w3.org/2000/svg".freeze
6
+
7
+ def initialize(content)
8
+ @document = Nokogiri::XML(content)
9
+ end
10
+
11
+ def content
12
+ @document.root.to_xml
13
+ end
14
+
15
+ def namespace(suffix, links, xpath_to_remove)
16
+ remap_links(links)
17
+ suffix_ids(suffix)
18
+ remove_xpath(xpath_to_remove)
19
+ end
20
+
21
+ def remap_links(map)
22
+ @document.xpath(".//m:a", "m" => SVG_NS).each do |a|
23
+ href_attrs = ["xlink:href", "href"]
24
+ href_attrs.each do |p|
25
+ a[p] and x = map[File.expand_path(a[p])] and a[p] = x
26
+ end
27
+ end
28
+
29
+ self
30
+ end
31
+
32
+ def suffix_ids(suffix)
33
+ ids = collect_ids
34
+ return if ids.empty?
35
+
36
+ update_ids_attrs(ids, suffix)
37
+ update_ids_css(ids, suffix)
38
+
39
+ self
40
+ end
41
+
42
+ def remove_xpath(xpath)
43
+ @document.xpath(xpath).remove
44
+
45
+ self
46
+ end
47
+
48
+ private
49
+
50
+ def collect_ids
51
+ @document.xpath("./@id | .//@id").map(&:value)
52
+ end
53
+
54
+ def update_ids_attrs(ids, suffix)
55
+ @document.xpath(". | .//*[@*]").each do |a|
56
+ a.attribute_nodes.each do |x|
57
+ ids.include?(x.value) and x.value += sprintf("_%09d", suffix)
58
+ end
59
+ end
60
+ end
61
+
62
+ def update_ids_css(ids, suffix)
63
+ @document.xpath("//m:style", "m" => SVG_NS).each do |s|
64
+ c = s.children.to_xml
65
+ ids.each do |i|
66
+ c = c.gsub(%r[##{i}\b],
67
+ sprintf("#%<id>s_%<suffix>09d", id: i, suffix: suffix))
68
+ .gsub(%r(\[id\s*=\s*['"]?#{i}['"]?\]),
69
+ sprintf("[id='%<id>s_%<suffix>09d']", id: i, suffix: suffix))
70
+ end
71
+
72
+ s.children = c
73
+ end
74
+ end
75
+ end
76
+ end
@@ -1,4 +1,5 @@
1
1
  require_relative "svg"
2
+ require_relative "svg_document"
2
3
 
3
4
  module Vectory
4
5
  class SvgMapping
@@ -70,13 +71,19 @@ module Vectory
70
71
  end
71
72
 
72
73
  def generate_content(image, svgmap, suffix)
73
- vector = build_vector(image)
74
- return unless vector
74
+ document = build_svg_document(image)
75
+ return unless document
75
76
 
76
77
  links_map = from_targets_to_links_map(svgmap)
77
- vector.namespace(suffix, links_map, PROCESSING_XPATH)
78
+ document.namespace(suffix, links_map, PROCESSING_XPATH)
79
+
80
+ document.content
81
+ end
82
+
83
+ def build_svg_document(image)
84
+ vector = build_vector(image)
78
85
 
79
- vector.content
86
+ SvgDocument.new(vector.content)
80
87
  end
81
88
 
82
89
  def build_vector(image)
@@ -33,6 +33,7 @@ module Vectory
33
33
  def execute(cmd)
34
34
  result = Capture.with_timeout(cmd,
35
35
  timeout: @timeout,
36
+ signal: :KILL, # only KILL works on Windows
36
37
  kill_after: @timeout)
37
38
  @stdout = result[:stdout]
38
39
  @stderr = result[:stderr]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Vectory
4
- VERSION = "0.7.2"
4
+ VERSION = "0.7.4"
5
5
  end
data/lib/vectory.rb CHANGED
@@ -28,6 +28,8 @@ module Vectory
28
28
 
29
29
  class NotWrittenToDiskError < Error; end
30
30
 
31
+ class ParsingError < Error; end
32
+
31
33
  def self.ui
32
34
  @ui ||= Logger.new(STDOUT).tap do |logger|
33
35
  logger.level = ENV['VECTORY_LOG'] || Logger::WARN
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vectory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.7.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-15 00:00:00.000000000 Z
11
+ date: 2024-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: emf2svg
@@ -112,6 +112,7 @@ files:
112
112
  - lib/vectory/inkscape_converter.rb
113
113
  - lib/vectory/ps.rb
114
114
  - lib/vectory/svg.rb
115
+ - lib/vectory/svg_document.rb
115
116
  - lib/vectory/svg_mapping.rb
116
117
  - lib/vectory/system_call.rb
117
118
  - lib/vectory/utils.rb