简体   繁体   English

如何在node.js后端中使用字体?

[英]How to use fonts in the node.js backend?

Background: 背景:

I am building a node.js-based Web app that needs to make use of various fonts. 我正在构建一个需要使用各种字体的基于node.js的Web应用程序。 But it only needs to do so in the backend since the results will be delivered as an image. 但是它只需要在后端这样做,因为结果将作为图像传递。 Consequently, the client/browser does not need access to the fonts at all in my case. 因此,客户端/浏览器并不需要在所有访问的字体在我的情况。

Question: 题:

I will try to formulate the question as little subjective as possible: What are the typical options to provide a node.js backend with a large collection of fonts? 我将尝试提出尽可能少的主观问题: 为node.js后端提供大量字体的典型选择是什么?

The options I came up with so far are: 到目前为止,我想到的选择是:

  1. Does one install these hundreds or thousands of fonts in the operating system of the (in my case: Ubuntu) server? 有人会在服务器(在我的情况下为Ubuntu)的操作系统中安装这数百种或数千种字体吗?
  2. Does one somehow serve the fonts from a cloud storage such as S3 or (online) database such as a Mongo DB server? 是否有人以某种方式从云存储(例如S3)或(在线)数据库(例如Mongo DB服务器)提供字体?
  3. Does one use a local file system to store the fonts and retrieve them? 是否使用本地文件系统存储字体并检索它们?
  4. ...other options ...其他选择

I am currently leaning towards Option 1 because this is the way a layman like me does it on a local machine. 我目前倾向于选择1,因为这是像我这样的外行在本地计算机上执行此操作的方式。

Without starting a discussion here, where could I find resources discussing the (dis-)advantages of the different options? 如果不在此处开始讨论,我在哪里可以找到讨论这些不同方案(不利)优势的资源?

EDIT: 编辑:

Thank you for all the responses. 感谢您的所有答复。 Thanks to these, I noticed that I need to clarify something. 多亏了这些,我注意到我需要澄清一些事情。 I need the fonts to be used in SVG processing libraries such as p5.js, paper.js, raphael.js. 我需要在SVG处理库(例如p5.js,paper.js,raphael.js)中使用的字体。 So I need to make the fonts available to these libraries that are run on node.js. 因此,我需要使字体可用于在node.js上运行的这些库。

The key to your question is 您问题的关键是

hundreds or thousands of fonts 成百上千种字体

Until I took that in there is no real difference between your methods. 在我接受之前,您的方法之间没有真正的区别。 But if that number is correct (kind of mind-boggling though) I would: 但是,如果这个数字是正确的(虽然有点令人难以置信),我会:

  1. not install them in the OS. 不要在操作系统中安装它们。 What happens if you move servers without an image? 如果移动没有映像的服务器会怎样? Or move OS? 还是移动操作系统?

  2. Local File system would be a sane way of doing it, though you would need to keep track manually of all the file names and paths for your code . 尽管需要手动跟踪代码的所有文件名和路径但是 Local File system是一种明智的做法。

  3. MongoDB - store file names+paths in the collection..and store the actual fonts in your system . MongoDB-将文件名和路径存储在collection .. 中,并将实际字体存储在系统中 In advent of moving servers you would have to pick up the directory where all the actual files are stored and the DB where you hold the file name+paths. 随着移动服务器的出现,您将必须选择存储所有实际文件的目录以及保存文件名和路径的DB。
  4. If you want you can place it all in a MongoDB but then that file would also be huge, I assume - that is up to you. 如果您愿意,可以将其全部放在MongoDB中,但是该文件也很大,我想-这取决于您。

Choice #3 is probably what I would do in such a case. 在这种情况下,选择3可能是我要做的。

If you have a decent enough server setup (eg a VPS or some other VM solution where you control what's installed) then another option you might want to consider is to do this job "out of node". 如果您有足够不错的服务器设置(例如,VPS或用于控制安装内容的其他VM解决方案),那么您可能要考虑的另一种选择是“在节点外”进行此工作。 For instance, in one of my projects where I need to build 175+ as-perfect-as-can-be maths statements, I offload that work to XeLaTeX instead: 例如,在我的一个项目中,我需要构建175个以上尽可能完美的数学语句,我将其工作卸载到XeLaTeX:

  1. I run a node script that takes the input text and builds a small but complete .tex file 我运行一个节点脚本,该脚本接受输入文本并构建一个小的但完整的.tex文件
  2. I then tell node to call "xelatex theFileIJustMade.tex" which yields a pdf 然后,我告诉节点调用“ xelatex theFileIJustMade.tex”,生成一个pdf
  3. I then tell node to call "pdfcrop" on that pdf, to remove the margins 然后,我告诉节点在该pdf上调用“ pdfcrop”,以删除边距
  4. I then tell node to call "pdf2svg", which is a free and amazingly effective utility 然后,我告诉node调用“ pdf2svg”,这是一个免费且非常有效的实用程序
  5. Then as a final step mostly to conserve space and bandwidth, I use "svgo" which is a nodejs based svg optimizer that can run either as normal script code, or as CLI utility. 然后,在最后一步,主要是为了节省空间和带宽,我使用“ svgo”,它是基于nodejs的svg优化器,可以作为常规脚本代码或CLI实用程序运行。

(some more details on that here , with concrete code here ) (对一些细节在这里 ,用实际的代码在这里

Of course, depending on how responsive a system you need, you can do entirely without steps 3 and 5. There is a limit to how fast we can run, but as a server-side task there should never be the expectation of real-time responsiveness. 当然,根据您需要的系统响应速度,您可以完全不执行步骤3和5。我们的运行速度有一定的限制,但是作为服务器端任务,永远不要期望实时响应。

This is a good example of remembering that your server runs inside a larger operating system that might also offer tools that can do the job. 这是记住您的服务器在较大的操作系统中运行的一个很好的示例,该操作系统也可能提供可以完成此任务的工具。 While you're using Node, and the obvious choice is a Node solution, Node is also a general purpose programming language and can call anything else through spawn and exec , much like python, php, java, C#, etc. As such, it's sometimes worth thinking about whether there might be another tool that is even better suited for your needs, especially when you're thinking about doing a highly specialized job like typesetting a string to SVG. 当您使用Node时,显而易见的选择是Node解决方案,Node还是一种通用的编程语言,可以通过spawnexec调用其他任何东西,就像python,php,java,C#等一样。有时值得考虑是否可能存在另一个更适合您需要的工具,尤其是在考虑进行高度专业化的工作(例如将字符串键入SVG)时。

In this case, LaTeX was specifically created to typeset text from the command line, and XeLaTeX was created to do that with full Unicode awareness and clean, easy access to fonts both from file and from the system, with full OpenType feature control, so would certainly qualify as just as worthwhile a candidate as any node-specific solution might be. 在这种情况下,专门创建LaTeX来从命令行排版文本,并且创建XeLaTeX时要具有完整的Unicode意识,并具有完整的OpenType功能控制,可以从文件和系统中干净方便地访问字体。当然,与任何特定于节点的解决方案一样,有资格成为候选人。

As for the tools used: XeLaTeX and pdfcrop come with TeX Live (installed using whatever package manager your OS uses, or through MiKTeX on Windows, but I suspect your server doesn't run on windows) pdf2svg is freely available on github , and svgo is available from npm ) 至于使用的工具:TeX Live附带XeLaTeX和pdfcrop(使用操作系统使用的任何软件包管理器安装,或者通过Windows上的MiKTeX安装,但我怀疑您的服务器不能在Windows上运行)pdf2svg 可在github和svgo 免费使用。 可从npm获得

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM