vectory 0.6.0 → 0.7.0

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: 50efb043c71096f66bc72c7804b8707b6210ab4af071f231d7e81f52a0c66da0
4
- data.tar.gz: a3ffda2f587f7796f2033fab1230db852d98e82da88b269d230646dad6acbd39
3
+ metadata.gz: ff5471297e323f4b1bac83778ca6bd5f51b78459e919c0905c0b584b87942b49
4
+ data.tar.gz: d4c5629914ba5ae8b0fc13781972b088359e9b71ea697a9d7a751f6b72c1128d
5
5
  SHA512:
6
- metadata.gz: aca9af8bee9a5f916fc0360b9473705307bfeda4b0086ae9fa53929381169a4791a483a126f98f1295574da06d70a2ea92bf3a706cd7359f91fa6b492c305205
7
- data.tar.gz: '05286cb944c4235c5d60d5591b63be2d15c43f796fa0d63b00205ced005fe8c84f314c2d468fd5ec2a02336682d4b4c0357fd63bd6757056f95aa727ec8a3884'
6
+ metadata.gz: 7b5bb40332285a833e1c2a8e4442617628c97d4cf334fe6216a518e88d3f822aeb8efd66907bed0cfbe3eb6e2bbbb036767b7ab70b58a1bf9ec959c96cc07bfc
7
+ data.tar.gz: 53f4070802a2baad61650431a7e3b36da9296f17e0e26d586dd43ed3285b7b2bd82c320541142cca856683930f894cc3dc547eee775fc2bccb404ea198b54f93
@@ -0,0 +1,70 @@
1
+ require "image_size"
2
+
3
+ module Vectory
4
+ class ImageResize
5
+ def call(img, path, maxheight, maxwidth)
6
+ s, realsize = get_image_size(img, path)
7
+ img.name == "svg" && !img["viewBox"] && s[0] && s[1] and
8
+ img["viewBox"] = "0 0 #{s[0]} #{s[1]}"
9
+ s, skip = image_dont_resize(s, realsize)
10
+ skip and return s
11
+ s = image_size_fillin(s, realsize)
12
+ image_shrink(s, maxheight, maxwidth)
13
+ end
14
+
15
+ def get_image_size(img, path)
16
+ realsize = ImageSize.path(path).size
17
+ s = image_size_interpret(img, realsize || [nil, nil])
18
+ image_size_zeroes_complete(s, realsize)
19
+ end
20
+
21
+ private
22
+
23
+ def image_dont_resize(dim, realsize)
24
+ dim.nil? and return [[nil, nil], true]
25
+ realsize.nil? and return [dim, true]
26
+ dim[0] == nil && dim[1] == nil and return [dim, true]
27
+ [dim, false]
28
+ end
29
+
30
+ def image_size_fillin(dim, realsize)
31
+ dim[1] = fill_size(dim[1], dim[0], realsize[1], realsize[0])
32
+ dim[0] = fill_size(dim[0], dim[1], realsize[0], realsize[1])
33
+
34
+ dim
35
+ end
36
+
37
+ def fill_size(current, another, real_current, real_another)
38
+ return current unless current.zero? && !another.zero?
39
+
40
+ another * real_current / real_another
41
+ end
42
+
43
+ def image_shrink(dim, maxheight, maxwidth)
44
+ dim[1] > maxheight and
45
+ dim = [(dim[0] * maxheight / dim[1]).ceil, maxheight]
46
+ dim[0] > maxwidth and
47
+ dim = [maxwidth, (dim[1] * maxwidth / dim[0]).ceil]
48
+ dim
49
+ end
50
+
51
+ def image_size_interpret(img, realsize)
52
+ w = image_size_percent(img["width"], realsize[0])
53
+ h = image_size_percent(img["height"], realsize[1])
54
+ [w, h]
55
+ end
56
+
57
+ def image_size_percent(value, real)
58
+ /%$/.match?(value) && !real.nil? and
59
+ value = real * (value.sub(/%$/, "").to_f / 100)
60
+ value.to_i
61
+ end
62
+
63
+ def image_size_zeroes_complete(dim, realsize)
64
+ if dim[0].zero? && dim[1].zero?
65
+ dim = realsize
66
+ end
67
+ [dim, realsize]
68
+ end
69
+ end
70
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Vectory
4
- VERSION = "0.6.0"
4
+ VERSION = "0.7.0"
5
5
  end
data/lib/vectory.rb CHANGED
@@ -4,6 +4,7 @@ require "logger"
4
4
  require_relative "vectory/version"
5
5
  require_relative "vectory/utils"
6
6
  require_relative "vectory/image"
7
+ require_relative "vectory/image_resize"
7
8
  require_relative "vectory/datauri"
8
9
  require_relative "vectory/vector"
9
10
  require_relative "vectory/eps"
@@ -41,4 +42,8 @@ module Vectory
41
42
  def self.convert(image, format)
42
43
  image.convert(format)
43
44
  end
45
+
46
+ def self.image_resize(img, path, maxheight, maxwidth)
47
+ Vectory::ImageResize.new.call(img, path, maxheight, maxwidth)
48
+ end
44
49
  end
data/vectory.gemspec CHANGED
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.required_ruby_version = ">= 2.5.0"
28
28
 
29
29
  spec.add_runtime_dependency "emf2svg"
30
+ spec.add_runtime_dependency "image_size", ">= 3.2.0"
30
31
  spec.add_runtime_dependency "marcel", "~> 1.0.0"
31
32
  spec.add_runtime_dependency "nokogiri", "~> 1.14"
32
33
  spec.add_runtime_dependency "thor", "~> 1.2.1"
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.6.0
4
+ version: 0.7.0
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-01-04 00:00:00.000000000 Z
11
+ date: 2024-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: emf2svg
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: image_size
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 3.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 3.2.0
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: marcel
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +108,7 @@ files:
94
108
  - lib/vectory/eps.rb
95
109
  - lib/vectory/file_magic.rb
96
110
  - lib/vectory/image.rb
111
+ - lib/vectory/image_resize.rb
97
112
  - lib/vectory/inkscape_converter.rb
98
113
  - lib/vectory/ps.rb
99
114
  - lib/vectory/svg.rb