diff --git a/pages/Truncate.html b/pages/Truncate.html index 4fd8381..bb74247 100644 --- a/pages/Truncate.html +++ b/pages/Truncate.html @@ -21,7 +21,7 @@ .TextBox() .data([d]) .select(this) - .fontResize(true) + .fontResize(d.resize) .height(radius) .width(radius) .text(d => d.name) @@ -35,9 +35,9 @@ }; const circles = [ - { x: 150, y: 150, name: "ABCDEFGHIJKLMNOPQRSTUVWXYZ12" }, - { x: 500, y: 150, name: "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789" }, - { x: 150, y: 500, name: "Bacon ipsum dolor amet venison ball tip meatloaf, shank sirloin pig filet mignon pastrami pork belly pork chop. Strip steak ham spare ribs sausage. Fatback venison burgdoggen bacon pancetta kevin shoulder porchetta spare ribs. Venison tongue short loin, pork belly pork loin jowl ham hock t-bone sausage doner. Rump bacon beef ribs short loin brisket buffalo turducken beef. Cupim swine burgdoggen kevin. Boudin tongue tri-tip sirloin, biltong short ribs pancetta pork loin. Boudin short loin brisket, shoulder porchetta biltong short ribs drumstick ground round jerky kevin andouille. Ham ribeye swine pork loin pork chop tenderloin, shoulder pork salami sausage ham hock pancetta. Tail andouille ham strip steak meatball ribeye. Bresaola pork chop tongue tri-tip. Pig tail ground round pork belly beef ribs meatball turducken kevin ball tip capicola salami pork chop hamburger brisket. Beef ribs venison turducken doner shankle landjaeger salami biltong cupim beef. Short loin kevin chicken andouille capicola tongue. Porchetta shank pastrami sirloin, fatback venison tri-tip." }, + { x: 150, y: 150, resize: true, name: "ABCDEFGHIJKLMNOPQRSTUVWXYZ12" }, + { x: 500, y: 150, resize: true, name: "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789" }, + { x: 150, y: 500, resize: true, name: "Bacon ipsum dolor amet venison ball tip meatloaf, shank sirloin pig filet mignon pastrami pork belly pork chop. Strip steak ham spare ribs sausage. Fatback venison burgdoggen bacon pancetta kevin shoulder porchetta spare ribs. Venison tongue short loin, pork belly pork loin jowl ham hock t-bone sausage doner. Rump bacon beef ribs short loin brisket buffalo turducken beef. Cupim swine burgdoggen kevin. Boudin tongue tri-tip sirloin, biltong short ribs pancetta pork loin. Boudin short loin brisket, shoulder porchetta biltong short ribs drumstick ground round jerky kevin andouille. Ham ribeye swine pork loin pork chop tenderloin, shoulder pork salami sausage ham hock pancetta. Tail andouille ham strip steak meatball ribeye. Bresaola pork chop tongue tri-tip. Pig tail ground round pork belly beef ribs meatball turducken kevin ball tip capicola salami pork chop hamburger brisket. Beef ribs venison turducken doner shankle landjaeger salami biltong cupim beef. Short loin kevin chicken andouille capicola tongue. Porchetta shank pastrami sirloin, fatback venison tri-tip." }, ]; const join = d3 diff --git a/src/TextBox.js b/src/TextBox.js index 37fd501..abebe3b 100644 --- a/src/TextBox.js +++ b/src/TextBox.js @@ -15,6 +15,7 @@ import strip from "./strip"; import textSplit from "./textSplit"; import measure from "./textWidth"; import wrap from "./textWrap"; +import truncateWord from "./textTruncate"; import {trimRight} from "./trim"; /** @@ -120,12 +121,14 @@ export default class TextBox extends BaseClass { @private */ function checkSize() { + const truncate = () => { + if (line < 1) lineData = [truncateWord(wrapResults.words[0], that._ellipsis("", line), w, style)]; + else lineData[line - 1] = that._ellipsis(lineData[line - 1], line); + }; - if (fS < fMin) { - lineData = []; - return; - } - else if (fS > fMax) fS = fMax; + // Constraint the font size + fS = Math.max(fS, fMin); + fS = Math.min(fS, fMax); if (resize) { lH = fS * lHRatio; @@ -141,18 +144,17 @@ export default class TextBox extends BaseClass { line = lineData.length; if (wrapResults.truncated) { - if (resize) { fS--; - if (fS < fMin) lineData = []; + if (fS < fMin) { + fS = fMin; + truncate(); + return; + } else checkSize(); } - else if (line < 1) lineData = [that._ellipsis("", line)]; - else lineData[line - 1] = that._ellipsis(lineData[line - 1], line); - + else truncate(); } - - } if (w > fMin && (h > lH || resize && h > fMin * lHRatio)) {