vectory 0.7.8 → 0.8.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ccf79449f78b1e3df6ed0792a5ef740e96da77d0e38f7e9ab4bf4d47d9b31334
4
- data.tar.gz: 16aec00dcfcd5c3bc9caf81796bbc93665987927dc8e320aaee607d2fe5aaaa8
3
+ metadata.gz: d7ebbb9e4146780f1f9d42e0dee106494c251eb27b7d3f52d69f172da0a0d2e7
4
+ data.tar.gz: 132e451c7c1e9298f305f67a7103f8da8b7d5af3df371f5705e8a2eb95b59991
5
5
  SHA512:
6
- metadata.gz: b87eeaf37029993930140bba471b9bd677465002e0bf5c7eb91ef2550d56d3c3456a3ab02e4e2a5d3db0b403cc129f7f68d1ec8f9dcd02bcd1a3afd2b7f3aad3
7
- data.tar.gz: 20bbd0ff186b26123d676cad1ffdf2f46ff49c383b728e21be6c14a3287af120cb5a0578b4762a1cdf2b5eca37c4afeccc712b831454daf83217a9c8e82c7724
6
+ metadata.gz: 33a7acbdacd776a6627d30a2641123743d325b3c5f89b3f1f40a244556b4f363c1c2a4b49458d2260014c53004a2d06a9c8d7f617e732f82457c30576e9c6085
7
+ data.tar.gz: 4a243d964fcda1b2d7e8dab7ef9bea706265c646d168d70df847bc6fd4d4e6c9f3453334adf4b0824670a59c029e4fa8a5812d4aecb4714298dfaa3425e1010a
@@ -6,6 +6,7 @@ module Vectory
6
6
 
7
7
  def call(img, path, maxheight, maxwidth)
8
8
  s, realsize = get_image_size(img, path)
9
+ # Ensure s[0] and s[1] are not nil before setting viewBox
9
10
  img.name == "svg" && !img["viewBox"] && s[0] && s[1] and
10
11
  img["viewBox"] = viewbox(s)
11
12
  s, skip = image_dont_resize(s, realsize)
@@ -18,7 +19,8 @@ module Vectory
18
19
  detected = detect_size(path)
19
20
  realsize = detected if detected
20
21
  s = image_size_interpret(img, realsize || [nil, nil])
21
- image_size_zeroes_complete(s, realsize)
22
+ # No call to image_size_zeroes_complete here
23
+ [s, realsize] # s should now be correctly populated
22
24
  end
23
25
 
24
26
  private
@@ -53,7 +55,11 @@ module Vectory
53
55
  end
54
56
 
55
57
  def fill_size(current, another, real_current, real_another)
56
- return current unless current.zero? && !another.zero?
58
+ # Ensure 'current' and 'another' are not nil before calling zero?
59
+ # Also ensure real_another is not nil or zero to prevent division by zero
60
+ return current unless (current.nil? || current.zero?) &&
61
+ !(another.nil? || another.zero?) &&
62
+ !(real_another.nil? || real_another.zero?)
57
63
 
58
64
  another * real_current / real_another
59
65
  end
@@ -67,22 +73,102 @@ module Vectory
67
73
  end
68
74
 
69
75
  def image_size_interpret(img, realsize)
70
- w = image_size_percent(img["width"], realsize[0])
71
- h = image_size_percent(img["height"], realsize[1])
76
+ # Extract parent dimensions for percentage calculations
77
+ parent_w_px = realsize.is_a?(Array) && realsize.length > 0 ? realsize[0] : nil
78
+ parent_h_px = realsize.is_a?(Array) && realsize.length > 1 ? realsize[1] : nil
79
+
80
+ # Process width and height using the original css_size_to_px signature
81
+ w = css_size_to_px(img["width"], parent_w_px)
82
+ h = css_size_to_px(img["height"], parent_h_px)
83
+
84
+ # If attributes resulted in no dimensions, but realsize exists, use realsize.
85
+ # This brings back some of the logic from the removed image_size_zeroes_complete.
86
+ if w.nil? && h.nil? && realsize && !(realsize[0].nil? && realsize[1].nil?)
87
+ w = realsize[0]
88
+ h = realsize[1]
89
+ end
90
+
91
+ # If a dimension attribute led to a nil value (e.g. "auto", or "%" of nil parent)
92
+ # and the other dimension is resolved, default the nil dimension to 0.
93
+ if img["width"] && w.nil? && !h.nil?
94
+ w = 0
95
+ end
96
+ if img["height"] && h.nil? && !w.nil?
97
+ h = 0
98
+ end
99
+
72
100
  [w, h]
