托管你自己的在线字体
字体

> 
> 使用自托管的开源字体来定制你的网页。
> 
> 
> 


![](/data/attachment/album/201903/27/093535ixx22d5xs7dbpmrs.jpg)


字体对许多计算机用户来说可能都是很神秘的东西。举个例子，你在制作好一张很酷的传单之后，你需要将它送到某个地方去打印，结果发现，你设计的所有字体都变成了 Arial，这多半是因为打印店没用安装你设计用到的那些字体。不过，我们仍有很多方法来避免这种情况：你可以将这些使用特定字体的单词转换为路径，你也可以将它封装为 PDF，或是把开源字体封装到你的设计文件中，或者至少列出所需字体。不过，我们总会忘记一些事情，所以这仍是一个问题。


Web 上也有类似的问题。如果你对 CSS 有所了解，你可能会见过这种声明：



```
h1 { font-family: "Times New Roman", Times, serif; }
```

这是设计师正在尝试定义网站使用要用到的特定字体，如果用户没有安装 Times New Roman 这个字体，便会回落到另一个字体；如果用户也没有安装 Times 这个字体，便再次回落。它比使用图片而不是文本更好一些，但是在没有字体托管的情况下，这仍是一种棘手且不雅观的方法。不过，在早期的互联网时代，我们不得不这样做。


### 在线字体


在线字体的登场，把字体管理从客户端搬上了服务端。如今网页上的字体通常由服务器为客户端渲染，而不是要求浏览器从用户的系统中查找字体。谷歌和其它供应商托管了许多开源字体，网站设计师们可以很轻松的用 CSS 来引用它们。


不过，问题是，引用这些字体并不是不花费任何代价的。虽然引用它们免费，但是像谷歌这样的巨头喜欢跟踪那些引用它们资源的网站，其中就包括了字体资源。如果你不想你的网站帮谷歌记录每个人的活动，你可以自己托管在线字体。别觉得这很难，它其实是很简单的，大概流程就是上传字体到你的主机，再使用一个简单的 CSS 便可完成。这样做还有个好处，你的网站能更快地加载，因为它会在加载每个页面的时候进行更少的外部调用。


### 自托管在线字体


首先，你需要一个开源字体。如果你没有了解过那些令人费解的软件协议，你可能会感到很疑惑，特别是很多字体看起来都是免费的。我们中应该很少有人有字体付费意识，但是他们却在电脑上安装了一些高价的字体。不过，由于授权协议，它使得你的电脑也许带着一些 [法律上不允许复制和再分发](https://docs.microsoft.com/en-us/typography/fonts/font-faq) 的字体。像 Arial、Verdana、Calibri、Georgia、Impact、Lucida 和 Lucida Grande、Times 和 Times New Roman、Trebuchet、Geneva 以及其它的很多字体都是被微软、苹果和 Adobe 这种大公司所拥有的。如果你购买了一台预装了 Windows 或 macOS 的电脑，你就获得了使用这些字体的权利，但是你并没有拥有那些字体，也没有被许可上传它们至服务器（除非额外说明）。


幸运的事，开源热潮在很久以前就席卷了字体界。然后就有了许多优秀的开源字体的合集和项目，比如 [The League of Moveable Type](https://www.theleagueofmoveabletype.com/)、[Font Library](https://fontlibrary.org/) 以及 [Omnibus Type](https://www.omnibus-type.com)，甚至还有一些来自 [Google](https://github.com/googlefonts) 和 [Adobe](https://github.com/adobe-fonts) 的字体。


常见的字体格式有 TTF、OTF、WOFF、EOT 等。因为 Sorts Mill Goudy 发行过 <ruby> WOFF <rt>  Web Open Font Format </rt></ruby>（互联网开放字体格式，Mozilla 参与了部分开发）版本，所以下文中我会用它来做例子。当然，其它字体的方法也是一样的。


假设你想在你的网站上使用 [Sorts Mill Goudy](https://www.theleagueofmoveabletype.com/sorts-mill-goudy) 这个字体：


1、将字体文件 `GoudyStM-webfont.woff` 上传至你的服务器：



```
scp GoudyStM-webfont.woff seth@example.com:~/www/fonts/
```

你的主机可能带有像 cPanel 这样的图形化工具，通过它们上传也是一样的。


2、在你网站的 CSS 文件中，添加 `@font-face` 语句，添加后应该和这个差不多：



```
@font-face { 
  font-family: "titlefont";
  src: url("../fonts/GoudyStM-webfont.woff"); 
}
```

`font-family` 的值是你来决定的。这是一个易于理解的名字，它用于放在使用字体名的地方。我在这里使用 “titlefont” 作为例子，是因为我希望它被用来显示标题字体。你也可以使用 “officialfont” 和 “myfont” 这样的名字。


`src` 值是你字体文件的路径。这是你服务器上字体的路径。在这里，我用 `fonts` 目录来作为示例，它和 `css` 在一个文件夹里。你服务器的文件结构可能和我的不一样，所以你需要调整一下这个路径。记住一点，一个点意味着*工作目录*，两个点则代表*父目录*。


3、现在，你已经定义了字体的名字和目录，你可以在任何指定的 CSS 类或 ID 来调用它了。举个例子，如果你希望以 Sorts Mill Goudy 字体来渲染 `<h1>`，只需要在 CSS 规则中加入你自己的字体名称：



```
h1 { font-family: "titlefont", serif; }
```

现在，你已经成功地托管并使用你自己的字体了。


![在线字体的实际效果](/data/attachment/album/201903/27/093600qvewerziqeipyzvj.jpg "Web fonts on a website")




---


via: <https://opensource.com/article/19/3/webfonts>


作者：[Seth Kenlon (Red Hat, Community Moderator)](https://opensource.com/users/seth) 选题：[lujun9972](https://github.com/lujun9972) 译者：[zhs852](https://github.com/zhs852) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Fonts are often a mystery to many computer users. For example, have you designed a cool flyer and, when you take the file somewhere for printing, find all the titles rendered in Arial because the printer doesn't have the fancy font you used in your design? There are ways to prevent this, of course: you can convert words in special fonts into paths, bundle fonts into a PDF, bundle open source fonts with your design files, or—at least—list the fonts required. And yet it's still a problem because we're human and we're forgetful.

The web has the same sort of problem. If you have even a basic understanding of CSS, you've probably seen this kind of declaration:

`h1 { font-family: "Times New Roman", Times, serif; }`


This is a designer's attempt to define a specific font, provide a fallback if a user doesn't have Times New Roman installed, and offer yet another fallback if the user doesn't have Times either. It's better than using a graphic instead of text, but it's still an awkward, inelegant method of font non-management, However, in the early-ish days of the web, it's all we had to work with.

## Webfonts

Then webfonts happened, moving font management from the client to the server. Fonts on websites were rendered for the client by the server, rather than requiring the web browser to find a font on the user's system. Google and other providers even host openly licensed fonts, which designers can include on their sites with a simple CSS rule.

The problem with this free convenience, of course, is that it doesn't come without cost. It's $0 to use, but major sites like Google love to keep track of who references their data, fonts included. If you don't see a need to assist Google in building a record of everyone's activity on the web, the good news is you can host your own webfonts, and it's as simple as uploading fonts to your host and using one easy CSS rule. As a side benefit, your site may load faster, as you'll be making one fewer external call upon loading each page.

## Self-hosted webfonts

The first thing you need is an openly licensed font. This can be confusing if you're not used to thinking or caring about obscure software licenses, especially since it seems like all fonts are free. Very few of us have consciously paid for a font, and yet most people have high-priced fonts on their computers. Thanks to licensing deals, your computer may have shipped with fonts that [you aren't legally allowed to copy and redistribute](https://docs.microsoft.com/en-us/typography/fonts/font-faq). Fonts like Arial, Verdana, Calibri, Georgia, Impact, Lucida and Lucida Grande, Times and Times New Roman, Trebuchet, Geneva, and many others are owned by Microsoft, Apple, and Adobe. If you purchased a computer preloaded with Windows or MacOS, you paid for the right to use the bundled fonts, but you don't own those fonts and are not permitted to upload them to a web server (unless otherwise stated).

Fortunately, the open source craze hit the font world long ago, and there are excellent collections of openly licensed fonts from collectives and projects like [The League of Moveable Type](https://www.theleagueofmoveabletype.com/), [Font Library](https://fontlibrary.org/), [Omnibus Type](https://www.omnibus-type.com), and even [Google](https://github.com/googlefonts) and [Adobe](https://github.com/adobe-fonts).

You can use most common font file formats, including TTF, OTF, WOFF, EOT, and so on. Since Sorts Mill Goudy includes a WOFF (Web Open Font Format, developed in part by Mozilla) version, I'll use it in this example. However, other formats work the same way.

Assuming you want to use [Sorts Mill Goudy](https://www.theleagueofmoveabletype.com/sorts-mill-goudy) on your web page:

- Upload the
**GoudyStM-webfont.woff**file to your web server:

`scp GoudyStM-webfont.woff seth@example.com:~/www/fonts/`

Your host may also provide a graphical upload tool through cPanel or a similar web control panel.


- In your site's CSS file, add an
**@font-face**rule, similar to this:

`@font-face { font-family: "titlefont"; src: url("../fonts/GoudyStM-webfont.woff"); }`

The

**font-family**value is something you make up. It's a human-friendly name for whatever the font face represents. I am using "titlefont" in this example because I imagine this font will be used for the main titles on an imaginary site. You could just as easily use "officialfont" or "myfont."


The**src**value is the path to the font file. The path to the font must be appropriate for your server's file structure; in this example, I have the**fonts**directory alongside a**css**directory. You may not have your site structured that way, so adjust the paths as needed, remembering that a single dot means*this folder*and two dots mean*a folder back*.

- Now that you've defined the font face name and the location, you can call it for any given CSS class or ID you desire. For example, if you want
**<h1>**to render in the Sorts Mill Goudy font, then make its CSS rule use your custom font name:

`h1 { font-family: "titlefont", serif; }`


You're now hosting and using your own fonts.

![Web fonts on a website Web fonts on a website](https://opensource.com/sites/default/files/uploads/webfont.jpg)

*Thanks to Alexandra Kanik for teaching me about @font-face and most everything else I know about good web design.*

## Comments are closed.