From 3cdef01461087dd7dbebf7f4a8fb5ccc4923bbf9 Mon Sep 17 00:00:00 2001 From: alafr Date: Fri, 16 Sep 2016 00:40:15 +0300 Subject: [PATCH 1/3] Add 'cover' option for images --- lib/mixins/images.coffee | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/mixins/images.coffee b/lib/mixins/images.coffee index 1458b3c..a550a9d 100644 --- a/lib/mixins/images.coffee +++ b/lib/mixins/images.coffee @@ -51,16 +51,26 @@ module.exports = else h = bh w = bh * ip - + + else if options.cover + [bw, bh] = options.cover + bp = bw / bh + ip = image.width / image.height + if ip > bp + h = bh + w = bh * ip + else + w = bw + h = bw / ip + + if options.fit or options.cover if options.align is 'center' x = x + bw / 2 - w / 2 - else if options.align is 'right' x = x + bw - w if options.valign is 'center' y = y + bh / 2 - h / 2 - else if options.valign is 'bottom' y = y + bh - h From bd98ce3456c5fae59825236eb9711ff4b23b3bd8 Mon Sep 17 00:00:00 2001 From: alafr Date: Mon, 17 Oct 2016 18:32:45 +0300 Subject: [PATCH 2/3] Possibility to open an image without inserting it immediately in the document --- lib/mixins/images.coffee | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/lib/mixins/images.coffee b/lib/mixins/images.coffee index a550a9d..91a62e8 100644 --- a/lib/mixins/images.coffee +++ b/lib/mixins/images.coffee @@ -9,24 +9,27 @@ module.exports = if typeof x is 'object' options = x x = null - + x = x ? options.x ? @x y = y ? options.y ? @y - + unless Buffer.isBuffer(src) image = @_imageRegistry[src] - + if not image - image = PDFImage.open src, 'I' + (++@_imageCount) + if src.width and src.height + image = src + else + image = @openImage src + + unless image.obj image.embed this - unless Buffer.isBuffer(src) - @_imageRegistry[src] = image - + @page.xobjects[image.label] ?= image.obj w = options.width or image.width h = options.height or image.height - + if options.width and not options.height wp = w / image.width w = image.width * wp @@ -68,12 +71,12 @@ module.exports = x = x + bw / 2 - w / 2 else if options.align is 'right' x = x + bw - w - + if options.valign is 'center' y = y + bh / 2 - h / 2 else if options.valign is 'bottom' y = y + bh - h - + # Set the current y position to below the image if it is in the document flow @y += h if @y is y @@ -81,5 +84,16 @@ module.exports = @transform w, 0, 0, -h, x, y + h @addContent "/#{image.label} Do" @restore() - + return this + + openImage: (src) -> + unless Buffer.isBuffer(src) + image = @_imageRegistry[src] + + if not image + image = PDFImage.open src, 'I' + (++@_imageCount) + unless Buffer.isBuffer(src) + @_imageRegistry[src] = image + + return image From baeae06d18fa3e070ab0bc88d984e05029e87c56 Mon Sep 17 00:00:00 2001 From: alafr Date: Mon, 17 Oct 2016 20:52:37 +0300 Subject: [PATCH 3/3] Fix for #559 --- lib/mixins/images.coffee | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/mixins/images.coffee b/lib/mixins/images.coffee index 91a62e8..e532194 100644 --- a/lib/mixins/images.coffee +++ b/lib/mixins/images.coffee @@ -13,7 +13,7 @@ module.exports = x = x ? options.x ? @x y = y ? options.y ? @y - unless Buffer.isBuffer(src) + if typeof src is 'string' image = @_imageRegistry[src] if not image @@ -88,12 +88,12 @@ module.exports = return this openImage: (src) -> - unless Buffer.isBuffer(src) + if typeof src is 'string' image = @_imageRegistry[src] if not image image = PDFImage.open src, 'I' + (++@_imageCount) - unless Buffer.isBuffer(src) + if typeof src is 'string' @_imageRegistry[src] = image return image