73
101
  end
74
102
 
75
- def image_size_percent(value, real)
76
- /%$/.match?(value) && !real.nil? and
77
- value = real * (value.sub(/%$/, "").to_f / 100)
78
- value.to_i
103
+ # Enhanced version of image_size_percent that handles percentage calculations
104
+ # This is now a helper method for css_size_to_px
105
+ def image_size_percent(percentage_numeric_value, real_dimension)
106
+ return nil if real_dimension.nil? || percentage_numeric_value.nil?
107
+
108
+ # Calculates the dimension in pixels. The result is a float.
109
+ (real_dimension * (percentage_numeric_value / 100.0))
79
110
  end
80
111
 
81
- def image_size_zeroes_complete(dim, realsize)
82
- if dim[0].zero? && dim[1].zero?
83
- dim = realsize
112
+ # New method from image_sizer.rb, incorporated directly into ImageResize
113
+ # Converts CSS length string to pixels.
114
+ def css_size_to_px(size_str, real_dimension_for_percentage, dpi: 96)
115
+ return nil unless size_str.is_a?(String)
116
+
117
+ cleaned_size_str = size_str.strip.downcase
118
+
119
+ # Handle keywords like "auto" which don't resolve to a numeric pixel value.
120
+ return nil if cleaned_size_str == "auto"
121
+
122
+ # Handle plain numbers (e.g., "150") as pixels.
123
+ if cleaned_size_str.match?(/^\d+(\.\d+)?$/)
124
+ return cleaned_size_str.to_f.to_i
125
+ end
126
+
127
+ # Match numeric value and unit (e.g., "10.5cm", "50%", "-5px").
128
+ match = cleaned_size_str.match(/^([-+]?[0-9]*\.?[0-9]+)([a-z%]+)$/)
129
+ unless match
130
+ # Not a plain number and not in "valueUnit" format
131
+ return nil
132
+ end
133
+
134
+ value = match[1].to_f
135
+ unit = match[2]
136
+ px_value = nil # This will store the calculated pixel value as a float.
137
+
138
+ case unit
139
+ when "px"
140
+ px_value = value
141
+ when "in"
142
+ px_value = value * dpi
143
+ when "cm"
144
+ px_value = value * dpi / 2.54
145
+ when "mm"
146
+ px_value = value * dpi / 25.4
147
+ when "pt" # 1 point = 1/72 inch
148
+ px_value = value * dpi / 72.0
149
+ when "pc" # 1 pica = 12 points
150
+ px_value = value * 12.0 * dpi / 72.0
151
+ when "%"
152
+ # Use the enhanced image_size_percent method
153
+ px_value = image_size_percent(value, real_dimension_for_percentage)
154
+ else
155
+ return nil # Unknown unit
156
+ end
157
+
158
+ return nil if px_value.nil?
159
+
160
+ # For 'px' and '%' units, truncate to match "integer conversion" expectation.
161
+ # For physical units, round to handle floating point inaccuracies correctly.
162
+ if ["px", "%"].include?(unit)
163
+ px_value.to_i
164
+ else
165
+ px_value.round
84
166
  end
85
- [dim, realsize]
86
167
  end
168
+
169
+ # REMOVE the image_size_zeroes_complete method entirely if it's still present
170
+ # def image_size_zeroes_complete(dim, realsize)
171
+ # ...
172
+ # end
87
173
  end
88
174
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Vectory
4
- VERSION = "0.7.8"
4
+ VERSION = "0.8.0"
5
5
  end
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.8
4
+ version: 0.8.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: 2025-04-26 00:00:00.000000000 Z
11
+ date: 2025-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: base64