如果它们不是Unicode的一部分,则映射到什么字体字形

时间:2018-09-18 11:12:07

标签: unicode fonts character-encoding keyboard character-codes

我试图了解字体文件和wondering about how glyphs are mapped to keyboard characters的结构。

作为其中的一部分,我还想知道当您拥有不属于unicode的字体字形(例如,使用FontAwesome icons时,您会怎么做)。 FontAwesome像这样在CSS中使用它们:

.fa4-clock-o:before {
  content: "\f017";
}

如果我想说使用键盘输入这些图标,则不确定该怎么做。

想知道我是否必须做以下其中之一:

  • 构建另一种以某种方式将它们映射到ALT键代码的字体(不确定ALT代码在字体定义中的工作方式)。
  • 构建一个自定义键盘工具,将正确的CSS类放置在div上(基本上根本不使用字体文件/字符映射,只是从头开始构建键盘工具)。
  • 其他方法。

想知道是否可以中等程度地解释如何完成此操作(也就是说,除非很简单,否则我不必知道具体的实现细节,除非这样做)。

2 个答案:

答案 0 :(得分:2)

理智的图标字体提供GSUB连字替换,因此,如果您的文本包含一系列代码点(例如与c + a + r相关的代码点),则 font 会重新映射到内部字形(例如汽车图标),因此在键入时,您会看到:

c
co
com
comp
compu
comput
compute

您键入“计算机”,一旦字体看到最后一个r,它就会触发c + o + m + p + u + t + e + r -> internal_id_24663的GSUB规则,并用该替换来成形文本。

当然,如果内部字形ID通过CMAP表公开,则也可以直接访问字形,只需指定将赋予字体形状的USHORT。在您的示例中,您通过使用十六进制值而不是“字母”在Unicode代码点中写出它来直接指定Fontawesome图标:字体使用了(按事实上的标准)与平台无关的Unicode映射,并且您已经问过对于与代码点0xF017相关的字形,它位于“专用区域”块中(从E0x000`到0xF8FF的代码点范围,每个点都没有指定标签:供应商可以在其中放置所需的任何内容,并且不能保证在版本之间解析为相同的字形)。

我已经在http://pomax.github.io/CFF-glyphlet-fonts

上写过有关字体内部的信息,包括这些部分。

答案 1 :(得分:1)

Glyphs are not "mapped to keyboard characters". It's rather the other way around, and it happens in multiple steps:

  1. Your keyboard controller picks up the physical signals that are generated by the movement of the keys, converts them into scancodes, and then sends them to your computer.
  2. Then the component of your operating system that is responsible for the keyboard layout transforms the scancodes into codepoints of the characters. For example, on my current operating system (Linux), the mapping from scancodes to code points is defined in a configuration file somewhere in /usr/share/X11/xkb/symbols. Here is an excerpt from the configuration file:

    key <AD01> { [     q,          Q,    adiaeresis,       Adiaeresis ] };
    key <AD02> { [     w,          W,         aring,            Aring ] };
    key <AD03> { [     e,          E,        eacute,           Eacute ] };
    key <AD04> { [     r,          R,    registered,       registered ] };
    key <AD05> { [     t,          T,         thorn,            THORN ] };
    key <AD06> { [     y,          Y,    udiaeresis,       Udiaeresis ] };
    key <AD07> { [     u,          U,        uacute,           Uacute ] };
    key <AD08> { [     i,          I,        iacute,           Iacute ] };
    key <AD09> { [     o,          O,        oacute,           Oacute ] };
    key <AD10> { [     p,          P,    odiaeresis,       Odiaeresis ] };
    

    For example, it says that the scancode <AD04> is mapped to the letter "r".

  3. The symbols that you type on your keyboard using your layout somehow end up in the memory of some server, and are then served as a part of an HTML-document.

  4. The CSS that accompanies the HTML document specifies that a particular font (e.g. Font Awesome) is to be used.

  5. This particular font specifies that, for example, the unicode code point U+F420 should be rendered as the logo of Angular, this is why you see a hexagonal glyph with an A in your browser.

In this whole pathway, there is no fundamental difference between the Angular logo and any other character. The minor differences are:

  1. The angular logo is not part of the Unicode, that is, the Unicode consortium has not defined a code point for the Angular logo. Instead, a code point U+F420 from a private usage area is used. In this case, "private" means that it is used only by you, by Twitter, and by few hundred million other people (anyone who visits a website that uses Font Awesome implicitly agrees to play on this "private" playground by the rules defined in the font).
  2. There is no predefined keyboard layout that maps scancodes to code points from some obscure private usage area.
  3. There is no hardware that has all those funny logos printed on the key caps (since you probably don't use such symbols all that often, it might be a problem).

If you wanted to type text with such glyphs anyway, you would have to:

  1. Somehow specify that you want to use a font that renders code points from the private usage area as the glyphs that you want (e.g. specify it in the CSS of a webpage)
  2. In order to type it, you would have to define your own keyboard layout. For example on Linux, you could copy-paste some configuration file from /usr/share/X11/xkb/symbols, and modify it accordingly.
  3. When you try to use this layout with an actual keyboard, you will probably need some hints which physical key corresponds to which glyph. E.g. you could put an image of the keyboard layout on the desktop background.