From 9bbca0457994b913808dfb639f19eab8a2f2542a Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 5 Aug 2018 10:34:06 +0200 Subject: [PATCH] Add a (basic) `isCFFFile` helper function to detect CFF font files Compared to most other font formats, the CFF doesn't have a constant header which makes is slightly more difficult to detect such font files. Please refer to the Compact Font Format specification: https://www.adobe.com/content/dam/acom/en/devnet/font/pdfs/5176.CFF.pdf#G3.32094 --- src/core/fonts.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/core/fonts.js b/src/core/fonts.js index a94cc510a..b32f0b7cb 100644 --- a/src/core/fonts.js +++ b/src/core/fonts.js @@ -708,6 +708,22 @@ var Font = (function FontClosure() { return false; } + /** + * Compared to other font formats, the header in CFF files is not constant + * but contains version numbers. To reduce the possibility of misclassifying + * font files as CFF, it's recommended to check for other font formats first. + */ + function isCFFFile(file) { + const header = file.peekBytes(4); + if (/* major version, [1, 255] */ header[0] >= 1 && + /* minor version, [0, 255]; header[1] */ + /* header size, [0, 255]; header[2] */ + /* offset(0) size, [1, 4] */ (header[3] >= 1 && header[3] <= 4)) { + return true; + } + return false; + } + function buildToFontChar(encoding, glyphsUnicodeMap, differences) { var toFontChar = [], unicode; for (var i = 0, ii = encoding.length; i < ii; i++) {