Turn audio into a shareable video. forked from nypublicradio/audiogram

fonts.js 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. var fonts = require("../lib/settings/").fonts || [];
  2. var bySlug = {};
  3. fonts.forEach(function(font, i){
  4. var extension = "",
  5. split = font.file.split(".");
  6. // Use existing file extension if there is one
  7. if (split.length > 1){
  8. extension = "." + split.pop();
  9. }
  10. bySlug[font.slug = "custom-" + i + extension] = font;
  11. });
  12. // Send a stylesheet with custom fonts
  13. function sendCSS(req, res) {
  14. res.set("Content-Type", "text/css")
  15. .send(fonts.map(declaration).join("\n\n"));
  16. }
  17. // Send JS that forces all custom fonts to download upfront
  18. function sendJS(req, res) {
  19. res.set("Content-Type", "application/javascript")
  20. .send("(function(){\n\n" + fonts.map(shim).join("\n\n") + "\n\n})();");
  21. }
  22. // Send custom file by its slug
  23. function sendFont(req, res) {
  24. var font = bySlug[req.params.font];
  25. if (font && font.file) {
  26. return res.sendFile(font.file);
  27. }
  28. res.status(404).send("Cannot GET " + req.baseUrl);
  29. }
  30. function declaration(font, i) {
  31. return [
  32. "@font-face {",
  33. " font-family: '" + font.family + "';",
  34. " src: url('/fonts/" + font.slug + "');",
  35. font.weight ? " font-weight: " + font.weight + ";" : "",
  36. font.style ? " font-style: " + font.style + ";" : "",
  37. "}"
  38. ].filter(function(d){ return d; }).join("\n");
  39. }
  40. function shim(font, i) {
  41. return [
  42. " var font" + i + " = document.createElement(\"div\");",
  43. " font" + i + ".innerHTML = '.';",
  44. " font" + i + ".style.fontFamily = \"" + font.family + "\";",
  45. font.weight ? " font" + i + ".style.fontWeight = \"" + font.weight + "\";" : "",
  46. font.style ? " font" + i + ".style.fontStyle = \"" + font.style + "\";" : "",
  47. " document.body.appendChild(font" + i + ");",
  48. " setTimeout(function(){ font" + i + ".remove(); }, 0);"
  49. ].filter(function(d){ return d; }).join("\n");
  50. }
  51. module.exports = {
  52. css: sendCSS,
  53. js: sendJS,
  54. font: sendFont
  55. };