mmastrac / webfont-dl

Webfont Downloader

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

could have a command line argument to use the original font file names when downloading

insinfo opened this issue · comments

could have a command line argument to use the original font file names when downloading

https://gists.rawgit.com/mfd/f3d96ec7f0e8f034cc22ea73b3797b59/raw/856f1dbb8d807aabceb80b6d4f94b464df461b3e/gotham.css

@font-face {
    font-family: GothamPro;
    src: url(GothamPro-Light.eot);
    src: url(GothamPro-Light.woff2) format("woff2"),url(GothamPro-Light.woff) format("woff"),url(GothamPro-Light.ttf) format("truetype"),url(GothamPro-Light.svg#GothamPro-Light) format("svg"),url(GothamPro-Light.eot?#iefix) format("embedded-opentype");
    font-weight: 100;
    font-style: normal
  }
  
  @font-face {
    font-family: GothamPro;
    src: url(GothamPro-LightItalic.eot);
    src: url(GothamPro-LightItalic.woff2) format("woff2"),url(GothamPro-LightItalic.woff) format("woff"),url(GothamPro-LightItalic.ttf) format("truetype"),url(GothamPro-LightItalic.svg#GothamPro-LightItalic) format("svg"),url(GothamPro-LightItalic.eot?#iefix) format("embedded-opentype");
    font-weight: 100;
    font-style: italic
  }
  
  @font-face {
    font-family: GothamPro;
    src: url(GothamPro.eot);
    src: url(GothamPro.woff2) format("woff2"),url(GothamPro.woff) format("woff"),url(GothamPro.ttf) format("truetype"),url(GothamPro.svg#GothamPro) format("svg"),url(GothamPro.eot?#iefix) format("embedded-opentype");
    font-weight: 200;
    font-style: normal
  }
  
  @font-face {
    font-family: GothamPro;
    src: url(GothamPro-Italic.eot);
    src: url(GothamPro-Italic.woff2) format("woff2"),url(GothamPro-Italic.woff) format("woff"),url(GothamPro-Italic.ttf) format("truetype"),url(GothamPro-Italic.svg#GothamPro-Italic) format("svg"),url(GothamPro-Italic.eot?#iefix) format("embedded-opentype");
    font-weight: 200;
    font-style: italic
  }
  
  @font-face {
    font-family: GothamPro;
    src: url(GothamPro-Medium.eot);
    src: url(GothamPro-Medium.woff2) format("woff2"),url(GothamPro-Medium.woff) format("woff"),url(GothamPro-Medium.ttf) format("truetype"),url(GothamPro-Medium.svg#GothamPro-Medium) format("svg"),url(GothamPro-Medium.eot?#iefix) format("embedded-opentype");
    font-weight: 400;
    font-style: normal
  }
  
  @font-face {
    font-family: GothamPro;
    src: url(GothamPro-MediumItalic.eot);
    src: url(GothamPro-MediumItalic.woff2) format("woff2"),url(GothamPro-MediumItalic.woff) format("woff"),url(GothamPro-MediumItalic.ttf) format("truetype"),url(GothamPro-MediumItalic.svg#GothamPro-MediumItalic) format("svg"),url(GothamPro-MediumItalic.eot?#iefix) format("embedded-opentype");
    font-weight: 400;
    font-style: italic
  }
  
  @font-face {
    font-family: GothamPro;
    src: url(GothamPro-Bold.eot);
    src: url(GothamPro-Bold.woff2) format("woff2"),url(GothamPro-Bold.woff) format("woff"),url(GothamPro-Bold.ttf) format("truetype"),url(GothamPro-Bold.svg#GothamPro-Bold) format("svg"),url(GothamPro-Bold.eot?#iefix) format("embedded-opentype");
    font-weight: 600;
    font-style: normal
  }
  
  @font-face {
    font-family: GothamPro;
    src: url(GothamPro-BoldItalic.eot);
    src: url(GothamPro-BoldItalic.woff2) format("woff2"),url(GothamPro-BoldItalic.woff) format("woff"),url(GothamPro-BoldItalic.ttf) format("truetype"),url(GothamPro-BoldItalic.svg#GothamPro-BoldItalic) format("svg"),url(GothamPro-BoldItalic.eot?#iefix) format("embedded-opentype");
    font-weight: 600;
    font-style: italic
  }
  
  @font-face {
    font-family: GothamPro;
    src: url(GothamPro-Black.eot);
    src: url(GothamPro-Black.woff2) format("woff2"),url(GothamPro-Black.woff) format("woff"),url(GothamPro-Black.ttf) format("truetype"),url(GothamPro-Black.svg#GothamPro-Black) format("svg"),url(GothamPro-Black.eot?#iefix) format("embedded-opentype");
    font-weight: 800;
    font-style: normal
  }
  
  @font-face {
    font-family: GothamPro;
    src: url(GothamPro-BlackItalic.eot);
    src: url(GothamPro-BlackItalic.woff2) format("woff2"),url(GothamPro-BlackItalic.woff) format("woff"),url(GothamPro-BlackItalic.ttf) format("truetype"),url(GothamPro-BlackItalic.svg#GothamPro-BlackItalic) format("svg"),url(GothamPro-BlackItalic.eot?#iefix) format("embedded-opentype");
    font-weight: 800;
    font-style: italic
  }

image

var crypto = require('crypto');
var fs = require('fs');
var url = require('url');
var path = require('path');
var Q = require('q');

var log = require('./log');

function toDataURL(mimeType, buffer, url) {
  return "data:" + mimeType + ";base64," + buffer.toString('base64') + (url.hash ? url.hash : "");  
}

function computeFilename(src, font, parsed, body) {
  var fmt = src.fmt;
  //var sha1 = crypto.createHash('sha1').update(body).digest('hex');
 
  
  
  var ext = path.extname(parsed.pathname);

  if (fmt && !ext) {
      log.debug("Inferring extension '." + fmt.ext + "' based on format '" + src.format + "'");
      ext = '.' + fmt.ext;
  }
  // var filename = font.font.family.toLowerCase().replace(/[^a-z]/g, "") + "-" + sha1 + ext;
  //var filename = font.font.family.toLowerCase().replace(/[^a-z]/g, "") + "-" + ext;
  var filename = getFileName(src.url);
  log.info("computeFilename filename: ",filename);
  return filename;
}

function getFileName(path) {
  // Usamos uma expressão regular para capturar o nome do arquivo
  const regex = /([^\/?#]+)(?:[?#].*)?$/;
  const match = path.match(regex);
  // Se houver uma correspondência, retornamos o primeiro grupo de captura, que é o nome do arquivo
  return match ? match[1] : null;
}

function writeFont(opts, src, font, body) {
  var promise = Q.defer();
  var parsed = url.parse(src.url);
  var asData = false;
  var fmt = src.fmt;

  // if (fmt && opts.modes[fmt.id] == "data") {
  //   log.debug("Converting " + fmt.id + " font " + font.font.getId() + " to data, mime-type '" + fmt.mime[0] + "'");
  //   src.url = toDataURL(fmt.mime[0], body, parsed);
  //   promise.resolve();
  //   return promise.promise;
  // }

  var filename = computeFilename(src, font, parsed, body);
  src.url = opts.cssRel + filename + (parsed.hash && fmt.hash ? parsed.hash : "");

  log.debug("Writing " + opts.fontOut + "/" + filename);
  fs.writeFile(opts.fontOut + "/" + filename, body, function(error) {
    if (error) {
      promise.reject(error);
    } else {
      promise.resolve();
    }
  });

  return promise.promise;
}

exports.writeFont = writeFont;