Skip to content

Commit

Permalink
PDFMaker: introduce pdfmaker/use_original_image_size parameter to dis…
Browse files Browse the repository at this point in the history
…able an automatic image scaling. Closes: #1461
  • Loading branch information
kmuto committed Jan 2, 2020
1 parent 50876fb commit ec10dd3
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 6 deletions.
3 changes: 3 additions & 0 deletions doc/config.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,9 @@ pdfmaker:
# 画像のscale=X.Xという指定を画像拡大縮小率からページ最大幅の相対倍率に変換する
# image_scale2width: true
#
# 画像のデフォルトのサイズを、版面横幅合わせではなく、原寸をそのまま利用する
# use_original_image_size: null
#
# PDFやIllustratorファイル(.ai)の画像のBoudingBoxの抽出に指定のボックスを採用する
# cropbox(デフォルト), mediabox, artbox, trimbox, bleedboxから選択する。
# Illustrator CC以降のIllustratorファイルに対してはmediaboxを指定する必要がある
Expand Down
4 changes: 2 additions & 2 deletions lib/review/compiler.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2009-2019 Minero Aoki, Kenshi Muto
# Copyright (c) 2009-2020 Minero Aoki, Kenshi Muto
# Copyright (c) 2002-2007 Minero Aoki
#
# This program is free software.
Expand Down Expand Up @@ -115,7 +115,7 @@ def inline_defined?(name)
defblock :emlist, 0..2
defblock :cmd, 0..1
defblock :table, 0..2
defblock :imgtable, 0..2
defblock :imgtable, 0..3
defblock :emtable, 0..1
defblock :quote, 0
defblock :image, 2..3, true
Expand Down
5 changes: 3 additions & 2 deletions lib/review/configure.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2012-2019 Masanori Kado, Masayoshi Takahashi, Kenshi Muto
# Copyright (c) 2012-2020 Masanori Kado, Masayoshi Takahashi, Kenshi Muto
#
# This program is free software.
# You can distribute or modify this program under the terms of
Expand Down Expand Up @@ -92,7 +92,8 @@ def self.values
'makeindex_dic' => nil,
'makeindex_mecab' => true,
'makeindex_mecab_opts' => '-Oyomi',
'use_cover_nombre' => true
'use_cover_nombre' => true,
'use_original_image_size' => nil
},
'imgmath_options' => {
'format' => 'png',
Expand Down
10 changes: 9 additions & 1 deletion lib/review/latexbuilder.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (c) 2002-2007 Minero Aoki
# 2008-2009 Minero Aoki, Kenshi Muto
# 2010-2019 Minero Aoki, Kenshi Muto, TAKAHASHI Masayoshi
# 2010-2020 Minero Aoki, Kenshi Muto, TAKAHASHI Masayoshi
#
# This program is free software.
# You can distribute or modify this program under the terms of
Expand Down Expand Up @@ -482,6 +482,14 @@ def source(lines, caption = nil, lang = nil)
def image_header(id, caption)
end

def parse_metric(type, metric)
s = super(type, metric)
if @book.config['pdfmaker']['use_original_image_size'] && s.empty? && metric.nil?
return ' ' # pass empty to \reviewincludegraphics
end
s
end

def handle_metric(str)
if @book.config['image_scale2width'] && str =~ /\Ascale=([\d.]+)\Z/
return "width=#{$1}\\maxwidth"
Expand Down
93 changes: 92 additions & 1 deletion test/test_latexbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def setup
'secnolevel' => 2, # for IDGXMLBuilder, EPUBBuilder
'toclevel' => 2,
'stylesheet' => nil, # for EPUBBuilder
'image_scale2width' => false,
'image_scale2width' => nil,
'texcommand' => 'uplatex',
'review_version' => '3'
)
Expand Down Expand Up @@ -857,6 +857,17 @@ def @chapter.image(_id)
\\reviewimagecaption{sample photo}
\\label{image:chap1:sampleimg}
\\end{reviewimage}
EOS
assert_equal expected, actual

@book.config['pdfmaker']['use_original_image_size'] = true
actual = compile_block("//image[sampleimg][sample photo]{\n//}\n")
expected = <<-EOS
\\begin{reviewimage}%%sampleimg
\\reviewincludegraphics[ ]{./images/chap1-sampleimg.png}
\\reviewimagecaption{sample photo}
\\label{image:chap1:sampleimg}
\\end{reviewimage}
EOS
assert_equal expected, actual
end
Expand All @@ -877,6 +888,10 @@ def @chapter.image(_id)
\\end{reviewimage}
EOS
assert_equal expected, actual

@book.config['pdfmaker']['use_original_image_size'] = true
actual = compile_block("//image[sampleimg][sample photo][scale=1.2]{\n//}\n")
assert_equal expected, actual
end

def test_image_with_metric_width
Expand All @@ -896,6 +911,10 @@ def @chapter.image(_id)
\\end{reviewimage}
EOS
assert_equal expected, actual

