Skip to content

Commit

Permalink
resolves asciidoctor#455 support default value for pdfwidth attribute
Browse files Browse the repository at this point in the history
- allow default value for pdfwidth attribute to be specified in theme
- only allow vw units when resolving width for block image
- use ViewportWidth module to mark width with vw units
- consolidate variable names
  • Loading branch information
mojavelinux committed Aug 16, 2016
1 parent 338b85a commit 9719171
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
8 changes: 8 additions & 0 deletions docs/theming-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1684,8 +1684,16 @@ The keys in this category control the arrangement of block images.
(default: left)
|image:
align: left

|width^[1]^
|<<measurement-units,Measurement>>
|image:
width: 100%
|===

. Only applies to block images.
If specified, the value in the theme takes precedence over the value of the `width` attribute on the image macro, but not the value of the `pdfwidth` attribute.

=== Lead

The keys in this category control the styling of lead paragraphs.
Expand Down
28 changes: 19 additions & 9 deletions lib/asciidoctor-pdf/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class Converter < ::Prawn::Document
ImageAttributeValueRx = /^image:{1,2}(.*?)\[(.*?)\]$/
LineScanRx = /\n|.+/
SourceHighlighters = ['coderay', 'pygments', 'rouge'].to_set
ViewportWidth = ::Module.new

def initialize backend, opts
super
Expand Down Expand Up @@ -818,8 +819,9 @@ def convert_image node
theme_margin :block, :top

# TODO support cover (aka canvas) image layout using "canvas" (or "cover") role
width = resolve_explicit_width node.attributes, bounds.width
if (width_relative_to_page = (node.attr? 'pdfwidth', nil, false) && ((node.attr 'pdfwidth').end_with? 'vw'))
width = resolve_explicit_width node.attributes, bounds.width, allow_vw: true, use_fallback: true
if (width_relative_to_page = ViewportWidth === width)
width = (width.to_f / 100) * page_width
overflow = [bounds_margin_left, bounds_margin_right]
else
overflow = 0
Expand Down Expand Up @@ -2507,20 +2509,28 @@ def resolve_background_image doc, theme, key
# max_width, the max_width value is returned.
#--
# QUESTION should we enforce positive result?
def resolve_explicit_width attrs, max_width = bounds.width
def resolve_explicit_width attrs, max_width = bounds.width, opts = {}
if attrs.key? 'pdfwidth'
if (pdfwidth = attrs['pdfwidth']).end_with? '%'
(pdfwidth.to_f / 100) * max_width
elsif pdfwidth.end_with? 'vw'
(pdfwidth.to_f / 100) * page_width
if (width = attrs['pdfwidth']).end_with? '%'
(width.to_f / 100) * max_width
elsif opts[:allow_vw] && (width.end_with? 'vw')
(width.chomp 'vw').extend ViewportWidth
else
str_to_pt pdfwidth
str_to_pt width
end
elsif attrs.key? 'scaledwidth'
(attrs['scaledwidth'].to_f / 100) * max_width
elsif opts[:use_fallback] && (width = @theme.image_width)
if width.end_with? '%'
(width.to_f / 100) * max_width
elsif opts[:allow_vw] && (width.end_with? 'vw')
(width.chomp 'vw').extend ViewportWidth
else
str_to_pt width
end
elsif attrs.key? 'width'
# QUESTION should we honor percentage width value?
# NOTE scale width down 75% to convert px to pt; restrict width to bounds.width
# NOTE scale width down 75% to convert px to pt; restrict width to max width
[max_width, attrs['width'].to_f * 0.75].min
end
end
Expand Down

0 comments on commit 9719171

Please sign in to comment.