diff --git a/PSD2HTML/lib/page.jsx b/PSD2HTML/lib/page.jsx index 7967ccd..374516e 100644 --- a/PSD2HTML/lib/page.jsx +++ b/PSD2HTML/lib/page.jsx @@ -173,8 +173,8 @@ page.formatHtml = function(htmlCode){ htmlCode = html.join(''); - //过滤多余的span - htmlCode = htmlCode.replace(/()(.*?)(<\/span>)/g,'$2'); + //过滤多余的span,不可以去掉,文本中的空格要依赖span + //htmlCode = htmlCode.replace(/()(.*?)(<\/span>)/g,'$2'); //td内img去空格去换行 var img = htmlCode.split('和中间的
+ htmlCode = htmlCode.replace(/(<\/p>\s*)(\s?)*?(\s*<\/[^p])/g, '$1$3'); + //htmlCode = htmlCode.replace(/(<\/p>)[^<]*?(<\/a>)/, '

'); return htmlCode; }; diff --git a/PSD2HTML/lib/page/css.jsx b/PSD2HTML/lib/page/css.jsx index 735b007..75005bd 100644 --- a/PSD2HTML/lib/page/css.jsx +++ b/PSD2HTML/lib/page/css.jsx @@ -25,14 +25,18 @@ page.css.prototype.get = function(){ style.push('text-indent:' + textInfo.indent + 'px'); } + if(textInfo.italic == true){ + style.push('font-style:italic'); + } + //对齐 style.push('text-align:' + textInfo.textAlign + ''); //非网页不需要这些样式 if(page.option.builder == "normal"){ //z-index style.push('z-index:' + item.index); - style.push('top:' + item.top + 'px'); - style.push('left:' + item.left + 'px'); + style.push('top:' + Math.round(item.top) + 'px'); + style.push('left:' + Math.round(item.left) + 'px'); if(textInfo.font != 'SimSun' && textInfo.font != 'NSimSun'){ if(textInfo.font.indexOf(' ')>-1 || textInfo.font.indexOf('-')>-1){ style.push('font-family:\''+textInfo.font+'\''); @@ -53,8 +57,13 @@ page.css.prototype.get = function(){ //文字大小 - style.push('font-size:' + textInfo.size + 'px'); + style.push('font-size:' + Math.round(textInfo.size) + 'px'); style.push('line-height:' + textInfo.lineHeight +'px'); + + if(textInfo.bold == true){ + style.push('font-weight:bold'); + } + if(textInfo.color != '000000'){ style.push('color:#' + textInfo.color); } diff --git a/PSD2HTML/lib/page/data.jsx b/PSD2HTML/lib/page/data.jsx index 46ed4c4..8d89631 100644 --- a/PSD2HTML/lib/page/data.jsx +++ b/PSD2HTML/lib/page/data.jsx @@ -91,7 +91,7 @@ page.data.prototype.parse = function(item){ bottom = item.bottom, size = item.textInfo.size, lineHeight = item.textInfo.lineHeight, - contents = item.textInfo.contents.replace(/\r\n/g, "\r").replace(/\n/g, "\r"), + contents = item.textInfo.contents.replace(/\r\n/g, "\r").replace(/\n/g, "\r").replace(/(\s*$)/g,""),//去\r\n和最右边空格 textType = item.textInfo.textType, widthOver = parseInt(size/4,10),//宽度误差 topOver = 0;//top误差 @@ -116,6 +116,19 @@ page.data.prototype.parse = function(item){ if(item.textInfo.textType == 'TextType.POINTTEXT' && contents.indexOf("\n")==-1 && new RegExp(contents.substr(contents.length-1)).test(this.unicode)){ width += size; right += size; + }else if(item.textInfo.textType == 'TextType.PARAGRAPHTEXT' && contents.indexOf("\r") == -1 && new RegExp(contents.substr(contents.length-1,contents.length)).test(this.unicode)){ + width += size; + right += size; + }else if(contents.indexOf("\r") > -1){ + var o = contents.split('\r'); + //有回车的换行 + var prevLen = o[0].length, + lastLen = o[o.length-1].length, + lastStr = contents.substr(contents.length-1); + if(lastLen >= prevLen && new RegExp(lastStr).test(this.unicode)){ + width += size; + right += size; + } }else if(item.textInfo.textType == 'TextType.PARAGRAPHTEXT'){ //一行文字数量 var aRowTextlen = Math.round(item.width/size), @@ -131,8 +144,8 @@ page.data.prototype.parse = function(item){ width += size; right += size; } - } + } //left if(page.option.builder == "normal"){ @@ -199,4 +212,4 @@ page.data.prototype.removeRepeat = function(){ }; -page.data.prototype.unicode = ",.\/<>?;\':\"[]{}()!,。《》、?;‘:“{}【】!"; +page.data.prototype.unicode = ",.\/<>?;\':\"[]{}()!,。《》、?;‘:“{}【】! "; diff --git a/PSD2HTML/lib/page/element.jsx b/PSD2HTML/lib/page/element.jsx index 577585d..8637992 100644 --- a/PSD2HTML/lib/page/element.jsx +++ b/PSD2HTML/lib/page/element.jsx @@ -141,23 +141,23 @@ page.element.prototype.text = function(){ var start = 0, - end = 0; - for(var o in pObj){ + end = 0, + newlineLen = 0, + createP = true; + for(var o=0;o

'); - isCreateP = true; if(page.option.builder != "normal"){ p['@style'] = 'margin:0px;padding:0px;'; }else{ p['@class'] = 'paragraph'; } - var len = rangeData.length + var len = rangeData.length; for(var i=0;i=curEnd && end>curStart){ var text = textContents.substring(curStart,curEnd); var textTrim = text.replace(/^\s+/, "").replace(/\s+$/, ""); @@ -192,9 +192,10 @@ page.element.prototype.text = function(){ span['@style'] = lineCss.join(';')+';'; } } - - }else{ - isCreateP = false; + createP = true; + newlineLen ++; + }else if(newlineLen == 0){ + createP = false; } } } @@ -207,7 +208,16 @@ page.element.prototype.text = function(){ if(p.toXMLString().indexOf("span")==-1){ p = new XML('
'); } - elm.appendChild(p); + if(createP === true){ + elm.appendChild(p); + } + //如果第一行就是BR,没有意思,直接干掉 + /*if(newLineStart == 0 && p.toXMLString() == '
'){ + newLineStart == 0; + }else{ + newLineStart ++; + elm.appendChild(p); + }*/ } @@ -216,8 +226,8 @@ page.element.prototype.text = function(){ page.element.prototype.getCss = function(parentStyle,textRange){ var lineCss = []; - if(textRange.size != parentStyle.size && textRange.size != 12){ - lineCss.push('font-size:'+textRange.size+'px'); + if(textRange.size != parentStyle.size && Math.round(textRange.size) != 12){ + lineCss.push('font-size:'+Math.round(textRange.size)+'px'); } if(textRange.color != parentStyle.color){ lineCss.push('color:#'+textRange.color+''); diff --git a/PSD2HTML/lib/page/table.jsx b/PSD2HTML/lib/page/table.jsx index 540d5ae..1258ffd 100644 --- a/PSD2HTML/lib/page/table.jsx +++ b/PSD2HTML/lib/page/table.jsx @@ -245,39 +245,29 @@ page.table.prototype.sortData = function(field,order){ }; /** - * 随机抽取50处的颜色值来比较是否相同,如果是返回true和值,或者返回false和值 + * 获取是否为纯色,如果是,返回true&色值,或则返回false&色值 * @param {Object} left * @param {Object} top * @param {Object} right * @param {Object} bottom */ page.table.prototype.getSolidColor = function(left,top,right,bottom){ - if(left < 1){left=1;} - if(top <1){top=1;} - - var color = getColor(left,top); - - for(var i=0;i<50;i++){ - var x = Math.floor(left+Math.random()*(right-left)), - y = Math.floor(top+Math.random()*(bottom-top)), - result = getColor(x,y); - if(color != result){ - return {solid:false,value:'#'+color}; - } - - } - - return {solid:true,value:'#'+color}; - //获取颜色 function getColor(x,y){ - if(x<1){x=1;} - if(x>page.width-1){x = page.width-1;} - if(y<1){y=1;} - if(y>page.height-1){y=page.height-1;} + if(x<=1){x=1;} + else if(x>=page.width-1){x = page.width-1;} + else{x+=1;} + if(y<=1){y=1;} + else if(y>=page.height-1){y=page.height-1;} + else{y+=1;} - return page.getPsdRGBColor(x,y); + return page.getPsdRGBColor(x+1,y); } + + return { + solid:page.psd.selectionIsMonochrome([[left,top],[right,top],[right,bottom],[left,bottom]]), + value:'#'+ getColor(left,top) + }; }; /** diff --git a/PSD2HTML/lib/psd.jsx b/PSD2HTML/lib/psd.jsx index 1886704..441cfc0 100644 --- a/PSD2HTML/lib/psd.jsx +++ b/PSD2HTML/lib/psd.jsx @@ -1,4 +1,4 @@ -// @include "json2-min.jsx" +// @include "json2-min.jsx" // @include "web-fonts.jsx" //setting for app preferences @@ -188,7 +188,7 @@ PSD.fn = PSD.prototype = { // text justification switch(textItem.justification.toString()){ case 'Justification.LEFT': - child.textInfo.textAlign = 'levft'; + child.textInfo.textAlign = 'left'; break; case 'Justification.RIGHT': child.textInfo.textAlign = 'right';