Commit a237acb5 by Edgar HIPP

Update API version 1:mandatory getImage,getSize

parent 78061767
......@@ -6,6 +6,8 @@ fs=require('fs')
class ImageModule
constructor:(@options={})->
if !@options.centered? then @options.centered=false
if !@options.getImage? then throw new Error("You should pass getImage")
if !@options.getSize? then throw new Error("You should pass getSize")
@qrQueue=[]
@imageNumber=1
handleEvent:(event,eventData)->
......@@ -35,29 +37,25 @@ class ImageModule
xmlTemplater.replaceXml(subContent,text)
convertPixelsToEmus:(pixel)->
Math.round(pixel * 9525)
getSizeFromData:(imgData)->
[150,150]
getImageFromData:(imgData)->
fs.readFileSync(imgData)
replaceTag:->
scopeManager=@manager.getInstance('scopeManager')
templaterState=@manager.getInstance('templaterState')
tag = templaterState.textInsideTag.substr(1)
imgData=scopeManager.getValueFromScope(tag)
tagValue = scopeManager.getValue(tag)
tagXml=@manager.getInstance('xmlTemplater').tagXml
startEnd= "<#{tagXml}></#{tagXml}>"
if imgData=='undefined' then return @replaceBy(startEnd,tagXml)
if !tagValue? then return @replaceBy(startEnd,tagXml)
try
imgBuffer=@getImageFromData(imgData)
imgBuffer=@options.getImage(tagValue)
catch e
return @replaceBy(startEnd,tagXml)
imageRels=@imgManager.loadImageRels();
if imageRels
rId=imageRels.addImageRels(@getNextImageName(),imgBuffer)
sizePixel=@getSizeFromData(imgBuffer)
sizePixel=@options.getSize(imgBuffer, tagValue)
size=[@convertPixelsToEmus(sizePixel[0]),@convertPixelsToEmus(sizePixel[1])]
if @options.centered==false
......@@ -72,10 +70,10 @@ class ImageModule
xmlTemplater=@manager.getInstance('xmlTemplater')
imR=new ImgReplacer(xmlTemplater,@imgManager)
imR.getDataFromString=(result,cb)=>
if @getImageFromDataAsync?
@getImageFromDataAsync(result,cb)
if @getImageAsync?
@getImageAsync(result,cb)
else
cb(null,@getImageFromData(result))
cb(null,@getImage(result))
imR.pushQrQueue=(num)=>
@qrQueue.push(num)
imR.popQrQueue=(num)=>
......
......@@ -12,6 +12,15 @@ fileNames=[
'qrExample2.docx',
]
opts={}
beforeEach ()->
opts={}
opts.getImage=(tagValue)->
fs.readFileSync(tagValue,'binary')
opts.getSize=(imgBuffer,tagValue)->
[150,150]
opts.centered = false
ImageModule=require('../js/index.js')
docX={}
......@@ -33,7 +42,7 @@ for name in fileNames
describe 'image adding with {% image} syntax', ()->
it 'should work with one image',()->
name='imageExample.docx'
imageModule=new ImageModule({centered:false})
imageModule=new ImageModule(opts)
docX[name].attachModule(imageModule)
out=docX[name]
.load(docX[name].loadedContent)
......@@ -61,7 +70,8 @@ describe 'image adding with {% image} syntax', ()->
it 'should work with centering',()->
d=new DocxGen()
name='imageExample.docx'
imageModule=new ImageModule({centered:true})
opts.centered = true
imageModule=new ImageModule(opts)
d.attachModule(imageModule)
out=d
.load(docX[name].loadedContent)
......@@ -92,7 +102,8 @@ describe 'image adding with {% image} syntax', ()->
it 'should work with loops',()->
name='imageLoopExample.docx'
imageModule=new ImageModule({centered:true})
opts.centered = true
imageModule=new ImageModule(opts)
docX[name].attachModule(imageModule)
out=docX[name]
......@@ -124,10 +135,10 @@ describe 'image adding with {% image} syntax', ()->
buffer=zip.generate({type:"nodebuffer"})
fs.writeFile("test_multi.docx",buffer);
it 'should work with image in header/footer',()->
name='imageHeaderFooterExample.docx'
imageModule=new ImageModule({centered:false})
imageModule=new ImageModule(opts)
docX[name].attachModule(imageModule)
out=docX[name]
.load(docX[name].loadedContent)
......@@ -176,10 +187,10 @@ describe 'qrcode replacing',->
zip=null
before (done)->
name='qrExample.docx'
opts.qrCode = true
imageModule=new ImageModule(opts)
imageModule=new ImageModule({qrCode:true})
imageModule.getImageFromData=(imgData)->
imageModule.getImage=(imgData)->
d=fs.readFileSync('examples/'+imgData,'binary')
d
......@@ -211,9 +222,10 @@ describe 'qrcode replacing',->
before (done)->
name='qrExample2.docx'
imageModule=new ImageModule({qrCode:true})
opts.qrCode = true
imageModule=new ImageModule(opts)
imageModule.getImageFromData=(imgData)->
imageModule.getImage=(imgData)->
d=fs.readFileSync('examples/'+imgData,'binary')
d
......@@ -246,7 +258,8 @@ describe 'qrcode replacing',->
before (done)->
d=new DocxGen()
name='noImage.docx'
imageModule=new ImageModule({qrCode:true})
opts.qrCode = true
imageModule=new ImageModule(opts)
imageModule.finished=()->
zip=d.getZip()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment