hb-cff-interp-dict-common.hh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright © 2018 Adobe Inc.
  3. *
  4. * This is part of HarfBuzz, a text shaping library.
  5. *
  6. * Permission is hereby granted, without written agreement and without
  7. * license or royalty fees, to use, copy, modify, and distribute this
  8. * software and its documentation for any purpose, provided that the
  9. * above copyright notice and the following two paragraphs appear in
  10. * all copies of this software.
  11. *
  12. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  13. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  14. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  15. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  16. * DAMAGE.
  17. *
  18. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  19. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  21. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  22. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  23. *
  24. * Adobe Author(s): Michiharu Ariza
  25. */
  26. #ifndef HB_CFF_INTERP_DICT_COMMON_HH
  27. #define HB_CFF_INTERP_DICT_COMMON_HH
  28. #include "hb-cff-interp-common.hh"
  29. namespace CFF {
  30. using namespace OT;
  31. /* an opstr and the parsed out dict value(s) */
  32. struct dict_val_t : op_str_t
  33. {
  34. void init () { single_val.set_int (0); }
  35. void fini () {}
  36. number_t single_val;
  37. };
  38. typedef dict_val_t num_dict_val_t;
  39. template <typename VAL> struct dict_values_t : parsed_values_t<VAL> {};
  40. template <typename OPSTR=op_str_t>
  41. struct top_dict_values_t : dict_values_t<OPSTR>
  42. {
  43. void init ()
  44. {
  45. dict_values_t<OPSTR>::init ();
  46. charStringsOffset = 0;
  47. FDArrayOffset = 0;
  48. }
  49. void fini () { dict_values_t<OPSTR>::fini (); }
  50. unsigned int charStringsOffset;
  51. unsigned int FDArrayOffset;
  52. };
  53. struct dict_opset_t : opset_t<number_t>
  54. {
  55. static void process_op (op_code_t op, interp_env_t<number_t>& env)
  56. {
  57. switch (op) {
  58. case OpCode_longintdict: /* 5-byte integer */
  59. env.argStack.push_longint_from_substr (env.str_ref);
  60. break;
  61. case OpCode_BCD: /* real number */
  62. env.argStack.push_real (parse_bcd (env.str_ref));
  63. break;
  64. default:
  65. opset_t<number_t>::process_op (op, env);
  66. break;
  67. }
  68. }
  69. /* Turns CFF's BCD format into strtod understandable string */
  70. static double parse_bcd (byte_str_ref_t& str_ref)
  71. {
  72. if (unlikely (str_ref.in_error ())) return .0;
  73. enum Nibble { DECIMAL=10, EXP_POS, EXP_NEG, RESERVED, NEG, END };
  74. char buf[32];
  75. unsigned char byte = 0;
  76. for (unsigned i = 0, count = 0; count < ARRAY_LENGTH (buf); ++i, ++count)
  77. {
  78. unsigned nibble;
  79. if (!(i & 1))
  80. {
  81. if (unlikely (!str_ref.avail ())) break;
  82. byte = str_ref[0];
  83. str_ref.inc ();
  84. nibble = byte >> 4;
  85. }
  86. else
  87. nibble = byte & 0x0F;
  88. if (unlikely (nibble == RESERVED)) break;
  89. else if (nibble == END)
  90. {
  91. const char *p = buf;
  92. double pv;
  93. if (unlikely (!hb_parse_double (&p, p + count, &pv, true/* whole buffer */)))
  94. break;
  95. return pv;
  96. }
  97. else
  98. {
  99. buf[count] = "0123456789.EE?-?"[nibble];
  100. if (nibble == EXP_NEG)
  101. {
  102. ++count;
  103. if (unlikely (count == ARRAY_LENGTH (buf))) break;
  104. buf[count] = '-';
  105. }
  106. }
  107. }
  108. str_ref.set_error ();
  109. return .0;
  110. }
  111. static bool is_hint_op (op_code_t op)
  112. {
  113. switch (op)
  114. {
  115. case OpCode_BlueValues:
  116. case OpCode_OtherBlues:
  117. case OpCode_FamilyBlues:
  118. case OpCode_FamilyOtherBlues:
  119. case OpCode_StemSnapH:
  120. case OpCode_StemSnapV:
  121. case OpCode_StdHW:
  122. case OpCode_StdVW:
  123. case OpCode_BlueScale:
  124. case OpCode_BlueShift:
  125. case OpCode_BlueFuzz:
  126. case OpCode_ForceBold:
  127. case OpCode_LanguageGroup:
  128. case OpCode_ExpansionFactor:
  129. return true;
  130. default:
  131. return false;
  132. }
  133. }
  134. };
  135. template <typename VAL=op_str_t>
  136. struct top_dict_opset_t : dict_opset_t
  137. {
  138. static void process_op (op_code_t op, interp_env_t<number_t>& env, top_dict_values_t<VAL> & dictval)
  139. {
  140. switch (op) {
  141. case OpCode_CharStrings:
  142. dictval.charStringsOffset = env.argStack.pop_uint ();
  143. env.clear_args ();
  144. break;
  145. case OpCode_FDArray:
  146. dictval.FDArrayOffset = env.argStack.pop_uint ();
  147. env.clear_args ();
  148. break;
  149. case OpCode_FontMatrix:
  150. env.clear_args ();
  151. break;
  152. default:
  153. dict_opset_t::process_op (op, env);
  154. break;
  155. }
  156. }
  157. };
  158. template <typename OPSET, typename PARAM, typename ENV=num_interp_env_t>
  159. struct dict_interpreter_t : interpreter_t<ENV>
  160. {
  161. dict_interpreter_t (ENV& env_) : interpreter_t<ENV> (env_) {}
  162. bool interpret (PARAM& param)
  163. {
  164. param.init ();
  165. while (SUPER::env.str_ref.avail ())
  166. {
  167. OPSET::process_op (SUPER::env.fetch_op (), SUPER::env, param);
  168. if (unlikely (SUPER::env.in_error ()))
  169. return false;
  170. }
  171. return true;
  172. }
  173. private:
  174. typedef interpreter_t<ENV> SUPER;
  175. };
  176. } /* namespace CFF */
  177. #endif /* HB_CFF_INTERP_DICT_COMMON_HH */