16 lines
6.1 KiB
JavaScript
16 lines
6.1 KiB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).TextFrame={})}(this,(function(t){"use strict";
|
||
/*! *****************************************************************************
|
||
Copyright (c) Microsoft Corporation.
|
||
|
||
Permission to use, copy, modify, and/or distribute this software for any
|
||
purpose with or without fee is hereby granted.
|
||
|
||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||
PERFORMANCE OF THIS SOFTWARE.
|
||
***************************************************************************** */var e,n=function(){return(n=Object.assign||function(t){for(var e,n=1,i=arguments.length;n<i;n++)for(var r in e=arguments[n])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t}).apply(this,arguments)};!function(t){t.center="center",t.start="start",t.end="end",t.left="left",t.right="right",t.justify="justify"}(e||(e={}));var i={color:"#000000",fontFamily:"serif",fontWeight:"normal",fontSize:16,rtl:!1,margin:0,marginCollapse:!0,noHeadMargin:!1,textIndent:0,textAlign:"justify",textAlignLast:"start"},r=n(n({},i),{viewWidth:300,viewHeight:150,lineStartProhibitedMarks:"、,,..。::;;!!??'\"」』”’)]})】〗〕]}》〉–~~—·.‧•・//",lineEndProhibitedMarks:"「『“‘([{(【〖〔[〔《〈//",unbreakableRule:/──|……|[\w\d]+/,fragments:null});function a(t){var a=devicePixelRatio||1,l=n(n({},i),t||Object());if(l.textIndent=(0|l.textIndent)*a,l.fontSize>0?l.fontSize*=a:l.fontSize=r.fontSize*a,l.lineHeight>0?l.lineHeight*=a:l.lineHeight=1.5*l.fontSize,l.textAlign in e||(l.textAlign=e.start),"object"==typeof l.margin&&null!==l)for(var o=0,f=["left","right","top","bottom"];o<f.length;o++){var s=f[o];l.margin[s]=(0|l.margin[s])*a}else{var g=l.margin*a;l.margin={left:g,right:g,top:g,bottom:g}}return l}function l(t,e){t.margin;var i=function(t,e){var n={};for(var i in t)Object.prototype.hasOwnProperty.call(t,i)&&e.indexOf(i)<0&&(n[i]=t[i]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var r=0;for(i=Object.getOwnPropertySymbols(t);r<i.length;r++)e.indexOf(i[r])<0&&Object.prototype.propertyIsEnumerable.call(t,i[r])&&(n[i[r]]=t[i[r]])}return n}(t,["margin"]),r=n(n({},i),e);return r.text=r.text||"",r.text=r.text.replace("\r\n","\n").replace("\r","\n"),(r=a(r)).trim&&(r.text=r.text.trim()),r}var o=document.createElement("canvas").getContext("2d");function f(t,n,i){if(i){var r=i.fragmentOptions,a=r.rtl?i.chars.slice(0).reverse():i.chars,l=n.margin,o=r.margin;!function(t,e,n){var i=e;if(i){t.font=i.fontWeight+" "+i.fontSize+"px "+i.fontFamily,t.fillStyle=i.color,t.direction="ltr";var r=i.textAlign;n.isLastLine&&"justify"===i.textAlign&&(r=i.textAlignLast),"start"===r?t.textAlign=i.rtl?"right":"left":"end"===r?t.textAlign=i.rtl?"left":"right":["center","left","right"].includes(r)?t.textAlign=r:t.textAlign="left",t.textBaseline="middle"}}(t,r,i);var f=i.offset,s=f.x,g=f.y,h=r.textAlign===e.justify,c=s-l.left-r.margin.left;if(g+=r.lineHeight/2,h&&i.isLastLine&&r.textAlignLast!==e.justify&&(h=!1),h){var u=t.measureText(a.join("")).width,d=(n.canvasWidth-u-l.left-l.right-o.left-o.right-c)/(a.length-1);r.rtl&&(s-=c);for(var v=0;v<a.length;++v)t.fillText(a[v],Math.round(s),g),s+=t.measureText(a[v]).width+d}else"center"===t.textAlign?s=t.canvas.width/2:"right"===t.textAlign&&(s=n.canvasWidth-l.right-r.margin.right-c),t.fillText(a.join(""),s,g);i=null}}t.computeTextFrames=function(t){var e,i,f,s,g=function(t){var e=devicePixelRatio,i=n(n({},r),a(t));return i.canvasWidth=t.canvasWidth||i.viewWidth*e,i.canvasHeight=t.canvasHeight||i.viewHeight*e,i}(t||{}),h=[],c=g.margin,u=g.canvasWidth-1-c.right,d=g.canvasHeight-1-c.bottom,v={x:0,y:0},x=0,m=0,p=0,y=!1;function b(){if(!i)return"";var t=i.codePointAt(x);if(void 0===t)return"";var e=String.fromCodePoint(t).length;return i.slice(x,x+e)}function w(){s&&O(),s={options:g,lines:[]},f=null,o.clearRect(0,0,o.canvas.width,o.canvas.height),v={x:c.left,y:c.top}}function j(){var t=e;(s||w(),A(),0===s.lines.length)?v={x:c.left+t.margin.left,y:c.top}:d-(v.y+t.lineHeight)<t.lineHeight?(w(),v={x:c.left+t.margin.left,y:c.top}):(v.x=c.left+t.margin.left,v.y+=t.lineHeight);f={chars:[],isLastLine:!1,fragmentOptions:t,offset:n({},v)}}function A(){f&&(s.lines.push(f),f=null)}function H(){f&&(f.isLastLine=!0),A()}function O(){s&&(f&&A(),h.push(s),s=null)}for(var S=0,P=g.fragments||[];S<P.length;S++){var L=P[S];for(H(),L=e=l(t,L),o.font=L.fontWeight+" "+L.fontSize+"px "+L.fontFamily,j(),0===s.lines.length&&(p=0),L.marginCollapse&&y?p=Math.max(L.margin.top,p):p+=L.margin.top,s.lines.length>0&&(v.y+p+L.lineHeight>d+1?(f=null,O(),j()):v.y+=p),0!==s.lines.length||L.noHeadMargin||(v.y=c.top+L.margin.top),L.textIndent>0&&(v.x+=L.textIndent),f.offset.x=v.x,f.offset.y=v.y,m=u-L.margin.right,i=L.text,x=0;x<i.length;){var M=b(),W=M.length,T=o.measureText(M).width,z=m+1-v.x;if("\n"===M)H(),j(),M=null,T=0;else if(z<T){for(var k=M,F=void 0,I=x,R=f.chars.length-1;R>=0;--R){if(F=f.chars[R],!g.lineStartProhibitedMarks.includes(k)&&!g.lineEndProhibitedMarks.includes(F)&&!g.unbreakableRule.test(F+k)){W=(M=k).length,T=o.measureText(M).width,x=I,f.chars=f.chars.slice(0,R+1);break}I-=k.length,k=F}j()}M&&f.chars.push(M),v.x+=T,x+=W}p=L.margin.bottom,y=L.marginCollapse}return H(),O(),h},t.renderFrame=function(t,e,n){if(!t)throw Error("error: text-frame - must provide drawing context!");if(e){var i=Math.round(e.options.canvasWidth),r=Math.round(e.options.canvasHeight);(n||t.canvas.width!==i||t.canvas.height!==r)&&(t.canvas.width=i,t.canvas.height=r);for(var a=0,l=e.lines;a<l.length;a++){var o=l[a];f(t,e.options,o)}}else t.clearRect(0,0,t.canvas.width,t.canvas.height)},Object.defineProperty(t,"__esModule",{value:!0})}));
|