MCPcopy Create free account
hub / github.com/MankaiHuang/screen-visualization / getTextInfo

Function getTextInfo

public/iframe/lib/js/echarts-wordcloud.js:644–818  ·  view source on GitHub ↗
(word, weight, rotateDeg)

Source from the content-addressed store, hash-verified

642 };
643
644 var getTextInfo = function getTextInfo(word, weight, rotateDeg) {
645 // calculate the acutal font size
646 // fontSize === 0 means weightFactor function wants the text skipped,
647 // and size < minSize means we cannot draw the text.
648 var debug = false;
649 var fontSize = settings.weightFactor(weight);
650 if (fontSize <= settings.minSize) {
651 return false;
652 }
653
654 // Scale factor here is to make sure fillText is not limited by
655 // the minium font size set by browser.
656 // It will always be 1 or 2n.
657 var mu = 1;
658 if (fontSize < minFontSize) {
659 mu = (function calculateScaleFactor() {
660 var mu = 2;
661 while (mu * fontSize < minFontSize) {
662 mu += 2;
663 }
664 return mu;
665 })();
666 }
667
668 var fcanvas = document.createElement('canvas');
669 var fctx = fcanvas.getContext('2d', { willReadFrequently: true });
670
671 fctx.font = settings.fontWeight + ' ' +
672 (fontSize * mu).toString(10) + 'px ' + settings.fontFamily;
673
674 // Estimate the dimension of the text with measureText().
675 var fw = fctx.measureText(word).width / mu;
676 var fh = Math.max(fontSize * mu,
677 fctx.measureText('m').width,
678 fctx.measureText('\uFF37').width) / mu;
679
680 // Create a boundary box that is larger than our estimates,
681 // so text don't get cut of (it sill might)
682 var boxWidth = fw + fh * 2;
683 var boxHeight = fh * 3;
684 var fgw = Math.ceil(boxWidth / g);
685 var fgh = Math.ceil(boxHeight / g);
686 boxWidth = fgw * g;
687 boxHeight = fgh * g;
688
689 // Calculate the proper offsets to make the text centered at
690 // the preferred position.
691
692 // This is simply half of the width.
693 var fillTextOffsetX = - fw / 2;
694 // Instead of moving the box to the exact middle of the preferred
695 // position, for Y-offset we move 0.4 instead, so Latin alphabets look
696 // vertical centered.
697 var fillTextOffsetY = - fh * 0.4;
698
699 // Calculate the actual dimension of the canvas, considering the rotation.
700 var cgh = Math.ceil((boxWidth * Math.abs(Math.sin(rotateDeg)) +
701 boxHeight * Math.abs(Math.cos(rotateDeg))) / g);

Callers 1

putWordFunction · 0.85

Calls 1

exceedTimeFunction · 0.85

Tested by

no test coverage detected