@bigtablet/design-system 4.10.1 → 4.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- import{jsx,jsxs}from"react/jsx-runtime";import*as React from"react";import{chooseDomBreaks,contentWidth,fontPending,ID_ATTR,releasePrehydrate,WORD_CLASS,wordSpans}from"./dom.js";import{toWords}from"./layout.js";import{createCanvasMeasurer,fontOf}from"./measure.js";import{balancedTextScript}from"./prehydrate-script.js";import"./text.css";const useSafeLayoutEffect=typeof window!=="undefined"?React.useLayoutEffect:React.useEffect;const textOf=node=>{if(typeof node==="string"||typeof node==="number")return String(node);if(React.isValidElement(node))return React.Children.toArray(node.props.children).map(textOf).join("");return""};const flatten=children=>React.Children.toArray(children).flatMap(child=>React.isValidElement(child)&&child.type===React.Fragment?flatten(child.props.children):[child]);const shapeOf=node=>{if(!React.isValidElement(node))return String(node);const{type}=node;const name=typeof type==="string"?type:type.displayName??type.name??"?";return`<${name}>${textOf(node)}`};const tokenize=children=>{const tokens=[];let gap="";let lead="";let joined=false;let pos=0;const add=(text,node,spaceBefore)=>{const last=tokens.at(-1);if(joined&&last&&!spaceBefore){last.text+=text;last.end+=text.length;last.nodes.push(node)}else{if(tokens.length===0)lead=gap;tokens.push({text,start:pos,end:pos+text.length,spaceBefore:tokens.length>0&&spaceBefore,nodes:[node],gap:tokens.length>0?gap:""})}pos+=text.length;gap="";joined=true};for(const child of flatten(children)){if(typeof child!=="string"&&typeof child!=="number"){add(textOf(child),child,gap!=="");continue}for(const chunk of String(child).split(/(\s+)/)){if(!chunk)continue;if(/^\s/.test(chunk)){gap+=chunk;joined=false;continue}toWords(chunk).forEach((word,index)=>{if(index>0)joined=false;add(word.text,word.text,index===0&&gap!=="")})}}return{tokens,lead,tail:gap}};const measurers=new Map;const measurerFor=(font,letterSpacing)=>{const key=`${font}|${letterSpacing}`;if(!measurers.has(key))measurers.set(key,createCanvasMeasurer(font,letterSpacing));return measurers.get(key)??null};const SETTLE_MS=100;const MAX_CHURN=6;const BalancedText=({children,as:Tag="span",className,...props})=>{const ref=React.useRef(null);const id=React.useId();const{tokens,lead,tail}=tokenize(children);const key=tokens.map(t=>t.nodes.map(shapeOf).join("")).join("");const[chosen,setChosen]=React.useState(null);const breaks=chosen?.key===key?chosen.breaks:null;const boxed=chosen?.boxed??false;const latest=React.useRef(tokens);const drawnFor=React.useRef(0);const contentSized=React.useRef(false);latest.current=tokens;useSafeLayoutEffect(()=>{const el=ref.current;if(!el)return;let timer;let cancelled=false;let width=contentWidth(el);let churn=0;let churnReset;let gaveUp=false;const apply=breaks2=>{if(++churn>MAX_CHURN){gaveUp=true;setChosen(null);return}clearTimeout(churnReset);churnReset=setTimeout(()=>{churn=0},1e3);drawnFor.current=width;setChosen({key,breaks:breaks2,boxed:contentSized.current})};const compute=()=>{if(gaveUp)return;const pending=fontPending(el);if(pending){pending.then(()=>{if(cancelled)return;for(const m of measurers.values())m?.clear();compute()},()=>{});return}if(el.querySelector(":scope > .text_balanced_line"))return;releasePrehydrate(el);if(wordSpans(el).length!==latest.current.length)return;width=contentWidth(el);const{font,letterSpacing}=fontOf(el);const measure=measurerFor(font,letterSpacing);const breaks2=measure&&chooseDomBreaks(el,measure);if(breaks2)apply(breaks2)};const recompute=()=>{setChosen(null);clearTimeout(timer);timer=setTimeout(()=>{if(!cancelled)compute()},SETTLE_MS)};compute();const observer=typeof ResizeObserver==="undefined"?null:new ResizeObserver(()=>{const next=contentWidth(el);if(next===width)return;width=next;recompute()});observer?.observe(el);return()=>{cancelled=true;clearTimeout(timer);clearTimeout(churnReset);observer?.disconnect()}},[key]);useSafeLayoutEffect(()=>{const el=ref.current;if(!el||!breaks||boxed)return;const style=getComputedStyle(el);const inner=el.clientWidth-Number.parseFloat(style.paddingLeft)-Number.parseFloat(style.paddingRight);let grew=false;if(inner>drawnFor.current+.5){const before=el.clientWidth;el.classList.add("text_balanced_probe");grew=el.clientWidth<before-.5;el.classList.remove("text_balanced_probe")}const right=el.getBoundingClientRect().right-Number.parseFloat(style.paddingRight);const lines=el.querySelectorAll(":scope > .text_balanced_line");const overflow=[...lines].some(line=>line.getBoundingClientRect().right>right+.5);if(grew)contentSized.current=true;if(grew||overflow)setChosen(c=>c?{...c,boxed:true}:c)},[chosen]);const word=i=>{const t=tokens[i];return jsx("span",{className:WORD_CLASS,children:t.nodes.map((node,j)=>jsx(React.Fragment,{children:node},j))},i)};const run=(from,to)=>{const out=[];for(let i=from;i<=to;i++){if(i>from)out.push(tokens[i]?.gap);out.push(word(i))}return out};const body=[];if(breaks){let from=0;for(const end of[...breaks,tokens.length-1]){if(from>0)body.push(tokens[from]?.gap||jsx("wbr",{},`wbr${from}`));body.push(jsx("span",{className:boxed?"text_balanced_line text_balanced_line_boxed":"text_balanced_line",children:run(from,end)},`line${from}`));from=end+1}}else{body.push(run(0,tokens.length-1))}return jsxs(Tag,{ref,...props,className:className?`text_balanced ${className}`:"text_balanced",...{[ID_ATTR]:id},children:[lead,body,tail]})};const BalancedTextScript=({nonce})=>jsx("script",{nonce,dangerouslySetInnerHTML:{__html:balancedTextScript}});export{BalancedText,BalancedTextScript};
1
+ "use client";import{jsx,jsxs}from"react/jsx-runtime";import*as React from"react";import{chooseDomBreaks,contentWidth,fontPending,ID_ATTR,releasePrehydrate,WORD_CLASS,wordSpans}from"./dom.js";import{toWords}from"./layout.js";import{createCanvasMeasurer,fontOf}from"./measure.js";import{balancedTextScript}from"./prehydrate-script.js";import"./text.css";const useSafeLayoutEffect=typeof window!=="undefined"?React.useLayoutEffect:React.useEffect;const textOf=node=>{if(typeof node==="string"||typeof node==="number")return String(node);if(React.isValidElement(node))return React.Children.toArray(node.props.children).map(textOf).join("");return""};const flatten=children=>React.Children.toArray(children).flatMap(child=>React.isValidElement(child)&&child.type===React.Fragment?flatten(child.props.children):[child]);const shapeOf=node=>{if(!React.isValidElement(node))return String(node);const{type}=node;const name=typeof type==="string"?type:type.displayName??type.name??"?";return`<${name}>${textOf(node)}`};const tokenize=children=>{const tokens=[];let gap="";let lead="";let joined=false;let pos=0;const add=(text,node,spaceBefore)=>{const last=tokens.at(-1);if(joined&&last&&!spaceBefore){last.text+=text;last.end+=text.length;last.nodes.push(node)}else{if(tokens.length===0)lead=gap;tokens.push({text,start:pos,end:pos+text.length,spaceBefore:tokens.length>0&&spaceBefore,nodes:[node],gap:tokens.length>0?gap:""})}pos+=text.length;gap="";joined=true};for(const child of flatten(children)){if(typeof child!=="string"&&typeof child!=="number"){add(textOf(child),child,gap!=="");continue}for(const chunk of String(child).split(/(\s+)/)){if(!chunk)continue;if(/^\s/.test(chunk)){gap+=chunk;joined=false;continue}toWords(chunk).forEach((word,index)=>{if(index>0)joined=false;add(word.text,word.text,index===0&&gap!=="")})}}return{tokens,lead,tail:gap}};const measurers=new Map;const measurerFor=(font,letterSpacing)=>{const key=`${font}|${letterSpacing}`;if(!measurers.has(key))measurers.set(key,createCanvasMeasurer(font,letterSpacing));return measurers.get(key)??null};const SETTLE_MS=100;const MAX_CHURN=6;const BalancedText=({children,as:Tag="span",className,...props})=>{const ref=React.useRef(null);const id=React.useId();const{tokens,lead,tail}=tokenize(children);const key=`${className??""}${tokens.map(t=>t.nodes.map(shapeOf).join("")).join("")}`;const[chosen,setChosen]=React.useState(null);const breaks=chosen?.key===key?chosen.breaks:null;const boxed=chosen?.boxed??false;const latest=React.useRef(tokens);const drawnFor=React.useRef(0);const contentSized=React.useRef(false);latest.current=tokens;useSafeLayoutEffect(()=>{const el=ref.current;if(!el)return;setChosen(c=>c&&c.key!==key?null:c);let timer;let cancelled=false;let width=contentWidth(el);let churn=0;let churnReset;let gaveUp=false;const apply=breaks2=>{if(++churn>MAX_CHURN){gaveUp=true;setChosen(null);return}clearTimeout(churnReset);churnReset=setTimeout(()=>{churn=0},1e3);drawnFor.current=width;setChosen({key,breaks:breaks2,boxed:contentSized.current})};const compute=(fontWaited=false)=>{if(gaveUp)return;const pending=fontWaited?null:fontPending(el);if(pending){pending.then(()=>{if(cancelled)return;for(const m of measurers.values())m?.clear();compute(true)},()=>{});return}if(el.querySelector(":scope > .text_balanced_line"))return;releasePrehydrate(el);if(wordSpans(el).length!==latest.current.length)return;width=contentWidth(el);const{font,letterSpacing}=fontOf(el);const measure=measurerFor(font,letterSpacing);const breaks2=measure&&chooseDomBreaks(el,measure);if(breaks2)apply(breaks2)};const recompute=()=>{setChosen(null);clearTimeout(timer);timer=setTimeout(()=>{if(!cancelled)compute()},SETTLE_MS)};compute();const observer=typeof ResizeObserver==="undefined"?null:new ResizeObserver(()=>{const next=contentWidth(el);if(next===width)return;width=next;recompute()});observer?.observe(el);return()=>{cancelled=true;clearTimeout(timer);clearTimeout(churnReset);observer?.disconnect()}},[key]);useSafeLayoutEffect(()=>{const el=ref.current;if(!el||!breaks||boxed)return;const style=getComputedStyle(el);const inner=el.clientWidth-Number.parseFloat(style.paddingLeft)-Number.parseFloat(style.paddingRight);let grew=false;if(inner>drawnFor.current+.5){const before=el.clientWidth;el.classList.add("text_balanced_probe");grew=el.clientWidth<before-.5;el.classList.remove("text_balanced_probe")}const right=el.getBoundingClientRect().right-Number.parseFloat(style.paddingRight);const lines=el.querySelectorAll(":scope > .text_balanced_line");const overflow=[...lines].some(line=>line.getBoundingClientRect().right>right+.5);if(grew)contentSized.current=true;if(grew||overflow)setChosen(c=>c?{...c,boxed:true}:c)},[chosen]);const word=i=>{const t=tokens[i];return jsx("span",{className:WORD_CLASS,children:t.nodes.map((node,j)=>jsx(React.Fragment,{children:node},j))},i)};const run=(from,to)=>{const out=[];for(let i=from;i<=to;i++){if(i>from)out.push(tokens[i]?.gap);out.push(word(i))}return out};const body=[];if(breaks){let from=0;for(const end of[...breaks,tokens.length-1]){if(from>0)body.push(tokens[from]?.gap||jsx("wbr",{},`wbr${from}`));body.push(jsx("span",{className:boxed?"text_balanced_line text_balanced_line_boxed":"text_balanced_line",children:run(from,end)},`line${from}`));from=end+1}}else{body.push(run(0,tokens.length-1))}return jsxs(Tag,{ref,...props,className:className?`text_balanced ${className}`:"text_balanced",...{[ID_ATTR]:id},children:[lead,body,tail]})};const BalancedTextScript=({nonce})=>jsx("script",{nonce,dangerouslySetInnerHTML:{__html:balancedTextScript}});export{BalancedText,BalancedTextScript};
@@ -1 +1 @@
1
- const SENTENCE_END=/(?:[^\d]\.|[!?])["')\]”’」』]*$/;const splitSentences=text=>{const sentences=[];let start=-1;let end=0;for(const match of text.matchAll(/\S+/g)){const index=match.index??0;if(start<0)start=index;end=index+match[0].length;if(SENTENCE_END.test(match[0])){sentences.push({text:text.slice(start,end),start,end});start=-1}}if(start>=0)sentences.push({text:text.slice(start,end),start,end});return sentences};const DETERMINERS=new Set(["\uC774","\uADF8","\uC800","\uD55C","\uB450","\uC138","\uB124","\uAC01","\uBAA8\uB4E0","\uCCAB","\uC0C8","\uBA87","\uC628\uAC16","\uC5EC\uB7EC","\uC5B4\uB290","\uBB34\uC2A8","\uC774\uB7F0","\uADF8\uB7F0","\uC800\uB7F0"]);const MAX_PHRASE=3;const NOUNS_ENDING_IN_UI=new Set(["\uD68C\uC758","\uB3D9\uC758","\uC8FC\uC758","\uD611\uC758","\uB17C\uC758","\uAC74\uC758","\uD569\uC758","\uBB38\uC758","\uAC15\uC758","\uC815\uC758","\uC758\uC758","\uD3B8\uC758","\uC608\uC758","\uC131\uC758","\uACB0\uC758","\uBAA8\uC758","\uC2EC\uC758","\uD1A0\uC758","\uC9C8\uC758","\uD56D\uC758"]);const bindsNext=word=>/[가-힣]의$/.test(word)&&!NOUNS_ENDING_IN_UI.has(word)&&!/[가-힣]주의$/.test(word)||DETERMINERS.has(word);const ATTACHES_PREV=/^(?:입니다|입니까|이다|이에요|예요|이죠|이고|이며|이면|(?:수|것|때|줄|뿐|듯|만큼|중)(?:가|도|는|은|을|를|이|에|에서|에는|만|이다|입니다|이에요|예요|이죠)?)[.,!?"')\]”’]*$/;const bindsPrev=word=>ATTACHES_PREV.test(word);const phraseRanges=text=>{const words=[...text.matchAll(/\S+/g)].map(m=>({word:m[0],start:m.index??0,end:(m.index??0)+m[0].length}));const ranges=[];for(let i=0;i<words.length;){let j=i;while(j<words.length-1&&j-i+1<MAX_PHRASE&&(bindsNext(words[j]?.word??"")||bindsPrev(words[j+1]?.word??"")))j++;const first=words[i];const last=words[j];if(j>i&&first&&last)ranges.push({start:first.start,end:last.end});i=j+1}return ranges};const hasFinal=ch=>(ch.charCodeAt(0)-44032)%28!==0;const isModifier=word=>{if(!/^[가-힣]{2,}$/.test(word))return false;const last=word.at(-1)??"";const stem=word.at(-2)??"";if(last==="\uB294")return hasFinal(stem)||/[하되이리오가주보지치키우두쓰뜨나내대르드]/.test(stem);if(last==="\uC740")return/[같작높낮좋많넓좁짧얇붙묶받읽남닫찾놓넣않깊밝]/.test(stem);return/[운된던]/.test(last)};const BARE_END=/[이가은는을를에의와과도만로서고며면게지요다나든야라니죠께해여어아듯]$/;const isBare=word=>/^[가-힣]+$/.test(word)&&!BARE_END.test(word)||/^[A-Za-z0-9][\w.+#-]*$/.test(word);const SPACED_PARTICLE=/^(?:이|가|은|는|을|를|의|와|과|도|만|로|으로|에|에서|에게|부터|까지|처럼|보다|마다|만큼|이나|나|라고|이라고|하고)(?:는|도|만|의)?[.,!?]*$/;const leansNext=(before,after)=>{if(!/^[가-힣\d]/.test(after))return false;return isBare(before)||isModifier(before)};const gapPenalty=(before,after)=>{if(SENTENCE_END.test(before))return 0;if(bindsNext(before)||bindsPrev(after)||after==="\xB7")return 100;if(/[A-Za-z0-9"')\]”’>]$/.test(before)&&SPACED_PARTICLE.test(after))return 100;if(/[,:;]["')\]”’]*$/.test(before))return 1;if(SENTENCE_END.test(after))return 12;if(/·$/.test(before))return 8;if(leansNext(before,after))return 6;return 4};export{gapPenalty,phraseRanges,splitSentences};
1
+ const SENTENCE_END=/(?:[^\d]\.|[!?])["')\]”’」』]*$/;const ABBREVIATION=/(?:^|[^A-Za-z])(?:[A-Za-z]\.){2,}["')\]”’」』]*$/;const endsSentence=word=>SENTENCE_END.test(word)&&!ABBREVIATION.test(word);const splitSentences=text=>{const sentences=[];let start=-1;let end=0;for(const match of text.matchAll(/\S+/g)){const index=match.index??0;if(start<0)start=index;end=index+match[0].length;if(endsSentence(match[0])){sentences.push({text:text.slice(start,end),start,end});start=-1}}if(start>=0)sentences.push({text:text.slice(start,end),start,end});return sentences};const DETERMINERS=new Set(["\uC774","\uADF8","\uC800","\uD55C","\uB450","\uC138","\uB124","\uAC01","\uBAA8\uB4E0","\uCCAB","\uC0C8","\uBA87","\uC628\uAC16","\uC5EC\uB7EC","\uC5B4\uB290","\uBB34\uC2A8","\uC774\uB7F0","\uADF8\uB7F0","\uC800\uB7F0"]);const MAX_PHRASE=3;const NOUNS_ENDING_IN_UI=new Set(["\uD68C\uC758","\uB3D9\uC758","\uC8FC\uC758","\uD611\uC758","\uB17C\uC758","\uAC74\uC758","\uD569\uC758","\uBB38\uC758","\uAC15\uC758","\uC815\uC758","\uC758\uC758","\uD3B8\uC758","\uC608\uC758","\uC131\uC758","\uACB0\uC758","\uBAA8\uC758","\uC2EC\uC758","\uD1A0\uC758","\uC9C8\uC758","\uD56D\uC758"]);const bindsNext=word=>/[가-힣]의$/.test(word)&&!NOUNS_ENDING_IN_UI.has(word)&&!/[가-힣]주의$/.test(word)||DETERMINERS.has(word);const ATTACHES_PREV=/^(?:입니다|입니까|이다|이에요|예요|이죠|이고|이며|이면|(?:수|것|때|줄|뿐|듯|만큼|중)(?:가|도|는|은|을|를|이|에|에서|에는|만|이다|입니다|이에요|예요|이죠)?)[.,!?"')\]”’]*$/;const bindsPrev=word=>ATTACHES_PREV.test(word);const phraseRanges=text=>{const words=[...text.matchAll(/\S+/g)].map(m=>({word:m[0],start:m.index??0,end:(m.index??0)+m[0].length}));const ranges=[];for(let i=0;i<words.length;){let j=i;while(j<words.length-1&&j-i+1<MAX_PHRASE&&(bindsNext(words[j]?.word??"")||bindsPrev(words[j+1]?.word??"")))j++;const first=words[i];const last=words[j];if(j>i&&first&&last)ranges.push({start:first.start,end:last.end});i=j+1}return ranges};const hasFinal=ch=>(ch.charCodeAt(0)-44032)%28!==0;const isModifier=word=>{if(!/^[가-힣]{2,}$/.test(word))return false;const last=word.at(-1)??"";const stem=word.at(-2)??"";if(last==="\uB294")return hasFinal(stem)||/[하되이리오가주보지치키우두쓰뜨나내대르드]/.test(stem);if(last==="\uC740")return/[같작높낮좋많넓좁짧얇붙묶받읽남닫찾놓넣않깊밝]/.test(stem);return/[운된던]/.test(last)};const BARE_END=/[이가은는을를에의와과도만로서고며면게지요다나든야라니죠께해여어아듯]$/;const isBare=word=>/^[가-힣]+$/.test(word)&&!BARE_END.test(word)||/^[A-Za-z0-9][\w.+#-]*$/.test(word);const SPACED_PARTICLE=/^(?:이|가|은|는|을|를|의|와|과|도|만|로|으로|에|에서|에게|부터|까지|처럼|보다|마다|만큼|이나|나|라고|이라고|하고)(?:는|도|만|의)?[.,!?]*$/;const leansNext=(before,after)=>{if(!/^[가-힣\d]/.test(after))return false;return isBare(before)||isModifier(before)};const gapPenalty=(before,after)=>{if(endsSentence(before))return 0;if(bindsNext(before)||bindsPrev(after)||after==="\xB7")return 100;if(/[A-Za-z0-9"')\]”’>]$/.test(before)&&SPACED_PARTICLE.test(after))return 100;if(/[,:;]["')\]”’]*$/.test(before))return 1;if(endsSentence(after))return 12;if(/·$/.test(before))return 8;if(leansNext(before,after))return 6;return 4};export{endsSentence,gapPenalty,phraseRanges,splitSentences};
@@ -1 +1 @@
1
- import{gapPenalty}from"./core.js";const toWords=text=>{const words=[];for(const m of text.matchAll(/\S+/g)){const start=m.index??0;let from=0;for(let i=1;i<m[0].length;i++){const prev=m[0][i-1]??"";const cur=m[0][i]??"";if(cur==="("&&/[가-힣]/.test(prev)||prev===")"&&/[가-힣]/.test(cur)){words.push({text:m[0].slice(from,i),start:start+from,end:start+i,spaceBefore:from===0});from=i}}words.push({text:m[0].slice(from),start:start+from,end:start+m[0].length,spaceBefore:from===0})}if(words[0])words[0].spaceBefore=false;return words};const lineWidths=(words,widths,space)=>{const sum=[0];words.forEach((w,i)=>{sum.push((sum[i]??0)+(widths[i]??0)+(i>0&&w.spaceBefore?space:0))});return(from,to)=>(sum[to+1]??0)-(sum[from]??0)-(from>0&&words[from]?.spaceBefore?space:0)};const layoutNative=(words,measure,maxWidth)=>{const widths=words.map(w=>w.width??measure(w.text));const space=measure(" ");const breaks=[];let width=0;for(let i=0;i<words.length;i++){const w=widths[i]??0;const gap=words[i]?.spaceBefore?space:0;if(i>0&&width+gap+w>maxWidth&&width>0){breaks.push(i-1);width=w}else width=i===0||width===0?w:width+gap+w}return breaks};const scoreBreaks=(words,measure,maxWidth,breaks)=>{const widths=words.map(w=>w.width??measure(w.text));const space=measure(" ");const costs=breakCosts(words);const lineWidth=lineWidths(words,widths,space);let penalty=0;let ragged=0;let from=0;for(const end of breaks){penalty+=costs[end]??0;const slack=(maxWidth-lineWidth(from,end))/maxWidth;ragged+=slack*slack;from=end+1}return{penalty,ragged}};const breakCosts=words=>{const closesAfter=[];let closes=0;for(let i=words.length-1;i>=0;i--){closesAfter[i]=closes;closes+=[...words[i]?.text??""].filter(ch=>ch===")").length}let depth=0;return words.map((word,i)=>{for(const ch of word.text)depth=Math.max(0,depth+(ch==="("?1:ch===")"?-1:0));const open=Math.min(depth,closesAfter[i]??0);const next=words[i+1];let penalty=gapPenalty(word.text,next?.text??"");if(penalty===6&&(words[i-1]?.text==="\xB7"||words[i+2]?.text==="\xB7"))penalty=10;if(open>0)return Math.max(60,penalty);return next?.spaceBefore===false?Math.max(50,penalty):penalty})};const RAGGED_WEIGHT=2;const layoutBest=(words,measure,maxWidth,lines)=>{const n=words.length;if(n===0||lines<1||lines>n)return null;const widths=words.map(w=>w.width??measure(w.text));const space=measure(" ");const costs=breakCosts(words);const cost=Array.from({length:lines},()=>new Array(n).fill(Number.POSITIVE_INFINITY));const from=Array.from({length:lines},()=>new Array(n).fill(-1));const lineWidth=lineWidths(words,widths,space);const fits=(a,b)=>lineWidth(a,b)<=maxWidth;const slack=(a,b)=>{const s=(maxWidth-lineWidth(a,b))/maxWidth;return s*s*RAGGED_WEIGHT};for(let j2=0;j2<n;j2++)if(fits(0,j2))cost[0][j2]=0;for(let k=1;k<lines;k++){const prev=cost[k-1];const cur=cost[k];const at=from[k];for(let j2=k;j2<n;j2++){for(let i=k;i<=j2;i++){if(!fits(i,j2))continue;const before=prev[i-1]??Number.POSITIVE_INFINITY;if(before===Number.POSITIVE_INFINITY)continue;const startOfPrev=k-1===0?0:from[k-1][i-1]??0;const c=before+(costs[i-1]??0)+slack(startOfPrev,i-1);if(c<(cur[j2]??Number.POSITIVE_INFINITY)){cur[j2]=c;at[j2]=i}}}}if(cost[lines-1][n-1]===Number.POSITIVE_INFINITY)return null;const breaks=[];let j=n-1;for(let k=lines-1;k>0;k--){const i=from[k][j]??0;breaks.unshift(i-1);j=i-1}return breaks};const chooseBreaks=(text,measure,maxWidth)=>chooseWordBreaks(toWords(text),measure,maxWidth);const chooseWordBreaks=(words,measure,maxWidth,native=layoutNative(words,measure,maxWidth))=>{const keep={breaks:native,native,changed:false};if(native.length===0||words.some(w=>(w.width??measure(w.text))>maxWidth))return keep;const best=layoutBest(words,measure,maxWidth,native.length+1);if(!best)return keep;const a=scoreBreaks(words,measure,maxWidth,native);const b=scoreBreaks(words,measure,maxWidth,best);return b.penalty<a.penalty?{breaks:best,native,changed:true}:keep};export{chooseBreaks,chooseWordBreaks,layoutBest,layoutNative,scoreBreaks,toWords};
1
+ import{endsSentence,gapPenalty}from"./core.js";const toWords=text=>{const words=[];for(const m of text.matchAll(/\S+/g)){const start=m.index??0;let from=0;for(let i=1;i<m[0].length;i++){const prev=m[0][i-1]??"";const cur=m[0][i]??"";if(cur==="("&&/[가-힣]/.test(prev)||prev===")"&&/[가-힣]/.test(cur)){words.push({text:m[0].slice(from,i),start:start+from,end:start+i,spaceBefore:from===0});from=i}}words.push({text:m[0].slice(from),start:start+from,end:start+m[0].length,spaceBefore:from===0})}if(words[0])words[0].spaceBefore=false;return words};const lineWidths=(words,widths,space)=>{const sum=[0];words.forEach((w,i)=>{sum.push((sum[i]??0)+(widths[i]??0)+(i>0&&w.spaceBefore?space:0))});return(from,to)=>(sum[to+1]??0)-(sum[from]??0)-(from>0&&words[from]?.spaceBefore?space:0)};const layoutNative=(words,measure,maxWidth)=>{const widths=words.map(w=>w.width??measure(w.text));const space=measure(" ");const breaks=[];let width=0;for(let i=0;i<words.length;i++){const w=widths[i]??0;const gap=words[i]?.spaceBefore?space:0;if(i>0&&width+gap+w>maxWidth&&width>0){breaks.push(i-1);width=w}else width=i===0||width===0?w:width+gap+w}return breaks};const scoreBreaks=(words,measure,maxWidth,breaks)=>{const widths=words.map(w=>w.width??measure(w.text));const space=measure(" ");const costs=breakCosts(words);const lineWidth=lineWidths(words,widths,space);let penalty=0;let ragged=0;let from=0;for(const end of breaks){const slack=(maxWidth-lineWidth(from,end))/maxWidth;penalty+=(costs[end]??0)+(from>0?shortLine(slack):0);ragged+=slack*slack;from=end+1}return{penalty,ragged}};const SHORT_LINE=8;const shortLine=slack=>slack>.5?SHORT_LINE:0;const breakCosts=words=>{const closesAfter=[];let closes=0;for(let i=words.length-1;i>=0;i--){closesAfter[i]=closes;closes+=[...words[i]?.text??""].filter(ch=>ch===")").length}let depth=0;return words.map((word,i)=>{for(const ch of word.text)depth=Math.max(0,depth+(ch==="("?1:ch===")"?-1:0));const open=Math.min(depth,closesAfter[i]??0);const next=words[i+1];let penalty=gapPenalty(word.text,next?.text??"");if(next&&i>0&&endsSentence(words[i-1]?.text??"")&&!endsSentence(word.text))penalty=Math.max(100,penalty);if(penalty===6&&(words[i-1]?.text==="\xB7"||words[i+2]?.text==="\xB7"))penalty=10;if(open>0)return Math.max(60,penalty);return next?.spaceBefore===false?Math.max(50,penalty):penalty})};const RAGGED_WEIGHT=2;const layoutBest=(words,measure,maxWidth,lines)=>{const n=words.length;if(n===0||lines<1||lines>n)return null;const widths=words.map(w=>w.width??measure(w.text));const space=measure(" ");const costs=breakCosts(words);const cost=Array.from({length:lines},()=>new Array(n).fill(Number.POSITIVE_INFINITY));const from=Array.from({length:lines},()=>new Array(n).fill(-1));const lineWidth=lineWidths(words,widths,space);const fits=(a,b)=>lineWidth(a,b)<=maxWidth;const lineCost=(a,b,last)=>{if(last)return 0;const s=(maxWidth-lineWidth(a,b))/maxWidth;return s*s*RAGGED_WEIGHT+(a>0?shortLine(s):0)};for(let j2=0;j2<n;j2++)if(fits(0,j2))cost[0][j2]=lineCost(0,j2,lines===1&&j2===n-1);for(let k=1;k<lines;k++){const prev=cost[k-1];const cur=cost[k];const at=from[k];for(let j2=k;j2<n;j2++){for(let i=k;i<=j2;i++){if(!fits(i,j2))continue;const before=prev[i-1]??Number.POSITIVE_INFINITY;if(before===Number.POSITIVE_INFINITY)continue;const c=before+(costs[i-1]??0)+lineCost(i,j2,k===lines-1&&j2===n-1);if(c<(cur[j2]??Number.POSITIVE_INFINITY)){cur[j2]=c;at[j2]=i}}}}if(cost[lines-1][n-1]===Number.POSITIVE_INFINITY)return null;const breaks=[];let j=n-1;for(let k=lines-1;k>0;k--){const i=from[k][j]??0;breaks.unshift(i-1);j=i-1}return breaks};const chooseBreaks=(text,measure,maxWidth)=>chooseWordBreaks(toWords(text),measure,maxWidth);const chooseWordBreaks=(words,measure,maxWidth,native=layoutNative(words,measure,maxWidth))=>{const keep={breaks:native,native,changed:false};if(native.length===0||words.some(w=>(w.width??measure(w.text))>maxWidth))return keep;const best=layoutBest(words,measure,maxWidth,native.length+1);if(!best)return keep;const a=scoreBreaks(words,measure,maxWidth,native);const b=scoreBreaks(words,measure,maxWidth,best);return b.penalty<a.penalty?{breaks:best,native,changed:true}:keep};export{chooseBreaks,chooseWordBreaks,layoutBest,layoutNative,scoreBreaks,toWords};
@@ -1 +1 @@
1
- const balancedTextScript='(function(){var e=/(?:[^\\d]\\.|[!?])["\')\\]\u201D\u2019\u300D\u300F]*$/,t=new Set([`\uC774`,`\uADF8`,`\uC800`,`\uD55C`,`\uB450`,`\uC138`,`\uB124`,`\uAC01`,`\uBAA8\uB4E0`,`\uCCAB`,`\uC0C8`,`\uBA87`,`\uC628\uAC16`,`\uC5EC\uB7EC`,`\uC5B4\uB290`,`\uBB34\uC2A8`,`\uC774\uB7F0`,`\uADF8\uB7F0`,`\uC800\uB7F0`]),n=new Set([`\uD68C\uC758`,`\uB3D9\uC758`,`\uC8FC\uC758`,`\uD611\uC758`,`\uB17C\uC758`,`\uAC74\uC758`,`\uD569\uC758`,`\uBB38\uC758`,`\uAC15\uC758`,`\uC815\uC758`,`\uC758\uC758`,`\uD3B8\uC758`,`\uC608\uC758`,`\uC131\uC758`,`\uACB0\uC758`,`\uBAA8\uC758`,`\uC2EC\uC758`,`\uD1A0\uC758`,`\uC9C8\uC758`,`\uD56D\uC758`]),r=e=>/[\uAC00-\uD7A3]\uC758$/.test(e)&&!n.has(e)&&!/[\uAC00-\uD7A3]\uC8FC\uC758$/.test(e)||t.has(e),i=/^(?:\uC785\uB2C8\uB2E4|\uC785\uB2C8\uAE4C|\uC774\uB2E4|\uC774\uC5D0\uC694|\uC608\uC694|\uC774\uC8E0|\uC774\uACE0|\uC774\uBA70|\uC774\uBA74|(?:\uC218|\uAC83|\uB54C|\uC904|\uBFD0|\uB4EF|\uB9CC\uD07C|\uC911)(?:\uAC00|\uB3C4|\uB294|\uC740|\uC744|\uB97C|\uC774|\uC5D0|\uC5D0\uC11C|\uC5D0\uB294|\uB9CC|\uC774\uB2E4|\uC785\uB2C8\uB2E4|\uC774\uC5D0\uC694|\uC608\uC694|\uC774\uC8E0)?)[.,!?"\')\\]\u201D\u2019]*$/,a=e=>i.test(e),o=e=>(e.charCodeAt(0)-44032)%28!=0,s=e=>{if(!/^[\uAC00-\uD7A3]{2,}$/.test(e))return!1;let t=e.at(-1)??``,n=e.at(-2)??``;return t===`\uB294`?o(n)||/[\uD558\uB418\uC774\uB9AC\uC624\uAC00\uC8FC\uBCF4\uC9C0\uCE58\uD0A4\uC6B0\uB450\uC4F0\uB728\uB098\uB0B4\uB300\uB974\uB4DC]/.test(n):t===`\uC740`?/[\uAC19\uC791\uB192\uB0AE\uC88B\uB9CE\uB113\uC881\uC9E7\uC587\uBD99\uBB36\uBC1B\uC77D\uB0A8\uB2EB\uCC3E\uB193\uB123\uC54A\uAE4A\uBC1D]/.test(n):/[\uC6B4\uB41C\uB358]/.test(t)},c=/[\uC774\uAC00\uC740\uB294\uC744\uB97C\uC5D0\uC758\uC640\uACFC\uB3C4\uB9CC\uB85C\uC11C\uACE0\uBA70\uBA74\uAC8C\uC9C0\uC694\uB2E4\uB098\uB4E0\uC57C\uB77C\uB2C8\uC8E0\uAED8\uD574\uC5EC\uC5B4\uC544\uB4EF]$/,l=e=>/^[\uAC00-\uD7A3]+$/.test(e)&&!c.test(e)||/^[A-Za-z0-9][\\w.+#-]*$/.test(e),u=/^(?:\uC774|\uAC00|\uC740|\uB294|\uC744|\uB97C|\uC758|\uC640|\uACFC|\uB3C4|\uB9CC|\uB85C|\uC73C\uB85C|\uC5D0|\uC5D0\uC11C|\uC5D0\uAC8C|\uBD80\uD130|\uAE4C\uC9C0|\uCC98\uB7FC|\uBCF4\uB2E4|\uB9C8\uB2E4|\uB9CC\uD07C|\uC774\uB098|\uB098|\uB77C\uACE0|\uC774\uB77C\uACE0|\uD558\uACE0)(?:\uB294|\uB3C4|\uB9CC|\uC758)?[.,!?]*$/,d=(e,t)=>/^[\uAC00-\uD7A3\\d]/.test(t)?l(e)||s(e):!1,f=(t,n)=>e.test(t)?0:r(t)||a(n)||n===`\xB7`||/[A-Za-z0-9"\')\\]\u201D\u2019>]$/.test(t)&&u.test(n)?100:/[,:;]["\')\\]\u201D\u2019]*$/.test(t)?1:e.test(n)?12:/\xB7$/.test(t)?8:d(t,n)?6:4,p=(e,t,n)=>{let r=[0];return e.forEach((e,i)=>{r.push((r[i]??0)+(t[i]??0)+(i>0&&e.spaceBefore?n:0))}),(t,i)=>(r[i+1]??0)-(r[t]??0)-(t>0&&e[t]?.spaceBefore?n:0)},m=(e,t,n)=>{let r=e.map(e=>e.width??t(e.text)),i=t(` `),a=[],o=0;for(let t=0;t<e.length;t++){let s=r[t]??0,c=e[t]?.spaceBefore?i:0;t>0&&o+c+s>n&&o>0?(a.push(t-1),o=s):o=t===0||o===0?s:o+c+s}return a},h=(e,t,n,r)=>{let i=e.map(e=>e.width??t(e.text)),a=t(` `),o=g(e),s=p(e,i,a),c=0,l=0,u=0;for(let e of r){c+=o[e]??0;let t=(n-s(u,e))/n;l+=t*t,u=e+1}return{penalty:c,ragged:l}},g=e=>{let t=[],n=0;for(let r=e.length-1;r>=0;r--)t[r]=n,n+=[...e[r]?.text??``].filter(e=>e===`)`).length;let r=0;return e.map((n,i)=>{for(let e of n.text)r=Math.max(0,r+(e===`(`?1:e===`)`?-1:0));let a=Math.min(r,t[i]??0),o=e[i+1],s=f(n.text,o?.text??``);return s===6&&(e[i-1]?.text===`\xB7`||e[i+2]?.text===`\xB7`)&&(s=10),a>0?Math.max(60,s):o?.spaceBefore===!1?Math.max(50,s):s})},_=2,v=(e,t,n,r)=>{let i=e.length;if(i===0||r<1||r>i)return null;let a=e.map(e=>e.width??t(e.text)),o=t(` `),s=g(e),c=Array.from({length:r},()=>Array(i).fill(1/0)),l=Array.from({length:r},()=>Array(i).fill(-1)),u=p(e,a,o),d=(e,t)=>u(e,t)<=n,f=(e,t)=>{let r=(n-u(e,t))/n;return r*r*_};for(let e=0;e<i;e++)d(0,e)&&(c[0][e]=0);for(let e=1;e<r;e++){let t=c[e-1],n=c[e],r=l[e];for(let a=e;a<i;a++)for(let i=e;i<=a;i++){if(!d(i,a))continue;let o=t[i-1]??1/0;if(o===1/0)continue;let c=e-1==0?0:l[e-1][i-1]??0,u=o+(s[i-1]??0)+f(c,i-1);u<(n[a]??1/0)&&(n[a]=u,r[a]=i)}}if(c[r-1][i-1]===1/0)return null;let m=[],h=i-1;for(let e=r-1;e>0;e--){let t=l[e][h]??0;m.unshift(t-1),h=t-1}return m},y=(e,t,n,r=m(e,t,n))=>{let i={breaks:r,native:r,changed:!1};if(r.length===0||e.some(e=>(e.width??t(e.text))>n))return i;let a=v(e,t,n,r.length+1);if(!a)return i;let o=h(e,t,n,r);return h(e,t,n,a).penalty<o.penalty?{breaks:a,native:r,changed:!0}:i},b=(e,t=0)=>{if(typeof document>`u`)return null;let n=document.createElement(`canvas`).getContext(`2d`);if(!n)return null;n.font=e;let r=new Map,i=(e=>{let i=r.get(e);if(i!==void 0)return i;let a=n.measureText(e).width+t*[...e].length;return r.set(e,a),a});return i.clear=()=>r.clear(),i},x=e=>{let t=getComputedStyle(e),n=Number.parseFloat(t.letterSpacing);return{font:`${t.fontStyle} ${t.fontWeight} ${t.fontSize} ${t.fontFamily}`,letterSpacing:Number.isFinite(n)?n:0}},S=`text_balanced_word`,C=`data-balanced`,w=`bt-balanced-prehydrate`,T=e=>`[${C}="${e.replace(/["\\\\]/g,`\\\\$&`)}"]`,E=e=>{let t=getComputedStyle(e);return e.clientWidth-Number.parseFloat(t.paddingLeft)-Number.parseFloat(t.paddingRight)},D=e=>[...e.querySelectorAll(`.${S}`)].filter(t=>!e.contains(t.parentElement?.closest(`.text_balanced_word`)??null)),O=e=>{let t=``;for(let n=e.previousSibling;n&&!(n instanceof Element&&n.classList.contains(`text_balanced_word`));n=n.previousSibling)t+=n.textContent??``;return/\\s/.test(t)},k=e=>{let t=[...e.getClientRects()];if(new Set(t.map(e=>Math.round(e.top))).size<2)return t.reduce((e,t)=>e+t.width,0);e.classList.add(`text_balanced_word_measure`);let n=e.getBoundingClientRect().width;return e.classList.remove(`text_balanced_word_measure`),n},A=(e,t)=>{let n=E(e),r=D(e);if(n<=0||r.length===0)return null;let i=n-1,a=r.map((e,t)=>({text:e.textContent??``,start:0,end:0,spaceBefore:t>0&&O(e),width:k(e)}));if(a.some(e=>(e.width??0)>i))return null;let o=0,s=r.map(e=>{let t=[...e.getClientRects()].map(e=>Math.round(e.top));return t.length===0?[o]:(o=t.at(-1)??o,t)}),c=new Set(s.flat()).size,l=s.some(e=>new Set(e).size>1),u=m(a,t,i).length+1,d=e=>{let n=v(a,t,i,e);return n&&h(a,t,i,n).penalty<100?n:null};if(l||u<c){let e=d(Math.min(u,c));if(e)return e;if(l)return d(c)??d(c+1)}let f=s.flatMap((e,t)=>t>0&&e[0]!==s[t-1]?.[0]?[t-1]:[]),p=y(a,t,i,f).breaks;return h(a,t,i,p).penalty>=100?d(f.length+2)??p:p},j=e=>{let t=typeof document>`u`?void 0:document.fonts;if(!t?.check)return null;let{font:n}=x(e),r=(e.textContent??``).slice(0,64);try{return t.check(n,r)?null:t.load(n,r)}catch{return null}},M=e=>{let t=e.getAttribute(C),n=document.getElementById(w)?.sheet;if(!t||!n)return;let r=T(t);for(let e=n.cssRules.length-1;e>=0;e--)n.cssRules[e].selectorText?.startsWith(r)&&n.deleteRule(e)};(()=>{let e=document,t=e.getElementById(w);t||(t=e.createElement(`style`),t.id=w,e.head.appendChild(t));let n=t.sheet;if(!n)return;let r=e=>{let t=e.getAttribute(C);if(!t||e.querySelector(`:scope > .text_balanced_line`))return;let r=j(e);if(r){r.catch(()=>{});return}let{font:i,letterSpacing:a}=x(e),o=b(i,a),s=o&&A(e,o);if(!s?.length)return;let c=T(t),l=e.clientWidth;try{n.insertRule(`${c}{white-space:nowrap}`,n.cssRules.length),n.insertRule(`${c} wbr{display:none}`,n.cssRules.length);for(let e of s)n.insertRule(`${c}>.${S}:nth-child(${e+1} of .${S})::after{content:"\\\\A";white-space:pre}`,n.cssRules.length)}catch{M(e);return}let u=e.getBoundingClientRect().right-Number.parseFloat(getComputedStyle(e).paddingRight),d=e.querySelectorAll(`:scope > .${S}`);(e.clientWidth>l+.5||[...d].some(e=>e.getBoundingClientRect().right>u+.5))&&M(e)};for(let t of e.querySelectorAll(`[${C}]`))r(t);addEventListener(`resize`,()=>t?.remove(),{once:!0})})()})();';export{balancedTextScript};
1
+ const balancedTextScript='(function(){var e=/(?:[^\\d]\\.|[!?])["\')\\]\u201D\u2019\u300D\u300F]*$/,t=/(?:^|[^A-Za-z])(?:[A-Za-z]\\.){2,}["\')\\]\u201D\u2019\u300D\u300F]*$/,n=n=>e.test(n)&&!t.test(n),r=new Set([`\uC774`,`\uADF8`,`\uC800`,`\uD55C`,`\uB450`,`\uC138`,`\uB124`,`\uAC01`,`\uBAA8\uB4E0`,`\uCCAB`,`\uC0C8`,`\uBA87`,`\uC628\uAC16`,`\uC5EC\uB7EC`,`\uC5B4\uB290`,`\uBB34\uC2A8`,`\uC774\uB7F0`,`\uADF8\uB7F0`,`\uC800\uB7F0`]),i=new Set([`\uD68C\uC758`,`\uB3D9\uC758`,`\uC8FC\uC758`,`\uD611\uC758`,`\uB17C\uC758`,`\uAC74\uC758`,`\uD569\uC758`,`\uBB38\uC758`,`\uAC15\uC758`,`\uC815\uC758`,`\uC758\uC758`,`\uD3B8\uC758`,`\uC608\uC758`,`\uC131\uC758`,`\uACB0\uC758`,`\uBAA8\uC758`,`\uC2EC\uC758`,`\uD1A0\uC758`,`\uC9C8\uC758`,`\uD56D\uC758`]),a=e=>/[\uAC00-\uD7A3]\uC758$/.test(e)&&!i.has(e)&&!/[\uAC00-\uD7A3]\uC8FC\uC758$/.test(e)||r.has(e),o=/^(?:\uC785\uB2C8\uB2E4|\uC785\uB2C8\uAE4C|\uC774\uB2E4|\uC774\uC5D0\uC694|\uC608\uC694|\uC774\uC8E0|\uC774\uACE0|\uC774\uBA70|\uC774\uBA74|(?:\uC218|\uAC83|\uB54C|\uC904|\uBFD0|\uB4EF|\uB9CC\uD07C|\uC911)(?:\uAC00|\uB3C4|\uB294|\uC740|\uC744|\uB97C|\uC774|\uC5D0|\uC5D0\uC11C|\uC5D0\uB294|\uB9CC|\uC774\uB2E4|\uC785\uB2C8\uB2E4|\uC774\uC5D0\uC694|\uC608\uC694|\uC774\uC8E0)?)[.,!?"\')\\]\u201D\u2019]*$/,s=e=>o.test(e),c=e=>(e.charCodeAt(0)-44032)%28!=0,l=e=>{if(!/^[\uAC00-\uD7A3]{2,}$/.test(e))return!1;let t=e.at(-1)??``,n=e.at(-2)??``;return t===`\uB294`?c(n)||/[\uD558\uB418\uC774\uB9AC\uC624\uAC00\uC8FC\uBCF4\uC9C0\uCE58\uD0A4\uC6B0\uB450\uC4F0\uB728\uB098\uB0B4\uB300\uB974\uB4DC]/.test(n):t===`\uC740`?/[\uAC19\uC791\uB192\uB0AE\uC88B\uB9CE\uB113\uC881\uC9E7\uC587\uBD99\uBB36\uBC1B\uC77D\uB0A8\uB2EB\uCC3E\uB193\uB123\uC54A\uAE4A\uBC1D]/.test(n):/[\uC6B4\uB41C\uB358]/.test(t)},u=/[\uC774\uAC00\uC740\uB294\uC744\uB97C\uC5D0\uC758\uC640\uACFC\uB3C4\uB9CC\uB85C\uC11C\uACE0\uBA70\uBA74\uAC8C\uC9C0\uC694\uB2E4\uB098\uB4E0\uC57C\uB77C\uB2C8\uC8E0\uAED8\uD574\uC5EC\uC5B4\uC544\uB4EF]$/,d=e=>/^[\uAC00-\uD7A3]+$/.test(e)&&!u.test(e)||/^[A-Za-z0-9][\\w.+#-]*$/.test(e),f=/^(?:\uC774|\uAC00|\uC740|\uB294|\uC744|\uB97C|\uC758|\uC640|\uACFC|\uB3C4|\uB9CC|\uB85C|\uC73C\uB85C|\uC5D0|\uC5D0\uC11C|\uC5D0\uAC8C|\uBD80\uD130|\uAE4C\uC9C0|\uCC98\uB7FC|\uBCF4\uB2E4|\uB9C8\uB2E4|\uB9CC\uD07C|\uC774\uB098|\uB098|\uB77C\uACE0|\uC774\uB77C\uACE0|\uD558\uACE0)(?:\uB294|\uB3C4|\uB9CC|\uC758)?[.,!?]*$/,p=(e,t)=>/^[\uAC00-\uD7A3\\d]/.test(t)?d(e)||l(e):!1,m=(e,t)=>n(e)?0:a(e)||s(t)||t===`\xB7`||/[A-Za-z0-9"\')\\]\u201D\u2019>]$/.test(e)&&f.test(t)?100:/[,:;]["\')\\]\u201D\u2019]*$/.test(e)?1:n(t)?12:/\xB7$/.test(e)?8:p(e,t)?6:4,h=(e,t,n)=>{let r=[0];return e.forEach((e,i)=>{r.push((r[i]??0)+(t[i]??0)+(i>0&&e.spaceBefore?n:0))}),(t,i)=>(r[i+1]??0)-(r[t]??0)-(t>0&&e[t]?.spaceBefore?n:0)},g=(e,t,n)=>{let r=e.map(e=>e.width??t(e.text)),i=t(` `),a=[],o=0;for(let t=0;t<e.length;t++){let s=r[t]??0,c=e[t]?.spaceBefore?i:0;t>0&&o+c+s>n&&o>0?(a.push(t-1),o=s):o=t===0||o===0?s:o+c+s}return a},_=(e,t,n,r)=>{let i=e.map(e=>e.width??t(e.text)),a=t(` `),o=b(e),s=h(e,i,a),c=0,l=0,u=0;for(let e of r){let t=(n-s(u,e))/n;c+=(o[e]??0)+(u>0?y(t):0),l+=t*t,u=e+1}return{penalty:c,ragged:l}},v=8,y=e=>e>.5?v:0,b=e=>{let t=[],r=0;for(let n=e.length-1;n>=0;n--)t[n]=r,r+=[...e[n]?.text??``].filter(e=>e===`)`).length;let i=0;return e.map((r,a)=>{for(let e of r.text)i=Math.max(0,i+(e===`(`?1:e===`)`?-1:0));let o=Math.min(i,t[a]??0),s=e[a+1],c=m(r.text,s?.text??``);return s&&a>0&&n(e[a-1]?.text??``)&&!n(r.text)&&(c=Math.max(100,c)),c===6&&(e[a-1]?.text===`\xB7`||e[a+2]?.text===`\xB7`)&&(c=10),o>0?Math.max(60,c):s?.spaceBefore===!1?Math.max(50,c):c})},x=2,S=(e,t,n,r)=>{let i=e.length;if(i===0||r<1||r>i)return null;let a=e.map(e=>e.width??t(e.text)),o=t(` `),s=b(e),c=Array.from({length:r},()=>Array(i).fill(1/0)),l=Array.from({length:r},()=>Array(i).fill(-1)),u=h(e,a,o),d=(e,t)=>u(e,t)<=n,f=(e,t,r)=>{if(r)return 0;let i=(n-u(e,t))/n;return i*i*x+(e>0?y(i):0)};for(let e=0;e<i;e++)d(0,e)&&(c[0][e]=f(0,e,r===1&&e===i-1));for(let e=1;e<r;e++){let t=c[e-1],n=c[e],a=l[e];for(let o=e;o<i;o++)for(let c=e;c<=o;c++){if(!d(c,o))continue;let l=t[c-1]??1/0;if(l===1/0)continue;let u=l+(s[c-1]??0)+f(c,o,e===r-1&&o===i-1);u<(n[o]??1/0)&&(n[o]=u,a[o]=c)}}if(c[r-1][i-1]===1/0)return null;let p=[],m=i-1;for(let e=r-1;e>0;e--){let t=l[e][m]??0;p.unshift(t-1),m=t-1}return p},C=(e,t,n,r=g(e,t,n))=>{let i={breaks:r,native:r,changed:!1};if(r.length===0||e.some(e=>(e.width??t(e.text))>n))return i;let a=S(e,t,n,r.length+1);if(!a)return i;let o=_(e,t,n,r);return _(e,t,n,a).penalty<o.penalty?{breaks:a,native:r,changed:!0}:i},w=(e,t=0)=>{if(typeof document>`u`)return null;let n=document.createElement(`canvas`).getContext(`2d`);if(!n)return null;n.font=e;let r=new Map,i=(e=>{let i=r.get(e);if(i!==void 0)return i;let a=n.measureText(e).width+t*[...e].length;return r.set(e,a),a});return i.clear=()=>r.clear(),i},T=e=>{let t=getComputedStyle(e),n=Number.parseFloat(t.letterSpacing);return{font:`${t.fontStyle} ${t.fontWeight} ${t.fontSize} ${t.fontFamily}`,letterSpacing:Number.isFinite(n)?n:0}},E=`text_balanced_word`,D=`data-balanced`,O=`bt-balanced-prehydrate`,k=e=>`[${D}="${e.replace(/["\\\\]/g,`\\\\$&`)}"]`,A=e=>{let t=getComputedStyle(e);return e.clientWidth-Number.parseFloat(t.paddingLeft)-Number.parseFloat(t.paddingRight)},j=e=>[...e.querySelectorAll(`.${E}`)].filter(t=>!e.contains(t.parentElement?.closest(`.text_balanced_word`)??null)),M=e=>{let t=``;for(let n=e.previousSibling;n&&!(n instanceof Element&&n.classList.contains(`text_balanced_word`));n=n.previousSibling)t+=n.textContent??``;return/\\s/.test(t)},N=e=>{let t=[...e.getClientRects()];if(new Set(t.map(e=>Math.round(e.top))).size<2)return t.reduce((e,t)=>e+t.width,0);e.classList.add(`text_balanced_word_measure`);let n=e.getBoundingClientRect().width;return e.classList.remove(`text_balanced_word_measure`),n},P=(e,t)=>{let n=A(e),r=j(e);if(n<=0||r.length===0)return null;let i=n-1,a=r.map((e,t)=>({text:e.textContent??``,start:0,end:0,spaceBefore:t>0&&M(e),width:N(e)}));if(a.some(e=>(e.width??0)>i))return null;let o=0,s=r.map(e=>{let t=[...e.getClientRects()].map(e=>Math.round(e.top));return t.length===0?[o]:(o=t.at(-1)??o,t)}),c=new Set(s.flat()).size,l=s.some(e=>new Set(e).size>1),u=g(a,t,i).length+1,d=e=>{let n=S(a,t,i,e);return n&&_(a,t,i,n).penalty<100?n:null};if(l||u<c){let e=d(Math.min(u,c));if(e)return e;if(l)return d(c)??d(c+1)}let f=s.flatMap((e,t)=>t>0&&e[0]!==s[t-1]?.[0]?[t-1]:[]),p=C(a,t,i,f).breaks;return _(a,t,i,p).penalty>=100?d(f.length+2)??p:p},F=e=>{let t=typeof document>`u`?void 0:document.fonts;if(!t?.check)return null;let{font:n}=T(e),r=(e.textContent??``).slice(0,64);try{return t.check(n,r)?null:t.load(n,r)}catch{return null}},I=e=>{let t=e.getAttribute(D),n=document.getElementById(O)?.sheet;if(!t||!n)return;let r=k(t);for(let e=n.cssRules.length-1;e>=0;e--)n.cssRules[e].selectorText?.startsWith(r)&&n.deleteRule(e)};(()=>{let e=document,t=e.getElementById(O);t||(t=e.createElement(`style`),t.id=O,e.head.appendChild(t));let n=t.sheet;if(!n)return;let r=e=>{let t=e.getAttribute(D);if(!t||e.querySelector(`:scope > .text_balanced_line`))return;let r=F(e);if(r){r.catch(()=>{});return}let{font:i,letterSpacing:a}=T(e),o=w(i,a),s=o&&P(e,o);if(!s?.length)return;let c=k(t),l=e.clientWidth;try{n.insertRule(`${c}{white-space:nowrap}`,n.cssRules.length),n.insertRule(`${c} wbr{display:none}`,n.cssRules.length);for(let e of s)n.insertRule(`${c}>.${E}:nth-child(${e+1} of .${E})::after{content:"\\\\A";white-space:pre}`,n.cssRules.length)}catch{I(e);return}let u=e.getBoundingClientRect().right-Number.parseFloat(getComputedStyle(e).paddingRight),d=e.querySelectorAll(`:scope > .${E}`);(e.clientWidth>l+.5||[...d].some(e=>e.getBoundingClientRect().right>u+.5))&&I(e)};for(let t of e.querySelectorAll(`[${D}]`))r(t);addEventListener(`resize`,()=>t?.remove(),{once:!0})})()})();';export{balancedTextScript};
@@ -1,4 +1,4 @@
1
1
  @charset "UTF-8";
2
2
  @layer theme, base, reset, bigtablet, components, utilities;
3
3
  @layer bigtablet {
4
- .sentences_item{display:inline-block}.sentences_phrase{display:inline-block}.text_balanced{text-wrap:pretty}.text_balanced_line{white-space:nowrap}.text_balanced_line wbr,.text_balanced_word_measure wbr{display:none}.text_balanced_word_measure{white-space:nowrap}.text_balanced_line_boxed,.text_balanced_probe>.text_balanced_line{display:inline-block;white-space:normal}}
4
+ .sentences_item{display:inline-block}.sentences_phrase{display:inline-block}.text_balanced{text-wrap:pretty}:where(span).text_balanced{display:block}.text_balanced_line{white-space:nowrap}.text_balanced_line wbr,.text_balanced_word_measure wbr{display:none}.text_balanced_word_measure{white-space:nowrap}.text_balanced_line_boxed,.text_balanced_probe>.text_balanced_line{display:inline-block;white-space:normal}}