cff_charstring.h (3143B)
1 // Copyright (c) 2010-2017 The OTS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef OTS_CFF_TYPE2_CHARSTRING_H_ 6 #define OTS_CFF_TYPE2_CHARSTRING_H_ 7 8 #include "cff.h" 9 #include "ots.h" 10 11 #include <map> 12 #include <vector> 13 14 namespace ots { 15 16 const size_t kMaxCFF1ArgumentStack = 48; 17 const size_t kMaxCFF2ArgumentStack = 513; 18 19 // Validates all charstrings in |char_strings_index|. Charstring is a small 20 // language for font hinting defined in Adobe Technical Note #5177. 21 // http://www.adobe.com/devnet/font/pdfs/5177.Type2.pdf 22 // 23 // The validation will fail if one of the following conditions is met: 24 // 1. The code uses more than the max argument stack size (48 for CFF1; 513 for CFF2) 25 // 2. The code uses deeply nested subroutine calls (more than 10 levels.) 26 // 3. The code passes invalid number of operands to an operator. 27 // 4. The code calls an undefined global or local subroutine. 28 // 5. The code uses one of the following operators that are unlikely used in 29 // an ordinary fonts, and could be dangerous: random, put, get, index, roll. 30 // 31 // Arguments: 32 // cff: parent OpenTypeCFF reference 33 // global_subrs_index: Global subroutines which could be called by a charstring 34 // in |char_strings_index|. 35 // cff_table: A buffer which contains actual byte code of charstring, global 36 // subroutines and local subroutines. 37 bool ValidateCFFCharStrings( 38 OpenTypeCFF& cff, 39 const CFFIndex &global_subrs_index, 40 Buffer *cff_table); 41 42 // The list of Operators. See Appendix. A in Adobe Technical Note #5177. 43 // and https://docs.microsoft.com/en-us/typography/opentype/spec/cff2charstr 44 enum CharStringOperator { 45 kHStem = 1, 46 kVStem = 3, 47 kVMoveTo = 4, 48 kRLineTo = 5, 49 kHLineTo = 6, 50 kVLineTo = 7, 51 kRRCurveTo = 8, 52 kCallSubr = 10, 53 kReturn = 11, 54 kEndChar = 14, 55 kVSIndex = 15, 56 kBlend = 16, 57 kHStemHm = 18, 58 kHintMask = 19, 59 kCntrMask = 20, 60 kRMoveTo = 21, 61 kHMoveTo = 22, 62 kVStemHm = 23, 63 kRCurveLine = 24, 64 kRLineCurve = 25, 65 kVVCurveTo = 26, 66 kHHCurveTo = 27, 67 kCallGSubr = 29, 68 kVHCurveTo = 30, 69 kHVCurveTo = 31, 70 kDotSection = 12 << 8, 71 kAnd = (12 << 8) + 3, 72 kOr = (12 << 8) + 4, 73 kNot = (12 << 8) + 5, 74 kAbs = (12 << 8) + 9, 75 kAdd = (12 << 8) + 10, 76 kSub = (12 << 8) + 11, 77 kDiv = (12 << 8) + 12, 78 kNeg = (12 << 8) + 14, 79 kEq = (12 << 8) + 15, 80 kDrop = (12 << 8) + 18, 81 kPut = (12 << 8) + 20, 82 kGet = (12 << 8) + 21, 83 kIfElse = (12 << 8) + 22, 84 kRandom = (12 << 8) + 23, 85 kMul = (12 << 8) + 24, 86 kSqrt = (12 << 8) + 26, 87 kDup = (12 << 8) + 27, 88 kExch = (12 << 8) + 28, 89 kIndex = (12 << 8) + 29, 90 kRoll = (12 << 8) + 30, 91 kHFlex = (12 << 8) + 34, 92 kFlex = (12 << 8) + 35, 93 kHFlex1 = (12 << 8) + 36, 94 kFlex1 = (12 << 8) + 37, 95 // Operators that are undocumented will be rejected. 96 }; 97 98 struct CharStringContext { 99 bool endchar_seen = false; 100 bool width_seen = false; 101 size_t num_stems = 0; 102 bool cff2 = false; 103 bool blend_seen = false; 104 bool vsindex_seen = false; 105 int32_t vsindex = 0; 106 }; 107 108 } // namespace ots 109 110 #endif // OTS_CFF_TYPE2_CHARSTRING_H_