1// Copyright (c) 2010 The Chromium 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
14namespace ots {
15
16// Validates all charstrings in |char_strings_index|. Charstring is a small
17// language for font hinting defined in Adobe Technical Note #5177.
18// http://www.adobe.com/devnet/font/pdfs/5177.Type2.pdf
19//
20// The validation will fail if one of the following conditions is met:
21//  1. The code uses more than 48 values of argument stack.
22//  2. The code uses deeply nested subroutine calls (more than 10 levels.)
23//  3. The code passes invalid number of operands to an operator.
24//  4. The code calls an undefined global or local subroutine.
25//  5. The code uses one of the following operators that are unlikely used in
26//     an ordinary fonts, and could be dangerous: random, put, get, index, roll.
27//
28// Arguments:
29//  global_subrs_index: Global subroutines which could be called by a charstring
30//                      in |char_strings_index|.
31//  fd_select: A map from glyph # to font #.
32//  local_subrs_per_font: A list of Local Subrs associated with FDArrays. Can be
33//                        empty.
34//  local_subrs: A Local Subrs associated with Top DICT. Can be NULL.
35//  cff_table: A buffer which contains actual byte code of charstring, global
36//             subroutines and local subroutines.
37bool ValidateType2CharStringIndex(
38    const CFFIndex &char_strings_index,
39    const CFFIndex &global_subrs_index,
40    const std::map<uint16_t, uint8_t> &fd_select,
41    const std::vector<CFFIndex *> &local_subrs_per_font,
42    const CFFIndex *local_subrs,
43    Buffer *cff_table);
44
45// The list of Operators. See Appendix. A in Adobe Technical Note #5177.
46enum Type2CharStringOperator {
47  kHStem = 1,
48  kVStem = 3,
49  kVMoveTo = 4,
50  kRLineTo = 5,
51  kHLineTo = 6,
52  kVLineTo = 7,
53  kRRCurveTo = 8,
54  kCallSubr = 10,
55  kReturn = 11,
56  kEndChar = 14,
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  kAnd = (12 << 8) + 3,
71  kOr = (12 << 8) + 4,
72  kNot = (12 << 8) + 5,
73  kAbs = (12 << 8) + 9,
74  kAdd = (12 << 8) + 10,
75  kSub = (12 << 8) + 11,
76  kDiv = (12 << 8) + 12,
77  kNeg = (12 << 8) + 14,
78  kEq = (12 << 8) + 15,
79  kDrop = (12 << 8) + 18,
80  kPut = (12 << 8) + 20,
81  kGet = (12 << 8) + 21,
82  kIfElse = (12 << 8) + 22,
83  kRandom = (12 << 8) + 23,
84  kMul = (12 << 8) + 24,
85  kSqrt = (12 << 8) + 26,
86  kDup = (12 << 8) + 27,
87  kExch = (12 << 8) + 28,
88  kIndex = (12 << 8) + 29,
89  kRoll = (12 << 8) + 30,
90  kHFlex = (12 << 8) + 34,
91  kFlex = (12 << 8) + 35,
92  kHFlex1 = (12 << 8) + 36,
93  kFlex1 = (12 << 8) + 37,
94  // Operators that are obsoleted or undocumented, such as 'blend', will be
95  // rejected.
96};
97
98}  // namespace ots
99
100#endif  // OTS_CFF_TYPE2_CHARSTRING_H_
101