@book.config['pdfmaker']['use_original_image_size'] = true
actual = compile_block("//image[sampleimg][sample photo][scale=1.2]{\n//}\n")
assert_equal expected, actual
end

def test_image_with_metric2
Expand All @@ -914,6 +933,10 @@ def @chapter.image(_id)
\\end{reviewimage}
EOS
assert_equal expected, actual

@book.config['pdfmaker']['use_original_image_size'] = true
actual = compile_block("//image[sampleimg][sample photo][scale=1.2,html::class=sample,latex::ignore=params]{\n//}\n")
assert_equal expected, actual
end

def test_image_with_metric2_width
Expand All @@ -933,6 +956,10 @@ def @chapter.image(_id)
\\end{reviewimage}
EOS
assert_equal expected, actual

@book.config['pdfmaker']['use_original_image_size'] = true
actual = compile_block("//image[sampleimg][sample photo][scale=1.2,html::class=sample,latex::ignore=params]{\n//}\n")
assert_equal expected, actual
end

def test_indepimage
Expand All @@ -948,6 +975,16 @@ def @chapter.image(_id)
\\reviewincludegraphics[width=\\maxwidth]{./images/chap1-sampleimg.png}
\\reviewindepimagecaption{図: sample photo}
\\end{reviewimage}
EOS
assert_equal expected, actual

@book.config['pdfmaker']['use_original_image_size'] = true
actual = compile_block("//indepimage[sampleimg][sample photo]\n")
expected = <<-EOS
\\begin{reviewimage}%%sampleimg
\\reviewincludegraphics[ ]{./images/chap1-sampleimg.png}
\\reviewindepimagecaption{図: sample photo}
\\end{reviewimage}
EOS
assert_equal expected, actual
end
Expand Down Expand Up @@ -984,6 +1021,10 @@ def @chapter.image(_id)
\\end{reviewimage}
EOS
assert_equal expected, actual

@book.config['pdfmaker']['use_original_image_size'] = true
actual = compile_block("//indepimage[sampleimg][sample photo][scale=1.2]\n")
assert_equal expected, actual
end

def test_indepimage_with_metric_width
Expand All @@ -1002,6 +1043,10 @@ def @chapter.image(_id)
\\end{reviewimage}
EOS
assert_equal expected, actual

@book.config['pdfmaker']['use_original_image_size'] = true
actual = compile_block("//indepimage[sampleimg][sample photo][scale=1.2]\n")
assert_equal expected, actual
end

def test_indepimage_with_metric2
Expand All @@ -1019,6 +1064,10 @@ def @chapter.image(_id)
\\end{reviewimage}
EOS
assert_equal expected, actual

@book.config['pdfmaker']['use_original_image_size'] = true
actual = compile_block(%Q(//indepimage[sampleimg][sample photo][scale=1.2, html::class="sample",latex::ignore=params]\n))
assert_equal expected, actual
end

def test_indepimage_without_caption_but_with_metric
Expand All @@ -1036,6 +1085,10 @@ def @chapter.image(_id)
\\end{reviewimage}
EOS
assert_equal expected, actual

@book.config['pdfmaker']['use_original_image_size'] = true
actual = compile_block("//indepimage[sampleimg][][scale=1.2]\n")
assert_equal expected, actual
end

def test_table
Expand Down Expand Up @@ -1194,6 +1247,44 @@ def @chapter.image(_id)
\\end{table}
EOS
assert_equal expected, actual

@book.config['pdfmaker']['use_original_image_size'] = true
actual = compile_block("//imgtable[sampleimg][test for imgtable]{\n//}\n")

expected = <<-EOS
\\begin{table}[h]%%sampleimg
\\reviewimgtablecaption{test for imgtable}
\\label{table:chap1:sampleimg}
\\begin{reviewimage}%%sampleimg
\\reviewincludegraphics[ ]{./images/chap1-sampleimg.png}
\\end{reviewimage}
\\end{table}
EOS
assert_equal expected, actual
end

def test_imgtable_with_metrics
def @chapter.image(_id)
item = Book::Index::Item.new('sampleimg', 1, 'sample img')
item.instance_eval { @path = './images/chap1-sampleimg.png' }
item
end

actual = compile_block("//imgtable[sampleimg][test for imgtable][scale=1.2]{\n//}\n")
expected = <<-EOS
\\begin{table}[h]%%sampleimg
\\reviewimgtablecaption{test for imgtable}
\\label{table:chap1:sampleimg}
\\begin{reviewimage}%%sampleimg
\\reviewincludegraphics[scale=1.2]{./images/chap1-sampleimg.png}
\\end{reviewimage}
\\end{table}
EOS
assert_equal expected, actual

@book.config['pdfmaker']['use_original_image_size'] = true
actual = compile_block("//imgtable[sampleimg][test for imgtable][scale=1.2]{\n//}\n")
assert_equal expected, actual
end

def test_table_row_separator
Expand Down

0 comments on commit ec10dd3

Please sign in to comment.