Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text Fallback for unavailable glyphs #5

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"compression": "^1.7.1",
"express": "^4.15.3",
"express-winston": "^2.4.0",
"he": "^1.1.1",
"sharp": "^0.18.4",
"text-to-svg": "3.1.0",
"winston": "^2.3.1"
Expand Down
59 changes: 45 additions & 14 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path');
const textToSVG = require('text-to-svg').loadSync(path.resolve(__dirname, 'font.woff'));
const COLOR = require('./color');
const sharp = require('sharp');
const he = require('he');
const app = express();
app.use(compression());

Expand All @@ -12,7 +13,12 @@ const FONT_SIZE = SIZE * 0.5;

const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args), ...args));

const svg = (initials, color) => {
const allGlyphsAvailable = initials => {
const { font } = textToSVG;
return !initials.split('').some(char => font.charToGlyph(char).index === 0);
};

const svgPath = (initials, color) => {
const path = textToSVG.getPath(initials, {
x: FONT_SIZE,
y: FONT_SIZE,
Expand All @@ -21,21 +27,46 @@ const svg = (initials, color) => {
attributes: { fill: '#FFFFFF' },
});
return `<svg
xmlns="http://www.w3.org/2000/svg"
version="1.1"
viewBox="0 0 ${SIZE} ${SIZE}" width="${SIZE}" height="${SIZE}">
<rect
x="0"
y="0"
width="${SIZE}"
height="${SIZE}"
fill="${color || '#ffeeee'}"
stroke-width="0"
/>
${path}
</svg>`;
xmlns="http://www.w3.org/2000/svg"
version="1.1"
viewBox="0 0 ${SIZE} ${SIZE}" width="${SIZE}" height="${SIZE}">
<rect
x="0"
y="0"
width="${SIZE}"
height="${SIZE}"
fill="${color || '#ffeeee'}"
stroke-width="0"
/>
${path}
</svg>`;
};

const svgText = (initials, color) => `<text
x="50%"
y="68%"
text-anchor="middle"
fill="white"
font-family="sans-serif"
font-size="${SIZE / 2}">
${he.escape(initials)}
</text>`;

const svg = (initials, color) => `<svg
xmlns="http://www.w3.org/2000/svg"
version="1.1"
viewBox="0 0 ${SIZE} ${SIZE}" width="${SIZE}" height="${SIZE}">
<rect
x="0"
y="0"
width="${SIZE}"
height="${SIZE}"
fill="${color || '#ffeeee'}"
stroke-width="0"
/>
${allGlyphsAvailable(initials) ? svgPath(initials, color) : svgText(initials, color)}
</svg>`;

const toSvg = (req, res) => {
const { initials, index } = req.params;
const color = COLOR[index];
Expand Down
12 changes: 11 additions & 1 deletion src/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('Avatar service', () => {
else done(new Error());
});
});
it('should render a path element', done => {
it('should render a path element for available glyphs', done => {
request(app)
.get(BASE_URL + '/12/PF.svg')
.expect(200)
Expand All @@ -51,6 +51,16 @@ describe('Avatar service', () => {
else done(new Error());
});
});
it('should render a text element for unavailable glyphs', done => {
request(app)
.get(BASE_URL + '/12/P%E6%A0%91.svg')
.expect(200)
.parse(binaryParser)
.end((err, res) => {
if (/<text(.|\n)*?\>/.test(res.body.toString())) done();
else done(new Error());
});
});
it('should always render a default color, even if `:index` is out of bounds', done => {
request(app)
.get(BASE_URL + '/182736178123761/AW.svg')
Expand Down