Skip to content

Commit

Permalink
newline bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaowuming committed Jul 18, 2012
1 parent 6a33839 commit ec4a6c6
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 45 deletions.
14 changes: 11 additions & 3 deletions PSD2HTML/lib/page.jsx
Expand Up @@ -173,8 +173,8 @@ page.formatHtml = function(htmlCode){

htmlCode = html.join('');

//过滤多余的span
htmlCode = htmlCode.replace(/(<span>)(.*?)(<\/span>)/g,'$2');
//过滤多余的span,不可以去掉,文本中的空格要依赖span
//htmlCode = htmlCode.replace(/(<span>)(.*?)(<\/span>)/g,'$2');
//td内img去空格去换行
var img = htmlCode.split('<img'),
html = [];
Expand All @@ -191,7 +191,15 @@ page.formatHtml = function(htmlCode){
htmlCode = html.join('');

//还原空格替代符
htmlCode = htmlCode.replace(new RegExp(page.spaceStr,'g'),' ');
if(page.option.builder == "normal"){
htmlCode = htmlCode.replace(new RegExp(page.spaceStr,'g'),' ');
}else{
htmlCode = htmlCode.replace(new RegExp(page.spaceStr,'g'),'&nbsp;');
}

//去掉</p>和</div>中间的<br/>
htmlCode = htmlCode.replace(/(<\/p>\s*)(<br\/>\s?)*?(\s*<\/[^p])/g, '$1$3');
//htmlCode = htmlCode.replace(/(<\/p>)[^<]*?(<br\/><\/a>)/, '</p></a>');
return htmlCode;

};
15 changes: 12 additions & 3 deletions PSD2HTML/lib/page/css.jsx
Expand Up @@ -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+'\'');
Expand All @@ -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);
}
Expand Down
19 changes: 16 additions & 3 deletions PSD2HTML/lib/page/data.jsx
Expand Up @@ -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误差
Expand All @@ -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),
Expand All @@ -131,8 +144,8 @@ page.data.prototype.parse = function(item){
width += size;
right += size;
}
}

}

//left
if(page.option.builder == "normal"){
Expand Down Expand Up @@ -199,4 +212,4 @@ page.data.prototype.removeRepeat = function(){
};


page.data.prototype.unicode = ",.\/<>?;\':\"[]{}()!,。《》、?;‘:“{}【】!";
page.data.prototype.unicode = ",.\/<>?;\':\"[]{}()!,。《》、?;‘:“{}【】! ";
32 changes: 21 additions & 11 deletions PSD2HTML/lib/page/element.jsx
Expand Up @@ -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<pObj.length;o++){
start = end;
end += pObj[o].length + overValue;
var p = new XML('<p></p>');
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<rangeData.length;i++){
var textRange = rangeData[i],
curStart = textRange.range[0],
curEnd = textRange.range[1];

if(start<=curStart && end+1>=curEnd && end>curStart){
var text = textContents.substring(curStart,curEnd);
var textTrim = text.replace(/^\s+/, "").replace(/\s+$/, "");
Expand Down Expand Up @@ -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;
}
}
}
Expand All @@ -207,7 +208,16 @@ page.element.prototype.text = function(){
if(p.toXMLString().indexOf("span")==-1){
p = new XML('<br/>');
}
elm.appendChild(p);
if(createP === true){
elm.appendChild(p);
}
//如果第一行就是BR,没有意思,直接干掉
/*if(newLineStart == 0 && p.toXMLString() == '<br/>'){
newLineStart == 0;
}else{
newLineStart ++;
elm.appendChild(p);
}*/


}
Expand All @@ -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+'');
Expand Down
36 changes: 13 additions & 23 deletions PSD2HTML/lib/page/table.jsx
Expand Up @@ -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)
};
};

/**
Expand Down
4 changes: 2 additions & 2 deletions PSD2HTML/lib/psd.jsx
@@ -1,4 +1,4 @@
// @include "json2-min.jsx"
// @include "json2-min.jsx"
// @include "web-fonts.jsx"

//setting for app preferences
Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit ec4a6c6

Please sign in to comment.