Skip to content

Commit

Permalink
Merge pull request #88 from metanorma/features/image-size
Browse files Browse the repository at this point in the history
refactor image size to metanorma-utils: https://github.com/metanorma/…
  • Loading branch information
opoudjis authored Jan 6, 2024
2 parents e8aed98 + 5b664ef commit f3be39b
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 71 deletions.
1 change: 1 addition & 0 deletions Gemfile.devel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gem "metanorma-utils", git: "https://github.com/metanorma/metanorma-utils", branch: "features/image-size"
1 change: 0 additions & 1 deletion html2doc.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")

spec.add_dependency "htmlentities", "~> 4.3.4"
spec.add_dependency "image_size", ">= 3.2.0"
spec.add_dependency "metanorma-utils"
spec.add_dependency "mime-types"
spec.add_dependency "nokogiri", "~> 1.15"
Expand Down
25 changes: 2 additions & 23 deletions lib/html2doc/mime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,6 @@ def contentid(mhtml)
end
end

def image_resize(img, path, maxheight, maxwidth)
s, realsize = get_image_size(img, path)
s[0] == nil && s[1] == nil and return s
if img.name == "svg" && !img["viewBox"]
img["viewBox"] = "0 0 #{s[0]} #{s[1]}"
end
s[1] = s[0] * realsize[1] / realsize[0] if s[1].zero? && !s[0].zero?
s[0] = s[1] * realsize[0] / realsize[1] if s[0].zero? && !s[1].zero?
s = [(s[0] * maxheight / s[1]).ceil, maxheight] if s[1] > maxheight
s = [maxwidth, (s[1] * maxwidth / s[0]).ceil] if s[0] > maxwidth
s
end

def get_image_size(img, path)
realsize = ImageSize.path(path).size
s = [img["width"].to_i, img["height"].to_i]
s = realsize if s[0].zero? && s[1].zero?
s = [nil, nil] if realsize.nil? || realsize[0].nil? || realsize[1].nil?
[s, realsize]
end

IMAGE_PATH = "//*[local-name() = 'img' or local-name() = 'imagedata']".freeze

def mkuuid
Expand All @@ -119,9 +98,9 @@ def image_cleanup(docxml, dir, localdir)
local_filename = rename_image(i, dir, localdir)
i["width"], i["height"] =
if landscape?(i)
image_resize(i, local_filename, maxwidth, maxheight)
Metanorma::Utils.image_resize(i, local_filename, maxwidth, maxheight)
else
image_resize(i, local_filename, maxheight, maxwidth)
Metanorma::Utils.image_resize(i, local_filename, maxheight, maxwidth)
end
end
docxml
Expand Down
47 changes: 0 additions & 47 deletions spec/html2doc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -816,53 +816,6 @@ def image_clean(xml)
OUTPUT
end

it "resizes images with missing or auto sizes" do
image = Nokogiri::XML("<img src='spec/19160-8.jpg'/>").root
expect(Html2Doc.new({}).image_resize(image, "spec/19160-8.jpg", 100, 100))
.to eq [30, 100]
image["width"] = "20"
expect(Html2Doc.new({}).image_resize(image, "spec/19160-8.jpg", 100, 100))
.to eq [20, 65]
image.delete("width")
image["height"] = "50"
expect(Html2Doc.new({}).image_resize(image, "spec/19160-8.jpg", 100, 100))
.to eq [15, 50]
image.delete("height")
image["width"] = "500"
expect(Html2Doc.new({}).image_resize(image, "spec/19160-8.jpg", 100, 100))
.to eq [30, 100]
image.delete("width")
image["height"] = "500"
expect(Html2Doc.new({}).image_resize(image, "spec/19160-8.jpg", 100, 100))
.to eq [30, 100]
image["width"] = "20"
image["height"] = "auto"
expect(Html2Doc.new({}).image_resize(image, "spec/19160-8.jpg", 100, 100))
.to eq [20, 65]
image["width"] = "auto"
image["height"] = "50"
expect(Html2Doc.new({}).image_resize(image, "spec/19160-8.jpg", 100, 100))
.to eq [15, 50]
image["width"] = "500"
image["height"] = "auto"
expect(Html2Doc.new({}).image_resize(image, "spec/19160-8.jpg", 100, 100))
.to eq [30, 100]
image["width"] = "auto"
image["height"] = "500"
expect(Html2Doc.new({}).image_resize(image, "spec/19160-8.jpg", 100, 100))
.to eq [30, 100]
image["width"] = "auto"
image["height"] = "auto"
expect(Html2Doc.new({}).image_resize(image, "spec/19160-8.jpg", 100, 100))
.to eq [30, 100]
end

it "resizes SVG with missing or auto sizes" do
image = Nokogiri::XML(File.read("spec/odf.svg")).root
Html2Doc.new({}).image_resize(image, "spec/odf.svg", 100, 100)
expect(image.to_xml).to match_fuzzy '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"/>'
end

it "does not move images if they are external URLs" do
simple_body = '<img src="https://example.com/19160-6.png">'
Html2Doc.new(filename: "test", imagedir: ".")
Expand Down

0 comments on commit f3be39b

Please sign in to comment.