misc: handle svg text revert back to one line with better scaling

This commit is contained in:
vicnaum
2023-11-15 18:18:39 +01:00
parent 7f30cc04d3
commit b1f888757c
2 changed files with 45 additions and 20 deletions

View File

@@ -2,9 +2,11 @@
pragma solidity ^0.8.0;
import {GintoNordFontSVG} from './GintoNordFontSVG.sol';
import {Strings} from '@openzeppelin/contracts/utils/Strings.sol';
library HandleSVG {
uint256 constant MAX_WIDTH = 245;
using Strings for uint256;
uint256 constant MAX_WIDTH = 275;
enum FaceColors {
GREEN,
@@ -102,26 +104,24 @@ library HandleSVG {
}
function getTextElement(string memory localName) internal pure returns (string memory) {
// 21px is the @ width
if (getTextWidth(localName) <= MAX_WIDTH - 21) {
return
string.concat(
'<text fill="black" xml:space="preserve" style="white-space: pre" x="50%" y="60" text-anchor="middle" font-family="Ginto Nord Medium" font-size="20" font-weight="500" letter-spacing="-0.7px">@',
localName,
'</text>'
);
} else {
(string memory line1, string memory line2) = splitTextToFit(localName);
return
string.concat(
'<text fill="black" xml:space="preserve" style="white-space: pre" x="50%" y="50" text-anchor="middle" font-family="Ginto Nord Medium" font-size="20" font-weight="500" letter-spacing="-0.7px">',
line1,
'</text>',
'<text fill="black" xml:space="preserve" style="white-space: pre" x="50%" y="70" text-anchor="middle" font-family="Ginto Nord Medium" font-size="20" font-weight="500" letter-spacing="-0.7px">',
line2,
'</text>'
);
uint256 textWidth = getTextWidth(string.concat('@', localName));
string memory fontSize = '20';
if (textWidth > MAX_WIDTH) {
uint256 sampleTextWidthAt20 = getWidthFromFontsize(20);
uint256 scalingFactor = (textWidth * 1000) / sampleTextWidthAt20;
uint256 equivalentSampleTextWidth = (((MAX_WIDTH * 10000) / scalingFactor) + 5) / 10;
uint256 fontSize10x = getFontsizeFromWidth10x(equivalentSampleTextWidth);
fontSize = string.concat((fontSize10x / 10).toString(), '.', (fontSize10x % 10).toString());
}
return
string.concat(
'<text fill="black" xml:space="preserve" style="white-space: pre" x="50%" y="60" text-anchor="middle" font-family="Ginto Nord Medium" font-size="',
fontSize,
'" font-weight="500" letter-spacing="-0.7px">@',
localName,
'</text>'
);
}
function getBaseFaceColor(FaceColors faceColor) internal pure returns (string memory) {
@@ -197,6 +197,14 @@ library HandleSVG {
revert(); // Avoid warnings.
}
function getWidthFromFontsize(uint256 fontSize) internal pure returns (uint256) {
return (((fontSize * 1244242 - 1075758 + 50000) / 10000) + 5) / 10;
}
function getFontsizeFromWidth10x(uint256 width) internal pure returns (uint256) {
return (((width * 10000000 + 107575800) / 1244242) + 5) / 10;
}
function getTextWidth(string memory text) internal pure returns (uint256) {
uint256 length = 0;
for (uint i = 0; i < bytes(text).length; i++) {

View File

@@ -54,6 +54,23 @@ contract HandleSVGGen is Test {
);
}
function testRealHandles() public {
vm.writeFile(string.concat(dir, 'handles/handle_real_01.svg'), handleNFT.tryWithName('bradorbradley'));
vm.writeFile(string.concat(dir, 'handles/handle_real_02.svg'), handleNFT.tryWithName('creatorfundincubator'));
vm.writeFile(string.concat(dir, 'handles/handle_real_03.svg'), handleNFT.tryWithName('ameerna17958863'));
vm.writeFile(string.concat(dir, 'handles/handle_real_04.svg'), handleNFT.tryWithName('lensprotocol'));
vm.writeFile(string.concat(dir, 'handles/handle_real_05.svg'), handleNFT.tryWithName('mariariivari'));
vm.writeFile(string.concat(dir, 'handles/handle_real_06.svg'), handleNFT.tryWithName('thesmurfssociety'));
vm.writeFile(string.concat(dir, 'handles/handle_real_07.svg'), handleNFT.tryWithName('donosonaumczuk'));
vm.writeFile(string.concat(dir, 'handles/handle_real_08.svg'), handleNFT.tryWithName('timeswap_labs'));
vm.writeFile(string.concat(dir, 'handles/handle_real_09.svg'), handleNFT.tryWithName('millionrecords'));
vm.writeFile(string.concat(dir, 'handles/handle_real_10.svg'), handleNFT.tryWithName('cointelegraph'));
vm.writeFile(string.concat(dir, 'handles/handle_real_11.svg'), handleNFT.tryWithName('christian_ronaldo'));
vm.writeFile(string.concat(dir, 'handles/handle_real_12.svg'), handleNFT.tryWithName('zombieshepherd'));
vm.writeFile(string.concat(dir, 'handles/handle_real_13.svg'), handleNFT.tryWithName('beautifuldestinations'));
vm.writeFile(string.concat(dir, 'handles/handle_real_14.svg'), handleNFT.tryWithName('shellprotocol_touchan'));
}
function testWWW() public {
for (uint256 i = 1; i <= 26; i++) {
string memory name = '';