123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304 |
- o2.xApplication.process.Xform.widget = o2.xApplication.process.Xform.widget || {};
- o2.xApplication.process.Xform.widget.OOXML = o2.xApplication.process.Xform.widget.OOXML || {};
- o2.OOXML = o2.OOXML || {};
- o2.xApplication.process.Xform.widget.OOXML.WordprocessingML = o2.OOXML.WML = new Class({
- Implements: [Options, Events],
- options: {
- "AppVersion": "16.0000",
- "Application": "o2oa",
- "Company": "",
- "ScaleCrop": "false",
- "LinksUpToDate": "false",
- "SharedDoc": "false",
- "HyperlinksChanged": "false",
- "w_document": "<w:document xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"\n" +
- " xmlns:wpc=\"http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas\"\n" +
- " xmlns:cx=\"http://schemas.microsoft.com/office/drawing/2014/chartex\"\n" +
- " xmlns:cx1=\"http://schemas.microsoft.com/office/drawing/2015/9/8/chartex\"\n" +
- " xmlns:cx2=\"http://schemas.microsoft.com/office/drawing/2015/10/21/chartex\"\n" +
- " xmlns:cx3=\"http://schemas.microsoft.com/office/drawing/2016/5/9/chartex\"\n" +
- " xmlns:cx4=\"http://schemas.microsoft.com/office/drawing/2016/5/10/chartex\"\n" +
- " xmlns:cx5=\"http://schemas.microsoft.com/office/drawing/2016/5/11/chartex\"\n" +
- " xmlns:cx6=\"http://schemas.microsoft.com/office/drawing/2016/5/12/chartex\"\n" +
- " xmlns:cx7=\"http://schemas.microsoft.com/office/drawing/2016/5/13/chartex\"\n" +
- " xmlns:cx8=\"http://schemas.microsoft.com/office/drawing/2016/5/14/chartex\"\n" +
- " xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\"\n" +
- " xmlns:aink=\"http://schemas.microsoft.com/office/drawing/2016/ink\"\n" +
- " xmlns:am3d=\"http://schemas.microsoft.com/office/drawing/2017/model3d\"\n" +
- " xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n" +
- " xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"\n" +
- " xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" xmlns:v=\"urn:schemas-microsoft-com:vml\"\n" +
- " xmlns:wp14=\"http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing\"\n" +
- " xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\"\n" +
- " xmlns:w10=\"urn:schemas-microsoft-com:office:word\"\n" +
- " xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\"\n" +
- " xmlns:w15=\"http://schemas.microsoft.com/office/word/2012/wordml\"\n" +
- " xmlns:w16cex=\"http://schemas.microsoft.com/office/word/2018/wordml/cex\"\n" +
- " xmlns:w16cid=\"http://schemas.microsoft.com/office/word/2016/wordml/cid\"\n" +
- " xmlns:w16=\"http://schemas.microsoft.com/office/word/2018/wordml\"\n" +
- " xmlns:w16sdtdh=\"http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash\"\n" +
- " xmlns:w16se=\"http://schemas.microsoft.com/office/word/2015/wordml/symex\"\n" +
- " xmlns:wpg=\"http://schemas.microsoft.com/office/word/2010/wordprocessingGroup\"\n" +
- " xmlns:wpi=\"http://schemas.microsoft.com/office/word/2010/wordprocessingInk\"\n" +
- " xmlns:wne=\"http://schemas.microsoft.com/office/word/2006/wordml\"\n" +
- " xmlns:wps=\"http://schemas.microsoft.com/office/word/2010/wordprocessingShape\"\n" +
- " xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\"\n" +
- " mc:Ignorable=\"w14 w15 w16se w16cid w16 w16cex w16sdtdh wp14\">",
- "w_setting": "<w:settings xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\"\n" +
- " xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n" +
- " xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"\n" +
- " xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" xmlns:v=\"urn:schemas-microsoft-com:vml\"\n" +
- " xmlns:w10=\"urn:schemas-microsoft-com:office:word\"\n" +
- " xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"\n" +
- " xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\"\n" +
- " xmlns:w15=\"http://schemas.microsoft.com/office/word/2012/wordml\"\n" +
- " xmlns:w16cex=\"http://schemas.microsoft.com/office/word/2018/wordml/cex\"\n" +
- " xmlns:w16cid=\"http://schemas.microsoft.com/office/word/2016/wordml/cid\"\n" +
- " xmlns:w16=\"http://schemas.microsoft.com/office/word/2018/wordml\"\n" +
- " xmlns:w16sdtdh=\"http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash\"\n" +
- " xmlns:w16se=\"http://schemas.microsoft.com/office/word/2015/wordml/symex\"\n" +
- " xmlns:sl=\"http://schemas.openxmlformats.org/schemaLibrary/2006/main\"\n" +
- " mc:Ignorable=\"w14 w15 w16se w16cid w16 w16cex w16sdtdh\">",
- "xmlHead": "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>",
- "divAsP": false,
- "protection": false,
- "firstPageNumber": true,
- "pageHeight": "841.9",
- "pageWidth": "595.3",
- "page-margin-left": "79.4",
- "page-margin-right": "73.7",
- "page-margin-top": "104.9",
- "page-margin-bottom": "99.25"
- },
- initialize: function(options){
- this.setOptions(options);
- this.path = "../x_component_process_Xform/widget/$OOXML/WordprocessingML/";
- this.dpi = this.getDPI();
- this.rid = 11;
- },
- getDPI: function(){
- var div = new Element("div", {"styles": {"width": "1in", "height": "1in"}}).inject(document.body);
- var dpi = div.offsetWidth.toInt();
- div.destroy();
- return dpi;
- },
- getZipTemplate: function(){
- //var zipFileName = (this.options.firstPageNumber) ? "template.zip" : "template.zip";
- var zipFileName = "template.zip";
- return fetch(this.path+zipFileName).then(function(res){
- return res.blob().then(JSZip.loadAsync);
- });
- },
- load: function(data){
- return new Promise(function(resolve){
- o2.load(["../o2_lib/jszip/jszip.min.js", "../o2_lib/jszip/FileSaver.js"], function(){
- //this.getZipTemplate();
- this.getZipTemplate().then(function(zip){
- //console.log(zip.files);
- this.zip = zip;
- return this.processDocument(data);
- }.bind(this)).then(function(oo_content){
- var word = new Blob( [oo_content], {type : "application/vnd.openxmlformats-officedocument.wordprocessingml.document"} );
- //oo_content.type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
- resolve(word);
- });
- }.bind(this));
- }.bind(this));
- },
- processDocument: function(data){
- var promise = [];
- var p1 = this.zip.file("word/document.xml").async("text").then(function(oo_string){
- return this.processWordDocument(oo_string, data);
- }.bind(this)).then(function(oo_str){
- if (oo_str.substring(0, 5)!=="<?xml"){
- oo_str = oo_str.replace(/<w:document.*\>/, this.options.w_document);
- oo_str = this.options.xmlHead + oo_str;
- }
- this.zip.file("word/document.xml", oo_str);
- if (this.pics && this.pics.length){
- return this.zip.file("word/_rels/document.xml.rels").async("text").then(function(oo_relString){
- return this.processWordRel(oo_relString);
- }.bind(this)).then(function(oo_relStr){
- //return oo_relStrPromise.then(function(oo_relStr){
- if (oo_relStr.substring(0, 5)!=="<?xml"){
- oo_relStr = this.options.xmlHead + oo_relStr;
- }
- //return this.zip.file("word/_rels/document.xml.rels", oo_relStr).generateAsync({type:"blob"});
- return this.zip.file("word/_rels/document.xml.rels", oo_relStr);
- //}.bind(this));
- }.bind(this));
- }
- return this.zip;
- // this.zip.file("word/document.xml", oo_str).generateAsync({type:"blob"}).then(function(oo_content) {
- // this.saveAs(oo_content, "example.docx");
- // }.bind(this));
- }.bind(this));
- promise.push(p1);
- if (this.options.protection){
- var p2 = this.zip.file("word/settings.xml").async("text").then(function(oo_string){
- return this.processWordSetting(oo_string);
- }.bind(this)).then(function(oo_settingStr){
- if (oo_settingStr.substring(0, 5)!=="<?xml"){
- oo_settingStr = oo_settingStr.replace(/<w:settings.*\>/, this.options.w_setting);
- oo_settingStr = this.options.xmlHead + oo_settingStr;
- }
- return this.zip.file("word/settings.xml", oo_settingStr);
- }.bind(this));
- promise.push(p2);
- }
- return Promise.all(promise).then(function(){
- return this.zip.generateAsync({type:"blob"});
- }.bind(this));
- },
- processWordSetting: function(oo_string){
- var domparser = new DOMParser();
- var oo_doc = domparser.parseFromString(oo_string, "text/xml");
- var oo_settings = oo_doc.documentElement;
- return new Promise(function(resolve){
- o2.require("o2.widget.Base64", function(){
- o2.load(["../o2_lib/CryptoJS/tripledes.js", "../o2_lib/CryptoJS/components/aes.js"], function () {
- var keyStr = (new o2.widget.UUID()).toString();
- var key = CryptoJS.enc.Utf8.parse(keyStr.substr(0, 16));
- var iv = CryptoJS.enc.Utf8.parse(keyStr.substr(0, 16));
- var ps = new o2.widget.UUID().toString();
- //var srcs = CryptoJS.enc.Utf8.parse(ps);
- encrypted = CryptoJS.AES.encrypt(ps, key, {
- iv: iv,
- mode: CryptoJS.mode.CBC,
- padding: CryptoJS.pad.Pkcs7
- });
- var hash = o2.widget.Base64.encode(encrypted.ciphertext.toString());
- var salt = o2.widget.Base64.encode(keyStr);
- var oo_protection = this.createEl(oo_doc, "documentProtection");
- this.setAttrs(oo_protection, {
- "salt": salt,
- "hash": hash,
- "cryptSpinCount": "100000",
- "cryptAlgorithmSid": "4",
- "cryptAlgorithmType": "typeAny",
- "cryptAlgorithmClass": "hash",
- "cryptProviderType": "rsaAES",
- "enforcement": "1",
- "formatting": "1",
- "edit": "readOnly"
- });
- oo_settings.appendChild(oo_protection);
- var s = new XMLSerializer();
- resolve(s.serializeToString(oo_doc));
- }.bind(this));
- }.bind(this));
- }.bind(this));
- },
- saveAs: function(content, name){
- o2.saveAs(content, name);
- },
- getPageRule: function(cssRules){
- var pageRule = null;
- if (cssRules){
- for (var i=0; i<cssRules.length; i++){
- if (cssRules[i].type===CSSRule.PAGE_RULE){
- pageRule = cssRules[i];
- break;
- }
- }
- }
- return pageRule;
- },
- getPicExt: function(contentType){
- switch (contentType.toLowerCase()){
- case "image/tiff": return "tif";
- case "image/gif": return "gif";
- case "image/jpeg": return "jpg";
- case "image/png": return "png";
- }
- return "";
- },
- processWordRel_createRel: function(name, idx, oo_doc, oo_relationships){
- var oo_relationship = this.createEl(oo_doc, "Relationship", "rel");
- this.setAttrs(oo_relationship, {"Target": "media/"+name, "Type": "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", "Id": "rId"+idx}, false);
- oo_relationships.appendChild(oo_relationship);
- },
- processWordRel_getPicByUrl: function(pic, idx, oo_doc, oo_relationships){
- var name = "image"+idx;
- var headers = {
- "Authorization": layout.session.user.token
- };
- headers[o2.tokenName] = layout.session.user.token;
- return fetch(pic,{
- credentials: 'include', // include, same-origin, *omit
- headers: headers,
- mode: 'cors', // no-cors, cors, *same-origin
- }).then(function(res){
- var ext = this.getPicExt(res.headers.get("content-type"));
- var fullName = name+"."+ext;
- return res.blob().then(function(d){
- this.zip.file("word/media/"+fullName, d);
- this.processWordRel_createRel(fullName, idx, oo_doc, oo_relationships);
- }.bind(this));
- }.bind(this))
- },
- processWordRel_getPicByBase64: function(pic, idx, oo_doc, oo_relationships){
- var name = "image"+idx;
- var arr = pic.split(','), contentType = arr[0].match(/:(.*?);/)[1];
- var data = arr[1];
- var ext = this.getPicExt(contentType);
- var fullName = name+"."+ext;
- this.zip.file("word/media/"+fullName, data, {"base64": true});
- this.processWordRel_createRel(fullName, idx, oo_doc, oo_relationships);
- return Promise.resolve();
- },
- processWordRel: function(oo_string, data){
- var domparser = new DOMParser();
- var oo_doc = domparser.parseFromString(oo_string, "text/xml");
- var oo_relationships = oo_doc.documentElement;
- // var oo_relationshipList = oo_relationships.querySelectorAll("relationship");
- // var idx = oo_relationshipList.length+1;
- var promises = [];
- var idx = 11;
- this.pics.each(function(pic){
- if (pic.substring(0, 4).toLowerCase()==="data"){
- promises.push( this.processWordRel_getPicByBase64(pic, idx, oo_doc, oo_relationships));
- }else{
- promises.push( this.processWordRel_getPicByUrl(pic, idx, oo_doc, oo_relationships));
- }
- idx++;
- // this.zip.file("word/media/"+pic.name, new Blob(pic.data.data));
- //
- // var oo_relationship = this.createEl(oo_doc, "Relationship", false);
- // this.setAttrs(oo_relationship, {"Target": "media/"+pic.name, "Type": "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", "Id": "rId"+idx}, false);
- // oo_relationships.appendChild(oo_relationship);
- //
- // idx++;
- // promises.push(
- // res.blob().then(function(d){
- //
- // }.bind(this), function(){idx++;})
- // );
- }.bind(this));
- return Promise.all(promises).then(function(){
- var s = new XMLSerializer();
- return s.serializeToString(oo_doc);
- });
- },
- processWordDocument: function(oo_string, data){
- //wgxpath.install();
- var domparser = new DOMParser();
- var oo_doc = domparser.parseFromString(oo_string, "text/xml");
- //var oo_body = oo_doc.evaluate("//w:document/w:body", oo_doc, this.nsResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
- // var oo_document = oo_doc.documentElement;
- // var keys = Object.keys(this.options.xmlns);
- // keys.forEach(function(k){
- // oo_document.setAttribute(k, this.options.xmlns[k]);
- // }.bind(this));
- var oo_body = oo_doc.documentElement.querySelector("body");
- // var dom_div;
- // if (o2.typeOf(data) === "string"){
- var dom_div = new Element("div", {"styles": {
- "display": "block",
- "width": "442.2pt",
- "padding": "104.9pt 73.7pt 99.25pt 79.4pt",
- }}).set("html", data).inject(document.body);
- // }else{
- // dom_div = data;
- // }
- return new Promise(function(resolve){
- var imgs = dom_div.getElements("img");
- var promises = [];
- imgs.each(function(img){
- if (!img.complete){
- var p = new Promise(function(r){
- img.addEvent("load", function(){
- r();
- }.bind(this))
- });
- promises.push(p);
- }
- }.bind(this));
- Promise.all(promises).then(function(){
- var style= dom_div.getElement("style");
- if (style){
- var dom_pageRule = this.getPageRule(dom_div.getElement("style").sheet.cssRules);
- if (dom_pageRule) this.processPageSection(dom_pageRule, oo_body);
- }
- var dom_wordSection = dom_div.getElement(".WordSection1");
- if (dom_wordSection){
- this.processPageSection(dom_wordSection, oo_body);
- this.processDom(dom_wordSection, oo_body);
- }
- dom_div.destroy();
- var s = new XMLSerializer();
- resolve(s.serializeToString(oo_doc));
- }.bind(this));
- }.bind(this));
- // var style= dom_div.getElement("style");
- // if (style){
- // var dom_pageRule = this.getPageRule(dom_div.getElement("style").sheet.cssRules);
- // if (dom_pageRule) this.processPageSection(dom_pageRule, oo_body);
- // }
- // var dom_wordSection = dom_div.getElement(".WordSection1");
- // if (dom_wordSection){
- // this.processPageSection(dom_wordSection, oo_body);
- // this.processDom(dom_wordSection, oo_body);
- // }
- //
- //
- // dom_div.destroy();
- // var s = new XMLSerializer();
- // return s.serializeToString(oo_doc);
- },
- processDom: function(dom, oo_body, append, divAsP){
- dom = dom.getFirst();
- while (dom){
- if (dom.getStyle("display")!=="none"){
- if (dom.hasClass("doc_layout_redHeader")){
- var node = dom.firstChild;
- while (node){
- if (node.nodeType===Node.TEXT_NODE){
- var text = node.nodeValue.replace(/(?=\s|\n|\r|\t)[^\u3000]/g, '');
- if (text){
- var oo_p = this.createParagraphFromDom(dom, oo_body, append);
- this.processRun(dom, oo_p, dom, text);
- }
- }else{
- this.processParagraph(node, oo_body, append);
- //this.processDom(node, oo_body, append, true);
- }
- node = node.nextSibling;
- }
- // if (node && node.nodeType===Node.TEXT_NODE && node.nodeValue.trim()){
- // var oo_p = this.createParagraphFromDom(dom, oo_body, append);
- // this.processRun(dom, oo_p, dom, node.nodeValue);
- // }
- //
- // this.processParagraph(dom, oo_body, append);
- }else if (dom.hasClass("doc_layout_filetext")){
- this.processFiletext(dom, oo_body, append);
- }else if (dom.hasClass("doc_layout_attachment_text")){
- this.processFiletext(dom, oo_body, append);
- }else if (dom.hasClass("doc_layout_editionArea")){
- var wordSection = dom.getParent(".WordSection1");
- var h = this.pxToPt(dom.getSize().y);
- var top = this.pxToPt(dom.getPosition(wordSection).y);
- var pageH = this.pageHeight/20-this["page-margin-top"]/20-this["page-margin-bottom"]/20;
- var offsetY = 0;
- if (wordSection) {
- var pageBreakNodes = wordSection.querySelectorAll(".cke_pagebreak");
- pageBreakNodes.forEach(function(node){
- var t = this.pxToPt(node.getPosition(wordSection).y);
- offsetY += pageH-(t % pageH);
- }.bind(this));
- }
- top += offsetY;
- var tmpp = new Element("p").set('class', 'MsoNormal').set('html', ' ').inject(dom, "top");
- var lineHeight = tmpp.getSize().y;
- tmpp.destroy();
- var tmp = (top+h)/pageH;
- var tmp2 = (top+lineHeight)/pageH;
- var ps = tmp.toInt();
- var ps2 = tmp2.toInt();
- if (tmp>ps) ps = ps+1;
- if (tmp2>ps2) ps2 = ps2+1;
- if ((ps % 2)!=0){
- // var p = new Element("p", {"styles": {"page-break-after":"always"}}).inject(dom, "top");
- // if ((ps2 % 2)==0){new Element("p", {"styles": {"page-break-after":"always"}}).inject(dom, "top");}
- var p = new Element("p").set('class', 'MsoNormal').set('html', ' ').setStyles({"page-break-after":"always"}).inject(dom, "top");
- var lineHeight = p.getSize().y;
- new Element("p").set("html", "<br>").inject(dom, "top");
- if (ps2<ps) {
- if ((ps2 - tmp2) * pageH > this.pxToPt(lineHeight)) {
- new Element("p").set('class', 'MsoNormal').setStyles({"page-break-after": "always"}).inject(dom, "top");
- }
- }
- }
- this.processDom(dom, oo_body, append, divAsP);
- }else if (dom.tagName.toLowerCase() === "p" || ((!!divAsP || !!this.options.divAsP) && dom.tagName.toLowerCase() === "div")){
- this.processParagraph(dom, oo_body, append);
- // }else if (dom.tagName.toLowerCase() === "span") {
- // this.processRun(dom, oo_body, append);
- }else if (dom.tagName.toLowerCase() === "hr") {
- this.processHr(dom, oo_body, append);
- }else if (dom.tagName.toLowerCase() === "img") {
- var oo_p = this.createParagraphFromDom(dom, oo_body, append);
- this.processPic(dom, oo_p, append);
- }else if (dom.tagName.toLowerCase() === "table") {
- this.processTable(dom, oo_body, append);
- }else if (dom.tagName.toLowerCase() === "span") {
- if (!oo_body || oo_body.tagName.toString().toLowerCase()!=="w:p") var oo_body = this.createParagraphFromDom(dom, oo_body, append);
- this.processRun(dom, oo_body);
- }else{
- this.processDom(dom, oo_body, append, divAsP);
- }
- }
- dom = dom.getNext();
- }
- },
- // processFiletextParagraphRun: function(node, oo_p, p){
- // node = node.firstChild;
- // while (node){
- // if (node.nodeType===Node.TEXT_NODE){
- // if (node.nodeValue.trim()) this.processRun(node.parentElement, oo_p, p);
- // }else if (node.nodeType===Node.ELEMENT_NODE){
- // if (node.tagName.toLowerCase() === "span") {
- // this.processRun(node, oo_p, p);
- // }else if (node.tagName.toLowerCase() === "table") {
- // this.processTable(node, oo_p, p);
- // }else{
- // this.processFiletextParagraphRun(node, oo_p, p);
- // }
- // }else{
- // this.processFiletextParagraphRun(node, oo_p, p);
- // }
- // node = node.nextSibling;
- // }
- // },
- processFiletext: function(dom, oo_body, append){
- var node = dom.firstChild;
- if (node && node.nodeType===Node.TEXT_NODE){
- var text = node.nodeValue.replace(/(?=\s|\n|\r|\t)[^\u3000]/g, '');
- if (text){
- var oo_p = this.createParagraphFromDom(dom, oo_body, append);
- this.processRun(dom, oo_p, dom, text);
- }
- }
- node = dom.getFirst();
- while (node){
- if (node.tagName.toLowerCase() === "div" || node.tagName.toLowerCase() === "p") {
- this.processParagraph(node, oo_body, append);
- }else if (node.tagName.toLowerCase() === "img") {
- var oo_p = this.createParagraphFromDom(node, oo_body, append);
- this.processPic(node, oo_p, append);
- }else if (node.tagName.toLowerCase() === "table") {
- this.processTable(node, oo_body, append);
- }
- node = node.getNext();
- }
- },
- // processFiletextParagraph: function(){
- //
- // },
- isEmptyP: function(p){
- var oo_t = p.querySelector("t");
- var oo_drawing = p.querySelector("drawing");
- var oo_br = p.querySelector("br");
- return !oo_t && !oo_drawing && !oo_br;
- },
- processParagraphRun: function(node, oo_p, p, oo_body, append, ilvl){
- node = node.firstChild;
- while (node){
- if (node.nodeType===Node.TEXT_NODE){
- var text = node.nodeValue.replace(/(?=\s|\n|\r|\t)[^\u3000]/g, '');
- if (text) this.processRun(node.parentElement || node.parentNode, oo_p, p, text);
- }else if (node.nodeType===Node.ELEMENT_NODE){
- if (node.getStyle("display") === "none") {
- continue;
- }else if (node.tagName.toLowerCase() === "span") {
- this.processRun(node, oo_p, p);
- }else if (node.tagName.toLowerCase() === "br") {
- // if (node.nextSibling)
- if (node.nextSibling || (!node.nextSibling && !node.previousSibling)){
- this.processRun(node, oo_p, p, "", "br");
- }
- }else if (node.tagName.toLowerCase() === "div" || node.tagName.toLowerCase() === "p") {
- if (!this.isEmptyP(oo_p)){
- oo_p = this.createParagraphFromDom(node, oo_body, append);
- }else{
- this.setParagraphAttrFromDom(node, oo_p);
- }
- this.processParagraphRun(node, oo_p, p, oo_body, append, ilvl);
- }else if (node.tagName.toLowerCase() === "ul" || node.tagName.toLowerCase() === "ol") {
- this.processNumbering(node, oo_p, p, oo_body, append, ilvl);
- }else if (node.tagName.toLowerCase() === "table") {
- this.processTable(node, oo_body);
- if (this.isEmptyP(oo_p)) oo_p.destroy();
- oo_p = this.createParagraphFromDom(p, oo_body, append);
- }else if (node.tagName.toLowerCase() === "img") {
- this.processPic(node, oo_p, append);
- if (this.isEmptyP(oo_p)) oo_p.destroy();
- oo_p = this.createParagraphFromDom(p, oo_body, append);
- }else{
- this.processParagraphRun(node, oo_p, p, oo_body, append, ilvl);
- }
- }else{
- this.processParagraphRun(node, oo_p, p, oo_body, append, ilvl);
- }
- node = node.nextSibling;
- }
- },
- processNumbering: function(node, oo_p, p, oo_body, append, ilvl){
- ilvl = ilvl || 0;
- var nextIlvl = ilvl + 1;
- var numId = (node.tagName.toLowerCase() === "ul") ? "1" : "2";
- var lis = node.getChildren("li");
- for (var i=0; i<lis.length; i++){
- var li = lis[i];
- if (!this.isEmptyP(oo_p)) oo_p = this.createParagraphFromDom(li, oo_body, append);
- var oo_pPr = oo_p.querySelector("pPr");
- if (!oo_pPr){
- oo_pPr = this.createEl(oo_body.ownerDocument, "pPr");
- oo_p.appendChild(oo_pPr);
- }
- oo_numPr = this.createEl(oo_body.ownerDocument, "numPr");
- oo_ilvl = this.createEl(oo_body.ownerDocument, "ilvl");
- this.setAttrs(oo_ilvl, {"val": ilvl});
- oo_numId = this.createEl(oo_body.ownerDocument, "numId");
- this.setAttrs(oo_numId, {"val": numId});
- oo_numPr.appendChild(oo_ilvl);
- oo_numPr.appendChild(oo_numId);
- oo_pPr.appendChild(oo_numPr);
- this.processParagraphRun(li, oo_p, li, oo_body, append, nextIlvl);
- }
- },
- getPPrs: function(dom){
- var pPrs = {};
- var align = dom.getStyle("text-align");
- if (align){
- var jc = "start"
- switch (align){
- case "center": jc = "center"; break;
- case "right":
- case "end": jc = "end"; break;
- case "justify": jc = "both"; break;
- }
- pPrs.jc = {"val": jc};
- }
- var left = dom.getStyle("margin-left");
- if (left && left.toFloat()){
- var left = this.pxToPt(left)*20;
- if (left) {
- if (!pPrs.ind) pPrs.ind = {};
- pPrs.ind.left = left;
- }
- }
- var right = dom.getStyle("margin-left");
- if (right && right.toFloat()){
- var right = this.pxToPt(right)*20;
- if (right) {
- if (!pPrs.ind) pPrs.ind = {};
- pPrs.ind.right = right;
- }
- }
- var indent = dom.getStyle("text-indent");
- if (indent && indent.toFloat()){
- var indent = this.pxToPt(indent)*20;
- if (indent) {
- if (!pPrs.ind) pPrs.ind = {};
- if (indent>0){
- pPrs.ind.firstLine = indent;
- }else{
- pPrs.ind.hanging = Math.abs(indent);
- }
- }
- }
- var line = (dom.currentStyle) ? dom.currentStyle["line-height"] : dom.getStyle("line-height");
- //var line = dom.getStyle("line-height");
- var msoStyle = this.getMsoStyle(dom);
- var lineRule = msoStyle["mso-line-rule"] || "exact";
- if (line && parseFloat(line)){
- var line = this.pxToPt(line)*20;
- if (line) {
- pPrs.spacing = {
- lineRule: lineRule,
- line: line
- };
- }
- }
- // if (line && line.toFloat()){
- // var line = this.pxToPt(line)*20;
- // if (line) {
- // pPrs.spacing = {
- // lineRule: "exact",
- // line: line
- // };
- // }
- // }
- var pageBreak = dom.getStyle("page-break-after");
- if (pageBreak && pageBreak.toString().toLowerCase()=="avoid"){
- pPrs.keepNext = {};
- }
- if (pageBreak && pageBreak.toString().toLowerCase()=="always"){
- pPrs.pageBreak = {};
- }
- return pPrs;
- },
- setParagraphAttrFromDom: function(dom, oo_p){
- var pPrs = this.getPPrs(dom);
- var oo_pPr = oo_p.querySelector("pPr");
- if (!oo_pPr){
- oo_pPr = this.createEl(oo_p.ownerDocument, "pPr");
- oo_p.appendChild(oo_pPr);
- }
- Object.keys(pPrs).each(function(k){
- if (k=="pageBreak") {
- var oo_r = this.createEl(oo_p.ownerDocument, "r");
- var oo_br = this.createEl(oo_p.ownerDocument, "br");
- this.setAttrs(oo_br, {"type": "page"});
- oo_r.appendChild(oo_br);
- oo_p.appendChild(oo_r);
- }else{
- var node = oo_pPr.querySelector(k);
- if (!node) node = this.createEl(oo_p.ownerDocument, k);
- this.setAttrs(node, pPrs[k]);
- oo_pPr.appendChild(node);
- }
- }.bind(this));
- },
- createParagraphFromDom: function(dom, oo_body, append){
- var oo_p = this.createParagraph(oo_body.ownerDocument, {"pPrs": this.getPPrs(dom)});
- if (append){
- oo_body.appendChild(oo_p);
- }else{
- var oo_sectPr = this.getEl(oo_body, "sectPr");
- if (oo_sectPr){
- this.insertSiblings(oo_sectPr, [oo_p], "beforebegin");
- }else{
- this.insertChildren(oo_body, [oo_p]);
- }
- }
- return oo_p;
- },
- processParagraph: function(dom, oo_body, append){
- var oo_p = this.createParagraphFromDom(dom, oo_body, append);
- this.processParagraphRun(dom, oo_p, dom, oo_body, append);
- if (this.isEmptyP(oo_p)) oo_p.destroy();
- return oo_p;
- },
- getTableTblW: function(table){
- var type = "dxa";
- //var w = table.clientWidth;
- // var w;
- // var tag = table.tagName.toString().toLowerCase();
- // if (tag==="td" || tag==="th"){
- // w = table.clientWidth;
- // w = this.pxToPt(w);
- // }else{
- // w = table.style.width;
- // if (!w) w = table.style.width;
- // if (!w) w = table.get("width");
- // }
- var w = table.style.width;
- //if (!w) w = table.style.width;
- if (!w){
- w = table.get("width");
- //if (w) w = this.pxToPt(w);
- }
- //if (w) w = this.pxToPt(w);
- if (w && o2.typeOf(w)==="string"){
- var u = w.substring(w.length-1, w.length);
- if (u==="%"){
- w = w.toFloat()*50;
- type = "pct";
- }else{
- u = w.substring(w.length-2, w.length);
- if (u.toLowerCase()!=="pt"){
- w = this.pxToPt(w);
- }
- }
- }
- if (!w || !w.toFloat()){
- w = 0;
- type = "auto";
- }else{
- //w = w.toFloat();
- if (type === "dxa") w = w.toFloat()*20;
- }
- return {"w": w, "type": type};
- },
- getTableBorder: function(table, where){
- var attr = {
- "space": "0",
- "val": "single",
- "color": "auto",
- "sz": "0"
- }
- // var border = (table.currentStyle) ? table.currentStyle[("border-"+where+"-style").camelCase()] : table.getStyle("border-"+where);
- // if (border==="none") border = table.get("border");
- // if (!border || border==="none"){
- // attr.val = "none";
- // }else{
- var sz;
- var border = (table.currentStyle) ? table.currentStyle[("border-"+where+"-style").camelCase()] : table.getStyle("border-"+where);
- if (!border || border==="none"){
- sz = table.get("border");
- if (!sz || sz==="none") sz = table.getStyle("border-"+where+"-width");
- }else{
- sz = (table.currentStyle) ? table.currentStyle[("border-"+where+"-width").camelCase()] : table.getStyle("border-"+where+"-width");
- if (!sz || !sz.toFloat()) sz = table.get("border");
- }
- if (sz && o2.typeOf(sz)==="string"){
- u = sz.substring(sz.length-2, sz.length);
- if (u.toLowerCase()!=="pt"){
- sz = this.pxToPt(sz);
- }
- }
- if (!sz || !sz.toFloat()) sz = 0;
- attr.sz = sz.toFloat()*8;
- if (Browser.name=="firefox") attr.sz = attr.sz*1.25; //firefox边框计算问题
- var color = this.getColorHex(((table.currentStyle) ? table.currentStyle[("border-"+where+"-color").camelCase()] : table.getStyle("border-"+where+"-color")));
- if (!color) color = "auto";
- attr.color = color;
- var style = (table.currentStyle) ? table.currentStyle[("border-"+where+"-style").camelCase()] : table.getStyle("border-"+where+"-style");
- switch (style){
- case "dashed": case "dotted": case "double": attr.val = "double"; break;
- default: attr.val = "single";
- }
- if (attr.sz===0) attr.val="none";
- // }
- // var sz = table.get("border");
- // if (sz) sz = this.pxToPt(sz);
- // if (!sz) sz = table.getStyle("border-"+where+"-width");
- // if (!sz || !sz.toFloat()) sz = 0;
- // attr.sz = sz.toFloat()*20;
- var space = table.get("cellspacing");
- if (space) attr.space = this.pxToPt(space);
- return attr;
- },
- getTableTblGrid: function(table){
- var grids = [];
- var trs = table.rows;
- for (var i = 0; i < trs.length; i++){
- var idx = 0;
- tds = trs[i].cells;
- for (var j=0; j<tds.length; j++){
- var td = tds[j];
- var colspan = td.get("colspan");
- if (!colspan || colspan.toInt()===1) {
- while (grids.length<=idx) grids.push(0);
- var pt = this.pxToPt(td.clientWidth);
- if (pt>grids[idx]) grids[idx] = pt;
- }else{
- var addTd = colspan.toInt()-1;
- var tempTds = [];
- for (var n=0; n<addTd; n++) tempTds.push(new Element("td").inject(td, "after"));
- while (grids.length<=idx) grids.push(0);
- var pt = this.pxToPt(td.clientWidth);
- if (pt>grids[idx]) grids[idx] = pt;
- tempTds.each(function(tmpTd){
- idx++;
- while (grids.length<=idx) grids.push(0);
- var pt = this.pxToPt(tmpTd.clientWidth);
- if (pt>grids[idx]) grids[idx] = pt;
- }.bind(this));
- tempTds.each(function(tmpTd){
- tmpTd.destroy();
- });
- }
- idx++;
- }
- }
- return grids;
- },
- getTdValign: function(td){
- var v = "";
- var valign = td.getStyle("vertical-align") || td.get("valign");
- if (valign) {
- switch (valign){
- case "bottom": v = "bottom"; break;
- case "top": v = "top"; break;
- default: v = "center";
- }
- }
- return v;
- },
- getMsoStyle: function(dom){
- var o = {};
- var s = dom.getAttribute("style");
- if (s){
- var sList = s.split(/\s*;\s*/g);
- sList.map(function(style){
- var styles = style.split(/\s*:\s*/g);
- if (styles.length===2){
- if (styles[0].substr(0,3).toLowerCase()==="mso"){
- o[styles[0]] = styles[1];
- }
- }
- return false;
- });
- }
- var d = dom.dataset;
- if (d){
- Object.keys(d).forEach(function(k){
- if (k.substr(0,3).toLowerCase()==="mso"){
- o[k.hyphenate()] = d[k];
- }
- });
- }
- return o;
- },
- processTableDom: function(dom, oo_body, append, divAsP, oo_tc){
- dom = dom.firstChild;
- while (dom){
- if (dom.nodeType===Node.ELEMENT_NODE){
- if (dom.getStyle("display") === "none") {
- continue;
- }else if (dom.hasClass("doc_layout_filetext")){
- this.processFiletext(dom, oo_body, append);
- }else if (dom.tagName.toLowerCase() === "p" || ((!!divAsP || !!this.options.divAsP) && dom.tagName.toLowerCase() === "div")){
- this.processParagraph(dom, oo_body, append);
- // }else if (dom.tagName.toLowerCase() === "span") {
- // this.processRun(dom, oo_body, append);
- }else if (dom.tagName.toLowerCase() === "br") {
- if (!oo_body || oo_body.tagName.toString().toLowerCase()!=="w:p") var oo_body = this.createParagraphFromDom(dom, oo_body, dom.parentElement);
- this.processRun(dom, oo_body, append, "", "br");
- }else if (dom.tagName.toLowerCase() === "hr") {
- this.processHr(dom, oo_body, append);
- }else if (dom.tagName.toLowerCase() === "img") {
- if (!oo_body || oo_body.tagName.toString().toLowerCase()!=="w:p") var oo_body = this.createParagraphFromDom(dom, oo_body, dom.parentElement);
- this.processPic(dom, oo_body, append);
- }else if (dom.tagName.toLowerCase() === "table") {
- this.processTable(dom, oo_body, append);
- }else if (dom.tagName.toLowerCase() === "span") {
- if (!oo_body || oo_body.tagName.toString().toLowerCase()!=="w:p") var oo_body = this.createParagraphFromDom(dom, oo_body, dom.parentElement);
- this.processRun(dom, oo_body, append);
- }else{
- this.processTableDom(dom, oo_body, append, divAsP);
- }
- }else if (dom.nodeType===Node.TEXT_NODE){
- var text = dom.nodeValue.replace(/(?=\s|\n|\r|\t)[^\u3000]/g, '');
- if (text){
- if (!oo_body || oo_body.tagName.toString().toLowerCase()!=="w:p") var oo_body = this.createParagraphFromDom(dom.parentElement || dom.parentNode, oo_body, dom.parentElement);
- this.processRun(dom.parentElement || dom.parentNode, oo_body, append, text);
- }
- }else{
- this.processTableDom(dom, oo_body, append);
- }
- dom = dom.nextSibling;
- }
- },
- arrangeTable: function(table){
- //检查table,不合理的colspan
- var tableMatrix = [];
- var trs = table.rows;
- var rowspan = {};
- for (var i=0; i<trs.length; i++){
- tableMatrix[i] = [];
- //垂直合并单元格处理
- var tdIdx = 0;
- var rowspanObj = rowspan[tdIdx];
- while (rowspanObj && rowspanObj.count){
- rowspanObj.count--;
- tableMatrix[i].push({"td": rowspanObj.td, type: 0});
- if (rowspanObj.count<1) delete rowspan[tdIdx];
- tdIdx++;
- rowspanObj = rowspan[tdIdx];
- }
- var tds = trs[i].cells;
- for (var j=0; j<tds.length; j++){
- var td = tds[j];
- //记录实体单元格
- tableMatrix[i].push({"td": td, type: 1});
- //记录此单元格是否有垂直合并
- var cspan = td.get("colspan");
- var rspan = td.get("rowspan");
- if (rspan && parseInt(rspan)>1){
- rowspan[tdIdx] = {
- "td": td,
- "count": parseInt(rspan)-1
- };
- if (cspan && parseInt(cspan)>1){
- for (var n = 1; n<parseInt(cspan); n++){
- rowspan[tdIdx+n] = {
- "td": td,
- "count": parseInt(rspan)-1
- };
- }
- }
- }
- //补齐水平合并的虚拟单元格
- if (cspan && parseInt(cspan)>1){
- for (var n=1; n<parseInt(cspan); n++){
- tableMatrix[i].push({"td": td, type: 0});
- tdIdx++;
- }
- }
- //检查有没有之前的垂直合并内容
- var nextIdx = tdIdx+1;
- var rowspanObj = rowspan[nextIdx];
- while (rowspanObj && rowspanObj.count){
- rowspanObj.count--;
- tableMatrix[i].push({"td": rowspanObj.td, type: 0});
- if (rowspanObj.count<1) delete rowspan[nextIdx];
- nextIdx++;
- rowspanObj = rowspan[nextIdx];
- }
- // if (cspan && parseInt(cspan)>1){
- // if (rowspan[tdIdx]){
- // rowspan[tdIdx].count = (rowspan[tdIdx].count)*parseInt(cspan);
- // for (var n=1; n<parseInt(cspan); n++){
- // rowspan[tdIdx+n] = rowspan[tdIdx];
- // }
- // }
- // }
- tdIdx = nextIdx-1;
- tdIdx++;
- }
- }
- var y = tableMatrix.length;
- if (y>0){
- var x = tableMatrix[0].length;
- for (var x1=0; x1<x; x1++){
- var flag = 0;
- for (var y1=0; y1<y; y1++){
- if (tableMatrix[y1][x1].type!==0){
- flag=1;
- break;
- }
- }
- if (flag===0){
- for (var y1=0; y1<y; y1++){
- if (y1==tableMatrix[y1][x1].td.getParent("tr").rowIndex){
- if (tableMatrix[y1][x1].td){
- var colspan = tableMatrix[y1][x1].td.get("colspan");
- colspan = parseInt(colspan)-1;
- tableMatrix[y1][x1].td.set("colspan", colspan);
- }
- }
- }
- }
- }
- }
- },
- processTable(table, oo_body, append) {
- this.arrangeTable(table);
- var oo_doc = oo_body.ownerDocument;
- var oo_tbl = this.createEl(oo_doc, 'tbl');
- var oo_tblPr = this.createEl(oo_doc, 'tblPr');
- //表格宽度属性
- var oo_tblW = this.createEl(oo_doc, 'tblW');
- var tblW = this.getTableTblW(table);
- this.setAttrs(oo_tblW, tblW);
- oo_tblPr.appendChild(oo_tblW);
- //表格边框属性
- var oo_tblBorders = this.createEl(oo_doc, 'tblBorders');
- var oo_top = this.createEl(oo_doc, 'top');
- this.setAttrs(oo_top, this.getTableBorder(table, 'top'));
- var oo_start = this.createEl(oo_doc, 'start');
- this.setAttrs(oo_start, this.getTableBorder(table, 'left'));
- var oo_bottom = this.createEl(oo_doc, 'bottom');
- this.setAttrs(oo_bottom, this.getTableBorder(table, 'bottom'));
- var oo_end = this.createEl(oo_doc, 'end');
- this.setAttrs(oo_end, this.getTableBorder(table, 'right'));
- this.insertSiblings(oo_tblBorders, [oo_top, oo_start, oo_bottom, oo_end], 'beforeend');
- oo_tblPr.appendChild(oo_tblBorders);
- //表格边距
- var mar = parseFloat(table.get('cellpadding'));
- if (!mar) mar = 0;
- //if (mar){
- mar = this.pxToPt(mar) * 20;
- var left = table.getStyle('padding-left');
- var right = table.getStyle('padding-right');
- var top = table.getStyle('padding-top');
- var bottom = table.getStyle('padding-bottom');
- left = (left) ? this.pxToPt(left) * 20 : 0;
- right = (right) ? this.pxToPt(right) * 20 : 0;
- top = (top) ? this.pxToPt(top) * 20 : 0;
- bottom = (bottom) ? this.pxToPt(bottom) * 20 : 0;
- var oo_tblCellMar = this.createEl(oo_doc, 'tblCellMar');
- var oo_mar = this.createEl(oo_doc, 'start');
- this.setAttrs(oo_mar, {'type': 'dxa', 'w': left || mar});
- oo_tblCellMar.appendChild(oo_mar);
- oo_mar = this.createEl(oo_doc, 'end');
- this.setAttrs(oo_mar, {'type': 'dxa', 'w': right || mar});
- oo_tblCellMar.appendChild(oo_mar);
- oo_mar = this.createEl(oo_doc, 'top');
- this.setAttrs(oo_mar, {'type': 'dxa', 'w': top || mar});
- oo_tblCellMar.appendChild(oo_mar);
- oo_mar = this.createEl(oo_doc, 'bottom');
- this.setAttrs(oo_mar, {'type': 'dxa', 'w': bottom || mar});
- oo_tblCellMar.appendChild(oo_mar);
- oo_tblPr.appendChild(oo_tblCellMar);
- //}
- //左右对齐
- var align = table.get('align');
- if (align) {
- var jc = 'start';
- switch (align) {
- case 'center':
- jc = 'center';
- break;
- case 'right':
- case 'end':
- jc = 'end';
- break;
- case 'justify':
- jc = 'both';
- break;
- }
- var oo_jc = this.createEl(oo_doc, 'jc');
- this.setAttrs(oo_jc, {'val': jc});
- oo_tblPr.appendChild(oo_jc);
- }
- //表格浮动
- var floatTable = false;
- var msoStyle = this.getMsoStyle(table);
- var horzAnchor = msoStyle['mso-table-anchor-horizontal'];
- var vertAnchor = msoStyle['mso-table-anchor-vertical'];
- var tblpXSpec = msoStyle['mso-table-left'];
- var tblpYSpec = msoStyle['mso-table-top'];
- if (horzAnchor || vertAnchor || tblpXSpec || tblpYSpec) {
- if (horzAnchor && horzAnchor !== 'page' && horzAnchor !== 'margin' && horzAnchor !== 'text') horzAnchor = 'margin';
- if (vertAnchor && vertAnchor !== 'page' && vertAnchor !== 'margin' && vertAnchor !== 'text') vertAnchor = 'margin';
- var o = {
- 'horzAnchor': horzAnchor || null,
- 'vertAnchor': vertAnchor || null,
- 'tblpXSpec': tblpXSpec || null,
- 'tblpYSpec': tblpYSpec || null
- };
- var oo_tblpPr = this.createEl(oo_doc, 'tblpPr');
- this.setAttrs(oo_tblpPr, o);
- oo_tblPr.appendChild(oo_tblpPr);
- floatTable = true;
- }
- //表格背景
- var bg = table.getStyle('background-color');
- if (bg && bg !== 'transparent') {
- bg = this.getColorHex(bg);
- if (bg !== 'transparent') {
- var oo_shd = this.createEl(oo_doc, 'shd');
- this.setAttrs(oo_shd, {'val': 'clear', 'color': 'auto', 'fill': bg});
- oo_tblPr.appendChild(oo_shd);
- }
- }
- //表格必须使用固定宽度表格布局算法
- //<w:tblLayout w:type="fixed"/>;
- var oo_tblLayout = this.createEl(oo_doc, 'tblLayout');
- this.setAttrs(oo_tblLayout, {'type': 'fixed'});
- oo_tblPr.appendChild(oo_tblLayout);
- oo_tbl.appendChild(oo_tblPr);
- //表格网格
- var grids = this.getTableTblGrid(table);
- var oo_tblGrid = this.createEl(oo_doc, 'tblGrid');
- grids.forEach(function (grid) {
- var oo_gridCol = this.createEl(oo_doc, 'gridCol');
- if (grid) this.setAttrs(oo_gridCol, {'w': grid * 20});
- oo_tblGrid.appendChild(oo_gridCol);
- }.bind(this));
- oo_tbl.appendChild(oo_tblGrid);
- var vmge = {};
- var trs = table.rows;
- for (var i = 0; i < trs.length; i++) {
- var tr = trs[i];
- if (!tr.hasClass('doc_layout_seal_position') || tr.querySelector('img')) {
- var oo_tr = this.createEl(oo_doc, 'tr');
- var oo_trPr = this.createEl(oo_doc, 'trPr');
- oo_tr.appendChild(oo_trPr);
- //行高度 设置模板中table的 data-compute-tr-height 为 y 时计算
- if (table.dataset.computeTrHeight === 'y') {
- var oo_trH = this.createEl(oo_doc, 'trHeight');
- var trH = this.getTableTrH(tr);
- this.setAttrs(oo_trH, trH);
- oo_trPr.appendChild(oo_trH);
- }
- if (floatTable) {
- // var oo_trPr = this.createEl(oo_doc, "trPr");
- var oo_cantSplit = this.createEl(oo_doc, 'cantSplit');
- var oo_tblHeader = this.createEl(oo_doc, 'tblHeader');
- oo_trPr.appendChild(oo_cantSplit);
- oo_trPr.appendChild(oo_tblHeader);
- }
- debugger;
- var tdIdx = 0;
- //垂直合并单元格
- var nextIdx = tdIdx;
- var mge = vmge['td' + nextIdx];
- while (mge) {
- // if (mge.idx>0){
- vmge['td' + nextIdx].idx--;
- var tcPr = vmge['td' + nextIdx].tcPr;
- var oo_mtc = this.createEl(oo_doc, 'tc');
- if (tcPr) {
- var oo_mtcPr = tcPr.cloneNode(true);
- var oo_mvMerge = oo_mtcPr.querySelector('vMerge');
- if (oo_mvMerge) oo_mvMerge.destroy();
- oo_mvMerge = this.createEl(oo_doc, 'vMerge');
- oo_mtcPr.appendChild(oo_mvMerge);
- if (mge.colspan && mge.colspan!='null'){
- nextIdx = nextIdx + (parseInt(mge.colspan) - 1);
- // tdIdx = tdIdx + (parseInt(mge.colspan) - 1);
- var oo_gridSpan = this.createEl(oo_doc, 'gridSpan');
- this.setAttrs(oo_gridSpan, {'val': mge.colspan});
- oo_mtcPr.appendChild(oo_gridSpan);
- }
- oo_mtc.appendChild(oo_mtcPr);
- } else {
- var oo_mtcPr = this.createEl(oo_doc, 'tcPr');
- var oo_mvMerge = this.createEl(oo_doc, 'vMerge');
- oo_mtcPr.appendChild(oo_mvMerge);
- if (mge.colspan && mge.colspan!='null'){
- nextIdx = nextIdx + (parseInt( mge.colspan) - 1);
- // tdIdx = tdIdx + (parseInt( mge.colspan) - 1);
- var oo_gridSpan = this.createEl(oo_doc, 'gridSpan');
- this.setAttrs(oo_gridSpan, {'val': mge.colspan});
- oo_mtcPr.appendChild(oo_gridSpan);
- }
- oo_mtc.appendChild(oo_mtcPr);
- }
- // var oo_mtc = this.createEl(oo_doc, "tc");
- // var oo_mtcPr = this.createEl(oo_doc, "tcPr");
- // var oo_mvMerge = this.createEl(oo_doc, "vMerge");
- // oo_mtcPr.appendChild(oo_mvMerge);
- // oo_mtc.appendChild(oo_mtcPr);
- var oo_mp = this.createEl(oo_doc, 'p');
- oo_mtc.appendChild(oo_mp);
- oo_tr.appendChild(oo_mtc);
- if (vmge['td' + tdIdx].idx < 1) delete vmge['td' + tdIdx];
- nextIdx++;
- tdIdx = nextIdx;
- mge = vmge['td' + nextIdx];
- // }
- // tdIdx++;
- }
- // tdIdx--;
- var tds = tr.cells;
- for (var j = 0; j < tds.length; j++) {
- var td = tds[j];
- var oo_tc = this.createEl(oo_doc, 'tc');
- var oo_tcPr = this.createEl(oo_doc, 'tcPr');
- //单元格宽度
- var oo_tcW = this.createEl(oo_doc, 'tcW');
- var tcW = this.getTableTblW(td);
- this.setAttrs(oo_tcW, tcW);
- //this.setAttrs(oo_tcW, {"w": this.pxToPt(td.clientWidth)*20, "type": "dxa"});
- oo_tcPr.appendChild(oo_tcW);
- // //单元格垂直对齐
- // var valign = td.get('valign');
- // v = (valign!=='top' && valign!=='bottom') ? 'center' : valign;
- // var oo_vAlign = this.createEl(oo_doc, "vAlign");
- // this.setAttrs(oo_vAlign, {'val': v})
- // // <w:vAlign w:val="center"/>
- // oo_tcPr.appendChild(oo_vAlign);
- //单元格边框
- var oo_tcBorders = this.createEl(oo_doc, 'tcBorders');
- var oo_top = this.createEl(oo_doc, 'top');
- this.setAttrs(oo_top, this.getTableBorder(td, 'top'));
- var oo_start = this.createEl(oo_doc, 'start');
- this.setAttrs(oo_start, this.getTableBorder(td, 'left'));
- var oo_bottom = this.createEl(oo_doc, 'bottom');
- this.setAttrs(oo_bottom, this.getTableBorder(td, 'bottom'));
- var oo_end = this.createEl(oo_doc, 'end');
- this.setAttrs(oo_end, this.getTableBorder(td, 'right'));
- this.insertSiblings(oo_tcBorders, [oo_top, oo_start, oo_bottom, oo_end], 'beforeend');
- oo_tcPr.appendChild(oo_tcBorders);
- //单元格背景
- var bg = td.getStyle('background-color');
- if (bg && bg !== 'transparent') {
- bg = this.getColorHex(bg);
- if (bg !== 'transparent') {
- var oo_shd = this.createEl(oo_doc, 'shd');
- this.setAttrs(oo_shd, {'val': 'clear', 'color': 'auto', 'fill': bg});
- oo_tcPr.appendChild(oo_shd);
- }
- }
- //单元格边距
- var left = td.getStyle('padding-left');
- var right = td.getStyle('padding-right');
- var top = td.getStyle('padding-top');
- var bottom = td.getStyle('padding-bottom');
- left = (left) ? this.pxToPt(left) * 20 : 0;
- right = (right) ? this.pxToPt(right) * 20 : 0;
- top = (top) ? this.pxToPt(top) * 20 : 0;
- bottom = (bottom) ? this.pxToPt(bottom) * 20 : 0;
- var oo_tcMar = this.createEl(oo_doc, 'tcMar');
- var oo_mar = this.createEl(oo_doc, 'start');
- this.setAttrs(oo_mar, {'type': 'dxa', 'w': left});
- oo_tcMar.appendChild(oo_mar);
- oo_mar = this.createEl(oo_doc, 'end');
- this.setAttrs(oo_mar, {'type': 'dxa', 'w': right});
- oo_tcMar.appendChild(oo_mar);
- oo_mar = this.createEl(oo_doc, 'top');
- this.setAttrs(oo_mar, {'type': 'dxa', 'w': top});
- oo_tcMar.appendChild(oo_mar);
- oo_mar = this.createEl(oo_doc, 'bottom');
- this.setAttrs(oo_mar, {'type': 'dxa', 'w': bottom});
- oo_tcMar.appendChild(oo_mar);
- oo_tcPr.appendChild(oo_tcMar);
- var v = this.getTdValign(td);
- if (v) {
- var oo_vAlign = this.createEl(oo_doc, 'vAlign');
- this.setAttrs(oo_vAlign, {'val': v});
- oo_tcPr.appendChild(oo_vAlign);
- }
- var oo_hideMark = this.createEl(oo_doc, 'hideMark');
- oo_tcPr.appendChild(oo_hideMark);
- //垂直合并单元格
- var colspan = td.get('colspan');
- var rowspan = td.get('rowspan');
- if (rowspan && parseInt(rowspan) > 1) {
- vmge['td' + tdIdx] = {
- 'tcPr': oo_tcPr,
- 'idx': parseInt(rowspan) - 1,
- 'colspan': (colspan && colspan!=='null') ? colspan : null
- };
- var oo_vMerge = this.createEl(oo_doc, 'vMerge');
- this.setAttrs(oo_vMerge, {'val': 'restart'});
- oo_tcPr.appendChild(oo_vMerge);
- }
- //水平合并单元格
- if (colspan && parseInt(colspan) > 1) {
- tdIdx = tdIdx + (parseInt(colspan) - 1);
- var oo_gridSpan = this.createEl(oo_doc, 'gridSpan');
- this.setAttrs(oo_gridSpan, {'val': colspan});
- oo_tcPr.appendChild(oo_gridSpan);
- }
- oo_tc.appendChild(oo_tcPr);
- //表格内容;
- this.processTableDom(td, oo_tc, td, true, oo_tc);
- var pflag = false;
- var node = oo_tc.firstChild;
- while (node) {
- if (node.tagName === 'w:p') {
- pflag = true;
- break;
- }
- node = node.nextSibling;
- }
- if (!pflag) {
- var oo_p = this.createEl(oo_doc, 'p');
- oo_tc.appendChild(oo_p);
- }
- oo_tr.appendChild(oo_tc);
- //垂直合并单元格
- tdIdx++;
- var nextIdx = tdIdx;
- var mge = vmge['td' + nextIdx];
- while (mge) {
- vmge['td' + nextIdx].idx--;
- var tcPr = vmge['td' + nextIdx].tcPr;
- var oo_mtc = this.createEl(oo_doc, 'tc');
- if (tcPr) {
- var oo_mtcPr = tcPr.cloneNode(true);
- var oo_mvMerge = oo_mtcPr.querySelector('vMerge');
- if (oo_mvMerge) oo_mvMerge.destroy();
- oo_mvMerge = this.createEl(oo_doc, 'vMerge');
- oo_mtcPr.appendChild(oo_mvMerge);
- if (mge.colspan && mge.colspan!='null'){
- nextIdx = nextIdx + (parseInt( mge.colspan) - 1);
- // tdIdx = tdIdx + (parseInt( mge.colspan) - 1);
- var oo_gridSpan = this.createEl(oo_doc, 'gridSpan');
- this.setAttrs(oo_gridSpan, {'val': mge.colspan});
- oo_mtcPr.appendChild(oo_gridSpan);
- }
- oo_mtc.appendChild(oo_mtcPr);
- } else {
- var oo_mtcPr = this.createEl(oo_doc, 'tcPr');
- var oo_mvMerge = this.createEl(oo_doc, 'vMerge');
- oo_mtcPr.appendChild(oo_mvMerge);
- if (mge.colspan && mge.colspan!='null'){
- nextIdx = nextIdx + (parseInt( mge.colspan) - 1);
- // tdIdx = tdIdx + (parseInt( mge.colspan) - 1);
- var oo_gridSpan = this.createEl(oo_doc, 'gridSpan');
- this.setAttrs(oo_gridSpan, {'val': mge.colspan});
- oo_mtcPr.appendChild(oo_gridSpan);
- }
- oo_mtc.appendChild(oo_mtcPr);
- }
- var oo_mp = this.createEl(oo_doc, 'p');
- oo_mtc.appendChild(oo_mp);
- oo_tr.appendChild(oo_mtc);
- if (vmge['td' + tdIdx].idx < 1) delete vmge['td' + tdIdx];
- nextIdx++;
- mge = vmge['td' + nextIdx];
- }
- // if (colspan && parseInt(colspan) > 1) {
- // if (vmge['td' + tdIdx]) {
- // vmge['td' + tdIdx].idx = (vmge['td' + tdIdx].idx) * parseInt(colspan);
- // for (var n = 1; n < parseInt(colspan); n++) {
- // var m = tdIdx + n;
- // rowspan[m] = vmge['td' + tdIdx];
- // }
- // }
- // }
- tdIdx = nextIdx - 1;
- tdIdx++;
- }
- oo_tbl.appendChild(oo_tr);
- }
- }
- if (append) {
- oo_body.appendChild(oo_tbl);
- } else {
- var oo_sectPr = this.getEl(oo_body, 'sectPr');
- if (oo_sectPr) {
- this.insertSiblings(oo_sectPr, [oo_tbl], 'beforebegin');
- } else {
- this.insertChildren(oo_body, [oo_tbl]);
- }
- }
- },
- pxToPt: function(px){
- if (!px) return 0;
- var v = px;
- if (px && o2.typeOf(px)==="string"){
- u = px.substring(px.length-2, px.length);
- if (u.toLowerCase()!=="pt"){
- v = (px.toFloat()/this.dpi)*72;
- }else{
- v = px.toFloat();
- }
- }else{
- v = (px.toFloat()/this.dpi)*72;
- }
- return v;
- },
- setPics: function(img){
- if (!this.pics || !this.pics.length) this.pics = [];
- this.pics.push(img.src);
- },
- processPic: function(img, oo_p, append){
- if (!img.src) return "";
- this.setPics(img);
- var idx = this.rid;
- this.rid++;
- var oo_doc = oo_p.ownerDocument;
- //
- // var oo_p = this.createParagraph(oo_doc, {});
- // if (append){
- // oo_body.appendChild(oo_p);
- // }else{
- // var oo_sectPr = this.getEl(oo_body, "sectPr");
- // if (oo_sectPr){
- // this.insertSiblings(oo_sectPr, [oo_p], "beforebegin");
- // }else{
- // this.insertChildren(oo_body, [oo_p]);
- // }
- // }
- var oo_run = this.createRun(oo_doc, {"rPrs": {"noProof":{}}});
- var oo_drawing = this.createEl(oo_doc, "drawing");
- var msoStyle = this.getMsoStyle(img);
- var position = img.getStyle("position");
- var p = (msoStyle["mso-position-vertical"]==="absolute") ? "anchor" : "inline";
- var oo_position;
- if (p==="anchor"){
- //var pos = img.getPosition(img.getParent(".WordSection1"));
- //var pos = img.getPosition();
- var positionV = this.pxToPt(msoStyle["mso-top"]);
- var positionH = this.pxToPt(msoStyle["mso-left"]);
- if (!positionV || !positionH){
- var pos = img.getPosition(img.getOffsetParent());
- if (!positionH) positionH = this.pxToPt(pos.x);
- if (!positionV) positionV = this.pxToPt(pos.y);
- }
- positionH = (positionH*12700).toInt();
- positionV = (positionV*12700).toInt();
- var oo_anchor = this.createEl(oo_doc, "anchor", "wp");
- var behindDoc = (msoStyle["mso-behinddoc"]=="true") ? "1" : "0";
- this.setAttrs(oo_anchor, {
- "distT": "0", "distB": "0", "distL": "0", "distR": "0", "simplePos": "0","behindDoc": behindDoc, "relativeHeight": "500", "locked": "0", "layoutInCell":"1", "allowOverlap": "0"
- }, false);
- oo_run.appendChild(oo_drawing);
- oo_drawing.appendChild(oo_anchor);
- var oo_simplePos = this.createEl(oo_doc, "simplePos", "wp");
- this.setAttrs(oo_simplePos, {"x": "0", "y": "0"}, false);
- var relativeFrom = msoStyle["mso-position-horizontal-relative"] || "margin";
- var oo_positionH = this.createEl(oo_doc, "positionH", "wp");
- this.setAttrs(oo_positionH, {"relativeFrom": relativeFrom}, false);
- var oo_posOffset = this.createEl(oo_doc, "posOffset", "wp");
- oo_posOffset.appendChild(oo_doc.createTextNode(positionH));
- oo_positionH.appendChild(oo_posOffset);
- relativeFrom = msoStyle["mso-position-vertical-relative"] || "margin";
- var oo_positionV = this.createEl(oo_doc, "positionV", "wp");
- this.setAttrs(oo_positionV, {"relativeFrom": relativeFrom}, false);
- var oo_posOffset = this.createEl(oo_doc, "posOffset", "wp");
- oo_posOffset.appendChild(oo_doc.createTextNode(positionV)); //此处需要根据行高来设置数值,暂时固定数值
- oo_positionV.appendChild(oo_posOffset);
- oo_position = oo_anchor;
- this.insertSiblings(oo_position, [oo_simplePos, oo_positionH, oo_positionV], "beforeend");
- }else{
- var oo_inline = this.createEl(oo_doc, "inline", "wp");
- this.setAttrs(oo_inline, {
- "distT": "0", "distB": "0", "distL": "0", "distR": "0"
- }, false);
- oo_run.appendChild(oo_drawing);
- oo_drawing.appendChild(oo_inline);
- oo_position = oo_inline;
- }
- var oo_extent = this.createEl(oo_doc, "extent", "wp");
- var cx = this.pxToPt(img.clientWidth)*12700;
- var cy = this.pxToPt(img.clientHeight)*12700;
- this.setAttrs(oo_extent, {"cx": cx, "cy": cy}, false); //(pt*12700)
- var oo_effectExtent = this.createEl(oo_doc, "effectExtent", "wp");
- this.setAttrs(oo_effectExtent, {"l": "0", "t": "0", "r": "0", "b": "0"}, false);
- var oo_wrapNone = this.createEl(oo_doc, "wrapNone", "wp");
- var oo_docPr = this.createEl(oo_doc, "docPr", "wp");
- id = (Math.random()*100).toInt();
- this.setAttrs(oo_docPr, {"id": id, "name": "PIC"+id}, false); //id设置随机整数
- var oo_cNvGraphicFramePr = this.createEl(oo_doc, "cNvGraphicFramePr", "wp");
- var oo_graphicFrameLocks = this.createEl(oo_doc, "graphicFrameLocks", "a");
- this.setAttrs(oo_graphicFrameLocks, {"noChangeAspect": "1"}, false);
- oo_cNvGraphicFramePr.appendChild(oo_graphicFrameLocks);
- this.insertSiblings(oo_position, [oo_extent, oo_effectExtent, oo_wrapNone, oo_docPr, oo_cNvGraphicFramePr], "beforeend");
- var oo_graphic = this.createEl(oo_doc, "graphic", "a");
- var oo_graphicData = this.createEl(oo_doc, "graphicData", "a");
- this.setAttrs(oo_graphicData, {"uri": "http://schemas.openxmlformats.org/drawingml/2006/picture"}, false);
- var oo_pic = this.createEl(oo_doc, "pic", "pic");
- this.insertChildren(oo_position, [oo_graphic, oo_graphicData, oo_pic], "beforeend");
- var oo_nvPicPr = this.createEl(oo_doc, "nvPicPr", "pic");
- var oo_cNvPr = this.createEl(oo_doc, "cNvPr", "pic");
- this.setAttrs(oo_cNvPr, {"id": id, "name": "PIC"+id}, false);
- var oo_cNvPicPr = this.createEl(oo_doc, "cNvPicPr", "pic");
- oo_nvPicPr.appendChild(oo_cNvPr);
- oo_nvPicPr.appendChild(oo_cNvPicPr);
- oo_pic.appendChild(oo_nvPicPr);
- var oo_blipFill = this.createEl(oo_doc, "blipFill", "pic");
- var oo_blip = this.createEl(oo_doc, "blip", "a");
- this.setAttrs(oo_blip, {"embed": "rId"+idx}, "r");
- this.setAttrs(oo_blip, {"cstate": "print"}, false);
- var oo_extLst = this.createEl(oo_doc, "extLst", "a");
- var oo_ext = this.createEl(oo_doc, "ext", "a");
- this.setAttrs(oo_ext, {"uri": "{28A0092B-C50C-407E-A947-70E740481C1C}"}, false);
- var oo_useLocalDpi = this.createEl(oo_doc, "useLocalDpi", "a14");
- this.setAttrs(oo_useLocalDpi, {"val": "0"}, false);
- oo_ext.appendChild(oo_useLocalDpi);
- oo_extLst.appendChild(oo_ext);
- oo_blip.appendChild(oo_extLst);
- oo_blipFill.appendChild(oo_blip);
- var oo_stretch = this.createEl(oo_doc, "stretch", "a");
- var oo_fillRect = this.createEl(oo_doc, "fillRect", "a");
- oo_stretch.appendChild(oo_fillRect);
- oo_blipFill.appendChild(oo_stretch);
- oo_pic.appendChild(oo_blipFill);
- var oo_spPr = this.createEl(oo_doc, "spPr", "pic");
- var oo_xfrm = this.createEl(oo_doc, "xfrm", "a");
- var oo_off = this.createEl(oo_doc, "off", "a");
- this.setAttrs(oo_off, {"x": "0", "y": "0"}, false);
- var oo_ext = this.createEl(oo_doc, "ext", "a");
- this.setAttrs(oo_ext, {"cx": cx, "cy": cy}, false);
- oo_xfrm.appendChild(oo_off);
- oo_xfrm.appendChild(oo_ext);
- oo_spPr.appendChild(oo_xfrm);
- var oo_prstGeom = this.createEl(oo_doc, "prstGeom", "a");
- this.setAttrs(oo_prstGeom, {"prst": "rect"}, false);
- var oo_avLst = this.createEl(oo_doc, "avLst", "a");
- oo_prstGeom.appendChild(oo_avLst);
- oo_spPr.appendChild(oo_prstGeom);
- oo_pic.appendChild(oo_spPr);
- // " <pic:blipFill>\n" +
- // " <a:blip r:embed=\"rId4\" cstate=\"print\">\n" +
- // " <a:extLst>\n" +
- // " <a:ext uri=\"{28A0092B-C50C-407E-A947-70E740481C1C}\">\n" +
- // " <a14:useLocalDpi xmlns:a14=\"http://schemas.microsoft.com/office/drawing/2010/main\" val=\"0\"/>\n" +
- // " </a:ext>\n" +
- // " </a:extLst>\n" +
- // " </a:blip>\n" +
- // " <a:stretch>\n" +
- // " <a:fillRect/>\n" +
- // " </a:stretch>\n" +
- // " </pic:blipFill>\n" +
- // " <pic:spPr>\n" +
- // " <a:xfrm>\n" +
- // " <a:off x=\"0\" y=\"0\"/>\n" +
- // " <a:ext cx=\""+cx+"\" cy=\""+cy+"\"/>\n" +
- // " </a:xfrm>\n" +
- // " <a:prstGeom prst=\"rect\">\n" +
- // " <a:avLst/>\n" +
- // " </a:prstGeom>\n" +
- // " </pic:spPr>";
- //
- // //oo_pic.appendHTML(inner, "top");
- // oo_pic.innerHTML = inner;
- oo_p.appendChild(oo_run);
- },
- processHr: function(hr, oo_body, append){
- var oo_doc = oo_body.ownerDocument;
- var oo_p = this.createParagraph(oo_doc, {
- "pPrs": {
- "spacing": {
- "lineRule": "exact",
- "line": "40" //段落行高设置为固定值2pt
- }
- }
- });
- if (append){
- oo_body.appendChild(oo_p);
- }else{
- var oo_sectPr = this.getEl(oo_body, "sectPr");
- if (oo_sectPr){
- this.insertSiblings(oo_sectPr, [oo_p], "beforebegin");
- }else{
- this.insertChildren(oo_body, [oo_p]);
- }
- }
- var oo_run = this.createRun(oo_doc, {"rPrs": {"noProof":{}}});
- var oo_drawing = this.createEl(oo_doc, "drawing");
- var oo_anchor = this.createEl(oo_doc, "anchor", "wp");
- this.setAttrs(oo_anchor, {
- "distT": "0", "distB": "0", "distL": "0", "distR": "0", "simplePos": "false","behindDoc": "false","relativeHeight": "500", "locked": "false", "layoutInCell":"true", "allowOverlap": "false"
- }, false);
- oo_run.appendChild(oo_drawing);
- oo_drawing.appendChild(oo_anchor);
- var oo_simplePos = this.createEl(oo_doc, "simplePos", "wp");
- this.setAttrs(oo_simplePos, {"x": "0", "y": "0"}, false);
- var oo_positionH = this.createEl(oo_doc, "positionH", "wp");
- this.setAttrs(oo_positionH, {"relativeFrom": "column"}, false);
- var oo_posOffset = this.createEl(oo_doc, "posOffset", "wp");
- oo_posOffset.appendChild(oo_doc.createTextNode("0"));
- oo_positionH.appendChild(oo_posOffset);
- var oo_positionV = this.createEl(oo_doc, "positionV", "wp");
- this.setAttrs(oo_positionV, {"relativeFrom": "paragraph"}, false);
- var oo_posOffset = this.createEl(oo_doc, "posOffset", "wp");
- oo_posOffset.appendChild(oo_doc.createTextNode("3810")); //此处需要根据行高来设置数值,暂时固定数值
- oo_positionV.appendChild(oo_posOffset);
- var oo_extent = this.createEl(oo_doc, "extent", "wp");
- var cx = this.pxToPt(hr.clientWidth)*12700;
- this.setAttrs(oo_extent, {"cx": cx, "cy": "0"}, false); //cx为线长度(pt*12700)
- var oo_effectExtent = this.createEl(oo_doc, "effectExtent", "wp");
- this.setAttrs(oo_effectExtent, {"l": "0", "t": "0", "r": "0", "b": "0"}, false);
- var oo_wrapNone = this.createEl(oo_doc, "wrapNone", "wp");
- var oo_docPr = this.createEl(oo_doc, "docPr", "wp");
- id = (Math.random()*100).toInt();
- this.setAttrs(oo_docPr, {"id": id, "name": "Red Line"}, false); //id设置随机整数
- var oo_cNvGraphicFramePr = this.createEl(oo_doc, "cNvGraphicFramePr", "wp");
- this.insertSiblings(oo_anchor, [oo_simplePos, oo_positionH, oo_positionV, oo_extent, oo_effectExtent, oo_wrapNone, oo_docPr, oo_cNvGraphicFramePr], "beforeend");
- var oo_graphic = this.createEl(oo_doc, "graphic", "a");
- var oo_graphicData = this.createEl(oo_doc, "graphicData", "a");
- this.setAttrs(oo_graphicData, {"uri": "http://schemas.microsoft.com/office/word/2010/wordprocessingShape"}, false);
- var oo_wsp = this.createEl(oo_doc, "wsp", "wps");
- this.insertChildren(oo_anchor, [oo_graphic, oo_graphicData, oo_wsp], "beforeend");
- var oo_cNvCnPr = this.createEl(oo_doc, "cNvCnPr", "wps");
- var oo_spPr = this.createEl(oo_doc, "spPr", "wps")
- var oo_xfrm = this.createEl(oo_doc, "xfrm", "a");
- var oo_off = this.createEl(oo_doc, "off", "a");
- this.setAttrs(oo_off, {"x": "0", "y": "0"}, false);
- var oo_ext = this.createEl(oo_doc, "ext", "a");
- this.setAttrs(oo_ext, {"cx": cx, "cy": "0"}, false);
- this.insertSiblings(oo_xfrm, [oo_off, oo_ext], "beforeend");
- var oo_prstGeom = this.createEl(oo_doc, "prstGeom", "a");
- this.setAttrs(oo_prstGeom, {"prst": "line"}, false);
- var oo_avLst = this.createEl(oo_doc, "avLst", "a");
- oo_prstGeom.appendChild(oo_avLst);
- var oo_ln = this.createEl(oo_doc, "ln", "a");
- var w = this.pxToPt(hr.clientHeight)*12700;
- this.setAttrs(oo_ln, {"w": w}, false); //线的粗细 pt*12700
- var oo_solidFill = this.createEl(oo_doc, "solidFill", "a");
- var oo_srgbClr = this.createEl(oo_doc, "srgbClr", "a");
- var color = this.getColorHex(hr.get("color"));
- if (!color) color = this.getColorHex(hr.getStyle("background-color"));
- if (!color) color = "FF0000";
- this.setAttrs(oo_srgbClr, {"val": color}, false); //line color
- oo_solidFill.appendChild(oo_srgbClr);
- oo_ln.appendChild(oo_solidFill);
- this.insertSiblings(oo_spPr, [oo_xfrm, oo_prstGeom, oo_ln], "beforeend");
- var oo_style = this.createEl(oo_doc, "style", "wps");
- //oo_style.innerHTML = '<a:lnRef idx="1"><a:schemeClr val="accent1"/></a:lnRef><a:fillRef idx="0"><a:schemeClr val="accent1"/></a:fillRef><a:effectRef idx="0"><a:schemeClr val="accent1"/></a:effectRef><a:fontRef idx="minor"><a:schemeClr val="tx1"/></a:fontRef>';
- var oo_lnRef = this.createEl(oo_doc, "lnRef", "a");
- this.setAttrs(oo_lnRef, {"idx": "1"}, false);
- var oo_schemeClr = this.createEl(oo_doc, "schemeClr", "a");
- this.setAttrs(oo_schemeClr, {"val": "accent1"}, false);
- oo_lnRef.appendChild(oo_schemeClr);
- var oo_fillRef = this.createEl(oo_doc, "fillRef", "a");
- this.setAttrs(oo_fillRef, {"idx": "0"}, false);
- var oo_schemeClr = this.createEl(oo_doc, "schemeClr", "a");
- this.setAttrs(oo_schemeClr, {"val": "accent1"}, false);
- oo_fillRef.appendChild(oo_schemeClr);
- var oo_effectRef = this.createEl(oo_doc, "effectRef", "a");
- this.setAttrs(oo_effectRef, {"idx": "0"}, false);
- var oo_schemeClr = this.createEl(oo_doc, "schemeClr", "a");
- this.setAttrs(oo_schemeClr, {"val": "accent1"}, false);
- oo_effectRef.appendChild(oo_schemeClr);
- var oo_fontRef = this.createEl(oo_doc, "fontRef", "a");
- this.setAttrs(oo_fontRef, {"idx": "minor"}, false);
- var oo_schemeClr = this.createEl(oo_doc, "schemeClr", "a");
- this.setAttrs(oo_schemeClr, {"val": "tx1"}, false);
- oo_fontRef.appendChild(oo_schemeClr);
- this.insertSiblings(oo_style, [oo_lnRef, oo_fillRef, oo_effectRef, oo_fontRef], "beforeend");
- var oo_bodyPr = this.createEl(oo_doc, "bodyPr", "wps");
- this.insertSiblings(oo_wsp, [oo_cNvCnPr, oo_spPr, oo_style, oo_bodyPr], "beforeend");
- var w = hr.style.width;
- if (w && o2.typeOf(w)==="string"){
- var u = w.substring(w.length-1, w.length);
- if (u==="%"){
- w = w.toFloat()*1000;
- var oo_sizeRelH = this.createEl(oo_doc, "sizeRelH", "wp14");
- this.setAttrs(oo_sizeRelH, {"relativeFrom": "margin"}, false);
- var oo_pctWidth = this.createEl(oo_doc, "pctWidth", "wp14");
- oo_pctWidth.appendChild(oo_doc.createTextNode(w));
- oo_sizeRelH.appendChild(oo_pctWidth);
- oo_anchor.appendChild(oo_sizeRelH);
- }
- }
- oo_p.appendChild(oo_run);
- },
- processRunFont: function(node, rPrs, font){
- //字体处理缩放
- var msoStyle = this.getMsoStyle(node);
- if (msoStyle["mso-font-width"]) rPrs.w = {"val": msoStyle["mso-font-width"].toFloat()};
- //处理字号
- if (msoStyle["mso-ansi-font-size"]) rPrs.sz = {"val": this.parseFontSize(msoStyle["mso-ansi-font-size"])*2};
- if (msoStyle["mso-hansi-font-size"]) rPrs.sz = {"val": this.parseFontSize(msoStyle["mso-hansi-font-size"])*2};
- if (msoStyle["mso-font-size"]) rPrs.sz = {"val": this.parseFontSize(msoStyle["mso-font-size"])*2};
- if (msoStyle["mso-fareast-font-size"]) rPrs.sz = {"val": this.parseFontSize(msoStyle["mso-fareast-font-size"])*2};
- //处理字体
- if (msoStyle["mso-ansi-font-family"]){
- if (!font) font = { "hint": "eastAsia" };
- font.ascii = this.parseFont(msoStyle["mso-ansi-font-family"]);
- }
- if (msoStyle["mso-hansi-font-family"]){
- if (!font) font = { "hint": "eastAsia" };
- font.hAnsi = this.parseFont(msoStyle["mso-hansi-font-family"]);
- }
- if (msoStyle["mso-font-family"]){
- if (!font) font = { "hint": "eastAsia" };
- font.eastAsia = this.parseFont(msoStyle["mso-font-family"]);
- }
- if (msoStyle["mso-fareast-font-family"]){
- if (!font) font = { "hint": "eastAsia" };
- font.eastAsia = this.parseFont(msoStyle["mso-fareast-font-family"]);
- }
- },
- parseFont: function(name){
- if (name.substr(0, 1)==="\""){
- return name.substr(1, name.length-2);
- }else{
- return name;
- }
- },
- parseFontSize: function(sz){
- var size = sz;
- if (size && o2.typeOf(size)==="string"){
- u = size.substring(size.length-2, size.length);
- if (u.toLowerCase()!=="pt"){
- size = Math.round(this.pxToPt(size));
- }
- }
- return size.toFloat();
- },
- processRun: function(span, oo_p, p, text, br){
- var rPrs = {"noProof": {}};
- var font = null;
- var styles = span.getStyles("font-size", "color", "letter-spacing", "font-weight", "font-family", "line-height");
- var keys = Object.keys(styles);
- var msoStyle = this.getMsoStyle(span);
- styles["font-size"] = window.getComputedStyle(span).fontSize;
- for (var i = 0; i<keys.length; i++){
- switch (keys[i]){
- case "font-size":
- rPrs.sz = {"val": this.parseFontSize(styles["font-size"])*2};
- break;
- case "color":
- rPrs.color = {"val": this.getColorHex(styles["color"])};
- break;
- case "letter-spacing":
- //实际测试发现letter-spacing * 0.55 转换word比较合适
- rPrs.spacing = {"val": (styles["letter-spacing"].toFloat()*20 || 0)};
- break;
- case "font-weight":
- var b = styles["font-weight"];
- if (b.toLowerCase()=="normal"){
- //nothing
- }else if (b.toLowerCase()=="bold") {
- rPrs.b = {"val": "true"};
- }else{
- var n = b.toFloat();
- if (n>=600) rPrs.b = {"val": "true"};
- }
- break;
- case "font-family":
- var fonts = styles["font-family"].split(/,\s*/);
- font = {
- "hint": "eastAsia",
- "eastAsia": this.parseFont(fonts[fonts.length-1])
- }
- if (fonts.length>1) font.other = this.parseFont(fonts[0]);
- break;
- case "line-height":
- var h = this.pxToPt(styles["line-height"])*20;
- if (oo_p){
- var oo_pPrs = oo_p.getElementsByTagNameNS(this.nsResolver("w"), "pPr");
- var oo_pPr = (oo_pPrs.length) ? oo_pPrs.item(0) : null;
- if (!oo_pPr){
- oo_pPr = this.createEl(oo_p.ownerDocument,"pPr");
- oo_p.appendChild(oo_pPr);
- }
- var oo_spacings = oo_pPr.getElementsByTagNameNS(this.nsResolver("w"), "spacing");
- var oo_spacing = (oo_spacings.length) ? oo_spacings.item(0) : null;
- if (!oo_spacing){
- oo_spacing = this.createEl(oo_p.ownerDocument,"spacing");
- oo_pPr.appendChild(oo_spacing);
- }
- var line = oo_spacing.getAttributeNS(this.nsResolver("w"), "line");
- if (line<h){
- this.setAttrs(oo_spacing, {"lineRule": "exact", "line": h});
- }
- }
- default:
- //nothing
- }
- }
- if (msoStyle["mso-letter-spacing"]){
- rPrs.spacing = {"val": (msoStyle["mso-letter-spacing"].toFloat()*20 || 0)};
- }
- if (p) this.processRunFont(p, rPrs, font);
- this.processRunFont(span, rPrs, font);
- var runPrs = {"rPrs": rPrs, "font": font};
- if (!text && !br){
- //if (span.tagName.toString().toLowerCase()==="span"){
- this.processRunTextDom(span, oo_p, runPrs);
- //}
- }else{
- runPrs.br = br;
- var oo_run = this.createRun(oo_p.ownerDocument, runPrs);
- if (text){
- text = text.replace(/(?=\s|\n|\r|\t)[^\u3000]/g, '');
- var oo_t = this.createEl(oo_run.ownerDocument,"t");
- oo_t.appendChild(oo_run.ownerDocument.createTextNode(text));
- oo_run.appendChild(oo_t);
- }
- oo_p.appendChild(oo_run);
- }
- },
- processRunSpan: function(span, oo_p, runPrs) {
- //var runPrs = {"rPrs": rPrs, "font": font, "text": text, "br": br};
- var rPrs = Object.clone(runPrs.rPrs);
- var font = Object.clone(runPrs.font);
- var styles = span.getStyles("font-size", "color", "letter-spacing", "font-weight", "font-family")
- var keys = Object.keys(styles);
- styles["font-size"] = window.getComputedStyle(span).fontSize;
- for (var i = 0; i<keys.length; i++){
- switch (keys[i]){
- case "font-size":
- rPrs.sz = {"val": this.parseFontSize(styles["font-size"])*2};
- break;
- case "color":
- rPrs.color = {"val": this.getColorHex(styles["color"])};
- break;
- case "letter-spacing":
- //实际测试发现letter-spacing * 0.55 转换word比较合适
- rPrs.spacing = {"val": (styles["letter-spacing"].toFloat()*20 || 0)};
- break;
- case "font-weight":
- var b = styles["font-weight"];
- if (b.toLowerCase()=="normal"){
- //nothing
- }else if (b.toLowerCase()=="bold") {
- rPrs.b = {"val": "true"};
- }else{
- var n = b.toFloat();
- if (n>=600) rPrs.b = {"val": "true"};
- }
- break;
- case "font-family":
- var fonts = styles["font-family"].split(/,\s*/);
- if (!font) font = {};
- font.hint = "eastAsia";
- font.eastAsia = this.parseFont(fonts[fonts.length-1]);
- // font = {
- // "hint": "eastAsia",
- // "eastAsia": this.parseFont(fonts[fonts.length-1])
- // }
- if (fonts.length>1) font.other = this.parseFont(fonts[0]);
- break;
- default:
- //nothing
- }
- }
- this.processRunFont(span, rPrs, font);
- var runPrs = {"rPrs": rPrs, "font": font};
- this.processRunTextDom(span, oo_p, runPrs);
- },
- processRunTextDom: function(span, oo_p, runPrs){
- var node = span.firstChild;
- while (node){
- if (node.nodeType===Node.ELEMENT_NODE){
- if (node.getStyle("display") === "none") {
- continue;
- }else if (node.tagName.toLowerCase() === "span"){
- this.processRunSpan(node, oo_p, runPrs);
- }else if (node.tagName.toLowerCase() === "br"){
- runPrs.br = "br";
- var oo_run = this.createRun(oo_p.ownerDocument, runPrs);
- oo_p.appendChild(oo_run);
- runPrs.br = "";
- }else{
- this.processRunTextDom(node, oo_p, runPrs);
- }
- }else if (node.nodeType===Node.TEXT_NODE){
- //if (node.nodeValue.trim()){
- var oo_run = this.createRun(oo_p.ownerDocument, runPrs);
- var oo_t = this.createEl(oo_p.ownerDocument,"t");
- oo_t.appendChild(oo_p.ownerDocument.createTextNode(node.nodeValue));
- oo_run.appendChild(oo_t);
- oo_p.appendChild(oo_run);
- //}
- }else{
- this.processRunTextDom(node, oo_p, runPrs);
- }
- node = node.nextSibling;
- }
- },
- processPageSection: function(dom_pageRule, oo_body){
- var oo_sectPr = this.getOrCreateEl(oo_body, "sectPr");
- if (oo_sectPr){
- for (var i = 0; i<dom_pageRule.style.length; i++){
- switch (dom_pageRule.style[i]){
- case "size":
- if( dom_pageRule.style["size"] ){
- var v = dom_pageRule.style["size"].split(/\s/);
- var w = v[0].toFloat()*20, h=v[1].toFloat()*20;
- var oo_pgSz = this.getOrCreateEl(oo_sectPr, "pgSz");
- this.setAttrs(oo_pgSz, {"w": w, "h": h});
- this.pageHeight = h;
- this.pageWidth = w;
- }
- break;
- case "margin-top":
- case "margin-right":
- case "margin-bottom":
- case "margin-left":
- var p = dom_pageRule.style[i].split("-")[1];
- var v = dom_pageRule.style["margin"+p.capitalize()].toFloat()*20;
- var oo_pgMar = this.getOrCreateEl(oo_sectPr, "pgMar");
- if (p==="bottom"){
- var footer = oo_pgMar.getAttributeNS(this.nsResolver("w"), "footer");
- if ((parseInt(footer)+454)>v){
- footer = v-454;
- this.setAttrs(oo_pgMar, {"footer": footer});
- }
- }
- var attrs = {};
- attrs[p] = v
- this.setAttrs(oo_pgMar, attrs);
- this["page-"+dom_pageRule.style[i]] = v;
- break;
- case "line-height":
- case "letter-spacing":
- var oo_docGrid = this.getOrCreateEl(oo_sectPr, "docGrid");
- var lh = dom_pageRule.style["lineHeight"].toFloat()*20;
- var cs = dom_pageRule.style["letterSpacing"].toFloat()*4096;
- var attrs = {"type": "linesAndChars"};
- if (lh) attrs["linePitch"] = lh;
- //if (cs) attrs["charSpace"] = cs;
- this.setAttrs(oo_docGrid, attrs);
- break;
- default:
- //nothing
- }
- }
- if (!this.pageHeight) this.pageHeight = this.options.pageHeight.toFloat()*20;
- if (!this.pageWidth) this.pageWidth = this.options.pageWidth.toFloat()*20;
- if (!this["page-margin-left"]) this["page-margin-left"] = this.options["page-margin-left"].toFloat()*20;
- if (!this["page-margin-right"]) this["page-margin-right"] = this.options["page-margin-right"].toFloat()*20;
- if (!this["page-margin-top"]) this["page-margin-top"] = this.options["page-margin-top"].toFloat()*20;
- if (!this["page-margin-bottom"]) this["page-margin-bottom"] = this.options["page-margin-bottom"].toFloat()*20;
- if (!this.options.firstPageNumber){
- var oo_titlePg = this.createEl(oo_body.ownerDocument, "titlePg");
- oo_sectPr.appendChild(oo_titlePg);
- }
- }
- },
- getOrCreateEl: function(el, tag){
- var node = this.getEl(el, tag);
- if (!node){
- node = this.createEl(el.ownerDocument, tag);
- el.appendChild(node);
- }
- return node;
- },
- createParagraph: function(xmlDoc, options){
- var p = this.createEl(xmlDoc,"p");
- var pPr = this.createEl(xmlDoc,"pPr");
- p.appendChild(pPr);
- /*
- * //如:对齐方式描述如下
- * {
- * "jc": { val: "both" },
- * }
- * */
- if (options && options.pPrs){
- Object.keys(options.pPrs).each(function(k){
- if (k=="pageBreak"){
- var oo_r = this.createEl(xmlDoc, "r");
- var oo_br = this.createEl(xmlDoc, "br");
- this.setAttrs(oo_br, {"type": "page"});
- oo_r.appendChild(oo_br);
- p.appendChild(oo_r);
- }else{
- var node = this.createEl(xmlDoc, k);
- this.setAttrs(node, options.pPrs[k]);
- pPr.appendChild(node);
- }
- }.bind(this));
- }
- return p;
- },
- createRun: function(xmlDoc, options){
- var r = this.createEl(xmlDoc, "r");
- var rPr = this.createEl(xmlDoc,"rPr");
- r.appendChild(rPr);
- if (options && options.text){
- var t = this.createEl(xmlDoc,"t");
- options.text = options.text.replace(/[\u200B-\u200D\uFEFF]/g, '');
- t.appendChild(xmlDoc.createTextNode(options.text));
- r.appendChild(t);
- }
- if (options && options.br){
- var oo_br = this.createEl(xmlDoc,"br");
- r.appendChild(oo_br);
- }
- if (options && options.font){
- var rFonts = this.createEl(xmlDoc,"rFonts");
- var font = {
- "eastAsia": options.font.eastAsia || options.font.font,
- "ascii": options.font.ascii || options.font.other || options.font.eastAsia || options.font.font,
- "hAnsi": options.font.hAnsi || options.font.other || options.font.eastAsia || options.font.font,
- }
- if (options.font.hint) font.hint = options.font.hint;
- this.setAttrs(rFonts, font);
- rPr.appendChild(rFonts);
- }
- /*
- * //如:粗体和字体颜色描述如下
- * {
- * "b": { val: "true" },
- * "color": { val: "FF0000" }
- * }
- * */
- if (options && options.rPrs){
- Object.keys(options.rPrs).each(function(k){
- var node = this.createEl(xmlDoc, k);
- this.setAttrs(node, options.rPrs[k]);
- rPr.appendChild(node);
- }.bind(this));
- }
- return r;
- },
- insertChildren: function(p, els, position){
- this.insertAdjacent(p, els, position, "beforeend");
- },
- insertSiblings: function(p, els, position){
- this.insertAdjacent(p, els, position, "afterend");
- },
- insertAdjacent: function(p, els, posFirst, posNext){
- var pos = posFirst || "beforeend";
- var posMapping = {
- 'beforebegin': "before",
- 'afterbegin': "top",
- 'beforeend': "bottom",
- 'afterend': "after"
- };
- els.each(function(e){
- (p.insertAdjacentElement) ? p.insertAdjacentElement(pos, e) : e.inject(p, posMapping[pos]);
- p = e;
- pos = posNext || "afterend";
- });
- },
- getEl: function(el, tag, ns){
- return this.getEls(el, tag, ns)[0];
- },
- getEls: function(el, tag, ns){
- var n = ns || "w";
- return el.getElementsByTagNameNS(this.nsResolver(n), tag);
- },
- createEl: function(xmlDoc, tag, ns){
- if (ns===false){
- return xmlDoc.createElement(tag);
- }else{
- var n = ns || "w";
- if (false && Browser.name==="ie"){
- return xmlDoc.createElement(n+":"+tag);
- }
- return xmlDoc.createElementNS(this.nsResolver(n), n+":"+tag);
- }
- },
- setAttr: function(node, name, value, ns){
- if (ns===false){
- node.setAttribute(name, value);
- }else{
- var n = ns || "w";
- if (false && Browser.name==="ie") {
- node.setAttribute(n+":"+name, value);
- }else{
- node.setAttributeNS(this.nsResolver(n), n+":"+name, value);
- }
- }
- },
- setAttrs: function(node, attrs, ns){
- if (ns===false){
- Object.keys(attrs).forEach(function(key){
- node.setAttribute(key, attrs[key]);
- });
- }else{
- var n = this.nsResolver(ns || "w");
- Object.keys(attrs).forEach(function(key){
- if (false && Browser.name==="ie") {
- node.setAttribute((ns || "w")+":"+key, attrs[key]);
- }else{
- node.setAttributeNS(n, (ns || "w")+":"+key, attrs[key]);
- }
- });
- }
- },
- nsResolver: function(prefix){
- var ns = {
- "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
- "wpc": "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
- "cx": "http://schemas.microsoft.com/office/drawing/2014/chartex",
- "cx1": "http://schemas.microsoft.com/office/drawing/2015/9/8/chartex",
- "cx2": "http://schemas.microsoft.com/office/drawing/2015/10/21/chartex",
- "cx3": "http://schemas.microsoft.com/office/drawing/2016/5/9/chartex",
- "cx4": "http://schemas.microsoft.com/office/drawing/2016/5/10/chartex",
- "cx5": "http://schemas.microsoft.com/office/drawing/2016/5/11/chartex",
- "cx6": "http://schemas.microsoft.com/office/drawing/2016/5/12/chartex",
- "cx7": "http://schemas.microsoft.com/office/drawing/2016/5/13/chartex",
- "cx8": "http://schemas.microsoft.com/office/drawing/2016/5/14/chartex",
- "mc": "http://schemas.openxmlformats.org/markup-compatibility/2006",
- "aink": "http://schemas.microsoft.com/office/drawing/2016/ink",
- "am3d": "http://schemas.microsoft.com/office/drawing/2017/model3d",
- "o": "urn:schemas-microsoft-com:office:office",
- "r": "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
- "m": "http://schemas.openxmlformats.org/officeDocument/2006/math",
- "v": "urn:schemas-microsoft-com:vml",
- "wp14": "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing",
- "wp": "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
- "w10": "urn:schemas-microsoft-com:office:word",
- "w14": "http://schemas.microsoft.com/office/word/2010/wordml",
- "w15": "http://schemas.microsoft.com/office/word/2012/wordml",
- "w16cex": "http://schemas.microsoft.com/office/word/2018/wordml/cex",
- "w16cid": "http://schemas.microsoft.com/office/word/2016/wordml/cid",
- "w16": "http://schemas.microsoft.com/office/word/2018/wordml",
- "w16sdtdh": "http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash",
- "w16se": "http://schemas.microsoft.com/office/word/2015/wordml/symex",
- "wpg": "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup",
- "wpi": "http://schemas.microsoft.com/office/word/2010/wordprocessingInk",
- "wne": "http://schemas.microsoft.com/office/word/2006/wordml",
- "wps": "http://schemas.microsoft.com/office/word/2010/wordprocessingShape",
- "a": "http://schemas.openxmlformats.org/drawingml/2006/main",
- "pic": "http://schemas.openxmlformats.org/drawingml/2006/picture",
- "a14": "http://schemas.microsoft.com/office/drawing/2010/main",
- "rel": "http://schemas.openxmlformats.org/package/2006/relationships"
- };
- return ns[prefix] || null;
- },
- getColorHex: function(clr){
- if (!clr) return "";
- var colorKeys = {
- "black": "000000",
- "silver": "c0c0c0",
- "gray": "808080",
- "white": "ffffff",
- "maroon": "800000",
- "red": "ff0000",
- "purple": "800080",
- "fuchsia": "ff00ff",
- "green": "008000",
- "lime": "00ff00",
- "olive": "808000",
- "yellow": "ffff00",
- "navy": "000080",
- "blue": "0000ff",
- "teal": "008080",
- "aqua": "00ffff",
- "initial": "000000"
- }
- if (colorKeys[clr]) return colorKeys[clr];
- var f = clr.substr(0,1);
- if (f==="#") return clr.replace("#", "");
- return clr.rgbToHex() || clr;
- }
- });
|