forked from apache/superset
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request apache#76 from kristw/kristw/cherry-pick-big-number
Cherry-pick big number changes and code cleanup
- Loading branch information
Showing
10 changed files
with
1,285 additions
and
1,518 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,105 @@ | ||
export function getTextWidth(text, fontDetails = '12px Roboto') { | ||
const canvas = document.createElement('canvas'); | ||
const context = canvas.getContext('2d'); | ||
if (context) { | ||
// Won't work outside of a browser context (ie unit tests) | ||
context.font = fontDetails; | ||
return context.measureText(text).width; | ||
const SVG_NS = 'http://www.w3.org/2000/svg'; | ||
|
||
function isDefined(x) { | ||
return x !== null && x !== undefined; | ||
} | ||
|
||
export function getTextDimension({ | ||
text, | ||
className, | ||
style, | ||
container = document.body, | ||
}) { | ||
const textNode = document.createElementNS(SVG_NS, 'text'); | ||
textNode.textContent = text; | ||
|
||
if (isDefined(className)) { | ||
textNode.setAttribute('class', className); | ||
} | ||
|
||
if (isDefined(style)) { | ||
['font', 'fontWeight', 'fontStyle', 'fontSize', 'fontFamily'] | ||
.filter(field => isDefined(style[field])) | ||
.forEach((field) => { | ||
textNode.style[field] = style[field]; | ||
}); | ||
} | ||
|
||
const svg = document.createElementNS(SVG_NS, 'svg'); | ||
svg.style.position = 'absolute'; // so it won't disrupt page layout | ||
svg.style.opacity = 0; // and not visible | ||
svg.appendChild(textNode); | ||
container.appendChild(svg); | ||
let result; | ||
if (textNode.getBBox) { | ||
const bbox = textNode.getBBox(); | ||
// round up | ||
result = { | ||
width: Math.ceil(bbox.width), | ||
height: Math.ceil(bbox.height), | ||
}; | ||
} else { | ||
// Handle when called from non-browser and do not support getBBox() | ||
result = { | ||
width: 100, | ||
height: 100, | ||
}; | ||
} | ||
return 100; | ||
container.removeChild(svg); | ||
return result; | ||
} | ||
|
||
/** | ||
* Shim to support legacy calls | ||
*/ | ||
export function getTextWidth(text, font = '12px Roboto') { | ||
return getTextDimension({ text, style: { font } }).width; | ||
} | ||
|
||
export default { | ||
getTextWidth, | ||
}; | ||
export function computeMaxFontSize({ | ||
text, | ||
idealFontSize, | ||
maxWidth, | ||
maxHeight, | ||
className, | ||
style, | ||
container, | ||
}) { | ||
let size = idealFontSize; | ||
if (!isDefined(idealFontSize)) { | ||
if (isDefined(maxHeight)) { | ||
size = Math.floor(maxHeight); | ||
} else { | ||
throw new Error('You must specify at least one of maxHeight or idealFontSize'); | ||
} | ||
} | ||
|
||
function computeDimension(fontSize) { | ||
return getTextDimension({ | ||
text, | ||
className, | ||
style: { ...style, fontSize }, | ||
container, | ||
}); | ||
} | ||
|
||
let textDimension = computeDimension(size); | ||
|
||
// Decrease size until textWidth is less than maxWidth | ||
if (isDefined(maxWidth)) { | ||
while (textDimension.width > maxWidth) { | ||
size -= 2; | ||
textDimension = computeDimension(size); | ||
} | ||
} | ||
|
||
// Decrease size until textHeight is less than maxHeight | ||
if (isDefined(maxHeight)) { | ||
while (textDimension.height > maxHeight) { | ||
size -= 2; | ||
textDimension = computeDimension(size); | ||
} | ||
} | ||
|
||
return size; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.