我使用 HTMLCellType 实现上下标 ,在打印pdf 的时候 出现 上下标 错位 的现象。
添加了一个上标 按钮, 一个下标 按钮。
case "supcornermark"://elnxc-a 上下角标 因为想做成复选的样子,名字需和checkbox里面的id一样
cornermark("sup");
break;
case "subcornermark"://elnxc-a 上下角标 因为想做成复选的样子,名字需和checkbox里面的id一样
cornermark("sub");
break;
按钮方法是:
var spreadNS = GC.Spread.Sheets;
function cornermark(cornermark) {
var cornermarkstart = "<" + cornermark + ">";
var cornermarkend = "</" + cornermark + ">";
var userSelection;
if (window.getSelection) {//一般浏览器
userSelection = window.getSelection();}
else if (document.selection) {//IE浏览器、Opera
userSelection = document.selection.createRange();}
var userSelectionx=userSelection.toString();
// 整个文本
var text = userSelection.anchorNode.data;
// 起点
var anchor = userSelection.anchorOffset;
// 终点
var focus = userSelection.extentOffset;
var length = text.length;
if(anchor > focus){
anchor += focus;
focus = anchor -focus;
anchor -=focus;
}
if(anchor == focus || length == (focus - anchor)){
alert("请选择部分内容!");
return;
}
//alert(text.substring(0,anchor));
var s1 = text.substring(0,anchor);
//alert(text.substring(anchor,focus));
var s2 = text.substring(anchor,focus);
//alert(text.substring(focus,length));
var s3 = text.substring(focus,length);
var ss = s1 + cornermarkstart + s2 + cornermarkend +s3;
var spread = designer.wrapper.spread;
var activeSheet = spread.getActiveSheet();
var rowindex = activeSheet.getActiveRowIndex();
var columnindex = activeSheet.getActiveColumnIndex();
activeSheet.suspendPaint();
activeSheet.setValue(rowindex,columnindex,ss);
activeSheet.resumePaint();
activeSheet.suspendPaint();
var cell = activeSheet.getCell(rowindex,columnindex);
cell.cellType(new HTMLCellType());//指定是上下标单元格
activeSheet.resumePaint();
setPrintInfo(spread);
}
//elnxc-a 上下标start HTMLCellType
function HTMLCellType() {
this.typeName = "HTMLCellType";
this.allowOverflow = false;
}
HTMLCellType.prototype = new spreadNS.CellTypes.Text();
HTMLCellType.prototype.paint = function (ctx, value, x, y, w, h, style, context) {
if(value == '' || value == null || value === undefined) {
return;
}
// 判断是不是有居中等格式
var xcstyle = style;
// 是否左右居中 0靠左 1居中 2靠右
var hAlign = style.hAlign;
// 是否上下居中 0靠上 1居中 2靠下
var vAlign = style.vAlign;
var thisrow = context.row;
var thiscol = context.col;
var spread = designer.wrapper.spread;
var activeSheet = spread.getActiveSheet();
var thiscell = activeSheet.getCell(thisrow,thiscol);
var thisfont = thiscell.font();
var thiscolor = thiscell.foreColor();
var newObj = value.toString();
var showObj = value.toString()
var lll=0;
// 绘制上下标显示
ctx.save();
ctx.rect(x, y, w, h);//表示具有特殊位置的矩形,以及其在二维空间中的宽度和高度。
ctx.clip();
ctx.font = style.font;
//计算整个展示效果占据的宽度
var showwidth = 0;
//计算整个展示效果占据的高度
var showhigh = 0;
var sfont = thisfont.toString();
var pxindex = sfont.indexOf("px");
sfont = sfont.substring(0,pxindex);
if(sfont.indexOf(" ")!=-1){
var arr = sfont.split(" ");
sfont = arr[arr.length-1];
}
sfont = Number(sfont);
showhigh = sfont>18 ? sfont: 18;
while(showObj.indexOf("<sup>")!=-1 || showObj.indexOf("<sub>")!=-1){
var supindex = showObj.indexOf("<sup>");
var subindex = showObj.indexOf("<sub>");
if(subindex==-1 || (supindex!=-1&&supindex<subindex)){//上标在前
var sb = showObj.substring(0,supindex);
var end = showObj.indexOf("</sup>");
var sup = showObj.substring(supindex+5,end);
var se = showObj.substring(end+6,showObj.length);
if(sb!=""){
ctx.font = thisfont;
ctx.fillStyle = thiscolor;
showwidth += ctx.measureText(sb).width;
//ctx.textBaseline = "middle";
//ctx.fillText(sb, showwidth, y+13);
}
ctx.font = "8pt simkai";
//ctx.textBaseline = "alphabetic";
//ctx.fillText(sup, showwidth+ctx.measureText(sup).width, y+13);
showObj = se;
if(ctx.measureText(sup).width>lll)
lll = ctx.measureText(sup).width;
}else{//下标在前
var sb = showObj.substring(0,subindex);
var end = showObj.indexOf("</sub>");
var sub = showObj.substring(subindex+5,end);
var se = showObj.substring(end+6,showObj.length);
if(sb!=""){
ctx.font = thisfont;
ctx.fillStyle = thiscolor;
showwidth += ctx.measureText(sb).width;
//ctx.textBaseline = "middle";
//ctx.fillText(sb, showwidth, y+13);
}
ctx.font = "8pt simkai";
//ctx.textBaseline = "top";
//ctx.fillText(sub, showwidth+ctx.measureText(sub).width, y+13);
showObj = se;
if(ctx.measureText(sub).width>lll)
lll = ctx.measureText(sub).width;
}
//判断一个上标/下标 后面是不是接着正文
if(showObj.indexOf("<sub>")!=0 && showObj.indexOf("<sup>")!=0){
showwidth += lll;
lll = 0;
}
//判断后面是否没有了角标---没有的话--需要自己把它做成正文
if(!(showObj.indexOf("<sup>")!=-1 || showObj.indexOf("<sub>")!=-1)){
var sb = showObj;
if(sb!=""){
ctx.font = thisfont;
ctx.fillStyle = thiscolor;
//ctx.textBaseline = "middle";
//ctx.fillText(sb, showwidth+ctx.measureText(sb).width, y+13);
showwidth +=ctx.measureText(sb).width;
}
}
}
//设置全局参数
var sblength = x+2;
var sbhight = y+13;
var ll = 0;
if(hAlign!=undefined&&hAlign==1){
sblength += (w-showwidth)/2;
}else if(hAlign!=undefined&&hAlign==2){
sblength += (w-showwidth-2-2);
}
if(vAlign!=undefined&&vAlign==1){
sbhight += (h-showhigh)/2;
}else if(vAlign!=undefined&&vAlign==2){
sbhight += (h-showhigh);
}
if(newObj.indexOf("<sup>")!=-1 || newObj.indexOf("<sub>")!=-1){}else{
thiscell.cellType(undefined);
}
while(newObj.indexOf("<sup>")!=-1 || newObj.indexOf("<sub>")!=-1){
var supindex = newObj.indexOf("<sup>");
var subindex = newObj.indexOf("<sub>");
if(subindex==-1 || (supindex!=-1&&supindex<subindex)){//上标在前
var sb = newObj.substring(0,supindex);
var end = newObj.indexOf("</sup>");
var sup = newObj.substring(supindex+5,end);
var se = newObj.substring(end+6,newObj.length);
if(sb!=""){
ctx.font = thisfont;
ctx.fillStyle = thiscolor;
sblength += ctx.measureText(sb).width;
ctx.textBaseline = "middle";
ctx.fillText(sb, sblength, sbhight);
}
ctx.font = "8pt simkai";
ctx.textBaseline = "alphabetic";
ctx.fillText(sup, sblength+ctx.measureText(sup).width, sbhight);
newObj = se;
if(ctx.measureText(sup).width>ll)
ll = ctx.measureText(sup).width;
}else{//下标在前
var sb = newObj.substring(0,subindex);
var end = newObj.indexOf("</sub>");
var sub = newObj.substring(subindex+5,end);
var se = newObj.substring(end+6,newObj.length);
if(sb!=""){
ctx.font = thisfont;
ctx.fillStyle = thiscolor;
sblength += ctx.measureText(sb).width;
ctx.textBaseline = "middle";
ctx.fillText(sb, sblength, sbhight);
}
ctx.font = "8pt simkai";
ctx.textBaseline = "top";
ctx.fillText(sub, sblength+ctx.measureText(sub).width, sbhight);
newObj = se;
if(ctx.measureText(sub).width>ll)
ll = ctx.measureText(sub).width;
}
//判断一个上标/下标 后面是不是接着正文
if(newObj.indexOf("<sub>")!=0 && newObj.indexOf("<sup>")!=0){
sblength += ll;
ll = 0;
}
//判断后面是否没有了角标---没有的话--需要自己把它做成正文
if(!(newObj.indexOf("<sup>")!=-1 || newObj.indexOf("<sub>")!=-1)){
var sb = newObj;
if(sb!=""){
ctx.font = thisfont;
ctx.fillStyle = thiscolor;
ctx.textBaseline = "middle";
ctx.fillText(sb, sblength+ctx.measureText(sb).width, sbhight);
}
}
}
ctx.restore();
setPrintInfo(spread);
};
HTMLCellType.prototype.getEditorValue = function (editorContext) {
return $(editorContext).text().toString();
}
/*code_end*/
function setPrintInfo(spread) {var sheet = spread.getActiveSheet();
var printInfo = sheet.printInfo();
printInfo.showBorder(true);
printInfo.showGridLine(true);
printInfo.showColumnHeader(GC.Spread.Sheets.Print.PrintVisibilityType.hide);
printInfo.showRowHeader(GC.Spread.Sheets.Print.PrintVisibilityType.hide);
printInfo.centering(GC.Spread.Sheets.Print.PrintCentering.horizontal);}
//elnxc-a 上下标end
|
|