PDF Viewer - Version 0.1

Version Description

  • Initial release.

=

Download this release

Release Info

Developer envigeek
Plugin Icon 128x128 PDF Viewer
Version 0.1
Comparing to
See all releases

Version 0.1

Files changed (3) hide show
  1. beta/LICENSE +177 -0
  2. beta/build/pdf.js +8011 -0
  3. beta/build/pdf.worker.js +22331 -0
beta/LICENSE ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
beta/build/pdf.js ADDED
@@ -0,0 +1,8011 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
+ /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
3
+ /* Copyright 2012 Mozilla Foundation
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ /*jshint globalstrict: false */
18
+ /* globals PDFJS */
19
+
20
+ // Initializing PDFJS global object (if still undefined)
21
+ if (typeof PDFJS === 'undefined') {
22
+ (typeof window !== 'undefined' ? window : this).PDFJS = {};
23
+ }
24
+
25
+ PDFJS.version = '1.1.114';
26
+ PDFJS.build = '3fd44fd';
27
+
28
+ (function pdfjsWrapper() {
29
+ // Use strict in our context only - users might not want it
30
+ 'use strict';
31
+
32
+ /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
33
+ /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
34
+ /* Copyright 2012 Mozilla Foundation
35
+ *
36
+ * Licensed under the Apache License, Version 2.0 (the "License");
37
+ * you may not use this file except in compliance with the License.
38
+ * You may obtain a copy of the License at
39
+ *
40
+ * http://www.apache.org/licenses/LICENSE-2.0
41
+ *
42
+ * Unless required by applicable law or agreed to in writing, software
43
+ * distributed under the License is distributed on an "AS IS" BASIS,
44
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
45
+ * See the License for the specific language governing permissions and
46
+ * limitations under the License.
47
+ */
48
+ /* globals Cmd, ColorSpace, Dict, MozBlobBuilder, Name, PDFJS, Ref, URL,
49
+ Promise */
50
+
51
+ 'use strict';
52
+
53
+ var globalScope = (typeof window === 'undefined') ? this : window;
54
+
55
+ var isWorker = (typeof window === 'undefined');
56
+
57
+ var FONT_IDENTITY_MATRIX = [0.001, 0, 0, 0.001, 0, 0];
58
+
59
+ var TextRenderingMode = {
60
+ FILL: 0,
61
+ STROKE: 1,
62
+ FILL_STROKE: 2,
63
+ INVISIBLE: 3,
64
+ FILL_ADD_TO_PATH: 4,
65
+ STROKE_ADD_TO_PATH: 5,
66
+ FILL_STROKE_ADD_TO_PATH: 6,
67
+ ADD_TO_PATH: 7,
68
+ FILL_STROKE_MASK: 3,
69
+ ADD_TO_PATH_FLAG: 4
70
+ };
71
+
72
+ var ImageKind = {
73
+ GRAYSCALE_1BPP: 1,
74
+ RGB_24BPP: 2,
75
+ RGBA_32BPP: 3
76
+ };
77
+
78
+ var AnnotationType = {
79
+ WIDGET: 1,
80
+ TEXT: 2,
81
+ LINK: 3
82
+ };
83
+
84
+ var StreamType = {
85
+ UNKNOWN: 0,
86
+ FLATE: 1,
87
+ LZW: 2,
88
+ DCT: 3,
89
+ JPX: 4,
90
+ JBIG: 5,
91
+ A85: 6,
92
+ AHX: 7,
93
+ CCF: 8,
94
+ RL: 9
95
+ };
96
+
97
+ var FontType = {
98
+ UNKNOWN: 0,
99
+ TYPE1: 1,
100
+ TYPE1C: 2,
101
+ CIDFONTTYPE0: 3,
102
+ CIDFONTTYPE0C: 4,
103
+ TRUETYPE: 5,
104
+ CIDFONTTYPE2: 6,
105
+ TYPE3: 7,
106
+ OPENTYPE: 8,
107
+ TYPE0: 9,
108
+ MMTYPE1: 10
109
+ };
110
+
111
+ // The global PDFJS object exposes the API
112
+ // In production, it will be declared outside a global wrapper
113
+ // In development, it will be declared here
114
+ if (!globalScope.PDFJS) {
115
+ globalScope.PDFJS = {};
116
+ }
117
+
118
+ globalScope.PDFJS.pdfBug = false;
119
+
120
+ PDFJS.VERBOSITY_LEVELS = {
121
+ errors: 0,
122
+ warnings: 1,
123
+ infos: 5
124
+ };
125
+
126
+ // All the possible operations for an operator list.
127
+ var OPS = PDFJS.OPS = {
128
+ // Intentionally start from 1 so it is easy to spot bad operators that will be
129
+ // 0's.
130
+ dependency: 1,
131
+ setLineWidth: 2,
132
+ setLineCap: 3,
133
+ setLineJoin: 4,
134
+ setMiterLimit: 5,
135
+ setDash: 6,
136
+ setRenderingIntent: 7,
137
+ setFlatness: 8,
138
+ setGState: 9,
139
+ save: 10,
140
+ restore: 11,
141
+ transform: 12,
142
+ moveTo: 13,
143
+ lineTo: 14,
144
+ curveTo: 15,
145
+ curveTo2: 16,
146
+ curveTo3: 17,
147
+ closePath: 18,
148
+ rectangle: 19,
149
+ stroke: 20,
150
+ closeStroke: 21,
151
+ fill: 22,
152
+ eoFill: 23,
153
+ fillStroke: 24,
154
+ eoFillStroke: 25,
155
+ closeFillStroke: 26,
156
+ closeEOFillStroke: 27,
157
+ endPath: 28,
158
+ clip: 29,
159
+ eoClip: 30,
160
+ beginText: 31,
161
+ endText: 32,
162
+ setCharSpacing: 33,
163
+ setWordSpacing: 34,
164
+ setHScale: 35,
165
+ setLeading: 36,
166
+ setFont: 37,
167
+ setTextRenderingMode: 38,
168
+ setTextRise: 39,
169
+ moveText: 40,
170
+ setLeadingMoveText: 41,
171
+ setTextMatrix: 42,
172
+ nextLine: 43,
173
+ showText: 44,
174
+ showSpacedText: 45,
175
+ nextLineShowText: 46,
176
+ nextLineSetSpacingShowText: 47,
177
+ setCharWidth: 48,
178
+ setCharWidthAndBounds: 49,
179
+ setStrokeColorSpace: 50,
180
+ setFillColorSpace: 51,
181
+ setStrokeColor: 52,
182
+ setStrokeColorN: 53,
183
+ setFillColor: 54,
184
+ setFillColorN: 55,
185
+ setStrokeGray: 56,
186
+ setFillGray: 57,
187
+ setStrokeRGBColor: 58,
188
+ setFillRGBColor: 59,
189
+ setStrokeCMYKColor: 60,
190
+ setFillCMYKColor: 61,
191
+ shadingFill: 62,
192
+ beginInlineImage: 63,
193
+ beginImageData: 64,
194
+ endInlineImage: 65,
195
+ paintXObject: 66,
196
+ markPoint: 67,
197
+ markPointProps: 68,
198
+ beginMarkedContent: 69,
199
+ beginMarkedContentProps: 70,
200
+ endMarkedContent: 71,
201
+ beginCompat: 72,
202
+ endCompat: 73,
203
+ paintFormXObjectBegin: 74,
204
+ paintFormXObjectEnd: 75,
205
+ beginGroup: 76,
206
+ endGroup: 77,
207
+ beginAnnotations: 78,
208
+ endAnnotations: 79,
209
+ beginAnnotation: 80,
210
+ endAnnotation: 81,
211
+ paintJpegXObject: 82,
212
+ paintImageMaskXObject: 83,
213
+ paintImageMaskXObjectGroup: 84,
214
+ paintImageXObject: 85,
215
+ paintInlineImageXObject: 86,
216
+ paintInlineImageXObjectGroup: 87,
217
+ paintImageXObjectRepeat: 88,
218
+ paintImageMaskXObjectRepeat: 89,
219
+ paintSolidColorImageMask: 90,
220
+ constructPath: 91
221
+ };
222
+
223
+ // A notice for devs. These are good for things that are helpful to devs, such
224
+ // as warning that Workers were disabled, which is important to devs but not
225
+ // end users.
226
+ function info(msg) {
227
+ if (PDFJS.verbosity >= PDFJS.VERBOSITY_LEVELS.infos) {
228
+ console.log('Info: ' + msg);
229
+ }
230
+ }
231
+
232
+ // Non-fatal warnings.
233
+ function warn(msg) {
234
+ if (PDFJS.verbosity >= PDFJS.VERBOSITY_LEVELS.warnings) {
235
+ console.log('Warning: ' + msg);
236
+ }
237
+ }
238
+
239
+ // Fatal errors that should trigger the fallback UI and halt execution by
240
+ // throwing an exception.
241
+ function error(msg) {
242
+ if (PDFJS.verbosity >= PDFJS.VERBOSITY_LEVELS.errors) {
243
+ console.log('Error: ' + msg);
244
+ console.log(backtrace());
245
+ }
246
+ UnsupportedManager.notify(UNSUPPORTED_FEATURES.unknown);
247
+ throw new Error(msg);
248
+ }
249
+
250
+ function backtrace() {
251
+ try {
252
+ throw new Error();
253
+ } catch (e) {
254
+ return e.stack ? e.stack.split('\n').slice(2).join('\n') : '';
255
+ }
256
+ }
257
+
258
+ function assert(cond, msg) {
259
+ if (!cond) {
260
+ error(msg);
261
+ }
262
+ }
263
+
264
+ var UNSUPPORTED_FEATURES = PDFJS.UNSUPPORTED_FEATURES = {
265
+ unknown: 'unknown',
266
+ forms: 'forms',
267
+ javaScript: 'javaScript',
268
+ smask: 'smask',
269
+ shadingPattern: 'shadingPattern',
270
+ font: 'font'
271
+ };
272
+
273
+ var UnsupportedManager = PDFJS.UnsupportedManager =
274
+ (function UnsupportedManagerClosure() {
275
+ var listeners = [];
276
+ return {
277
+ listen: function (cb) {
278
+ listeners.push(cb);
279
+ },
280
+ notify: function (featureId) {
281
+ warn('Unsupported feature "' + featureId + '"');
282
+ for (var i = 0, ii = listeners.length; i < ii; i++) {
283
+ listeners[i](featureId);
284
+ }
285
+ }
286
+ };
287
+ })();
288
+
289
+ // Combines two URLs. The baseUrl shall be absolute URL. If the url is an
290
+ // absolute URL, it will be returned as is.
291
+ function combineUrl(baseUrl, url) {
292
+ if (!url) {
293
+ return baseUrl;
294
+ }
295
+ if (/^[a-z][a-z0-9+\-.]*:/i.test(url)) {
296
+ return url;
297
+ }
298
+ var i;
299
+ if (url.charAt(0) === '/') {
300
+ // absolute path
301
+ i = baseUrl.indexOf('://');
302
+ if (url.charAt(1) === '/') {
303
+ ++i;
304
+ } else {
305
+ i = baseUrl.indexOf('/', i + 3);
306
+ }
307
+ return baseUrl.substring(0, i) + url;
308
+ } else {
309
+ // relative path
310
+ var pathLength = baseUrl.length;
311
+ i = baseUrl.lastIndexOf('#');
312
+ pathLength = i >= 0 ? i : pathLength;
313
+ i = baseUrl.lastIndexOf('?', pathLength);
314
+ pathLength = i >= 0 ? i : pathLength;
315
+ var prefixLength = baseUrl.lastIndexOf('/', pathLength);
316
+ return baseUrl.substring(0, prefixLength + 1) + url;
317
+ }
318
+ }
319
+
320
+ // Validates if URL is safe and allowed, e.g. to avoid XSS.
321
+ function isValidUrl(url, allowRelative) {
322
+ if (!url) {
323
+ return false;
324
+ }
325
+ // RFC 3986 (http://tools.ietf.org/html/rfc3986#section-3.1)
326
+ // scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
327
+ var protocol = /^[a-z][a-z0-9+\-.]*(?=:)/i.exec(url);
328
+ if (!protocol) {
329
+ return allowRelative;
330
+ }
331
+ protocol = protocol[0].toLowerCase();
332
+ switch (protocol) {
333
+ case 'http':
334
+ case 'https':
335
+ case 'ftp':
336
+ case 'mailto':
337
+ case 'tel':
338
+ return true;
339
+ default:
340
+ return false;
341
+ }
342
+ }
343
+ PDFJS.isValidUrl = isValidUrl;
344
+
345
+ function shadow(obj, prop, value) {
346
+ Object.defineProperty(obj, prop, { value: value,
347
+ enumerable: true,
348
+ configurable: true,
349
+ writable: false });
350
+ return value;
351
+ }
352
+ PDFJS.shadow = shadow;
353
+
354
+ var PasswordResponses = PDFJS.PasswordResponses = {
355
+ NEED_PASSWORD: 1,
356
+ INCORRECT_PASSWORD: 2
357
+ };
358
+
359
+ var PasswordException = (function PasswordExceptionClosure() {
360
+ function PasswordException(msg, code) {
361
+ this.name = 'PasswordException';
362
+ this.message = msg;
363
+ this.code = code;
364
+ }
365
+
366
+ PasswordException.prototype = new Error();
367
+ PasswordException.constructor = PasswordException;
368
+
369
+ return PasswordException;
370
+ })();
371
+ PDFJS.PasswordException = PasswordException;
372
+
373
+ var UnknownErrorException = (function UnknownErrorExceptionClosure() {
374
+ function UnknownErrorException(msg, details) {
375
+ this.name = 'UnknownErrorException';
376
+ this.message = msg;
377
+ this.details = details;
378
+ }
379
+
380
+ UnknownErrorException.prototype = new Error();
381
+ UnknownErrorException.constructor = UnknownErrorException;
382
+
383
+ return UnknownErrorException;
384
+ })();
385
+ PDFJS.UnknownErrorException = UnknownErrorException;
386
+
387
+ var InvalidPDFException = (function InvalidPDFExceptionClosure() {
388
+ function InvalidPDFException(msg) {
389
+ this.name = 'InvalidPDFException';
390
+ this.message = msg;
391
+ }
392
+
393
+ InvalidPDFException.prototype = new Error();
394
+ InvalidPDFException.constructor = InvalidPDFException;
395
+
396
+ return InvalidPDFException;
397
+ })();
398
+ PDFJS.InvalidPDFException = InvalidPDFException;
399
+
400
+ var MissingPDFException = (function MissingPDFExceptionClosure() {
401
+ function MissingPDFException(msg) {
402
+ this.name = 'MissingPDFException';
403
+ this.message = msg;
404
+ }
405
+
406
+ MissingPDFException.prototype = new Error();
407
+ MissingPDFException.constructor = MissingPDFException;
408
+
409
+ return MissingPDFException;
410
+ })();
411
+ PDFJS.MissingPDFException = MissingPDFException;
412
+
413
+ var UnexpectedResponseException =
414
+ (function UnexpectedResponseExceptionClosure() {
415
+ function UnexpectedResponseException(msg, status) {
416
+ this.name = 'UnexpectedResponseException';
417
+ this.message = msg;
418
+ this.status = status;
419
+ }
420
+
421
+ UnexpectedResponseException.prototype = new Error();
422
+ UnexpectedResponseException.constructor = UnexpectedResponseException;
423
+
424
+ return UnexpectedResponseException;
425
+ })();
426
+ PDFJS.UnexpectedResponseException = UnexpectedResponseException;
427
+
428
+ var NotImplementedException = (function NotImplementedExceptionClosure() {
429
+ function NotImplementedException(msg) {
430
+ this.message = msg;
431
+ }
432
+
433
+ NotImplementedException.prototype = new Error();
434
+ NotImplementedException.prototype.name = 'NotImplementedException';
435
+ NotImplementedException.constructor = NotImplementedException;
436
+
437
+ return NotImplementedException;
438
+ })();
439
+
440
+ var MissingDataException = (function MissingDataExceptionClosure() {
441
+ function MissingDataException(begin, end) {
442
+ this.begin = begin;
443
+ this.end = end;
444
+ this.message = 'Missing data [' + begin + ', ' + end + ')';
445
+ }
446
+
447
+ MissingDataException.prototype = new Error();
448
+ MissingDataException.prototype.name = 'MissingDataException';
449
+ MissingDataException.constructor = MissingDataException;
450
+
451
+ return MissingDataException;
452
+ })();
453
+
454
+ var XRefParseException = (function XRefParseExceptionClosure() {
455
+ function XRefParseException(msg) {
456
+ this.message = msg;
457
+ }
458
+
459
+ XRefParseException.prototype = new Error();
460
+ XRefParseException.prototype.name = 'XRefParseException';
461
+ XRefParseException.constructor = XRefParseException;
462
+
463
+ return XRefParseException;
464
+ })();
465
+
466
+
467
+ function bytesToString(bytes) {
468
+ assert(bytes !== null && typeof bytes === 'object' &&
469
+ bytes.length !== undefined, 'Invalid argument for bytesToString');
470
+ var length = bytes.length;
471
+ var MAX_ARGUMENT_COUNT = 8192;
472
+ if (length < MAX_ARGUMENT_COUNT) {
473
+ return String.fromCharCode.apply(null, bytes);
474
+ }
475
+ var strBuf = [];
476
+ for (var i = 0; i < length; i += MAX_ARGUMENT_COUNT) {
477
+ var chunkEnd = Math.min(i + MAX_ARGUMENT_COUNT, length);
478
+ var chunk = bytes.subarray(i, chunkEnd);
479
+ strBuf.push(String.fromCharCode.apply(null, chunk));
480
+ }
481
+ return strBuf.join('');
482
+ }
483
+
484
+ function stringToBytes(str) {
485
+ assert(typeof str === 'string', 'Invalid argument for stringToBytes');
486
+ var length = str.length;
487
+ var bytes = new Uint8Array(length);
488
+ for (var i = 0; i < length; ++i) {
489
+ bytes[i] = str.charCodeAt(i) & 0xFF;
490
+ }
491
+ return bytes;
492
+ }
493
+
494
+ function string32(value) {
495
+ return String.fromCharCode((value >> 24) & 0xff, (value >> 16) & 0xff,
496
+ (value >> 8) & 0xff, value & 0xff);
497
+ }
498
+
499
+ function log2(x) {
500
+ var n = 1, i = 0;
501
+ while (x > n) {
502
+ n <<= 1;
503
+ i++;
504
+ }
505
+ return i;
506
+ }
507
+
508
+ function readInt8(data, start) {
509
+ return (data[start] << 24) >> 24;
510
+ }
511
+
512
+ function readUint16(data, offset) {
513
+ return (data[offset] << 8) | data[offset + 1];
514
+ }
515
+
516
+ function readUint32(data, offset) {
517
+ return ((data[offset] << 24) | (data[offset + 1] << 16) |
518
+ (data[offset + 2] << 8) | data[offset + 3]) >>> 0;
519
+ }
520
+
521
+ // Lazy test the endianness of the platform
522
+ // NOTE: This will be 'true' for simulated TypedArrays
523
+ function isLittleEndian() {
524
+ var buffer8 = new Uint8Array(2);
525
+ buffer8[0] = 1;
526
+ var buffer16 = new Uint16Array(buffer8.buffer);
527
+ return (buffer16[0] === 1);
528
+ }
529
+
530
+ Object.defineProperty(PDFJS, 'isLittleEndian', {
531
+ configurable: true,
532
+ get: function PDFJS_isLittleEndian() {
533
+ return shadow(PDFJS, 'isLittleEndian', isLittleEndian());
534
+ }
535
+ });
536
+
537
+ // Lazy test if the userAgant support CanvasTypedArrays
538
+ function hasCanvasTypedArrays() {
539
+ var canvas = document.createElement('canvas');
540
+ canvas.width = canvas.height = 1;
541
+ var ctx = canvas.getContext('2d');
542
+ var imageData = ctx.createImageData(1, 1);
543
+ return (typeof imageData.data.buffer !== 'undefined');
544
+ }
545
+
546
+ Object.defineProperty(PDFJS, 'hasCanvasTypedArrays', {
547
+ configurable: true,
548
+ get: function PDFJS_hasCanvasTypedArrays() {
549
+ return shadow(PDFJS, 'hasCanvasTypedArrays', hasCanvasTypedArrays());
550
+ }
551
+ });
552
+
553
+ var Uint32ArrayView = (function Uint32ArrayViewClosure() {
554
+
555
+ function Uint32ArrayView(buffer, length) {
556
+ this.buffer = buffer;
557
+ this.byteLength = buffer.length;
558
+ this.length = length === undefined ? (this.byteLength >> 2) : length;
559
+ ensureUint32ArrayViewProps(this.length);
560
+ }
561
+ Uint32ArrayView.prototype = Object.create(null);
562
+
563
+ var uint32ArrayViewSetters = 0;
564
+ function createUint32ArrayProp(index) {
565
+ return {
566
+ get: function () {
567
+ var buffer = this.buffer, offset = index << 2;
568
+ return (buffer[offset] | (buffer[offset + 1] << 8) |
569
+ (buffer[offset + 2] << 16) | (buffer[offset + 3] << 24)) >>> 0;
570
+ },
571
+ set: function (value) {
572
+ var buffer = this.buffer, offset = index << 2;
573
+ buffer[offset] = value & 255;
574
+ buffer[offset + 1] = (value >> 8) & 255;
575
+ buffer[offset + 2] = (value >> 16) & 255;
576
+ buffer[offset + 3] = (value >>> 24) & 255;
577
+ }
578
+ };
579
+ }
580
+
581
+ function ensureUint32ArrayViewProps(length) {
582
+ while (uint32ArrayViewSetters < length) {
583
+ Object.defineProperty(Uint32ArrayView.prototype,
584
+ uint32ArrayViewSetters,
585
+ createUint32ArrayProp(uint32ArrayViewSetters));
586
+ uint32ArrayViewSetters++;
587
+ }
588
+ }
589
+
590
+ return Uint32ArrayView;
591
+ })();
592
+
593
+ var IDENTITY_MATRIX = [1, 0, 0, 1, 0, 0];
594
+
595
+ var Util = PDFJS.Util = (function UtilClosure() {
596
+ function Util() {}
597
+
598
+ var rgbBuf = ['rgb(', 0, ',', 0, ',', 0, ')'];
599
+
600
+ // makeCssRgb() can be called thousands of times. Using |rgbBuf| avoids
601
+ // creating many intermediate strings.
602
+ Util.makeCssRgb = function Util_makeCssRgb(r, g, b) {
603
+ rgbBuf[1] = r;
604
+ rgbBuf[3] = g;
605
+ rgbBuf[5] = b;
606
+ return rgbBuf.join('');
607
+ };
608
+
609
+ // Concatenates two transformation matrices together and returns the result.
610
+ Util.transform = function Util_transform(m1, m2) {
611
+ return [
612
+ m1[0] * m2[0] + m1[2] * m2[1],
613
+ m1[1] * m2[0] + m1[3] * m2[1],
614
+ m1[0] * m2[2] + m1[2] * m2[3],
615
+ m1[1] * m2[2] + m1[3] * m2[3],
616
+ m1[0] * m2[4] + m1[2] * m2[5] + m1[4],
617
+ m1[1] * m2[4] + m1[3] * m2[5] + m1[5]
618
+ ];
619
+ };
620
+
621
+ // For 2d affine transforms
622
+ Util.applyTransform = function Util_applyTransform(p, m) {
623
+ var xt = p[0] * m[0] + p[1] * m[2] + m[4];
624
+ var yt = p[0] * m[1] + p[1] * m[3] + m[5];
625
+ return [xt, yt];
626
+ };
627
+
628
+ Util.applyInverseTransform = function Util_applyInverseTransform(p, m) {
629
+ var d = m[0] * m[3] - m[1] * m[2];
630
+ var xt = (p[0] * m[3] - p[1] * m[2] + m[2] * m[5] - m[4] * m[3]) / d;
631
+ var yt = (-p[0] * m[1] + p[1] * m[0] + m[4] * m[1] - m[5] * m[0]) / d;
632
+ return [xt, yt];
633
+ };
634
+
635
+ // Applies the transform to the rectangle and finds the minimum axially
636
+ // aligned bounding box.
637
+ Util.getAxialAlignedBoundingBox =
638
+ function Util_getAxialAlignedBoundingBox(r, m) {
639
+
640
+ var p1 = Util.applyTransform(r, m);
641
+ var p2 = Util.applyTransform(r.slice(2, 4), m);
642
+ var p3 = Util.applyTransform([r[0], r[3]], m);
643
+ var p4 = Util.applyTransform([r[2], r[1]], m);
644
+ return [
645
+ Math.min(p1[0], p2[0], p3[0], p4[0]),
646
+ Math.min(p1[1], p2[1], p3[1], p4[1]),
647
+ Math.max(p1[0], p2[0], p3[0], p4[0]),
648
+ Math.max(p1[1], p2[1], p3[1], p4[1])
649
+ ];
650
+ };
651
+
652
+ Util.inverseTransform = function Util_inverseTransform(m) {
653
+ var d = m[0] * m[3] - m[1] * m[2];
654
+ return [m[3] / d, -m[1] / d, -m[2] / d, m[0] / d,
655
+ (m[2] * m[5] - m[4] * m[3]) / d, (m[4] * m[1] - m[5] * m[0]) / d];
656
+ };
657
+
658
+ // Apply a generic 3d matrix M on a 3-vector v:
659
+ // | a b c | | X |
660
+ // | d e f | x | Y |
661
+ // | g h i | | Z |
662
+ // M is assumed to be serialized as [a,b,c,d,e,f,g,h,i],
663
+ // with v as [X,Y,Z]
664
+ Util.apply3dTransform = function Util_apply3dTransform(m, v) {
665
+ return [
666
+ m[0] * v[0] + m[1] * v[1] + m[2] * v[2],
667
+ m[3] * v[0] + m[4] * v[1] + m[5] * v[2],
668
+ m[6] * v[0] + m[7] * v[1] + m[8] * v[2]
669
+ ];
670
+ };
671
+
672
+ // This calculation uses Singular Value Decomposition.
673
+ // The SVD can be represented with formula A = USV. We are interested in the
674
+ // matrix S here because it represents the scale values.
675
+ Util.singularValueDecompose2dScale =
676
+ function Util_singularValueDecompose2dScale(m) {
677
+
678
+ var transpose = [m[0], m[2], m[1], m[3]];
679
+
680
+ // Multiply matrix m with its transpose.
681
+ var a = m[0] * transpose[0] + m[1] * transpose[2];
682
+ var b = m[0] * transpose[1] + m[1] * transpose[3];
683
+ var c = m[2] * transpose[0] + m[3] * transpose[2];
684
+ var d = m[2] * transpose[1] + m[3] * transpose[3];
685
+
686
+ // Solve the second degree polynomial to get roots.
687
+ var first = (a + d) / 2;
688
+ var second = Math.sqrt((a + d) * (a + d) - 4 * (a * d - c * b)) / 2;
689
+ var sx = first + second || 1;
690
+ var sy = first - second || 1;
691
+
692
+ // Scale values are the square roots of the eigenvalues.
693
+ return [Math.sqrt(sx), Math.sqrt(sy)];
694
+ };
695
+
696
+ // Normalize rectangle rect=[x1, y1, x2, y2] so that (x1,y1) < (x2,y2)
697
+ // For coordinate systems whose origin lies in the bottom-left, this
698
+ // means normalization to (BL,TR) ordering. For systems with origin in the
699
+ // top-left, this means (TL,BR) ordering.
700
+ Util.normalizeRect = function Util_normalizeRect(rect) {
701
+ var r = rect.slice(0); // clone rect
702
+ if (rect[0] > rect[2]) {
703
+ r[0] = rect[2];
704
+ r[2] = rect[0];
705
+ }
706
+ if (rect[1] > rect[3]) {
707
+ r[1] = rect[3];
708
+ r[3] = rect[1];
709
+ }
710
+ return r;
711
+ };
712
+
713
+ // Returns a rectangle [x1, y1, x2, y2] corresponding to the
714
+ // intersection of rect1 and rect2. If no intersection, returns 'false'
715
+ // The rectangle coordinates of rect1, rect2 should be [x1, y1, x2, y2]
716
+ Util.intersect = function Util_intersect(rect1, rect2) {
717
+ function compare(a, b) {
718
+ return a - b;
719
+ }
720
+
721
+ // Order points along the axes
722
+ var orderedX = [rect1[0], rect1[2], rect2[0], rect2[2]].sort(compare),
723
+ orderedY = [rect1[1], rect1[3], rect2[1], rect2[3]].sort(compare),
724
+ result = [];
725
+
726
+ rect1 = Util.normalizeRect(rect1);
727
+ rect2 = Util.normalizeRect(rect2);
728
+
729
+ // X: first and second points belong to different rectangles?
730
+ if ((orderedX[0] === rect1[0] && orderedX[1] === rect2[0]) ||
731
+ (orderedX[0] === rect2[0] && orderedX[1] === rect1[0])) {
732
+ // Intersection must be between second and third points
733
+ result[0] = orderedX[1];
734
+ result[2] = orderedX[2];
735
+ } else {
736
+ return false;
737
+ }
738
+
739
+ // Y: first and second points belong to different rectangles?
740
+ if ((orderedY[0] === rect1[1] && orderedY[1] === rect2[1]) ||
741
+ (orderedY[0] === rect2[1] && orderedY[1] === rect1[1])) {
742
+ // Intersection must be between second and third points
743
+ result[1] = orderedY[1];
744
+ result[3] = orderedY[2];
745
+ } else {
746
+ return false;
747
+ }
748
+
749
+ return result;
750
+ };
751
+
752
+ Util.sign = function Util_sign(num) {
753
+ return num < 0 ? -1 : 1;
754
+ };
755
+
756
+ Util.appendToArray = function Util_appendToArray(arr1, arr2) {
757
+ Array.prototype.push.apply(arr1, arr2);
758
+ };
759
+
760
+ Util.prependToArray = function Util_prependToArray(arr1, arr2) {
761
+ Array.prototype.unshift.apply(arr1, arr2);
762
+ };
763
+
764
+ Util.extendObj = function extendObj(obj1, obj2) {
765
+ for (var key in obj2) {
766
+ obj1[key] = obj2[key];
767
+ }
768
+ };
769
+
770
+ Util.getInheritableProperty = function Util_getInheritableProperty(dict,
771
+ name) {
772
+ while (dict && !dict.has(name)) {
773
+ dict = dict.get('Parent');
774
+ }
775
+ if (!dict) {
776
+ return null;
777
+ }
778
+ return dict.get(name);
779
+ };
780
+
781
+ Util.inherit = function Util_inherit(sub, base, prototype) {
782
+ sub.prototype = Object.create(base.prototype);
783
+ sub.prototype.constructor = sub;
784
+ for (var prop in prototype) {
785
+ sub.prototype[prop] = prototype[prop];
786
+ }
787
+ };
788
+
789
+ Util.loadScript = function Util_loadScript(src, callback) {
790
+ var script = document.createElement('script');
791
+ var loaded = false;
792
+ script.setAttribute('src', src);
793
+ if (callback) {
794
+ script.onload = function() {
795
+ if (!loaded) {
796
+ callback();
797
+ }
798
+ loaded = true;
799
+ };
800
+ }
801
+ document.getElementsByTagName('head')[0].appendChild(script);
802
+ };
803
+
804
+ return Util;
805
+ })();
806
+
807
+ /**
808
+ * PDF page viewport created based on scale, rotation and offset.
809
+ * @class
810
+ * @alias PDFJS.PageViewport
811
+ */
812
+ var PageViewport = PDFJS.PageViewport = (function PageViewportClosure() {
813
+ /**
814
+ * @constructor
815
+ * @private
816
+ * @param viewBox {Array} xMin, yMin, xMax and yMax coordinates.
817
+ * @param scale {number} scale of the viewport.
818
+ * @param rotation {number} rotations of the viewport in degrees.
819
+ * @param offsetX {number} offset X
820
+ * @param offsetY {number} offset Y
821
+ * @param dontFlip {boolean} if true, axis Y will not be flipped.
822
+ */
823
+ function PageViewport(viewBox, scale, rotation, offsetX, offsetY, dontFlip) {
824
+ this.viewBox = viewBox;
825
+ this.scale = scale;
826
+ this.rotation = rotation;
827
+ this.offsetX = offsetX;
828
+ this.offsetY = offsetY;
829
+
830
+ // creating transform to convert pdf coordinate system to the normal
831
+ // canvas like coordinates taking in account scale and rotation
832
+ var centerX = (viewBox[2] + viewBox[0]) / 2;
833
+ var centerY = (viewBox[3] + viewBox[1]) / 2;
834
+ var rotateA, rotateB, rotateC, rotateD;
835
+ rotation = rotation % 360;
836
+ rotation = rotation < 0 ? rotation + 360 : rotation;
837
+ switch (rotation) {
838
+ case 180:
839
+ rotateA = -1; rotateB = 0; rotateC = 0; rotateD = 1;
840
+ break;
841
+ case 90:
842
+ rotateA = 0; rotateB = 1; rotateC = 1; rotateD = 0;
843
+ break;
844
+ case 270:
845
+ rotateA = 0; rotateB = -1; rotateC = -1; rotateD = 0;
846
+ break;
847
+ //case 0:
848
+ default:
849
+ rotateA = 1; rotateB = 0; rotateC = 0; rotateD = -1;
850
+ break;
851
+ }
852
+
853
+ if (dontFlip) {
854
+ rotateC = -rotateC; rotateD = -rotateD;
855
+ }
856
+
857
+ var offsetCanvasX, offsetCanvasY;
858
+ var width, height;
859
+ if (rotateA === 0) {
860
+ offsetCanvasX = Math.abs(centerY - viewBox[1]) * scale + offsetX;
861
+ offsetCanvasY = Math.abs(centerX - viewBox[0]) * scale + offsetY;
862
+ width = Math.abs(viewBox[3] - viewBox[1]) * scale;
863
+ height = Math.abs(viewBox[2] - viewBox[0]) * scale;
864
+ } else {
865
+ offsetCanvasX = Math.abs(centerX - viewBox[0]) * scale + offsetX;
866
+ offsetCanvasY = Math.abs(centerY - viewBox[1]) * scale + offsetY;
867
+ width = Math.abs(viewBox[2] - viewBox[0]) * scale;
868
+ height = Math.abs(viewBox[3] - viewBox[1]) * scale;
869
+ }
870
+ // creating transform for the following operations:
871
+ // translate(-centerX, -centerY), rotate and flip vertically,
872
+ // scale, and translate(offsetCanvasX, offsetCanvasY)
873
+ this.transform = [
874
+ rotateA * scale,
875
+ rotateB * scale,
876
+ rotateC * scale,
877
+ rotateD * scale,
878
+ offsetCanvasX - rotateA * scale * centerX - rotateC * scale * centerY,
879
+ offsetCanvasY - rotateB * scale * centerX - rotateD * scale * centerY
880
+ ];
881
+
882
+ this.width = width;
883
+ this.height = height;
884
+ this.fontScale = scale;
885
+ }
886
+ PageViewport.prototype = /** @lends PDFJS.PageViewport.prototype */ {
887
+ /**
888
+ * Clones viewport with additional properties.
889
+ * @param args {Object} (optional) If specified, may contain the 'scale' or
890
+ * 'rotation' properties to override the corresponding properties in
891
+ * the cloned viewport.
892
+ * @returns {PDFJS.PageViewport} Cloned viewport.
893
+ */
894
+ clone: function PageViewPort_clone(args) {
895
+ args = args || {};
896
+ var scale = 'scale' in args ? args.scale : this.scale;
897
+ var rotation = 'rotation' in args ? args.rotation : this.rotation;
898
+ return new PageViewport(this.viewBox.slice(), scale, rotation,
899
+ this.offsetX, this.offsetY, args.dontFlip);
900
+ },
901
+ /**
902
+ * Converts PDF point to the viewport coordinates. For examples, useful for
903
+ * converting PDF location into canvas pixel coordinates.
904
+ * @param x {number} X coordinate.
905
+ * @param y {number} Y coordinate.
906
+ * @returns {Object} Object that contains 'x' and 'y' properties of the
907
+ * point in the viewport coordinate space.
908
+ * @see {@link convertToPdfPoint}
909
+ * @see {@link convertToViewportRectangle}
910
+ */
911
+ convertToViewportPoint: function PageViewport_convertToViewportPoint(x, y) {
912
+ return Util.applyTransform([x, y], this.transform);
913
+ },
914
+ /**
915
+ * Converts PDF rectangle to the viewport coordinates.
916
+ * @param rect {Array} xMin, yMin, xMax and yMax coordinates.
917
+ * @returns {Array} Contains corresponding coordinates of the rectangle
918
+ * in the viewport coordinate space.
919
+ * @see {@link convertToViewportPoint}
920
+ */
921
+ convertToViewportRectangle:
922
+ function PageViewport_convertToViewportRectangle(rect) {
923
+ var tl = Util.applyTransform([rect[0], rect[1]], this.transform);
924
+ var br = Util.applyTransform([rect[2], rect[3]], this.transform);
925
+ return [tl[0], tl[1], br[0], br[1]];
926
+ },
927
+ /**
928
+ * Converts viewport coordinates to the PDF location. For examples, useful
929
+ * for converting canvas pixel location into PDF one.
930
+ * @param x {number} X coordinate.
931
+ * @param y {number} Y coordinate.
932
+ * @returns {Object} Object that contains 'x' and 'y' properties of the
933
+ * point in the PDF coordinate space.
934
+ * @see {@link convertToViewportPoint}
935
+ */
936
+ convertToPdfPoint: function PageViewport_convertToPdfPoint(x, y) {
937
+ return Util.applyInverseTransform([x, y], this.transform);
938
+ }
939
+ };
940
+ return PageViewport;
941
+ })();
942
+
943
+ var PDFStringTranslateTable = [
944
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
945
+ 0x2D8, 0x2C7, 0x2C6, 0x2D9, 0x2DD, 0x2DB, 0x2DA, 0x2DC, 0, 0, 0, 0, 0, 0, 0,
946
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
947
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
948
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
949
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x2022, 0x2020, 0x2021, 0x2026, 0x2014,
950
+ 0x2013, 0x192, 0x2044, 0x2039, 0x203A, 0x2212, 0x2030, 0x201E, 0x201C,
951
+ 0x201D, 0x2018, 0x2019, 0x201A, 0x2122, 0xFB01, 0xFB02, 0x141, 0x152, 0x160,
952
+ 0x178, 0x17D, 0x131, 0x142, 0x153, 0x161, 0x17E, 0, 0x20AC
953
+ ];
954
+
955
+ function stringToPDFString(str) {
956
+ var i, n = str.length, strBuf = [];
957
+ if (str[0] === '\xFE' && str[1] === '\xFF') {
958
+ // UTF16BE BOM
959
+ for (i = 2; i < n; i += 2) {
960
+ strBuf.push(String.fromCharCode(
961
+ (str.charCodeAt(i) << 8) | str.charCodeAt(i + 1)));
962
+ }
963
+ } else {
964
+ for (i = 0; i < n; ++i) {
965
+ var code = PDFStringTranslateTable[str.charCodeAt(i)];
966
+ strBuf.push(code ? String.fromCharCode(code) : str.charAt(i));
967
+ }
968
+ }
969
+ return strBuf.join('');
970
+ }
971
+
972
+ function stringToUTF8String(str) {
973
+ return decodeURIComponent(escape(str));
974
+ }
975
+
976
+ function isEmptyObj(obj) {
977
+ for (var key in obj) {
978
+ return false;
979
+ }
980
+ return true;
981
+ }
982
+
983
+ function isBool(v) {
984
+ return typeof v === 'boolean';
985
+ }
986
+
987
+ function isInt(v) {
988
+ return typeof v === 'number' && ((v | 0) === v);
989
+ }
990
+
991
+ function isNum(v) {
992
+ return typeof v === 'number';
993
+ }
994
+
995
+ function isString(v) {
996
+ return typeof v === 'string';
997
+ }
998
+
999
+ function isName(v) {
1000
+ return v instanceof Name;
1001
+ }
1002
+
1003
+ function isCmd(v, cmd) {
1004
+ return v instanceof Cmd && (cmd === undefined || v.cmd === cmd);
1005
+ }
1006
+
1007
+ function isDict(v, type) {
1008
+ if (!(v instanceof Dict)) {
1009
+ return false;
1010
+ }
1011
+ if (!type) {
1012
+ return true;
1013
+ }
1014
+ var dictType = v.get('Type');
1015
+ return isName(dictType) && dictType.name === type;
1016
+ }
1017
+
1018
+ function isArray(v) {
1019
+ return v instanceof Array;
1020
+ }
1021
+
1022
+ function isStream(v) {
1023
+ return typeof v === 'object' && v !== null && v.getBytes !== undefined;
1024
+ }
1025
+
1026
+ function isArrayBuffer(v) {
1027
+ return typeof v === 'object' && v !== null && v.byteLength !== undefined;
1028
+ }
1029
+
1030
+ function isRef(v) {
1031
+ return v instanceof Ref;
1032
+ }
1033
+
1034
+ /**
1035
+ * Promise Capability object.
1036
+ *
1037
+ * @typedef {Object} PromiseCapability
1038
+ * @property {Promise} promise - A promise object.
1039
+ * @property {function} resolve - Fullfills the promise.
1040
+ * @property {function} reject - Rejects the promise.
1041
+ */
1042
+
1043
+ /**
1044
+ * Creates a promise capability object.
1045
+ * @alias PDFJS.createPromiseCapability
1046
+ *
1047
+ * @return {PromiseCapability} A capability object contains:
1048
+ * - a Promise, resolve and reject methods.
1049
+ */
1050
+ function createPromiseCapability() {
1051
+ var capability = {};
1052
+ capability.promise = new Promise(function (resolve, reject) {
1053
+ capability.resolve = resolve;
1054
+ capability.reject = reject;
1055
+ });
1056
+ return capability;
1057
+ }
1058
+
1059
+ PDFJS.createPromiseCapability = createPromiseCapability;
1060
+
1061
+ /**
1062
+ * Polyfill for Promises:
1063
+ * The following promise implementation tries to generally implement the
1064
+ * Promise/A+ spec. Some notable differences from other promise libaries are:
1065
+ * - There currently isn't a seperate deferred and promise object.
1066
+ * - Unhandled rejections eventually show an error if they aren't handled.
1067
+ *
1068
+ * Based off of the work in:
1069
+ * https://bugzilla.mozilla.org/show_bug.cgi?id=810490
1070
+ */
1071
+ (function PromiseClosure() {
1072
+ if (globalScope.Promise) {
1073
+ // Promises existing in the DOM/Worker, checking presence of all/resolve
1074
+ if (typeof globalScope.Promise.all !== 'function') {
1075
+ globalScope.Promise.all = function (iterable) {
1076
+ var count = 0, results = [], resolve, reject;
1077
+ var promise = new globalScope.Promise(function (resolve_, reject_) {
1078
+ resolve = resolve_;
1079
+ reject = reject_;
1080
+ });
1081
+ iterable.forEach(function (p, i) {
1082
+ count++;
1083
+ p.then(function (result) {
1084
+ results[i] = result;
1085
+ count--;
1086
+ if (count === 0) {
1087
+ resolve(results);
1088
+ }
1089
+ }, reject);
1090
+ });
1091
+ if (count === 0) {
1092
+ resolve(results);
1093
+ }
1094
+ return promise;
1095
+ };
1096
+ }
1097
+ if (typeof globalScope.Promise.resolve !== 'function') {
1098
+ globalScope.Promise.resolve = function (value) {
1099
+ return new globalScope.Promise(function (resolve) { resolve(value); });
1100
+ };
1101
+ }
1102
+ if (typeof globalScope.Promise.reject !== 'function') {
1103
+ globalScope.Promise.reject = function (reason) {
1104
+ return new globalScope.Promise(function (resolve, reject) {
1105
+ reject(reason);
1106
+ });
1107
+ };
1108
+ }
1109
+ if (typeof globalScope.Promise.prototype.catch !== 'function') {
1110
+ globalScope.Promise.prototype.catch = function (onReject) {
1111
+ return globalScope.Promise.prototype.then(undefined, onReject);
1112
+ };
1113
+ }
1114
+ return;
1115
+ }
1116
+ var STATUS_PENDING = 0;
1117
+ var STATUS_RESOLVED = 1;
1118
+ var STATUS_REJECTED = 2;
1119
+
1120
+ // In an attempt to avoid silent exceptions, unhandled rejections are
1121
+ // tracked and if they aren't handled in a certain amount of time an
1122
+ // error is logged.
1123
+ var REJECTION_TIMEOUT = 500;
1124
+
1125
+ var HandlerManager = {
1126
+ handlers: [],
1127
+ running: false,
1128
+ unhandledRejections: [],
1129
+ pendingRejectionCheck: false,
1130
+
1131
+ scheduleHandlers: function scheduleHandlers(promise) {
1132
+ if (promise._status === STATUS_PENDING) {
1133
+ return;
1134
+ }
1135
+
1136
+ this.handlers = this.handlers.concat(promise._handlers);
1137
+ promise._handlers = [];
1138
+
1139
+ if (this.running) {
1140
+ return;
1141
+ }
1142
+ this.running = true;
1143
+
1144
+ setTimeout(this.runHandlers.bind(this), 0);
1145
+ },
1146
+
1147
+ runHandlers: function runHandlers() {
1148
+ var RUN_TIMEOUT = 1; // ms
1149
+ var timeoutAt = Date.now() + RUN_TIMEOUT;
1150
+ while (this.handlers.length > 0) {
1151
+ var handler = this.handlers.shift();
1152
+
1153
+ var nextStatus = handler.thisPromise._status;
1154
+ var nextValue = handler.thisPromise._value;
1155
+
1156
+ try {
1157
+ if (nextStatus === STATUS_RESOLVED) {
1158
+ if (typeof handler.onResolve === 'function') {
1159
+ nextValue = handler.onResolve(nextValue);
1160
+ }
1161
+ } else if (typeof handler.onReject === 'function') {
1162
+ nextValue = handler.onReject(nextValue);
1163
+ nextStatus = STATUS_RESOLVED;
1164
+
1165
+ if (handler.thisPromise._unhandledRejection) {
1166
+ this.removeUnhandeledRejection(handler.thisPromise);
1167
+ }
1168
+ }
1169
+ } catch (ex) {
1170
+ nextStatus = STATUS_REJECTED;
1171
+ nextValue = ex;
1172
+ }
1173
+
1174
+ handler.nextPromise._updateStatus(nextStatus, nextValue);
1175
+ if (Date.now() >= timeoutAt) {
1176
+ break;
1177
+ }
1178
+ }
1179
+
1180
+ if (this.handlers.length > 0) {
1181
+ setTimeout(this.runHandlers.bind(this), 0);
1182
+ return;
1183
+ }
1184
+
1185
+ this.running = false;
1186
+ },
1187
+
1188
+ addUnhandledRejection: function addUnhandledRejection(promise) {
1189
+ this.unhandledRejections.push({
1190
+ promise: promise,
1191
+ time: Date.now()
1192
+ });
1193
+ this.scheduleRejectionCheck();
1194
+ },
1195
+
1196
+ removeUnhandeledRejection: function removeUnhandeledRejection(promise) {
1197
+ promise._unhandledRejection = false;
1198
+ for (var i = 0; i < this.unhandledRejections.length; i++) {
1199
+ if (this.unhandledRejections[i].promise === promise) {
1200
+ this.unhandledRejections.splice(i);
1201
+ i--;
1202
+ }
1203
+ }
1204
+ },
1205
+
1206
+ scheduleRejectionCheck: function scheduleRejectionCheck() {
1207
+ if (this.pendingRejectionCheck) {
1208
+ return;
1209
+ }
1210
+ this.pendingRejectionCheck = true;
1211
+ setTimeout(function rejectionCheck() {
1212
+ this.pendingRejectionCheck = false;
1213
+ var now = Date.now();
1214
+ for (var i = 0; i < this.unhandledRejections.length; i++) {
1215
+ if (now - this.unhandledRejections[i].time > REJECTION_TIMEOUT) {
1216
+ var unhandled = this.unhandledRejections[i].promise._value;
1217
+ var msg = 'Unhandled rejection: ' + unhandled;
1218
+ if (unhandled.stack) {
1219
+ msg += '\n' + unhandled.stack;
1220
+ }
1221
+ warn(msg);
1222
+ this.unhandledRejections.splice(i);
1223
+ i--;
1224
+ }
1225
+ }
1226
+ if (this.unhandledRejections.length) {
1227
+ this.scheduleRejectionCheck();
1228
+ }
1229
+ }.bind(this), REJECTION_TIMEOUT);
1230
+ }
1231
+ };
1232
+
1233
+ function Promise(resolver) {
1234
+ this._status = STATUS_PENDING;
1235
+ this._handlers = [];
1236
+ try {
1237
+ resolver.call(this, this._resolve.bind(this), this._reject.bind(this));
1238
+ } catch (e) {
1239
+ this._reject(e);
1240
+ }
1241
+ }
1242
+ /**
1243
+ * Builds a promise that is resolved when all the passed in promises are
1244
+ * resolved.
1245
+ * @param {array} array of data and/or promises to wait for.
1246
+ * @return {Promise} New dependant promise.
1247
+ */
1248
+ Promise.all = function Promise_all(promises) {
1249
+ var resolveAll, rejectAll;
1250
+ var deferred = new Promise(function (resolve, reject) {
1251
+ resolveAll = resolve;
1252
+ rejectAll = reject;
1253
+ });
1254
+ var unresolved = promises.length;
1255
+ var results = [];
1256
+ if (unresolved === 0) {
1257
+ resolveAll(results);
1258
+ return deferred;
1259
+ }
1260
+ function reject(reason) {
1261
+ if (deferred._status === STATUS_REJECTED) {
1262
+ return;
1263
+ }
1264
+ results = [];
1265
+ rejectAll(reason);
1266
+ }
1267
+ for (var i = 0, ii = promises.length; i < ii; ++i) {
1268
+ var promise = promises[i];
1269
+ var resolve = (function(i) {
1270
+ return function(value) {
1271
+ if (deferred._status === STATUS_REJECTED) {
1272
+ return;
1273
+ }
1274
+ results[i] = value;
1275
+ unresolved--;
1276
+ if (unresolved === 0) {
1277
+ resolveAll(results);
1278
+ }
1279
+ };
1280
+ })(i);
1281
+ if (Promise.isPromise(promise)) {
1282
+ promise.then(resolve, reject);
1283
+ } else {
1284
+ resolve(promise);
1285
+ }
1286
+ }
1287
+ return deferred;
1288
+ };
1289
+
1290
+ /**
1291
+ * Checks if the value is likely a promise (has a 'then' function).
1292
+ * @return {boolean} true if value is thenable
1293
+ */
1294
+ Promise.isPromise = function Promise_isPromise(value) {
1295
+ return value && typeof value.then === 'function';
1296
+ };
1297
+
1298
+ /**
1299
+ * Creates resolved promise
1300
+ * @param value resolve value
1301
+ * @returns {Promise}
1302
+ */
1303
+ Promise.resolve = function Promise_resolve(value) {
1304
+ return new Promise(function (resolve) { resolve(value); });
1305
+ };
1306
+
1307
+ /**
1308
+ * Creates rejected promise
1309
+ * @param reason rejection value
1310
+ * @returns {Promise}
1311
+ */
1312
+ Promise.reject = function Promise_reject(reason) {
1313
+ return new Promise(function (resolve, reject) { reject(reason); });
1314
+ };
1315
+
1316
+ Promise.prototype = {
1317
+ _status: null,
1318
+ _value: null,
1319
+ _handlers: null,
1320
+ _unhandledRejection: null,
1321
+
1322
+ _updateStatus: function Promise__updateStatus(status, value) {
1323
+ if (this._status === STATUS_RESOLVED ||
1324
+ this._status === STATUS_REJECTED) {
1325
+ return;
1326
+ }
1327
+
1328
+ if (status === STATUS_RESOLVED &&
1329
+ Promise.isPromise(value)) {
1330
+ value.then(this._updateStatus.bind(this, STATUS_RESOLVED),
1331
+ this._updateStatus.bind(this, STATUS_REJECTED));
1332
+ return;
1333
+ }
1334
+
1335
+ this._status = status;
1336
+ this._value = value;
1337
+
1338
+ if (status === STATUS_REJECTED && this._handlers.length === 0) {
1339
+ this._unhandledRejection = true;
1340
+ HandlerManager.addUnhandledRejection(this);
1341
+ }
1342
+
1343
+ HandlerManager.scheduleHandlers(this);
1344
+ },
1345
+
1346
+ _resolve: function Promise_resolve(value) {
1347
+ this._updateStatus(STATUS_RESOLVED, value);
1348
+ },
1349
+
1350
+ _reject: function Promise_reject(reason) {
1351
+ this._updateStatus(STATUS_REJECTED, reason);
1352
+ },
1353
+
1354
+ then: function Promise_then(onResolve, onReject) {
1355
+ var nextPromise = new Promise(function (resolve, reject) {
1356
+ this.resolve = resolve;
1357
+ this.reject = reject;
1358
+ });
1359
+ this._handlers.push({
1360
+ thisPromise: this,
1361
+ onResolve: onResolve,
1362
+ onReject: onReject,
1363
+ nextPromise: nextPromise
1364
+ });
1365
+ HandlerManager.scheduleHandlers(this);
1366
+ return nextPromise;
1367
+ },
1368
+
1369
+ catch: function Promise_catch(onReject) {
1370
+ return this.then(undefined, onReject);
1371
+ }
1372
+ };
1373
+
1374
+ globalScope.Promise = Promise;
1375
+ })();
1376
+
1377
+ var StatTimer = (function StatTimerClosure() {
1378
+ function rpad(str, pad, length) {
1379
+ while (str.length < length) {
1380
+ str += pad;
1381
+ }
1382
+ return str;
1383
+ }
1384
+ function StatTimer() {
1385
+ this.started = {};
1386
+ this.times = [];
1387
+ this.enabled = true;
1388
+ }
1389
+ StatTimer.prototype = {
1390
+ time: function StatTimer_time(name) {
1391
+ if (!this.enabled) {
1392
+ return;
1393
+ }
1394
+ if (name in this.started) {
1395
+ warn('Timer is already running for ' + name);
1396
+ }
1397
+ this.started[name] = Date.now();
1398
+ },
1399
+ timeEnd: function StatTimer_timeEnd(name) {
1400
+ if (!this.enabled) {
1401
+ return;
1402
+ }
1403
+ if (!(name in this.started)) {
1404
+ warn('Timer has not been started for ' + name);
1405
+ }
1406
+ this.times.push({
1407
+ 'name': name,
1408
+ 'start': this.started[name],
1409
+ 'end': Date.now()
1410
+ });
1411
+ // Remove timer from started so it can be called again.
1412
+ delete this.started[name];
1413
+ },
1414
+ toString: function StatTimer_toString() {
1415
+ var i, ii;
1416
+ var times = this.times;
1417
+ var out = '';
1418
+ // Find the longest name for padding purposes.
1419
+ var longest = 0;
1420
+ for (i = 0, ii = times.length; i < ii; ++i) {
1421
+ var name = times[i]['name'];
1422
+ if (name.length > longest) {
1423
+ longest = name.length;
1424
+ }
1425
+ }
1426
+ for (i = 0, ii = times.length; i < ii; ++i) {
1427
+ var span = times[i];
1428
+ var duration = span.end - span.start;
1429
+ out += rpad(span['name'], ' ', longest) + ' ' + duration + 'ms\n';
1430
+ }
1431
+ return out;
1432
+ }
1433
+ };
1434
+ return StatTimer;
1435
+ })();
1436
+
1437
+ PDFJS.createBlob = function createBlob(data, contentType) {
1438
+ if (typeof Blob !== 'undefined') {
1439
+ return new Blob([data], { type: contentType });
1440
+ }
1441
+ // Blob builder is deprecated in FF14 and removed in FF18.
1442
+ var bb = new MozBlobBuilder();
1443
+ bb.append(data);
1444
+ return bb.getBlob(contentType);
1445
+ };
1446
+
1447
+ PDFJS.createObjectURL = (function createObjectURLClosure() {
1448
+ // Blob/createObjectURL is not available, falling back to data schema.
1449
+ var digits =
1450
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
1451
+
1452
+ return function createObjectURL(data, contentType) {
1453
+ if (!PDFJS.disableCreateObjectURL &&
1454
+ typeof URL !== 'undefined' && URL.createObjectURL) {
1455
+ var blob = PDFJS.createBlob(data, contentType);
1456
+ return URL.createObjectURL(blob);
1457
+ }
1458
+
1459
+ var buffer = 'data:' + contentType + ';base64,';
1460
+ for (var i = 0, ii = data.length; i < ii; i += 3) {
1461
+ var b1 = data[i] & 0xFF;
1462
+ var b2 = data[i + 1] & 0xFF;
1463
+ var b3 = data[i + 2] & 0xFF;
1464
+ var d1 = b1 >> 2, d2 = ((b1 & 3) << 4) | (b2 >> 4);
1465
+ var d3 = i + 1 < ii ? ((b2 & 0xF) << 2) | (b3 >> 6) : 64;
1466
+ var d4 = i + 2 < ii ? (b3 & 0x3F) : 64;
1467
+ buffer += digits[d1] + digits[d2] + digits[d3] + digits[d4];
1468
+ }
1469
+ return buffer;
1470
+ };
1471
+ })();
1472
+
1473
+ function MessageHandler(name, comObj) {
1474
+ this.name = name;
1475
+ this.comObj = comObj;
1476
+ this.callbackIndex = 1;
1477
+ this.postMessageTransfers = true;
1478
+ var callbacksCapabilities = this.callbacksCapabilities = {};
1479
+ var ah = this.actionHandler = {};
1480
+
1481
+ ah['console_log'] = [function ahConsoleLog(data) {
1482
+ console.log.apply(console, data);
1483
+ }];
1484
+ ah['console_error'] = [function ahConsoleError(data) {
1485
+ console.error.apply(console, data);
1486
+ }];
1487
+ ah['_unsupported_feature'] = [function ah_unsupportedFeature(data) {
1488
+ UnsupportedManager.notify(data);
1489
+ }];
1490
+
1491
+ comObj.onmessage = function messageHandlerComObjOnMessage(event) {
1492
+ var data = event.data;
1493
+ if (data.isReply) {
1494
+ var callbackId = data.callbackId;
1495
+ if (data.callbackId in callbacksCapabilities) {
1496
+ var callback = callbacksCapabilities[callbackId];
1497
+ delete callbacksCapabilities[callbackId];
1498
+ if ('error' in data) {
1499
+ callback.reject(data.error);
1500
+ } else {
1501
+ callback.resolve(data.data);
1502
+ }
1503
+ } else {
1504
+ error('Cannot resolve callback ' + callbackId);
1505
+ }
1506
+ } else if (data.action in ah) {
1507
+ var action = ah[data.action];
1508
+ if (data.callbackId) {
1509
+ Promise.resolve().then(function () {
1510
+ return action[0].call(action[1], data.data);
1511
+ }).then(function (result) {
1512
+ comObj.postMessage({
1513
+ isReply: true,
1514
+ callbackId: data.callbackId,
1515
+ data: result
1516
+ });
1517
+ }, function (reason) {
1518
+ comObj.postMessage({
1519
+ isReply: true,
1520
+ callbackId: data.callbackId,
1521
+ error: reason
1522
+ });
1523
+ });
1524
+ } else {
1525
+ action[0].call(action[1], data.data);
1526
+ }
1527
+ } else {
1528
+ error('Unknown action from worker: ' + data.action);
1529
+ }
1530
+ };
1531
+ }
1532
+
1533
+ MessageHandler.prototype = {
1534
+ on: function messageHandlerOn(actionName, handler, scope) {
1535
+ var ah = this.actionHandler;
1536
+ if (ah[actionName]) {
1537
+ error('There is already an actionName called "' + actionName + '"');
1538
+ }
1539
+ ah[actionName] = [handler, scope];
1540
+ },
1541
+ /**
1542
+ * Sends a message to the comObj to invoke the action with the supplied data.
1543
+ * @param {String} actionName Action to call.
1544
+ * @param {JSON} data JSON data to send.
1545
+ * @param {Array} [transfers] Optional list of transfers/ArrayBuffers
1546
+ */
1547
+ send: function messageHandlerSend(actionName, data, transfers) {
1548
+ var message = {
1549
+ action: actionName,
1550
+ data: data
1551
+ };
1552
+ this.postMessage(message, transfers);
1553
+ },
1554
+ /**
1555
+ * Sends a message to the comObj to invoke the action with the supplied data.
1556
+ * Expects that other side will callback with the response.
1557
+ * @param {String} actionName Action to call.
1558
+ * @param {JSON} data JSON data to send.
1559
+ * @param {Array} [transfers] Optional list of transfers/ArrayBuffers.
1560
+ * @returns {Promise} Promise to be resolved with response data.
1561
+ */
1562
+ sendWithPromise:
1563
+ function messageHandlerSendWithPromise(actionName, data, transfers) {
1564
+ var callbackId = this.callbackIndex++;
1565
+ var message = {
1566
+ action: actionName,
1567
+ data: data,
1568
+ callbackId: callbackId
1569
+ };
1570
+ var capability = createPromiseCapability();
1571
+ this.callbacksCapabilities[callbackId] = capability;
1572
+ try {
1573
+ this.postMessage(message, transfers);
1574
+ } catch (e) {
1575
+ capability.reject(e);
1576
+ }
1577
+ return capability.promise;
1578
+ },
1579
+ /**
1580
+ * Sends raw message to the comObj.
1581
+ * @private
1582
+ * @param message {Object} Raw message.
1583
+ * @param transfers List of transfers/ArrayBuffers, or undefined.
1584
+ */
1585
+ postMessage: function (message, transfers) {
1586
+ if (transfers && this.postMessageTransfers) {
1587
+ this.comObj.postMessage(message, transfers);
1588
+ } else {
1589
+ this.comObj.postMessage(message);
1590
+ }
1591
+ }
1592
+ };
1593
+
1594
+ function loadJpegStream(id, imageUrl, objs) {
1595
+ var img = new Image();
1596
+ img.onload = (function loadJpegStream_onloadClosure() {
1597
+ objs.resolve(id, img);
1598
+ });
1599
+ img.onerror = (function loadJpegStream_onerrorClosure() {
1600
+ objs.resolve(id, null);
1601
+ warn('Error during JPEG image loading');
1602
+ });
1603
+ img.src = imageUrl;
1604
+ }
1605
+
1606
+
1607
+ /**
1608
+ * The maximum allowed image size in total pixels e.g. width * height. Images
1609
+ * above this value will not be drawn. Use -1 for no limit.
1610
+ * @var {number}
1611
+ */
1612
+ PDFJS.maxImageSize = (PDFJS.maxImageSize === undefined ?
1613
+ -1 : PDFJS.maxImageSize);
1614
+
1615
+ /**
1616
+ * The url of where the predefined Adobe CMaps are located. Include trailing
1617
+ * slash.
1618
+ * @var {string}
1619
+ */
1620
+ PDFJS.cMapUrl = (PDFJS.cMapUrl === undefined ? null : PDFJS.cMapUrl);
1621
+
1622
+ /**
1623
+ * Specifies if CMaps are binary packed.
1624
+ * @var {boolean}
1625
+ */
1626
+ PDFJS.cMapPacked = PDFJS.cMapPacked === undefined ? false : PDFJS.cMapPacked;
1627
+
1628
+ /**
1629
+ * By default fonts are converted to OpenType fonts and loaded via font face
1630
+ * rules. If disabled, the font will be rendered using a built in font renderer
1631
+ * that constructs the glyphs with primitive path commands.
1632
+ * @var {boolean}
1633
+ */
1634
+ PDFJS.disableFontFace = (PDFJS.disableFontFace === undefined ?
1635
+ false : PDFJS.disableFontFace);
1636
+
1637
+ /**
1638
+ * Path for image resources, mainly for annotation icons. Include trailing
1639
+ * slash.
1640
+ * @var {string}
1641
+ */
1642
+ PDFJS.imageResourcesPath = (PDFJS.imageResourcesPath === undefined ?
1643
+ '' : PDFJS.imageResourcesPath);
1644
+
1645
+ /**
1646
+ * Disable the web worker and run all code on the main thread. This will happen
1647
+ * automatically if the browser doesn't support workers or sending typed arrays
1648
+ * to workers.
1649
+ * @var {boolean}
1650
+ */
1651
+ PDFJS.disableWorker = (PDFJS.disableWorker === undefined ?
1652
+ false : PDFJS.disableWorker);
1653
+
1654
+ /**
1655
+ * Path and filename of the worker file. Required when the worker is enabled in
1656
+ * development mode. If unspecified in the production build, the worker will be
1657
+ * loaded based on the location of the pdf.js file.
1658
+ * @var {string}
1659
+ */
1660
+ PDFJS.workerSrc = (PDFJS.workerSrc === undefined ? null : PDFJS.workerSrc);
1661
+
1662
+ /**
1663
+ * Disable range request loading of PDF files. When enabled and if the server
1664
+ * supports partial content requests then the PDF will be fetched in chunks.
1665
+ * Enabled (false) by default.
1666
+ * @var {boolean}
1667
+ */
1668
+ PDFJS.disableRange = (PDFJS.disableRange === undefined ?
1669
+ false : PDFJS.disableRange);
1670
+
1671
+ /**
1672
+ * Disable streaming of PDF file data. By default PDF.js attempts to load PDF
1673
+ * in chunks. This default behavior can be disabled.
1674
+ * @var {boolean}
1675
+ */
1676
+ PDFJS.disableStream = (PDFJS.disableStream === undefined ?
1677
+ false : PDFJS.disableStream);
1678
+
1679
+ /**
1680
+ * Disable pre-fetching of PDF file data. When range requests are enabled PDF.js
1681
+ * will automatically keep fetching more data even if it isn't needed to display
1682
+ * the current page. This default behavior can be disabled.
1683
+ *
1684
+ * NOTE: It is also necessary to disable streaming, see above,
1685
+ * in order for disabling of pre-fetching to work correctly.
1686
+ * @var {boolean}
1687
+ */
1688
+ PDFJS.disableAutoFetch = (PDFJS.disableAutoFetch === undefined ?
1689
+ false : PDFJS.disableAutoFetch);
1690
+
1691
+ /**
1692
+ * Enables special hooks for debugging PDF.js.
1693
+ * @var {boolean}
1694
+ */
1695
+ PDFJS.pdfBug = (PDFJS.pdfBug === undefined ? false : PDFJS.pdfBug);
1696
+
1697
+ /**
1698
+ * Enables transfer usage in postMessage for ArrayBuffers.
1699
+ * @var {boolean}
1700
+ */
1701
+ PDFJS.postMessageTransfers = (PDFJS.postMessageTransfers === undefined ?
1702
+ true : PDFJS.postMessageTransfers);
1703
+
1704
+ /**
1705
+ * Disables URL.createObjectURL usage.
1706
+ * @var {boolean}
1707
+ */
1708
+ PDFJS.disableCreateObjectURL = (PDFJS.disableCreateObjectURL === undefined ?
1709
+ false : PDFJS.disableCreateObjectURL);
1710
+
1711
+ /**
1712
+ * Disables WebGL usage.
1713
+ * @var {boolean}
1714
+ */
1715
+ PDFJS.disableWebGL = (PDFJS.disableWebGL === undefined ?
1716
+ true : PDFJS.disableWebGL);
1717
+
1718
+ /**
1719
+ * Disables fullscreen support, and by extension Presentation Mode,
1720
+ * in browsers which support the fullscreen API.
1721
+ * @var {boolean}
1722
+ */
1723
+ PDFJS.disableFullscreen = (PDFJS.disableFullscreen === undefined ?
1724
+ false : PDFJS.disableFullscreen);
1725
+
1726
+ /**
1727
+ * Enables CSS only zooming.
1728
+ * @var {boolean}
1729
+ */
1730
+ PDFJS.useOnlyCssZoom = (PDFJS.useOnlyCssZoom === undefined ?
1731
+ false : PDFJS.useOnlyCssZoom);
1732
+
1733
+ /**
1734
+ * Controls the logging level.
1735
+ * The constants from PDFJS.VERBOSITY_LEVELS should be used:
1736
+ * - errors
1737
+ * - warnings [default]
1738
+ * - infos
1739
+ * @var {number}
1740
+ */
1741
+ PDFJS.verbosity = (PDFJS.verbosity === undefined ?
1742
+ PDFJS.VERBOSITY_LEVELS.warnings : PDFJS.verbosity);
1743
+
1744
+ /**
1745
+ * The maximum supported canvas size in total pixels e.g. width * height.
1746
+ * The default value is 4096 * 4096. Use -1 for no limit.
1747
+ * @var {number}
1748
+ */
1749
+ PDFJS.maxCanvasPixels = (PDFJS.maxCanvasPixels === undefined ?
1750
+ 16777216 : PDFJS.maxCanvasPixels);
1751
+
1752
+ /**
1753
+ * Opens external links in a new window if enabled. The default behavior opens
1754
+ * external links in the PDF.js window.
1755
+ * @var {boolean}
1756
+ */
1757
+ PDFJS.openExternalLinksInNewWindow = (
1758
+ PDFJS.openExternalLinksInNewWindow === undefined ?
1759
+ false : PDFJS.openExternalLinksInNewWindow);
1760
+
1761
+ /**
1762
+ * Document initialization / loading parameters object.
1763
+ *
1764
+ * @typedef {Object} DocumentInitParameters
1765
+ * @property {string} url - The URL of the PDF.
1766
+ * @property {TypedArray|Array|string} data - Binary PDF data. Use typed arrays
1767
+ * (Uint8Array) to improve the memory usage. If PDF data is BASE64-encoded,
1768
+ * use atob() to convert it to a binary string first.
1769
+ * @property {Object} httpHeaders - Basic authentication headers.
1770
+ * @property {boolean} withCredentials - Indicates whether or not cross-site
1771
+ * Access-Control requests should be made using credentials such as cookies
1772
+ * or authorization headers. The default is false.
1773
+ * @property {string} password - For decrypting password-protected PDFs.
1774
+ * @property {TypedArray} initialData - A typed array with the first portion or
1775
+ * all of the pdf data. Used by the extension since some data is already
1776
+ * loaded before the switch to range requests.
1777
+ * @property {number} length - The PDF file length. It's used for progress
1778
+ * reports and range requests operations.
1779
+ * @property {PDFDataRangeTransport} range
1780
+ */
1781
+
1782
+ /**
1783
+ * @typedef {Object} PDFDocumentStats
1784
+ * @property {Array} streamTypes - Used stream types in the document (an item
1785
+ * is set to true if specific stream ID was used in the document).
1786
+ * @property {Array} fontTypes - Used font type in the document (an item is set
1787
+ * to true if specific font ID was used in the document).
1788
+ */
1789
+
1790
+ /**
1791
+ * This is the main entry point for loading a PDF and interacting with it.
1792
+ * NOTE: If a URL is used to fetch the PDF data a standard XMLHttpRequest(XHR)
1793
+ * is used, which means it must follow the same origin rules that any XHR does
1794
+ * e.g. No cross domain requests without CORS.
1795
+ *
1796
+ * @param {string|TypedArray|DocumentInitParameters|PDFDataRangeTransport} src
1797
+ * Can be a url to where a PDF is located, a typed array (Uint8Array)
1798
+ * already populated with data or parameter object.
1799
+ *
1800
+ * @param {PDFDataRangeTransport} pdfDataRangeTransport (deprecated) It is used
1801
+ * if you want to manually serve range requests for data in the PDF.
1802
+ *
1803
+ * @param {function} passwordCallback (deprecated) It is used to request a
1804
+ * password if wrong or no password was provided. The callback receives two
1805
+ * parameters: function that needs to be called with new password and reason
1806
+ * (see {PasswordResponses}).
1807
+ *
1808
+ * @param {function} progressCallback (deprecated) It is used to be able to
1809
+ * monitor the loading progress of the PDF file (necessary to implement e.g.
1810
+ * a loading bar). The callback receives an {Object} with the properties:
1811
+ * {number} loaded and {number} total.
1812
+ *
1813
+ * @return {PDFDocumentLoadingTask}
1814
+ */
1815
+ PDFJS.getDocument = function getDocument(src,
1816
+ pdfDataRangeTransport,
1817
+ passwordCallback,
1818
+ progressCallback) {
1819
+ var task = new PDFDocumentLoadingTask();
1820
+
1821
+ // Support of the obsolete arguments (for compatibility with API v1.0)
1822
+ if (pdfDataRangeTransport) {
1823
+ if (!(pdfDataRangeTransport instanceof PDFDataRangeTransport)) {
1824
+ // Not a PDFDataRangeTransport instance, trying to add missing properties.
1825
+ pdfDataRangeTransport = Object.create(pdfDataRangeTransport);
1826
+ pdfDataRangeTransport.length = src.length;
1827
+ pdfDataRangeTransport.initialData = src.initialData;
1828
+ }
1829
+ src = Object.create(src);
1830
+ src.range = pdfDataRangeTransport;
1831
+ }
1832
+ task.onPassword = passwordCallback || null;
1833
+ task.onProgress = progressCallback || null;
1834
+
1835
+ var workerInitializedCapability, transport;
1836
+ var source;
1837
+ if (typeof src === 'string') {
1838
+ source = { url: src };
1839
+ } else if (isArrayBuffer(src)) {
1840
+ source = { data: src };
1841
+ } else if (src instanceof PDFDataRangeTransport) {
1842
+ source = { range: src };
1843
+ } else {
1844
+ if (typeof src !== 'object') {
1845
+ error('Invalid parameter in getDocument, need either Uint8Array, ' +
1846
+ 'string or a parameter object');
1847
+ }
1848
+ if (!src.url && !src.data && !src.range) {
1849
+ error('Invalid parameter object: need either .data, .range or .url');
1850
+ }
1851
+
1852
+ source = src;
1853
+ }
1854
+
1855
+ var params = {};
1856
+ for (var key in source) {
1857
+ if (key === 'url' && typeof window !== 'undefined') {
1858
+ // The full path is required in the 'url' field.
1859
+ params[key] = combineUrl(window.location.href, source[key]);
1860
+ continue;
1861
+ } else if (key === 'range') {
1862
+ continue;
1863
+ } else if (key === 'data' && !(source[key] instanceof Uint8Array)) {
1864
+ // Converting string or array-like data to Uint8Array.
1865
+ var pdfBytes = source[key];
1866
+ if (typeof pdfBytes === 'string') {
1867
+ params[key] = stringToBytes(pdfBytes);
1868
+ } else if (typeof pdfBytes === 'object' && pdfBytes !== null &&
1869
+ !isNaN(pdfBytes.length)) {
1870
+ params[key] = new Uint8Array(pdfBytes);
1871
+ } else {
1872
+ error('Invalid PDF binary data: either typed array, string or ' +
1873
+ 'array-like object is expected in the data property.');
1874
+ }
1875
+ continue;
1876
+ }
1877
+ params[key] = source[key];
1878
+ }
1879
+
1880
+ workerInitializedCapability = createPromiseCapability();
1881
+ transport = new WorkerTransport(workerInitializedCapability, source.range);
1882
+ workerInitializedCapability.promise.then(function transportInitialized() {
1883
+ transport.fetchDocument(task, params);
1884
+ });
1885
+
1886
+ return task;
1887
+ };
1888
+
1889
+ /**
1890
+ * PDF document loading operation.
1891
+ * @class
1892
+ */
1893
+ var PDFDocumentLoadingTask = (function PDFDocumentLoadingTaskClosure() {
1894
+ /** @constructs PDFDocumentLoadingTask */
1895
+ function PDFDocumentLoadingTask() {
1896
+ this._capability = createPromiseCapability();
1897
+
1898
+ /**
1899
+ * Callback to request a password if wrong or no password was provided.
1900
+ * The callback receives two parameters: function that needs to be called
1901
+ * with new password and reason (see {PasswordResponses}).
1902
+ */
1903
+ this.onPassword = null;
1904
+
1905
+ /**
1906
+ * Callback to be able to monitor the loading progress of the PDF file
1907
+ * (necessary to implement e.g. a loading bar). The callback receives
1908
+ * an {Object} with the properties: {number} loaded and {number} total.
1909
+ */
1910
+ this.onProgress = null;
1911
+ }
1912
+
1913
+ PDFDocumentLoadingTask.prototype =
1914
+ /** @lends PDFDocumentLoadingTask.prototype */ {
1915
+ /**
1916
+ * @return {Promise}
1917
+ */
1918
+ get promise() {
1919
+ return this._capability.promise;
1920
+ },
1921
+
1922
+ // TODO add cancel or abort method
1923
+
1924
+ /**
1925
+ * Registers callbacks to indicate the document loading completion.
1926
+ *
1927
+ * @param {function} onFulfilled The callback for the loading completion.
1928
+ * @param {function} onRejected The callback for the loading failure.
1929
+ * @return {Promise} A promise that is resolved after the onFulfilled or
1930
+ * onRejected callback.
1931
+ */
1932
+ then: function PDFDocumentLoadingTask_then(onFulfilled, onRejected) {
1933
+ return this.promise.then.apply(this.promise, arguments);
1934
+ }
1935
+ };
1936
+
1937
+ return PDFDocumentLoadingTask;
1938
+ })();
1939
+
1940
+ /**
1941
+ * Abstract class to support range requests file loading.
1942
+ * @class
1943
+ */
1944
+ var PDFDataRangeTransport = (function pdfDataRangeTransportClosure() {
1945
+ /**
1946
+ * @constructs PDFDataRangeTransport
1947
+ * @param {number} length
1948
+ * @param {Uint8Array} initialData
1949
+ */
1950
+ function PDFDataRangeTransport(length, initialData) {
1951
+ this.length = length;
1952
+ this.initialData = initialData;
1953
+
1954
+ this._rangeListeners = [];
1955
+ this._progressListeners = [];
1956
+ this._progressiveReadListeners = [];
1957
+ this._readyCapability = createPromiseCapability();
1958
+ }
1959
+ PDFDataRangeTransport.prototype =
1960
+ /** @lends PDFDataRangeTransport.prototype */ {
1961
+ addRangeListener:
1962
+ function PDFDataRangeTransport_addRangeListener(listener) {
1963
+ this._rangeListeners.push(listener);
1964
+ },
1965
+
1966
+ addProgressListener:
1967
+ function PDFDataRangeTransport_addProgressListener(listener) {
1968
+ this._progressListeners.push(listener);
1969
+ },
1970
+
1971
+ addProgressiveReadListener:
1972
+ function PDFDataRangeTransport_addProgressiveReadListener(listener) {
1973
+ this._progressiveReadListeners.push(listener);
1974
+ },
1975
+
1976
+ onDataRange: function PDFDataRangeTransport_onDataRange(begin, chunk) {
1977
+ var listeners = this._rangeListeners;
1978
+ for (var i = 0, n = listeners.length; i < n; ++i) {
1979
+ listeners[i](begin, chunk);
1980
+ }
1981
+ },
1982
+
1983
+ onDataProgress: function PDFDataRangeTransport_onDataProgress(loaded) {
1984
+ this._readyCapability.promise.then(function () {
1985
+ var listeners = this._progressListeners;
1986
+ for (var i = 0, n = listeners.length; i < n; ++i) {
1987
+ listeners[i](loaded);
1988
+ }
1989
+ }.bind(this));
1990
+ },
1991
+
1992
+ onDataProgressiveRead:
1993
+ function PDFDataRangeTransport_onDataProgress(chunk) {
1994
+ this._readyCapability.promise.then(function () {
1995
+ var listeners = this._progressiveReadListeners;
1996
+ for (var i = 0, n = listeners.length; i < n; ++i) {
1997
+ listeners[i](chunk);
1998
+ }
1999
+ }.bind(this));
2000
+ },
2001
+
2002
+ transportReady: function PDFDataRangeTransport_transportReady() {
2003
+ this._readyCapability.resolve();
2004
+ },
2005
+
2006
+ requestDataRange:
2007
+ function PDFDataRangeTransport_requestDataRange(begin, end) {
2008
+ throw new Error('Abstract method PDFDataRangeTransport.requestDataRange');
2009
+ }
2010
+ };
2011
+ return PDFDataRangeTransport;
2012
+ })();
2013
+
2014
+ PDFJS.PDFDataRangeTransport = PDFDataRangeTransport;
2015
+
2016
+ /**
2017
+ * Proxy to a PDFDocument in the worker thread. Also, contains commonly used
2018
+ * properties that can be read synchronously.
2019
+ * @class
2020
+ */
2021
+ var PDFDocumentProxy = (function PDFDocumentProxyClosure() {
2022
+ function PDFDocumentProxy(pdfInfo, transport) {
2023
+ this.pdfInfo = pdfInfo;
2024
+ this.transport = transport;
2025
+ }
2026
+ PDFDocumentProxy.prototype = /** @lends PDFDocumentProxy.prototype */ {
2027
+ /**
2028
+ * @return {number} Total number of pages the PDF contains.
2029
+ */
2030
+ get numPages() {
2031
+ return this.pdfInfo.numPages;
2032
+ },
2033
+ /**
2034
+ * @return {string} A unique ID to identify a PDF. Not guaranteed to be
2035
+ * unique.
2036
+ */
2037
+ get fingerprint() {
2038
+ return this.pdfInfo.fingerprint;
2039
+ },
2040
+ /**
2041
+ * @param {number} pageNumber The page number to get. The first page is 1.
2042
+ * @return {Promise} A promise that is resolved with a {@link PDFPageProxy}
2043
+ * object.
2044
+ */
2045
+ getPage: function PDFDocumentProxy_getPage(pageNumber) {
2046
+ return this.transport.getPage(pageNumber);
2047
+ },
2048
+ /**
2049
+ * @param {{num: number, gen: number}} ref The page reference. Must have
2050
+ * the 'num' and 'gen' properties.
2051
+ * @return {Promise} A promise that is resolved with the page index that is
2052
+ * associated with the reference.
2053
+ */
2054
+ getPageIndex: function PDFDocumentProxy_getPageIndex(ref) {
2055
+ return this.transport.getPageIndex(ref);
2056
+ },
2057
+ /**
2058
+ * @return {Promise} A promise that is resolved with a lookup table for
2059
+ * mapping named destinations to reference numbers.
2060
+ *
2061
+ * This can be slow for large documents: use getDestination instead
2062
+ */
2063
+ getDestinations: function PDFDocumentProxy_getDestinations() {
2064
+ return this.transport.getDestinations();
2065
+ },
2066
+ /**
2067
+ * @param {string} id The named destination to get.
2068
+ * @return {Promise} A promise that is resolved with all information
2069
+ * of the given named destination.
2070
+ */
2071
+ getDestination: function PDFDocumentProxy_getDestination(id) {
2072
+ return this.transport.getDestination(id);
2073
+ },
2074
+ /**
2075
+ * @return {Promise} A promise that is resolved with a lookup table for
2076
+ * mapping named attachments to their content.
2077
+ */
2078
+ getAttachments: function PDFDocumentProxy_getAttachments() {
2079
+ return this.transport.getAttachments();
2080
+ },
2081
+ /**
2082
+ * @return {Promise} A promise that is resolved with an array of all the
2083
+ * JavaScript strings in the name tree.
2084
+ */
2085
+ getJavaScript: function PDFDocumentProxy_getJavaScript() {
2086
+ return this.transport.getJavaScript();
2087
+ },
2088
+ /**
2089
+ * @return {Promise} A promise that is resolved with an {Array} that is a
2090
+ * tree outline (if it has one) of the PDF. The tree is in the format of:
2091
+ * [
2092
+ * {
2093
+ * title: string,
2094
+ * bold: boolean,
2095
+ * italic: boolean,
2096
+ * color: rgb array,
2097
+ * dest: dest obj,
2098
+ * items: array of more items like this
2099
+ * },
2100
+ * ...
2101
+ * ].
2102
+ */
2103
+ getOutline: function PDFDocumentProxy_getOutline() {
2104
+ return this.transport.getOutline();
2105
+ },
2106
+ /**
2107
+ * @return {Promise} A promise that is resolved with an {Object} that has
2108
+ * info and metadata properties. Info is an {Object} filled with anything
2109
+ * available in the information dictionary and similarly metadata is a
2110
+ * {Metadata} object with information from the metadata section of the PDF.
2111
+ */
2112
+ getMetadata: function PDFDocumentProxy_getMetadata() {
2113
+ return this.transport.getMetadata();
2114
+ },
2115
+ /**
2116
+ * @return {Promise} A promise that is resolved with a TypedArray that has
2117
+ * the raw data from the PDF.
2118
+ */
2119
+ getData: function PDFDocumentProxy_getData() {
2120
+ return this.transport.getData();
2121
+ },
2122
+ /**
2123
+ * @return {Promise} A promise that is resolved when the document's data
2124
+ * is loaded. It is resolved with an {Object} that contains the length
2125
+ * property that indicates size of the PDF data in bytes.
2126
+ */
2127
+ getDownloadInfo: function PDFDocumentProxy_getDownloadInfo() {
2128
+ return this.transport.downloadInfoCapability.promise;
2129
+ },
2130
+ /**
2131
+ * @return {Promise} A promise this is resolved with current stats about
2132
+ * document structures (see {@link PDFDocumentStats}).
2133
+ */
2134
+ getStats: function PDFDocumentProxy_getStats() {
2135
+ return this.transport.getStats();
2136
+ },
2137
+ /**
2138
+ * Cleans up resources allocated by the document, e.g. created @font-face.
2139
+ */
2140
+ cleanup: function PDFDocumentProxy_cleanup() {
2141
+ this.transport.startCleanup();
2142
+ },
2143
+ /**
2144
+ * Destroys current document instance and terminates worker.
2145
+ */
2146
+ destroy: function PDFDocumentProxy_destroy() {
2147
+ this.transport.destroy();
2148
+ }
2149
+ };
2150
+ return PDFDocumentProxy;
2151
+ })();
2152
+
2153
+ /**
2154
+ * Page text content.
2155
+ *
2156
+ * @typedef {Object} TextContent
2157
+ * @property {array} items - array of {@link TextItem}
2158
+ * @property {Object} styles - {@link TextStyles} objects, indexed by font
2159
+ * name.
2160
+ */
2161
+
2162
+ /**
2163
+ * Page text content part.
2164
+ *
2165
+ * @typedef {Object} TextItem
2166
+ * @property {string} str - text content.
2167
+ * @property {string} dir - text direction: 'ttb', 'ltr' or 'rtl'.
2168
+ * @property {array} transform - transformation matrix.
2169
+ * @property {number} width - width in device space.
2170
+ * @property {number} height - height in device space.
2171
+ * @property {string} fontName - font name used by pdf.js for converted font.
2172
+ */
2173
+
2174
+ /**
2175
+ * Text style.
2176
+ *
2177
+ * @typedef {Object} TextStyle
2178
+ * @property {number} ascent - font ascent.
2179
+ * @property {number} descent - font descent.
2180
+ * @property {boolean} vertical - text is in vertical mode.
2181
+ * @property {string} fontFamily - possible font family
2182
+ */
2183
+
2184
+ /**
2185
+ * Page render parameters.
2186
+ *
2187
+ * @typedef {Object} RenderParameters
2188
+ * @property {Object} canvasContext - A 2D context of a DOM Canvas object.
2189
+ * @property {PDFJS.PageViewport} viewport - Rendering viewport obtained by
2190
+ * calling of PDFPage.getViewport method.
2191
+ * @property {string} intent - Rendering intent, can be 'display' or 'print'
2192
+ * (default value is 'display').
2193
+ * @property {Object} imageLayer - (optional) An object that has beginLayout,
2194
+ * endLayout and appendImage functions.
2195
+ * @property {function} continueCallback - (deprecated) A function that will be
2196
+ * called each time the rendering is paused. To continue
2197
+ * rendering call the function that is the first argument
2198
+ * to the callback.
2199
+ */
2200
+
2201
+ /**
2202
+ * PDF page operator list.
2203
+ *
2204
+ * @typedef {Object} PDFOperatorList
2205
+ * @property {Array} fnArray - Array containing the operator functions.
2206
+ * @property {Array} argsArray - Array containing the arguments of the
2207
+ * functions.
2208
+ */
2209
+
2210
+ /**
2211
+ * Proxy to a PDFPage in the worker thread.
2212
+ * @class
2213
+ */
2214
+ var PDFPageProxy = (function PDFPageProxyClosure() {
2215
+ function PDFPageProxy(pageIndex, pageInfo, transport) {
2216
+ this.pageIndex = pageIndex;
2217
+ this.pageInfo = pageInfo;
2218
+ this.transport = transport;
2219
+ this.stats = new StatTimer();
2220
+ this.stats.enabled = !!globalScope.PDFJS.enableStats;
2221
+ this.commonObjs = transport.commonObjs;
2222
+ this.objs = new PDFObjects();
2223
+ this.cleanupAfterRender = false;
2224
+ this.pendingDestroy = false;
2225
+ this.intentStates = {};
2226
+ }
2227
+ PDFPageProxy.prototype = /** @lends PDFPageProxy.prototype */ {
2228
+ /**
2229
+ * @return {number} Page number of the page. First page is 1.
2230
+ */
2231
+ get pageNumber() {
2232
+ return this.pageIndex + 1;
2233
+ },
2234
+ /**
2235
+ * @return {number} The number of degrees the page is rotated clockwise.
2236
+ */
2237
+ get rotate() {
2238
+ return this.pageInfo.rotate;
2239
+ },
2240
+ /**
2241
+ * @return {Object} The reference that points to this page. It has 'num' and
2242
+ * 'gen' properties.
2243
+ */
2244
+ get ref() {
2245
+ return this.pageInfo.ref;
2246
+ },
2247
+ /**
2248
+ * @return {Array} An array of the visible portion of the PDF page in the
2249
+ * user space units - [x1, y1, x2, y2].
2250
+ */
2251
+ get view() {
2252
+ return this.pageInfo.view;
2253
+ },
2254
+ /**
2255
+ * @param {number} scale The desired scale of the viewport.
2256
+ * @param {number} rotate Degrees to rotate the viewport. If omitted this
2257
+ * defaults to the page rotation.
2258
+ * @return {PDFJS.PageViewport} Contains 'width' and 'height' properties
2259
+ * along with transforms required for rendering.
2260
+ */
2261
+ getViewport: function PDFPageProxy_getViewport(scale, rotate) {
2262
+ if (arguments.length < 2) {
2263
+ rotate = this.rotate;
2264
+ }
2265
+ return new PDFJS.PageViewport(this.view, scale, rotate, 0, 0);
2266
+ },
2267
+ /**
2268
+ * @return {Promise} A promise that is resolved with an {Array} of the
2269
+ * annotation objects.
2270
+ */
2271
+ getAnnotations: function PDFPageProxy_getAnnotations() {
2272
+ if (this.annotationsPromise) {
2273
+ return this.annotationsPromise;
2274
+ }
2275
+
2276
+ var promise = this.transport.getAnnotations(this.pageIndex);
2277
+ this.annotationsPromise = promise;
2278
+ return promise;
2279
+ },
2280
+ /**
2281
+ * Begins the process of rendering a page to the desired context.
2282
+ * @param {RenderParameters} params Page render parameters.
2283
+ * @return {RenderTask} An object that contains the promise, which
2284
+ * is resolved when the page finishes rendering.
2285
+ */
2286
+ render: function PDFPageProxy_render(params) {
2287
+ var stats = this.stats;
2288
+ stats.time('Overall');
2289
+
2290
+ // If there was a pending destroy cancel it so no cleanup happens during
2291
+ // this call to render.
2292
+ this.pendingDestroy = false;
2293
+
2294
+ var renderingIntent = (params.intent === 'print' ? 'print' : 'display');
2295
+
2296
+ if (!this.intentStates[renderingIntent]) {
2297
+ this.intentStates[renderingIntent] = {};
2298
+ }
2299
+ var intentState = this.intentStates[renderingIntent];
2300
+
2301
+ // If there's no displayReadyCapability yet, then the operatorList
2302
+ // was never requested before. Make the request and create the promise.
2303
+ if (!intentState.displayReadyCapability) {
2304
+ intentState.receivingOperatorList = true;
2305
+ intentState.displayReadyCapability = createPromiseCapability();
2306
+ intentState.operatorList = {
2307
+ fnArray: [],
2308
+ argsArray: [],
2309
+ lastChunk: false
2310
+ };
2311
+
2312
+ this.stats.time('Page Request');
2313
+ this.transport.messageHandler.send('RenderPageRequest', {
2314
+ pageIndex: this.pageNumber - 1,
2315
+ intent: renderingIntent
2316
+ });
2317
+ }
2318
+
2319
+ var internalRenderTask = new InternalRenderTask(complete, params,
2320
+ this.objs,
2321
+ this.commonObjs,
2322
+ intentState.operatorList,
2323
+ this.pageNumber);
2324
+ if (!intentState.renderTasks) {
2325
+ intentState.renderTasks = [];
2326
+ }
2327
+ intentState.renderTasks.push(internalRenderTask);
2328
+ var renderTask = internalRenderTask.task;
2329
+
2330
+ // Obsolete parameter support
2331
+ if (params.continueCallback) {
2332
+ renderTask.onContinue = params.continueCallback;
2333
+ }
2334
+
2335
+ var self = this;
2336
+ intentState.displayReadyCapability.promise.then(
2337
+ function pageDisplayReadyPromise(transparency) {
2338
+ if (self.pendingDestroy) {
2339
+ complete();
2340
+ return;
2341
+ }
2342
+ stats.time('Rendering');
2343
+ internalRenderTask.initalizeGraphics(transparency);
2344
+ internalRenderTask.operatorListChanged();
2345
+ },
2346
+ function pageDisplayReadPromiseError(reason) {
2347
+ complete(reason);
2348
+ }
2349
+ );
2350
+
2351
+ function complete(error) {
2352
+ var i = intentState.renderTasks.indexOf(internalRenderTask);
2353
+ if (i >= 0) {
2354
+ intentState.renderTasks.splice(i, 1);
2355
+ }
2356
+
2357
+ if (self.cleanupAfterRender) {
2358
+ self.pendingDestroy = true;
2359
+ }
2360
+ self._tryDestroy();
2361
+
2362
+ if (error) {
2363
+ internalRenderTask.capability.reject(error);
2364
+ } else {
2365
+ internalRenderTask.capability.resolve();
2366
+ }
2367
+ stats.timeEnd('Rendering');
2368
+ stats.timeEnd('Overall');
2369
+ }
2370
+
2371
+ return renderTask;
2372
+ },
2373
+
2374
+ /**
2375
+ * @return {Promise} A promise resolved with an {@link PDFOperatorList}
2376
+ * object that represents page's operator list.
2377
+ */
2378
+ getOperatorList: function PDFPageProxy_getOperatorList() {
2379
+ function operatorListChanged() {
2380
+ if (intentState.operatorList.lastChunk) {
2381
+ intentState.opListReadCapability.resolve(intentState.operatorList);
2382
+ }
2383
+ }
2384
+
2385
+ var renderingIntent = 'oplist';
2386
+ if (!this.intentStates[renderingIntent]) {
2387
+ this.intentStates[renderingIntent] = {};
2388
+ }
2389
+ var intentState = this.intentStates[renderingIntent];
2390
+
2391
+ if (!intentState.opListReadCapability) {
2392
+ var opListTask = {};
2393
+ opListTask.operatorListChanged = operatorListChanged;
2394
+ intentState.receivingOperatorList = true;
2395
+ intentState.opListReadCapability = createPromiseCapability();
2396
+ intentState.renderTasks = [];
2397
+ intentState.renderTasks.push(opListTask);
2398
+ intentState.operatorList = {
2399
+ fnArray: [],
2400
+ argsArray: [],
2401
+ lastChunk: false
2402
+ };
2403
+
2404
+ this.transport.messageHandler.send('RenderPageRequest', {
2405
+ pageIndex: this.pageIndex,
2406
+ intent: renderingIntent
2407
+ });
2408
+ }
2409
+ return intentState.opListReadCapability.promise;
2410
+ },
2411
+
2412
+ /**
2413
+ * @return {Promise} That is resolved a {@link TextContent}
2414
+ * object that represent the page text content.
2415
+ */
2416
+ getTextContent: function PDFPageProxy_getTextContent() {
2417
+ return this.transport.messageHandler.sendWithPromise('GetTextContent', {
2418
+ pageIndex: this.pageNumber - 1
2419
+ });
2420
+ },
2421
+ /**
2422
+ * Destroys resources allocated by the page.
2423
+ */
2424
+ destroy: function PDFPageProxy_destroy() {
2425
+ this.pendingDestroy = true;
2426
+ this._tryDestroy();
2427
+ },
2428
+ /**
2429
+ * For internal use only. Attempts to clean up if rendering is in a state
2430
+ * where that's possible.
2431
+ * @ignore
2432
+ */
2433
+ _tryDestroy: function PDFPageProxy__destroy() {
2434
+ if (!this.pendingDestroy ||
2435
+ Object.keys(this.intentStates).some(function(intent) {
2436
+ var intentState = this.intentStates[intent];
2437
+ return (intentState.renderTasks.length !== 0 ||
2438
+ intentState.receivingOperatorList);
2439
+ }, this)) {
2440
+ return;
2441
+ }
2442
+
2443
+ Object.keys(this.intentStates).forEach(function(intent) {
2444
+ delete this.intentStates[intent];
2445
+ }, this);
2446
+ this.objs.clear();
2447
+ this.annotationsPromise = null;
2448
+ this.pendingDestroy = false;
2449
+ },
2450
+ /**
2451
+ * For internal use only.
2452
+ * @ignore
2453
+ */
2454
+ _startRenderPage: function PDFPageProxy_startRenderPage(transparency,
2455
+ intent) {
2456
+ var intentState = this.intentStates[intent];
2457
+ // TODO Refactor RenderPageRequest to separate rendering
2458
+ // and operator list logic
2459
+ if (intentState.displayReadyCapability) {
2460
+ intentState.displayReadyCapability.resolve(transparency);
2461
+ }
2462
+ },
2463
+ /**
2464
+ * For internal use only.
2465
+ * @ignore
2466
+ */
2467
+ _renderPageChunk: function PDFPageProxy_renderPageChunk(operatorListChunk,
2468
+ intent) {
2469
+ var intentState = this.intentStates[intent];
2470
+ var i, ii;
2471
+ // Add the new chunk to the current operator list.
2472
+ for (i = 0, ii = operatorListChunk.length; i < ii; i++) {
2473
+ intentState.operatorList.fnArray.push(operatorListChunk.fnArray[i]);
2474
+ intentState.operatorList.argsArray.push(
2475
+ operatorListChunk.argsArray[i]);
2476
+ }
2477
+ intentState.operatorList.lastChunk = operatorListChunk.lastChunk;
2478
+
2479
+ // Notify all the rendering tasks there are more operators to be consumed.
2480
+ for (i = 0; i < intentState.renderTasks.length; i++) {
2481
+ intentState.renderTasks[i].operatorListChanged();
2482
+ }
2483
+
2484
+ if (operatorListChunk.lastChunk) {
2485
+ intentState.receivingOperatorList = false;
2486
+ this._tryDestroy();
2487
+ }
2488
+ }
2489
+ };
2490
+ return PDFPageProxy;
2491
+ })();
2492
+
2493
+ /**
2494
+ * For internal use only.
2495
+ * @ignore
2496
+ */
2497
+ var WorkerTransport = (function WorkerTransportClosure() {
2498
+ function WorkerTransport(workerInitializedCapability, pdfDataRangeTransport) {
2499
+ this.pdfDataRangeTransport = pdfDataRangeTransport;
2500
+ this.workerInitializedCapability = workerInitializedCapability;
2501
+ this.commonObjs = new PDFObjects();
2502
+
2503
+ this.loadingTask = null;
2504
+
2505
+ this.pageCache = [];
2506
+ this.pagePromises = [];
2507
+ this.downloadInfoCapability = createPromiseCapability();
2508
+
2509
+ // If worker support isn't disabled explicit and the browser has worker
2510
+ // support, create a new web worker and test if it/the browser fullfills
2511
+ // all requirements to run parts of pdf.js in a web worker.
2512
+ // Right now, the requirement is, that an Uint8Array is still an Uint8Array
2513
+ // as it arrives on the worker. Chrome added this with version 15.
2514
+ if (!globalScope.PDFJS.disableWorker && typeof Worker !== 'undefined') {
2515
+ var workerSrc = PDFJS.workerSrc;
2516
+ if (!workerSrc) {
2517
+ error('No PDFJS.workerSrc specified');
2518
+ }
2519
+
2520
+ try {
2521
+ // Some versions of FF can't create a worker on localhost, see:
2522
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=683280
2523
+ var worker = new Worker(workerSrc);
2524
+ var messageHandler = new MessageHandler('main', worker);
2525
+ this.messageHandler = messageHandler;
2526
+
2527
+ messageHandler.on('test', function transportTest(data) {
2528
+ var supportTypedArray = data && data.supportTypedArray;
2529
+ if (supportTypedArray) {
2530
+ this.worker = worker;
2531
+ if (!data.supportTransfers) {
2532
+ PDFJS.postMessageTransfers = false;
2533
+ }
2534
+ this.setupMessageHandler(messageHandler);
2535
+ workerInitializedCapability.resolve();
2536
+ } else {
2537
+ this.setupFakeWorker();
2538
+ }
2539
+ }.bind(this));
2540
+
2541
+ var testObj = new Uint8Array([PDFJS.postMessageTransfers ? 255 : 0]);
2542
+ // Some versions of Opera throw a DATA_CLONE_ERR on serializing the
2543
+ // typed array. Also, checking if we can use transfers.
2544
+ try {
2545
+ messageHandler.send('test', testObj, [testObj.buffer]);
2546
+ } catch (ex) {
2547
+ info('Cannot use postMessage transfers');
2548
+ testObj[0] = 0;
2549
+ messageHandler.send('test', testObj);
2550
+ }
2551
+ return;
2552
+ } catch (e) {
2553
+ info('The worker has been disabled.');
2554
+ }
2555
+ }
2556
+ // Either workers are disabled, not supported or have thrown an exception.
2557
+ // Thus, we fallback to a faked worker.
2558
+ this.setupFakeWorker();
2559
+ }
2560
+ WorkerTransport.prototype = {
2561
+ destroy: function WorkerTransport_destroy() {
2562
+ this.pageCache = [];
2563
+ this.pagePromises = [];
2564
+ var self = this;
2565
+ this.messageHandler.sendWithPromise('Terminate', null).then(function () {
2566
+ FontLoader.clear();
2567
+ if (self.worker) {
2568
+ self.worker.terminate();
2569
+ }
2570
+ });
2571
+ },
2572
+
2573
+ setupFakeWorker: function WorkerTransport_setupFakeWorker() {
2574
+ globalScope.PDFJS.disableWorker = true;
2575
+
2576
+ if (!PDFJS.fakeWorkerFilesLoadedCapability) {
2577
+ PDFJS.fakeWorkerFilesLoadedCapability = createPromiseCapability();
2578
+ // In the developer build load worker_loader which in turn loads all the
2579
+ // other files and resolves the promise. In production only the
2580
+ // pdf.worker.js file is needed.
2581
+ Util.loadScript(PDFJS.workerSrc, function() {
2582
+ PDFJS.fakeWorkerFilesLoadedCapability.resolve();
2583
+ });
2584
+ }
2585
+ PDFJS.fakeWorkerFilesLoadedCapability.promise.then(function () {
2586
+ warn('Setting up fake worker.');
2587
+ // If we don't use a worker, just post/sendMessage to the main thread.
2588
+ var fakeWorker = {
2589
+ postMessage: function WorkerTransport_postMessage(obj) {
2590
+ fakeWorker.onmessage({data: obj});
2591
+ },
2592
+ terminate: function WorkerTransport_terminate() {}
2593
+ };
2594
+
2595
+ var messageHandler = new MessageHandler('main', fakeWorker);
2596
+ this.setupMessageHandler(messageHandler);
2597
+
2598
+ // If the main thread is our worker, setup the handling for the messages
2599
+ // the main thread sends to it self.
2600
+ PDFJS.WorkerMessageHandler.setup(messageHandler);
2601
+
2602
+ this.workerInitializedCapability.resolve();
2603
+ }.bind(this));
2604
+ },
2605
+
2606
+ setupMessageHandler:
2607
+ function WorkerTransport_setupMessageHandler(messageHandler) {
2608
+ this.messageHandler = messageHandler;
2609
+
2610
+ function updatePassword(password) {
2611
+ messageHandler.send('UpdatePassword', password);
2612
+ }
2613
+
2614
+ var pdfDataRangeTransport = this.pdfDataRangeTransport;
2615
+ if (pdfDataRangeTransport) {
2616
+ pdfDataRangeTransport.addRangeListener(function(begin, chunk) {
2617
+ messageHandler.send('OnDataRange', {
2618
+ begin: begin,
2619
+ chunk: chunk
2620
+ });
2621
+ });
2622
+
2623
+ pdfDataRangeTransport.addProgressListener(function(loaded) {
2624
+ messageHandler.send('OnDataProgress', {
2625
+ loaded: loaded
2626
+ });
2627
+ });
2628
+
2629
+ pdfDataRangeTransport.addProgressiveReadListener(function(chunk) {
2630
+ messageHandler.send('OnDataRange', {
2631
+ chunk: chunk
2632
+ });
2633
+ });
2634
+
2635
+ messageHandler.on('RequestDataRange',
2636
+ function transportDataRange(data) {
2637
+ pdfDataRangeTransport.requestDataRange(data.begin, data.end);
2638
+ }, this);
2639
+ }
2640
+
2641
+ messageHandler.on('GetDoc', function transportDoc(data) {
2642
+ var pdfInfo = data.pdfInfo;
2643
+ this.numPages = data.pdfInfo.numPages;
2644
+ var pdfDocument = new PDFDocumentProxy(pdfInfo, this);
2645
+ this.pdfDocument = pdfDocument;
2646
+ this.loadingTask._capability.resolve(pdfDocument);
2647
+ }, this);
2648
+
2649
+ messageHandler.on('NeedPassword',
2650
+ function transportNeedPassword(exception) {
2651
+ var loadingTask = this.loadingTask;
2652
+ if (loadingTask.onPassword) {
2653
+ return loadingTask.onPassword(updatePassword,
2654
+ PasswordResponses.NEED_PASSWORD);
2655
+ }
2656
+ loadingTask._capability.reject(
2657
+ new PasswordException(exception.message, exception.code));
2658
+ }, this);
2659
+
2660
+ messageHandler.on('IncorrectPassword',
2661
+ function transportIncorrectPassword(exception) {
2662
+ var loadingTask = this.loadingTask;
2663
+ if (loadingTask.onPassword) {
2664
+ return loadingTask.onPassword(updatePassword,
2665
+ PasswordResponses.INCORRECT_PASSWORD);
2666
+ }
2667
+ loadingTask._capability.reject(
2668
+ new PasswordException(exception.message, exception.code));
2669
+ }, this);
2670
+
2671
+ messageHandler.on('InvalidPDF', function transportInvalidPDF(exception) {
2672
+ this.loadingTask._capability.reject(
2673
+ new InvalidPDFException(exception.message));
2674
+ }, this);
2675
+
2676
+ messageHandler.on('MissingPDF', function transportMissingPDF(exception) {
2677
+ this.loadingTask._capability.reject(
2678
+ new MissingPDFException(exception.message));
2679
+ }, this);
2680
+
2681
+ messageHandler.on('UnexpectedResponse',
2682
+ function transportUnexpectedResponse(exception) {
2683
+ this.loadingTask._capability.reject(
2684
+ new UnexpectedResponseException(exception.message, exception.status));
2685
+ }, this);
2686
+
2687
+ messageHandler.on('UnknownError',
2688
+ function transportUnknownError(exception) {
2689
+ this.loadingTask._capability.reject(
2690
+ new UnknownErrorException(exception.message, exception.details));
2691
+ }, this);
2692
+
2693
+ messageHandler.on('DataLoaded', function transportPage(data) {
2694
+ this.downloadInfoCapability.resolve(data);
2695
+ }, this);
2696
+
2697
+ messageHandler.on('PDFManagerReady', function transportPage(data) {
2698
+ if (this.pdfDataRangeTransport) {
2699
+ this.pdfDataRangeTransport.transportReady();
2700
+ }
2701
+ }, this);
2702
+
2703
+ messageHandler.on('StartRenderPage', function transportRender(data) {
2704
+ var page = this.pageCache[data.pageIndex];
2705
+
2706
+ page.stats.timeEnd('Page Request');
2707
+ page._startRenderPage(data.transparency, data.intent);
2708
+ }, this);
2709
+
2710
+ messageHandler.on('RenderPageChunk', function transportRender(data) {
2711
+ var page = this.pageCache[data.pageIndex];
2712
+
2713
+ page._renderPageChunk(data.operatorList, data.intent);
2714
+ }, this);
2715
+
2716
+ messageHandler.on('commonobj', function transportObj(data) {
2717
+ var id = data[0];
2718
+ var type = data[1];
2719
+ if (this.commonObjs.hasData(id)) {
2720
+ return;
2721
+ }
2722
+
2723
+ switch (type) {
2724
+ case 'Font':
2725
+ var exportedData = data[2];
2726
+
2727
+ var font;
2728
+ if ('error' in exportedData) {
2729
+ var error = exportedData.error;
2730
+ warn('Error during font loading: ' + error);
2731
+ this.commonObjs.resolve(id, error);
2732
+ break;
2733
+ } else {
2734
+ font = new FontFaceObject(exportedData);
2735
+ }
2736
+
2737
+ FontLoader.bind(
2738
+ [font],
2739
+ function fontReady(fontObjs) {
2740
+ this.commonObjs.resolve(id, font);
2741
+ }.bind(this)
2742
+ );
2743
+ break;
2744
+ case 'FontPath':
2745
+ this.commonObjs.resolve(id, data[2]);
2746
+ break;
2747
+ default:
2748
+ error('Got unknown common object type ' + type);
2749
+ }
2750
+ }, this);
2751
+
2752
+ messageHandler.on('obj', function transportObj(data) {
2753
+ var id = data[0];
2754
+ var pageIndex = data[1];
2755
+ var type = data[2];
2756
+ var pageProxy = this.pageCache[pageIndex];
2757
+ var imageData;
2758
+ if (pageProxy.objs.hasData(id)) {
2759
+ return;
2760
+ }
2761
+
2762
+ switch (type) {
2763
+ case 'JpegStream':
2764
+ imageData = data[3];
2765
+ loadJpegStream(id, imageData, pageProxy.objs);
2766
+ break;
2767
+ case 'Image':
2768
+ imageData = data[3];
2769
+ pageProxy.objs.resolve(id, imageData);
2770
+
2771
+ // heuristics that will allow not to store large data
2772
+ var MAX_IMAGE_SIZE_TO_STORE = 8000000;
2773
+ if (imageData && 'data' in imageData &&
2774
+ imageData.data.length > MAX_IMAGE_SIZE_TO_STORE) {
2775
+ pageProxy.cleanupAfterRender = true;
2776
+ }
2777
+ break;
2778
+ default:
2779
+ error('Got unknown object type ' + type);
2780
+ }
2781
+ }, this);
2782
+
2783
+ messageHandler.on('DocProgress', function transportDocProgress(data) {
2784
+ var loadingTask = this.loadingTask;
2785
+ if (loadingTask.onProgress) {
2786
+ loadingTask.onProgress({
2787
+ loaded: data.loaded,
2788
+ total: data.total
2789
+ });
2790
+ }
2791
+ }, this);
2792
+
2793
+ messageHandler.on('PageError', function transportError(data) {
2794
+ var page = this.pageCache[data.pageNum - 1];
2795
+ var intentState = page.intentStates[data.intent];
2796
+ if (intentState.displayReadyCapability) {
2797
+ intentState.displayReadyCapability.reject(data.error);
2798
+ } else {
2799
+ error(data.error);
2800
+ }
2801
+ }, this);
2802
+
2803
+ messageHandler.on('JpegDecode', function(data) {
2804
+ var imageUrl = data[0];
2805
+ var components = data[1];
2806
+ if (components !== 3 && components !== 1) {
2807
+ return Promise.reject(
2808
+ new Error('Only 3 components or 1 component can be returned'));
2809
+ }
2810
+
2811
+ return new Promise(function (resolve, reject) {
2812
+ var img = new Image();
2813
+ img.onload = function () {
2814
+ var width = img.width;
2815
+ var height = img.height;
2816
+ var size = width * height;
2817
+ var rgbaLength = size * 4;
2818
+ var buf = new Uint8Array(size * components);
2819
+ var tmpCanvas = createScratchCanvas(width, height);
2820
+ var tmpCtx = tmpCanvas.getContext('2d');
2821
+ tmpCtx.drawImage(img, 0, 0);
2822
+ var data = tmpCtx.getImageData(0, 0, width, height).data;
2823
+ var i, j;
2824
+
2825
+ if (components === 3) {
2826
+ for (i = 0, j = 0; i < rgbaLength; i += 4, j += 3) {
2827
+ buf[j] = data[i];
2828
+ buf[j + 1] = data[i + 1];
2829
+ buf[j + 2] = data[i + 2];
2830
+ }
2831
+ } else if (components === 1) {
2832
+ for (i = 0, j = 0; i < rgbaLength; i += 4, j++) {
2833
+ buf[j] = data[i];
2834
+ }
2835
+ }
2836
+ resolve({ data: buf, width: width, height: height});
2837
+ };
2838
+ img.onerror = function () {
2839
+ reject(new Error('JpegDecode failed to load image'));
2840
+ };
2841
+ img.src = imageUrl;
2842
+ });
2843
+ });
2844
+ },
2845
+
2846
+ fetchDocument: function WorkerTransport_fetchDocument(loadingTask, source) {
2847
+ this.loadingTask = loadingTask;
2848
+
2849
+ source.disableAutoFetch = PDFJS.disableAutoFetch;
2850
+ source.disableStream = PDFJS.disableStream;
2851
+ source.chunkedViewerLoading = !!this.pdfDataRangeTransport;
2852
+ if (this.pdfDataRangeTransport) {
2853
+ source.length = this.pdfDataRangeTransport.length;
2854
+ source.initialData = this.pdfDataRangeTransport.initialData;
2855
+ }
2856
+ this.messageHandler.send('GetDocRequest', {
2857
+ source: source,
2858
+ disableRange: PDFJS.disableRange,
2859
+ maxImageSize: PDFJS.maxImageSize,
2860
+ cMapUrl: PDFJS.cMapUrl,
2861
+ cMapPacked: PDFJS.cMapPacked,
2862
+ disableFontFace: PDFJS.disableFontFace,
2863
+ disableCreateObjectURL: PDFJS.disableCreateObjectURL,
2864
+ verbosity: PDFJS.verbosity
2865
+ });
2866
+ },
2867
+
2868
+ getData: function WorkerTransport_getData() {
2869
+ return this.messageHandler.sendWithPromise('GetData', null);
2870
+ },
2871
+
2872
+ getPage: function WorkerTransport_getPage(pageNumber, capability) {
2873
+ if (pageNumber <= 0 || pageNumber > this.numPages ||
2874
+ (pageNumber|0) !== pageNumber) {
2875
+ return Promise.reject(new Error('Invalid page request'));
2876
+ }
2877
+
2878
+ var pageIndex = pageNumber - 1;
2879
+ if (pageIndex in this.pagePromises) {
2880
+ return this.pagePromises[pageIndex];
2881
+ }
2882
+ var promise = this.messageHandler.sendWithPromise('GetPage', {
2883
+ pageIndex: pageIndex
2884
+ }).then(function (pageInfo) {
2885
+ var page = new PDFPageProxy(pageIndex, pageInfo, this);
2886
+ this.pageCache[pageIndex] = page;
2887
+ return page;
2888
+ }.bind(this));
2889
+ this.pagePromises[pageIndex] = promise;
2890
+ return promise;
2891
+ },
2892
+
2893
+ getPageIndex: function WorkerTransport_getPageIndexByRef(ref) {
2894
+ return this.messageHandler.sendWithPromise('GetPageIndex', { ref: ref });
2895
+ },
2896
+
2897
+ getAnnotations: function WorkerTransport_getAnnotations(pageIndex) {
2898
+ return this.messageHandler.sendWithPromise('GetAnnotations',
2899
+ { pageIndex: pageIndex });
2900
+ },
2901
+
2902
+ getDestinations: function WorkerTransport_getDestinations() {
2903
+ return this.messageHandler.sendWithPromise('GetDestinations', null);
2904
+ },
2905
+
2906
+ getDestination: function WorkerTransport_getDestination(id) {
2907
+ return this.messageHandler.sendWithPromise('GetDestination', { id: id } );
2908
+ },
2909
+
2910
+ getAttachments: function WorkerTransport_getAttachments() {
2911
+ return this.messageHandler.sendWithPromise('GetAttachments', null);
2912
+ },
2913
+
2914
+ getJavaScript: function WorkerTransport_getJavaScript() {
2915
+ return this.messageHandler.sendWithPromise('GetJavaScript', null);
2916
+ },
2917
+
2918
+ getOutline: function WorkerTransport_getOutline() {
2919
+ return this.messageHandler.sendWithPromise('GetOutline', null);
2920
+ },
2921
+
2922
+ getMetadata: function WorkerTransport_getMetadata() {
2923
+ return this.messageHandler.sendWithPromise('GetMetadata', null).
2924
+ then(function transportMetadata(results) {
2925
+ return {
2926
+ info: results[0],
2927
+ metadata: (results[1] ? new PDFJS.Metadata(results[1]) : null)
2928
+ };
2929
+ });
2930
+ },
2931
+
2932
+ getStats: function WorkerTransport_getStats() {
2933
+ return this.messageHandler.sendWithPromise('GetStats', null);
2934
+ },
2935
+
2936
+ startCleanup: function WorkerTransport_startCleanup() {
2937
+ this.messageHandler.sendWithPromise('Cleanup', null).
2938
+ then(function endCleanup() {
2939
+ for (var i = 0, ii = this.pageCache.length; i < ii; i++) {
2940
+ var page = this.pageCache[i];
2941
+ if (page) {
2942
+ page.destroy();
2943
+ }
2944
+ }
2945
+ this.commonObjs.clear();
2946
+ FontLoader.clear();
2947
+ }.bind(this));
2948
+ }
2949
+ };
2950
+ return WorkerTransport;
2951
+
2952
+ })();
2953
+
2954
+ /**
2955
+ * A PDF document and page is built of many objects. E.g. there are objects
2956
+ * for fonts, images, rendering code and such. These objects might get processed
2957
+ * inside of a worker. The `PDFObjects` implements some basic functions to
2958
+ * manage these objects.
2959
+ * @ignore
2960
+ */
2961
+ var PDFObjects = (function PDFObjectsClosure() {
2962
+ function PDFObjects() {
2963
+ this.objs = {};
2964
+ }
2965
+
2966
+ PDFObjects.prototype = {
2967
+ /**
2968
+ * Internal function.
2969
+ * Ensures there is an object defined for `objId`.
2970
+ */
2971
+ ensureObj: function PDFObjects_ensureObj(objId) {
2972
+ if (this.objs[objId]) {
2973
+ return this.objs[objId];
2974
+ }
2975
+
2976
+ var obj = {
2977
+ capability: createPromiseCapability(),
2978
+ data: null,
2979
+ resolved: false
2980
+ };
2981
+ this.objs[objId] = obj;
2982
+
2983
+ return obj;
2984
+ },
2985
+
2986
+ /**
2987
+ * If called *without* callback, this returns the data of `objId` but the
2988
+ * object needs to be resolved. If it isn't, this function throws.
2989
+ *
2990
+ * If called *with* a callback, the callback is called with the data of the
2991
+ * object once the object is resolved. That means, if you call this
2992
+ * function and the object is already resolved, the callback gets called
2993
+ * right away.
2994
+ */
2995
+ get: function PDFObjects_get(objId, callback) {
2996
+ // If there is a callback, then the get can be async and the object is
2997
+ // not required to be resolved right now
2998
+ if (callback) {
2999
+ this.ensureObj(objId).capability.promise.then(callback);
3000
+ return null;
3001
+ }
3002
+
3003
+ // If there isn't a callback, the user expects to get the resolved data
3004
+ // directly.
3005
+ var obj = this.objs[objId];
3006
+
3007
+ // If there isn't an object yet or the object isn't resolved, then the
3008
+ // data isn't ready yet!
3009
+ if (!obj || !obj.resolved) {
3010
+ error('Requesting object that isn\'t resolved yet ' + objId);
3011
+ }
3012
+
3013
+ return obj.data;
3014
+ },
3015
+
3016
+ /**
3017
+ * Resolves the object `objId` with optional `data`.
3018
+ */
3019
+ resolve: function PDFObjects_resolve(objId, data) {
3020
+ var obj = this.ensureObj(objId);
3021
+
3022
+ obj.resolved = true;
3023
+ obj.data = data;
3024
+ obj.capability.resolve(data);
3025
+ },
3026
+
3027
+ isResolved: function PDFObjects_isResolved(objId) {
3028
+ var objs = this.objs;
3029
+
3030
+ if (!objs[objId]) {
3031
+ return false;
3032
+ } else {
3033
+ return objs[objId].resolved;
3034
+ }
3035
+ },
3036
+
3037
+ hasData: function PDFObjects_hasData(objId) {
3038
+ return this.isResolved(objId);
3039
+ },
3040
+
3041
+ /**
3042
+ * Returns the data of `objId` if object exists, null otherwise.
3043
+ */
3044
+ getData: function PDFObjects_getData(objId) {
3045
+ var objs = this.objs;
3046
+ if (!objs[objId] || !objs[objId].resolved) {
3047
+ return null;
3048
+ } else {
3049
+ return objs[objId].data;
3050
+ }
3051
+ },
3052
+
3053
+ clear: function PDFObjects_clear() {
3054
+ this.objs = {};
3055
+ }
3056
+ };
3057
+ return PDFObjects;
3058
+ })();
3059
+
3060
+ /**
3061
+ * Allows controlling of the rendering tasks.
3062
+ * @class
3063
+ */
3064
+ var RenderTask = (function RenderTaskClosure() {
3065
+ function RenderTask(internalRenderTask) {
3066
+ this._internalRenderTask = internalRenderTask;
3067
+
3068
+ /**
3069
+ * Callback for incremental rendering -- a function that will be called
3070
+ * each time the rendering is paused. To continue rendering call the
3071
+ * function that is the first argument to the callback.
3072
+ * @type {function}
3073
+ */
3074
+ this.onContinue = null;
3075
+ }
3076
+
3077
+ RenderTask.prototype = /** @lends RenderTask.prototype */ {
3078
+ /**
3079
+ * Promise for rendering task completion.
3080
+ * @return {Promise}
3081
+ */
3082
+ get promise() {
3083
+ return this._internalRenderTask.capability.promise;
3084
+ },
3085
+
3086
+ /**
3087
+ * Cancels the rendering task. If the task is currently rendering it will
3088
+ * not be cancelled until graphics pauses with a timeout. The promise that
3089
+ * this object extends will resolved when cancelled.
3090
+ */
3091
+ cancel: function RenderTask_cancel() {
3092
+ this._internalRenderTask.cancel();
3093
+ },
3094
+
3095
+ /**
3096
+ * Registers callbacks to indicate the rendering task completion.
3097
+ *
3098
+ * @param {function} onFulfilled The callback for the rendering completion.
3099
+ * @param {function} onRejected The callback for the rendering failure.
3100
+ * @return {Promise} A promise that is resolved after the onFulfilled or
3101
+ * onRejected callback.
3102
+ */
3103
+ then: function RenderTask_then(onFulfilled, onRejected) {
3104
+ return this.promise.then.apply(this.promise, arguments);
3105
+ }
3106
+ };
3107
+
3108
+ return RenderTask;
3109
+ })();
3110
+
3111
+ /**
3112
+ * For internal use only.
3113
+ * @ignore
3114
+ */
3115
+ var InternalRenderTask = (function InternalRenderTaskClosure() {
3116
+
3117
+ function InternalRenderTask(callback, params, objs, commonObjs, operatorList,
3118
+ pageNumber) {
3119
+ this.callback = callback;
3120
+ this.params = params;
3121
+ this.objs = objs;
3122
+ this.commonObjs = commonObjs;
3123
+ this.operatorListIdx = null;
3124
+ this.operatorList = operatorList;
3125
+ this.pageNumber = pageNumber;
3126
+ this.running = false;
3127
+ this.graphicsReadyCallback = null;
3128
+ this.graphicsReady = false;
3129
+ this.cancelled = false;
3130
+ this.capability = createPromiseCapability();
3131
+ this.task = new RenderTask(this);
3132
+ // caching this-bound methods
3133
+ this._continueBound = this._continue.bind(this);
3134
+ this._scheduleNextBound = this._scheduleNext.bind(this);
3135
+ this._nextBound = this._next.bind(this);
3136
+ }
3137
+
3138
+ InternalRenderTask.prototype = {
3139
+
3140
+ initalizeGraphics:
3141
+ function InternalRenderTask_initalizeGraphics(transparency) {
3142
+
3143
+ if (this.cancelled) {
3144
+ return;
3145
+ }
3146
+ if (PDFJS.pdfBug && 'StepperManager' in globalScope &&
3147
+ globalScope.StepperManager.enabled) {
3148
+ this.stepper = globalScope.StepperManager.create(this.pageNumber - 1);
3149
+ this.stepper.init(this.operatorList);
3150
+ this.stepper.nextBreakPoint = this.stepper.getNextBreakPoint();
3151
+ }
3152
+
3153
+ var params = this.params;
3154
+ this.gfx = new CanvasGraphics(params.canvasContext, this.commonObjs,
3155
+ this.objs, params.imageLayer);
3156
+
3157
+ this.gfx.beginDrawing(params.viewport, transparency);
3158
+ this.operatorListIdx = 0;
3159
+ this.graphicsReady = true;
3160
+ if (this.graphicsReadyCallback) {
3161
+ this.graphicsReadyCallback();
3162
+ }
3163
+ },
3164
+
3165
+ cancel: function InternalRenderTask_cancel() {
3166
+ this.running = false;
3167
+ this.cancelled = true;
3168
+ this.callback('cancelled');
3169
+ },
3170
+
3171
+ operatorListChanged: function InternalRenderTask_operatorListChanged() {
3172
+ if (!this.graphicsReady) {
3173
+ if (!this.graphicsReadyCallback) {
3174
+ this.graphicsReadyCallback = this._continueBound;
3175
+ }
3176
+ return;
3177
+ }
3178
+
3179
+ if (this.stepper) {
3180
+ this.stepper.updateOperatorList(this.operatorList);
3181
+ }
3182
+
3183
+ if (this.running) {
3184
+ return;
3185
+ }
3186
+ this._continue();
3187
+ },
3188
+
3189
+ _continue: function InternalRenderTask__continue() {
3190
+ this.running = true;
3191
+ if (this.cancelled) {
3192
+ return;
3193
+ }
3194
+ if (this.task.onContinue) {
3195
+ this.task.onContinue.call(this.task, this._scheduleNextBound);
3196
+ } else {
3197
+ this._scheduleNext();
3198
+ }
3199
+ },
3200
+
3201
+ _scheduleNext: function InternalRenderTask__scheduleNext() {
3202
+ window.requestAnimationFrame(this._nextBound);
3203
+ },
3204
+
3205
+ _next: function InternalRenderTask__next() {
3206
+ if (this.cancelled) {
3207
+ return;
3208
+ }
3209
+ this.operatorListIdx = this.gfx.executeOperatorList(this.operatorList,
3210
+ this.operatorListIdx,
3211
+ this._continueBound,
3212
+ this.stepper);
3213
+ if (this.operatorListIdx === this.operatorList.argsArray.length) {
3214
+ this.running = false;
3215
+ if (this.operatorList.lastChunk) {
3216
+ this.gfx.endDrawing();
3217
+ this.callback();
3218
+ }
3219
+ }
3220
+ }
3221
+
3222
+ };
3223
+
3224
+ return InternalRenderTask;
3225
+ })();
3226
+
3227
+
3228
+ var Metadata = PDFJS.Metadata = (function MetadataClosure() {
3229
+ function fixMetadata(meta) {
3230
+ return meta.replace(/>\\376\\377([^<]+)/g, function(all, codes) {
3231
+ var bytes = codes.replace(/\\([0-3])([0-7])([0-7])/g,
3232
+ function(code, d1, d2, d3) {
3233
+ return String.fromCharCode(d1 * 64 + d2 * 8 + d3 * 1);
3234
+ });
3235
+ var chars = '';
3236
+ for (var i = 0; i < bytes.length; i += 2) {
3237
+ var code = bytes.charCodeAt(i) * 256 + bytes.charCodeAt(i + 1);
3238
+ chars += code >= 32 && code < 127 && code !== 60 && code !== 62 &&
3239
+ code !== 38 && false ? String.fromCharCode(code) :
3240
+ '&#x' + (0x10000 + code).toString(16).substring(1) + ';';
3241
+ }
3242
+ return '>' + chars;
3243
+ });
3244
+ }
3245
+
3246
+ function Metadata(meta) {
3247
+ if (typeof meta === 'string') {
3248
+ // Ghostscript produces invalid metadata
3249
+ meta = fixMetadata(meta);
3250
+
3251
+ var parser = new DOMParser();
3252
+ meta = parser.parseFromString(meta, 'application/xml');
3253
+ } else if (!(meta instanceof Document)) {
3254
+ error('Metadata: Invalid metadata object');
3255
+ }
3256
+
3257
+ this.metaDocument = meta;
3258
+ this.metadata = {};
3259
+ this.parse();
3260
+ }
3261
+
3262
+ Metadata.prototype = {
3263
+ parse: function Metadata_parse() {
3264
+ var doc = this.metaDocument;
3265
+ var rdf = doc.documentElement;
3266
+
3267
+ if (rdf.nodeName.toLowerCase() !== 'rdf:rdf') { // Wrapped in <xmpmeta>
3268
+ rdf = rdf.firstChild;
3269
+ while (rdf && rdf.nodeName.toLowerCase() !== 'rdf:rdf') {
3270
+ rdf = rdf.nextSibling;
3271
+ }
3272
+ }
3273
+
3274
+ var nodeName = (rdf) ? rdf.nodeName.toLowerCase() : null;
3275
+ if (!rdf || nodeName !== 'rdf:rdf' || !rdf.hasChildNodes()) {
3276
+ return;
3277
+ }
3278
+
3279
+ var children = rdf.childNodes, desc, entry, name, i, ii, length, iLength;
3280
+ for (i = 0, length = children.length; i < length; i++) {
3281
+ desc = children[i];
3282
+ if (desc.nodeName.toLowerCase() !== 'rdf:description') {
3283
+ continue;
3284
+ }
3285
+
3286
+ for (ii = 0, iLength = desc.childNodes.length; ii < iLength; ii++) {
3287
+ if (desc.childNodes[ii].nodeName.toLowerCase() !== '#text') {
3288
+ entry = desc.childNodes[ii];
3289
+ name = entry.nodeName.toLowerCase();
3290
+ this.metadata[name] = entry.textContent.trim();
3291
+ }
3292
+ }
3293
+ }
3294
+ },
3295
+
3296
+ get: function Metadata_get(name) {
3297
+ return this.metadata[name] || null;
3298
+ },
3299
+
3300
+ has: function Metadata_has(name) {
3301
+ return typeof this.metadata[name] !== 'undefined';
3302
+ }
3303
+ };
3304
+
3305
+ return Metadata;
3306
+ })();
3307
+
3308
+
3309
+ // <canvas> contexts store most of the state we need natively.
3310
+ // However, PDF needs a bit more state, which we store here.
3311
+
3312
+ // Minimal font size that would be used during canvas fillText operations.
3313
+ var MIN_FONT_SIZE = 16;
3314
+ // Maximum font size that would be used during canvas fillText operations.
3315
+ var MAX_FONT_SIZE = 100;
3316
+ var MAX_GROUP_SIZE = 4096;
3317
+
3318
+ // Heuristic value used when enforcing minimum line widths.
3319
+ var MIN_WIDTH_FACTOR = 0.65;
3320
+
3321
+ var COMPILE_TYPE3_GLYPHS = true;
3322
+ var MAX_SIZE_TO_COMPILE = 1000;
3323
+
3324
+ var FULL_CHUNK_HEIGHT = 16;
3325
+
3326
+ function createScratchCanvas(width, height) {
3327
+ var canvas = document.createElement('canvas');
3328
+ canvas.width = width;
3329
+ canvas.height = height;
3330
+ return canvas;
3331
+ }
3332
+
3333
+ function addContextCurrentTransform(ctx) {
3334
+ // If the context doesn't expose a `mozCurrentTransform`, add a JS based one.
3335
+ if (!ctx.mozCurrentTransform) {
3336
+ ctx._originalSave = ctx.save;
3337
+ ctx._originalRestore = ctx.restore;
3338
+ ctx._originalRotate = ctx.rotate;
3339
+ ctx._originalScale = ctx.scale;
3340
+ ctx._originalTranslate = ctx.translate;
3341
+ ctx._originalTransform = ctx.transform;
3342
+ ctx._originalSetTransform = ctx.setTransform;
3343
+
3344
+ ctx._transformMatrix = ctx._transformMatrix || [1, 0, 0, 1, 0, 0];
3345
+ ctx._transformStack = [];
3346
+
3347
+ Object.defineProperty(ctx, 'mozCurrentTransform', {
3348
+ get: function getCurrentTransform() {
3349
+ return this._transformMatrix;
3350
+ }
3351
+ });
3352
+
3353
+ Object.defineProperty(ctx, 'mozCurrentTransformInverse', {
3354
+ get: function getCurrentTransformInverse() {
3355
+ // Calculation done using WolframAlpha:
3356
+ // http://www.wolframalpha.com/input/?
3357
+ // i=Inverse+{{a%2C+c%2C+e}%2C+{b%2C+d%2C+f}%2C+{0%2C+0%2C+1}}
3358
+
3359
+ var m = this._transformMatrix;
3360
+ var a = m[0], b = m[1], c = m[2], d = m[3], e = m[4], f = m[5];
3361
+
3362
+ var ad_bc = a * d - b * c;
3363
+ var bc_ad = b * c - a * d;
3364
+
3365
+ return [
3366
+ d / ad_bc,
3367
+ b / bc_ad,
3368
+ c / bc_ad,
3369
+ a / ad_bc,
3370
+ (d * e - c * f) / bc_ad,
3371
+ (b * e - a * f) / ad_bc
3372
+ ];
3373
+ }
3374
+ });
3375
+
3376
+ ctx.save = function ctxSave() {
3377
+ var old = this._transformMatrix;
3378
+ this._transformStack.push(old);
3379
+ this._transformMatrix = old.slice(0, 6);
3380
+
3381
+ this._originalSave();
3382
+ };
3383
+
3384
+ ctx.restore = function ctxRestore() {
3385
+ var prev = this._transformStack.pop();
3386
+ if (prev) {
3387
+ this._transformMatrix = prev;
3388
+ this._originalRestore();
3389
+ }
3390
+ };
3391
+
3392
+ ctx.translate = function ctxTranslate(x, y) {
3393
+ var m = this._transformMatrix;
3394
+ m[4] = m[0] * x + m[2] * y + m[4];
3395
+ m[5] = m[1] * x + m[3] * y + m[5];
3396
+
3397
+ this._originalTranslate(x, y);
3398
+ };
3399
+
3400
+ ctx.scale = function ctxScale(x, y) {
3401
+ var m = this._transformMatrix;
3402
+ m[0] = m[0] * x;
3403
+ m[1] = m[1] * x;
3404
+ m[2] = m[2] * y;
3405
+ m[3] = m[3] * y;
3406
+
3407
+ this._originalScale(x, y);
3408
+ };
3409
+
3410
+ ctx.transform = function ctxTransform(a, b, c, d, e, f) {
3411
+ var m = this._transformMatrix;
3412
+ this._transformMatrix = [
3413
+ m[0] * a + m[2] * b,
3414
+ m[1] * a + m[3] * b,
3415
+ m[0] * c + m[2] * d,
3416
+ m[1] * c + m[3] * d,
3417
+ m[0] * e + m[2] * f + m[4],
3418
+ m[1] * e + m[3] * f + m[5]
3419
+ ];
3420
+
3421
+ ctx._originalTransform(a, b, c, d, e, f);
3422
+ };
3423
+
3424
+ ctx.setTransform = function ctxSetTransform(a, b, c, d, e, f) {
3425
+ this._transformMatrix = [a, b, c, d, e, f];
3426
+
3427
+ ctx._originalSetTransform(a, b, c, d, e, f);
3428
+ };
3429
+
3430
+ ctx.rotate = function ctxRotate(angle) {
3431
+ var cosValue = Math.cos(angle);
3432
+ var sinValue = Math.sin(angle);
3433
+
3434
+ var m = this._transformMatrix;
3435
+ this._transformMatrix = [
3436
+ m[0] * cosValue + m[2] * sinValue,
3437
+ m[1] * cosValue + m[3] * sinValue,
3438
+ m[0] * (-sinValue) + m[2] * cosValue,
3439
+ m[1] * (-sinValue) + m[3] * cosValue,
3440
+ m[4],
3441
+ m[5]
3442
+ ];
3443
+
3444
+ this._originalRotate(angle);
3445
+ };
3446
+ }
3447
+ }
3448
+
3449
+ var CachedCanvases = (function CachedCanvasesClosure() {
3450
+ var cache = {};
3451
+ return {
3452
+ getCanvas: function CachedCanvases_getCanvas(id, width, height,
3453
+ trackTransform) {
3454
+ var canvasEntry;
3455
+ if (cache[id] !== undefined) {
3456
+ canvasEntry = cache[id];
3457
+ canvasEntry.canvas.width = width;
3458
+ canvasEntry.canvas.height = height;
3459
+ // reset canvas transform for emulated mozCurrentTransform, if needed
3460
+ canvasEntry.context.setTransform(1, 0, 0, 1, 0, 0);
3461
+ } else {
3462
+ var canvas = createScratchCanvas(width, height);
3463
+ var ctx = canvas.getContext('2d');
3464
+ if (trackTransform) {
3465
+ addContextCurrentTransform(ctx);
3466
+ }
3467
+ cache[id] = canvasEntry = {canvas: canvas, context: ctx};
3468
+ }
3469
+ return canvasEntry;
3470
+ },
3471
+ clear: function () {
3472
+ for (var id in cache) {
3473
+ var canvasEntry = cache[id];
3474
+ // Zeroing the width and height causes Firefox to release graphics
3475
+ // resources immediately, which can greatly reduce memory consumption.
3476
+ canvasEntry.canvas.width = 0;
3477
+ canvasEntry.canvas.height = 0;
3478
+ delete cache[id];
3479
+ }
3480
+ }
3481
+ };
3482
+ })();
3483
+
3484
+ function compileType3Glyph(imgData) {
3485
+ var POINT_TO_PROCESS_LIMIT = 1000;
3486
+
3487
+ var width = imgData.width, height = imgData.height;
3488
+ var i, j, j0, width1 = width + 1;
3489
+ var points = new Uint8Array(width1 * (height + 1));
3490
+ var POINT_TYPES =
3491
+ new Uint8Array([0, 2, 4, 0, 1, 0, 5, 4, 8, 10, 0, 8, 0, 2, 1, 0]);
3492
+
3493
+ // decodes bit-packed mask data
3494
+ var lineSize = (width + 7) & ~7, data0 = imgData.data;
3495
+ var data = new Uint8Array(lineSize * height), pos = 0, ii;
3496
+ for (i = 0, ii = data0.length; i < ii; i++) {
3497
+ var mask = 128, elem = data0[i];
3498
+ while (mask > 0) {
3499
+ data[pos++] = (elem & mask) ? 0 : 255;
3500
+ mask >>= 1;
3501
+ }
3502
+ }
3503
+
3504
+ // finding iteresting points: every point is located between mask pixels,
3505
+ // so there will be points of the (width + 1)x(height + 1) grid. Every point
3506
+ // will have flags assigned based on neighboring mask pixels:
3507
+ // 4 | 8
3508
+ // --P--
3509
+ // 2 | 1
3510
+ // We are interested only in points with the flags:
3511
+ // - outside corners: 1, 2, 4, 8;
3512
+ // - inside corners: 7, 11, 13, 14;
3513
+ // - and, intersections: 5, 10.
3514
+ var count = 0;
3515
+ pos = 0;
3516
+ if (data[pos] !== 0) {
3517
+ points[0] = 1;
3518
+ ++count;
3519
+ }
3520
+ for (j = 1; j < width; j++) {
3521
+ if (data[pos] !== data[pos + 1]) {
3522
+ points[j] = data[pos] ? 2 : 1;
3523
+ ++count;
3524
+ }
3525
+ pos++;
3526
+ }
3527
+ if (data[pos] !== 0) {
3528
+ points[j] = 2;
3529
+ ++count;
3530
+ }
3531
+ for (i = 1; i < height; i++) {
3532
+ pos = i * lineSize;
3533
+ j0 = i * width1;
3534
+ if (data[pos - lineSize] !== data[pos]) {
3535
+ points[j0] = data[pos] ? 1 : 8;
3536
+ ++count;
3537
+ }
3538
+ // 'sum' is the position of the current pixel configuration in the 'TYPES'
3539
+ // array (in order 8-1-2-4, so we can use '>>2' to shift the column).
3540
+ var sum = (data[pos] ? 4 : 0) + (data[pos - lineSize] ? 8 : 0);
3541
+ for (j = 1; j < width; j++) {
3542
+ sum = (sum >> 2) + (data[pos + 1] ? 4 : 0) +
3543
+ (data[pos - lineSize + 1] ? 8 : 0);
3544
+ if (POINT_TYPES[sum]) {
3545
+ points[j0 + j] = POINT_TYPES[sum];
3546
+ ++count;
3547
+ }
3548
+ pos++;
3549
+ }
3550
+ if (data[pos - lineSize] !== data[pos]) {
3551
+ points[j0 + j] = data[pos] ? 2 : 4;
3552
+ ++count;
3553
+ }
3554
+
3555
+ if (count > POINT_TO_PROCESS_LIMIT) {
3556
+ return null;
3557
+ }
3558
+ }
3559
+
3560
+ pos = lineSize * (height - 1);
3561
+ j0 = i * width1;
3562
+ if (data[pos] !== 0) {
3563
+ points[j0] = 8;
3564
+ ++count;
3565
+ }
3566
+ for (j = 1; j < width; j++) {
3567
+ if (data[pos] !== data[pos + 1]) {
3568
+ points[j0 + j] = data[pos] ? 4 : 8;
3569
+ ++count;
3570
+ }
3571
+ pos++;
3572
+ }
3573
+ if (data[pos] !== 0) {
3574
+ points[j0 + j] = 4;
3575
+ ++count;
3576
+ }
3577
+ if (count > POINT_TO_PROCESS_LIMIT) {
3578
+ return null;
3579
+ }
3580
+
3581
+ // building outlines
3582
+ var steps = new Int32Array([0, width1, -1, 0, -width1, 0, 0, 0, 1]);
3583
+ var outlines = [];
3584
+ for (i = 0; count && i <= height; i++) {
3585
+ var p = i * width1;
3586
+ var end = p + width;
3587
+ while (p < end && !points[p]) {
3588
+ p++;
3589
+ }
3590
+ if (p === end) {
3591
+ continue;
3592
+ }
3593
+ var coords = [p % width1, i];
3594
+
3595
+ var type = points[p], p0 = p, pp;
3596
+ do {
3597
+ var step = steps[type];
3598
+ do {
3599
+ p += step;
3600
+ } while (!points[p]);
3601
+
3602
+ pp = points[p];
3603
+ if (pp !== 5 && pp !== 10) {
3604
+ // set new direction
3605
+ type = pp;
3606
+ // delete mark
3607
+ points[p] = 0;
3608
+ } else { // type is 5 or 10, ie, a crossing
3609
+ // set new direction
3610
+ type = pp & ((0x33 * type) >> 4);
3611
+ // set new type for "future hit"
3612
+ points[p] &= (type >> 2 | type << 2);
3613
+ }
3614
+
3615
+ coords.push(p % width1);
3616
+ coords.push((p / width1) | 0);
3617
+ --count;
3618
+ } while (p0 !== p);
3619
+ outlines.push(coords);
3620
+ --i;
3621
+ }
3622
+
3623
+ var drawOutline = function(c) {
3624
+ c.save();
3625
+ // the path shall be painted in [0..1]x[0..1] space
3626
+ c.scale(1 / width, -1 / height);
3627
+ c.translate(0, -height);
3628
+ c.beginPath();
3629
+ for (var i = 0, ii = outlines.length; i < ii; i++) {
3630
+ var o = outlines[i];
3631
+ c.moveTo(o[0], o[1]);
3632
+ for (var j = 2, jj = o.length; j < jj; j += 2) {
3633
+ c.lineTo(o[j], o[j+1]);
3634
+ }
3635
+ }
3636
+ c.fill();
3637
+ c.beginPath();
3638
+ c.restore();
3639
+ };
3640
+
3641
+ return drawOutline;
3642
+ }
3643
+
3644
+ var CanvasExtraState = (function CanvasExtraStateClosure() {
3645
+ function CanvasExtraState(old) {
3646
+ // Are soft masks and alpha values shapes or opacities?
3647
+ this.alphaIsShape = false;
3648
+ this.fontSize = 0;
3649
+ this.fontSizeScale = 1;
3650
+ this.textMatrix = IDENTITY_MATRIX;
3651
+ this.textMatrixScale = 1;
3652
+ this.fontMatrix = FONT_IDENTITY_MATRIX;
3653
+ this.leading = 0;
3654
+ // Current point (in user coordinates)
3655
+ this.x = 0;
3656
+ this.y = 0;
3657
+ // Start of text line (in text coordinates)
3658
+ this.lineX = 0;
3659
+ this.lineY = 0;
3660
+ // Character and word spacing
3661
+ this.charSpacing = 0;
3662
+ this.wordSpacing = 0;
3663
+ this.textHScale = 1;
3664
+ this.textRenderingMode = TextRenderingMode.FILL;
3665
+ this.textRise = 0;
3666
+ // Default fore and background colors
3667
+ this.fillColor = '#000000';
3668
+ this.strokeColor = '#000000';
3669
+ this.patternFill = false;
3670
+ // Note: fill alpha applies to all non-stroking operations
3671
+ this.fillAlpha = 1;
3672
+ this.strokeAlpha = 1;
3673
+ this.lineWidth = 1;
3674
+ this.activeSMask = null; // nonclonable field (see the save method below)
3675
+
3676
+ this.old = old;
3677
+ }
3678
+
3679
+ CanvasExtraState.prototype = {
3680
+ clone: function CanvasExtraState_clone() {
3681
+ return Object.create(this);
3682
+ },
3683
+ setCurrentPoint: function CanvasExtraState_setCurrentPoint(x, y) {
3684
+ this.x = x;
3685
+ this.y = y;
3686
+ }
3687
+ };
3688
+ return CanvasExtraState;
3689
+ })();
3690
+
3691
+ var CanvasGraphics = (function CanvasGraphicsClosure() {
3692
+ // Defines the time the executeOperatorList is going to be executing
3693
+ // before it stops and shedules a continue of execution.
3694
+ var EXECUTION_TIME = 15;
3695
+ // Defines the number of steps before checking the execution time
3696
+ var EXECUTION_STEPS = 10;
3697
+
3698
+ function CanvasGraphics(canvasCtx, commonObjs, objs, imageLayer) {
3699
+ this.ctx = canvasCtx;
3700
+ this.current = new CanvasExtraState();
3701
+ this.stateStack = [];
3702
+ this.pendingClip = null;
3703
+ this.pendingEOFill = false;
3704
+ this.res = null;
3705
+ this.xobjs = null;
3706
+ this.commonObjs = commonObjs;
3707
+ this.objs = objs;
3708
+ this.imageLayer = imageLayer;
3709
+ this.groupStack = [];
3710
+ this.processingType3 = null;
3711
+ // Patterns are painted relative to the initial page/form transform, see pdf
3712
+ // spec 8.7.2 NOTE 1.
3713
+ this.baseTransform = null;
3714
+ this.baseTransformStack = [];
3715
+ this.groupLevel = 0;
3716
+ this.smaskStack = [];
3717
+ this.smaskCounter = 0;
3718
+ this.tempSMask = null;
3719
+ if (canvasCtx) {
3720
+ // NOTE: if mozCurrentTransform is polyfilled, then the current state of
3721
+ // the transformation must already be set in canvasCtx._transformMatrix.
3722
+ addContextCurrentTransform(canvasCtx);
3723
+ }
3724
+ this.cachedGetSinglePixelWidth = null;
3725
+ }
3726
+
3727
+ function putBinaryImageData(ctx, imgData) {
3728
+ if (typeof ImageData !== 'undefined' && imgData instanceof ImageData) {
3729
+ ctx.putImageData(imgData, 0, 0);
3730
+ return;
3731
+ }
3732
+
3733
+ // Put the image data to the canvas in chunks, rather than putting the
3734
+ // whole image at once. This saves JS memory, because the ImageData object
3735
+ // is smaller. It also possibly saves C++ memory within the implementation
3736
+ // of putImageData(). (E.g. in Firefox we make two short-lived copies of
3737
+ // the data passed to putImageData()). |n| shouldn't be too small, however,
3738
+ // because too many putImageData() calls will slow things down.
3739
+ //
3740
+ // Note: as written, if the last chunk is partial, the putImageData() call
3741
+ // will (conceptually) put pixels past the bounds of the canvas. But
3742
+ // that's ok; any such pixels are ignored.
3743
+
3744
+ var height = imgData.height, width = imgData.width;
3745
+ var partialChunkHeight = height % FULL_CHUNK_HEIGHT;
3746
+ var fullChunks = (height - partialChunkHeight) / FULL_CHUNK_HEIGHT;
3747
+ var totalChunks = partialChunkHeight === 0 ? fullChunks : fullChunks + 1;
3748
+
3749
+ var chunkImgData = ctx.createImageData(width, FULL_CHUNK_HEIGHT);
3750
+ var srcPos = 0, destPos;
3751
+ var src = imgData.data;
3752
+ var dest = chunkImgData.data;
3753
+ var i, j, thisChunkHeight, elemsInThisChunk;
3754
+
3755
+ // There are multiple forms in which the pixel data can be passed, and
3756
+ // imgData.kind tells us which one this is.
3757
+ if (imgData.kind === ImageKind.GRAYSCALE_1BPP) {
3758
+ // Grayscale, 1 bit per pixel (i.e. black-and-white).
3759
+ var srcLength = src.byteLength;
3760
+ var dest32 = PDFJS.hasCanvasTypedArrays ? new Uint32Array(dest.buffer) :
3761
+ new Uint32ArrayView(dest);
3762
+ var dest32DataLength = dest32.length;
3763
+ var fullSrcDiff = (width + 7) >> 3;
3764
+ var white = 0xFFFFFFFF;
3765
+ var black = (PDFJS.isLittleEndian || !PDFJS.hasCanvasTypedArrays) ?
3766
+ 0xFF000000 : 0x000000FF;
3767
+ for (i = 0; i < totalChunks; i++) {
3768
+ thisChunkHeight =
3769
+ (i < fullChunks) ? FULL_CHUNK_HEIGHT : partialChunkHeight;
3770
+ destPos = 0;
3771
+ for (j = 0; j < thisChunkHeight; j++) {
3772
+ var srcDiff = srcLength - srcPos;
3773
+ var k = 0;
3774
+ var kEnd = (srcDiff > fullSrcDiff) ? width : srcDiff * 8 - 7;
3775
+ var kEndUnrolled = kEnd & ~7;
3776
+ var mask = 0;
3777
+ var srcByte = 0;
3778
+ for (; k < kEndUnrolled; k += 8) {
3779
+ srcByte = src[srcPos++];
3780
+ dest32[destPos++] = (srcByte & 128) ? white : black;
3781
+ dest32[destPos++] = (srcByte & 64) ? white : black;
3782
+ dest32[destPos++] = (srcByte & 32) ? white : black;
3783
+ dest32[destPos++] = (srcByte & 16) ? white : black;
3784
+ dest32[destPos++] = (srcByte & 8) ? white : black;
3785
+ dest32[destPos++] = (srcByte & 4) ? white : black;
3786
+ dest32[destPos++] = (srcByte & 2) ? white : black;
3787
+ dest32[destPos++] = (srcByte & 1) ? white : black;
3788
+ }
3789
+ for (; k < kEnd; k++) {
3790
+ if (mask === 0) {
3791
+ srcByte = src[srcPos++];
3792
+ mask = 128;
3793
+ }
3794
+
3795
+ dest32[destPos++] = (srcByte & mask) ? white : black;
3796
+ mask >>= 1;
3797
+ }
3798
+ }
3799
+ // We ran out of input. Make all remaining pixels transparent.
3800
+ while (destPos < dest32DataLength) {
3801
+ dest32[destPos++] = 0;
3802
+ }
3803
+
3804
+ ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT);
3805
+ }
3806
+ } else if (imgData.kind === ImageKind.RGBA_32BPP) {
3807
+ // RGBA, 32-bits per pixel.
3808
+
3809
+ j = 0;
3810
+ elemsInThisChunk = width * FULL_CHUNK_HEIGHT * 4;
3811
+ for (i = 0; i < fullChunks; i++) {
3812
+ dest.set(src.subarray(srcPos, srcPos + elemsInThisChunk));
3813
+ srcPos += elemsInThisChunk;
3814
+
3815
+ ctx.putImageData(chunkImgData, 0, j);
3816
+ j += FULL_CHUNK_HEIGHT;
3817
+ }
3818
+ if (i < totalChunks) {
3819
+ elemsInThisChunk = width * partialChunkHeight * 4;
3820
+ dest.set(src.subarray(srcPos, srcPos + elemsInThisChunk));
3821
+ ctx.putImageData(chunkImgData, 0, j);
3822
+ }
3823
+
3824
+ } else if (imgData.kind === ImageKind.RGB_24BPP) {
3825
+ // RGB, 24-bits per pixel.
3826
+ thisChunkHeight = FULL_CHUNK_HEIGHT;
3827
+ elemsInThisChunk = width * thisChunkHeight;
3828
+ for (i = 0; i < totalChunks; i++) {
3829
+ if (i >= fullChunks) {
3830
+ thisChunkHeight = partialChunkHeight;
3831
+ elemsInThisChunk = width * thisChunkHeight;
3832
+ }
3833
+
3834
+ destPos = 0;
3835
+ for (j = elemsInThisChunk; j--;) {
3836
+ dest[destPos++] = src[srcPos++];
3837
+ dest[destPos++] = src[srcPos++];
3838
+ dest[destPos++] = src[srcPos++];
3839
+ dest[destPos++] = 255;
3840
+ }
3841
+ ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT);
3842
+ }
3843
+ } else {
3844
+ error('bad image kind: ' + imgData.kind);
3845
+ }
3846
+ }
3847
+
3848
+ function putBinaryImageMask(ctx, imgData) {
3849
+ var height = imgData.height, width = imgData.width;
3850
+ var partialChunkHeight = height % FULL_CHUNK_HEIGHT;
3851
+ var fullChunks = (height - partialChunkHeight) / FULL_CHUNK_HEIGHT;
3852
+ var totalChunks = partialChunkHeight === 0 ? fullChunks : fullChunks + 1;
3853
+
3854
+ var chunkImgData = ctx.createImageData(width, FULL_CHUNK_HEIGHT);
3855
+ var srcPos = 0;
3856
+ var src = imgData.data;
3857
+ var dest = chunkImgData.data;
3858
+
3859
+ for (var i = 0; i < totalChunks; i++) {
3860
+ var thisChunkHeight =
3861
+ (i < fullChunks) ? FULL_CHUNK_HEIGHT : partialChunkHeight;
3862
+
3863
+ // Expand the mask so it can be used by the canvas. Any required
3864
+ // inversion has already been handled.
3865
+ var destPos = 3; // alpha component offset
3866
+ for (var j = 0; j < thisChunkHeight; j++) {
3867
+ var mask = 0;
3868
+ for (var k = 0; k < width; k++) {
3869
+ if (!mask) {
3870
+ var elem = src[srcPos++];
3871
+ mask = 128;
3872
+ }
3873
+ dest[destPos] = (elem & mask) ? 0 : 255;
3874
+ destPos += 4;
3875
+ mask >>= 1;
3876
+ }
3877
+ }
3878
+ ctx.putImageData(chunkImgData, 0, i * FULL_CHUNK_HEIGHT);
3879
+ }
3880
+ }
3881
+
3882
+ function copyCtxState(sourceCtx, destCtx) {
3883
+ var properties = ['strokeStyle', 'fillStyle', 'fillRule', 'globalAlpha',
3884
+ 'lineWidth', 'lineCap', 'lineJoin', 'miterLimit',
3885
+ 'globalCompositeOperation', 'font'];
3886
+ for (var i = 0, ii = properties.length; i < ii; i++) {
3887
+ var property = properties[i];
3888
+ if (sourceCtx[property] !== undefined) {
3889
+ destCtx[property] = sourceCtx[property];
3890
+ }
3891
+ }
3892
+ if (sourceCtx.setLineDash !== undefined) {
3893
+ destCtx.setLineDash(sourceCtx.getLineDash());
3894
+ destCtx.lineDashOffset = sourceCtx.lineDashOffset;
3895
+ } else if (sourceCtx.mozDashOffset !== undefined) {
3896
+ destCtx.mozDash = sourceCtx.mozDash;
3897
+ destCtx.mozDashOffset = sourceCtx.mozDashOffset;
3898
+ }
3899
+ }
3900
+
3901
+ function composeSMaskBackdrop(bytes, r0, g0, b0) {
3902
+ var length = bytes.length;
3903
+ for (var i = 3; i < length; i += 4) {
3904
+ var alpha = bytes[i];
3905
+ if (alpha === 0) {
3906
+ bytes[i - 3] = r0;
3907
+ bytes[i - 2] = g0;
3908
+ bytes[i - 1] = b0;
3909
+ } else if (alpha < 255) {
3910
+ var alpha_ = 255 - alpha;
3911
+ bytes[i - 3] = (bytes[i - 3] * alpha + r0 * alpha_) >> 8;
3912
+ bytes[i - 2] = (bytes[i - 2] * alpha + g0 * alpha_) >> 8;
3913
+ bytes[i - 1] = (bytes[i - 1] * alpha + b0 * alpha_) >> 8;
3914
+ }
3915
+ }
3916
+ }
3917
+
3918
+ function composeSMaskAlpha(maskData, layerData) {
3919
+ var length = maskData.length;
3920
+ var scale = 1 / 255;
3921
+ for (var i = 3; i < length; i += 4) {
3922
+ var alpha = maskData[i];
3923
+ layerData[i] = (layerData[i] * alpha * scale) | 0;
3924
+ }
3925
+ }
3926
+
3927
+ function composeSMaskLuminosity(maskData, layerData) {
3928
+ var length = maskData.length;
3929
+ for (var i = 3; i < length; i += 4) {
3930
+ var y = (maskData[i - 3] * 77) + // * 0.3 / 255 * 0x10000
3931
+ (maskData[i - 2] * 152) + // * 0.59 ....
3932
+ (maskData[i - 1] * 28); // * 0.11 ....
3933
+ layerData[i] = (layerData[i] * y) >> 16;
3934
+ }
3935
+ }
3936
+
3937
+ function genericComposeSMask(maskCtx, layerCtx, width, height,
3938
+ subtype, backdrop) {
3939
+ var hasBackdrop = !!backdrop;
3940
+ var r0 = hasBackdrop ? backdrop[0] : 0;
3941
+ var g0 = hasBackdrop ? backdrop[1] : 0;
3942
+ var b0 = hasBackdrop ? backdrop[2] : 0;
3943
+
3944
+ var composeFn;
3945
+ if (subtype === 'Luminosity') {
3946
+ composeFn = composeSMaskLuminosity;
3947
+ } else {
3948
+ composeFn = composeSMaskAlpha;
3949
+ }
3950
+
3951
+ // processing image in chunks to save memory
3952
+ var PIXELS_TO_PROCESS = 1048576;
3953
+ var chunkSize = Math.min(height, Math.ceil(PIXELS_TO_PROCESS / width));
3954
+ for (var row = 0; row < height; row += chunkSize) {
3955
+ var chunkHeight = Math.min(chunkSize, height - row);
3956
+ var maskData = maskCtx.getImageData(0, row, width, chunkHeight);
3957
+ var layerData = layerCtx.getImageData(0, row, width, chunkHeight);
3958
+
3959
+ if (hasBackdrop) {
3960
+ composeSMaskBackdrop(maskData.data, r0, g0, b0);
3961
+ }
3962
+ composeFn(maskData.data, layerData.data);
3963
+
3964
+ maskCtx.putImageData(layerData, 0, row);
3965
+ }
3966
+ }
3967
+
3968
+ function composeSMask(ctx, smask, layerCtx) {
3969
+ var mask = smask.canvas;
3970
+ var maskCtx = smask.context;
3971
+
3972
+ ctx.setTransform(smask.scaleX, 0, 0, smask.scaleY,
3973
+ smask.offsetX, smask.offsetY);
3974
+
3975
+ var backdrop = smask.backdrop || null;
3976
+ if (WebGLUtils.isEnabled) {
3977
+ var composed = WebGLUtils.composeSMask(layerCtx.canvas, mask,
3978
+ {subtype: smask.subtype, backdrop: backdrop});
3979
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
3980
+ ctx.drawImage(composed, smask.offsetX, smask.offsetY);
3981
+ return;
3982
+ }
3983
+ genericComposeSMask(maskCtx, layerCtx, mask.width, mask.height,
3984
+ smask.subtype, backdrop);
3985
+ ctx.drawImage(mask, 0, 0);
3986
+ }
3987
+
3988
+ var LINE_CAP_STYLES = ['butt', 'round', 'square'];
3989
+ var LINE_JOIN_STYLES = ['miter', 'round', 'bevel'];
3990
+ var NORMAL_CLIP = {};
3991
+ var EO_CLIP = {};
3992
+
3993
+ CanvasGraphics.prototype = {
3994
+
3995
+ beginDrawing: function CanvasGraphics_beginDrawing(viewport, transparency) {
3996
+ // For pdfs that use blend modes we have to clear the canvas else certain
3997
+ // blend modes can look wrong since we'd be blending with a white
3998
+ // backdrop. The problem with a transparent backdrop though is we then
3999
+ // don't get sub pixel anti aliasing on text, so we fill with white if
4000
+ // we can.
4001
+ var width = this.ctx.canvas.width;
4002
+ var height = this.ctx.canvas.height;
4003
+ if (transparency) {
4004
+ this.ctx.clearRect(0, 0, width, height);
4005
+ } else {
4006
+ this.ctx.mozOpaque = true;
4007
+ this.ctx.save();
4008
+ this.ctx.fillStyle = 'rgb(255, 255, 255)';
4009
+ this.ctx.fillRect(0, 0, width, height);
4010
+ this.ctx.restore();
4011
+ }
4012
+
4013
+ var transform = viewport.transform;
4014
+
4015
+ this.ctx.save();
4016
+ this.ctx.transform.apply(this.ctx, transform);
4017
+
4018
+ this.baseTransform = this.ctx.mozCurrentTransform.slice();
4019
+
4020
+ if (this.imageLayer) {
4021
+ this.imageLayer.beginLayout();
4022
+ }
4023
+ },
4024
+
4025
+ executeOperatorList: function CanvasGraphics_executeOperatorList(
4026
+ operatorList,
4027
+ executionStartIdx, continueCallback,
4028
+ stepper) {
4029
+ var argsArray = operatorList.argsArray;
4030
+ var fnArray = operatorList.fnArray;
4031
+ var i = executionStartIdx || 0;
4032
+ var argsArrayLen = argsArray.length;
4033
+
4034
+ // Sometimes the OperatorList to execute is empty.
4035
+ if (argsArrayLen === i) {
4036
+ return i;
4037
+ }
4038
+
4039
+ var chunkOperations = (argsArrayLen - i > EXECUTION_STEPS &&
4040
+ typeof continueCallback === 'function');
4041
+ var endTime = chunkOperations ? Date.now() + EXECUTION_TIME : 0;
4042
+ var steps = 0;
4043
+
4044
+ var commonObjs = this.commonObjs;
4045
+ var objs = this.objs;
4046
+ var fnId;
4047
+
4048
+ while (true) {
4049
+ if (stepper !== undefined && i === stepper.nextBreakPoint) {
4050
+ stepper.breakIt(i, continueCallback);
4051
+ return i;
4052
+ }
4053
+
4054
+ fnId = fnArray[i];
4055
+
4056
+ if (fnId !== OPS.dependency) {
4057
+ this[fnId].apply(this, argsArray[i]);
4058
+ } else {
4059
+ var deps = argsArray[i];
4060
+ for (var n = 0, nn = deps.length; n < nn; n++) {
4061
+ var depObjId = deps[n];
4062
+ var common = depObjId[0] === 'g' && depObjId[1] === '_';
4063
+ var objsPool = common ? commonObjs : objs;
4064
+
4065
+ // If the promise isn't resolved yet, add the continueCallback
4066
+ // to the promise and bail out.
4067
+ if (!objsPool.isResolved(depObjId)) {
4068
+ objsPool.get(depObjId, continueCallback);
4069
+ return i;
4070
+ }
4071
+ }
4072
+ }
4073
+
4074
+ i++;
4075
+
4076
+ // If the entire operatorList was executed, stop as were done.
4077
+ if (i === argsArrayLen) {
4078
+ return i;
4079
+ }
4080
+
4081
+ // If the execution took longer then a certain amount of time and
4082
+ // `continueCallback` is specified, interrupt the execution.
4083
+ if (chunkOperations && ++steps > EXECUTION_STEPS) {
4084
+ if (Date.now() > endTime) {
4085
+ continueCallback();
4086
+ return i;
4087
+ }
4088
+ steps = 0;
4089
+ }
4090
+
4091
+ // If the operatorList isn't executed completely yet OR the execution
4092
+ // time was short enough, do another execution round.
4093
+ }
4094
+ },
4095
+
4096
+ endDrawing: function CanvasGraphics_endDrawing() {
4097
+ this.ctx.restore();
4098
+ CachedCanvases.clear();
4099
+ WebGLUtils.clear();
4100
+
4101
+ if (this.imageLayer) {
4102
+ this.imageLayer.endLayout();
4103
+ }
4104
+ },
4105
+
4106
+ // Graphics state
4107
+ setLineWidth: function CanvasGraphics_setLineWidth(width) {
4108
+ this.current.lineWidth = width;
4109
+ this.ctx.lineWidth = width;
4110
+ },
4111
+ setLineCap: function CanvasGraphics_setLineCap(style) {
4112
+ this.ctx.lineCap = LINE_CAP_STYLES[style];
4113
+ },
4114
+ setLineJoin: function CanvasGraphics_setLineJoin(style) {
4115
+ this.ctx.lineJoin = LINE_JOIN_STYLES[style];
4116
+ },
4117
+ setMiterLimit: function CanvasGraphics_setMiterLimit(limit) {
4118
+ this.ctx.miterLimit = limit;
4119
+ },
4120
+ setDash: function CanvasGraphics_setDash(dashArray, dashPhase) {
4121
+ var ctx = this.ctx;
4122
+ if (ctx.setLineDash !== undefined) {
4123
+ ctx.setLineDash(dashArray);
4124
+ ctx.lineDashOffset = dashPhase;
4125
+ } else {
4126
+ ctx.mozDash = dashArray;
4127
+ ctx.mozDashOffset = dashPhase;
4128
+ }
4129
+ },
4130
+ setRenderingIntent: function CanvasGraphics_setRenderingIntent(intent) {
4131
+ // Maybe if we one day fully support color spaces this will be important
4132
+ // for now we can ignore.
4133
+ // TODO set rendering intent?
4134
+ },
4135
+ setFlatness: function CanvasGraphics_setFlatness(flatness) {
4136
+ // There's no way to control this with canvas, but we can safely ignore.
4137
+ // TODO set flatness?
4138
+ },
4139
+ setGState: function CanvasGraphics_setGState(states) {
4140
+ for (var i = 0, ii = states.length; i < ii; i++) {
4141
+ var state = states[i];
4142
+ var key = state[0];
4143
+ var value = state[1];
4144
+
4145
+ switch (key) {
4146
+ case 'LW':
4147
+ this.setLineWidth(value);
4148
+ break;
4149
+ case 'LC':
4150
+ this.setLineCap(value);
4151
+ break;
4152
+ case 'LJ':
4153
+ this.setLineJoin(value);
4154
+ break;
4155
+ case 'ML':
4156
+ this.setMiterLimit(value);
4157
+ break;
4158
+ case 'D':
4159
+ this.setDash(value[0], value[1]);
4160
+ break;
4161
+ case 'RI':
4162
+ this.setRenderingIntent(value);
4163
+ break;
4164
+ case 'FL':
4165
+ this.setFlatness(value);
4166
+ break;
4167
+ case 'Font':
4168
+ this.setFont(value[0], value[1]);
4169
+ break;
4170
+ case 'CA':
4171
+ this.current.strokeAlpha = state[1];
4172
+ break;
4173
+ case 'ca':
4174
+ this.current.fillAlpha = state[1];
4175
+ this.ctx.globalAlpha = state[1];
4176
+ break;
4177
+ case 'BM':
4178
+ if (value && value.name && (value.name !== 'Normal')) {
4179
+ var mode = value.name.replace(/([A-Z])/g,
4180
+ function(c) {
4181
+ return '-' + c.toLowerCase();
4182
+ }
4183
+ ).substring(1);
4184
+ this.ctx.globalCompositeOperation = mode;
4185
+ if (this.ctx.globalCompositeOperation !== mode) {
4186
+ warn('globalCompositeOperation "' + mode +
4187
+ '" is not supported');
4188
+ }
4189
+ } else {
4190
+ this.ctx.globalCompositeOperation = 'source-over';
4191
+ }
4192
+ break;
4193
+ case 'SMask':
4194
+ if (this.current.activeSMask) {
4195
+ this.endSMaskGroup();
4196
+ }
4197
+ this.current.activeSMask = value ? this.tempSMask : null;
4198
+ if (this.current.activeSMask) {
4199
+ this.beginSMaskGroup();
4200
+ }
4201
+ this.tempSMask = null;
4202
+ break;
4203
+ }
4204
+ }
4205
+ },
4206
+ beginSMaskGroup: function CanvasGraphics_beginSMaskGroup() {
4207
+
4208
+ var activeSMask = this.current.activeSMask;
4209
+ var drawnWidth = activeSMask.canvas.width;
4210
+ var drawnHeight = activeSMask.canvas.height;
4211
+ var cacheId = 'smaskGroupAt' + this.groupLevel;
4212
+ var scratchCanvas = CachedCanvases.getCanvas(
4213
+ cacheId, drawnWidth, drawnHeight, true);
4214
+
4215
+ var currentCtx = this.ctx;
4216
+ var currentTransform = currentCtx.mozCurrentTransform;
4217
+ this.ctx.save();
4218
+
4219
+ var groupCtx = scratchCanvas.context;
4220
+ groupCtx.scale(1 / activeSMask.scaleX, 1 / activeSMask.scaleY);
4221
+ groupCtx.translate(-activeSMask.offsetX, -activeSMask.offsetY);
4222
+ groupCtx.transform.apply(groupCtx, currentTransform);
4223
+
4224
+ copyCtxState(currentCtx, groupCtx);
4225
+ this.ctx = groupCtx;
4226
+ this.setGState([
4227
+ ['BM', 'Normal'],
4228
+ ['ca', 1],
4229
+ ['CA', 1]
4230
+ ]);
4231
+ this.groupStack.push(currentCtx);
4232
+ this.groupLevel++;
4233
+ },
4234
+ endSMaskGroup: function CanvasGraphics_endSMaskGroup() {
4235
+ var groupCtx = this.ctx;
4236
+ this.groupLevel--;
4237
+ this.ctx = this.groupStack.pop();
4238
+
4239
+ composeSMask(this.ctx, this.current.activeSMask, groupCtx);
4240
+ this.ctx.restore();
4241
+ },
4242
+ save: function CanvasGraphics_save() {
4243
+ this.ctx.save();
4244
+ var old = this.current;
4245
+ this.stateStack.push(old);
4246
+ this.current = old.clone();
4247
+ this.current.activeSMask = null;
4248
+ },
4249
+ restore: function CanvasGraphics_restore() {
4250
+ if (this.stateStack.length !== 0) {
4251
+ if (this.current.activeSMask !== null) {
4252
+ this.endSMaskGroup();
4253
+ }
4254
+
4255
+ this.current = this.stateStack.pop();
4256
+ this.ctx.restore();
4257
+
4258
+ this.cachedGetSinglePixelWidth = null;
4259
+ }
4260
+ },
4261
+ transform: function CanvasGraphics_transform(a, b, c, d, e, f) {
4262
+ this.ctx.transform(a, b, c, d, e, f);
4263
+
4264
+ this.cachedGetSinglePixelWidth = null;
4265
+ },
4266
+
4267
+ // Path
4268
+ constructPath: function CanvasGraphics_constructPath(ops, args) {
4269
+ var ctx = this.ctx;
4270
+ var current = this.current;
4271
+ var x = current.x, y = current.y;
4272
+ for (var i = 0, j = 0, ii = ops.length; i < ii; i++) {
4273
+ switch (ops[i] | 0) {
4274
+ case OPS.rectangle:
4275
+ x = args[j++];
4276
+ y = args[j++];
4277
+ var width = args[j++];
4278
+ var height = args[j++];
4279
+ if (width === 0) {
4280
+ width = this.getSinglePixelWidth();
4281
+ }
4282
+ if (height === 0) {
4283
+ height = this.getSinglePixelWidth();
4284
+ }
4285
+ var xw = x + width;
4286
+ var yh = y + height;
4287
+ this.ctx.moveTo(x, y);
4288
+ this.ctx.lineTo(xw, y);
4289
+ this.ctx.lineTo(xw, yh);
4290
+ this.ctx.lineTo(x, yh);
4291
+ this.ctx.lineTo(x, y);
4292
+ this.ctx.closePath();
4293
+ break;
4294
+ case OPS.moveTo:
4295
+ x = args[j++];
4296
+ y = args[j++];
4297
+ ctx.moveTo(x, y);
4298
+ break;
4299
+ case OPS.lineTo:
4300
+ x = args[j++];
4301
+ y = args[j++];
4302
+ ctx.lineTo(x, y);
4303
+ break;
4304
+ case OPS.curveTo:
4305
+ x = args[j + 4];
4306
+ y = args[j + 5];
4307
+ ctx.bezierCurveTo(args[j], args[j + 1], args[j + 2], args[j + 3],
4308
+ x, y);
4309
+ j += 6;
4310
+ break;
4311
+ case OPS.curveTo2:
4312
+ ctx.bezierCurveTo(x, y, args[j], args[j + 1],
4313
+ args[j + 2], args[j + 3]);
4314
+ x = args[j + 2];
4315
+ y = args[j + 3];
4316
+ j += 4;
4317
+ break;
4318
+ case OPS.curveTo3:
4319
+ x = args[j + 2];
4320
+ y = args[j + 3];
4321
+ ctx.bezierCurveTo(args[j], args[j + 1], x, y, x, y);
4322
+ j += 4;
4323
+ break;
4324
+ case OPS.closePath:
4325
+ ctx.closePath();
4326
+ break;
4327
+ }
4328
+ }
4329
+ current.setCurrentPoint(x, y);
4330
+ },
4331
+ closePath: function CanvasGraphics_closePath() {
4332
+ this.ctx.closePath();
4333
+ },
4334
+ stroke: function CanvasGraphics_stroke(consumePath) {
4335
+ consumePath = typeof consumePath !== 'undefined' ? consumePath : true;
4336
+ var ctx = this.ctx;
4337
+ var strokeColor = this.current.strokeColor;
4338
+ // Prevent drawing too thin lines by enforcing a minimum line width.
4339
+ ctx.lineWidth = Math.max(this.getSinglePixelWidth() * MIN_WIDTH_FACTOR,
4340
+ this.current.lineWidth);
4341
+ // For stroke we want to temporarily change the global alpha to the
4342
+ // stroking alpha.
4343
+ ctx.globalAlpha = this.current.strokeAlpha;
4344
+ if (strokeColor && strokeColor.hasOwnProperty('type') &&
4345
+ strokeColor.type === 'Pattern') {
4346
+ // for patterns, we transform to pattern space, calculate
4347
+ // the pattern, call stroke, and restore to user space
4348
+ ctx.save();
4349
+ ctx.strokeStyle = strokeColor.getPattern(ctx, this);
4350
+ ctx.stroke();
4351
+ ctx.restore();
4352
+ } else {
4353
+ ctx.stroke();
4354
+ }
4355
+ if (consumePath) {
4356
+ this.consumePath();
4357
+ }
4358
+ // Restore the global alpha to the fill alpha
4359
+ ctx.globalAlpha = this.current.fillAlpha;
4360
+ },
4361
+ closeStroke: function CanvasGraphics_closeStroke() {
4362
+ this.closePath();
4363
+ this.stroke();
4364
+ },
4365
+ fill: function CanvasGraphics_fill(consumePath) {
4366
+ consumePath = typeof consumePath !== 'undefined' ? consumePath : true;
4367
+ var ctx = this.ctx;
4368
+ var fillColor = this.current.fillColor;
4369
+ var isPatternFill = this.current.patternFill;
4370
+ var needRestore = false;
4371
+
4372
+ if (isPatternFill) {
4373
+ ctx.save();
4374
+ ctx.fillStyle = fillColor.getPattern(ctx, this);
4375
+ needRestore = true;
4376
+ }
4377
+
4378
+ if (this.pendingEOFill) {
4379
+ if (ctx.mozFillRule !== undefined) {
4380
+ ctx.mozFillRule = 'evenodd';
4381
+ ctx.fill();
4382
+ ctx.mozFillRule = 'nonzero';
4383
+ } else {
4384
+ try {
4385
+ ctx.fill('evenodd');
4386
+ } catch (ex) {
4387
+ // shouldn't really happen, but browsers might think differently
4388
+ ctx.fill();
4389
+ }
4390
+ }
4391
+ this.pendingEOFill = false;
4392
+ } else {
4393
+ ctx.fill();
4394
+ }
4395
+
4396
+ if (needRestore) {
4397
+ ctx.restore();
4398
+ }
4399
+ if (consumePath) {
4400
+ this.consumePath();
4401
+ }
4402
+ },
4403
+ eoFill: function CanvasGraphics_eoFill() {
4404
+ this.pendingEOFill = true;
4405
+ this.fill();
4406
+ },
4407
+ fillStroke: function CanvasGraphics_fillStroke() {
4408
+ this.fill(false);
4409
+ this.stroke(false);
4410
+
4411
+ this.consumePath();
4412
+ },
4413
+ eoFillStroke: function CanvasGraphics_eoFillStroke() {
4414
+ this.pendingEOFill = true;
4415
+ this.fillStroke();
4416
+ },
4417
+ closeFillStroke: function CanvasGraphics_closeFillStroke() {
4418
+ this.closePath();
4419
+ this.fillStroke();
4420
+ },
4421
+ closeEOFillStroke: function CanvasGraphics_closeEOFillStroke() {
4422
+ this.pendingEOFill = true;
4423
+ this.closePath();
4424
+ this.fillStroke();
4425
+ },
4426
+ endPath: function CanvasGraphics_endPath() {
4427
+ this.consumePath();
4428
+ },
4429
+
4430
+ // Clipping
4431
+ clip: function CanvasGraphics_clip() {
4432
+ this.pendingClip = NORMAL_CLIP;
4433
+ },
4434
+ eoClip: function CanvasGraphics_eoClip() {
4435
+ this.pendingClip = EO_CLIP;
4436
+ },
4437
+
4438
+ // Text
4439
+ beginText: function CanvasGraphics_beginText() {
4440
+ this.current.textMatrix = IDENTITY_MATRIX;
4441
+ this.current.textMatrixScale = 1;
4442
+ this.current.x = this.current.lineX = 0;
4443
+ this.current.y = this.current.lineY = 0;
4444
+ },
4445
+ endText: function CanvasGraphics_endText() {
4446
+ var paths = this.pendingTextPaths;
4447
+ var ctx = this.ctx;
4448
+ if (paths === undefined) {
4449
+ ctx.beginPath();
4450
+ return;
4451
+ }
4452
+
4453
+ ctx.save();
4454
+ ctx.beginPath();
4455
+ for (var i = 0; i < paths.length; i++) {
4456
+ var path = paths[i];
4457
+ ctx.setTransform.apply(ctx, path.transform);
4458
+ ctx.translate(path.x, path.y);
4459
+ path.addToPath(ctx, path.fontSize);
4460
+ }
4461
+ ctx.restore();
4462
+ ctx.clip();
4463
+ ctx.beginPath();
4464
+ delete this.pendingTextPaths;
4465
+ },
4466
+ setCharSpacing: function CanvasGraphics_setCharSpacing(spacing) {
4467
+ this.current.charSpacing = spacing;
4468
+ },
4469
+ setWordSpacing: function CanvasGraphics_setWordSpacing(spacing) {
4470
+ this.current.wordSpacing = spacing;
4471
+ },
4472
+ setHScale: function CanvasGraphics_setHScale(scale) {
4473
+ this.current.textHScale = scale / 100;
4474
+ },
4475
+ setLeading: function CanvasGraphics_setLeading(leading) {
4476
+ this.current.leading = -leading;
4477
+ },
4478
+ setFont: function CanvasGraphics_setFont(fontRefName, size) {
4479
+ var fontObj = this.commonObjs.get(fontRefName);
4480
+ var current = this.current;
4481
+
4482
+ if (!fontObj) {
4483
+ error('Can\'t find font for ' + fontRefName);
4484
+ }
4485
+
4486
+ current.fontMatrix = (fontObj.fontMatrix ?
4487
+ fontObj.fontMatrix : FONT_IDENTITY_MATRIX);
4488
+
4489
+ // A valid matrix needs all main diagonal elements to be non-zero
4490
+ // This also ensures we bypass FF bugzilla bug #719844.
4491
+ if (current.fontMatrix[0] === 0 ||
4492
+ current.fontMatrix[3] === 0) {
4493
+ warn('Invalid font matrix for font ' + fontRefName);
4494
+ }
4495
+
4496
+ // The spec for Tf (setFont) says that 'size' specifies the font 'scale',
4497
+ // and in some docs this can be negative (inverted x-y axes).
4498
+ if (size < 0) {
4499
+ size = -size;
4500
+ current.fontDirection = -1;
4501
+ } else {
4502
+ current.fontDirection = 1;
4503
+ }
4504
+
4505
+ this.current.font = fontObj;
4506
+ this.current.fontSize = size;
4507
+
4508
+ if (fontObj.isType3Font) {
4509
+ return; // we don't need ctx.font for Type3 fonts
4510
+ }
4511
+
4512
+ var name = fontObj.loadedName || 'sans-serif';
4513
+ var bold = fontObj.black ? (fontObj.bold ? 'bolder' : 'bold') :
4514
+ (fontObj.bold ? 'bold' : 'normal');
4515
+
4516
+ var italic = fontObj.italic ? 'italic' : 'normal';
4517
+ var typeface = '"' + name + '", ' + fontObj.fallbackName;
4518
+
4519
+ // Some font backends cannot handle fonts below certain size.
4520
+ // Keeping the font at minimal size and using the fontSizeScale to change
4521
+ // the current transformation matrix before the fillText/strokeText.
4522
+ // See https://bugzilla.mozilla.org/show_bug.cgi?id=726227
4523
+ var browserFontSize = size < MIN_FONT_SIZE ? MIN_FONT_SIZE :
4524
+ size > MAX_FONT_SIZE ? MAX_FONT_SIZE : size;
4525
+ this.current.fontSizeScale = size / browserFontSize;
4526
+
4527
+ var rule = italic + ' ' + bold + ' ' + browserFontSize + 'px ' + typeface;
4528
+ this.ctx.font = rule;
4529
+ },
4530
+ setTextRenderingMode: function CanvasGraphics_setTextRenderingMode(mode) {
4531
+ this.current.textRenderingMode = mode;
4532
+ },
4533
+ setTextRise: function CanvasGraphics_setTextRise(rise) {
4534
+ this.current.textRise = rise;
4535
+ },
4536
+ moveText: function CanvasGraphics_moveText(x, y) {
4537
+ this.current.x = this.current.lineX += x;
4538
+ this.current.y = this.current.lineY += y;
4539
+ },
4540
+ setLeadingMoveText: function CanvasGraphics_setLeadingMoveText(x, y) {
4541
+ this.setLeading(-y);
4542
+ this.moveText(x, y);
4543
+ },
4544
+ setTextMatrix: function CanvasGraphics_setTextMatrix(a, b, c, d, e, f) {
4545
+ this.current.textMatrix = [a, b, c, d, e, f];
4546
+ this.current.textMatrixScale = Math.sqrt(a * a + b * b);
4547
+
4548
+ this.current.x = this.current.lineX = 0;
4549
+ this.current.y = this.current.lineY = 0;
4550
+ },
4551
+ nextLine: function CanvasGraphics_nextLine() {
4552
+ this.moveText(0, this.current.leading);
4553
+ },
4554
+
4555
+ paintChar: function CanvasGraphics_paintChar(character, x, y) {
4556
+ var ctx = this.ctx;
4557
+ var current = this.current;
4558
+ var font = current.font;
4559
+ var textRenderingMode = current.textRenderingMode;
4560
+ var fontSize = current.fontSize / current.fontSizeScale;
4561
+ var fillStrokeMode = textRenderingMode &
4562
+ TextRenderingMode.FILL_STROKE_MASK;
4563
+ var isAddToPathSet = !!(textRenderingMode &
4564
+ TextRenderingMode.ADD_TO_PATH_FLAG);
4565
+
4566
+ var addToPath;
4567
+ if (font.disableFontFace || isAddToPathSet) {
4568
+ addToPath = font.getPathGenerator(this.commonObjs, character);
4569
+ }
4570
+
4571
+ if (font.disableFontFace) {
4572
+ ctx.save();
4573
+ ctx.translate(x, y);
4574
+ ctx.beginPath();
4575
+ addToPath(ctx, fontSize);
4576
+ if (fillStrokeMode === TextRenderingMode.FILL ||
4577
+ fillStrokeMode === TextRenderingMode.FILL_STROKE) {
4578
+ ctx.fill();
4579
+ }
4580
+ if (fillStrokeMode === TextRenderingMode.STROKE ||
4581
+ fillStrokeMode === TextRenderingMode.FILL_STROKE) {
4582
+ ctx.stroke();
4583
+ }
4584
+ ctx.restore();
4585
+ } else {
4586
+ if (fillStrokeMode === TextRenderingMode.FILL ||
4587
+ fillStrokeMode === TextRenderingMode.FILL_STROKE) {
4588
+ ctx.fillText(character, x, y);
4589
+ }
4590
+ if (fillStrokeMode === TextRenderingMode.STROKE ||
4591
+ fillStrokeMode === TextRenderingMode.FILL_STROKE) {
4592
+ ctx.strokeText(character, x, y);
4593
+ }
4594
+ }
4595
+
4596
+ if (isAddToPathSet) {
4597
+ var paths = this.pendingTextPaths || (this.pendingTextPaths = []);
4598
+ paths.push({
4599
+ transform: ctx.mozCurrentTransform,
4600
+ x: x,
4601
+ y: y,
4602
+ fontSize: fontSize,
4603
+ addToPath: addToPath
4604
+ });
4605
+ }
4606
+ },
4607
+
4608
+ get isFontSubpixelAAEnabled() {
4609
+ // Checks if anti-aliasing is enabled when scaled text is painted.
4610
+ // On Windows GDI scaled fonts looks bad.
4611
+ var ctx = document.createElement('canvas').getContext('2d');
4612
+ ctx.scale(1.5, 1);
4613
+ ctx.fillText('I', 0, 10);
4614
+ var data = ctx.getImageData(0, 0, 10, 10).data;
4615
+ var enabled = false;
4616
+ for (var i = 3; i < data.length; i += 4) {
4617
+ if (data[i] > 0 && data[i] < 255) {
4618
+ enabled = true;
4619
+ break;
4620
+ }
4621
+ }
4622
+ return shadow(this, 'isFontSubpixelAAEnabled', enabled);
4623
+ },
4624
+
4625
+ showText: function CanvasGraphics_showText(glyphs) {
4626
+ var current = this.current;
4627
+ var font = current.font;
4628
+ if (font.isType3Font) {
4629
+ return this.showType3Text(glyphs);
4630
+ }
4631
+
4632
+ var fontSize = current.fontSize;
4633
+ if (fontSize === 0) {
4634
+ return;
4635
+ }
4636
+
4637
+ var ctx = this.ctx;
4638
+ var fontSizeScale = current.fontSizeScale;
4639
+ var charSpacing = current.charSpacing;
4640
+ var wordSpacing = current.wordSpacing;
4641
+ var fontDirection = current.fontDirection;
4642
+ var textHScale = current.textHScale * fontDirection;
4643
+ var glyphsLength = glyphs.length;
4644
+ var vertical = font.vertical;
4645
+ var defaultVMetrics = font.defaultVMetrics;
4646
+ var widthAdvanceScale = fontSize * current.fontMatrix[0];
4647
+
4648
+ var simpleFillText =
4649
+ current.textRenderingMode === TextRenderingMode.FILL &&
4650
+ !font.disableFontFace;
4651
+
4652
+ ctx.save();
4653
+ ctx.transform.apply(ctx, current.textMatrix);
4654
+ ctx.translate(current.x, current.y + current.textRise);
4655
+
4656
+ if (fontDirection > 0) {
4657
+ ctx.scale(textHScale, -1);
4658
+ } else {
4659
+ ctx.scale(textHScale, 1);
4660
+ }
4661
+
4662
+ var lineWidth = current.lineWidth;
4663
+ var scale = current.textMatrixScale;
4664
+ if (scale === 0 || lineWidth === 0) {
4665
+ var fillStrokeMode = current.textRenderingMode &
4666
+ TextRenderingMode.FILL_STROKE_MASK;
4667
+ if (fillStrokeMode === TextRenderingMode.STROKE ||
4668
+ fillStrokeMode === TextRenderingMode.FILL_STROKE) {
4669
+ this.cachedGetSinglePixelWidth = null;
4670
+ lineWidth = this.getSinglePixelWidth() * MIN_WIDTH_FACTOR;
4671
+ }
4672
+ } else {
4673
+ lineWidth /= scale;
4674
+ }
4675
+
4676
+ if (fontSizeScale !== 1.0) {
4677
+ ctx.scale(fontSizeScale, fontSizeScale);
4678
+ lineWidth /= fontSizeScale;
4679
+ }
4680
+
4681
+ ctx.lineWidth = lineWidth;
4682
+
4683
+ var x = 0, i;
4684
+ for (i = 0; i < glyphsLength; ++i) {
4685
+ var glyph = glyphs[i];
4686
+ if (glyph === null) {
4687
+ // word break
4688
+ x += fontDirection * wordSpacing;
4689
+ continue;
4690
+ } else if (isNum(glyph)) {
4691
+ x += -glyph * fontSize * 0.001;
4692
+ continue;
4693
+ }
4694
+
4695
+ var restoreNeeded = false;
4696
+ var character = glyph.fontChar;
4697
+ var accent = glyph.accent;
4698
+ var scaledX, scaledY, scaledAccentX, scaledAccentY;
4699
+ var width = glyph.width;
4700
+ if (vertical) {
4701
+ var vmetric, vx, vy;
4702
+ vmetric = glyph.vmetric || defaultVMetrics;
4703
+ vx = glyph.vmetric ? vmetric[1] : width * 0.5;
4704
+ vx = -vx * widthAdvanceScale;
4705
+ vy = vmetric[2] * widthAdvanceScale;
4706
+
4707
+ width = vmetric ? -vmetric[0] : width;
4708
+ scaledX = vx / fontSizeScale;
4709
+ scaledY = (x + vy) / fontSizeScale;
4710
+ } else {
4711
+ scaledX = x / fontSizeScale;
4712
+ scaledY = 0;
4713
+ }
4714
+
4715
+ if (font.remeasure && width > 0 && this.isFontSubpixelAAEnabled) {
4716
+ // some standard fonts may not have the exact width, trying to
4717
+ // rescale per character
4718
+ var measuredWidth = ctx.measureText(character).width * 1000 /
4719
+ fontSize * fontSizeScale;
4720
+ var characterScaleX = width / measuredWidth;
4721
+ restoreNeeded = true;
4722
+ ctx.save();
4723
+ ctx.scale(characterScaleX, 1);
4724
+ scaledX /= characterScaleX;
4725
+ }
4726
+
4727
+ if (simpleFillText && !accent) {
4728
+ // common case
4729
+ ctx.fillText(character, scaledX, scaledY);
4730
+ } else {
4731
+ this.paintChar(character, scaledX, scaledY);
4732
+ if (accent) {
4733
+ scaledAccentX = scaledX + accent.offset.x / fontSizeScale;
4734
+ scaledAccentY = scaledY - accent.offset.y / fontSizeScale;
4735
+ this.paintChar(accent.fontChar, scaledAccentX, scaledAccentY);
4736
+ }
4737
+ }
4738
+
4739
+ var charWidth = width * widthAdvanceScale + charSpacing * fontDirection;
4740
+ x += charWidth;
4741
+
4742
+ if (restoreNeeded) {
4743
+ ctx.restore();
4744
+ }
4745
+ }
4746
+ if (vertical) {
4747
+ current.y -= x * textHScale;
4748
+ } else {
4749
+ current.x += x * textHScale;
4750
+ }
4751
+ ctx.restore();
4752
+ },
4753
+
4754
+ showType3Text: function CanvasGraphics_showType3Text(glyphs) {
4755
+ // Type3 fonts - each glyph is a "mini-PDF"
4756
+ var ctx = this.ctx;
4757
+ var current = this.current;
4758
+ var font = current.font;
4759
+ var fontSize = current.fontSize;
4760
+ var fontDirection = current.fontDirection;
4761
+ var charSpacing = current.charSpacing;
4762
+ var wordSpacing = current.wordSpacing;
4763
+ var textHScale = current.textHScale * fontDirection;
4764
+ var fontMatrix = current.fontMatrix || FONT_IDENTITY_MATRIX;
4765
+ var glyphsLength = glyphs.length;
4766
+ var isTextInvisible =
4767
+ current.textRenderingMode === TextRenderingMode.INVISIBLE;
4768
+ var i, glyph, width;
4769
+
4770
+ if (isTextInvisible || fontSize === 0) {
4771
+ return;
4772
+ }
4773
+
4774
+ ctx.save();
4775
+ ctx.transform.apply(ctx, current.textMatrix);
4776
+ ctx.translate(current.x, current.y);
4777
+
4778
+ ctx.scale(textHScale, fontDirection);
4779
+
4780
+ for (i = 0; i < glyphsLength; ++i) {
4781
+ glyph = glyphs[i];
4782
+ if (glyph === null) {
4783
+ // word break
4784
+ this.ctx.translate(wordSpacing, 0);
4785
+ current.x += wordSpacing * textHScale;
4786
+ continue;
4787
+ } else if (isNum(glyph)) {
4788
+ var spacingLength = -glyph * 0.001 * fontSize;
4789
+ this.ctx.translate(spacingLength, 0);
4790
+ current.x += spacingLength * textHScale;
4791
+ continue;
4792
+ }
4793
+
4794
+ var operatorList = font.charProcOperatorList[glyph.operatorListId];
4795
+ if (!operatorList) {
4796
+ warn('Type3 character \"' + glyph.operatorListId +
4797
+ '\" is not available');
4798
+ continue;
4799
+ }
4800
+ this.processingType3 = glyph;
4801
+ this.save();
4802
+ ctx.scale(fontSize, fontSize);
4803
+ ctx.transform.apply(ctx, fontMatrix);
4804
+ this.executeOperatorList(operatorList);
4805
+ this.restore();
4806
+
4807
+ var transformed = Util.applyTransform([glyph.width, 0], fontMatrix);
4808
+ width = transformed[0] * fontSize + charSpacing;
4809
+
4810
+ ctx.translate(width, 0);
4811
+ current.x += width * textHScale;
4812
+ }
4813
+ ctx.restore();
4814
+ this.processingType3 = null;
4815
+ },
4816
+
4817
+ // Type3 fonts
4818
+ setCharWidth: function CanvasGraphics_setCharWidth(xWidth, yWidth) {
4819
+ // We can safely ignore this since the width should be the same
4820
+ // as the width in the Widths array.
4821
+ },
4822
+ setCharWidthAndBounds: function CanvasGraphics_setCharWidthAndBounds(xWidth,
4823
+ yWidth,
4824
+ llx,
4825
+ lly,
4826
+ urx,
4827
+ ury) {
4828
+ // TODO According to the spec we're also suppose to ignore any operators
4829
+ // that set color or include images while processing this type3 font.
4830
+ this.ctx.rect(llx, lly, urx - llx, ury - lly);
4831
+ this.clip();
4832
+ this.endPath();
4833
+ },
4834
+
4835
+ // Color
4836
+ getColorN_Pattern: function CanvasGraphics_getColorN_Pattern(IR) {
4837
+ var pattern;
4838
+ if (IR[0] === 'TilingPattern') {
4839
+ var color = IR[1];
4840
+ pattern = new TilingPattern(IR, color, this.ctx, this.objs,
4841
+ this.commonObjs, this.baseTransform);
4842
+ } else {
4843
+ pattern = getShadingPatternFromIR(IR);
4844
+ }
4845
+ return pattern;
4846
+ },
4847
+ setStrokeColorN: function CanvasGraphics_setStrokeColorN(/*...*/) {
4848
+ this.current.strokeColor = this.getColorN_Pattern(arguments);
4849
+ },
4850
+ setFillColorN: function CanvasGraphics_setFillColorN(/*...*/) {
4851
+ this.current.fillColor = this.getColorN_Pattern(arguments);
4852
+ this.current.patternFill = true;
4853
+ },
4854
+ setStrokeRGBColor: function CanvasGraphics_setStrokeRGBColor(r, g, b) {
4855
+ var color = Util.makeCssRgb(r, g, b);
4856
+ this.ctx.strokeStyle = color;
4857
+ this.current.strokeColor = color;
4858
+ },
4859
+ setFillRGBColor: function CanvasGraphics_setFillRGBColor(r, g, b) {
4860
+ var color = Util.makeCssRgb(r, g, b);
4861
+ this.ctx.fillStyle = color;
4862
+ this.current.fillColor = color;
4863
+ this.current.patternFill = false;
4864
+ },
4865
+
4866
+ shadingFill: function CanvasGraphics_shadingFill(patternIR) {
4867
+ var ctx = this.ctx;
4868
+
4869
+ this.save();
4870
+ var pattern = getShadingPatternFromIR(patternIR);
4871
+ ctx.fillStyle = pattern.getPattern(ctx, this, true);
4872
+
4873
+ var inv = ctx.mozCurrentTransformInverse;
4874
+ if (inv) {
4875
+ var canvas = ctx.canvas;
4876
+ var width = canvas.width;
4877
+ var height = canvas.height;
4878
+
4879
+ var bl = Util.applyTransform([0, 0], inv);
4880
+ var br = Util.applyTransform([0, height], inv);
4881
+ var ul = Util.applyTransform([width, 0], inv);
4882
+ var ur = Util.applyTransform([width, height], inv);
4883
+
4884
+ var x0 = Math.min(bl[0], br[0], ul[0], ur[0]);
4885
+ var y0 = Math.min(bl[1], br[1], ul[1], ur[1]);
4886
+ var x1 = Math.max(bl[0], br[0], ul[0], ur[0]);
4887
+ var y1 = Math.max(bl[1], br[1], ul[1], ur[1]);
4888
+
4889
+ this.ctx.fillRect(x0, y0, x1 - x0, y1 - y0);
4890
+ } else {
4891
+ // HACK to draw the gradient onto an infinite rectangle.
4892
+ // PDF gradients are drawn across the entire image while
4893
+ // Canvas only allows gradients to be drawn in a rectangle
4894
+ // The following bug should allow us to remove this.
4895
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=664884
4896
+
4897
+ this.ctx.fillRect(-1e10, -1e10, 2e10, 2e10);
4898
+ }
4899
+
4900
+ this.restore();
4901
+ },
4902
+
4903
+ // Images
4904
+ beginInlineImage: function CanvasGraphics_beginInlineImage() {
4905
+ error('Should not call beginInlineImage');
4906
+ },
4907
+ beginImageData: function CanvasGraphics_beginImageData() {
4908
+ error('Should not call beginImageData');
4909
+ },
4910
+
4911
+ paintFormXObjectBegin: function CanvasGraphics_paintFormXObjectBegin(matrix,
4912
+ bbox) {
4913
+ this.save();
4914
+ this.baseTransformStack.push(this.baseTransform);
4915
+
4916
+ if (isArray(matrix) && 6 === matrix.length) {
4917
+ this.transform.apply(this, matrix);
4918
+ }
4919
+
4920
+ this.baseTransform = this.ctx.mozCurrentTransform;
4921
+
4922
+ if (isArray(bbox) && 4 === bbox.length) {
4923
+ var width = bbox[2] - bbox[0];
4924
+ var height = bbox[3] - bbox[1];
4925
+ this.ctx.rect(bbox[0], bbox[1], width, height);
4926
+ this.clip();
4927
+ this.endPath();
4928
+ }
4929
+ },
4930
+
4931
+ paintFormXObjectEnd: function CanvasGraphics_paintFormXObjectEnd() {
4932
+ this.restore();
4933
+ this.baseTransform = this.baseTransformStack.pop();
4934
+ },
4935
+
4936
+ beginGroup: function CanvasGraphics_beginGroup(group) {
4937
+ this.save();
4938
+ var currentCtx = this.ctx;
4939
+ // TODO non-isolated groups - according to Rik at adobe non-isolated
4940
+ // group results aren't usually that different and they even have tools
4941
+ // that ignore this setting. Notes from Rik on implmenting:
4942
+ // - When you encounter an transparency group, create a new canvas with
4943
+ // the dimensions of the bbox
4944
+ // - copy the content from the previous canvas to the new canvas
4945
+ // - draw as usual
4946
+ // - remove the backdrop alpha:
4947
+ // alphaNew = 1 - (1 - alpha)/(1 - alphaBackdrop) with 'alpha' the alpha
4948
+ // value of your transparency group and 'alphaBackdrop' the alpha of the
4949
+ // backdrop
4950
+ // - remove background color:
4951
+ // colorNew = color - alphaNew *colorBackdrop /(1 - alphaNew)
4952
+ if (!group.isolated) {
4953
+ info('TODO: Support non-isolated groups.');
4954
+ }
4955
+
4956
+ // TODO knockout - supposedly possible with the clever use of compositing
4957
+ // modes.
4958
+ if (group.knockout) {
4959
+ warn('Knockout groups not supported.');
4960
+ }
4961
+
4962
+ var currentTransform = currentCtx.mozCurrentTransform;
4963
+ if (group.matrix) {
4964
+ currentCtx.transform.apply(currentCtx, group.matrix);
4965
+ }
4966
+ assert(group.bbox, 'Bounding box is required.');
4967
+
4968
+ // Based on the current transform figure out how big the bounding box
4969
+ // will actually be.
4970
+ var bounds = Util.getAxialAlignedBoundingBox(
4971
+ group.bbox,
4972
+ currentCtx.mozCurrentTransform);
4973
+ // Clip the bounding box to the current canvas.
4974
+ var canvasBounds = [0,
4975
+ 0,
4976
+ currentCtx.canvas.width,
4977
+ currentCtx.canvas.height];
4978
+ bounds = Util.intersect(bounds, canvasBounds) || [0, 0, 0, 0];
4979
+ // Use ceil in case we're between sizes so we don't create canvas that is
4980
+ // too small and make the canvas at least 1x1 pixels.
4981
+ var offsetX = Math.floor(bounds[0]);
4982
+ var offsetY = Math.floor(bounds[1]);
4983
+ var drawnWidth = Math.max(Math.ceil(bounds[2]) - offsetX, 1);
4984
+ var drawnHeight = Math.max(Math.ceil(bounds[3]) - offsetY, 1);
4985
+ var scaleX = 1, scaleY = 1;
4986
+ if (drawnWidth > MAX_GROUP_SIZE) {
4987
+ scaleX = drawnWidth / MAX_GROUP_SIZE;
4988
+ drawnWidth = MAX_GROUP_SIZE;
4989
+ }
4990
+ if (drawnHeight > MAX_GROUP_SIZE) {
4991
+ scaleY = drawnHeight / MAX_GROUP_SIZE;
4992
+ drawnHeight = MAX_GROUP_SIZE;
4993
+ }
4994
+
4995
+ var cacheId = 'groupAt' + this.groupLevel;
4996
+ if (group.smask) {
4997
+ // Using two cache entries is case if masks are used one after another.
4998
+ cacheId += '_smask_' + ((this.smaskCounter++) % 2);
4999
+ }
5000
+ var scratchCanvas = CachedCanvases.getCanvas(
5001
+ cacheId, drawnWidth, drawnHeight, true);
5002
+ var groupCtx = scratchCanvas.context;
5003
+
5004
+ // Since we created a new canvas that is just the size of the bounding box
5005
+ // we have to translate the group ctx.
5006
+ groupCtx.scale(1 / scaleX, 1 / scaleY);
5007
+ groupCtx.translate(-offsetX, -offsetY);
5008
+ groupCtx.transform.apply(groupCtx, currentTransform);
5009
+
5010
+ if (group.smask) {
5011
+ // Saving state and cached mask to be used in setGState.
5012
+ this.smaskStack.push({
5013
+ canvas: scratchCanvas.canvas,
5014
+ context: groupCtx,
5015
+ offsetX: offsetX,
5016
+ offsetY: offsetY,
5017
+ scaleX: scaleX,
5018
+ scaleY: scaleY,
5019
+ subtype: group.smask.subtype,
5020
+ backdrop: group.smask.backdrop
5021
+ });
5022
+ } else {
5023
+ // Setup the current ctx so when the group is popped we draw it at the
5024
+ // right location.
5025
+ currentCtx.setTransform(1, 0, 0, 1, 0, 0);
5026
+ currentCtx.translate(offsetX, offsetY);
5027
+ currentCtx.scale(scaleX, scaleY);
5028
+ }
5029
+ // The transparency group inherits all off the current graphics state
5030
+ // except the blend mode, soft mask, and alpha constants.
5031
+ copyCtxState(currentCtx, groupCtx);
5032
+ this.ctx = groupCtx;
5033
+ this.setGState([
5034
+ ['BM', 'Normal'],
5035
+ ['ca', 1],
5036
+ ['CA', 1]
5037
+ ]);
5038
+ this.groupStack.push(currentCtx);
5039
+ this.groupLevel++;
5040
+ },
5041
+
5042
+ endGroup: function CanvasGraphics_endGroup(group) {
5043
+ this.groupLevel--;
5044
+ var groupCtx = this.ctx;
5045
+ this.ctx = this.groupStack.pop();
5046
+ // Turn off image smoothing to avoid sub pixel interpolation which can
5047
+ // look kind of blurry for some pdfs.
5048
+ if (this.ctx.imageSmoothingEnabled !== undefined) {
5049
+ this.ctx.imageSmoothingEnabled = false;
5050
+ } else {
5051
+ this.ctx.mozImageSmoothingEnabled = false;
5052
+ }
5053
+ if (group.smask) {
5054
+ this.tempSMask = this.smaskStack.pop();
5055
+ } else {
5056
+ this.ctx.drawImage(groupCtx.canvas, 0, 0);
5057
+ }
5058
+ this.restore();
5059
+ },
5060
+
5061
+ beginAnnotations: function CanvasGraphics_beginAnnotations() {
5062
+ this.save();
5063
+ this.current = new CanvasExtraState();
5064
+ },
5065
+
5066
+ endAnnotations: function CanvasGraphics_endAnnotations() {
5067
+ this.restore();
5068
+ },
5069
+
5070
+ beginAnnotation: function CanvasGraphics_beginAnnotation(rect, transform,
5071
+ matrix) {
5072
+ this.save();
5073
+
5074
+ if (isArray(rect) && 4 === rect.length) {
5075
+ var width = rect[2] - rect[0];
5076
+ var height = rect[3] - rect[1];
5077
+ this.ctx.rect(rect[0], rect[1], width, height);
5078
+ this.clip();
5079
+ this.endPath();
5080
+ }
5081
+
5082
+ this.transform.apply(this, transform);
5083
+ this.transform.apply(this, matrix);
5084
+ },
5085
+
5086
+ endAnnotation: function CanvasGraphics_endAnnotation() {
5087
+ this.restore();
5088
+ },
5089
+
5090
+ paintJpegXObject: function CanvasGraphics_paintJpegXObject(objId, w, h) {
5091
+ var domImage = this.objs.get(objId);
5092
+ if (!domImage) {
5093
+ warn('Dependent image isn\'t ready yet');
5094
+ return;
5095
+ }
5096
+
5097
+ this.save();
5098
+
5099
+ var ctx = this.ctx;
5100
+ // scale the image to the unit square
5101
+ ctx.scale(1 / w, -1 / h);
5102
+
5103
+ ctx.drawImage(domImage, 0, 0, domImage.width, domImage.height,
5104
+ 0, -h, w, h);
5105
+ if (this.imageLayer) {
5106
+ var currentTransform = ctx.mozCurrentTransformInverse;
5107
+ var position = this.getCanvasPosition(0, 0);
5108
+ this.imageLayer.appendImage({
5109
+ objId: objId,
5110
+ left: position[0],
5111
+ top: position[1],
5112
+ width: w / currentTransform[0],
5113
+ height: h / currentTransform[3]
5114
+ });
5115
+ }
5116
+ this.restore();
5117
+ },
5118
+
5119
+ paintImageMaskXObject: function CanvasGraphics_paintImageMaskXObject(img) {
5120
+ var ctx = this.ctx;
5121
+ var width = img.width, height = img.height;
5122
+ var fillColor = this.current.fillColor;
5123
+ var isPatternFill = this.current.patternFill;
5124
+
5125
+ var glyph = this.processingType3;
5126
+
5127
+ if (COMPILE_TYPE3_GLYPHS && glyph && glyph.compiled === undefined) {
5128
+ if (width <= MAX_SIZE_TO_COMPILE && height <= MAX_SIZE_TO_COMPILE) {
5129
+ glyph.compiled =
5130
+ compileType3Glyph({data: img.data, width: width, height: height});
5131
+ } else {
5132
+ glyph.compiled = null;
5133
+ }
5134
+ }
5135
+
5136
+ if (glyph && glyph.compiled) {
5137
+ glyph.compiled(ctx);
5138
+ return;
5139
+ }
5140
+
5141
+ var maskCanvas = CachedCanvases.getCanvas('maskCanvas', width, height);
5142
+ var maskCtx = maskCanvas.context;
5143
+ maskCtx.save();
5144
+
5145
+ putBinaryImageMask(maskCtx, img);
5146
+
5147
+ maskCtx.globalCompositeOperation = 'source-in';
5148
+
5149
+ maskCtx.fillStyle = isPatternFill ?
5150
+ fillColor.getPattern(maskCtx, this) : fillColor;
5151
+ maskCtx.fillRect(0, 0, width, height);
5152
+
5153
+ maskCtx.restore();
5154
+
5155
+ this.paintInlineImageXObject(maskCanvas.canvas);
5156
+ },
5157
+
5158
+ paintImageMaskXObjectRepeat:
5159
+ function CanvasGraphics_paintImageMaskXObjectRepeat(imgData, scaleX,
5160
+ scaleY, positions) {
5161
+ var width = imgData.width;
5162
+ var height = imgData.height;
5163
+ var fillColor = this.current.fillColor;
5164
+ var isPatternFill = this.current.patternFill;
5165
+
5166
+ var maskCanvas = CachedCanvases.getCanvas('maskCanvas', width, height);
5167
+ var maskCtx = maskCanvas.context;
5168
+ maskCtx.save();
5169
+
5170
+ putBinaryImageMask(maskCtx, imgData);
5171
+
5172
+ maskCtx.globalCompositeOperation = 'source-in';
5173
+
5174
+ maskCtx.fillStyle = isPatternFill ?
5175
+ fillColor.getPattern(maskCtx, this) : fillColor;
5176
+ maskCtx.fillRect(0, 0, width, height);
5177
+
5178
+ maskCtx.restore();
5179
+
5180
+ var ctx = this.ctx;
5181
+ for (var i = 0, ii = positions.length; i < ii; i += 2) {
5182
+ ctx.save();
5183
+ ctx.transform(scaleX, 0, 0, scaleY, positions[i], positions[i + 1]);
5184
+ ctx.scale(1, -1);
5185
+ ctx.drawImage(maskCanvas.canvas, 0, 0, width, height,
5186
+ 0, -1, 1, 1);
5187
+ ctx.restore();
5188
+ }
5189
+ },
5190
+
5191
+ paintImageMaskXObjectGroup:
5192
+ function CanvasGraphics_paintImageMaskXObjectGroup(images) {
5193
+ var ctx = this.ctx;
5194
+
5195
+ var fillColor = this.current.fillColor;
5196
+ var isPatternFill = this.current.patternFill;
5197
+ for (var i = 0, ii = images.length; i < ii; i++) {
5198
+ var image = images[i];
5199
+ var width = image.width, height = image.height;
5200
+
5201
+ var maskCanvas = CachedCanvases.getCanvas('maskCanvas', width, height);
5202
+ var maskCtx = maskCanvas.context;
5203
+ maskCtx.save();
5204
+
5205
+ putBinaryImageMask(maskCtx, image);
5206
+
5207
+ maskCtx.globalCompositeOperation = 'source-in';
5208
+
5209
+ maskCtx.fillStyle = isPatternFill ?
5210
+ fillColor.getPattern(maskCtx, this) : fillColor;
5211
+ maskCtx.fillRect(0, 0, width, height);
5212
+
5213
+ maskCtx.restore();
5214
+
5215
+ ctx.save();
5216
+ ctx.transform.apply(ctx, image.transform);
5217
+ ctx.scale(1, -1);
5218
+ ctx.drawImage(maskCanvas.canvas, 0, 0, width, height,
5219
+ 0, -1, 1, 1);
5220
+ ctx.restore();
5221
+ }
5222
+ },
5223
+
5224
+ paintImageXObject: function CanvasGraphics_paintImageXObject(objId) {
5225
+ var imgData = this.objs.get(objId);
5226
+ if (!imgData) {
5227
+ warn('Dependent image isn\'t ready yet');
5228
+ return;
5229
+ }
5230
+
5231
+ this.paintInlineImageXObject(imgData);
5232
+ },
5233
+
5234
+ paintImageXObjectRepeat:
5235
+ function CanvasGraphics_paintImageXObjectRepeat(objId, scaleX, scaleY,
5236
+ positions) {
5237
+ var imgData = this.objs.get(objId);
5238
+ if (!imgData) {
5239
+ warn('Dependent image isn\'t ready yet');
5240
+ return;
5241
+ }
5242
+
5243
+ var width = imgData.width;
5244
+ var height = imgData.height;
5245
+ var map = [];
5246
+ for (var i = 0, ii = positions.length; i < ii; i += 2) {
5247
+ map.push({transform: [scaleX, 0, 0, scaleY, positions[i],
5248
+ positions[i + 1]], x: 0, y: 0, w: width, h: height});
5249
+ }
5250
+ this.paintInlineImageXObjectGroup(imgData, map);
5251
+ },
5252
+
5253
+ paintInlineImageXObject:
5254
+ function CanvasGraphics_paintInlineImageXObject(imgData) {
5255
+ var width = imgData.width;
5256
+ var height = imgData.height;
5257
+ var ctx = this.ctx;
5258
+
5259
+ this.save();
5260
+ // scale the image to the unit square
5261
+ ctx.scale(1 / width, -1 / height);
5262
+
5263
+ var currentTransform = ctx.mozCurrentTransformInverse;
5264
+ var a = currentTransform[0], b = currentTransform[1];
5265
+ var widthScale = Math.max(Math.sqrt(a * a + b * b), 1);
5266
+ var c = currentTransform[2], d = currentTransform[3];
5267
+ var heightScale = Math.max(Math.sqrt(c * c + d * d), 1);
5268
+
5269
+ var imgToPaint, tmpCanvas;
5270
+ // instanceof HTMLElement does not work in jsdom node.js module
5271
+ if (imgData instanceof HTMLElement || !imgData.data) {
5272
+ imgToPaint = imgData;
5273
+ } else {
5274
+ tmpCanvas = CachedCanvases.getCanvas('inlineImage', width, height);
5275
+ var tmpCtx = tmpCanvas.context;
5276
+ putBinaryImageData(tmpCtx, imgData);
5277
+ imgToPaint = tmpCanvas.canvas;
5278
+ }
5279
+
5280
+ var paintWidth = width, paintHeight = height;
5281
+ var tmpCanvasId = 'prescale1';
5282
+ // Vertial or horizontal scaling shall not be more than 2 to not loose the
5283
+ // pixels during drawImage operation, painting on the temporary canvas(es)
5284
+ // that are twice smaller in size
5285
+ while ((widthScale > 2 && paintWidth > 1) ||
5286
+ (heightScale > 2 && paintHeight > 1)) {
5287
+ var newWidth = paintWidth, newHeight = paintHeight;
5288
+ if (widthScale > 2 && paintWidth > 1) {
5289
+ newWidth = Math.ceil(paintWidth / 2);
5290
+ widthScale /= paintWidth / newWidth;
5291
+ }
5292
+ if (heightScale > 2 && paintHeight > 1) {
5293
+ newHeight = Math.ceil(paintHeight / 2);
5294
+ heightScale /= paintHeight / newHeight;
5295
+ }
5296
+ tmpCanvas = CachedCanvases.getCanvas(tmpCanvasId, newWidth, newHeight);
5297
+ tmpCtx = tmpCanvas.context;
5298
+ tmpCtx.clearRect(0, 0, newWidth, newHeight);
5299
+ tmpCtx.drawImage(imgToPaint, 0, 0, paintWidth, paintHeight,
5300
+ 0, 0, newWidth, newHeight);
5301
+ imgToPaint = tmpCanvas.canvas;
5302
+ paintWidth = newWidth;
5303
+ paintHeight = newHeight;
5304
+ tmpCanvasId = tmpCanvasId === 'prescale1' ? 'prescale2' : 'prescale1';
5305
+ }
5306
+ ctx.drawImage(imgToPaint, 0, 0, paintWidth, paintHeight,
5307
+ 0, -height, width, height);
5308
+
5309
+ if (this.imageLayer) {
5310
+ var position = this.getCanvasPosition(0, -height);
5311
+ this.imageLayer.appendImage({
5312
+ imgData: imgData,
5313
+ left: position[0],
5314
+ top: position[1],
5315
+ width: width / currentTransform[0],
5316
+ height: height / currentTransform[3]
5317
+ });
5318
+ }
5319
+ this.restore();
5320
+ },
5321
+
5322
+ paintInlineImageXObjectGroup:
5323
+ function CanvasGraphics_paintInlineImageXObjectGroup(imgData, map) {
5324
+ var ctx = this.ctx;
5325
+ var w = imgData.width;
5326
+ var h = imgData.height;
5327
+
5328
+ var tmpCanvas = CachedCanvases.getCanvas('inlineImage', w, h);
5329
+ var tmpCtx = tmpCanvas.context;
5330
+ putBinaryImageData(tmpCtx, imgData);
5331
+
5332
+ for (var i = 0, ii = map.length; i < ii; i++) {
5333
+ var entry = map[i];
5334
+ ctx.save();
5335
+ ctx.transform.apply(ctx, entry.transform);
5336
+ ctx.scale(1, -1);
5337
+ ctx.drawImage(tmpCanvas.canvas, entry.x, entry.y, entry.w, entry.h,
5338
+ 0, -1, 1, 1);
5339
+ if (this.imageLayer) {
5340
+ var position = this.getCanvasPosition(entry.x, entry.y);
5341
+ this.imageLayer.appendImage({
5342
+ imgData: imgData,
5343
+ left: position[0],
5344
+ top: position[1],
5345
+ width: w,
5346
+ height: h
5347
+ });
5348
+ }
5349
+ ctx.restore();
5350
+ }
5351
+ },
5352
+
5353
+ paintSolidColorImageMask:
5354
+ function CanvasGraphics_paintSolidColorImageMask() {
5355
+ this.ctx.fillRect(0, 0, 1, 1);
5356
+ },
5357
+
5358
+ // Marked content
5359
+
5360
+ markPoint: function CanvasGraphics_markPoint(tag) {
5361
+ // TODO Marked content.
5362
+ },
5363
+ markPointProps: function CanvasGraphics_markPointProps(tag, properties) {
5364
+ // TODO Marked content.
5365
+ },
5366
+ beginMarkedContent: function CanvasGraphics_beginMarkedContent(tag) {
5367
+ // TODO Marked content.
5368
+ },
5369
+ beginMarkedContentProps: function CanvasGraphics_beginMarkedContentProps(
5370
+ tag, properties) {
5371
+ // TODO Marked content.
5372
+ },
5373
+ endMarkedContent: function CanvasGraphics_endMarkedContent() {
5374
+ // TODO Marked content.
5375
+ },
5376
+
5377
+ // Compatibility
5378
+
5379
+ beginCompat: function CanvasGraphics_beginCompat() {
5380
+ // TODO ignore undefined operators (should we do that anyway?)
5381
+ },
5382
+ endCompat: function CanvasGraphics_endCompat() {
5383
+ // TODO stop ignoring undefined operators
5384
+ },
5385
+
5386
+ // Helper functions
5387
+
5388
+ consumePath: function CanvasGraphics_consumePath() {
5389
+ var ctx = this.ctx;
5390
+ if (this.pendingClip) {
5391
+ if (this.pendingClip === EO_CLIP) {
5392
+ if (ctx.mozFillRule !== undefined) {
5393
+ ctx.mozFillRule = 'evenodd';
5394
+ ctx.clip();
5395
+ ctx.mozFillRule = 'nonzero';
5396
+ } else {
5397
+ try {
5398
+ ctx.clip('evenodd');
5399
+ } catch (ex) {
5400
+ // shouldn't really happen, but browsers might think differently
5401
+ ctx.clip();
5402
+ }
5403
+ }
5404
+ } else {
5405
+ ctx.clip();
5406
+ }
5407
+ this.pendingClip = null;
5408
+ }
5409
+ ctx.beginPath();
5410
+ },
5411
+ getSinglePixelWidth: function CanvasGraphics_getSinglePixelWidth(scale) {
5412
+ if (this.cachedGetSinglePixelWidth === null) {
5413
+ var inverse = this.ctx.mozCurrentTransformInverse;
5414
+ // max of the current horizontal and vertical scale
5415
+ this.cachedGetSinglePixelWidth = Math.sqrt(Math.max(
5416
+ (inverse[0] * inverse[0] + inverse[1] * inverse[1]),
5417
+ (inverse[2] * inverse[2] + inverse[3] * inverse[3])));
5418
+ }
5419
+ return this.cachedGetSinglePixelWidth;
5420
+ },
5421
+ getCanvasPosition: function CanvasGraphics_getCanvasPosition(x, y) {
5422
+ var transform = this.ctx.mozCurrentTransform;
5423
+ return [
5424
+ transform[0] * x + transform[2] * y + transform[4],
5425
+ transform[1] * x + transform[3] * y + transform[5]
5426
+ ];
5427
+ }
5428
+ };
5429
+
5430
+ for (var op in OPS) {
5431
+ CanvasGraphics.prototype[OPS[op]] = CanvasGraphics.prototype[op];
5432
+ }
5433
+
5434
+ return CanvasGraphics;
5435
+ })();
5436
+
5437
+
5438
+ var WebGLUtils = (function WebGLUtilsClosure() {
5439
+ function loadShader(gl, code, shaderType) {
5440
+ var shader = gl.createShader(shaderType);
5441
+ gl.shaderSource(shader, code);
5442
+ gl.compileShader(shader);
5443
+ var compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
5444
+ if (!compiled) {
5445
+ var errorMsg = gl.getShaderInfoLog(shader);
5446
+ throw new Error('Error during shader compilation: ' + errorMsg);
5447
+ }
5448
+ return shader;
5449
+ }
5450
+ function createVertexShader(gl, code) {
5451
+ return loadShader(gl, code, gl.VERTEX_SHADER);
5452
+ }
5453
+ function createFragmentShader(gl, code) {
5454
+ return loadShader(gl, code, gl.FRAGMENT_SHADER);
5455
+ }
5456
+ function createProgram(gl, shaders) {
5457
+ var program = gl.createProgram();
5458
+ for (var i = 0, ii = shaders.length; i < ii; ++i) {
5459
+ gl.attachShader(program, shaders[i]);
5460
+ }
5461
+ gl.linkProgram(program);
5462
+ var linked = gl.getProgramParameter(program, gl.LINK_STATUS);
5463
+ if (!linked) {
5464
+ var errorMsg = gl.getProgramInfoLog(program);
5465
+ throw new Error('Error during program linking: ' + errorMsg);
5466
+ }
5467
+ return program;
5468
+ }
5469
+ function createTexture(gl, image, textureId) {
5470
+ gl.activeTexture(textureId);
5471
+ var texture = gl.createTexture();
5472
+ gl.bindTexture(gl.TEXTURE_2D, texture);
5473
+
5474
+ // Set the parameters so we can render any size image.
5475
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
5476
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
5477
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
5478
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
5479
+
5480
+ // Upload the image into the texture.
5481
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
5482
+ return texture;
5483
+ }
5484
+
5485
+ var currentGL, currentCanvas;
5486
+ function generateGL() {
5487
+ if (currentGL) {
5488
+ return;
5489
+ }
5490
+ currentCanvas = document.createElement('canvas');
5491
+ currentGL = currentCanvas.getContext('webgl',
5492
+ { premultipliedalpha: false });
5493
+ }
5494
+
5495
+ var smaskVertexShaderCode = '\
5496
+ attribute vec2 a_position; \
5497
+ attribute vec2 a_texCoord; \
5498
+ \
5499
+ uniform vec2 u_resolution; \
5500
+ \
5501
+ varying vec2 v_texCoord; \
5502
+ \
5503
+ void main() { \
5504
+ vec2 clipSpace = (a_position / u_resolution) * 2.0 - 1.0; \
5505
+ gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1); \
5506
+ \
5507
+ v_texCoord = a_texCoord; \
5508
+ } ';
5509
+
5510
+ var smaskFragmentShaderCode = '\
5511
+ precision mediump float; \
5512
+ \
5513
+ uniform vec4 u_backdrop; \
5514
+ uniform int u_subtype; \
5515
+ uniform sampler2D u_image; \
5516
+ uniform sampler2D u_mask; \
5517
+ \
5518
+ varying vec2 v_texCoord; \
5519
+ \
5520
+ void main() { \
5521
+ vec4 imageColor = texture2D(u_image, v_texCoord); \
5522
+ vec4 maskColor = texture2D(u_mask, v_texCoord); \
5523
+ if (u_backdrop.a > 0.0) { \
5524
+ maskColor.rgb = maskColor.rgb * maskColor.a + \
5525
+ u_backdrop.rgb * (1.0 - maskColor.a); \
5526
+ } \
5527
+ float lum; \
5528
+ if (u_subtype == 0) { \
5529
+ lum = maskColor.a; \
5530
+ } else { \
5531
+ lum = maskColor.r * 0.3 + maskColor.g * 0.59 + \
5532
+ maskColor.b * 0.11; \
5533
+ } \
5534
+ imageColor.a *= lum; \
5535
+ imageColor.rgb *= imageColor.a; \
5536
+ gl_FragColor = imageColor; \
5537
+ } ';
5538
+
5539
+ var smaskCache = null;
5540
+
5541
+ function initSmaskGL() {
5542
+ var canvas, gl;
5543
+
5544
+ generateGL();
5545
+ canvas = currentCanvas;
5546
+ currentCanvas = null;
5547
+ gl = currentGL;
5548
+ currentGL = null;
5549
+
5550
+ // setup a GLSL program
5551
+ var vertexShader = createVertexShader(gl, smaskVertexShaderCode);
5552
+ var fragmentShader = createFragmentShader(gl, smaskFragmentShaderCode);
5553
+ var program = createProgram(gl, [vertexShader, fragmentShader]);
5554
+ gl.useProgram(program);
5555
+
5556
+ var cache = {};
5557
+ cache.gl = gl;
5558
+ cache.canvas = canvas;
5559
+ cache.resolutionLocation = gl.getUniformLocation(program, 'u_resolution');
5560
+ cache.positionLocation = gl.getAttribLocation(program, 'a_position');
5561
+ cache.backdropLocation = gl.getUniformLocation(program, 'u_backdrop');
5562
+ cache.subtypeLocation = gl.getUniformLocation(program, 'u_subtype');
5563
+
5564
+ var texCoordLocation = gl.getAttribLocation(program, 'a_texCoord');
5565
+ var texLayerLocation = gl.getUniformLocation(program, 'u_image');
5566
+ var texMaskLocation = gl.getUniformLocation(program, 'u_mask');
5567
+
5568
+ // provide texture coordinates for the rectangle.
5569
+ var texCoordBuffer = gl.createBuffer();
5570
+ gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
5571
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
5572
+ 0.0, 0.0,
5573
+ 1.0, 0.0,
5574
+ 0.0, 1.0,
5575
+ 0.0, 1.0,
5576
+ 1.0, 0.0,
5577
+ 1.0, 1.0]), gl.STATIC_DRAW);
5578
+ gl.enableVertexAttribArray(texCoordLocation);
5579
+ gl.vertexAttribPointer(texCoordLocation, 2, gl.FLOAT, false, 0, 0);
5580
+
5581
+ gl.uniform1i(texLayerLocation, 0);
5582
+ gl.uniform1i(texMaskLocation, 1);
5583
+
5584
+ smaskCache = cache;
5585
+ }
5586
+
5587
+ function composeSMask(layer, mask, properties) {
5588
+ var width = layer.width, height = layer.height;
5589
+
5590
+ if (!smaskCache) {
5591
+ initSmaskGL();
5592
+ }
5593
+ var cache = smaskCache,canvas = cache.canvas, gl = cache.gl;
5594
+ canvas.width = width;
5595
+ canvas.height = height;
5596
+ gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
5597
+ gl.uniform2f(cache.resolutionLocation, width, height);
5598
+
5599
+ if (properties.backdrop) {
5600
+ gl.uniform4f(cache.resolutionLocation, properties.backdrop[0],
5601
+ properties.backdrop[1], properties.backdrop[2], 1);
5602
+ } else {
5603
+ gl.uniform4f(cache.resolutionLocation, 0, 0, 0, 0);
5604
+ }
5605
+ gl.uniform1i(cache.subtypeLocation,
5606
+ properties.subtype === 'Luminosity' ? 1 : 0);
5607
+
5608
+ // Create a textures
5609
+ var texture = createTexture(gl, layer, gl.TEXTURE0);
5610
+ var maskTexture = createTexture(gl, mask, gl.TEXTURE1);
5611
+
5612
+
5613
+ // Create a buffer and put a single clipspace rectangle in
5614
+ // it (2 triangles)
5615
+ var buffer = gl.createBuffer();
5616
+ gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
5617
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
5618
+ 0, 0,
5619
+ width, 0,
5620
+ 0, height,
5621
+ 0, height,
5622
+ width, 0,
5623
+ width, height]), gl.STATIC_DRAW);
5624
+ gl.enableVertexAttribArray(cache.positionLocation);
5625
+ gl.vertexAttribPointer(cache.positionLocation, 2, gl.FLOAT, false, 0, 0);
5626
+
5627
+ // draw
5628
+ gl.clearColor(0, 0, 0, 0);
5629
+ gl.enable(gl.BLEND);
5630
+ gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
5631
+ gl.clear(gl.COLOR_BUFFER_BIT);
5632
+
5633
+ gl.drawArrays(gl.TRIANGLES, 0, 6);
5634
+
5635
+ gl.flush();
5636
+
5637
+ gl.deleteTexture(texture);
5638
+ gl.deleteTexture(maskTexture);
5639
+ gl.deleteBuffer(buffer);
5640
+
5641
+ return canvas;
5642
+ }
5643
+
5644
+ var figuresVertexShaderCode = '\
5645
+ attribute vec2 a_position; \
5646
+ attribute vec3 a_color; \
5647
+ \
5648
+ uniform vec2 u_resolution; \
5649
+ uniform vec2 u_scale; \
5650
+ uniform vec2 u_offset; \
5651
+ \
5652
+ varying vec4 v_color; \
5653
+ \
5654
+ void main() { \
5655
+ vec2 position = (a_position + u_offset) * u_scale; \
5656
+ vec2 clipSpace = (position / u_resolution) * 2.0 - 1.0; \
5657
+ gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1); \
5658
+ \
5659
+ v_color = vec4(a_color / 255.0, 1.0); \
5660
+ } ';
5661
+
5662
+ var figuresFragmentShaderCode = '\
5663
+ precision mediump float; \
5664
+ \
5665
+ varying vec4 v_color; \
5666
+ \
5667
+ void main() { \
5668
+ gl_FragColor = v_color; \
5669
+ } ';
5670
+
5671
+ var figuresCache = null;
5672
+
5673
+ function initFiguresGL() {
5674
+ var canvas, gl;
5675
+
5676
+ generateGL();
5677
+ canvas = currentCanvas;
5678
+ currentCanvas = null;
5679
+ gl = currentGL;
5680
+ currentGL = null;
5681
+
5682
+ // setup a GLSL program
5683
+ var vertexShader = createVertexShader(gl, figuresVertexShaderCode);
5684
+ var fragmentShader = createFragmentShader(gl, figuresFragmentShaderCode);
5685
+ var program = createProgram(gl, [vertexShader, fragmentShader]);
5686
+ gl.useProgram(program);
5687
+
5688
+ var cache = {};
5689
+ cache.gl = gl;
5690
+ cache.canvas = canvas;
5691
+ cache.resolutionLocation = gl.getUniformLocation(program, 'u_resolution');
5692
+ cache.scaleLocation = gl.getUniformLocation(program, 'u_scale');
5693
+ cache.offsetLocation = gl.getUniformLocation(program, 'u_offset');
5694
+ cache.positionLocation = gl.getAttribLocation(program, 'a_position');
5695
+ cache.colorLocation = gl.getAttribLocation(program, 'a_color');
5696
+
5697
+ figuresCache = cache;
5698
+ }
5699
+
5700
+ function drawFigures(width, height, backgroundColor, figures, context) {
5701
+ if (!figuresCache) {
5702
+ initFiguresGL();
5703
+ }
5704
+ var cache = figuresCache, canvas = cache.canvas, gl = cache.gl;
5705
+
5706
+ canvas.width = width;
5707
+ canvas.height = height;
5708
+ gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
5709
+ gl.uniform2f(cache.resolutionLocation, width, height);
5710
+
5711
+ // count triangle points
5712
+ var count = 0;
5713
+ var i, ii, rows;
5714
+ for (i = 0, ii = figures.length; i < ii; i++) {
5715
+ switch (figures[i].type) {
5716
+ case 'lattice':
5717
+ rows = (figures[i].coords.length / figures[i].verticesPerRow) | 0;
5718
+ count += (rows - 1) * (figures[i].verticesPerRow - 1) * 6;
5719
+ break;
5720
+ case 'triangles':
5721
+ count += figures[i].coords.length;
5722
+ break;
5723
+ }
5724
+ }
5725
+ // transfer data
5726
+ var coords = new Float32Array(count * 2);
5727
+ var colors = new Uint8Array(count * 3);
5728
+ var coordsMap = context.coords, colorsMap = context.colors;
5729
+ var pIndex = 0, cIndex = 0;
5730
+ for (i = 0, ii = figures.length; i < ii; i++) {
5731
+ var figure = figures[i], ps = figure.coords, cs = figure.colors;
5732
+ switch (figure.type) {
5733
+ case 'lattice':
5734
+ var cols = figure.verticesPerRow;
5735
+ rows = (ps.length / cols) | 0;
5736
+ for (var row = 1; row < rows; row++) {
5737
+ var offset = row * cols + 1;
5738
+ for (var col = 1; col < cols; col++, offset++) {
5739
+ coords[pIndex] = coordsMap[ps[offset - cols - 1]];
5740
+ coords[pIndex + 1] = coordsMap[ps[offset - cols - 1] + 1];
5741
+ coords[pIndex + 2] = coordsMap[ps[offset - cols]];
5742
+ coords[pIndex + 3] = coordsMap[ps[offset - cols] + 1];
5743
+ coords[pIndex + 4] = coordsMap[ps[offset - 1]];
5744
+ coords[pIndex + 5] = coordsMap[ps[offset - 1] + 1];
5745
+ colors[cIndex] = colorsMap[cs[offset - cols - 1]];
5746
+ colors[cIndex + 1] = colorsMap[cs[offset - cols - 1] + 1];
5747
+ colors[cIndex + 2] = colorsMap[cs[offset - cols - 1] + 2];
5748
+ colors[cIndex + 3] = colorsMap[cs[offset - cols]];
5749
+ colors[cIndex + 4] = colorsMap[cs[offset - cols] + 1];
5750
+ colors[cIndex + 5] = colorsMap[cs[offset - cols] + 2];
5751
+ colors[cIndex + 6] = colorsMap[cs[offset - 1]];
5752
+ colors[cIndex + 7] = colorsMap[cs[offset - 1] + 1];
5753
+ colors[cIndex + 8] = colorsMap[cs[offset - 1] + 2];
5754
+
5755
+ coords[pIndex + 6] = coords[pIndex + 2];
5756
+ coords[pIndex + 7] = coords[pIndex + 3];
5757
+ coords[pIndex + 8] = coords[pIndex + 4];
5758
+ coords[pIndex + 9] = coords[pIndex + 5];
5759
+ coords[pIndex + 10] = coordsMap[ps[offset]];
5760
+ coords[pIndex + 11] = coordsMap[ps[offset] + 1];
5761
+ colors[cIndex + 9] = colors[cIndex + 3];
5762
+ colors[cIndex + 10] = colors[cIndex + 4];
5763
+ colors[cIndex + 11] = colors[cIndex + 5];
5764
+ colors[cIndex + 12] = colors[cIndex + 6];
5765
+ colors[cIndex + 13] = colors[cIndex + 7];
5766
+ colors[cIndex + 14] = colors[cIndex + 8];
5767
+ colors[cIndex + 15] = colorsMap[cs[offset]];
5768
+ colors[cIndex + 16] = colorsMap[cs[offset] + 1];
5769
+ colors[cIndex + 17] = colorsMap[cs[offset] + 2];
5770
+ pIndex += 12;
5771
+ cIndex += 18;
5772
+ }
5773
+ }
5774
+ break;
5775
+ case 'triangles':
5776
+ for (var j = 0, jj = ps.length; j < jj; j++) {
5777
+ coords[pIndex] = coordsMap[ps[j]];
5778
+ coords[pIndex + 1] = coordsMap[ps[j] + 1];
5779
+ colors[cIndex] = colorsMap[cs[i]];
5780
+ colors[cIndex + 1] = colorsMap[cs[j] + 1];
5781
+ colors[cIndex + 2] = colorsMap[cs[j] + 2];
5782
+ pIndex += 2;
5783
+ cIndex += 3;
5784
+ }
5785
+ break;
5786
+ }
5787
+ }
5788
+
5789
+ // draw
5790
+ if (backgroundColor) {
5791
+ gl.clearColor(backgroundColor[0] / 255, backgroundColor[1] / 255,
5792
+ backgroundColor[2] / 255, 1.0);
5793
+ } else {
5794
+ gl.clearColor(0, 0, 0, 0);
5795
+ }
5796
+ gl.clear(gl.COLOR_BUFFER_BIT);
5797
+
5798
+ var coordsBuffer = gl.createBuffer();
5799
+ gl.bindBuffer(gl.ARRAY_BUFFER, coordsBuffer);
5800
+ gl.bufferData(gl.ARRAY_BUFFER, coords, gl.STATIC_DRAW);
5801
+ gl.enableVertexAttribArray(cache.positionLocation);
5802
+ gl.vertexAttribPointer(cache.positionLocation, 2, gl.FLOAT, false, 0, 0);
5803
+
5804
+ var colorsBuffer = gl.createBuffer();
5805
+ gl.bindBuffer(gl.ARRAY_BUFFER, colorsBuffer);
5806
+ gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW);
5807
+ gl.enableVertexAttribArray(cache.colorLocation);
5808
+ gl.vertexAttribPointer(cache.colorLocation, 3, gl.UNSIGNED_BYTE, false,
5809
+ 0, 0);
5810
+
5811
+ gl.uniform2f(cache.scaleLocation, context.scaleX, context.scaleY);
5812
+ gl.uniform2f(cache.offsetLocation, context.offsetX, context.offsetY);
5813
+
5814
+ gl.drawArrays(gl.TRIANGLES, 0, count);
5815
+
5816
+ gl.flush();
5817
+
5818
+ gl.deleteBuffer(coordsBuffer);
5819
+ gl.deleteBuffer(colorsBuffer);
5820
+
5821
+ return canvas;
5822
+ }
5823
+
5824
+ function cleanup() {
5825
+ if (smaskCache && smaskCache.canvas) {
5826
+ smaskCache.canvas.width = 0;
5827
+ smaskCache.canvas.height = 0;
5828
+ }
5829
+ if (figuresCache && figuresCache.canvas) {
5830
+ figuresCache.canvas.width = 0;
5831
+ figuresCache.canvas.height = 0;
5832
+ }
5833
+ smaskCache = null;
5834
+ figuresCache = null;
5835
+ }
5836
+
5837
+ return {
5838
+ get isEnabled() {
5839
+ if (PDFJS.disableWebGL) {
5840
+ return false;
5841
+ }
5842
+ var enabled = false;
5843
+ try {
5844
+ generateGL();
5845
+ enabled = !!currentGL;
5846
+ } catch (e) { }
5847
+ return shadow(this, 'isEnabled', enabled);
5848
+ },
5849
+ composeSMask: composeSMask,
5850
+ drawFigures: drawFigures,
5851
+ clear: cleanup
5852
+ };
5853
+ })();
5854
+
5855
+
5856
+ var ShadingIRs = {};
5857
+
5858
+ ShadingIRs.RadialAxial = {
5859
+ fromIR: function RadialAxial_fromIR(raw) {
5860
+ var type = raw[1];
5861
+ var colorStops = raw[2];
5862
+ var p0 = raw[3];
5863
+ var p1 = raw[4];
5864
+ var r0 = raw[5];
5865
+ var r1 = raw[6];
5866
+ return {
5867
+ type: 'Pattern',
5868
+ getPattern: function RadialAxial_getPattern(ctx) {
5869
+ var grad;
5870
+ if (type === 'axial') {
5871
+ grad = ctx.createLinearGradient(p0[0], p0[1], p1[0], p1[1]);
5872
+ } else if (type === 'radial') {
5873
+ grad = ctx.createRadialGradient(p0[0], p0[1], r0, p1[0], p1[1], r1);
5874
+ }
5875
+
5876
+ for (var i = 0, ii = colorStops.length; i < ii; ++i) {
5877
+ var c = colorStops[i];
5878
+ grad.addColorStop(c[0], c[1]);
5879
+ }
5880
+ return grad;
5881
+ }
5882
+ };
5883
+ }
5884
+ };
5885
+
5886
+ var createMeshCanvas = (function createMeshCanvasClosure() {
5887
+ function drawTriangle(data, context, p1, p2, p3, c1, c2, c3) {
5888
+ // Very basic Gouraud-shaded triangle rasterization algorithm.
5889
+ var coords = context.coords, colors = context.colors;
5890
+ var bytes = data.data, rowSize = data.width * 4;
5891
+ var tmp;
5892
+ if (coords[p1 + 1] > coords[p2 + 1]) {
5893
+ tmp = p1; p1 = p2; p2 = tmp; tmp = c1; c1 = c2; c2 = tmp;
5894
+ }
5895
+ if (coords[p2 + 1] > coords[p3 + 1]) {
5896
+ tmp = p2; p2 = p3; p3 = tmp; tmp = c2; c2 = c3; c3 = tmp;
5897
+ }
5898
+ if (coords[p1 + 1] > coords[p2 + 1]) {
5899
+ tmp = p1; p1 = p2; p2 = tmp; tmp = c1; c1 = c2; c2 = tmp;
5900
+ }
5901
+ var x1 = (coords[p1] + context.offsetX) * context.scaleX;
5902
+ var y1 = (coords[p1 + 1] + context.offsetY) * context.scaleY;
5903
+ var x2 = (coords[p2] + context.offsetX) * context.scaleX;
5904
+ var y2 = (coords[p2 + 1] + context.offsetY) * context.scaleY;
5905
+ var x3 = (coords[p3] + context.offsetX) * context.scaleX;
5906
+ var y3 = (coords[p3 + 1] + context.offsetY) * context.scaleY;
5907
+ if (y1 >= y3) {
5908
+ return;
5909
+ }
5910
+ var c1r = colors[c1], c1g = colors[c1 + 1], c1b = colors[c1 + 2];
5911
+ var c2r = colors[c2], c2g = colors[c2 + 1], c2b = colors[c2 + 2];
5912
+ var c3r = colors[c3], c3g = colors[c3 + 1], c3b = colors[c3 + 2];
5913
+
5914
+ var minY = Math.round(y1), maxY = Math.round(y3);
5915
+ var xa, car, cag, cab;
5916
+ var xb, cbr, cbg, cbb;
5917
+ var k;
5918
+ for (var y = minY; y <= maxY; y++) {
5919
+ if (y < y2) {
5920
+ k = y < y1 ? 0 : y1 === y2 ? 1 : (y1 - y) / (y1 - y2);
5921
+ xa = x1 - (x1 - x2) * k;
5922
+ car = c1r - (c1r - c2r) * k;
5923
+ cag = c1g - (c1g - c2g) * k;
5924
+ cab = c1b - (c1b - c2b) * k;
5925
+ } else {
5926
+ k = y > y3 ? 1 : y2 === y3 ? 0 : (y2 - y) / (y2 - y3);
5927
+ xa = x2 - (x2 - x3) * k;
5928
+ car = c2r - (c2r - c3r) * k;
5929
+ cag = c2g - (c2g - c3g) * k;
5930
+ cab = c2b - (c2b - c3b) * k;
5931
+ }
5932
+ k = y < y1 ? 0 : y > y3 ? 1 : (y1 - y) / (y1 - y3);
5933
+ xb = x1 - (x1 - x3) * k;
5934
+ cbr = c1r - (c1r - c3r) * k;
5935
+ cbg = c1g - (c1g - c3g) * k;
5936
+ cbb = c1b - (c1b - c3b) * k;
5937
+ var x1_ = Math.round(Math.min(xa, xb));
5938
+ var x2_ = Math.round(Math.max(xa, xb));
5939
+ var j = rowSize * y + x1_ * 4;
5940
+ for (var x = x1_; x <= x2_; x++) {
5941
+ k = (xa - x) / (xa - xb);
5942
+ k = k < 0 ? 0 : k > 1 ? 1 : k;
5943
+ bytes[j++] = (car - (car - cbr) * k) | 0;
5944
+ bytes[j++] = (cag - (cag - cbg) * k) | 0;
5945
+ bytes[j++] = (cab - (cab - cbb) * k) | 0;
5946
+ bytes[j++] = 255;
5947
+ }
5948
+ }
5949
+ }
5950
+
5951
+ function drawFigure(data, figure, context) {
5952
+ var ps = figure.coords;
5953
+ var cs = figure.colors;
5954
+ var i, ii;
5955
+ switch (figure.type) {
5956
+ case 'lattice':
5957
+ var verticesPerRow = figure.verticesPerRow;
5958
+ var rows = Math.floor(ps.length / verticesPerRow) - 1;
5959
+ var cols = verticesPerRow - 1;
5960
+ for (i = 0; i < rows; i++) {
5961
+ var q = i * verticesPerRow;
5962
+ for (var j = 0; j < cols; j++, q++) {
5963
+ drawTriangle(data, context,
5964
+ ps[q], ps[q + 1], ps[q + verticesPerRow],
5965
+ cs[q], cs[q + 1], cs[q + verticesPerRow]);
5966
+ drawTriangle(data, context,
5967
+ ps[q + verticesPerRow + 1], ps[q + 1], ps[q + verticesPerRow],
5968
+ cs[q + verticesPerRow + 1], cs[q + 1], cs[q + verticesPerRow]);
5969
+ }
5970
+ }
5971
+ break;
5972
+ case 'triangles':
5973
+ for (i = 0, ii = ps.length; i < ii; i += 3) {
5974
+ drawTriangle(data, context,
5975
+ ps[i], ps[i + 1], ps[i + 2],
5976
+ cs[i], cs[i + 1], cs[i + 2]);
5977
+ }
5978
+ break;
5979
+ default:
5980
+ error('illigal figure');
5981
+ break;
5982
+ }
5983
+ }
5984
+
5985
+ function createMeshCanvas(bounds, combinesScale, coords, colors, figures,
5986
+ backgroundColor) {
5987
+ // we will increase scale on some weird factor to let antialiasing take
5988
+ // care of "rough" edges
5989
+ var EXPECTED_SCALE = 1.1;
5990
+ // MAX_PATTERN_SIZE is used to avoid OOM situation.
5991
+ var MAX_PATTERN_SIZE = 3000; // 10in @ 300dpi shall be enough
5992
+
5993
+ var offsetX = Math.floor(bounds[0]);
5994
+ var offsetY = Math.floor(bounds[1]);
5995
+ var boundsWidth = Math.ceil(bounds[2]) - offsetX;
5996
+ var boundsHeight = Math.ceil(bounds[3]) - offsetY;
5997
+
5998
+ var width = Math.min(Math.ceil(Math.abs(boundsWidth * combinesScale[0] *
5999
+ EXPECTED_SCALE)), MAX_PATTERN_SIZE);
6000
+ var height = Math.min(Math.ceil(Math.abs(boundsHeight * combinesScale[1] *
6001
+ EXPECTED_SCALE)), MAX_PATTERN_SIZE);
6002
+ var scaleX = boundsWidth / width;
6003
+ var scaleY = boundsHeight / height;
6004
+
6005
+ var context = {
6006
+ coords: coords,
6007
+ colors: colors,
6008
+ offsetX: -offsetX,
6009
+ offsetY: -offsetY,
6010
+ scaleX: 1 / scaleX,
6011
+ scaleY: 1 / scaleY
6012
+ };
6013
+
6014
+ var canvas, tmpCanvas, i, ii;
6015
+ if (WebGLUtils.isEnabled) {
6016
+ canvas = WebGLUtils.drawFigures(width, height, backgroundColor,
6017
+ figures, context);
6018
+
6019
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=972126
6020
+ tmpCanvas = CachedCanvases.getCanvas('mesh', width, height, false);
6021
+ tmpCanvas.context.drawImage(canvas, 0, 0);
6022
+ canvas = tmpCanvas.canvas;
6023
+ } else {
6024
+ tmpCanvas = CachedCanvases.getCanvas('mesh', width, height, false);
6025
+ var tmpCtx = tmpCanvas.context;
6026
+
6027
+ var data = tmpCtx.createImageData(width, height);
6028
+ if (backgroundColor) {
6029
+ var bytes = data.data;
6030
+ for (i = 0, ii = bytes.length; i < ii; i += 4) {
6031
+ bytes[i] = backgroundColor[0];
6032
+ bytes[i + 1] = backgroundColor[1];
6033
+ bytes[i + 2] = backgroundColor[2];
6034
+ bytes[i + 3] = 255;
6035
+ }
6036
+ }
6037
+ for (i = 0; i < figures.length; i++) {
6038
+ drawFigure(data, figures[i], context);
6039
+ }
6040
+ tmpCtx.putImageData(data, 0, 0);
6041
+ canvas = tmpCanvas.canvas;
6042
+ }
6043
+
6044
+ return {canvas: canvas, offsetX: offsetX, offsetY: offsetY,
6045
+ scaleX: scaleX, scaleY: scaleY};
6046
+ }
6047
+ return createMeshCanvas;
6048
+ })();
6049
+
6050
+ ShadingIRs.Mesh = {
6051
+ fromIR: function Mesh_fromIR(raw) {
6052
+ //var type = raw[1];
6053
+ var coords = raw[2];
6054
+ var colors = raw[3];
6055
+ var figures = raw[4];
6056
+ var bounds = raw[5];
6057
+ var matrix = raw[6];
6058
+ //var bbox = raw[7];
6059
+ var background = raw[8];
6060
+ return {
6061
+ type: 'Pattern',
6062
+ getPattern: function Mesh_getPattern(ctx, owner, shadingFill) {
6063
+ var scale;
6064
+ if (shadingFill) {
6065
+ scale = Util.singularValueDecompose2dScale(ctx.mozCurrentTransform);
6066
+ } else {
6067
+ // Obtain scale from matrix and current transformation matrix.
6068
+ scale = Util.singularValueDecompose2dScale(owner.baseTransform);
6069
+ if (matrix) {
6070
+ var matrixScale = Util.singularValueDecompose2dScale(matrix);
6071
+ scale = [scale[0] * matrixScale[0],
6072
+ scale[1] * matrixScale[1]];
6073
+ }
6074
+ }
6075
+
6076
+
6077
+ // Rasterizing on the main thread since sending/queue large canvases
6078
+ // might cause OOM.
6079
+ var temporaryPatternCanvas = createMeshCanvas(bounds, scale, coords,
6080
+ colors, figures, shadingFill ? null : background);
6081
+
6082
+ if (!shadingFill) {
6083
+ ctx.setTransform.apply(ctx, owner.baseTransform);
6084
+ if (matrix) {
6085
+ ctx.transform.apply(ctx, matrix);
6086
+ }
6087
+ }
6088
+
6089
+ ctx.translate(temporaryPatternCanvas.offsetX,
6090
+ temporaryPatternCanvas.offsetY);
6091
+ ctx.scale(temporaryPatternCanvas.scaleX,
6092
+ temporaryPatternCanvas.scaleY);
6093
+
6094
+ return ctx.createPattern(temporaryPatternCanvas.canvas, 'no-repeat');
6095
+ }
6096
+ };
6097
+ }
6098
+ };
6099
+
6100
+ ShadingIRs.Dummy = {
6101
+ fromIR: function Dummy_fromIR() {
6102
+ return {
6103
+ type: 'Pattern',
6104
+ getPattern: function Dummy_fromIR_getPattern() {
6105
+ return 'hotpink';
6106
+ }
6107
+ };
6108
+ }
6109
+ };
6110
+
6111
+ function getShadingPatternFromIR(raw) {
6112
+ var shadingIR = ShadingIRs[raw[0]];
6113
+ if (!shadingIR) {
6114
+ error('Unknown IR type: ' + raw[0]);
6115
+ }
6116
+ return shadingIR.fromIR(raw);
6117
+ }
6118
+
6119
+ var TilingPattern = (function TilingPatternClosure() {
6120
+ var PaintType = {
6121
+ COLORED: 1,
6122
+ UNCOLORED: 2
6123
+ };
6124
+
6125
+ var MAX_PATTERN_SIZE = 3000; // 10in @ 300dpi shall be enough
6126
+
6127
+ function TilingPattern(IR, color, ctx, objs, commonObjs, baseTransform) {
6128
+ this.operatorList = IR[2];
6129
+ this.matrix = IR[3] || [1, 0, 0, 1, 0, 0];
6130
+ this.bbox = IR[4];
6131
+ this.xstep = IR[5];
6132
+ this.ystep = IR[6];
6133
+ this.paintType = IR[7];
6134
+ this.tilingType = IR[8];
6135
+ this.color = color;
6136
+ this.objs = objs;
6137
+ this.commonObjs = commonObjs;
6138
+ this.baseTransform = baseTransform;
6139
+ this.type = 'Pattern';
6140
+ this.ctx = ctx;
6141
+ }
6142
+
6143
+ TilingPattern.prototype = {
6144
+ createPatternCanvas: function TilinPattern_createPatternCanvas(owner) {
6145
+ var operatorList = this.operatorList;
6146
+ var bbox = this.bbox;
6147
+ var xstep = this.xstep;
6148
+ var ystep = this.ystep;
6149
+ var paintType = this.paintType;
6150
+ var tilingType = this.tilingType;
6151
+ var color = this.color;
6152
+ var objs = this.objs;
6153
+ var commonObjs = this.commonObjs;
6154
+
6155
+ info('TilingType: ' + tilingType);
6156
+
6157
+ var x0 = bbox[0], y0 = bbox[1], x1 = bbox[2], y1 = bbox[3];
6158
+
6159
+ var topLeft = [x0, y0];
6160
+ // we want the canvas to be as large as the step size
6161
+ var botRight = [x0 + xstep, y0 + ystep];
6162
+
6163
+ var width = botRight[0] - topLeft[0];
6164
+ var height = botRight[1] - topLeft[1];
6165
+
6166
+ // Obtain scale from matrix and current transformation matrix.
6167
+ var matrixScale = Util.singularValueDecompose2dScale(this.matrix);
6168
+ var curMatrixScale = Util.singularValueDecompose2dScale(
6169
+ this.baseTransform);
6170
+ var combinedScale = [matrixScale[0] * curMatrixScale[0],
6171
+ matrixScale[1] * curMatrixScale[1]];
6172
+
6173
+ // MAX_PATTERN_SIZE is used to avoid OOM situation.
6174
+ // Use width and height values that are as close as possible to the end
6175
+ // result when the pattern is used. Too low value makes the pattern look
6176
+ // blurry. Too large value makes it look too crispy.
6177
+ width = Math.min(Math.ceil(Math.abs(width * combinedScale[0])),
6178
+ MAX_PATTERN_SIZE);
6179
+
6180
+ height = Math.min(Math.ceil(Math.abs(height * combinedScale[1])),
6181
+ MAX_PATTERN_SIZE);
6182
+
6183
+ var tmpCanvas = CachedCanvases.getCanvas('pattern', width, height, true);
6184
+ var tmpCtx = tmpCanvas.context;
6185
+ var graphics = new CanvasGraphics(tmpCtx, commonObjs, objs);
6186
+ graphics.groupLevel = owner.groupLevel;
6187
+
6188
+ this.setFillAndStrokeStyleToContext(tmpCtx, paintType, color);
6189
+
6190
+ this.setScale(width, height, xstep, ystep);
6191
+ this.transformToScale(graphics);
6192
+
6193
+ // transform coordinates to pattern space
6194
+ var tmpTranslate = [1, 0, 0, 1, -topLeft[0], -topLeft[1]];
6195
+ graphics.transform.apply(graphics, tmpTranslate);
6196
+
6197
+ this.clipBbox(graphics, bbox, x0, y0, x1, y1);
6198
+
6199
+ graphics.executeOperatorList(operatorList);
6200
+ return tmpCanvas.canvas;
6201
+ },
6202
+
6203
+ setScale: function TilingPattern_setScale(width, height, xstep, ystep) {
6204
+ this.scale = [width / xstep, height / ystep];
6205
+ },
6206
+
6207
+ transformToScale: function TilingPattern_transformToScale(graphics) {
6208
+ var scale = this.scale;
6209
+ var tmpScale = [scale[0], 0, 0, scale[1], 0, 0];
6210
+ graphics.transform.apply(graphics, tmpScale);
6211
+ },
6212
+
6213
+ scaleToContext: function TilingPattern_scaleToContext() {
6214
+ var scale = this.scale;
6215
+ this.ctx.scale(1 / scale[0], 1 / scale[1]);
6216
+ },
6217
+
6218
+ clipBbox: function clipBbox(graphics, bbox, x0, y0, x1, y1) {
6219
+ if (bbox && isArray(bbox) && bbox.length === 4) {
6220
+ var bboxWidth = x1 - x0;
6221
+ var bboxHeight = y1 - y0;
6222
+ graphics.ctx.rect(x0, y0, bboxWidth, bboxHeight);
6223
+ graphics.clip();
6224
+ graphics.endPath();
6225
+ }
6226
+ },
6227
+
6228
+ setFillAndStrokeStyleToContext:
6229
+ function setFillAndStrokeStyleToContext(context, paintType, color) {
6230
+ switch (paintType) {
6231
+ case PaintType.COLORED:
6232
+ var ctx = this.ctx;
6233
+ context.fillStyle = ctx.fillStyle;
6234
+ context.strokeStyle = ctx.strokeStyle;
6235
+ break;
6236
+ case PaintType.UNCOLORED:
6237
+ var cssColor = Util.makeCssRgb(color[0], color[1], color[2]);
6238
+ context.fillStyle = cssColor;
6239
+ context.strokeStyle = cssColor;
6240
+ break;
6241
+ default:
6242
+ error('Unsupported paint type: ' + paintType);
6243
+ }
6244
+ },
6245
+
6246
+ getPattern: function TilingPattern_getPattern(ctx, owner) {
6247
+ var temporaryPatternCanvas = this.createPatternCanvas(owner);
6248
+
6249
+ ctx = this.ctx;
6250
+ ctx.setTransform.apply(ctx, this.baseTransform);
6251
+ ctx.transform.apply(ctx, this.matrix);
6252
+ this.scaleToContext();
6253
+
6254
+ return ctx.createPattern(temporaryPatternCanvas, 'repeat');
6255
+ }
6256
+ };
6257
+
6258
+ return TilingPattern;
6259
+ })();
6260
+
6261
+
6262
+ PDFJS.disableFontFace = false;
6263
+
6264
+ var FontLoader = {
6265
+ insertRule: function fontLoaderInsertRule(rule) {
6266
+ var styleElement = document.getElementById('PDFJS_FONT_STYLE_TAG');
6267
+ if (!styleElement) {
6268
+ styleElement = document.createElement('style');
6269
+ styleElement.id = 'PDFJS_FONT_STYLE_TAG';
6270
+ document.documentElement.getElementsByTagName('head')[0].appendChild(
6271
+ styleElement);
6272
+ }
6273
+
6274
+ var styleSheet = styleElement.sheet;
6275
+ styleSheet.insertRule(rule, styleSheet.cssRules.length);
6276
+ },
6277
+
6278
+ clear: function fontLoaderClear() {
6279
+ var styleElement = document.getElementById('PDFJS_FONT_STYLE_TAG');
6280
+ if (styleElement) {
6281
+ styleElement.parentNode.removeChild(styleElement);
6282
+ }
6283
+ this.nativeFontFaces.forEach(function(nativeFontFace) {
6284
+ document.fonts.delete(nativeFontFace);
6285
+ });
6286
+ this.nativeFontFaces.length = 0;
6287
+ },
6288
+ get loadTestFont() {
6289
+ // This is a CFF font with 1 glyph for '.' that fills its entire width and
6290
+ // height.
6291
+ return shadow(this, 'loadTestFont', atob(
6292
+ 'T1RUTwALAIAAAwAwQ0ZGIDHtZg4AAAOYAAAAgUZGVE1lkzZwAAAEHAAAABxHREVGABQAFQ' +
6293
+ 'AABDgAAAAeT1MvMlYNYwkAAAEgAAAAYGNtYXABDQLUAAACNAAAAUJoZWFk/xVFDQAAALwA' +
6294
+ 'AAA2aGhlYQdkA+oAAAD0AAAAJGhtdHgD6AAAAAAEWAAAAAZtYXhwAAJQAAAAARgAAAAGbm' +
6295
+ 'FtZVjmdH4AAAGAAAAAsXBvc3T/hgAzAAADeAAAACAAAQAAAAEAALZRFsRfDzz1AAsD6AAA' +
6296
+ 'AADOBOTLAAAAAM4KHDwAAAAAA+gDIQAAAAgAAgAAAAAAAAABAAADIQAAAFoD6AAAAAAD6A' +
6297
+ 'ABAAAAAAAAAAAAAAAAAAAAAQAAUAAAAgAAAAQD6AH0AAUAAAKKArwAAACMAooCvAAAAeAA' +
6298
+ 'MQECAAACAAYJAAAAAAAAAAAAAQAAAAAAAAAAAAAAAFBmRWQAwAAuAC4DIP84AFoDIQAAAA' +
6299
+ 'AAAQAAAAAAAAAAACAAIAABAAAADgCuAAEAAAAAAAAAAQAAAAEAAAAAAAEAAQAAAAEAAAAA' +
6300
+ 'AAIAAQAAAAEAAAAAAAMAAQAAAAEAAAAAAAQAAQAAAAEAAAAAAAUAAQAAAAEAAAAAAAYAAQ' +
6301
+ 'AAAAMAAQQJAAAAAgABAAMAAQQJAAEAAgABAAMAAQQJAAIAAgABAAMAAQQJAAMAAgABAAMA' +
6302
+ 'AQQJAAQAAgABAAMAAQQJAAUAAgABAAMAAQQJAAYAAgABWABYAAAAAAAAAwAAAAMAAAAcAA' +
6303
+ 'EAAAAAADwAAwABAAAAHAAEACAAAAAEAAQAAQAAAC7//wAAAC7////TAAEAAAAAAAABBgAA' +
6304
+ 'AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAA' +
6305
+ 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' +
6306
+ 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' +
6307
+ 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' +
6308
+ 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAA' +
6309
+ 'AAAAD/gwAyAAAAAQAAAAAAAAAAAAAAAAAAAAABAAQEAAEBAQJYAAEBASH4DwD4GwHEAvgc' +
6310
+ 'A/gXBIwMAYuL+nz5tQXkD5j3CBLnEQACAQEBIVhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWF' +
6311
+ 'hYWFhYWFhYAAABAQAADwACAQEEE/t3Dov6fAH6fAT+fPp8+nwHDosMCvm1Cvm1DAz6fBQA' +
6312
+ 'AAAAAAABAAAAAMmJbzEAAAAAzgTjFQAAAADOBOQpAAEAAAAAAAAADAAUAAQAAAABAAAAAg' +
6313
+ 'ABAAAAAAAAAAAD6AAAAAAAAA=='
6314
+ ));
6315
+ },
6316
+
6317
+ loadTestFontId: 0,
6318
+
6319
+ loadingContext: {
6320
+ requests: [],
6321
+ nextRequestId: 0
6322
+ },
6323
+
6324
+ isSyncFontLoadingSupported: (function detectSyncFontLoadingSupport() {
6325
+ if (isWorker) {
6326
+ return false;
6327
+ }
6328
+
6329
+ // User agent string sniffing is bad, but there is no reliable way to tell
6330
+ // if font is fully loaded and ready to be used with canvas.
6331
+ var userAgent = window.navigator.userAgent;
6332
+ var m = /Mozilla\/5.0.*?rv:(\d+).*? Gecko/.exec(userAgent);
6333
+ if (m && m[1] >= 14) {
6334
+ return true;
6335
+ }
6336
+ // TODO other browsers
6337
+ if (userAgent === 'node') {
6338
+ return true;
6339
+ }
6340
+ return false;
6341
+ })(),
6342
+
6343
+ nativeFontFaces: [],
6344
+
6345
+ isFontLoadingAPISupported: (!isWorker && typeof document !== 'undefined' &&
6346
+ !!document.fonts),
6347
+
6348
+ addNativeFontFace: function fontLoader_addNativeFontFace(nativeFontFace) {
6349
+ this.nativeFontFaces.push(nativeFontFace);
6350
+ document.fonts.add(nativeFontFace);
6351
+ },
6352
+
6353
+ bind: function fontLoaderBind(fonts, callback) {
6354
+ assert(!isWorker, 'bind() shall be called from main thread');
6355
+
6356
+ var rules = [];
6357
+ var fontsToLoad = [];
6358
+ var fontLoadPromises = [];
6359
+ for (var i = 0, ii = fonts.length; i < ii; i++) {
6360
+ var font = fonts[i];
6361
+
6362
+ // Add the font to the DOM only once or skip if the font
6363
+ // is already loaded.
6364
+ if (font.attached || font.loading === false) {
6365
+ continue;
6366
+ }
6367
+ font.attached = true;
6368
+
6369
+ if (this.isFontLoadingAPISupported) {
6370
+ var nativeFontFace = font.createNativeFontFace();
6371
+ if (nativeFontFace) {
6372
+ fontLoadPromises.push(nativeFontFace.loaded);
6373
+ }
6374
+ } else {
6375
+ var rule = font.bindDOM();
6376
+ if (rule) {
6377
+ rules.push(rule);
6378
+ fontsToLoad.push(font);
6379
+ }
6380
+ }
6381
+ }
6382
+
6383
+ var request = FontLoader.queueLoadingCallback(callback);
6384
+ if (this.isFontLoadingAPISupported) {
6385
+ Promise.all(fontsToLoad).then(function() {
6386
+ request.complete();
6387
+ });
6388
+ } else if (rules.length > 0 && !this.isSyncFontLoadingSupported) {
6389
+ FontLoader.prepareFontLoadEvent(rules, fontsToLoad, request);
6390
+ } else {
6391
+ request.complete();
6392
+ }
6393
+ },
6394
+
6395
+ queueLoadingCallback: function FontLoader_queueLoadingCallback(callback) {
6396
+ function LoadLoader_completeRequest() {
6397
+ assert(!request.end, 'completeRequest() cannot be called twice');
6398
+ request.end = Date.now();
6399
+
6400
+ // sending all completed requests in order how they were queued
6401
+ while (context.requests.length > 0 && context.requests[0].end) {
6402
+ var otherRequest = context.requests.shift();
6403
+ setTimeout(otherRequest.callback, 0);
6404
+ }
6405
+ }
6406
+
6407
+ var context = FontLoader.loadingContext;
6408
+ var requestId = 'pdfjs-font-loading-' + (context.nextRequestId++);
6409
+ var request = {
6410
+ id: requestId,
6411
+ complete: LoadLoader_completeRequest,
6412
+ callback: callback,
6413
+ started: Date.now()
6414
+ };
6415
+ context.requests.push(request);
6416
+ return request;
6417
+ },
6418
+
6419
+ prepareFontLoadEvent: function fontLoaderPrepareFontLoadEvent(rules,
6420
+ fonts,
6421
+ request) {
6422
+ /** Hack begin */
6423
+ // There's currently no event when a font has finished downloading so the
6424
+ // following code is a dirty hack to 'guess' when a font is
6425
+ // ready. It's assumed fonts are loaded in order, so add a known test
6426
+ // font after the desired fonts and then test for the loading of that
6427
+ // test font.
6428
+
6429
+ function int32(data, offset) {
6430
+ return (data.charCodeAt(offset) << 24) |
6431
+ (data.charCodeAt(offset + 1) << 16) |
6432
+ (data.charCodeAt(offset + 2) << 8) |
6433
+ (data.charCodeAt(offset + 3) & 0xff);
6434
+ }
6435
+
6436
+ function spliceString(s, offset, remove, insert) {
6437
+ var chunk1 = s.substr(0, offset);
6438
+ var chunk2 = s.substr(offset + remove);
6439
+ return chunk1 + insert + chunk2;
6440
+ }
6441
+
6442
+ var i, ii;
6443
+
6444
+ var canvas = document.createElement('canvas');
6445
+ canvas.width = 1;
6446
+ canvas.height = 1;
6447
+ var ctx = canvas.getContext('2d');
6448
+
6449
+ var called = 0;
6450
+ function isFontReady(name, callback) {
6451
+ called++;
6452
+ // With setTimeout clamping this gives the font ~100ms to load.
6453
+ if(called > 30) {
6454
+ warn('Load test font never loaded.');
6455
+ callback();
6456
+ return;
6457
+ }
6458
+ ctx.font = '30px ' + name;
6459
+ ctx.fillText('.', 0, 20);
6460
+ var imageData = ctx.getImageData(0, 0, 1, 1);
6461
+ if (imageData.data[3] > 0) {
6462
+ callback();
6463
+ return;
6464
+ }
6465
+ setTimeout(isFontReady.bind(null, name, callback));
6466
+ }
6467
+
6468
+ var loadTestFontId = 'lt' + Date.now() + this.loadTestFontId++;
6469
+ // Chromium seems to cache fonts based on a hash of the actual font data,
6470
+ // so the font must be modified for each load test else it will appear to
6471
+ // be loaded already.
6472
+ // TODO: This could maybe be made faster by avoiding the btoa of the full
6473
+ // font by splitting it in chunks before hand and padding the font id.
6474
+ var data = this.loadTestFont;
6475
+ var COMMENT_OFFSET = 976; // has to be on 4 byte boundary (for checksum)
6476
+ data = spliceString(data, COMMENT_OFFSET, loadTestFontId.length,
6477
+ loadTestFontId);
6478
+ // CFF checksum is important for IE, adjusting it
6479
+ var CFF_CHECKSUM_OFFSET = 16;
6480
+ var XXXX_VALUE = 0x58585858; // the "comment" filled with 'X'
6481
+ var checksum = int32(data, CFF_CHECKSUM_OFFSET);
6482
+ for (i = 0, ii = loadTestFontId.length - 3; i < ii; i += 4) {
6483
+ checksum = (checksum - XXXX_VALUE + int32(loadTestFontId, i)) | 0;
6484
+ }
6485
+ if (i < loadTestFontId.length) { // align to 4 bytes boundary
6486
+ checksum = (checksum - XXXX_VALUE +
6487
+ int32(loadTestFontId + 'XXX', i)) | 0;
6488
+ }
6489
+ data = spliceString(data, CFF_CHECKSUM_OFFSET, 4, string32(checksum));
6490
+
6491
+ var url = 'url(data:font/opentype;base64,' + btoa(data) + ');';
6492
+ var rule = '@font-face { font-family:"' + loadTestFontId + '";src:' +
6493
+ url + '}';
6494
+ FontLoader.insertRule(rule);
6495
+
6496
+ var names = [];
6497
+ for (i = 0, ii = fonts.length; i < ii; i++) {
6498
+ names.push(fonts[i].loadedName);
6499
+ }
6500
+ names.push(loadTestFontId);
6501
+
6502
+ var div = document.createElement('div');
6503
+ div.setAttribute('style',
6504
+ 'visibility: hidden;' +
6505
+ 'width: 10px; height: 10px;' +
6506
+ 'position: absolute; top: 0px; left: 0px;');
6507
+ for (i = 0, ii = names.length; i < ii; ++i) {
6508
+ var span = document.createElement('span');
6509
+ span.textContent = 'Hi';
6510
+ span.style.fontFamily = names[i];
6511
+ div.appendChild(span);
6512
+ }
6513
+ document.body.appendChild(div);
6514
+
6515
+ isFontReady(loadTestFontId, function() {
6516
+ document.body.removeChild(div);
6517
+ request.complete();
6518
+ });
6519
+ /** Hack end */
6520
+ }
6521
+ };
6522
+
6523
+ var FontFaceObject = (function FontFaceObjectClosure() {
6524
+ function FontFaceObject(name, file, properties) {
6525
+ this.compiledGlyphs = {};
6526
+ if (arguments.length === 1) {
6527
+ // importing translated data
6528
+ var data = arguments[0];
6529
+ for (var i in data) {
6530
+ this[i] = data[i];
6531
+ }
6532
+ return;
6533
+ }
6534
+ }
6535
+ FontFaceObject.prototype = {
6536
+ createNativeFontFace: function FontFaceObject_createNativeFontFace() {
6537
+ if (!this.data) {
6538
+ return null;
6539
+ }
6540
+
6541
+ if (PDFJS.disableFontFace) {
6542
+ this.disableFontFace = true;
6543
+ return null;
6544
+ }
6545
+
6546
+ var nativeFontFace = new FontFace(this.loadedName, this.data, {});
6547
+
6548
+ FontLoader.addNativeFontFace(nativeFontFace);
6549
+
6550
+ if (PDFJS.pdfBug && 'FontInspector' in globalScope &&
6551
+ globalScope['FontInspector'].enabled) {
6552
+ globalScope['FontInspector'].fontAdded(this);
6553
+ }
6554
+ return nativeFontFace;
6555
+ },
6556
+
6557
+ bindDOM: function FontFaceObject_bindDOM() {
6558
+ if (!this.data) {
6559
+ return null;
6560
+ }
6561
+
6562
+ if (PDFJS.disableFontFace) {
6563
+ this.disableFontFace = true;
6564
+ return null;
6565
+ }
6566
+
6567
+ var data = bytesToString(new Uint8Array(this.data));
6568
+ var fontName = this.loadedName;
6569
+
6570
+ // Add the font-face rule to the document
6571
+ var url = ('url(data:' + this.mimetype + ';base64,' +
6572
+ window.btoa(data) + ');');
6573
+ var rule = '@font-face { font-family:"' + fontName + '";src:' + url + '}';
6574
+ FontLoader.insertRule(rule);
6575
+
6576
+ if (PDFJS.pdfBug && 'FontInspector' in globalScope &&
6577
+ globalScope['FontInspector'].enabled) {
6578
+ globalScope['FontInspector'].fontAdded(this, url);
6579
+ }
6580
+
6581
+ return rule;
6582
+ },
6583
+
6584
+ getPathGenerator: function FontLoader_getPathGenerator(objs, character) {
6585
+ if (!(character in this.compiledGlyphs)) {
6586
+ var js = objs.get(this.loadedName + '_path_' + character);
6587
+ /*jshint -W054 */
6588
+ this.compiledGlyphs[character] = new Function('c', 'size', js);
6589
+ }
6590
+ return this.compiledGlyphs[character];
6591
+ }
6592
+ };
6593
+ return FontFaceObject;
6594
+ })();
6595
+
6596
+
6597
+ var ANNOT_MIN_SIZE = 10; // px
6598
+
6599
+ var AnnotationUtils = (function AnnotationUtilsClosure() {
6600
+ // TODO(mack): This dupes some of the logic in CanvasGraphics.setFont()
6601
+ function setTextStyles(element, item, fontObj) {
6602
+
6603
+ var style = element.style;
6604
+ style.fontSize = item.fontSize + 'px';
6605
+ style.direction = item.fontDirection < 0 ? 'rtl': 'ltr';
6606
+
6607
+ if (!fontObj) {
6608
+ return;
6609
+ }
6610
+
6611
+ style.fontWeight = fontObj.black ?
6612
+ (fontObj.bold ? 'bolder' : 'bold') :
6613
+ (fontObj.bold ? 'bold' : 'normal');
6614
+ style.fontStyle = fontObj.italic ? 'italic' : 'normal';
6615
+
6616
+ var fontName = fontObj.loadedName;
6617
+ var fontFamily = fontName ? '"' + fontName + '", ' : '';
6618
+ // Use a reasonable default font if the font doesn't specify a fallback
6619
+ var fallbackName = fontObj.fallbackName || 'Helvetica, sans-serif';
6620
+ style.fontFamily = fontFamily + fallbackName;
6621
+ }
6622
+
6623
+ function initContainer(item, drawBorder) {
6624
+ var container = document.createElement('section');
6625
+ var cstyle = container.style;
6626
+ var width = item.rect[2] - item.rect[0];
6627
+ var height = item.rect[3] - item.rect[1];
6628
+
6629
+ var bWidth = item.borderWidth || 0;
6630
+ if (bWidth) {
6631
+ width = width - 2 * bWidth;
6632
+ height = height - 2 * bWidth;
6633
+ cstyle.borderWidth = bWidth + 'px';
6634
+ var color = item.color;
6635
+ if (drawBorder && color) {
6636
+ cstyle.borderStyle = 'solid';
6637
+ cstyle.borderColor = Util.makeCssRgb(Math.round(color[0] * 255),
6638
+ Math.round(color[1] * 255),
6639
+ Math.round(color[2] * 255));
6640
+ }
6641
+ }
6642
+ cstyle.width = width + 'px';
6643
+ cstyle.height = height + 'px';
6644
+ return container;
6645
+ }
6646
+
6647
+ function getHtmlElementForTextWidgetAnnotation(item, commonObjs) {
6648
+ var element = document.createElement('div');
6649
+ var width = item.rect[2] - item.rect[0];
6650
+ var height = item.rect[3] - item.rect[1];
6651
+ element.style.width = width + 'px';
6652
+ element.style.height = height + 'px';
6653
+ element.style.display = 'table';
6654
+
6655
+ var content = document.createElement('div');
6656
+ content.textContent = item.fieldValue;
6657
+ var textAlignment = item.textAlignment;
6658
+ content.style.textAlign = ['left', 'center', 'right'][textAlignment];
6659
+ content.style.verticalAlign = 'middle';
6660
+ content.style.display = 'table-cell';
6661
+
6662
+ var fontObj = item.fontRefName ?
6663
+ commonObjs.getData(item.fontRefName) : null;
6664
+ setTextStyles(content, item, fontObj);
6665
+
6666
+ element.appendChild(content);
6667
+
6668
+ return element;
6669
+ }
6670
+
6671
+ function getHtmlElementForTextAnnotation(item) {
6672
+ var rect = item.rect;
6673
+
6674
+ // sanity check because of OOo-generated PDFs
6675
+ if ((rect[3] - rect[1]) < ANNOT_MIN_SIZE) {
6676
+ rect[3] = rect[1] + ANNOT_MIN_SIZE;
6677
+ }
6678
+ if ((rect[2] - rect[0]) < ANNOT_MIN_SIZE) {
6679
+ rect[2] = rect[0] + (rect[3] - rect[1]); // make it square
6680
+ }
6681
+
6682
+ var container = initContainer(item, false);
6683
+ container.className = 'annotText';
6684
+
6685
+ var image = document.createElement('img');
6686
+ image.style.height = container.style.height;
6687
+ image.style.width = container.style.width;
6688
+ var iconName = item.name;
6689
+ image.src = PDFJS.imageResourcesPath + 'annotation-' +
6690
+ iconName.toLowerCase() + '.svg';
6691
+ image.alt = '[{{type}} Annotation]';
6692
+ image.dataset.l10nId = 'text_annotation_type';
6693
+ image.dataset.l10nArgs = JSON.stringify({type: iconName});
6694
+
6695
+ var contentWrapper = document.createElement('div');
6696
+ contentWrapper.className = 'annotTextContentWrapper';
6697
+ contentWrapper.style.left = Math.floor(rect[2] - rect[0] + 5) + 'px';
6698
+ contentWrapper.style.top = '-10px';
6699
+
6700
+ var content = document.createElement('div');
6701
+ content.className = 'annotTextContent';
6702
+ content.setAttribute('hidden', true);
6703
+
6704
+ var i, ii;
6705
+ if (item.hasBgColor) {
6706
+ var color = item.color;
6707
+
6708
+ // Enlighten the color (70%)
6709
+ var BACKGROUND_ENLIGHT = 0.7;
6710
+ var r = BACKGROUND_ENLIGHT * (1.0 - color[0]) + color[0];
6711
+ var g = BACKGROUND_ENLIGHT * (1.0 - color[1]) + color[1];
6712
+ var b = BACKGROUND_ENLIGHT * (1.0 - color[2]) + color[2];
6713
+ content.style.backgroundColor = Util.makeCssRgb((r * 255) | 0,
6714
+ (g * 255) | 0,
6715
+ (b * 255) | 0);
6716
+ }
6717
+
6718
+ var title = document.createElement('h1');
6719
+ var text = document.createElement('p');
6720
+ title.textContent = item.title;
6721
+
6722
+ if (!item.content && !item.title) {
6723
+ content.setAttribute('hidden', true);
6724
+ } else {
6725
+ var e = document.createElement('span');
6726
+ var lines = item.content.split(/(?:\r\n?|\n)/);
6727
+ for (i = 0, ii = lines.length; i < ii; ++i) {
6728
+ var line = lines[i];
6729
+ e.appendChild(document.createTextNode(line));
6730
+ if (i < (ii - 1)) {
6731
+ e.appendChild(document.createElement('br'));
6732
+ }
6733
+ }
6734
+ text.appendChild(e);
6735
+
6736
+ var pinned = false;
6737
+
6738
+ var showAnnotation = function showAnnotation(pin) {
6739
+ if (pin) {
6740
+ pinned = true;
6741
+ }
6742
+ if (content.hasAttribute('hidden')) {
6743
+ container.style.zIndex += 1;
6744
+ content.removeAttribute('hidden');
6745
+ }
6746
+ };
6747
+
6748
+ var hideAnnotation = function hideAnnotation(unpin) {
6749
+ if (unpin) {
6750
+ pinned = false;
6751
+ }
6752
+ if (!content.hasAttribute('hidden') && !pinned) {
6753
+ container.style.zIndex -= 1;
6754
+ content.setAttribute('hidden', true);
6755
+ }
6756
+ };
6757
+
6758
+ var toggleAnnotation = function toggleAnnotation() {
6759
+ if (pinned) {
6760
+ hideAnnotation(true);
6761
+ } else {
6762
+ showAnnotation(true);
6763
+ }
6764
+ };
6765
+
6766
+ image.addEventListener('click', function image_clickHandler() {
6767
+ toggleAnnotation();
6768
+ }, false);
6769
+ image.addEventListener('mouseover', function image_mouseOverHandler() {
6770
+ showAnnotation();
6771
+ }, false);
6772
+ image.addEventListener('mouseout', function image_mouseOutHandler() {
6773
+ hideAnnotation();
6774
+ }, false);
6775
+
6776
+ content.addEventListener('click', function content_clickHandler() {
6777
+ hideAnnotation(true);
6778
+ }, false);
6779
+ }
6780
+
6781
+ content.appendChild(title);
6782
+ content.appendChild(text);
6783
+ contentWrapper.appendChild(content);
6784
+ container.appendChild(image);
6785
+ container.appendChild(contentWrapper);
6786
+
6787
+ return container;
6788
+ }
6789
+
6790
+ function getHtmlElementForLinkAnnotation(item) {
6791
+ var container = initContainer(item, true);
6792
+ container.className = 'annotLink';
6793
+
6794
+ var link = document.createElement('a');
6795
+ link.href = link.title = item.url || '';
6796
+ if (item.url && PDFJS.openExternalLinksInNewWindow) {
6797
+ link.target = '_blank';
6798
+ }
6799
+
6800
+ container.appendChild(link);
6801
+
6802
+ return container;
6803
+ }
6804
+
6805
+ function getHtmlElement(data, objs) {
6806
+ switch (data.annotationType) {
6807
+ case AnnotationType.WIDGET:
6808
+ return getHtmlElementForTextWidgetAnnotation(data, objs);
6809
+ case AnnotationType.TEXT:
6810
+ return getHtmlElementForTextAnnotation(data);
6811
+ case AnnotationType.LINK:
6812
+ return getHtmlElementForLinkAnnotation(data);
6813
+ default:
6814
+ throw new Error('Unsupported annotationType: ' + data.annotationType);
6815
+ }
6816
+ }
6817
+
6818
+ return {
6819
+ getHtmlElement: getHtmlElement
6820
+ };
6821
+ })();
6822
+ PDFJS.AnnotationUtils = AnnotationUtils;
6823
+
6824
+
6825
+ var SVG_DEFAULTS = {
6826
+ fontStyle: 'normal',
6827
+ fontWeight: 'normal',
6828
+ fillColor: '#000000'
6829
+ };
6830
+
6831
+ var convertImgDataToPng = (function convertImgDataToPngClosure() {
6832
+ var PNG_HEADER =
6833
+ new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
6834
+
6835
+ var CHUNK_WRAPPER_SIZE = 12;
6836
+
6837
+ var crcTable = new Int32Array(256);
6838
+ for (var i = 0; i < 256; i++) {
6839
+ var c = i;
6840
+ for (var h = 0; h < 8; h++) {
6841
+ if (c & 1) {
6842
+ c = 0xedB88320 ^ ((c >> 1) & 0x7fffffff);
6843
+ } else {
6844
+ c = (c >> 1) & 0x7fffffff;
6845
+ }
6846
+ }
6847
+ crcTable[i] = c;
6848
+ }
6849
+
6850
+ function crc32(data, start, end) {
6851
+ var crc = -1;
6852
+ for (var i = start; i < end; i++) {
6853
+ var a = (crc ^ data[i]) & 0xff;
6854
+ var b = crcTable[a];
6855
+ crc = (crc >>> 8) ^ b;
6856
+ }
6857
+ return crc ^ -1;
6858
+ }
6859
+
6860
+ function writePngChunk(type, body, data, offset) {
6861
+ var p = offset;
6862
+ var len = body.length;
6863
+
6864
+ data[p] = len >> 24 & 0xff;
6865
+ data[p + 1] = len >> 16 & 0xff;
6866
+ data[p + 2] = len >> 8 & 0xff;
6867
+ data[p + 3] = len & 0xff;
6868
+ p += 4;
6869
+
6870
+ data[p] = type.charCodeAt(0) & 0xff;
6871
+ data[p + 1] = type.charCodeAt(1) & 0xff;
6872
+ data[p + 2] = type.charCodeAt(2) & 0xff;
6873
+ data[p + 3] = type.charCodeAt(3) & 0xff;
6874
+ p += 4;
6875
+
6876
+ data.set(body, p);
6877
+ p += body.length;
6878
+
6879
+ var crc = crc32(data, offset + 4, p);
6880
+
6881
+ data[p] = crc >> 24 & 0xff;
6882
+ data[p + 1] = crc >> 16 & 0xff;
6883
+ data[p + 2] = crc >> 8 & 0xff;
6884
+ data[p + 3] = crc & 0xff;
6885
+ }
6886
+
6887
+ function adler32(data, start, end) {
6888
+ var a = 1;
6889
+ var b = 0;
6890
+ for (var i = start; i < end; ++i) {
6891
+ a = (a + (data[i] & 0xff)) % 65521;
6892
+ b = (b + a) % 65521;
6893
+ }
6894
+ return (b << 16) | a;
6895
+ }
6896
+
6897
+ function encode(imgData, kind) {
6898
+ var width = imgData.width;
6899
+ var height = imgData.height;
6900
+ var bitDepth, colorType, lineSize;
6901
+ var bytes = imgData.data;
6902
+
6903
+ switch (kind) {
6904
+ case ImageKind.GRAYSCALE_1BPP:
6905
+ colorType = 0;
6906
+ bitDepth = 1;
6907
+ lineSize = (width + 7) >> 3;
6908
+ break;
6909
+ case ImageKind.RGB_24BPP:
6910
+ colorType = 2;
6911
+ bitDepth = 8;
6912
+ lineSize = width * 3;
6913
+ break;
6914
+ case ImageKind.RGBA_32BPP:
6915
+ colorType = 6;
6916
+ bitDepth = 8;
6917
+ lineSize = width * 4;
6918
+ break;
6919
+ default:
6920
+ throw new Error('invalid format');
6921
+ }
6922
+
6923
+ // prefix every row with predictor 0
6924
+ var literals = new Uint8Array((1 + lineSize) * height);
6925
+ var offsetLiterals = 0, offsetBytes = 0;
6926
+ var y, i;
6927
+ for (y = 0; y < height; ++y) {
6928
+ literals[offsetLiterals++] = 0; // no prediction
6929
+ literals.set(bytes.subarray(offsetBytes, offsetBytes + lineSize),
6930
+ offsetLiterals);
6931
+ offsetBytes += lineSize;
6932
+ offsetLiterals += lineSize;
6933
+ }
6934
+
6935
+ if (kind === ImageKind.GRAYSCALE_1BPP) {
6936
+ // inverting for B/W
6937
+ offsetLiterals = 0;
6938
+ for (y = 0; y < height; y++) {
6939
+ offsetLiterals++; // skipping predictor
6940
+ for (i = 0; i < lineSize; i++) {
6941
+ literals[offsetLiterals++] ^= 0xFF;
6942
+ }
6943
+ }
6944
+ }
6945
+
6946
+ var ihdr = new Uint8Array([
6947
+ width >> 24 & 0xff,
6948
+ width >> 16 & 0xff,
6949
+ width >> 8 & 0xff,
6950
+ width & 0xff,
6951
+ height >> 24 & 0xff,
6952
+ height >> 16 & 0xff,
6953
+ height >> 8 & 0xff,
6954
+ height & 0xff,
6955
+ bitDepth, // bit depth
6956
+ colorType, // color type
6957
+ 0x00, // compression method
6958
+ 0x00, // filter method
6959
+ 0x00 // interlace method
6960
+ ]);
6961
+
6962
+ var len = literals.length;
6963
+ var maxBlockLength = 0xFFFF;
6964
+
6965
+ var deflateBlocks = Math.ceil(len / maxBlockLength);
6966
+ var idat = new Uint8Array(2 + len + deflateBlocks * 5 + 4);
6967
+ var pi = 0;
6968
+ idat[pi++] = 0x78; // compression method and flags
6969
+ idat[pi++] = 0x9c; // flags
6970
+
6971
+ var pos = 0;
6972
+ while (len > maxBlockLength) {
6973
+ // writing non-final DEFLATE blocks type 0 and length of 65535
6974
+ idat[pi++] = 0x00;
6975
+ idat[pi++] = 0xff;
6976
+ idat[pi++] = 0xff;
6977
+ idat[pi++] = 0x00;
6978
+ idat[pi++] = 0x00;
6979
+ idat.set(literals.subarray(pos, pos + maxBlockLength), pi);
6980
+ pi += maxBlockLength;
6981
+ pos += maxBlockLength;
6982
+ len -= maxBlockLength;
6983
+ }
6984
+
6985
+ // writing non-final DEFLATE blocks type 0
6986
+ idat[pi++] = 0x01;
6987
+ idat[pi++] = len & 0xff;
6988
+ idat[pi++] = len >> 8 & 0xff;
6989
+ idat[pi++] = (~len & 0xffff) & 0xff;
6990
+ idat[pi++] = (~len & 0xffff) >> 8 & 0xff;
6991
+ idat.set(literals.subarray(pos), pi);
6992
+ pi += literals.length - pos;
6993
+
6994
+ var adler = adler32(literals, 0, literals.length); // checksum
6995
+ idat[pi++] = adler >> 24 & 0xff;
6996
+ idat[pi++] = adler >> 16 & 0xff;
6997
+ idat[pi++] = adler >> 8 & 0xff;
6998
+ idat[pi++] = adler & 0xff;
6999
+
7000
+ // PNG will consists: header, IHDR+data, IDAT+data, and IEND.
7001
+ var pngLength = PNG_HEADER.length + (CHUNK_WRAPPER_SIZE * 3) +
7002
+ ihdr.length + idat.length;
7003
+ var data = new Uint8Array(pngLength);
7004
+ var offset = 0;
7005
+ data.set(PNG_HEADER, offset);
7006
+ offset += PNG_HEADER.length;
7007
+ writePngChunk('IHDR', ihdr, data, offset);
7008
+ offset += CHUNK_WRAPPER_SIZE + ihdr.length;
7009
+ writePngChunk('IDATA', idat, data, offset);
7010
+ offset += CHUNK_WRAPPER_SIZE + idat.length;
7011
+ writePngChunk('IEND', new Uint8Array(0), data, offset);
7012
+
7013
+ return PDFJS.createObjectURL(data, 'image/png');
7014
+ }
7015
+
7016
+ return function convertImgDataToPng(imgData) {
7017
+ var kind = (imgData.kind === undefined ?
7018
+ ImageKind.GRAYSCALE_1BPP : imgData.kind);
7019
+ return encode(imgData, kind);
7020
+ };
7021
+ })();
7022
+
7023
+ var SVGExtraState = (function SVGExtraStateClosure() {
7024
+ function SVGExtraState() {
7025
+ this.fontSizeScale = 1;
7026
+ this.fontWeight = SVG_DEFAULTS.fontWeight;
7027
+ this.fontSize = 0;
7028
+
7029
+ this.textMatrix = IDENTITY_MATRIX;
7030
+ this.fontMatrix = FONT_IDENTITY_MATRIX;
7031
+ this.leading = 0;
7032
+
7033
+ // Current point (in user coordinates)
7034
+ this.x = 0;
7035
+ this.y = 0;
7036
+
7037
+ // Start of text line (in text coordinates)
7038
+ this.lineX = 0;
7039
+ this.lineY = 0;
7040
+
7041
+ // Character and word spacing
7042
+ this.charSpacing = 0;
7043
+ this.wordSpacing = 0;
7044
+ this.textHScale = 1;
7045
+ this.textRise = 0;
7046
+
7047
+ // Default foreground and background colors
7048
+ this.fillColor = SVG_DEFAULTS.fillColor;
7049
+ this.strokeColor = '#000000';
7050
+
7051
+ this.fillAlpha = 1;
7052
+ this.strokeAlpha = 1;
7053
+ this.lineWidth = 1;
7054
+ this.lineJoin = '';
7055
+ this.lineCap = '';
7056
+ this.miterLimit = 0;
7057
+
7058
+ this.dashArray = [];
7059
+ this.dashPhase = 0;
7060
+
7061
+ this.dependencies = [];
7062
+
7063
+ // Clipping
7064
+ this.clipId = '';
7065
+ this.pendingClip = false;
7066
+
7067
+ this.maskId = '';
7068
+ }
7069
+
7070
+ SVGExtraState.prototype = {
7071
+ clone: function SVGExtraState_clone() {
7072
+ return Object.create(this);
7073
+ },
7074
+ setCurrentPoint: function SVGExtraState_setCurrentPoint(x, y) {
7075
+ this.x = x;
7076
+ this.y = y;
7077
+ }
7078
+ };
7079
+ return SVGExtraState;
7080
+ })();
7081
+
7082
+ var SVGGraphics = (function SVGGraphicsClosure() {
7083
+ function createScratchSVG(width, height) {
7084
+ var NS = 'http://www.w3.org/2000/svg';
7085
+ var svg = document.createElementNS(NS, 'svg:svg');
7086
+ svg.setAttributeNS(null, 'version', '1.1');
7087
+ svg.setAttributeNS(null, 'width', width + 'px');
7088
+ svg.setAttributeNS(null, 'height', height + 'px');
7089
+ svg.setAttributeNS(null, 'viewBox', '0 0 ' + width + ' ' + height);
7090
+ return svg;
7091
+ }
7092
+
7093
+ function opListToTree(opList) {
7094
+ var opTree = [];
7095
+ var tmp = [];
7096
+ var opListLen = opList.length;
7097
+
7098
+ for (var x = 0; x < opListLen; x++) {
7099
+ if (opList[x].fn === 'save') {
7100
+ opTree.push({'fnId': 92, 'fn': 'group', 'items': []});
7101
+ tmp.push(opTree);
7102
+ opTree = opTree[opTree.length - 1].items;
7103
+ continue;
7104
+ }
7105
+
7106
+ if(opList[x].fn === 'restore') {
7107
+ opTree = tmp.pop();
7108
+ } else {
7109
+ opTree.push(opList[x]);
7110
+ }
7111
+ }
7112
+ return opTree;
7113
+ }
7114
+
7115
+ /**
7116
+ * Formats float number.
7117
+ * @param value {number} number to format.
7118
+ * @returns {string}
7119
+ */
7120
+ function pf(value) {
7121
+ if (value === (value | 0)) { // integer number
7122
+ return value.toString();
7123
+ }
7124
+ var s = value.toFixed(10);
7125
+ var i = s.length - 1;
7126
+ if (s[i] !== '0') {
7127
+ return s;
7128
+ }
7129
+ // removing trailing zeros
7130
+ do {
7131
+ i--;
7132
+ } while (s[i] === '0');
7133
+ return s.substr(0, s[i] === '.' ? i : i + 1);
7134
+ }
7135
+
7136
+ /**
7137
+ * Formats transform matrix. The standard rotation, scale and translate
7138
+ * matrices are replaced by their shorter forms, and for identity matrix
7139
+ * returns empty string to save the memory.
7140
+ * @param m {Array} matrix to format.
7141
+ * @returns {string}
7142
+ */
7143
+ function pm(m) {
7144
+ if (m[4] === 0 && m[5] === 0) {
7145
+ if (m[1] === 0 && m[2] === 0) {
7146
+ if (m[0] === 1 && m[3] === 1) {
7147
+ return '';
7148
+ }
7149
+ return 'scale(' + pf(m[0]) + ' ' + pf(m[3]) + ')';
7150
+ }
7151
+ if (m[0] === m[3] && m[1] === -m[2]) {
7152
+ var a = Math.acos(m[0]) * 180 / Math.PI;
7153
+ return 'rotate(' + pf(a) + ')';
7154
+ }
7155
+ } else {
7156
+ if (m[0] === 1 && m[1] === 0 && m[2] === 0 && m[3] === 1) {
7157
+ return 'translate(' + pf(m[4]) + ' ' + pf(m[5]) + ')';
7158
+ }
7159
+ }
7160
+ return 'matrix(' + pf(m[0]) + ' ' + pf(m[1]) + ' ' + pf(m[2]) + ' ' +
7161
+ pf(m[3]) + ' ' + pf(m[4]) + ' ' + pf(m[5]) + ')';
7162
+ }
7163
+
7164
+ function SVGGraphics(commonObjs, objs) {
7165
+ this.current = new SVGExtraState();
7166
+ this.transformMatrix = IDENTITY_MATRIX; // Graphics state matrix
7167
+ this.transformStack = [];
7168
+ this.extraStack = [];
7169
+ this.commonObjs = commonObjs;
7170
+ this.objs = objs;
7171
+ this.pendingEOFill = false;
7172
+
7173
+ this.embedFonts = false;
7174
+ this.embeddedFonts = {};
7175
+ this.cssStyle = null;
7176
+ }
7177
+
7178
+ var NS = 'http://www.w3.org/2000/svg';
7179
+ var XML_NS = 'http://www.w3.org/XML/1998/namespace';
7180
+ var XLINK_NS = 'http://www.w3.org/1999/xlink';
7181
+ var LINE_CAP_STYLES = ['butt', 'round', 'square'];
7182
+ var LINE_JOIN_STYLES = ['miter', 'round', 'bevel'];
7183
+ var clipCount = 0;
7184
+ var maskCount = 0;
7185
+
7186
+ SVGGraphics.prototype = {
7187
+ save: function SVGGraphics_save() {
7188
+ this.transformStack.push(this.transformMatrix);
7189
+ var old = this.current;
7190
+ this.extraStack.push(old);
7191
+ this.current = old.clone();
7192
+ },
7193
+
7194
+ restore: function SVGGraphics_restore() {
7195
+ this.transformMatrix = this.transformStack.pop();
7196
+ this.current = this.extraStack.pop();
7197
+
7198
+ this.tgrp = document.createElementNS(NS, 'svg:g');
7199
+ this.tgrp.setAttributeNS(null, 'transform', pm(this.transformMatrix));
7200
+ this.pgrp.appendChild(this.tgrp);
7201
+ },
7202
+
7203
+ group: function SVGGraphics_group(items) {
7204
+ this.save();
7205
+ this.executeOpTree(items);
7206
+ this.restore();
7207
+ },
7208
+
7209
+ loadDependencies: function SVGGraphics_loadDependencies(operatorList) {
7210
+ var fnArray = operatorList.fnArray;
7211
+ var fnArrayLen = fnArray.length;
7212
+ var argsArray = operatorList.argsArray;
7213
+
7214
+ var self = this;
7215
+ for (var i = 0; i < fnArrayLen; i++) {
7216
+ if (OPS.dependency === fnArray[i]) {
7217
+ var deps = argsArray[i];
7218
+ for (var n = 0, nn = deps.length; n < nn; n++) {
7219
+ var obj = deps[n];
7220
+ var common = obj.substring(0, 2) === 'g_';
7221
+ var promise;
7222
+ if (common) {
7223
+ promise = new Promise(function(resolve) {
7224
+ self.commonObjs.get(obj, resolve);
7225
+ });
7226
+ } else {
7227
+ promise = new Promise(function(resolve) {
7228
+ self.objs.get(obj, resolve);
7229
+ });
7230
+ }
7231
+ this.current.dependencies.push(promise);
7232
+ }
7233
+ }
7234
+ }
7235
+ return Promise.all(this.current.dependencies);
7236
+ },
7237
+
7238
+ transform: function SVGGraphics_transform(a, b, c, d, e, f) {
7239
+ var transformMatrix = [a, b, c, d, e, f];
7240
+ this.transformMatrix = PDFJS.Util.transform(this.transformMatrix,
7241
+ transformMatrix);
7242
+
7243
+ this.tgrp = document.createElementNS(NS, 'svg:g');
7244
+ this.tgrp.setAttributeNS(null, 'transform', pm(this.transformMatrix));
7245
+ },
7246
+
7247
+ getSVG: function SVGGraphics_getSVG(operatorList, viewport) {
7248
+ this.svg = createScratchSVG(viewport.width, viewport.height);
7249
+ this.viewport = viewport;
7250
+
7251
+ return this.loadDependencies(operatorList).then(function () {
7252
+ this.transformMatrix = IDENTITY_MATRIX;
7253
+ this.pgrp = document.createElementNS(NS, 'svg:g'); // Parent group
7254
+ this.pgrp.setAttributeNS(null, 'transform', pm(viewport.transform));
7255
+ this.tgrp = document.createElementNS(NS, 'svg:g'); // Transform group
7256
+ this.tgrp.setAttributeNS(null, 'transform', pm(this.transformMatrix));
7257
+ this.defs = document.createElementNS(NS, 'svg:defs');
7258
+ this.pgrp.appendChild(this.defs);
7259
+ this.pgrp.appendChild(this.tgrp);
7260
+ this.svg.appendChild(this.pgrp);
7261
+ var opTree = this.convertOpList(operatorList);
7262
+ this.executeOpTree(opTree);
7263
+ return this.svg;
7264
+ }.bind(this));
7265
+ },
7266
+
7267
+ convertOpList: function SVGGraphics_convertOpList(operatorList) {
7268
+ var argsArray = operatorList.argsArray;
7269
+ var fnArray = operatorList.fnArray;
7270
+ var fnArrayLen = fnArray.length;
7271
+ var REVOPS = [];
7272
+ var opList = [];
7273
+
7274
+ for (var op in OPS) {
7275
+ REVOPS[OPS[op]] = op;
7276
+ }
7277
+
7278
+ for (var x = 0; x < fnArrayLen; x++) {
7279
+ var fnId = fnArray[x];
7280
+ opList.push({'fnId' : fnId, 'fn': REVOPS[fnId], 'args': argsArray[x]});
7281
+ }
7282
+ return opListToTree(opList);
7283
+ },
7284
+
7285
+ executeOpTree: function SVGGraphics_executeOpTree(opTree) {
7286
+ var opTreeLen = opTree.length;
7287
+ for(var x = 0; x < opTreeLen; x++) {
7288
+ var fn = opTree[x].fn;
7289
+ var fnId = opTree[x].fnId;
7290
+ var args = opTree[x].args;
7291
+
7292
+ switch (fnId | 0) {
7293
+ case OPS.beginText:
7294
+ this.beginText();
7295
+ break;
7296
+ case OPS.setLeading:
7297
+ this.setLeading(args);
7298
+ break;
7299
+ case OPS.setLeadingMoveText:
7300
+ this.setLeadingMoveText(args[0], args[1]);
7301
+ break;
7302
+ case OPS.setFont:
7303
+ this.setFont(args);
7304
+ break;
7305
+ case OPS.showText:
7306
+ this.showText(args[0]);
7307
+ break;
7308
+ case OPS.showSpacedText:
7309
+ this.showText(args[0]);
7310
+ break;
7311
+ case OPS.endText:
7312
+ this.endText();
7313
+ break;
7314
+ case OPS.moveText:
7315
+ this.moveText(args[0], args[1]);
7316
+ break;
7317
+ case OPS.setCharSpacing:
7318
+ this.setCharSpacing(args[0]);
7319
+ break;
7320
+ case OPS.setWordSpacing:
7321
+ this.setWordSpacing(args[0]);
7322
+ break;
7323
+ case OPS.setHScale:
7324
+ this.setHScale(args[0]);
7325
+ break;
7326
+ case OPS.setTextMatrix:
7327
+ this.setTextMatrix(args[0], args[1], args[2],
7328
+ args[3], args[4], args[5]);
7329
+ break;
7330
+ case OPS.setLineWidth:
7331
+ this.setLineWidth(args[0]);
7332
+ break;
7333
+ case OPS.setLineJoin:
7334
+ this.setLineJoin(args[0]);
7335
+ break;
7336
+ case OPS.setLineCap:
7337
+ this.setLineCap(args[0]);
7338
+ break;
7339
+ case OPS.setMiterLimit:
7340
+ this.setMiterLimit(args[0]);
7341
+ break;
7342
+ case OPS.setFillRGBColor:
7343
+ this.setFillRGBColor(args[0], args[1], args[2]);
7344
+ break;
7345
+ case OPS.setStrokeRGBColor:
7346
+ this.setStrokeRGBColor(args[0], args[1], args[2]);
7347
+ break;
7348
+ case OPS.setDash:
7349
+ this.setDash(args[0], args[1]);
7350
+ break;
7351
+ case OPS.setGState:
7352
+ this.setGState(args[0]);
7353
+ break;
7354
+ case OPS.fill:
7355
+ this.fill();
7356
+ break;
7357
+ case OPS.eoFill:
7358
+ this.eoFill();
7359
+ break;
7360
+ case OPS.stroke:
7361
+ this.stroke();
7362
+ break;
7363
+ case OPS.fillStroke:
7364
+ this.fillStroke();
7365
+ break;
7366
+ case OPS.eoFillStroke:
7367
+ this.eoFillStroke();
7368
+ break;
7369
+ case OPS.clip:
7370
+ this.clip('nonzero');
7371
+ break;
7372
+ case OPS.eoClip:
7373
+ this.clip('evenodd');
7374
+ break;
7375
+ case OPS.paintSolidColorImageMask:
7376
+ this.paintSolidColorImageMask();
7377
+ break;
7378
+ case OPS.paintJpegXObject:
7379
+ this.paintJpegXObject(args[0], args[1], args[2]);
7380
+ break;
7381
+ case OPS.paintImageXObject:
7382
+ this.paintImageXObject(args[0]);
7383
+ break;
7384
+ case OPS.paintInlineImageXObject:
7385
+ this.paintInlineImageXObject(args[0]);
7386
+ break;
7387
+ case OPS.paintImageMaskXObject:
7388
+ this.paintImageMaskXObject(args[0]);
7389
+ break;
7390
+ case OPS.paintFormXObjectBegin:
7391
+ this.paintFormXObjectBegin(args[0], args[1]);
7392
+ break;
7393
+ case OPS.paintFormXObjectEnd:
7394
+ this.paintFormXObjectEnd();
7395
+ break;
7396
+ case OPS.closePath:
7397
+ this.closePath();
7398
+ break;
7399
+ case OPS.closeStroke:
7400
+ this.closeStroke();
7401
+ break;
7402
+ case OPS.closeFillStroke:
7403
+ this.closeFillStroke();
7404
+ break;
7405
+ case OPS.nextLine:
7406
+ this.nextLine();
7407
+ break;
7408
+ case OPS.transform:
7409
+ this.transform(args[0], args[1], args[2], args[3],
7410
+ args[4], args[5]);
7411
+ break;
7412
+ case OPS.constructPath:
7413
+ this.constructPath(args[0], args[1]);
7414
+ break;
7415
+ case OPS.endPath:
7416
+ this.endPath();
7417
+ break;
7418
+ case 92:
7419
+ this.group(opTree[x].items);
7420
+ break;
7421
+ default:
7422
+ warn('Unimplemented method '+ fn);
7423
+ break;
7424
+ }
7425
+ }
7426
+ },
7427
+
7428
+ setWordSpacing: function SVGGraphics_setWordSpacing(wordSpacing) {
7429
+ this.current.wordSpacing = wordSpacing;
7430
+ },
7431
+
7432
+ setCharSpacing: function SVGGraphics_setCharSpacing(charSpacing) {
7433
+ this.current.charSpacing = charSpacing;
7434
+ },
7435
+
7436
+ nextLine: function SVGGraphics_nextLine() {
7437
+ this.moveText(0, this.current.leading);
7438
+ },
7439
+
7440
+ setTextMatrix: function SVGGraphics_setTextMatrix(a, b, c, d, e, f) {
7441
+ var current = this.current;
7442
+ this.current.textMatrix = this.current.lineMatrix = [a, b, c, d, e, f];
7443
+
7444
+ this.current.x = this.current.lineX = 0;
7445
+ this.current.y = this.current.lineY = 0;
7446
+
7447
+ current.xcoords = [];
7448
+ current.tspan = document.createElementNS(NS, 'svg:tspan');
7449
+ current.tspan.setAttributeNS(null, 'font-family', current.fontFamily);
7450
+ current.tspan.setAttributeNS(null, 'font-size',
7451
+ pf(current.fontSize) + 'px');
7452
+ current.tspan.setAttributeNS(null, 'y', pf(-current.y));
7453
+
7454
+ current.txtElement = document.createElementNS(NS, 'svg:text');
7455
+ current.txtElement.appendChild(current.tspan);
7456
+ },
7457
+
7458
+ beginText: function SVGGraphics_beginText() {
7459
+ this.current.x = this.current.lineX = 0;
7460
+ this.current.y = this.current.lineY = 0;
7461
+ this.current.textMatrix = IDENTITY_MATRIX;
7462
+ this.current.lineMatrix = IDENTITY_MATRIX;
7463
+ this.current.tspan = document.createElementNS(NS, 'svg:tspan');
7464
+ this.current.txtElement = document.createElementNS(NS, 'svg:text');
7465
+ this.current.txtgrp = document.createElementNS(NS, 'svg:g');
7466
+ this.current.xcoords = [];
7467
+ },
7468
+
7469
+ moveText: function SVGGraphics_moveText(x, y) {
7470
+ var current = this.current;
7471
+ this.current.x = this.current.lineX += x;
7472
+ this.current.y = this.current.lineY += y;
7473
+
7474
+ current.xcoords = [];
7475
+ current.tspan = document.createElementNS(NS, 'svg:tspan');
7476
+ current.tspan.setAttributeNS(null, 'font-family', current.fontFamily);
7477
+ current.tspan.setAttributeNS(null, 'font-size',
7478
+ pf(current.fontSize) + 'px');
7479
+ current.tspan.setAttributeNS(null, 'y', pf(-current.y));
7480
+ },
7481
+
7482
+ showText: function SVGGraphics_showText(glyphs) {
7483
+ var current = this.current;
7484
+ var font = current.font;
7485
+ var fontSize = current.fontSize;
7486
+
7487
+ if (fontSize === 0) {
7488
+ return;
7489
+ }
7490
+
7491
+ var charSpacing = current.charSpacing;
7492
+ var wordSpacing = current.wordSpacing;
7493
+ var fontDirection = current.fontDirection;
7494
+ var textHScale = current.textHScale * fontDirection;
7495
+ var glyphsLength = glyphs.length;
7496
+ var vertical = font.vertical;
7497
+ var widthAdvanceScale = fontSize * current.fontMatrix[0];
7498
+
7499
+ var x = 0, i;
7500
+ for (i = 0; i < glyphsLength; ++i) {
7501
+ var glyph = glyphs[i];
7502
+ if (glyph === null) {
7503
+ // word break
7504
+ x += fontDirection * wordSpacing;
7505
+ continue;
7506
+ } else if (isNum(glyph)) {
7507
+ x += -glyph * fontSize * 0.001;
7508
+ continue;
7509
+ }
7510
+ current.xcoords.push(current.x + x * textHScale);
7511
+
7512
+ var width = glyph.width;
7513
+ var character = glyph.fontChar;
7514
+ var charWidth = width * widthAdvanceScale + charSpacing * fontDirection;
7515
+ x += charWidth;
7516
+
7517
+ current.tspan.textContent += character;
7518
+ }
7519
+ if (vertical) {
7520
+ current.y -= x * textHScale;
7521
+ } else {
7522
+ current.x += x * textHScale;
7523
+ }
7524
+
7525
+ current.tspan.setAttributeNS(null, 'x',
7526
+ current.xcoords.map(pf).join(' '));
7527
+ current.tspan.setAttributeNS(null, 'y', pf(-current.y));
7528
+ current.tspan.setAttributeNS(null, 'font-family', current.fontFamily);
7529
+ current.tspan.setAttributeNS(null, 'font-size',
7530
+ pf(current.fontSize) + 'px');
7531
+ if (current.fontStyle !== SVG_DEFAULTS.fontStyle) {
7532
+ current.tspan.setAttributeNS(null, 'font-style', current.fontStyle);
7533
+ }
7534
+ if (current.fontWeight !== SVG_DEFAULTS.fontWeight) {
7535
+ current.tspan.setAttributeNS(null, 'font-weight', current.fontWeight);
7536
+ }
7537
+ if (current.fillColor !== SVG_DEFAULTS.fillColor) {
7538
+ current.tspan.setAttributeNS(null, 'fill', current.fillColor);
7539
+ }
7540
+
7541
+ current.txtElement.setAttributeNS(null, 'transform',
7542
+ pm(current.textMatrix) +
7543
+ ' scale(1, -1)' );
7544
+ current.txtElement.setAttributeNS(XML_NS, 'xml:space', 'preserve');
7545
+ current.txtElement.appendChild(current.tspan);
7546
+ current.txtgrp.appendChild(current.txtElement);
7547
+
7548
+ this.tgrp.appendChild(current.txtElement);
7549
+
7550
+ },
7551
+
7552
+ setLeadingMoveText: function SVGGraphics_setLeadingMoveText(x, y) {
7553
+ this.setLeading(-y);
7554
+ this.moveText(x, y);
7555
+ },
7556
+
7557
+ addFontStyle: function SVGGraphics_addFontStyle(fontObj) {
7558
+ if (!this.cssStyle) {
7559
+ this.cssStyle = document.createElementNS(NS, 'svg:style');
7560
+ this.cssStyle.setAttributeNS(null, 'type', 'text/css');
7561
+ this.defs.appendChild(this.cssStyle);
7562
+ }
7563
+
7564
+ var url = PDFJS.createObjectURL(fontObj.data, fontObj.mimetype);
7565
+ this.cssStyle.textContent +=
7566
+ '@font-face { font-family: "' + fontObj.loadedName + '";' +
7567
+ ' src: url(' + url + '); }\n';
7568
+ },
7569
+
7570
+ setFont: function SVGGraphics_setFont(details) {
7571
+ var current = this.current;
7572
+ var fontObj = this.commonObjs.get(details[0]);
7573
+ var size = details[1];
7574
+ this.current.font = fontObj;
7575
+
7576
+ if (this.embedFonts && fontObj.data &&
7577
+ !this.embeddedFonts[fontObj.loadedName]) {
7578
+ this.addFontStyle(fontObj);
7579
+ this.embeddedFonts[fontObj.loadedName] = fontObj;
7580
+ }
7581
+
7582
+ current.fontMatrix = (fontObj.fontMatrix ?
7583
+ fontObj.fontMatrix : FONT_IDENTITY_MATRIX);
7584
+
7585
+ var bold = fontObj.black ? (fontObj.bold ? 'bolder' : 'bold') :
7586
+ (fontObj.bold ? 'bold' : 'normal');
7587
+ var italic = fontObj.italic ? 'italic' : 'normal';
7588
+
7589
+ if (size < 0) {
7590
+ size = -size;
7591
+ current.fontDirection = -1;
7592
+ } else {
7593
+ current.fontDirection = 1;
7594
+ }
7595
+ current.fontSize = size;
7596
+ current.fontFamily = fontObj.loadedName;
7597
+ current.fontWeight = bold;
7598
+ current.fontStyle = italic;
7599
+
7600
+ current.tspan = document.createElementNS(NS, 'svg:tspan');
7601
+ current.tspan.setAttributeNS(null, 'y', pf(-current.y));
7602
+ current.xcoords = [];
7603
+ },
7604
+
7605
+ endText: function SVGGraphics_endText() {
7606
+ if (this.current.pendingClip) {
7607
+ this.cgrp.appendChild(this.tgrp);
7608
+ this.pgrp.appendChild(this.cgrp);
7609
+ } else {
7610
+ this.pgrp.appendChild(this.tgrp);
7611
+ }
7612
+ this.tgrp = document.createElementNS(NS, 'svg:g');
7613
+ this.tgrp.setAttributeNS(null, 'transform', pm(this.transformMatrix));
7614
+ },
7615
+
7616
+ // Path properties
7617
+ setLineWidth: function SVGGraphics_setLineWidth(width) {
7618
+ this.current.lineWidth = width;
7619
+ },
7620
+ setLineCap: function SVGGraphics_setLineCap(style) {
7621
+ this.current.lineCap = LINE_CAP_STYLES[style];
7622
+ },
7623
+ setLineJoin: function SVGGraphics_setLineJoin(style) {
7624
+ this.current.lineJoin = LINE_JOIN_STYLES[style];
7625
+ },
7626
+ setMiterLimit: function SVGGraphics_setMiterLimit(limit) {
7627
+ this.current.miterLimit = limit;
7628
+ },
7629
+ setStrokeRGBColor: function SVGGraphics_setStrokeRGBColor(r, g, b) {
7630
+ var color = Util.makeCssRgb(r, g, b);
7631
+ this.current.strokeColor = color;
7632
+ },
7633
+ setFillRGBColor: function SVGGraphics_setFillRGBColor(r, g, b) {
7634
+ var color = Util.makeCssRgb(r, g, b);
7635
+ this.current.fillColor = color;
7636
+ this.current.tspan = document.createElementNS(NS, 'svg:tspan');
7637
+ this.current.xcoords = [];
7638
+ },
7639
+ setDash: function SVGGraphics_setDash(dashArray, dashPhase) {
7640
+ this.current.dashArray = dashArray;
7641
+ this.current.dashPhase = dashPhase;
7642
+ },
7643
+
7644
+ constructPath: function SVGGraphics_constructPath(ops, args) {
7645
+ var current = this.current;
7646
+ var x = current.x, y = current.y;
7647
+ current.path = document.createElementNS(NS, 'svg:path');
7648
+ var d = [];
7649
+ var opLength = ops.length;
7650
+
7651
+ for (var i = 0, j = 0; i < opLength; i++) {
7652
+ switch (ops[i] | 0) {
7653
+ case OPS.rectangle:
7654
+ x = args[j++];
7655
+ y = args[j++];
7656
+ var width = args[j++];
7657
+ var height = args[j++];
7658
+ var xw = x + width;
7659
+ var yh = y + height;
7660
+ d.push('M', pf(x), pf(y), 'L', pf(xw) , pf(y), 'L', pf(xw), pf(yh),
7661
+ 'L', pf(x), pf(yh), 'Z');
7662
+ break;
7663
+ case OPS.moveTo:
7664
+ x = args[j++];
7665
+ y = args[j++];
7666
+ d.push('M', pf(x), pf(y));
7667
+ break;
7668
+ case OPS.lineTo:
7669
+ x = args[j++];
7670
+ y = args[j++];
7671
+ d.push('L', pf(x) , pf(y));
7672
+ break;
7673
+ case OPS.curveTo:
7674
+ x = args[j + 4];
7675
+ y = args[j + 5];
7676
+ d.push('C', pf(args[j]), pf(args[j + 1]), pf(args[j + 2]),
7677
+ pf(args[j + 3]), pf(x), pf(y));
7678
+ j += 6;
7679
+ break;
7680
+ case OPS.curveTo2:
7681
+ x = args[j + 2];
7682
+ y = args[j + 3];
7683
+ d.push('C', pf(x), pf(y), pf(args[j]), pf(args[j + 1]),
7684
+ pf(args[j + 2]), pf(args[j + 3]));
7685
+ j += 4;
7686
+ break;
7687
+ case OPS.curveTo3:
7688
+ x = args[j + 2];
7689
+ y = args[j + 3];
7690
+ d.push('C', pf(args[j]), pf(args[j + 1]), pf(x), pf(y),
7691
+ pf(x), pf(y));
7692
+ j += 4;
7693
+ break;
7694
+ case OPS.closePath:
7695
+ d.push('Z');
7696
+ break;
7697
+ }
7698
+ }
7699
+ current.path.setAttributeNS(null, 'd', d.join(' '));
7700
+ current.path.setAttributeNS(null, 'stroke-miterlimit',
7701
+ pf(current.miterLimit));
7702
+ current.path.setAttributeNS(null, 'stroke-linecap', current.lineCap);
7703
+ current.path.setAttributeNS(null, 'stroke-linejoin', current.lineJoin);
7704
+ current.path.setAttributeNS(null, 'stroke-width',
7705
+ pf(current.lineWidth) + 'px');
7706
+ current.path.setAttributeNS(null, 'stroke-dasharray',
7707
+ current.dashArray.map(pf).join(' '));
7708
+ current.path.setAttributeNS(null, 'stroke-dashoffset',
7709
+ pf(current.dashPhase) + 'px');
7710
+ current.path.setAttributeNS(null, 'fill', 'none');
7711
+
7712
+ this.tgrp.appendChild(current.path);
7713
+ if (current.pendingClip) {
7714
+ this.cgrp.appendChild(this.tgrp);
7715
+ this.pgrp.appendChild(this.cgrp);
7716
+ } else {
7717
+ this.pgrp.appendChild(this.tgrp);
7718
+ }
7719
+ // Saving a reference in current.element so that it can be addressed
7720
+ // in 'fill' and 'stroke'
7721
+ current.element = current.path;
7722
+ current.setCurrentPoint(x, y);
7723
+ },
7724
+
7725
+ endPath: function SVGGraphics_endPath() {
7726
+ var current = this.current;
7727
+ if (current.pendingClip) {
7728
+ this.cgrp.appendChild(this.tgrp);
7729
+ this.pgrp.appendChild(this.cgrp);
7730
+ } else {
7731
+ this.pgrp.appendChild(this.tgrp);
7732
+ }
7733
+ this.tgrp = document.createElementNS(NS, 'svg:g');
7734
+ this.tgrp.setAttributeNS(null, 'transform', pm(this.transformMatrix));
7735
+ },
7736
+
7737
+ clip: function SVGGraphics_clip(type) {
7738
+ var current = this.current;
7739
+ // Add current path to clipping path
7740
+ current.clipId = 'clippath' + clipCount;
7741
+ clipCount++;
7742
+ this.clippath = document.createElementNS(NS, 'svg:clipPath');
7743
+ this.clippath.setAttributeNS(null, 'id', current.clipId);
7744
+ var clipElement = current.element.cloneNode();
7745
+ if (type === 'evenodd') {
7746
+ clipElement.setAttributeNS(null, 'clip-rule', 'evenodd');
7747
+ } else {
7748
+ clipElement.setAttributeNS(null, 'clip-rule', 'nonzero');
7749
+ }
7750
+ this.clippath.setAttributeNS(null, 'transform', pm(this.transformMatrix));
7751
+ this.clippath.appendChild(clipElement);
7752
+ this.defs.appendChild(this.clippath);
7753
+
7754
+ // Create a new group with that attribute
7755
+ current.pendingClip = true;
7756
+ this.cgrp = document.createElementNS(NS, 'svg:g');
7757
+ this.cgrp.setAttributeNS(null, 'clip-path',
7758
+ 'url(#' + current.clipId + ')');
7759
+ this.pgrp.appendChild(this.cgrp);
7760
+ },
7761
+
7762
+ closePath: function SVGGraphics_closePath() {
7763
+ var current = this.current;
7764
+ var d = current.path.getAttributeNS(null, 'd');
7765
+ d += 'Z';
7766
+ current.path.setAttributeNS(null, 'd', d);
7767
+ },
7768
+
7769
+ setLeading: function SVGGraphics_setLeading(leading) {
7770
+ this.current.leading = -leading;
7771
+ },
7772
+
7773
+ setTextRise: function SVGGraphics_setTextRise(textRise) {
7774
+ this.current.textRise = textRise;
7775
+ },
7776
+
7777
+ setHScale: function SVGGraphics_setHScale(scale) {
7778
+ this.current.textHScale = scale / 100;
7779
+ },
7780
+
7781
+ setGState: function SVGGraphics_setGState(states) {
7782
+ for (var i = 0, ii = states.length; i < ii; i++) {
7783
+ var state = states[i];
7784
+ var key = state[0];
7785
+ var value = state[1];
7786
+
7787
+ switch (key) {
7788
+ case 'LW':
7789
+ this.setLineWidth(value);
7790
+ break;
7791
+ case 'LC':
7792
+ this.setLineCap(value);
7793
+ break;
7794
+ case 'LJ':
7795
+ this.setLineJoin(value);
7796
+ break;
7797
+ case 'ML':
7798
+ this.setMiterLimit(value);
7799
+ break;
7800
+ case 'D':
7801
+ this.setDash(value[0], value[1]);
7802
+ break;
7803
+ case 'RI':
7804
+ break;
7805
+ case 'FL':
7806
+ break;
7807
+ case 'Font':
7808
+ this.setFont(value);
7809
+ break;
7810
+ case 'CA':
7811
+ break;
7812
+ case 'ca':
7813
+ break;
7814
+ case 'BM':
7815
+ break;
7816
+ case 'SMask':
7817
+ break;
7818
+ }
7819
+ }
7820
+ },
7821
+
7822
+ fill: function SVGGraphics_fill() {
7823
+ var current = this.current;
7824
+ current.element.setAttributeNS(null, 'fill', current.fillColor);
7825
+ },
7826
+
7827
+ stroke: function SVGGraphics_stroke() {
7828
+ var current = this.current;
7829
+ current.element.setAttributeNS(null, 'stroke', current.strokeColor);
7830
+ current.element.setAttributeNS(null, 'fill', 'none');
7831
+ },
7832
+
7833
+ eoFill: function SVGGraphics_eoFill() {
7834
+ var current = this.current;
7835
+ current.element.setAttributeNS(null, 'fill', current.fillColor);
7836
+ current.element.setAttributeNS(null, 'fill-rule', 'evenodd');
7837
+ },
7838
+
7839
+ fillStroke: function SVGGraphics_fillStroke() {
7840
+ // Order is important since stroke wants fill to be none.
7841
+ // First stroke, then if fill needed, it will be overwritten.
7842
+ this.stroke();
7843
+ this.fill();
7844
+ },
7845
+
7846
+ eoFillStroke: function SVGGraphics_eoFillStroke() {
7847
+ this.current.element.setAttributeNS(null, 'fill-rule', 'evenodd');
7848
+ this.fillStroke();
7849
+ },
7850
+
7851
+ closeStroke: function SVGGraphics_closeStroke() {
7852
+ this.closePath();
7853
+ this.stroke();
7854
+ },
7855
+
7856
+ closeFillStroke: function SVGGraphics_closeFillStroke() {
7857
+ this.closePath();
7858
+ this.fillStroke();
7859
+ },
7860
+
7861
+ paintSolidColorImageMask:
7862
+ function SVGGraphics_paintSolidColorImageMask() {
7863
+ var current = this.current;
7864
+ var rect = document.createElementNS(NS, 'svg:rect');
7865
+ rect.setAttributeNS(null, 'x', '0');
7866
+ rect.setAttributeNS(null, 'y', '0');
7867
+ rect.setAttributeNS(null, 'width', '1px');
7868
+ rect.setAttributeNS(null, 'height', '1px');
7869
+ rect.setAttributeNS(null, 'fill', current.fillColor);
7870
+ this.tgrp.appendChild(rect);
7871
+ },
7872
+
7873
+ paintJpegXObject: function SVGGraphics_paintJpegXObject(objId, w, h) {
7874
+ var current = this.current;
7875
+ var imgObj = this.objs.get(objId);
7876
+ var imgEl = document.createElementNS(NS, 'svg:image');
7877
+ imgEl.setAttributeNS(XLINK_NS, 'xlink:href', imgObj.src);
7878
+ imgEl.setAttributeNS(null, 'width', imgObj.width + 'px');
7879
+ imgEl.setAttributeNS(null, 'height', imgObj.height + 'px');
7880
+ imgEl.setAttributeNS(null, 'x', '0');
7881
+ imgEl.setAttributeNS(null, 'y', pf(-h));
7882
+ imgEl.setAttributeNS(null, 'transform',
7883
+ 'scale(' + pf(1 / w) + ' ' + pf(-1 / h) + ')');
7884
+
7885
+ this.tgrp.appendChild(imgEl);
7886
+ if (current.pendingClip) {
7887
+ this.cgrp.appendChild(this.tgrp);
7888
+ this.pgrp.appendChild(this.cgrp);
7889
+ } else {
7890
+ this.pgrp.appendChild(this.tgrp);
7891
+ }
7892
+ },
7893
+
7894
+ paintImageXObject: function SVGGraphics_paintImageXObject(objId) {
7895
+ var imgData = this.objs.get(objId);
7896
+ if (!imgData) {
7897
+ warn('Dependent image isn\'t ready yet');
7898
+ return;
7899
+ }
7900
+ this.paintInlineImageXObject(imgData);
7901
+ },
7902
+
7903
+ paintInlineImageXObject:
7904
+ function SVGGraphics_paintInlineImageXObject(imgData, mask) {
7905
+ var current = this.current;
7906
+ var width = imgData.width;
7907
+ var height = imgData.height;
7908
+
7909
+ var imgSrc = convertImgDataToPng(imgData);
7910
+ var cliprect = document.createElementNS(NS, 'svg:rect');
7911
+ cliprect.setAttributeNS(null, 'x', '0');
7912
+ cliprect.setAttributeNS(null, 'y', '0');
7913
+ cliprect.setAttributeNS(null, 'width', pf(width));
7914
+ cliprect.setAttributeNS(null, 'height', pf(height));
7915
+ current.element = cliprect;
7916
+ this.clip('nonzero');
7917
+ var imgEl = document.createElementNS(NS, 'svg:image');
7918
+ imgEl.setAttributeNS(XLINK_NS, 'xlink:href', imgSrc);
7919
+ imgEl.setAttributeNS(null, 'x', '0');
7920
+ imgEl.setAttributeNS(null, 'y', pf(-height));
7921
+ imgEl.setAttributeNS(null, 'width', pf(width) + 'px');
7922
+ imgEl.setAttributeNS(null, 'height', pf(height) + 'px');
7923
+ imgEl.setAttributeNS(null, 'transform',
7924
+ 'scale(' + pf(1 / width) + ' ' +
7925
+ pf(-1 / height) + ')');
7926
+ if (mask) {
7927
+ mask.appendChild(imgEl);
7928
+ } else {
7929
+ this.tgrp.appendChild(imgEl);
7930
+ }
7931
+ if (current.pendingClip) {
7932
+ this.cgrp.appendChild(this.tgrp);
7933
+ this.pgrp.appendChild(this.cgrp);
7934
+ } else {
7935
+ this.pgrp.appendChild(this.tgrp);
7936
+ }
7937
+ },
7938
+
7939
+ paintImageMaskXObject:
7940
+ function SVGGraphics_paintImageMaskXObject(imgData) {
7941
+ var current = this.current;
7942
+ var width = imgData.width;
7943
+ var height = imgData.height;
7944
+ var fillColor = current.fillColor;
7945
+
7946
+ current.maskId = 'mask' + maskCount++;
7947
+ var mask = document.createElementNS(NS, 'svg:mask');
7948
+ mask.setAttributeNS(null, 'id', current.maskId);
7949
+
7950
+ var rect = document.createElementNS(NS, 'svg:rect');
7951
+ rect.setAttributeNS(null, 'x', '0');
7952
+ rect.setAttributeNS(null, 'y', '0');
7953
+ rect.setAttributeNS(null, 'width', pf(width));
7954
+ rect.setAttributeNS(null, 'height', pf(height));
7955
+ rect.setAttributeNS(null, 'fill', fillColor);
7956
+ rect.setAttributeNS(null, 'mask', 'url(#' + current.maskId +')');
7957
+ this.defs.appendChild(mask);
7958
+ this.tgrp.appendChild(rect);
7959
+
7960
+ this.paintInlineImageXObject(imgData, mask);
7961
+ },
7962
+
7963
+ paintFormXObjectBegin:
7964
+ function SVGGraphics_paintFormXObjectBegin(matrix, bbox) {
7965
+ this.save();
7966
+
7967
+ if (isArray(matrix) && matrix.length === 6) {
7968
+ this.transform(matrix[0], matrix[1], matrix[2],
7969
+ matrix[3], matrix[4], matrix[5]);
7970
+ }
7971
+
7972
+ if (isArray(bbox) && bbox.length === 4) {
7973
+ var width = bbox[2] - bbox[0];
7974
+ var height = bbox[3] - bbox[1];
7975
+
7976
+ var cliprect = document.createElementNS(NS, 'svg:rect');
7977
+ cliprect.setAttributeNS(null, 'x', bbox[0]);
7978
+ cliprect.setAttributeNS(null, 'y', bbox[1]);
7979
+ cliprect.setAttributeNS(null, 'width', pf(width));
7980
+ cliprect.setAttributeNS(null, 'height', pf(height));
7981
+ this.current.element = cliprect;
7982
+ this.clip('nonzero');
7983
+ this.endPath();
7984
+ }
7985
+ },
7986
+
7987
+ paintFormXObjectEnd:
7988
+ function SVGGraphics_paintFormXObjectEnd() {
7989
+ this.restore();
7990
+ }
7991
+ };
7992
+ return SVGGraphics;
7993
+ })();
7994
+
7995
+ PDFJS.SVGGraphics = SVGGraphics;
7996
+
7997
+
7998
+ }).call((typeof window === 'undefined') ? this : window);
7999
+
8000
+ if (!PDFJS.workerSrc && typeof document !== 'undefined') {
8001
+ // workerSrc is not set -- using last script url to define default location
8002
+ PDFJS.workerSrc = (function () {
8003
+ 'use strict';
8004
+ var scriptTagContainer = document.body ||
8005
+ document.getElementsByTagName('head')[0];
8006
+ var pdfjsSrc = scriptTagContainer.lastChild.src;
8007
+ return pdfjsSrc && pdfjsSrc.replace(/\.js$/i, '.worker.js');
8008
+ })();
8009
+ }
8010
+
8011
+
beta/build/pdf.worker.js ADDED
@@ -0,0 +1,39321 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
+ /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
3
+ /* Copyright 2012 Mozilla Foundation
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ /*jshint globalstrict: false */
18
+ /* globals PDFJS */
19
+
20
+ // Initializing PDFJS global object (if still undefined)
21
+ if (typeof PDFJS === 'undefined') {
22
+ (typeof window !== 'undefined' ? window : this).PDFJS = {};
23
+ }
24
+
25
+ PDFJS.version = '1.1.114';
26
+ PDFJS.build = '3fd44fd';
27
+
28
+ (function pdfjsWrapper() {
29
+ // Use strict in our context only - users might not want it
30
+ 'use strict';
31
+
32
+ /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
33
+ /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
34
+ /* Copyright 2012 Mozilla Foundation
35
+ *
36
+ * Licensed under the Apache License, Version 2.0 (the "License");
37
+ * you may not use this file except in compliance with the License.
38
+ * You may obtain a copy of the License at
39
+ *
40
+ * http://www.apache.org/licenses/LICENSE-2.0
41
+ *
42
+ * Unless required by applicable law or agreed to in writing, software
43
+ * distributed under the License is distributed on an "AS IS" BASIS,
44
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
45
+ * See the License for the specific language governing permissions and
46
+ * limitations under the License.
47
+ */
48
+ /* globals Cmd, ColorSpace, Dict, MozBlobBuilder, Name, PDFJS, Ref, URL,
49
+ Promise */
50
+
51
+ 'use strict';
52
+
53
+ var globalScope = (typeof window === 'undefined') ? this : window;
54
+
55
+ var isWorker = (typeof window === 'undefined');
56
+
57
+ var FONT_IDENTITY_MATRIX = [0.001, 0, 0, 0.001, 0, 0];
58
+
59
+ var TextRenderingMode = {
60
+ FILL: 0,
61
+ STROKE: 1,
62
+ FILL_STROKE: 2,
63
+ INVISIBLE: 3,
64
+ FILL_ADD_TO_PATH: 4,
65
+ STROKE_ADD_TO_PATH: 5,
66
+ FILL_STROKE_ADD_TO_PATH: 6,
67
+ ADD_TO_PATH: 7,
68
+ FILL_STROKE_MASK: 3,
69
+ ADD_TO_PATH_FLAG: 4
70
+ };
71
+
72
+ var ImageKind = {
73
+ GRAYSCALE_1BPP: 1,
74
+ RGB_24BPP: 2,
75
+ RGBA_32BPP: 3
76
+ };
77
+
78
+ var AnnotationType = {
79
+ WIDGET: 1,
80
+ TEXT: 2,
81
+ LINK: 3
82
+ };
83
+
84
+ var StreamType = {
85
+ UNKNOWN: 0,
86
+ FLATE: 1,
87
+ LZW: 2,
88
+ DCT: 3,
89
+ JPX: 4,
90
+ JBIG: 5,
91
+ A85: 6,
92
+ AHX: 7,
93
+ CCF: 8,
94
+ RL: 9
95
+ };
96
+
97
+ var FontType = {
98
+ UNKNOWN: 0,
99
+ TYPE1: 1,
100
+ TYPE1C: 2,
101
+ CIDFONTTYPE0: 3,
102
+ CIDFONTTYPE0C: 4,
103
+ TRUETYPE: 5,
104
+ CIDFONTTYPE2: 6,
105
+ TYPE3: 7,
106
+ OPENTYPE: 8,
107
+ TYPE0: 9,
108
+ MMTYPE1: 10
109
+ };
110
+
111
+ // The global PDFJS object exposes the API
112
+ // In production, it will be declared outside a global wrapper
113
+ // In development, it will be declared here
114
+ if (!globalScope.PDFJS) {
115
+ globalScope.PDFJS = {};
116
+ }
117
+
118
+ globalScope.PDFJS.pdfBug = false;
119
+
120
+ PDFJS.VERBOSITY_LEVELS = {
121
+ errors: 0,
122
+ warnings: 1,
123
+ infos: 5
124
+ };
125
+
126
+ // All the possible operations for an operator list.
127
+ var OPS = PDFJS.OPS = {
128
+ // Intentionally start from 1 so it is easy to spot bad operators that will be
129
+ // 0's.
130
+ dependency: 1,
131
+ setLineWidth: 2,
132
+ setLineCap: 3,
133
+ setLineJoin: 4,
134
+ setMiterLimit: 5,
135
+ setDash: 6,
136
+ setRenderingIntent: 7,
137
+ setFlatness: 8,
138
+ setGState: 9,
139
+ save: 10,
140
+ restore: 11,
141
+ transform: 12,
142
+ moveTo: 13,
143
+ lineTo: 14,
144
+ curveTo: 15,
145
+ curveTo2: 16,
146
+ curveTo3: 17,
147
+ closePath: 18,
148
+ rectangle: 19,
149
+ stroke: 20,
150
+ closeStroke: 21,
151
+ fill: 22,
152
+ eoFill: 23,
153
+ fillStroke: 24,
154
+ eoFillStroke: 25,
155
+ closeFillStroke: 26,
156
+ closeEOFillStroke: 27,
157
+ endPath: 28,
158
+ clip: 29,
159
+ eoClip: 30,
160
+ beginText: 31,
161
+ endText: 32,
162
+ setCharSpacing: 33,
163
+ setWordSpacing: 34,
164
+ setHScale: 35,
165
+ setLeading: 36,
166
+ setFont: 37,
167
+ setTextRenderingMode: 38,
168
+ setTextRise: 39,
169
+ moveText: 40,
170
+ setLeadingMoveText: 41,
171
+ setTextMatrix: 42,
172
+ nextLine: 43,
173
+ showText: 44,
174
+ showSpacedText: 45,
175
+ nextLineShowText: 46,
176
+ nextLineSetSpacingShowText: 47,
177
+ setCharWidth: 48,
178
+ setCharWidthAndBounds: 49,
179
+ setStrokeColorSpace: 50,
180
+ setFillColorSpace: 51,
181
+ setStrokeColor: 52,
182
+ setStrokeColorN: 53,
183
+ setFillColor: 54,
184
+ setFillColorN: 55,
185
+ setStrokeGray: 56,
186
+ setFillGray: 57,
187
+ setStrokeRGBColor: 58,
188
+ setFillRGBColor: 59,
189
+ setStrokeCMYKColor: 60,
190
+ setFillCMYKColor: 61,
191
+ shadingFill: 62,
192
+ beginInlineImage: 63,
193
+ beginImageData: 64,
194
+ endInlineImage: 65,
195
+ paintXObject: 66,
196
+ markPoint: 67,
197
+ markPointProps: 68,
198
+ beginMarkedContent: 69,
199
+ beginMarkedContentProps: 70,
200
+ endMarkedContent: 71,
201
+ beginCompat: 72,
202
+ endCompat: 73,
203
+ paintFormXObjectBegin: 74,
204
+ paintFormXObjectEnd: 75,
205
+ beginGroup: 76,
206
+ endGroup: 77,
207
+ beginAnnotations: 78,
208
+ endAnnotations: 79,
209
+ beginAnnotation: 80,
210
+ endAnnotation: 81,
211
+ paintJpegXObject: 82,
212
+ paintImageMaskXObject: 83,
213
+ paintImageMaskXObjectGroup: 84,
214
+ paintImageXObject: 85,
215
+ paintInlineImageXObject: 86,
216
+ paintInlineImageXObjectGroup: 87,
217
+ paintImageXObjectRepeat: 88,
218
+ paintImageMaskXObjectRepeat: 89,
219
+ paintSolidColorImageMask: 90,
220
+ constructPath: 91
221
+ };
222
+
223
+ // A notice for devs. These are good for things that are helpful to devs, such
224
+ // as warning that Workers were disabled, which is important to devs but not
225
+ // end users.
226
+ function info(msg) {
227
+ if (PDFJS.verbosity >= PDFJS.VERBOSITY_LEVELS.infos) {
228
+ console.log('Info: ' + msg);
229
+ }
230
+ }
231
+
232
+ // Non-fatal warnings.
233
+ function warn(msg) {
234
+ if (PDFJS.verbosity >= PDFJS.VERBOSITY_LEVELS.warnings) {
235
+ console.log('Warning: ' + msg);
236
+ }
237
+ }
238
+
239
+ // Fatal errors that should trigger the fallback UI and halt execution by
240
+ // throwing an exception.
241
+ function error(msg) {
242
+ if (PDFJS.verbosity >= PDFJS.VERBOSITY_LEVELS.errors) {
243
+ console.log('Error: ' + msg);
244
+ console.log(backtrace());
245
+ }
246
+ UnsupportedManager.notify(UNSUPPORTED_FEATURES.unknown);
247
+ throw new Error(msg);
248
+ }
249
+
250
+ function backtrace() {
251
+ try {
252
+ throw new Error();
253
+ } catch (e) {
254
+ return e.stack ? e.stack.split('\n').slice(2).join('\n') : '';
255
+ }
256
+ }
257
+
258
+ function assert(cond, msg) {
259
+ if (!cond) {
260
+ error(msg);
261
+ }
262
+ }
263
+
264
+ var UNSUPPORTED_FEATURES = PDFJS.UNSUPPORTED_FEATURES = {
265
+ unknown: 'unknown',
266
+ forms: 'forms',
267
+ javaScript: 'javaScript',
268
+ smask: 'smask',
269
+ shadingPattern: 'shadingPattern',
270
+ font: 'font'
271
+ };
272
+
273
+ var UnsupportedManager = PDFJS.UnsupportedManager =
274
+ (function UnsupportedManagerClosure() {
275
+ var listeners = [];
276
+ return {
277
+ listen: function (cb) {
278
+ listeners.push(cb);
279
+ },
280
+ notify: function (featureId) {
281
+ warn('Unsupported feature "' + featureId + '"');
282
+ for (var i = 0, ii = listeners.length; i < ii; i++) {
283
+ listeners[i](featureId);
284
+ }
285
+ }
286
+ };
287
+ })();
288
+
289
+ // Combines two URLs. The baseUrl shall be absolute URL. If the url is an
290
+ // absolute URL, it will be returned as is.
291
+ function combineUrl(baseUrl, url) {
292
+ if (!url) {
293
+ return baseUrl;
294
+ }
295
+ if (/^[a-z][a-z0-9+\-.]*:/i.test(url)) {
296
+ return url;
297
+ }
298
+ var i;
299
+ if (url.charAt(0) === '/') {
300
+ // absolute path
301
+ i = baseUrl.indexOf('://');
302
+ if (url.charAt(1) === '/') {
303
+ ++i;
304
+ } else {
305
+ i = baseUrl.indexOf('/', i + 3);
306
+ }
307
+ return baseUrl.substring(0, i) + url;
308
+ } else {
309
+ // relative path
310
+ var pathLength = baseUrl.length;
311
+ i = baseUrl.lastIndexOf('#');
312
+ pathLength = i >= 0 ? i : pathLength;
313
+ i = baseUrl.lastIndexOf('?', pathLength);
314
+ pathLength = i >= 0 ? i : pathLength;
315
+ var prefixLength = baseUrl.lastIndexOf('/', pathLength);
316
+ return baseUrl.substring(0, prefixLength + 1) + url;
317
+ }
318
+ }
319
+
320
+ // Validates if URL is safe and allowed, e.g. to avoid XSS.
321
+ function isValidUrl(url, allowRelative) {
322
+ if (!url) {
323
+ return false;
324
+ }
325
+ // RFC 3986 (http://tools.ietf.org/html/rfc3986#section-3.1)
326
+ // scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
327
+ var protocol = /^[a-z][a-z0-9+\-.]*(?=:)/i.exec(url);
328
+ if (!protocol) {
329
+ return allowRelative;
330
+ }
331
+ protocol = protocol[0].toLowerCase();
332
+ switch (protocol) {
333
+ case 'http':
334
+ case 'https':
335
+ case 'ftp':
336
+ case 'mailto':
337
+ case 'tel':
338
+ return true;
339
+ default:
340
+ return false;
341
+ }
342
+ }
343
+ PDFJS.isValidUrl = isValidUrl;
344
+
345
+ function shadow(obj, prop, value) {
346
+ Object.defineProperty(obj, prop, { value: value,
347
+ enumerable: true,
348
+ configurable: true,
349
+ writable: false });
350
+ return value;
351
+ }
352
+ PDFJS.shadow = shadow;
353
+
354
+ var PasswordResponses = PDFJS.PasswordResponses = {
355
+ NEED_PASSWORD: 1,
356
+ INCORRECT_PASSWORD: 2
357
+ };
358
+
359
+ var PasswordException = (function PasswordExceptionClosure() {
360
+ function PasswordException(msg, code) {
361
+ this.name = 'PasswordException';
362
+ this.message = msg;
363
+ this.code = code;
364
+ }
365
+
366
+ PasswordException.prototype = new Error();
367
+ PasswordException.constructor = PasswordException;
368
+
369
+ return PasswordException;
370
+ })();
371
+ PDFJS.PasswordException = PasswordException;
372
+
373
+ var UnknownErrorException = (function UnknownErrorExceptionClosure() {
374
+ function UnknownErrorException(msg, details) {
375
+ this.name = 'UnknownErrorException';
376
+ this.message = msg;
377
+ this.details = details;
378
+ }
379
+
380
+ UnknownErrorException.prototype = new Error();
381
+ UnknownErrorException.constructor = UnknownErrorException;
382
+
383
+ return UnknownErrorException;
384
+ })();
385
+ PDFJS.UnknownErrorException = UnknownErrorException;
386
+
387
+ var InvalidPDFException = (function InvalidPDFExceptionClosure() {
388
+ function InvalidPDFException(msg) {
389
+ this.name = 'InvalidPDFException';
390
+ this.message = msg;
391
+ }
392
+
393
+ InvalidPDFException.prototype = new Error();
394
+ InvalidPDFException.constructor = InvalidPDFException;
395
+
396
+ return InvalidPDFException;
397
+ })();
398
+ PDFJS.InvalidPDFException = InvalidPDFException;
399
+
400
+ var MissingPDFException = (function MissingPDFExceptionClosure() {
401
+ function MissingPDFException(msg) {
402
+ this.name = 'MissingPDFException';
403
+ this.message = msg;
404
+ }
405
+
406
+ MissingPDFException.prototype = new Error();
407
+ MissingPDFException.constructor = MissingPDFException;
408
+
409
+ return MissingPDFException;
410
+ })();
411
+ PDFJS.MissingPDFException = MissingPDFException;
412
+
413
+ var UnexpectedResponseException =
414
+ (function UnexpectedResponseExceptionClosure() {
415
+ function UnexpectedResponseException(msg, status) {
416
+ this.name = 'UnexpectedResponseException';
417
+ this.message = msg;
418
+ this.status = status;
419
+ }
420
+
421
+ UnexpectedResponseException.prototype = new Error();
422
+ UnexpectedResponseException.constructor = UnexpectedResponseException;
423
+
424
+ return UnexpectedResponseException;
425
+ })();
426
+ PDFJS.UnexpectedResponseException = UnexpectedResponseException;
427
+
428
+ var NotImplementedException = (function NotImplementedExceptionClosure() {
429
+ function NotImplementedException(msg) {
430
+ this.message = msg;
431
+ }
432
+
433
+ NotImplementedException.prototype = new Error();
434
+ NotImplementedException.prototype.name = 'NotImplementedException';
435
+ NotImplementedException.constructor = NotImplementedException;
436
+
437
+ return NotImplementedException;
438
+ })();
439
+
440
+ var MissingDataException = (function MissingDataExceptionClosure() {
441
+ function MissingDataException(begin, end) {
442
+ this.begin = begin;
443
+ this.end = end;
444
+ this.message = 'Missing data [' + begin + ', ' + end + ')';
445
+ }
446
+
447
+ MissingDataException.prototype = new Error();
448
+ MissingDataException.prototype.name = 'MissingDataException';
449
+ MissingDataException.constructor = MissingDataException;
450
+
451
+ return MissingDataException;
452
+ })();
453
+
454
+ var XRefParseException = (function XRefParseExceptionClosure() {
455
+ function XRefParseException(msg) {
456
+ this.message = msg;
457
+ }
458
+
459
+ XRefParseException.prototype = new Error();
460
+ XRefParseException.prototype.name = 'XRefParseException';
461
+ XRefParseException.constructor = XRefParseException;
462
+
463
+ return XRefParseException;
464
+ })();
465
+
466
+
467
+ function bytesToString(bytes) {
468
+ assert(bytes !== null && typeof bytes === 'object' &&
469
+ bytes.length !== undefined, 'Invalid argument for bytesToString');
470
+ var length = bytes.length;
471
+ var MAX_ARGUMENT_COUNT = 8192;
472
+ if (length < MAX_ARGUMENT_COUNT) {
473
+ return String.fromCharCode.apply(null, bytes);
474
+ }
475
+ var strBuf = [];
476
+ for (var i = 0; i < length; i += MAX_ARGUMENT_COUNT) {
477
+ var chunkEnd = Math.min(i + MAX_ARGUMENT_COUNT, length);
478
+ var chunk = bytes.subarray(i, chunkEnd);
479
+ strBuf.push(String.fromCharCode.apply(null, chunk));
480
+ }
481
+ return strBuf.join('');
482
+ }
483
+
484
+ function stringToBytes(str) {
485
+ assert(typeof str === 'string', 'Invalid argument for stringToBytes');
486
+ var length = str.length;
487
+ var bytes = new Uint8Array(length);
488
+ for (var i = 0; i < length; ++i) {
489
+ bytes[i] = str.charCodeAt(i) & 0xFF;
490
+ }
491
+ return bytes;
492
+ }
493
+
494
+ function string32(value) {
495
+ return String.fromCharCode((value >> 24) & 0xff, (value >> 16) & 0xff,
496
+ (value >> 8) & 0xff, value & 0xff);
497
+ }
498
+
499
+ function log2(x) {
500
+ var n = 1, i = 0;
501
+ while (x > n) {
502
+ n <<= 1;
503
+ i++;
504
+ }
505
+ return i;
506
+ }
507
+
508
+ function readInt8(data, start) {
509
+ return (data[start] << 24) >> 24;
510
+ }
511
+
512
+ function readUint16(data, offset) {
513
+ return (data[offset] << 8) | data[offset + 1];
514
+ }
515
+
516
+ function readUint32(data, offset) {
517
+ return ((data[offset] << 24) | (data[offset + 1] << 16) |
518
+ (data[offset + 2] << 8) | data[offset + 3]) >>> 0;
519
+ }
520
+
521
+ // Lazy test the endianness of the platform
522
+ // NOTE: This will be 'true' for simulated TypedArrays
523
+ function isLittleEndian() {
524
+ var buffer8 = new Uint8Array(2);
525
+ buffer8[0] = 1;
526
+ var buffer16 = new Uint16Array(buffer8.buffer);
527
+ return (buffer16[0] === 1);
528
+ }
529
+
530
+ Object.defineProperty(PDFJS, 'isLittleEndian', {
531
+ configurable: true,
532
+ get: function PDFJS_isLittleEndian() {
533
+ return shadow(PDFJS, 'isLittleEndian', isLittleEndian());
534
+ }
535
+ });
536
+
537
+ // Lazy test if the userAgant support CanvasTypedArrays
538
+ function hasCanvasTypedArrays() {
539
+ var canvas = document.createElement('canvas');
540
+ canvas.width = canvas.height = 1;
541
+ var ctx = canvas.getContext('2d');
542
+ var imageData = ctx.createImageData(1, 1);
543
+ return (typeof imageData.data.buffer !== 'undefined');
544
+ }
545
+
546
+ Object.defineProperty(PDFJS, 'hasCanvasTypedArrays', {
547
+ configurable: true,
548
+ get: function PDFJS_hasCanvasTypedArrays() {
549
+ return shadow(PDFJS, 'hasCanvasTypedArrays', hasCanvasTypedArrays());
550
+ }
551
+ });
552
+
553
+ var Uint32ArrayView = (function Uint32ArrayViewClosure() {
554
+
555
+ function Uint32ArrayView(buffer, length) {
556
+ this.buffer = buffer;
557
+ this.byteLength = buffer.length;
558
+ this.length = length === undefined ? (this.byteLength >> 2) : length;
559
+ ensureUint32ArrayViewProps(this.length);
560
+ }
561
+ Uint32ArrayView.prototype = Object.create(null);
562
+
563
+ var uint32ArrayViewSetters = 0;
564
+ function createUint32ArrayProp(index) {
565
+ return {
566
+ get: function () {
567
+ var buffer = this.buffer, offset = index << 2;
568
+ return (buffer[offset] | (buffer[offset + 1] << 8) |
569
+ (buffer[offset + 2] << 16) | (buffer[offset + 3] << 24)) >>> 0;
570
+ },
571
+ set: function (value) {
572
+ var buffer = this.buffer, offset = index << 2;
573
+ buffer[offset] = value & 255;
574
+ buffer[offset + 1] = (value >> 8) & 255;
575
+ buffer[offset + 2] = (value >> 16) & 255;
576
+ buffer[offset + 3] = (value >>> 24) & 255;
577
+ }
578
+ };
579
+ }
580
+
581
+ function ensureUint32ArrayViewProps(length) {
582
+ while (uint32ArrayViewSetters < length) {
583
+ Object.defineProperty(Uint32ArrayView.prototype,
584
+ uint32ArrayViewSetters,
585
+ createUint32ArrayProp(uint32ArrayViewSetters));
586
+ uint32ArrayViewSetters++;
587
+ }
588
+ }
589
+
590
+ return Uint32ArrayView;
591
+ })();
592
+
593
+ var IDENTITY_MATRIX = [1, 0, 0, 1, 0, 0];
594
+
595
+ var Util = PDFJS.Util = (function UtilClosure() {
596
+ function Util() {}
597
+
598
+ var rgbBuf = ['rgb(', 0, ',', 0, ',', 0, ')'];
599
+
600
+ // makeCssRgb() can be called thousands of times. Using |rgbBuf| avoids
601
+ // creating many intermediate strings.
602
+ Util.makeCssRgb = function Util_makeCssRgb(r, g, b) {
603
+ rgbBuf[1] = r;
604
+ rgbBuf[3] = g;
605
+ rgbBuf[5] = b;
606
+ return rgbBuf.join('');
607
+ };
608
+
609
+ // Concatenates two transformation matrices together and returns the result.
610
+ Util.transform = function Util_transform(m1, m2) {
611
+ return [
612
+ m1[0] * m2[0] + m1[2] * m2[1],
613
+ m1[1] * m2[0] + m1[3] * m2[1],
614
+ m1[0] * m2[2] + m1[2] * m2[3],
615
+ m1[1] * m2[2] + m1[3] * m2[3],
616
+ m1[0] * m2[4] + m1[2] * m2[5] + m1[4],
617
+ m1[1] * m2[4] + m1[3] * m2[5] + m1[5]
618
+ ];
619
+ };
620
+
621
+ // For 2d affine transforms
622
+ Util.applyTransform = function Util_applyTransform(p, m) {
623
+ var xt = p[0] * m[0] + p[1] * m[2] + m[4];
624
+ var yt = p[0] * m[1] + p[1] * m[3] + m[5];
625
+ return [xt, yt];
626
+ };
627
+
628
+ Util.applyInverseTransform = function Util_applyInverseTransform(p, m) {
629
+ var d = m[0] * m[3] - m[1] * m[2];
630
+ var xt = (p[0] * m[3] - p[1] * m[2] + m[2] * m[5] - m[4] * m[3]) / d;
631
+ var yt = (-p[0] * m[1] + p[1] * m[0] + m[4] * m[1] - m[5] * m[0]) / d;
632
+ return [xt, yt];
633
+ };
634
+
635
+ // Applies the transform to the rectangle and finds the minimum axially
636
+ // aligned bounding box.
637
+ Util.getAxialAlignedBoundingBox =
638
+ function Util_getAxialAlignedBoundingBox(r, m) {
639
+
640
+ var p1 = Util.applyTransform(r, m);
641
+ var p2 = Util.applyTransform(r.slice(2, 4), m);
642
+ var p3 = Util.applyTransform([r[0], r[3]], m);
643
+ var p4 = Util.applyTransform([r[2], r[1]], m);
644
+ return [
645
+ Math.min(p1[0], p2[0], p3[0], p4[0]),
646
+ Math.min(p1[1], p2[1], p3[1], p4[1]),
647
+ Math.max(p1[0], p2[0], p3[0], p4[0]),
648
+ Math.max(p1[1], p2[1], p3[1], p4[1])
649
+ ];
650
+ };
651
+
652
+ Util.inverseTransform = function Util_inverseTransform(m) {
653
+ var d = m[0] * m[3] - m[1] * m[2];
654
+ return [m[3] / d, -m[1] / d, -m[2] / d, m[0] / d,
655
+ (m[2] * m[5] - m[4] * m[3]) / d, (m[4] * m[1] - m[5] * m[0]) / d];
656
+ };
657
+
658
+ // Apply a generic 3d matrix M on a 3-vector v:
659
+ // | a b c | | X |
660
+ // | d e f | x | Y |
661
+ // | g h i | | Z |
662
+ // M is assumed to be serialized as [a,b,c,d,e,f,g,h,i],
663
+ // with v as [X,Y,Z]
664
+ Util.apply3dTransform = function Util_apply3dTransform(m, v) {
665
+ return [
666
+ m[0] * v[0] + m[1] * v[1] + m[2] * v[2],
667
+ m[3] * v[0] + m[4] * v[1] + m[5] * v[2],
668
+ m[6] * v[0] + m[7] * v[1] + m[8] * v[2]
669
+ ];
670
+ };
671
+
672
+ // This calculation uses Singular Value Decomposition.
673
+ // The SVD can be represented with formula A = USV. We are interested in the
674
+ // matrix S here because it represents the scale values.
675
+ Util.singularValueDecompose2dScale =
676
+ function Util_singularValueDecompose2dScale(m) {
677
+
678
+ var transpose = [m[0], m[2], m[1], m[3]];
679
+
680
+ // Multiply matrix m with its transpose.
681
+ var a = m[0] * transpose[0] + m[1] * transpose[2];
682
+ var b = m[0] * transpose[1] + m[1] * transpose[3];
683
+ var c = m[2] * transpose[0] + m[3] * transpose[2];
684
+ var d = m[2] * transpose[1] + m[3] * transpose[3];
685
+
686
+ // Solve the second degree polynomial to get roots.
687
+ var first = (a + d) / 2;
688
+ var second = Math.sqrt((a + d) * (a + d) - 4 * (a * d - c * b)) / 2;
689
+ var sx = first + second || 1;
690
+ var sy = first - second || 1;
691
+
692
+ // Scale values are the square roots of the eigenvalues.
693
+ return [Math.sqrt(sx), Math.sqrt(sy)];
694
+ };
695
+
696
+ // Normalize rectangle rect=[x1, y1, x2, y2] so that (x1,y1) < (x2,y2)
697
+ // For coordinate systems whose origin lies in the bottom-left, this
698
+ // means normalization to (BL,TR) ordering. For systems with origin in the
699
+ // top-left, this means (TL,BR) ordering.
700
+ Util.normalizeRect = function Util_normalizeRect(rect) {
701
+ var r = rect.slice(0); // clone rect
702
+ if (rect[0] > rect[2]) {
703
+ r[0] = rect[2];
704
+ r[2] = rect[0];
705
+ }
706
+ if (rect[1] > rect[3]) {
707
+ r[1] = rect[3];
708
+ r[3] = rect[1];
709
+ }
710
+ return r;
711
+ };
712
+
713
+ // Returns a rectangle [x1, y1, x2, y2] corresponding to the
714
+ // intersection of rect1 and rect2. If no intersection, returns 'false'
715
+ // The rectangle coordinates of rect1, rect2 should be [x1, y1, x2, y2]
716
+ Util.intersect = function Util_intersect(rect1, rect2) {
717
+ function compare(a, b) {
718
+ return a - b;
719
+ }
720
+
721
+ // Order points along the axes
722
+ var orderedX = [rect1[0], rect1[2], rect2[0], rect2[2]].sort(compare),
723
+ orderedY = [rect1[1], rect1[3], rect2[1], rect2[3]].sort(compare),
724
+ result = [];
725
+
726
+ rect1 = Util.normalizeRect(rect1);
727
+ rect2 = Util.normalizeRect(rect2);
728
+
729
+ // X: first and second points belong to different rectangles?
730
+ if ((orderedX[0] === rect1[0] && orderedX[1] === rect2[0]) ||
731
+ (orderedX[0] === rect2[0] && orderedX[1] === rect1[0])) {
732
+ // Intersection must be between second and third points
733
+ result[0] = orderedX[1];
734
+ result[2] = orderedX[2];
735
+ } else {
736
+ return false;
737
+ }
738
+
739
+ // Y: first and second points belong to different rectangles?
740
+ if ((orderedY[0] === rect1[1] && orderedY[1] === rect2[1]) ||
741
+ (orderedY[0] === rect2[1] && orderedY[1] === rect1[1])) {
742
+ // Intersection must be between second and third points
743
+ result[1] = orderedY[1];
744
+ result[3] = orderedY[2];
745
+ } else {
746
+ return false;
747
+ }
748
+
749
+ return result;
750
+ };
751
+
752
+ Util.sign = function Util_sign(num) {
753
+ return num < 0 ? -1 : 1;
754
+ };
755
+
756
+ Util.appendToArray = function Util_appendToArray(arr1, arr2) {
757
+ Array.prototype.push.apply(arr1, arr2);
758
+ };
759
+
760
+ Util.prependToArray = function Util_prependToArray(arr1, arr2) {
761
+ Array.prototype.unshift.apply(arr1, arr2);
762
+ };
763
+
764
+ Util.extendObj = function extendObj(obj1, obj2) {
765
+ for (var key in obj2) {
766
+ obj1[key] = obj2[key];
767
+ }
768
+ };
769
+
770
+ Util.getInheritableProperty = function Util_getInheritableProperty(dict,
771
+ name) {
772
+ while (dict && !dict.has(name)) {
773
+ dict = dict.get('Parent');
774
+ }
775
+ if (!dict) {
776
+ return null;
777
+ }
778
+ return dict.get(name);
779
+ };
780
+
781
+ Util.inherit = function Util_inherit(sub, base, prototype) {
782
+ sub.prototype = Object.create(base.prototype);
783
+ sub.prototype.constructor = sub;
784
+ for (var prop in prototype) {
785
+ sub.prototype[prop] = prototype[prop];
786
+ }
787
+ };
788
+
789
+ Util.loadScript = function Util_loadScript(src, callback) {
790
+ var script = document.createElement('script');
791
+ var loaded = false;
792
+ script.setAttribute('src', src);
793
+ if (callback) {
794
+ script.onload = function() {
795
+ if (!loaded) {
796
+ callback();
797
+ }
798
+ loaded = true;
799
+ };
800
+ }
801
+ document.getElementsByTagName('head')[0].appendChild(script);
802
+ };
803
+
804
+ return Util;
805
+ })();
806
+
807
+ /**
808
+ * PDF page viewport created based on scale, rotation and offset.
809
+ * @class
810
+ * @alias PDFJS.PageViewport
811
+ */
812
+ var PageViewport = PDFJS.PageViewport = (function PageViewportClosure() {
813
+ /**
814
+ * @constructor
815
+ * @private
816
+ * @param viewBox {Array} xMin, yMin, xMax and yMax coordinates.
817
+ * @param scale {number} scale of the viewport.
818
+ * @param rotation {number} rotations of the viewport in degrees.
819
+ * @param offsetX {number} offset X
820
+ * @param offsetY {number} offset Y
821
+ * @param dontFlip {boolean} if true, axis Y will not be flipped.
822
+ */
823
+ function PageViewport(viewBox, scale, rotation, offsetX, offsetY, dontFlip) {
824
+ this.viewBox = viewBox;
825
+ this.scale = scale;
826
+ this.rotation = rotation;
827
+ this.offsetX = offsetX;
828
+ this.offsetY = offsetY;
829
+
830
+ // creating transform to convert pdf coordinate system to the normal
831
+ // canvas like coordinates taking in account scale and rotation
832
+ var centerX = (viewBox[2] + viewBox[0]) / 2;
833
+ var centerY = (viewBox[3] + viewBox[1]) / 2;
834
+ var rotateA, rotateB, rotateC, rotateD;
835
+ rotation = rotation % 360;
836
+ rotation = rotation < 0 ? rotation + 360 : rotation;
837
+ switch (rotation) {
838
+ case 180:
839
+ rotateA = -1; rotateB = 0; rotateC = 0; rotateD = 1;
840
+ break;
841
+ case 90:
842
+ rotateA = 0; rotateB = 1; rotateC = 1; rotateD = 0;
843
+ break;
844
+ case 270:
845
+ rotateA = 0; rotateB = -1; rotateC = -1; rotateD = 0;
846
+ break;
847
+ //case 0:
848
+ default:
849
+ rotateA = 1; rotateB = 0; rotateC = 0; rotateD = -1;
850
+ break;
851
+ }
852
+
853
+ if (dontFlip) {
854
+ rotateC = -rotateC; rotateD = -rotateD;
855
+ }
856
+
857
+ var offsetCanvasX, offsetCanvasY;
858
+ var width, height;
859
+ if (rotateA === 0) {
860
+ offsetCanvasX = Math.abs(centerY - viewBox[1]) * scale + offsetX;
861
+ offsetCanvasY = Math.abs(centerX - viewBox[0]) * scale + offsetY;
862
+ width = Math.abs(viewBox[3] - viewBox[1]) * scale;
863
+ height = Math.abs(viewBox[2] - viewBox[0]) * scale;
864
+ } else {
865
+ offsetCanvasX = Math.abs(centerX - viewBox[0]) * scale + offsetX;
866
+ offsetCanvasY = Math.abs(centerY - viewBox[1]) * scale + offsetY;
867
+ width = Math.abs(viewBox[2] - viewBox[0]) * scale;
868
+ height = Math.abs(viewBox[3] - viewBox[1]) * scale;
869
+ }
870
+ // creating transform for the following operations:
871
+ // translate(-centerX, -centerY), rotate and flip vertically,
872
+ // scale, and translate(offsetCanvasX, offsetCanvasY)
873
+ this.transform = [
874
+ rotateA * scale,
875
+ rotateB * scale,
876
+ rotateC * scale,
877
+ rotateD * scale,
878
+ offsetCanvasX - rotateA * scale * centerX - rotateC * scale * centerY,
879
+ offsetCanvasY - rotateB * scale * centerX - rotateD * scale * centerY
880
+ ];
881
+
882
+ this.width = width;
883
+ this.height = height;
884
+ this.fontScale = scale;
885
+ }
886
+ PageViewport.prototype = /** @lends PDFJS.PageViewport.prototype */ {
887
+ /**
888
+ * Clones viewport with additional properties.
889
+ * @param args {Object} (optional) If specified, may contain the 'scale' or
890
+ * 'rotation' properties to override the corresponding properties in
891
+ * the cloned viewport.
892
+ * @returns {PDFJS.PageViewport} Cloned viewport.
893
+ */
894
+ clone: function PageViewPort_clone(args) {
895
+ args = args || {};
896
+ var scale = 'scale' in args ? args.scale : this.scale;
897
+ var rotation = 'rotation' in args ? args.rotation : this.rotation;
898
+ return new PageViewport(this.viewBox.slice(), scale, rotation,
899
+ this.offsetX, this.offsetY, args.dontFlip);
900
+ },
901
+ /**
902
+ * Converts PDF point to the viewport coordinates. For examples, useful for
903
+ * converting PDF location into canvas pixel coordinates.
904
+ * @param x {number} X coordinate.
905
+ * @param y {number} Y coordinate.
906
+ * @returns {Object} Object that contains 'x' and 'y' properties of the
907
+ * point in the viewport coordinate space.
908
+ * @see {@link convertToPdfPoint}
909
+ * @see {@link convertToViewportRectangle}
910
+ */
911
+ convertToViewportPoint: function PageViewport_convertToViewportPoint(x, y) {
912
+ return Util.applyTransform([x, y], this.transform);
913
+ },
914
+ /**
915
+ * Converts PDF rectangle to the viewport coordinates.
916
+ * @param rect {Array} xMin, yMin, xMax and yMax coordinates.
917
+ * @returns {Array} Contains corresponding coordinates of the rectangle
918
+ * in the viewport coordinate space.
919
+ * @see {@link convertToViewportPoint}
920
+ */
921
+ convertToViewportRectangle:
922
+ function PageViewport_convertToViewportRectangle(rect) {
923
+ var tl = Util.applyTransform([rect[0], rect[1]], this.transform);
924
+ var br = Util.applyTransform([rect[2], rect[3]], this.transform);
925
+ return [tl[0], tl[1], br[0], br[1]];
926
+ },
927
+ /**
928
+ * Converts viewport coordinates to the PDF location. For examples, useful
929
+ * for converting canvas pixel location into PDF one.
930
+ * @param x {number} X coordinate.
931
+ * @param y {number} Y coordinate.
932
+ * @returns {Object} Object that contains 'x' and 'y' properties of the
933
+ * point in the PDF coordinate space.
934
+ * @see {@link convertToViewportPoint}
935
+ */
936
+ convertToPdfPoint: function PageViewport_convertToPdfPoint(x, y) {
937
+ return Util.applyInverseTransform([x, y], this.transform);
938
+ }
939
+ };
940
+ return PageViewport;
941
+ })();
942
+
943
+ var PDFStringTranslateTable = [
944
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
945
+ 0x2D8, 0x2C7, 0x2C6, 0x2D9, 0x2DD, 0x2DB, 0x2DA, 0x2DC, 0, 0, 0, 0, 0, 0, 0,
946
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
947
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
948
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
949
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x2022, 0x2020, 0x2021, 0x2026, 0x2014,
950
+ 0x2013, 0x192, 0x2044, 0x2039, 0x203A, 0x2212, 0x2030, 0x201E, 0x201C,
951
+ 0x201D, 0x2018, 0x2019, 0x201A, 0x2122, 0xFB01, 0xFB02, 0x141, 0x152, 0x160,
952
+ 0x178, 0x17D, 0x131, 0x142, 0x153, 0x161, 0x17E, 0, 0x20AC
953
+ ];
954
+
955
+ function stringToPDFString(str) {
956
+ var i, n = str.length, strBuf = [];
957
+ if (str[0] === '\xFE' && str[1] === '\xFF') {
958
+ // UTF16BE BOM
959
+ for (i = 2; i < n; i += 2) {
960
+ strBuf.push(String.fromCharCode(
961
+ (str.charCodeAt(i) << 8) | str.charCodeAt(i + 1)));
962
+ }
963
+ } else {
964
+ for (i = 0; i < n; ++i) {
965
+ var code = PDFStringTranslateTable[str.charCodeAt(i)];
966
+ strBuf.push(code ? String.fromCharCode(code) : str.charAt(i));
967
+ }
968
+ }
969
+ return strBuf.join('');
970
+ }
971
+
972
+ function stringToUTF8String(str) {
973
+ return decodeURIComponent(escape(str));
974
+ }
975
+
976
+ function isEmptyObj(obj) {
977
+ for (var key in obj) {
978
+ return false;
979
+ }
980
+ return true;
981
+ }
982
+
983
+ function isBool(v) {
984
+ return typeof v === 'boolean';
985
+ }
986
+
987
+ function isInt(v) {
988
+ return typeof v === 'number' && ((v | 0) === v);
989
+ }
990
+
991
+ function isNum(v) {
992
+ return typeof v === 'number';
993
+ }
994
+
995
+ function isString(v) {
996
+ return typeof v === 'string';
997
+ }
998
+
999
+ function isName(v) {
1000
+ return v instanceof Name;
1001
+ }
1002
+
1003
+ function isCmd(v, cmd) {
1004
+ return v instanceof Cmd && (cmd === undefined || v.cmd === cmd);
1005
+ }
1006
+
1007
+ function isDict(v, type) {
1008
+ if (!(v instanceof Dict)) {
1009
+ return false;
1010
+ }
1011
+ if (!type) {
1012
+ return true;
1013
+ }
1014
+ var dictType = v.get('Type');
1015
+ return isName(dictType) && dictType.name === type;
1016
+ }
1017
+
1018
+ function isArray(v) {
1019
+ return v instanceof Array;
1020
+ }
1021
+
1022
+ function isStream(v) {
1023
+ return typeof v === 'object' && v !== null && v.getBytes !== undefined;
1024
+ }
1025
+
1026
+ function isArrayBuffer(v) {
1027
+ return typeof v === 'object' && v !== null && v.byteLength !== undefined;
1028
+ }
1029
+
1030
+ function isRef(v) {
1031
+ return v instanceof Ref;
1032
+ }
1033
+
1034
+ /**
1035
+ * Promise Capability object.
1036
+ *
1037
+ * @typedef {Object} PromiseCapability
1038
+ * @property {Promise} promise - A promise object.
1039
+ * @property {function} resolve - Fullfills the promise.
1040
+ * @property {function} reject - Rejects the promise.
1041
+ */
1042
+
1043
+ /**
1044
+ * Creates a promise capability object.
1045
+ * @alias PDFJS.createPromiseCapability
1046
+ *
1047
+ * @return {PromiseCapability} A capability object contains:
1048
+ * - a Promise, resolve and reject methods.
1049
+ */
1050
+ function createPromiseCapability() {
1051
+ var capability = {};
1052
+ capability.promise = new Promise(function (resolve, reject) {
1053
+ capability.resolve = resolve;
1054
+ capability.reject = reject;
1055
+ });
1056
+ return capability;
1057
+ }
1058
+
1059
+ PDFJS.createPromiseCapability = createPromiseCapability;
1060
+
1061
+ /**
1062
+ * Polyfill for Promises:
1063
+ * The following promise implementation tries to generally implement the
1064
+ * Promise/A+ spec. Some notable differences from other promise libaries are:
1065
+ * - There currently isn't a seperate deferred and promise object.
1066
+ * - Unhandled rejections eventually show an error if they aren't handled.
1067
+ *
1068
+ * Based off of the work in:
1069
+ * https://bugzilla.mozilla.org/show_bug.cgi?id=810490
1070
+ */
1071
+ (function PromiseClosure() {
1072
+ if (globalScope.Promise) {
1073
+ // Promises existing in the DOM/Worker, checking presence of all/resolve
1074
+ if (typeof globalScope.Promise.all !== 'function') {
1075
+ globalScope.Promise.all = function (iterable) {
1076
+ var count = 0, results = [], resolve, reject;
1077
+ var promise = new globalScope.Promise(function (resolve_, reject_) {
1078
+ resolve = resolve_;
1079
+ reject = reject_;
1080
+ });
1081
+ iterable.forEach(function (p, i) {
1082
+ count++;
1083
+ p.then(function (result) {
1084
+ results[i] = result;
1085
+ count--;
1086
+ if (count === 0) {
1087
+ resolve(results);
1088
+ }
1089
+ }, reject);
1090
+ });
1091
+ if (count === 0) {
1092
+ resolve(results);
1093
+ }
1094
+ return promise;
1095
+ };
1096
+ }
1097
+ if (typeof globalScope.Promise.resolve !== 'function') {
1098
+ globalScope.Promise.resolve = function (value) {
1099
+ return new globalScope.Promise(function (resolve) { resolve(value); });
1100
+ };
1101
+ }
1102
+ if (typeof globalScope.Promise.reject !== 'function') {
1103
+ globalScope.Promise.reject = function (reason) {
1104
+ return new globalScope.Promise(function (resolve, reject) {
1105
+ reject(reason);
1106
+ });
1107
+ };
1108
+ }
1109
+ if (typeof globalScope.Promise.prototype.catch !== 'function') {
1110
+ globalScope.Promise.prototype.catch = function (onReject) {
1111
+ return globalScope.Promise.prototype.then(undefined, onReject);
1112
+ };
1113
+ }
1114
+ return;
1115
+ }
1116
+ var STATUS_PENDING = 0;
1117
+ var STATUS_RESOLVED = 1;
1118
+ var STATUS_REJECTED = 2;
1119
+
1120
+ // In an attempt to avoid silent exceptions, unhandled rejections are
1121
+ // tracked and if they aren't handled in a certain amount of time an
1122
+ // error is logged.
1123
+ var REJECTION_TIMEOUT = 500;
1124
+
1125
+ var HandlerManager = {
1126
+ handlers: [],
1127
+ running: false,
1128
+ unhandledRejections: [],
1129
+ pendingRejectionCheck: false,
1130
+
1131
+ scheduleHandlers: function scheduleHandlers(promise) {
1132
+ if (promise._status === STATUS_PENDING) {
1133
+ return;
1134
+ }
1135
+
1136
+ this.handlers = this.handlers.concat(promise._handlers);
1137
+ promise._handlers = [];
1138
+
1139
+ if (this.running) {
1140
+ return;
1141
+ }
1142
+ this.running = true;
1143
+
1144
+ setTimeout(this.runHandlers.bind(this), 0);
1145
+ },
1146
+
1147
+ runHandlers: function runHandlers() {
1148
+ var RUN_TIMEOUT = 1; // ms
1149
+ var timeoutAt = Date.now() + RUN_TIMEOUT;
1150
+ while (this.handlers.length > 0) {
1151
+ var handler = this.handlers.shift();
1152
+
1153
+ var nextStatus = handler.thisPromise._status;
1154
+ var nextValue = handler.thisPromise._value;
1155
+
1156
+ try {
1157
+ if (nextStatus === STATUS_RESOLVED) {
1158
+ if (typeof handler.onResolve === 'function') {
1159
+ nextValue = handler.onResolve(nextValue);
1160
+ }
1161
+ } else if (typeof handler.onReject === 'function') {
1162
+ nextValue = handler.onReject(nextValue);
1163
+ nextStatus = STATUS_RESOLVED;
1164
+
1165
+ if (handler.thisPromise._unhandledRejection) {
1166
+ this.removeUnhandeledRejection(handler.thisPromise);
1167
+ }
1168
+ }
1169
+ } catch (ex) {
1170
+ nextStatus = STATUS_REJECTED;
1171
+ nextValue = ex;
1172
+ }
1173
+
1174
+ handler.nextPromise._updateStatus(nextStatus, nextValue);
1175
+ if (Date.now() >= timeoutAt) {
1176
+ break;
1177
+ }
1178
+ }
1179
+
1180
+ if (this.handlers.length > 0) {
1181
+ setTimeout(this.runHandlers.bind(this), 0);
1182
+ return;
1183
+ }
1184
+
1185
+ this.running = false;
1186
+ },
1187
+
1188
+ addUnhandledRejection: function addUnhandledRejection(promise) {
1189
+ this.unhandledRejections.push({
1190
+ promise: promise,
1191
+ time: Date.now()
1192
+ });
1193
+ this.scheduleRejectionCheck();
1194
+ },
1195
+
1196
+ removeUnhandeledRejection: function removeUnhandeledRejection(promise) {
1197
+ promise._unhandledRejection = false;
1198
+ for (var i = 0; i < this.unhandledRejections.length; i++) {
1199
+ if (this.unhandledRejections[i].promise === promise) {
1200
+ this.unhandledRejections.splice(i);
1201
+ i--;
1202
+ }
1203
+ }
1204
+ },
1205
+
1206
+ scheduleRejectionCheck: function scheduleRejectionCheck() {
1207
+ if (this.pendingRejectionCheck) {
1208
+ return;
1209
+ }
1210
+ this.pendingRejectionCheck = true;
1211
+ setTimeout(function rejectionCheck() {
1212
+ this.pendingRejectionCheck = false;
1213
+ var now = Date.now();
1214
+ for (var i = 0; i < this.unhandledRejections.length; i++) {
1215
+ if (now - this.unhandledRejections[i].time > REJECTION_TIMEOUT) {
1216
+ var unhandled = this.unhandledRejections[i].promise._value;
1217
+ var msg = 'Unhandled rejection: ' + unhandled;
1218
+ if (unhandled.stack) {
1219
+ msg += '\n' + unhandled.stack;
1220
+ }
1221
+ warn(msg);
1222
+ this.unhandledRejections.splice(i);
1223
+ i--;
1224
+ }
1225
+ }
1226
+ if (this.unhandledRejections.length) {
1227
+ this.scheduleRejectionCheck();
1228
+ }
1229
+ }.bind(this), REJECTION_TIMEOUT);
1230
+ }
1231
+ };
1232
+
1233
+ function Promise(resolver) {
1234
+ this._status = STATUS_PENDING;
1235
+ this._handlers = [];
1236
+ try {
1237
+ resolver.call(this, this._resolve.bind(this), this._reject.bind(this));
1238
+ } catch (e) {
1239
+ this._reject(e);
1240
+ }
1241
+ }
1242
+ /**
1243
+ * Builds a promise that is resolved when all the passed in promises are
1244
+ * resolved.
1245
+ * @param {array} array of data and/or promises to wait for.
1246
+ * @return {Promise} New dependant promise.
1247
+ */
1248
+ Promise.all = function Promise_all(promises) {
1249
+ var resolveAll, rejectAll;
1250
+ var deferred = new Promise(function (resolve, reject) {
1251
+ resolveAll = resolve;
1252
+ rejectAll = reject;
1253
+ });
1254
+ var unresolved = promises.length;
1255
+ var results = [];
1256
+ if (unresolved === 0) {
1257
+ resolveAll(results);
1258
+ return deferred;
1259
+ }
1260
+ function reject(reason) {
1261
+ if (deferred._status === STATUS_REJECTED) {
1262
+ return;
1263
+ }
1264
+ results = [];
1265
+ rejectAll(reason);
1266
+ }
1267
+ for (var i = 0, ii = promises.length; i < ii; ++i) {
1268
+ var promise = promises[i];
1269
+ var resolve = (function(i) {
1270
+ return function(value) {
1271
+ if (deferred._status === STATUS_REJECTED) {
1272
+ return;
1273
+ }
1274
+ results[i] = value;
1275
+ unresolved--;
1276
+ if (unresolved === 0) {
1277
+ resolveAll(results);
1278
+ }
1279
+ };
1280
+ })(i);
1281
+ if (Promise.isPromise(promise)) {
1282
+ promise.then(resolve, reject);
1283
+ } else {
1284
+ resolve(promise);
1285
+ }
1286
+ }
1287
+ return deferred;
1288
+ };
1289
+
1290
+ /**
1291
+ * Checks if the value is likely a promise (has a 'then' function).
1292
+ * @return {boolean} true if value is thenable
1293
+ */
1294
+ Promise.isPromise = function Promise_isPromise(value) {
1295
+ return value && typeof value.then === 'function';
1296
+ };
1297
+
1298
+ /**
1299
+ * Creates resolved promise
1300
+ * @param value resolve value
1301
+ * @returns {Promise}
1302
+ */
1303
+ Promise.resolve = function Promise_resolve(value) {
1304
+ return new Promise(function (resolve) { resolve(value); });
1305
+ };
1306
+
1307
+ /**
1308
+ * Creates rejected promise
1309
+ * @param reason rejection value
1310
+ * @returns {Promise}
1311
+ */
1312
+ Promise.reject = function Promise_reject(reason) {
1313
+ return new Promise(function (resolve, reject) { reject(reason); });
1314
+ };
1315
+
1316
+ Promise.prototype = {
1317
+ _status: null,
1318
+ _value: null,
1319
+ _handlers: null,
1320
+ _unhandledRejection: null,
1321
+
1322
+ _updateStatus: function Promise__updateStatus(status, value) {
1323
+ if (this._status === STATUS_RESOLVED ||
1324
+ this._status === STATUS_REJECTED) {
1325
+ return;
1326
+ }
1327
+
1328
+ if (status === STATUS_RESOLVED &&
1329
+ Promise.isPromise(value)) {
1330
+ value.then(this._updateStatus.bind(this, STATUS_RESOLVED),
1331
+ this._updateStatus.bind(this, STATUS_REJECTED));
1332
+ return;
1333
+ }
1334
+
1335
+ this._status = status;
1336
+ this._value = value;
1337
+
1338
+ if (status === STATUS_REJECTED && this._handlers.length === 0) {
1339
+ this._unhandledRejection = true;
1340
+ HandlerManager.addUnhandledRejection(this);
1341
+ }
1342
+
1343
+ HandlerManager.scheduleHandlers(this);
1344
+ },
1345
+
1346
+ _resolve: function Promise_resolve(value) {
1347
+ this._updateStatus(STATUS_RESOLVED, value);
1348
+ },
1349
+
1350
+ _reject: function Promise_reject(reason) {
1351
+ this._updateStatus(STATUS_REJECTED, reason);
1352
+ },
1353
+
1354
+ then: function Promise_then(onResolve, onReject) {
1355
+ var nextPromise = new Promise(function (resolve, reject) {
1356
+ this.resolve = resolve;
1357
+ this.reject = reject;
1358
+ });
1359
+ this._handlers.push({
1360
+ thisPromise: this,
1361
+ onResolve: onResolve,
1362
+ onReject: onReject,
1363
+ nextPromise: nextPromise
1364
+ });
1365
+ HandlerManager.scheduleHandlers(this);
1366
+ return nextPromise;
1367
+ },
1368
+
1369
+ catch: function Promise_catch(onReject) {
1370
+ return this.then(undefined, onReject);
1371
+ }
1372
+ };
1373
+
1374
+ globalScope.Promise = Promise;
1375
+ })();
1376
+
1377
+ var StatTimer = (function StatTimerClosure() {
1378
+ function rpad(str, pad, length) {
1379
+ while (str.length < length) {
1380
+ str += pad;
1381
+ }
1382
+ return str;
1383
+ }
1384
+ function StatTimer() {
1385
+ this.started = {};
1386
+ this.times = [];
1387
+ this.enabled = true;
1388
+ }
1389
+ StatTimer.prototype = {
1390
+ time: function StatTimer_time(name) {
1391
+ if (!this.enabled) {
1392
+ return;
1393
+ }
1394
+ if (name in this.started) {
1395
+ warn('Timer is already running for ' + name);
1396
+ }
1397
+ this.started[name] = Date.now();
1398
+ },
1399
+ timeEnd: function StatTimer_timeEnd(name) {
1400
+ if (!this.enabled) {
1401
+ return;
1402
+ }
1403
+ if (!(name in this.started)) {
1404
+ warn('Timer has not been started for ' + name);
1405
+ }
1406
+ this.times.push({
1407
+ 'name': name,
1408
+ 'start': this.started[name],
1409
+ 'end': Date.now()
1410
+ });
1411
+ // Remove timer from started so it can be called again.
1412
+ delete this.started[name];
1413
+ },
1414
+ toString: function StatTimer_toString() {
1415
+ var i, ii;
1416
+ var times = this.times;
1417
+ var out = '';
1418
+ // Find the longest name for padding purposes.
1419
+ var longest = 0;
1420
+ for (i = 0, ii = times.length; i < ii; ++i) {
1421
+ var name = times[i]['name'];
1422
+ if (name.length > longest) {
1423
+ longest = name.length;
1424
+ }
1425
+ }
1426
+ for (i = 0, ii = times.length; i < ii; ++i) {
1427
+ var span = times[i];
1428
+ var duration = span.end - span.start;
1429
+ out += rpad(span['name'], ' ', longest) + ' ' + duration + 'ms\n';
1430
+ }
1431
+ return out;
1432
+ }
1433
+ };
1434
+ return StatTimer;
1435
+ })();
1436
+
1437
+ PDFJS.createBlob = function createBlob(data, contentType) {
1438
+ if (typeof Blob !== 'undefined') {
1439
+ return new Blob([data], { type: contentType });
1440
+ }
1441
+ // Blob builder is deprecated in FF14 and removed in FF18.
1442
+ var bb = new MozBlobBuilder();
1443
+ bb.append(data);
1444
+ return bb.getBlob(contentType);
1445
+ };
1446
+
1447
+ PDFJS.createObjectURL = (function createObjectURLClosure() {
1448
+ // Blob/createObjectURL is not available, falling back to data schema.
1449
+ var digits =
1450
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
1451
+
1452
+ return function createObjectURL(data, contentType) {
1453
+ if (!PDFJS.disableCreateObjectURL &&
1454
+ typeof URL !== 'undefined' && URL.createObjectURL) {
1455
+ var blob = PDFJS.createBlob(data, contentType);
1456
+ return URL.createObjectURL(blob);
1457
+ }
1458
+
1459
+ var buffer = 'data:' + contentType + ';base64,';
1460
+ for (var i = 0, ii = data.length; i < ii; i += 3) {
1461
+ var b1 = data[i] & 0xFF;
1462
+ var b2 = data[i + 1] & 0xFF;
1463
+ var b3 = data[i + 2] & 0xFF;
1464
+ var d1 = b1 >> 2, d2 = ((b1 & 3) << 4) | (b2 >> 4);
1465
+ var d3 = i + 1 < ii ? ((b2 & 0xF) << 2) | (b3 >> 6) : 64;
1466
+ var d4 = i + 2 < ii ? (b3 & 0x3F) : 64;
1467
+ buffer += digits[d1] + digits[d2] + digits[d3] + digits[d4];
1468
+ }
1469
+ return buffer;
1470
+ };
1471
+ })();
1472
+
1473
+ function MessageHandler(name, comObj) {
1474
+ this.name = name;
1475
+ this.comObj = comObj;
1476
+ this.callbackIndex = 1;
1477
+ this.postMessageTransfers = true;
1478
+ var callbacksCapabilities = this.callbacksCapabilities = {};
1479
+ var ah = this.actionHandler = {};
1480
+
1481
+ ah['console_log'] = [function ahConsoleLog(data) {
1482
+ console.log.apply(console, data);
1483
+ }];
1484
+ ah['console_error'] = [function ahConsoleError(data) {
1485
+ console.error.apply(console, data);
1486
+ }];
1487
+ ah['_unsupported_feature'] = [function ah_unsupportedFeature(data) {
1488
+ UnsupportedManager.notify(data);
1489
+ }];
1490
+
1491
+ comObj.onmessage = function messageHandlerComObjOnMessage(event) {
1492
+ var data = event.data;
1493
+ if (data.isReply) {
1494
+ var callbackId = data.callbackId;
1495
+ if (data.callbackId in callbacksCapabilities) {
1496
+ var callback = callbacksCapabilities[callbackId];
1497
+ delete callbacksCapabilities[callbackId];
1498
+ if ('error' in data) {
1499
+ callback.reject(data.error);
1500
+ } else {
1501
+ callback.resolve(data.data);
1502
+ }
1503
+ } else {
1504
+ error('Cannot resolve callback ' + callbackId);
1505
+ }
1506
+ } else if (data.action in ah) {
1507
+ var action = ah[data.action];
1508
+ if (data.callbackId) {
1509
+ Promise.resolve().then(function () {
1510
+ return action[0].call(action[1], data.data);
1511
+ }).then(function (result) {
1512
+ comObj.postMessage({
1513
+ isReply: true,
1514
+ callbackId: data.callbackId,
1515
+ data: result
1516
+ });
1517
+ }, function (reason) {
1518
+ comObj.postMessage({
1519
+ isReply: true,
1520
+ callbackId: data.callbackId,
1521
+ error: reason
1522
+ });
1523
+ });
1524
+ } else {
1525
+ action[0].call(action[1], data.data);
1526
+ }
1527
+ } else {
1528
+ error('Unknown action from worker: ' + data.action);
1529
+ }
1530
+ };
1531
+ }
1532
+
1533
+ MessageHandler.prototype = {
1534
+ on: function messageHandlerOn(actionName, handler, scope) {
1535
+ var ah = this.actionHandler;
1536
+ if (ah[actionName]) {
1537
+ error('There is already an actionName called "' + actionName + '"');
1538
+ }
1539
+ ah[actionName] = [handler, scope];
1540
+ },
1541
+ /**
1542
+ * Sends a message to the comObj to invoke the action with the supplied data.
1543
+ * @param {String} actionName Action to call.
1544
+ * @param {JSON} data JSON data to send.
1545
+ * @param {Array} [transfers] Optional list of transfers/ArrayBuffers
1546
+ */
1547
+ send: function messageHandlerSend(actionName, data, transfers) {
1548
+ var message = {
1549
+ action: actionName,
1550
+ data: data
1551
+ };
1552
+ this.postMessage(message, transfers);
1553
+ },
1554
+ /**
1555
+ * Sends a message to the comObj to invoke the action with the supplied data.
1556
+ * Expects that other side will callback with the response.
1557
+ * @param {String} actionName Action to call.
1558
+ * @param {JSON} data JSON data to send.
1559
+ * @param {Array} [transfers] Optional list of transfers/ArrayBuffers.
1560
+ * @returns {Promise} Promise to be resolved with response data.
1561
+ */
1562
+ sendWithPromise:
1563
+ function messageHandlerSendWithPromise(actionName, data, transfers) {
1564
+ var callbackId = this.callbackIndex++;
1565
+ var message = {
1566
+ action: actionName,
1567
+ data: data,
1568
+ callbackId: callbackId
1569
+ };
1570
+ var capability = createPromiseCapability();
1571
+ this.callbacksCapabilities[callbackId] = capability;
1572
+ try {
1573
+ this.postMessage(message, transfers);
1574
+ } catch (e) {
1575
+ capability.reject(e);
1576
+ }
1577
+ return capability.promise;
1578
+ },
1579
+ /**
1580
+ * Sends raw message to the comObj.
1581
+ * @private
1582
+ * @param message {Object} Raw message.
1583
+ * @param transfers List of transfers/ArrayBuffers, or undefined.
1584
+ */
1585
+ postMessage: function (message, transfers) {
1586
+ if (transfers && this.postMessageTransfers) {
1587
+ this.comObj.postMessage(message, transfers);
1588
+ } else {
1589
+ this.comObj.postMessage(message);
1590
+ }
1591
+ }
1592
+ };
1593
+
1594
+ function loadJpegStream(id, imageUrl, objs) {
1595
+ var img = new Image();
1596
+ img.onload = (function loadJpegStream_onloadClosure() {
1597
+ objs.resolve(id, img);
1598
+ });
1599
+ img.onerror = (function loadJpegStream_onerrorClosure() {
1600
+ objs.resolve(id, null);
1601
+ warn('Error during JPEG image loading');
1602
+ });
1603
+ img.src = imageUrl;
1604
+ }
1605
+
1606
+
1607
+
1608
+
1609
+ var NetworkManager = (function NetworkManagerClosure() {
1610
+
1611
+ var OK_RESPONSE = 200;
1612
+ var PARTIAL_CONTENT_RESPONSE = 206;
1613
+
1614
+ function NetworkManager(url, args) {
1615
+ this.url = url;
1616
+ args = args || {};
1617
+ this.isHttp = /^https?:/i.test(url);
1618
+ this.httpHeaders = (this.isHttp && args.httpHeaders) || {};
1619
+ this.withCredentials = args.withCredentials || false;
1620
+ this.getXhr = args.getXhr ||
1621
+ function NetworkManager_getXhr() {
1622
+ return new XMLHttpRequest();
1623
+ };
1624
+
1625
+ this.currXhrId = 0;
1626
+ this.pendingRequests = {};
1627
+ this.loadedRequests = {};
1628
+ }
1629
+
1630
+ function getArrayBuffer(xhr) {
1631
+ var data = xhr.response;
1632
+ if (typeof data !== 'string') {
1633
+ return data;
1634
+ }
1635
+ var length = data.length;
1636
+ var array = new Uint8Array(length);
1637
+ for (var i = 0; i < length; i++) {
1638
+ array[i] = data.charCodeAt(i) & 0xFF;
1639
+ }
1640
+ return array.buffer;
1641
+ }
1642
+
1643
+ NetworkManager.prototype = {
1644
+ requestRange: function NetworkManager_requestRange(begin, end, listeners) {
1645
+ var args = {
1646
+ begin: begin,
1647
+ end: end
1648
+ };
1649
+ for (var prop in listeners) {
1650
+ args[prop] = listeners[prop];
1651
+ }
1652
+ return this.request(args);
1653
+ },
1654
+
1655
+ requestFull: function NetworkManager_requestFull(listeners) {
1656
+ return this.request(listeners);
1657
+ },
1658
+
1659
+ request: function NetworkManager_request(args) {
1660
+ var xhr = this.getXhr();
1661
+ var xhrId = this.currXhrId++;
1662
+ var pendingRequest = this.pendingRequests[xhrId] = {
1663
+ xhr: xhr
1664
+ };
1665
+
1666
+ xhr.open('GET', this.url);
1667
+ xhr.withCredentials = this.withCredentials;
1668
+ for (var property in this.httpHeaders) {
1669
+ var value = this.httpHeaders[property];
1670
+ if (typeof value === 'undefined') {
1671
+ continue;
1672
+ }
1673
+ xhr.setRequestHeader(property, value);
1674
+ }
1675
+ if (this.isHttp && 'begin' in args && 'end' in args) {
1676
+ var rangeStr = args.begin + '-' + (args.end - 1);
1677
+ xhr.setRequestHeader('Range', 'bytes=' + rangeStr);
1678
+ pendingRequest.expectedStatus = 206;
1679
+ } else {
1680
+ pendingRequest.expectedStatus = 200;
1681
+ }
1682
+
1683
+ if (args.onProgressiveData) {
1684
+ // Some legacy browsers might throw an exception.
1685
+ try {
1686
+ xhr.responseType = 'moz-chunked-arraybuffer';
1687
+ } catch(e) {}
1688
+ if (xhr.responseType === 'moz-chunked-arraybuffer') {
1689
+ pendingRequest.onProgressiveData = args.onProgressiveData;
1690
+ pendingRequest.mozChunked = true;
1691
+ } else {
1692
+ xhr.responseType = 'arraybuffer';
1693
+ }
1694
+ } else {
1695
+ xhr.responseType = 'arraybuffer';
1696
+ }
1697
+
1698
+ if (args.onError) {
1699
+ xhr.onerror = function(evt) {
1700
+ args.onError(xhr.status);
1701
+ };
1702
+ }
1703
+ xhr.onreadystatechange = this.onStateChange.bind(this, xhrId);
1704
+ xhr.onprogress = this.onProgress.bind(this, xhrId);
1705
+
1706
+ pendingRequest.onHeadersReceived = args.onHeadersReceived;
1707
+ pendingRequest.onDone = args.onDone;
1708
+ pendingRequest.onError = args.onError;
1709
+ pendingRequest.onProgress = args.onProgress;
1710
+
1711
+ xhr.send(null);
1712
+
1713
+ return xhrId;
1714
+ },
1715
+
1716
+ onProgress: function NetworkManager_onProgress(xhrId, evt) {
1717
+ var pendingRequest = this.pendingRequests[xhrId];
1718
+ if (!pendingRequest) {
1719
+ // Maybe abortRequest was called...
1720
+ return;
1721
+ }
1722
+
1723
+ if (pendingRequest.mozChunked) {
1724
+ var chunk = getArrayBuffer(pendingRequest.xhr);
1725
+ pendingRequest.onProgressiveData(chunk);
1726
+ }
1727
+
1728
+ var onProgress = pendingRequest.onProgress;
1729
+ if (onProgress) {
1730
+ onProgress(evt);
1731
+ }
1732
+ },
1733
+
1734
+ onStateChange: function NetworkManager_onStateChange(xhrId, evt) {
1735
+ var pendingRequest = this.pendingRequests[xhrId];
1736
+ if (!pendingRequest) {
1737
+ // Maybe abortRequest was called...
1738
+ return;
1739
+ }
1740
+
1741
+ var xhr = pendingRequest.xhr;
1742
+ if (xhr.readyState >= 2 && pendingRequest.onHeadersReceived) {
1743
+ pendingRequest.onHeadersReceived();
1744
+ delete pendingRequest.onHeadersReceived;
1745
+ }
1746
+
1747
+ if (xhr.readyState !== 4) {
1748
+ return;
1749
+ }
1750
+
1751
+ if (!(xhrId in this.pendingRequests)) {
1752
+ // The XHR request might have been aborted in onHeadersReceived()
1753
+ // callback, in which case we should abort request
1754
+ return;
1755
+ }
1756
+
1757
+ delete this.pendingRequests[xhrId];
1758
+
1759
+ // success status == 0 can be on ftp, file and other protocols
1760
+ if (xhr.status === 0 && this.isHttp) {
1761
+ if (pendingRequest.onError) {
1762
+ pendingRequest.onError(xhr.status);
1763
+ }
1764
+ return;
1765
+ }
1766
+ var xhrStatus = xhr.status || OK_RESPONSE;
1767
+
1768
+ // From http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35.2:
1769
+ // "A server MAY ignore the Range header". This means it's possible to
1770
+ // get a 200 rather than a 206 response from a range request.
1771
+ var ok_response_on_range_request =
1772
+ xhrStatus === OK_RESPONSE &&
1773
+ pendingRequest.expectedStatus === PARTIAL_CONTENT_RESPONSE;
1774
+
1775
+ if (!ok_response_on_range_request &&
1776
+ xhrStatus !== pendingRequest.expectedStatus) {
1777
+ if (pendingRequest.onError) {
1778
+ pendingRequest.onError(xhr.status);
1779
+ }
1780
+ return;
1781
+ }
1782
+
1783
+ this.loadedRequests[xhrId] = true;
1784
+
1785
+ var chunk = getArrayBuffer(xhr);
1786
+ if (xhrStatus === PARTIAL_CONTENT_RESPONSE) {
1787
+ var rangeHeader = xhr.getResponseHeader('Content-Range');
1788
+ var matches = /bytes (\d+)-(\d+)\/(\d+)/.exec(rangeHeader);
1789
+ var begin = parseInt(matches[1], 10);
1790
+ pendingRequest.onDone({
1791
+ begin: begin,
1792
+ chunk: chunk
1793
+ });
1794
+ } else if (pendingRequest.onProgressiveData) {
1795
+ pendingRequest.onDone(null);
1796
+ } else {
1797
+ pendingRequest.onDone({
1798
+ begin: 0,
1799
+ chunk: chunk
1800
+ });
1801
+ }
1802
+ },
1803
+
1804
+ hasPendingRequests: function NetworkManager_hasPendingRequests() {
1805
+ for (var xhrId in this.pendingRequests) {
1806
+ return true;
1807
+ }
1808
+ return false;
1809
+ },
1810
+
1811
+ getRequestXhr: function NetworkManager_getXhr(xhrId) {
1812
+ return this.pendingRequests[xhrId].xhr;
1813
+ },
1814
+
1815
+ isStreamingRequest: function NetworkManager_isStreamingRequest(xhrId) {
1816
+ return !!(this.pendingRequests[xhrId].onProgressiveData);
1817
+ },
1818
+
1819
+ isPendingRequest: function NetworkManager_isPendingRequest(xhrId) {
1820
+ return xhrId in this.pendingRequests;
1821
+ },
1822
+
1823
+ isLoadedRequest: function NetworkManager_isLoadedRequest(xhrId) {
1824
+ return xhrId in this.loadedRequests;
1825
+ },
1826
+
1827
+ abortAllRequests: function NetworkManager_abortAllRequests() {
1828
+ for (var xhrId in this.pendingRequests) {
1829
+ this.abortRequest(xhrId | 0);
1830
+ }
1831
+ },
1832
+
1833
+ abortRequest: function NetworkManager_abortRequest(xhrId) {
1834
+ var xhr = this.pendingRequests[xhrId].xhr;
1835
+ delete this.pendingRequests[xhrId];
1836
+ xhr.abort();
1837
+ }
1838
+ };
1839
+
1840
+ return NetworkManager;
1841
+ })();
1842
+
1843
+
1844
+ var ChunkedStream = (function ChunkedStreamClosure() {
1845
+ function ChunkedStream(length, chunkSize, manager) {
1846
+ this.bytes = new Uint8Array(length);
1847
+ this.start = 0;
1848
+ this.pos = 0;
1849
+ this.end = length;
1850
+ this.chunkSize = chunkSize;
1851
+ this.loadedChunks = [];
1852
+ this.numChunksLoaded = 0;
1853
+ this.numChunks = Math.ceil(length / chunkSize);
1854
+ this.manager = manager;
1855
+ this.progressiveDataLength = 0;
1856
+ this.lastSuccessfulEnsureByteChunk = -1; // a single-entry cache
1857
+ }
1858
+
1859
+ // required methods for a stream. if a particular stream does not
1860
+ // implement these, an error should be thrown
1861
+ ChunkedStream.prototype = {
1862
+
1863
+ getMissingChunks: function ChunkedStream_getMissingChunks() {
1864
+ var chunks = [];
1865
+ for (var chunk = 0, n = this.numChunks; chunk < n; ++chunk) {
1866
+ if (!this.loadedChunks[chunk]) {
1867
+ chunks.push(chunk);
1868
+ }
1869
+ }
1870
+ return chunks;
1871
+ },
1872
+
1873
+ getBaseStreams: function ChunkedStream_getBaseStreams() {
1874
+ return [this];
1875
+ },
1876
+
1877
+ allChunksLoaded: function ChunkedStream_allChunksLoaded() {
1878
+ return this.numChunksLoaded === this.numChunks;
1879
+ },
1880
+
1881
+ onReceiveData: function ChunkedStream_onReceiveData(begin, chunk) {
1882
+ var end = begin + chunk.byteLength;
1883
+
1884
+ assert(begin % this.chunkSize === 0, 'Bad begin offset: ' + begin);
1885
+ // Using this.length is inaccurate here since this.start can be moved
1886
+ // See ChunkedStream.moveStart()
1887
+ var length = this.bytes.length;
1888
+ assert(end % this.chunkSize === 0 || end === length,
1889
+ 'Bad end offset: ' + end);
1890
+
1891
+ this.bytes.set(new Uint8Array(chunk), begin);
1892
+ var chunkSize = this.chunkSize;
1893
+ var beginChunk = Math.floor(begin / chunkSize);
1894
+ var endChunk = Math.floor((end - 1) / chunkSize) + 1;
1895
+ var curChunk;
1896
+
1897
+ for (curChunk = beginChunk; curChunk < endChunk; ++curChunk) {
1898
+ if (!this.loadedChunks[curChunk]) {
1899
+ this.loadedChunks[curChunk] = true;
1900
+ ++this.numChunksLoaded;
1901
+ }
1902
+ }
1903
+ },
1904
+
1905
+ onReceiveProgressiveData:
1906
+ function ChunkedStream_onReceiveProgressiveData(data) {
1907
+ var position = this.progressiveDataLength;
1908
+ var beginChunk = Math.floor(position / this.chunkSize);
1909
+
1910
+ this.bytes.set(new Uint8Array(data), position);
1911
+ position += data.byteLength;
1912
+ this.progressiveDataLength = position;
1913
+ var endChunk = position >= this.end ? this.numChunks :
1914
+ Math.floor(position / this.chunkSize);
1915
+ var curChunk;
1916
+ for (curChunk = beginChunk; curChunk < endChunk; ++curChunk) {
1917
+ if (!this.loadedChunks[curChunk]) {
1918
+ this.loadedChunks[curChunk] = true;
1919
+ ++this.numChunksLoaded;
1920
+ }
1921
+ }
1922
+ },
1923
+
1924
+ ensureByte: function ChunkedStream_ensureByte(pos) {
1925
+ var chunk = Math.floor(pos / this.chunkSize);
1926
+ if (chunk === this.lastSuccessfulEnsureByteChunk) {
1927
+ return;
1928
+ }
1929
+
1930
+ if (!this.loadedChunks[chunk]) {
1931
+ throw new MissingDataException(pos, pos + 1);
1932
+ }
1933
+ this.lastSuccessfulEnsureByteChunk = chunk;
1934
+ },
1935
+
1936
+ ensureRange: function ChunkedStream_ensureRange(begin, end) {
1937
+ if (begin >= end) {
1938
+ return;
1939
+ }
1940
+
1941
+ if (end <= this.progressiveDataLength) {
1942
+ return;
1943
+ }
1944
+
1945
+ var chunkSize = this.chunkSize;
1946
+ var beginChunk = Math.floor(begin / chunkSize);
1947
+ var endChunk = Math.floor((end - 1) / chunkSize) + 1;
1948
+ for (var chunk = beginChunk; chunk < endChunk; ++chunk) {
1949
+ if (!this.loadedChunks[chunk]) {
1950
+ throw new MissingDataException(begin, end);
1951
+ }
1952
+ }
1953
+ },
1954
+
1955
+ nextEmptyChunk: function ChunkedStream_nextEmptyChunk(beginChunk) {
1956
+ var chunk, n;
1957
+ for (chunk = beginChunk, n = this.numChunks; chunk < n; ++chunk) {
1958
+ if (!this.loadedChunks[chunk]) {
1959
+ return chunk;
1960
+ }
1961
+ }
1962
+ // Wrap around to beginning
1963
+ for (chunk = 0; chunk < beginChunk; ++chunk) {
1964
+ if (!this.loadedChunks[chunk]) {
1965
+ return chunk;
1966
+ }
1967
+ }
1968
+ return null;
1969
+ },
1970
+
1971
+ hasChunk: function ChunkedStream_hasChunk(chunk) {
1972
+ return !!this.loadedChunks[chunk];
1973
+ },
1974
+
1975
+ get length() {
1976
+ return this.end - this.start;
1977
+ },
1978
+
1979
+ get isEmpty() {
1980
+ return this.length === 0;
1981
+ },
1982
+
1983
+ getByte: function ChunkedStream_getByte() {
1984
+ var pos = this.pos;
1985
+ if (pos >= this.end) {
1986
+ return -1;
1987
+ }
1988
+ this.ensureByte(pos);
1989
+ return this.bytes[this.pos++];
1990
+ },
1991
+
1992
+ getUint16: function ChunkedStream_getUint16() {
1993
+ var b0 = this.getByte();
1994
+ var b1 = this.getByte();
1995
+ if (b0 === -1 || b1 === -1) {
1996
+ return -1;
1997
+ }
1998
+ return (b0 << 8) + b1;
1999
+ },
2000
+
2001
+ getInt32: function ChunkedStream_getInt32() {
2002
+ var b0 = this.getByte();
2003
+ var b1 = this.getByte();
2004
+ var b2 = this.getByte();
2005
+ var b3 = this.getByte();
2006
+ return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
2007
+ },
2008
+
2009
+ // returns subarray of original buffer
2010
+ // should only be read
2011
+ getBytes: function ChunkedStream_getBytes(length) {
2012
+ var bytes = this.bytes;
2013
+ var pos = this.pos;
2014
+ var strEnd = this.end;
2015
+
2016
+ if (!length) {
2017
+ this.ensureRange(pos, strEnd);
2018
+ return bytes.subarray(pos, strEnd);
2019
+ }
2020
+
2021
+ var end = pos + length;
2022
+ if (end > strEnd) {
2023
+ end = strEnd;
2024
+ }
2025
+ this.ensureRange(pos, end);
2026
+
2027
+ this.pos = end;
2028
+ return bytes.subarray(pos, end);
2029
+ },
2030
+
2031
+ peekByte: function ChunkedStream_peekByte() {
2032
+ var peekedByte = this.getByte();
2033
+ this.pos--;
2034
+ return peekedByte;
2035
+ },
2036
+
2037
+ peekBytes: function ChunkedStream_peekBytes(length) {
2038
+ var bytes = this.getBytes(length);
2039
+ this.pos -= bytes.length;
2040
+ return bytes;
2041
+ },
2042
+
2043
+ getByteRange: function ChunkedStream_getBytes(begin, end) {
2044
+ this.ensureRange(begin, end);
2045
+ return this.bytes.subarray(begin, end);
2046
+ },
2047
+
2048
+ skip: function ChunkedStream_skip(n) {
2049
+ if (!n) {
2050
+ n = 1;
2051
+ }
2052
+ this.pos += n;
2053
+ },
2054
+
2055
+ reset: function ChunkedStream_reset() {
2056
+ this.pos = this.start;
2057
+ },
2058
+
2059
+ moveStart: function ChunkedStream_moveStart() {
2060
+ this.start = this.pos;
2061
+ },
2062
+
2063
+ makeSubStream: function ChunkedStream_makeSubStream(start, length, dict) {
2064
+ this.ensureRange(start, start + length);
2065
+
2066
+ function ChunkedStreamSubstream() {}
2067
+ ChunkedStreamSubstream.prototype = Object.create(this);
2068
+ ChunkedStreamSubstream.prototype.getMissingChunks = function() {
2069
+ var chunkSize = this.chunkSize;
2070
+ var beginChunk = Math.floor(this.start / chunkSize);
2071
+ var endChunk = Math.floor((this.end - 1) / chunkSize) + 1;
2072
+ var missingChunks = [];
2073
+ for (var chunk = beginChunk; chunk < endChunk; ++chunk) {
2074
+ if (!this.loadedChunks[chunk]) {
2075
+ missingChunks.push(chunk);
2076
+ }
2077
+ }
2078
+ return missingChunks;
2079
+ };
2080
+ var subStream = new ChunkedStreamSubstream();
2081
+ subStream.pos = subStream.start = start;
2082
+ subStream.end = start + length || this.end;
2083
+ subStream.dict = dict;
2084
+ return subStream;
2085
+ },
2086
+
2087
+ isStream: true
2088
+ };
2089
+
2090
+ return ChunkedStream;
2091
+ })();
2092
+
2093
+ var ChunkedStreamManager = (function ChunkedStreamManagerClosure() {
2094
+
2095
+ function ChunkedStreamManager(length, chunkSize, url, args) {
2096
+ this.stream = new ChunkedStream(length, chunkSize, this);
2097
+ this.length = length;
2098
+ this.chunkSize = chunkSize;
2099
+ this.url = url;
2100
+ this.disableAutoFetch = args.disableAutoFetch;
2101
+ var msgHandler = this.msgHandler = args.msgHandler;
2102
+
2103
+ if (args.chunkedViewerLoading) {
2104
+ msgHandler.on('OnDataRange', this.onReceiveData.bind(this));
2105
+ msgHandler.on('OnDataProgress', this.onProgress.bind(this));
2106
+ this.sendRequest = function ChunkedStreamManager_sendRequest(begin, end) {
2107
+ msgHandler.send('RequestDataRange', { begin: begin, end: end });
2108
+ };
2109
+ } else {
2110
+
2111
+ var getXhr = function getXhr() {
2112
+ return new XMLHttpRequest();
2113
+ };
2114
+ this.networkManager = new NetworkManager(this.url, {
2115
+ getXhr: getXhr,
2116
+ httpHeaders: args.httpHeaders,
2117
+ withCredentials: args.withCredentials
2118
+ });
2119
+ this.sendRequest = function ChunkedStreamManager_sendRequest(begin, end) {
2120
+ this.networkManager.requestRange(begin, end, {
2121
+ onDone: this.onReceiveData.bind(this),
2122
+ onProgress: this.onProgress.bind(this)
2123
+ });
2124
+ };
2125
+ }
2126
+
2127
+ this.currRequestId = 0;
2128
+
2129
+ this.chunksNeededByRequest = {};
2130
+ this.requestsByChunk = {};
2131
+ this.callbacksByRequest = {};
2132
+ this.progressiveDataLength = 0;
2133
+
2134
+ this._loadedStreamCapability = createPromiseCapability();
2135
+
2136
+ if (args.initialData) {
2137
+ this.onReceiveData({chunk: args.initialData});
2138
+ }
2139
+ }
2140
+
2141
+ ChunkedStreamManager.prototype = {
2142
+ onLoadedStream: function ChunkedStreamManager_getLoadedStream() {
2143
+ return this._loadedStreamCapability.promise;
2144
+ },
2145
+
2146
+ // Get all the chunks that are not yet loaded and groups them into
2147
+ // contiguous ranges to load in as few requests as possible
2148
+ requestAllChunks: function ChunkedStreamManager_requestAllChunks() {
2149
+ var missingChunks = this.stream.getMissingChunks();
2150
+ this.requestChunks(missingChunks);
2151
+ return this._loadedStreamCapability.promise;
2152
+ },
2153
+
2154
+ requestChunks: function ChunkedStreamManager_requestChunks(chunks,
2155
+ callback) {
2156
+ var requestId = this.currRequestId++;
2157
+
2158
+ var chunksNeeded;
2159
+ var i, ii;
2160
+ this.chunksNeededByRequest[requestId] = chunksNeeded = {};
2161
+ for (i = 0, ii = chunks.length; i < ii; i++) {
2162
+ if (!this.stream.hasChunk(chunks[i])) {
2163
+ chunksNeeded[chunks[i]] = true;
2164
+ }
2165
+ }
2166
+
2167
+ if (isEmptyObj(chunksNeeded)) {
2168
+ if (callback) {
2169
+ callback();
2170
+ }
2171
+ return;
2172
+ }
2173
+
2174
+ this.callbacksByRequest[requestId] = callback;
2175
+
2176
+ var chunksToRequest = [];
2177
+ for (var chunk in chunksNeeded) {
2178
+ chunk = chunk | 0;
2179
+ if (!(chunk in this.requestsByChunk)) {
2180
+ this.requestsByChunk[chunk] = [];
2181
+ chunksToRequest.push(chunk);
2182
+ }
2183
+ this.requestsByChunk[chunk].push(requestId);
2184
+ }
2185
+
2186
+ if (!chunksToRequest.length) {
2187
+ return;
2188
+ }
2189
+
2190
+ var groupedChunksToRequest = this.groupChunks(chunksToRequest);
2191
+
2192
+ for (i = 0; i < groupedChunksToRequest.length; ++i) {
2193
+ var groupedChunk = groupedChunksToRequest[i];
2194
+ var begin = groupedChunk.beginChunk * this.chunkSize;
2195
+ var end = Math.min(groupedChunk.endChunk * this.chunkSize, this.length);
2196
+ this.sendRequest(begin, end);
2197
+ }
2198
+ },
2199
+
2200
+ getStream: function ChunkedStreamManager_getStream() {
2201
+ return this.stream;
2202
+ },
2203
+
2204
+ // Loads any chunks in the requested range that are not yet loaded
2205
+ requestRange: function ChunkedStreamManager_requestRange(
2206
+ begin, end, callback) {
2207
+
2208
+ end = Math.min(end, this.length);
2209
+
2210
+ var beginChunk = this.getBeginChunk(begin);
2211
+ var endChunk = this.getEndChunk(end);
2212
+
2213
+ var chunks = [];
2214
+ for (var chunk = beginChunk; chunk < endChunk; ++chunk) {
2215
+ chunks.push(chunk);
2216
+ }
2217
+
2218
+ this.requestChunks(chunks, callback);
2219
+ },
2220
+
2221
+ requestRanges: function ChunkedStreamManager_requestRanges(ranges,
2222
+ callback) {
2223
+ ranges = ranges || [];
2224
+ var chunksToRequest = [];
2225
+
2226
+ for (var i = 0; i < ranges.length; i++) {
2227
+ var beginChunk = this.getBeginChunk(ranges[i].begin);
2228
+ var endChunk = this.getEndChunk(ranges[i].end);
2229
+ for (var chunk = beginChunk; chunk < endChunk; ++chunk) {
2230
+ if (chunksToRequest.indexOf(chunk) < 0) {
2231
+ chunksToRequest.push(chunk);
2232
+ }
2233
+ }
2234
+ }
2235
+
2236
+ chunksToRequest.sort(function(a, b) { return a - b; });
2237
+ this.requestChunks(chunksToRequest, callback);
2238
+ },
2239
+
2240
+ // Groups a sorted array of chunks into as few continguous larger
2241
+ // chunks as possible
2242
+ groupChunks: function ChunkedStreamManager_groupChunks(chunks) {
2243
+ var groupedChunks = [];
2244
+ var beginChunk = -1;
2245
+ var prevChunk = -1;
2246
+ for (var i = 0; i < chunks.length; ++i) {
2247
+ var chunk = chunks[i];
2248
+
2249
+ if (beginChunk < 0) {
2250
+ beginChunk = chunk;
2251
+ }
2252
+
2253
+ if (prevChunk >= 0 && prevChunk + 1 !== chunk) {
2254
+ groupedChunks.push({ beginChunk: beginChunk,
2255
+ endChunk: prevChunk + 1 });
2256
+ beginChunk = chunk;
2257
+ }
2258
+ if (i + 1 === chunks.length) {
2259
+ groupedChunks.push({ beginChunk: beginChunk,
2260
+ endChunk: chunk + 1 });
2261
+ }
2262
+
2263
+ prevChunk = chunk;
2264
+ }
2265
+ return groupedChunks;
2266
+ },
2267
+
2268
+ onProgress: function ChunkedStreamManager_onProgress(args) {
2269
+ var bytesLoaded = (this.stream.numChunksLoaded * this.chunkSize +
2270
+ args.loaded);
2271
+ this.msgHandler.send('DocProgress', {
2272
+ loaded: bytesLoaded,
2273
+ total: this.length
2274
+ });
2275
+ },
2276
+
2277
+ onReceiveData: function ChunkedStreamManager_onReceiveData(args) {
2278
+ var chunk = args.chunk;
2279
+ var isProgressive = args.begin === undefined;
2280
+ var begin = isProgressive ? this.progressiveDataLength : args.begin;
2281
+ var end = begin + chunk.byteLength;
2282
+
2283
+ var beginChunk = Math.floor(begin / this.chunkSize);
2284
+ var endChunk = end < this.length ? Math.floor(end / this.chunkSize) :
2285
+ Math.ceil(end / this.chunkSize);
2286
+
2287
+ if (isProgressive) {
2288
+ this.stream.onReceiveProgressiveData(chunk);
2289
+ this.progressiveDataLength = end;
2290
+ } else {
2291
+ this.stream.onReceiveData(begin, chunk);
2292
+ }
2293
+
2294
+ if (this.stream.allChunksLoaded()) {
2295
+ this._loadedStreamCapability.resolve(this.stream);
2296
+ }
2297
+
2298
+ var loadedRequests = [];
2299
+ var i, requestId;
2300
+ for (chunk = beginChunk; chunk < endChunk; ++chunk) {
2301
+ // The server might return more chunks than requested
2302
+ var requestIds = this.requestsByChunk[chunk] || [];
2303
+ delete this.requestsByChunk[chunk];
2304
+
2305
+ for (i = 0; i < requestIds.length; ++i) {
2306
+ requestId = requestIds[i];
2307
+ var chunksNeeded = this.chunksNeededByRequest[requestId];
2308
+ if (chunk in chunksNeeded) {
2309
+ delete chunksNeeded[chunk];
2310
+ }
2311
+
2312
+ if (!isEmptyObj(chunksNeeded)) {
2313
+ continue;
2314
+ }
2315
+
2316
+ loadedRequests.push(requestId);
2317
+ }
2318
+ }
2319
+
2320
+ // If there are no pending requests, automatically fetch the next
2321
+ // unfetched chunk of the PDF
2322
+ if (!this.disableAutoFetch && isEmptyObj(this.requestsByChunk)) {
2323
+ var nextEmptyChunk;
2324
+ if (this.stream.numChunksLoaded === 1) {
2325
+ // This is a special optimization so that after fetching the first
2326
+ // chunk, rather than fetching the second chunk, we fetch the last
2327
+ // chunk.
2328
+ var lastChunk = this.stream.numChunks - 1;
2329
+ if (!this.stream.hasChunk(lastChunk)) {
2330
+ nextEmptyChunk = lastChunk;
2331
+ }
2332
+ } else {
2333
+ nextEmptyChunk = this.stream.nextEmptyChunk(endChunk);
2334
+ }
2335
+ if (isInt(nextEmptyChunk)) {
2336
+ this.requestChunks([nextEmptyChunk]);
2337
+ }
2338
+ }
2339
+
2340
+ for (i = 0; i < loadedRequests.length; ++i) {
2341
+ requestId = loadedRequests[i];
2342
+ var callback = this.callbacksByRequest[requestId];
2343
+ delete this.callbacksByRequest[requestId];
2344
+ if (callback) {
2345
+ callback();
2346
+ }
2347
+ }
2348
+
2349
+ this.msgHandler.send('DocProgress', {
2350
+ loaded: this.stream.numChunksLoaded * this.chunkSize,
2351
+ total: this.length
2352
+ });
2353
+ },
2354
+
2355
+ onError: function ChunkedStreamManager_onError(err) {
2356
+ this._loadedStreamCapability.reject(err);
2357
+ },
2358
+
2359
+ getBeginChunk: function ChunkedStreamManager_getBeginChunk(begin) {
2360
+ var chunk = Math.floor(begin / this.chunkSize);
2361
+ return chunk;
2362
+ },
2363
+
2364
+ getEndChunk: function ChunkedStreamManager_getEndChunk(end) {
2365
+ if (end % this.chunkSize === 0) {
2366
+ return end / this.chunkSize;
2367
+ }
2368
+
2369
+ // 0 -> 0
2370
+ // 1 -> 1
2371
+ // 99 -> 1
2372
+ // 100 -> 1
2373
+ // 101 -> 2
2374
+ var chunk = Math.floor((end - 1) / this.chunkSize) + 1;
2375
+ return chunk;
2376
+ }
2377
+ };
2378
+
2379
+ return ChunkedStreamManager;
2380
+ })();
2381
+
2382
+
2383
+ // The maximum number of bytes fetched per range request
2384
+ var RANGE_CHUNK_SIZE = 65536;
2385
+
2386
+ // TODO(mack): Make use of PDFJS.Util.inherit() when it becomes available
2387
+ var BasePdfManager = (function BasePdfManagerClosure() {
2388
+ function BasePdfManager() {
2389
+ throw new Error('Cannot initialize BaseManagerManager');
2390
+ }
2391
+
2392
+ BasePdfManager.prototype = {
2393
+ onLoadedStream: function BasePdfManager_onLoadedStream() {
2394
+ throw new NotImplementedException();
2395
+ },
2396
+
2397
+ ensureDoc: function BasePdfManager_ensureDoc(prop, args) {
2398
+ return this.ensure(this.pdfDocument, prop, args);
2399
+ },
2400
+
2401
+ ensureXRef: function BasePdfManager_ensureXRef(prop, args) {
2402
+ return this.ensure(this.pdfDocument.xref, prop, args);
2403
+ },
2404
+
2405
+ ensureCatalog: function BasePdfManager_ensureCatalog(prop, args) {
2406
+ return this.ensure(this.pdfDocument.catalog, prop, args);
2407
+ },
2408
+
2409
+ getPage: function BasePdfManager_pagePage(pageIndex) {
2410
+ return this.pdfDocument.getPage(pageIndex);
2411
+ },
2412
+
2413
+ cleanup: function BasePdfManager_cleanup() {
2414
+ return this.pdfDocument.cleanup();
2415
+ },
2416
+
2417
+ ensure: function BasePdfManager_ensure(obj, prop, args) {
2418
+ return new NotImplementedException();
2419
+ },
2420
+
2421
+ requestRange: function BasePdfManager_ensure(begin, end) {
2422
+ return new NotImplementedException();
2423
+ },
2424
+
2425
+ requestLoadedStream: function BasePdfManager_requestLoadedStream() {
2426
+ return new NotImplementedException();
2427
+ },
2428
+
2429
+ sendProgressiveData: function BasePdfManager_sendProgressiveData(chunk) {
2430
+ return new NotImplementedException();
2431
+ },
2432
+
2433
+ updatePassword: function BasePdfManager_updatePassword(password) {
2434
+ this.pdfDocument.xref.password = this.password = password;
2435
+ if (this._passwordChangedCapability) {
2436
+ this._passwordChangedCapability.resolve();
2437
+ }
2438
+ },
2439
+
2440
+ passwordChanged: function BasePdfManager_passwordChanged() {
2441
+ this._passwordChangedCapability = createPromiseCapability();
2442
+ return this._passwordChangedCapability.promise;
2443
+ },
2444
+
2445
+ terminate: function BasePdfManager_terminate() {
2446
+ return new NotImplementedException();
2447
+ }
2448
+ };
2449
+
2450
+ return BasePdfManager;
2451
+ })();
2452
+
2453
+ var LocalPdfManager = (function LocalPdfManagerClosure() {
2454
+ function LocalPdfManager(data, password) {
2455
+ var stream = new Stream(data);
2456
+ this.pdfDocument = new PDFDocument(this, stream, password);
2457
+ this._loadedStreamCapability = createPromiseCapability();
2458
+ this._loadedStreamCapability.resolve(stream);
2459
+ }
2460
+
2461
+ LocalPdfManager.prototype = Object.create(BasePdfManager.prototype);
2462
+ LocalPdfManager.prototype.constructor = LocalPdfManager;
2463
+
2464
+ LocalPdfManager.prototype.ensure =
2465
+ function LocalPdfManager_ensure(obj, prop, args) {
2466
+ return new Promise(function (resolve, reject) {
2467
+ try {
2468
+ var value = obj[prop];
2469
+ var result;
2470
+ if (typeof value === 'function') {
2471
+ result = value.apply(obj, args);
2472
+ } else {
2473
+ result = value;
2474
+ }
2475
+ resolve(result);
2476
+ } catch (e) {
2477
+ reject(e);
2478
+ }
2479
+ });
2480
+ };
2481
+
2482
+ LocalPdfManager.prototype.requestRange =
2483
+ function LocalPdfManager_requestRange(begin, end) {
2484
+ return Promise.resolve();
2485
+ };
2486
+
2487
+ LocalPdfManager.prototype.requestLoadedStream =
2488
+ function LocalPdfManager_requestLoadedStream() {
2489
+ };
2490
+
2491
+ LocalPdfManager.prototype.onLoadedStream =
2492
+ function LocalPdfManager_getLoadedStream() {
2493
+ return this._loadedStreamCapability.promise;
2494
+ };
2495
+
2496
+ LocalPdfManager.prototype.terminate =
2497
+ function LocalPdfManager_terminate() {
2498
+ return;
2499
+ };
2500
+
2501
+ return LocalPdfManager;
2502
+ })();
2503
+
2504
+ var NetworkPdfManager = (function NetworkPdfManagerClosure() {
2505
+ function NetworkPdfManager(args, msgHandler) {
2506
+
2507
+ this.msgHandler = msgHandler;
2508
+
2509
+ var params = {
2510
+ msgHandler: msgHandler,
2511
+ httpHeaders: args.httpHeaders,
2512
+ withCredentials: args.withCredentials,
2513
+ chunkedViewerLoading: args.chunkedViewerLoading,
2514
+ disableAutoFetch: args.disableAutoFetch,
2515
+ initialData: args.initialData
2516
+ };
2517
+ this.streamManager = new ChunkedStreamManager(args.length, RANGE_CHUNK_SIZE,
2518
+ args.url, params);
2519
+
2520
+ this.pdfDocument = new PDFDocument(this, this.streamManager.getStream(),
2521
+ args.password);
2522
+ }
2523
+
2524
+ NetworkPdfManager.prototype = Object.create(BasePdfManager.prototype);
2525
+ NetworkPdfManager.prototype.constructor = NetworkPdfManager;
2526
+
2527
+ NetworkPdfManager.prototype.ensure =
2528
+ function NetworkPdfManager_ensure(obj, prop, args) {
2529
+ var pdfManager = this;
2530
+
2531
+ return new Promise(function (resolve, reject) {
2532
+ function ensureHelper() {
2533
+ try {
2534
+ var result;
2535
+ var value = obj[prop];
2536
+ if (typeof value === 'function') {
2537
+ result = value.apply(obj, args);
2538
+ } else {
2539
+ result = value;
2540
+ }
2541
+ resolve(result);
2542
+ } catch(e) {
2543
+ if (!(e instanceof MissingDataException)) {
2544
+ reject(e);
2545
+ return;
2546
+ }
2547
+ pdfManager.streamManager.requestRange(e.begin, e.end, ensureHelper);
2548
+ }
2549
+ }
2550
+
2551
+ ensureHelper();
2552
+ });
2553
+ };
2554
+
2555
+ NetworkPdfManager.prototype.requestRange =
2556
+ function NetworkPdfManager_requestRange(begin, end) {
2557
+ return new Promise(function (resolve) {
2558
+ this.streamManager.requestRange(begin, end, function() {
2559
+ resolve();
2560
+ });
2561
+ }.bind(this));
2562
+ };
2563
+
2564
+ NetworkPdfManager.prototype.requestLoadedStream =
2565
+ function NetworkPdfManager_requestLoadedStream() {
2566
+ this.streamManager.requestAllChunks();
2567
+ };
2568
+
2569
+ NetworkPdfManager.prototype.sendProgressiveData =
2570
+ function NetworkPdfManager_sendProgressiveData(chunk) {
2571
+ this.streamManager.onReceiveData({ chunk: chunk });
2572
+ };
2573
+
2574
+ NetworkPdfManager.prototype.onLoadedStream =
2575
+ function NetworkPdfManager_getLoadedStream() {
2576
+ return this.streamManager.onLoadedStream();
2577
+ };
2578
+
2579
+ NetworkPdfManager.prototype.terminate =
2580
+ function NetworkPdfManager_terminate() {
2581
+ this.streamManager.networkManager.abortAllRequests();
2582
+ };
2583
+
2584
+ return NetworkPdfManager;
2585
+ })();
2586
+
2587
+
2588
+ var Page = (function PageClosure() {
2589
+
2590
+ var LETTER_SIZE_MEDIABOX = [0, 0, 612, 792];
2591
+
2592
+ function Page(pdfManager, xref, pageIndex, pageDict, ref, fontCache) {
2593
+ this.pdfManager = pdfManager;
2594
+ this.pageIndex = pageIndex;
2595
+ this.pageDict = pageDict;
2596
+ this.xref = xref;
2597
+ this.ref = ref;
2598
+ this.fontCache = fontCache;
2599
+ this.idCounters = {
2600
+ obj: 0
2601
+ };
2602
+ this.resourcesPromise = null;
2603
+ }
2604
+
2605
+ Page.prototype = {
2606
+ getPageProp: function Page_getPageProp(key) {
2607
+ return this.pageDict.get(key);
2608
+ },
2609
+
2610
+ getInheritedPageProp: function Page_inheritPageProp(key) {
2611
+ var dict = this.pageDict;
2612
+ var value = dict.get(key);
2613
+ while (value === undefined) {
2614
+ dict = dict.get('Parent');
2615
+ if (!dict) {
2616
+ break;
2617
+ }
2618
+ value = dict.get(key);
2619
+ }
2620
+ return value;
2621
+ },
2622
+
2623
+ get content() {
2624
+ return this.getPageProp('Contents');
2625
+ },
2626
+
2627
+ get resources() {
2628
+ var value = this.getInheritedPageProp('Resources');
2629
+ // For robustness: The spec states that a \Resources entry has to be
2630
+ // present, but can be empty. Some document omit it still. In this case
2631
+ // return an empty dictionary:
2632
+ if (value === undefined) {
2633
+ value = Dict.empty;
2634
+ }
2635
+ return shadow(this, 'resources', value);
2636
+ },
2637
+
2638
+ get mediaBox() {
2639
+ var obj = this.getInheritedPageProp('MediaBox');
2640
+ // Reset invalid media box to letter size.
2641
+ if (!isArray(obj) || obj.length !== 4) {
2642
+ obj = LETTER_SIZE_MEDIABOX;
2643
+ }
2644
+ return shadow(this, 'mediaBox', obj);
2645
+ },
2646
+
2647
+ get view() {
2648
+ var mediaBox = this.mediaBox;
2649
+ var cropBox = this.getInheritedPageProp('CropBox');
2650
+ if (!isArray(cropBox) || cropBox.length !== 4) {
2651
+ return shadow(this, 'view', mediaBox);
2652
+ }
2653
+
2654
+ // From the spec, 6th ed., p.963:
2655
+ // "The crop, bleed, trim, and art boxes should not ordinarily
2656
+ // extend beyond the boundaries of the media box. If they do, they are
2657
+ // effectively reduced to their intersection with the media box."
2658
+ cropBox = Util.intersect(cropBox, mediaBox);
2659
+ if (!cropBox) {
2660
+ return shadow(this, 'view', mediaBox);
2661
+ }
2662
+ return shadow(this, 'view', cropBox);
2663
+ },
2664
+
2665
+ get annotationRefs() {
2666
+ return shadow(this, 'annotationRefs',
2667
+ this.getInheritedPageProp('Annots'));
2668
+ },
2669
+
2670
+ get rotate() {
2671
+ var rotate = this.getInheritedPageProp('Rotate') || 0;
2672
+ // Normalize rotation so it's a multiple of 90 and between 0 and 270
2673
+ if (rotate % 90 !== 0) {
2674
+ rotate = 0;
2675
+ } else if (rotate >= 360) {
2676
+ rotate = rotate % 360;
2677
+ } else if (rotate < 0) {
2678
+ // The spec doesn't cover negatives, assume its counterclockwise
2679
+ // rotation. The following is the other implementation of modulo.
2680
+ rotate = ((rotate % 360) + 360) % 360;
2681
+ }
2682
+ return shadow(this, 'rotate', rotate);
2683
+ },
2684
+
2685
+ getContentStream: function Page_getContentStream() {
2686
+ var content = this.content;
2687
+ var stream;
2688
+ if (isArray(content)) {
2689
+ // fetching items
2690
+ var xref = this.xref;
2691
+ var i, n = content.length;
2692
+ var streams = [];
2693
+ for (i = 0; i < n; ++i) {
2694
+ streams.push(xref.fetchIfRef(content[i]));
2695
+ }
2696
+ stream = new StreamsSequenceStream(streams);
2697
+ } else if (isStream(content)) {
2698
+ stream = content;
2699
+ } else {
2700
+ // replacing non-existent page content with empty one
2701
+ stream = new NullStream();
2702
+ }
2703
+ return stream;
2704
+ },
2705
+
2706
+ loadResources: function Page_loadResources(keys) {
2707
+ if (!this.resourcesPromise) {
2708
+ // TODO: add async getInheritedPageProp and remove this.
2709
+ this.resourcesPromise = this.pdfManager.ensure(this, 'resources');
2710
+ }
2711
+ return this.resourcesPromise.then(function resourceSuccess() {
2712
+ var objectLoader = new ObjectLoader(this.resources.map,
2713
+ keys,
2714
+ this.xref);
2715
+ return objectLoader.load();
2716
+ }.bind(this));
2717
+ },
2718
+
2719
+ getOperatorList: function Page_getOperatorList(handler, intent) {
2720
+ var self = this;
2721
+
2722
+ var pdfManager = this.pdfManager;
2723
+ var contentStreamPromise = pdfManager.ensure(this, 'getContentStream',
2724
+ []);
2725
+ var resourcesPromise = this.loadResources([
2726
+ 'ExtGState',
2727
+ 'ColorSpace',
2728
+ 'Pattern',
2729
+ 'Shading',
2730
+ 'XObject',
2731
+ 'Font'
2732
+ // ProcSet
2733
+ // Properties
2734
+ ]);
2735
+
2736
+ var partialEvaluator = new PartialEvaluator(pdfManager, this.xref,
2737
+ handler, this.pageIndex,
2738
+ 'p' + this.pageIndex + '_',
2739
+ this.idCounters,
2740
+ this.fontCache);
2741
+
2742
+ var dataPromises = Promise.all([contentStreamPromise, resourcesPromise]);
2743
+ var pageListPromise = dataPromises.then(function(data) {
2744
+ var contentStream = data[0];
2745
+ var opList = new OperatorList(intent, handler, self.pageIndex);
2746
+
2747
+ handler.send('StartRenderPage', {
2748
+ transparency: partialEvaluator.hasBlendModes(self.resources),
2749
+ pageIndex: self.pageIndex,
2750
+ intent: intent
2751
+ });
2752
+ return partialEvaluator.getOperatorList(contentStream, self.resources,
2753
+ opList).then(function () {
2754
+ return opList;
2755
+ });
2756
+ });
2757
+
2758
+ var annotationsPromise = pdfManager.ensure(this, 'annotations');
2759
+ return Promise.all([pageListPromise, annotationsPromise]).then(
2760
+ function(datas) {
2761
+ var pageOpList = datas[0];
2762
+ var annotations = datas[1];
2763
+
2764
+ if (annotations.length === 0) {
2765
+ pageOpList.flush(true);
2766
+ return pageOpList;
2767
+ }
2768
+
2769
+ var annotationsReadyPromise = Annotation.appendToOperatorList(
2770
+ annotations, pageOpList, pdfManager, partialEvaluator, intent);
2771
+ return annotationsReadyPromise.then(function () {
2772
+ pageOpList.flush(true);
2773
+ return pageOpList;
2774
+ });
2775
+ });
2776
+ },
2777
+
2778
+ extractTextContent: function Page_extractTextContent() {
2779
+ var handler = {
2780
+ on: function nullHandlerOn() {},
2781
+ send: function nullHandlerSend() {}
2782
+ };
2783
+
2784
+ var self = this;
2785
+
2786
+ var pdfManager = this.pdfManager;
2787
+ var contentStreamPromise = pdfManager.ensure(this, 'getContentStream',
2788
+ []);
2789
+
2790
+ var resourcesPromise = this.loadResources([
2791
+ 'ExtGState',
2792
+ 'XObject',
2793
+ 'Font'
2794
+ ]);
2795
+
2796
+ var dataPromises = Promise.all([contentStreamPromise,
2797
+ resourcesPromise]);
2798
+ return dataPromises.then(function(data) {
2799
+ var contentStream = data[0];
2800
+ var partialEvaluator = new PartialEvaluator(pdfManager, self.xref,
2801
+ handler, self.pageIndex,
2802
+ 'p' + self.pageIndex + '_',
2803
+ self.idCounters,
2804
+ self.fontCache);
2805
+
2806
+ return partialEvaluator.getTextContent(contentStream,
2807
+ self.resources);
2808
+ });
2809
+ },
2810
+
2811
+ getAnnotationsData: function Page_getAnnotationsData() {
2812
+ var annotations = this.annotations;
2813
+ var annotationsData = [];
2814
+ for (var i = 0, n = annotations.length; i < n; ++i) {
2815
+ annotationsData.push(annotations[i].getData());
2816
+ }
2817
+ return annotationsData;
2818
+ },
2819
+
2820
+ get annotations() {
2821
+ var annotations = [];
2822
+ var annotationRefs = (this.annotationRefs || []);
2823
+ for (var i = 0, n = annotationRefs.length; i < n; ++i) {
2824
+ var annotationRef = annotationRefs[i];
2825
+ var annotation = Annotation.fromRef(this.xref, annotationRef);
2826
+ if (annotation) {
2827
+ annotations.push(annotation);
2828
+ }
2829
+ }
2830
+ return shadow(this, 'annotations', annotations);
2831
+ }
2832
+ };
2833
+
2834
+ return Page;
2835
+ })();
2836
+
2837
+ /**
2838
+ * The `PDFDocument` holds all the data of the PDF file. Compared to the
2839
+ * `PDFDoc`, this one doesn't have any job management code.
2840
+ * Right now there exists one PDFDocument on the main thread + one object
2841
+ * for each worker. If there is no worker support enabled, there are two
2842
+ * `PDFDocument` objects on the main thread created.
2843
+ */
2844
+ var PDFDocument = (function PDFDocumentClosure() {
2845
+ var FINGERPRINT_FIRST_BYTES = 1024;
2846
+ var EMPTY_FINGERPRINT = '\x00\x00\x00\x00\x00\x00\x00' +
2847
+ '\x00\x00\x00\x00\x00\x00\x00\x00\x00';
2848
+
2849
+ function PDFDocument(pdfManager, arg, password) {
2850
+ if (isStream(arg)) {
2851
+ init.call(this, pdfManager, arg, password);
2852
+ } else if (isArrayBuffer(arg)) {
2853
+ init.call(this, pdfManager, new Stream(arg), password);
2854
+ } else {
2855
+ error('PDFDocument: Unknown argument type');
2856
+ }
2857
+ }
2858
+
2859
+ function init(pdfManager, stream, password) {
2860
+ assert(stream.length > 0, 'stream must have data');
2861
+ this.pdfManager = pdfManager;
2862
+ this.stream = stream;
2863
+ var xref = new XRef(this.stream, password, pdfManager);
2864
+ this.xref = xref;
2865
+ }
2866
+
2867
+ function find(stream, needle, limit, backwards) {
2868
+ var pos = stream.pos;
2869
+ var end = stream.end;
2870
+ var strBuf = [];
2871
+ if (pos + limit > end) {
2872
+ limit = end - pos;
2873
+ }
2874
+ for (var n = 0; n < limit; ++n) {
2875
+ strBuf.push(String.fromCharCode(stream.getByte()));
2876
+ }
2877
+ var str = strBuf.join('');
2878
+ stream.pos = pos;
2879
+ var index = backwards ? str.lastIndexOf(needle) : str.indexOf(needle);
2880
+ if (index === -1) {
2881
+ return false; /* not found */
2882
+ }
2883
+ stream.pos += index;
2884
+ return true; /* found */
2885
+ }
2886
+
2887
+ var DocumentInfoValidators = {
2888
+ get entries() {
2889
+ // Lazily build this since all the validation functions below are not
2890
+ // defined until after this file loads.
2891
+ return shadow(this, 'entries', {
2892
+ Title: isString,
2893
+ Author: isString,
2894
+ Subject: isString,
2895
+ Keywords: isString,
2896
+ Creator: isString,
2897
+ Producer: isString,
2898
+ CreationDate: isString,
2899
+ ModDate: isString,
2900
+ Trapped: isName
2901
+ });
2902
+ }
2903
+ };
2904
+
2905
+ PDFDocument.prototype = {
2906
+ parse: function PDFDocument_parse(recoveryMode) {
2907
+ this.setup(recoveryMode);
2908
+ try {
2909
+ // checking if AcroForm is present
2910
+ this.acroForm = this.catalog.catDict.get('AcroForm');
2911
+ if (this.acroForm) {
2912
+ this.xfa = this.acroForm.get('XFA');
2913
+ var fields = this.acroForm.get('Fields');
2914
+ if ((!fields || !isArray(fields) || fields.length === 0) &&
2915
+ !this.xfa) {
2916
+ // no fields and no XFA -- not a form (?)
2917
+ this.acroForm = null;
2918
+ }
2919
+ }
2920
+ } catch (ex) {
2921
+ info('Something wrong with AcroForm entry');
2922
+ this.acroForm = null;
2923
+ }
2924
+ },
2925
+
2926
+ get linearization() {
2927
+ var linearization = null;
2928
+ if (this.stream.length) {
2929
+ try {
2930
+ linearization = Linearization.create(this.stream);
2931
+ } catch (err) {
2932
+ if (err instanceof MissingDataException) {
2933
+ throw err;
2934
+ }
2935
+ info(err);
2936
+ }
2937
+ }
2938
+ // shadow the prototype getter with a data property
2939
+ return shadow(this, 'linearization', linearization);
2940
+ },
2941
+ get startXRef() {
2942
+ var stream = this.stream;
2943
+ var startXRef = 0;
2944
+ var linearization = this.linearization;
2945
+ if (linearization) {
2946
+ // Find end of first obj.
2947
+ stream.reset();
2948
+ if (find(stream, 'endobj', 1024)) {
2949
+ startXRef = stream.pos + 6;
2950
+ }
2951
+ } else {
2952
+ // Find startxref by jumping backward from the end of the file.
2953
+ var step = 1024;
2954
+ var found = false, pos = stream.end;
2955
+ while (!found && pos > 0) {
2956
+ pos -= step - 'startxref'.length;
2957
+ if (pos < 0) {
2958
+ pos = 0;
2959
+ }
2960
+ stream.pos = pos;
2961
+ found = find(stream, 'startxref', step, true);
2962
+ }
2963
+ if (found) {
2964
+ stream.skip(9);
2965
+ var ch;
2966
+ do {
2967
+ ch = stream.getByte();
2968
+ } while (Lexer.isSpace(ch));
2969
+ var str = '';
2970
+ while (ch >= 0x20 && ch <= 0x39) { // < '9'
2971
+ str += String.fromCharCode(ch);
2972
+ ch = stream.getByte();
2973
+ }
2974
+ startXRef = parseInt(str, 10);
2975
+ if (isNaN(startXRef)) {
2976
+ startXRef = 0;
2977
+ }
2978
+ }
2979
+ }
2980
+ // shadow the prototype getter with a data property
2981
+ return shadow(this, 'startXRef', startXRef);
2982
+ },
2983
+ get mainXRefEntriesOffset() {
2984
+ var mainXRefEntriesOffset = 0;
2985
+ var linearization = this.linearization;
2986
+ if (linearization) {
2987
+ mainXRefEntriesOffset = linearization.mainXRefEntriesOffset;
2988
+ }
2989
+ // shadow the prototype getter with a data property
2990
+ return shadow(this, 'mainXRefEntriesOffset', mainXRefEntriesOffset);
2991
+ },
2992
+ // Find the header, remove leading garbage and setup the stream
2993
+ // starting from the header.
2994
+ checkHeader: function PDFDocument_checkHeader() {
2995
+ var stream = this.stream;
2996
+ stream.reset();
2997
+ if (find(stream, '%PDF-', 1024)) {
2998
+ // Found the header, trim off any garbage before it.
2999
+ stream.moveStart();
3000
+ // Reading file format version
3001
+ var MAX_VERSION_LENGTH = 12;
3002
+ var version = '', ch;
3003
+ while ((ch = stream.getByte()) > 0x20) { // SPACE
3004
+ if (version.length >= MAX_VERSION_LENGTH) {
3005
+ break;
3006
+ }
3007
+ version += String.fromCharCode(ch);
3008
+ }
3009
+ // removing "%PDF-"-prefix
3010
+ this.pdfFormatVersion = version.substring(5);
3011
+ return;
3012
+ }
3013
+ // May not be a PDF file, continue anyway.
3014
+ },
3015
+ parseStartXRef: function PDFDocument_parseStartXRef() {
3016
+ var startXRef = this.startXRef;
3017
+ this.xref.setStartXRef(startXRef);
3018
+ },
3019
+ setup: function PDFDocument_setup(recoveryMode) {
3020
+ this.xref.parse(recoveryMode);
3021
+ this.catalog = new Catalog(this.pdfManager, this.xref);
3022
+ },
3023
+ get numPages() {
3024
+ var linearization = this.linearization;
3025
+ var num = linearization ? linearization.numPages : this.catalog.numPages;
3026
+ // shadow the prototype getter
3027
+ return shadow(this, 'numPages', num);
3028
+ },
3029
+ get documentInfo() {
3030
+ var docInfo = {
3031
+ PDFFormatVersion: this.pdfFormatVersion,
3032
+ IsAcroFormPresent: !!this.acroForm,
3033
+ IsXFAPresent: !!this.xfa
3034
+ };
3035
+ var infoDict;
3036
+ try {
3037
+ infoDict = this.xref.trailer.get('Info');
3038
+ } catch (err) {
3039
+ info('The document information dictionary is invalid.');
3040
+ }
3041
+ if (infoDict) {
3042
+ var validEntries = DocumentInfoValidators.entries;
3043
+ // Only fill the document info with valid entries from the spec.
3044
+ for (var key in validEntries) {
3045
+ if (infoDict.has(key)) {
3046
+ var value = infoDict.get(key);
3047
+ // Make sure the value conforms to the spec.
3048
+ if (validEntries[key](value)) {
3049
+ docInfo[key] = (typeof value !== 'string' ?
3050
+ value : stringToPDFString(value));
3051
+ } else {
3052
+ info('Bad value in document info for "' + key + '"');
3053
+ }
3054
+ }
3055
+ }
3056
+ }
3057
+ return shadow(this, 'documentInfo', docInfo);
3058
+ },
3059
+ get fingerprint() {
3060
+ var xref = this.xref, idArray, hash, fileID = '';
3061
+
3062
+ if (xref.trailer.has('ID')) {
3063
+ idArray = xref.trailer.get('ID');
3064
+ }
3065
+ if (idArray && isArray(idArray) && idArray[0] !== EMPTY_FINGERPRINT) {
3066
+ hash = stringToBytes(idArray[0]);
3067
+ } else {
3068
+ if (this.stream.ensureRange) {
3069
+ this.stream.ensureRange(0,
3070
+ Math.min(FINGERPRINT_FIRST_BYTES, this.stream.end));
3071
+ }
3072
+ hash = calculateMD5(this.stream.bytes.subarray(0,
3073
+ FINGERPRINT_FIRST_BYTES), 0, FINGERPRINT_FIRST_BYTES);
3074
+ }
3075
+
3076
+ for (var i = 0, n = hash.length; i < n; i++) {
3077
+ var hex = hash[i].toString(16);
3078
+ fileID += hex.length === 1 ? '0' + hex : hex;
3079
+ }
3080
+
3081
+ return shadow(this, 'fingerprint', fileID);
3082
+ },
3083
+
3084
+ getPage: function PDFDocument_getPage(pageIndex) {
3085
+ return this.catalog.getPage(pageIndex);
3086
+ },
3087
+
3088
+ cleanup: function PDFDocument_cleanup() {
3089
+ return this.catalog.cleanup();
3090
+ }
3091
+ };
3092
+
3093
+ return PDFDocument;
3094
+ })();
3095
+
3096
+
3097
+ var Name = (function NameClosure() {
3098
+ function Name(name) {
3099
+ this.name = name;
3100
+ }
3101
+
3102
+ Name.prototype = {};
3103
+
3104
+ var nameCache = {};
3105
+
3106
+ Name.get = function Name_get(name) {
3107
+ var nameValue = nameCache[name];
3108
+ return (nameValue ? nameValue : (nameCache[name] = new Name(name)));
3109
+ };
3110
+
3111
+ return Name;
3112
+ })();
3113
+
3114
+ var Cmd = (function CmdClosure() {
3115
+ function Cmd(cmd) {
3116
+ this.cmd = cmd;
3117
+ }
3118
+
3119
+ Cmd.prototype = {};
3120
+
3121
+ var cmdCache = {};
3122
+
3123
+ Cmd.get = function Cmd_get(cmd) {
3124
+ var cmdValue = cmdCache[cmd];
3125
+ return (cmdValue ? cmdValue : (cmdCache[cmd] = new Cmd(cmd)));
3126
+ };
3127
+
3128
+ return Cmd;
3129
+ })();
3130
+
3131
+ var Dict = (function DictClosure() {
3132
+ var nonSerializable = function nonSerializableClosure() {
3133
+ return nonSerializable; // creating closure on some variable
3134
+ };
3135
+
3136
+ var GETALL_DICTIONARY_TYPES_WHITELIST = {
3137
+ 'Background': true,
3138
+ 'ExtGState': true,
3139
+ 'Halftone': true,
3140
+ 'Layout': true,
3141
+ 'Mask': true,
3142
+ 'Pagination': true,
3143
+ 'Printing': true
3144
+ };
3145
+
3146
+ function isRecursionAllowedFor(dict) {
3147
+ if (!isName(dict.Type)) {
3148
+ return true;
3149
+ }
3150
+ var dictType = dict.Type.name;
3151
+ return GETALL_DICTIONARY_TYPES_WHITELIST[dictType] === true;
3152
+ }
3153
+
3154
+ // xref is optional
3155
+ function Dict(xref) {
3156
+ // Map should only be used internally, use functions below to access.
3157
+ this.map = Object.create(null);
3158
+ this.xref = xref;
3159
+ this.objId = null;
3160
+ this.__nonSerializable__ = nonSerializable; // disable cloning of the Dict
3161
+ }
3162
+
3163
+ Dict.prototype = {
3164
+ assignXref: function Dict_assignXref(newXref) {
3165
+ this.xref = newXref;
3166
+ },
3167
+
3168
+ // automatically dereferences Ref objects
3169
+ get: function Dict_get(key1, key2, key3) {
3170
+ var value;
3171
+ var xref = this.xref;
3172
+ if (typeof (value = this.map[key1]) !== 'undefined' || key1 in this.map ||
3173
+ typeof key2 === 'undefined') {
3174
+ return xref ? xref.fetchIfRef(value) : value;
3175
+ }
3176
+ if (typeof (value = this.map[key2]) !== 'undefined' || key2 in this.map ||
3177
+ typeof key3 === 'undefined') {
3178
+ return xref ? xref.fetchIfRef(value) : value;
3179
+ }
3180
+ value = this.map[key3] || null;
3181
+ return xref ? xref.fetchIfRef(value) : value;
3182
+ },
3183
+
3184
+ // Same as get(), but returns a promise and uses fetchIfRefAsync().
3185
+ getAsync: function Dict_getAsync(key1, key2, key3) {
3186
+ var value;
3187
+ var xref = this.xref;
3188
+ if (typeof (value = this.map[key1]) !== 'undefined' || key1 in this.map ||
3189
+ typeof key2 === 'undefined') {
3190
+ if (xref) {
3191
+ return xref.fetchIfRefAsync(value);
3192
+ }
3193
+ return Promise.resolve(value);
3194
+ }
3195
+ if (typeof (value = this.map[key2]) !== 'undefined' || key2 in this.map ||
3196
+ typeof key3 === 'undefined') {
3197
+ if (xref) {
3198
+ return xref.fetchIfRefAsync(value);
3199
+ }
3200
+ return Promise.resolve(value);
3201
+ }
3202
+ value = this.map[key3] || null;
3203
+ if (xref) {
3204
+ return xref.fetchIfRefAsync(value);
3205
+ }
3206
+ return Promise.resolve(value);
3207
+ },
3208
+
3209
+ // no dereferencing
3210
+ getRaw: function Dict_getRaw(key) {
3211
+ return this.map[key];
3212
+ },
3213
+
3214
+ // creates new map and dereferences all Refs
3215
+ getAll: function Dict_getAll() {
3216
+ var all = Object.create(null);
3217
+ var queue = null;
3218
+ var key, obj;
3219
+ for (key in this.map) {
3220
+ obj = this.get(key);
3221
+ if (obj instanceof Dict) {
3222
+ if (isRecursionAllowedFor(obj)) {
3223
+ (queue || (queue = [])).push({target: all, key: key, obj: obj});
3224
+ } else {
3225
+ all[key] = this.getRaw(key);
3226
+ }
3227
+ } else {
3228
+ all[key] = obj;
3229
+ }
3230
+ }
3231
+ if (!queue) {
3232
+ return all;
3233
+ }
3234
+
3235
+ // trying to take cyclic references into the account
3236
+ var processed = Object.create(null);
3237
+ while (queue.length > 0) {
3238
+ var item = queue.shift();
3239
+ var itemObj = item.obj;
3240
+ var objId = itemObj.objId;
3241
+ if (objId && objId in processed) {
3242
+ item.target[item.key] = processed[objId];
3243
+ continue;
3244
+ }
3245
+ var dereferenced = Object.create(null);
3246
+ for (key in itemObj.map) {
3247
+ obj = itemObj.get(key);
3248
+ if (obj instanceof Dict) {
3249
+ if (isRecursionAllowedFor(obj)) {
3250
+ queue.push({target: dereferenced, key: key, obj: obj});
3251
+ } else {
3252
+ dereferenced[key] = itemObj.getRaw(key);
3253
+ }
3254
+ } else {
3255
+ dereferenced[key] = obj;
3256
+ }
3257
+ }
3258
+ if (objId) {
3259
+ processed[objId] = dereferenced;
3260
+ }
3261
+ item.target[item.key] = dereferenced;
3262
+ }
3263
+ return all;
3264
+ },
3265
+
3266
+ getKeys: function Dict_getKeys() {
3267
+ return Object.keys(this.map);
3268
+ },
3269
+
3270
+ set: function Dict_set(key, value) {
3271
+ this.map[key] = value;
3272
+ },
3273
+
3274
+ has: function Dict_has(key) {
3275
+ return key in this.map;
3276
+ },
3277
+
3278
+ forEach: function Dict_forEach(callback) {
3279
+ for (var key in this.map) {
3280
+ callback(key, this.get(key));
3281
+ }
3282
+ }
3283
+ };
3284
+
3285
+ Dict.empty = new Dict(null);
3286
+
3287
+ return Dict;
3288
+ })();
3289
+
3290
+ var Ref = (function RefClosure() {
3291
+ function Ref(num, gen) {
3292
+ this.num = num;
3293
+ this.gen = gen;
3294
+ }
3295
+
3296
+ Ref.prototype = {
3297
+ toString: function Ref_toString() {
3298
+ // This function is hot, so we make the string as compact as possible.
3299
+ // |this.gen| is almost always zero, so we treat that case specially.
3300
+ var str = this.num + 'R';
3301
+ if (this.gen !== 0) {
3302
+ str += this.gen;
3303
+ }
3304
+ return str;
3305
+ }
3306
+ };
3307
+
3308
+ return Ref;
3309
+ })();
3310
+
3311
+ // The reference is identified by number and generation.
3312
+ // This structure stores only one instance of the reference.
3313
+ var RefSet = (function RefSetClosure() {
3314
+ function RefSet() {
3315
+ this.dict = {};
3316
+ }
3317
+
3318
+ RefSet.prototype = {
3319
+ has: function RefSet_has(ref) {
3320
+ return ref.toString() in this.dict;
3321
+ },
3322
+
3323
+ put: function RefSet_put(ref) {
3324
+ this.dict[ref.toString()] = true;
3325
+ },
3326
+
3327
+ remove: function RefSet_remove(ref) {
3328
+ delete this.dict[ref.toString()];
3329
+ }
3330
+ };
3331
+
3332
+ return RefSet;
3333
+ })();
3334
+
3335
+ var RefSetCache = (function RefSetCacheClosure() {
3336
+ function RefSetCache() {
3337
+ this.dict = Object.create(null);
3338
+ }
3339
+
3340
+ RefSetCache.prototype = {
3341
+ get: function RefSetCache_get(ref) {
3342
+ return this.dict[ref.toString()];
3343
+ },
3344
+
3345
+ has: function RefSetCache_has(ref) {
3346
+ return ref.toString() in this.dict;
3347
+ },
3348
+
3349
+ put: function RefSetCache_put(ref, obj) {
3350
+ this.dict[ref.toString()] = obj;
3351
+ },
3352
+
3353
+ putAlias: function RefSetCache_putAlias(ref, aliasRef) {
3354
+ this.dict[ref.toString()] = this.get(aliasRef);
3355
+ },
3356
+
3357
+ forEach: function RefSetCache_forEach(fn, thisArg) {
3358
+ for (var i in this.dict) {
3359
+ fn.call(thisArg, this.dict[i]);
3360
+ }
3361
+ },
3362
+
3363
+ clear: function RefSetCache_clear() {
3364
+ this.dict = Object.create(null);
3365
+ }
3366
+ };
3367
+
3368
+ return RefSetCache;
3369
+ })();
3370
+
3371
+ var Catalog = (function CatalogClosure() {
3372
+ function Catalog(pdfManager, xref) {
3373
+ this.pdfManager = pdfManager;
3374
+ this.xref = xref;
3375
+ this.catDict = xref.getCatalogObj();
3376
+ this.fontCache = new RefSetCache();
3377
+ assert(isDict(this.catDict),
3378
+ 'catalog object is not a dictionary');
3379
+
3380
+ this.pagePromises = [];
3381
+ }
3382
+
3383
+ Catalog.prototype = {
3384
+ get metadata() {
3385
+ var streamRef = this.catDict.getRaw('Metadata');
3386
+ if (!isRef(streamRef)) {
3387
+ return shadow(this, 'metadata', null);
3388
+ }
3389
+
3390
+ var encryptMetadata = (!this.xref.encrypt ? false :
3391
+ this.xref.encrypt.encryptMetadata);
3392
+
3393
+ var stream = this.xref.fetch(streamRef, !encryptMetadata);
3394
+ var metadata;
3395
+ if (stream && isDict(stream.dict)) {
3396
+ var type = stream.dict.get('Type');
3397
+ var subtype = stream.dict.get('Subtype');
3398
+
3399
+ if (isName(type) && isName(subtype) &&
3400
+ type.name === 'Metadata' && subtype.name === 'XML') {
3401
+ // XXX: This should examine the charset the XML document defines,
3402
+ // however since there are currently no real means to decode
3403
+ // arbitrary charsets, let's just hope that the author of the PDF
3404
+ // was reasonable enough to stick with the XML default charset,
3405
+ // which is UTF-8.
3406
+ try {
3407
+ metadata = stringToUTF8String(bytesToString(stream.getBytes()));
3408
+ } catch (e) {
3409
+ info('Skipping invalid metadata.');
3410
+ }
3411
+ }
3412
+ }
3413
+
3414
+ return shadow(this, 'metadata', metadata);
3415
+ },
3416
+ get toplevelPagesDict() {
3417
+ var pagesObj = this.catDict.get('Pages');
3418
+ assert(isDict(pagesObj), 'invalid top-level pages dictionary');
3419
+ // shadow the prototype getter
3420
+ return shadow(this, 'toplevelPagesDict', pagesObj);
3421
+ },
3422
+ get documentOutline() {
3423
+ var obj = null;
3424
+ try {
3425
+ obj = this.readDocumentOutline();
3426
+ } catch (ex) {
3427
+ if (ex instanceof MissingDataException) {
3428
+ throw ex;
3429
+ }
3430
+ warn('Unable to read document outline');
3431
+ }
3432
+ return shadow(this, 'documentOutline', obj);
3433
+ },
3434
+ readDocumentOutline: function Catalog_readDocumentOutline() {
3435
+ var xref = this.xref;
3436
+ var obj = this.catDict.get('Outlines');
3437
+ var root = { items: [] };
3438
+ if (isDict(obj)) {
3439
+ obj = obj.getRaw('First');
3440
+ var processed = new RefSet();
3441
+ if (isRef(obj)) {
3442
+ var queue = [{obj: obj, parent: root}];
3443
+ // to avoid recursion keeping track of the items
3444
+ // in the processed dictionary
3445
+ processed.put(obj);
3446
+ while (queue.length > 0) {
3447
+ var i = queue.shift();
3448
+ var outlineDict = xref.fetchIfRef(i.obj);
3449
+ if (outlineDict === null) {
3450
+ continue;
3451
+ }
3452
+ if (!outlineDict.has('Title')) {
3453
+ error('Invalid outline item');
3454
+ }
3455
+ var dest = outlineDict.get('A');
3456
+ if (dest) {
3457
+ dest = dest.get('D');
3458
+ } else if (outlineDict.has('Dest')) {
3459
+ dest = outlineDict.getRaw('Dest');
3460
+ if (isName(dest)) {
3461
+ dest = dest.name;
3462
+ }
3463
+ }
3464
+ var title = outlineDict.get('Title');
3465
+ var outlineItem = {
3466
+ dest: dest,
3467
+ title: stringToPDFString(title),
3468
+ color: outlineDict.get('C') || [0, 0, 0],
3469
+ count: outlineDict.get('Count'),
3470
+ bold: !!(outlineDict.get('F') & 2),
3471
+ italic: !!(outlineDict.get('F') & 1),
3472
+ items: []
3473
+ };
3474
+ i.parent.items.push(outlineItem);
3475
+ obj = outlineDict.getRaw('First');
3476
+ if (isRef(obj) && !processed.has(obj)) {
3477
+ queue.push({obj: obj, parent: outlineItem});
3478
+ processed.put(obj);
3479
+ }
3480
+ obj = outlineDict.getRaw('Next');
3481
+ if (isRef(obj) && !processed.has(obj)) {
3482
+ queue.push({obj: obj, parent: i.parent});
3483
+ processed.put(obj);
3484
+ }
3485
+ }
3486
+ }
3487
+ }
3488
+ return (root.items.length > 0 ? root.items : null);
3489
+ },
3490
+ get numPages() {
3491
+ var obj = this.toplevelPagesDict.get('Count');
3492
+ assert(
3493
+ isInt(obj),
3494
+ 'page count in top level pages object is not an integer'
3495
+ );
3496
+ // shadow the prototype getter
3497
+ return shadow(this, 'num', obj);
3498
+ },
3499
+ get destinations() {
3500
+ function fetchDestination(dest) {
3501
+ return isDict(dest) ? dest.get('D') : dest;
3502
+ }
3503
+
3504
+ var xref = this.xref;
3505
+ var dests = {}, nameTreeRef, nameDictionaryRef;
3506
+ var obj = this.catDict.get('Names');
3507
+ if (obj && obj.has('Dests')) {
3508
+ nameTreeRef = obj.getRaw('Dests');
3509
+ } else if (this.catDict.has('Dests')) {
3510
+ nameDictionaryRef = this.catDict.get('Dests');
3511
+ }
3512
+
3513
+ if (nameDictionaryRef) {
3514
+ // reading simple destination dictionary
3515
+ obj = nameDictionaryRef;
3516
+ obj.forEach(function catalogForEach(key, value) {
3517
+ if (!value) {
3518
+ return;
3519
+ }
3520
+ dests[key] = fetchDestination(value);
3521
+ });
3522
+ }
3523
+ if (nameTreeRef) {
3524
+ var nameTree = new NameTree(nameTreeRef, xref);
3525
+ var names = nameTree.getAll();
3526
+ for (var name in names) {
3527
+ if (!names.hasOwnProperty(name)) {
3528
+ continue;
3529
+ }
3530
+ dests[name] = fetchDestination(names[name]);
3531
+ }
3532
+ }
3533
+ return shadow(this, 'destinations', dests);
3534
+ },
3535
+ getDestination: function Catalog_getDestination(destinationId) {
3536
+ function fetchDestination(dest) {
3537
+ return isDict(dest) ? dest.get('D') : dest;
3538
+ }
3539
+
3540
+ var xref = this.xref;
3541
+ var dest, nameTreeRef, nameDictionaryRef;
3542
+ var obj = this.catDict.get('Names');
3543
+ if (obj && obj.has('Dests')) {
3544
+ nameTreeRef = obj.getRaw('Dests');
3545
+ } else if (this.catDict.has('Dests')) {
3546
+ nameDictionaryRef = this.catDict.get('Dests');
3547
+ }
3548
+
3549
+ if (nameDictionaryRef) {
3550
+ // reading simple destination dictionary
3551
+ obj = nameDictionaryRef;
3552
+ obj.forEach(function catalogForEach(key, value) {
3553
+ if (!value) {
3554
+ return;
3555
+ }
3556
+ if (key === destinationId) {
3557
+ dest = fetchDestination(value);
3558
+ }
3559
+ });
3560
+ }
3561
+ if (nameTreeRef) {
3562
+ var nameTree = new NameTree(nameTreeRef, xref);
3563
+ dest = fetchDestination(nameTree.get(destinationId));
3564
+ }
3565
+ return dest;
3566
+ },
3567
+ get attachments() {
3568
+ var xref = this.xref;
3569
+ var attachments = null, nameTreeRef;
3570
+ var obj = this.catDict.get('Names');
3571
+ if (obj) {
3572
+ nameTreeRef = obj.getRaw('EmbeddedFiles');
3573
+ }
3574
+
3575
+ if (nameTreeRef) {
3576
+ var nameTree = new NameTree(nameTreeRef, xref);
3577
+ var names = nameTree.getAll();
3578
+ for (var name in names) {
3579
+ if (!names.hasOwnProperty(name)) {
3580
+ continue;
3581
+ }
3582
+ var fs = new FileSpec(names[name], xref);
3583
+ if (!attachments) {
3584
+ attachments = {};
3585
+ }
3586
+ attachments[stringToPDFString(name)] = fs.serializable;
3587
+ }
3588
+ }
3589
+ return shadow(this, 'attachments', attachments);
3590
+ },
3591
+ get javaScript() {
3592
+ var xref = this.xref;
3593
+ var obj = this.catDict.get('Names');
3594
+
3595
+ var javaScript = [];
3596
+ if (obj && obj.has('JavaScript')) {
3597
+ var nameTree = new NameTree(obj.getRaw('JavaScript'), xref);
3598
+ var names = nameTree.getAll();
3599
+ for (var name in names) {
3600
+ if (!names.hasOwnProperty(name)) {
3601
+ continue;
3602
+ }
3603
+ // We don't really use the JavaScript right now. This code is
3604
+ // defensive so we don't cause errors on document load.
3605
+ var jsDict = names[name];
3606
+ if (!isDict(jsDict)) {
3607
+ continue;
3608
+ }
3609
+ var type = jsDict.get('S');
3610
+ if (!isName(type) || type.name !== 'JavaScript') {
3611
+ continue;
3612
+ }
3613
+ var js = jsDict.get('JS');
3614
+ if (!isString(js) && !isStream(js)) {
3615
+ continue;
3616
+ }
3617
+ if (isStream(js)) {
3618
+ js = bytesToString(js.getBytes());
3619
+ }
3620
+ javaScript.push(stringToPDFString(js));
3621
+ }
3622
+ }
3623
+
3624
+ // Append OpenAction actions to javaScript array
3625
+ var openactionDict = this.catDict.get('OpenAction');
3626
+ if (isDict(openactionDict)) {
3627
+ var objType = openactionDict.get('Type');
3628
+ var actionType = openactionDict.get('S');
3629
+ var action = openactionDict.get('N');
3630
+ var isPrintAction = (isName(objType) && objType.name === 'Action' &&
3631
+ isName(actionType) && actionType.name === 'Named' &&
3632
+ isName(action) && action.name === 'Print');
3633
+
3634
+ if (isPrintAction) {
3635
+ javaScript.push('print(true);');
3636
+ }
3637
+ }
3638
+
3639
+ return shadow(this, 'javaScript', javaScript);
3640
+ },
3641
+
3642
+ cleanup: function Catalog_cleanup() {
3643
+ var promises = [];
3644
+ this.fontCache.forEach(function (promise) {
3645
+ promises.push(promise);
3646
+ });
3647
+ return Promise.all(promises).then(function (translatedFonts) {
3648
+ for (var i = 0, ii = translatedFonts.length; i < ii; i++) {
3649
+ var font = translatedFonts[i].dict;
3650
+ delete font.translated;
3651
+ }
3652
+ this.fontCache.clear();
3653
+ }.bind(this));
3654
+ },
3655
+
3656
+ getPage: function Catalog_getPage(pageIndex) {
3657
+ if (!(pageIndex in this.pagePromises)) {
3658
+ this.pagePromises[pageIndex] = this.getPageDict(pageIndex).then(
3659
+ function (a) {
3660
+ var dict = a[0];
3661
+ var ref = a[1];
3662
+ return new Page(this.pdfManager, this.xref, pageIndex, dict, ref,
3663
+ this.fontCache);
3664
+ }.bind(this)
3665
+ );
3666
+ }
3667
+ return this.pagePromises[pageIndex];
3668
+ },
3669
+
3670
+ getPageDict: function Catalog_getPageDict(pageIndex) {
3671
+ var capability = createPromiseCapability();
3672
+ var nodesToVisit = [this.catDict.getRaw('Pages')];
3673
+ var currentPageIndex = 0;
3674
+ var xref = this.xref;
3675
+ var checkAllKids = false;
3676
+
3677
+ function next() {
3678
+ while (nodesToVisit.length) {
3679
+ var currentNode = nodesToVisit.pop();
3680
+
3681
+ if (isRef(currentNode)) {
3682
+ xref.fetchAsync(currentNode).then(function (obj) {
3683
+ if (isDict(obj, 'Page') || (isDict(obj) && !obj.has('Kids'))) {
3684
+ if (pageIndex === currentPageIndex) {
3685
+ capability.resolve([obj, currentNode]);
3686
+ } else {
3687
+ currentPageIndex++;
3688
+ next();
3689
+ }
3690
+ return;
3691
+ }
3692
+ nodesToVisit.push(obj);
3693
+ next();
3694
+ }, capability.reject);
3695
+ return;
3696
+ }
3697
+
3698
+ // Must be a child page dictionary.
3699
+ assert(
3700
+ isDict(currentNode),
3701
+ 'page dictionary kid reference points to wrong type of object'
3702
+ );
3703
+ var count = currentNode.get('Count');
3704
+ // If the current node doesn't have any children, avoid getting stuck
3705
+ // in an empty node further down in the tree (see issue5644.pdf).
3706
+ if (count === 0) {
3707
+ checkAllKids = true;
3708
+ }
3709
+ // Skip nodes where the page can't be.
3710
+ if (currentPageIndex + count <= pageIndex) {
3711
+ currentPageIndex += count;
3712
+ continue;
3713
+ }
3714
+
3715
+ var kids = currentNode.get('Kids');
3716
+ assert(isArray(kids), 'page dictionary kids object is not an array');
3717
+ if (!checkAllKids && count === kids.length) {
3718
+ // Nodes that don't have the page have been skipped and this is the
3719
+ // bottom of the tree which means the page requested must be a
3720
+ // descendant of this pages node. Ideally we would just resolve the
3721
+ // promise with the page ref here, but there is the case where more
3722
+ // pages nodes could link to single a page (see issue 3666 pdf). To
3723
+ // handle this push it back on the queue so if it is a pages node it
3724
+ // will be descended into.
3725
+ nodesToVisit = [kids[pageIndex - currentPageIndex]];
3726
+ currentPageIndex = pageIndex;
3727
+ continue;
3728
+ } else {
3729
+ for (var last = kids.length - 1; last >= 0; last--) {
3730
+ nodesToVisit.push(kids[last]);
3731
+ }
3732
+ }
3733
+ }
3734
+ capability.reject('Page index ' + pageIndex + ' not found.');
3735
+ }
3736
+ next();
3737
+ return capability.promise;
3738
+ },
3739
+
3740
+ getPageIndex: function Catalog_getPageIndex(ref) {
3741
+ // The page tree nodes have the count of all the leaves below them. To get
3742
+ // how many pages are before we just have to walk up the tree and keep
3743
+ // adding the count of siblings to the left of the node.
3744
+ var xref = this.xref;
3745
+ function pagesBeforeRef(kidRef) {
3746
+ var total = 0;
3747
+ var parentRef;
3748
+ return xref.fetchAsync(kidRef).then(function (node) {
3749
+ if (!node) {
3750
+ return null;
3751
+ }
3752
+ parentRef = node.getRaw('Parent');
3753
+ return node.getAsync('Parent');
3754
+ }).then(function (parent) {
3755
+ if (!parent) {
3756
+ return null;
3757
+ }
3758
+ return parent.getAsync('Kids');
3759
+ }).then(function (kids) {
3760
+ if (!kids) {
3761
+ return null;
3762
+ }
3763
+ var kidPromises = [];
3764
+ var found = false;
3765
+ for (var i = 0; i < kids.length; i++) {
3766
+ var kid = kids[i];
3767
+ assert(isRef(kid), 'kids must be a ref');
3768
+ if (kid.num === kidRef.num) {
3769
+ found = true;
3770
+ break;
3771
+ }
3772
+ kidPromises.push(xref.fetchAsync(kid).then(function (kid) {
3773
+ if (kid.has('Count')) {
3774
+ var count = kid.get('Count');
3775
+ total += count;
3776
+ } else { // page leaf node
3777
+ total++;
3778
+ }
3779
+ }));
3780
+ }
3781
+ if (!found) {
3782
+ error('kid ref not found in parents kids');
3783
+ }
3784
+ return Promise.all(kidPromises).then(function () {
3785
+ return [total, parentRef];
3786
+ });
3787
+ });
3788
+ }
3789
+
3790
+ var total = 0;
3791
+ function next(ref) {
3792
+ return pagesBeforeRef(ref).then(function (args) {
3793
+ if (!args) {
3794
+ return total;
3795
+ }
3796
+ var count = args[0];
3797
+ var parentRef = args[1];
3798
+ total += count;
3799
+ return next(parentRef);
3800
+ });
3801
+ }
3802
+
3803
+ return next(ref);
3804
+ }
3805
+ };
3806
+
3807
+ return Catalog;
3808
+ })();
3809
+
3810
+ var XRef = (function XRefClosure() {
3811
+ function XRef(stream, password) {
3812
+ this.stream = stream;
3813
+ this.entries = [];
3814
+ this.xrefstms = {};
3815
+ // prepare the XRef cache
3816
+ this.cache = [];
3817
+ this.password = password;
3818
+ this.stats = {
3819
+ streamTypes: [],
3820
+ fontTypes: []
3821
+ };
3822
+ }
3823
+
3824
+ XRef.prototype = {
3825
+ setStartXRef: function XRef_setStartXRef(startXRef) {
3826
+ // Store the starting positions of xref tables as we process them
3827
+ // so we can recover from missing data errors
3828
+ this.startXRefQueue = [startXRef];
3829
+ },
3830
+
3831
+ parse: function XRef_parse(recoveryMode) {
3832
+ var trailerDict;
3833
+ if (!recoveryMode) {
3834
+ trailerDict = this.readXRef();
3835
+ } else {
3836
+ warn('Indexing all PDF objects');
3837
+ trailerDict = this.indexObjects();
3838
+ }
3839
+ trailerDict.assignXref(this);
3840
+ this.trailer = trailerDict;
3841
+ var encrypt = trailerDict.get('Encrypt');
3842
+ if (encrypt) {
3843
+ var ids = trailerDict.get('ID');
3844
+ var fileId = (ids && ids.length) ? ids[0] : '';
3845
+ this.encrypt = new CipherTransformFactory(encrypt, fileId,
3846
+ this.password);
3847
+ }
3848
+
3849
+ // get the root dictionary (catalog) object
3850
+ if (!(this.root = trailerDict.get('Root'))) {
3851
+ error('Invalid root reference');
3852
+ }
3853
+ },
3854
+
3855
+ processXRefTable: function XRef_processXRefTable(parser) {
3856
+ if (!('tableState' in this)) {
3857
+ // Stores state of the table as we process it so we can resume
3858
+ // from middle of table in case of missing data error
3859
+ this.tableState = {
3860
+ entryNum: 0,
3861
+ streamPos: parser.lexer.stream.pos,
3862
+ parserBuf1: parser.buf1,
3863
+ parserBuf2: parser.buf2
3864
+ };
3865
+ }
3866
+
3867
+ var obj = this.readXRefTable(parser);
3868
+
3869
+ // Sanity check
3870
+ if (!isCmd(obj, 'trailer')) {
3871
+ error('Invalid XRef table: could not find trailer dictionary');
3872
+ }
3873
+ // Read trailer dictionary, e.g.
3874
+ // trailer
3875
+ // << /Size 22
3876
+ // /Root 20R
3877
+ // /Info 10R
3878
+ // /ID [ <81b14aafa313db63dbd6f981e49f94f4> ]
3879
+ // >>
3880
+ // The parser goes through the entire stream << ... >> and provides
3881
+ // a getter interface for the key-value table
3882
+ var dict = parser.getObj();
3883
+
3884
+ // The pdflib PDF generator can generate a nested trailer dictionary
3885
+ if (!isDict(dict) && dict.dict) {
3886
+ dict = dict.dict;
3887
+ }
3888
+ if (!isDict(dict)) {
3889
+ error('Invalid XRef table: could not parse trailer dictionary');
3890
+ }
3891
+ delete this.tableState;
3892
+
3893
+ return dict;
3894
+ },
3895
+
3896
+ readXRefTable: function XRef_readXRefTable(parser) {
3897
+ // Example of cross-reference table:
3898
+ // xref
3899
+ // 0 1 <-- subsection header (first obj #, obj count)
3900
+ // 0000000000 65535 f <-- actual object (offset, generation #, f/n)
3901
+ // 23 2 <-- subsection header ... and so on ...
3902
+ // 0000025518 00002 n
3903
+ // 0000025635 00000 n
3904
+ // trailer
3905
+ // ...
3906
+
3907
+ var stream = parser.lexer.stream;
3908
+ var tableState = this.tableState;
3909
+ stream.pos = tableState.streamPos;
3910
+ parser.buf1 = tableState.parserBuf1;
3911
+ parser.buf2 = tableState.parserBuf2;
3912
+
3913
+ // Outer loop is over subsection headers
3914
+ var obj;
3915
+
3916
+ while (true) {
3917
+ if (!('firstEntryNum' in tableState) || !('entryCount' in tableState)) {
3918
+ if (isCmd(obj = parser.getObj(), 'trailer')) {
3919
+ break;
3920
+ }
3921
+ tableState.firstEntryNum = obj;
3922
+ tableState.entryCount = parser.getObj();
3923
+ }
3924
+
3925
+ var first = tableState.firstEntryNum;
3926
+ var count = tableState.entryCount;
3927
+ if (!isInt(first) || !isInt(count)) {
3928
+ error('Invalid XRef table: wrong types in subsection header');
3929
+ }
3930
+ // Inner loop is over objects themselves
3931
+ for (var i = tableState.entryNum; i < count; i++) {
3932
+ tableState.streamPos = stream.pos;
3933
+ tableState.entryNum = i;
3934
+ tableState.parserBuf1 = parser.buf1;
3935
+ tableState.parserBuf2 = parser.buf2;
3936
+
3937
+ var entry = {};
3938
+ entry.offset = parser.getObj();
3939
+ entry.gen = parser.getObj();
3940
+ var type = parser.getObj();
3941
+
3942
+ if (isCmd(type, 'f')) {
3943
+ entry.free = true;
3944
+ } else if (isCmd(type, 'n')) {
3945
+ entry.uncompressed = true;
3946
+ }
3947
+
3948
+ // Validate entry obj
3949
+ if (!isInt(entry.offset) || !isInt(entry.gen) ||
3950
+ !(entry.free || entry.uncompressed)) {
3951
+ error('Invalid entry in XRef subsection: ' + first + ', ' + count);
3952
+ }
3953
+
3954
+ if (!this.entries[i + first]) {
3955
+ this.entries[i + first] = entry;
3956
+ }
3957
+ }
3958
+
3959
+ tableState.entryNum = 0;
3960
+ tableState.streamPos = stream.pos;
3961
+ tableState.parserBuf1 = parser.buf1;
3962
+ tableState.parserBuf2 = parser.buf2;
3963
+ delete tableState.firstEntryNum;
3964
+ delete tableState.entryCount;
3965
+ }
3966
+
3967
+ // Per issue 3248: hp scanners generate bad XRef
3968
+ if (first === 1 && this.entries[1] && this.entries[1].free) {
3969
+ // shifting the entries
3970
+ this.entries.shift();
3971
+ }
3972
+
3973
+ // Sanity check: as per spec, first object must be free
3974
+ if (this.entries[0] && !this.entries[0].free) {
3975
+ error('Invalid XRef table: unexpected first object');
3976
+ }
3977
+ return obj;
3978
+ },
3979
+
3980
+ processXRefStream: function XRef_processXRefStream(stream) {
3981
+ if (!('streamState' in this)) {
3982
+ // Stores state of the stream as we process it so we can resume
3983
+ // from middle of stream in case of missing data error
3984
+ var streamParameters = stream.dict;
3985
+ var byteWidths = streamParameters.get('W');
3986
+ var range = streamParameters.get('Index');
3987
+ if (!range) {
3988
+ range = [0, streamParameters.get('Size')];
3989
+ }
3990
+
3991
+ this.streamState = {
3992
+ entryRanges: range,
3993
+ byteWidths: byteWidths,
3994
+ entryNum: 0,
3995
+ streamPos: stream.pos
3996
+ };
3997
+ }
3998
+ this.readXRefStream(stream);
3999
+ delete this.streamState;
4000
+
4001
+ return stream.dict;
4002
+ },
4003
+
4004
+ readXRefStream: function XRef_readXRefStream(stream) {
4005
+ var i, j;
4006
+ var streamState = this.streamState;
4007
+ stream.pos = streamState.streamPos;
4008
+
4009
+ var byteWidths = streamState.byteWidths;
4010
+ var typeFieldWidth = byteWidths[0];
4011
+ var offsetFieldWidth = byteWidths[1];
4012
+ var generationFieldWidth = byteWidths[2];
4013
+
4014
+ var entryRanges = streamState.entryRanges;
4015
+ while (entryRanges.length > 0) {
4016
+ var first = entryRanges[0];
4017
+ var n = entryRanges[1];
4018
+
4019
+ if (!isInt(first) || !isInt(n)) {
4020
+ error('Invalid XRef range fields: ' + first + ', ' + n);
4021
+ }
4022
+ if (!isInt(typeFieldWidth) || !isInt(offsetFieldWidth) ||
4023
+ !isInt(generationFieldWidth)) {
4024
+ error('Invalid XRef entry fields length: ' + first + ', ' + n);
4025
+ }
4026
+ for (i = streamState.entryNum; i < n; ++i) {
4027
+ streamState.entryNum = i;
4028
+ streamState.streamPos = stream.pos;
4029
+
4030
+ var type = 0, offset = 0, generation = 0;
4031
+ for (j = 0; j < typeFieldWidth; ++j) {
4032
+ type = (type << 8) | stream.getByte();
4033
+ }
4034
+ // if type field is absent, its default value is 1
4035
+ if (typeFieldWidth === 0) {
4036
+ type = 1;
4037
+ }
4038
+ for (j = 0; j < offsetFieldWidth; ++j) {
4039
+ offset = (offset << 8) | stream.getByte();
4040
+ }
4041
+ for (j = 0; j < generationFieldWidth; ++j) {
4042
+ generation = (generation << 8) | stream.getByte();
4043
+ }
4044
+ var entry = {};
4045
+ entry.offset = offset;
4046
+ entry.gen = generation;
4047
+ switch (type) {
4048
+ case 0:
4049
+ entry.free = true;
4050
+ break;
4051
+ case 1:
4052
+ entry.uncompressed = true;
4053
+ break;
4054
+ case 2:
4055
+ break;
4056
+ default:
4057
+ error('Invalid XRef entry type: ' + type);
4058
+ }
4059
+ if (!this.entries[first + i]) {
4060
+ this.entries[first + i] = entry;
4061
+ }
4062
+ }
4063
+
4064
+ streamState.entryNum = 0;
4065
+ streamState.streamPos = stream.pos;
4066
+ entryRanges.splice(0, 2);
4067
+ }
4068
+ },
4069
+
4070
+ indexObjects: function XRef_indexObjects() {
4071
+ // Simple scan through the PDF content to find objects,
4072
+ // trailers and XRef streams.
4073
+ function readToken(data, offset) {
4074
+ var token = '', ch = data[offset];
4075
+ while (ch !== 13 && ch !== 10) {
4076
+ if (++offset >= data.length) {
4077
+ break;
4078
+ }
4079
+ token += String.fromCharCode(ch);
4080
+ ch = data[offset];
4081
+ }
4082
+ return token;
4083
+ }
4084
+ function skipUntil(data, offset, what) {
4085
+ var length = what.length, dataLength = data.length;
4086
+ var skipped = 0;
4087
+ // finding byte sequence
4088
+ while (offset < dataLength) {
4089
+ var i = 0;
4090
+ while (i < length && data[offset + i] === what[i]) {
4091
+ ++i;
4092
+ }
4093
+ if (i >= length) {
4094
+ break; // sequence found
4095
+ }
4096
+ offset++;
4097
+ skipped++;
4098
+ }
4099
+ return skipped;
4100
+ }
4101
+ var trailerBytes = new Uint8Array([116, 114, 97, 105, 108, 101, 114]);
4102
+ var startxrefBytes = new Uint8Array([115, 116, 97, 114, 116, 120, 114,
4103
+ 101, 102]);
4104
+ var endobjBytes = new Uint8Array([101, 110, 100, 111, 98, 106]);
4105
+ var xrefBytes = new Uint8Array([47, 88, 82, 101, 102]);
4106
+
4107
+ var stream = this.stream;
4108
+ stream.pos = 0;
4109
+ var buffer = stream.getBytes();
4110
+ var position = stream.start, length = buffer.length;
4111
+ var trailers = [], xrefStms = [];
4112
+ while (position < length) {
4113
+ var ch = buffer[position];
4114
+ if (ch === 32 || ch === 9 || ch === 13 || ch === 10) {
4115
+ ++position;
4116
+ continue;
4117
+ }
4118
+ if (ch === 37) { // %-comment
4119
+ do {
4120
+ ++position;
4121
+ if (position >= length) {
4122
+ break;
4123
+ }
4124
+ ch = buffer[position];
4125
+ } while (ch !== 13 && ch !== 10);
4126
+ continue;
4127
+ }
4128
+ var token = readToken(buffer, position);
4129
+ var m;
4130
+ if (token === 'xref') {
4131
+ position += skipUntil(buffer, position, trailerBytes);
4132
+ trailers.push(position);
4133
+ position += skipUntil(buffer, position, startxrefBytes);
4134
+ } else if ((m = /^(\d+)\s+(\d+)\s+obj\b/.exec(token))) {
4135
+ this.entries[m[1]] = {
4136
+ offset: position,
4137
+ gen: m[2] | 0,
4138
+ uncompressed: true
4139
+ };
4140
+
4141
+ var contentLength = skipUntil(buffer, position, endobjBytes) + 7;
4142
+ var content = buffer.subarray(position, position + contentLength);
4143
+
4144
+ // checking XRef stream suspect
4145
+ // (it shall have '/XRef' and next char is not a letter)
4146
+ var xrefTagOffset = skipUntil(content, 0, xrefBytes);
4147
+ if (xrefTagOffset < contentLength &&
4148
+ content[xrefTagOffset + 5] < 64) {
4149
+ xrefStms.push(position);
4150
+ this.xrefstms[position] = 1; // don't read it recursively
4151
+ }
4152
+
4153
+ position += contentLength;
4154
+ } else {
4155
+ position += token.length + 1;
4156
+ }
4157
+ }
4158
+ // reading XRef streams
4159
+ var i, ii;
4160
+ for (i = 0, ii = xrefStms.length; i < ii; ++i) {
4161
+ this.startXRefQueue.push(xrefStms[i]);
4162
+ this.readXRef(/* recoveryMode */ true);
4163
+ }
4164
+ // finding main trailer
4165
+ var dict;
4166
+ for (i = 0, ii = trailers.length; i < ii; ++i) {
4167
+ stream.pos = trailers[i];
4168
+ var parser = new Parser(new Lexer(stream), true, this);
4169
+ var obj = parser.getObj();
4170
+ if (!isCmd(obj, 'trailer')) {
4171
+ continue;
4172
+ }
4173
+ // read the trailer dictionary
4174
+ if (!isDict(dict = parser.getObj())) {
4175
+ continue;
4176
+ }
4177
+ // taking the first one with 'ID'
4178
+ if (dict.has('ID')) {
4179
+ return dict;
4180
+ }
4181
+ }
4182
+ // no tailer with 'ID', taking last one (if exists)
4183
+ if (dict) {
4184
+ return dict;
4185
+ }
4186
+ // nothing helps
4187
+ // calling error() would reject worker with an UnknownErrorException.
4188
+ throw new InvalidPDFException('Invalid PDF structure');
4189
+ },
4190
+
4191
+ readXRef: function XRef_readXRef(recoveryMode) {
4192
+ var stream = this.stream;
4193
+
4194
+ try {
4195
+ while (this.startXRefQueue.length) {
4196
+ var startXRef = this.startXRefQueue[0];
4197
+
4198
+ stream.pos = startXRef + stream.start;
4199
+
4200
+ var parser = new Parser(new Lexer(stream), true, this);
4201
+ var obj = parser.getObj();
4202
+ var dict;
4203
+
4204
+ // Get dictionary
4205
+ if (isCmd(obj, 'xref')) {
4206
+ // Parse end-of-file XRef
4207
+ dict = this.processXRefTable(parser);
4208
+ if (!this.topDict) {
4209
+ this.topDict = dict;
4210
+ }
4211
+
4212
+ // Recursively get other XRefs 'XRefStm', if any
4213
+ obj = dict.get('XRefStm');
4214
+ if (isInt(obj)) {
4215
+ var pos = obj;
4216
+ // ignore previously loaded xref streams
4217
+ // (possible infinite recursion)
4218
+ if (!(pos in this.xrefstms)) {
4219
+ this.xrefstms[pos] = 1;
4220
+ this.startXRefQueue.push(pos);
4221
+ }
4222
+ }
4223
+ } else if (isInt(obj)) {
4224
+ // Parse in-stream XRef
4225
+ if (!isInt(parser.getObj()) ||
4226
+ !isCmd(parser.getObj(), 'obj') ||
4227
+ !isStream(obj = parser.getObj())) {
4228
+ error('Invalid XRef stream');
4229
+ }
4230
+ dict = this.processXRefStream(obj);
4231
+ if (!this.topDict) {
4232
+ this.topDict = dict;
4233
+ }
4234
+ if (!dict) {
4235
+ error('Failed to read XRef stream');
4236
+ }
4237
+ } else {
4238
+ error('Invalid XRef stream header');
4239
+ }
4240
+
4241
+ // Recursively get previous dictionary, if any
4242
+ obj = dict.get('Prev');
4243
+ if (isInt(obj)) {
4244
+ this.startXRefQueue.push(obj);
4245
+ } else if (isRef(obj)) {
4246
+ // The spec says Prev must not be a reference, i.e. "/Prev NNN"
4247
+ // This is a fallback for non-compliant PDFs, i.e. "/Prev NNN 0 R"
4248
+ this.startXRefQueue.push(obj.num);
4249
+ }
4250
+
4251
+ this.startXRefQueue.shift();
4252
+ }
4253
+
4254
+ return this.topDict;
4255
+ } catch (e) {
4256
+ if (e instanceof MissingDataException) {
4257
+ throw e;
4258
+ }
4259
+ info('(while reading XRef): ' + e);
4260
+ }
4261
+
4262
+ if (recoveryMode) {
4263
+ return;
4264
+ }
4265
+ throw new XRefParseException();
4266
+ },
4267
+
4268
+ getEntry: function XRef_getEntry(i) {
4269
+ var xrefEntry = this.entries[i];
4270
+ if (xrefEntry && !xrefEntry.free && xrefEntry.offset) {
4271
+ return xrefEntry;
4272
+ }
4273
+ return null;
4274
+ },
4275
+
4276
+ fetchIfRef: function XRef_fetchIfRef(obj) {
4277
+ if (!isRef(obj)) {
4278
+ return obj;
4279
+ }
4280
+ return this.fetch(obj);
4281
+ },
4282
+
4283
+ fetch: function XRef_fetch(ref, suppressEncryption) {
4284
+ assert(isRef(ref), 'ref object is not a reference');
4285
+ var num = ref.num;
4286
+ if (num in this.cache) {
4287
+ var cacheEntry = this.cache[num];
4288
+ return cacheEntry;
4289
+ }
4290
+
4291
+ var xrefEntry = this.getEntry(num);
4292
+
4293
+ // the referenced entry can be free
4294
+ if (xrefEntry === null) {
4295
+ return (this.cache[num] = null);
4296
+ }
4297
+
4298
+ if (xrefEntry.uncompressed) {
4299
+ xrefEntry = this.fetchUncompressed(ref, xrefEntry, suppressEncryption);
4300
+ } else {
4301
+ xrefEntry = this.fetchCompressed(xrefEntry, suppressEncryption);
4302
+ }
4303
+ if (isDict(xrefEntry)){
4304
+ xrefEntry.objId = ref.toString();
4305
+ } else if (isStream(xrefEntry)) {
4306
+ xrefEntry.dict.objId = ref.toString();
4307
+ }
4308
+ return xrefEntry;
4309
+ },
4310
+
4311
+ fetchUncompressed: function XRef_fetchUncompressed(ref, xrefEntry,
4312
+ suppressEncryption) {
4313
+ var gen = ref.gen;
4314
+ var num = ref.num;
4315
+ if (xrefEntry.gen !== gen) {
4316
+ error('inconsistent generation in XRef');
4317
+ }
4318
+ var stream = this.stream.makeSubStream(xrefEntry.offset +
4319
+ this.stream.start);
4320
+ var parser = new Parser(new Lexer(stream), true, this);
4321
+ var obj1 = parser.getObj();
4322
+ var obj2 = parser.getObj();
4323
+ var obj3 = parser.getObj();
4324
+ if (!isInt(obj1) || parseInt(obj1, 10) !== num ||
4325
+ !isInt(obj2) || parseInt(obj2, 10) !== gen ||
4326
+ !isCmd(obj3)) {
4327
+ error('bad XRef entry');
4328
+ }
4329
+ if (!isCmd(obj3, 'obj')) {
4330
+ // some bad PDFs use "obj1234" and really mean 1234
4331
+ if (obj3.cmd.indexOf('obj') === 0) {
4332
+ num = parseInt(obj3.cmd.substring(3), 10);
4333
+ if (!isNaN(num)) {
4334
+ return num;
4335
+ }
4336
+ }
4337
+ error('bad XRef entry');
4338
+ }
4339
+ if (this.encrypt && !suppressEncryption) {
4340
+ xrefEntry = parser.getObj(this.encrypt.createCipherTransform(num, gen));
4341
+ } else {
4342
+ xrefEntry = parser.getObj();
4343
+ }
4344
+ if (!isStream(xrefEntry)) {
4345
+ this.cache[num] = xrefEntry;
4346
+ }
4347
+ return xrefEntry;
4348
+ },
4349
+
4350
+ fetchCompressed: function XRef_fetchCompressed(xrefEntry,
4351
+ suppressEncryption) {
4352
+ var tableOffset = xrefEntry.offset;
4353
+ var stream = this.fetch(new Ref(tableOffset, 0));
4354
+ if (!isStream(stream)) {
4355
+ error('bad ObjStm stream');
4356
+ }
4357
+ var first = stream.dict.get('First');
4358
+ var n = stream.dict.get('N');
4359
+ if (!isInt(first) || !isInt(n)) {
4360
+ error('invalid first and n parameters for ObjStm stream');
4361
+ }
4362
+ var parser = new Parser(new Lexer(stream), false, this);
4363
+ parser.allowStreams = true;
4364
+ var i, entries = [], num, nums = [];
4365
+ // read the object numbers to populate cache
4366
+ for (i = 0; i < n; ++i) {
4367
+ num = parser.getObj();
4368
+ if (!isInt(num)) {
4369
+ error('invalid object number in the ObjStm stream: ' + num);
4370
+ }
4371
+ nums.push(num);
4372
+ var offset = parser.getObj();
4373
+ if (!isInt(offset)) {
4374
+ error('invalid object offset in the ObjStm stream: ' + offset);
4375
+ }
4376
+ }
4377
+ // read stream objects for cache
4378
+ for (i = 0; i < n; ++i) {
4379
+ entries.push(parser.getObj());
4380
+ num = nums[i];
4381
+ var entry = this.entries[num];
4382
+ if (entry && entry.offset === tableOffset && entry.gen === i) {
4383
+ this.cache[num] = entries[i];
4384
+ }
4385
+ }
4386
+ xrefEntry = entries[xrefEntry.gen];
4387
+ if (xrefEntry === undefined) {
4388
+ error('bad XRef entry for compressed object');
4389
+ }
4390
+ return xrefEntry;
4391
+ },
4392
+
4393
+ fetchIfRefAsync: function XRef_fetchIfRefAsync(obj) {
4394
+ if (!isRef(obj)) {
4395
+ return Promise.resolve(obj);
4396
+ }
4397
+ return this.fetchAsync(obj);
4398
+ },
4399
+
4400
+ fetchAsync: function XRef_fetchAsync(ref, suppressEncryption) {
4401
+ var streamManager = this.stream.manager;
4402
+ var xref = this;
4403
+ return new Promise(function tryFetch(resolve, reject) {
4404
+ try {
4405
+ resolve(xref.fetch(ref, suppressEncryption));
4406
+ } catch (e) {
4407
+ if (e instanceof MissingDataException) {
4408
+ streamManager.requestRange(e.begin, e.end, function () {
4409
+ tryFetch(resolve, reject);
4410
+ });
4411
+ return;
4412
+ }
4413
+ reject(e);
4414
+ }
4415
+ });
4416
+ },
4417
+
4418
+ getCatalogObj: function XRef_getCatalogObj() {
4419
+ return this.root;
4420
+ }
4421
+ };
4422
+
4423
+ return XRef;
4424
+ })();
4425
+
4426
+ /**
4427
+ * A NameTree is like a Dict but has some advantageous properties, see the
4428
+ * spec (7.9.6) for more details.
4429
+ * TODO: implement all the Dict functions and make this more efficent.
4430
+ */
4431
+ var NameTree = (function NameTreeClosure() {
4432
+ function NameTree(root, xref) {
4433
+ this.root = root;
4434
+ this.xref = xref;
4435
+ }
4436
+
4437
+ NameTree.prototype = {
4438
+ getAll: function NameTree_getAll() {
4439
+ var dict = {};
4440
+ if (!this.root) {
4441
+ return dict;
4442
+ }
4443
+ var xref = this.xref;
4444
+ // reading name tree
4445
+ var processed = new RefSet();
4446
+ processed.put(this.root);
4447
+ var queue = [this.root];
4448
+ while (queue.length > 0) {
4449
+ var i, n;
4450
+ var obj = xref.fetchIfRef(queue.shift());
4451
+ if (!isDict(obj)) {
4452
+ continue;
4453
+ }
4454
+ if (obj.has('Kids')) {
4455
+ var kids = obj.get('Kids');
4456
+ for (i = 0, n = kids.length; i < n; i++) {
4457
+ var kid = kids[i];
4458
+ if (processed.has(kid)) {
4459
+ error('invalid destinations');
4460
+ }
4461
+ queue.push(kid);
4462
+ processed.put(kid);
4463
+ }
4464
+ continue;
4465
+ }
4466
+ var names = obj.get('Names');
4467
+ if (names) {
4468
+ for (i = 0, n = names.length; i < n; i += 2) {
4469
+ dict[names[i]] = xref.fetchIfRef(names[i + 1]);
4470
+ }
4471
+ }
4472
+ }
4473
+ return dict;
4474
+ },
4475
+
4476
+ get: function NameTree_get(destinationId) {
4477
+ if (!this.root) {
4478
+ return null;
4479
+ }
4480
+
4481
+ var xref = this.xref;
4482
+ var kidsOrNames = xref.fetchIfRef(this.root);
4483
+ var loopCount = 0;
4484
+ var MAX_NAMES_LEVELS = 10;
4485
+ var l, r, m;
4486
+
4487
+ // Perform a binary search to quickly find the entry that
4488
+ // contains the named destination we are looking for.
4489
+ while (kidsOrNames.has('Kids')) {
4490
+ loopCount++;
4491
+ if (loopCount > MAX_NAMES_LEVELS) {
4492
+ warn('Search depth limit for named destionations has been reached.');
4493
+ return null;
4494
+ }
4495
+
4496
+ var kids = kidsOrNames.get('Kids');
4497
+ if (!isArray(kids)) {
4498
+ return null;
4499
+ }
4500
+
4501
+ l = 0;
4502
+ r = kids.length - 1;
4503
+ while (l <= r) {
4504
+ m = (l + r) >> 1;
4505
+ var kid = xref.fetchIfRef(kids[m]);
4506
+ var limits = kid.get('Limits');
4507
+
4508
+ if (destinationId < limits[0]) {
4509
+ r = m - 1;
4510
+ } else if (destinationId > limits[1]) {
4511
+ l = m + 1;
4512
+ } else {
4513
+ kidsOrNames = xref.fetchIfRef(kids[m]);
4514
+ break;
4515
+ }
4516
+ }
4517
+ if (l > r) {
4518
+ return null;
4519
+ }
4520
+ }
4521
+
4522
+ // If we get here, then we have found the right entry. Now
4523
+ // go through the named destinations in the Named dictionary
4524
+ // until we find the exact destination we're looking for.
4525
+ var names = kidsOrNames.get('Names');
4526
+ if (isArray(names)) {
4527
+ // Perform a binary search to reduce the lookup time.
4528
+ l = 0;
4529
+ r = names.length - 2;
4530
+ while (l <= r) {
4531
+ // Check only even indices (0, 2, 4, ...) because the
4532
+ // odd indices contain the actual D array.
4533
+ m = (l + r) & ~1;
4534
+ if (destinationId < names[m]) {
4535
+ r = m - 2;
4536
+ } else if (destinationId > names[m]) {
4537
+ l = m + 2;
4538
+ } else {
4539
+ return xref.fetchIfRef(names[m + 1]);
4540
+ }
4541
+ }
4542
+ }
4543
+ return null;
4544
+ }
4545
+ };
4546
+ return NameTree;
4547
+ })();
4548
+
4549
+ /**
4550
+ * "A PDF file can refer to the contents of another file by using a File
4551
+ * Specification (PDF 1.1)", see the spec (7.11) for more details.
4552
+ * NOTE: Only embedded files are supported (as part of the attachments support)
4553
+ * TODO: support the 'URL' file system (with caching if !/V), portable
4554
+ * collections attributes and related files (/RF)
4555
+ */
4556
+ var FileSpec = (function FileSpecClosure() {
4557
+ function FileSpec(root, xref) {
4558
+ if (!root || !isDict(root)) {
4559
+ return;
4560
+ }
4561
+ this.xref = xref;
4562
+ this.root = root;
4563
+ if (root.has('FS')) {
4564
+ this.fs = root.get('FS');
4565
+ }
4566
+ this.description = root.has('Desc') ?
4567
+ stringToPDFString(root.get('Desc')) :
4568
+ '';
4569
+ if (root.has('RF')) {
4570
+ warn('Related file specifications are not supported');
4571
+ }
4572
+ this.contentAvailable = true;
4573
+ if (!root.has('EF')) {
4574
+ this.contentAvailable = false;
4575
+ warn('Non-embedded file specifications are not supported');
4576
+ }
4577
+ }
4578
+
4579
+ function pickPlatformItem(dict) {
4580
+ // Look for the filename in this order:
4581
+ // UF, F, Unix, Mac, DOS
4582
+ if (dict.has('UF')) {
4583
+ return dict.get('UF');
4584
+ } else if (dict.has('F')) {
4585
+ return dict.get('F');
4586
+ } else if (dict.has('Unix')) {
4587
+ return dict.get('Unix');
4588
+ } else if (dict.has('Mac')) {
4589
+ return dict.get('Mac');
4590
+ } else if (dict.has('DOS')) {
4591
+ return dict.get('DOS');
4592
+ } else {
4593
+ return null;
4594
+ }
4595
+ }
4596
+
4597
+ FileSpec.prototype = {
4598
+ get filename() {
4599
+ if (!this._filename && this.root) {
4600
+ var filename = pickPlatformItem(this.root) || 'unnamed';
4601
+ this._filename = stringToPDFString(filename).
4602
+ replace(/\\\\/g, '\\').
4603
+ replace(/\\\//g, '/').
4604
+ replace(/\\/g, '/');
4605
+ }
4606
+ return this._filename;
4607
+ },
4608
+ get content() {
4609
+ if (!this.contentAvailable) {
4610
+ return null;
4611
+ }
4612
+ if (!this.contentRef && this.root) {
4613
+ this.contentRef = pickPlatformItem(this.root.get('EF'));
4614
+ }
4615
+ var content = null;
4616
+ if (this.contentRef) {
4617
+ var xref = this.xref;
4618
+ var fileObj = xref.fetchIfRef(this.contentRef);
4619
+ if (fileObj && isStream(fileObj)) {
4620
+ content = fileObj.getBytes();
4621
+ } else {
4622
+ warn('Embedded file specification points to non-existing/invalid ' +
4623
+ 'content');
4624
+ }
4625
+ } else {
4626
+ warn('Embedded file specification does not have a content');
4627
+ }
4628
+ return content;
4629
+ },
4630
+ get serializable() {
4631
+ return {
4632
+ filename: this.filename,
4633
+ content: this.content
4634
+ };
4635
+ }
4636
+ };
4637
+ return FileSpec;
4638
+ })();
4639
+
4640
+ /**
4641
+ * A helper for loading missing data in object graphs. It traverses the graph
4642
+ * depth first and queues up any objects that have missing data. Once it has
4643
+ * has traversed as many objects that are available it attempts to bundle the
4644
+ * missing data requests and then resume from the nodes that weren't ready.
4645
+ *
4646
+ * NOTE: It provides protection from circular references by keeping track of
4647
+ * of loaded references. However, you must be careful not to load any graphs
4648
+ * that have references to the catalog or other pages since that will cause the
4649
+ * entire PDF document object graph to be traversed.
4650
+ */
4651
+ var ObjectLoader = (function() {
4652
+ function mayHaveChildren(value) {
4653
+ return isRef(value) || isDict(value) || isArray(value) || isStream(value);
4654
+ }
4655
+
4656
+ function addChildren(node, nodesToVisit) {
4657
+ var value;
4658
+ if (isDict(node) || isStream(node)) {
4659
+ var map;
4660
+ if (isDict(node)) {
4661
+ map = node.map;
4662
+ } else {
4663
+ map = node.dict.map;
4664
+ }
4665
+ for (var key in map) {
4666
+ value = map[key];
4667
+ if (mayHaveChildren(value)) {
4668
+ nodesToVisit.push(value);
4669
+ }
4670
+ }
4671
+ } else if (isArray(node)) {
4672
+ for (var i = 0, ii = node.length; i < ii; i++) {
4673
+ value = node[i];
4674
+ if (mayHaveChildren(value)) {
4675
+ nodesToVisit.push(value);
4676
+ }
4677
+ }
4678
+ }
4679
+ }
4680
+
4681
+ function ObjectLoader(obj, keys, xref) {
4682
+ this.obj = obj;
4683
+ this.keys = keys;
4684
+ this.xref = xref;
4685
+ this.refSet = null;
4686
+ }
4687
+
4688
+ ObjectLoader.prototype = {
4689
+ load: function ObjectLoader_load() {
4690
+ var keys = this.keys;
4691
+ this.capability = createPromiseCapability();
4692
+ // Don't walk the graph if all the data is already loaded.
4693
+ if (!(this.xref.stream instanceof ChunkedStream) ||
4694
+ this.xref.stream.getMissingChunks().length === 0) {
4695
+ this.capability.resolve();
4696
+ return this.capability.promise;
4697
+ }
4698
+
4699
+ this.refSet = new RefSet();
4700
+ // Setup the initial nodes to visit.
4701
+ var nodesToVisit = [];
4702
+ for (var i = 0; i < keys.length; i++) {
4703
+ nodesToVisit.push(this.obj[keys[i]]);
4704
+ }
4705
+
4706
+ this.walk(nodesToVisit);
4707
+ return this.capability.promise;
4708
+ },
4709
+
4710
+ walk: function ObjectLoader_walk(nodesToVisit) {
4711
+ var nodesToRevisit = [];
4712
+ var pendingRequests = [];
4713
+ // DFS walk of the object graph.
4714
+ while (nodesToVisit.length) {
4715
+ var currentNode = nodesToVisit.pop();
4716
+
4717
+ // Only references or chunked streams can cause missing data exceptions.
4718
+ if (isRef(currentNode)) {
4719
+ // Skip nodes that have already been visited.
4720
+ if (this.refSet.has(currentNode)) {
4721
+ continue;
4722
+ }
4723
+ try {
4724
+ var ref = currentNode;
4725
+ this.refSet.put(ref);
4726
+ currentNode = this.xref.fetch(currentNode);
4727
+ } catch (e) {
4728
+ if (!(e instanceof MissingDataException)) {
4729
+ throw e;
4730
+ }
4731
+ nodesToRevisit.push(currentNode);
4732
+ pendingRequests.push({ begin: e.begin, end: e.end });
4733
+ }
4734
+ }
4735
+ if (currentNode && currentNode.getBaseStreams) {
4736
+ var baseStreams = currentNode.getBaseStreams();
4737
+ var foundMissingData = false;
4738
+ for (var i = 0; i < baseStreams.length; i++) {
4739
+ var stream = baseStreams[i];
4740
+ if (stream.getMissingChunks && stream.getMissingChunks().length) {
4741
+ foundMissingData = true;
4742
+ pendingRequests.push({
4743
+ begin: stream.start,
4744
+ end: stream.end
4745
+ });
4746
+ }
4747
+ }
4748
+ if (foundMissingData) {
4749
+ nodesToRevisit.push(currentNode);
4750
+ }
4751
+ }
4752
+
4753
+ addChildren(currentNode, nodesToVisit);
4754
+ }
4755
+
4756
+ if (pendingRequests.length) {
4757
+ this.xref.stream.manager.requestRanges(pendingRequests,
4758
+ function pendingRequestCallback() {
4759
+ nodesToVisit = nodesToRevisit;
4760
+ for (var i = 0; i < nodesToRevisit.length; i++) {
4761
+ var node = nodesToRevisit[i];
4762
+ // Remove any reference nodes from the currrent refset so they
4763
+ // aren't skipped when we revist them.
4764
+ if (isRef(node)) {
4765
+ this.refSet.remove(node);
4766
+ }
4767
+ }
4768
+ this.walk(nodesToVisit);
4769
+ }.bind(this));
4770
+ return;
4771
+ }
4772
+ // Everything is loaded.
4773
+ this.refSet = null;
4774
+ this.capability.resolve();
4775
+ }
4776
+ };
4777
+
4778
+ return ObjectLoader;
4779
+ })();
4780
+
4781
+
4782
+ var ISOAdobeCharset = [
4783
+ '.notdef', 'space', 'exclam', 'quotedbl', 'numbersign', 'dollar',
4784
+ 'percent', 'ampersand', 'quoteright', 'parenleft', 'parenright',
4785
+ 'asterisk', 'plus', 'comma', 'hyphen', 'period', 'slash', 'zero',
4786
+ 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',
4787
+ 'nine', 'colon', 'semicolon', 'less', 'equal', 'greater', 'question',
4788
+ 'at', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
4789
+ 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
4790
+ 'bracketleft', 'backslash', 'bracketright', 'asciicircum', 'underscore',
4791
+ 'quoteleft', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
4792
+ 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
4793
+ 'braceleft', 'bar', 'braceright', 'asciitilde', 'exclamdown', 'cent',
4794
+ 'sterling', 'fraction', 'yen', 'florin', 'section', 'currency',
4795
+ 'quotesingle', 'quotedblleft', 'guillemotleft', 'guilsinglleft',
4796
+ 'guilsinglright', 'fi', 'fl', 'endash', 'dagger', 'daggerdbl',
4797
+ 'periodcentered', 'paragraph', 'bullet', 'quotesinglbase',
4798
+ 'quotedblbase', 'quotedblright', 'guillemotright', 'ellipsis',
4799
+ 'perthousand', 'questiondown', 'grave', 'acute', 'circumflex', 'tilde',
4800
+ 'macron', 'breve', 'dotaccent', 'dieresis', 'ring', 'cedilla',
4801
+ 'hungarumlaut', 'ogonek', 'caron', 'emdash', 'AE', 'ordfeminine',
4802
+ 'Lslash', 'Oslash', 'OE', 'ordmasculine', 'ae', 'dotlessi', 'lslash',
4803
+ 'oslash', 'oe', 'germandbls', 'onesuperior', 'logicalnot', 'mu',
4804
+ 'trademark', 'Eth', 'onehalf', 'plusminus', 'Thorn', 'onequarter',
4805
+ 'divide', 'brokenbar', 'degree', 'thorn', 'threequarters', 'twosuperior',
4806
+ 'registered', 'minus', 'eth', 'multiply', 'threesuperior', 'copyright',
4807
+ 'Aacute', 'Acircumflex', 'Adieresis', 'Agrave', 'Aring', 'Atilde',
4808
+ 'Ccedilla', 'Eacute', 'Ecircumflex', 'Edieresis', 'Egrave', 'Iacute',
4809
+ 'Icircumflex', 'Idieresis', 'Igrave', 'Ntilde', 'Oacute', 'Ocircumflex',
4810
+ 'Odieresis', 'Ograve', 'Otilde', 'Scaron', 'Uacute', 'Ucircumflex',
4811
+ 'Udieresis', 'Ugrave', 'Yacute', 'Ydieresis', 'Zcaron', 'aacute',
4812
+ 'acircumflex', 'adieresis', 'agrave', 'aring', 'atilde', 'ccedilla',
4813
+ 'eacute', 'ecircumflex', 'edieresis', 'egrave', 'iacute', 'icircumflex',
4814
+ 'idieresis', 'igrave', 'ntilde', 'oacute', 'ocircumflex', 'odieresis',
4815
+ 'ograve', 'otilde', 'scaron', 'uacute', 'ucircumflex', 'udieresis',
4816
+ 'ugrave', 'yacute', 'ydieresis', 'zcaron'
4817
+ ];
4818
+
4819
+ var ExpertCharset = [
4820
+ '.notdef', 'space', 'exclamsmall', 'Hungarumlautsmall', 'dollaroldstyle',
4821
+ 'dollarsuperior', 'ampersandsmall', 'Acutesmall', 'parenleftsuperior',
4822
+ 'parenrightsuperior', 'twodotenleader', 'onedotenleader', 'comma',
4823
+ 'hyphen', 'period', 'fraction', 'zerooldstyle', 'oneoldstyle',
4824
+ 'twooldstyle', 'threeoldstyle', 'fouroldstyle', 'fiveoldstyle',
4825
+ 'sixoldstyle', 'sevenoldstyle', 'eightoldstyle', 'nineoldstyle',
4826
+ 'colon', 'semicolon', 'commasuperior', 'threequartersemdash',
4827
+ 'periodsuperior', 'questionsmall', 'asuperior', 'bsuperior',
4828
+ 'centsuperior', 'dsuperior', 'esuperior', 'isuperior', 'lsuperior',
4829
+ 'msuperior', 'nsuperior', 'osuperior', 'rsuperior', 'ssuperior',
4830
+ 'tsuperior', 'ff', 'fi', 'fl', 'ffi', 'ffl', 'parenleftinferior',
4831
+ 'parenrightinferior', 'Circumflexsmall', 'hyphensuperior', 'Gravesmall',
4832
+ 'Asmall', 'Bsmall', 'Csmall', 'Dsmall', 'Esmall', 'Fsmall', 'Gsmall',
4833
+ 'Hsmall', 'Ismall', 'Jsmall', 'Ksmall', 'Lsmall', 'Msmall', 'Nsmall',
4834
+ 'Osmall', 'Psmall', 'Qsmall', 'Rsmall', 'Ssmall', 'Tsmall', 'Usmall',
4835
+ 'Vsmall', 'Wsmall', 'Xsmall', 'Ysmall', 'Zsmall', 'colonmonetary',
4836
+ 'onefitted', 'rupiah', 'Tildesmall', 'exclamdownsmall', 'centoldstyle',
4837
+ 'Lslashsmall', 'Scaronsmall', 'Zcaronsmall', 'Dieresissmall',
4838
+ 'Brevesmall', 'Caronsmall', 'Dotaccentsmall', 'Macronsmall',
4839
+ 'figuredash', 'hypheninferior', 'Ogoneksmall', 'Ringsmall',
4840
+ 'Cedillasmall', 'onequarter', 'onehalf', 'threequarters',
4841
+ 'questiondownsmall', 'oneeighth', 'threeeighths', 'fiveeighths',
4842
+ 'seveneighths', 'onethird', 'twothirds', 'zerosuperior', 'onesuperior',
4843
+ 'twosuperior', 'threesuperior', 'foursuperior', 'fivesuperior',
4844
+ 'sixsuperior', 'sevensuperior', 'eightsuperior', 'ninesuperior',
4845
+ 'zeroinferior', 'oneinferior', 'twoinferior', 'threeinferior',
4846
+ 'fourinferior', 'fiveinferior', 'sixinferior', 'seveninferior',
4847
+ 'eightinferior', 'nineinferior', 'centinferior', 'dollarinferior',
4848
+ 'periodinferior', 'commainferior', 'Agravesmall', 'Aacutesmall',
4849
+ 'Acircumflexsmall', 'Atildesmall', 'Adieresissmall', 'Aringsmall',
4850
+ 'AEsmall', 'Ccedillasmall', 'Egravesmall', 'Eacutesmall',
4851
+ 'Ecircumflexsmall', 'Edieresissmall', 'Igravesmall', 'Iacutesmall',
4852
+ 'Icircumflexsmall', 'Idieresissmall', 'Ethsmall', 'Ntildesmall',
4853
+ 'Ogravesmall', 'Oacutesmall', 'Ocircumflexsmall', 'Otildesmall',
4854
+ 'Odieresissmall', 'OEsmall', 'Oslashsmall', 'Ugravesmall', 'Uacutesmall',
4855
+ 'Ucircumflexsmall', 'Udieresissmall', 'Yacutesmall', 'Thornsmall',
4856
+ 'Ydieresissmall'
4857
+ ];
4858
+
4859
+ var ExpertSubsetCharset = [
4860
+ '.notdef', 'space', 'dollaroldstyle', 'dollarsuperior',
4861
+ 'parenleftsuperior', 'parenrightsuperior', 'twodotenleader',
4862
+ 'onedotenleader', 'comma', 'hyphen', 'period', 'fraction',
4863
+ 'zerooldstyle', 'oneoldstyle', 'twooldstyle', 'threeoldstyle',
4864
+ 'fouroldstyle', 'fiveoldstyle', 'sixoldstyle', 'sevenoldstyle',
4865
+ 'eightoldstyle', 'nineoldstyle', 'colon', 'semicolon', 'commasuperior',
4866
+ 'threequartersemdash', 'periodsuperior', 'asuperior', 'bsuperior',
4867
+ 'centsuperior', 'dsuperior', 'esuperior', 'isuperior', 'lsuperior',
4868
+ 'msuperior', 'nsuperior', 'osuperior', 'rsuperior', 'ssuperior',
4869
+ 'tsuperior', 'ff', 'fi', 'fl', 'ffi', 'ffl', 'parenleftinferior',
4870
+ 'parenrightinferior', 'hyphensuperior', 'colonmonetary', 'onefitted',
4871
+ 'rupiah', 'centoldstyle', 'figuredash', 'hypheninferior', 'onequarter',
4872
+ 'onehalf', 'threequarters', 'oneeighth', 'threeeighths', 'fiveeighths',
4873
+ 'seveneighths', 'onethird', 'twothirds', 'zerosuperior', 'onesuperior',
4874
+ 'twosuperior', 'threesuperior', 'foursuperior', 'fivesuperior',
4875
+ 'sixsuperior', 'sevensuperior', 'eightsuperior', 'ninesuperior',
4876
+ 'zeroinferior', 'oneinferior', 'twoinferior', 'threeinferior',
4877
+ 'fourinferior', 'fiveinferior', 'sixinferior', 'seveninferior',
4878
+ 'eightinferior', 'nineinferior', 'centinferior', 'dollarinferior',
4879
+ 'periodinferior', 'commainferior'
4880
+ ];
4881
+
4882
+
4883
+ var DEFAULT_ICON_SIZE = 22; // px
4884
+ var SUPPORTED_TYPES = ['Link', 'Text', 'Widget'];
4885
+
4886
+ var Annotation = (function AnnotationClosure() {
4887
+ // 12.5.5: Algorithm: Appearance streams
4888
+ function getTransformMatrix(rect, bbox, matrix) {
4889
+ var bounds = Util.getAxialAlignedBoundingBox(bbox, matrix);
4890
+ var minX = bounds[0];
4891
+ var minY = bounds[1];
4892
+ var maxX = bounds[2];
4893
+ var maxY = bounds[3];
4894
+
4895
+ if (minX === maxX || minY === maxY) {
4896
+ // From real-life file, bbox was [0, 0, 0, 0]. In this case,
4897
+ // just apply the transform for rect
4898
+ return [1, 0, 0, 1, rect[0], rect[1]];
4899
+ }
4900
+
4901
+ var xRatio = (rect[2] - rect[0]) / (maxX - minX);
4902
+ var yRatio = (rect[3] - rect[1]) / (maxY - minY);
4903
+ return [
4904
+ xRatio,
4905
+ 0,
4906
+ 0,
4907
+ yRatio,
4908
+ rect[0] - minX * xRatio,
4909
+ rect[1] - minY * yRatio
4910
+ ];
4911
+ }
4912
+
4913
+ function getDefaultAppearance(dict) {
4914
+ var appearanceState = dict.get('AP');
4915
+ if (!isDict(appearanceState)) {
4916
+ return;
4917
+ }
4918
+
4919
+ var appearance;
4920
+ var appearances = appearanceState.get('N');
4921
+ if (isDict(appearances)) {
4922
+ var as = dict.get('AS');
4923
+ if (as && appearances.has(as.name)) {
4924
+ appearance = appearances.get(as.name);
4925
+ }
4926
+ } else {
4927
+ appearance = appearances;
4928
+ }
4929
+ return appearance;
4930
+ }
4931
+
4932
+ function Annotation(params) {
4933
+ var dict = params.dict;
4934
+ var data = this.data = {};
4935
+
4936
+ data.subtype = dict.get('Subtype').name;
4937
+ var rect = dict.get('Rect') || [0, 0, 0, 0];
4938
+ data.rect = Util.normalizeRect(rect);
4939
+ data.annotationFlags = dict.get('F');
4940
+
4941
+ var color = dict.get('C');
4942
+ if (!color) {
4943
+ // The PDF spec does not mention how a missing color array is interpreted.
4944
+ // Adobe Reader seems to default to black in this case.
4945
+ data.color = [0, 0, 0];
4946
+ } else if (isArray(color)) {
4947
+ switch (color.length) {
4948
+ case 0:
4949
+ // Empty array denotes transparent border.
4950
+ data.color = null;
4951
+ break;
4952
+ case 1:
4953
+ // TODO: implement DeviceGray
4954
+ break;
4955
+ case 3:
4956
+ data.color = color;
4957
+ break;
4958
+ case 4:
4959
+ // TODO: implement DeviceCMYK
4960
+ break;
4961
+ }
4962
+ }
4963
+
4964
+ // Some types of annotations have border style dict which has more
4965
+ // info than the border array
4966
+ if (dict.has('BS')) {
4967
+ var borderStyle = dict.get('BS');
4968
+ data.borderWidth = borderStyle.has('W') ? borderStyle.get('W') : 1;
4969
+ } else {
4970
+ var borderArray = dict.get('Border') || [0, 0, 1];
4971
+ data.borderWidth = borderArray[2] || 0;
4972
+
4973
+ // TODO: implement proper support for annotations with line dash patterns.
4974
+ var dashArray = borderArray[3];
4975
+ if (data.borderWidth > 0 && dashArray) {
4976
+ if (!isArray(dashArray)) {
4977
+ // Ignore the border if dashArray is not actually an array,
4978
+ // this is consistent with the behaviour in Adobe Reader.
4979
+ data.borderWidth = 0;
4980
+ } else {
4981
+ var dashArrayLength = dashArray.length;
4982
+ if (dashArrayLength > 0) {
4983
+ // According to the PDF specification: the elements in a dashArray
4984
+ // shall be numbers that are nonnegative and not all equal to zero.
4985
+ var isInvalid = false;
4986
+ var numPositive = 0;
4987
+ for (var i = 0; i < dashArrayLength; i++) {
4988
+ var validNumber = (+dashArray[i] >= 0);
4989
+ if (!validNumber) {
4990
+ isInvalid = true;
4991
+ break;
4992
+ } else if (dashArray[i] > 0) {
4993
+ numPositive++;
4994
+ }
4995
+ }
4996
+ if (isInvalid || numPositive === 0) {
4997
+ data.borderWidth = 0;
4998
+ }
4999
+ }
5000
+ }
5001
+ }
5002
+ }
5003
+
5004
+ this.appearance = getDefaultAppearance(dict);
5005
+ data.hasAppearance = !!this.appearance;
5006
+ data.id = params.ref.num;
5007
+ }
5008
+
5009
+ Annotation.prototype = {
5010
+
5011
+ getData: function Annotation_getData() {
5012
+ return this.data;
5013
+ },
5014
+
5015
+ isInvisible: function Annotation_isInvisible() {
5016
+ var data = this.data;
5017
+ if (data && SUPPORTED_TYPES.indexOf(data.subtype) !== -1) {
5018
+ return false;
5019
+ } else {
5020
+ return !!(data &&
5021
+ data.annotationFlags && // Default: not invisible
5022
+ data.annotationFlags & 0x1); // Invisible
5023
+ }
5024
+ },
5025
+
5026
+ isViewable: function Annotation_isViewable() {
5027
+ var data = this.data;
5028
+ return !!(!this.isInvisible() &&
5029
+ data &&
5030
+ (!data.annotationFlags ||
5031
+ !(data.annotationFlags & 0x22)) && // Hidden or NoView
5032
+ data.rect); // rectangle is necessary
5033
+ },
5034
+
5035
+ isPrintable: function Annotation_isPrintable() {
5036
+ var data = this.data;
5037
+ return !!(!this.isInvisible() &&
5038
+ data &&
5039
+ data.annotationFlags && // Default: not printable
5040
+ data.annotationFlags & 0x4 && // Print
5041
+ !(data.annotationFlags & 0x2) && // Hidden
5042
+ data.rect); // rectangle is necessary
5043
+ },
5044
+
5045
+ loadResources: function Annotation_loadResources(keys) {
5046
+ return new Promise(function (resolve, reject) {
5047
+ this.appearance.dict.getAsync('Resources').then(function (resources) {
5048
+ if (!resources) {
5049
+ resolve();
5050
+ return;
5051
+ }
5052
+ var objectLoader = new ObjectLoader(resources.map,
5053
+ keys,
5054
+ resources.xref);
5055
+ objectLoader.load().then(function() {
5056
+ resolve(resources);
5057
+ }, reject);
5058
+ }, reject);
5059
+ }.bind(this));
5060
+ },
5061
+
5062
+ getOperatorList: function Annotation_getOperatorList(evaluator) {
5063
+
5064
+ if (!this.appearance) {
5065
+ return Promise.resolve(new OperatorList());
5066
+ }
5067
+
5068
+ var data = this.data;
5069
+
5070
+ var appearanceDict = this.appearance.dict;
5071
+ var resourcesPromise = this.loadResources([
5072
+ 'ExtGState',
5073
+ 'ColorSpace',
5074
+ 'Pattern',
5075
+ 'Shading',
5076
+ 'XObject',
5077
+ 'Font'
5078
+ // ProcSet
5079
+ // Properties
5080
+ ]);
5081
+ var bbox = appearanceDict.get('BBox') || [0, 0, 1, 1];
5082
+ var matrix = appearanceDict.get('Matrix') || [1, 0, 0, 1, 0 ,0];
5083
+ var transform = getTransformMatrix(data.rect, bbox, matrix);
5084
+ var self = this;
5085
+
5086
+ return resourcesPromise.then(function(resources) {
5087
+ var opList = new OperatorList();
5088
+ opList.addOp(OPS.beginAnnotation, [data.rect, transform, matrix]);
5089
+ return evaluator.getOperatorList(self.appearance, resources, opList).
5090
+ then(function () {
5091
+ opList.addOp(OPS.endAnnotation, []);
5092
+ self.appearance.reset();
5093
+ return opList;
5094
+ });
5095
+ });
5096
+ }
5097
+ };
5098
+
5099
+ Annotation.getConstructor =
5100
+ function Annotation_getConstructor(subtype, fieldType) {
5101
+
5102
+ if (!subtype) {
5103
+ return;
5104
+ }
5105
+
5106
+ // TODO(mack): Implement FreeText annotations
5107
+ if (subtype === 'Link') {
5108
+ return LinkAnnotation;
5109
+ } else if (subtype === 'Text') {
5110
+ return TextAnnotation;
5111
+ } else if (subtype === 'Widget') {
5112
+ if (!fieldType) {
5113
+ return;
5114
+ }
5115
+
5116
+ if (fieldType === 'Tx') {
5117
+ return TextWidgetAnnotation;
5118
+ } else {
5119
+ return WidgetAnnotation;
5120
+ }
5121
+ } else {
5122
+ return Annotation;
5123
+ }
5124
+ };
5125
+
5126
+ Annotation.fromRef = function Annotation_fromRef(xref, ref) {
5127
+
5128
+ var dict = xref.fetchIfRef(ref);
5129
+ if (!isDict(dict)) {
5130
+ return;
5131
+ }
5132
+
5133
+ var subtype = dict.get('Subtype');
5134
+ subtype = isName(subtype) ? subtype.name : '';
5135
+ if (!subtype) {
5136
+ return;
5137
+ }
5138
+
5139
+ var fieldType = Util.getInheritableProperty(dict, 'FT');
5140
+ fieldType = isName(fieldType) ? fieldType.name : '';
5141
+
5142
+ var Constructor = Annotation.getConstructor(subtype, fieldType);
5143
+ if (!Constructor) {
5144
+ return;
5145
+ }
5146
+
5147
+ var params = {
5148
+ dict: dict,
5149
+ ref: ref,
5150
+ };
5151
+
5152
+ var annotation = new Constructor(params);
5153
+
5154
+ if (annotation.isViewable() || annotation.isPrintable()) {
5155
+ return annotation;
5156
+ } else {
5157
+ if (SUPPORTED_TYPES.indexOf(subtype) === -1) {
5158
+ warn('unimplemented annotation type: ' + subtype);
5159
+ }
5160
+ }
5161
+ };
5162
+
5163
+ Annotation.appendToOperatorList = function Annotation_appendToOperatorList(
5164
+ annotations, opList, pdfManager, partialEvaluator, intent) {
5165
+
5166
+ function reject(e) {
5167
+ annotationsReadyCapability.reject(e);
5168
+ }
5169
+
5170
+ var annotationsReadyCapability = createPromiseCapability();
5171
+
5172
+ var annotationPromises = [];
5173
+ for (var i = 0, n = annotations.length; i < n; ++i) {
5174
+ if (intent === 'display' && annotations[i].isViewable() ||
5175
+ intent === 'print' && annotations[i].isPrintable()) {
5176
+ annotationPromises.push(
5177
+ annotations[i].getOperatorList(partialEvaluator));
5178
+ }
5179
+ }
5180
+ Promise.all(annotationPromises).then(function(datas) {
5181
+ opList.addOp(OPS.beginAnnotations, []);
5182
+ for (var i = 0, n = datas.length; i < n; ++i) {
5183
+ var annotOpList = datas[i];
5184
+ opList.addOpList(annotOpList);
5185
+ }
5186
+ opList.addOp(OPS.endAnnotations, []);
5187
+ annotationsReadyCapability.resolve();
5188
+ }, reject);
5189
+
5190
+ return annotationsReadyCapability.promise;
5191
+ };
5192
+
5193
+ return Annotation;
5194
+ })();
5195
+
5196
+ var WidgetAnnotation = (function WidgetAnnotationClosure() {
5197
+
5198
+ function WidgetAnnotation(params) {
5199
+ Annotation.call(this, params);
5200
+
5201
+ var dict = params.dict;
5202
+ var data = this.data;
5203
+
5204
+ data.fieldValue = stringToPDFString(
5205
+ Util.getInheritableProperty(dict, 'V') || '');
5206
+ data.alternativeText = stringToPDFString(dict.get('TU') || '');
5207
+ data.defaultAppearance = Util.getInheritableProperty(dict, 'DA') || '';
5208
+ var fieldType = Util.getInheritableProperty(dict, 'FT');
5209
+ data.fieldType = isName(fieldType) ? fieldType.name : '';
5210
+ data.fieldFlags = Util.getInheritableProperty(dict, 'Ff') || 0;
5211
+ this.fieldResources = Util.getInheritableProperty(dict, 'DR') || Dict.empty;
5212
+
5213
+ // Building the full field name by collecting the field and
5214
+ // its ancestors 'T' data and joining them using '.'.
5215
+ var fieldName = [];
5216
+ var namedItem = dict;
5217
+ var ref = params.ref;
5218
+ while (namedItem) {
5219
+ var parent = namedItem.get('Parent');
5220
+ var parentRef = namedItem.getRaw('Parent');
5221
+ var name = namedItem.get('T');
5222
+ if (name) {
5223
+ fieldName.unshift(stringToPDFString(name));
5224
+ } else if (parent && ref) {
5225
+ // The field name is absent, that means more than one field
5226
+ // with the same name may exist. Replacing the empty name
5227
+ // with the '`' plus index in the parent's 'Kids' array.
5228
+ // This is not in the PDF spec but necessary to id the
5229
+ // the input controls.
5230
+ var kids = parent.get('Kids');
5231
+ var j, jj;
5232
+ for (j = 0, jj = kids.length; j < jj; j++) {
5233
+ var kidRef = kids[j];
5234
+ if (kidRef.num === ref.num && kidRef.gen === ref.gen) {
5235
+ break;
5236
+ }
5237
+ }
5238
+ fieldName.unshift('`' + j);
5239
+ }
5240
+ namedItem = parent;
5241
+ ref = parentRef;
5242
+ }
5243
+ data.fullName = fieldName.join('.');
5244
+ }
5245
+
5246
+ var parent = Annotation.prototype;
5247
+ Util.inherit(WidgetAnnotation, Annotation, {
5248
+ isViewable: function WidgetAnnotation_isViewable() {
5249
+ if (this.data.fieldType === 'Sig') {
5250
+ warn('unimplemented annotation type: Widget signature');
5251
+ return false;
5252
+ }
5253
+
5254
+ return parent.isViewable.call(this);
5255
+ }
5256
+ });
5257
+
5258
+ return WidgetAnnotation;
5259
+ })();
5260
+
5261
+ var TextWidgetAnnotation = (function TextWidgetAnnotationClosure() {
5262
+ function TextWidgetAnnotation(params) {
5263
+ WidgetAnnotation.call(this, params);
5264
+
5265
+ this.data.textAlignment = Util.getInheritableProperty(params.dict, 'Q');
5266
+ this.data.annotationType = AnnotationType.WIDGET;
5267
+ this.data.hasHtml = !this.data.hasAppearance && !!this.data.fieldValue;
5268
+ }
5269
+
5270
+ Util.inherit(TextWidgetAnnotation, WidgetAnnotation, {
5271
+ getOperatorList: function TextWidgetAnnotation_getOperatorList(evaluator) {
5272
+ if (this.appearance) {
5273
+ return Annotation.prototype.getOperatorList.call(this, evaluator);
5274
+ }
5275
+
5276
+ var opList = new OperatorList();
5277
+ var data = this.data;
5278
+
5279
+ // Even if there is an appearance stream, ignore it. This is the
5280
+ // behaviour used by Adobe Reader.
5281
+ if (!data.defaultAppearance) {
5282
+ return Promise.resolve(opList);
5283
+ }
5284
+
5285
+ var stream = new Stream(stringToBytes(data.defaultAppearance));
5286
+ return evaluator.getOperatorList(stream, this.fieldResources, opList).
5287
+ then(function () {
5288
+ return opList;
5289
+ });
5290
+ }
5291
+ });
5292
+
5293
+ return TextWidgetAnnotation;
5294
+ })();
5295
+
5296
+ var InteractiveAnnotation = (function InteractiveAnnotationClosure() {
5297
+ function InteractiveAnnotation(params) {
5298
+ Annotation.call(this, params);
5299
+
5300
+ this.data.hasHtml = true;
5301
+ }
5302
+
5303
+ Util.inherit(InteractiveAnnotation, Annotation, { });
5304
+
5305
+ return InteractiveAnnotation;
5306
+ })();
5307
+
5308
+ var TextAnnotation = (function TextAnnotationClosure() {
5309
+ function TextAnnotation(params) {
5310
+ InteractiveAnnotation.call(this, params);
5311
+
5312
+ var dict = params.dict;
5313
+ var data = this.data;
5314
+
5315
+ var content = dict.get('Contents');
5316
+ var title = dict.get('T');
5317
+ data.annotationType = AnnotationType.TEXT;
5318
+ data.content = stringToPDFString(content || '');
5319
+ data.title = stringToPDFString(title || '');
5320
+
5321
+ if (data.hasAppearance) {
5322
+ data.name = 'NoIcon';
5323
+ } else {
5324
+ data.rect[1] = data.rect[3] - DEFAULT_ICON_SIZE;
5325
+ data.rect[2] = data.rect[0] + DEFAULT_ICON_SIZE;
5326
+ data.name = dict.has('Name') ? dict.get('Name').name : 'Note';
5327
+ }
5328
+
5329
+ if (dict.has('C')) {
5330
+ data.hasBgColor = true;
5331
+ }
5332
+ }
5333
+
5334
+ Util.inherit(TextAnnotation, InteractiveAnnotation, { });
5335
+
5336
+ return TextAnnotation;
5337
+ })();
5338
+
5339
+ var LinkAnnotation = (function LinkAnnotationClosure() {
5340
+ function LinkAnnotation(params) {
5341
+ InteractiveAnnotation.call(this, params);
5342
+
5343
+ var dict = params.dict;
5344
+ var data = this.data;
5345
+ data.annotationType = AnnotationType.LINK;
5346
+
5347
+ var action = dict.get('A');
5348
+ if (action && isDict(action)) {
5349
+ var linkType = action.get('S').name;
5350
+ if (linkType === 'URI') {
5351
+ var url = action.get('URI');
5352
+ if (isName(url)) {
5353
+ // Some bad PDFs do not put parentheses around relative URLs.
5354
+ url = '/' + url.name;
5355
+ } else if (url) {
5356
+ url = addDefaultProtocolToUrl(url);
5357
+ }
5358
+ // TODO: pdf spec mentions urls can be relative to a Base
5359
+ // entry in the dictionary.
5360
+ if (!isValidUrl(url, false)) {
5361
+ url = '';
5362
+ }
5363
+ data.url = url;
5364
+ } else if (linkType === 'GoTo') {
5365
+ data.dest = action.get('D');
5366
+ } else if (linkType === 'GoToR') {
5367
+ var urlDict = action.get('F');
5368
+ if (isDict(urlDict)) {
5369
+ // We assume that the 'url' is a Filspec dictionary
5370
+ // and fetch the url without checking any further
5371
+ url = urlDict.get('F') || '';
5372
+ }
5373
+
5374
+ // TODO: pdf reference says that GoToR
5375
+ // can also have 'NewWindow' attribute
5376
+ if (!isValidUrl(url, false)) {
5377
+ url = '';
5378
+ }
5379
+ data.url = url;
5380
+ data.dest = action.get('D');
5381
+ } else if (linkType === 'Named') {
5382
+ data.action = action.get('N').name;
5383
+ } else {
5384
+ warn('unrecognized link type: ' + linkType);
5385
+ }
5386
+ } else if (dict.has('Dest')) {
5387
+ // simple destination link
5388
+ var dest = dict.get('Dest');
5389
+ data.dest = isName(dest) ? dest.name : dest;
5390
+ }
5391
+ }
5392
+
5393
+ // Lets URLs beginning with 'www.' default to using the 'http://' protocol.
5394
+ function addDefaultProtocolToUrl(url) {
5395
+ if (url && url.indexOf('www.') === 0) {
5396
+ return ('http://' + url);
5397
+ }
5398
+ return url;
5399
+ }
5400
+
5401
+ Util.inherit(LinkAnnotation, InteractiveAnnotation, { });
5402
+
5403
+ return LinkAnnotation;
5404
+ })();
5405
+
5406
+
5407
+ var PDFFunction = (function PDFFunctionClosure() {
5408
+ var CONSTRUCT_SAMPLED = 0;
5409
+ var CONSTRUCT_INTERPOLATED = 2;
5410
+ var CONSTRUCT_STICHED = 3;
5411
+ var CONSTRUCT_POSTSCRIPT = 4;
5412
+
5413
+ return {
5414
+ getSampleArray: function PDFFunction_getSampleArray(size, outputSize, bps,
5415
+ str) {
5416
+ var i, ii;
5417
+ var length = 1;
5418
+ for (i = 0, ii = size.length; i < ii; i++) {
5419
+ length *= size[i];
5420
+ }
5421
+ length *= outputSize;
5422
+
5423
+ var array = new Array(length);
5424
+ var codeSize = 0;
5425
+ var codeBuf = 0;
5426
+ // 32 is a valid bps so shifting won't work
5427
+ var sampleMul = 1.0 / (Math.pow(2.0, bps) - 1);
5428
+
5429
+ var strBytes = str.getBytes((length * bps + 7) / 8);
5430
+ var strIdx = 0;
5431
+ for (i = 0; i < length; i++) {
5432
+ while (codeSize < bps) {
5433
+ codeBuf <<= 8;
5434
+ codeBuf |= strBytes[strIdx++];
5435
+ codeSize += 8;
5436
+ }
5437
+ codeSize -= bps;
5438
+ array[i] = (codeBuf >> codeSize) * sampleMul;
5439
+ codeBuf &= (1 << codeSize) - 1;
5440
+ }
5441
+ return array;
5442
+ },
5443
+
5444
+ getIR: function PDFFunction_getIR(xref, fn) {
5445
+ var dict = fn.dict;
5446
+ if (!dict) {
5447
+ dict = fn;
5448
+ }
5449
+
5450
+ var types = [this.constructSampled,
5451
+ null,
5452
+ this.constructInterpolated,
5453
+ this.constructStiched,
5454
+ this.constructPostScript];
5455
+
5456
+ var typeNum = dict.get('FunctionType');
5457
+ var typeFn = types[typeNum];
5458
+ if (!typeFn) {
5459
+ error('Unknown type of function');
5460
+ }
5461
+
5462
+ return typeFn.call(this, fn, dict, xref);
5463
+ },
5464
+
5465
+ fromIR: function PDFFunction_fromIR(IR) {
5466
+ var type = IR[0];
5467
+ switch (type) {
5468
+ case CONSTRUCT_SAMPLED:
5469
+ return this.constructSampledFromIR(IR);
5470
+ case CONSTRUCT_INTERPOLATED:
5471
+ return this.constructInterpolatedFromIR(IR);
5472
+ case CONSTRUCT_STICHED:
5473
+ return this.constructStichedFromIR(IR);
5474
+ //case CONSTRUCT_POSTSCRIPT:
5475
+ default:
5476
+ return this.constructPostScriptFromIR(IR);
5477
+ }
5478
+ },
5479
+
5480
+ parse: function PDFFunction_parse(xref, fn) {
5481
+ var IR = this.getIR(xref, fn);
5482
+ return this.fromIR(IR);
5483
+ },
5484
+
5485
+ parseArray: function PDFFunction_parseArray(xref, fnObj) {
5486
+ if (!isArray(fnObj)) {
5487
+ // not an array -- parsing as regular function
5488
+ return this.parse(xref, fnObj);
5489
+ }
5490
+
5491
+ var fnArray = [];
5492
+ for (var j = 0, jj = fnObj.length; j < jj; j++) {
5493
+ var obj = xref.fetchIfRef(fnObj[j]);
5494
+ fnArray.push(PDFFunction.parse(xref, obj));
5495
+ }
5496
+ return function (src, srcOffset, dest, destOffset) {
5497
+ for (var i = 0, ii = fnArray.length; i < ii; i++) {
5498
+ fnArray[i](src, srcOffset, dest, destOffset + i);
5499
+ }
5500
+ };
5501
+ },
5502
+
5503
+ constructSampled: function PDFFunction_constructSampled(str, dict) {
5504
+ function toMultiArray(arr) {
5505
+ var inputLength = arr.length;
5506
+ var out = [];
5507
+ var index = 0;
5508
+ for (var i = 0; i < inputLength; i += 2) {
5509
+ out[index] = [arr[i], arr[i + 1]];
5510
+ ++index;
5511
+ }
5512
+ return out;
5513
+ }
5514
+ var domain = dict.get('Domain');
5515
+ var range = dict.get('Range');
5516
+
5517
+ if (!domain || !range) {
5518
+ error('No domain or range');
5519
+ }
5520
+
5521
+ var inputSize = domain.length / 2;
5522
+ var outputSize = range.length / 2;
5523
+
5524
+ domain = toMultiArray(domain);
5525
+ range = toMultiArray(range);
5526
+
5527
+ var size = dict.get('Size');
5528
+ var bps = dict.get('BitsPerSample');
5529
+ var order = dict.get('Order') || 1;
5530
+ if (order !== 1) {
5531
+ // No description how cubic spline interpolation works in PDF32000:2008
5532
+ // As in poppler, ignoring order, linear interpolation may work as good
5533
+ info('No support for cubic spline interpolation: ' + order);
5534
+ }
5535
+
5536
+ var encode = dict.get('Encode');
5537
+ if (!encode) {
5538
+ encode = [];
5539
+ for (var i = 0; i < inputSize; ++i) {
5540
+ encode.push(0);
5541
+ encode.push(size[i] - 1);
5542
+ }
5543
+ }
5544
+ encode = toMultiArray(encode);
5545
+
5546
+ var decode = dict.get('Decode');
5547
+ if (!decode) {
5548
+ decode = range;
5549
+ } else {
5550
+ decode = toMultiArray(decode);
5551
+ }
5552
+
5553
+ var samples = this.getSampleArray(size, outputSize, bps, str);
5554
+
5555
+ return [
5556
+ CONSTRUCT_SAMPLED, inputSize, domain, encode, decode, samples, size,
5557
+ outputSize, Math.pow(2, bps) - 1, range
5558
+ ];
5559
+ },
5560
+
5561
+ constructSampledFromIR: function PDFFunction_constructSampledFromIR(IR) {
5562
+ // See chapter 3, page 109 of the PDF reference
5563
+ function interpolate(x, xmin, xmax, ymin, ymax) {
5564
+ return ymin + ((x - xmin) * ((ymax - ymin) / (xmax - xmin)));
5565
+ }
5566
+
5567
+ return function constructSampledFromIRResult(src, srcOffset,
5568
+ dest, destOffset) {
5569
+ // See chapter 3, page 110 of the PDF reference.
5570
+ var m = IR[1];
5571
+ var domain = IR[2];
5572
+ var encode = IR[3];
5573
+ var decode = IR[4];
5574
+ var samples = IR[5];
5575
+ var size = IR[6];
5576
+ var n = IR[7];
5577
+ //var mask = IR[8];
5578
+ var range = IR[9];
5579
+
5580
+ // Building the cube vertices: its part and sample index
5581
+ // http://rjwagner49.com/Mathematics/Interpolation.pdf
5582
+ var cubeVertices = 1 << m;
5583
+ var cubeN = new Float64Array(cubeVertices);
5584
+ var cubeVertex = new Uint32Array(cubeVertices);
5585
+ var i, j;
5586
+ for (j = 0; j < cubeVertices; j++) {
5587
+ cubeN[j] = 1;
5588
+ }
5589
+
5590
+ var k = n, pos = 1;
5591
+ // Map x_i to y_j for 0 <= i < m using the sampled function.
5592
+ for (i = 0; i < m; ++i) {
5593
+ // x_i' = min(max(x_i, Domain_2i), Domain_2i+1)
5594
+ var domain_2i = domain[i][0];
5595
+ var domain_2i_1 = domain[i][1];
5596
+ var xi = Math.min(Math.max(src[srcOffset +i], domain_2i),
5597
+ domain_2i_1);
5598
+
5599
+ // e_i = Interpolate(x_i', Domain_2i, Domain_2i+1,
5600
+ // Encode_2i, Encode_2i+1)
5601
+ var e = interpolate(xi, domain_2i, domain_2i_1,
5602
+ encode[i][0], encode[i][1]);
5603
+
5604
+ // e_i' = min(max(e_i, 0), Size_i - 1)
5605
+ var size_i = size[i];
5606
+ e = Math.min(Math.max(e, 0), size_i - 1);
5607
+
5608
+ // Adjusting the cube: N and vertex sample index
5609
+ var e0 = e < size_i - 1 ? Math.floor(e) : e - 1; // e1 = e0 + 1;
5610
+ var n0 = e0 + 1 - e; // (e1 - e) / (e1 - e0);
5611
+ var n1 = e - e0; // (e - e0) / (e1 - e0);
5612
+ var offset0 = e0 * k;
5613
+ var offset1 = offset0 + k; // e1 * k
5614
+ for (j = 0; j < cubeVertices; j++) {
5615
+ if (j & pos) {
5616
+ cubeN[j] *= n1;
5617
+ cubeVertex[j] += offset1;
5618
+ } else {
5619
+ cubeN[j] *= n0;
5620
+ cubeVertex[j] += offset0;
5621
+ }
5622
+ }
5623
+
5624
+ k *= size_i;
5625
+ pos <<= 1;
5626
+ }
5627
+
5628
+ for (j = 0; j < n; ++j) {
5629
+ // Sum all cube vertices' samples portions
5630
+ var rj = 0;
5631
+ for (i = 0; i < cubeVertices; i++) {
5632
+ rj += samples[cubeVertex[i] + j] * cubeN[i];
5633
+ }
5634
+
5635
+ // r_j' = Interpolate(r_j, 0, 2^BitsPerSample - 1,
5636
+ // Decode_2j, Decode_2j+1)
5637
+ rj = interpolate(rj, 0, 1, decode[j][0], decode[j][1]);
5638
+
5639
+ // y_j = min(max(r_j, range_2j), range_2j+1)
5640
+ dest[destOffset + j] = Math.min(Math.max(rj, range[j][0]),
5641
+ range[j][1]);
5642
+ }
5643
+ };
5644
+ },
5645
+
5646
+ constructInterpolated: function PDFFunction_constructInterpolated(str,
5647
+ dict) {
5648
+ var c0 = dict.get('C0') || [0];
5649
+ var c1 = dict.get('C1') || [1];
5650
+ var n = dict.get('N');
5651
+
5652
+ if (!isArray(c0) || !isArray(c1)) {
5653
+ error('Illegal dictionary for interpolated function');
5654
+ }
5655
+
5656
+ var length = c0.length;
5657
+ var diff = [];
5658
+ for (var i = 0; i < length; ++i) {
5659
+ diff.push(c1[i] - c0[i]);
5660
+ }
5661
+
5662
+ return [CONSTRUCT_INTERPOLATED, c0, diff, n];
5663
+ },
5664
+
5665
+ constructInterpolatedFromIR:
5666
+ function PDFFunction_constructInterpolatedFromIR(IR) {
5667
+ var c0 = IR[1];
5668
+ var diff = IR[2];
5669
+ var n = IR[3];
5670
+
5671
+ var length = diff.length;
5672
+
5673
+ return function constructInterpolatedFromIRResult(src, srcOffset,
5674
+ dest, destOffset) {
5675
+ var x = n === 1 ? src[srcOffset] : Math.pow(src[srcOffset], n);
5676
+
5677
+ for (var j = 0; j < length; ++j) {
5678
+ dest[destOffset + j] = c0[j] + (x * diff[j]);
5679
+ }
5680
+ };
5681
+ },
5682
+
5683
+ constructStiched: function PDFFunction_constructStiched(fn, dict, xref) {
5684
+ var domain = dict.get('Domain');
5685
+
5686
+ if (!domain) {
5687
+ error('No domain');
5688
+ }
5689
+
5690
+ var inputSize = domain.length / 2;
5691
+ if (inputSize !== 1) {
5692
+ error('Bad domain for stiched function');
5693
+ }
5694
+
5695
+ var fnRefs = dict.get('Functions');
5696
+ var fns = [];
5697
+ for (var i = 0, ii = fnRefs.length; i < ii; ++i) {
5698
+ fns.push(PDFFunction.getIR(xref, xref.fetchIfRef(fnRefs[i])));
5699
+ }
5700
+
5701
+ var bounds = dict.get('Bounds');
5702
+ var encode = dict.get('Encode');
5703
+
5704
+ return [CONSTRUCT_STICHED, domain, bounds, encode, fns];
5705
+ },
5706
+
5707
+ constructStichedFromIR: function PDFFunction_constructStichedFromIR(IR) {
5708
+ var domain = IR[1];
5709
+ var bounds = IR[2];
5710
+ var encode = IR[3];
5711
+ var fnsIR = IR[4];
5712
+ var fns = [];
5713
+ var tmpBuf = new Float32Array(1);
5714
+
5715
+ for (var i = 0, ii = fnsIR.length; i < ii; i++) {
5716
+ fns.push(PDFFunction.fromIR(fnsIR[i]));
5717
+ }
5718
+
5719
+ return function constructStichedFromIRResult(src, srcOffset,
5720
+ dest, destOffset) {
5721
+ var clip = function constructStichedFromIRClip(v, min, max) {
5722
+ if (v > max) {
5723
+ v = max;
5724
+ } else if (v < min) {
5725
+ v = min;
5726
+ }
5727
+ return v;
5728
+ };
5729
+
5730
+ // clip to domain
5731
+ var v = clip(src[srcOffset], domain[0], domain[1]);
5732
+ // calulate which bound the value is in
5733
+ for (var i = 0, ii = bounds.length; i < ii; ++i) {
5734
+ if (v < bounds[i]) {
5735
+ break;
5736
+ }
5737
+ }
5738
+
5739
+ // encode value into domain of function
5740
+ var dmin = domain[0];
5741
+ if (i > 0) {
5742
+ dmin = bounds[i - 1];
5743
+ }
5744
+ var dmax = domain[1];
5745
+ if (i < bounds.length) {
5746
+ dmax = bounds[i];
5747
+ }
5748
+
5749
+ var rmin = encode[2 * i];
5750
+ var rmax = encode[2 * i + 1];
5751
+
5752
+ tmpBuf[0] = rmin + (v - dmin) * (rmax - rmin) / (dmax - dmin);
5753
+
5754
+ // call the appropriate function
5755
+ fns[i](tmpBuf, 0, dest, destOffset);
5756
+ };
5757
+ },
5758
+
5759
+ constructPostScript: function PDFFunction_constructPostScript(fn, dict,
5760
+ xref) {
5761
+ var domain = dict.get('Domain');
5762
+ var range = dict.get('Range');
5763
+
5764
+ if (!domain) {
5765
+ error('No domain.');
5766
+ }
5767
+
5768
+ if (!range) {
5769
+ error('No range.');
5770
+ }
5771
+
5772
+ var lexer = new PostScriptLexer(fn);
5773
+ var parser = new PostScriptParser(lexer);
5774
+ var code = parser.parse();
5775
+
5776
+ return [CONSTRUCT_POSTSCRIPT, domain, range, code];
5777
+ },
5778
+
5779
+ constructPostScriptFromIR: function PDFFunction_constructPostScriptFromIR(
5780
+ IR) {
5781
+ var domain = IR[1];
5782
+ var range = IR[2];
5783
+ var code = IR[3];
5784
+
5785
+ var compiled = (new PostScriptCompiler()).compile(code, domain, range);
5786
+ if (compiled) {
5787
+ // Compiled function consists of simple expressions such as addition,
5788
+ // subtraction, Math.max, and also contains 'var' and 'return'
5789
+ // statements. See the generation in the PostScriptCompiler below.
5790
+ /*jshint -W054 */
5791
+ return new Function('src', 'srcOffset', 'dest', 'destOffset', compiled);
5792
+ }
5793
+
5794
+ info('Unable to compile PS function');
5795
+
5796
+ var numOutputs = range.length >> 1;
5797
+ var numInputs = domain.length >> 1;
5798
+ var evaluator = new PostScriptEvaluator(code);
5799
+ // Cache the values for a big speed up, the cache size is limited though
5800
+ // since the number of possible values can be huge from a PS function.
5801
+ var cache = {};
5802
+ // The MAX_CACHE_SIZE is set to ~4x the maximum number of distinct values
5803
+ // seen in our tests.
5804
+ var MAX_CACHE_SIZE = 2048 * 4;
5805
+ var cache_available = MAX_CACHE_SIZE;
5806
+ var tmpBuf = new Float32Array(numInputs);
5807
+
5808
+ return function constructPostScriptFromIRResult(src, srcOffset,
5809
+ dest, destOffset) {
5810
+ var i, value;
5811
+ var key = '';
5812
+ var input = tmpBuf;
5813
+ for (i = 0; i < numInputs; i++) {
5814
+ value = src[srcOffset + i];
5815
+ input[i] = value;
5816
+ key += value + '_';
5817
+ }
5818
+
5819
+ var cachedValue = cache[key];
5820
+ if (cachedValue !== undefined) {
5821
+ dest.set(cachedValue, destOffset);
5822
+ return;
5823
+ }
5824
+
5825
+ var output = new Float32Array(numOutputs);
5826
+ var stack = evaluator.execute(input);
5827
+ var stackIndex = stack.length - numOutputs;
5828
+ for (i = 0; i < numOutputs; i++) {
5829
+ value = stack[stackIndex + i];
5830
+ var bound = range[i * 2];
5831
+ if (value < bound) {
5832
+ value = bound;
5833
+ } else {
5834
+ bound = range[i * 2 +1];
5835
+ if (value > bound) {
5836
+ value = bound;
5837
+ }
5838
+ }
5839
+ output[i] = value;
5840
+ }
5841
+ if (cache_available > 0) {
5842
+ cache_available--;
5843
+ cache[key] = output;
5844
+ }
5845
+ dest.set(output, destOffset);
5846
+ };
5847
+ }
5848
+ };
5849
+ })();
5850
+
5851
+ function isPDFFunction(v) {
5852
+ var fnDict;
5853
+ if (typeof v !== 'object') {
5854
+ return false;
5855
+ } else if (isDict(v)) {
5856
+ fnDict = v;
5857
+ } else if (isStream(v)) {
5858
+ fnDict = v.dict;
5859
+ } else {
5860
+ return false;
5861
+ }
5862
+ return fnDict.has('FunctionType');
5863
+ }
5864
+
5865
+ var PostScriptStack = (function PostScriptStackClosure() {
5866
+ var MAX_STACK_SIZE = 100;
5867
+ function PostScriptStack(initialStack) {
5868
+ this.stack = !initialStack ? [] :
5869
+ Array.prototype.slice.call(initialStack, 0);
5870
+ }
5871
+
5872
+ PostScriptStack.prototype = {
5873
+ push: function PostScriptStack_push(value) {
5874
+ if (this.stack.length >= MAX_STACK_SIZE) {
5875
+ error('PostScript function stack overflow.');
5876
+ }
5877
+ this.stack.push(value);
5878
+ },
5879
+ pop: function PostScriptStack_pop() {
5880
+ if (this.stack.length <= 0) {
5881
+ error('PostScript function stack underflow.');
5882
+ }
5883
+ return this.stack.pop();
5884
+ },
5885
+ copy: function PostScriptStack_copy(n) {
5886
+ if (this.stack.length + n >= MAX_STACK_SIZE) {
5887
+ error('PostScript function stack overflow.');
5888
+ }
5889
+ var stack = this.stack;
5890
+ for (var i = stack.length - n, j = n - 1; j >= 0; j--, i++) {
5891
+ stack.push(stack[i]);
5892
+ }
5893
+ },
5894
+ index: function PostScriptStack_index(n) {
5895
+ this.push(this.stack[this.stack.length - n - 1]);
5896
+ },
5897
+ // rotate the last n stack elements p times
5898
+ roll: function PostScriptStack_roll(n, p) {
5899
+ var stack = this.stack;
5900
+ var l = stack.length - n;
5901
+ var r = stack.length - 1, c = l + (p - Math.floor(p / n) * n), i, j, t;
5902
+ for (i = l, j = r; i < j; i++, j--) {
5903
+ t = stack[i]; stack[i] = stack[j]; stack[j] = t;
5904
+ }
5905
+ for (i = l, j = c - 1; i < j; i++, j--) {
5906
+ t = stack[i]; stack[i] = stack[j]; stack[j] = t;
5907
+ }
5908
+ for (i = c, j = r; i < j; i++, j--) {
5909
+ t = stack[i]; stack[i] = stack[j]; stack[j] = t;
5910
+ }
5911
+ }
5912
+ };
5913
+ return PostScriptStack;
5914
+ })();
5915
+ var PostScriptEvaluator = (function PostScriptEvaluatorClosure() {
5916
+ function PostScriptEvaluator(operators) {
5917
+ this.operators = operators;
5918
+ }
5919
+ PostScriptEvaluator.prototype = {
5920
+ execute: function PostScriptEvaluator_execute(initialStack) {
5921
+ var stack = new PostScriptStack(initialStack);
5922
+ var counter = 0;
5923
+ var operators = this.operators;
5924
+ var length = operators.length;
5925
+ var operator, a, b;
5926
+ while (counter < length) {
5927
+ operator = operators[counter++];
5928
+ if (typeof operator === 'number') {
5929
+ // Operator is really an operand and should be pushed to the stack.
5930
+ stack.push(operator);
5931
+ continue;
5932
+ }
5933
+ switch (operator) {
5934
+ // non standard ps operators
5935
+ case 'jz': // jump if false
5936
+ b = stack.pop();
5937
+ a = stack.pop();
5938
+ if (!a) {
5939
+ counter = b;
5940
+ }
5941
+ break;
5942
+ case 'j': // jump
5943
+ a = stack.pop();
5944
+ counter = a;
5945
+ break;
5946
+
5947
+ // all ps operators in alphabetical order (excluding if/ifelse)
5948
+ case 'abs':
5949
+ a = stack.pop();
5950
+ stack.push(Math.abs(a));
5951
+ break;
5952
+ case 'add':
5953
+ b = stack.pop();
5954
+ a = stack.pop();
5955
+ stack.push(a + b);
5956
+ break;
5957
+ case 'and':
5958
+ b = stack.pop();
5959
+ a = stack.pop();
5960
+ if (isBool(a) && isBool(b)) {
5961
+ stack.push(a && b);
5962
+ } else {
5963
+ stack.push(a & b);
5964
+ }
5965
+ break;
5966
+ case 'atan':
5967
+ a = stack.pop();
5968
+ stack.push(Math.atan(a));
5969
+ break;
5970
+ case 'bitshift':
5971
+ b = stack.pop();
5972
+ a = stack.pop();
5973
+ if (a > 0) {
5974
+ stack.push(a << b);
5975
+ } else {
5976
+ stack.push(a >> b);
5977
+ }
5978
+ break;
5979
+ case 'ceiling':
5980
+ a = stack.pop();
5981
+ stack.push(Math.ceil(a));
5982
+ break;
5983
+ case 'copy':
5984
+ a = stack.pop();
5985
+ stack.copy(a);
5986
+ break;
5987
+ case 'cos':
5988
+ a = stack.pop();
5989
+ stack.push(Math.cos(a));
5990
+ break;
5991
+ case 'cvi':
5992
+ a = stack.pop() | 0;
5993
+ stack.push(a);
5994
+ break;
5995
+ case 'cvr':
5996
+ // noop
5997
+ break;
5998
+ case 'div':
5999
+ b = stack.pop();
6000
+ a = stack.pop();
6001
+ stack.push(a / b);
6002
+ break;
6003
+ case 'dup':
6004
+ stack.copy(1);
6005
+ break;
6006
+ case 'eq':
6007
+ b = stack.pop();
6008
+ a = stack.pop();
6009
+ stack.push(a === b);
6010
+ break;
6011
+ case 'exch':
6012
+ stack.roll(2, 1);
6013
+ break;
6014
+ case 'exp':
6015
+ b = stack.pop();
6016
+ a = stack.pop();
6017
+ stack.push(Math.pow(a, b));
6018
+ break;
6019
+ case 'false':
6020
+ stack.push(false);
6021
+ break;
6022
+ case 'floor':
6023
+ a = stack.pop();
6024
+ stack.push(Math.floor(a));
6025
+ break;
6026
+ case 'ge':
6027
+ b = stack.pop();
6028
+ a = stack.pop();
6029
+ stack.push(a >= b);
6030
+ break;
6031
+ case 'gt':
6032
+ b = stack.pop();
6033
+ a = stack.pop();
6034
+ stack.push(a > b);
6035
+ break;
6036
+ case 'idiv':
6037
+ b = stack.pop();
6038
+ a = stack.pop();
6039
+ stack.push((a / b) | 0);
6040
+ break;
6041
+ case 'index':
6042
+ a = stack.pop();
6043
+ stack.index(a);
6044
+ break;
6045
+ case 'le':
6046
+ b = stack.pop();
6047
+ a = stack.pop();
6048
+ stack.push(a <= b);
6049
+ break;
6050
+ case 'ln':
6051
+ a = stack.pop();
6052
+ stack.push(Math.log(a));
6053
+ break;
6054
+ case 'log':
6055
+ a = stack.pop();
6056
+ stack.push(Math.log(a) / Math.LN10);
6057
+ break;
6058
+ case 'lt':
6059
+ b = stack.pop();
6060
+ a = stack.pop();
6061
+ stack.push(a < b);
6062
+ break;
6063
+ case 'mod':
6064
+ b = stack.pop();
6065
+ a = stack.pop();
6066
+ stack.push(a % b);
6067
+ break;
6068
+ case 'mul':
6069
+ b = stack.pop();
6070
+ a = stack.pop();
6071
+ stack.push(a * b);
6072
+ break;
6073
+ case 'ne':
6074
+ b = stack.pop();
6075
+ a = stack.pop();
6076
+ stack.push(a !== b);
6077
+ break;
6078
+ case 'neg':
6079
+ a = stack.pop();
6080
+ stack.push(-a);
6081
+ break;
6082
+ case 'not':
6083
+ a = stack.pop();
6084
+ if (isBool(a)) {
6085
+ stack.push(!a);
6086
+ } else {
6087
+ stack.push(~a);
6088
+ }
6089
+ break;
6090
+ case 'or':
6091
+ b = stack.pop();
6092
+ a = stack.pop();
6093
+ if (isBool(a) && isBool(b)) {
6094
+ stack.push(a || b);
6095
+ } else {
6096
+ stack.push(a | b);
6097
+ }
6098
+ break;
6099
+ case 'pop':
6100
+ stack.pop();
6101
+ break;
6102
+ case 'roll':
6103
+ b = stack.pop();
6104
+ a = stack.pop();
6105
+ stack.roll(a, b);
6106
+ break;
6107
+ case 'round':
6108
+ a = stack.pop();
6109
+ stack.push(Math.round(a));
6110
+ break;
6111
+ case 'sin':
6112
+ a = stack.pop();
6113
+ stack.push(Math.sin(a));
6114
+ break;
6115
+ case 'sqrt':
6116
+ a = stack.pop();
6117
+ stack.push(Math.sqrt(a));
6118
+ break;
6119
+ case 'sub':
6120
+ b = stack.pop();
6121
+ a = stack.pop();
6122
+ stack.push(a - b);
6123
+ break;
6124
+ case 'true':
6125
+ stack.push(true);
6126
+ break;
6127
+ case 'truncate':
6128
+ a = stack.pop();
6129
+ a = a < 0 ? Math.ceil(a) : Math.floor(a);
6130
+ stack.push(a);
6131
+ break;
6132
+ case 'xor':
6133
+ b = stack.pop();
6134
+ a = stack.pop();
6135
+ if (isBool(a) && isBool(b)) {
6136
+ stack.push(a !== b);
6137
+ } else {
6138
+ stack.push(a ^ b);
6139
+ }
6140
+ break;
6141
+ default:
6142
+ error('Unknown operator ' + operator);
6143
+ break;
6144
+ }
6145
+ }
6146
+ return stack.stack;
6147
+ }
6148
+ };
6149
+ return PostScriptEvaluator;
6150
+ })();
6151
+
6152
+ // Most of the PDFs functions consist of simple operations such as:
6153
+ // roll, exch, sub, cvr, pop, index, dup, mul, if, gt, add.
6154
+ //
6155
+ // We can compile most of such programs, and at the same moment, we can
6156
+ // optimize some expressions using basic math properties. Keeping track of
6157
+ // min/max values will allow us to avoid extra Math.min/Math.max calls.
6158
+ var PostScriptCompiler = (function PostScriptCompilerClosure() {
6159
+ function AstNode(type) {
6160
+ this.type = type;
6161
+ }
6162
+ AstNode.prototype.visit = function (visitor) {
6163
+ throw new Error('abstract method');
6164
+ };
6165
+
6166
+ function AstArgument(index, min, max) {
6167
+ AstNode.call(this, 'args');
6168
+ this.index = index;
6169
+ this.min = min;
6170
+ this.max = max;
6171
+ }
6172
+ AstArgument.prototype = Object.create(AstNode.prototype);
6173
+ AstArgument.prototype.visit = function (visitor) {
6174
+ visitor.visitArgument(this);
6175
+ };
6176
+
6177
+ function AstLiteral(number) {
6178
+ AstNode.call(this, 'literal');
6179
+ this.number = number;
6180
+ this.min = number;
6181
+ this.max = number;
6182
+ }
6183
+ AstLiteral.prototype = Object.create(AstNode.prototype);
6184
+ AstLiteral.prototype.visit = function (visitor) {
6185
+ visitor.visitLiteral(this);
6186
+ };
6187
+
6188
+ function AstBinaryOperation(op, arg1, arg2, min, max) {
6189
+ AstNode.call(this, 'binary');
6190
+ this.op = op;
6191
+ this.arg1 = arg1;
6192
+ this.arg2 = arg2;
6193
+ this.min = min;
6194
+ this.max = max;
6195
+ }
6196
+ AstBinaryOperation.prototype = Object.create(AstNode.prototype);
6197
+ AstBinaryOperation.prototype.visit = function (visitor) {
6198
+ visitor.visitBinaryOperation(this);
6199
+ };
6200
+
6201
+ function AstMin(arg, max) {
6202
+ AstNode.call(this, 'max');
6203
+ this.arg = arg;
6204
+ this.min = arg.min;
6205
+ this.max = max;
6206
+ }
6207
+ AstMin.prototype = Object.create(AstNode.prototype);
6208
+ AstMin.prototype.visit = function (visitor) {
6209
+ visitor.visitMin(this);
6210
+ };
6211
+
6212
+ function AstVariable(index, min, max) {
6213
+ AstNode.call(this, 'var');
6214
+ this.index = index;
6215
+ this.min = min;
6216
+ this.max = max;
6217
+ }
6218
+ AstVariable.prototype = Object.create(AstNode.prototype);
6219
+ AstVariable.prototype.visit = function (visitor) {
6220
+ visitor.visitVariable(this);
6221
+ };
6222
+
6223
+ function AstVariableDefinition(variable, arg) {
6224
+ AstNode.call(this, 'definition');
6225
+ this.variable = variable;
6226
+ this.arg = arg;
6227
+ }
6228
+ AstVariableDefinition.prototype = Object.create(AstNode.prototype);
6229
+ AstVariableDefinition.prototype.visit = function (visitor) {
6230
+ visitor.visitVariableDefinition(this);
6231
+ };
6232
+
6233
+ function ExpressionBuilderVisitor() {
6234
+ this.parts = [];
6235
+ }
6236
+ ExpressionBuilderVisitor.prototype = {
6237
+ visitArgument: function (arg) {
6238
+ this.parts.push('Math.max(', arg.min, ', Math.min(',
6239
+ arg.max, ', src[srcOffset + ', arg.index, ']))');
6240
+ },
6241
+ visitVariable: function (variable) {
6242
+ this.parts.push('v', variable.index);
6243
+ },
6244
+ visitLiteral: function (literal) {
6245
+ this.parts.push(literal.number);
6246
+ },
6247
+ visitBinaryOperation: function (operation) {
6248
+ this.parts.push('(');
6249
+ operation.arg1.visit(this);
6250
+ this.parts.push(' ', operation.op, ' ');
6251
+ operation.arg2.visit(this);
6252
+ this.parts.push(')');
6253
+ },
6254
+ visitVariableDefinition: function (definition) {
6255
+ this.parts.push('var ');
6256
+ definition.variable.visit(this);
6257
+ this.parts.push(' = ');
6258
+ definition.arg.visit(this);
6259
+ this.parts.push(';');
6260
+ },
6261
+ visitMin: function (max) {
6262
+ this.parts.push('Math.min(');
6263
+ max.arg.visit(this);
6264
+ this.parts.push(', ', max.max, ')');
6265
+ },
6266
+ toString: function () {
6267
+ return this.parts.join('');
6268
+ }
6269
+ };
6270
+
6271
+ function buildAddOperation(num1, num2) {
6272
+ if (num2.type === 'literal' && num2.number === 0) {
6273
+ // optimization: second operand is 0
6274
+ return num1;
6275
+ }
6276
+ if (num1.type === 'literal' && num1.number === 0) {
6277
+ // optimization: first operand is 0
6278
+ return num2;
6279
+ }
6280
+ if (num2.type === 'literal' && num1.type === 'literal') {
6281
+ // optimization: operands operand are literals
6282
+ return new AstLiteral(num1.number + num2.number);
6283
+ }
6284
+ return new AstBinaryOperation('+', num1, num2,
6285
+ num1.min + num2.min, num1.max + num2.max);
6286
+ }
6287
+
6288
+ function buildMulOperation(num1, num2) {
6289
+ if (num2.type === 'literal') {
6290
+ // optimization: second operands is a literal...
6291
+ if (num2.number === 0) {
6292
+ return new AstLiteral(0); // and it's 0
6293
+ } else if (num2.number === 1) {
6294
+ return num1; // and it's 1
6295
+ } else if (num1.type === 'literal') {
6296
+ // ... and first operands is a literal too
6297
+ return new AstLiteral(num1.number * num2.number);
6298
+ }
6299
+ }
6300
+ if (num1.type === 'literal') {
6301
+ // optimization: first operands is a literal...
6302
+ if (num1.number === 0) {
6303
+ return new AstLiteral(0); // and it's 0
6304
+ } else if (num1.number === 1) {
6305
+ return num2; // and it's 1
6306
+ }
6307
+ }
6308
+ var min = Math.min(num1.min * num2.min, num1.min * num2.max,
6309
+ num1.max * num2.min, num1.max * num2.max);
6310
+ var max = Math.max(num1.min * num2.min, num1.min * num2.max,
6311
+ num1.max * num2.min, num1.max * num2.max);
6312
+ return new AstBinaryOperation('*', num1, num2, min, max);
6313
+ }
6314
+
6315
+ function buildSubOperation(num1, num2) {
6316
+ if (num2.type === 'literal') {
6317
+ // optimization: second operands is a literal...
6318
+ if (num2.number === 0) {
6319
+ return num1; // ... and it's 0
6320
+ } else if (num1.type === 'literal') {
6321
+ // ... and first operands is a literal too
6322
+ return new AstLiteral(num1.number - num2.number);
6323
+ }
6324
+ }
6325
+ if (num2.type === 'binary' && num2.op === '-' &&
6326
+ num1.type === 'literal' && num1.number === 1 &&
6327
+ num2.arg1.type === 'literal' && num2.arg1.number === 1) {
6328
+ // optimization for case: 1 - (1 - x)
6329
+ return num2.arg2;
6330
+ }
6331
+ return new AstBinaryOperation('-', num1, num2,
6332
+ num1.min - num2.max, num1.max - num2.min);
6333
+ }
6334
+
6335
+ function buildMinOperation(num1, max) {
6336
+ if (num1.min >= max) {
6337
+ // optimization: num1 min value is not less than required max
6338
+ return new AstLiteral(max); // just returning max
6339
+ } else if (num1.max <= max) {
6340
+ // optimization: num1 max value is not greater than required max
6341
+ return num1; // just returning an argument
6342
+ }
6343
+ return new AstMin(num1, max);
6344
+ }
6345
+
6346
+ function PostScriptCompiler() {}
6347
+ PostScriptCompiler.prototype = {
6348
+ compile: function PostScriptCompiler_compile(code, domain, range) {
6349
+ var stack = [];
6350
+ var i, ii;
6351
+ var instructions = [];
6352
+ var inputSize = domain.length >> 1, outputSize = range.length >> 1;
6353
+ var lastRegister = 0;
6354
+ var n, j, min, max;
6355
+ var num1, num2, ast1, ast2, tmpVar, item;
6356
+ for (i = 0; i < inputSize; i++) {
6357
+ stack.push(new AstArgument(i, domain[i * 2], domain[i * 2 + 1]));
6358
+ }
6359
+
6360
+ for (i = 0, ii = code.length; i < ii; i++) {
6361
+ item = code[i];
6362
+ if (typeof item === 'number') {
6363
+ stack.push(new AstLiteral(item));
6364
+ continue;
6365
+ }
6366
+
6367
+ switch (item) {
6368
+ case 'add':
6369
+ if (stack.length < 2) {
6370
+ return null;
6371
+ }
6372
+ num2 = stack.pop();
6373
+ num1 = stack.pop();
6374
+ stack.push(buildAddOperation(num1, num2));
6375
+ break;
6376
+ case 'cvr':
6377
+ if (stack.length < 1) {
6378
+ return null;
6379
+ }
6380
+ break;
6381
+ case 'mul':
6382
+ if (stack.length < 2) {
6383
+ return null;
6384
+ }
6385
+ num2 = stack.pop();
6386
+ num1 = stack.pop();
6387
+ stack.push(buildMulOperation(num1, num2));
6388
+ break;
6389
+ case 'sub':
6390
+ if (stack.length < 2) {
6391
+ return null;
6392
+ }
6393
+ num2 = stack.pop();
6394
+ num1 = stack.pop();
6395
+ stack.push(buildSubOperation(num1, num2));
6396
+ break;
6397
+ case 'exch':
6398
+ if (stack.length < 2) {
6399
+ return null;
6400
+ }
6401
+ ast1 = stack.pop(); ast2 = stack.pop();
6402
+ stack.push(ast1, ast2);
6403
+ break;
6404
+ case 'pop':
6405
+ if (stack.length < 1) {
6406
+ return null;
6407
+ }
6408
+ stack.pop();
6409
+ break;
6410
+ case 'index':
6411
+ if (stack.length < 1) {
6412
+ return null;
6413
+ }
6414
+ num1 = stack.pop();
6415
+ if (num1.type !== 'literal') {
6416
+ return null;
6417
+ }
6418
+ n = num1.number;
6419
+ if (n < 0 || (n|0) !== n || stack.length < n) {
6420
+ return null;
6421
+ }
6422
+ ast1 = stack[stack.length - n - 1];
6423
+ if (ast1.type === 'literal' || ast1.type === 'var') {
6424
+ stack.push(ast1);
6425
+ break;
6426
+ }
6427
+ tmpVar = new AstVariable(lastRegister++, ast1.min, ast1.max);
6428
+ stack[stack.length - n - 1] = tmpVar;
6429
+ stack.push(tmpVar);
6430
+ instructions.push(new AstVariableDefinition(tmpVar, ast1));
6431
+ break;
6432
+ case 'dup':
6433
+ if (stack.length < 1) {
6434
+ return null;
6435
+ }
6436
+ if (typeof code[i + 1] === 'number' && code[i + 2] === 'gt' &&
6437
+ code[i + 3] === i + 7 && code[i + 4] === 'jz' &&
6438
+ code[i + 5] === 'pop' && code[i + 6] === code[i + 1]) {
6439
+ // special case of the commands sequence for the min operation
6440
+ num1 = stack.pop();
6441
+ stack.push(buildMinOperation(num1, code[i + 1]));
6442
+ i += 6;
6443
+ break;
6444
+ }
6445
+ ast1 = stack[stack.length - 1];
6446
+ if (ast1.type === 'literal' || ast1.type === 'var') {
6447
+ // we don't have to save into intermediate variable a literal or
6448
+ // variable.
6449
+ stack.push(ast1);
6450
+ break;
6451
+ }
6452
+ tmpVar = new AstVariable(lastRegister++, ast1.min, ast1.max);
6453
+ stack[stack.length - 1] = tmpVar;
6454
+ stack.push(tmpVar);
6455
+ instructions.push(new AstVariableDefinition(tmpVar, ast1));
6456
+ break;
6457
+ case 'roll':
6458
+ if (stack.length < 2) {
6459
+ return null;
6460
+ }
6461
+ num2 = stack.pop();
6462
+ num1 = stack.pop();
6463
+ if (num2.type !== 'literal' || num1.type !== 'literal') {
6464
+ // both roll operands must be numbers
6465
+ return null;
6466
+ }
6467
+ j = num2.number;
6468
+ n = num1.number;
6469
+ if (n <= 0 || (n|0) !== n || (j|0) !== j || stack.length < n) {
6470
+ // ... and integers
6471
+ return null;
6472
+ }
6473
+ j = ((j % n) + n) % n;
6474
+ if (j === 0) {
6475
+ break; // just skipping -- there are nothing to rotate
6476
+ }
6477
+ Array.prototype.push.apply(stack,
6478
+ stack.splice(stack.length - n, n - j));
6479
+ break;
6480
+ default:
6481
+ return null; // unsupported operator
6482
+ }
6483
+ }
6484
+
6485
+ if (stack.length !== outputSize) {
6486
+ return null;
6487
+ }
6488
+
6489
+ var result = [];
6490
+ instructions.forEach(function (instruction) {
6491
+ var statementBuilder = new ExpressionBuilderVisitor();
6492
+ instruction.visit(statementBuilder);
6493
+ result.push(statementBuilder.toString());
6494
+ });
6495
+ stack.forEach(function (expr, i) {
6496
+ var statementBuilder = new ExpressionBuilderVisitor();
6497
+ expr.visit(statementBuilder);
6498
+ var min = range[i * 2], max = range[i * 2 + 1];
6499
+ var out = [statementBuilder.toString()];
6500
+ if (min > expr.min) {
6501
+ out.unshift('Math.max(', min, ', ');
6502
+ out.push(')');
6503
+ }
6504
+ if (max < expr.max) {
6505
+ out.unshift('Math.min(', max, ', ');
6506
+ out.push(')');
6507
+ }
6508
+ out.unshift('dest[destOffset + ', i, '] = ');
6509
+ out.push(';');
6510
+ result.push(out.join(''));
6511
+ });
6512
+ return result.join('\n');
6513
+ }
6514
+ };
6515
+
6516
+ return PostScriptCompiler;
6517
+ })();
6518
+
6519
+
6520
+ var ColorSpace = (function ColorSpaceClosure() {
6521
+ // Constructor should define this.numComps, this.defaultColor, this.name
6522
+ function ColorSpace() {
6523
+ error('should not call ColorSpace constructor');
6524
+ }
6525
+
6526
+ ColorSpace.prototype = {
6527
+ /**
6528
+ * Converts the color value to the RGB color. The color components are
6529
+ * located in the src array starting from the srcOffset. Returns the array
6530
+ * of the rgb components, each value ranging from [0,255].
6531
+ */
6532
+ getRgb: function ColorSpace_getRgb(src, srcOffset) {
6533
+ var rgb = new Uint8Array(3);
6534
+ this.getRgbItem(src, srcOffset, rgb, 0);
6535
+ return rgb;
6536
+ },
6537
+ /**
6538
+ * Converts the color value to the RGB color, similar to the getRgb method.
6539
+ * The result placed into the dest array starting from the destOffset.
6540
+ */
6541
+ getRgbItem: function ColorSpace_getRgbItem(src, srcOffset,
6542
+ dest, destOffset) {
6543
+ error('Should not call ColorSpace.getRgbItem');
6544
+ },
6545
+ /**
6546
+ * Converts the specified number of the color values to the RGB colors.
6547
+ * The colors are located in the src array starting from the srcOffset.
6548
+ * The result is placed into the dest array starting from the destOffset.
6549
+ * The src array items shall be in [0,2^bits) range, the dest array items
6550
+ * will be in [0,255] range. alpha01 indicates how many alpha components
6551
+ * there are in the dest array; it will be either 0 (RGB array) or 1 (RGBA
6552
+ * array).
6553
+ */
6554
+ getRgbBuffer: function ColorSpace_getRgbBuffer(src, srcOffset, count,
6555
+ dest, destOffset, bits,
6556
+ alpha01) {
6557
+ error('Should not call ColorSpace.getRgbBuffer');
6558
+ },
6559
+ /**
6560
+ * Determines the number of bytes required to store the result of the
6561
+ * conversion done by the getRgbBuffer method. As in getRgbBuffer,
6562
+ * |alpha01| is either 0 (RGB output) or 1 (RGBA output).
6563
+ */
6564
+ getOutputLength: function ColorSpace_getOutputLength(inputLength,
6565
+ alpha01) {
6566
+ error('Should not call ColorSpace.getOutputLength');
6567
+ },
6568
+ /**
6569
+ * Returns true if source data will be equal the result/output data.
6570
+ */
6571
+ isPassthrough: function ColorSpace_isPassthrough(bits) {
6572
+ return false;
6573
+ },
6574
+ /**
6575
+ * Fills in the RGB colors in the destination buffer. alpha01 indicates
6576
+ * how many alpha components there are in the dest array; it will be either
6577
+ * 0 (RGB array) or 1 (RGBA array).
6578
+ */
6579
+ fillRgb: function ColorSpace_fillRgb(dest, originalWidth,
6580
+ originalHeight, width, height,
6581
+ actualHeight, bpc, comps, alpha01) {
6582
+ var count = originalWidth * originalHeight;
6583
+ var rgbBuf = null;
6584
+ var numComponentColors = 1 << bpc;
6585
+ var needsResizing = originalHeight !== height || originalWidth !== width;
6586
+ var i, ii;
6587
+
6588
+ if (this.isPassthrough(bpc)) {
6589
+ rgbBuf = comps;
6590
+ } else if (this.numComps === 1 && count > numComponentColors &&
6591
+ this.name !== 'DeviceGray' && this.name !== 'DeviceRGB') {
6592
+ // Optimization: create a color map when there is just one component and
6593
+ // we are converting more colors than the size of the color map. We
6594
+ // don't build the map if the colorspace is gray or rgb since those
6595
+ // methods are faster than building a map. This mainly offers big speed
6596
+ // ups for indexed and alternate colorspaces.
6597
+ //
6598
+ // TODO it may be worth while to cache the color map. While running
6599
+ // testing I never hit a cache so I will leave that out for now (perhaps
6600
+ // we are reparsing colorspaces too much?).
6601
+ var allColors = bpc <= 8 ? new Uint8Array(numComponentColors) :
6602
+ new Uint16Array(numComponentColors);
6603
+ var key;
6604
+ for (i = 0; i < numComponentColors; i++) {
6605
+ allColors[i] = i;
6606
+ }
6607
+ var colorMap = new Uint8Array(numComponentColors * 3);
6608
+ this.getRgbBuffer(allColors, 0, numComponentColors, colorMap, 0, bpc,
6609
+ /* alpha01 = */ 0);
6610
+
6611
+ var destPos, rgbPos;
6612
+ if (!needsResizing) {
6613
+ // Fill in the RGB values directly into |dest|.
6614
+ destPos = 0;
6615
+ for (i = 0; i < count; ++i) {
6616
+ key = comps[i] * 3;
6617
+ dest[destPos++] = colorMap[key];
6618
+ dest[destPos++] = colorMap[key + 1];
6619
+ dest[destPos++] = colorMap[key + 2];
6620
+ destPos += alpha01;
6621
+ }
6622
+ } else {
6623
+ rgbBuf = new Uint8Array(count * 3);
6624
+ rgbPos = 0;
6625
+ for (i = 0; i < count; ++i) {
6626
+ key = comps[i] * 3;
6627
+ rgbBuf[rgbPos++] = colorMap[key];
6628
+ rgbBuf[rgbPos++] = colorMap[key + 1];
6629
+ rgbBuf[rgbPos++] = colorMap[key + 2];
6630
+ }
6631
+ }
6632
+ } else {
6633
+ if (!needsResizing) {
6634
+ // Fill in the RGB values directly into |dest|.
6635
+ this.getRgbBuffer(comps, 0, width * actualHeight, dest, 0, bpc,
6636
+ alpha01);
6637
+ } else {
6638
+ rgbBuf = new Uint8Array(count * 3);
6639
+ this.getRgbBuffer(comps, 0, count, rgbBuf, 0, bpc,
6640
+ /* alpha01 = */ 0);
6641
+ }
6642
+ }
6643
+
6644
+ if (rgbBuf) {
6645
+ if (needsResizing) {
6646
+ PDFImage.resize(rgbBuf, bpc, 3, originalWidth, originalHeight, width,
6647
+ height, dest, alpha01);
6648
+ } else {
6649
+ rgbPos = 0;
6650
+ destPos = 0;
6651
+ for (i = 0, ii = width * actualHeight; i < ii; i++) {
6652
+ dest[destPos++] = rgbBuf[rgbPos++];
6653
+ dest[destPos++] = rgbBuf[rgbPos++];
6654
+ dest[destPos++] = rgbBuf[rgbPos++];
6655
+ destPos += alpha01;
6656
+ }
6657
+ }
6658
+ }
6659
+ },
6660
+ /**
6661
+ * True if the colorspace has components in the default range of [0, 1].
6662
+ * This should be true for all colorspaces except for lab color spaces
6663
+ * which are [0,100], [-128, 127], [-128, 127].
6664
+ */
6665
+ usesZeroToOneRange: true
6666
+ };
6667
+
6668
+ ColorSpace.parse = function ColorSpace_parse(cs, xref, res) {
6669
+ var IR = ColorSpace.parseToIR(cs, xref, res);
6670
+ if (IR instanceof AlternateCS) {
6671
+ return IR;
6672
+ }
6673
+ return ColorSpace.fromIR(IR);
6674
+ };
6675
+
6676
+ ColorSpace.fromIR = function ColorSpace_fromIR(IR) {
6677
+ var name = isArray(IR) ? IR[0] : IR;
6678
+ var whitePoint, blackPoint, gamma;
6679
+
6680
+ switch (name) {
6681
+ case 'DeviceGrayCS':
6682
+ return this.singletons.gray;
6683
+ case 'DeviceRgbCS':
6684
+ return this.singletons.rgb;
6685
+ case 'DeviceCmykCS':
6686
+ return this.singletons.cmyk;
6687
+ case 'CalGrayCS':
6688
+ whitePoint = IR[1].WhitePoint;
6689
+ blackPoint = IR[1].BlackPoint;
6690
+ gamma = IR[1].Gamma;
6691
+ return new CalGrayCS(whitePoint, blackPoint, gamma);
6692
+ case 'CalRGBCS':
6693
+ whitePoint = IR[1].WhitePoint;
6694
+ blackPoint = IR[1].BlackPoint;
6695
+ gamma = IR[1].Gamma;
6696
+ var matrix = IR[1].Matrix;
6697
+ return new CalRGBCS(whitePoint, blackPoint, gamma, matrix);
6698
+ case 'PatternCS':
6699
+ var basePatternCS = IR[1];
6700
+ if (basePatternCS) {
6701
+ basePatternCS = ColorSpace.fromIR(basePatternCS);
6702
+ }
6703
+ return new PatternCS(basePatternCS);
6704
+ case 'IndexedCS':
6705
+ var baseIndexedCS = IR[1];
6706
+ var hiVal = IR[2];
6707
+ var lookup = IR[3];
6708
+ return new IndexedCS(ColorSpace.fromIR(baseIndexedCS), hiVal, lookup);
6709
+ case 'AlternateCS':
6710
+ var numComps = IR[1];
6711
+ var alt = IR[2];
6712
+ var tintFnIR = IR[3];
6713
+
6714
+ return new AlternateCS(numComps, ColorSpace.fromIR(alt),
6715
+ PDFFunction.fromIR(tintFnIR));
6716
+ case 'LabCS':
6717
+ whitePoint = IR[1].WhitePoint;
6718
+ blackPoint = IR[1].BlackPoint;
6719
+ var range = IR[1].Range;
6720
+ return new LabCS(whitePoint, blackPoint, range);
6721
+ default:
6722
+ error('Unknown name ' + name);
6723
+ }
6724
+ return null;
6725
+ };
6726
+
6727
+ ColorSpace.parseToIR = function ColorSpace_parseToIR(cs, xref, res) {
6728
+ if (isName(cs)) {
6729
+ var colorSpaces = res.get('ColorSpace');
6730
+ if (isDict(colorSpaces)) {
6731
+ var refcs = colorSpaces.get(cs.name);
6732
+ if (refcs) {
6733
+ cs = refcs;
6734
+ }
6735
+ }
6736
+ }
6737
+
6738
+ cs = xref.fetchIfRef(cs);
6739
+ var mode;
6740
+
6741
+ if (isName(cs)) {
6742
+ mode = cs.name;
6743
+ this.mode = mode;
6744
+
6745
+ switch (mode) {
6746
+ case 'DeviceGray':
6747
+ case 'G':
6748
+ return 'DeviceGrayCS';
6749
+ case 'DeviceRGB':
6750
+ case 'RGB':
6751
+ return 'DeviceRgbCS';
6752
+ case 'DeviceCMYK':
6753
+ case 'CMYK':
6754
+ return 'DeviceCmykCS';
6755
+ case 'Pattern':
6756
+ return ['PatternCS', null];
6757
+ default:
6758
+ error('unrecognized colorspace ' + mode);
6759
+ }
6760
+ } else if (isArray(cs)) {
6761
+ mode = cs[0].name;
6762
+ this.mode = mode;
6763
+ var numComps, params;
6764
+
6765
+ switch (mode) {
6766
+ case 'DeviceGray':
6767
+ case 'G':
6768
+ return 'DeviceGrayCS';
6769
+ case 'DeviceRGB':
6770
+ case 'RGB':
6771
+ return 'DeviceRgbCS';
6772
+ case 'DeviceCMYK':
6773
+ case 'CMYK':
6774
+ return 'DeviceCmykCS';
6775
+ case 'CalGray':
6776
+ params = xref.fetchIfRef(cs[1]).getAll();
6777
+ return ['CalGrayCS', params];
6778
+ case 'CalRGB':
6779
+ params = xref.fetchIfRef(cs[1]).getAll();
6780
+ return ['CalRGBCS', params];
6781
+ case 'ICCBased':
6782
+ var stream = xref.fetchIfRef(cs[1]);
6783
+ var dict = stream.dict;
6784
+ numComps = dict.get('N');
6785
+ if (numComps === 1) {
6786
+ return 'DeviceGrayCS';
6787
+ } else if (numComps === 3) {
6788
+ return 'DeviceRgbCS';
6789
+ } else if (numComps === 4) {
6790
+ return 'DeviceCmykCS';
6791
+ }
6792
+ break;
6793
+ case 'Pattern':
6794
+ var basePatternCS = cs[1];
6795
+ if (basePatternCS) {
6796
+ basePatternCS = ColorSpace.parseToIR(basePatternCS, xref, res);
6797
+ }
6798
+ return ['PatternCS', basePatternCS];
6799
+ case 'Indexed':
6800
+ case 'I':
6801
+ var baseIndexedCS = ColorSpace.parseToIR(cs[1], xref, res);
6802
+ var hiVal = cs[2] + 1;
6803
+ var lookup = xref.fetchIfRef(cs[3]);
6804
+ if (isStream(lookup)) {
6805
+ lookup = lookup.getBytes();
6806
+ }
6807
+ return ['IndexedCS', baseIndexedCS, hiVal, lookup];
6808
+ case 'Separation':
6809
+ case 'DeviceN':
6810
+ var name = cs[1];
6811
+ numComps = 1;
6812
+ if (isName(name)) {
6813
+ numComps = 1;
6814
+ } else if (isArray(name)) {
6815
+ numComps = name.length;
6816
+ }
6817
+ var alt = ColorSpace.parseToIR(cs[2], xref, res);
6818
+ var tintFnIR = PDFFunction.getIR(xref, xref.fetchIfRef(cs[3]));
6819
+ return ['AlternateCS', numComps, alt, tintFnIR];
6820
+ case 'Lab':
6821
+ params = cs[1].getAll();
6822
+ return ['LabCS', params];
6823
+ default:
6824
+ error('unimplemented color space object "' + mode + '"');
6825
+ }
6826
+ } else {
6827
+ error('unrecognized color space object: "' + cs + '"');
6828
+ }
6829
+ return null;
6830
+ };
6831
+ /**
6832
+ * Checks if a decode map matches the default decode map for a color space.
6833
+ * This handles the general decode maps where there are two values per
6834
+ * component. e.g. [0, 1, 0, 1, 0, 1] for a RGB color.
6835
+ * This does not handle Lab, Indexed, or Pattern decode maps since they are
6836
+ * slightly different.
6837
+ * @param {Array} decode Decode map (usually from an image).
6838
+ * @param {Number} n Number of components the color space has.
6839
+ */
6840
+ ColorSpace.isDefaultDecode = function ColorSpace_isDefaultDecode(decode, n) {
6841
+ if (!decode) {
6842
+ return true;
6843
+ }
6844
+
6845
+ if (n * 2 !== decode.length) {
6846
+ warn('The decode map is not the correct length');
6847
+ return true;
6848
+ }
6849
+ for (var i = 0, ii = decode.length; i < ii; i += 2) {
6850
+ if (decode[i] !== 0 || decode[i + 1] !== 1) {
6851
+ return false;
6852
+ }
6853
+ }
6854
+ return true;
6855
+ };
6856
+
6857
+ ColorSpace.singletons = {
6858
+ get gray() {
6859
+ return shadow(this, 'gray', new DeviceGrayCS());
6860
+ },
6861
+ get rgb() {
6862
+ return shadow(this, 'rgb', new DeviceRgbCS());
6863
+ },
6864
+ get cmyk() {
6865
+ return shadow(this, 'cmyk', new DeviceCmykCS());
6866
+ }
6867
+ };
6868
+
6869
+ return ColorSpace;
6870
+ })();
6871
+
6872
+ /**
6873
+ * Alternate color space handles both Separation and DeviceN color spaces. A
6874
+ * Separation color space is actually just a DeviceN with one color component.
6875
+ * Both color spaces use a tinting function to convert colors to a base color
6876
+ * space.
6877
+ */
6878
+ var AlternateCS = (function AlternateCSClosure() {
6879
+ function AlternateCS(numComps, base, tintFn) {
6880
+ this.name = 'Alternate';
6881
+ this.numComps = numComps;
6882
+ this.defaultColor = new Float32Array(numComps);
6883
+ for (var i = 0; i < numComps; ++i) {
6884
+ this.defaultColor[i] = 1;
6885
+ }
6886
+ this.base = base;
6887
+ this.tintFn = tintFn;
6888
+ this.tmpBuf = new Float32Array(base.numComps);
6889
+ }
6890
+
6891
+ AlternateCS.prototype = {
6892
+ getRgb: ColorSpace.prototype.getRgb,
6893
+ getRgbItem: function AlternateCS_getRgbItem(src, srcOffset,
6894
+ dest, destOffset) {
6895
+ var tmpBuf = this.tmpBuf;
6896
+ this.tintFn(src, srcOffset, tmpBuf, 0);
6897
+ this.base.getRgbItem(tmpBuf, 0, dest, destOffset);
6898
+ },
6899
+ getRgbBuffer: function AlternateCS_getRgbBuffer(src, srcOffset, count,
6900
+ dest, destOffset, bits,
6901
+ alpha01) {
6902
+ var tintFn = this.tintFn;
6903
+ var base = this.base;
6904
+ var scale = 1 / ((1 << bits) - 1);
6905
+ var baseNumComps = base.numComps;
6906
+ var usesZeroToOneRange = base.usesZeroToOneRange;
6907
+ var isPassthrough = (base.isPassthrough(8) || !usesZeroToOneRange) &&
6908
+ alpha01 === 0;
6909
+ var pos = isPassthrough ? destOffset : 0;
6910
+ var baseBuf = isPassthrough ? dest : new Uint8Array(baseNumComps * count);
6911
+ var numComps = this.numComps;
6912
+
6913
+ var scaled = new Float32Array(numComps);
6914
+ var tinted = new Float32Array(baseNumComps);
6915
+ var i, j;
6916
+ if (usesZeroToOneRange) {
6917
+ for (i = 0; i < count; i++) {
6918
+ for (j = 0; j < numComps; j++) {
6919
+ scaled[j] = src[srcOffset++] * scale;
6920
+ }
6921
+ tintFn(scaled, 0, tinted, 0);
6922
+ for (j = 0; j < baseNumComps; j++) {
6923
+ baseBuf[pos++] = tinted[j] * 255;
6924
+ }
6925
+ }
6926
+ } else {
6927
+ for (i = 0; i < count; i++) {
6928
+ for (j = 0; j < numComps; j++) {
6929
+ scaled[j] = src[srcOffset++] * scale;
6930
+ }
6931
+ tintFn(scaled, 0, tinted, 0);
6932
+ base.getRgbItem(tinted, 0, baseBuf, pos);
6933
+ pos += baseNumComps;
6934
+ }
6935
+ }
6936
+ if (!isPassthrough) {
6937
+ base.getRgbBuffer(baseBuf, 0, count, dest, destOffset, 8, alpha01);
6938
+ }
6939
+ },
6940
+ getOutputLength: function AlternateCS_getOutputLength(inputLength,
6941
+ alpha01) {
6942
+ return this.base.getOutputLength(inputLength *
6943
+ this.base.numComps / this.numComps,
6944
+ alpha01);
6945
+ },
6946
+ isPassthrough: ColorSpace.prototype.isPassthrough,
6947
+ fillRgb: ColorSpace.prototype.fillRgb,
6948
+ isDefaultDecode: function AlternateCS_isDefaultDecode(decodeMap) {
6949
+ return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
6950
+ },
6951
+ usesZeroToOneRange: true
6952
+ };
6953
+
6954
+ return AlternateCS;
6955
+ })();
6956
+
6957
+ var PatternCS = (function PatternCSClosure() {
6958
+ function PatternCS(baseCS) {
6959
+ this.name = 'Pattern';
6960
+ this.base = baseCS;
6961
+ }
6962
+ PatternCS.prototype = {};
6963
+
6964
+ return PatternCS;
6965
+ })();
6966
+
6967
+ var IndexedCS = (function IndexedCSClosure() {
6968
+ function IndexedCS(base, highVal, lookup) {
6969
+ this.name = 'Indexed';
6970
+ this.numComps = 1;
6971
+ this.defaultColor = new Uint8Array([0]);
6972
+ this.base = base;
6973
+ this.highVal = highVal;
6974
+
6975
+ var baseNumComps = base.numComps;
6976
+ var length = baseNumComps * highVal;
6977
+ var lookupArray;
6978
+
6979
+ if (isStream(lookup)) {
6980
+ lookupArray = new Uint8Array(length);
6981
+ var bytes = lookup.getBytes(length);
6982
+ lookupArray.set(bytes);
6983
+ } else if (isString(lookup)) {
6984
+ lookupArray = new Uint8Array(length);
6985
+ for (var i = 0; i < length; ++i) {
6986
+ lookupArray[i] = lookup.charCodeAt(i);
6987
+ }
6988
+ } else if (lookup instanceof Uint8Array || lookup instanceof Array) {
6989
+ lookupArray = lookup;
6990
+ } else {
6991
+ error('Unrecognized lookup table: ' + lookup);
6992
+ }
6993
+ this.lookup = lookupArray;
6994
+ }
6995
+
6996
+ IndexedCS.prototype = {
6997
+ getRgb: ColorSpace.prototype.getRgb,
6998
+ getRgbItem: function IndexedCS_getRgbItem(src, srcOffset,
6999
+ dest, destOffset) {
7000
+ var numComps = this.base.numComps;
7001
+ var start = src[srcOffset] * numComps;
7002
+ this.base.getRgbItem(this.lookup, start, dest, destOffset);
7003
+ },
7004
+ getRgbBuffer: function IndexedCS_getRgbBuffer(src, srcOffset, count,
7005
+ dest, destOffset, bits,
7006
+ alpha01) {
7007
+ var base = this.base;
7008
+ var numComps = base.numComps;
7009
+ var outputDelta = base.getOutputLength(numComps, alpha01);
7010
+ var lookup = this.lookup;
7011
+
7012
+ for (var i = 0; i < count; ++i) {
7013
+ var lookupPos = src[srcOffset++] * numComps;
7014
+ base.getRgbBuffer(lookup, lookupPos, 1, dest, destOffset, 8, alpha01);
7015
+ destOffset += outputDelta;
7016
+ }
7017
+ },
7018
+ getOutputLength: function IndexedCS_getOutputLength(inputLength, alpha01) {
7019
+ return this.base.getOutputLength(inputLength * this.base.numComps,
7020
+ alpha01);
7021
+ },
7022
+ isPassthrough: ColorSpace.prototype.isPassthrough,
7023
+ fillRgb: ColorSpace.prototype.fillRgb,
7024
+ isDefaultDecode: function IndexedCS_isDefaultDecode(decodeMap) {
7025
+ // indexed color maps shouldn't be changed
7026
+ return true;
7027
+ },
7028
+ usesZeroToOneRange: true
7029
+ };
7030
+ return IndexedCS;
7031
+ })();
7032
+
7033
+ var DeviceGrayCS = (function DeviceGrayCSClosure() {
7034
+ function DeviceGrayCS() {
7035
+ this.name = 'DeviceGray';
7036
+ this.numComps = 1;
7037
+ this.defaultColor = new Float32Array([0]);
7038
+ }
7039
+
7040
+ DeviceGrayCS.prototype = {
7041
+ getRgb: ColorSpace.prototype.getRgb,
7042
+ getRgbItem: function DeviceGrayCS_getRgbItem(src, srcOffset,
7043
+ dest, destOffset) {
7044
+ var c = (src[srcOffset] * 255) | 0;
7045
+ c = c < 0 ? 0 : c > 255 ? 255 : c;
7046
+ dest[destOffset] = dest[destOffset + 1] = dest[destOffset + 2] = c;
7047
+ },
7048
+ getRgbBuffer: function DeviceGrayCS_getRgbBuffer(src, srcOffset, count,
7049
+ dest, destOffset, bits,
7050
+ alpha01) {
7051
+ var scale = 255 / ((1 << bits) - 1);
7052
+ var j = srcOffset, q = destOffset;
7053
+ for (var i = 0; i < count; ++i) {
7054
+ var c = (scale * src[j++]) | 0;
7055
+ dest[q++] = c;
7056
+ dest[q++] = c;
7057
+ dest[q++] = c;
7058
+ q += alpha01;
7059
+ }
7060
+ },
7061
+ getOutputLength: function DeviceGrayCS_getOutputLength(inputLength,
7062
+ alpha01) {
7063
+ return inputLength * (3 + alpha01);
7064
+ },
7065
+ isPassthrough: ColorSpace.prototype.isPassthrough,
7066
+ fillRgb: ColorSpace.prototype.fillRgb,
7067
+ isDefaultDecode: function DeviceGrayCS_isDefaultDecode(decodeMap) {
7068
+ return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
7069
+ },
7070
+ usesZeroToOneRange: true
7071
+ };
7072
+ return DeviceGrayCS;
7073
+ })();
7074
+
7075
+ var DeviceRgbCS = (function DeviceRgbCSClosure() {
7076
+ function DeviceRgbCS() {
7077
+ this.name = 'DeviceRGB';
7078
+ this.numComps = 3;
7079
+ this.defaultColor = new Float32Array([0, 0, 0]);
7080
+ }
7081
+ DeviceRgbCS.prototype = {
7082
+ getRgb: ColorSpace.prototype.getRgb,
7083
+ getRgbItem: function DeviceRgbCS_getRgbItem(src, srcOffset,
7084
+ dest, destOffset) {
7085
+ var r = (src[srcOffset] * 255) | 0;
7086
+ var g = (src[srcOffset + 1] * 255) | 0;
7087
+ var b = (src[srcOffset + 2] * 255) | 0;
7088
+ dest[destOffset] = r < 0 ? 0 : r > 255 ? 255 : r;
7089
+ dest[destOffset + 1] = g < 0 ? 0 : g > 255 ? 255 : g;
7090
+ dest[destOffset + 2] = b < 0 ? 0 : b > 255 ? 255 : b;
7091
+ },
7092
+ getRgbBuffer: function DeviceRgbCS_getRgbBuffer(src, srcOffset, count,
7093
+ dest, destOffset, bits,
7094
+ alpha01) {
7095
+ if (bits === 8 && alpha01 === 0) {
7096
+ dest.set(src.subarray(srcOffset, srcOffset + count * 3), destOffset);
7097
+ return;
7098
+ }
7099
+ var scale = 255 / ((1 << bits) - 1);
7100
+ var j = srcOffset, q = destOffset;
7101
+ for (var i = 0; i < count; ++i) {
7102
+ dest[q++] = (scale * src[j++]) | 0;
7103
+ dest[q++] = (scale * src[j++]) | 0;
7104
+ dest[q++] = (scale * src[j++]) | 0;
7105
+ q += alpha01;
7106
+ }
7107
+ },
7108
+ getOutputLength: function DeviceRgbCS_getOutputLength(inputLength,
7109
+ alpha01) {
7110
+ return (inputLength * (3 + alpha01) / 3) | 0;
7111
+ },
7112
+ isPassthrough: function DeviceRgbCS_isPassthrough(bits) {
7113
+ return bits === 8;
7114
+ },
7115
+ fillRgb: ColorSpace.prototype.fillRgb,
7116
+ isDefaultDecode: function DeviceRgbCS_isDefaultDecode(decodeMap) {
7117
+ return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
7118
+ },
7119
+ usesZeroToOneRange: true
7120
+ };
7121
+ return DeviceRgbCS;
7122
+ })();
7123
+
7124
+ var DeviceCmykCS = (function DeviceCmykCSClosure() {
7125
+ // The coefficients below was found using numerical analysis: the method of
7126
+ // steepest descent for the sum((f_i - color_value_i)^2) for r/g/b colors,
7127
+ // where color_value is the tabular value from the table of sampled RGB colors
7128
+ // from CMYK US Web Coated (SWOP) colorspace, and f_i is the corresponding
7129
+ // CMYK color conversion using the estimation below:
7130
+ // f(A, B,.. N) = Acc+Bcm+Ccy+Dck+c+Fmm+Gmy+Hmk+Im+Jyy+Kyk+Ly+Mkk+Nk+255
7131
+ function convertToRgb(src, srcOffset, srcScale, dest, destOffset) {
7132
+ var c = src[srcOffset + 0] * srcScale;
7133
+ var m = src[srcOffset + 1] * srcScale;
7134
+ var y = src[srcOffset + 2] * srcScale;
7135
+ var k = src[srcOffset + 3] * srcScale;
7136
+
7137
+ var r =
7138
+ (c * (-4.387332384609988 * c + 54.48615194189176 * m +
7139
+ 18.82290502165302 * y + 212.25662451639585 * k +
7140
+ -285.2331026137004) +
7141
+ m * (1.7149763477362134 * m - 5.6096736904047315 * y +
7142
+ -17.873870861415444 * k - 5.497006427196366) +
7143
+ y * (-2.5217340131683033 * y - 21.248923337353073 * k +
7144
+ 17.5119270841813) +
7145
+ k * (-21.86122147463605 * k - 189.48180835922747) + 255) | 0;
7146
+ var g =
7147
+ (c * (8.841041422036149 * c + 60.118027045597366 * m +
7148
+ 6.871425592049007 * y + 31.159100130055922 * k +
7149
+ -79.2970844816548) +
7150
+ m * (-15.310361306967817 * m + 17.575251261109482 * y +
7151
+ 131.35250912493976 * k - 190.9453302588951) +
7152
+ y * (4.444339102852739 * y + 9.8632861493405 * k - 24.86741582555878) +
7153
+ k * (-20.737325471181034 * k - 187.80453709719578) + 255) | 0;
7154
+ var b =
7155
+ (c * (0.8842522430003296 * c + 8.078677503112928 * m +
7156
+ 30.89978309703729 * y - 0.23883238689178934 * k +
7157
+ -14.183576799673286) +
7158
+ m * (10.49593273432072 * m + 63.02378494754052 * y +
7159
+ 50.606957656360734 * k - 112.23884253719248) +
7160
+ y * (0.03296041114873217 * y + 115.60384449646641 * k +
7161
+ -193.58209356861505) +
7162
+ k * (-22.33816807309886 * k - 180.12613974708367) + 255) | 0;
7163
+
7164
+ dest[destOffset] = r > 255 ? 255 : r < 0 ? 0 : r;
7165
+ dest[destOffset + 1] = g > 255 ? 255 : g < 0 ? 0 : g;
7166
+ dest[destOffset + 2] = b > 255 ? 255 : b < 0 ? 0 : b;
7167
+ }
7168
+
7169
+ function DeviceCmykCS() {
7170
+ this.name = 'DeviceCMYK';
7171
+ this.numComps = 4;
7172
+ this.defaultColor = new Float32Array([0, 0, 0, 1]);
7173
+ }
7174
+ DeviceCmykCS.prototype = {
7175
+ getRgb: ColorSpace.prototype.getRgb,
7176
+ getRgbItem: function DeviceCmykCS_getRgbItem(src, srcOffset,
7177
+ dest, destOffset) {
7178
+ convertToRgb(src, srcOffset, 1, dest, destOffset);
7179
+ },
7180
+ getRgbBuffer: function DeviceCmykCS_getRgbBuffer(src, srcOffset, count,
7181
+ dest, destOffset, bits,
7182
+ alpha01) {
7183
+ var scale = 1 / ((1 << bits) - 1);
7184
+ for (var i = 0; i < count; i++) {
7185
+ convertToRgb(src, srcOffset, scale, dest, destOffset);
7186
+ srcOffset += 4;
7187
+ destOffset += 3 + alpha01;
7188
+ }
7189
+ },
7190
+ getOutputLength: function DeviceCmykCS_getOutputLength(inputLength,
7191
+ alpha01) {
7192
+ return (inputLength / 4 * (3 + alpha01)) | 0;
7193
+ },
7194
+ isPassthrough: ColorSpace.prototype.isPassthrough,
7195
+ fillRgb: ColorSpace.prototype.fillRgb,
7196
+ isDefaultDecode: function DeviceCmykCS_isDefaultDecode(decodeMap) {
7197
+ return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
7198
+ },
7199
+ usesZeroToOneRange: true
7200
+ };
7201
+
7202
+ return DeviceCmykCS;
7203
+ })();
7204
+
7205
+ //
7206
+ // CalGrayCS: Based on "PDF Reference, Sixth Ed", p.245
7207
+ //
7208
+ var CalGrayCS = (function CalGrayCSClosure() {
7209
+ function CalGrayCS(whitePoint, blackPoint, gamma) {
7210
+ this.name = 'CalGray';
7211
+ this.numComps = 1;
7212
+ this.defaultColor = new Float32Array([0]);
7213
+
7214
+ if (!whitePoint) {
7215
+ error('WhitePoint missing - required for color space CalGray');
7216
+ }
7217
+ blackPoint = blackPoint || [0, 0, 0];
7218
+ gamma = gamma || 1;
7219
+
7220
+ // Translate arguments to spec variables.
7221
+ this.XW = whitePoint[0];
7222
+ this.YW = whitePoint[1];
7223
+ this.ZW = whitePoint[2];
7224
+
7225
+ this.XB = blackPoint[0];
7226
+ this.YB = blackPoint[1];
7227
+ this.ZB = blackPoint[2];
7228
+
7229
+ this.G = gamma;
7230
+
7231
+ // Validate variables as per spec.
7232
+ if (this.XW < 0 || this.ZW < 0 || this.YW !== 1) {
7233
+ error('Invalid WhitePoint components for ' + this.name +
7234
+ ', no fallback available');
7235
+ }
7236
+
7237
+ if (this.XB < 0 || this.YB < 0 || this.ZB < 0) {
7238
+ info('Invalid BlackPoint for ' + this.name + ', falling back to default');
7239
+ this.XB = this.YB = this.ZB = 0;
7240
+ }
7241
+
7242
+ if (this.XB !== 0 || this.YB !== 0 || this.ZB !== 0) {
7243
+ warn(this.name + ', BlackPoint: XB: ' + this.XB + ', YB: ' + this.YB +
7244
+ ', ZB: ' + this.ZB + ', only default values are supported.');
7245
+ }
7246
+
7247
+ if (this.G < 1) {
7248
+ info('Invalid Gamma: ' + this.G + ' for ' + this.name +
7249
+ ', falling back to default');
7250
+ this.G = 1;
7251
+ }
7252
+ }
7253
+
7254
+ function convertToRgb(cs, src, srcOffset, dest, destOffset, scale) {
7255
+ // A represents a gray component of a calibrated gray space.
7256
+ // A <---> AG in the spec
7257
+ var A = src[srcOffset] * scale;
7258
+ var AG = Math.pow(A, cs.G);
7259
+
7260
+ // Computes L as per spec. ( = cs.YW * AG )
7261
+ // Except if other than default BlackPoint values are used.
7262
+ var L = cs.YW * AG;
7263
+ // http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html, Ch 4.
7264
+ // Convert values to rgb range [0, 255].
7265
+ var val = Math.max(295.8 * Math.pow(L, 0.333333333333333333) - 40.8, 0) | 0;
7266
+ dest[destOffset] = val;
7267
+ dest[destOffset + 1] = val;
7268
+ dest[destOffset + 2] = val;
7269
+ }
7270
+
7271
+ CalGrayCS.prototype = {
7272
+ getRgb: ColorSpace.prototype.getRgb,
7273
+ getRgbItem: function CalGrayCS_getRgbItem(src, srcOffset,
7274
+ dest, destOffset) {
7275
+ convertToRgb(this, src, srcOffset, dest, destOffset, 1);
7276
+ },
7277
+ getRgbBuffer: function CalGrayCS_getRgbBuffer(src, srcOffset, count,
7278
+ dest, destOffset, bits,
7279
+ alpha01) {
7280
+ var scale = 1 / ((1 << bits) - 1);
7281
+
7282
+ for (var i = 0; i < count; ++i) {
7283
+ convertToRgb(this, src, srcOffset, dest, destOffset, scale);
7284
+ srcOffset += 1;
7285
+ destOffset += 3 + alpha01;
7286
+ }
7287
+ },
7288
+ getOutputLength: function CalGrayCS_getOutputLength(inputLength, alpha01) {
7289
+ return inputLength * (3 + alpha01);
7290
+ },
7291
+ isPassthrough: ColorSpace.prototype.isPassthrough,
7292
+ fillRgb: ColorSpace.prototype.fillRgb,
7293
+ isDefaultDecode: function CalGrayCS_isDefaultDecode(decodeMap) {
7294
+ return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
7295
+ },
7296
+ usesZeroToOneRange: true
7297
+ };
7298
+ return CalGrayCS;
7299
+ })();
7300
+
7301
+ //
7302
+ // CalRGBCS: Based on "PDF Reference, Sixth Ed", p.247
7303
+ //
7304
+ var CalRGBCS = (function CalRGBCSClosure() {
7305
+
7306
+ // See http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.html for these
7307
+ // matrices.
7308
+ var BRADFORD_SCALE_MATRIX = new Float32Array([
7309
+ 0.8951, 0.2664, -0.1614,
7310
+ -0.7502, 1.7135, 0.0367,
7311
+ 0.0389, -0.0685, 1.0296]);
7312
+
7313
+ var BRADFORD_SCALE_INVERSE_MATRIX = new Float32Array([
7314
+ 0.9869929, -0.1470543, 0.1599627,
7315
+ 0.4323053, 0.5183603, 0.0492912,
7316
+ -0.0085287, 0.0400428, 0.9684867]);
7317
+
7318
+ // See http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html.
7319
+ var SRGB_D65_XYZ_TO_RGB_MATRIX = new Float32Array([
7320
+ 3.2404542, -1.5371385, -0.4985314,
7321
+ -0.9692660, 1.8760108, 0.0415560,
7322
+ 0.0556434, -0.2040259, 1.0572252]);
7323
+
7324
+ var FLAT_WHITEPOINT_MATRIX = new Float32Array([1, 1, 1]);
7325
+
7326
+ var tempNormalizeMatrix = new Float32Array(3);
7327
+ var tempConvertMatrix1 = new Float32Array(3);
7328
+ var tempConvertMatrix2 = new Float32Array(3);
7329
+
7330
+ var DECODE_L_CONSTANT = Math.pow(((8 + 16) / 116), 3) / 8.0;
7331
+
7332
+ function CalRGBCS(whitePoint, blackPoint, gamma, matrix) {
7333
+ this.name = 'CalRGB';
7334
+ this.numComps = 3;
7335
+ this.defaultColor = new Float32Array(3);
7336
+
7337
+ if (!whitePoint) {
7338
+ error('WhitePoint missing - required for color space CalRGB');
7339
+ }
7340
+ blackPoint = blackPoint || new Float32Array(3);
7341
+ gamma = gamma || new Float32Array([1, 1, 1]);
7342
+ matrix = matrix || new Float32Array([1, 0, 0, 0, 1, 0, 0, 0, 1]);
7343
+
7344
+ // Translate arguments to spec variables.
7345
+ var XW = whitePoint[0];
7346
+ var YW = whitePoint[1];
7347
+ var ZW = whitePoint[2];
7348
+ this.whitePoint = whitePoint;
7349
+
7350
+ var XB = blackPoint[0];
7351
+ var YB = blackPoint[1];
7352
+ var ZB = blackPoint[2];
7353
+ this.blackPoint = blackPoint;
7354
+
7355
+ this.GR = gamma[0];
7356
+ this.GG = gamma[1];
7357
+ this.GB = gamma[2];
7358
+
7359
+ this.MXA = matrix[0];
7360
+ this.MYA = matrix[1];
7361
+ this.MZA = matrix[2];
7362
+ this.MXB = matrix[3];
7363
+ this.MYB = matrix[4];
7364
+ this.MZB = matrix[5];
7365
+ this.MXC = matrix[6];
7366
+ this.MYC = matrix[7];
7367
+ this.MZC = matrix[8];
7368
+
7369
+ // Validate variables as per spec.
7370
+ if (XW < 0 || ZW < 0 || YW !== 1) {
7371
+ error('Invalid WhitePoint components for ' + this.name +
7372
+ ', no fallback available');
7373
+ }
7374
+
7375
+ if (XB < 0 || YB < 0 || ZB < 0) {
7376
+ info('Invalid BlackPoint for ' + this.name + ' [' + XB + ', ' + YB +
7377
+ ', ' + ZB + '], falling back to default');
7378
+ this.blackPoint = new Float32Array(3);
7379
+ }
7380
+
7381
+ if (this.GR < 0 || this.GG < 0 || this.GB < 0) {
7382
+ info('Invalid Gamma [' + this.GR + ', ' + this.GG + ', ' + this.GB +
7383
+ '] for ' + this.name + ', falling back to default');
7384
+ this.GR = this.GG = this.GB = 1;
7385
+ }
7386
+
7387
+ if (this.MXA < 0 || this.MYA < 0 || this.MZA < 0 ||
7388
+ this.MXB < 0 || this.MYB < 0 || this.MZB < 0 ||
7389
+ this.MXC < 0 || this.MYC < 0 || this.MZC < 0) {
7390
+ info('Invalid Matrix for ' + this.name + ' [' +
7391
+ this.MXA + ', ' + this.MYA + ', ' + this.MZA +
7392
+ this.MXB + ', ' + this.MYB + ', ' + this.MZB +
7393
+ this.MXC + ', ' + this.MYC + ', ' + this.MZC +
7394
+ '], falling back to default');
7395
+ this.MXA = this.MYB = this.MZC = 1;
7396
+ this.MXB = this.MYA = this.MZA = this.MXC = this.MYC = this.MZB = 0;
7397
+ }
7398
+ }
7399
+
7400
+ function matrixProduct(a, b, result) {
7401
+ result[0] = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
7402
+ result[1] = a[3] * b[0] + a[4] * b[1] + a[5] * b[2];
7403
+ result[2] = a[6] * b[0] + a[7] * b[1] + a[8] * b[2];
7404
+ }
7405
+
7406
+ function convertToFlat(sourceWhitePoint, LMS, result) {
7407
+ result[0] = LMS[0] * 1 / sourceWhitePoint[0];
7408
+ result[1] = LMS[1] * 1 / sourceWhitePoint[1];
7409
+ result[2] = LMS[2] * 1 / sourceWhitePoint[2];
7410
+ }
7411
+
7412
+ function convertToD65(sourceWhitePoint, LMS, result) {
7413
+ var D65X = 0.95047;
7414
+ var D65Y = 1;
7415
+ var D65Z = 1.08883;
7416
+
7417
+ result[0] = LMS[0] * D65X / sourceWhitePoint[0];
7418
+ result[1] = LMS[1] * D65Y / sourceWhitePoint[1];
7419
+ result[2] = LMS[2] * D65Z / sourceWhitePoint[2];
7420
+ }
7421
+
7422
+ function sRGBTransferFunction(color) {
7423
+ // See http://en.wikipedia.org/wiki/SRGB.
7424
+ if (color <= 0.0031308){
7425
+ return adjustToRange(0, 1, 12.92 * color);
7426
+ }
7427
+
7428
+ return adjustToRange(0, 1, (1 + 0.055) * Math.pow(color, 1 / 2.4) - 0.055);
7429
+ }
7430
+
7431
+ function adjustToRange(min, max, value) {
7432
+ return Math.max(min, Math.min(max, value));
7433
+ }
7434
+
7435
+ function decodeL(L) {
7436
+ if (L < 0) {
7437
+ return -decodeL(-L);
7438
+ }
7439
+
7440
+ if (L > 8.0) {
7441
+ return Math.pow(((L + 16) / 116), 3);
7442
+ }
7443
+
7444
+ return L * DECODE_L_CONSTANT;
7445
+ }
7446
+
7447
+ function compensateBlackPoint(sourceBlackPoint, XYZ_Flat, result) {
7448
+
7449
+ // In case the blackPoint is already the default blackPoint then there is
7450
+ // no need to do compensation.
7451
+ if (sourceBlackPoint[0] === 0 &&
7452
+ sourceBlackPoint[1] === 0 &&
7453
+ sourceBlackPoint[2] === 0) {
7454
+ result[0] = XYZ_Flat[0];
7455
+ result[1] = XYZ_Flat[1];
7456
+ result[2] = XYZ_Flat[2];
7457
+ return;
7458
+ }
7459
+
7460
+ // For the blackPoint calculation details, please see
7461
+ // http://www.adobe.com/content/dam/Adobe/en/devnet/photoshop/sdk/
7462
+ // AdobeBPC.pdf.
7463
+ // The destination blackPoint is the default blackPoint [0, 0, 0].
7464
+ var zeroDecodeL = decodeL(0);
7465
+
7466
+ var X_DST = zeroDecodeL;
7467
+ var X_SRC = decodeL(sourceBlackPoint[0]);
7468
+
7469
+ var Y_DST = zeroDecodeL;
7470
+ var Y_SRC = decodeL(sourceBlackPoint[1]);
7471
+
7472
+ var Z_DST = zeroDecodeL;
7473
+ var Z_SRC = decodeL(sourceBlackPoint[2]);
7474
+
7475
+ var X_Scale = (1 - X_DST) / (1 - X_SRC);
7476
+ var X_Offset = 1 - X_Scale;
7477
+
7478
+ var Y_Scale = (1 - Y_DST) / (1 - Y_SRC);
7479
+ var Y_Offset = 1 - Y_Scale;
7480
+
7481
+ var Z_Scale = (1 - Z_DST) / (1 - Z_SRC);
7482
+ var Z_Offset = 1 - Z_Scale;
7483
+
7484
+ result[0] = XYZ_Flat[0] * X_Scale + X_Offset;
7485
+ result[1] = XYZ_Flat[1] * Y_Scale + Y_Offset;
7486
+ result[2] = XYZ_Flat[2] * Z_Scale + Z_Offset;
7487
+ }
7488
+
7489
+ function normalizeWhitePointToFlat(sourceWhitePoint, XYZ_In, result) {
7490
+
7491
+ // In case the whitePoint is already flat then there is no need to do
7492
+ // normalization.
7493
+ if (sourceWhitePoint[0] === 1 && sourceWhitePoint[2] === 1) {
7494
+ result[0] = XYZ_In[0];
7495
+ result[1] = XYZ_In[1];
7496
+ result[2] = XYZ_In[2];
7497
+ return;
7498
+ }
7499
+
7500
+ var LMS = result;
7501
+ matrixProduct(BRADFORD_SCALE_MATRIX, XYZ_In, LMS);
7502
+
7503
+ var LMS_Flat = tempNormalizeMatrix;
7504
+ convertToFlat(sourceWhitePoint, LMS, LMS_Flat);
7505
+
7506
+ matrixProduct(BRADFORD_SCALE_INVERSE_MATRIX, LMS_Flat, result);
7507
+ }
7508
+
7509
+ function normalizeWhitePointToD65(sourceWhitePoint, XYZ_In, result) {
7510
+
7511
+ var LMS = result;
7512
+ matrixProduct(BRADFORD_SCALE_MATRIX, XYZ_In, LMS);
7513
+
7514
+ var LMS_D65 = tempNormalizeMatrix;
7515
+ convertToD65(sourceWhitePoint, LMS, LMS_D65);
7516
+
7517
+ matrixProduct(BRADFORD_SCALE_INVERSE_MATRIX, LMS_D65, result);
7518
+ }
7519
+
7520
+ function convertToRgb(cs, src, srcOffset, dest, destOffset, scale) {
7521
+ // A, B and C represent a red, green and blue components of a calibrated
7522
+ // rgb space.
7523
+ var A = adjustToRange(0, 1, src[srcOffset] * scale);
7524
+ var B = adjustToRange(0, 1, src[srcOffset + 1] * scale);
7525
+ var C = adjustToRange(0, 1, src[srcOffset + 2] * scale);
7526
+
7527
+ // A <---> AGR in the spec
7528
+ // B <---> BGG in the spec
7529
+ // C <---> CGB in the spec
7530
+ var AGR = Math.pow(A, cs.GR);
7531
+ var BGG = Math.pow(B, cs.GG);
7532
+ var CGB = Math.pow(C, cs.GB);
7533
+
7534
+ // Computes intermediate variables L, M, N as per spec.
7535
+ // To decode X, Y, Z values map L, M, N directly to them.
7536
+ var X = cs.MXA * AGR + cs.MXB * BGG + cs.MXC * CGB;
7537
+ var Y = cs.MYA * AGR + cs.MYB * BGG + cs.MYC * CGB;
7538
+ var Z = cs.MZA * AGR + cs.MZB * BGG + cs.MZC * CGB;
7539
+
7540
+ // The following calculations are based on this document:
7541
+ // http://www.adobe.com/content/dam/Adobe/en/devnet/photoshop/sdk/
7542
+ // AdobeBPC.pdf.
7543
+ var XYZ = tempConvertMatrix1;
7544
+ XYZ[0] = X;
7545
+ XYZ[1] = Y;
7546
+ XYZ[2] = Z;
7547
+ var XYZ_Flat = tempConvertMatrix2;
7548
+
7549
+ normalizeWhitePointToFlat(cs.whitePoint, XYZ, XYZ_Flat);
7550
+
7551
+ var XYZ_Black = tempConvertMatrix1;
7552
+ compensateBlackPoint(cs.blackPoint, XYZ_Flat, XYZ_Black);
7553
+
7554
+ var XYZ_D65 = tempConvertMatrix2;
7555
+ normalizeWhitePointToD65(FLAT_WHITEPOINT_MATRIX, XYZ_Black, XYZ_D65);
7556
+
7557
+ var SRGB = tempConvertMatrix1;
7558
+ matrixProduct(SRGB_D65_XYZ_TO_RGB_MATRIX, XYZ_D65, SRGB);
7559
+
7560
+ var sR = sRGBTransferFunction(SRGB[0]);
7561
+ var sG = sRGBTransferFunction(SRGB[1]);
7562
+ var sB = sRGBTransferFunction(SRGB[2]);
7563
+
7564
+ // Convert the values to rgb range [0, 255].
7565
+ dest[destOffset] = Math.round(sR * 255);
7566
+ dest[destOffset + 1] = Math.round(sG * 255);
7567
+ dest[destOffset + 2] = Math.round(sB * 255);
7568
+ }
7569
+
7570
+ CalRGBCS.prototype = {
7571
+ getRgb: function CalRGBCS_getRgb(src, srcOffset) {
7572
+ var rgb = new Uint8Array(3);
7573
+ this.getRgbItem(src, srcOffset, rgb, 0);
7574
+ return rgb;
7575
+ },
7576
+ getRgbItem: function CalRGBCS_getRgbItem(src, srcOffset,
7577
+ dest, destOffset) {
7578
+ convertToRgb(this, src, srcOffset, dest, destOffset, 1);
7579
+ },
7580
+ getRgbBuffer: function CalRGBCS_getRgbBuffer(src, srcOffset, count,
7581
+ dest, destOffset, bits,
7582
+ alpha01) {
7583
+ var scale = 1 / ((1 << bits) - 1);
7584
+
7585
+ for (var i = 0; i < count; ++i) {
7586
+ convertToRgb(this, src, srcOffset, dest, destOffset, scale);
7587
+ srcOffset += 3;
7588
+ destOffset += 3 + alpha01;
7589
+ }
7590
+ },
7591
+ getOutputLength: function CalRGBCS_getOutputLength(inputLength, alpha01) {
7592
+ return (inputLength * (3 + alpha01) / 3) | 0;
7593
+ },
7594
+ isPassthrough: ColorSpace.prototype.isPassthrough,
7595
+ fillRgb: ColorSpace.prototype.fillRgb,
7596
+ isDefaultDecode: function CalRGBCS_isDefaultDecode(decodeMap) {
7597
+ return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
7598
+ },
7599
+ usesZeroToOneRange: true
7600
+ };
7601
+ return CalRGBCS;
7602
+ })();
7603
+
7604
+ //
7605
+ // LabCS: Based on "PDF Reference, Sixth Ed", p.250
7606
+ //
7607
+ var LabCS = (function LabCSClosure() {
7608
+ function LabCS(whitePoint, blackPoint, range) {
7609
+ this.name = 'Lab';
7610
+ this.numComps = 3;
7611
+ this.defaultColor = new Float32Array([0, 0, 0]);
7612
+
7613
+ if (!whitePoint) {
7614
+ error('WhitePoint missing - required for color space Lab');
7615
+ }
7616
+ blackPoint = blackPoint || [0, 0, 0];
7617
+ range = range || [-100, 100, -100, 100];
7618
+
7619
+ // Translate args to spec variables
7620
+ this.XW = whitePoint[0];
7621
+ this.YW = whitePoint[1];
7622
+ this.ZW = whitePoint[2];
7623
+ this.amin = range[0];
7624
+ this.amax = range[1];
7625
+ this.bmin = range[2];
7626
+ this.bmax = range[3];
7627
+
7628
+ // These are here just for completeness - the spec doesn't offer any
7629
+ // formulas that use BlackPoint in Lab
7630
+ this.XB = blackPoint[0];
7631
+ this.YB = blackPoint[1];
7632
+ this.ZB = blackPoint[2];
7633
+
7634
+ // Validate vars as per spec
7635
+ if (this.XW < 0 || this.ZW < 0 || this.YW !== 1) {
7636
+ error('Invalid WhitePoint components, no fallback available');
7637
+ }
7638
+
7639
+ if (this.XB < 0 || this.YB < 0 || this.ZB < 0) {
7640
+ info('Invalid BlackPoint, falling back to default');
7641
+ this.XB = this.YB = this.ZB = 0;
7642
+ }
7643
+
7644
+ if (this.amin > this.amax || this.bmin > this.bmax) {
7645
+ info('Invalid Range, falling back to defaults');
7646
+ this.amin = -100;
7647
+ this.amax = 100;
7648
+ this.bmin = -100;
7649
+ this.bmax = 100;
7650
+ }
7651
+ }
7652
+
7653
+ // Function g(x) from spec
7654
+ function fn_g(x) {
7655
+ if (x >= 6 / 29) {
7656
+ return x * x * x;
7657
+ } else {
7658
+ return (108 / 841) * (x - 4 / 29);
7659
+ }
7660
+ }
7661
+
7662
+ function decode(value, high1, low2, high2) {
7663
+ return low2 + (value) * (high2 - low2) / (high1);
7664
+ }
7665
+
7666
+ // If decoding is needed maxVal should be 2^bits per component - 1.
7667
+ function convertToRgb(cs, src, srcOffset, maxVal, dest, destOffset) {
7668
+ // XXX: Lab input is in the range of [0, 100], [amin, amax], [bmin, bmax]
7669
+ // not the usual [0, 1]. If a command like setFillColor is used the src
7670
+ // values will already be within the correct range. However, if we are
7671
+ // converting an image we have to map the values to the correct range given
7672
+ // above.
7673
+ // Ls,as,bs <---> L*,a*,b* in the spec
7674
+ var Ls = src[srcOffset];
7675
+ var as = src[srcOffset + 1];
7676
+ var bs = src[srcOffset + 2];
7677
+ if (maxVal !== false) {
7678
+ Ls = decode(Ls, maxVal, 0, 100);
7679
+ as = decode(as, maxVal, cs.amin, cs.amax);
7680
+ bs = decode(bs, maxVal, cs.bmin, cs.bmax);
7681
+ }
7682
+
7683
+ // Adjust limits of 'as' and 'bs'
7684
+ as = as > cs.amax ? cs.amax : as < cs.amin ? cs.amin : as;
7685
+ bs = bs > cs.bmax ? cs.bmax : bs < cs.bmin ? cs.bmin : bs;
7686
+
7687
+ // Computes intermediate variables X,Y,Z as per spec
7688
+ var M = (Ls + 16) / 116;
7689
+ var L = M + (as / 500);
7690
+ var N = M - (bs / 200);
7691
+
7692
+ var X = cs.XW * fn_g(L);
7693
+ var Y = cs.YW * fn_g(M);
7694
+ var Z = cs.ZW * fn_g(N);
7695
+
7696
+ var r, g, b;
7697
+ // Using different conversions for D50 and D65 white points,
7698
+ // per http://www.color.org/srgb.pdf
7699
+ if (cs.ZW < 1) {
7700
+ // Assuming D50 (X=0.9642, Y=1.00, Z=0.8249)
7701
+ r = X * 3.1339 + Y * -1.6170 + Z * -0.4906;
7702
+ g = X * -0.9785 + Y * 1.9160 + Z * 0.0333;
7703
+ b = X * 0.0720 + Y * -0.2290 + Z * 1.4057;
7704
+ } else {
7705
+ // Assuming D65 (X=0.9505, Y=1.00, Z=1.0888)
7706
+ r = X * 3.2406 + Y * -1.5372 + Z * -0.4986;
7707
+ g = X * -0.9689 + Y * 1.8758 + Z * 0.0415;
7708
+ b = X * 0.0557 + Y * -0.2040 + Z * 1.0570;
7709
+ }
7710
+ // clamp color values to [0,1] range then convert to [0,255] range.
7711
+ dest[destOffset] = r <= 0 ? 0 : r >= 1 ? 255 : Math.sqrt(r) * 255 | 0;
7712
+ dest[destOffset + 1] = g <= 0 ? 0 : g >= 1 ? 255 : Math.sqrt(g) * 255 | 0;
7713
+ dest[destOffset + 2] = b <= 0 ? 0 : b >= 1 ? 255 : Math.sqrt(b) * 255 | 0;
7714
+ }
7715
+
7716
+ LabCS.prototype = {
7717
+ getRgb: ColorSpace.prototype.getRgb,
7718
+ getRgbItem: function LabCS_getRgbItem(src, srcOffset, dest, destOffset) {
7719
+ convertToRgb(this, src, srcOffset, false, dest, destOffset);
7720
+ },
7721
+ getRgbBuffer: function LabCS_getRgbBuffer(src, srcOffset, count,
7722
+ dest, destOffset, bits,
7723
+ alpha01) {
7724
+ var maxVal = (1 << bits) - 1;
7725
+ for (var i = 0; i < count; i++) {
7726
+ convertToRgb(this, src, srcOffset, maxVal, dest, destOffset);
7727
+ srcOffset += 3;
7728
+ destOffset += 3 + alpha01;
7729
+ }
7730
+ },
7731
+ getOutputLength: function LabCS_getOutputLength(inputLength, alpha01) {
7732
+ return (inputLength * (3 + alpha01) / 3) | 0;
7733
+ },
7734
+ isPassthrough: ColorSpace.prototype.isPassthrough,
7735
+ fillRgb: ColorSpace.prototype.fillRgb,
7736
+ isDefaultDecode: function LabCS_isDefaultDecode(decodeMap) {
7737
+ // XXX: Decoding is handled with the lab conversion because of the strange
7738
+ // ranges that are used.
7739
+ return true;
7740
+ },
7741
+ usesZeroToOneRange: false
7742
+ };
7743
+ return LabCS;
7744
+ })();
7745
+
7746
+
7747
+ var ARCFourCipher = (function ARCFourCipherClosure() {
7748
+ function ARCFourCipher(key) {
7749
+ this.a = 0;
7750
+ this.b = 0;
7751
+ var s = new Uint8Array(256);
7752
+ var i, j = 0, tmp, keyLength = key.length;
7753
+ for (i = 0; i < 256; ++i) {
7754
+ s[i] = i;
7755
+ }
7756
+ for (i = 0; i < 256; ++i) {
7757
+ tmp = s[i];
7758
+ j = (j + tmp + key[i % keyLength]) & 0xFF;
7759
+ s[i] = s[j];
7760
+ s[j] = tmp;
7761
+ }
7762
+ this.s = s;
7763
+ }
7764
+
7765
+ ARCFourCipher.prototype = {
7766
+ encryptBlock: function ARCFourCipher_encryptBlock(data) {
7767
+ var i, n = data.length, tmp, tmp2;
7768
+ var a = this.a, b = this.b, s = this.s;
7769
+ var output = new Uint8Array(n);
7770
+ for (i = 0; i < n; ++i) {
7771
+ a = (a + 1) & 0xFF;
7772
+ tmp = s[a];
7773
+ b = (b + tmp) & 0xFF;
7774
+ tmp2 = s[b];
7775
+ s[a] = tmp2;
7776
+ s[b] = tmp;
7777
+ output[i] = data[i] ^ s[(tmp + tmp2) & 0xFF];
7778
+ }
7779
+ this.a = a;
7780
+ this.b = b;
7781
+ return output;
7782
+ }
7783
+ };
7784
+ ARCFourCipher.prototype.decryptBlock = ARCFourCipher.prototype.encryptBlock;
7785
+
7786
+ return ARCFourCipher;
7787
+ })();
7788
+
7789
+ var calculateMD5 = (function calculateMD5Closure() {
7790
+ var r = new Uint8Array([
7791
+ 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
7792
+ 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
7793
+ 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
7794
+ 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21]);
7795
+
7796
+ var k = new Int32Array([
7797
+ -680876936, -389564586, 606105819, -1044525330, -176418897, 1200080426,
7798
+ -1473231341, -45705983, 1770035416, -1958414417, -42063, -1990404162,
7799
+ 1804603682, -40341101, -1502002290, 1236535329, -165796510, -1069501632,
7800
+ 643717713, -373897302, -701558691, 38016083, -660478335, -405537848,
7801
+ 568446438, -1019803690, -187363961, 1163531501, -1444681467, -51403784,
7802
+ 1735328473, -1926607734, -378558, -2022574463, 1839030562, -35309556,
7803
+ -1530992060, 1272893353, -155497632, -1094730640, 681279174, -358537222,
7804
+ -722521979, 76029189, -640364487, -421815835, 530742520, -995338651,
7805
+ -198630844, 1126891415, -1416354905, -57434055, 1700485571, -1894986606,
7806
+ -1051523, -2054922799, 1873313359, -30611744, -1560198380, 1309151649,
7807
+ -145523070, -1120210379, 718787259, -343485551]);
7808
+
7809
+ function hash(data, offset, length) {
7810
+ var h0 = 1732584193, h1 = -271733879, h2 = -1732584194, h3 = 271733878;
7811
+ // pre-processing
7812
+ var paddedLength = (length + 72) & ~63; // data + 9 extra bytes
7813
+ var padded = new Uint8Array(paddedLength);
7814
+ var i, j, n;
7815
+ for (i = 0; i < length; ++i) {
7816
+ padded[i] = data[offset++];
7817
+ }
7818
+ padded[i++] = 0x80;
7819
+ n = paddedLength - 8;
7820
+ while (i < n) {
7821
+ padded[i++] = 0;
7822
+ }
7823
+ padded[i++] = (length << 3) & 0xFF;
7824
+ padded[i++] = (length >> 5) & 0xFF;
7825
+ padded[i++] = (length >> 13) & 0xFF;
7826
+ padded[i++] = (length >> 21) & 0xFF;
7827
+ padded[i++] = (length >>> 29) & 0xFF;
7828
+ padded[i++] = 0;
7829
+ padded[i++] = 0;
7830
+ padded[i++] = 0;
7831
+ var w = new Int32Array(16);
7832
+ for (i = 0; i < paddedLength;) {
7833
+ for (j = 0; j < 16; ++j, i += 4) {
7834
+ w[j] = (padded[i] | (padded[i + 1] << 8) |
7835
+ (padded[i + 2] << 16) | (padded[i + 3] << 24));
7836
+ }
7837
+ var a = h0, b = h1, c = h2, d = h3, f, g;
7838
+ for (j = 0; j < 64; ++j) {
7839
+ if (j < 16) {
7840
+ f = (b & c) | ((~b) & d);
7841
+ g = j;
7842
+ } else if (j < 32) {
7843
+ f = (d & b) | ((~d) & c);
7844
+ g = (5 * j + 1) & 15;
7845
+ } else if (j < 48) {
7846
+ f = b ^ c ^ d;
7847
+ g = (3 * j + 5) & 15;
7848
+ } else {
7849
+ f = c ^ (b | (~d));
7850
+ g = (7 * j) & 15;
7851
+ }
7852
+ var tmp = d, rotateArg = (a + f + k[j] + w[g]) | 0, rotate = r[j];
7853
+ d = c;
7854
+ c = b;
7855
+ b = (b + ((rotateArg << rotate) | (rotateArg >>> (32 - rotate)))) | 0;
7856
+ a = tmp;
7857
+ }
7858
+ h0 = (h0 + a) | 0;
7859
+ h1 = (h1 + b) | 0;
7860
+ h2 = (h2 + c) | 0;
7861
+ h3 = (h3 + d) | 0;
7862
+ }
7863
+ return new Uint8Array([
7864
+ h0 & 0xFF, (h0 >> 8) & 0xFF, (h0 >> 16) & 0xFF, (h0 >>> 24) & 0xFF,
7865
+ h1 & 0xFF, (h1 >> 8) & 0xFF, (h1 >> 16) & 0xFF, (h1 >>> 24) & 0xFF,
7866
+ h2 & 0xFF, (h2 >> 8) & 0xFF, (h2 >> 16) & 0xFF, (h2 >>> 24) & 0xFF,
7867
+ h3 & 0xFF, (h3 >> 8) & 0xFF, (h3 >> 16) & 0xFF, (h3 >>> 24) & 0xFF
7868
+ ]);
7869
+ }
7870
+
7871
+ return hash;
7872
+ })();
7873
+ var Word64 = (function Word64Closure() {
7874
+ function Word64(highInteger, lowInteger) {
7875
+ this.high = highInteger | 0;
7876
+ this.low = lowInteger | 0;
7877
+ }
7878
+ Word64.prototype = {
7879
+ and: function Word64_and(word) {
7880
+ this.high &= word.high;
7881
+ this.low &= word.low;
7882
+ },
7883
+ xor: function Word64_xor(word) {
7884
+ this.high ^= word.high;
7885
+ this.low ^= word.low;
7886
+ },
7887
+
7888
+ or: function Word64_or(word) {
7889
+ this.high |= word.high;
7890
+ this.low |= word.low;
7891
+ },
7892
+
7893
+ shiftRight: function Word64_shiftRight(places) {
7894
+ if (places >= 32) {
7895
+ this.low = (this.high >>> (places - 32)) | 0;
7896
+ this.high = 0;
7897
+ } else {
7898
+ this.low = (this.low >>> places) | (this.high << (32 - places));
7899
+ this.high = (this.high >>> places) | 0;
7900
+ }
7901
+ },
7902
+
7903
+ shiftLeft: function Word64_shiftLeft(places) {
7904
+ if (places >= 32) {
7905
+ this.high = this.low << (places - 32);
7906
+ this.low = 0;
7907
+ } else {
7908
+ this.high = (this.high << places) | (this.low >>> (32 - places));
7909
+ this.low = this.low << places;
7910
+ }
7911
+ },
7912
+
7913
+ rotateRight: function Word64_rotateRight(places) {
7914
+ var low, high;
7915
+ if (places & 32) {
7916
+ high = this.low;
7917
+ low = this.high;
7918
+ } else {
7919
+ low = this.low;
7920
+ high = this.high;
7921
+ }
7922
+ places &= 31;
7923
+ this.low = (low >>> places) | (high << (32 - places));
7924
+ this.high = (high >>> places) | (low << (32 - places));
7925
+ },
7926
+
7927
+ not: function Word64_not() {
7928
+ this.high = ~this.high;
7929
+ this.low = ~this.low;
7930
+ },
7931
+
7932
+ add: function Word64_add(word) {
7933
+ var lowAdd = (this.low >>> 0) + (word.low >>> 0);
7934
+ var highAdd = (this.high >>> 0) + (word.high >>> 0);
7935
+ if (lowAdd > 0xFFFFFFFF) {
7936
+ highAdd += 1;
7937
+ }
7938
+ this.low = lowAdd | 0;
7939
+ this.high = highAdd | 0;
7940
+ },
7941
+
7942
+ copyTo: function Word64_copyTo(bytes, offset) {
7943
+ bytes[offset] = (this.high >>> 24) & 0xFF;
7944
+ bytes[offset + 1] = (this.high >> 16) & 0xFF;
7945
+ bytes[offset + 2] = (this.high >> 8) & 0xFF;
7946
+ bytes[offset + 3] = this.high & 0xFF;
7947
+ bytes[offset + 4] = (this.low >>> 24) & 0xFF;
7948
+ bytes[offset + 5] = (this.low >> 16) & 0xFF;
7949
+ bytes[offset + 6] = (this.low >> 8) & 0xFF;
7950
+ bytes[offset + 7] = this.low & 0xFF;
7951
+ },
7952
+
7953
+ assign: function Word64_assign(word) {
7954
+ this.high = word.high;
7955
+ this.low = word.low;
7956
+ }
7957
+ };
7958
+ return Word64;
7959
+ })();
7960
+
7961
+ var calculateSHA256 = (function calculateSHA256Closure() {
7962
+ function rotr(x, n) {
7963
+ return (x >>> n) | (x << 32 - n);
7964
+ }
7965
+
7966
+ function ch(x, y, z) {
7967
+ return (x & y) ^ (~x & z);
7968
+ }
7969
+
7970
+ function maj(x, y, z) {
7971
+ return (x & y) ^ (x & z) ^ (y & z);
7972
+ }
7973
+
7974
+ function sigma(x) {
7975
+ return rotr(x, 2) ^ rotr(x, 13) ^ rotr(x, 22);
7976
+ }
7977
+
7978
+ function sigmaPrime(x) {
7979
+ return rotr(x, 6) ^ rotr(x, 11) ^ rotr(x, 25);
7980
+ }
7981
+
7982
+ function littleSigma(x) {
7983
+ return rotr(x, 7) ^ rotr(x, 18) ^ x >>> 3;
7984
+ }
7985
+
7986
+ function littleSigmaPrime(x) {
7987
+ return rotr(x, 17) ^ rotr(x, 19) ^ x >>> 10;
7988
+ }
7989
+
7990
+ var k = [0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
7991
+ 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
7992
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
7993
+ 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
7994
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
7995
+ 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
7996
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
7997
+ 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
7998
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
7999
+ 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
8000
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
8001
+ 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
8002
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
8003
+ 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
8004
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
8005
+ 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2];
8006
+
8007
+ function hash(data, offset, length) {
8008
+ // initial hash values
8009
+ var h0 = 0x6a09e667, h1 = 0xbb67ae85, h2 = 0x3c6ef372,
8010
+ h3 = 0xa54ff53a, h4 = 0x510e527f, h5 = 0x9b05688c,
8011
+ h6 = 0x1f83d9ab, h7 = 0x5be0cd19;
8012
+ // pre-processing
8013
+ var paddedLength = Math.ceil((length + 9) / 64) * 64;
8014
+ var padded = new Uint8Array(paddedLength);
8015
+ var i, j, n;
8016
+ for (i = 0; i < length; ++i) {
8017
+ padded[i] = data[offset++];
8018
+ }
8019
+ padded[i++] = 0x80;
8020
+ n = paddedLength - 8;
8021
+ while (i < n) {
8022
+ padded[i++] = 0;
8023
+ }
8024
+ padded[i++] = 0;
8025
+ padded[i++] = 0;
8026
+ padded[i++] = 0;
8027
+ padded[i++] = (length >>> 29) & 0xFF;
8028
+ padded[i++] = (length >> 21) & 0xFF;
8029
+ padded[i++] = (length >> 13) & 0xFF;
8030
+ padded[i++] = (length >> 5) & 0xFF;
8031
+ padded[i++] = (length << 3) & 0xFF;
8032
+ var w = new Uint32Array(64);
8033
+ // for each 512 bit block
8034
+ for (i = 0; i < paddedLength;) {
8035
+ for (j = 0; j < 16; ++j) {
8036
+ w[j] = (padded[i] << 24 | (padded[i + 1] << 16) |
8037
+ (padded[i + 2] << 8) | (padded[i + 3]));
8038
+ i += 4;
8039
+ }
8040
+
8041
+ for (j = 16; j < 64; ++j) {
8042
+ w[j] = littleSigmaPrime(w[j - 2]) + w[j - 7] +
8043
+ littleSigma(w[j - 15]) + w[j - 16] | 0;
8044
+ }
8045
+ var a = h0, b = h1, c = h2, d = h3, e = h4,
8046
+ f = h5, g = h6, h = h7, t1, t2;
8047
+ for (j = 0; j < 64; ++j) {
8048
+ t1 = h + sigmaPrime(e) + ch(e, f, g) + k[j] + w[j];
8049
+ t2 = sigma(a) + maj(a, b, c);
8050
+ h = g;
8051
+ g = f;
8052
+ f = e;
8053
+ e = (d + t1) | 0;
8054
+ d = c;
8055
+ c = b;
8056
+ b = a;
8057
+ a = (t1 + t2) | 0;
8058
+ }
8059
+ h0 = (h0 + a) | 0;
8060
+ h1 = (h1 + b) | 0;
8061
+ h2 = (h2 + c) | 0;
8062
+ h3 = (h3 + d) | 0;
8063
+ h4 = (h4 + e) | 0;
8064
+ h5 = (h5 + f) | 0;
8065
+ h6 = (h6 + g) | 0;
8066
+ h7 = (h7 + h) | 0;
8067
+ }
8068
+ return new Uint8Array([
8069
+ (h0 >> 24) & 0xFF, (h0 >> 16) & 0xFF, (h0 >> 8) & 0xFF, (h0) & 0xFF,
8070
+ (h1 >> 24) & 0xFF, (h1 >> 16) & 0xFF, (h1 >> 8) & 0xFF, (h1) & 0xFF,
8071
+ (h2 >> 24) & 0xFF, (h2 >> 16) & 0xFF, (h2 >> 8) & 0xFF, (h2) & 0xFF,
8072
+ (h3 >> 24) & 0xFF, (h3 >> 16) & 0xFF, (h3 >> 8) & 0xFF, (h3) & 0xFF,
8073
+ (h4 >> 24) & 0xFF, (h4 >> 16) & 0xFF, (h4 >> 8) & 0xFF, (h4) & 0xFF,
8074
+ (h5 >> 24) & 0xFF, (h5 >> 16) & 0xFF, (h5 >> 8) & 0xFF, (h5) & 0xFF,
8075
+ (h6 >> 24) & 0xFF, (h6 >> 16) & 0xFF, (h6 >> 8) & 0xFF, (h6) & 0xFF,
8076
+ (h7 >> 24) & 0xFF, (h7 >> 16) & 0xFF, (h7 >> 8) & 0xFF, (h7) & 0xFF
8077
+ ]);
8078
+ }
8079
+
8080
+ return hash;
8081
+ })();
8082
+
8083
+ var calculateSHA512 = (function calculateSHA512Closure() {
8084
+ function ch(result, x, y, z, tmp) {
8085
+ result.assign(x);
8086
+ result.and(y);
8087
+ tmp.assign(x);
8088
+ tmp.not();
8089
+ tmp.and(z);
8090
+ result.xor(tmp);
8091
+ }
8092
+
8093
+ function maj(result, x, y, z, tmp) {
8094
+ result.assign(x);
8095
+ result.and(y);
8096
+ tmp.assign(x);
8097
+ tmp.and(z);
8098
+ result.xor(tmp);
8099
+ tmp.assign(y);
8100
+ tmp.and(z);
8101
+ result.xor(tmp);
8102
+ }
8103
+
8104
+ function sigma(result, x, tmp) {
8105
+ result.assign(x);
8106
+ result.rotateRight(28);
8107
+ tmp.assign(x);
8108
+ tmp.rotateRight(34);
8109
+ result.xor(tmp);
8110
+ tmp.assign(x);
8111
+ tmp.rotateRight(39);
8112
+ result.xor(tmp);
8113
+ }
8114
+
8115
+ function sigmaPrime(result, x, tmp) {
8116
+ result.assign(x);
8117
+ result.rotateRight(14);
8118
+ tmp.assign(x);
8119
+ tmp.rotateRight(18);
8120
+ result.xor(tmp);
8121
+ tmp.assign(x);
8122
+ tmp.rotateRight(41);
8123
+ result.xor(tmp);
8124
+ }
8125
+
8126
+ function littleSigma(result, x, tmp) {
8127
+ result.assign(x);
8128
+ result.rotateRight(1);
8129
+ tmp.assign(x);
8130
+ tmp.rotateRight(8);
8131
+ result.xor(tmp);
8132
+ tmp.assign(x);
8133
+ tmp.shiftRight(7);
8134
+ result.xor(tmp);
8135
+ }
8136
+
8137
+ function littleSigmaPrime(result, x, tmp) {
8138
+ result.assign(x);
8139
+ result.rotateRight(19);
8140
+ tmp.assign(x);
8141
+ tmp.rotateRight(61);
8142
+ result.xor(tmp);
8143
+ tmp.assign(x);
8144
+ tmp.shiftRight(6);
8145
+ result.xor(tmp);
8146
+ }
8147
+
8148
+ var k = [
8149
+ new Word64(0x428a2f98, 0xd728ae22), new Word64(0x71374491, 0x23ef65cd),
8150
+ new Word64(0xb5c0fbcf, 0xec4d3b2f), new Word64(0xe9b5dba5, 0x8189dbbc),
8151
+ new Word64(0x3956c25b, 0xf348b538), new Word64(0x59f111f1, 0xb605d019),
8152
+ new Word64(0x923f82a4, 0xaf194f9b), new Word64(0xab1c5ed5, 0xda6d8118),
8153
+ new Word64(0xd807aa98, 0xa3030242), new Word64(0x12835b01, 0x45706fbe),
8154
+ new Word64(0x243185be, 0x4ee4b28c), new Word64(0x550c7dc3, 0xd5ffb4e2),
8155
+ new Word64(0x72be5d74, 0xf27b896f), new Word64(0x80deb1fe, 0x3b1696b1),
8156
+ new Word64(0x9bdc06a7, 0x25c71235), new Word64(0xc19bf174, 0xcf692694),
8157
+ new Word64(0xe49b69c1, 0x9ef14ad2), new Word64(0xefbe4786, 0x384f25e3),
8158
+ new Word64(0x0fc19dc6, 0x8b8cd5b5), new Word64(0x240ca1cc, 0x77ac9c65),
8159
+ new Word64(0x2de92c6f, 0x592b0275), new Word64(0x4a7484aa, 0x6ea6e483),
8160
+ new Word64(0x5cb0a9dc, 0xbd41fbd4), new Word64(0x76f988da, 0x831153b5),
8161
+ new Word64(0x983e5152, 0xee66dfab), new Word64(0xa831c66d, 0x2db43210),
8162
+ new Word64(0xb00327c8, 0x98fb213f), new Word64(0xbf597fc7, 0xbeef0ee4),
8163
+ new Word64(0xc6e00bf3, 0x3da88fc2), new Word64(0xd5a79147, 0x930aa725),
8164
+ new Word64(0x06ca6351, 0xe003826f), new Word64(0x14292967, 0x0a0e6e70),
8165
+ new Word64(0x27b70a85, 0x46d22ffc), new Word64(0x2e1b2138, 0x5c26c926),
8166
+ new Word64(0x4d2c6dfc, 0x5ac42aed), new Word64(0x53380d13, 0x9d95b3df),
8167
+ new Word64(0x650a7354, 0x8baf63de), new Word64(0x766a0abb, 0x3c77b2a8),
8168
+ new Word64(0x81c2c92e, 0x47edaee6), new Word64(0x92722c85, 0x1482353b),
8169
+ new Word64(0xa2bfe8a1, 0x4cf10364), new Word64(0xa81a664b, 0xbc423001),
8170
+ new Word64(0xc24b8b70, 0xd0f89791), new Word64(0xc76c51a3, 0x0654be30),
8171
+ new Word64(0xd192e819, 0xd6ef5218), new Word64(0xd6990624, 0x5565a910),
8172
+ new Word64(0xf40e3585, 0x5771202a), new Word64(0x106aa070, 0x32bbd1b8),
8173
+ new Word64(0x19a4c116, 0xb8d2d0c8), new Word64(0x1e376c08, 0x5141ab53),
8174
+ new Word64(0x2748774c, 0xdf8eeb99), new Word64(0x34b0bcb5, 0xe19b48a8),
8175
+ new Word64(0x391c0cb3, 0xc5c95a63), new Word64(0x4ed8aa4a, 0xe3418acb),
8176
+ new Word64(0x5b9cca4f, 0x7763e373), new Word64(0x682e6ff3, 0xd6b2b8a3),
8177
+ new Word64(0x748f82ee, 0x5defb2fc), new Word64(0x78a5636f, 0x43172f60),
8178
+ new Word64(0x84c87814, 0xa1f0ab72), new Word64(0x8cc70208, 0x1a6439ec),
8179
+ new Word64(0x90befffa, 0x23631e28), new Word64(0xa4506ceb, 0xde82bde9),
8180
+ new Word64(0xbef9a3f7, 0xb2c67915), new Word64(0xc67178f2, 0xe372532b),
8181
+ new Word64(0xca273ece, 0xea26619c), new Word64(0xd186b8c7, 0x21c0c207),
8182
+ new Word64(0xeada7dd6, 0xcde0eb1e), new Word64(0xf57d4f7f, 0xee6ed178),
8183
+ new Word64(0x06f067aa, 0x72176fba), new Word64(0x0a637dc5, 0xa2c898a6),
8184
+ new Word64(0x113f9804, 0xbef90dae), new Word64(0x1b710b35, 0x131c471b),
8185
+ new Word64(0x28db77f5, 0x23047d84), new Word64(0x32caab7b, 0x40c72493),
8186
+ new Word64(0x3c9ebe0a, 0x15c9bebc), new Word64(0x431d67c4, 0x9c100d4c),
8187
+ new Word64(0x4cc5d4be, 0xcb3e42b6), new Word64(0x597f299c, 0xfc657e2a),
8188
+ new Word64(0x5fcb6fab, 0x3ad6faec), new Word64(0x6c44198c, 0x4a475817)];
8189
+
8190
+ function hash(data, offset, length, mode384) {
8191
+ mode384 = !!mode384;
8192
+ // initial hash values
8193
+ var h0, h1, h2, h3, h4, h5, h6, h7;
8194
+ if (!mode384) {
8195
+ h0 = new Word64(0x6a09e667, 0xf3bcc908);
8196
+ h1 = new Word64(0xbb67ae85, 0x84caa73b);
8197
+ h2 = new Word64(0x3c6ef372, 0xfe94f82b);
8198
+ h3 = new Word64(0xa54ff53a, 0x5f1d36f1);
8199
+ h4 = new Word64(0x510e527f, 0xade682d1);
8200
+ h5 = new Word64(0x9b05688c, 0x2b3e6c1f);
8201
+ h6 = new Word64(0x1f83d9ab, 0xfb41bd6b);
8202
+ h7 = new Word64(0x5be0cd19, 0x137e2179);
8203
+ }
8204
+ else {
8205
+ // SHA384 is exactly the same
8206
+ // except with different starting values and a trimmed result
8207
+ h0 = new Word64(0xcbbb9d5d, 0xc1059ed8);
8208
+ h1 = new Word64(0x629a292a, 0x367cd507);
8209
+ h2 = new Word64(0x9159015a, 0x3070dd17);
8210
+ h3 = new Word64(0x152fecd8, 0xf70e5939);
8211
+ h4 = new Word64(0x67332667, 0xffc00b31);
8212
+ h5 = new Word64(0x8eb44a87, 0x68581511);
8213
+ h6 = new Word64(0xdb0c2e0d, 0x64f98fa7);
8214
+ h7 = new Word64(0x47b5481d, 0xbefa4fa4);
8215
+ }
8216
+
8217
+ // pre-processing
8218
+ var paddedLength = Math.ceil((length + 17) / 128) * 128;
8219
+ var padded = new Uint8Array(paddedLength);
8220
+ var i, j, n;
8221
+ for (i = 0; i < length; ++i) {
8222
+ padded[i] = data[offset++];
8223
+ }
8224
+ padded[i++] = 0x80;
8225
+ n = paddedLength - 16;
8226
+ while (i < n) {
8227
+ padded[i++] = 0;
8228
+ }
8229
+ padded[i++] = 0;
8230
+ padded[i++] = 0;
8231
+ padded[i++] = 0;
8232
+ padded[i++] = 0;
8233
+ padded[i++] = 0;
8234
+ padded[i++] = 0;
8235
+ padded[i++] = 0;
8236
+ padded[i++] = 0;
8237
+ padded[i++] = 0;
8238
+ padded[i++] = 0;
8239
+ padded[i++] = 0;
8240
+ padded[i++] = (length >>> 29) & 0xFF;
8241
+ padded[i++] = (length >> 21) & 0xFF;
8242
+ padded[i++] = (length >> 13) & 0xFF;
8243
+ padded[i++] = (length >> 5) & 0xFF;
8244
+ padded[i++] = (length << 3) & 0xFF;
8245
+
8246
+ var w = new Array(80);
8247
+ for (i = 0; i < 80; i++) {
8248
+ w[i] = new Word64(0, 0);
8249
+ }
8250
+ var a = new Word64(0, 0), b = new Word64(0, 0), c = new Word64(0, 0);
8251
+ var d = new Word64(0, 0), e = new Word64(0, 0), f = new Word64(0, 0);
8252
+ var g = new Word64(0, 0), h = new Word64(0, 0);
8253
+ var t1 = new Word64(0, 0), t2 = new Word64(0, 0);
8254
+ var tmp1 = new Word64(0, 0), tmp2 = new Word64(0, 0), tmp3;
8255
+
8256
+ // for each 1024 bit block
8257
+ for (i = 0; i < paddedLength;) {
8258
+ for (j = 0; j < 16; ++j) {
8259
+ w[j].high = (padded[i] << 24) | (padded[i + 1] << 16) |
8260
+ (padded[i + 2] << 8) | (padded[i + 3]);
8261
+ w[j].low = (padded[i + 4]) << 24 | (padded[i + 5]) << 16 |
8262
+ (padded[i + 6]) << 8 | (padded[i + 7]);
8263
+ i += 8;
8264
+ }
8265
+ for (j = 16; j < 80; ++j) {
8266
+ tmp3 = w[j];
8267
+ littleSigmaPrime(tmp3, w[j - 2], tmp2);
8268
+ tmp3.add(w[j - 7]);
8269
+ littleSigma(tmp1, w[j - 15], tmp2);
8270
+ tmp3.add(tmp1);
8271
+ tmp3.add(w[j - 16]);
8272
+ }
8273
+
8274
+ a.assign(h0); b.assign(h1); c.assign(h2); d.assign(h3);
8275
+ e.assign(h4); f.assign(h5); g.assign(h6); h.assign(h7);
8276
+ for (j = 0; j < 80; ++j) {
8277
+ t1.assign(h);
8278
+ sigmaPrime(tmp1, e, tmp2);
8279
+ t1.add(tmp1);
8280
+ ch(tmp1, e, f, g, tmp2);
8281
+ t1.add(tmp1);
8282
+ t1.add(k[j]);
8283
+ t1.add(w[j]);
8284
+
8285
+ sigma(t2, a, tmp2);
8286
+ maj(tmp1, a, b, c, tmp2);
8287
+ t2.add(tmp1);
8288
+
8289
+ tmp3 = h;
8290
+ h = g;
8291
+ g = f;
8292
+ f = e;
8293
+ d.add(t1);
8294
+ e = d;
8295
+ d = c;
8296
+ c = b;
8297
+ b = a;
8298
+ tmp3.assign(t1);
8299
+ tmp3.add(t2);
8300
+ a = tmp3;
8301
+ }
8302
+ h0.add(a);
8303
+ h1.add(b);
8304
+ h2.add(c);
8305
+ h3.add(d);
8306
+ h4.add(e);
8307
+ h5.add(f);
8308
+ h6.add(g);
8309
+ h7.add(h);
8310
+ }
8311
+
8312
+ var result;
8313
+ if (!mode384) {
8314
+ result = new Uint8Array(64);
8315
+ h0.copyTo(result,0);
8316
+ h1.copyTo(result,8);
8317
+ h2.copyTo(result,16);
8318
+ h3.copyTo(result,24);
8319
+ h4.copyTo(result,32);
8320
+ h5.copyTo(result,40);
8321
+ h6.copyTo(result,48);
8322
+ h7.copyTo(result,56);
8323
+ }
8324
+ else {
8325
+ result = new Uint8Array(48);
8326
+ h0.copyTo(result,0);
8327
+ h1.copyTo(result,8);
8328
+ h2.copyTo(result,16);
8329
+ h3.copyTo(result,24);
8330
+ h4.copyTo(result,32);
8331
+ h5.copyTo(result,40);
8332
+ }
8333
+ return result;
8334
+ }
8335
+
8336
+ return hash;
8337
+ })();
8338
+ var calculateSHA384 = (function calculateSHA384Closure() {
8339
+ function hash(data, offset, length) {
8340
+ return calculateSHA512(data, offset, length, true);
8341
+ }
8342
+
8343
+ return hash;
8344
+ })();
8345
+ var NullCipher = (function NullCipherClosure() {
8346
+ function NullCipher() {
8347
+ }
8348
+
8349
+ NullCipher.prototype = {
8350
+ decryptBlock: function NullCipher_decryptBlock(data) {
8351
+ return data;
8352
+ }
8353
+ };
8354
+
8355
+ return NullCipher;
8356
+ })();
8357
+
8358
+ var AES128Cipher = (function AES128CipherClosure() {
8359
+ var rcon = new Uint8Array([
8360
+ 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c,
8361
+ 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a,
8362
+ 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
8363
+ 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a,
8364
+ 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
8365
+ 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6,
8366
+ 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72,
8367
+ 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc,
8368
+ 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10,
8369
+ 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e,
8370
+ 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5,
8371
+ 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94,
8372
+ 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02,
8373
+ 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d,
8374
+ 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d,
8375
+ 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f,
8376
+ 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb,
8377
+ 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c,
8378
+ 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a,
8379
+ 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
8380
+ 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a,
8381
+ 0x74, 0xe8, 0xcb, 0x8d]);
8382
+
8383
+ var s = new Uint8Array([
8384
+ 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b,
8385
+ 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
8386
+ 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26,
8387
+ 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
8388
+ 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2,
8389
+ 0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0,
8390
+ 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed,
8391
+ 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
8392
+ 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f,
8393
+ 0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
8394
+ 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c, 0x13, 0xec,
8395
+ 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
8396
+ 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14,
8397
+ 0xde, 0x5e, 0x0b, 0xdb, 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c,
8398
+ 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d,
8399
+ 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
8400
+ 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f,
8401
+ 0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,
8402
+ 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11,
8403
+ 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
8404
+ 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f,
8405
+ 0xb0, 0x54, 0xbb, 0x16]);
8406
+
8407
+ var inv_s = new Uint8Array([
8408
+ 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e,
8409
+ 0x81, 0xf3, 0xd7, 0xfb, 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87,
8410
+ 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, 0x54, 0x7b, 0x94, 0x32,
8411
+ 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
8412
+ 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49,
8413
+ 0x6d, 0x8b, 0xd1, 0x25, 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16,
8414
+ 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, 0x6c, 0x70, 0x48, 0x50,
8415
+ 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
8416
+ 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05,
8417
+ 0xb8, 0xb3, 0x45, 0x06, 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02,
8418
+ 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, 0x3a, 0x91, 0x11, 0x41,
8419
+ 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
8420
+ 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8,
8421
+ 0x1c, 0x75, 0xdf, 0x6e, 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89,
8422
+ 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, 0xfc, 0x56, 0x3e, 0x4b,
8423
+ 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
8424
+ 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59,
8425
+ 0x27, 0x80, 0xec, 0x5f, 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d,
8426
+ 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, 0xa0, 0xe0, 0x3b, 0x4d,
8427
+ 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
8428
+ 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63,
8429
+ 0x55, 0x21, 0x0c, 0x7d]);
8430
+ var mixCol = new Uint8Array(256);
8431
+ for (var i = 0; i < 256; i++) {
8432
+ if (i < 128) {
8433
+ mixCol[i] = i << 1;
8434
+ } else {
8435
+ mixCol[i] = (i << 1) ^ 0x1b;
8436
+ }
8437
+ }
8438
+ var mix = new Uint32Array([
8439
+ 0x00000000, 0x0e090d0b, 0x1c121a16, 0x121b171d, 0x3824342c, 0x362d3927,
8440
+ 0x24362e3a, 0x2a3f2331, 0x70486858, 0x7e416553, 0x6c5a724e, 0x62537f45,
8441
+ 0x486c5c74, 0x4665517f, 0x547e4662, 0x5a774b69, 0xe090d0b0, 0xee99ddbb,
8442
+ 0xfc82caa6, 0xf28bc7ad, 0xd8b4e49c, 0xd6bde997, 0xc4a6fe8a, 0xcaaff381,
8443
+ 0x90d8b8e8, 0x9ed1b5e3, 0x8ccaa2fe, 0x82c3aff5, 0xa8fc8cc4, 0xa6f581cf,
8444
+ 0xb4ee96d2, 0xbae79bd9, 0xdb3bbb7b, 0xd532b670, 0xc729a16d, 0xc920ac66,
8445
+ 0xe31f8f57, 0xed16825c, 0xff0d9541, 0xf104984a, 0xab73d323, 0xa57ade28,
8446
+ 0xb761c935, 0xb968c43e, 0x9357e70f, 0x9d5eea04, 0x8f45fd19, 0x814cf012,
8447
+ 0x3bab6bcb, 0x35a266c0, 0x27b971dd, 0x29b07cd6, 0x038f5fe7, 0x0d8652ec,
8448
+ 0x1f9d45f1, 0x119448fa, 0x4be30393, 0x45ea0e98, 0x57f11985, 0x59f8148e,
8449
+ 0x73c737bf, 0x7dce3ab4, 0x6fd52da9, 0x61dc20a2, 0xad766df6, 0xa37f60fd,
8450
+ 0xb16477e0, 0xbf6d7aeb, 0x955259da, 0x9b5b54d1, 0x894043cc, 0x87494ec7,
8451
+ 0xdd3e05ae, 0xd33708a5, 0xc12c1fb8, 0xcf2512b3, 0xe51a3182, 0xeb133c89,
8452
+ 0xf9082b94, 0xf701269f, 0x4de6bd46, 0x43efb04d, 0x51f4a750, 0x5ffdaa5b,
8453
+ 0x75c2896a, 0x7bcb8461, 0x69d0937c, 0x67d99e77, 0x3daed51e, 0x33a7d815,
8454
+ 0x21bccf08, 0x2fb5c203, 0x058ae132, 0x0b83ec39, 0x1998fb24, 0x1791f62f,
8455
+ 0x764dd68d, 0x7844db86, 0x6a5fcc9b, 0x6456c190, 0x4e69e2a1, 0x4060efaa,
8456
+ 0x527bf8b7, 0x5c72f5bc, 0x0605bed5, 0x080cb3de, 0x1a17a4c3, 0x141ea9c8,
8457
+ 0x3e218af9, 0x302887f2, 0x223390ef, 0x2c3a9de4, 0x96dd063d, 0x98d40b36,
8458
+ 0x8acf1c2b, 0x84c61120, 0xaef93211, 0xa0f03f1a, 0xb2eb2807, 0xbce2250c,
8459
+ 0xe6956e65, 0xe89c636e, 0xfa877473, 0xf48e7978, 0xdeb15a49, 0xd0b85742,
8460
+ 0xc2a3405f, 0xccaa4d54, 0x41ecdaf7, 0x4fe5d7fc, 0x5dfec0e1, 0x53f7cdea,
8461
+ 0x79c8eedb, 0x77c1e3d0, 0x65daf4cd, 0x6bd3f9c6, 0x31a4b2af, 0x3fadbfa4,
8462
+ 0x2db6a8b9, 0x23bfa5b2, 0x09808683, 0x07898b88, 0x15929c95, 0x1b9b919e,
8463
+ 0xa17c0a47, 0xaf75074c, 0xbd6e1051, 0xb3671d5a, 0x99583e6b, 0x97513360,
8464
+ 0x854a247d, 0x8b432976, 0xd134621f, 0xdf3d6f14, 0xcd267809, 0xc32f7502,
8465
+ 0xe9105633, 0xe7195b38, 0xf5024c25, 0xfb0b412e, 0x9ad7618c, 0x94de6c87,
8466
+ 0x86c57b9a, 0x88cc7691, 0xa2f355a0, 0xacfa58ab, 0xbee14fb6, 0xb0e842bd,
8467
+ 0xea9f09d4, 0xe49604df, 0xf68d13c2, 0xf8841ec9, 0xd2bb3df8, 0xdcb230f3,
8468
+ 0xcea927ee, 0xc0a02ae5, 0x7a47b13c, 0x744ebc37, 0x6655ab2a, 0x685ca621,
8469
+ 0x42638510, 0x4c6a881b, 0x5e719f06, 0x5078920d, 0x0a0fd964, 0x0406d46f,
8470
+ 0x161dc372, 0x1814ce79, 0x322bed48, 0x3c22e043, 0x2e39f75e, 0x2030fa55,
8471
+ 0xec9ab701, 0xe293ba0a, 0xf088ad17, 0xfe81a01c, 0xd4be832d, 0xdab78e26,
8472
+ 0xc8ac993b, 0xc6a59430, 0x9cd2df59, 0x92dbd252, 0x80c0c54f, 0x8ec9c844,
8473
+ 0xa4f6eb75, 0xaaffe67e, 0xb8e4f163, 0xb6edfc68, 0x0c0a67b1, 0x02036aba,
8474
+ 0x10187da7, 0x1e1170ac, 0x342e539d, 0x3a275e96, 0x283c498b, 0x26354480,
8475
+ 0x7c420fe9, 0x724b02e2, 0x605015ff, 0x6e5918f4, 0x44663bc5, 0x4a6f36ce,
8476
+ 0x587421d3, 0x567d2cd8, 0x37a10c7a, 0x39a80171, 0x2bb3166c, 0x25ba1b67,
8477
+ 0x0f853856, 0x018c355d, 0x13972240, 0x1d9e2f4b, 0x47e96422, 0x49e06929,
8478
+ 0x5bfb7e34, 0x55f2733f, 0x7fcd500e, 0x71c45d05, 0x63df4a18, 0x6dd64713,
8479
+ 0xd731dcca, 0xd938d1c1, 0xcb23c6dc, 0xc52acbd7, 0xef15e8e6, 0xe11ce5ed,
8480
+ 0xf307f2f0, 0xfd0efffb, 0xa779b492, 0xa970b999, 0xbb6bae84, 0xb562a38f,
8481
+ 0x9f5d80be, 0x91548db5, 0x834f9aa8, 0x8d4697a3]);
8482
+
8483
+ function expandKey128(cipherKey) {
8484
+ var b = 176, result = new Uint8Array(b);
8485
+ result.set(cipherKey);
8486
+ for (var j = 16, i = 1; j < b; ++i) {
8487
+ // RotWord
8488
+ var t1 = result[j - 3], t2 = result[j - 2],
8489
+ t3 = result[j - 1], t4 = result[j - 4];
8490
+ // SubWord
8491
+ t1 = s[t1];
8492
+ t2 = s[t2];
8493
+ t3 = s[t3];
8494
+ t4 = s[t4];
8495
+ // Rcon
8496
+ t1 = t1 ^ rcon[i];
8497
+ for (var n = 0; n < 4; ++n) {
8498
+ result[j] = (t1 ^= result[j - 16]);
8499
+ j++;
8500
+ result[j] = (t2 ^= result[j - 16]);
8501
+ j++;
8502
+ result[j] = (t3 ^= result[j - 16]);
8503
+ j++;
8504
+ result[j] = (t4 ^= result[j - 16]);
8505
+ j++;
8506
+ }
8507
+ }
8508
+ return result;
8509
+ }
8510
+
8511
+ function decrypt128(input, key) {
8512
+ var state = new Uint8Array(16);
8513
+ state.set(input);
8514
+ var i, j, k;
8515
+ var t, u, v;
8516
+ // AddRoundKey
8517
+ for (j = 0, k = 160; j < 16; ++j, ++k) {
8518
+ state[j] ^= key[k];
8519
+ }
8520
+ for (i = 9; i >= 1; --i) {
8521
+ // InvShiftRows
8522
+ t = state[13];
8523
+ state[13] = state[9];
8524
+ state[9] = state[5];
8525
+ state[5] = state[1];
8526
+ state[1] = t;
8527
+ t = state[14];
8528
+ u = state[10];
8529
+ state[14] = state[6];
8530
+ state[10] = state[2];
8531
+ state[6] = t;
8532
+ state[2] = u;
8533
+ t = state[15];
8534
+ u = state[11];
8535
+ v = state[7];
8536
+ state[15] = state[3];
8537
+ state[11] = t;
8538
+ state[7] = u;
8539
+ state[3] = v;
8540
+ // InvSubBytes
8541
+ for (j = 0; j < 16; ++j) {
8542
+ state[j] = inv_s[state[j]];
8543
+ }
8544
+ // AddRoundKey
8545
+ for (j = 0, k = i * 16; j < 16; ++j, ++k) {
8546
+ state[j] ^= key[k];
8547
+ }
8548
+ // InvMixColumns
8549
+ for (j = 0; j < 16; j += 4) {
8550
+ var s0 = mix[state[j]], s1 = mix[state[j + 1]],
8551
+ s2 = mix[state[j + 2]], s3 = mix[state[j + 3]];
8552
+ t = (s0 ^ (s1 >>> 8) ^ (s1 << 24) ^ (s2 >>> 16) ^ (s2 << 16) ^
8553
+ (s3 >>> 24) ^ (s3 << 8));
8554
+ state[j] = (t >>> 24) & 0xFF;
8555
+ state[j + 1] = (t >> 16) & 0xFF;
8556
+ state[j + 2] = (t >> 8) & 0xFF;
8557
+ state[j + 3] = t & 0xFF;
8558
+ }
8559
+ }
8560
+ // InvShiftRows
8561
+ t = state[13];
8562
+ state[13] = state[9];
8563
+ state[9] = state[5];
8564
+ state[5] = state[1];
8565
+ state[1] = t;
8566
+ t = state[14];
8567
+ u = state[10];
8568
+ state[14] = state[6];
8569
+ state[10] = state[2];
8570
+ state[6] = t;
8571
+ state[2] = u;
8572
+ t = state[15];
8573
+ u = state[11];
8574
+ v = state[7];
8575
+ state[15] = state[3];
8576
+ state[11] = t;
8577
+ state[7] = u;
8578
+ state[3] = v;
8579
+ for (j = 0; j < 16; ++j) {
8580
+ // InvSubBytes
8581
+ state[j] = inv_s[state[j]];
8582
+ // AddRoundKey
8583
+ state[j] ^= key[j];
8584
+ }
8585
+ return state;
8586
+ }
8587
+
8588
+ function encrypt128(input, key) {
8589
+ var t, u, v, k;
8590
+ var state = new Uint8Array(16);
8591
+ state.set(input);
8592
+ for (j = 0; j < 16; ++j) {
8593
+ // AddRoundKey
8594
+ state[j] ^= key[j];
8595
+ }
8596
+
8597
+ for (i = 1; i < 10; i++) {
8598
+ //SubBytes
8599
+ for (j = 0; j < 16; ++j) {
8600
+ state[j] = s[state[j]];
8601
+ }
8602
+ //ShiftRows
8603
+ v = state[1];
8604
+ state[1] = state[5];
8605
+ state[5] = state[9];
8606
+ state[9] = state[13];
8607
+ state[13] = v;
8608
+ v = state[2];
8609
+ u = state[6];
8610
+ state[2] = state[10];
8611
+ state[6] = state[14];
8612
+ state[10] = v;
8613
+ state[14] = u;
8614
+ v = state[3];
8615
+ u = state[7];
8616
+ t = state[11];
8617
+ state[3] = state[15];
8618
+ state[7] = v;
8619
+ state[11] = u;
8620
+ state[15] = t;
8621
+ //MixColumns
8622
+ for (var j = 0; j < 16; j += 4) {
8623
+ var s0 = state[j + 0], s1 = state[j + 1];
8624
+ var s2 = state[j + 2], s3 = state[j + 3];
8625
+ t = s0 ^ s1 ^ s2 ^ s3;
8626
+ state[j + 0] ^= t ^ mixCol[s0 ^ s1];
8627
+ state[j + 1] ^= t ^ mixCol[s1 ^ s2];
8628
+ state[j + 2] ^= t ^ mixCol[s2 ^ s3];
8629
+ state[j + 3] ^= t ^ mixCol[s3 ^ s0];
8630
+ }
8631
+ //AddRoundKey
8632
+ for (j = 0, k = i * 16; j < 16; ++j, ++k) {
8633
+ state[j] ^= key[k];
8634
+ }
8635
+ }
8636
+
8637
+ //SubBytes
8638
+ for (j = 0; j < 16; ++j) {
8639
+ state[j] = s[state[j]];
8640
+ }
8641
+ //ShiftRows
8642
+ v = state[1];
8643
+ state[1] = state[5];
8644
+ state[5] = state[9];
8645
+ state[9] = state[13];
8646
+ state[13] = v;
8647
+ v = state[2];
8648
+ u = state[6];
8649
+ state[2] = state[10];
8650
+ state[6] = state[14];
8651
+ state[10] = v;
8652
+ state[14] = u;
8653
+ v = state[3];
8654
+ u = state[7];
8655
+ t = state[11];
8656
+ state[3] = state[15];
8657
+ state[7] = v;
8658
+ state[11] = u;
8659
+ state[15] = t;
8660
+ //AddRoundKey
8661
+ for (j = 0, k = 160; j < 16; ++j, ++k) {
8662
+ state[j] ^= key[k];
8663
+ }
8664
+ return state;
8665
+ }
8666
+
8667
+ function AES128Cipher(key) {
8668
+ this.key = expandKey128(key);
8669
+ this.buffer = new Uint8Array(16);
8670
+ this.bufferPosition = 0;
8671
+ }
8672
+
8673
+ function decryptBlock2(data, finalize) {
8674
+ var i, j, ii, sourceLength = data.length,
8675
+ buffer = this.buffer, bufferLength = this.bufferPosition,
8676
+ result = [], iv = this.iv;
8677
+ for (i = 0; i < sourceLength; ++i) {
8678
+ buffer[bufferLength] = data[i];
8679
+ ++bufferLength;
8680
+ if (bufferLength < 16) {
8681
+ continue;
8682
+ }
8683
+ // buffer is full, decrypting
8684
+ var plain = decrypt128(buffer, this.key);
8685
+ // xor-ing the IV vector to get plain text
8686
+ for (j = 0; j < 16; ++j) {
8687
+ plain[j] ^= iv[j];
8688
+ }
8689
+ iv = buffer;
8690
+ result.push(plain);
8691
+ buffer = new Uint8Array(16);
8692
+ bufferLength = 0;
8693
+ }
8694
+ // saving incomplete buffer
8695
+ this.buffer = buffer;
8696
+ this.bufferLength = bufferLength;
8697
+ this.iv = iv;
8698
+ if (result.length === 0) {
8699
+ return new Uint8Array([]);
8700
+ }
8701
+ // combining plain text blocks into one
8702
+ var outputLength = 16 * result.length;
8703
+ if (finalize) {
8704
+ // undo a padding that is described in RFC 2898
8705
+ var lastBlock = result[result.length - 1];
8706
+ var psLen = lastBlock[15];
8707
+ if (psLen <= 16) {
8708
+ for (i = 15, ii = 16 - psLen; i >= ii; --i) {
8709
+ if (lastBlock[i] !== psLen) {
8710
+ // Invalid padding, assume that the block has no padding.
8711
+ psLen = 0;
8712
+ break;
8713
+ }
8714
+ }
8715
+ outputLength -= psLen;
8716
+ result[result.length - 1] = lastBlock.subarray(0, 16 - psLen);
8717
+ }
8718
+ }
8719
+ var output = new Uint8Array(outputLength);
8720
+ for (i = 0, j = 0, ii = result.length; i < ii; ++i, j += 16) {
8721
+ output.set(result[i], j);
8722
+ }
8723
+ return output;
8724
+ }
8725
+
8726
+ AES128Cipher.prototype = {
8727
+ decryptBlock: function AES128Cipher_decryptBlock(data, finalize) {
8728
+ var i, sourceLength = data.length;
8729
+ var buffer = this.buffer, bufferLength = this.bufferPosition;
8730
+ // waiting for IV values -- they are at the start of the stream
8731
+ for (i = 0; bufferLength < 16 && i < sourceLength; ++i, ++bufferLength) {
8732
+ buffer[bufferLength] = data[i];
8733
+ }
8734
+ if (bufferLength < 16) {
8735
+ // need more data
8736
+ this.bufferLength = bufferLength;
8737
+ return new Uint8Array([]);
8738
+ }
8739
+ this.iv = buffer;
8740
+ this.buffer = new Uint8Array(16);
8741
+ this.bufferLength = 0;
8742
+ // starting decryption
8743
+ this.decryptBlock = decryptBlock2;
8744
+ return this.decryptBlock(data.subarray(16), finalize);
8745
+ },
8746
+ encrypt: function AES128Cipher_encrypt(data, iv) {
8747
+ var i, j, ii, sourceLength = data.length,
8748
+ buffer = this.buffer, bufferLength = this.bufferPosition,
8749
+ result = [];
8750
+ if (!iv) {
8751
+ iv = new Uint8Array(16);
8752
+ }
8753
+ for (i = 0; i < sourceLength; ++i) {
8754
+ buffer[bufferLength] = data[i];
8755
+ ++bufferLength;
8756
+ if (bufferLength < 16) {
8757
+ continue;
8758
+ }
8759
+ for (j = 0; j < 16; ++j) {
8760
+ buffer[j] ^= iv[j];
8761
+ }
8762
+
8763
+ // buffer is full, encrypting
8764
+ var cipher = encrypt128(buffer, this.key);
8765
+ iv = cipher;
8766
+ result.push(cipher);
8767
+ buffer = new Uint8Array(16);
8768
+ bufferLength = 0;
8769
+ }
8770
+ // saving incomplete buffer
8771
+ this.buffer = buffer;
8772
+ this.bufferLength = bufferLength;
8773
+ this.iv = iv;
8774
+ if (result.length === 0) {
8775
+ return new Uint8Array([]);
8776
+ }
8777
+ // combining plain text blocks into one
8778
+ var outputLength = 16 * result.length;
8779
+ var output = new Uint8Array(outputLength);
8780
+ for (i = 0, j = 0, ii = result.length; i < ii; ++i, j += 16) {
8781
+ output.set(result[i], j);
8782
+ }
8783
+ return output;
8784
+ }
8785
+ };
8786
+
8787
+ return AES128Cipher;
8788
+ })();
8789
+
8790
+ var AES256Cipher = (function AES256CipherClosure() {
8791
+ var rcon = new Uint8Array([
8792
+ 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c,
8793
+ 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a,
8794
+ 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
8795
+ 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a,
8796
+ 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
8797
+ 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6,
8798
+ 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72,
8799
+ 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc,
8800
+ 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10,
8801
+ 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e,
8802
+ 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5,
8803
+ 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94,
8804
+ 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02,
8805
+ 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d,
8806
+ 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d,
8807
+ 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f,
8808
+ 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb,
8809
+ 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c,
8810
+ 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a,
8811
+ 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd,
8812
+ 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a,
8813
+ 0x74, 0xe8, 0xcb, 0x8d]);
8814
+
8815
+ var s = new Uint8Array([
8816
+ 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b,
8817
+ 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
8818
+ 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26,
8819
+ 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
8820
+ 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2,
8821
+ 0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0,
8822
+ 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, 0x53, 0xd1, 0x00, 0xed,
8823
+ 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
8824
+ 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f,
8825
+ 0x50, 0x3c, 0x9f, 0xa8, 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
8826
+ 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, 0xcd, 0x0c, 0x13, 0xec,
8827
+ 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
8828
+ 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14,
8829
+ 0xde, 0x5e, 0x0b, 0xdb, 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c,
8830
+ 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, 0xe7, 0xc8, 0x37, 0x6d,
8831
+ 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
8832
+ 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f,
8833
+ 0x4b, 0xbd, 0x8b, 0x8a, 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,
8834
+ 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, 0xe1, 0xf8, 0x98, 0x11,
8835
+ 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
8836
+ 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f,
8837
+ 0xb0, 0x54, 0xbb, 0x16]);
8838
+
8839
+ var inv_s = new Uint8Array([
8840
+ 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e,
8841
+ 0x81, 0xf3, 0xd7, 0xfb, 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87,
8842
+ 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, 0x54, 0x7b, 0x94, 0x32,
8843
+ 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
8844
+ 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49,
8845
+ 0x6d, 0x8b, 0xd1, 0x25, 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16,
8846
+ 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, 0x6c, 0x70, 0x48, 0x50,
8847
+ 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
8848
+ 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05,
8849
+ 0xb8, 0xb3, 0x45, 0x06, 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02,
8850
+ 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, 0x3a, 0x91, 0x11, 0x41,
8851
+ 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
8852
+ 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8,
8853
+ 0x1c, 0x75, 0xdf, 0x6e, 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89,
8854
+ 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, 0xfc, 0x56, 0x3e, 0x4b,
8855
+ 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
8856
+ 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59,
8857
+ 0x27, 0x80, 0xec, 0x5f, 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d,
8858
+ 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, 0xa0, 0xe0, 0x3b, 0x4d,
8859
+ 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
8860
+ 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63,
8861
+ 0x55, 0x21, 0x0c, 0x7d]);
8862
+
8863
+ var mixCol = new Uint8Array(256);
8864
+ for (var i = 0; i < 256; i++) {
8865
+ if (i < 128) {
8866
+ mixCol[i] = i << 1;
8867
+ } else {
8868
+ mixCol[i] = (i << 1) ^ 0x1b;
8869
+ }
8870
+ }
8871
+ var mix = new Uint32Array([
8872
+ 0x00000000, 0x0e090d0b, 0x1c121a16, 0x121b171d, 0x3824342c, 0x362d3927,
8873
+ 0x24362e3a, 0x2a3f2331, 0x70486858, 0x7e416553, 0x6c5a724e, 0x62537f45,
8874
+ 0x486c5c74, 0x4665517f, 0x547e4662, 0x5a774b69, 0xe090d0b0, 0xee99ddbb,
8875
+ 0xfc82caa6, 0xf28bc7ad, 0xd8b4e49c, 0xd6bde997, 0xc4a6fe8a, 0xcaaff381,
8876
+ 0x90d8b8e8, 0x9ed1b5e3, 0x8ccaa2fe, 0x82c3aff5, 0xa8fc8cc4, 0xa6f581cf,
8877
+ 0xb4ee96d2, 0xbae79bd9, 0xdb3bbb7b, 0xd532b670, 0xc729a16d, 0xc920ac66,
8878
+ 0xe31f8f57, 0xed16825c, 0xff0d9541, 0xf104984a, 0xab73d323, 0xa57ade28,
8879
+ 0xb761c935, 0xb968c43e, 0x9357e70f, 0x9d5eea04, 0x8f45fd19, 0x814cf012,
8880
+ 0x3bab6bcb, 0x35a266c0, 0x27b971dd, 0x29b07cd6, 0x038f5fe7, 0x0d8652ec,
8881
+ 0x1f9d45f1, 0x119448fa, 0x4be30393, 0x45ea0e98, 0x57f11985, 0x59f8148e,
8882
+ 0x73c737bf, 0x7dce3ab4, 0x6fd52da9, 0x61dc20a2, 0xad766df6, 0xa37f60fd,
8883
+ 0xb16477e0, 0xbf6d7aeb, 0x955259da, 0x9b5b54d1, 0x894043cc, 0x87494ec7,
8884
+ 0xdd3e05ae, 0xd33708a5, 0xc12c1fb8, 0xcf2512b3, 0xe51a3182, 0xeb133c89,
8885
+ 0xf9082b94, 0xf701269f, 0x4de6bd46, 0x43efb04d, 0x51f4a750, 0x5ffdaa5b,
8886
+ 0x75c2896a, 0x7bcb8461, 0x69d0937c, 0x67d99e77, 0x3daed51e, 0x33a7d815,
8887
+ 0x21bccf08, 0x2fb5c203, 0x058ae132, 0x0b83ec39, 0x1998fb24, 0x1791f62f,
8888
+ 0x764dd68d, 0x7844db86, 0x6a5fcc9b, 0x6456c190, 0x4e69e2a1, 0x4060efaa,
8889
+ 0x527bf8b7, 0x5c72f5bc, 0x0605bed5, 0x080cb3de, 0x1a17a4c3, 0x141ea9c8,
8890
+ 0x3e218af9, 0x302887f2, 0x223390ef, 0x2c3a9de4, 0x96dd063d, 0x98d40b36,
8891
+ 0x8acf1c2b, 0x84c61120, 0xaef93211, 0xa0f03f1a, 0xb2eb2807, 0xbce2250c,
8892
+ 0xe6956e65, 0xe89c636e, 0xfa877473, 0xf48e7978, 0xdeb15a49, 0xd0b85742,
8893
+ 0xc2a3405f, 0xccaa4d54, 0x41ecdaf7, 0x4fe5d7fc, 0x5dfec0e1, 0x53f7cdea,
8894
+ 0x79c8eedb, 0x77c1e3d0, 0x65daf4cd, 0x6bd3f9c6, 0x31a4b2af, 0x3fadbfa4,
8895
+ 0x2db6a8b9, 0x23bfa5b2, 0x09808683, 0x07898b88, 0x15929c95, 0x1b9b919e,
8896
+ 0xa17c0a47, 0xaf75074c, 0xbd6e1051, 0xb3671d5a, 0x99583e6b, 0x97513360,
8897
+ 0x854a247d, 0x8b432976, 0xd134621f, 0xdf3d6f14, 0xcd267809, 0xc32f7502,
8898
+ 0xe9105633, 0xe7195b38, 0xf5024c25, 0xfb0b412e, 0x9ad7618c, 0x94de6c87,
8899
+ 0x86c57b9a, 0x88cc7691, 0xa2f355a0, 0xacfa58ab, 0xbee14fb6, 0xb0e842bd,
8900
+ 0xea9f09d4, 0xe49604df, 0xf68d13c2, 0xf8841ec9, 0xd2bb3df8, 0xdcb230f3,
8901
+ 0xcea927ee, 0xc0a02ae5, 0x7a47b13c, 0x744ebc37, 0x6655ab2a, 0x685ca621,
8902
+ 0x42638510, 0x4c6a881b, 0x5e719f06, 0x5078920d, 0x0a0fd964, 0x0406d46f,
8903
+ 0x161dc372, 0x1814ce79, 0x322bed48, 0x3c22e043, 0x2e39f75e, 0x2030fa55,
8904
+ 0xec9ab701, 0xe293ba0a, 0xf088ad17, 0xfe81a01c, 0xd4be832d, 0xdab78e26,
8905
+ 0xc8ac993b, 0xc6a59430, 0x9cd2df59, 0x92dbd252, 0x80c0c54f, 0x8ec9c844,
8906
+ 0xa4f6eb75, 0xaaffe67e, 0xb8e4f163, 0xb6edfc68, 0x0c0a67b1, 0x02036aba,
8907
+ 0x10187da7, 0x1e1170ac, 0x342e539d, 0x3a275e96, 0x283c498b, 0x26354480,
8908
+ 0x7c420fe9, 0x724b02e2, 0x605015ff, 0x6e5918f4, 0x44663bc5, 0x4a6f36ce,
8909
+ 0x587421d3, 0x567d2cd8, 0x37a10c7a, 0x39a80171, 0x2bb3166c, 0x25ba1b67,
8910
+ 0x0f853856, 0x018c355d, 0x13972240, 0x1d9e2f4b, 0x47e96422, 0x49e06929,
8911
+ 0x5bfb7e34, 0x55f2733f, 0x7fcd500e, 0x71c45d05, 0x63df4a18, 0x6dd64713,
8912
+ 0xd731dcca, 0xd938d1c1, 0xcb23c6dc, 0xc52acbd7, 0xef15e8e6, 0xe11ce5ed,
8913
+ 0xf307f2f0, 0xfd0efffb, 0xa779b492, 0xa970b999, 0xbb6bae84, 0xb562a38f,
8914
+ 0x9f5d80be, 0x91548db5, 0x834f9aa8, 0x8d4697a3]);
8915
+
8916
+ function expandKey256(cipherKey) {
8917
+ var b = 240, result = new Uint8Array(b);
8918
+ var r = 1;
8919
+
8920
+ result.set(cipherKey);
8921
+ for (var j = 32, i = 1; j < b; ++i) {
8922
+ if (j % 32 === 16) {
8923
+ t1 = s[t1];
8924
+ t2 = s[t2];
8925
+ t3 = s[t3];
8926
+ t4 = s[t4];
8927
+ } else if (j % 32 === 0) {
8928
+ // RotWord
8929
+ var t1 = result[j - 3], t2 = result[j - 2],
8930
+ t3 = result[j - 1], t4 = result[j - 4];
8931
+ // SubWord
8932
+ t1 = s[t1];
8933
+ t2 = s[t2];
8934
+ t3 = s[t3];
8935
+ t4 = s[t4];
8936
+ // Rcon
8937
+ t1 = t1 ^ r;
8938
+ if ((r <<= 1) >= 256) {
8939
+ r = (r ^ 0x1b) & 0xFF;
8940
+ }
8941
+ }
8942
+
8943
+ for (var n = 0; n < 4; ++n) {
8944
+ result[j] = (t1 ^= result[j - 32]);
8945
+ j++;
8946
+ result[j] = (t2 ^= result[j - 32]);
8947
+ j++;
8948
+ result[j] = (t3 ^= result[j - 32]);
8949
+ j++;
8950
+ result[j] = (t4 ^= result[j - 32]);
8951
+ j++;
8952
+ }
8953
+ }
8954
+ return result;
8955
+ }
8956
+
8957
+ function decrypt256(input, key) {
8958
+ var state = new Uint8Array(16);
8959
+ state.set(input);
8960
+ var i, j, k;
8961
+ var t, u, v;
8962
+ // AddRoundKey
8963
+ for (j = 0, k = 224; j < 16; ++j, ++k) {
8964
+ state[j] ^= key[k];
8965
+ }
8966
+ for (i = 13; i >= 1; --i) {
8967
+ // InvShiftRows
8968
+ t = state[13];
8969
+ state[13] = state[9];
8970
+ state[9] = state[5];
8971
+ state[5] = state[1];
8972
+ state[1] = t;
8973
+ t = state[14];
8974
+ u = state[10];
8975
+ state[14] = state[6];
8976
+ state[10] = state[2];
8977
+ state[6] = t;
8978
+ state[2] = u;
8979
+ t = state[15];
8980
+ u = state[11];
8981
+ v = state[7];
8982
+ state[15] = state[3];
8983
+ state[11] = t;
8984
+ state[7] = u;
8985
+ state[3] = v;
8986
+ // InvSubBytes
8987
+ for (j = 0; j < 16; ++j) {
8988
+ state[j] = inv_s[state[j]];
8989
+ }
8990
+ // AddRoundKey
8991
+ for (j = 0, k = i * 16; j < 16; ++j, ++k) {
8992
+ state[j] ^= key[k];
8993
+ }
8994
+ // InvMixColumns
8995
+ for (j = 0; j < 16; j += 4) {
8996
+ var s0 = mix[state[j]], s1 = mix[state[j + 1]],
8997
+ s2 = mix[state[j + 2]], s3 = mix[state[j + 3]];
8998
+ t = (s0 ^ (s1 >>> 8) ^ (s1 << 24) ^ (s2 >>> 16) ^ (s2 << 16) ^
8999
+ (s3 >>> 24) ^ (s3 << 8));
9000
+ state[j] = (t >>> 24) & 0xFF;
9001
+ state[j + 1] = (t >> 16) & 0xFF;
9002
+ state[j + 2] = (t >> 8) & 0xFF;
9003
+ state[j + 3] = t & 0xFF;
9004
+ }
9005
+ }
9006
+ // InvShiftRows
9007
+ t = state[13];
9008
+ state[13] = state[9];
9009
+ state[9] = state[5];
9010
+ state[5] = state[1];
9011
+ state[1] = t;
9012
+ t = state[14];
9013
+ u = state[10];
9014
+ state[14] = state[6];
9015
+ state[10] = state[2];
9016
+ state[6] = t;
9017
+ state[2] = u;
9018
+ t = state[15];
9019
+ u = state[11];
9020
+ v = state[7];
9021
+ state[15] = state[3];
9022
+ state[11] = t;
9023
+ state[7] = u;
9024
+ state[3] = v;
9025
+ for (j = 0; j < 16; ++j) {
9026
+ // InvSubBytes
9027
+ state[j] = inv_s[state[j]];
9028
+ // AddRoundKey
9029
+ state[j] ^= key[j];
9030
+ }
9031
+ return state;
9032
+ }
9033
+
9034
+ function encrypt256(input, key) {
9035
+ var t, u, v, k;
9036
+ var state = new Uint8Array(16);
9037
+ state.set(input);
9038
+ for (j = 0; j < 16; ++j) {
9039
+ // AddRoundKey
9040
+ state[j] ^= key[j];
9041
+ }
9042
+
9043
+ for (i = 1; i < 14; i++) {
9044
+ //SubBytes
9045
+ for (j = 0; j < 16; ++j) {
9046
+ state[j] = s[state[j]];
9047
+ }
9048
+ //ShiftRows
9049
+ v = state[1];
9050
+ state[1] = state[5];
9051
+ state[5] = state[9];
9052
+ state[9] = state[13];
9053
+ state[13] = v;
9054
+ v = state[2];
9055
+ u = state[6];
9056
+ state[2] = state[10];
9057
+ state[6] = state[14];
9058
+ state[10] = v;
9059
+ state[14] = u;
9060
+ v = state[3];
9061
+ u = state[7];
9062
+ t = state[11];
9063
+ state[3] = state[15];
9064
+ state[7] = v;
9065
+ state[11] = u;
9066
+ state[15] = t;
9067
+ //MixColumns
9068
+ for (var j = 0; j < 16; j += 4) {
9069
+ var s0 = state[j + 0], s1 = state[j + 1];
9070
+ var s2 = state[j + 2], s3 = state[j + 3];
9071
+ t = s0 ^ s1 ^ s2 ^ s3;
9072
+ state[j + 0] ^= t ^ mixCol[s0 ^ s1];
9073
+ state[j + 1] ^= t ^ mixCol[s1 ^ s2];
9074
+ state[j + 2] ^= t ^ mixCol[s2 ^ s3];
9075
+ state[j + 3] ^= t ^ mixCol[s3 ^ s0];
9076
+ }
9077
+ //AddRoundKey
9078
+ for (j = 0, k = i * 16; j < 16; ++j, ++k) {
9079
+ state[j] ^= key[k];
9080
+ }
9081
+ }
9082
+
9083
+ //SubBytes
9084
+ for (j = 0; j < 16; ++j) {
9085
+ state[j] = s[state[j]];
9086
+ }
9087
+ //ShiftRows
9088
+ v = state[1];
9089
+ state[1] = state[5];
9090
+ state[5] = state[9];
9091
+ state[9] = state[13];
9092
+ state[13] = v;
9093
+ v = state[2];
9094
+ u = state[6];
9095
+ state[2] = state[10];
9096
+ state[6] = state[14];
9097
+ state[10] = v;
9098
+ state[14] = u;
9099
+ v = state[3];
9100
+ u = state[7];
9101
+ t = state[11];
9102
+ state[3] = state[15];
9103
+ state[7] = v;
9104
+ state[11] = u;
9105
+ state[15] = t;
9106
+ //AddRoundKey
9107
+ for (j = 0, k = 224; j < 16; ++j, ++k) {
9108
+ state[j] ^= key[k];
9109
+ }
9110
+
9111
+ return state;
9112
+
9113
+ }
9114
+
9115
+ function AES256Cipher(key) {
9116
+ this.key = expandKey256(key);
9117
+ this.buffer = new Uint8Array(16);
9118
+ this.bufferPosition = 0;
9119
+ }
9120
+
9121
+ function decryptBlock2(data, finalize) {
9122
+ var i, j, ii, sourceLength = data.length,
9123
+ buffer = this.buffer, bufferLength = this.bufferPosition,
9124
+ result = [], iv = this.iv;
9125
+
9126
+ for (i = 0; i < sourceLength; ++i) {
9127
+ buffer[bufferLength] = data[i];
9128
+ ++bufferLength;
9129
+ if (bufferLength < 16) {
9130
+ continue;
9131
+ }
9132
+ // buffer is full, decrypting
9133
+ var plain = decrypt256(buffer, this.key);
9134
+ // xor-ing the IV vector to get plain text
9135
+ for (j = 0; j < 16; ++j) {
9136
+ plain[j] ^= iv[j];
9137
+ }
9138
+ iv = buffer;
9139
+ result.push(plain);
9140
+ buffer = new Uint8Array(16);
9141
+ bufferLength = 0;
9142
+ }
9143
+ // saving incomplete buffer
9144
+ this.buffer = buffer;
9145
+ this.bufferLength = bufferLength;
9146
+ this.iv = iv;
9147
+ if (result.length === 0) {
9148
+ return new Uint8Array([]);
9149
+ }
9150
+ // combining plain text blocks into one
9151
+ var outputLength = 16 * result.length;
9152
+ if (finalize) {
9153
+ // undo a padding that is described in RFC 2898
9154
+ var lastBlock = result[result.length - 1];
9155
+ var psLen = lastBlock[15];
9156
+ if (psLen <= 16) {
9157
+ for (i = 15, ii = 16 - psLen; i >= ii; --i) {
9158
+ if (lastBlock[i] !== psLen) {
9159
+ // Invalid padding, assume that the block has no padding.
9160
+ psLen = 0;
9161
+ break;
9162
+ }
9163
+ }
9164
+ outputLength -= psLen;
9165
+ result[result.length - 1] = lastBlock.subarray(0, 16 - psLen);
9166
+ }
9167
+ }
9168
+ var output = new Uint8Array(outputLength);
9169
+ for (i = 0, j = 0, ii = result.length; i < ii; ++i, j += 16) {
9170
+ output.set(result[i], j);
9171
+ }
9172
+ return output;
9173
+
9174
+ }
9175
+
9176
+ AES256Cipher.prototype = {
9177
+ decryptBlock: function AES256Cipher_decryptBlock(data, finalize, iv) {
9178
+ var i, sourceLength = data.length;
9179
+ var buffer = this.buffer, bufferLength = this.bufferPosition;
9180
+ // if not supplied an IV wait for IV values
9181
+ // they are at the start of the stream
9182
+ if (iv) {
9183
+ this.iv = iv;
9184
+ } else {
9185
+ for (i = 0; bufferLength < 16 &&
9186
+ i < sourceLength; ++i, ++bufferLength) {
9187
+ buffer[bufferLength] = data[i];
9188
+ }
9189
+ if (bufferLength < 16) {
9190
+ //need more data
9191
+ this.bufferLength = bufferLength;
9192
+ return new Uint8Array([]);
9193
+ }
9194
+ this.iv = buffer;
9195
+ data = data.subarray(16);
9196
+ }
9197
+ this.buffer = new Uint8Array(16);
9198
+ this.bufferLength = 0;
9199
+ // starting decryption
9200
+ this.decryptBlock = decryptBlock2;
9201
+ return this.decryptBlock(data, finalize);
9202
+ },
9203
+ encrypt: function AES256Cipher_encrypt(data, iv) {
9204
+ var i, j, ii, sourceLength = data.length,
9205
+ buffer = this.buffer, bufferLength = this.bufferPosition,
9206
+ result = [];
9207
+ if (!iv) {
9208
+ iv = new Uint8Array(16);
9209
+ }
9210
+ for (i = 0; i < sourceLength; ++i) {
9211
+ buffer[bufferLength] = data[i];
9212
+ ++bufferLength;
9213
+ if (bufferLength < 16) {
9214
+ continue;
9215
+ }
9216
+ for (j = 0; j < 16; ++j) {
9217
+ buffer[j] ^= iv[j];
9218
+ }
9219
+
9220
+ // buffer is full, encrypting
9221
+ var cipher = encrypt256(buffer, this.key);
9222
+ this.iv = cipher;
9223
+ result.push(cipher);
9224
+ buffer = new Uint8Array(16);
9225
+ bufferLength = 0;
9226
+ }
9227
+ // saving incomplete buffer
9228
+ this.buffer = buffer;
9229
+ this.bufferLength = bufferLength;
9230
+ this.iv = iv;
9231
+ if (result.length === 0) {
9232
+ return new Uint8Array([]);
9233
+ }
9234
+ // combining plain text blocks into one
9235
+ var outputLength = 16 * result.length;
9236
+ var output = new Uint8Array(outputLength);
9237
+ for (i = 0, j = 0, ii = result.length; i < ii; ++i, j += 16) {
9238
+ output.set(result[i], j);
9239
+ }
9240
+ return output;
9241
+ }
9242
+ };
9243
+
9244
+ return AES256Cipher;
9245
+ })();
9246
+
9247
+ var PDF17 = (function PDF17Closure() {
9248
+
9249
+ function compareByteArrays(array1, array2) {
9250
+ if (array1.length !== array2.length) {
9251
+ return false;
9252
+ }
9253
+ for (var i = 0; i < array1.length; i++) {
9254
+ if (array1[i] !== array2[i]) {
9255
+ return false;
9256
+ }
9257
+ }
9258
+ return true;
9259
+ }
9260
+
9261
+ function PDF17() {
9262
+ }
9263
+
9264
+ PDF17.prototype = {
9265
+ checkOwnerPassword: function PDF17_checkOwnerPassword(password,
9266
+ ownerValidationSalt,
9267
+ userBytes,
9268
+ ownerPassword) {
9269
+ var hashData = new Uint8Array(password.length + 56);
9270
+ hashData.set(password, 0);
9271
+ hashData.set(ownerValidationSalt, password.length);
9272
+ hashData.set(userBytes, password.length + ownerValidationSalt.length);
9273
+ var result = calculateSHA256(hashData, 0, hashData.length);
9274
+ return compareByteArrays(result, ownerPassword);
9275
+ },
9276
+ checkUserPassword: function PDF17_checkUserPassword(password,
9277
+ userValidationSalt,
9278
+ userPassword) {
9279
+ var hashData = new Uint8Array(password.length + 8);
9280
+ hashData.set(password, 0);
9281
+ hashData.set(userValidationSalt, password.length);
9282
+ var result = calculateSHA256(hashData, 0, hashData.length);
9283
+ return compareByteArrays(result, userPassword);
9284
+ },
9285
+ getOwnerKey: function PDF17_getOwnerKey(password, ownerKeySalt, userBytes,
9286
+ ownerEncryption) {
9287
+ var hashData = new Uint8Array(password.length + 56);
9288
+ hashData.set(password, 0);
9289
+ hashData.set(ownerKeySalt, password.length);
9290
+ hashData.set(userBytes, password.length + ownerKeySalt.length);
9291
+ var key = calculateSHA256(hashData, 0, hashData.length);
9292
+ var cipher = new AES256Cipher(key);
9293
+ return cipher.decryptBlock(ownerEncryption,
9294
+ false,
9295
+ new Uint8Array(16));
9296
+
9297
+ },
9298
+ getUserKey: function PDF17_getUserKey(password, userKeySalt,
9299
+ userEncryption) {
9300
+ var hashData = new Uint8Array(password.length + 8);
9301
+ hashData.set(password, 0);
9302
+ hashData.set(userKeySalt, password.length);
9303
+ //key is the decryption key for the UE string
9304
+ var key = calculateSHA256(hashData, 0, hashData.length);
9305
+ var cipher = new AES256Cipher(key);
9306
+ return cipher.decryptBlock(userEncryption,
9307
+ false,
9308
+ new Uint8Array(16));
9309
+ }
9310
+ };
9311
+ return PDF17;
9312
+ })();
9313
+
9314
+ var PDF20 = (function PDF20Closure() {
9315
+
9316
+ function concatArrays(array1, array2) {
9317
+ var t = new Uint8Array(array1.length + array2.length);
9318
+ t.set(array1, 0);
9319
+ t.set(array2, array1.length);
9320
+ return t;
9321
+ }
9322
+
9323
+ function calculatePDF20Hash(password, input, userBytes) {
9324
+ //This refers to Algorithm 2.B as defined in ISO 32000-2
9325
+ var k = calculateSHA256(input, 0, input.length).subarray(0, 32);
9326
+ var e = [0];
9327
+ var i = 0;
9328
+ while (i < 64 || e[e.length - 1] > i - 32) {
9329
+ var arrayLength = password.length + k.length + userBytes.length;
9330
+
9331
+ var k1 = new Uint8Array(arrayLength * 64);
9332
+ var array = concatArrays(password, k);
9333
+ array = concatArrays(array, userBytes);
9334
+ for (var j = 0, pos = 0; j < 64; j++, pos += arrayLength) {
9335
+ k1.set(array, pos);
9336
+ }
9337
+ //AES128 CBC NO PADDING with
9338
+ //first 16 bytes of k as the key and the second 16 as the iv.
9339
+ var cipher = new AES128Cipher(k.subarray(0, 16));
9340
+ e = cipher.encrypt(k1, k.subarray(16, 32));
9341
+ //Now we have to take the first 16 bytes of an unsigned
9342
+ //big endian integer... and compute the remainder
9343
+ //modulo 3.... That is a fairly large number and
9344
+ //JavaScript isn't going to handle that well...
9345
+ //So we're using a trick that allows us to perform
9346
+ //modulo math byte by byte
9347
+ var remainder = 0;
9348
+ for (var z = 0; z < 16; z++) {
9349
+ remainder *= (256 % 3);
9350
+ remainder %= 3;
9351
+ remainder += ((e[z] >>> 0) % 3);
9352
+ remainder %= 3;
9353
+ }
9354
+ if (remainder === 0) {
9355
+ k = calculateSHA256(e, 0, e.length);
9356
+ }
9357
+ else if (remainder === 1) {
9358
+ k = calculateSHA384(e, 0, e.length);
9359
+ }
9360
+ else if (remainder === 2) {
9361
+ k = calculateSHA512(e, 0, e.length);
9362
+ }
9363
+ i++;
9364
+ }
9365
+ return k.subarray(0, 32);
9366
+ }
9367
+
9368
+ function PDF20() {
9369
+ }
9370
+
9371
+ function compareByteArrays(array1, array2) {
9372
+ if (array1.length !== array2.length) {
9373
+ return false;
9374
+ }
9375
+ for (var i = 0; i < array1.length; i++) {
9376
+ if (array1[i] !== array2[i]) {
9377
+ return false;
9378
+ }
9379
+ }
9380
+ return true;
9381
+ }
9382
+
9383
+ PDF20.prototype = {
9384
+ hash: function PDF20_hash(password, concatBytes, userBytes) {
9385
+ return calculatePDF20Hash(password, concatBytes, userBytes);
9386
+ },
9387
+ checkOwnerPassword: function PDF20_checkOwnerPassword(password,
9388
+ ownerValidationSalt,
9389
+ userBytes,
9390
+ ownerPassword) {
9391
+ var hashData = new Uint8Array(password.length + 56);
9392
+ hashData.set(password, 0);
9393
+ hashData.set(ownerValidationSalt, password.length);
9394
+ hashData.set(userBytes, password.length + ownerValidationSalt.length);
9395
+ var result = calculatePDF20Hash(password, hashData, userBytes);
9396
+ return compareByteArrays(result, ownerPassword);
9397
+ },
9398
+ checkUserPassword: function PDF20_checkUserPassword(password,
9399
+ userValidationSalt,
9400
+ userPassword) {
9401
+ var hashData = new Uint8Array(password.length + 8);
9402
+ hashData.set(password, 0);
9403
+ hashData.set(userValidationSalt, password.length);
9404
+ var result = calculatePDF20Hash(password, hashData, []);
9405
+ return compareByteArrays(result, userPassword);
9406
+ },
9407
+ getOwnerKey: function PDF20_getOwnerKey(password, ownerKeySalt, userBytes,
9408
+ ownerEncryption) {
9409
+ var hashData = new Uint8Array(password.length + 56);
9410
+ hashData.set(password, 0);
9411
+ hashData.set(ownerKeySalt, password.length);
9412
+ hashData.set(userBytes, password.length + ownerKeySalt.length);
9413
+ var key = calculatePDF20Hash(password, hashData, userBytes);
9414
+ var cipher = new AES256Cipher(key);
9415
+ return cipher.decryptBlock(ownerEncryption,
9416
+ false,
9417
+ new Uint8Array(16));
9418
+
9419
+ },
9420
+ getUserKey: function PDF20_getUserKey(password, userKeySalt,
9421
+ userEncryption) {
9422
+ var hashData = new Uint8Array(password.length + 8);
9423
+ hashData.set(password, 0);
9424
+ hashData.set(userKeySalt, password.length);
9425
+ //key is the decryption key for the UE string
9426
+ var key = calculatePDF20Hash(password, hashData, []);
9427
+ var cipher = new AES256Cipher(key);
9428
+ return cipher.decryptBlock(userEncryption,
9429
+ false,
9430
+ new Uint8Array(16));
9431
+ }
9432
+ };
9433
+ return PDF20;
9434
+ })();
9435
+
9436
+ var CipherTransform = (function CipherTransformClosure() {
9437
+ function CipherTransform(stringCipherConstructor, streamCipherConstructor) {
9438
+ this.stringCipherConstructor = stringCipherConstructor;
9439
+ this.streamCipherConstructor = streamCipherConstructor;
9440
+ }
9441
+
9442
+ CipherTransform.prototype = {
9443
+ createStream: function CipherTransform_createStream(stream, length) {
9444
+ var cipher = new this.streamCipherConstructor();
9445
+ return new DecryptStream(stream, length,
9446
+ function cipherTransformDecryptStream(data, finalize) {
9447
+ return cipher.decryptBlock(data, finalize);
9448
+ }
9449
+ );
9450
+ },
9451
+ decryptString: function CipherTransform_decryptString(s) {
9452
+ var cipher = new this.stringCipherConstructor();
9453
+ var data = stringToBytes(s);
9454
+ data = cipher.decryptBlock(data, true);
9455
+ return bytesToString(data);
9456
+ }
9457
+ };
9458
+ return CipherTransform;
9459
+ })();
9460
+
9461
+ var CipherTransformFactory = (function CipherTransformFactoryClosure() {
9462
+ var defaultPasswordBytes = new Uint8Array([
9463
+ 0x28, 0xBF, 0x4E, 0x5E, 0x4E, 0x75, 0x8A, 0x41,
9464
+ 0x64, 0x00, 0x4E, 0x56, 0xFF, 0xFA, 0x01, 0x08,
9465
+ 0x2E, 0x2E, 0x00, 0xB6, 0xD0, 0x68, 0x3E, 0x80,
9466
+ 0x2F, 0x0C, 0xA9, 0xFE, 0x64, 0x53, 0x69, 0x7A]);
9467
+
9468
+ function createEncryptionKey20(revision, password, ownerPassword,
9469
+ ownerValidationSalt, ownerKeySalt, uBytes,
9470
+ userPassword, userValidationSalt, userKeySalt,
9471
+ ownerEncryption, userEncryption, perms) {
9472
+ if (password) {
9473
+ var passwordLength = Math.min(127, password.length);
9474
+ password = password.subarray(0, passwordLength);
9475
+ } else {
9476
+ password = [];
9477
+ }
9478
+ var pdfAlgorithm;
9479
+ if (revision === 6) {
9480
+ pdfAlgorithm = new PDF20();
9481
+ } else {
9482
+ pdfAlgorithm = new PDF17();
9483
+ }
9484
+
9485
+ if (pdfAlgorithm) {
9486
+ if (pdfAlgorithm.checkUserPassword(password, userValidationSalt,
9487
+ userPassword)) {
9488
+ return pdfAlgorithm.getUserKey(password, userKeySalt, userEncryption);
9489
+ } else if (pdfAlgorithm.checkOwnerPassword(password, ownerValidationSalt,
9490
+ uBytes,
9491
+ ownerPassword)) {
9492
+ return pdfAlgorithm.getOwnerKey(password, ownerKeySalt, uBytes,
9493
+ ownerEncryption);
9494
+ }
9495
+ }
9496
+
9497
+ return null;
9498
+ }
9499
+
9500
+ function prepareKeyData(fileId, password, ownerPassword, userPassword,
9501
+ flags, revision, keyLength, encryptMetadata) {
9502
+ var hashDataSize = 40 + ownerPassword.length + fileId.length;
9503
+ var hashData = new Uint8Array(hashDataSize), i = 0, j, n;
9504
+ if (password) {
9505
+ n = Math.min(32, password.length);
9506
+ for (; i < n; ++i) {
9507
+ hashData[i] = password[i];
9508
+ }
9509
+ }
9510
+ j = 0;
9511
+ while (i < 32) {
9512
+ hashData[i++] = defaultPasswordBytes[j++];
9513
+ }
9514
+ // as now the padded password in the hashData[0..i]
9515
+ for (j = 0, n = ownerPassword.length; j < n; ++j) {
9516
+ hashData[i++] = ownerPassword[j];
9517
+ }
9518
+ hashData[i++] = flags & 0xFF;
9519
+ hashData[i++] = (flags >> 8) & 0xFF;
9520
+ hashData[i++] = (flags >> 16) & 0xFF;
9521
+ hashData[i++] = (flags >>> 24) & 0xFF;
9522
+ for (j = 0, n = fileId.length; j < n; ++j) {
9523
+ hashData[i++] = fileId[j];
9524
+ }
9525
+ if (revision >= 4 && !encryptMetadata) {
9526
+ hashData[i++] = 0xFF;
9527
+ hashData[i++] = 0xFF;
9528
+ hashData[i++] = 0xFF;
9529
+ hashData[i++] = 0xFF;
9530
+ }
9531
+ var hash = calculateMD5(hashData, 0, i);
9532
+ var keyLengthInBytes = keyLength >> 3;
9533
+ if (revision >= 3) {
9534
+ for (j = 0; j < 50; ++j) {
9535
+ hash = calculateMD5(hash, 0, keyLengthInBytes);
9536
+ }
9537
+ }
9538
+ var encryptionKey = hash.subarray(0, keyLengthInBytes);
9539
+ var cipher, checkData;
9540
+
9541
+ if (revision >= 3) {
9542
+ for (i = 0; i < 32; ++i) {
9543
+ hashData[i] = defaultPasswordBytes[i];
9544
+ }
9545
+ for (j = 0, n = fileId.length; j < n; ++j) {
9546
+ hashData[i++] = fileId[j];
9547
+ }
9548
+ cipher = new ARCFourCipher(encryptionKey);
9549
+ checkData = cipher.encryptBlock(calculateMD5(hashData, 0, i));
9550
+ n = encryptionKey.length;
9551
+ var derivedKey = new Uint8Array(n), k;
9552
+ for (j = 1; j <= 19; ++j) {
9553
+ for (k = 0; k < n; ++k) {
9554
+ derivedKey[k] = encryptionKey[k] ^ j;
9555
+ }
9556
+ cipher = new ARCFourCipher(derivedKey);
9557
+ checkData = cipher.encryptBlock(checkData);
9558
+ }
9559
+ for (j = 0, n = checkData.length; j < n; ++j) {
9560
+ if (userPassword[j] !== checkData[j]) {
9561
+ return null;
9562
+ }
9563
+ }
9564
+ } else {
9565
+ cipher = new ARCFourCipher(encryptionKey);
9566
+ checkData = cipher.encryptBlock(defaultPasswordBytes);
9567
+ for (j = 0, n = checkData.length; j < n; ++j) {
9568
+ if (userPassword[j] !== checkData[j]) {
9569
+ return null;
9570
+ }
9571
+ }
9572
+ }
9573
+ return encryptionKey;
9574
+ }
9575
+
9576
+ function decodeUserPassword(password, ownerPassword, revision, keyLength) {
9577
+ var hashData = new Uint8Array(32), i = 0, j, n;
9578
+ n = Math.min(32, password.length);
9579
+ for (; i < n; ++i) {
9580
+ hashData[i] = password[i];
9581
+ }
9582
+ j = 0;
9583
+ while (i < 32) {
9584
+ hashData[i++] = defaultPasswordBytes[j++];
9585
+ }
9586
+ var hash = calculateMD5(hashData, 0, i);
9587
+ var keyLengthInBytes = keyLength >> 3;
9588
+ if (revision >= 3) {
9589
+ for (j = 0; j < 50; ++j) {
9590
+ hash = calculateMD5(hash, 0, hash.length);
9591
+ }
9592
+ }
9593
+
9594
+ var cipher, userPassword;
9595
+ if (revision >= 3) {
9596
+ userPassword = ownerPassword;
9597
+ var derivedKey = new Uint8Array(keyLengthInBytes), k;
9598
+ for (j = 19; j >= 0; j--) {
9599
+ for (k = 0; k < keyLengthInBytes; ++k) {
9600
+ derivedKey[k] = hash[k] ^ j;
9601
+ }
9602
+ cipher = new ARCFourCipher(derivedKey);
9603
+ userPassword = cipher.encryptBlock(userPassword);
9604
+ }
9605
+ } else {
9606
+ cipher = new ARCFourCipher(hash.subarray(0, keyLengthInBytes));
9607
+ userPassword = cipher.encryptBlock(ownerPassword);
9608
+ }
9609
+ return userPassword;
9610
+ }
9611
+
9612
+ var identityName = Name.get('Identity');
9613
+
9614
+ function CipherTransformFactory(dict, fileId, password) {
9615
+ var filter = dict.get('Filter');
9616
+ if (!isName(filter) || filter.name !== 'Standard') {
9617
+ error('unknown encryption method');
9618
+ }
9619
+ this.dict = dict;
9620
+ var algorithm = dict.get('V');
9621
+ if (!isInt(algorithm) ||
9622
+ (algorithm !== 1 && algorithm !== 2 && algorithm !== 4 &&
9623
+ algorithm !== 5)) {
9624
+ error('unsupported encryption algorithm');
9625
+ }
9626
+ this.algorithm = algorithm;
9627
+ var keyLength = dict.get('Length') || 40;
9628
+ if (!isInt(keyLength) ||
9629
+ keyLength < 40 || (keyLength % 8) !== 0) {
9630
+ error('invalid key length');
9631
+ }
9632
+
9633
+ // prepare keys
9634
+ var ownerPassword = stringToBytes(dict.get('O')).subarray(0, 32);
9635
+ var userPassword = stringToBytes(dict.get('U')).subarray(0, 32);
9636
+ var flags = dict.get('P');
9637
+ var revision = dict.get('R');
9638
+ // meaningful when V is 4 or 5
9639
+ var encryptMetadata = ((algorithm === 4 || algorithm === 5) &&
9640
+ dict.get('EncryptMetadata') !== false);
9641
+ this.encryptMetadata = encryptMetadata;
9642
+
9643
+ var fileIdBytes = stringToBytes(fileId);
9644
+ var passwordBytes;
9645
+ if (password) {
9646
+ passwordBytes = stringToBytes(password);
9647
+ }
9648
+
9649
+ var encryptionKey;
9650
+ if (algorithm !== 5) {
9651
+ encryptionKey = prepareKeyData(fileIdBytes, passwordBytes,
9652
+ ownerPassword, userPassword, flags,
9653
+ revision, keyLength, encryptMetadata);
9654
+ }
9655
+ else {
9656
+ var ownerValidationSalt = stringToBytes(dict.get('O')).subarray(32, 40);
9657
+ var ownerKeySalt = stringToBytes(dict.get('O')).subarray(40, 48);
9658
+ var uBytes = stringToBytes(dict.get('U')).subarray(0, 48);
9659
+ var userValidationSalt = stringToBytes(dict.get('U')).subarray(32, 40);
9660
+ var userKeySalt = stringToBytes(dict.get('U')).subarray(40, 48);
9661
+ var ownerEncryption = stringToBytes(dict.get('OE'));
9662
+ var userEncryption = stringToBytes(dict.get('UE'));
9663
+ var perms = stringToBytes(dict.get('Perms'));
9664
+ encryptionKey =
9665
+ createEncryptionKey20(revision, passwordBytes,
9666
+ ownerPassword, ownerValidationSalt,
9667
+ ownerKeySalt, uBytes,
9668
+ userPassword, userValidationSalt,
9669
+ userKeySalt, ownerEncryption,
9670
+ userEncryption, perms);
9671
+ }
9672
+ if (!encryptionKey && !password) {
9673
+ throw new PasswordException('No password given',
9674
+ PasswordResponses.NEED_PASSWORD);
9675
+ } else if (!encryptionKey && password) {
9676
+ // Attempting use the password as an owner password
9677
+ var decodedPassword = decodeUserPassword(passwordBytes, ownerPassword,
9678
+ revision, keyLength);
9679
+ encryptionKey = prepareKeyData(fileIdBytes, decodedPassword,
9680
+ ownerPassword, userPassword, flags,
9681
+ revision, keyLength, encryptMetadata);
9682
+ }
9683
+
9684
+ if (!encryptionKey) {
9685
+ throw new PasswordException('Incorrect Password',
9686
+ PasswordResponses.INCORRECT_PASSWORD);
9687
+ }
9688
+
9689
+ this.encryptionKey = encryptionKey;
9690
+
9691
+ if (algorithm >= 4) {
9692
+ this.cf = dict.get('CF');
9693
+ this.stmf = dict.get('StmF') || identityName;
9694
+ this.strf = dict.get('StrF') || identityName;
9695
+ this.eff = dict.get('EFF') || this.stmf;
9696
+ }
9697
+ }
9698
+
9699
+ function buildObjectKey(num, gen, encryptionKey, isAes) {
9700
+ var key = new Uint8Array(encryptionKey.length + 9), i, n;
9701
+ for (i = 0, n = encryptionKey.length; i < n; ++i) {
9702
+ key[i] = encryptionKey[i];
9703
+ }
9704
+ key[i++] = num & 0xFF;
9705
+ key[i++] = (num >> 8) & 0xFF;
9706
+ key[i++] = (num >> 16) & 0xFF;
9707
+ key[i++] = gen & 0xFF;
9708
+ key[i++] = (gen >> 8) & 0xFF;
9709
+ if (isAes) {
9710
+ key[i++] = 0x73;
9711
+ key[i++] = 0x41;
9712
+ key[i++] = 0x6C;
9713
+ key[i++] = 0x54;
9714
+ }
9715
+ var hash = calculateMD5(key, 0, i);
9716
+ return hash.subarray(0, Math.min(encryptionKey.length + 5, 16));
9717
+ }
9718
+
9719
+ function buildCipherConstructor(cf, name, num, gen, key) {
9720
+ var cryptFilter = cf.get(name.name);
9721
+ var cfm;
9722
+ if (cryptFilter !== null && cryptFilter !== undefined) {
9723
+ cfm = cryptFilter.get('CFM');
9724
+ }
9725
+ if (!cfm || cfm.name === 'None') {
9726
+ return function cipherTransformFactoryBuildCipherConstructorNone() {
9727
+ return new NullCipher();
9728
+ };
9729
+ }
9730
+ if ('V2' === cfm.name) {
9731
+ return function cipherTransformFactoryBuildCipherConstructorV2() {
9732
+ return new ARCFourCipher(buildObjectKey(num, gen, key, false));
9733
+ };
9734
+ }
9735
+ if ('AESV2' === cfm.name) {
9736
+ return function cipherTransformFactoryBuildCipherConstructorAESV2() {
9737
+ return new AES128Cipher(buildObjectKey(num, gen, key, true));
9738
+ };
9739
+ }
9740
+ if ('AESV3' === cfm.name) {
9741
+ return function cipherTransformFactoryBuildCipherConstructorAESV3() {
9742
+ return new AES256Cipher(key);
9743
+ };
9744
+ }
9745
+ error('Unknown crypto method');
9746
+ }
9747
+
9748
+ CipherTransformFactory.prototype = {
9749
+ createCipherTransform:
9750
+ function CipherTransformFactory_createCipherTransform(num, gen) {
9751
+ if (this.algorithm === 4 || this.algorithm === 5) {
9752
+ return new CipherTransform(
9753
+ buildCipherConstructor(this.cf, this.stmf,
9754
+ num, gen, this.encryptionKey),
9755
+ buildCipherConstructor(this.cf, this.strf,
9756
+ num, gen, this.encryptionKey));
9757
+ }
9758
+ // algorithms 1 and 2
9759
+ var key = buildObjectKey(num, gen, this.encryptionKey, false);
9760
+ var cipherConstructor = function buildCipherCipherConstructor() {
9761
+ return new ARCFourCipher(key);
9762
+ };
9763
+ return new CipherTransform(cipherConstructor, cipherConstructor);
9764
+ }
9765
+ };
9766
+
9767
+ return CipherTransformFactory;
9768
+ })();
9769
+
9770
+
9771
+ var PatternType = {
9772
+ FUNCTION_BASED: 1,
9773
+ AXIAL: 2,
9774
+ RADIAL: 3,
9775
+ FREE_FORM_MESH: 4,
9776
+ LATTICE_FORM_MESH: 5,
9777
+ COONS_PATCH_MESH: 6,
9778
+ TENSOR_PATCH_MESH: 7
9779
+ };
9780
+
9781
+ var Pattern = (function PatternClosure() {
9782
+ // Constructor should define this.getPattern
9783
+ function Pattern() {
9784
+ error('should not call Pattern constructor');
9785
+ }
9786
+
9787
+ Pattern.prototype = {
9788
+ // Input: current Canvas context
9789
+ // Output: the appropriate fillStyle or strokeStyle
9790
+ getPattern: function Pattern_getPattern(ctx) {
9791
+ error('Should not call Pattern.getStyle: ' + ctx);
9792
+ }
9793
+ };
9794
+
9795
+ Pattern.parseShading = function Pattern_parseShading(shading, matrix, xref,
9796
+ res) {
9797
+
9798
+ var dict = isStream(shading) ? shading.dict : shading;
9799
+ var type = dict.get('ShadingType');
9800
+
9801
+ try {
9802
+ switch (type) {
9803
+ case PatternType.AXIAL:
9804
+ case PatternType.RADIAL:
9805
+ // Both radial and axial shadings are handled by RadialAxial shading.
9806
+ return new Shadings.RadialAxial(dict, matrix, xref, res);
9807
+ case PatternType.FREE_FORM_MESH:
9808
+ case PatternType.LATTICE_FORM_MESH:
9809
+ case PatternType.COONS_PATCH_MESH:
9810
+ case PatternType.TENSOR_PATCH_MESH:
9811
+ return new Shadings.Mesh(shading, matrix, xref, res);
9812
+ default:
9813
+ throw new Error('Unknown PatternType: ' + type);
9814
+ }
9815
+ } catch (ex) {
9816
+ if (ex instanceof MissingDataException) {
9817
+ throw ex;
9818
+ }
9819
+ UnsupportedManager.notify(UNSUPPORTED_FEATURES.shadingPattern);
9820
+ warn(ex);
9821
+ return new Shadings.Dummy();
9822
+ }
9823
+ };
9824
+ return Pattern;
9825
+ })();
9826
+
9827
+ var Shadings = {};
9828
+
9829
+ // A small number to offset the first/last color stops so we can insert ones to
9830
+ // support extend. Number.MIN_VALUE appears to be too small and breaks the
9831
+ // extend. 1e-7 works in FF but chrome seems to use an even smaller sized number
9832
+ // internally so we have to go bigger.
9833
+ Shadings.SMALL_NUMBER = 1e-2;
9834
+
9835
+ // Radial and axial shading have very similar implementations
9836
+ // If needed, the implementations can be broken into two classes
9837
+ Shadings.RadialAxial = (function RadialAxialClosure() {
9838
+ function RadialAxial(dict, matrix, xref, res) {
9839
+ this.matrix = matrix;
9840
+ this.coordsArr = dict.get('Coords');
9841
+ this.shadingType = dict.get('ShadingType');
9842
+ this.type = 'Pattern';
9843
+ var cs = dict.get('ColorSpace', 'CS');
9844
+ cs = ColorSpace.parse(cs, xref, res);
9845
+ this.cs = cs;
9846
+
9847
+ var t0 = 0.0, t1 = 1.0;
9848
+ if (dict.has('Domain')) {
9849
+ var domainArr = dict.get('Domain');
9850
+ t0 = domainArr[0];
9851
+ t1 = domainArr[1];
9852
+ }
9853
+
9854
+ var extendStart = false, extendEnd = false;
9855
+ if (dict.has('Extend')) {
9856
+ var extendArr = dict.get('Extend');
9857
+ extendStart = extendArr[0];
9858
+ extendEnd = extendArr[1];
9859
+ }
9860
+
9861
+ if (this.shadingType === PatternType.RADIAL &&
9862
+ (!extendStart || !extendEnd)) {
9863
+ // Radial gradient only currently works if either circle is fully within
9864
+ // the other circle.
9865
+ var x1 = this.coordsArr[0];
9866
+ var y1 = this.coordsArr[1];
9867
+ var r1 = this.coordsArr[2];
9868
+ var x2 = this.coordsArr[3];
9869
+ var y2 = this.coordsArr[4];
9870
+ var r2 = this.coordsArr[5];
9871
+ var distance = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
9872
+ if (r1 <= r2 + distance &&
9873
+ r2 <= r1 + distance) {
9874
+ warn('Unsupported radial gradient.');
9875
+ }
9876
+ }
9877
+
9878
+ this.extendStart = extendStart;
9879
+ this.extendEnd = extendEnd;
9880
+
9881
+ var fnObj = dict.get('Function');
9882
+ var fn = PDFFunction.parseArray(xref, fnObj);
9883
+
9884
+ // 10 samples seems good enough for now, but probably won't work
9885
+ // if there are sharp color changes. Ideally, we would implement
9886
+ // the spec faithfully and add lossless optimizations.
9887
+ var diff = t1 - t0;
9888
+ var step = diff / 10;
9889
+
9890
+ var colorStops = this.colorStops = [];
9891
+
9892
+ // Protect against bad domains so we don't end up in an infinte loop below.
9893
+ if (t0 >= t1 || step <= 0) {
9894
+ // Acrobat doesn't seem to handle these cases so we'll ignore for
9895
+ // now.
9896
+ info('Bad shading domain.');
9897
+ return;
9898
+ }
9899
+
9900
+ var color = new Float32Array(cs.numComps), ratio = new Float32Array(1);
9901
+ var rgbColor;
9902
+ for (var i = t0; i <= t1; i += step) {
9903
+ ratio[0] = i;
9904
+ fn(ratio, 0, color, 0);
9905
+ rgbColor = cs.getRgb(color, 0);
9906
+ var cssColor = Util.makeCssRgb(rgbColor[0], rgbColor[1], rgbColor[2]);
9907
+ colorStops.push([(i - t0) / diff, cssColor]);
9908
+ }
9909
+
9910
+ var background = 'transparent';
9911
+ if (dict.has('Background')) {
9912
+ rgbColor = cs.getRgb(dict.get('Background'), 0);
9913
+ background = Util.makeCssRgb(rgbColor[0], rgbColor[1], rgbColor[2]);
9914
+ }
9915
+
9916
+ if (!extendStart) {
9917
+ // Insert a color stop at the front and offset the first real color stop
9918
+ // so it doesn't conflict with the one we insert.
9919
+ colorStops.unshift([0, background]);
9920
+ colorStops[1][0] += Shadings.SMALL_NUMBER;
9921
+ }
9922
+ if (!extendEnd) {
9923
+ // Same idea as above in extendStart but for the end.
9924
+ colorStops[colorStops.length - 1][0] -= Shadings.SMALL_NUMBER;
9925
+ colorStops.push([1, background]);
9926
+ }
9927
+
9928
+ this.colorStops = colorStops;
9929
+ }
9930
+
9931
+ RadialAxial.prototype = {
9932
+ getIR: function RadialAxial_getIR() {
9933
+ var coordsArr = this.coordsArr;
9934
+ var shadingType = this.shadingType;
9935
+ var type, p0, p1, r0, r1;
9936
+ if (shadingType === PatternType.AXIAL) {
9937
+ p0 = [coordsArr[0], coordsArr[1]];
9938
+ p1 = [coordsArr[2], coordsArr[3]];
9939
+ r0 = null;
9940
+ r1 = null;
9941
+ type = 'axial';
9942
+ } else if (shadingType === PatternType.RADIAL) {
9943
+ p0 = [coordsArr[0], coordsArr[1]];
9944
+ p1 = [coordsArr[3], coordsArr[4]];
9945
+ r0 = coordsArr[2];
9946
+ r1 = coordsArr[5];
9947
+ type = 'radial';
9948
+ } else {
9949
+ error('getPattern type unknown: ' + shadingType);
9950
+ }
9951
+
9952
+ var matrix = this.matrix;
9953
+ if (matrix) {
9954
+ p0 = Util.applyTransform(p0, matrix);
9955
+ p1 = Util.applyTransform(p1, matrix);
9956
+ }
9957
+
9958
+ return ['RadialAxial', type, this.colorStops, p0, p1, r0, r1];
9959
+ }
9960
+ };
9961
+
9962
+ return RadialAxial;
9963
+ })();
9964
+
9965
+ // All mesh shading. For now, they will be presented as set of the triangles
9966
+ // to be drawn on the canvas and rgb color for each vertex.
9967
+ Shadings.Mesh = (function MeshClosure() {
9968
+ function MeshStreamReader(stream, context) {
9969
+ this.stream = stream;
9970
+ this.context = context;
9971
+ this.buffer = 0;
9972
+ this.bufferLength = 0;
9973
+
9974
+ var numComps = context.numComps;
9975
+ this.tmpCompsBuf = new Float32Array(numComps);
9976
+ var csNumComps = context.colorSpace;
9977
+ this.tmpCsCompsBuf = context.colorFn ? new Float32Array(csNumComps) :
9978
+ this.tmpCompsBuf;
9979
+ }
9980
+ MeshStreamReader.prototype = {
9981
+ get hasData() {
9982
+ if (this.stream.end) {
9983
+ return this.stream.pos < this.stream.end;
9984
+ }
9985
+ if (this.bufferLength > 0) {
9986
+ return true;
9987
+ }
9988
+ var nextByte = this.stream.getByte();
9989
+ if (nextByte < 0) {
9990
+ return false;
9991
+ }
9992
+ this.buffer = nextByte;
9993
+ this.bufferLength = 8;
9994
+ return true;
9995
+ },
9996
+ readBits: function MeshStreamReader_readBits(n) {
9997
+ var buffer = this.buffer;
9998
+ var bufferLength = this.bufferLength;
9999
+ if (n === 32) {
10000
+ if (bufferLength === 0) {
10001
+ return ((this.stream.getByte() << 24) |
10002
+ (this.stream.getByte() << 16) | (this.stream.getByte() << 8) |
10003
+ this.stream.getByte()) >>> 0;
10004
+ }
10005
+ buffer = (buffer << 24) | (this.stream.getByte() << 16) |
10006
+ (this.stream.getByte() << 8) | this.stream.getByte();
10007
+ var nextByte = this.stream.getByte();
10008
+ this.buffer = nextByte & ((1 << bufferLength) - 1);
10009
+ return ((buffer << (8 - bufferLength)) |
10010
+ ((nextByte & 0xFF) >> bufferLength)) >>> 0;
10011
+ }
10012
+ if (n === 8 && bufferLength === 0) {
10013
+ return this.stream.getByte();
10014
+ }
10015
+ while (bufferLength < n) {
10016
+ buffer = (buffer << 8) | this.stream.getByte();
10017
+ bufferLength += 8;
10018
+ }
10019
+ bufferLength -= n;
10020
+ this.bufferLength = bufferLength;
10021
+ this.buffer = buffer & ((1 << bufferLength) - 1);
10022
+ return buffer >> bufferLength;
10023
+ },
10024
+ align: function MeshStreamReader_align() {
10025
+ this.buffer = 0;
10026
+ this.bufferLength = 0;
10027
+ },
10028
+ readFlag: function MeshStreamReader_readFlag() {
10029
+ return this.readBits(this.context.bitsPerFlag);
10030
+ },
10031
+ readCoordinate: function MeshStreamReader_readCoordinate() {
10032
+ var bitsPerCoordinate = this.context.bitsPerCoordinate;
10033
+ var xi = this.readBits(bitsPerCoordinate);
10034
+ var yi = this.readBits(bitsPerCoordinate);
10035
+ var decode = this.context.decode;
10036
+ var scale = bitsPerCoordinate < 32 ? 1 / ((1 << bitsPerCoordinate) - 1) :
10037
+ 2.3283064365386963e-10; // 2 ^ -32
10038
+ return [
10039
+ xi * scale * (decode[1] - decode[0]) + decode[0],
10040
+ yi * scale * (decode[3] - decode[2]) + decode[2]
10041
+ ];
10042
+ },
10043
+ readComponents: function MeshStreamReader_readComponents() {
10044
+ var numComps = this.context.numComps;
10045
+ var bitsPerComponent = this.context.bitsPerComponent;
10046
+ var scale = bitsPerComponent < 32 ? 1 / ((1 << bitsPerComponent) - 1) :
10047
+ 2.3283064365386963e-10; // 2 ^ -32
10048
+ var decode = this.context.decode;
10049
+ var components = this.tmpCompsBuf;
10050
+ for (var i = 0, j = 4; i < numComps; i++, j += 2) {
10051
+ var ci = this.readBits(bitsPerComponent);
10052
+ components[i] = ci * scale * (decode[j + 1] - decode[j]) + decode[j];
10053
+ }
10054
+ var color = this.tmpCsCompsBuf;
10055
+ if (this.context.colorFn) {
10056
+ this.context.colorFn(components, 0, color, 0);
10057
+ }
10058
+ return this.context.colorSpace.getRgb(color, 0);
10059
+ }
10060
+ };
10061
+
10062
+ function decodeType4Shading(mesh, reader) {
10063
+ var coords = mesh.coords;
10064
+ var colors = mesh.colors;
10065
+ var operators = [];
10066
+ var ps = []; // not maintaining cs since that will match ps
10067
+ var verticesLeft = 0; // assuming we have all data to start a new triangle
10068
+ while (reader.hasData) {
10069
+ var f = reader.readFlag();
10070
+ var coord = reader.readCoordinate();
10071
+ var color = reader.readComponents();
10072
+ if (verticesLeft === 0) { // ignoring flags if we started a triangle
10073
+ assert(0 <= f && f <= 2, 'Unknown type4 flag');
10074
+ switch (f) {
10075
+ case 0:
10076
+ verticesLeft = 3;
10077
+ break;
10078
+ case 1:
10079
+ ps.push(ps[ps.length - 2], ps[ps.length - 1]);
10080
+ verticesLeft = 1;
10081
+ break;
10082
+ case 2:
10083
+ ps.push(ps[ps.length - 3], ps[ps.length - 1]);
10084
+ verticesLeft = 1;
10085
+ break;
10086
+ }
10087
+ operators.push(f);
10088
+ }
10089
+ ps.push(coords.length);
10090
+ coords.push(coord);
10091
+ colors.push(color);
10092
+ verticesLeft--;
10093
+
10094
+ reader.align();
10095
+ }
10096
+
10097
+ var psPacked = new Int32Array(ps);
10098
+
10099
+ mesh.figures.push({
10100
+ type: 'triangles',
10101
+ coords: psPacked,
10102
+ colors: psPacked
10103
+ });
10104
+ }
10105
+
10106
+ function decodeType5Shading(mesh, reader, verticesPerRow) {
10107
+ var coords = mesh.coords;
10108
+ var colors = mesh.colors;
10109
+ var ps = []; // not maintaining cs since that will match ps
10110
+ while (reader.hasData) {
10111
+ var coord = reader.readCoordinate();
10112
+ var color = reader.readComponents();
10113
+ ps.push(coords.length);
10114
+ coords.push(coord);
10115
+ colors.push(color);
10116
+ }
10117
+
10118
+ var psPacked = new Int32Array(ps);
10119
+
10120
+ mesh.figures.push({
10121
+ type: 'lattice',
10122
+ coords: psPacked,
10123
+ colors: psPacked,
10124
+ verticesPerRow: verticesPerRow
10125
+ });
10126
+ }
10127
+
10128
+ var MIN_SPLIT_PATCH_CHUNKS_AMOUNT = 3;
10129
+ var MAX_SPLIT_PATCH_CHUNKS_AMOUNT = 20;
10130
+
10131
+ var TRIANGLE_DENSITY = 20; // count of triangles per entire mesh bounds
10132
+
10133
+ var getB = (function getBClosure() {
10134
+ function buildB(count) {
10135
+ var lut = [];
10136
+ for (var i = 0; i <= count; i++) {
10137
+ var t = i / count, t_ = 1 - t;
10138
+ lut.push(new Float32Array([t_ * t_ * t_, 3 * t * t_ * t_,
10139
+ 3 * t * t * t_, t * t * t]));
10140
+ }
10141
+ return lut;
10142
+ }
10143
+ var cache = [];
10144
+ return function getB(count) {
10145
+ if (!cache[count]) {
10146
+ cache[count] = buildB(count);
10147
+ }
10148
+ return cache[count];
10149
+ };
10150
+ })();
10151
+
10152
+ function buildFigureFromPatch(mesh, index) {
10153
+ var figure = mesh.figures[index];
10154
+ assert(figure.type === 'patch', 'Unexpected patch mesh figure');
10155
+
10156
+ var coords = mesh.coords, colors = mesh.colors;
10157
+ var pi = figure.coords;
10158
+ var ci = figure.colors;
10159
+
10160
+ var figureMinX = Math.min(coords[pi[0]][0], coords[pi[3]][0],
10161
+ coords[pi[12]][0], coords[pi[15]][0]);
10162
+ var figureMinY = Math.min(coords[pi[0]][1], coords[pi[3]][1],
10163
+ coords[pi[12]][1], coords[pi[15]][1]);
10164
+ var figureMaxX = Math.max(coords[pi[0]][0], coords[pi[3]][0],
10165
+ coords[pi[12]][0], coords[pi[15]][0]);
10166
+ var figureMaxY = Math.max(coords[pi[0]][1], coords[pi[3]][1],
10167
+ coords[pi[12]][1], coords[pi[15]][1]);
10168
+ var splitXBy = Math.ceil((figureMaxX - figureMinX) * TRIANGLE_DENSITY /
10169
+ (mesh.bounds[2] - mesh.bounds[0]));
10170
+ splitXBy = Math.max(MIN_SPLIT_PATCH_CHUNKS_AMOUNT,
10171
+ Math.min(MAX_SPLIT_PATCH_CHUNKS_AMOUNT, splitXBy));
10172
+ var splitYBy = Math.ceil((figureMaxY - figureMinY) * TRIANGLE_DENSITY /
10173
+ (mesh.bounds[3] - mesh.bounds[1]));
10174
+ splitYBy = Math.max(MIN_SPLIT_PATCH_CHUNKS_AMOUNT,
10175
+ Math.min(MAX_SPLIT_PATCH_CHUNKS_AMOUNT, splitYBy));
10176
+
10177
+ var verticesPerRow = splitXBy + 1;
10178
+ var figureCoords = new Int32Array((splitYBy + 1) * verticesPerRow);
10179
+ var figureColors = new Int32Array((splitYBy + 1) * verticesPerRow);
10180
+ var k = 0;
10181
+ var cl = new Uint8Array(3), cr = new Uint8Array(3);
10182
+ var c0 = colors[ci[0]], c1 = colors[ci[1]],
10183
+ c2 = colors[ci[2]], c3 = colors[ci[3]];
10184
+ var bRow = getB(splitYBy), bCol = getB(splitXBy);
10185
+ for (var row = 0; row <= splitYBy; row++) {
10186
+ cl[0] = ((c0[0] * (splitYBy - row) + c2[0] * row) / splitYBy) | 0;
10187
+ cl[1] = ((c0[1] * (splitYBy - row) + c2[1] * row) / splitYBy) | 0;
10188
+ cl[2] = ((c0[2] * (splitYBy - row) + c2[2] * row) / splitYBy) | 0;
10189
+
10190
+ cr[0] = ((c1[0] * (splitYBy - row) + c3[0] * row) / splitYBy) | 0;
10191
+ cr[1] = ((c1[1] * (splitYBy - row) + c3[1] * row) / splitYBy) | 0;
10192
+ cr[2] = ((c1[2] * (splitYBy - row) + c3[2] * row) / splitYBy) | 0;
10193
+
10194
+ for (var col = 0; col <= splitXBy; col++, k++) {
10195
+ if ((row === 0 || row === splitYBy) &&
10196
+ (col === 0 || col === splitXBy)) {
10197
+ continue;
10198
+ }
10199
+ var x = 0, y = 0;
10200
+ var q = 0;
10201
+ for (var i = 0; i <= 3; i++) {
10202
+ for (var j = 0; j <= 3; j++, q++) {
10203
+ var m = bRow[row][i] * bCol[col][j];
10204
+ x += coords[pi[q]][0] * m;
10205
+ y += coords[pi[q]][1] * m;
10206
+ }
10207
+ }
10208
+ figureCoords[k] = coords.length;
10209
+ coords.push([x, y]);
10210
+ figureColors[k] = colors.length;
10211
+ var newColor = new Uint8Array(3);
10212
+ newColor[0] = ((cl[0] * (splitXBy - col) + cr[0] * col) / splitXBy) | 0;
10213
+ newColor[1] = ((cl[1] * (splitXBy - col) + cr[1] * col) / splitXBy) | 0;
10214
+ newColor[2] = ((cl[2] * (splitXBy - col) + cr[2] * col) / splitXBy) | 0;
10215
+ colors.push(newColor);
10216
+ }
10217
+ }
10218
+ figureCoords[0] = pi[0];
10219
+ figureColors[0] = ci[0];
10220
+ figureCoords[splitXBy] = pi[3];
10221
+ figureColors[splitXBy] = ci[1];
10222
+ figureCoords[verticesPerRow * splitYBy] = pi[12];
10223
+ figureColors[verticesPerRow * splitYBy] = ci[2];
10224
+ figureCoords[verticesPerRow * splitYBy + splitXBy] = pi[15];
10225
+ figureColors[verticesPerRow * splitYBy + splitXBy] = ci[3];
10226
+
10227
+ mesh.figures[index] = {
10228
+ type: 'lattice',
10229
+ coords: figureCoords,
10230
+ colors: figureColors,
10231
+ verticesPerRow: verticesPerRow
10232
+ };
10233
+ }
10234
+
10235
+ function decodeType6Shading(mesh, reader) {
10236
+ // A special case of Type 7. The p11, p12, p21, p22 automatically filled
10237
+ var coords = mesh.coords;
10238
+ var colors = mesh.colors;
10239
+ var ps = new Int32Array(16); // p00, p10, ..., p30, p01, ..., p33
10240
+ var cs = new Int32Array(4); // c00, c30, c03, c33
10241
+ while (reader.hasData) {
10242
+ var f = reader.readFlag();
10243
+ assert(0 <= f && f <= 3, 'Unknown type6 flag');
10244
+ var i, ii;
10245
+ var pi = coords.length;
10246
+ for (i = 0, ii = (f !== 0 ? 8 : 12); i < ii; i++) {
10247
+ coords.push(reader.readCoordinate());
10248
+ }
10249
+ var ci = colors.length;
10250
+ for (i = 0, ii = (f !== 0 ? 2 : 4); i < ii; i++) {
10251
+ colors.push(reader.readComponents());
10252
+ }
10253
+ var tmp1, tmp2, tmp3, tmp4;
10254
+ switch (f) {
10255
+ case 0:
10256
+ ps[12] = pi + 3; ps[13] = pi + 4; ps[14] = pi + 5; ps[15] = pi + 6;
10257
+ ps[ 8] = pi + 2; /* values for 5, 6, 9, 10 are */ ps[11] = pi + 7;
10258
+ ps[ 4] = pi + 1; /* calculated below */ ps[ 7] = pi + 8;
10259
+ ps[ 0] = pi; ps[ 1] = pi + 11; ps[ 2] = pi + 10; ps[ 3] = pi + 9;
10260
+ cs[2] = ci + 1; cs[3] = ci + 2;
10261
+ cs[0] = ci; cs[1] = ci + 3;
10262
+ break;
10263
+ case 1:
10264
+ tmp1 = ps[12]; tmp2 = ps[13]; tmp3 = ps[14]; tmp4 = ps[15];
10265
+ ps[12] = pi + 5; ps[13] = pi + 4; ps[14] = pi + 3; ps[15] = pi + 2;
10266
+ ps[ 8] = pi + 6; /* values for 5, 6, 9, 10 are */ ps[11] = pi + 1;
10267
+ ps[ 4] = pi + 7; /* calculated below */ ps[ 7] = pi;
10268
+ ps[ 0] = tmp1; ps[ 1] = tmp2; ps[ 2] = tmp3; ps[ 3] = tmp4;
10269
+ tmp1 = cs[2]; tmp2 = cs[3];
10270
+ cs[2] = ci + 1; cs[3] = ci;
10271
+ cs[0] = tmp1; cs[1] = tmp2;
10272
+ break;
10273
+ case 2:
10274
+ ps[12] = ps[15]; ps[13] = pi + 7; ps[14] = pi + 6; ps[15] = pi + 5;
10275
+ ps[ 8] = ps[11]; /* values for 5, 6, 9, 10 are */ ps[11] = pi + 4;
10276
+ ps[ 4] = ps[7]; /* calculated below */ ps[ 7] = pi + 3;
10277
+ ps[ 0] = ps[3]; ps[ 1] = pi; ps[ 2] = pi + 1; ps[ 3] = pi + 2;
10278
+ cs[2] = cs[3]; cs[3] = ci + 1;
10279
+ cs[0] = cs[1]; cs[1] = ci;
10280
+ break;
10281
+ case 3:
10282
+ ps[12] = ps[0]; ps[13] = ps[1]; ps[14] = ps[2]; ps[15] = ps[3];
10283
+ ps[ 8] = pi; /* values for 5, 6, 9, 10 are */ ps[11] = pi + 7;
10284
+ ps[ 4] = pi + 1; /* calculated below */ ps[ 7] = pi + 6;
10285
+ ps[ 0] = pi + 2; ps[ 1] = pi + 3; ps[ 2] = pi + 4; ps[ 3] = pi + 5;
10286
+ cs[2] = cs[0]; cs[3] = cs[1];
10287
+ cs[0] = ci; cs[1] = ci + 1;
10288
+ break;
10289
+ }
10290
+ // set p11, p12, p21, p22
10291
+ ps[5] = coords.length;
10292
+ coords.push([
10293
+ (-4 * coords[ps[0]][0] - coords[ps[15]][0] +
10294
+ 6 * (coords[ps[4]][0] + coords[ps[1]][0]) -
10295
+ 2 * (coords[ps[12]][0] + coords[ps[3]][0]) +
10296
+ 3 * (coords[ps[13]][0] + coords[ps[7]][0])) / 9,
10297
+ (-4 * coords[ps[0]][1] - coords[ps[15]][1] +
10298
+ 6 * (coords[ps[4]][1] + coords[ps[1]][1]) -
10299
+ 2 * (coords[ps[12]][1] + coords[ps[3]][1]) +
10300
+ 3 * (coords[ps[13]][1] + coords[ps[7]][1])) / 9
10301
+ ]);
10302
+ ps[6] = coords.length;
10303
+ coords.push([
10304
+ (-4 * coords[ps[3]][0] - coords[ps[12]][0] +
10305
+ 6 * (coords[ps[2]][0] + coords[ps[7]][0]) -
10306
+ 2 * (coords[ps[0]][0] + coords[ps[15]][0]) +
10307
+ 3 * (coords[ps[4]][0] + coords[ps[14]][0])) / 9,
10308
+ (-4 * coords[ps[3]][1] - coords[ps[12]][1] +
10309
+ 6 * (coords[ps[2]][1] + coords[ps[7]][1]) -
10310
+ 2 * (coords[ps[0]][1] + coords[ps[15]][1]) +
10311
+ 3 * (coords[ps[4]][1] + coords[ps[14]][1])) / 9
10312
+ ]);
10313
+ ps[9] = coords.length;
10314
+ coords.push([
10315
+ (-4 * coords[ps[12]][0] - coords[ps[3]][0] +
10316
+ 6 * (coords[ps[8]][0] + coords[ps[13]][0]) -
10317
+ 2 * (coords[ps[0]][0] + coords[ps[15]][0]) +
10318
+ 3 * (coords[ps[11]][0] + coords[ps[1]][0])) / 9,
10319
+ (-4 * coords[ps[12]][1] - coords[ps[3]][1] +
10320
+ 6 * (coords[ps[8]][1] + coords[ps[13]][1]) -
10321
+ 2 * (coords[ps[0]][1] + coords[ps[15]][1]) +
10322
+ 3 * (coords[ps[11]][1] + coords[ps[1]][1])) / 9
10323
+ ]);
10324
+ ps[10] = coords.length;
10325
+ coords.push([
10326
+ (-4 * coords[ps[15]][0] - coords[ps[0]][0] +
10327
+ 6 * (coords[ps[11]][0] + coords[ps[14]][0]) -
10328
+ 2 * (coords[ps[12]][0] + coords[ps[3]][0]) +
10329
+ 3 * (coords[ps[2]][0] + coords[ps[8]][0])) / 9,
10330
+ (-4 * coords[ps[15]][1] - coords[ps[0]][1] +
10331
+ 6 * (coords[ps[11]][1] + coords[ps[14]][1]) -
10332
+ 2 * (coords[ps[12]][1] + coords[ps[3]][1]) +
10333
+ 3 * (coords[ps[2]][1] + coords[ps[8]][1])) / 9
10334
+ ]);
10335
+ mesh.figures.push({
10336
+ type: 'patch',
10337
+ coords: new Int32Array(ps), // making copies of ps and cs
10338
+ colors: new Int32Array(cs)
10339
+ });
10340
+ }
10341
+ }
10342
+
10343
+ function decodeType7Shading(mesh, reader) {
10344
+ var coords = mesh.coords;
10345
+ var colors = mesh.colors;
10346
+ var ps = new Int32Array(16); // p00, p10, ..., p30, p01, ..., p33
10347
+ var cs = new Int32Array(4); // c00, c30, c03, c33
10348
+ while (reader.hasData) {
10349
+ var f = reader.readFlag();
10350
+ assert(0 <= f && f <= 3, 'Unknown type7 flag');
10351
+ var i, ii;
10352
+ var pi = coords.length;
10353
+ for (i = 0, ii = (f !== 0 ? 12 : 16); i < ii; i++) {
10354
+ coords.push(reader.readCoordinate());
10355
+ }
10356
+ var ci = colors.length;
10357
+ for (i = 0, ii = (f !== 0 ? 2 : 4); i < ii; i++) {
10358
+ colors.push(reader.readComponents());
10359
+ }
10360
+ var tmp1, tmp2, tmp3, tmp4;
10361
+ switch (f) {
10362
+ case 0:
10363
+ ps[12] = pi + 3; ps[13] = pi + 4; ps[14] = pi + 5; ps[15] = pi + 6;
10364
+ ps[ 8] = pi + 2; ps[ 9] = pi + 13; ps[10] = pi + 14; ps[11] = pi + 7;
10365
+ ps[ 4] = pi + 1; ps[ 5] = pi + 12; ps[ 6] = pi + 15; ps[ 7] = pi + 8;
10366
+ ps[ 0] = pi; ps[ 1] = pi + 11; ps[ 2] = pi + 10; ps[ 3] = pi + 9;
10367
+ cs[2] = ci + 1; cs[3] = ci + 2;
10368
+ cs[0] = ci; cs[1] = ci + 3;
10369
+ break;
10370
+ case 1:
10371
+ tmp1 = ps[12]; tmp2 = ps[13]; tmp3 = ps[14]; tmp4 = ps[15];
10372
+ ps[12] = pi + 5; ps[13] = pi + 4; ps[14] = pi + 3; ps[15] = pi + 2;
10373
+ ps[ 8] = pi + 6; ps[ 9] = pi + 11; ps[10] = pi + 10; ps[11] = pi + 1;
10374
+ ps[ 4] = pi + 7; ps[ 5] = pi + 8; ps[ 6] = pi + 9; ps[ 7] = pi;
10375
+ ps[ 0] = tmp1; ps[ 1] = tmp2; ps[ 2] = tmp3; ps[ 3] = tmp4;
10376
+ tmp1 = cs[2]; tmp2 = cs[3];
10377
+ cs[2] = ci + 1; cs[3] = ci;
10378
+ cs[0] = tmp1; cs[1] = tmp2;
10379
+ break;
10380
+ case 2:
10381
+ ps[12] = ps[15]; ps[13] = pi + 7; ps[14] = pi + 6; ps[15] = pi + 5;
10382
+ ps[ 8] = ps[11]; ps[ 9] = pi + 8; ps[10] = pi + 11; ps[11] = pi + 4;
10383
+ ps[ 4] = ps[7]; ps[ 5] = pi + 9; ps[ 6] = pi + 10; ps[ 7] = pi + 3;
10384
+ ps[ 0] = ps[3]; ps[ 1] = pi; ps[ 2] = pi + 1; ps[ 3] = pi + 2;
10385
+ cs[2] = cs[3]; cs[3] = ci + 1;
10386
+ cs[0] = cs[1]; cs[1] = ci;
10387
+ break;
10388
+ case 3:
10389
+ ps[12] = ps[0]; ps[13] = ps[1]; ps[14] = ps[2]; ps[15] = ps[3];
10390
+ ps[ 8] = pi; ps[ 9] = pi + 9; ps[10] = pi + 8; ps[11] = pi + 7;
10391
+ ps[ 4] = pi + 1; ps[ 5] = pi + 10; ps[ 6] = pi + 11; ps[ 7] = pi + 6;
10392
+ ps[ 0] = pi + 2; ps[ 1] = pi + 3; ps[ 2] = pi + 4; ps[ 3] = pi + 5;
10393
+ cs[2] = cs[0]; cs[3] = cs[1];
10394
+ cs[0] = ci; cs[1] = ci + 1;
10395
+ break;
10396
+ }
10397
+ mesh.figures.push({
10398
+ type: 'patch',
10399
+ coords: new Int32Array(ps), // making copies of ps and cs
10400
+ colors: new Int32Array(cs)
10401
+ });
10402
+ }
10403
+ }
10404
+
10405
+ function updateBounds(mesh) {
10406
+ var minX = mesh.coords[0][0], minY = mesh.coords[0][1],
10407
+ maxX = minX, maxY = minY;
10408
+ for (var i = 1, ii = mesh.coords.length; i < ii; i++) {
10409
+ var x = mesh.coords[i][0], y = mesh.coords[i][1];
10410
+ minX = minX > x ? x : minX;
10411
+ minY = minY > y ? y : minY;
10412
+ maxX = maxX < x ? x : maxX;
10413
+ maxY = maxY < y ? y : maxY;
10414
+ }
10415
+ mesh.bounds = [minX, minY, maxX, maxY];
10416
+ }
10417
+
10418
+ function packData(mesh) {
10419
+ var i, ii, j, jj;
10420
+
10421
+ var coords = mesh.coords;
10422
+ var coordsPacked = new Float32Array(coords.length * 2);
10423
+ for (i = 0, j = 0, ii = coords.length; i < ii; i++) {
10424
+ var xy = coords[i];
10425
+ coordsPacked[j++] = xy[0];
10426
+ coordsPacked[j++] = xy[1];
10427
+ }
10428
+ mesh.coords = coordsPacked;
10429
+
10430
+ var colors = mesh.colors;
10431
+ var colorsPacked = new Uint8Array(colors.length * 3);
10432
+ for (i = 0, j = 0, ii = colors.length; i < ii; i++) {
10433
+ var c = colors[i];
10434
+ colorsPacked[j++] = c[0];
10435
+ colorsPacked[j++] = c[1];
10436
+ colorsPacked[j++] = c[2];
10437
+ }
10438
+ mesh.colors = colorsPacked;
10439
+
10440
+ var figures = mesh.figures;
10441
+ for (i = 0, ii = figures.length; i < ii; i++) {
10442
+ var figure = figures[i], ps = figure.coords, cs = figure.colors;
10443
+ for (j = 0, jj = ps.length; j < jj; j++) {
10444
+ ps[j] *= 2;
10445
+ cs[j] *= 3;
10446
+ }
10447
+ }
10448
+ }
10449
+
10450
+ function Mesh(stream, matrix, xref, res) {
10451
+ assert(isStream(stream), 'Mesh data is not a stream');
10452
+ var dict = stream.dict;
10453
+ this.matrix = matrix;
10454
+ this.shadingType = dict.get('ShadingType');
10455
+ this.type = 'Pattern';
10456
+ this.bbox = dict.get('BBox');
10457
+ var cs = dict.get('ColorSpace', 'CS');
10458
+ cs = ColorSpace.parse(cs, xref, res);
10459
+ this.cs = cs;
10460
+ this.background = dict.has('Background') ?
10461
+ cs.getRgb(dict.get('Background'), 0) : null;
10462
+
10463
+ var fnObj = dict.get('Function');
10464
+ var fn = fnObj ? PDFFunction.parseArray(xref, fnObj) : null;
10465
+
10466
+ this.coords = [];
10467
+ this.colors = [];
10468
+ this.figures = [];
10469
+
10470
+ var decodeContext = {
10471
+ bitsPerCoordinate: dict.get('BitsPerCoordinate'),
10472
+ bitsPerComponent: dict.get('BitsPerComponent'),
10473
+ bitsPerFlag: dict.get('BitsPerFlag'),
10474
+ decode: dict.get('Decode'),
10475
+ colorFn: fn,
10476
+ colorSpace: cs,
10477
+ numComps: fn ? 1 : cs.numComps
10478
+ };
10479
+ var reader = new MeshStreamReader(stream, decodeContext);
10480
+
10481
+ var patchMesh = false;
10482
+ switch (this.shadingType) {
10483
+ case PatternType.FREE_FORM_MESH:
10484
+ decodeType4Shading(this, reader);
10485
+ break;
10486
+ case PatternType.LATTICE_FORM_MESH:
10487
+ var verticesPerRow = dict.get('VerticesPerRow') | 0;
10488
+ assert(verticesPerRow >= 2, 'Invalid VerticesPerRow');
10489
+ decodeType5Shading(this, reader, verticesPerRow);
10490
+ break;
10491
+ case PatternType.COONS_PATCH_MESH:
10492
+ decodeType6Shading(this, reader);
10493
+ patchMesh = true;
10494
+ break;
10495
+ case PatternType.TENSOR_PATCH_MESH:
10496
+ decodeType7Shading(this, reader);
10497
+ patchMesh = true;
10498
+ break;
10499
+ default:
10500
+ error('Unsupported mesh type.');
10501
+ break;
10502
+ }
10503
+
10504
+ if (patchMesh) {
10505
+ // dirty bounds calculation for determining, how dense shall be triangles
10506
+ updateBounds(this);
10507
+ for (var i = 0, ii = this.figures.length; i < ii; i++) {
10508
+ buildFigureFromPatch(this, i);
10509
+ }
10510
+ }
10511
+ // calculate bounds
10512
+ updateBounds(this);
10513
+
10514
+ packData(this);
10515
+ }
10516
+
10517
+ Mesh.prototype = {
10518
+ getIR: function Mesh_getIR() {
10519
+ return ['Mesh', this.shadingType, this.coords, this.colors, this.figures,
10520
+ this.bounds, this.matrix, this.bbox, this.background];
10521
+ }
10522
+ };
10523
+
10524
+ return Mesh;
10525
+ })();
10526
+
10527
+ Shadings.Dummy = (function DummyClosure() {
10528
+ function Dummy() {
10529
+ this.type = 'Pattern';
10530
+ }
10531
+
10532
+ Dummy.prototype = {
10533
+ getIR: function Dummy_getIR() {
10534
+ return ['Dummy'];
10535
+ }
10536
+ };
10537
+ return Dummy;
10538
+ })();
10539
+
10540
+ function getTilingPatternIR(operatorList, dict, args) {
10541
+ var matrix = dict.get('Matrix');
10542
+ var bbox = dict.get('BBox');
10543
+ var xstep = dict.get('XStep');
10544
+ var ystep = dict.get('YStep');
10545
+ var paintType = dict.get('PaintType');
10546
+ var tilingType = dict.get('TilingType');
10547
+
10548
+ return [
10549
+ 'TilingPattern', args, operatorList, matrix, bbox, xstep, ystep,
10550
+ paintType, tilingType
10551
+ ];
10552
+ }
10553
+
10554
+
10555
+ var PartialEvaluator = (function PartialEvaluatorClosure() {
10556
+ function PartialEvaluator(pdfManager, xref, handler, pageIndex,
10557
+ uniquePrefix, idCounters, fontCache) {
10558
+ this.pdfManager = pdfManager;
10559
+ this.xref = xref;
10560
+ this.handler = handler;
10561
+ this.pageIndex = pageIndex;
10562
+ this.uniquePrefix = uniquePrefix;
10563
+ this.idCounters = idCounters;
10564
+ this.fontCache = fontCache;
10565
+ }
10566
+
10567
+ // Trying to minimize Date.now() usage and check every 100 time
10568
+ var TIME_SLOT_DURATION_MS = 20;
10569
+ var CHECK_TIME_EVERY = 100;
10570
+ function TimeSlotManager() {
10571
+ this.reset();
10572
+ }
10573
+ TimeSlotManager.prototype = {
10574
+ check: function TimeSlotManager_check() {
10575
+ if (++this.checked < CHECK_TIME_EVERY) {
10576
+ return false;
10577
+ }
10578
+ this.checked = 0;
10579
+ return this.endTime <= Date.now();
10580
+ },
10581
+ reset: function TimeSlotManager_reset() {
10582
+ this.endTime = Date.now() + TIME_SLOT_DURATION_MS;
10583
+ this.checked = 0;
10584
+ }
10585
+ };
10586
+
10587
+ var deferred = Promise.resolve();
10588
+
10589
+ var TILING_PATTERN = 1, SHADING_PATTERN = 2;
10590
+
10591
+ PartialEvaluator.prototype = {
10592
+ hasBlendModes: function PartialEvaluator_hasBlendModes(resources) {
10593
+ if (!isDict(resources)) {
10594
+ return false;
10595
+ }
10596
+
10597
+ var processed = Object.create(null);
10598
+ if (resources.objId) {
10599
+ processed[resources.objId] = true;
10600
+ }
10601
+
10602
+ var nodes = [resources];
10603
+ while (nodes.length) {
10604
+ var key;
10605
+ var node = nodes.shift();
10606
+ // First check the current resources for blend modes.
10607
+ var graphicStates = node.get('ExtGState');
10608
+ if (isDict(graphicStates)) {
10609
+ graphicStates = graphicStates.getAll();
10610
+ for (key in graphicStates) {
10611
+ var graphicState = graphicStates[key];
10612
+ var bm = graphicState['BM'];
10613
+ if (isName(bm) && bm.name !== 'Normal') {
10614
+ return true;
10615
+ }
10616
+ }
10617
+ }
10618
+ // Descend into the XObjects to look for more resources and blend modes.
10619
+ var xObjects = node.get('XObject');
10620
+ if (!isDict(xObjects)) {
10621
+ continue;
10622
+ }
10623
+ xObjects = xObjects.getAll();
10624
+ for (key in xObjects) {
10625
+ var xObject = xObjects[key];
10626
+ if (!isStream(xObject)) {
10627
+ continue;
10628
+ }
10629
+ if (xObject.dict.objId) {
10630
+ if (processed[xObject.dict.objId]) {
10631
+ // stream has objId and is processed already
10632
+ continue;
10633
+ }
10634
+ processed[xObject.dict.objId] = true;
10635
+ }
10636
+ var xResources = xObject.dict.get('Resources');
10637
+ // Checking objId to detect an infinite loop.
10638
+ if (isDict(xResources) &&
10639
+ (!xResources.objId || !processed[xResources.objId])) {
10640
+ nodes.push(xResources);
10641
+ if (xResources.objId) {
10642
+ processed[xResources.objId] = true;
10643
+ }
10644
+ }
10645
+ }
10646
+ }
10647
+ return false;
10648
+ },
10649
+
10650
+ buildFormXObject: function PartialEvaluator_buildFormXObject(resources,
10651
+ xobj, smask,
10652
+ operatorList,
10653
+ initialState) {
10654
+ var matrix = xobj.dict.get('Matrix');
10655
+ var bbox = xobj.dict.get('BBox');
10656
+ var group = xobj.dict.get('Group');
10657
+ if (group) {
10658
+ var groupOptions = {
10659
+ matrix: matrix,
10660
+ bbox: bbox,
10661
+ smask: smask,
10662
+ isolated: false,
10663
+ knockout: false
10664
+ };
10665
+
10666
+ var groupSubtype = group.get('S');
10667
+ var colorSpace;
10668
+ if (isName(groupSubtype) && groupSubtype.name === 'Transparency') {
10669
+ groupOptions.isolated = (group.get('I') || false);
10670
+ groupOptions.knockout = (group.get('K') || false);
10671
+ colorSpace = (group.has('CS') ?
10672
+ ColorSpace.parse(group.get('CS'), this.xref, resources) : null);
10673
+ }
10674
+
10675
+ if (smask && smask.backdrop) {
10676
+ colorSpace = colorSpace || ColorSpace.singletons.rgb;
10677
+ smask.backdrop = colorSpace.getRgb(smask.backdrop, 0);
10678
+ }
10679
+
10680
+ operatorList.addOp(OPS.beginGroup, [groupOptions]);
10681
+ }
10682
+
10683
+ operatorList.addOp(OPS.paintFormXObjectBegin, [matrix, bbox]);
10684
+
10685
+ return this.getOperatorList(xobj,
10686
+ (xobj.dict.get('Resources') || resources), operatorList, initialState).
10687
+ then(function () {
10688
+ operatorList.addOp(OPS.paintFormXObjectEnd, []);
10689
+
10690
+ if (group) {
10691
+ operatorList.addOp(OPS.endGroup, [groupOptions]);
10692
+ }
10693
+ });
10694
+ },
10695
+
10696
+ buildPaintImageXObject:
10697
+ function PartialEvaluator_buildPaintImageXObject(resources, image,
10698
+ inline, operatorList,
10699
+ cacheKey, imageCache) {
10700
+ var self = this;
10701
+ var dict = image.dict;
10702
+ var w = dict.get('Width', 'W');
10703
+ var h = dict.get('Height', 'H');
10704
+
10705
+ if (!(w && isNum(w)) || !(h && isNum(h))) {
10706
+ warn('Image dimensions are missing, or not numbers.');
10707
+ return;
10708
+ }
10709
+ if (PDFJS.maxImageSize !== -1 && w * h > PDFJS.maxImageSize) {
10710
+ warn('Image exceeded maximum allowed size and was removed.');
10711
+ return;
10712
+ }
10713
+
10714
+ var imageMask = (dict.get('ImageMask', 'IM') || false);
10715
+ var imgData, args;
10716
+ if (imageMask) {
10717
+ // This depends on a tmpCanvas being filled with the
10718
+ // current fillStyle, such that processing the pixel
10719
+ // data can't be done here. Instead of creating a
10720
+ // complete PDFImage, only read the information needed
10721
+ // for later.
10722
+
10723
+ var width = dict.get('Width', 'W');
10724
+ var height = dict.get('Height', 'H');
10725
+ var bitStrideLength = (width + 7) >> 3;
10726
+ var imgArray = image.getBytes(bitStrideLength * height);
10727
+ var decode = dict.get('Decode', 'D');
10728
+ var inverseDecode = (!!decode && decode[0] > 0);
10729
+
10730
+ imgData = PDFImage.createMask(imgArray, width, height,
10731
+ image instanceof DecodeStream,
10732
+ inverseDecode);
10733
+ imgData.cached = true;
10734
+ args = [imgData];
10735
+ operatorList.addOp(OPS.paintImageMaskXObject, args);
10736
+ if (cacheKey) {
10737
+ imageCache[cacheKey] = {
10738
+ fn: OPS.paintImageMaskXObject,
10739
+ args: args
10740
+ };
10741
+ }
10742
+ return;
10743
+ }
10744
+
10745
+ var softMask = (dict.get('SMask', 'SM') || false);
10746
+ var mask = (dict.get('Mask') || false);
10747
+
10748
+ var SMALL_IMAGE_DIMENSIONS = 200;
10749
+ // Inlining small images into the queue as RGB data
10750
+ if (inline && !softMask && !mask && !(image instanceof JpegStream) &&
10751
+ (w + h) < SMALL_IMAGE_DIMENSIONS) {
10752
+ var imageObj = new PDFImage(this.xref, resources, image,
10753
+ inline, null, null);
10754
+ // We force the use of RGBA_32BPP images here, because we can't handle
10755
+ // any other kind.
10756
+ imgData = imageObj.createImageData(/* forceRGBA = */ true);
10757
+ operatorList.addOp(OPS.paintInlineImageXObject, [imgData]);
10758
+ return;
10759
+ }
10760
+
10761
+ // If there is no imageMask, create the PDFImage and a lot
10762
+ // of image processing can be done here.
10763
+ var uniquePrefix = (this.uniquePrefix || '');
10764
+ var objId = 'img_' + uniquePrefix + (++this.idCounters.obj);
10765
+ operatorList.addDependency(objId);
10766
+ args = [objId, w, h];
10767
+
10768
+ if (!softMask && !mask && image instanceof JpegStream &&
10769
+ image.isNativelySupported(this.xref, resources)) {
10770
+ // These JPEGs don't need any more processing so we can just send it.
10771
+ operatorList.addOp(OPS.paintJpegXObject, args);
10772
+ this.handler.send('obj',
10773
+ [objId, this.pageIndex, 'JpegStream', image.getIR()]);
10774
+ return;
10775
+ }
10776
+
10777
+ PDFImage.buildImage(self.handler, self.xref, resources, image, inline).
10778
+ then(function(imageObj) {
10779
+ var imgData = imageObj.createImageData(/* forceRGBA = */ false);
10780
+ self.handler.send('obj', [objId, self.pageIndex, 'Image', imgData],
10781
+ [imgData.data.buffer]);
10782
+ }).then(undefined, function (reason) {
10783
+ warn('Unable to decode image: ' + reason);
10784
+ self.handler.send('obj', [objId, self.pageIndex, 'Image', null]);
10785
+ });
10786
+
10787
+ operatorList.addOp(OPS.paintImageXObject, args);
10788
+ if (cacheKey) {
10789
+ imageCache[cacheKey] = {
10790
+ fn: OPS.paintImageXObject,
10791
+ args: args
10792
+ };
10793
+ }
10794
+ },
10795
+
10796
+ handleSMask: function PartialEvaluator_handleSmask(smask, resources,
10797
+ operatorList,
10798
+ stateManager) {
10799
+ var smaskContent = smask.get('G');
10800
+ var smaskOptions = {
10801
+ subtype: smask.get('S').name,
10802
+ backdrop: smask.get('BC')
10803
+ };
10804
+ return this.buildFormXObject(resources, smaskContent, smaskOptions,
10805
+ operatorList, stateManager.state.clone());
10806
+ },
10807
+
10808
+ handleTilingType:
10809
+ function PartialEvaluator_handleTilingType(fn, args, resources,
10810
+ pattern, patternDict,
10811
+ operatorList) {
10812
+ // Create an IR of the pattern code.
10813
+ var tilingOpList = new OperatorList();
10814
+ return this.getOperatorList(pattern,
10815
+ (patternDict.get('Resources') || resources), tilingOpList).
10816
+ then(function () {
10817
+ // Add the dependencies to the parent operator list so they are
10818
+ // resolved before sub operator list is executed synchronously.
10819
+ operatorList.addDependencies(tilingOpList.dependencies);
10820
+ operatorList.addOp(fn, getTilingPatternIR({
10821
+ fnArray: tilingOpList.fnArray,
10822
+ argsArray: tilingOpList.argsArray
10823
+ }, patternDict, args));
10824
+ });
10825
+ },
10826
+
10827
+ handleSetFont:
10828
+ function PartialEvaluator_handleSetFont(resources, fontArgs, fontRef,
10829
+ operatorList, state) {
10830
+ // TODO(mack): Not needed?
10831
+ var fontName;
10832
+ if (fontArgs) {
10833
+ fontArgs = fontArgs.slice();
10834
+ fontName = fontArgs[0].name;
10835
+ }
10836
+
10837
+ var self = this;
10838
+ return this.loadFont(fontName, fontRef, this.xref, resources).then(
10839
+ function (translated) {
10840
+ if (!translated.font.isType3Font) {
10841
+ return translated;
10842
+ }
10843
+ return translated.loadType3Data(self, resources, operatorList).then(
10844
+ function () {
10845
+ return translated;
10846
+ });
10847
+ }).then(function (translated) {
10848
+ state.font = translated.font;
10849
+ translated.send(self.handler);
10850
+ return translated.loadedName;
10851
+ });
10852
+ },
10853
+
10854
+ handleText: function PartialEvaluator_handleText(chars, state) {
10855
+ var font = state.font;
10856
+ var glyphs = font.charsToGlyphs(chars);
10857
+ var isAddToPathSet = !!(state.textRenderingMode &
10858
+ TextRenderingMode.ADD_TO_PATH_FLAG);
10859
+ if (font.data && (isAddToPathSet || PDFJS.disableFontFace)) {
10860
+ var buildPath = function (fontChar) {
10861
+ if (!font.renderer.hasBuiltPath(fontChar)) {
10862
+ var path = font.renderer.getPathJs(fontChar);
10863
+ this.handler.send('commonobj', [
10864
+ font.loadedName + '_path_' + fontChar,
10865
+ 'FontPath',
10866
+ path
10867
+ ]);
10868
+ }
10869
+ }.bind(this);
10870
+
10871
+ for (var i = 0, ii = glyphs.length; i < ii; i++) {
10872
+ var glyph = glyphs[i];
10873
+ if (glyph === null) {
10874
+ continue;
10875
+ }
10876
+ buildPath(glyph.fontChar);
10877
+
10878
+ // If the glyph has an accent we need to build a path for its
10879
+ // fontChar too, otherwise CanvasGraphics_paintChar will fail.
10880
+ var accent = glyph.accent;
10881
+ if (accent && accent.fontChar) {
10882
+ buildPath(accent.fontChar);
10883
+ }
10884
+ }
10885
+ }
10886
+
10887
+ return glyphs;
10888
+ },
10889
+
10890
+ setGState: function PartialEvaluator_setGState(resources, gState,
10891
+ operatorList, xref,
10892
+ stateManager) {
10893
+ // This array holds the converted/processed state data.
10894
+ var gStateObj = [];
10895
+ var gStateMap = gState.map;
10896
+ var self = this;
10897
+ var promise = Promise.resolve();
10898
+ for (var key in gStateMap) {
10899
+ var value = gStateMap[key];
10900
+ switch (key) {
10901
+ case 'Type':
10902
+ break;
10903
+ case 'LW':
10904
+ case 'LC':
10905
+ case 'LJ':
10906
+ case 'ML':
10907
+ case 'D':
10908
+ case 'RI':
10909
+ case 'FL':
10910
+ case 'CA':
10911
+ case 'ca':
10912
+ gStateObj.push([key, value]);
10913
+ break;
10914
+ case 'Font':
10915
+ promise = promise.then(function () {
10916
+ return self.handleSetFont(resources, null, value[0],
10917
+ operatorList, stateManager.state).
10918
+ then(function (loadedName) {
10919
+ operatorList.addDependency(loadedName);
10920
+ gStateObj.push([key, [loadedName, value[1]]]);
10921
+ });
10922
+ });
10923
+ break;
10924
+ case 'BM':
10925
+ gStateObj.push([key, value]);
10926
+ break;
10927
+ case 'SMask':
10928
+ if (isName(value) && value.name === 'None') {
10929
+ gStateObj.push([key, false]);
10930
+ break;
10931
+ }
10932
+ var dict = xref.fetchIfRef(value);
10933
+ if (isDict(dict)) {
10934
+ promise = promise.then(function () {
10935
+ return self.handleSMask(dict, resources, operatorList,
10936
+ stateManager);
10937
+ });
10938
+ gStateObj.push([key, true]);
10939
+ } else {
10940
+ warn('Unsupported SMask type');
10941
+ }
10942
+
10943
+ break;
10944
+ // Only generate info log messages for the following since
10945
+ // they are unlikely to have a big impact on the rendering.
10946
+ case 'OP':
10947
+ case 'op':
10948
+ case 'OPM':
10949
+ case 'BG':
10950
+ case 'BG2':
10951
+ case 'UCR':
10952
+ case 'UCR2':
10953
+ case 'TR':
10954
+ case 'TR2':
10955
+ case 'HT':
10956
+ case 'SM':
10957
+ case 'SA':
10958
+ case 'AIS':
10959
+ case 'TK':
10960
+ // TODO implement these operators.
10961
+ info('graphic state operator ' + key);
10962
+ break;
10963
+ default:
10964
+ info('Unknown graphic state operator ' + key);
10965
+ break;
10966
+ }
10967
+ }
10968
+ return promise.then(function () {
10969
+ if (gStateObj.length >= 0) {
10970
+ operatorList.addOp(OPS.setGState, [gStateObj]);
10971
+ }
10972
+ });
10973
+ },
10974
+
10975
+ loadFont: function PartialEvaluator_loadFont(fontName, font, xref,
10976
+ resources) {
10977
+
10978
+ function errorFont() {
10979
+ return Promise.resolve(new TranslatedFont('g_font_error',
10980
+ new ErrorFont('Font ' + fontName + ' is not available'), font));
10981
+ }
10982
+ var fontRef;
10983
+ if (font) { // Loading by ref.
10984
+ assert(isRef(font));
10985
+ fontRef = font;
10986
+ } else { // Loading by name.
10987
+ var fontRes = resources.get('Font');
10988
+ if (fontRes) {
10989
+ fontRef = fontRes.getRaw(fontName);
10990
+ } else {
10991
+ warn('fontRes not available');
10992
+ return errorFont();
10993
+ }
10994
+ }
10995
+ if (!fontRef) {
10996
+ warn('fontRef not available');
10997
+ return errorFont();
10998
+ }
10999
+
11000
+ if (this.fontCache.has(fontRef)) {
11001
+ return this.fontCache.get(fontRef);
11002
+ }
11003
+
11004
+ font = xref.fetchIfRef(fontRef);
11005
+ if (!isDict(font)) {
11006
+ return errorFont();
11007
+ }
11008
+
11009
+ // We are holding font.translated references just for fontRef that are not
11010
+ // dictionaries (Dict). See explanation below.
11011
+ if (font.translated) {
11012
+ return font.translated;
11013
+ }
11014
+
11015
+ var fontCapability = createPromiseCapability();
11016
+
11017
+ var preEvaluatedFont = this.preEvaluateFont(font, xref);
11018
+ var descriptor = preEvaluatedFont.descriptor;
11019
+ var fontID = fontRef.num + '_' + fontRef.gen;
11020
+ if (isDict(descriptor)) {
11021
+ if (!descriptor.fontAliases) {
11022
+ descriptor.fontAliases = Object.create(null);
11023
+ }
11024
+
11025
+ var fontAliases = descriptor.fontAliases;
11026
+ var hash = preEvaluatedFont.hash;
11027
+ if (fontAliases[hash]) {
11028
+ var aliasFontRef = fontAliases[hash].aliasRef;
11029
+ if (aliasFontRef && this.fontCache.has(aliasFontRef)) {
11030
+ this.fontCache.putAlias(fontRef, aliasFontRef);
11031
+ return this.fontCache.get(fontRef);
11032
+ }
11033
+ }
11034
+
11035
+ if (!fontAliases[hash]) {
11036
+ fontAliases[hash] = {
11037
+ fontID: Font.getFontID()
11038
+ };
11039
+ }
11040
+
11041
+ fontAliases[hash].aliasRef = fontRef;
11042
+ fontID = fontAliases[hash].fontID;
11043
+ }
11044
+
11045
+ // Workaround for bad PDF generators that don't reference fonts
11046
+ // properly, i.e. by not using an object identifier.
11047
+ // Check if the fontRef is a Dict (as opposed to a standard object),
11048
+ // in which case we don't cache the font and instead reference it by
11049
+ // fontName in font.loadedName below.
11050
+ var fontRefIsDict = isDict(fontRef);
11051
+ if (!fontRefIsDict) {
11052
+ this.fontCache.put(fontRef, fontCapability.promise);
11053
+ }
11054
+
11055
+ // Keep track of each font we translated so the caller can
11056
+ // load them asynchronously before calling display on a page.
11057
+ font.loadedName = 'g_font_' + (fontRefIsDict ?
11058
+ fontName.replace(/\W/g, '') : fontID);
11059
+
11060
+ font.translated = fontCapability.promise;
11061
+
11062
+ // TODO move promises into translate font
11063
+ var translatedPromise;
11064
+ try {
11065
+ translatedPromise = Promise.resolve(
11066
+ this.translateFont(preEvaluatedFont, xref));
11067
+ } catch (e) {
11068
+ translatedPromise = Promise.reject(e);
11069
+ }
11070
+
11071
+ translatedPromise.then(function (translatedFont) {
11072
+ if (translatedFont.fontType !== undefined) {
11073
+ var xrefFontStats = xref.stats.fontTypes;
11074
+ xrefFontStats[translatedFont.fontType] = true;
11075
+ }
11076
+
11077
+ fontCapability.resolve(new TranslatedFont(font.loadedName,
11078
+ translatedFont, font));
11079
+ }, function (reason) {
11080
+ // TODO fontCapability.reject?
11081
+ UnsupportedManager.notify(UNSUPPORTED_FEATURES.font);
11082
+
11083
+ try {
11084
+ // error, but it's still nice to have font type reported
11085
+ var descriptor = preEvaluatedFont.descriptor;
11086
+ var fontFile3 = descriptor && descriptor.get('FontFile3');
11087
+ var subtype = fontFile3 && fontFile3.get('Subtype');
11088
+ var fontType = getFontType(preEvaluatedFont.type,
11089
+ subtype && subtype.name);
11090
+ var xrefFontStats = xref.stats.fontTypes;
11091
+ xrefFontStats[fontType] = true;
11092
+ } catch (ex) { }
11093
+
11094
+ fontCapability.resolve(new TranslatedFont(font.loadedName,
11095
+ new ErrorFont(reason instanceof Error ? reason.message : reason),
11096
+ font));
11097
+ });
11098
+ return fontCapability.promise;
11099
+ },
11100
+
11101
+ buildPath: function PartialEvaluator_buildPath(operatorList, fn, args) {
11102
+ var lastIndex = operatorList.length - 1;
11103
+ if (!args) {
11104
+ args = [];
11105
+ }
11106
+ if (lastIndex < 0 ||
11107
+ operatorList.fnArray[lastIndex] !== OPS.constructPath) {
11108
+ operatorList.addOp(OPS.constructPath, [[fn], args]);
11109
+ } else {
11110
+ var opArgs = operatorList.argsArray[lastIndex];
11111
+ opArgs[0].push(fn);
11112
+ Array.prototype.push.apply(opArgs[1], args);
11113
+ }
11114
+ },
11115
+
11116
+ handleColorN: function PartialEvaluator_handleColorN(operatorList, fn, args,
11117
+ cs, patterns, resources, xref) {
11118
+ // compile tiling patterns
11119
+ var patternName = args[args.length - 1];
11120
+ // SCN/scn applies patterns along with normal colors
11121
+ var pattern;
11122
+ if (isName(patternName) &&
11123
+ (pattern = patterns.get(patternName.name))) {
11124
+ var dict = (isStream(pattern) ? pattern.dict : pattern);
11125
+ var typeNum = dict.get('PatternType');
11126
+
11127
+ if (typeNum === TILING_PATTERN) {
11128
+ var color = cs.base ? cs.base.getRgb(args, 0) : null;
11129
+ return this.handleTilingType(fn, color, resources, pattern,
11130
+ dict, operatorList);
11131
+ } else if (typeNum === SHADING_PATTERN) {
11132
+ var shading = dict.get('Shading');
11133
+ var matrix = dict.get('Matrix');
11134
+ pattern = Pattern.parseShading(shading, matrix, xref, resources);
11135
+ operatorList.addOp(fn, pattern.getIR());
11136
+ return Promise.resolve();
11137
+ } else {
11138
+ return Promise.reject('Unknown PatternType: ' + typeNum);
11139
+ }
11140
+ }
11141
+ // TODO shall we fail here?
11142
+ operatorList.addOp(fn, args);
11143
+ return Promise.resolve();
11144
+ },
11145
+
11146
+ getOperatorList: function PartialEvaluator_getOperatorList(stream,
11147
+ resources,
11148
+ operatorList,
11149
+ initialState) {
11150
+
11151
+ var self = this;
11152
+ var xref = this.xref;
11153
+ var imageCache = {};
11154
+
11155
+ assert(operatorList);
11156
+
11157
+ resources = (resources || Dict.empty);
11158
+ var xobjs = (resources.get('XObject') || Dict.empty);
11159
+ var patterns = (resources.get('Pattern') || Dict.empty);
11160
+ var stateManager = new StateManager(initialState || new EvalState());
11161
+ var preprocessor = new EvaluatorPreprocessor(stream, xref, stateManager);
11162
+ var timeSlotManager = new TimeSlotManager();
11163
+
11164
+ return new Promise(function next(resolve, reject) {
11165
+ timeSlotManager.reset();
11166
+ var stop, operation = {}, i, ii, cs;
11167
+ while (!(stop = timeSlotManager.check())) {
11168
+ // The arguments parsed by read() are used beyond this loop, so we
11169
+ // cannot reuse the same array on each iteration. Therefore we pass
11170
+ // in |null| as the initial value (see the comment on
11171
+ // EvaluatorPreprocessor_read() for why).
11172
+ operation.args = null;
11173
+ if (!(preprocessor.read(operation))) {
11174
+ break;
11175
+ }
11176
+ var args = operation.args;
11177
+ var fn = operation.fn;
11178
+
11179
+ switch (fn | 0) {
11180
+ case OPS.paintXObject:
11181
+ if (args[0].code) {
11182
+ break;
11183
+ }
11184
+ // eagerly compile XForm objects
11185
+ var name = args[0].name;
11186
+ if (imageCache[name] !== undefined) {
11187
+ operatorList.addOp(imageCache[name].fn, imageCache[name].args);
11188
+ args = null;
11189
+ continue;
11190
+ }
11191
+
11192
+ var xobj = xobjs.get(name);
11193
+ if (xobj) {
11194
+ assert(isStream(xobj), 'XObject should be a stream');
11195
+
11196
+ var type = xobj.dict.get('Subtype');
11197
+ assert(isName(type),
11198
+ 'XObject should have a Name subtype');
11199
+
11200
+ if (type.name === 'Form') {
11201
+ stateManager.save();
11202
+ return self.buildFormXObject(resources, xobj, null,
11203
+ operatorList,
11204
+ stateManager.state.clone()).
11205
+ then(function () {
11206
+ stateManager.restore();
11207
+ next(resolve, reject);
11208
+ }, reject);
11209
+ } else if (type.name === 'Image') {
11210
+ self.buildPaintImageXObject(resources, xobj, false,
11211
+ operatorList, name, imageCache);
11212
+ args = null;
11213
+ continue;
11214
+ } else if (type.name === 'PS') {
11215
+ // PostScript XObjects are unused when viewing documents.
11216
+ // See section 4.7.1 of Adobe's PDF reference.
11217
+ info('Ignored XObject subtype PS');
11218
+ continue;
11219
+ } else {
11220
+ error('Unhandled XObject subtype ' + type.name);
11221
+ }
11222
+ }
11223
+ break;
11224
+ case OPS.setFont:
11225
+ var fontSize = args[1];
11226
+ // eagerly collect all fonts
11227
+ return self.handleSetFont(resources, args, null,
11228
+ operatorList, stateManager.state).
11229
+ then(function (loadedName) {
11230
+ operatorList.addDependency(loadedName);
11231
+ operatorList.addOp(OPS.setFont, [loadedName, fontSize]);
11232
+ next(resolve, reject);
11233
+ }, reject);
11234
+ case OPS.endInlineImage:
11235
+ var cacheKey = args[0].cacheKey;
11236
+ if (cacheKey) {
11237
+ var cacheEntry = imageCache[cacheKey];
11238
+ if (cacheEntry !== undefined) {
11239
+ operatorList.addOp(cacheEntry.fn, cacheEntry.args);
11240
+ args = null;
11241
+ continue;
11242
+ }
11243
+ }
11244
+ self.buildPaintImageXObject(resources, args[0], true,
11245
+ operatorList, cacheKey, imageCache);
11246
+ args = null;
11247
+ continue;
11248
+ case OPS.showText:
11249
+ args[0] = self.handleText(args[0], stateManager.state);
11250
+ break;
11251
+ case OPS.showSpacedText:
11252
+ var arr = args[0];
11253
+ var combinedGlyphs = [];
11254
+ var arrLength = arr.length;
11255
+ for (i = 0; i < arrLength; ++i) {
11256
+ var arrItem = arr[i];
11257
+ if (isString(arrItem)) {
11258
+ Array.prototype.push.apply(combinedGlyphs,
11259
+ self.handleText(arrItem, stateManager.state));
11260
+ } else if (isNum(arrItem)) {
11261
+ combinedGlyphs.push(arrItem);
11262
+ }
11263
+ }
11264
+ args[0] = combinedGlyphs;
11265
+ fn = OPS.showText;
11266
+ break;
11267
+ case OPS.nextLineShowText:
11268
+ operatorList.addOp(OPS.nextLine);
11269
+ args[0] = self.handleText(args[0], stateManager.state);
11270
+ fn = OPS.showText;
11271
+ break;
11272
+ case OPS.nextLineSetSpacingShowText:
11273
+ operatorList.addOp(OPS.nextLine);
11274
+ operatorList.addOp(OPS.setWordSpacing, [args.shift()]);
11275
+ operatorList.addOp(OPS.setCharSpacing, [args.shift()]);
11276
+ args[0] = self.handleText(args[0], stateManager.state);
11277
+ fn = OPS.showText;
11278
+ break;
11279
+ case OPS.setTextRenderingMode:
11280
+ stateManager.state.textRenderingMode = args[0];
11281
+ break;
11282
+
11283
+ case OPS.setFillColorSpace:
11284
+ stateManager.state.fillColorSpace =
11285
+ ColorSpace.parse(args[0], xref, resources);
11286
+ continue;
11287
+ case OPS.setStrokeColorSpace:
11288
+ stateManager.state.strokeColorSpace =
11289
+ ColorSpace.parse(args[0], xref, resources);
11290
+ continue;
11291
+ case OPS.setFillColor:
11292
+ cs = stateManager.state.fillColorSpace;
11293
+ args = cs.getRgb(args, 0);
11294
+ fn = OPS.setFillRGBColor;
11295
+ break;
11296
+ case OPS.setStrokeColor:
11297
+ cs = stateManager.state.strokeColorSpace;
11298
+ args = cs.getRgb(args, 0);
11299
+ fn = OPS.setStrokeRGBColor;
11300
+ break;
11301
+ case OPS.setFillGray:
11302
+ stateManager.state.fillColorSpace = ColorSpace.singletons.gray;
11303
+ args = ColorSpace.singletons.gray.getRgb(args, 0);
11304
+ fn = OPS.setFillRGBColor;
11305
+ break;
11306
+ case OPS.setStrokeGray:
11307
+ stateManager.state.strokeColorSpace = ColorSpace.singletons.gray;
11308
+ args = ColorSpace.singletons.gray.getRgb(args, 0);
11309
+ fn = OPS.setStrokeRGBColor;
11310
+ break;
11311
+ case OPS.setFillCMYKColor:
11312
+ stateManager.state.fillColorSpace = ColorSpace.singletons.cmyk;
11313
+ args = ColorSpace.singletons.cmyk.getRgb(args, 0);
11314
+ fn = OPS.setFillRGBColor;
11315
+ break;
11316
+ case OPS.setStrokeCMYKColor:
11317
+ stateManager.state.strokeColorSpace = ColorSpace.singletons.cmyk;
11318
+ args = ColorSpace.singletons.cmyk.getRgb(args, 0);
11319
+ fn = OPS.setStrokeRGBColor;
11320
+ break;
11321
+ case OPS.setFillRGBColor:
11322
+ stateManager.state.fillColorSpace = ColorSpace.singletons.rgb;
11323
+ args = ColorSpace.singletons.rgb.getRgb(args, 0);
11324
+ break;
11325
+ case OPS.setStrokeRGBColor:
11326
+ stateManager.state.strokeColorSpace = ColorSpace.singletons.rgb;
11327
+ args = ColorSpace.singletons.rgb.getRgb(args, 0);
11328
+ break;
11329
+ case OPS.setFillColorN:
11330
+ cs = stateManager.state.fillColorSpace;
11331
+ if (cs.name === 'Pattern') {
11332
+ return self.handleColorN(operatorList, OPS.setFillColorN,
11333
+ args, cs, patterns, resources, xref).then(function() {
11334
+ next(resolve, reject);
11335
+ }, reject);
11336
+ }
11337
+ args = cs.getRgb(args, 0);
11338
+ fn = OPS.setFillRGBColor;
11339
+ break;
11340
+ case OPS.setStrokeColorN:
11341
+ cs = stateManager.state.strokeColorSpace;
11342
+ if (cs.name === 'Pattern') {
11343
+ return self.handleColorN(operatorList, OPS.setStrokeColorN,
11344
+ args, cs, patterns, resources, xref).then(function() {
11345
+ next(resolve, reject);
11346
+ }, reject);
11347
+ }
11348
+ args = cs.getRgb(args, 0);
11349
+ fn = OPS.setStrokeRGBColor;
11350
+ break;
11351
+
11352
+ case OPS.shadingFill:
11353
+ var shadingRes = resources.get('Shading');
11354
+ if (!shadingRes) {
11355
+ error('No shading resource found');
11356
+ }
11357
+
11358
+ var shading = shadingRes.get(args[0].name);
11359
+ if (!shading) {
11360
+ error('No shading object found');
11361
+ }
11362
+
11363
+ var shadingFill = Pattern.parseShading(shading, null, xref,
11364
+ resources);
11365
+ var patternIR = shadingFill.getIR();
11366
+ args = [patternIR];
11367
+ fn = OPS.shadingFill;
11368
+ break;
11369
+ case OPS.setGState:
11370
+ var dictName = args[0];
11371
+ var extGState = resources.get('ExtGState');
11372
+
11373
+ if (!isDict(extGState) || !extGState.has(dictName.name)) {
11374
+ break;
11375
+ }
11376
+
11377
+ var gState = extGState.get(dictName.name);
11378
+ return self.setGState(resources, gState, operatorList, xref,
11379
+ stateManager).then(function() {
11380
+ next(resolve, reject);
11381
+ }, reject);
11382
+ case OPS.moveTo:
11383
+ case OPS.lineTo:
11384
+ case OPS.curveTo:
11385
+ case OPS.curveTo2:
11386
+ case OPS.curveTo3:
11387
+ case OPS.closePath:
11388
+ self.buildPath(operatorList, fn, args);
11389
+ continue;
11390
+ case OPS.rectangle:
11391
+ self.buildPath(operatorList, fn, args);
11392
+ continue;
11393
+ }
11394
+ operatorList.addOp(fn, args);
11395
+ }
11396
+ if (stop) {
11397
+ deferred.then(function () {
11398
+ next(resolve, reject);
11399
+ });
11400
+ return;
11401
+ }
11402
+ // Some PDFs don't close all restores inside object/form.
11403
+ // Closing those for them.
11404
+ for (i = 0, ii = preprocessor.savedStatesDepth; i < ii; i++) {
11405
+ operatorList.addOp(OPS.restore, []);
11406
+ }
11407
+ resolve();
11408
+ });
11409
+ },
11410
+
11411
+ getTextContent: function PartialEvaluator_getTextContent(stream, resources,
11412
+ stateManager) {
11413
+
11414
+ stateManager = (stateManager || new StateManager(new TextState()));
11415
+
11416
+ var textContent = {
11417
+ items: [],
11418
+ styles: Object.create(null)
11419
+ };
11420
+ var bidiTexts = textContent.items;
11421
+ var SPACE_FACTOR = 0.3;
11422
+ var MULTI_SPACE_FACTOR = 1.5;
11423
+
11424
+ var self = this;
11425
+ var xref = this.xref;
11426
+
11427
+ resources = (xref.fetchIfRef(resources) || Dict.empty);
11428
+
11429
+ // The xobj is parsed iff it's needed, e.g. if there is a `DO` cmd.
11430
+ var xobjs = null;
11431
+ var xobjsCache = {};
11432
+
11433
+ var preprocessor = new EvaluatorPreprocessor(stream, xref, stateManager);
11434
+
11435
+ var textState;
11436
+
11437
+ function newTextChunk() {
11438
+ var font = textState.font;
11439
+ if (!(font.loadedName in textContent.styles)) {
11440
+ textContent.styles[font.loadedName] = {
11441
+ fontFamily: font.fallbackName,
11442
+ ascent: font.ascent,
11443
+ descent: font.descent,
11444
+ vertical: font.vertical
11445
+ };
11446
+ }
11447
+ return {
11448
+ // |str| is initially an array which we push individual chars to, and
11449
+ // then runBidi() overwrites it with the final string.
11450
+ str: [],
11451
+ dir: null,
11452
+ width: 0,
11453
+ height: 0,
11454
+ transform: null,
11455
+ fontName: font.loadedName
11456
+ };
11457
+ }
11458
+
11459
+ function runBidi(textChunk) {
11460
+ var str = textChunk.str.join('');
11461
+ var bidiResult = PDFJS.bidi(str, -1, textState.font.vertical);
11462
+ textChunk.str = bidiResult.str;
11463
+ textChunk.dir = bidiResult.dir;
11464
+ return textChunk;
11465
+ }
11466
+
11467
+ function handleSetFont(fontName, fontRef) {
11468
+ return self.loadFont(fontName, fontRef, xref, resources).
11469
+ then(function (translated) {
11470
+ textState.font = translated.font;
11471
+ textState.fontMatrix = translated.font.fontMatrix ||
11472
+ FONT_IDENTITY_MATRIX;
11473
+ });
11474
+ }
11475
+
11476
+ function buildTextGeometry(chars, textChunk) {
11477
+ var font = textState.font;
11478
+ textChunk = textChunk || newTextChunk();
11479
+ if (!textChunk.transform) {
11480
+ // 9.4.4 Text Space Details
11481
+ var tsm = [textState.fontSize * textState.textHScale, 0,
11482
+ 0, textState.fontSize,
11483
+ 0, textState.textRise];
11484
+ var trm = textChunk.transform = Util.transform(textState.ctm,
11485
+ Util.transform(textState.textMatrix, tsm));
11486
+ if (!font.vertical) {
11487
+ textChunk.height = Math.sqrt(trm[2] * trm[2] + trm[3] * trm[3]);
11488
+ } else {
11489
+ textChunk.width = Math.sqrt(trm[0] * trm[0] + trm[1] * trm[1]);
11490
+ }
11491
+ }
11492
+ var width = 0;
11493
+ var height = 0;
11494
+ var glyphs = font.charsToGlyphs(chars);
11495
+ var defaultVMetrics = font.defaultVMetrics;
11496
+ for (var i = 0; i < glyphs.length; i++) {
11497
+ var glyph = glyphs[i];
11498
+ if (!glyph) { // Previous glyph was a space.
11499
+ width += textState.wordSpacing * textState.textHScale;
11500
+ continue;
11501
+ }
11502
+ var vMetricX = null;
11503
+ var vMetricY = null;
11504
+ var glyphWidth = null;
11505
+ if (font.vertical) {
11506
+ if (glyph.vmetric) {
11507
+ glyphWidth = glyph.vmetric[0];
11508
+ vMetricX = glyph.vmetric[1];
11509
+ vMetricY = glyph.vmetric[2];
11510
+ } else {
11511
+ glyphWidth = glyph.width;
11512
+ vMetricX = glyph.width * 0.5;
11513
+ vMetricY = defaultVMetrics[2];
11514
+ }
11515
+ } else {
11516
+ glyphWidth = glyph.width;
11517
+ }
11518
+
11519
+ var glyphUnicode = glyph.unicode;
11520
+ if (NormalizedUnicodes[glyphUnicode] !== undefined) {
11521
+ glyphUnicode = NormalizedUnicodes[glyphUnicode];
11522
+ }
11523
+ glyphUnicode = reverseIfRtl(glyphUnicode);
11524
+
11525
+ // The following will calculate the x and y of the individual glyphs.
11526
+ // if (font.vertical) {
11527
+ // tsm[4] -= vMetricX * Math.abs(textState.fontSize) *
11528
+ // textState.fontMatrix[0];
11529
+ // tsm[5] -= vMetricY * textState.fontSize *
11530
+ // textState.fontMatrix[0];
11531
+ // }
11532
+ // var trm = Util.transform(textState.textMatrix, tsm);
11533
+ // var pt = Util.applyTransform([trm[4], trm[5]], textState.ctm);
11534
+ // var x = pt[0];
11535
+ // var y = pt[1];
11536
+
11537
+ var tx = 0;
11538
+ var ty = 0;
11539
+ if (!font.vertical) {
11540
+ var w0 = glyphWidth * textState.fontMatrix[0];
11541
+ tx = (w0 * textState.fontSize + textState.charSpacing) *
11542
+ textState.textHScale;
11543
+ width += tx;
11544
+ } else {
11545
+ var w1 = glyphWidth * textState.fontMatrix[0];
11546
+ ty = w1 * textState.fontSize + textState.charSpacing;
11547
+ height += ty;
11548
+ }
11549
+ textState.translateTextMatrix(tx, ty);
11550
+
11551
+ textChunk.str.push(glyphUnicode);
11552
+ }
11553
+
11554
+ var a = textState.textLineMatrix[0];
11555
+ var b = textState.textLineMatrix[1];
11556
+ var scaleLineX = Math.sqrt(a * a + b * b);
11557
+ a = textState.ctm[0];
11558
+ b = textState.ctm[1];
11559
+ var scaleCtmX = Math.sqrt(a * a + b * b);
11560
+ if (!font.vertical) {
11561
+ textChunk.width += width * scaleCtmX * scaleLineX;
11562
+ } else {
11563
+ textChunk.height += Math.abs(height * scaleCtmX * scaleLineX);
11564
+ }
11565
+ return textChunk;
11566
+ }
11567
+
11568
+ var timeSlotManager = new TimeSlotManager();
11569
+
11570
+ return new Promise(function next(resolve, reject) {
11571
+ timeSlotManager.reset();
11572
+ var stop, operation = {}, args = [];
11573
+ while (!(stop = timeSlotManager.check())) {
11574
+ // The arguments parsed by read() are not used beyond this loop, so
11575
+ // we can reuse the same array on every iteration, thus avoiding
11576
+ // unnecessary allocations.
11577
+ args.length = 0;
11578
+ operation.args = args;
11579
+ if (!(preprocessor.read(operation))) {
11580
+ break;
11581
+ }
11582
+ textState = stateManager.state;
11583
+ var fn = operation.fn;
11584
+ args = operation.args;
11585
+
11586
+ switch (fn | 0) {
11587
+ case OPS.setFont:
11588
+ textState.fontSize = args[1];
11589
+ return handleSetFont(args[0].name).then(function() {
11590
+ next(resolve, reject);
11591
+ }, reject);
11592
+ case OPS.setTextRise:
11593
+ textState.textRise = args[0];
11594
+ break;
11595
+ case OPS.setHScale:
11596
+ textState.textHScale = args[0] / 100;
11597
+ break;
11598
+ case OPS.setLeading:
11599
+ textState.leading = args[0];
11600
+ break;
11601
+ case OPS.moveText:
11602
+ textState.translateTextLineMatrix(args[0], args[1]);
11603
+ textState.textMatrix = textState.textLineMatrix.slice();
11604
+ break;
11605
+ case OPS.setLeadingMoveText:
11606
+ textState.leading = -args[1];
11607
+ textState.translateTextLineMatrix(args[0], args[1]);
11608
+ textState.textMatrix = textState.textLineMatrix.slice();
11609
+ break;
11610
+ case OPS.nextLine:
11611
+ textState.carriageReturn();
11612
+ break;
11613
+ case OPS.setTextMatrix:
11614
+ textState.setTextMatrix(args[0], args[1], args[2], args[3],
11615
+ args[4], args[5]);
11616
+ textState.setTextLineMatrix(args[0], args[1], args[2], args[3],
11617
+ args[4], args[5]);
11618
+ break;
11619
+ case OPS.setCharSpacing:
11620
+ textState.charSpacing = args[0];
11621
+ break;
11622
+ case OPS.setWordSpacing:
11623
+ textState.wordSpacing = args[0];
11624
+ break;
11625
+ case OPS.beginText:
11626
+ textState.textMatrix = IDENTITY_MATRIX.slice();
11627
+ textState.textLineMatrix = IDENTITY_MATRIX.slice();
11628
+ break;
11629
+ case OPS.showSpacedText:
11630
+ var items = args[0];
11631
+ var textChunk = newTextChunk();
11632
+ var offset;
11633
+ for (var j = 0, jj = items.length; j < jj; j++) {
11634
+ if (typeof items[j] === 'string') {
11635
+ buildTextGeometry(items[j], textChunk);
11636
+ } else {
11637
+ var val = items[j] / 1000;
11638
+ if (!textState.font.vertical) {
11639
+ offset = -val * textState.fontSize * textState.textHScale *
11640
+ textState.textMatrix[0];
11641
+ textState.translateTextMatrix(offset, 0);
11642
+ textChunk.width += offset;
11643
+ } else {
11644
+ offset = -val * textState.fontSize *
11645
+ textState.textMatrix[3];
11646
+ textState.translateTextMatrix(0, offset);
11647
+ textChunk.height += offset;
11648
+ }
11649
+ if (items[j] < 0 && textState.font.spaceWidth > 0) {
11650
+ var fakeSpaces = -items[j] / textState.font.spaceWidth;
11651
+ if (fakeSpaces > MULTI_SPACE_FACTOR) {
11652
+ fakeSpaces = Math.round(fakeSpaces);
11653
+ while (fakeSpaces--) {
11654
+ textChunk.str.push(' ');
11655
+ }
11656
+ } else if (fakeSpaces > SPACE_FACTOR) {
11657
+ textChunk.str.push(' ');
11658
+ }
11659
+ }
11660
+ }
11661
+ }
11662
+ bidiTexts.push(runBidi(textChunk));
11663
+ break;
11664
+ case OPS.showText:
11665
+ bidiTexts.push(runBidi(buildTextGeometry(args[0])));
11666
+ break;
11667
+ case OPS.nextLineShowText:
11668
+ textState.carriageReturn();
11669
+ bidiTexts.push(runBidi(buildTextGeometry(args[0])));
11670
+ break;
11671
+ case OPS.nextLineSetSpacingShowText:
11672
+ textState.wordSpacing = args[0];
11673
+ textState.charSpacing = args[1];
11674
+ textState.carriageReturn();
11675
+ bidiTexts.push(runBidi(buildTextGeometry(args[2])));
11676
+ break;
11677
+ case OPS.paintXObject:
11678
+ if (args[0].code) {
11679
+ break;
11680
+ }
11681
+
11682
+ if (!xobjs) {
11683
+ xobjs = (resources.get('XObject') || Dict.empty);
11684
+ }
11685
+
11686
+ var name = args[0].name;
11687
+ if (xobjsCache.key === name) {
11688
+ if (xobjsCache.texts) {
11689
+ Util.appendToArray(bidiTexts, xobjsCache.texts.items);
11690
+ Util.extendObj(textContent.styles, xobjsCache.texts.styles);
11691
+ }
11692
+ break;
11693
+ }
11694
+
11695
+ var xobj = xobjs.get(name);
11696
+ if (!xobj) {
11697
+ break;
11698
+ }
11699
+ assert(isStream(xobj), 'XObject should be a stream');
11700
+
11701
+ var type = xobj.dict.get('Subtype');
11702
+ assert(isName(type),
11703
+ 'XObject should have a Name subtype');
11704
+
11705
+ if ('Form' !== type.name) {
11706
+ xobjsCache.key = name;
11707
+ xobjsCache.texts = null;
11708
+ break;
11709
+ }
11710
+
11711
+ stateManager.save();
11712
+ var matrix = xobj.dict.get('Matrix');
11713
+ if (isArray(matrix) && matrix.length === 6) {
11714
+ stateManager.transform(matrix);
11715
+ }
11716
+
11717
+ return self.getTextContent(xobj,
11718
+ xobj.dict.get('Resources') || resources, stateManager).
11719
+ then(function (formTextContent) {
11720
+ Util.appendToArray(bidiTexts, formTextContent.items);
11721
+ Util.extendObj(textContent.styles, formTextContent.styles);
11722
+ stateManager.restore();
11723
+
11724
+ xobjsCache.key = name;
11725
+ xobjsCache.texts = formTextContent;
11726
+
11727
+ next(resolve, reject);
11728
+ }, reject);
11729
+ case OPS.setGState:
11730
+ var dictName = args[0];
11731
+ var extGState = resources.get('ExtGState');
11732
+
11733
+ if (!isDict(extGState) || !extGState.has(dictName.name)) {
11734
+ break;
11735
+ }
11736
+
11737
+ var gsStateMap = extGState.get(dictName.name);
11738
+ var gsStateFont = null;
11739
+ for (var key in gsStateMap) {
11740
+ if (key === 'Font') {
11741
+ assert(!gsStateFont);
11742
+ gsStateFont = gsStateMap[key];
11743
+ }
11744
+ }
11745
+ if (gsStateFont) {
11746
+ textState.fontSize = gsStateFont[1];
11747
+ return handleSetFont(gsStateFont[0]).then(function() {
11748
+ next(resolve, reject);
11749
+ }, reject);
11750
+ }
11751
+ break;
11752
+ } // switch
11753
+ } // while
11754
+ if (stop) {
11755
+ deferred.then(function () {
11756
+ next(resolve, reject);
11757
+ });
11758
+ return;
11759
+ }
11760
+ resolve(textContent);
11761
+ });
11762
+ },
11763
+
11764
+ extractDataStructures: function
11765
+ partialEvaluatorExtractDataStructures(dict, baseDict,
11766
+ xref, properties) {
11767
+ // 9.10.2
11768
+ var toUnicode = (dict.get('ToUnicode') || baseDict.get('ToUnicode'));
11769
+ if (toUnicode) {
11770
+ properties.toUnicode = this.readToUnicode(toUnicode);
11771
+ }
11772
+ if (properties.composite) {
11773
+ // CIDSystemInfo helps to match CID to glyphs
11774
+ var cidSystemInfo = dict.get('CIDSystemInfo');
11775
+ if (isDict(cidSystemInfo)) {
11776
+ properties.cidSystemInfo = {
11777
+ registry: cidSystemInfo.get('Registry'),
11778
+ ordering: cidSystemInfo.get('Ordering'),
11779
+ supplement: cidSystemInfo.get('Supplement')
11780
+ };
11781
+ }
11782
+
11783
+ var cidToGidMap = dict.get('CIDToGIDMap');
11784
+ if (isStream(cidToGidMap)) {
11785
+ properties.cidToGidMap = this.readCidToGidMap(cidToGidMap);
11786
+ }
11787
+ }
11788
+
11789
+ // Based on 9.6.6 of the spec the encoding can come from multiple places
11790
+ // and depends on the font type. The base encoding and differences are
11791
+ // read here, but the encoding that is actually used is chosen during
11792
+ // glyph mapping in the font.
11793
+ // TODO: Loading the built in encoding in the font would allow the
11794
+ // differences to be merged in here not require us to hold on to it.
11795
+ var differences = [];
11796
+ var baseEncodingName = null;
11797
+ var encoding;
11798
+ if (dict.has('Encoding')) {
11799
+ encoding = dict.get('Encoding');
11800
+ if (isDict(encoding)) {
11801
+ baseEncodingName = encoding.get('BaseEncoding');
11802
+ baseEncodingName = (isName(baseEncodingName) ?
11803
+ baseEncodingName.name : null);
11804
+ // Load the differences between the base and original
11805
+ if (encoding.has('Differences')) {
11806
+ var diffEncoding = encoding.get('Differences');
11807
+ var index = 0;
11808
+ for (var j = 0, jj = diffEncoding.length; j < jj; j++) {
11809
+ var data = diffEncoding[j];
11810
+ if (isNum(data)) {
11811
+ index = data;
11812
+ } else {
11813
+ differences[index++] = data.name;
11814
+ }
11815
+ }
11816
+ }
11817
+ } else if (isName(encoding)) {
11818
+ baseEncodingName = encoding.name;
11819
+ } else {
11820
+ error('Encoding is not a Name nor a Dict');
11821
+ }
11822
+ // According to table 114 if the encoding is a named encoding it must be
11823
+ // one of these predefined encodings.
11824
+ if ((baseEncodingName !== 'MacRomanEncoding' &&
11825
+ baseEncodingName !== 'MacExpertEncoding' &&
11826
+ baseEncodingName !== 'WinAnsiEncoding')) {
11827
+ baseEncodingName = null;
11828
+ }
11829
+ }
11830
+
11831
+ if (baseEncodingName) {
11832
+ properties.defaultEncoding = Encodings[baseEncodingName].slice();
11833
+ } else {
11834
+ encoding = (properties.type === 'TrueType' ?
11835
+ Encodings.WinAnsiEncoding : Encodings.StandardEncoding);
11836
+ // The Symbolic attribute can be misused for regular fonts
11837
+ // Heuristic: we have to check if the font is a standard one also
11838
+ if (!!(properties.flags & FontFlags.Symbolic)) {
11839
+ encoding = Encodings.MacRomanEncoding;
11840
+ if (!properties.file) {
11841
+ if (/Symbol/i.test(properties.name)) {
11842
+ encoding = Encodings.SymbolSetEncoding;
11843
+ } else if (/Dingbats/i.test(properties.name)) {
11844
+ encoding = Encodings.ZapfDingbatsEncoding;
11845
+ }
11846
+ }
11847
+ }
11848
+ properties.defaultEncoding = encoding;
11849
+ }
11850
+
11851
+ properties.differences = differences;
11852
+ properties.baseEncodingName = baseEncodingName;
11853
+ properties.dict = dict;
11854
+ },
11855
+
11856
+ readToUnicode: function PartialEvaluator_readToUnicode(toUnicode) {
11857
+ var cmap, cmapObj = toUnicode;
11858
+ if (isName(cmapObj)) {
11859
+ cmap = CMapFactory.create(cmapObj,
11860
+ { url: PDFJS.cMapUrl, packed: PDFJS.cMapPacked }, null);
11861
+ if (cmap instanceof IdentityCMap) {
11862
+ return new IdentityToUnicodeMap(0, 0xFFFF);
11863
+ }
11864
+ return new ToUnicodeMap(cmap.getMap());
11865
+ } else if (isStream(cmapObj)) {
11866
+ cmap = CMapFactory.create(cmapObj,
11867
+ { url: PDFJS.cMapUrl, packed: PDFJS.cMapPacked }, null);
11868
+ if (cmap instanceof IdentityCMap) {
11869
+ return new IdentityToUnicodeMap(0, 0xFFFF);
11870
+ }
11871
+ cmap = cmap.getMap();
11872
+ // Convert UTF-16BE
11873
+ // NOTE: cmap can be a sparse array, so use forEach instead of for(;;)
11874
+ // to iterate over all keys.
11875
+ cmap.forEach(function(token, i) {
11876
+ var str = [];
11877
+ for (var k = 0; k < token.length; k += 2) {
11878
+ var w1 = (token.charCodeAt(k) << 8) | token.charCodeAt(k + 1);
11879
+ if ((w1 & 0xF800) !== 0xD800) { // w1 < 0xD800 || w1 > 0xDFFF
11880
+ str.push(w1);
11881
+ continue;
11882
+ }
11883
+ k += 2;
11884
+ var w2 = (token.charCodeAt(k) << 8) | token.charCodeAt(k + 1);
11885
+ str.push(((w1 & 0x3ff) << 10) + (w2 & 0x3ff) + 0x10000);
11886
+ }
11887
+ cmap[i] = String.fromCharCode.apply(String, str);
11888
+ });
11889
+ return new ToUnicodeMap(cmap);
11890
+ }
11891
+ return null;
11892
+ },
11893
+
11894
+ readCidToGidMap: function PartialEvaluator_readCidToGidMap(cidToGidStream) {
11895
+ // Extract the encoding from the CIDToGIDMap
11896
+ var glyphsData = cidToGidStream.getBytes();
11897
+
11898
+ // Set encoding 0 to later verify the font has an encoding
11899
+ var result = [];
11900
+ for (var j = 0, jj = glyphsData.length; j < jj; j++) {
11901
+ var glyphID = (glyphsData[j++] << 8) | glyphsData[j];
11902
+ if (glyphID === 0) {
11903
+ continue;
11904
+ }
11905
+ var code = j >> 1;
11906
+ result[code] = glyphID;
11907
+ }
11908
+ return result;
11909
+ },
11910
+
11911
+ extractWidths: function PartialEvaluator_extractWidths(dict, xref,
11912
+ descriptor,
11913
+ properties) {
11914
+ var glyphsWidths = [];
11915
+ var defaultWidth = 0;
11916
+ var glyphsVMetrics = [];
11917
+ var defaultVMetrics;
11918
+ var i, ii, j, jj, start, code, widths;
11919
+ if (properties.composite) {
11920
+ defaultWidth = dict.get('DW') || 1000;
11921
+
11922
+ widths = dict.get('W');
11923
+ if (widths) {
11924
+ for (i = 0, ii = widths.length; i < ii; i++) {
11925
+ start = widths[i++];
11926
+ code = xref.fetchIfRef(widths[i]);
11927
+ if (isArray(code)) {
11928
+ for (j = 0, jj = code.length; j < jj; j++) {
11929
+ glyphsWidths[start++] = code[j];
11930
+ }
11931
+ } else {
11932
+ var width = widths[++i];
11933
+ for (j = start; j <= code; j++) {
11934
+ glyphsWidths[j] = width;
11935
+ }
11936
+ }
11937
+ }
11938
+ }
11939
+
11940
+ if (properties.vertical) {
11941
+ var vmetrics = (dict.get('DW2') || [880, -1000]);
11942
+ defaultVMetrics = [vmetrics[1], defaultWidth * 0.5, vmetrics[0]];
11943
+ vmetrics = dict.get('W2');
11944
+ if (vmetrics) {
11945
+ for (i = 0, ii = vmetrics.length; i < ii; i++) {
11946
+ start = vmetrics[i++];
11947
+ code = xref.fetchIfRef(vmetrics[i]);
11948
+ if (isArray(code)) {
11949
+ for (j = 0, jj = code.length; j < jj; j++) {
11950
+ glyphsVMetrics[start++] = [code[j++], code[j++], code[j]];
11951
+ }
11952
+ } else {
11953
+ var vmetric = [vmetrics[++i], vmetrics[++i], vmetrics[++i]];
11954
+ for (j = start; j <= code; j++) {
11955
+ glyphsVMetrics[j] = vmetric;
11956
+ }
11957
+ }
11958
+ }
11959
+ }
11960
+ }
11961
+ } else {
11962
+ var firstChar = properties.firstChar;
11963
+ widths = dict.get('Widths');
11964
+ if (widths) {
11965
+ j = firstChar;
11966
+ for (i = 0, ii = widths.length; i < ii; i++) {
11967
+ glyphsWidths[j++] = widths[i];
11968
+ }
11969
+ defaultWidth = (parseFloat(descriptor.get('MissingWidth')) || 0);
11970
+ } else {
11971
+ // Trying get the BaseFont metrics (see comment above).
11972
+ var baseFontName = dict.get('BaseFont');
11973
+ if (isName(baseFontName)) {
11974
+ var metrics = this.getBaseFontMetrics(baseFontName.name);
11975
+
11976
+ glyphsWidths = this.buildCharCodeToWidth(metrics.widths,
11977
+ properties);
11978
+ defaultWidth = metrics.defaultWidth;
11979
+ }
11980
+ }
11981
+ }
11982
+
11983
+ // Heuristic: detection of monospace font by checking all non-zero widths
11984
+ var isMonospace = true;
11985
+ var firstWidth = defaultWidth;
11986
+ for (var glyph in glyphsWidths) {
11987
+ var glyphWidth = glyphsWidths[glyph];
11988
+ if (!glyphWidth) {
11989
+ continue;
11990
+ }
11991
+ if (!firstWidth) {
11992
+ firstWidth = glyphWidth;
11993
+ continue;
11994
+ }
11995
+ if (firstWidth !== glyphWidth) {
11996
+ isMonospace = false;
11997
+ break;
11998
+ }
11999
+ }
12000
+ if (isMonospace) {
12001
+ properties.flags |= FontFlags.FixedPitch;
12002
+ }
12003
+
12004
+ properties.defaultWidth = defaultWidth;
12005
+ properties.widths = glyphsWidths;
12006
+ properties.defaultVMetrics = defaultVMetrics;
12007
+ properties.vmetrics = glyphsVMetrics;
12008
+ },
12009
+
12010
+ isSerifFont: function PartialEvaluator_isSerifFont(baseFontName) {
12011
+ // Simulating descriptor flags attribute
12012
+ var fontNameWoStyle = baseFontName.split('-')[0];
12013
+ return (fontNameWoStyle in serifFonts) ||
12014
+ (fontNameWoStyle.search(/serif/gi) !== -1);
12015
+ },
12016
+
12017
+ getBaseFontMetrics: function PartialEvaluator_getBaseFontMetrics(name) {
12018
+ var defaultWidth = 0;
12019
+ var widths = [];
12020
+ var monospace = false;
12021
+ var lookupName = (stdFontMap[name] || name);
12022
+
12023
+ if (!(lookupName in Metrics)) {
12024
+ // Use default fonts for looking up font metrics if the passed
12025
+ // font is not a base font
12026
+ if (this.isSerifFont(name)) {
12027
+ lookupName = 'Times-Roman';
12028
+ } else {
12029
+ lookupName = 'Helvetica';
12030
+ }
12031
+ }
12032
+ var glyphWidths = Metrics[lookupName];
12033
+
12034
+ if (isNum(glyphWidths)) {
12035
+ defaultWidth = glyphWidths;
12036
+ monospace = true;
12037
+ } else {
12038
+ widths = glyphWidths;
12039
+ }
12040
+
12041
+ return {
12042
+ defaultWidth: defaultWidth,
12043
+ monospace: monospace,
12044
+ widths: widths
12045
+ };
12046
+ },
12047
+
12048
+ buildCharCodeToWidth:
12049
+ function PartialEvaluator_bulildCharCodeToWidth(widthsByGlyphName,
12050
+ properties) {
12051
+ var widths = Object.create(null);
12052
+ var differences = properties.differences;
12053
+ var encoding = properties.defaultEncoding;
12054
+ for (var charCode = 0; charCode < 256; charCode++) {
12055
+ if (charCode in differences &&
12056
+ widthsByGlyphName[differences[charCode]]) {
12057
+ widths[charCode] = widthsByGlyphName[differences[charCode]];
12058
+ continue;
12059
+ }
12060
+ if (charCode in encoding && widthsByGlyphName[encoding[charCode]]) {
12061
+ widths[charCode] = widthsByGlyphName[encoding[charCode]];
12062
+ continue;
12063
+ }
12064
+ }
12065
+ return widths;
12066
+ },
12067
+
12068
+ preEvaluateFont: function PartialEvaluator_preEvaluateFont(dict, xref) {
12069
+ var baseDict = dict;
12070
+ var type = dict.get('Subtype');
12071
+ assert(isName(type), 'invalid font Subtype');
12072
+
12073
+ var composite = false;
12074
+ var uint8array;
12075
+ if (type.name === 'Type0') {
12076
+ // If font is a composite
12077
+ // - get the descendant font
12078
+ // - set the type according to the descendant font
12079
+ // - get the FontDescriptor from the descendant font
12080
+ var df = dict.get('DescendantFonts');
12081
+ if (!df) {
12082
+ error('Descendant fonts are not specified');
12083
+ }
12084
+ dict = (isArray(df) ? xref.fetchIfRef(df[0]) : df);
12085
+
12086
+ type = dict.get('Subtype');
12087
+ assert(isName(type), 'invalid font Subtype');
12088
+ composite = true;
12089
+ }
12090
+
12091
+ var descriptor = dict.get('FontDescriptor');
12092
+ if (descriptor) {
12093
+ var hash = new MurmurHash3_64();
12094
+ var encoding = baseDict.getRaw('Encoding');
12095
+ if (isName(encoding)) {
12096
+ hash.update(encoding.name);
12097
+ } else if (isRef(encoding)) {
12098
+ hash.update(encoding.num + '_' + encoding.gen);
12099
+ } else if (isDict(encoding)) {
12100
+ var keys = encoding.getKeys();
12101
+ for (var i = 0, ii = keys.length; i < ii; i++) {
12102
+ var entry = encoding.getRaw(keys[i]);
12103
+ if (isName(entry)) {
12104
+ hash.update(entry.name);
12105
+ } else if (isRef(entry)) {
12106
+ hash.update(entry.num + '_' + entry.gen);
12107
+ } else if (isArray(entry)) { // 'Differences' entry.
12108
+ // Ideally we should check the contents of the array, but to avoid
12109
+ // parsing it here and then again in |extractDataStructures|,
12110
+ // we only use the array length for now (fixes bug1157493.pdf).
12111
+ hash.update(entry.length.toString());
12112
+ }
12113
+ }
12114
+ }
12115
+
12116
+ var toUnicode = dict.get('ToUnicode') || baseDict.get('ToUnicode');
12117
+ if (isStream(toUnicode)) {
12118
+ var stream = toUnicode.str || toUnicode;
12119
+ uint8array = stream.buffer ?
12120
+ new Uint8Array(stream.buffer.buffer, 0, stream.bufferLength) :
12121
+ new Uint8Array(stream.bytes.buffer,
12122
+ stream.start, stream.end - stream.start);
12123
+ hash.update(uint8array);
12124
+
12125
+ } else if (isName(toUnicode)) {
12126
+ hash.update(toUnicode.name);
12127
+ }
12128
+
12129
+ var widths = dict.get('Widths') || baseDict.get('Widths');
12130
+ if (widths) {
12131
+ uint8array = new Uint8Array(new Uint32Array(widths).buffer);
12132
+ hash.update(uint8array);
12133
+ }
12134
+ }
12135
+
12136
+ return {
12137
+ descriptor: descriptor,
12138
+ dict: dict,
12139
+ baseDict: baseDict,
12140
+ composite: composite,
12141
+ type: type.name,
12142
+ hash: hash ? hash.hexdigest() : ''
12143
+ };
12144
+ },
12145
+
12146
+ translateFont: function PartialEvaluator_translateFont(preEvaluatedFont,
12147
+ xref) {
12148
+ var baseDict = preEvaluatedFont.baseDict;
12149
+ var dict = preEvaluatedFont.dict;
12150
+ var composite = preEvaluatedFont.composite;
12151
+ var descriptor = preEvaluatedFont.descriptor;
12152
+ var type = preEvaluatedFont.type;
12153
+ var maxCharIndex = (composite ? 0xFFFF : 0xFF);
12154
+ var properties;
12155
+
12156
+ if (!descriptor) {
12157
+ if (type === 'Type3') {
12158
+ // FontDescriptor is only required for Type3 fonts when the document
12159
+ // is a tagged pdf. Create a barbebones one to get by.
12160
+ descriptor = new Dict(null);
12161
+ descriptor.set('FontName', Name.get(type));
12162
+ } else {
12163
+ // Before PDF 1.5 if the font was one of the base 14 fonts, having a
12164
+ // FontDescriptor was not required.
12165
+ // This case is here for compatibility.
12166
+ var baseFontName = dict.get('BaseFont');
12167
+ if (!isName(baseFontName)) {
12168
+ error('Base font is not specified');
12169
+ }
12170
+
12171
+ // Using base font name as a font name.
12172
+ baseFontName = baseFontName.name.replace(/[,_]/g, '-');
12173
+ var metrics = this.getBaseFontMetrics(baseFontName);
12174
+
12175
+ // Simulating descriptor flags attribute
12176
+ var fontNameWoStyle = baseFontName.split('-')[0];
12177
+ var flags =
12178
+ (this.isSerifFont(fontNameWoStyle) ? FontFlags.Serif : 0) |
12179
+ (metrics.monospace ? FontFlags.FixedPitch : 0) |
12180
+ (symbolsFonts[fontNameWoStyle] ? FontFlags.Symbolic :
12181
+ FontFlags.Nonsymbolic);
12182
+
12183
+ properties = {
12184
+ type: type,
12185
+ name: baseFontName,
12186
+ widths: metrics.widths,
12187
+ defaultWidth: metrics.defaultWidth,
12188
+ flags: flags,
12189
+ firstChar: 0,
12190
+ lastChar: maxCharIndex
12191
+ };
12192
+ this.extractDataStructures(dict, dict, xref, properties);
12193
+ properties.widths = this.buildCharCodeToWidth(metrics.widths,
12194
+ properties);
12195
+ return new Font(baseFontName, null, properties);
12196
+ }
12197
+ }
12198
+
12199
+ // According to the spec if 'FontDescriptor' is declared, 'FirstChar',
12200
+ // 'LastChar' and 'Widths' should exist too, but some PDF encoders seem
12201
+ // to ignore this rule when a variant of a standart font is used.
12202
+ // TODO Fill the width array depending on which of the base font this is
12203
+ // a variant.
12204
+ var firstChar = (dict.get('FirstChar') || 0);
12205
+ var lastChar = (dict.get('LastChar') || maxCharIndex);
12206
+
12207
+ var fontName = descriptor.get('FontName');
12208
+ var baseFont = dict.get('BaseFont');
12209
+ // Some bad PDFs have a string as the font name.
12210
+ if (isString(fontName)) {
12211
+ fontName = Name.get(fontName);
12212
+ }
12213
+ if (isString(baseFont)) {
12214
+ baseFont = Name.get(baseFont);
12215
+ }
12216
+
12217
+ if (type !== 'Type3') {
12218
+ var fontNameStr = fontName && fontName.name;
12219
+ var baseFontStr = baseFont && baseFont.name;
12220
+ if (fontNameStr !== baseFontStr) {
12221
+ info('The FontDescriptor\'s FontName is "' + fontNameStr +
12222
+ '" but should be the same as the Font\'s BaseFont "' +
12223
+ baseFontStr + '"');
12224
+ // Workaround for cases where e.g. fontNameStr = 'Arial' and
12225
+ // baseFontStr = 'Arial,Bold' (needed when no font file is embedded).
12226
+ if (fontNameStr && baseFontStr &&
12227
+ baseFontStr.indexOf(fontNameStr) === 0) {
12228
+ fontName = baseFont;
12229
+ }
12230
+ }
12231
+ }
12232
+ fontName = (fontName || baseFont);
12233
+
12234
+ assert(isName(fontName), 'invalid font name');
12235
+
12236
+ var fontFile = descriptor.get('FontFile', 'FontFile2', 'FontFile3');
12237
+ if (fontFile) {
12238
+ if (fontFile.dict) {
12239
+ var subtype = fontFile.dict.get('Subtype');
12240
+ if (subtype) {
12241
+ subtype = subtype.name;
12242
+ }
12243
+ var length1 = fontFile.dict.get('Length1');
12244
+ var length2 = fontFile.dict.get('Length2');
12245
+ }
12246
+ }
12247
+
12248
+ properties = {
12249
+ type: type,
12250
+ name: fontName.name,
12251
+ subtype: subtype,
12252
+ file: fontFile,
12253
+ length1: length1,
12254
+ length2: length2,
12255
+ loadedName: baseDict.loadedName,
12256
+ composite: composite,
12257
+ wideChars: composite,
12258
+ fixedPitch: false,
12259
+ fontMatrix: (dict.get('FontMatrix') || FONT_IDENTITY_MATRIX),
12260
+ firstChar: firstChar || 0,
12261
+ lastChar: (lastChar || maxCharIndex),
12262
+ bbox: descriptor.get('FontBBox'),
12263
+ ascent: descriptor.get('Ascent'),
12264
+ descent: descriptor.get('Descent'),
12265
+ xHeight: descriptor.get('XHeight'),
12266
+ capHeight: descriptor.get('CapHeight'),
12267
+ flags: descriptor.get('Flags'),
12268
+ italicAngle: descriptor.get('ItalicAngle'),
12269
+ coded: false
12270
+ };
12271
+
12272
+ if (composite) {
12273
+ var cidEncoding = baseDict.get('Encoding');
12274
+ if (isName(cidEncoding)) {
12275
+ properties.cidEncoding = cidEncoding.name;
12276
+ }
12277
+ properties.cMap = CMapFactory.create(cidEncoding,
12278
+ { url: PDFJS.cMapUrl, packed: PDFJS.cMapPacked }, null);
12279
+ properties.vertical = properties.cMap.vertical;
12280
+ }
12281
+ this.extractDataStructures(dict, baseDict, xref, properties);
12282
+ this.extractWidths(dict, xref, descriptor, properties);
12283
+
12284
+ if (type === 'Type3') {
12285
+ properties.isType3Font = true;
12286
+ }
12287
+
12288
+ return new Font(fontName.name, fontFile, properties);
12289
+ }
12290
+ };
12291
+
12292
+ return PartialEvaluator;
12293
+ })();
12294
+
12295
+ var TranslatedFont = (function TranslatedFontClosure() {
12296
+ function TranslatedFont(loadedName, font, dict) {
12297
+ this.loadedName = loadedName;
12298
+ this.font = font;
12299
+ this.dict = dict;
12300
+ this.type3Loaded = null;
12301
+ this.sent = false;
12302
+ }
12303
+ TranslatedFont.prototype = {
12304
+ send: function (handler) {
12305
+ if (this.sent) {
12306
+ return;
12307
+ }
12308
+ var fontData = this.font.exportData();
12309
+ handler.send('commonobj', [
12310
+ this.loadedName,
12311
+ 'Font',
12312
+ fontData
12313
+ ]);
12314
+ this.sent = true;
12315
+ },
12316
+ loadType3Data: function (evaluator, resources, parentOperatorList) {
12317
+ assert(this.font.isType3Font);
12318
+
12319
+ if (this.type3Loaded) {
12320
+ return this.type3Loaded;
12321
+ }
12322
+
12323
+ var translatedFont = this.font;
12324
+ var loadCharProcsPromise = Promise.resolve();
12325
+ var charProcs = this.dict.get('CharProcs').getAll();
12326
+ var fontResources = this.dict.get('Resources') || resources;
12327
+ var charProcKeys = Object.keys(charProcs);
12328
+ var charProcOperatorList = {};
12329
+ for (var i = 0, n = charProcKeys.length; i < n; ++i) {
12330
+ loadCharProcsPromise = loadCharProcsPromise.then(function (key) {
12331
+ var glyphStream = charProcs[key];
12332
+ var operatorList = new OperatorList();
12333
+ return evaluator.getOperatorList(glyphStream, fontResources,
12334
+ operatorList).then(function () {
12335
+ charProcOperatorList[key] = operatorList.getIR();
12336
+
12337
+ // Add the dependencies to the parent operator list so they are
12338
+ // resolved before sub operator list is executed synchronously.
12339
+ parentOperatorList.addDependencies(operatorList.dependencies);
12340
+ }, function (reason) {
12341
+ warn('Type3 font resource \"' + key + '\" is not available');
12342
+ var operatorList = new OperatorList();
12343
+ charProcOperatorList[key] = operatorList.getIR();
12344
+ });
12345
+ }.bind(this, charProcKeys[i]));
12346
+ }
12347
+ this.type3Loaded = loadCharProcsPromise.then(function () {
12348
+ translatedFont.charProcOperatorList = charProcOperatorList;
12349
+ });
12350
+ return this.type3Loaded;
12351
+ }
12352
+ };
12353
+ return TranslatedFont;
12354
+ })();
12355
+
12356
+ var OperatorList = (function OperatorListClosure() {
12357
+ var CHUNK_SIZE = 1000;
12358
+ var CHUNK_SIZE_ABOUT = CHUNK_SIZE - 5; // close to chunk size
12359
+
12360
+ function getTransfers(queue) {
12361
+ var transfers = [];
12362
+ var fnArray = queue.fnArray, argsArray = queue.argsArray;
12363
+ for (var i = 0, ii = queue.length; i < ii; i++) {
12364
+ switch (fnArray[i]) {
12365
+ case OPS.paintInlineImageXObject:
12366
+ case OPS.paintInlineImageXObjectGroup:
12367
+ case OPS.paintImageMaskXObject:
12368
+ var arg = argsArray[i][0]; // first param in imgData
12369
+ if (!arg.cached) {
12370
+ transfers.push(arg.data.buffer);
12371
+ }
12372
+ break;
12373
+ }
12374
+ }
12375
+ return transfers;
12376
+ }
12377
+
12378
+ function OperatorList(intent, messageHandler, pageIndex) {
12379
+ this.messageHandler = messageHandler;
12380
+ this.fnArray = [];
12381
+ this.argsArray = [];
12382
+ this.dependencies = {};
12383
+ this.pageIndex = pageIndex;
12384
+ this.intent = intent;
12385
+ }
12386
+
12387
+ OperatorList.prototype = {
12388
+ get length() {
12389
+ return this.argsArray.length;
12390
+ },
12391
+
12392
+ addOp: function(fn, args) {
12393
+ this.fnArray.push(fn);
12394
+ this.argsArray.push(args);
12395
+ if (this.messageHandler) {
12396
+ if (this.fnArray.length >= CHUNK_SIZE) {
12397
+ this.flush();
12398
+ } else if (this.fnArray.length >= CHUNK_SIZE_ABOUT &&
12399
+ (fn === OPS.restore || fn === OPS.endText)) {
12400
+ // heuristic to flush on boundary of restore or endText
12401
+ this.flush();
12402
+ }
12403
+ }
12404
+ },
12405
+
12406
+ addDependency: function(dependency) {
12407
+ if (dependency in this.dependencies) {
12408
+ return;
12409
+ }
12410
+ this.dependencies[dependency] = true;
12411
+ this.addOp(OPS.dependency, [dependency]);
12412
+ },
12413
+
12414
+ addDependencies: function(dependencies) {
12415
+ for (var key in dependencies) {
12416
+ this.addDependency(key);
12417
+ }
12418
+ },
12419
+
12420
+ addOpList: function(opList) {
12421
+ Util.extendObj(this.dependencies, opList.dependencies);
12422
+ for (var i = 0, ii = opList.length; i < ii; i++) {
12423
+ this.addOp(opList.fnArray[i], opList.argsArray[i]);
12424
+ }
12425
+ },
12426
+
12427
+ getIR: function() {
12428
+ return {
12429
+ fnArray: this.fnArray,
12430
+ argsArray: this.argsArray,
12431
+ length: this.length
12432
+ };
12433
+ },
12434
+
12435
+ flush: function(lastChunk) {
12436
+ if (this.intent !== 'oplist') {
12437
+ new QueueOptimizer().optimize(this);
12438
+ }
12439
+ var transfers = getTransfers(this);
12440
+ this.messageHandler.send('RenderPageChunk', {
12441
+ operatorList: {
12442
+ fnArray: this.fnArray,
12443
+ argsArray: this.argsArray,
12444
+ lastChunk: lastChunk,
12445
+ length: this.length
12446
+ },
12447
+ pageIndex: this.pageIndex,
12448
+ intent: this.intent
12449
+ }, transfers);
12450
+ this.dependencies = {};
12451
+ this.fnArray.length = 0;
12452
+ this.argsArray.length = 0;
12453
+ }
12454
+ };
12455
+
12456
+ return OperatorList;
12457
+ })();
12458
+
12459
+ var StateManager = (function StateManagerClosure() {
12460
+ function StateManager(initialState) {
12461
+ this.state = initialState;
12462
+ this.stateStack = [];
12463
+ }
12464
+ StateManager.prototype = {
12465
+ save: function () {
12466
+ var old = this.state;
12467
+ this.stateStack.push(this.state);
12468
+ this.state = old.clone();
12469
+ },
12470
+ restore: function () {
12471
+ var prev = this.stateStack.pop();
12472
+ if (prev) {
12473
+ this.state = prev;
12474
+ }
12475
+ },
12476
+ transform: function (args) {
12477
+ this.state.ctm = Util.transform(this.state.ctm, args);
12478
+ }
12479
+ };
12480
+ return StateManager;
12481
+ })();
12482
+
12483
+ var TextState = (function TextStateClosure() {
12484
+ function TextState() {
12485
+ this.ctm = new Float32Array(IDENTITY_MATRIX);
12486
+ this.fontSize = 0;
12487
+ this.font = null;
12488
+ this.fontMatrix = FONT_IDENTITY_MATRIX;
12489
+ this.textMatrix = IDENTITY_MATRIX.slice();
12490
+ this.textLineMatrix = IDENTITY_MATRIX.slice();
12491
+ this.charSpacing = 0;
12492
+ this.wordSpacing = 0;
12493
+ this.leading = 0;
12494
+ this.textHScale = 1;
12495
+ this.textRise = 0;
12496
+ }
12497
+
12498
+ TextState.prototype = {
12499
+ setTextMatrix: function TextState_setTextMatrix(a, b, c, d, e, f) {
12500
+ var m = this.textMatrix;
12501
+ m[0] = a; m[1] = b; m[2] = c; m[3] = d; m[4] = e; m[5] = f;
12502
+ },
12503
+ setTextLineMatrix: function TextState_setTextMatrix(a, b, c, d, e, f) {
12504
+ var m = this.textLineMatrix;
12505
+ m[0] = a; m[1] = b; m[2] = c; m[3] = d; m[4] = e; m[5] = f;
12506
+ },
12507
+ translateTextMatrix: function TextState_translateTextMatrix(x, y) {
12508
+ var m = this.textMatrix;
12509
+ m[4] = m[0] * x + m[2] * y + m[4];
12510
+ m[5] = m[1] * x + m[3] * y + m[5];
12511
+ },
12512
+ translateTextLineMatrix: function TextState_translateTextMatrix(x, y) {
12513
+ var m = this.textLineMatrix;
12514
+ m[4] = m[0] * x + m[2] * y + m[4];
12515
+ m[5] = m[1] * x + m[3] * y + m[5];
12516
+ },
12517
+ calcRenderMatrix: function TextState_calcRendeMatrix(ctm) {
12518
+ // 9.4.4 Text Space Details
12519
+ var tsm = [this.fontSize * this.textHScale, 0,
12520
+ 0, this.fontSize,
12521
+ 0, this.textRise];
12522
+ return Util.transform(ctm, Util.transform(this.textMatrix, tsm));
12523
+ },
12524
+ carriageReturn: function TextState_carriageReturn() {
12525
+ this.translateTextLineMatrix(0, -this.leading);
12526
+ this.textMatrix = this.textLineMatrix.slice();
12527
+ },
12528
+ clone: function TextState_clone() {
12529
+ var clone = Object.create(this);
12530
+ clone.textMatrix = this.textMatrix.slice();
12531
+ clone.textLineMatrix = this.textLineMatrix.slice();
12532
+ clone.fontMatrix = this.fontMatrix.slice();
12533
+ return clone;
12534
+ }
12535
+ };
12536
+ return TextState;
12537
+ })();
12538
+
12539
+ var EvalState = (function EvalStateClosure() {
12540
+ function EvalState() {
12541
+ this.ctm = new Float32Array(IDENTITY_MATRIX);
12542
+ this.font = null;
12543
+ this.textRenderingMode = TextRenderingMode.FILL;
12544
+ this.fillColorSpace = ColorSpace.singletons.gray;
12545
+ this.strokeColorSpace = ColorSpace.singletons.gray;
12546
+ }
12547
+ EvalState.prototype = {
12548
+ clone: function CanvasExtraState_clone() {
12549
+ return Object.create(this);
12550
+ },
12551
+ };
12552
+ return EvalState;
12553
+ })();
12554
+
12555
+ var EvaluatorPreprocessor = (function EvaluatorPreprocessorClosure() {
12556
+ // Specifies properties for each command
12557
+ //
12558
+ // If variableArgs === true: [0, `numArgs`] expected
12559
+ // If variableArgs === false: exactly `numArgs` expected
12560
+ var OP_MAP = {
12561
+ // Graphic state
12562
+ w: { id: OPS.setLineWidth, numArgs: 1, variableArgs: false },
12563
+ J: { id: OPS.setLineCap, numArgs: 1, variableArgs: false },
12564
+ j: { id: OPS.setLineJoin, numArgs: 1, variableArgs: false },
12565
+ M: { id: OPS.setMiterLimit, numArgs: 1, variableArgs: false },
12566
+ d: { id: OPS.setDash, numArgs: 2, variableArgs: false },
12567
+ ri: { id: OPS.setRenderingIntent, numArgs: 1, variableArgs: false },
12568
+ i: { id: OPS.setFlatness, numArgs: 1, variableArgs: false },
12569
+ gs: { id: OPS.setGState, numArgs: 1, variableArgs: false },
12570
+ q: { id: OPS.save, numArgs: 0, variableArgs: false },
12571
+ Q: { id: OPS.restore, numArgs: 0, variableArgs: false },
12572
+ cm: { id: OPS.transform, numArgs: 6, variableArgs: false },
12573
+
12574
+ // Path
12575
+ m: { id: OPS.moveTo, numArgs: 2, variableArgs: false },
12576
+ l: { id: OPS.lineTo, numArgs: 2, variableArgs: false },
12577
+ c: { id: OPS.curveTo, numArgs: 6, variableArgs: false },
12578
+ v: { id: OPS.curveTo2, numArgs: 4, variableArgs: false },
12579
+ y: { id: OPS.curveTo3, numArgs: 4, variableArgs: false },
12580
+ h: { id: OPS.closePath, numArgs: 0, variableArgs: false },
12581
+ re: { id: OPS.rectangle, numArgs: 4, variableArgs: false },
12582
+ S: { id: OPS.stroke, numArgs: 0, variableArgs: false },
12583
+ s: { id: OPS.closeStroke, numArgs: 0, variableArgs: false },
12584
+ f: { id: OPS.fill, numArgs: 0, variableArgs: false },
12585
+ F: { id: OPS.fill, numArgs: 0, variableArgs: false },
12586
+ 'f*': { id: OPS.eoFill, numArgs: 0, variableArgs: false },
12587
+ B: { id: OPS.fillStroke, numArgs: 0, variableArgs: false },
12588
+ 'B*': { id: OPS.eoFillStroke, numArgs: 0, variableArgs: false },
12589
+ b: { id: OPS.closeFillStroke, numArgs: 0, variableArgs: false },
12590
+ 'b*': { id: OPS.closeEOFillStroke, numArgs: 0, variableArgs: false },
12591
+ n: { id: OPS.endPath, numArgs: 0, variableArgs: false },
12592
+
12593
+ // Clipping
12594
+ W: { id: OPS.clip, numArgs: 0, variableArgs: false },
12595
+ 'W*': { id: OPS.eoClip, numArgs: 0, variableArgs: false },
12596
+
12597
+ // Text
12598
+ BT: { id: OPS.beginText, numArgs: 0, variableArgs: false },
12599
+ ET: { id: OPS.endText, numArgs: 0, variableArgs: false },
12600
+ Tc: { id: OPS.setCharSpacing, numArgs: 1, variableArgs: false },
12601
+ Tw: { id: OPS.setWordSpacing, numArgs: 1, variableArgs: false },
12602
+ Tz: { id: OPS.setHScale, numArgs: 1, variableArgs: false },
12603
+ TL: { id: OPS.setLeading, numArgs: 1, variableArgs: false },
12604
+ Tf: { id: OPS.setFont, numArgs: 2, variableArgs: false },
12605
+ Tr: { id: OPS.setTextRenderingMode, numArgs: 1, variableArgs: false },
12606
+ Ts: { id: OPS.setTextRise, numArgs: 1, variableArgs: false },
12607
+ Td: { id: OPS.moveText, numArgs: 2, variableArgs: false },
12608
+ TD: { id: OPS.setLeadingMoveText, numArgs: 2, variableArgs: false },
12609
+ Tm: { id: OPS.setTextMatrix, numArgs: 6, variableArgs: false },
12610
+ 'T*': { id: OPS.nextLine, numArgs: 0, variableArgs: false },
12611
+ Tj: { id: OPS.showText, numArgs: 1, variableArgs: false },
12612
+ TJ: { id: OPS.showSpacedText, numArgs: 1, variableArgs: false },
12613
+ '\'': { id: OPS.nextLineShowText, numArgs: 1, variableArgs: false },
12614
+ '"': { id: OPS.nextLineSetSpacingShowText, numArgs: 3,
12615
+ variableArgs: false },
12616
+
12617
+ // Type3 fonts
12618
+ d0: { id: OPS.setCharWidth, numArgs: 2, variableArgs: false },
12619
+ d1: { id: OPS.setCharWidthAndBounds, numArgs: 6, variableArgs: false },
12620
+
12621
+ // Color
12622
+ CS: { id: OPS.setStrokeColorSpace, numArgs: 1, variableArgs: false },
12623
+ cs: { id: OPS.setFillColorSpace, numArgs: 1, variableArgs: false },
12624
+ SC: { id: OPS.setStrokeColor, numArgs: 4, variableArgs: true },
12625
+ SCN: { id: OPS.setStrokeColorN, numArgs: 33, variableArgs: true },
12626
+ sc: { id: OPS.setFillColor, numArgs: 4, variableArgs: true },
12627
+ scn: { id: OPS.setFillColorN, numArgs: 33, variableArgs: true },
12628
+ G: { id: OPS.setStrokeGray, numArgs: 1, variableArgs: false },
12629
+ g: { id: OPS.setFillGray, numArgs: 1, variableArgs: false },
12630
+ RG: { id: OPS.setStrokeRGBColor, numArgs: 3, variableArgs: false },
12631
+ rg: { id: OPS.setFillRGBColor, numArgs: 3, variableArgs: false },
12632
+ K: { id: OPS.setStrokeCMYKColor, numArgs: 4, variableArgs: false },
12633
+ k: { id: OPS.setFillCMYKColor, numArgs: 4, variableArgs: false },
12634
+
12635
+ // Shading
12636
+ sh: { id: OPS.shadingFill, numArgs: 1, variableArgs: false },
12637
+
12638
+ // Images
12639
+ BI: { id: OPS.beginInlineImage, numArgs: 0, variableArgs: false },
12640
+ ID: { id: OPS.beginImageData, numArgs: 0, variableArgs: false },
12641
+ EI: { id: OPS.endInlineImage, numArgs: 1, variableArgs: false },
12642
+
12643
+ // XObjects
12644
+ Do: { id: OPS.paintXObject, numArgs: 1, variableArgs: false },
12645
+ MP: { id: OPS.markPoint, numArgs: 1, variableArgs: false },
12646
+ DP: { id: OPS.markPointProps, numArgs: 2, variableArgs: false },
12647
+ BMC: { id: OPS.beginMarkedContent, numArgs: 1, variableArgs: false },
12648
+ BDC: { id: OPS.beginMarkedContentProps, numArgs: 2,
12649
+ variableArgs: false },
12650
+ EMC: { id: OPS.endMarkedContent, numArgs: 0, variableArgs: false },
12651
+
12652
+ // Compatibility
12653
+ BX: { id: OPS.beginCompat, numArgs: 0, variableArgs: false },
12654
+ EX: { id: OPS.endCompat, numArgs: 0, variableArgs: false },
12655
+
12656
+ // (reserved partial commands for the lexer)
12657
+ BM: null,
12658
+ BD: null,
12659
+ 'true': null,
12660
+ fa: null,
12661
+ fal: null,
12662
+ fals: null,
12663
+ 'false': null,
12664
+ nu: null,
12665
+ nul: null,
12666
+ 'null': null
12667
+ };
12668
+
12669
+ function EvaluatorPreprocessor(stream, xref, stateManager) {
12670
+ // TODO(mduan): pass array of knownCommands rather than OP_MAP
12671
+ // dictionary
12672
+ this.parser = new Parser(new Lexer(stream, OP_MAP), false, xref);
12673
+ this.stateManager = stateManager;
12674
+ this.nonProcessedArgs = [];
12675
+ }
12676
+
12677
+ EvaluatorPreprocessor.prototype = {
12678
+ get savedStatesDepth() {
12679
+ return this.stateManager.stateStack.length;
12680
+ },
12681
+
12682
+ // |operation| is an object with two fields:
12683
+ //
12684
+ // - |fn| is an out param.
12685
+ //
12686
+ // - |args| is an inout param. On entry, it should have one of two values.
12687
+ //
12688
+ // - An empty array. This indicates that the caller is providing the
12689
+ // array in which the args will be stored in. The caller should use
12690
+ // this value if it can reuse a single array for each call to read().
12691
+ //
12692
+ // - |null|. This indicates that the caller needs this function to create
12693
+ // the array in which any args are stored in. If there are zero args,
12694
+ // this function will leave |operation.args| as |null| (thus avoiding
12695
+ // allocations that would occur if we used an empty array to represent
12696
+ // zero arguments). Otherwise, it will replace |null| with a new array
12697
+ // containing the arguments. The caller should use this value if it
12698
+ // cannot reuse an array for each call to read().
12699
+ //
12700
+ // These two modes are present because this function is very hot and so
12701
+ // avoiding allocations where possible is worthwhile.
12702
+ //
12703
+ read: function EvaluatorPreprocessor_read(operation) {
12704
+ var args = operation.args;
12705
+ while (true) {
12706
+ var obj = this.parser.getObj();
12707
+ if (isCmd(obj)) {
12708
+ var cmd = obj.cmd;
12709
+ // Check that the command is valid
12710
+ var opSpec = OP_MAP[cmd];
12711
+ if (!opSpec) {
12712
+ warn('Unknown command "' + cmd + '"');
12713
+ continue;
12714
+ }
12715
+
12716
+ var fn = opSpec.id;
12717
+ var numArgs = opSpec.numArgs;
12718
+ var argsLength = args !== null ? args.length : 0;
12719
+
12720
+ if (!opSpec.variableArgs) {
12721
+ // Postscript commands can be nested, e.g. /F2 /GS2 gs 5.711 Tf
12722
+ if (argsLength !== numArgs) {
12723
+ var nonProcessedArgs = this.nonProcessedArgs;
12724
+ while (argsLength > numArgs) {
12725
+ nonProcessedArgs.push(args.shift());
12726
+ argsLength--;
12727
+ }
12728
+ while (argsLength < numArgs && nonProcessedArgs.length !== 0) {
12729
+ if (!args) {
12730
+ args = [];
12731
+ }
12732
+ args.unshift(nonProcessedArgs.pop());
12733
+ argsLength++;
12734
+ }
12735
+ }
12736
+
12737
+ if (argsLength < numArgs) {
12738
+ // If we receive too few args, it's not possible to possible
12739
+ // to execute the command, so skip the command
12740
+ info('Command ' + fn + ': because expected ' +
12741
+ numArgs + ' args, but received ' + argsLength +
12742
+ ' args; skipping');
12743
+ args = null;
12744
+ continue;
12745
+ }
12746
+ } else if (argsLength > numArgs) {
12747
+ info('Command ' + fn + ': expected [0,' + numArgs +
12748
+ '] args, but received ' + argsLength + ' args');
12749
+ }
12750
+
12751
+ // TODO figure out how to type-check vararg functions
12752
+ this.preprocessCommand(fn, args);
12753
+
12754
+ operation.fn = fn;
12755
+ operation.args = args;
12756
+ return true;
12757
+ } else {
12758
+ if (isEOF(obj)) {
12759
+ return false; // no more commands
12760
+ }
12761
+ // argument
12762
+ if (obj !== null) {
12763
+ if (!args) {
12764
+ args = [];
12765
+ }
12766
+ args.push((obj instanceof Dict ? obj.getAll() : obj));
12767
+ assert(args.length <= 33, 'Too many arguments');
12768
+ }
12769
+ }
12770
+ }
12771
+ },
12772
+
12773
+ preprocessCommand:
12774
+ function EvaluatorPreprocessor_preprocessCommand(fn, args) {
12775
+ switch (fn | 0) {
12776
+ case OPS.save:
12777
+ this.stateManager.save();
12778
+ break;
12779
+ case OPS.restore:
12780
+ this.stateManager.restore();
12781
+ break;
12782
+ case OPS.transform:
12783
+ this.stateManager.transform(args);
12784
+ break;
12785
+ }
12786
+ }
12787
+ };
12788
+ return EvaluatorPreprocessor;
12789
+ })();
12790
+
12791
+ var QueueOptimizer = (function QueueOptimizerClosure() {
12792
+ function addState(parentState, pattern, fn) {
12793
+ var state = parentState;
12794
+ for (var i = 0, ii = pattern.length - 1; i < ii; i++) {
12795
+ var item = pattern[i];
12796
+ state = (state[item] || (state[item] = []));
12797
+ }
12798
+ state[pattern[pattern.length - 1]] = fn;
12799
+ }
12800
+
12801
+ function handlePaintSolidColorImageMask(iFirstSave, count, fnArray,
12802
+ argsArray) {
12803
+ // Handles special case of mainly LaTeX documents which use image masks to
12804
+ // draw lines with the current fill style.
12805
+ // 'count' groups of (save, transform, paintImageMaskXObject, restore)+
12806
+ // have been found at iFirstSave.
12807
+ var iFirstPIMXO = iFirstSave + 2;
12808
+ for (var i = 0; i < count; i++) {
12809
+ var arg = argsArray[iFirstPIMXO + 4 * i];
12810
+ var imageMask = arg.length === 1 && arg[0];
12811
+ if (imageMask && imageMask.width === 1 && imageMask.height === 1 &&
12812
+ (!imageMask.data.length ||
12813
+ (imageMask.data.length === 1 && imageMask.data[0] === 0))) {
12814
+ fnArray[iFirstPIMXO + 4 * i] = OPS.paintSolidColorImageMask;
12815
+ continue;
12816
+ }
12817
+ break;
12818
+ }
12819
+ return count - i;
12820
+ }
12821
+
12822
+ var InitialState = [];
12823
+
12824
+ // This replaces (save, transform, paintInlineImageXObject, restore)+
12825
+ // sequences with one |paintInlineImageXObjectGroup| operation.
12826
+ addState(InitialState,
12827
+ [OPS.save, OPS.transform, OPS.paintInlineImageXObject, OPS.restore],
12828
+ function foundInlineImageGroup(context) {
12829
+ var MIN_IMAGES_IN_INLINE_IMAGES_BLOCK = 10;
12830
+ var MAX_IMAGES_IN_INLINE_IMAGES_BLOCK = 200;
12831
+ var MAX_WIDTH = 1000;
12832
+ var IMAGE_PADDING = 1;
12833
+
12834
+ var fnArray = context.fnArray, argsArray = context.argsArray;
12835
+ var curr = context.iCurr;
12836
+ var iFirstSave = curr - 3;
12837
+ var iFirstTransform = curr - 2;
12838
+ var iFirstPIIXO = curr - 1;
12839
+
12840
+ // Look for the quartets.
12841
+ var i = iFirstSave + 4;
12842
+ var ii = fnArray.length;
12843
+ while (i + 3 < ii) {
12844
+ if (fnArray[i] !== OPS.save ||
12845
+ fnArray[i + 1] !== OPS.transform ||
12846
+ fnArray[i + 2] !== OPS.paintInlineImageXObject ||
12847
+ fnArray[i + 3] !== OPS.restore) {
12848
+ break; // ops don't match
12849
+ }
12850
+ i += 4;
12851
+ }
12852
+
12853
+ // At this point, i is the index of the first op past the last valid
12854
+ // quartet.
12855
+ var count = Math.min((i - iFirstSave) / 4,
12856
+ MAX_IMAGES_IN_INLINE_IMAGES_BLOCK);
12857
+ if (count < MIN_IMAGES_IN_INLINE_IMAGES_BLOCK) {
12858
+ return i;
12859
+ }
12860
+
12861
+ // assuming that heights of those image is too small (~1 pixel)
12862
+ // packing as much as possible by lines
12863
+ var maxX = 0;
12864
+ var map = [], maxLineHeight = 0;
12865
+ var currentX = IMAGE_PADDING, currentY = IMAGE_PADDING;
12866
+ var q;
12867
+ for (q = 0; q < count; q++) {
12868
+ var transform = argsArray[iFirstTransform + (q << 2)];
12869
+ var img = argsArray[iFirstPIIXO + (q << 2)][0];
12870
+ if (currentX + img.width > MAX_WIDTH) {
12871
+ // starting new line
12872
+ maxX = Math.max(maxX, currentX);
12873
+ currentY += maxLineHeight + 2 * IMAGE_PADDING;
12874
+ currentX = 0;
12875
+ maxLineHeight = 0;
12876
+ }
12877
+ map.push({
12878
+ transform: transform,
12879
+ x: currentX, y: currentY,
12880
+ w: img.width, h: img.height
12881
+ });
12882
+ currentX += img.width + 2 * IMAGE_PADDING;
12883
+ maxLineHeight = Math.max(maxLineHeight, img.height);
12884
+ }
12885
+ var imgWidth = Math.max(maxX, currentX) + IMAGE_PADDING;
12886
+ var imgHeight = currentY + maxLineHeight + IMAGE_PADDING;
12887
+ var imgData = new Uint8Array(imgWidth * imgHeight * 4);
12888
+ var imgRowSize = imgWidth << 2;
12889
+ for (q = 0; q < count; q++) {
12890
+ var data = argsArray[iFirstPIIXO + (q << 2)][0].data;
12891
+ // Copy image by lines and extends pixels into padding.
12892
+ var rowSize = map[q].w << 2;
12893
+ var dataOffset = 0;
12894
+ var offset = (map[q].x + map[q].y * imgWidth) << 2;
12895
+ imgData.set(data.subarray(0, rowSize), offset - imgRowSize);
12896
+ for (var k = 0, kk = map[q].h; k < kk; k++) {
12897
+ imgData.set(data.subarray(dataOffset, dataOffset + rowSize), offset);
12898
+ dataOffset += rowSize;
12899
+ offset += imgRowSize;
12900
+ }
12901
+ imgData.set(data.subarray(dataOffset - rowSize, dataOffset), offset);
12902
+ while (offset >= 0) {
12903
+ data[offset - 4] = data[offset];
12904
+ data[offset - 3] = data[offset + 1];
12905
+ data[offset - 2] = data[offset + 2];
12906
+ data[offset - 1] = data[offset + 3];
12907
+ data[offset + rowSize] = data[offset + rowSize - 4];
12908
+ data[offset + rowSize + 1] = data[offset + rowSize - 3];
12909
+ data[offset + rowSize + 2] = data[offset + rowSize - 2];
12910
+ data[offset + rowSize + 3] = data[offset + rowSize - 1];
12911
+ offset -= imgRowSize;
12912
+ }
12913
+ }
12914
+
12915
+ // Replace queue items.
12916
+ fnArray.splice(iFirstSave, count * 4, OPS.paintInlineImageXObjectGroup);
12917
+ argsArray.splice(iFirstSave, count * 4,
12918
+ [{ width: imgWidth, height: imgHeight, kind: ImageKind.RGBA_32BPP,
12919
+ data: imgData }, map]);
12920
+
12921
+ return iFirstSave + 1;
12922
+ });
12923
+
12924
+ // This replaces (save, transform, paintImageMaskXObject, restore)+
12925
+ // sequences with one |paintImageMaskXObjectGroup| or one
12926
+ // |paintImageMaskXObjectRepeat| operation.
12927
+ addState(InitialState,
12928
+ [OPS.save, OPS.transform, OPS.paintImageMaskXObject, OPS.restore],
12929
+ function foundImageMaskGroup(context) {
12930
+ var MIN_IMAGES_IN_MASKS_BLOCK = 10;
12931
+ var MAX_IMAGES_IN_MASKS_BLOCK = 100;
12932
+ var MAX_SAME_IMAGES_IN_MASKS_BLOCK = 1000;
12933
+
12934
+ var fnArray = context.fnArray, argsArray = context.argsArray;
12935
+ var curr = context.iCurr;
12936
+ var iFirstSave = curr - 3;
12937
+ var iFirstTransform = curr - 2;
12938
+ var iFirstPIMXO = curr - 1;
12939
+
12940
+ // Look for the quartets.
12941
+ var i = iFirstSave + 4;
12942
+ var ii = fnArray.length;
12943
+ while (i + 3 < ii) {
12944
+ if (fnArray[i] !== OPS.save ||
12945
+ fnArray[i + 1] !== OPS.transform ||
12946
+ fnArray[i + 2] !== OPS.paintImageMaskXObject ||
12947
+ fnArray[i + 3] !== OPS.restore) {
12948
+ break; // ops don't match
12949
+ }
12950
+ i += 4;
12951
+ }
12952
+
12953
+ // At this point, i is the index of the first op past the last valid
12954
+ // quartet.
12955
+ var count = (i - iFirstSave) / 4;
12956
+ count = handlePaintSolidColorImageMask(iFirstSave, count, fnArray,
12957
+ argsArray);
12958
+ if (count < MIN_IMAGES_IN_MASKS_BLOCK) {
12959
+ return i;
12960
+ }
12961
+
12962
+ var q;
12963
+ var isSameImage = false;
12964
+ var iTransform, transformArgs;
12965
+ var firstPIMXOArg0 = argsArray[iFirstPIMXO][0];
12966
+ if (argsArray[iFirstTransform][1] === 0 &&
12967
+ argsArray[iFirstTransform][2] === 0) {
12968
+ isSameImage = true;
12969
+ var firstTransformArg0 = argsArray[iFirstTransform][0];
12970
+ var firstTransformArg3 = argsArray[iFirstTransform][3];
12971
+ iTransform = iFirstTransform + 4;
12972
+ var iPIMXO = iFirstPIMXO + 4;
12973
+ for (q = 1; q < count; q++, iTransform += 4, iPIMXO += 4) {
12974
+ transformArgs = argsArray[iTransform];
12975
+ if (argsArray[iPIMXO][0] !== firstPIMXOArg0 ||
12976
+ transformArgs[0] !== firstTransformArg0 ||
12977
+ transformArgs[1] !== 0 ||
12978
+ transformArgs[2] !== 0 ||
12979
+ transformArgs[3] !== firstTransformArg3) {
12980
+ if (q < MIN_IMAGES_IN_MASKS_BLOCK) {
12981
+ isSameImage = false;
12982
+ } else {
12983
+ count = q;
12984
+ }
12985
+ break; // different image or transform
12986
+ }
12987
+ }
12988
+ }
12989
+
12990
+ if (isSameImage) {
12991
+ count = Math.min(count, MAX_SAME_IMAGES_IN_MASKS_BLOCK);
12992
+ var positions = new Float32Array(count * 2);
12993
+ iTransform = iFirstTransform;
12994
+ for (q = 0; q < count; q++, iTransform += 4) {
12995
+ transformArgs = argsArray[iTransform];
12996
+ positions[(q << 1)] = transformArgs[4];
12997
+ positions[(q << 1) + 1] = transformArgs[5];
12998
+ }
12999
+
13000
+ // Replace queue items.
13001
+ fnArray.splice(iFirstSave, count * 4, OPS.paintImageMaskXObjectRepeat);
13002
+ argsArray.splice(iFirstSave, count * 4,
13003
+ [firstPIMXOArg0, firstTransformArg0, firstTransformArg3, positions]);
13004
+ } else {
13005
+ count = Math.min(count, MAX_IMAGES_IN_MASKS_BLOCK);
13006
+ var images = [];
13007
+ for (q = 0; q < count; q++) {
13008
+ transformArgs = argsArray[iFirstTransform + (q << 2)];
13009
+ var maskParams = argsArray[iFirstPIMXO + (q << 2)][0];
13010
+ images.push({ data: maskParams.data, width: maskParams.width,
13011
+ height: maskParams.height,
13012
+ transform: transformArgs });
13013
+ }
13014
+
13015
+ // Replace queue items.
13016
+ fnArray.splice(iFirstSave, count * 4, OPS.paintImageMaskXObjectGroup);
13017
+ argsArray.splice(iFirstSave, count * 4, [images]);
13018
+ }
13019
+
13020
+ return iFirstSave + 1;
13021
+ });
13022
+
13023
+ // This replaces (save, transform, paintImageXObject, restore)+ sequences
13024
+ // with one paintImageXObjectRepeat operation, if the |transform| and
13025
+ // |paintImageXObjectRepeat| ops are appropriate.
13026
+ addState(InitialState,
13027
+ [OPS.save, OPS.transform, OPS.paintImageXObject, OPS.restore],
13028
+ function (context) {
13029
+ var MIN_IMAGES_IN_BLOCK = 3;
13030
+ var MAX_IMAGES_IN_BLOCK = 1000;
13031
+
13032
+ var fnArray = context.fnArray, argsArray = context.argsArray;
13033
+ var curr = context.iCurr;
13034
+ var iFirstSave = curr - 3;
13035
+ var iFirstTransform = curr - 2;
13036
+ var iFirstPIXO = curr - 1;
13037
+ var iFirstRestore = curr;
13038
+
13039
+ if (argsArray[iFirstTransform][1] !== 0 ||
13040
+ argsArray[iFirstTransform][2] !== 0) {
13041
+ return iFirstRestore + 1; // transform has the wrong form
13042
+ }
13043
+
13044
+ // Look for the quartets.
13045
+ var firstPIXOArg0 = argsArray[iFirstPIXO][0];
13046
+ var firstTransformArg0 = argsArray[iFirstTransform][0];
13047
+ var firstTransformArg3 = argsArray[iFirstTransform][3];
13048
+ var i = iFirstSave + 4;
13049
+ var ii = fnArray.length;
13050
+ while (i + 3 < ii) {
13051
+ if (fnArray[i] !== OPS.save ||
13052
+ fnArray[i + 1] !== OPS.transform ||
13053
+ fnArray[i + 2] !== OPS.paintImageXObject ||
13054
+ fnArray[i + 3] !== OPS.restore) {
13055
+ break; // ops don't match
13056
+ }
13057
+ if (argsArray[i + 1][0] !== firstTransformArg0 ||
13058
+ argsArray[i + 1][1] !== 0 ||
13059
+ argsArray[i + 1][2] !== 0 ||
13060
+ argsArray[i + 1][3] !== firstTransformArg3) {
13061
+ break; // transforms don't match
13062
+ }
13063
+ if (argsArray[i + 2][0] !== firstPIXOArg0) {
13064
+ break; // images don't match
13065
+ }
13066
+ i += 4;
13067
+ }
13068
+
13069
+ // At this point, i is the index of the first op past the last valid
13070
+ // quartet.
13071
+ var count = Math.min((i - iFirstSave) / 4, MAX_IMAGES_IN_BLOCK);
13072
+ if (count < MIN_IMAGES_IN_BLOCK) {
13073
+ return i;
13074
+ }
13075
+
13076
+ // Extract the (x,y) positions from all of the matching transforms.
13077
+ var positions = new Float32Array(count * 2);
13078
+ var iTransform = iFirstTransform;
13079
+ for (var q = 0; q < count; q++, iTransform += 4) {
13080
+ var transformArgs = argsArray[iTransform];
13081
+ positions[(q << 1)] = transformArgs[4];
13082
+ positions[(q << 1) + 1] = transformArgs[5];
13083
+ }
13084
+
13085
+ // Replace queue items.
13086
+ var args = [firstPIXOArg0, firstTransformArg0, firstTransformArg3,
13087
+ positions];
13088
+ fnArray.splice(iFirstSave, count * 4, OPS.paintImageXObjectRepeat);
13089
+ argsArray.splice(iFirstSave, count * 4, args);
13090
+
13091
+ return iFirstSave + 1;
13092
+ });
13093
+
13094
+ // This replaces (beginText, setFont, setTextMatrix, showText, endText)+
13095
+ // sequences with (beginText, setFont, (setTextMatrix, showText)+, endText)+
13096
+ // sequences, if the font for each one is the same.
13097
+ addState(InitialState,
13098
+ [OPS.beginText, OPS.setFont, OPS.setTextMatrix, OPS.showText, OPS.endText],
13099
+ function (context) {
13100
+ var MIN_CHARS_IN_BLOCK = 3;
13101
+ var MAX_CHARS_IN_BLOCK = 1000;
13102
+
13103
+ var fnArray = context.fnArray, argsArray = context.argsArray;
13104
+ var curr = context.iCurr;
13105
+ var iFirstBeginText = curr - 4;
13106
+ var iFirstSetFont = curr - 3;
13107
+ var iFirstSetTextMatrix = curr - 2;
13108
+ var iFirstShowText = curr - 1;
13109
+ var iFirstEndText = curr;
13110
+
13111
+ // Look for the quintets.
13112
+ var firstSetFontArg0 = argsArray[iFirstSetFont][0];
13113
+ var firstSetFontArg1 = argsArray[iFirstSetFont][1];
13114
+ var i = iFirstBeginText + 5;
13115
+ var ii = fnArray.length;
13116
+ while (i + 4 < ii) {
13117
+ if (fnArray[i] !== OPS.beginText ||
13118
+ fnArray[i + 1] !== OPS.setFont ||
13119
+ fnArray[i + 2] !== OPS.setTextMatrix ||
13120
+ fnArray[i + 3] !== OPS.showText ||
13121
+ fnArray[i + 4] !== OPS.endText) {
13122
+ break; // ops don't match
13123
+ }
13124
+ if (argsArray[i + 1][0] !== firstSetFontArg0 ||
13125
+ argsArray[i + 1][1] !== firstSetFontArg1) {
13126
+ break; // fonts don't match
13127
+ }
13128
+ i += 5;
13129
+ }
13130
+
13131
+ // At this point, i is the index of the first op past the last valid
13132
+ // quintet.
13133
+ var count = Math.min(((i - iFirstBeginText) / 5), MAX_CHARS_IN_BLOCK);
13134
+ if (count < MIN_CHARS_IN_BLOCK) {
13135
+ return i;
13136
+ }
13137
+
13138
+ // If the preceding quintet is (<something>, setFont, setTextMatrix,
13139
+ // showText, endText), include that as well. (E.g. <something> might be
13140
+ // |dependency|.)
13141
+ var iFirst = iFirstBeginText;
13142
+ if (iFirstBeginText >= 4 &&
13143
+ fnArray[iFirstBeginText - 4] === fnArray[iFirstSetFont] &&
13144
+ fnArray[iFirstBeginText - 3] === fnArray[iFirstSetTextMatrix] &&
13145
+ fnArray[iFirstBeginText - 2] === fnArray[iFirstShowText] &&
13146
+ fnArray[iFirstBeginText - 1] === fnArray[iFirstEndText] &&
13147
+ argsArray[iFirstBeginText - 4][0] === firstSetFontArg0 &&
13148
+ argsArray[iFirstBeginText - 4][1] === firstSetFontArg1) {
13149
+ count++;
13150
+ iFirst -= 5;
13151
+ }
13152
+
13153
+ // Remove (endText, beginText, setFont) trios.
13154
+ var iEndText = iFirst + 4;
13155
+ for (var q = 1; q < count; q++) {
13156
+ fnArray.splice(iEndText, 3);
13157
+ argsArray.splice(iEndText, 3);
13158
+ iEndText += 2;
13159
+ }
13160
+
13161
+ return iEndText + 1;
13162
+ });
13163
+
13164
+ function QueueOptimizer() {}
13165
+
13166
+ QueueOptimizer.prototype = {
13167
+ optimize: function QueueOptimizer_optimize(queue) {
13168
+ var fnArray = queue.fnArray, argsArray = queue.argsArray;
13169
+ var context = {
13170
+ iCurr: 0,
13171
+ fnArray: fnArray,
13172
+ argsArray: argsArray
13173
+ };
13174
+ var state;
13175
+ var i = 0, ii = fnArray.length;
13176
+ while (i < ii) {
13177
+ state = (state || InitialState)[fnArray[i]];
13178
+ if (typeof state === 'function') { // we found some handler
13179
+ context.iCurr = i;
13180
+ // state() returns the index of the first non-matching op (if we
13181
+ // didn't match) or the first op past the modified ops (if we did
13182
+ // match and replace).
13183
+ i = state(context);
13184
+ state = undefined; // reset the state machine
13185
+ ii = context.fnArray.length;
13186
+ } else {
13187
+ i++;
13188
+ }
13189
+ }
13190
+ }
13191
+ };
13192
+ return QueueOptimizer;
13193
+ })();
13194
+
13195
+
13196
+ var BUILT_IN_CMAPS = [
13197
+ // << Start unicode maps.
13198
+ 'Adobe-GB1-UCS2',
13199
+ 'Adobe-CNS1-UCS2',
13200
+ 'Adobe-Japan1-UCS2',
13201
+ 'Adobe-Korea1-UCS2',
13202
+ // >> End unicode maps.
13203
+ '78-EUC-H',
13204
+ '78-EUC-V',
13205
+ '78-H',
13206
+ '78-RKSJ-H',
13207
+ '78-RKSJ-V',
13208
+ '78-V',
13209
+ '78ms-RKSJ-H',
13210
+ '78ms-RKSJ-V',
13211
+ '83pv-RKSJ-H',
13212
+ '90ms-RKSJ-H',
13213
+ '90ms-RKSJ-V',
13214
+ '90msp-RKSJ-H',
13215
+ '90msp-RKSJ-V',
13216
+ '90pv-RKSJ-H',
13217
+ '90pv-RKSJ-V',
13218
+ 'Add-H',
13219
+ 'Add-RKSJ-H',
13220
+ 'Add-RKSJ-V',
13221
+ 'Add-V',
13222
+ 'Adobe-CNS1-0',
13223
+ 'Adobe-CNS1-1',
13224
+ 'Adobe-CNS1-2',
13225
+ 'Adobe-CNS1-3',
13226
+ 'Adobe-CNS1-4',
13227
+ 'Adobe-CNS1-5',
13228
+ 'Adobe-CNS1-6',
13229
+ 'Adobe-GB1-0',
13230
+ 'Adobe-GB1-1',
13231
+ 'Adobe-GB1-2',
13232
+ 'Adobe-GB1-3',
13233
+ 'Adobe-GB1-4',
13234
+ 'Adobe-GB1-5',
13235
+ 'Adobe-Japan1-0',
13236
+ 'Adobe-Japan1-1',
13237
+ 'Adobe-Japan1-2',
13238
+ 'Adobe-Japan1-3',
13239
+ 'Adobe-Japan1-4',
13240
+ 'Adobe-Japan1-5',
13241
+ 'Adobe-Japan1-6',
13242
+ 'Adobe-Korea1-0',
13243
+ 'Adobe-Korea1-1',
13244
+ 'Adobe-Korea1-2',
13245
+ 'B5-H',
13246
+ 'B5-V',
13247
+ 'B5pc-H',
13248
+ 'B5pc-V',
13249
+ 'CNS-EUC-H',
13250
+ 'CNS-EUC-V',
13251
+ 'CNS1-H',
13252
+ 'CNS1-V',
13253
+ 'CNS2-H',
13254
+ 'CNS2-V',
13255
+ 'ETHK-B5-H',
13256
+ 'ETHK-B5-V',
13257
+ 'ETen-B5-H',
13258
+ 'ETen-B5-V',
13259
+ 'ETenms-B5-H',
13260
+ 'ETenms-B5-V',
13261
+ 'EUC-H',
13262
+ 'EUC-V',
13263
+ 'Ext-H',
13264
+ 'Ext-RKSJ-H',
13265
+ 'Ext-RKSJ-V',
13266
+ 'Ext-V',
13267
+ 'GB-EUC-H',
13268
+ 'GB-EUC-V',
13269
+ 'GB-H',
13270
+ 'GB-V',
13271
+ 'GBK-EUC-H',
13272
+ 'GBK-EUC-V',
13273
+ 'GBK2K-H',
13274
+ 'GBK2K-V',
13275
+ 'GBKp-EUC-H',
13276
+ 'GBKp-EUC-V',
13277
+ 'GBT-EUC-H',
13278
+ 'GBT-EUC-V',
13279
+ 'GBT-H',
13280
+ 'GBT-V',
13281
+ 'GBTpc-EUC-H',
13282
+ 'GBTpc-EUC-V',
13283
+ 'GBpc-EUC-H',
13284
+ 'GBpc-EUC-V',
13285
+ 'H',
13286
+ 'HKdla-B5-H',
13287
+ 'HKdla-B5-V',
13288
+ 'HKdlb-B5-H',
13289
+ 'HKdlb-B5-V',
13290
+ 'HKgccs-B5-H',
13291
+ 'HKgccs-B5-V',
13292
+ 'HKm314-B5-H',
13293
+ 'HKm314-B5-V',
13294
+ 'HKm471-B5-H',
13295
+ 'HKm471-B5-V',
13296
+ 'HKscs-B5-H',
13297
+ 'HKscs-B5-V',
13298
+ 'Hankaku',
13299
+ 'Hiragana',
13300
+ 'KSC-EUC-H',
13301
+ 'KSC-EUC-V',
13302
+ 'KSC-H',
13303
+ 'KSC-Johab-H',
13304
+ 'KSC-Johab-V',
13305
+ 'KSC-V',
13306
+ 'KSCms-UHC-H',
13307
+ 'KSCms-UHC-HW-H',
13308
+ 'KSCms-UHC-HW-V',
13309
+ 'KSCms-UHC-V',
13310
+ 'KSCpc-EUC-H',
13311
+ 'KSCpc-EUC-V',
13312
+ 'Katakana',
13313
+ 'NWP-H',
13314
+ 'NWP-V',
13315
+ 'RKSJ-H',
13316
+ 'RKSJ-V',
13317
+ 'Roman',
13318
+ 'UniCNS-UCS2-H',
13319
+ 'UniCNS-UCS2-V',
13320
+ 'UniCNS-UTF16-H',
13321
+ 'UniCNS-UTF16-V',
13322
+ 'UniCNS-UTF32-H',
13323
+ 'UniCNS-UTF32-V',
13324
+ 'UniCNS-UTF8-H',
13325
+ 'UniCNS-UTF8-V',
13326
+ 'UniGB-UCS2-H',
13327
+ 'UniGB-UCS2-V',
13328
+ 'UniGB-UTF16-H',
13329
+ 'UniGB-UTF16-V',
13330
+ 'UniGB-UTF32-H',
13331
+ 'UniGB-UTF32-V',
13332
+ 'UniGB-UTF8-H',
13333
+ 'UniGB-UTF8-V',
13334
+ 'UniJIS-UCS2-H',
13335
+ 'UniJIS-UCS2-HW-H',
13336
+ 'UniJIS-UCS2-HW-V',
13337
+ 'UniJIS-UCS2-V',
13338
+ 'UniJIS-UTF16-H',
13339
+ 'UniJIS-UTF16-V',
13340
+ 'UniJIS-UTF32-H',
13341
+ 'UniJIS-UTF32-V',
13342
+ 'UniJIS-UTF8-H',
13343
+ 'UniJIS-UTF8-V',
13344
+ 'UniJIS2004-UTF16-H',
13345
+ 'UniJIS2004-UTF16-V',
13346
+ 'UniJIS2004-UTF32-H',
13347
+ 'UniJIS2004-UTF32-V',
13348
+ 'UniJIS2004-UTF8-H',
13349
+ 'UniJIS2004-UTF8-V',
13350
+ 'UniJISPro-UCS2-HW-V',
13351
+ 'UniJISPro-UCS2-V',
13352
+ 'UniJISPro-UTF8-V',
13353
+ 'UniJISX0213-UTF32-H',
13354
+ 'UniJISX0213-UTF32-V',
13355
+ 'UniJISX02132004-UTF32-H',
13356
+ 'UniJISX02132004-UTF32-V',
13357
+ 'UniKS-UCS2-H',
13358
+ 'UniKS-UCS2-V',
13359
+ 'UniKS-UTF16-H',
13360
+ 'UniKS-UTF16-V',
13361
+ 'UniKS-UTF32-H',
13362
+ 'UniKS-UTF32-V',
13363
+ 'UniKS-UTF8-H',
13364
+ 'UniKS-UTF8-V',
13365
+ 'V',
13366
+ 'WP-Symbol'];
13367
+
13368
+ // CMap, not to be confused with TrueType's cmap.
13369
+ var CMap = (function CMapClosure() {
13370
+ function CMap(builtInCMap) {
13371
+ // Codespace ranges are stored as follows:
13372
+ // [[1BytePairs], [2BytePairs], [3BytePairs], [4BytePairs]]
13373
+ // where nBytePairs are ranges e.g. [low1, high1, low2, high2, ...]
13374
+ this.codespaceRanges = [[], [], [], []];
13375
+ this.numCodespaceRanges = 0;
13376
+ // Map entries have one of two forms.
13377
+ // - cid chars are 16-bit unsigned integers, stored as integers.
13378
+ // - bf chars are variable-length byte sequences, stored as strings, with
13379
+ // one byte per character.
13380
+ this._map = [];
13381
+ this.name = '';
13382
+ this.vertical = false;
13383
+ this.useCMap = null;
13384
+ this.builtInCMap = builtInCMap;
13385
+ }
13386
+ CMap.prototype = {
13387
+ addCodespaceRange: function(n, low, high) {
13388
+ this.codespaceRanges[n - 1].push(low, high);
13389
+ this.numCodespaceRanges++;
13390
+ },
13391
+
13392
+ mapCidRange: function(low, high, dstLow) {
13393
+ while (low <= high) {
13394
+ this._map[low++] = dstLow++;
13395
+ }
13396
+ },
13397
+
13398
+ mapBfRange: function(low, high, dstLow) {
13399
+ var lastByte = dstLow.length - 1;
13400
+ while (low <= high) {
13401
+ this._map[low++] = dstLow;
13402
+ // Only the last byte has to be incremented.
13403
+ dstLow = dstLow.substr(0, lastByte) +
13404
+ String.fromCharCode(dstLow.charCodeAt(lastByte) + 1);
13405
+ }
13406
+ },
13407
+
13408
+ mapBfRangeToArray: function(low, high, array) {
13409
+ var i = 0, ii = array.length;
13410
+ while (low <= high && i < ii) {
13411
+ this._map[low] = array[i++];
13412
+ ++low;
13413
+ }
13414
+ },
13415
+
13416
+ // This is used for both bf and cid chars.
13417
+ mapOne: function(src, dst) {
13418
+ this._map[src] = dst;
13419
+ },
13420
+
13421
+ lookup: function(code) {
13422
+ return this._map[code];
13423
+ },
13424
+
13425
+ contains: function(code) {
13426
+ return this._map[code] !== undefined;
13427
+ },
13428
+
13429
+ forEach: function(callback) {
13430
+ // Most maps have fewer than 65536 entries, and for those we use normal
13431
+ // array iteration. But really sparse tables are possible -- e.g. with
13432
+ // indices in the *billions*. For such tables we use for..in, which isn't
13433
+ // ideal because it stringifies the indices for all present elements, but
13434
+ // it does avoid iterating over every undefined entry.
13435
+ var map = this._map;
13436
+ var length = map.length;
13437
+ var i;
13438
+ if (length <= 0x10000) {
13439
+ for (i = 0; i < length; i++) {
13440
+ if (map[i] !== undefined) {
13441
+ callback(i, map[i]);
13442
+ }
13443
+ }
13444
+ } else {
13445
+ for (i in this._map) {
13446
+ callback(i, map[i]);
13447
+ }
13448
+ }
13449
+ },
13450
+
13451
+ charCodeOf: function(value) {
13452
+ return this._map.indexOf(value);
13453
+ },
13454
+
13455
+ getMap: function() {
13456
+ return this._map;
13457
+ },
13458
+
13459
+ readCharCode: function(str, offset, out) {
13460
+ var c = 0;
13461
+ var codespaceRanges = this.codespaceRanges;
13462
+ var codespaceRangesLen = this.codespaceRanges.length;
13463
+ // 9.7.6.2 CMap Mapping
13464
+ // The code length is at most 4.
13465
+ for (var n = 0; n < codespaceRangesLen; n++) {
13466
+ c = ((c << 8) | str.charCodeAt(offset + n)) >>> 0;
13467
+ // Check each codespace range to see if it falls within.
13468
+ var codespaceRange = codespaceRanges[n];
13469
+ for (var k = 0, kk = codespaceRange.length; k < kk;) {
13470
+ var low = codespaceRange[k++];
13471
+ var high = codespaceRange[k++];
13472
+ if (c >= low && c <= high) {
13473
+ out.charcode = c;
13474
+ out.length = n + 1;
13475
+ return;
13476
+ }
13477
+ }
13478
+ }
13479
+ out.charcode = 0;
13480
+ out.length = 1;
13481
+ },
13482
+
13483
+ get isIdentityCMap() {
13484
+ if (!(this.name === 'Identity-H' || this.name === 'Identity-V')) {
13485
+ return false;
13486
+ }
13487
+ if (this._map.length !== 0x10000) {
13488
+ return false;
13489
+ }
13490
+ for (var i = 0; i < 0x10000; i++) {
13491
+ if (this._map[i] !== i) {
13492
+ return false;
13493
+ }
13494
+ }
13495
+ return true;
13496
+ }
13497
+ };
13498
+ return CMap;
13499
+ })();
13500
+
13501
+ // A special case of CMap, where the _map array implicitly has a length of
13502
+ // 65536 and each element is equal to its index.
13503
+ var IdentityCMap = (function IdentityCMapClosure() {
13504
+ function IdentityCMap(vertical, n) {
13505
+ CMap.call(this);
13506
+ this.vertical = vertical;
13507
+ this.addCodespaceRange(n, 0, 0xffff);
13508
+ }
13509
+ Util.inherit(IdentityCMap, CMap, {});
13510
+
13511
+ IdentityCMap.prototype = {
13512
+ addCodespaceRange: CMap.prototype.addCodespaceRange,
13513
+
13514
+ mapCidRange: function(low, high, dstLow) {
13515
+ error('should not call mapCidRange');
13516
+ },
13517
+
13518
+ mapBfRange: function(low, high, dstLow) {
13519
+ error('should not call mapBfRange');
13520
+ },
13521
+
13522
+ mapBfRangeToArray: function(low, high, array) {
13523
+ error('should not call mapBfRangeToArray');
13524
+ },
13525
+
13526
+ mapOne: function(src, dst) {
13527
+ error('should not call mapCidOne');
13528
+ },
13529
+
13530
+ lookup: function(code) {
13531
+ return (isInt(code) && code <= 0xffff) ? code : undefined;
13532
+ },
13533
+
13534
+ contains: function(code) {
13535
+ return isInt(code) && code <= 0xffff;
13536
+ },
13537
+
13538
+ forEach: function(callback) {
13539
+ for (var i = 0; i <= 0xffff; i++) {
13540
+ callback(i, i);
13541
+ }
13542
+ },
13543
+
13544
+ charCodeOf: function(value) {
13545
+ return (isInt(value) && value <= 0xffff) ? value : -1;
13546
+ },
13547
+
13548
+ getMap: function() {
13549
+ // Sometimes identity maps must be instantiated, but it's rare.
13550
+ var map = new Array(0x10000);
13551
+ for (var i = 0; i <= 0xffff; i++) {
13552
+ map[i] = i;
13553
+ }
13554
+ return map;
13555
+ },
13556
+
13557
+ readCharCode: CMap.prototype.readCharCode,
13558
+
13559
+ get isIdentityCMap() {
13560
+ error('should not access .isIdentityCMap');
13561
+ }
13562
+ };
13563
+
13564
+ return IdentityCMap;
13565
+ })();
13566
+
13567
+ var BinaryCMapReader = (function BinaryCMapReaderClosure() {
13568
+ function fetchBinaryData(url) {
13569
+ var nonBinaryRequest = PDFJS.disableWorker;
13570
+ var request = new XMLHttpRequest();
13571
+ request.open('GET', url, false);
13572
+ if (!nonBinaryRequest) {
13573
+ try {
13574
+ request.responseType = 'arraybuffer';
13575
+ nonBinaryRequest = request.responseType !== 'arraybuffer';
13576
+ } catch (e) {
13577
+ nonBinaryRequest = true;
13578
+ }
13579
+ }
13580
+ if (nonBinaryRequest && request.overrideMimeType) {
13581
+ request.overrideMimeType('text/plain; charset=x-user-defined');
13582
+ }
13583
+ request.send(null);
13584
+ if (nonBinaryRequest ? !request.responseText : !request.response) {
13585
+ error('Unable to get binary cMap at: ' + url);
13586
+ }
13587
+ if (nonBinaryRequest) {
13588
+ var data = Array.prototype.map.call(request.responseText, function (ch) {
13589
+ return ch.charCodeAt(0) & 255;
13590
+ });
13591
+ return new Uint8Array(data);
13592
+ }
13593
+ return new Uint8Array(request.response);
13594
+ }
13595
+
13596
+ function hexToInt(a, size) {
13597
+ var n = 0;
13598
+ for (var i = 0; i <= size; i++) {
13599
+ n = (n << 8) | a[i];
13600
+ }
13601
+ return n >>> 0;
13602
+ }
13603
+
13604
+ function hexToStr(a, size) {
13605
+ // This code is hot. Special-case some common values to avoid creating an
13606
+ // object with subarray().
13607
+ if (size === 1) {
13608
+ return String.fromCharCode(a[0], a[1]);
13609
+ }
13610
+ if (size === 3) {
13611
+ return String.fromCharCode(a[0], a[1], a[2], a[3]);
13612
+ }
13613
+ return String.fromCharCode.apply(null, a.subarray(0, size + 1));
13614
+ }
13615
+
13616
+ function addHex(a, b, size) {
13617
+ var c = 0;
13618
+ for (var i = size; i >= 0; i--) {
13619
+ c += a[i] + b[i];
13620
+ a[i] = c & 255;
13621
+ c >>= 8;
13622
+ }
13623
+ }
13624
+
13625
+ function incHex(a, size) {
13626
+ var c = 1;
13627
+ for (var i = size; i >= 0 && c > 0; i--) {
13628
+ c += a[i];
13629
+ a[i] = c & 255;
13630
+ c >>= 8;
13631
+ }
13632
+ }
13633
+
13634
+ var MAX_NUM_SIZE = 16;
13635
+ var MAX_ENCODED_NUM_SIZE = 19; // ceil(MAX_NUM_SIZE * 7 / 8)
13636
+
13637
+ function BinaryCMapStream(data) {
13638
+ this.buffer = data;
13639
+ this.pos = 0;
13640
+ this.end = data.length;
13641
+ this.tmpBuf = new Uint8Array(MAX_ENCODED_NUM_SIZE);
13642
+ }
13643
+
13644
+ BinaryCMapStream.prototype = {
13645
+ readByte: function () {
13646
+ if (this.pos >= this.end) {
13647
+ return -1;
13648
+ }
13649
+ return this.buffer[this.pos++];
13650
+ },
13651
+ readNumber: function () {
13652
+ var n = 0;
13653
+ var last;
13654
+ do {
13655
+ var b = this.readByte();
13656
+ if (b < 0) {
13657
+ error('unexpected EOF in bcmap');
13658
+ }
13659
+ last = !(b & 0x80);
13660
+ n = (n << 7) | (b & 0x7F);
13661
+ } while (!last);
13662
+ return n;
13663
+ },
13664
+ readSigned: function () {
13665
+ var n = this.readNumber();
13666
+ return (n & 1) ? ~(n >>> 1) : n >>> 1;
13667
+ },
13668
+ readHex: function (num, size) {
13669
+ num.set(this.buffer.subarray(this.pos,
13670
+ this.pos + size + 1));
13671
+ this.pos += size + 1;
13672
+ },
13673
+ readHexNumber: function (num, size) {
13674
+ var last;
13675
+ var stack = this.tmpBuf, sp = 0;
13676
+ do {
13677
+ var b = this.readByte();
13678
+ if (b < 0) {
13679
+ error('unexpected EOF in bcmap');
13680
+ }
13681
+ last = !(b & 0x80);
13682
+ stack[sp++] = b & 0x7F;
13683
+ } while (!last);
13684
+ var i = size, buffer = 0, bufferSize = 0;
13685
+ while (i >= 0) {
13686
+ while (bufferSize < 8 && stack.length > 0) {
13687
+ buffer = (stack[--sp] << bufferSize) | buffer;
13688
+ bufferSize += 7;
13689
+ }
13690
+ num[i] = buffer & 255;
13691
+ i--;
13692
+ buffer >>= 8;
13693
+ bufferSize -= 8;
13694
+ }
13695
+ },
13696
+ readHexSigned: function (num, size) {
13697
+ this.readHexNumber(num, size);
13698
+ var sign = num[size] & 1 ? 255 : 0;
13699
+ var c = 0;
13700
+ for (var i = 0; i <= size; i++) {
13701
+ c = ((c & 1) << 8) | num[i];
13702
+ num[i] = (c >> 1) ^ sign;
13703
+ }
13704
+ },
13705
+ readString: function () {
13706
+ var len = this.readNumber();
13707
+ var s = '';
13708
+ for (var i = 0; i < len; i++) {
13709
+ s += String.fromCharCode(this.readNumber());
13710
+ }
13711
+ return s;
13712
+ }
13713
+ };
13714
+
13715
+ function processBinaryCMap(url, cMap, extend) {
13716
+ var data = fetchBinaryData(url);
13717
+ var stream = new BinaryCMapStream(data);
13718
+
13719
+ var header = stream.readByte();
13720
+ cMap.vertical = !!(header & 1);
13721
+
13722
+ var useCMap = null;
13723
+ var start = new Uint8Array(MAX_NUM_SIZE);
13724
+ var end = new Uint8Array(MAX_NUM_SIZE);
13725
+ var char = new Uint8Array(MAX_NUM_SIZE);
13726
+ var charCode = new Uint8Array(MAX_NUM_SIZE);
13727
+ var tmp = new Uint8Array(MAX_NUM_SIZE);
13728
+ var code;
13729
+
13730
+ var b;
13731
+ while ((b = stream.readByte()) >= 0) {
13732
+ var type = b >> 5;
13733
+ if (type === 7) { // metadata, e.g. comment or usecmap
13734
+ switch (b & 0x1F) {
13735
+ case 0:
13736
+ stream.readString(); // skipping comment
13737
+ break;
13738
+ case 1:
13739
+ useCMap = stream.readString();
13740
+ break;
13741
+ }
13742
+ continue;
13743
+ }
13744
+ var sequence = !!(b & 0x10);
13745
+ var dataSize = b & 15;
13746
+
13747
+ assert(dataSize + 1 <= MAX_NUM_SIZE);
13748
+
13749
+ var ucs2DataSize = 1;
13750
+ var subitemsCount = stream.readNumber();
13751
+ var i;
13752
+ switch (type) {
13753
+ case 0: // codespacerange
13754
+ stream.readHex(start, dataSize);
13755
+ stream.readHexNumber(end, dataSize);
13756
+ addHex(end, start, dataSize);
13757
+ cMap.addCodespaceRange(dataSize + 1, hexToInt(start, dataSize),
13758
+ hexToInt(end, dataSize));
13759
+ for (i = 1; i < subitemsCount; i++) {
13760
+ incHex(end, dataSize);
13761
+ stream.readHexNumber(start, dataSize);
13762
+ addHex(start, end, dataSize);
13763
+ stream.readHexNumber(end, dataSize);
13764
+ addHex(end, start, dataSize);
13765
+ cMap.addCodespaceRange(dataSize + 1, hexToInt(start, dataSize),
13766
+ hexToInt(end, dataSize));
13767
+ }
13768
+ break;
13769
+ case 1: // notdefrange
13770
+ stream.readHex(start, dataSize);
13771
+ stream.readHexNumber(end, dataSize);
13772
+ addHex(end, start, dataSize);
13773
+ code = stream.readNumber();
13774
+ // undefined range, skipping
13775
+ for (i = 1; i < subitemsCount; i++) {
13776
+ incHex(end, dataSize);
13777
+ stream.readHexNumber(start, dataSize);
13778
+ addHex(start, end, dataSize);
13779
+ stream.readHexNumber(end, dataSize);
13780
+ addHex(end, start, dataSize);
13781
+ code = stream.readNumber();
13782
+ // nop
13783
+ }
13784
+ break;
13785
+ case 2: // cidchar
13786
+ stream.readHex(char, dataSize);
13787
+ code = stream.readNumber();
13788
+ cMap.mapOne(hexToInt(char, dataSize), code);
13789
+ for (i = 1; i < subitemsCount; i++) {
13790
+ incHex(char, dataSize);
13791
+ if (!sequence) {
13792
+ stream.readHexNumber(tmp, dataSize);
13793
+ addHex(char, tmp, dataSize);
13794
+ }
13795
+ code = stream.readSigned() + (code + 1);
13796
+ cMap.mapOne(hexToInt(char, dataSize), code);
13797
+ }
13798
+ break;
13799
+ case 3: // cidrange
13800
+ stream.readHex(start, dataSize);
13801
+ stream.readHexNumber(end, dataSize);
13802
+ addHex(end, start, dataSize);
13803
+ code = stream.readNumber();
13804
+ cMap.mapCidRange(hexToInt(start, dataSize), hexToInt(end, dataSize),
13805
+ code);
13806
+ for (i = 1; i < subitemsCount; i++) {
13807
+ incHex(end, dataSize);
13808
+ if (!sequence) {
13809
+ stream.readHexNumber(start, dataSize);
13810
+ addHex(start, end, dataSize);
13811
+ } else {
13812
+ start.set(end);
13813
+ }
13814
+ stream.readHexNumber(end, dataSize);
13815
+ addHex(end, start, dataSize);
13816
+ code = stream.readNumber();
13817
+ cMap.mapCidRange(hexToInt(start, dataSize), hexToInt(end, dataSize),
13818
+ code);
13819
+ }
13820
+ break;
13821
+ case 4: // bfchar
13822
+ stream.readHex(char, ucs2DataSize);
13823
+ stream.readHex(charCode, dataSize);
13824
+ cMap.mapOne(hexToInt(char, ucs2DataSize),
13825
+ hexToStr(charCode, dataSize));
13826
+ for (i = 1; i < subitemsCount; i++) {
13827
+ incHex(char, ucs2DataSize);
13828
+ if (!sequence) {
13829
+ stream.readHexNumber(tmp, ucs2DataSize);
13830
+ addHex(char, tmp, ucs2DataSize);
13831
+ }
13832
+ incHex(charCode, dataSize);
13833
+ stream.readHexSigned(tmp, dataSize);
13834
+ addHex(charCode, tmp, dataSize);
13835
+ cMap.mapOne(hexToInt(char, ucs2DataSize),
13836
+ hexToStr(charCode, dataSize));
13837
+ }
13838
+ break;
13839
+ case 5: // bfrange
13840
+ stream.readHex(start, ucs2DataSize);
13841
+ stream.readHexNumber(end, ucs2DataSize);
13842
+ addHex(end, start, ucs2DataSize);
13843
+ stream.readHex(charCode, dataSize);
13844
+ cMap.mapBfRange(hexToInt(start, ucs2DataSize),
13845
+ hexToInt(end, ucs2DataSize),
13846
+ hexToStr(charCode, dataSize));
13847
+ for (i = 1; i < subitemsCount; i++) {
13848
+ incHex(end, ucs2DataSize);
13849
+ if (!sequence) {
13850
+ stream.readHexNumber(start, ucs2DataSize);
13851
+ addHex(start, end, ucs2DataSize);
13852
+ } else {
13853
+ start.set(end);
13854
+ }
13855
+ stream.readHexNumber(end, ucs2DataSize);
13856
+ addHex(end, start, ucs2DataSize);
13857
+ stream.readHex(charCode, dataSize);
13858
+ cMap.mapBfRange(hexToInt(start, ucs2DataSize),
13859
+ hexToInt(end, ucs2DataSize),
13860
+ hexToStr(charCode, dataSize));
13861
+ }
13862
+ break;
13863
+ default:
13864
+ error('Unknown type: ' + type);
13865
+ break;
13866
+ }
13867
+ }
13868
+
13869
+ if (useCMap) {
13870
+ extend(useCMap);
13871
+ }
13872
+ return cMap;
13873
+ }
13874
+
13875
+ function BinaryCMapReader() {}
13876
+
13877
+ BinaryCMapReader.prototype = {
13878
+ read: processBinaryCMap
13879
+ };
13880
+
13881
+ return BinaryCMapReader;
13882
+ })();
13883
+
13884
+ var CMapFactory = (function CMapFactoryClosure() {
13885
+ function strToInt(str) {
13886
+ var a = 0;
13887
+ for (var i = 0; i < str.length; i++) {
13888
+ a = (a << 8) | str.charCodeAt(i);
13889
+ }
13890
+ return a >>> 0;
13891
+ }
13892
+
13893
+ function expectString(obj) {
13894
+ if (!isString(obj)) {
13895
+ error('Malformed CMap: expected string.');
13896
+ }
13897
+ }
13898
+
13899
+ function expectInt(obj) {
13900
+ if (!isInt(obj)) {
13901
+ error('Malformed CMap: expected int.');
13902
+ }
13903
+ }
13904
+
13905
+ function parseBfChar(cMap, lexer) {
13906
+ while (true) {
13907
+ var obj = lexer.getObj();
13908
+ if (isEOF(obj)) {
13909
+ break;
13910
+ }
13911
+ if (isCmd(obj, 'endbfchar')) {
13912
+ return;
13913
+ }
13914
+ expectString(obj);
13915
+ var src = strToInt(obj);
13916
+ obj = lexer.getObj();
13917
+ // TODO are /dstName used?
13918
+ expectString(obj);
13919
+ var dst = obj;
13920
+ cMap.mapOne(src, dst);
13921
+ }
13922
+ }
13923
+
13924
+ function parseBfRange(cMap, lexer) {
13925
+ while (true) {
13926
+ var obj = lexer.getObj();
13927
+ if (isEOF(obj)) {
13928
+ break;
13929
+ }
13930
+ if (isCmd(obj, 'endbfrange')) {
13931
+ return;
13932
+ }
13933
+ expectString(obj);
13934
+ var low = strToInt(obj);
13935
+ obj = lexer.getObj();
13936
+ expectString(obj);
13937
+ var high = strToInt(obj);
13938
+ obj = lexer.getObj();
13939
+ if (isInt(obj) || isString(obj)) {
13940
+ var dstLow = isInt(obj) ? String.fromCharCode(obj) : obj;
13941
+ cMap.mapBfRange(low, high, dstLow);
13942
+ } else if (isCmd(obj, '[')) {
13943
+ obj = lexer.getObj();
13944
+ var array = [];
13945
+ while (!isCmd(obj, ']') && !isEOF(obj)) {
13946
+ array.push(obj);
13947
+ obj = lexer.getObj();
13948
+ }
13949
+ cMap.mapBfRangeToArray(low, high, array);
13950
+ } else {
13951
+ break;
13952
+ }
13953
+ }
13954
+ error('Invalid bf range.');
13955
+ }
13956
+
13957
+ function parseCidChar(cMap, lexer) {
13958
+ while (true) {
13959
+ var obj = lexer.getObj();
13960
+ if (isEOF(obj)) {
13961
+ break;
13962
+ }
13963
+ if (isCmd(obj, 'endcidchar')) {
13964
+ return;
13965
+ }
13966
+ expectString(obj);
13967
+ var src = strToInt(obj);
13968
+ obj = lexer.getObj();
13969
+ expectInt(obj);
13970
+ var dst = obj;
13971
+ cMap.mapOne(src, dst);
13972
+ }
13973
+ }
13974
+
13975
+ function parseCidRange(cMap, lexer) {
13976
+ while (true) {
13977
+ var obj = lexer.getObj();
13978
+ if (isEOF(obj)) {
13979
+ break;
13980
+ }
13981
+ if (isCmd(obj, 'endcidrange')) {
13982
+ return;
13983
+ }
13984
+ expectString(obj);
13985
+ var low = strToInt(obj);
13986
+ obj = lexer.getObj();
13987
+ expectString(obj);
13988
+ var high = strToInt(obj);
13989
+ obj = lexer.getObj();
13990
+ expectInt(obj);
13991
+ var dstLow = obj;
13992
+ cMap.mapCidRange(low, high, dstLow);
13993
+ }
13994
+ }
13995
+
13996
+ function parseCodespaceRange(cMap, lexer) {
13997
+ while (true) {
13998
+ var obj = lexer.getObj();
13999
+ if (isEOF(obj)) {
14000
+ break;
14001
+ }
14002
+ if (isCmd(obj, 'endcodespacerange')) {
14003
+ return;
14004
+ }
14005
+ if (!isString(obj)) {
14006
+ break;
14007
+ }
14008
+ var low = strToInt(obj);
14009
+ obj = lexer.getObj();
14010
+ if (!isString(obj)) {
14011
+ break;
14012
+ }
14013
+ var high = strToInt(obj);
14014
+ cMap.addCodespaceRange(obj.length, low, high);
14015
+ }
14016
+ error('Invalid codespace range.');
14017
+ }
14018
+
14019
+ function parseWMode(cMap, lexer) {
14020
+ var obj = lexer.getObj();
14021
+ if (isInt(obj)) {
14022
+ cMap.vertical = !!obj;
14023
+ }
14024
+ }
14025
+
14026
+ function parseCMapName(cMap, lexer) {
14027
+ var obj = lexer.getObj();
14028
+ if (isName(obj) && isString(obj.name)) {
14029
+ cMap.name = obj.name;
14030
+ }
14031
+ }
14032
+
14033
+ function parseCMap(cMap, lexer, builtInCMapParams, useCMap) {
14034
+ var previous;
14035
+ var embededUseCMap;
14036
+ objLoop: while (true) {
14037
+ var obj = lexer.getObj();
14038
+ if (isEOF(obj)) {
14039
+ break;
14040
+ } else if (isName(obj)) {
14041
+ if (obj.name === 'WMode') {
14042
+ parseWMode(cMap, lexer);
14043
+ } else if (obj.name === 'CMapName') {
14044
+ parseCMapName(cMap, lexer);
14045
+ }
14046
+ previous = obj;
14047
+ } else if (isCmd(obj)) {
14048
+ switch (obj.cmd) {
14049
+ case 'endcmap':
14050
+ break objLoop;
14051
+ case 'usecmap':
14052
+ if (isName(previous)) {
14053
+ embededUseCMap = previous.name;
14054
+ }
14055
+ break;
14056
+ case 'begincodespacerange':
14057
+ parseCodespaceRange(cMap, lexer);
14058
+ break;
14059
+ case 'beginbfchar':
14060
+ parseBfChar(cMap, lexer);
14061
+ break;
14062
+ case 'begincidchar':
14063
+ parseCidChar(cMap, lexer);
14064
+ break;
14065
+ case 'beginbfrange':
14066
+ parseBfRange(cMap, lexer);
14067
+ break;
14068
+ case 'begincidrange':
14069
+ parseCidRange(cMap, lexer);
14070
+ break;
14071
+ }
14072
+ }
14073
+ }
14074
+
14075
+ if (!useCMap && embededUseCMap) {
14076
+ // Load the usecmap definition from the file only if there wasn't one
14077
+ // specified.
14078
+ useCMap = embededUseCMap;
14079
+ }
14080
+ if (useCMap) {
14081
+ extendCMap(cMap, builtInCMapParams, useCMap);
14082
+ }
14083
+ }
14084
+
14085
+ function extendCMap(cMap, builtInCMapParams, useCMap) {
14086
+ cMap.useCMap = createBuiltInCMap(useCMap, builtInCMapParams);
14087
+ // If there aren't any code space ranges defined clone all the parent ones
14088
+ // into this cMap.
14089
+ if (cMap.numCodespaceRanges === 0) {
14090
+ var useCodespaceRanges = cMap.useCMap.codespaceRanges;
14091
+ for (var i = 0; i < useCodespaceRanges.length; i++) {
14092
+ cMap.codespaceRanges[i] = useCodespaceRanges[i].slice();
14093
+ }
14094
+ cMap.numCodespaceRanges = cMap.useCMap.numCodespaceRanges;
14095
+ }
14096
+ // Merge the map into the current one, making sure not to override
14097
+ // any previously defined entries.
14098
+ cMap.useCMap.forEach(function(key, value) {
14099
+ if (!cMap.contains(key)) {
14100
+ cMap.mapOne(key, cMap.useCMap.lookup(key));
14101
+ }
14102
+ });
14103
+ }
14104
+
14105
+ function parseBinaryCMap(name, builtInCMapParams) {
14106
+ var url = builtInCMapParams.url + name + '.bcmap';
14107
+ var cMap = new CMap(true);
14108
+ new BinaryCMapReader().read(url, cMap, function (useCMap) {
14109
+ extendCMap(cMap, builtInCMapParams, useCMap);
14110
+ });
14111
+ return cMap;
14112
+ }
14113
+
14114
+ function createBuiltInCMap(name, builtInCMapParams) {
14115
+ if (name === 'Identity-H') {
14116
+ return new IdentityCMap(false, 2);
14117
+ } else if (name === 'Identity-V') {
14118
+ return new IdentityCMap(true, 2);
14119
+ }
14120
+ if (BUILT_IN_CMAPS.indexOf(name) === -1) {
14121
+ error('Unknown cMap name: ' + name);
14122
+ }
14123
+ assert(builtInCMapParams, 'built-in cMap parameters are not provided');
14124
+
14125
+ if (builtInCMapParams.packed) {
14126
+ return parseBinaryCMap(name, builtInCMapParams);
14127
+ }
14128
+
14129
+ var request = new XMLHttpRequest();
14130
+ var url = builtInCMapParams.url + name;
14131
+ request.open('GET', url, false);
14132
+ request.send(null);
14133
+ if (!request.responseText) {
14134
+ error('Unable to get cMap at: ' + url);
14135
+ }
14136
+ var cMap = new CMap(true);
14137
+ var lexer = new Lexer(new StringStream(request.responseText));
14138
+ parseCMap(cMap, lexer, builtInCMapParams, null);
14139
+ return cMap;
14140
+ }
14141
+
14142
+ return {
14143
+ create: function (encoding, builtInCMapParams, useCMap) {
14144
+ if (isName(encoding)) {
14145
+ return createBuiltInCMap(encoding.name, builtInCMapParams);
14146
+ } else if (isStream(encoding)) {
14147
+ var cMap = new CMap();
14148
+ var lexer = new Lexer(encoding);
14149
+ try {
14150
+ parseCMap(cMap, lexer, builtInCMapParams, useCMap);
14151
+ } catch (e) {
14152
+ warn('Invalid CMap data. ' + e);
14153
+ }
14154
+ if (cMap.isIdentityCMap) {
14155
+ return createBuiltInCMap(cMap.name, builtInCMapParams);
14156
+ }
14157
+ return cMap;
14158
+ }
14159
+ error('Encoding required.');
14160
+ }
14161
+ };
14162
+ })();
14163
+
14164
+
14165
+ // Unicode Private Use Area
14166
+ var PRIVATE_USE_OFFSET_START = 0xE000;
14167
+ var PRIVATE_USE_OFFSET_END = 0xF8FF;
14168
+ var SKIP_PRIVATE_USE_RANGE_F000_TO_F01F = false;
14169
+
14170
+ // PDF Glyph Space Units are one Thousandth of a TextSpace Unit
14171
+ // except for Type 3 fonts
14172
+ var PDF_GLYPH_SPACE_UNITS = 1000;
14173
+
14174
+ // Hinting is currently disabled due to unknown problems on windows
14175
+ // in tracemonkey and various other pdfs with type1 fonts.
14176
+ var HINTING_ENABLED = false;
14177
+
14178
+ // Accented charactars are not displayed properly on windows, using this flag
14179
+ // to control analysis of seac charstrings.
14180
+ var SEAC_ANALYSIS_ENABLED = false;
14181
+
14182
+ var FontFlags = {
14183
+ FixedPitch: 1,
14184
+ Serif: 2,
14185
+ Symbolic: 4,
14186
+ Script: 8,
14187
+ Nonsymbolic: 32,
14188
+ Italic: 64,
14189
+ AllCap: 65536,
14190
+ SmallCap: 131072,
14191
+ ForceBold: 262144
14192
+ };
14193
+
14194
+ var Encodings = {
14195
+ ExpertEncoding: ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
14196
+ '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
14197
+ 'space', 'exclamsmall', 'Hungarumlautsmall', '', 'dollaroldstyle',
14198
+ 'dollarsuperior', 'ampersandsmall', 'Acutesmall', 'parenleftsuperior',
14199
+ 'parenrightsuperior', 'twodotenleader', 'onedotenleader', 'comma',
14200
+ 'hyphen', 'period', 'fraction', 'zerooldstyle', 'oneoldstyle',
14201
+ 'twooldstyle', 'threeoldstyle', 'fouroldstyle', 'fiveoldstyle',
14202
+ 'sixoldstyle', 'sevenoldstyle', 'eightoldstyle', 'nineoldstyle', 'colon',
14203
+ 'semicolon', 'commasuperior', 'threequartersemdash', 'periodsuperior',
14204
+ 'questionsmall', '', 'asuperior', 'bsuperior', 'centsuperior', 'dsuperior',
14205
+ 'esuperior', '', '', 'isuperior', '', '', 'lsuperior', 'msuperior',
14206
+ 'nsuperior', 'osuperior', '', '', 'rsuperior', 'ssuperior', 'tsuperior',
14207
+ '', 'ff', 'fi', 'fl', 'ffi', 'ffl', 'parenleftinferior', '',
14208
+ 'parenrightinferior', 'Circumflexsmall', 'hyphensuperior', 'Gravesmall',
14209
+ 'Asmall', 'Bsmall', 'Csmall', 'Dsmall', 'Esmall', 'Fsmall', 'Gsmall',
14210
+ 'Hsmall', 'Ismall', 'Jsmall', 'Ksmall', 'Lsmall', 'Msmall', 'Nsmall',
14211
+ 'Osmall', 'Psmall', 'Qsmall', 'Rsmall', 'Ssmall', 'Tsmall', 'Usmall',
14212
+ 'Vsmall', 'Wsmall', 'Xsmall', 'Ysmall', 'Zsmall', 'colonmonetary',
14213
+ 'onefitted', 'rupiah', 'Tildesmall', '', '', '', '', '', '', '', '', '',
14214
+ '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
14215
+ '', '', '', '', '', '', 'exclamdownsmall', 'centoldstyle', 'Lslashsmall',
14216
+ '', '', 'Scaronsmall', 'Zcaronsmall', 'Dieresissmall', 'Brevesmall',
14217
+ 'Caronsmall', '', 'Dotaccentsmall', '', '', 'Macronsmall', '', '',
14218
+ 'figuredash', 'hypheninferior', '', '', 'Ogoneksmall', 'Ringsmall',
14219
+ 'Cedillasmall', '', '', '', 'onequarter', 'onehalf', 'threequarters',
14220
+ 'questiondownsmall', 'oneeighth', 'threeeighths', 'fiveeighths',
14221
+ 'seveneighths', 'onethird', 'twothirds', '', '', 'zerosuperior',
14222
+ 'onesuperior', 'twosuperior', 'threesuperior', 'foursuperior',
14223
+ 'fivesuperior', 'sixsuperior', 'sevensuperior', 'eightsuperior',
14224
+ 'ninesuperior', 'zeroinferior', 'oneinferior', 'twoinferior',
14225
+ 'threeinferior', 'fourinferior', 'fiveinferior', 'sixinferior',
14226
+ 'seveninferior', 'eightinferior', 'nineinferior', 'centinferior',
14227
+ 'dollarinferior', 'periodinferior', 'commainferior', 'Agravesmall',
14228
+ 'Aacutesmall', 'Acircumflexsmall', 'Atildesmall', 'Adieresissmall',
14229
+ 'Aringsmall', 'AEsmall', 'Ccedillasmall', 'Egravesmall', 'Eacutesmall',
14230
+ 'Ecircumflexsmall', 'Edieresissmall', 'Igravesmall', 'Iacutesmall',
14231
+ 'Icircumflexsmall', 'Idieresissmall', 'Ethsmall', 'Ntildesmall',
14232
+ 'Ogravesmall', 'Oacutesmall', 'Ocircumflexsmall', 'Otildesmall',
14233
+ 'Odieresissmall', 'OEsmall', 'Oslashsmall', 'Ugravesmall', 'Uacutesmall',
14234
+ 'Ucircumflexsmall', 'Udieresissmall', 'Yacutesmall', 'Thornsmall',
14235
+ 'Ydieresissmall'],
14236
+ MacExpertEncoding: ['', '', '', '', '', '', '', '', '', '', '', '', '', '',
14237
+ '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
14238
+ 'space', 'exclamsmall', 'Hungarumlautsmall', 'centoldstyle',
14239
+ 'dollaroldstyle', 'dollarsuperior', 'ampersandsmall', 'Acutesmall',
14240
+ 'parenleftsuperior', 'parenrightsuperior', 'twodotenleader',
14241
+ 'onedotenleader', 'comma', 'hyphen', 'period', 'fraction', 'zerooldstyle',
14242
+ 'oneoldstyle', 'twooldstyle', 'threeoldstyle', 'fouroldstyle',
14243
+ 'fiveoldstyle', 'sixoldstyle', 'sevenoldstyle', 'eightoldstyle',
14244
+ 'nineoldstyle', 'colon', 'semicolon', '', 'threequartersemdash', '',
14245
+ 'questionsmall', '', '', '', '', 'Ethsmall', '', '', 'onequarter',
14246
+ 'onehalf', 'threequarters', 'oneeighth', 'threeeighths', 'fiveeighths',
14247
+ 'seveneighths', 'onethird', 'twothirds', '', '', '', '', '', '', 'ff',
14248
+ 'fi', 'fl', 'ffi', 'ffl', 'parenleftinferior', '', 'parenrightinferior',
14249
+ 'Circumflexsmall', 'hypheninferior', 'Gravesmall', 'Asmall', 'Bsmall',
14250
+ 'Csmall', 'Dsmall', 'Esmall', 'Fsmall', 'Gsmall', 'Hsmall', 'Ismall',
14251
+ 'Jsmall', 'Ksmall', 'Lsmall', 'Msmall', 'Nsmall', 'Osmall', 'Psmall',
14252
+ 'Qsmall', 'Rsmall', 'Ssmall', 'Tsmall', 'Usmall', 'Vsmall', 'Wsmall',
14253
+ 'Xsmall', 'Ysmall', 'Zsmall', 'colonmonetary', 'onefitted', 'rupiah',
14254
+ 'Tildesmall', '', '', 'asuperior', 'centsuperior', '', '', '', '',
14255
+ 'Aacutesmall', 'Agravesmall', 'Acircumflexsmall', 'Adieresissmall',
14256
+ 'Atildesmall', 'Aringsmall', 'Ccedillasmall', 'Eacutesmall', 'Egravesmall',
14257
+ 'Ecircumflexsmall', 'Edieresissmall', 'Iacutesmall', 'Igravesmall',
14258
+ 'Icircumflexsmall', 'Idieresissmall', 'Ntildesmall', 'Oacutesmall',
14259
+ 'Ogravesmall', 'Ocircumflexsmall', 'Odieresissmall', 'Otildesmall',
14260
+ 'Uacutesmall', 'Ugravesmall', 'Ucircumflexsmall', 'Udieresissmall', '',
14261
+ 'eightsuperior', 'fourinferior', 'threeinferior', 'sixinferior',
14262
+ 'eightinferior', 'seveninferior', 'Scaronsmall', '', 'centinferior',
14263
+ 'twoinferior', '', 'Dieresissmall', '', 'Caronsmall', 'osuperior',
14264
+ 'fiveinferior', '', 'commainferior', 'periodinferior', 'Yacutesmall', '',
14265
+ 'dollarinferior', '', 'Thornsmall', '', 'nineinferior', 'zeroinferior',
14266
+ 'Zcaronsmall', 'AEsmall', 'Oslashsmall', 'questiondownsmall',
14267
+ 'oneinferior', 'Lslashsmall', '', '', '', '', '', '', 'Cedillasmall', '',
14268
+ '', '', '', '', 'OEsmall', 'figuredash', 'hyphensuperior', '', '', '', '',
14269
+ 'exclamdownsmall', '', 'Ydieresissmall', '', 'onesuperior', 'twosuperior',
14270
+ 'threesuperior', 'foursuperior', 'fivesuperior', 'sixsuperior',
14271
+ 'sevensuperior', 'ninesuperior', 'zerosuperior', '', 'esuperior',
14272
+ 'rsuperior', 'tsuperior', '', '', 'isuperior', 'ssuperior', 'dsuperior',
14273
+ '', '', '', '', '', 'lsuperior', 'Ogoneksmall', 'Brevesmall',
14274
+ 'Macronsmall', 'bsuperior', 'nsuperior', 'msuperior', 'commasuperior',
14275
+ 'periodsuperior', 'Dotaccentsmall', 'Ringsmall'],
14276
+ MacRomanEncoding: ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
14277
+ '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
14278
+ 'space', 'exclam', 'quotedbl', 'numbersign', 'dollar', 'percent',
14279
+ 'ampersand', 'quotesingle', 'parenleft', 'parenright', 'asterisk', 'plus',
14280
+ 'comma', 'hyphen', 'period', 'slash', 'zero', 'one', 'two', 'three',
14281
+ 'four', 'five', 'six', 'seven', 'eight', 'nine', 'colon', 'semicolon',
14282
+ 'less', 'equal', 'greater', 'question', 'at', 'A', 'B', 'C', 'D', 'E', 'F',
14283
+ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
14284
+ 'V', 'W', 'X', 'Y', 'Z', 'bracketleft', 'backslash', 'bracketright',
14285
+ 'asciicircum', 'underscore', 'grave', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
14286
+ 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
14287
+ 'w', 'x', 'y', 'z', 'braceleft', 'bar', 'braceright', 'asciitilde', '',
14288
+ 'Adieresis', 'Aring', 'Ccedilla', 'Eacute', 'Ntilde', 'Odieresis',
14289
+ 'Udieresis', 'aacute', 'agrave', 'acircumflex', 'adieresis', 'atilde',
14290
+ 'aring', 'ccedilla', 'eacute', 'egrave', 'ecircumflex', 'edieresis',
14291
+ 'iacute', 'igrave', 'icircumflex', 'idieresis', 'ntilde', 'oacute',
14292
+ 'ograve', 'ocircumflex', 'odieresis', 'otilde', 'uacute', 'ugrave',
14293
+ 'ucircumflex', 'udieresis', 'dagger', 'degree', 'cent', 'sterling',
14294
+ 'section', 'bullet', 'paragraph', 'germandbls', 'registered', 'copyright',
14295
+ 'trademark', 'acute', 'dieresis', 'notequal', 'AE', 'Oslash', 'infinity',
14296
+ 'plusminus', 'lessequal', 'greaterequal', 'yen', 'mu', 'partialdiff',
14297
+ 'summation', 'product', 'pi', 'integral', 'ordfeminine', 'ordmasculine',
14298
+ 'Omega', 'ae', 'oslash', 'questiondown', 'exclamdown', 'logicalnot',
14299
+ 'radical', 'florin', 'approxequal', 'Delta', 'guillemotleft',
14300
+ 'guillemotright', 'ellipsis', 'space', 'Agrave', 'Atilde', 'Otilde', 'OE',
14301
+ 'oe', 'endash', 'emdash', 'quotedblleft', 'quotedblright', 'quoteleft',
14302
+ 'quoteright', 'divide', 'lozenge', 'ydieresis', 'Ydieresis', 'fraction',
14303
+ 'currency', 'guilsinglleft', 'guilsinglright', 'fi', 'fl', 'daggerdbl',
14304
+ 'periodcentered', 'quotesinglbase', 'quotedblbase', 'perthousand',
14305
+ 'Acircumflex', 'Ecircumflex', 'Aacute', 'Edieresis', 'Egrave', 'Iacute',
14306
+ 'Icircumflex', 'Idieresis', 'Igrave', 'Oacute', 'Ocircumflex', 'apple',
14307
+ 'Ograve', 'Uacute', 'Ucircumflex', 'Ugrave', 'dotlessi', 'circumflex',
14308
+ 'tilde', 'macron', 'breve', 'dotaccent', 'ring', 'cedilla', 'hungarumlaut',
14309
+ 'ogonek', 'caron'],
14310
+ StandardEncoding: ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
14311
+ '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
14312
+ 'space', 'exclam', 'quotedbl', 'numbersign', 'dollar', 'percent',
14313
+ 'ampersand', 'quoteright', 'parenleft', 'parenright', 'asterisk', 'plus',
14314
+ 'comma', 'hyphen', 'period', 'slash', 'zero', 'one', 'two', 'three',
14315
+ 'four', 'five', 'six', 'seven', 'eight', 'nine', 'colon', 'semicolon',
14316
+ 'less', 'equal', 'greater', 'question', 'at', 'A', 'B', 'C', 'D', 'E', 'F',
14317
+ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
14318
+ 'V', 'W', 'X', 'Y', 'Z', 'bracketleft', 'backslash', 'bracketright',
14319
+ 'asciicircum', 'underscore', 'quoteleft', 'a', 'b', 'c', 'd', 'e', 'f',
14320
+ 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
14321
+ 'v', 'w', 'x', 'y', 'z', 'braceleft', 'bar', 'braceright', 'asciitilde',
14322
+ '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
14323
+ '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'exclamdown',
14324
+ 'cent', 'sterling', 'fraction', 'yen', 'florin', 'section', 'currency',
14325
+ 'quotesingle', 'quotedblleft', 'guillemotleft', 'guilsinglleft',
14326
+ 'guilsinglright', 'fi', 'fl', '', 'endash', 'dagger', 'daggerdbl',
14327
+ 'periodcentered', '', 'paragraph', 'bullet', 'quotesinglbase',
14328
+ 'quotedblbase', 'quotedblright', 'guillemotright', 'ellipsis',
14329
+ 'perthousand', '', 'questiondown', '', 'grave', 'acute', 'circumflex',
14330
+ 'tilde', 'macron', 'breve', 'dotaccent', 'dieresis', '', 'ring', 'cedilla',
14331
+ '', 'hungarumlaut', 'ogonek', 'caron', 'emdash', '', '', '', '', '', '',
14332
+ '', '', '', '', '', '', '', '', '', '', 'AE', '', 'ordfeminine', '', '',
14333
+ '', '', 'Lslash', 'Oslash', 'OE', 'ordmasculine', '', '', '', '', '', 'ae',
14334
+ '', '', '', 'dotlessi', '', '', 'lslash', 'oslash', 'oe', 'germandbls'],
14335
+ WinAnsiEncoding: ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
14336
+ '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
14337
+ 'space', 'exclam', 'quotedbl', 'numbersign', 'dollar', 'percent',
14338
+ 'ampersand', 'quotesingle', 'parenleft', 'parenright', 'asterisk', 'plus',
14339
+ 'comma', 'hyphen', 'period', 'slash', 'zero', 'one', 'two', 'three',
14340
+ 'four', 'five', 'six', 'seven', 'eight', 'nine', 'colon', 'semicolon',
14341
+ 'less', 'equal', 'greater', 'question', 'at', 'A', 'B', 'C', 'D', 'E', 'F',
14342
+ 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
14343
+ 'V', 'W', 'X', 'Y', 'Z', 'bracketleft', 'backslash', 'bracketright',
14344
+ 'asciicircum', 'underscore', 'grave', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
14345
+ 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
14346
+ 'w', 'x', 'y', 'z', 'braceleft', 'bar', 'braceright', 'asciitilde',
14347
+ 'bullet', 'Euro', 'bullet', 'quotesinglbase', 'florin', 'quotedblbase',
14348
+ 'ellipsis', 'dagger', 'daggerdbl', 'circumflex', 'perthousand', 'Scaron',
14349
+ 'guilsinglleft', 'OE', 'bullet', 'Zcaron', 'bullet', 'bullet', 'quoteleft',
14350
+ 'quoteright', 'quotedblleft', 'quotedblright', 'bullet', 'endash',
14351
+ 'emdash', 'tilde', 'trademark', 'scaron', 'guilsinglright', 'oe', 'bullet',
14352
+ 'zcaron', 'Ydieresis', 'space', 'exclamdown', 'cent', 'sterling',
14353
+ 'currency', 'yen', 'brokenbar', 'section', 'dieresis', 'copyright',
14354
+ 'ordfeminine', 'guillemotleft', 'logicalnot', 'hyphen', 'registered',
14355
+ 'macron', 'degree', 'plusminus', 'twosuperior', 'threesuperior', 'acute',
14356
+ 'mu', 'paragraph', 'periodcentered', 'cedilla', 'onesuperior',
14357
+ 'ordmasculine', 'guillemotright', 'onequarter', 'onehalf', 'threequarters',
14358
+ 'questiondown', 'Agrave', 'Aacute', 'Acircumflex', 'Atilde', 'Adieresis',
14359
+ 'Aring', 'AE', 'Ccedilla', 'Egrave', 'Eacute', 'Ecircumflex', 'Edieresis',
14360
+ 'Igrave', 'Iacute', 'Icircumflex', 'Idieresis', 'Eth', 'Ntilde', 'Ograve',
14361
+ 'Oacute', 'Ocircumflex', 'Otilde', 'Odieresis', 'multiply', 'Oslash',
14362
+ 'Ugrave', 'Uacute', 'Ucircumflex', 'Udieresis', 'Yacute', 'Thorn',
14363
+ 'germandbls', 'agrave', 'aacute', 'acircumflex', 'atilde', 'adieresis',
14364
+ 'aring', 'ae', 'ccedilla', 'egrave', 'eacute', 'ecircumflex', 'edieresis',
14365
+ 'igrave', 'iacute', 'icircumflex', 'idieresis', 'eth', 'ntilde', 'ograve',
14366
+ 'oacute', 'ocircumflex', 'otilde', 'odieresis', 'divide', 'oslash',
14367
+ 'ugrave', 'uacute', 'ucircumflex', 'udieresis', 'yacute', 'thorn',
14368
+ 'ydieresis'],
14369
+ SymbolSetEncoding: ['', '', '', '', '', '', '', '', '', '', '', '', '', '',
14370
+ '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
14371
+ 'space', 'exclam', 'universal', 'numbersign', 'existential', 'percent',
14372
+ 'ampersand', 'suchthat', 'parenleft', 'parenright', 'asteriskmath', 'plus',
14373
+ 'comma', 'minus', 'period', 'slash', 'zero', 'one', 'two', 'three', 'four',
14374
+ 'five', 'six', 'seven', 'eight', 'nine', 'colon', 'semicolon', 'less',
14375
+ 'equal', 'greater', 'question', 'congruent', 'Alpha', 'Beta', 'Chi',
14376
+ 'Delta', 'Epsilon', 'Phi', 'Gamma', 'Eta', 'Iota', 'theta1', 'Kappa',
14377
+ 'Lambda', 'Mu', 'Nu', 'Omicron', 'Pi', 'Theta', 'Rho', 'Sigma', 'Tau',
14378
+ 'Upsilon', 'sigma1', 'Omega', 'Xi', 'Psi', 'Zeta', 'bracketleft',
14379
+ 'therefore', 'bracketright', 'perpendicular', 'underscore', 'radicalex',
14380
+ 'alpha', 'beta', 'chi', 'delta', 'epsilon', 'phi', 'gamma', 'eta', 'iota',
14381
+ 'phi1', 'kappa', 'lambda', 'mu', 'nu', 'omicron', 'pi', 'theta', 'rho',
14382
+ 'sigma', 'tau', 'upsilon', 'omega1', 'omega', 'xi', 'psi', 'zeta',
14383
+ 'braceleft', 'bar', 'braceright', 'similar', '', '', '', '', '', '', '',
14384
+ '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
14385
+ '', '', '', '', '', '', '', 'Euro', 'Upsilon1', 'minute', 'lessequal',
14386
+ 'fraction', 'infinity', 'florin', 'club', 'diamond', 'heart', 'spade',
14387
+ 'arrowboth', 'arrowleft', 'arrowup', 'arrowright', 'arrowdown', 'degree',
14388
+ 'plusminus', 'second', 'greaterequal', 'multiply', 'proportional',
14389
+ 'partialdiff', 'bullet', 'divide', 'notequal', 'equivalence',
14390
+ 'approxequal', 'ellipsis', 'arrowvertex', 'arrowhorizex', 'carriagereturn',
14391
+ 'aleph', 'Ifraktur', 'Rfraktur', 'weierstrass', 'circlemultiply',
14392
+ 'circleplus', 'emptyset', 'intersection', 'union', 'propersuperset',
14393
+ 'reflexsuperset', 'notsubset', 'propersubset', 'reflexsubset', 'element',
14394
+ 'notelement', 'angle', 'gradient', 'registerserif', 'copyrightserif',
14395
+ 'trademarkserif', 'product', 'radical', 'dotmath', 'logicalnot',
14396
+ 'logicaland', 'logicalor', 'arrowdblboth', 'arrowdblleft', 'arrowdblup',
14397
+ 'arrowdblright', 'arrowdbldown', 'lozenge', 'angleleft', 'registersans',
14398
+ 'copyrightsans', 'trademarksans', 'summation', 'parenlefttp',
14399
+ 'parenleftex', 'parenleftbt', 'bracketlefttp', 'bracketleftex',
14400
+ 'bracketleftbt', 'bracelefttp', 'braceleftmid', 'braceleftbt', 'braceex',
14401
+ '', 'angleright', 'integral', 'integraltp', 'integralex', 'integralbt',
14402
+ 'parenrighttp', 'parenrightex', 'parenrightbt', 'bracketrighttp',
14403
+ 'bracketrightex', 'bracketrightbt', 'bracerighttp', 'bracerightmid',
14404
+ 'bracerightbt'],
14405
+ ZapfDingbatsEncoding: ['', '', '', '', '', '', '', '', '', '', '', '', '', '',
14406
+ '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
14407
+ 'space', 'a1', 'a2', 'a202', 'a3', 'a4', 'a5', 'a119', 'a118', 'a117',
14408
+ 'a11', 'a12', 'a13', 'a14', 'a15', 'a16', 'a105', 'a17', 'a18', 'a19',
14409
+ 'a20', 'a21', 'a22', 'a23', 'a24', 'a25', 'a26', 'a27', 'a28', 'a6', 'a7',
14410
+ 'a8', 'a9', 'a10', 'a29', 'a30', 'a31', 'a32', 'a33', 'a34', 'a35', 'a36',
14411
+ 'a37', 'a38', 'a39', 'a40', 'a41', 'a42', 'a43', 'a44', 'a45', 'a46',
14412
+ 'a47', 'a48', 'a49', 'a50', 'a51', 'a52', 'a53', 'a54', 'a55', 'a56',
14413
+ 'a57', 'a58', 'a59', 'a60', 'a61', 'a62', 'a63', 'a64', 'a65', 'a66',
14414
+ 'a67', 'a68', 'a69', 'a70', 'a71', 'a72', 'a73', 'a74', 'a203', 'a75',
14415
+ 'a204', 'a76', 'a77', 'a78', 'a79', 'a81', 'a82', 'a83', 'a84', 'a97',
14416
+ 'a98', 'a99', 'a100', '', 'a89', 'a90', 'a93', 'a94', 'a91', 'a92', 'a205',
14417
+ 'a85', 'a206', 'a86', 'a87', 'a88', 'a95', 'a96', '', '', '', '', '', '',
14418
+ '', '', '', '', '', '', '', '', '', '', '', '', '', 'a101', 'a102', 'a103',
14419
+ 'a104', 'a106', 'a107', 'a108', 'a112', 'a111', 'a110', 'a109', 'a120',
14420
+ 'a121', 'a122', 'a123', 'a124', 'a125', 'a126', 'a127', 'a128', 'a129',
14421
+ 'a130', 'a131', 'a132', 'a133', 'a134', 'a135', 'a136', 'a137', 'a138',
14422
+ 'a139', 'a140', 'a141', 'a142', 'a143', 'a144', 'a145', 'a146', 'a147',
14423
+ 'a148', 'a149', 'a150', 'a151', 'a152', 'a153', 'a154', 'a155', 'a156',
14424
+ 'a157', 'a158', 'a159', 'a160', 'a161', 'a163', 'a164', 'a196', 'a165',
14425
+ 'a192', 'a166', 'a167', 'a168', 'a169', 'a170', 'a171', 'a172', 'a173',
14426
+ 'a162', 'a174', 'a175', 'a176', 'a177', 'a178', 'a179', 'a193', 'a180',
14427
+ 'a199', 'a181', 'a200', 'a182', '', 'a201', 'a183', 'a184', 'a197', 'a185',
14428
+ 'a194', 'a198', 'a186', 'a195', 'a187', 'a188', 'a189', 'a190', 'a191']
14429
+ };
14430
+
14431
+ /**
14432
+ * Hold a map of decoded fonts and of the standard fourteen Type1
14433
+ * fonts and their acronyms.
14434
+ */
14435
+ var stdFontMap = {
14436
+ 'ArialNarrow': 'Helvetica',
14437
+ 'ArialNarrow-Bold': 'Helvetica-Bold',
14438
+ 'ArialNarrow-BoldItalic': 'Helvetica-BoldOblique',
14439
+ 'ArialNarrow-Italic': 'Helvetica-Oblique',
14440
+ 'ArialBlack': 'Helvetica',
14441
+ 'ArialBlack-Bold': 'Helvetica-Bold',
14442
+ 'ArialBlack-BoldItalic': 'Helvetica-BoldOblique',
14443
+ 'ArialBlack-Italic': 'Helvetica-Oblique',
14444
+ 'Arial': 'Helvetica',
14445
+ 'Arial-Bold': 'Helvetica-Bold',
14446
+ 'Arial-BoldItalic': 'Helvetica-BoldOblique',
14447
+ 'Arial-Italic': 'Helvetica-Oblique',
14448
+ 'Arial-BoldItalicMT': 'Helvetica-BoldOblique',
14449
+ 'Arial-BoldMT': 'Helvetica-Bold',
14450
+ 'Arial-ItalicMT': 'Helvetica-Oblique',
14451
+ 'ArialMT': 'Helvetica',
14452
+ 'Courier-Bold': 'Courier-Bold',
14453
+ 'Courier-BoldItalic': 'Courier-BoldOblique',
14454
+ 'Courier-Italic': 'Courier-Oblique',
14455
+ 'CourierNew': 'Courier',
14456
+ 'CourierNew-Bold': 'Courier-Bold',
14457
+ 'CourierNew-BoldItalic': 'Courier-BoldOblique',
14458
+ 'CourierNew-Italic': 'Courier-Oblique',
14459
+ 'CourierNewPS-BoldItalicMT': 'Courier-BoldOblique',
14460
+ 'CourierNewPS-BoldMT': 'Courier-Bold',
14461
+ 'CourierNewPS-ItalicMT': 'Courier-Oblique',
14462
+ 'CourierNewPSMT': 'Courier',
14463
+ 'Helvetica': 'Helvetica',
14464
+ 'Helvetica-Bold': 'Helvetica-Bold',
14465
+ 'Helvetica-BoldItalic': 'Helvetica-BoldOblique',
14466
+ 'Helvetica-BoldOblique': 'Helvetica-BoldOblique',
14467
+ 'Helvetica-Italic': 'Helvetica-Oblique',
14468
+ 'Helvetica-Oblique':'Helvetica-Oblique',
14469
+ 'Symbol-Bold': 'Symbol',
14470
+ 'Symbol-BoldItalic': 'Symbol',
14471
+ 'Symbol-Italic': 'Symbol',
14472
+ 'TimesNewRoman': 'Times-Roman',
14473
+ 'TimesNewRoman-Bold': 'Times-Bold',
14474
+ 'TimesNewRoman-BoldItalic': 'Times-BoldItalic',
14475
+ 'TimesNewRoman-Italic': 'Times-Italic',
14476
+ 'TimesNewRomanPS': 'Times-Roman',
14477
+ 'TimesNewRomanPS-Bold': 'Times-Bold',
14478
+ 'TimesNewRomanPS-BoldItalic': 'Times-BoldItalic',
14479
+ 'TimesNewRomanPS-BoldItalicMT': 'Times-BoldItalic',
14480
+ 'TimesNewRomanPS-BoldMT': 'Times-Bold',
14481
+ 'TimesNewRomanPS-Italic': 'Times-Italic',
14482
+ 'TimesNewRomanPS-ItalicMT': 'Times-Italic',
14483
+ 'TimesNewRomanPSMT': 'Times-Roman',
14484
+ 'TimesNewRomanPSMT-Bold': 'Times-Bold',
14485
+ 'TimesNewRomanPSMT-BoldItalic': 'Times-BoldItalic',
14486
+ 'TimesNewRomanPSMT-Italic': 'Times-Italic'
14487
+ };
14488
+
14489
+ /**
14490
+ * Holds the map of the non-standard fonts that might be included as a standard
14491
+ * fonts without glyph data.
14492
+ */
14493
+ var nonStdFontMap = {
14494
+ 'CenturyGothic': 'Helvetica',
14495
+ 'CenturyGothic-Bold': 'Helvetica-Bold',
14496
+ 'CenturyGothic-BoldItalic': 'Helvetica-BoldOblique',
14497
+ 'CenturyGothic-Italic': 'Helvetica-Oblique',
14498
+ 'ComicSansMS': 'Comic Sans MS',
14499
+ 'ComicSansMS-Bold': 'Comic Sans MS-Bold',
14500
+ 'ComicSansMS-BoldItalic': 'Comic Sans MS-BoldItalic',
14501
+ 'ComicSansMS-Italic': 'Comic Sans MS-Italic',
14502
+ 'LucidaConsole': 'Courier',
14503
+ 'LucidaConsole-Bold': 'Courier-Bold',
14504
+ 'LucidaConsole-BoldItalic': 'Courier-BoldOblique',
14505
+ 'LucidaConsole-Italic': 'Courier-Oblique',
14506
+ 'MS-Gothic': 'MS Gothic',
14507
+ 'MS-Gothic-Bold': 'MS Gothic-Bold',
14508
+ 'MS-Gothic-BoldItalic': 'MS Gothic-BoldItalic',
14509
+ 'MS-Gothic-Italic': 'MS Gothic-Italic',
14510
+ 'MS-Mincho': 'MS Mincho',
14511
+ 'MS-Mincho-Bold': 'MS Mincho-Bold',
14512
+ 'MS-Mincho-BoldItalic': 'MS Mincho-BoldItalic',
14513
+ 'MS-Mincho-Italic': 'MS Mincho-Italic',
14514
+ 'MS-PGothic': 'MS PGothic',
14515
+ 'MS-PGothic-Bold': 'MS PGothic-Bold',
14516
+ 'MS-PGothic-BoldItalic': 'MS PGothic-BoldItalic',
14517
+ 'MS-PGothic-Italic': 'MS PGothic-Italic',
14518
+ 'MS-PMincho': 'MS PMincho',
14519
+ 'MS-PMincho-Bold': 'MS PMincho-Bold',
14520
+ 'MS-PMincho-BoldItalic': 'MS PMincho-BoldItalic',
14521
+ 'MS-PMincho-Italic': 'MS PMincho-Italic',
14522
+ 'Wingdings': 'ZapfDingbats'
14523
+ };
14524
+
14525
+ var serifFonts = {
14526
+ 'Adobe Jenson': true, 'Adobe Text': true, 'Albertus': true,
14527
+ 'Aldus': true, 'Alexandria': true, 'Algerian': true,
14528
+ 'American Typewriter': true, 'Antiqua': true, 'Apex': true,
14529
+ 'Arno': true, 'Aster': true, 'Aurora': true,
14530
+ 'Baskerville': true, 'Bell': true, 'Bembo': true,
14531
+ 'Bembo Schoolbook': true, 'Benguiat': true, 'Berkeley Old Style': true,
14532
+ 'Bernhard Modern': true, 'Berthold City': true, 'Bodoni': true,
14533
+ 'Bauer Bodoni': true, 'Book Antiqua': true, 'Bookman': true,
14534
+ 'Bordeaux Roman': true, 'Californian FB': true, 'Calisto': true,
14535
+ 'Calvert': true, 'Capitals': true, 'Cambria': true,
14536
+ 'Cartier': true, 'Caslon': true, 'Catull': true,
14537
+ 'Centaur': true, 'Century Old Style': true, 'Century Schoolbook': true,
14538
+ 'Chaparral': true, 'Charis SIL': true, 'Cheltenham': true,
14539
+ 'Cholla Slab': true, 'Clarendon': true, 'Clearface': true,
14540
+ 'Cochin': true, 'Colonna': true, 'Computer Modern': true,
14541
+ 'Concrete Roman': true, 'Constantia': true, 'Cooper Black': true,
14542
+ 'Corona': true, 'Ecotype': true, 'Egyptienne': true,
14543
+ 'Elephant': true, 'Excelsior': true, 'Fairfield': true,
14544
+ 'FF Scala': true, 'Folkard': true, 'Footlight': true,
14545
+ 'FreeSerif': true, 'Friz Quadrata': true, 'Garamond': true,
14546
+ 'Gentium': true, 'Georgia': true, 'Gloucester': true,
14547
+ 'Goudy Old Style': true, 'Goudy Schoolbook': true, 'Goudy Pro Font': true,
14548
+ 'Granjon': true, 'Guardian Egyptian': true, 'Heather': true,
14549
+ 'Hercules': true, 'High Tower Text': true, 'Hiroshige': true,
14550
+ 'Hoefler Text': true, 'Humana Serif': true, 'Imprint': true,
14551
+ 'Ionic No. 5': true, 'Janson': true, 'Joanna': true,
14552
+ 'Korinna': true, 'Lexicon': true, 'Liberation Serif': true,
14553
+ 'Linux Libertine': true, 'Literaturnaya': true, 'Lucida': true,
14554
+ 'Lucida Bright': true, 'Melior': true, 'Memphis': true,
14555
+ 'Miller': true, 'Minion': true, 'Modern': true,
14556
+ 'Mona Lisa': true, 'Mrs Eaves': true, 'MS Serif': true,
14557
+ 'Museo Slab': true, 'New York': true, 'Nimbus Roman': true,
14558
+ 'NPS Rawlinson Roadway': true, 'Palatino': true, 'Perpetua': true,
14559
+ 'Plantin': true, 'Plantin Schoolbook': true, 'Playbill': true,
14560
+ 'Poor Richard': true, 'Rawlinson Roadway': true, 'Renault': true,
14561
+ 'Requiem': true, 'Rockwell': true, 'Roman': true,
14562
+ 'Rotis Serif': true, 'Sabon': true, 'Scala': true,
14563
+ 'Seagull': true, 'Sistina': true, 'Souvenir': true,
14564
+ 'STIX': true, 'Stone Informal': true, 'Stone Serif': true,
14565
+ 'Sylfaen': true, 'Times': true, 'Trajan': true,
14566
+ 'Trinité': true, 'Trump Mediaeval': true, 'Utopia': true,
14567
+ 'Vale Type': true, 'Bitstream Vera': true, 'Vera Serif': true,
14568
+ 'Versailles': true, 'Wanted': true, 'Weiss': true,
14569
+ 'Wide Latin': true, 'Windsor': true, 'XITS': true
14570
+ };
14571
+
14572
+ var symbolsFonts = {
14573
+ 'Dingbats': true, 'Symbol': true, 'ZapfDingbats': true
14574
+ };
14575
+
14576
+ // Glyph map for well-known standard fonts. Sometimes Ghostscript uses CID fonts
14577
+ // but does not embed the CID to GID mapping. The mapping is incomplete for all
14578
+ // glyphs, but common for some set of the standard fonts.
14579
+ var GlyphMapForStandardFonts = {
14580
+ '2': 10, '3': 32, '4': 33, '5': 34, '6': 35, '7': 36, '8': 37, '9': 38,
14581
+ '10': 39, '11': 40, '12': 41, '13': 42, '14': 43, '15': 44, '16': 45,
14582
+ '17': 46, '18': 47, '19': 48, '20': 49, '21': 50, '22': 51, '23': 52,
14583
+ '24': 53, '25': 54, '26': 55, '27': 56, '28': 57, '29': 58, '30': 894,
14584
+ '31': 60, '32': 61, '33': 62, '34': 63, '35': 64, '36': 65, '37': 66,
14585
+ '38': 67, '39': 68, '40': 69, '41': 70, '42': 71, '43': 72, '44': 73,
14586
+ '45': 74, '46': 75, '47': 76, '48': 77, '49': 78, '50': 79, '51': 80,
14587
+ '52': 81, '53': 82, '54': 83, '55': 84, '56': 85, '57': 86, '58': 87,
14588
+ '59': 88, '60': 89, '61': 90, '62': 91, '63': 92, '64': 93, '65': 94,
14589
+ '66': 95, '67': 96, '68': 97, '69': 98, '70': 99, '71': 100, '72': 101,
14590
+ '73': 102, '74': 103, '75': 104, '76': 105, '77': 106, '78': 107, '79': 108,
14591
+ '80': 109, '81': 110, '82': 111, '83': 112, '84': 113, '85': 114, '86': 115,
14592
+ '87': 116, '88': 117, '89': 118, '90': 119, '91': 120, '92': 121, '93': 122,
14593
+ '94': 123, '95': 124, '96': 125, '97': 126, '98': 196, '99': 197, '100': 199,
14594
+ '101': 201, '102': 209, '103': 214, '104': 220, '105': 225, '106': 224,
14595
+ '107': 226, '108': 228, '109': 227, '110': 229, '111': 231, '112': 233,
14596
+ '113': 232, '114': 234, '115': 235, '116': 237, '117': 236, '118': 238,
14597
+ '119': 239, '120': 241, '121': 243, '122': 242, '123': 244, '124': 246,
14598
+ '125': 245, '126': 250, '127': 249, '128': 251, '129': 252, '130': 8224,
14599
+ '131': 176, '132': 162, '133': 163, '134': 167, '135': 8226, '136': 182,
14600
+ '137': 223, '138': 174, '139': 169, '140': 8482, '141': 180, '142': 168,
14601
+ '143': 8800, '144': 198, '145': 216, '146': 8734, '147': 177, '148': 8804,
14602
+ '149': 8805, '150': 165, '151': 181, '152': 8706, '153': 8721, '154': 8719,
14603
+ '156': 8747, '157': 170, '158': 186, '159': 8486, '160': 230, '161': 248,
14604
+ '162': 191, '163': 161, '164': 172, '165': 8730, '166': 402, '167': 8776,
14605
+ '168': 8710, '169': 171, '170': 187, '171': 8230, '210': 218, '223': 711,
14606
+ '224': 321, '225': 322, '227': 353, '229': 382, '234': 253, '252': 263,
14607
+ '253': 268, '254': 269, '258': 258, '260': 260, '261': 261, '265': 280,
14608
+ '266': 281, '268': 283, '269': 313, '275': 323, '276': 324, '278': 328,
14609
+ '284': 345, '285': 346, '286': 347, '292': 367, '295': 377, '296': 378,
14610
+ '298': 380, '305': 963,
14611
+ '306': 964, '307': 966, '308': 8215, '309': 8252, '310': 8319, '311': 8359,
14612
+ '312': 8592, '313': 8593, '337': 9552, '493': 1039, '494': 1040, '705': 1524,
14613
+ '706': 8362, '710': 64288, '711': 64298, '759': 1617, '761': 1776,
14614
+ '763': 1778, '775': 1652, '777': 1764, '778': 1780, '779': 1781, '780': 1782,
14615
+ '782': 771, '783': 64726, '786': 8363, '788': 8532, '790': 768, '791': 769,
14616
+ '792': 768, '795': 803, '797': 64336, '798': 64337, '799': 64342,
14617
+ '800': 64343, '801': 64344, '802': 64345, '803': 64362, '804': 64363,
14618
+ '805': 64364, '2424': 7821, '2425': 7822, '2426': 7823, '2427': 7824,
14619
+ '2428': 7825, '2429': 7826, '2430': 7827, '2433': 7682, '2678': 8045,
14620
+ '2679': 8046, '2830': 1552, '2838': 686, '2840': 751, '2842': 753,
14621
+ '2843': 754, '2844': 755, '2846': 757, '2856': 767, '2857': 848, '2858': 849,
14622
+ '2862': 853, '2863': 854, '2864': 855, '2865': 861, '2866': 862, '2906': 7460,
14623
+ '2908': 7462, '2909': 7463, '2910': 7464, '2912': 7466, '2913': 7467,
14624
+ '2914': 7468, '2916': 7470, '2917': 7471, '2918': 7472, '2920': 7474,
14625
+ '2921': 7475, '2922': 7476, '2924': 7478, '2925': 7479, '2926': 7480,
14626
+ '2928': 7482, '2929': 7483, '2930': 7484, '2932': 7486, '2933': 7487,
14627
+ '2934': 7488, '2936': 7490, '2937': 7491, '2938': 7492, '2940': 7494,
14628
+ '2941': 7495, '2942': 7496, '2944': 7498, '2946': 7500, '2948': 7502,
14629
+ '2950': 7504, '2951': 7505, '2952': 7506, '2954': 7508, '2955': 7509,
14630
+ '2956': 7510, '2958': 7512, '2959': 7513, '2960': 7514, '2962': 7516,
14631
+ '2963': 7517, '2964': 7518, '2966': 7520, '2967': 7521, '2968': 7522,
14632
+ '2970': 7524, '2971': 7525, '2972': 7526, '2974': 7528, '2975': 7529,
14633
+ '2976': 7530, '2978': 1537, '2979': 1538, '2980': 1539, '2982': 1549,
14634
+ '2983': 1551, '2984': 1552, '2986': 1554, '2987': 1555, '2988': 1556,
14635
+ '2990': 1623, '2991': 1624, '2995': 1775, '2999': 1791, '3002': 64290,
14636
+ '3003': 64291, '3004': 64292, '3006': 64294, '3007': 64295, '3008': 64296,
14637
+ '3011': 1900, '3014': 8223, '3015': 8244, '3017': 7532, '3018': 7533,
14638
+ '3019': 7534, '3075': 7590, '3076': 7591, '3079': 7594, '3080': 7595,
14639
+ '3083': 7598, '3084': 7599, '3087': 7602, '3088': 7603, '3091': 7606,
14640
+ '3092': 7607, '3095': 7610, '3096': 7611, '3099': 7614, '3100': 7615,
14641
+ '3103': 7618, '3104': 7619, '3107': 8337, '3108': 8338, '3116': 1884,
14642
+ '3119': 1885, '3120': 1885, '3123': 1886, '3124': 1886, '3127': 1887,
14643
+ '3128': 1887, '3131': 1888, '3132': 1888, '3135': 1889, '3136': 1889,
14644
+ '3139': 1890, '3140': 1890, '3143': 1891, '3144': 1891, '3147': 1892,
14645
+ '3148': 1892, '3153': 580, '3154': 581, '3157': 584, '3158': 585, '3161': 588,
14646
+ '3162': 589, '3165': 891, '3166': 892, '3169': 1274, '3170': 1275,
14647
+ '3173': 1278, '3174': 1279, '3181': 7622, '3182': 7623, '3282': 11799,
14648
+ '3316': 578, '3379': 42785, '3393': 1159, '3416': 8377
14649
+ };
14650
+
14651
+ // Some characters, e.g. copyrightserif, are mapped to the private use area and
14652
+ // might not be displayed using standard fonts. Mapping/hacking well-known chars
14653
+ // to the similar equivalents in the normal characters range.
14654
+ var SpecialPUASymbols = {
14655
+ '63721': 0x00A9, // copyrightsans (0xF8E9) => copyright
14656
+ '63193': 0x00A9, // copyrightserif (0xF6D9) => copyright
14657
+ '63720': 0x00AE, // registersans (0xF8E8) => registered
14658
+ '63194': 0x00AE, // registerserif (0xF6DA) => registered
14659
+ '63722': 0x2122, // trademarksans (0xF8EA) => trademark
14660
+ '63195': 0x2122, // trademarkserif (0xF6DB) => trademark
14661
+ '63729': 0x23A7, // bracelefttp (0xF8F1)
14662
+ '63730': 0x23A8, // braceleftmid (0xF8F2)
14663
+ '63731': 0x23A9, // braceleftbt (0xF8F3)
14664
+ '63740': 0x23AB, // bracerighttp (0xF8FC)
14665
+ '63741': 0x23AC, // bracerightmid (0xF8FD)
14666
+ '63742': 0x23AD, // bracerightbt (0xF8FE)
14667
+ '63726': 0x23A1, // bracketlefttp (0xF8EE)
14668
+ '63727': 0x23A2, // bracketleftex (0xF8EF)
14669
+ '63728': 0x23A3, // bracketleftbt (0xF8F0)
14670
+ '63737': 0x23A4, // bracketrighttp (0xF8F9)
14671
+ '63738': 0x23A5, // bracketrightex (0xF8FA)
14672
+ '63739': 0x23A6, // bracketrightbt (0xF8FB)
14673
+ '63723': 0x239B, // parenlefttp (0xF8EB)
14674
+ '63724': 0x239C, // parenleftex (0xF8EC)
14675
+ '63725': 0x239D, // parenleftbt (0xF8ED)
14676
+ '63734': 0x239E, // parenrighttp (0xF8F6)
14677
+ '63735': 0x239F, // parenrightex (0xF8F7)
14678
+ '63736': 0x23A0, // parenrightbt (0xF8F8)
14679
+ };
14680
+ function mapSpecialUnicodeValues(code) {
14681
+ if (code >= 0xFFF0 && code <= 0xFFFF) { // Specials unicode block.
14682
+ return 0;
14683
+ } else if (code >= 0xF600 && code <= 0xF8FF) {
14684
+ return (SpecialPUASymbols[code] || code);
14685
+ }
14686
+ return code;
14687
+ }
14688
+
14689
+ var UnicodeRanges = [
14690
+ { 'begin': 0x0000, 'end': 0x007F }, // Basic Latin
14691
+ { 'begin': 0x0080, 'end': 0x00FF }, // Latin-1 Supplement
14692
+ { 'begin': 0x0100, 'end': 0x017F }, // Latin Extended-A
14693
+ { 'begin': 0x0180, 'end': 0x024F }, // Latin Extended-B
14694
+ { 'begin': 0x0250, 'end': 0x02AF }, // IPA Extensions
14695
+ { 'begin': 0x02B0, 'end': 0x02FF }, // Spacing Modifier Letters
14696
+ { 'begin': 0x0300, 'end': 0x036F }, // Combining Diacritical Marks
14697
+ { 'begin': 0x0370, 'end': 0x03FF }, // Greek and Coptic
14698
+ { 'begin': 0x2C80, 'end': 0x2CFF }, // Coptic
14699
+ { 'begin': 0x0400, 'end': 0x04FF }, // Cyrillic
14700
+ { 'begin': 0x0530, 'end': 0x058F }, // Armenian
14701
+ { 'begin': 0x0590, 'end': 0x05FF }, // Hebrew
14702
+ { 'begin': 0xA500, 'end': 0xA63F }, // Vai
14703
+ { 'begin': 0x0600, 'end': 0x06FF }, // Arabic
14704
+ { 'begin': 0x07C0, 'end': 0x07FF }, // NKo
14705
+ { 'begin': 0x0900, 'end': 0x097F }, // Devanagari
14706
+ { 'begin': 0x0980, 'end': 0x09FF }, // Bengali
14707
+ { 'begin': 0x0A00, 'end': 0x0A7F }, // Gurmukhi
14708
+ { 'begin': 0x0A80, 'end': 0x0AFF }, // Gujarati
14709
+ { 'begin': 0x0B00, 'end': 0x0B7F }, // Oriya
14710
+ { 'begin': 0x0B80, 'end': 0x0BFF }, // Tamil
14711
+ { 'begin': 0x0C00, 'end': 0x0C7F }, // Telugu
14712
+ { 'begin': 0x0C80, 'end': 0x0CFF }, // Kannada
14713
+ { 'begin': 0x0D00, 'end': 0x0D7F }, // Malayalam
14714
+ { 'begin': 0x0E00, 'end': 0x0E7F }, // Thai
14715
+ { 'begin': 0x0E80, 'end': 0x0EFF }, // Lao
14716
+ { 'begin': 0x10A0, 'end': 0x10FF }, // Georgian
14717
+ { 'begin': 0x1B00, 'end': 0x1B7F }, // Balinese
14718
+ { 'begin': 0x1100, 'end': 0x11FF }, // Hangul Jamo
14719
+ { 'begin': 0x1E00, 'end': 0x1EFF }, // Latin Extended Additional
14720
+ { 'begin': 0x1F00, 'end': 0x1FFF }, // Greek Extended
14721
+ { 'begin': 0x2000, 'end': 0x206F }, // General Punctuation
14722
+ { 'begin': 0x2070, 'end': 0x209F }, // Superscripts And Subscripts
14723
+ { 'begin': 0x20A0, 'end': 0x20CF }, // Currency Symbol
14724
+ { 'begin': 0x20D0, 'end': 0x20FF }, // Combining Diacritical Marks For Symbols
14725
+ { 'begin': 0x2100, 'end': 0x214F }, // Letterlike Symbols
14726
+ { 'begin': 0x2150, 'end': 0x218F }, // Number Forms
14727
+ { 'begin': 0x2190, 'end': 0x21FF }, // Arrows
14728
+ { 'begin': 0x2200, 'end': 0x22FF }, // Mathematical Operators
14729
+ { 'begin': 0x2300, 'end': 0x23FF }, // Miscellaneous Technical
14730
+ { 'begin': 0x2400, 'end': 0x243F }, // Control Pictures
14731
+ { 'begin': 0x2440, 'end': 0x245F }, // Optical Character Recognition
14732
+ { 'begin': 0x2460, 'end': 0x24FF }, // Enclosed Alphanumerics
14733
+ { 'begin': 0x2500, 'end': 0x257F }, // Box Drawing
14734
+ { 'begin': 0x2580, 'end': 0x259F }, // Block Elements
14735
+ { 'begin': 0x25A0, 'end': 0x25FF }, // Geometric Shapes
14736
+ { 'begin': 0x2600, 'end': 0x26FF }, // Miscellaneous Symbols
14737
+ { 'begin': 0x2700, 'end': 0x27BF }, // Dingbats
14738
+ { 'begin': 0x3000, 'end': 0x303F }, // CJK Symbols And Punctuation
14739
+ { 'begin': 0x3040, 'end': 0x309F }, // Hiragana
14740
+ { 'begin': 0x30A0, 'end': 0x30FF }, // Katakana
14741
+ { 'begin': 0x3100, 'end': 0x312F }, // Bopomofo
14742
+ { 'begin': 0x3130, 'end': 0x318F }, // Hangul Compatibility Jamo
14743
+ { 'begin': 0xA840, 'end': 0xA87F }, // Phags-pa
14744
+ { 'begin': 0x3200, 'end': 0x32FF }, // Enclosed CJK Letters And Months
14745
+ { 'begin': 0x3300, 'end': 0x33FF }, // CJK Compatibility
14746
+ { 'begin': 0xAC00, 'end': 0xD7AF }, // Hangul Syllables
14747
+ { 'begin': 0xD800, 'end': 0xDFFF }, // Non-Plane 0 *
14748
+ { 'begin': 0x10900, 'end': 0x1091F }, // Phoenicia
14749
+ { 'begin': 0x4E00, 'end': 0x9FFF }, // CJK Unified Ideographs
14750
+ { 'begin': 0xE000, 'end': 0xF8FF }, // Private Use Area (plane 0)
14751
+ { 'begin': 0x31C0, 'end': 0x31EF }, // CJK Strokes
14752
+ { 'begin': 0xFB00, 'end': 0xFB4F }, // Alphabetic Presentation Forms
14753
+ { 'begin': 0xFB50, 'end': 0xFDFF }, // Arabic Presentation Forms-A
14754
+ { 'begin': 0xFE20, 'end': 0xFE2F }, // Combining Half Marks
14755
+ { 'begin': 0xFE10, 'end': 0xFE1F }, // Vertical Forms
14756
+ { 'begin': 0xFE50, 'end': 0xFE6F }, // Small Form Variants
14757
+ { 'begin': 0xFE70, 'end': 0xFEFF }, // Arabic Presentation Forms-B
14758
+ { 'begin': 0xFF00, 'end': 0xFFEF }, // Halfwidth And Fullwidth Forms
14759
+ { 'begin': 0xFFF0, 'end': 0xFFFF }, // Specials
14760
+ { 'begin': 0x0F00, 'end': 0x0FFF }, // Tibetan
14761
+ { 'begin': 0x0700, 'end': 0x074F }, // Syriac
14762
+ { 'begin': 0x0780, 'end': 0x07BF }, // Thaana
14763
+ { 'begin': 0x0D80, 'end': 0x0DFF }, // Sinhala
14764
+ { 'begin': 0x1000, 'end': 0x109F }, // Myanmar
14765
+ { 'begin': 0x1200, 'end': 0x137F }, // Ethiopic
14766
+ { 'begin': 0x13A0, 'end': 0x13FF }, // Cherokee
14767
+ { 'begin': 0x1400, 'end': 0x167F }, // Unified Canadian Aboriginal Syllabics
14768
+ { 'begin': 0x1680, 'end': 0x169F }, // Ogham
14769
+ { 'begin': 0x16A0, 'end': 0x16FF }, // Runic
14770
+ { 'begin': 0x1780, 'end': 0x17FF }, // Khmer
14771
+ { 'begin': 0x1800, 'end': 0x18AF }, // Mongolian
14772
+ { 'begin': 0x2800, 'end': 0x28FF }, // Braille Patterns
14773
+ { 'begin': 0xA000, 'end': 0xA48F }, // Yi Syllables
14774
+ { 'begin': 0x1700, 'end': 0x171F }, // Tagalog
14775
+ { 'begin': 0x10300, 'end': 0x1032F }, // Old Italic
14776
+ { 'begin': 0x10330, 'end': 0x1034F }, // Gothic
14777
+ { 'begin': 0x10400, 'end': 0x1044F }, // Deseret
14778
+ { 'begin': 0x1D000, 'end': 0x1D0FF }, // Byzantine Musical Symbols
14779
+ { 'begin': 0x1D400, 'end': 0x1D7FF }, // Mathematical Alphanumeric Symbols
14780
+ { 'begin': 0xFF000, 'end': 0xFFFFD }, // Private Use (plane 15)
14781
+ { 'begin': 0xFE00, 'end': 0xFE0F }, // Variation Selectors
14782
+ { 'begin': 0xE0000, 'end': 0xE007F }, // Tags
14783
+ { 'begin': 0x1900, 'end': 0x194F }, // Limbu
14784
+ { 'begin': 0x1950, 'end': 0x197F }, // Tai Le
14785
+ { 'begin': 0x1980, 'end': 0x19DF }, // New Tai Lue
14786
+ { 'begin': 0x1A00, 'end': 0x1A1F }, // Buginese
14787
+ { 'begin': 0x2C00, 'end': 0x2C5F }, // Glagolitic
14788
+ { 'begin': 0x2D30, 'end': 0x2D7F }, // Tifinagh
14789
+ { 'begin': 0x4DC0, 'end': 0x4DFF }, // Yijing Hexagram Symbols
14790
+ { 'begin': 0xA800, 'end': 0xA82F }, // Syloti Nagri
14791
+ { 'begin': 0x10000, 'end': 0x1007F }, // Linear B Syllabary
14792
+ { 'begin': 0x10140, 'end': 0x1018F }, // Ancient Greek Numbers
14793
+ { 'begin': 0x10380, 'end': 0x1039F }, // Ugaritic
14794
+ { 'begin': 0x103A0, 'end': 0x103DF }, // Old Persian
14795
+ { 'begin': 0x10450, 'end': 0x1047F }, // Shavian
14796
+ { 'begin': 0x10480, 'end': 0x104AF }, // Osmanya
14797
+ { 'begin': 0x10800, 'end': 0x1083F }, // Cypriot Syllabary
14798
+ { 'begin': 0x10A00, 'end': 0x10A5F }, // Kharoshthi
14799
+ { 'begin': 0x1D300, 'end': 0x1D35F }, // Tai Xuan Jing Symbols
14800
+ { 'begin': 0x12000, 'end': 0x123FF }, // Cuneiform
14801
+ { 'begin': 0x1D360, 'end': 0x1D37F }, // Counting Rod Numerals
14802
+ { 'begin': 0x1B80, 'end': 0x1BBF }, // Sundanese
14803
+ { 'begin': 0x1C00, 'end': 0x1C4F }, // Lepcha
14804
+ { 'begin': 0x1C50, 'end': 0x1C7F }, // Ol Chiki
14805
+ { 'begin': 0xA880, 'end': 0xA8DF }, // Saurashtra
14806
+ { 'begin': 0xA900, 'end': 0xA92F }, // Kayah Li
14807
+ { 'begin': 0xA930, 'end': 0xA95F }, // Rejang
14808
+ { 'begin': 0xAA00, 'end': 0xAA5F }, // Cham
14809
+ { 'begin': 0x10190, 'end': 0x101CF }, // Ancient Symbols
14810
+ { 'begin': 0x101D0, 'end': 0x101FF }, // Phaistos Disc
14811
+ { 'begin': 0x102A0, 'end': 0x102DF }, // Carian
14812
+ { 'begin': 0x1F030, 'end': 0x1F09F } // Domino Tiles
14813
+ ];
14814
+
14815
+ var MacStandardGlyphOrdering = [
14816
+ '.notdef', '.null', 'nonmarkingreturn', 'space', 'exclam', 'quotedbl',
14817
+ 'numbersign', 'dollar', 'percent', 'ampersand', 'quotesingle', 'parenleft',
14818
+ 'parenright', 'asterisk', 'plus', 'comma', 'hyphen', 'period', 'slash',
14819
+ 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',
14820
+ 'nine', 'colon', 'semicolon', 'less', 'equal', 'greater', 'question', 'at',
14821
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
14822
+ 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'bracketleft',
14823
+ 'backslash', 'bracketright', 'asciicircum', 'underscore', 'grave', 'a', 'b',
14824
+ 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
14825
+ 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'braceleft', 'bar', 'braceright',
14826
+ 'asciitilde', 'Adieresis', 'Aring', 'Ccedilla', 'Eacute', 'Ntilde',
14827
+ 'Odieresis', 'Udieresis', 'aacute', 'agrave', 'acircumflex', 'adieresis',
14828
+ 'atilde', 'aring', 'ccedilla', 'eacute', 'egrave', 'ecircumflex', 'edieresis',
14829
+ 'iacute', 'igrave', 'icircumflex', 'idieresis', 'ntilde', 'oacute', 'ograve',
14830
+ 'ocircumflex', 'odieresis', 'otilde', 'uacute', 'ugrave', 'ucircumflex',
14831
+ 'udieresis', 'dagger', 'degree', 'cent', 'sterling', 'section', 'bullet',
14832
+ 'paragraph', 'germandbls', 'registered', 'copyright', 'trademark', 'acute',
14833
+ 'dieresis', 'notequal', 'AE', 'Oslash', 'infinity', 'plusminus', 'lessequal',
14834
+ 'greaterequal', 'yen', 'mu', 'partialdiff', 'summation', 'product', 'pi',
14835
+ 'integral', 'ordfeminine', 'ordmasculine', 'Omega', 'ae', 'oslash',
14836
+ 'questiondown', 'exclamdown', 'logicalnot', 'radical', 'florin',
14837
+ 'approxequal', 'Delta', 'guillemotleft', 'guillemotright', 'ellipsis',
14838
+ 'nonbreakingspace', 'Agrave', 'Atilde', 'Otilde', 'OE', 'oe', 'endash',
14839
+ 'emdash', 'quotedblleft', 'quotedblright', 'quoteleft', 'quoteright',
14840
+ 'divide', 'lozenge', 'ydieresis', 'Ydieresis', 'fraction', 'currency',
14841
+ 'guilsinglleft', 'guilsinglright', 'fi', 'fl', 'daggerdbl', 'periodcentered',
14842
+ 'quotesinglbase', 'quotedblbase', 'perthousand', 'Acircumflex',
14843
+ 'Ecircumflex', 'Aacute', 'Edieresis', 'Egrave', 'Iacute', 'Icircumflex',
14844
+ 'Idieresis', 'Igrave', 'Oacute', 'Ocircumflex', 'apple', 'Ograve', 'Uacute',
14845
+ 'Ucircumflex', 'Ugrave', 'dotlessi', 'circumflex', 'tilde', 'macron',
14846
+ 'breve', 'dotaccent', 'ring', 'cedilla', 'hungarumlaut', 'ogonek', 'caron',
14847
+ 'Lslash', 'lslash', 'Scaron', 'scaron', 'Zcaron', 'zcaron', 'brokenbar',
14848
+ 'Eth', 'eth', 'Yacute', 'yacute', 'Thorn', 'thorn', 'minus', 'multiply',
14849
+ 'onesuperior', 'twosuperior', 'threesuperior', 'onehalf', 'onequarter',
14850
+ 'threequarters', 'franc', 'Gbreve', 'gbreve', 'Idotaccent', 'Scedilla',
14851
+ 'scedilla', 'Cacute', 'cacute', 'Ccaron', 'ccaron', 'dcroat'];
14852
+
14853
+ function getUnicodeRangeFor(value) {
14854
+ for (var i = 0, ii = UnicodeRanges.length; i < ii; i++) {
14855
+ var range = UnicodeRanges[i];
14856
+ if (value >= range.begin && value < range.end) {
14857
+ return i;
14858
+ }
14859
+ }
14860
+ return -1;
14861
+ }
14862
+
14863
+ function isRTLRangeFor(value) {
14864
+ var range = UnicodeRanges[13];
14865
+ if (value >= range.begin && value < range.end) {
14866
+ return true;
14867
+ }
14868
+ range = UnicodeRanges[11];
14869
+ if (value >= range.begin && value < range.end) {
14870
+ return true;
14871
+ }
14872
+ return false;
14873
+ }
14874
+
14875
+ // The normalization table is obtained by filtering the Unicode characters
14876
+ // database with <compat> entries.
14877
+ var NormalizedUnicodes = {
14878
+ '\u00A8': '\u0020\u0308',
14879
+ '\u00AF': '\u0020\u0304',
14880
+ '\u00B4': '\u0020\u0301',
14881
+ '\u00B5': '\u03BC',
14882
+ '\u00B8': '\u0020\u0327',
14883
+ '\u0132': '\u0049\u004A',
14884
+ '\u0133': '\u0069\u006A',
14885
+ '\u013F': '\u004C\u00B7',
14886
+ '\u0140': '\u006C\u00B7',
14887
+ '\u0149': '\u02BC\u006E',
14888
+ '\u017F': '\u0073',
14889
+ '\u01C4': '\u0044\u017D',
14890
+ '\u01C5': '\u0044\u017E',
14891
+ '\u01C6': '\u0064\u017E',
14892
+ '\u01C7': '\u004C\u004A',
14893
+ '\u01C8': '\u004C\u006A',
14894
+ '\u01C9': '\u006C\u006A',
14895
+ '\u01CA': '\u004E\u004A',
14896
+ '\u01CB': '\u004E\u006A',
14897
+ '\u01CC': '\u006E\u006A',
14898
+ '\u01F1': '\u0044\u005A',
14899
+ '\u01F2': '\u0044\u007A',
14900
+ '\u01F3': '\u0064\u007A',
14901
+ '\u02D8': '\u0020\u0306',
14902
+ '\u02D9': '\u0020\u0307',
14903
+ '\u02DA': '\u0020\u030A',
14904
+ '\u02DB': '\u0020\u0328',
14905
+ '\u02DC': '\u0020\u0303',
14906
+ '\u02DD': '\u0020\u030B',
14907
+ '\u037A': '\u0020\u0345',
14908
+ '\u0384': '\u0020\u0301',
14909
+ '\u03D0': '\u03B2',
14910
+ '\u03D1': '\u03B8',
14911
+ '\u03D2': '\u03A5',
14912
+ '\u03D5': '\u03C6',
14913
+ '\u03D6': '\u03C0',
14914
+ '\u03F0': '\u03BA',
14915
+ '\u03F1': '\u03C1',
14916
+ '\u03F2': '\u03C2',
14917
+ '\u03F4': '\u0398',
14918
+ '\u03F5': '\u03B5',
14919
+ '\u03F9': '\u03A3',
14920
+ '\u0587': '\u0565\u0582',
14921
+ '\u0675': '\u0627\u0674',
14922
+ '\u0676': '\u0648\u0674',
14923
+ '\u0677': '\u06C7\u0674',
14924
+ '\u0678': '\u064A\u0674',
14925
+ '\u0E33': '\u0E4D\u0E32',
14926
+ '\u0EB3': '\u0ECD\u0EB2',
14927
+ '\u0EDC': '\u0EAB\u0E99',
14928
+ '\u0EDD': '\u0EAB\u0EA1',
14929
+ '\u0F77': '\u0FB2\u0F81',
14930
+ '\u0F79': '\u0FB3\u0F81',
14931
+ '\u1E9A': '\u0061\u02BE',
14932
+ '\u1FBD': '\u0020\u0313',
14933
+ '\u1FBF': '\u0020\u0313',
14934
+ '\u1FC0': '\u0020\u0342',
14935
+ '\u1FFE': '\u0020\u0314',
14936
+ '\u2002': '\u0020',
14937
+ '\u2003': '\u0020',
14938
+ '\u2004': '\u0020',
14939
+ '\u2005': '\u0020',
14940
+ '\u2006': '\u0020',
14941
+ '\u2008': '\u0020',
14942
+ '\u2009': '\u0020',
14943
+ '\u200A': '\u0020',
14944
+ '\u2017': '\u0020\u0333',
14945
+ '\u2024': '\u002E',
14946
+ '\u2025': '\u002E\u002E',
14947
+ '\u2026': '\u002E\u002E\u002E',
14948
+ '\u2033': '\u2032\u2032',
14949
+ '\u2034': '\u2032\u2032\u2032',
14950
+ '\u2036': '\u2035\u2035',
14951
+ '\u2037': '\u2035\u2035\u2035',
14952
+ '\u203C': '\u0021\u0021',
14953
+ '\u203E': '\u0020\u0305',
14954
+ '\u2047': '\u003F\u003F',
14955
+ '\u2048': '\u003F\u0021',
14956
+ '\u2049': '\u0021\u003F',
14957
+ '\u2057': '\u2032\u2032\u2032\u2032',
14958
+ '\u205F': '\u0020',
14959
+ '\u20A8': '\u0052\u0073',
14960
+ '\u2100': '\u0061\u002F\u0063',
14961
+ '\u2101': '\u0061\u002F\u0073',
14962
+ '\u2103': '\u00B0\u0043',
14963
+ '\u2105': '\u0063\u002F\u006F',
14964
+ '\u2106': '\u0063\u002F\u0075',
14965
+ '\u2107': '\u0190',
14966
+ '\u2109': '\u00B0\u0046',
14967
+ '\u2116': '\u004E\u006F',
14968
+ '\u2121': '\u0054\u0045\u004C',
14969
+ '\u2135': '\u05D0',
14970
+ '\u2136': '\u05D1',
14971
+ '\u2137': '\u05D2',
14972
+ '\u2138': '\u05D3',
14973
+ '\u213B': '\u0046\u0041\u0058',
14974
+ '\u2160': '\u0049',
14975
+ '\u2161': '\u0049\u0049',
14976
+ '\u2162': '\u0049\u0049\u0049',
14977
+ '\u2163': '\u0049\u0056',
14978
+ '\u2164': '\u0056',
14979
+ '\u2165': '\u0056\u0049',
14980
+ '\u2166': '\u0056\u0049\u0049',
14981
+ '\u2167': '\u0056\u0049\u0049\u0049',
14982
+ '\u2168': '\u0049\u0058',
14983
+ '\u2169': '\u0058',
14984
+ '\u216A': '\u0058\u0049',
14985
+ '\u216B': '\u0058\u0049\u0049',
14986
+ '\u216C': '\u004C',
14987
+ '\u216D': '\u0043',
14988
+ '\u216E': '\u0044',
14989
+ '\u216F': '\u004D',
14990
+ '\u2170': '\u0069',
14991
+ '\u2171': '\u0069\u0069',
14992
+ '\u2172': '\u0069\u0069\u0069',
14993
+ '\u2173': '\u0069\u0076',
14994
+ '\u2174': '\u0076',
14995
+ '\u2175': '\u0076\u0069',
14996
+ '\u2176': '\u0076\u0069\u0069',
14997
+ '\u2177': '\u0076\u0069\u0069\u0069',
14998
+ '\u2178': '\u0069\u0078',
14999
+ '\u2179': '\u0078',
15000
+ '\u217A': '\u0078\u0069',
15001
+ '\u217B': '\u0078\u0069\u0069',
15002
+ '\u217C': '\u006C',
15003
+ '\u217D': '\u0063',
15004
+ '\u217E': '\u0064',
15005
+ '\u217F': '\u006D',
15006
+ '\u222C': '\u222B\u222B',
15007
+ '\u222D': '\u222B\u222B\u222B',
15008
+ '\u222F': '\u222E\u222E',
15009
+ '\u2230': '\u222E\u222E\u222E',
15010
+ '\u2474': '\u0028\u0031\u0029',
15011
+ '\u2475': '\u0028\u0032\u0029',
15012
+ '\u2476': '\u0028\u0033\u0029',
15013
+ '\u2477': '\u0028\u0034\u0029',
15014
+ '\u2478': '\u0028\u0035\u0029',
15015
+ '\u2479': '\u0028\u0036\u0029',
15016
+ '\u247A': '\u0028\u0037\u0029',
15017
+ '\u247B': '\u0028\u0038\u0029',
15018
+ '\u247C': '\u0028\u0039\u0029',
15019
+ '\u247D': '\u0028\u0031\u0030\u0029',
15020
+ '\u247E': '\u0028\u0031\u0031\u0029',
15021
+ '\u247F': '\u0028\u0031\u0032\u0029',
15022
+ '\u2480': '\u0028\u0031\u0033\u0029',
15023
+ '\u2481': '\u0028\u0031\u0034\u0029',
15024
+ '\u2482': '\u0028\u0031\u0035\u0029',
15025
+ '\u2483': '\u0028\u0031\u0036\u0029',
15026
+ '\u2484': '\u0028\u0031\u0037\u0029',
15027
+ '\u2485': '\u0028\u0031\u0038\u0029',
15028
+ '\u2486': '\u0028\u0031\u0039\u0029',
15029
+ '\u2487': '\u0028\u0032\u0030\u0029',
15030
+ '\u2488': '\u0031\u002E',
15031
+ '\u2489': '\u0032\u002E',
15032
+ '\u248A': '\u0033\u002E',
15033
+ '\u248B': '\u0034\u002E',
15034
+ '\u248C': '\u0035\u002E',
15035
+ '\u248D': '\u0036\u002E',
15036
+ '\u248E': '\u0037\u002E',
15037
+ '\u248F': '\u0038\u002E',
15038
+ '\u2490': '\u0039\u002E',
15039
+ '\u2491': '\u0031\u0030\u002E',
15040
+ '\u2492': '\u0031\u0031\u002E',
15041
+ '\u2493': '\u0031\u0032\u002E',
15042
+ '\u2494': '\u0031\u0033\u002E',
15043
+ '\u2495': '\u0031\u0034\u002E',
15044
+ '\u2496': '\u0031\u0035\u002E',
15045
+ '\u2497': '\u0031\u0036\u002E',
15046
+ '\u2498': '\u0031\u0037\u002E',
15047
+ '\u2499': '\u0031\u0038\u002E',
15048
+ '\u249A': '\u0031\u0039\u002E',
15049
+ '\u249B': '\u0032\u0030\u002E',
15050
+ '\u249C': '\u0028\u0061\u0029',
15051
+ '\u249D': '\u0028\u0062\u0029',
15052
+ '\u249E': '\u0028\u0063\u0029',
15053
+ '\u249F': '\u0028\u0064\u0029',
15054
+ '\u24A0': '\u0028\u0065\u0029',
15055
+ '\u24A1': '\u0028\u0066\u0029',
15056
+ '\u24A2': '\u0028\u0067\u0029',
15057
+ '\u24A3': '\u0028\u0068\u0029',
15058
+ '\u24A4': '\u0028\u0069\u0029',
15059
+ '\u24A5': '\u0028\u006A\u0029',
15060
+ '\u24A6': '\u0028\u006B\u0029',
15061
+ '\u24A7': '\u0028\u006C\u0029',
15062
+ '\u24A8': '\u0028\u006D\u0029',
15063
+ '\u24A9': '\u0028\u006E\u0029',
15064
+ '\u24AA': '\u0028\u006F\u0029',
15065
+ '\u24AB': '\u0028\u0070\u0029',
15066
+ '\u24AC': '\u0028\u0071\u0029',
15067
+ '\u24AD': '\u0028\u0072\u0029',
15068
+ '\u24AE': '\u0028\u0073\u0029',
15069
+ '\u24AF': '\u0028\u0074\u0029',
15070
+ '\u24B0': '\u0028\u0075\u0029',
15071
+ '\u24B1': '\u0028\u0076\u0029',
15072
+ '\u24B2': '\u0028\u0077\u0029',
15073
+ '\u24B3': '\u0028\u0078\u0029',
15074
+ '\u24B4': '\u0028\u0079\u0029',
15075
+ '\u24B5': '\u0028\u007A\u0029',
15076
+ '\u2A0C': '\u222B\u222B\u222B\u222B',
15077
+ '\u2A74': '\u003A\u003A\u003D',
15078
+ '\u2A75': '\u003D\u003D',
15079
+ '\u2A76': '\u003D\u003D\u003D',
15080
+ '\u2E9F': '\u6BCD',
15081
+ '\u2EF3': '\u9F9F',
15082
+ '\u2F00': '\u4E00',
15083
+ '\u2F01': '\u4E28',
15084
+ '\u2F02': '\u4E36',
15085
+ '\u2F03': '\u4E3F',
15086
+ '\u2F04': '\u4E59',
15087
+ '\u2F05': '\u4E85',
15088
+ '\u2F06': '\u4E8C',
15089
+ '\u2F07': '\u4EA0',
15090
+ '\u2F08': '\u4EBA',
15091
+ '\u2F09': '\u513F',
15092
+ '\u2F0A': '\u5165',
15093
+ '\u2F0B': '\u516B',
15094
+ '\u2F0C': '\u5182',
15095
+ '\u2F0D': '\u5196',
15096
+ '\u2F0E': '\u51AB',
15097
+ '\u2F0F': '\u51E0',
15098
+ '\u2F10': '\u51F5',
15099
+ '\u2F11': '\u5200',
15100
+ '\u2F12': '\u529B',
15101
+ '\u2F13': '\u52F9',
15102
+ '\u2F14': '\u5315',
15103
+ '\u2F15': '\u531A',
15104
+ '\u2F16': '\u5338',
15105
+ '\u2F17': '\u5341',
15106
+ '\u2F18': '\u535C',
15107
+ '\u2F19': '\u5369',
15108
+ '\u2F1A': '\u5382',
15109
+ '\u2F1B': '\u53B6',
15110
+ '\u2F1C': '\u53C8',
15111
+ '\u2F1D': '\u53E3',
15112
+ '\u2F1E': '\u56D7',
15113
+ '\u2F1F': '\u571F',
15114
+ '\u2F20': '\u58EB',
15115
+ '\u2F21': '\u5902',
15116
+ '\u2F22': '\u590A',
15117
+ '\u2F23': '\u5915',
15118
+ '\u2F24': '\u5927',
15119
+ '\u2F25': '\u5973',
15120
+ '\u2F26': '\u5B50',
15121
+ '\u2F27': '\u5B80',
15122
+ '\u2F28': '\u5BF8',
15123
+ '\u2F29': '\u5C0F',
15124
+ '\u2F2A': '\u5C22',
15125
+ '\u2F2B': '\u5C38',
15126
+ '\u2F2C': '\u5C6E',
15127
+ '\u2F2D': '\u5C71',
15128
+ '\u2F2E': '\u5DDB',
15129
+ '\u2F2F': '\u5DE5',
15130
+ '\u2F30': '\u5DF1',
15131
+ '\u2F31': '\u5DFE',
15132
+ '\u2F32': '\u5E72',
15133
+ '\u2F33': '\u5E7A',
15134
+ '\u2F34': '\u5E7F',
15135
+ '\u2F35': '\u5EF4',
15136
+ '\u2F36': '\u5EFE',
15137
+ '\u2F37': '\u5F0B',
15138
+ '\u2F38': '\u5F13',
15139
+ '\u2F39': '\u5F50',
15140
+ '\u2F3A': '\u5F61',
15141
+ '\u2F3B': '\u5F73',
15142
+ '\u2F3C': '\u5FC3',
15143
+ '\u2F3D': '\u6208',
15144
+ '\u2F3E': '\u6236',
15145
+ '\u2F3F': '\u624B',
15146
+ '\u2F40': '\u652F',
15147
+ '\u2F41': '\u6534',
15148
+ '\u2F42': '\u6587',
15149
+ '\u2F43': '\u6597',
15150
+ '\u2F44': '\u65A4',
15151
+ '\u2F45': '\u65B9',
15152
+ '\u2F46': '\u65E0',
15153
+ '\u2F47': '\u65E5',
15154
+ '\u2F48': '\u66F0',
15155
+ '\u2F49': '\u6708',
15156
+ '\u2F4A': '\u6728',
15157
+ '\u2F4B': '\u6B20',
15158
+ '\u2F4C': '\u6B62',
15159
+ '\u2F4D': '\u6B79',
15160
+ '\u2F4E': '\u6BB3',
15161
+ '\u2F4F': '\u6BCB',
15162
+ '\u2F50': '\u6BD4',
15163
+ '\u2F51': '\u6BDB',
15164
+ '\u2F52': '\u6C0F',
15165
+ '\u2F53': '\u6C14',
15166
+ '\u2F54': '\u6C34',
15167
+ '\u2F55': '\u706B',
15168
+ '\u2F56': '\u722A',
15169
+ '\u2F57': '\u7236',
15170
+ '\u2F58': '\u723B',
15171
+ '\u2F59': '\u723F',
15172
+ '\u2F5A': '\u7247',
15173
+ '\u2F5B': '\u7259',
15174
+ '\u2F5C': '\u725B',
15175
+ '\u2F5D': '\u72AC',
15176
+ '\u2F5E': '\u7384',
15177
+ '\u2F5F': '\u7389',
15178
+ '\u2F60': '\u74DC',
15179
+ '\u2F61': '\u74E6',
15180
+ '\u2F62': '\u7518',
15181
+ '\u2F63': '\u751F',
15182
+ '\u2F64': '\u7528',
15183
+ '\u2F65': '\u7530',
15184
+ '\u2F66': '\u758B',
15185
+ '\u2F67': '\u7592',
15186
+ '\u2F68': '\u7676',
15187
+ '\u2F69': '\u767D',
15188
+ '\u2F6A': '\u76AE',
15189
+ '\u2F6B': '\u76BF',
15190
+ '\u2F6C': '\u76EE',
15191
+ '\u2F6D': '\u77DB',
15192
+ '\u2F6E': '\u77E2',
15193
+ '\u2F6F': '\u77F3',
15194
+ '\u2F70': '\u793A',
15195
+ '\u2F71': '\u79B8',
15196
+ '\u2F72': '\u79BE',
15197
+ '\u2F73': '\u7A74',
15198
+ '\u2F74': '\u7ACB',
15199
+ '\u2F75': '\u7AF9',
15200
+ '\u2F76': '\u7C73',
15201
+ '\u2F77': '\u7CF8',
15202
+ '\u2F78': '\u7F36',
15203
+ '\u2F79': '\u7F51',
15204
+ '\u2F7A': '\u7F8A',
15205
+ '\u2F7B': '\u7FBD',
15206
+ '\u2F7C': '\u8001',
15207
+ '\u2F7D': '\u800C',
15208
+ '\u2F7E': '\u8012',
15209
+ '\u2F7F': '\u8033',
15210
+ '\u2F80': '\u807F',
15211
+ '\u2F81': '\u8089',
15212
+ '\u2F82': '\u81E3',
15213
+ '\u2F83': '\u81EA',
15214
+ '\u2F84': '\u81F3',
15215
+ '\u2F85': '\u81FC',
15216
+ '\u2F86': '\u820C',
15217
+ '\u2F87': '\u821B',
15218
+ '\u2F88': '\u821F',
15219
+ '\u2F89': '\u826E',
15220
+ '\u2F8A': '\u8272',
15221
+ '\u2F8B': '\u8278',
15222
+ '\u2F8C': '\u864D',
15223
+ '\u2F8D': '\u866B',
15224
+ '\u2F8E': '\u8840',
15225
+ '\u2F8F': '\u884C',
15226
+ '\u2F90': '\u8863',
15227
+ '\u2F91': '\u897E',
15228
+ '\u2F92': '\u898B',
15229
+ '\u2F93': '\u89D2',
15230
+ '\u2F94': '\u8A00',
15231
+ '\u2F95': '\u8C37',
15232
+ '\u2F96': '\u8C46',
15233
+ '\u2F97': '\u8C55',
15234
+ '\u2F98': '\u8C78',
15235
+ '\u2F99': '\u8C9D',
15236
+ '\u2F9A': '\u8D64',
15237
+ '\u2F9B': '\u8D70',
15238
+ '\u2F9C': '\u8DB3',
15239
+ '\u2F9D': '\u8EAB',
15240
+ '\u2F9E': '\u8ECA',
15241
+ '\u2F9F': '\u8F9B',
15242
+ '\u2FA0': '\u8FB0',
15243
+ '\u2FA1': '\u8FB5',
15244
+ '\u2FA2': '\u9091',
15245
+ '\u2FA3': '\u9149',
15246
+ '\u2FA4': '\u91C6',
15247
+ '\u2FA5': '\u91CC',
15248
+ '\u2FA6': '\u91D1',
15249
+ '\u2FA7': '\u9577',
15250
+ '\u2FA8': '\u9580',
15251
+ '\u2FA9': '\u961C',
15252
+ '\u2FAA': '\u96B6',
15253
+ '\u2FAB': '\u96B9',
15254
+ '\u2FAC': '\u96E8',
15255
+ '\u2FAD': '\u9751',
15256
+ '\u2FAE': '\u975E',
15257
+ '\u2FAF': '\u9762',
15258
+ '\u2FB0': '\u9769',
15259
+ '\u2FB1': '\u97CB',
15260
+ '\u2FB2': '\u97ED',
15261
+ '\u2FB3': '\u97F3',
15262
+ '\u2FB4': '\u9801',
15263
+ '\u2FB5': '\u98A8',
15264
+ '\u2FB6': '\u98DB',
15265
+ '\u2FB7': '\u98DF',
15266
+ '\u2FB8': '\u9996',
15267
+ '\u2FB9': '\u9999',
15268
+ '\u2FBA': '\u99AC',
15269
+ '\u2FBB': '\u9AA8',
15270
+ '\u2FBC': '\u9AD8',
15271
+ '\u2FBD': '\u9ADF',
15272
+ '\u2FBE': '\u9B25',
15273
+ '\u2FBF': '\u9B2F',
15274
+ '\u2FC0': '\u9B32',
15275
+ '\u2FC1': '\u9B3C',
15276
+ '\u2FC2': '\u9B5A',
15277
+ '\u2FC3': '\u9CE5',
15278
+ '\u2FC4': '\u9E75',
15279
+ '\u2FC5': '\u9E7F',
15280
+ '\u2FC6': '\u9EA5',
15281
+ '\u2FC7': '\u9EBB',
15282
+ '\u2FC8': '\u9EC3',
15283
+ '\u2FC9': '\u9ECD',
15284
+ '\u2FCA': '\u9ED1',
15285
+ '\u2FCB': '\u9EF9',
15286
+ '\u2FCC': '\u9EFD',
15287
+ '\u2FCD': '\u9F0E',
15288
+ '\u2FCE': '\u9F13',
15289
+ '\u2FCF': '\u9F20',
15290
+ '\u2FD0': '\u9F3B',
15291
+ '\u2FD1': '\u9F4A',
15292
+ '\u2FD2': '\u9F52',
15293
+ '\u2FD3': '\u9F8D',
15294
+ '\u2FD4': '\u9F9C',
15295
+ '\u2FD5': '\u9FA0',
15296
+ '\u3036': '\u3012',
15297
+ '\u3038': '\u5341',
15298
+ '\u3039': '\u5344',
15299
+ '\u303A': '\u5345',
15300
+ '\u309B': '\u0020\u3099',
15301
+ '\u309C': '\u0020\u309A',
15302
+ '\u3131': '\u1100',
15303
+ '\u3132': '\u1101',
15304
+ '\u3133': '\u11AA',
15305
+ '\u3134': '\u1102',
15306
+ '\u3135': '\u11AC',
15307
+ '\u3136': '\u11AD',
15308
+ '\u3137': '\u1103',
15309
+ '\u3138': '\u1104',
15310
+ '\u3139': '\u1105',
15311
+ '\u313A': '\u11B0',
15312
+ '\u313B': '\u11B1',
15313
+ '\u313C': '\u11B2',
15314
+ '\u313D': '\u11B3',
15315
+ '\u313E': '\u11B4',
15316
+ '\u313F': '\u11B5',
15317
+ '\u3140': '\u111A',
15318
+ '\u3141': '\u1106',
15319
+ '\u3142': '\u1107',
15320
+ '\u3143': '\u1108',
15321
+ '\u3144': '\u1121',
15322
+ '\u3145': '\u1109',
15323
+ '\u3146': '\u110A',
15324
+ '\u3147': '\u110B',
15325
+ '\u3148': '\u110C',
15326
+ '\u3149': '\u110D',
15327
+ '\u314A': '\u110E',
15328
+ '\u314B': '\u110F',
15329
+ '\u314C': '\u1110',
15330
+ '\u314D': '\u1111',
15331
+ '\u314E': '\u1112',
15332
+ '\u314F': '\u1161',
15333
+ '\u3150': '\u1162',
15334
+ '\u3151': '\u1163',
15335
+ '\u3152': '\u1164',
15336
+ '\u3153': '\u1165',
15337
+ '\u3154': '\u1166',
15338
+ '\u3155': '\u1167',
15339
+ '\u3156': '\u1168',
15340
+ '\u3157': '\u1169',
15341
+ '\u3158': '\u116A',
15342
+ '\u3159': '\u116B',
15343
+ '\u315A': '\u116C',
15344
+ '\u315B': '\u116D',
15345
+ '\u315C': '\u116E',
15346
+ '\u315D': '\u116F',
15347
+ '\u315E': '\u1170',
15348
+ '\u315F': '\u1171',
15349
+ '\u3160': '\u1172',
15350
+ '\u3161': '\u1173',
15351
+ '\u3162': '\u1174',
15352
+ '\u3163': '\u1175',
15353
+ '\u3164': '\u1160',
15354
+ '\u3165': '\u1114',
15355
+ '\u3166': '\u1115',
15356
+ '\u3167': '\u11C7',
15357
+ '\u3168': '\u11C8',
15358
+ '\u3169': '\u11CC',
15359
+ '\u316A': '\u11CE',
15360
+ '\u316B': '\u11D3',
15361
+ '\u316C': '\u11D7',
15362
+ '\u316D': '\u11D9',
15363
+ '\u316E': '\u111C',
15364
+ '\u316F': '\u11DD',
15365
+ '\u3170': '\u11DF',
15366
+ '\u3171': '\u111D',
15367
+ '\u3172': '\u111E',
15368
+ '\u3173': '\u1120',
15369
+ '\u3174': '\u1122',
15370
+ '\u3175': '\u1123',
15371
+ '\u3176': '\u1127',
15372
+ '\u3177': '\u1129',
15373
+ '\u3178': '\u112B',
15374
+ '\u3179': '\u112C',
15375
+ '\u317A': '\u112D',
15376
+ '\u317B': '\u112E',
15377
+ '\u317C': '\u112F',
15378
+ '\u317D': '\u1132',
15379
+ '\u317E': '\u1136',
15380
+ '\u317F': '\u1140',
15381
+ '\u3180': '\u1147',
15382
+ '\u3181': '\u114C',
15383
+ '\u3182': '\u11F1',
15384
+ '\u3183': '\u11F2',
15385
+ '\u3184': '\u1157',
15386
+ '\u3185': '\u1158',
15387
+ '\u3186': '\u1159',
15388
+ '\u3187': '\u1184',
15389
+ '\u3188': '\u1185',
15390
+ '\u3189': '\u1188',
15391
+ '\u318A': '\u1191',
15392
+ '\u318B': '\u1192',
15393
+ '\u318C': '\u1194',
15394
+ '\u318D': '\u119E',
15395
+ '\u318E': '\u11A1',
15396
+ '\u3200': '\u0028\u1100\u0029',
15397
+ '\u3201': '\u0028\u1102\u0029',
15398
+ '\u3202': '\u0028\u1103\u0029',
15399
+ '\u3203': '\u0028\u1105\u0029',
15400
+ '\u3204': '\u0028\u1106\u0029',
15401
+ '\u3205': '\u0028\u1107\u0029',
15402
+ '\u3206': '\u0028\u1109\u0029',
15403
+ '\u3207': '\u0028\u110B\u0029',
15404
+ '\u3208': '\u0028\u110C\u0029',
15405
+ '\u3209': '\u0028\u110E\u0029',
15406
+ '\u320A': '\u0028\u110F\u0029',
15407
+ '\u320B': '\u0028\u1110\u0029',
15408
+ '\u320C': '\u0028\u1111\u0029',
15409
+ '\u320D': '\u0028\u1112\u0029',
15410
+ '\u320E': '\u0028\u1100\u1161\u0029',
15411
+ '\u320F': '\u0028\u1102\u1161\u0029',
15412
+ '\u3210': '\u0028\u1103\u1161\u0029',
15413
+ '\u3211': '\u0028\u1105\u1161\u0029',
15414
+ '\u3212': '\u0028\u1106\u1161\u0029',
15415
+ '\u3213': '\u0028\u1107\u1161\u0029',
15416
+ '\u3214': '\u0028\u1109\u1161\u0029',
15417
+ '\u3215': '\u0028\u110B\u1161\u0029',
15418
+ '\u3216': '\u0028\u110C\u1161\u0029',
15419
+ '\u3217': '\u0028\u110E\u1161\u0029',
15420
+ '\u3218': '\u0028\u110F\u1161\u0029',
15421
+ '\u3219': '\u0028\u1110\u1161\u0029',
15422
+ '\u321A': '\u0028\u1111\u1161\u0029',
15423
+ '\u321B': '\u0028\u1112\u1161\u0029',
15424
+ '\u321C': '\u0028\u110C\u116E\u0029',
15425
+ '\u321D': '\u0028\u110B\u1169\u110C\u1165\u11AB\u0029',
15426
+ '\u321E': '\u0028\u110B\u1169\u1112\u116E\u0029',
15427
+ '\u3220': '\u0028\u4E00\u0029',
15428
+ '\u3221': '\u0028\u4E8C\u0029',
15429
+ '\u3222': '\u0028\u4E09\u0029',
15430
+ '\u3223': '\u0028\u56DB\u0029',
15431
+ '\u3224': '\u0028\u4E94\u0029',
15432
+ '\u3225': '\u0028\u516D\u0029',
15433
+ '\u3226': '\u0028\u4E03\u0029',
15434
+ '\u3227': '\u0028\u516B\u0029',
15435
+ '\u3228': '\u0028\u4E5D\u0029',
15436
+ '\u3229': '\u0028\u5341\u0029',
15437
+ '\u322A': '\u0028\u6708\u0029',
15438
+ '\u322B': '\u0028\u706B\u0029',
15439
+ '\u322C': '\u0028\u6C34\u0029',
15440
+ '\u322D': '\u0028\u6728\u0029',
15441
+ '\u322E': '\u0028\u91D1\u0029',
15442
+ '\u322F': '\u0028\u571F\u0029',
15443
+ '\u3230': '\u0028\u65E5\u0029',
15444
+ '\u3231': '\u0028\u682A\u0029',
15445
+ '\u3232': '\u0028\u6709\u0029',
15446
+ '\u3233': '\u0028\u793E\u0029',
15447
+ '\u3234': '\u0028\u540D\u0029',
15448
+ '\u3235': '\u0028\u7279\u0029',
15449
+ '\u3236': '\u0028\u8CA1\u0029',
15450
+ '\u3237': '\u0028\u795D\u0029',
15451
+ '\u3238': '\u0028\u52B4\u0029',
15452
+ '\u3239': '\u0028\u4EE3\u0029',
15453
+ '\u323A': '\u0028\u547C\u0029',
15454
+ '\u323B': '\u0028\u5B66\u0029',
15455
+ '\u323C': '\u0028\u76E3\u0029',
15456
+ '\u323D': '\u0028\u4F01\u0029',
15457
+ '\u323E': '\u0028\u8CC7\u0029',
15458
+ '\u323F': '\u0028\u5354\u0029',
15459
+ '\u3240': '\u0028\u796D\u0029',
15460
+ '\u3241': '\u0028\u4F11\u0029',
15461
+ '\u3242': '\u0028\u81EA\u0029',
15462
+ '\u3243': '\u0028\u81F3\u0029',
15463
+ '\u32C0': '\u0031\u6708',
15464
+ '\u32C1': '\u0032\u6708',
15465
+ '\u32C2': '\u0033\u6708',
15466
+ '\u32C3': '\u0034\u6708',
15467
+ '\u32C4': '\u0035\u6708',
15468
+ '\u32C5': '\u0036\u6708',
15469
+ '\u32C6': '\u0037\u6708',
15470
+ '\u32C7': '\u0038\u6708',
15471
+ '\u32C8': '\u0039\u6708',
15472
+ '\u32C9': '\u0031\u0030\u6708',
15473
+ '\u32CA': '\u0031\u0031\u6708',
15474
+ '\u32CB': '\u0031\u0032\u6708',
15475
+ '\u3358': '\u0030\u70B9',
15476
+ '\u3359': '\u0031\u70B9',
15477
+ '\u335A': '\u0032\u70B9',
15478
+ '\u335B': '\u0033\u70B9',
15479
+ '\u335C': '\u0034\u70B9',
15480
+ '\u335D': '\u0035\u70B9',
15481
+ '\u335E': '\u0036\u70B9',
15482
+ '\u335F': '\u0037\u70B9',
15483
+ '\u3360': '\u0038\u70B9',
15484
+ '\u3361': '\u0039\u70B9',
15485
+ '\u3362': '\u0031\u0030\u70B9',
15486
+ '\u3363': '\u0031\u0031\u70B9',
15487
+ '\u3364': '\u0031\u0032\u70B9',
15488
+ '\u3365': '\u0031\u0033\u70B9',
15489
+ '\u3366': '\u0031\u0034\u70B9',
15490
+ '\u3367': '\u0031\u0035\u70B9',
15491
+ '\u3368': '\u0031\u0036\u70B9',
15492
+ '\u3369': '\u0031\u0037\u70B9',
15493
+ '\u336A': '\u0031\u0038\u70B9',
15494
+ '\u336B': '\u0031\u0039\u70B9',
15495
+ '\u336C': '\u0032\u0030\u70B9',
15496
+ '\u336D': '\u0032\u0031\u70B9',
15497
+ '\u336E': '\u0032\u0032\u70B9',
15498
+ '\u336F': '\u0032\u0033\u70B9',
15499
+ '\u3370': '\u0032\u0034\u70B9',
15500
+ '\u33E0': '\u0031\u65E5',
15501
+ '\u33E1': '\u0032\u65E5',
15502
+ '\u33E2': '\u0033\u65E5',
15503
+ '\u33E3': '\u0034\u65E5',
15504
+ '\u33E4': '\u0035\u65E5',
15505
+ '\u33E5': '\u0036\u65E5',
15506
+ '\u33E6': '\u0037\u65E5',
15507
+ '\u33E7': '\u0038\u65E5',
15508
+ '\u33E8': '\u0039\u65E5',
15509
+ '\u33E9': '\u0031\u0030\u65E5',
15510
+ '\u33EA': '\u0031\u0031\u65E5',
15511
+ '\u33EB': '\u0031\u0032\u65E5',
15512
+ '\u33EC': '\u0031\u0033\u65E5',
15513
+ '\u33ED': '\u0031\u0034\u65E5',
15514
+ '\u33EE': '\u0031\u0035\u65E5',
15515
+ '\u33EF': '\u0031\u0036\u65E5',
15516
+ '\u33F0': '\u0031\u0037\u65E5',
15517
+ '\u33F1': '\u0031\u0038\u65E5',
15518
+ '\u33F2': '\u0031\u0039\u65E5',
15519
+ '\u33F3': '\u0032\u0030\u65E5',
15520
+ '\u33F4': '\u0032\u0031\u65E5',
15521
+ '\u33F5': '\u0032\u0032\u65E5',
15522
+ '\u33F6': '\u0032\u0033\u65E5',
15523
+ '\u33F7': '\u0032\u0034\u65E5',
15524
+ '\u33F8': '\u0032\u0035\u65E5',
15525
+ '\u33F9': '\u0032\u0036\u65E5',
15526
+ '\u33FA': '\u0032\u0037\u65E5',
15527
+ '\u33FB': '\u0032\u0038\u65E5',
15528
+ '\u33FC': '\u0032\u0039\u65E5',
15529
+ '\u33FD': '\u0033\u0030\u65E5',
15530
+ '\u33FE': '\u0033\u0031\u65E5',
15531
+ '\uFB00': '\u0066\u0066',
15532
+ '\uFB01': '\u0066\u0069',
15533
+ '\uFB02': '\u0066\u006C',
15534
+ '\uFB03': '\u0066\u0066\u0069',
15535
+ '\uFB04': '\u0066\u0066\u006C',
15536
+ '\uFB05': '\u017F\u0074',
15537
+ '\uFB06': '\u0073\u0074',
15538
+ '\uFB13': '\u0574\u0576',
15539
+ '\uFB14': '\u0574\u0565',
15540
+ '\uFB15': '\u0574\u056B',
15541
+ '\uFB16': '\u057E\u0576',
15542
+ '\uFB17': '\u0574\u056D',
15543
+ '\uFB4F': '\u05D0\u05DC',
15544
+ '\uFB50': '\u0671',
15545
+ '\uFB51': '\u0671',
15546
+ '\uFB52': '\u067B',
15547
+ '\uFB53': '\u067B',
15548
+ '\uFB54': '\u067B',
15549
+ '\uFB55': '\u067B',
15550
+ '\uFB56': '\u067E',
15551
+ '\uFB57': '\u067E',
15552
+ '\uFB58': '\u067E',
15553
+ '\uFB59': '\u067E',
15554
+ '\uFB5A': '\u0680',
15555
+ '\uFB5B': '\u0680',
15556
+ '\uFB5C': '\u0680',
15557
+ '\uFB5D': '\u0680',
15558
+ '\uFB5E': '\u067A',
15559
+ '\uFB5F': '\u067A',
15560
+ '\uFB60': '\u067A',
15561
+ '\uFB61': '\u067A',
15562
+ '\uFB62': '\u067F',
15563
+ '\uFB63': '\u067F',
15564
+ '\uFB64': '\u067F',
15565
+ '\uFB65': '\u067F',
15566
+ '\uFB66': '\u0679',
15567
+ '\uFB67': '\u0679',
15568
+ '\uFB68': '\u0679',
15569
+ '\uFB69': '\u0679',
15570
+ '\uFB6A': '\u06A4',
15571
+ '\uFB6B': '\u06A4',
15572
+ '\uFB6C': '\u06A4',
15573
+ '\uFB6D': '\u06A4',
15574
+ '\uFB6E': '\u06A6',
15575
+ '\uFB6F': '\u06A6',
15576
+ '\uFB70': '\u06A6',
15577
+ '\uFB71': '\u06A6',
15578
+ '\uFB72': '\u0684',
15579
+ '\uFB73': '\u0684',
15580
+ '\uFB74': '\u0684',
15581
+ '\uFB75': '\u0684',
15582
+ '\uFB76': '\u0683',
15583
+ '\uFB77': '\u0683',
15584
+ '\uFB78': '\u0683',
15585
+ '\uFB79': '\u0683',
15586
+ '\uFB7A': '\u0686',
15587
+ '\uFB7B': '\u0686',
15588
+ '\uFB7C': '\u0686',
15589
+ '\uFB7D': '\u0686',
15590
+ '\uFB7E': '\u0687',
15591
+ '\uFB7F': '\u0687',
15592
+ '\uFB80': '\u0687',
15593
+ '\uFB81': '\u0687',
15594
+ '\uFB82': '\u068D',
15595
+ '\uFB83': '\u068D',
15596
+ '\uFB84': '\u068C',
15597
+ '\uFB85': '\u068C',
15598
+ '\uFB86': '\u068E',
15599
+ '\uFB87': '\u068E',
15600
+ '\uFB88': '\u0688',
15601
+ '\uFB89': '\u0688',
15602
+ '\uFB8A': '\u0698',
15603
+ '\uFB8B': '\u0698',
15604
+ '\uFB8C': '\u0691',
15605
+ '\uFB8D': '\u0691',
15606
+ '\uFB8E': '\u06A9',
15607
+ '\uFB8F': '\u06A9',
15608
+ '\uFB90': '\u06A9',
15609
+ '\uFB91': '\u06A9',
15610
+ '\uFB92': '\u06AF',
15611
+ '\uFB93': '\u06AF',
15612
+ '\uFB94': '\u06AF',
15613
+ '\uFB95': '\u06AF',
15614
+ '\uFB96': '\u06B3',
15615
+ '\uFB97': '\u06B3',
15616
+ '\uFB98': '\u06B3',
15617
+ '\uFB99': '\u06B3',
15618
+ '\uFB9A': '\u06B1',
15619
+ '\uFB9B': '\u06B1',
15620
+ '\uFB9C': '\u06B1',
15621
+ '\uFB9D': '\u06B1',
15622
+ '\uFB9E': '\u06BA',
15623
+ '\uFB9F': '\u06BA',
15624
+ '\uFBA0': '\u06BB',
15625
+ '\uFBA1': '\u06BB',
15626
+ '\uFBA2': '\u06BB',
15627
+ '\uFBA3': '\u06BB',
15628
+ '\uFBA4': '\u06C0',
15629
+ '\uFBA5': '\u06C0',
15630
+ '\uFBA6': '\u06C1',
15631
+ '\uFBA7': '\u06C1',
15632
+ '\uFBA8': '\u06C1',
15633
+ '\uFBA9': '\u06C1',
15634
+ '\uFBAA': '\u06BE',
15635
+ '\uFBAB': '\u06BE',
15636
+ '\uFBAC': '\u06BE',
15637
+ '\uFBAD': '\u06BE',
15638
+ '\uFBAE': '\u06D2',
15639
+ '\uFBAF': '\u06D2',
15640
+ '\uFBB0': '\u06D3',
15641
+ '\uFBB1': '\u06D3',
15642
+ '\uFBD3': '\u06AD',
15643
+ '\uFBD4': '\u06AD',
15644
+ '\uFBD5': '\u06AD',
15645
+ '\uFBD6': '\u06AD',
15646
+ '\uFBD7': '\u06C7',
15647
+ '\uFBD8': '\u06C7',
15648
+ '\uFBD9': '\u06C6',
15649
+ '\uFBDA': '\u06C6',
15650
+ '\uFBDB': '\u06C8',
15651
+ '\uFBDC': '\u06C8',
15652
+ '\uFBDD': '\u0677',
15653
+ '\uFBDE': '\u06CB',
15654
+ '\uFBDF': '\u06CB',
15655
+ '\uFBE0': '\u06C5',
15656
+ '\uFBE1': '\u06C5',
15657
+ '\uFBE2': '\u06C9',
15658
+ '\uFBE3': '\u06C9',
15659
+ '\uFBE4': '\u06D0',
15660
+ '\uFBE5': '\u06D0',
15661
+ '\uFBE6': '\u06D0',
15662
+ '\uFBE7': '\u06D0',
15663
+ '\uFBE8': '\u0649',
15664
+ '\uFBE9': '\u0649',
15665
+ '\uFBEA': '\u0626\u0627',
15666
+ '\uFBEB': '\u0626\u0627',
15667
+ '\uFBEC': '\u0626\u06D5',
15668
+ '\uFBED': '\u0626\u06D5',
15669
+ '\uFBEE': '\u0626\u0648',
15670
+ '\uFBEF': '\u0626\u0648',
15671
+ '\uFBF0': '\u0626\u06C7',
15672
+ '\uFBF1': '\u0626\u06C7',
15673
+ '\uFBF2': '\u0626\u06C6',
15674
+ '\uFBF3': '\u0626\u06C6',
15675
+ '\uFBF4': '\u0626\u06C8',
15676
+ '\uFBF5': '\u0626\u06C8',
15677
+ '\uFBF6': '\u0626\u06D0',
15678
+ '\uFBF7': '\u0626\u06D0',
15679
+ '\uFBF8': '\u0626\u06D0',
15680
+ '\uFBF9': '\u0626\u0649',
15681
+ '\uFBFA': '\u0626\u0649',
15682
+ '\uFBFB': '\u0626\u0649',
15683
+ '\uFBFC': '\u06CC',
15684
+ '\uFBFD': '\u06CC',
15685
+ '\uFBFE': '\u06CC',
15686
+ '\uFBFF': '\u06CC',
15687
+ '\uFC00': '\u0626\u062C',
15688
+ '\uFC01': '\u0626\u062D',
15689
+ '\uFC02': '\u0626\u0645',
15690
+ '\uFC03': '\u0626\u0649',
15691
+ '\uFC04': '\u0626\u064A',
15692
+ '\uFC05': '\u0628\u062C',
15693
+ '\uFC06': '\u0628\u062D',
15694
+ '\uFC07': '\u0628\u062E',
15695
+ '\uFC08': '\u0628\u0645',
15696
+ '\uFC09': '\u0628\u0649',
15697
+ '\uFC0A': '\u0628\u064A',
15698
+ '\uFC0B': '\u062A\u062C',
15699
+ '\uFC0C': '\u062A\u062D',
15700
+ '\uFC0D': '\u062A\u062E',
15701
+ '\uFC0E': '\u062A\u0645',
15702
+ '\uFC0F': '\u062A\u0649',
15703
+ '\uFC10': '\u062A\u064A',
15704
+ '\uFC11': '\u062B\u062C',
15705
+ '\uFC12': '\u062B\u0645',
15706
+ '\uFC13': '\u062B\u0649',
15707
+ '\uFC14': '\u062B\u064A',
15708
+ '\uFC15': '\u062C\u062D',
15709
+ '\uFC16': '\u062C\u0645',
15710
+ '\uFC17': '\u062D\u062C',
15711
+ '\uFC18': '\u062D\u0645',
15712
+ '\uFC19': '\u062E\u062C',
15713
+ '\uFC1A': '\u062E\u062D',
15714
+ '\uFC1B': '\u062E\u0645',
15715
+ '\uFC1C': '\u0633\u062C',
15716
+ '\uFC1D': '\u0633\u062D',
15717
+ '\uFC1E': '\u0633\u062E',
15718
+ '\uFC1F': '\u0633\u0645',
15719
+ '\uFC20': '\u0635\u062D',
15720
+ '\uFC21': '\u0635\u0645',
15721
+ '\uFC22': '\u0636\u062C',
15722
+ '\uFC23': '\u0636\u062D',
15723
+ '\uFC24': '\u0636\u062E',
15724
+ '\uFC25': '\u0636\u0645',
15725
+ '\uFC26': '\u0637\u062D',
15726
+ '\uFC27': '\u0637\u0645',
15727
+ '\uFC28': '\u0638\u0645',
15728
+ '\uFC29': '\u0639\u062C',
15729
+ '\uFC2A': '\u0639\u0645',
15730
+ '\uFC2B': '\u063A\u062C',
15731
+ '\uFC2C': '\u063A\u0645',
15732
+ '\uFC2D': '\u0641\u062C',
15733
+ '\uFC2E': '\u0641\u062D',
15734
+ '\uFC2F': '\u0641\u062E',
15735
+ '\uFC30': '\u0641\u0645',
15736
+ '\uFC31': '\u0641\u0649',
15737
+ '\uFC32': '\u0641\u064A',
15738
+ '\uFC33': '\u0642\u062D',
15739
+ '\uFC34': '\u0642\u0645',
15740
+ '\uFC35': '\u0642\u0649',
15741
+ '\uFC36': '\u0642\u064A',
15742
+ '\uFC37': '\u0643\u0627',
15743
+ '\uFC38': '\u0643\u062C',
15744
+ '\uFC39': '\u0643\u062D',
15745
+ '\uFC3A': '\u0643\u062E',
15746
+ '\uFC3B': '\u0643\u0644',
15747
+ '\uFC3C': '\u0643\u0645',
15748
+ '\uFC3D': '\u0643\u0649',
15749
+ '\uFC3E': '\u0643\u064A',
15750
+ '\uFC3F': '\u0644\u062C',
15751
+ '\uFC40': '\u0644\u062D',
15752
+ '\uFC41': '\u0644\u062E',
15753
+ '\uFC42': '\u0644\u0645',
15754
+ '\uFC43': '\u0644\u0649',
15755
+ '\uFC44': '\u0644\u064A',
15756
+ '\uFC45': '\u0645\u062C',
15757
+ '\uFC46': '\u0645\u062D',
15758
+ '\uFC47': '\u0645\u062E',
15759
+ '\uFC48': '\u0645\u0645',
15760
+ '\uFC49': '\u0645\u0649',
15761
+ '\uFC4A': '\u0645\u064A',
15762
+ '\uFC4B': '\u0646\u062C',
15763
+ '\uFC4C': '\u0646\u062D',
15764
+ '\uFC4D': '\u0646\u062E',
15765
+ '\uFC4E': '\u0646\u0645',
15766
+ '\uFC4F': '\u0646\u0649',
15767
+ '\uFC50': '\u0646\u064A',
15768
+ '\uFC51': '\u0647\u062C',
15769
+ '\uFC52': '\u0647\u0645',
15770
+ '\uFC53': '\u0647\u0649',
15771
+ '\uFC54': '\u0647\u064A',
15772
+ '\uFC55': '\u064A\u062C',
15773
+ '\uFC56': '\u064A\u062D',
15774
+ '\uFC57': '\u064A\u062E',
15775
+ '\uFC58': '\u064A\u0645',
15776
+ '\uFC59': '\u064A\u0649',
15777
+ '\uFC5A': '\u064A\u064A',
15778
+ '\uFC5B': '\u0630\u0670',
15779
+ '\uFC5C': '\u0631\u0670',
15780
+ '\uFC5D': '\u0649\u0670',
15781
+ '\uFC5E': '\u0020\u064C\u0651',
15782
+ '\uFC5F': '\u0020\u064D\u0651',
15783
+ '\uFC60': '\u0020\u064E\u0651',
15784
+ '\uFC61': '\u0020\u064F\u0651',
15785
+ '\uFC62': '\u0020\u0650\u0651',
15786
+ '\uFC63': '\u0020\u0651\u0670',
15787
+ '\uFC64': '\u0626\u0631',
15788
+ '\uFC65': '\u0626\u0632',
15789
+ '\uFC66': '\u0626\u0645',
15790
+ '\uFC67': '\u0626\u0646',
15791
+ '\uFC68': '\u0626\u0649',
15792
+ '\uFC69': '\u0626\u064A',
15793
+ '\uFC6A': '\u0628\u0631',
15794
+ '\uFC6B': '\u0628\u0632',
15795
+ '\uFC6C': '\u0628\u0645',
15796
+ '\uFC6D': '\u0628\u0646',
15797
+ '\uFC6E': '\u0628\u0649',
15798
+ '\uFC6F': '\u0628\u064A',
15799
+ '\uFC70': '\u062A\u0631',
15800
+ '\uFC71': '\u062A\u0632',
15801
+ '\uFC72': '\u062A\u0645',
15802
+ '\uFC73': '\u062A\u0646',
15803
+ '\uFC74': '\u062A\u0649',
15804
+ '\uFC75': '\u062A\u064A',
15805
+ '\uFC76': '\u062B\u0631',
15806
+ '\uFC77': '\u062B\u0632',
15807
+ '\uFC78': '\u062B\u0645',
15808
+ '\uFC79': '\u062B\u0646',
15809
+ '\uFC7A': '\u062B\u0649',
15810
+ '\uFC7B': '\u062B\u064A',
15811
+ '\uFC7C': '\u0641\u0649',
15812
+ '\uFC7D': '\u0641\u064A',
15813
+ '\uFC7E': '\u0642\u0649',
15814
+ '\uFC7F': '\u0642\u064A',
15815
+ '\uFC80': '\u0643\u0627',
15816
+ '\uFC81': '\u0643\u0644',
15817
+ '\uFC82': '\u0643\u0645',
15818
+ '\uFC83': '\u0643\u0649',
15819
+ '\uFC84': '\u0643\u064A',
15820
+ '\uFC85': '\u0644\u0645',
15821
+ '\uFC86': '\u0644\u0649',
15822
+ '\uFC87': '\u0644\u064A',
15823
+ '\uFC88': '\u0645\u0627',
15824
+ '\uFC89': '\u0645\u0645',
15825
+ '\uFC8A': '\u0646\u0631',
15826
+ '\uFC8B': '\u0646\u0632',
15827
+ '\uFC8C': '\u0646\u0645',
15828
+ '\uFC8D': '\u0646\u0646',
15829
+ '\uFC8E': '\u0646\u0649',
15830
+ '\uFC8F': '\u0646\u064A',
15831
+ '\uFC90': '\u0649\u0670',
15832
+ '\uFC91': '\u064A\u0631',
15833
+ '\uFC92': '\u064A\u0632',
15834
+ '\uFC93': '\u064A\u0645',
15835
+ '\uFC94': '\u064A\u0646',
15836
+ '\uFC95': '\u064A\u0649',
15837
+ '\uFC96': '\u064A\u064A',
15838
+ '\uFC97': '\u0626\u062C',
15839
+ '\uFC98': '\u0626\u062D',
15840
+ '\uFC99': '\u0626\u062E',
15841
+ '\uFC9A': '\u0626\u0645',
15842
+ '\uFC9B': '\u0626\u0647',
15843
+ '\uFC9C': '\u0628\u062C',
15844
+ '\uFC9D': '\u0628\u062D',
15845
+ '\uFC9E': '\u0628\u062E',
15846
+ '\uFC9F': '\u0628\u0645',
15847
+ '\uFCA0': '\u0628\u0647',
15848
+ '\uFCA1': '\u062A\u062C',
15849
+ '\uFCA2': '\u062A\u062D',
15850
+ '\uFCA3': '\u062A\u062E',
15851
+ '\uFCA4': '\u062A\u0645',
15852
+ '\uFCA5': '\u062A\u0647',
15853
+ '\uFCA6': '\u062B\u0645',
15854
+ '\uFCA7': '\u062C\u062D',
15855
+ '\uFCA8': '\u062C\u0645',
15856
+ '\uFCA9': '\u062D\u062C',
15857
+ '\uFCAA': '\u062D\u0645',
15858
+ '\uFCAB': '\u062E\u062C',
15859
+ '\uFCAC': '\u062E\u0645',
15860
+ '\uFCAD': '\u0633\u062C',
15861
+ '\uFCAE': '\u0633\u062D',
15862
+ '\uFCAF': '\u0633\u062E',
15863
+ '\uFCB0': '\u0633\u0645',
15864
+ '\uFCB1': '\u0635\u062D',
15865
+ '\uFCB2': '\u0635\u062E',
15866
+ '\uFCB3': '\u0635\u0645',
15867
+ '\uFCB4': '\u0636\u062C',
15868
+ '\uFCB5': '\u0636\u062D',
15869
+ '\uFCB6': '\u0636\u062E',
15870
+ '\uFCB7': '\u0636\u0645',
15871
+ '\uFCB8': '\u0637\u062D',
15872
+ '\uFCB9': '\u0638\u0645',
15873
+ '\uFCBA': '\u0639\u062C',
15874
+ '\uFCBB': '\u0639\u0645',
15875
+ '\uFCBC': '\u063A\u062C',
15876
+ '\uFCBD': '\u063A\u0645',
15877
+ '\uFCBE': '\u0641\u062C',
15878
+ '\uFCBF': '\u0641\u062D',
15879
+ '\uFCC0': '\u0641\u062E',
15880
+ '\uFCC1': '\u0641\u0645',
15881
+ '\uFCC2': '\u0642\u062D',
15882
+ '\uFCC3': '\u0642\u0645',
15883
+ '\uFCC4': '\u0643\u062C',
15884
+ '\uFCC5': '\u0643\u062D',
15885
+ '\uFCC6': '\u0643\u062E',
15886
+ '\uFCC7': '\u0643\u0644',
15887
+ '\uFCC8': '\u0643\u0645',
15888
+ '\uFCC9': '\u0644\u062C',
15889
+ '\uFCCA': '\u0644\u062D',
15890
+ '\uFCCB': '\u0644\u062E',
15891
+ '\uFCCC': '\u0644\u0645',
15892
+ '\uFCCD': '\u0644\u0647',
15893
+ '\uFCCE': '\u0645\u062C',
15894
+ '\uFCCF': '\u0645\u062D',
15895
+ '\uFCD0': '\u0645\u062E',
15896
+ '\uFCD1': '\u0645\u0645',
15897
+ '\uFCD2': '\u0646\u062C',
15898
+ '\uFCD3': '\u0646\u062D',
15899
+ '\uFCD4': '\u0646\u062E',
15900
+ '\uFCD5': '\u0646\u0645',
15901
+ '\uFCD6': '\u0646\u0647',
15902
+ '\uFCD7': '\u0647\u062C',
15903
+ '\uFCD8': '\u0647\u0645',
15904
+ '\uFCD9': '\u0647\u0670',
15905
+ '\uFCDA': '\u064A\u062C',
15906
+ '\uFCDB': '\u064A\u062D',
15907
+ '\uFCDC': '\u064A\u062E',
15908
+ '\uFCDD': '\u064A\u0645',
15909
+ '\uFCDE': '\u064A\u0647',
15910
+ '\uFCDF': '\u0626\u0645',
15911
+ '\uFCE0': '\u0626\u0647',
15912
+ '\uFCE1': '\u0628\u0645',
15913
+ '\uFCE2': '\u0628\u0647',
15914
+ '\uFCE3': '\u062A\u0645',
15915
+ '\uFCE4': '\u062A\u0647',
15916
+ '\uFCE5': '\u062B\u0645',
15917
+ '\uFCE6': '\u062B\u0647',
15918
+ '\uFCE7': '\u0633\u0645',
15919
+ '\uFCE8': '\u0633\u0647',
15920
+ '\uFCE9': '\u0634\u0645',
15921
+ '\uFCEA': '\u0634\u0647',
15922
+ '\uFCEB': '\u0643\u0644',
15923
+ '\uFCEC': '\u0643\u0645',
15924
+ '\uFCED': '\u0644\u0645',
15925
+ '\uFCEE': '\u0646\u0645',
15926
+ '\uFCEF': '\u0646\u0647',
15927
+ '\uFCF0': '\u064A\u0645',
15928
+ '\uFCF1': '\u064A\u0647',
15929
+ '\uFCF2': '\u0640\u064E\u0651',
15930
+ '\uFCF3': '\u0640\u064F\u0651',
15931
+ '\uFCF4': '\u0640\u0650\u0651',
15932
+ '\uFCF5': '\u0637\u0649',
15933
+ '\uFCF6': '\u0637\u064A',
15934
+ '\uFCF7': '\u0639\u0649',
15935
+ '\uFCF8': '\u0639\u064A',
15936
+ '\uFCF9': '\u063A\u0649',
15937
+ '\uFCFA': '\u063A\u064A',
15938
+ '\uFCFB': '\u0633\u0649',
15939
+ '\uFCFC': '\u0633\u064A',
15940
+ '\uFCFD': '\u0634\u0649',
15941
+ '\uFCFE': '\u0634\u064A',
15942
+ '\uFCFF': '\u062D\u0649',
15943
+ '\uFD00': '\u062D\u064A',
15944
+ '\uFD01': '\u062C\u0649',
15945
+ '\uFD02': '\u062C\u064A',
15946
+ '\uFD03': '\u062E\u0649',
15947
+ '\uFD04': '\u062E\u064A',
15948
+ '\uFD05': '\u0635\u0649',
15949
+ '\uFD06': '\u0635\u064A',
15950
+ '\uFD07': '\u0636\u0649',
15951
+ '\uFD08': '\u0636\u064A',
15952
+ '\uFD09': '\u0634\u062C',
15953
+ '\uFD0A': '\u0634\u062D',
15954
+ '\uFD0B': '\u0634\u062E',
15955
+ '\uFD0C': '\u0634\u0645',
15956
+ '\uFD0D': '\u0634\u0631',
15957
+ '\uFD0E': '\u0633\u0631',
15958
+ '\uFD0F': '\u0635\u0631',
15959
+ '\uFD10': '\u0636\u0631',
15960
+ '\uFD11': '\u0637\u0649',
15961
+ '\uFD12': '\u0637\u064A',
15962
+ '\uFD13': '\u0639\u0649',
15963
+ '\uFD14': '\u0639\u064A',
15964
+ '\uFD15': '\u063A\u0649',
15965
+ '\uFD16': '\u063A\u064A',
15966
+ '\uFD17': '\u0633\u0649',
15967
+ '\uFD18': '\u0633\u064A',
15968
+ '\uFD19': '\u0634\u0649',
15969
+ '\uFD1A': '\u0634\u064A',
15970
+ '\uFD1B': '\u062D\u0649',
15971
+ '\uFD1C': '\u062D\u064A',
15972
+ '\uFD1D': '\u062C\u0649',
15973
+ '\uFD1E': '\u062C\u064A',
15974
+ '\uFD1F': '\u062E\u0649',
15975
+ '\uFD20': '\u062E\u064A',
15976
+ '\uFD21': '\u0635\u0649',
15977
+ '\uFD22': '\u0635\u064A',
15978
+ '\uFD23': '\u0636\u0649',
15979
+ '\uFD24': '\u0636\u064A',
15980
+ '\uFD25': '\u0634\u062C',
15981
+ '\uFD26': '\u0634\u062D',
15982
+ '\uFD27': '\u0634\u062E',
15983
+ '\uFD28': '\u0634\u0645',
15984
+ '\uFD29': '\u0634\u0631',
15985
+ '\uFD2A': '\u0633\u0631',
15986
+ '\uFD2B': '\u0635\u0631',
15987
+ '\uFD2C': '\u0636\u0631',
15988
+ '\uFD2D': '\u0634\u062C',
15989
+ '\uFD2E': '\u0634\u062D',
15990
+ '\uFD2F': '\u0634\u062E',
15991
+ '\uFD30': '\u0634\u0645',
15992
+ '\uFD31': '\u0633\u0647',
15993
+ '\uFD32': '\u0634\u0647',
15994
+ '\uFD33': '\u0637\u0645',
15995
+ '\uFD34': '\u0633\u062C',
15996
+ '\uFD35': '\u0633\u062D',
15997
+ '\uFD36': '\u0633\u062E',
15998
+ '\uFD37': '\u0634\u062C',
15999
+ '\uFD38': '\u0634\u062D',
16000
+ '\uFD39': '\u0634\u062E',
16001
+ '\uFD3A': '\u0637\u0645',
16002
+ '\uFD3B': '\u0638\u0645',
16003
+ '\uFD3C': '\u0627\u064B',
16004
+ '\uFD3D': '\u0627\u064B',
16005
+ '\uFD50': '\u062A\u062C\u0645',
16006
+ '\uFD51': '\u062A\u062D\u062C',
16007
+ '\uFD52': '\u062A\u062D\u062C',
16008
+ '\uFD53': '\u062A\u062D\u0645',
16009
+ '\uFD54': '\u062A\u062E\u0645',
16010
+ '\uFD55': '\u062A\u0645\u062C',
16011
+ '\uFD56': '\u062A\u0645\u062D',
16012
+ '\uFD57': '\u062A\u0645\u062E',
16013
+ '\uFD58': '\u062C\u0645\u062D',
16014
+ '\uFD59': '\u062C\u0645\u062D',
16015
+ '\uFD5A': '\u062D\u0645\u064A',
16016
+ '\uFD5B': '\u062D\u0645\u0649',
16017
+ '\uFD5C': '\u0633\u062D\u062C',
16018
+ '\uFD5D': '\u0633\u062C\u062D',
16019
+ '\uFD5E': '\u0633\u062C\u0649',
16020
+ '\uFD5F': '\u0633\u0645\u062D',
16021
+ '\uFD60': '\u0633\u0645\u062D',
16022
+ '\uFD61': '\u0633\u0645\u062C',
16023
+ '\uFD62': '\u0633\u0645\u0645',
16024
+ '\uFD63': '\u0633\u0645\u0645',
16025
+ '\uFD64': '\u0635\u062D\u062D',
16026
+ '\uFD65': '\u0635\u062D\u062D',
16027
+ '\uFD66': '\u0635\u0645\u0645',
16028
+ '\uFD67': '\u0634\u062D\u0645',
16029
+ '\uFD68': '\u0634\u062D\u0645',
16030
+ '\uFD69': '\u0634\u062C\u064A',
16031
+ '\uFD6A': '\u0634\u0645\u062E',
16032
+ '\uFD6B': '\u0634\u0645\u062E',
16033
+ '\uFD6C': '\u0634\u0645\u0645',
16034
+ '\uFD6D': '\u0634\u0645\u0645',
16035
+ '\uFD6E': '\u0636\u062D\u0649',
16036
+ '\uFD6F': '\u0636\u062E\u0645',
16037
+ '\uFD70': '\u0636\u062E\u0645',
16038
+ '\uFD71': '\u0637\u0645\u062D',
16039
+ '\uFD72': '\u0637\u0645\u062D',
16040
+ '\uFD73': '\u0637\u0645\u0645',
16041
+ '\uFD74': '\u0637\u0645\u064A',
16042
+ '\uFD75': '\u0639\u062C\u0645',
16043
+ '\uFD76': '\u0639\u0645\u0645',
16044
+ '\uFD77': '\u0639\u0645\u0645',
16045
+ '\uFD78': '\u0639\u0645\u0649',
16046
+ '\uFD79': '\u063A\u0645\u0645',
16047
+ '\uFD7A': '\u063A\u0645\u064A',
16048
+ '\uFD7B': '\u063A\u0645\u0649',
16049
+ '\uFD7C': '\u0641\u062E\u0645',
16050
+ '\uFD7D': '\u0641\u062E\u0645',
16051
+ '\uFD7E': '\u0642\u0645\u062D',
16052
+ '\uFD7F': '\u0642\u0645\u0645',
16053
+ '\uFD80': '\u0644\u062D\u0645',
16054
+ '\uFD81': '\u0644\u062D\u064A',
16055
+ '\uFD82': '\u0644\u062D\u0649',
16056
+ '\uFD83': '\u0644\u062C\u062C',
16057
+ '\uFD84': '\u0644\u062C\u062C',
16058
+ '\uFD85': '\u0644\u062E\u0645',
16059
+ '\uFD86': '\u0644\u062E\u0645',
16060
+ '\uFD87': '\u0644\u0645\u062D',
16061
+ '\uFD88': '\u0644\u0645\u062D',
16062
+ '\uFD89': '\u0645\u062D\u062C',
16063
+ '\uFD8A': '\u0645\u062D\u0645',
16064
+ '\uFD8B': '\u0645\u062D\u064A',
16065
+ '\uFD8C': '\u0645\u062C\u062D',
16066
+ '\uFD8D': '\u0645\u062C\u0645',
16067
+ '\uFD8E': '\u0645\u062E\u062C',
16068
+ '\uFD8F': '\u0645\u062E\u0645',
16069
+ '\uFD92': '\u0645\u062C\u062E',
16070
+ '\uFD93': '\u0647\u0645\u062C',
16071
+ '\uFD94': '\u0647\u0645\u0645',
16072
+ '\uFD95': '\u0646\u062D\u0645',
16073
+ '\uFD96': '\u0646\u062D\u0649',
16074
+ '\uFD97': '\u0646\u062C\u0645',
16075
+ '\uFD98': '\u0646\u062C\u0645',
16076
+ '\uFD99': '\u0646\u062C\u0649',
16077
+ '\uFD9A': '\u0646\u0645\u064A',
16078
+ '\uFD9B': '\u0646\u0645\u0649',
16079
+ '\uFD9C': '\u064A\u0645\u0645',
16080
+ '\uFD9D': '\u064A\u0645\u0645',
16081
+ '\uFD9E': '\u0628\u062E\u064A',
16082
+ '\uFD9F': '\u062A\u062C\u064A',
16083
+ '\uFDA0': '\u062A\u062C\u0649',
16084
+ '\uFDA1': '\u062A\u062E\u064A',
16085
+ '\uFDA2': '\u062A\u062E\u0649',
16086
+ '\uFDA3': '\u062A\u0645\u064A',
16087
+ '\uFDA4': '\u062A\u0645\u0649',
16088
+ '\uFDA5': '\u062C\u0645\u064A',
16089
+ '\uFDA6': '\u062C\u062D\u0649',
16090
+ '\uFDA7': '\u062C\u0645\u0649',
16091
+ '\uFDA8': '\u0633\u062E\u0649',
16092
+ '\uFDA9': '\u0635\u062D\u064A',
16093
+ '\uFDAA': '\u0634\u062D\u064A',
16094
+ '\uFDAB': '\u0636\u062D\u064A',
16095
+ '\uFDAC': '\u0644\u062C\u064A',
16096
+ '\uFDAD': '\u0644\u0645\u064A',
16097
+ '\uFDAE': '\u064A\u062D\u064A',
16098
+ '\uFDAF': '\u064A\u062C\u064A',
16099
+ '\uFDB0': '\u064A\u0645\u064A',
16100
+ '\uFDB1': '\u0645\u0645\u064A',
16101
+ '\uFDB2': '\u0642\u0645\u064A',
16102
+ '\uFDB3': '\u0646\u062D\u064A',
16103
+ '\uFDB4': '\u0642\u0645\u062D',
16104
+ '\uFDB5': '\u0644\u062D\u0645',
16105
+ '\uFDB6': '\u0639\u0645\u064A',
16106
+ '\uFDB7': '\u0643\u0645\u064A',
16107
+ '\uFDB8': '\u0646\u062C\u062D',
16108
+ '\uFDB9': '\u0645\u062E\u064A',
16109
+ '\uFDBA': '\u0644\u062C\u0645',
16110
+ '\uFDBB': '\u0643\u0645\u0645',
16111
+ '\uFDBC': '\u0644\u062C\u0645',
16112
+ '\uFDBD': '\u0646\u062C\u062D',
16113
+ '\uFDBE': '\u062C\u062D\u064A',
16114
+ '\uFDBF': '\u062D\u062C\u064A',
16115
+ '\uFDC0': '\u0645\u062C\u064A',
16116
+ '\uFDC1': '\u0641\u0645\u064A',
16117
+ '\uFDC2': '\u0628\u062D\u064A',
16118
+ '\uFDC3': '\u0643\u0645\u0645',
16119
+ '\uFDC4': '\u0639\u062C\u0645',
16120
+ '\uFDC5': '\u0635\u0645\u0645',
16121
+ '\uFDC6': '\u0633\u062E\u064A',
16122
+ '\uFDC7': '\u0646\u062C\u064A',
16123
+ '\uFE49': '\u203E',
16124
+ '\uFE4A': '\u203E',
16125
+ '\uFE4B': '\u203E',
16126
+ '\uFE4C': '\u203E',
16127
+ '\uFE4D': '\u005F',
16128
+ '\uFE4E': '\u005F',
16129
+ '\uFE4F': '\u005F',
16130
+ '\uFE80': '\u0621',
16131
+ '\uFE81': '\u0622',
16132
+ '\uFE82': '\u0622',
16133
+ '\uFE83': '\u0623',
16134
+ '\uFE84': '\u0623',
16135
+ '\uFE85': '\u0624',
16136
+ '\uFE86': '\u0624',
16137
+ '\uFE87': '\u0625',
16138
+ '\uFE88': '\u0625',
16139
+ '\uFE89': '\u0626',
16140
+ '\uFE8A': '\u0626',
16141
+ '\uFE8B': '\u0626',
16142
+ '\uFE8C': '\u0626',
16143
+ '\uFE8D': '\u0627',
16144
+ '\uFE8E': '\u0627',
16145
+ '\uFE8F': '\u0628',
16146
+ '\uFE90': '\u0628',
16147
+ '\uFE91': '\u0628',
16148
+ '\uFE92': '\u0628',
16149
+ '\uFE93': '\u0629',
16150
+ '\uFE94': '\u0629',
16151
+ '\uFE95': '\u062A',
16152
+ '\uFE96': '\u062A',
16153
+ '\uFE97': '\u062A',
16154
+ '\uFE98': '\u062A',
16155
+ '\uFE99': '\u062B',
16156
+ '\uFE9A': '\u062B',
16157
+ '\uFE9B': '\u062B',
16158
+ '\uFE9C': '\u062B',
16159
+ '\uFE9D': '\u062C',
16160
+ '\uFE9E': '\u062C',
16161
+ '\uFE9F': '\u062C',
16162
+ '\uFEA0': '\u062C',
16163
+ '\uFEA1': '\u062D',
16164
+ '\uFEA2': '\u062D',
16165
+ '\uFEA3': '\u062D',
16166
+ '\uFEA4': '\u062D',
16167
+ '\uFEA5': '\u062E',
16168
+ '\uFEA6': '\u062E',
16169
+ '\uFEA7': '\u062E',
16170
+ '\uFEA8': '\u062E',
16171
+ '\uFEA9': '\u062F',
16172
+ '\uFEAA': '\u062F',
16173
+ '\uFEAB': '\u0630',
16174
+ '\uFEAC': '\u0630',
16175
+ '\uFEAD': '\u0631',
16176
+ '\uFEAE': '\u0631',
16177
+ '\uFEAF': '\u0632',
16178
+ '\uFEB0': '\u0632',
16179
+ '\uFEB1': '\u0633',
16180
+ '\uFEB2': '\u0633',
16181
+ '\uFEB3': '\u0633',
16182
+ '\uFEB4': '\u0633',
16183
+ '\uFEB5': '\u0634',
16184
+ '\uFEB6': '\u0634',
16185
+ '\uFEB7': '\u0634',
16186
+ '\uFEB8': '\u0634',
16187
+ '\uFEB9': '\u0635',
16188
+ '\uFEBA': '\u0635',
16189
+ '\uFEBB': '\u0635',
16190
+ '\uFEBC': '\u0635',
16191
+ '\uFEBD': '\u0636',
16192
+ '\uFEBE': '\u0636',
16193
+ '\uFEBF': '\u0636',
16194
+ '\uFEC0': '\u0636',
16195
+ '\uFEC1': '\u0637',
16196
+ '\uFEC2': '\u0637',
16197
+ '\uFEC3': '\u0637',
16198
+ '\uFEC4': '\u0637',
16199
+ '\uFEC5': '\u0638',
16200
+ '\uFEC6': '\u0638',
16201
+ '\uFEC7': '\u0638',
16202
+ '\uFEC8': '\u0638',
16203
+ '\uFEC9': '\u0639',
16204
+ '\uFECA': '\u0639',
16205
+ '\uFECB': '\u0639',
16206
+ '\uFECC': '\u0639',
16207
+ '\uFECD': '\u063A',
16208
+ '\uFECE': '\u063A',
16209
+ '\uFECF': '\u063A',
16210
+ '\uFED0': '\u063A',
16211
+ '\uFED1': '\u0641',
16212
+ '\uFED2': '\u0641',
16213
+ '\uFED3': '\u0641',
16214
+ '\uFED4': '\u0641',
16215
+ '\uFED5': '\u0642',
16216
+ '\uFED6': '\u0642',
16217
+ '\uFED7': '\u0642',
16218
+ '\uFED8': '\u0642',
16219
+ '\uFED9': '\u0643',
16220
+ '\uFEDA': '\u0643',
16221
+ '\uFEDB': '\u0643',
16222
+ '\uFEDC': '\u0643',
16223
+ '\uFEDD': '\u0644',
16224
+ '\uFEDE': '\u0644',
16225
+ '\uFEDF': '\u0644',
16226
+ '\uFEE0': '\u0644',
16227
+ '\uFEE1': '\u0645',
16228
+ '\uFEE2': '\u0645',
16229
+ '\uFEE3': '\u0645',
16230
+ '\uFEE4': '\u0645',
16231
+ '\uFEE5': '\u0646',
16232
+ '\uFEE6': '\u0646',
16233
+ '\uFEE7': '\u0646',
16234
+ '\uFEE8': '\u0646',
16235
+ '\uFEE9': '\u0647',
16236
+ '\uFEEA': '\u0647',
16237
+ '\uFEEB': '\u0647',
16238
+ '\uFEEC': '\u0647',
16239
+ '\uFEED': '\u0648',
16240
+ '\uFEEE': '\u0648',
16241
+ '\uFEEF': '\u0649',
16242
+ '\uFEF0': '\u0649',
16243
+ '\uFEF1': '\u064A',
16244
+ '\uFEF2': '\u064A',
16245
+ '\uFEF3': '\u064A',
16246
+ '\uFEF4': '\u064A',
16247
+ '\uFEF5': '\u0644\u0622',
16248
+ '\uFEF6': '\u0644\u0622',
16249
+ '\uFEF7': '\u0644\u0623',
16250
+ '\uFEF8': '\u0644\u0623',
16251
+ '\uFEF9': '\u0644\u0625',
16252
+ '\uFEFA': '\u0644\u0625',
16253
+ '\uFEFB': '\u0644\u0627',
16254
+ '\uFEFC': '\u0644\u0627'
16255
+ };
16256
+
16257
+ function reverseIfRtl(chars) {
16258
+ var charsLength = chars.length;
16259
+ //reverse an arabic ligature
16260
+ if (charsLength <= 1 || !isRTLRangeFor(chars.charCodeAt(0))) {
16261
+ return chars;
16262
+ }
16263
+ var s = '';
16264
+ for (var ii = charsLength - 1; ii >= 0; ii--) {
16265
+ s += chars[ii];
16266
+ }
16267
+ return s;
16268
+ }
16269
+
16270
+ function adjustWidths(properties) {
16271
+ if (properties.fontMatrix[0] === FONT_IDENTITY_MATRIX[0]) {
16272
+ return;
16273
+ }
16274
+ // adjusting width to fontMatrix scale
16275
+ var scale = 0.001 / properties.fontMatrix[0];
16276
+ var glyphsWidths = properties.widths;
16277
+ for (var glyph in glyphsWidths) {
16278
+ glyphsWidths[glyph] *= scale;
16279
+ }
16280
+ properties.defaultWidth *= scale;
16281
+ }
16282
+
16283
+ function getFontType(type, subtype) {
16284
+ switch (type) {
16285
+ case 'Type1':
16286
+ return subtype === 'Type1C' ? FontType.TYPE1C : FontType.TYPE1;
16287
+ case 'CIDFontType0':
16288
+ return subtype === 'CIDFontType0C' ? FontType.CIDFONTTYPE0C :
16289
+ FontType.CIDFONTTYPE0;
16290
+ case 'OpenType':
16291
+ return FontType.OPENTYPE;
16292
+ case 'TrueType':
16293
+ return FontType.TRUETYPE;
16294
+ case 'CIDFontType2':
16295
+ return FontType.CIDFONTTYPE2;
16296
+ case 'MMType1':
16297
+ return FontType.MMTYPE1;
16298
+ case 'Type0':
16299
+ return FontType.TYPE0;
16300
+ default:
16301
+ return FontType.UNKNOWN;
16302
+ }
16303
+ }
16304
+
16305
+ var Glyph = (function GlyphClosure() {
16306
+ function Glyph(fontChar, unicode, accent, width, vmetric, operatorListId) {
16307
+ this.fontChar = fontChar;
16308
+ this.unicode = unicode;
16309
+ this.accent = accent;
16310
+ this.width = width;
16311
+ this.vmetric = vmetric;
16312
+ this.operatorListId = operatorListId;
16313
+ }
16314
+
16315
+ Glyph.prototype.matchesForCache =
16316
+ function(fontChar, unicode, accent, width, vmetric, operatorListId) {
16317
+ return this.fontChar === fontChar &&
16318
+ this.unicode === unicode &&
16319
+ this.accent === accent &&
16320
+ this.width === width &&
16321
+ this.vmetric === vmetric &&
16322
+ this.operatorListId === operatorListId;
16323
+ };
16324
+
16325
+ return Glyph;
16326
+ })();
16327
+
16328
+ var ToUnicodeMap = (function ToUnicodeMapClosure() {
16329
+ function ToUnicodeMap(cmap) {
16330
+ // The elements of this._map can be integers or strings, depending on how
16331
+ // |cmap| was created.
16332
+ this._map = cmap;
16333
+ }
16334
+
16335
+ ToUnicodeMap.prototype = {
16336
+ get length() {
16337
+ return this._map.length;
16338
+ },
16339
+
16340
+ forEach: function(callback) {
16341
+ for (var charCode in this._map) {
16342
+ callback(charCode, this._map[charCode].charCodeAt(0));
16343
+ }
16344
+ },
16345
+
16346
+ has: function(i) {
16347
+ return this._map[i] !== undefined;
16348
+ },
16349
+
16350
+ get: function(i) {
16351
+ return this._map[i];
16352
+ },
16353
+
16354
+ charCodeOf: function(v) {
16355
+ return this._map.indexOf(v);
16356
+ }
16357
+ };
16358
+
16359
+ return ToUnicodeMap;
16360
+ })();
16361
+
16362
+ var IdentityToUnicodeMap = (function IdentityToUnicodeMapClosure() {
16363
+ function IdentityToUnicodeMap(firstChar, lastChar) {
16364
+ this.firstChar = firstChar;
16365
+ this.lastChar = lastChar;
16366
+ }
16367
+
16368
+ IdentityToUnicodeMap.prototype = {
16369
+ get length() {
16370
+ return (this.lastChar + 1) - this.firstChar;
16371
+ },
16372
+
16373
+ forEach: function (callback) {
16374
+ for (var i = this.firstChar, ii = this.lastChar; i <= ii; i++) {
16375
+ callback(i, i);
16376
+ }
16377
+ },
16378
+
16379
+ has: function (i) {
16380
+ return this.firstChar <= i && i <= this.lastChar;
16381
+ },
16382
+
16383
+ get: function (i) {
16384
+ if (this.firstChar <= i && i <= this.lastChar) {
16385
+ return String.fromCharCode(i);
16386
+ }
16387
+ return undefined;
16388
+ },
16389
+
16390
+ charCodeOf: function (v) {
16391
+ error('should not call .charCodeOf');
16392
+ }
16393
+ };
16394
+
16395
+ return IdentityToUnicodeMap;
16396
+ })();
16397
+
16398
+ var OpenTypeFileBuilder = (function OpenTypeFileBuilderClosure() {
16399
+ function writeInt16(dest, offset, num) {
16400
+ dest[offset] = (num >> 8) & 0xFF;
16401
+ dest[offset + 1] = num & 0xFF;
16402
+ }
16403
+
16404
+ function writeInt32(dest, offset, num) {
16405
+ dest[offset] = (num >> 24) & 0xFF;
16406
+ dest[offset + 1] = (num >> 16) & 0xFF;
16407
+ dest[offset + 2] = (num >> 8) & 0xFF;
16408
+ dest[offset + 3] = num & 0xFF;
16409
+ }
16410
+
16411
+ function writeData(dest, offset, data) {
16412
+ var i, ii;
16413
+ if (data instanceof Uint8Array) {
16414
+ dest.set(data, offset);
16415
+ } else if (typeof data === 'string') {
16416
+ for (i = 0, ii = data.length; i < ii; i++) {
16417
+ dest[offset++] = data.charCodeAt(i) & 0xFF;
16418
+ }
16419
+ } else {
16420
+ // treating everything else as array
16421
+ for (i = 0, ii = data.length; i < ii; i++) {
16422
+ dest[offset++] = data[i] & 0xFF;
16423
+ }
16424
+ }
16425
+ }
16426
+
16427
+ function OpenTypeFileBuilder(sfnt) {
16428
+ this.sfnt = sfnt;
16429
+ this.tables = Object.create(null);
16430
+ }
16431
+
16432
+ OpenTypeFileBuilder.getSearchParams =
16433
+ function OpenTypeFileBuilder_getSearchParams(entriesCount, entrySize) {
16434
+ var maxPower2 = 1, log2 = 0;
16435
+ while ((maxPower2 ^ entriesCount) > maxPower2) {
16436
+ maxPower2 <<= 1;
16437
+ log2++;
16438
+ }
16439
+ var searchRange = maxPower2 * entrySize;
16440
+ return {
16441
+ range: searchRange,
16442
+ entry: log2,
16443
+ rangeShift: entrySize * entriesCount - searchRange
16444
+ };
16445
+ };
16446
+
16447
+ var OTF_HEADER_SIZE = 12;
16448
+ var OTF_TABLE_ENTRY_SIZE = 16;
16449
+
16450
+ OpenTypeFileBuilder.prototype = {
16451
+ toArray: function OpenTypeFileBuilder_toArray() {
16452
+ var sfnt = this.sfnt;
16453
+
16454
+ // Tables needs to be written by ascendant alphabetic order
16455
+ var tables = this.tables;
16456
+ var tablesNames = Object.keys(tables);
16457
+ tablesNames.sort();
16458
+ var numTables = tablesNames.length;
16459
+
16460
+ var i, j, jj, table, tableName;
16461
+ // layout the tables data
16462
+ var offset = OTF_HEADER_SIZE + numTables * OTF_TABLE_ENTRY_SIZE;
16463
+ var tableOffsets = [offset];
16464
+ for (i = 0; i < numTables; i++) {
16465
+ table = tables[tablesNames[i]];
16466
+ var paddedLength = ((table.length + 3) & ~3) >>> 0;
16467
+ offset += paddedLength;
16468
+ tableOffsets.push(offset);
16469
+ }
16470
+
16471
+ var file = new Uint8Array(offset);
16472
+ // write the table data first (mostly for checksum)
16473
+ for (i = 0; i < numTables; i++) {
16474
+ table = tables[tablesNames[i]];
16475
+ writeData(file, tableOffsets[i], table);
16476
+ }
16477
+
16478
+ // sfnt version (4 bytes)
16479
+ if (sfnt === 'true') {
16480
+ // Windows hates the Mac TrueType sfnt version number
16481
+ sfnt = string32(0x00010000);
16482
+ }
16483
+ file[0] = sfnt.charCodeAt(0) & 0xFF;
16484
+ file[1] = sfnt.charCodeAt(1) & 0xFF;
16485
+ file[2] = sfnt.charCodeAt(2) & 0xFF;
16486
+ file[3] = sfnt.charCodeAt(3) & 0xFF;
16487
+
16488
+ // numTables (2 bytes)
16489
+ writeInt16(file, 4, numTables);
16490
+
16491
+ var searchParams = OpenTypeFileBuilder.getSearchParams(numTables, 16);
16492
+
16493
+ // searchRange (2 bytes)
16494
+ writeInt16(file, 6, searchParams.range);
16495
+ // entrySelector (2 bytes)
16496
+ writeInt16(file, 8, searchParams.entry);
16497
+ // rangeShift (2 bytes)
16498
+ writeInt16(file, 10, searchParams.rangeShift);
16499
+
16500
+ offset = OTF_HEADER_SIZE;
16501
+ // writing table entries
16502
+ for (i = 0; i < numTables; i++) {
16503
+ tableName = tablesNames[i];
16504
+ file[offset] = tableName.charCodeAt(0) & 0xFF;
16505
+ file[offset + 1] = tableName.charCodeAt(1) & 0xFF;
16506
+ file[offset + 2] = tableName.charCodeAt(2) & 0xFF;
16507
+ file[offset + 3] = tableName.charCodeAt(3) & 0xFF;
16508
+
16509
+ // checksum
16510
+ var checksum = 0;
16511
+ for (j = tableOffsets[i], jj = tableOffsets[i + 1]; j < jj; j += 4) {
16512
+ var quad = (file[j] << 24) + (file[j + 1] << 16) +
16513
+ (file[j + 2] << 8) + file[j + 3];
16514
+ checksum = (checksum + quad) | 0;
16515
+ }
16516
+ writeInt32(file, offset + 4, checksum);
16517
+
16518
+ // offset
16519
+ writeInt32(file, offset + 8, tableOffsets[i]);
16520
+ // length
16521
+ writeInt32(file, offset + 12, tables[tableName].length);
16522
+
16523
+ offset += OTF_TABLE_ENTRY_SIZE;
16524
+ }
16525
+ return file;
16526
+ },
16527
+
16528
+ addTable: function OpenTypeFileBuilder_addTable(tag, data) {
16529
+ if (tag in this.tables) {
16530
+ throw new Error('Table ' + tag + ' already exists');
16531
+ }
16532
+ this.tables[tag] = data;
16533
+ }
16534
+ };
16535
+
16536
+ return OpenTypeFileBuilder;
16537
+ })();
16538
+
16539
+ /**
16540
+ * 'Font' is the class the outside world should use, it encapsulate all the font
16541
+ * decoding logics whatever type it is (assuming the font type is supported).
16542
+ *
16543
+ * For example to read a Type1 font and to attach it to the document:
16544
+ * var type1Font = new Font("MyFontName", binaryFile, propertiesObject);
16545
+ * type1Font.bind();
16546
+ */
16547
+ var Font = (function FontClosure() {
16548
+ function Font(name, file, properties) {
16549
+ var charCode, glyphName, fontChar;
16550
+
16551
+ this.name = name;
16552
+ this.loadedName = properties.loadedName;
16553
+ this.isType3Font = properties.isType3Font;
16554
+ this.sizes = [];
16555
+
16556
+ this.glyphCache = {};
16557
+
16558
+ var names = name.split('+');
16559
+ names = names.length > 1 ? names[1] : names[0];
16560
+ names = names.split(/[-,_]/g)[0];
16561
+ this.isSerifFont = !!(properties.flags & FontFlags.Serif);
16562
+ this.isSymbolicFont = !!(properties.flags & FontFlags.Symbolic);
16563
+ this.isMonospace = !!(properties.flags & FontFlags.FixedPitch);
16564
+
16565
+ var type = properties.type;
16566
+ var subtype = properties.subtype;
16567
+ this.type = type;
16568
+
16569
+ this.fallbackName = (this.isMonospace ? 'monospace' :
16570
+ (this.isSerifFont ? 'serif' : 'sans-serif'));
16571
+
16572
+ this.differences = properties.differences;
16573
+ this.widths = properties.widths;
16574
+ this.defaultWidth = properties.defaultWidth;
16575
+ this.composite = properties.composite;
16576
+ this.wideChars = properties.wideChars;
16577
+ this.cMap = properties.cMap;
16578
+ this.ascent = properties.ascent / PDF_GLYPH_SPACE_UNITS;
16579
+ this.descent = properties.descent / PDF_GLYPH_SPACE_UNITS;
16580
+ this.fontMatrix = properties.fontMatrix;
16581
+
16582
+ this.toUnicode = properties.toUnicode = this.buildToUnicode(properties);
16583
+
16584
+ this.toFontChar = [];
16585
+
16586
+ if (properties.type === 'Type3') {
16587
+ for (charCode = 0; charCode < 256; charCode++) {
16588
+ this.toFontChar[charCode] = (this.differences[charCode] ||
16589
+ properties.defaultEncoding[charCode]);
16590
+ }
16591
+ this.fontType = FontType.TYPE3;
16592
+ return;
16593
+ }
16594
+
16595
+ this.cidEncoding = properties.cidEncoding;
16596
+ this.vertical = properties.vertical;
16597
+ if (this.vertical) {
16598
+ this.vmetrics = properties.vmetrics;
16599
+ this.defaultVMetrics = properties.defaultVMetrics;
16600
+ }
16601
+
16602
+ if (!file || file.isEmpty) {
16603
+ if (file) {
16604
+ // Some bad PDF generators will include empty font files,
16605
+ // attempting to recover by assuming that no file exists.
16606
+ warn('Font file is empty in "' + name + '" (' + this.loadedName + ')');
16607
+ }
16608
+
16609
+ this.missingFile = true;
16610
+ // The file data is not specified. Trying to fix the font name
16611
+ // to be used with the canvas.font.
16612
+ var fontName = name.replace(/[,_]/g, '-');
16613
+ var isStandardFont = !!stdFontMap[fontName] ||
16614
+ !!(nonStdFontMap[fontName] && stdFontMap[nonStdFontMap[fontName]]);
16615
+ fontName = stdFontMap[fontName] || nonStdFontMap[fontName] || fontName;
16616
+
16617
+ this.bold = (fontName.search(/bold/gi) !== -1);
16618
+ this.italic = ((fontName.search(/oblique/gi) !== -1) ||
16619
+ (fontName.search(/italic/gi) !== -1));
16620
+
16621
+ // Use 'name' instead of 'fontName' here because the original
16622
+ // name ArialBlack for example will be replaced by Helvetica.
16623
+ this.black = (name.search(/Black/g) !== -1);
16624
+
16625
+ // if at least one width is present, remeasure all chars when exists
16626
+ this.remeasure = Object.keys(this.widths).length > 0;
16627
+ if (isStandardFont && type === 'CIDFontType2' &&
16628
+ properties.cidEncoding.indexOf('Identity-') === 0) {
16629
+ // Standard fonts might be embedded as CID font without glyph mapping.
16630
+ // Building one based on GlyphMapForStandardFonts.
16631
+ var map = [];
16632
+ for (var code in GlyphMapForStandardFonts) {
16633
+ map[+code] = GlyphMapForStandardFonts[code];
16634
+ }
16635
+ var isIdentityUnicode = this.toUnicode instanceof IdentityToUnicodeMap;
16636
+ if (!isIdentityUnicode) {
16637
+ this.toUnicode.forEach(function(charCode, unicodeCharCode) {
16638
+ map[+charCode] = unicodeCharCode;
16639
+ });
16640
+ }
16641
+ this.toFontChar = map;
16642
+ this.toUnicode = new ToUnicodeMap(map);
16643
+ } else if (/Symbol/i.test(fontName)) {
16644
+ var symbols = Encodings.SymbolSetEncoding;
16645
+ for (charCode in symbols) {
16646
+ fontChar = GlyphsUnicode[symbols[charCode]];
16647
+ if (!fontChar) {
16648
+ continue;
16649
+ }
16650
+ this.toFontChar[charCode] = fontChar;
16651
+ }
16652
+ for (charCode in properties.differences) {
16653
+ fontChar = GlyphsUnicode[properties.differences[charCode]];
16654
+ if (!fontChar) {
16655
+ continue;
16656
+ }
16657
+ this.toFontChar[charCode] = fontChar;
16658
+ }
16659
+ } else if (/Dingbats/i.test(fontName)) {
16660
+ if (/Wingdings/i.test(name)) {
16661
+ warn('Wingdings font without embedded font file, ' +
16662
+ 'falling back to the ZapfDingbats encoding.');
16663
+ }
16664
+ var dingbats = Encodings.ZapfDingbatsEncoding;
16665
+ for (charCode in dingbats) {
16666
+ fontChar = DingbatsGlyphsUnicode[dingbats[charCode]];
16667
+ if (!fontChar) {
16668
+ continue;
16669
+ }
16670
+ this.toFontChar[charCode] = fontChar;
16671
+ }
16672
+ for (charCode in properties.differences) {
16673
+ fontChar = DingbatsGlyphsUnicode[properties.differences[charCode]];
16674
+ if (!fontChar) {
16675
+ continue;
16676
+ }
16677
+ this.toFontChar[charCode] = fontChar;
16678
+ }
16679
+ } else if (isStandardFont) {
16680
+ this.toFontChar = [];
16681
+ for (charCode in properties.defaultEncoding) {
16682
+ glyphName = (properties.differences[charCode] ||
16683
+ properties.defaultEncoding[charCode]);
16684
+ this.toFontChar[charCode] = GlyphsUnicode[glyphName];
16685
+ }
16686
+ } else {
16687
+ var unicodeCharCode, notCidFont = (type.indexOf('CIDFontType') === -1);
16688
+ this.toUnicode.forEach(function(charCode, unicodeCharCode) {
16689
+ if (notCidFont) {
16690
+ glyphName = (properties.differences[charCode] ||
16691
+ properties.defaultEncoding[charCode]);
16692
+ unicodeCharCode = (GlyphsUnicode[glyphName] || unicodeCharCode);
16693
+ }
16694
+ this.toFontChar[charCode] = unicodeCharCode;
16695
+ }.bind(this));
16696
+ }
16697
+ this.loadedName = fontName.split('-')[0];
16698
+ this.loading = false;
16699
+ this.fontType = getFontType(type, subtype);
16700
+ return;
16701
+ }
16702
+
16703
+ // Some fonts might use wrong font types for Type1C or CIDFontType0C
16704
+ if (subtype === 'Type1C' && (type !== 'Type1' && type !== 'MMType1')) {
16705
+ // Some TrueType fonts by mistake claim Type1C
16706
+ if (isTrueTypeFile(file)) {
16707
+ subtype = 'TrueType';
16708
+ } else {
16709
+ type = 'Type1';
16710
+ }
16711
+ }
16712
+ if (subtype === 'CIDFontType0C' && type !== 'CIDFontType0') {
16713
+ type = 'CIDFontType0';
16714
+ }
16715
+ if (subtype === 'OpenType') {
16716
+ type = 'OpenType';
16717
+ }
16718
+ // Some CIDFontType0C fonts by mistake claim CIDFontType0.
16719
+ if (type === 'CIDFontType0') {
16720
+ subtype = isType1File(file) ? 'CIDFontType0' : 'CIDFontType0C';
16721
+ }
16722
+
16723
+ var data;
16724
+ switch (type) {
16725
+ case 'MMType1':
16726
+ info('MMType1 font (' + name + '), falling back to Type1.');
16727
+ /* falls through */
16728
+ case 'Type1':
16729
+ case 'CIDFontType0':
16730
+ this.mimetype = 'font/opentype';
16731
+
16732
+ var cff = (subtype === 'Type1C' || subtype === 'CIDFontType0C') ?
16733
+ new CFFFont(file, properties) : new Type1Font(name, file, properties);
16734
+
16735
+ adjustWidths(properties);
16736
+
16737
+ // Wrap the CFF data inside an OTF font file
16738
+ data = this.convert(name, cff, properties);
16739
+ break;
16740
+
16741
+ case 'OpenType':
16742
+ case 'TrueType':
16743
+ case 'CIDFontType2':
16744
+ this.mimetype = 'font/opentype';
16745
+
16746
+ // Repair the TrueType file. It is can be damaged in the point of
16747
+ // view of the sanitizer
16748
+ data = this.checkAndRepair(name, file, properties);
16749
+ if (this.isOpenType) {
16750
+ type = 'OpenType';
16751
+ }
16752
+ break;
16753
+
16754
+ default:
16755
+ error('Font ' + type + ' is not supported');
16756
+ break;
16757
+ }
16758
+
16759
+ this.data = data;
16760
+ this.fontType = getFontType(type, subtype);
16761
+
16762
+ // Transfer some properties again that could change during font conversion
16763
+ this.fontMatrix = properties.fontMatrix;
16764
+ this.widths = properties.widths;
16765
+ this.defaultWidth = properties.defaultWidth;
16766
+ this.encoding = properties.baseEncoding;
16767
+ this.seacMap = properties.seacMap;
16768
+
16769
+ this.loading = true;
16770
+ }
16771
+
16772
+ Font.getFontID = (function () {
16773
+ var ID = 1;
16774
+ return function Font_getFontID() {
16775
+ return String(ID++);
16776
+ };
16777
+ })();
16778
+
16779
+ function int16(b0, b1) {
16780
+ return (b0 << 8) + b1;
16781
+ }
16782
+
16783
+ function int32(b0, b1, b2, b3) {
16784
+ return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
16785
+ }
16786
+
16787
+ function string16(value) {
16788
+ return String.fromCharCode((value >> 8) & 0xff, value & 0xff);
16789
+ }
16790
+
16791
+ function safeString16(value) {
16792
+ // clamp value to the 16-bit int range
16793
+ value = (value > 0x7FFF ? 0x7FFF : (value < -0x8000 ? -0x8000 : value));
16794
+ return String.fromCharCode((value >> 8) & 0xff, value & 0xff);
16795
+ }
16796
+
16797
+ function isTrueTypeFile(file) {
16798
+ var header = file.peekBytes(4);
16799
+ return readUint32(header, 0) === 0x00010000;
16800
+ }
16801
+
16802
+ function isType1File(file) {
16803
+ var header = file.peekBytes(2);
16804
+ // All Type1 font programs must begin with the comment '%!' (0x25 + 0x21).
16805
+ if (header[0] === 0x25 && header[1] === 0x21) {
16806
+ return true;
16807
+ }
16808
+ // ... obviously some fonts violate that part of the specification,
16809
+ // please refer to the comment in |Type1Font| below.
16810
+ if (header[0] === 0x80 && header[1] === 0x01) { // pfb file header.
16811
+ return true;
16812
+ }
16813
+ return false;
16814
+ }
16815
+
16816
+ /**
16817
+ * Helper function for |adjustMapping|.
16818
+ * @return {boolean}
16819
+ */
16820
+ function isProblematicUnicodeLocation(code) {
16821
+ if (code <= 0x1F) { // Control chars
16822
+ return true;
16823
+ }
16824
+ if (code >= 0x80 && code <= 0x9F) { // Control chars
16825
+ return true;
16826
+ }
16827
+ if ((code >= 0x2000 && code <= 0x200F) || // General punctuation chars
16828
+ (code >= 0x2028 && code <= 0x202F) ||
16829
+ (code >= 0x2060 && code <= 0x206F)) {
16830
+ return true;
16831
+ }
16832
+ if (code >= 0xFFF0 && code <= 0xFFFF) { // Specials Unicode block
16833
+ return true;
16834
+ }
16835
+ switch (code) {
16836
+ case 0x7F: // Control char
16837
+ case 0xA0: // Non breaking space
16838
+ case 0xAD: // Soft hyphen
16839
+ case 0x0E33: // Thai character SARA AM
16840
+ case 0x2011: // Non breaking hyphen
16841
+ case 0x205F: // Medium mathematical space
16842
+ case 0x25CC: // Dotted circle (combining mark)
16843
+ return true;
16844
+ }
16845
+ return false;
16846
+ }
16847
+
16848
+ /**
16849
+ * Rebuilds the char code to glyph ID map by trying to replace the char codes
16850
+ * with their unicode value. It also moves char codes that are in known
16851
+ * problematic locations.
16852
+ * @return {Object} Two properties:
16853
+ * 'toFontChar' - maps original char codes(the value that will be read
16854
+ * from commands such as show text) to the char codes that will be used in the
16855
+ * font that we build
16856
+ * 'charCodeToGlyphId' - maps the new font char codes to glyph ids
16857
+ */
16858
+ function adjustMapping(charCodeToGlyphId, properties) {
16859
+ var toUnicode = properties.toUnicode;
16860
+ var isSymbolic = !!(properties.flags & FontFlags.Symbolic);
16861
+ var isIdentityUnicode =
16862
+ properties.toUnicode instanceof IdentityToUnicodeMap;
16863
+ var newMap = Object.create(null);
16864
+ var toFontChar = [];
16865
+ var usedFontCharCodes = [];
16866
+ var nextAvailableFontCharCode = PRIVATE_USE_OFFSET_START;
16867
+ for (var originalCharCode in charCodeToGlyphId) {
16868
+ originalCharCode |= 0;
16869
+ var glyphId = charCodeToGlyphId[originalCharCode];
16870
+ var fontCharCode = originalCharCode;
16871
+ // First try to map the value to a unicode position if a non identity map
16872
+ // was created.
16873
+ if (!isIdentityUnicode && toUnicode.has(originalCharCode)) {
16874
+ var unicode = toUnicode.get(fontCharCode);
16875
+ // TODO: Try to map ligatures to the correct spot.
16876
+ if (unicode.length === 1) {
16877
+ fontCharCode = unicode.charCodeAt(0);
16878
+ }
16879
+ }
16880
+ // Try to move control characters, special characters and already mapped
16881
+ // characters to the private use area since they will not be drawn by
16882
+ // canvas if left in their current position. Also, move characters if the
16883
+ // font was symbolic and there is only an identity unicode map since the
16884
+ // characters probably aren't in the correct position (fixes an issue
16885
+ // with firefox and thuluthfont).
16886
+ if ((usedFontCharCodes[fontCharCode] !== undefined ||
16887
+ isProblematicUnicodeLocation(fontCharCode) ||
16888
+ (isSymbolic && isIdentityUnicode)) &&
16889
+ nextAvailableFontCharCode <= PRIVATE_USE_OFFSET_END) { // Room left.
16890
+ // Loop to try and find a free spot in the private use area.
16891
+ do {
16892
+ fontCharCode = nextAvailableFontCharCode++;
16893
+
16894
+ if (SKIP_PRIVATE_USE_RANGE_F000_TO_F01F && fontCharCode === 0xF000) {
16895
+ fontCharCode = 0xF020;
16896
+ nextAvailableFontCharCode = fontCharCode + 1;
16897
+ }
16898
+
16899
+ } while (usedFontCharCodes[fontCharCode] !== undefined &&
16900
+ nextAvailableFontCharCode <= PRIVATE_USE_OFFSET_END);
16901
+ }
16902
+
16903
+ newMap[fontCharCode] = glyphId;
16904
+ toFontChar[originalCharCode] = fontCharCode;
16905
+ usedFontCharCodes[fontCharCode] = true;
16906
+ }
16907
+ return {
16908
+ toFontChar: toFontChar,
16909
+ charCodeToGlyphId: newMap,
16910
+ nextAvailableFontCharCode: nextAvailableFontCharCode
16911
+ };
16912
+ }
16913
+
16914
+ function getRanges(glyphs) {
16915
+ // Array.sort() sorts by characters, not numerically, so convert to an
16916
+ // array of characters.
16917
+ var codes = [];
16918
+ for (var charCode in glyphs) {
16919
+ codes.push({ fontCharCode: charCode | 0, glyphId: glyphs[charCode] });
16920
+ }
16921
+ codes.sort(function fontGetRangesSort(a, b) {
16922
+ return a.fontCharCode - b.fontCharCode;
16923
+ });
16924
+
16925
+ // Split the sorted codes into ranges.
16926
+ var ranges = [];
16927
+ var length = codes.length;
16928
+ for (var n = 0; n < length; ) {
16929
+ var start = codes[n].fontCharCode;
16930
+ var codeIndices = [codes[n].glyphId];
16931
+ ++n;
16932
+ var end = start;
16933
+ while (n < length && end + 1 === codes[n].fontCharCode) {
16934
+ codeIndices.push(codes[n].glyphId);
16935
+ ++end;
16936
+ ++n;
16937
+ if (end === 0xFFFF) {
16938
+ break;
16939
+ }
16940
+ }
16941
+ ranges.push([start, end, codeIndices]);
16942
+ }
16943
+
16944
+ return ranges;
16945
+ }
16946
+
16947
+ function createCmapTable(glyphs) {
16948
+ var ranges = getRanges(glyphs);
16949
+ var numTables = ranges[ranges.length - 1][1] > 0xFFFF ? 2 : 1;
16950
+ var cmap = '\x00\x00' + // version
16951
+ string16(numTables) + // numTables
16952
+ '\x00\x03' + // platformID
16953
+ '\x00\x01' + // encodingID
16954
+ string32(4 + numTables * 8); // start of the table record
16955
+
16956
+ var i, ii, j, jj;
16957
+ for (i = ranges.length - 1; i >= 0; --i) {
16958
+ if (ranges[i][0] <= 0xFFFF) { break; }
16959
+ }
16960
+ var bmpLength = i + 1;
16961
+
16962
+ if (ranges[i][0] < 0xFFFF && ranges[i][1] === 0xFFFF) {
16963
+ ranges[i][1] = 0xFFFE;
16964
+ }
16965
+ var trailingRangesCount = ranges[i][1] < 0xFFFF ? 1 : 0;
16966
+ var segCount = bmpLength + trailingRangesCount;
16967
+ var searchParams = OpenTypeFileBuilder.getSearchParams(segCount, 2);
16968
+
16969
+ // Fill up the 4 parallel arrays describing the segments.
16970
+ var startCount = '';
16971
+ var endCount = '';
16972
+ var idDeltas = '';
16973
+ var idRangeOffsets = '';
16974
+ var glyphsIds = '';
16975
+ var bias = 0;
16976
+
16977
+ var range, start, end, codes;
16978
+ for (i = 0, ii = bmpLength; i < ii; i++) {
16979
+ range = ranges[i];
16980
+ start = range[0];
16981
+ end = range[1];
16982
+ startCount += string16(start);
16983
+ endCount += string16(end);
16984
+ codes = range[2];
16985
+ var contiguous = true;
16986
+ for (j = 1, jj = codes.length; j < jj; ++j) {
16987
+ if (codes[j] !== codes[j - 1] + 1) {
16988
+ contiguous = false;
16989
+ break;
16990
+ }
16991
+ }
16992
+ if (!contiguous) {
16993
+ var offset = (segCount - i) * 2 + bias * 2;
16994
+ bias += (end - start + 1);
16995
+
16996
+ idDeltas += string16(0);
16997
+ idRangeOffsets += string16(offset);
16998
+
16999
+ for (j = 0, jj = codes.length; j < jj; ++j) {
17000
+ glyphsIds += string16(codes[j]);
17001
+ }
17002
+ } else {
17003
+ var startCode = codes[0];
17004
+
17005
+ idDeltas += string16((startCode - start) & 0xFFFF);
17006
+ idRangeOffsets += string16(0);
17007
+ }
17008
+ }
17009
+
17010
+ if (trailingRangesCount > 0) {
17011
+ endCount += '\xFF\xFF';
17012
+ startCount += '\xFF\xFF';
17013
+ idDeltas += '\x00\x01';
17014
+ idRangeOffsets += '\x00\x00';
17015
+ }
17016
+
17017
+ var format314 = '\x00\x00' + // language
17018
+ string16(2 * segCount) +
17019
+ string16(searchParams.range) +
17020
+ string16(searchParams.entry) +
17021
+ string16(searchParams.rangeShift) +
17022
+ endCount + '\x00\x00' + startCount +
17023
+ idDeltas + idRangeOffsets + glyphsIds;
17024
+
17025
+ var format31012 = '';
17026
+ var header31012 = '';
17027
+ if (numTables > 1) {
17028
+ cmap += '\x00\x03' + // platformID
17029
+ '\x00\x0A' + // encodingID
17030
+ string32(4 + numTables * 8 +
17031
+ 4 + format314.length); // start of the table record
17032
+ format31012 = '';
17033
+ for (i = 0, ii = ranges.length; i < ii; i++) {
17034
+ range = ranges[i];
17035
+ start = range[0];
17036
+ codes = range[2];
17037
+ var code = codes[0];
17038
+ for (j = 1, jj = codes.length; j < jj; ++j) {
17039
+ if (codes[j] !== codes[j - 1] + 1) {
17040
+ end = range[0] + j - 1;
17041
+ format31012 += string32(start) + // startCharCode
17042
+ string32(end) + // endCharCode
17043
+ string32(code); // startGlyphID
17044
+ start = end + 1;
17045
+ code = codes[j];
17046
+ }
17047
+ }
17048
+ format31012 += string32(start) + // startCharCode
17049
+ string32(range[1]) + // endCharCode
17050
+ string32(code); // startGlyphID
17051
+ }
17052
+ header31012 = '\x00\x0C' + // format
17053
+ '\x00\x00' + // reserved
17054
+ string32(format31012.length + 16) + // length
17055
+ '\x00\x00\x00\x00' + // language
17056
+ string32(format31012.length / 12); // nGroups
17057
+ }
17058
+
17059
+ return cmap + '\x00\x04' + // format
17060
+ string16(format314.length + 4) + // length
17061
+ format314 + header31012 + format31012;
17062
+ }
17063
+
17064
+ function validateOS2Table(os2) {
17065
+ var stream = new Stream(os2.data);
17066
+ var version = stream.getUint16();
17067
+ // TODO verify all OS/2 tables fields, but currently we validate only those
17068
+ // that give us issues
17069
+ stream.getBytes(60); // skipping type, misc sizes, panose, unicode ranges
17070
+ var selection = stream.getUint16();
17071
+ if (version < 4 && (selection & 0x0300)) {
17072
+ return false;
17073
+ }
17074
+ var firstChar = stream.getUint16();
17075
+ var lastChar = stream.getUint16();
17076
+ if (firstChar > lastChar) {
17077
+ return false;
17078
+ }
17079
+ stream.getBytes(6); // skipping sTypoAscender/Descender/LineGap
17080
+ var usWinAscent = stream.getUint16();
17081
+ if (usWinAscent === 0) { // makes font unreadable by windows
17082
+ return false;
17083
+ }
17084
+
17085
+ // OS/2 appears to be valid, resetting some fields
17086
+ os2.data[8] = os2.data[9] = 0; // IE rejects fonts if fsType != 0
17087
+ return true;
17088
+ }
17089
+
17090
+ function createOS2Table(properties, charstrings, override) {
17091
+ override = override || {
17092
+ unitsPerEm: 0,
17093
+ yMax: 0,
17094
+ yMin: 0,
17095
+ ascent: 0,
17096
+ descent: 0
17097
+ };
17098
+
17099
+ var ulUnicodeRange1 = 0;
17100
+ var ulUnicodeRange2 = 0;
17101
+ var ulUnicodeRange3 = 0;
17102
+ var ulUnicodeRange4 = 0;
17103
+
17104
+ var firstCharIndex = null;
17105
+ var lastCharIndex = 0;
17106
+
17107
+ if (charstrings) {
17108
+ for (var code in charstrings) {
17109
+ code |= 0;
17110
+ if (firstCharIndex > code || !firstCharIndex) {
17111
+ firstCharIndex = code;
17112
+ }
17113
+ if (lastCharIndex < code) {
17114
+ lastCharIndex = code;
17115
+ }
17116
+
17117
+ var position = getUnicodeRangeFor(code);
17118
+ if (position < 32) {
17119
+ ulUnicodeRange1 |= 1 << position;
17120
+ } else if (position < 64) {
17121
+ ulUnicodeRange2 |= 1 << position - 32;
17122
+ } else if (position < 96) {
17123
+ ulUnicodeRange3 |= 1 << position - 64;
17124
+ } else if (position < 123) {
17125
+ ulUnicodeRange4 |= 1 << position - 96;
17126
+ } else {
17127
+ error('Unicode ranges Bits > 123 are reserved for internal usage');
17128
+ }
17129
+ }
17130
+ } else {
17131
+ // TODO
17132
+ firstCharIndex = 0;
17133
+ lastCharIndex = 255;
17134
+ }
17135
+
17136
+ var bbox = properties.bbox || [0, 0, 0, 0];
17137
+ var unitsPerEm = (override.unitsPerEm ||
17138
+ 1 / (properties.fontMatrix || FONT_IDENTITY_MATRIX)[0]);
17139
+
17140
+ // if the font units differ to the PDF glyph space units
17141
+ // then scale up the values
17142
+ var scale = (properties.ascentScaled ? 1.0 :
17143
+ unitsPerEm / PDF_GLYPH_SPACE_UNITS);
17144
+
17145
+ var typoAscent = (override.ascent ||
17146
+ Math.round(scale * (properties.ascent || bbox[3])));
17147
+ var typoDescent = (override.descent ||
17148
+ Math.round(scale * (properties.descent || bbox[1])));
17149
+ if (typoDescent > 0 && properties.descent > 0 && bbox[1] < 0) {
17150
+ typoDescent = -typoDescent; // fixing incorrect descent
17151
+ }
17152
+ var winAscent = override.yMax || typoAscent;
17153
+ var winDescent = -override.yMin || -typoDescent;
17154
+
17155
+ return '\x00\x03' + // version
17156
+ '\x02\x24' + // xAvgCharWidth
17157
+ '\x01\xF4' + // usWeightClass
17158
+ '\x00\x05' + // usWidthClass
17159
+ '\x00\x00' + // fstype (0 to let the font loads via font-face on IE)
17160
+ '\x02\x8A' + // ySubscriptXSize
17161
+ '\x02\xBB' + // ySubscriptYSize
17162
+ '\x00\x00' + // ySubscriptXOffset
17163
+ '\x00\x8C' + // ySubscriptYOffset
17164
+ '\x02\x8A' + // ySuperScriptXSize
17165
+ '\x02\xBB' + // ySuperScriptYSize
17166
+ '\x00\x00' + // ySuperScriptXOffset
17167
+ '\x01\xDF' + // ySuperScriptYOffset
17168
+ '\x00\x31' + // yStrikeOutSize
17169
+ '\x01\x02' + // yStrikeOutPosition
17170
+ '\x00\x00' + // sFamilyClass
17171
+ '\x00\x00\x06' +
17172
+ String.fromCharCode(properties.fixedPitch ? 0x09 : 0x00) +
17173
+ '\x00\x00\x00\x00\x00\x00' + // Panose
17174
+ string32(ulUnicodeRange1) + // ulUnicodeRange1 (Bits 0-31)
17175
+ string32(ulUnicodeRange2) + // ulUnicodeRange2 (Bits 32-63)
17176
+ string32(ulUnicodeRange3) + // ulUnicodeRange3 (Bits 64-95)
17177
+ string32(ulUnicodeRange4) + // ulUnicodeRange4 (Bits 96-127)
17178
+ '\x2A\x32\x31\x2A' + // achVendID
17179
+ string16(properties.italicAngle ? 1 : 0) + // fsSelection
17180
+ string16(firstCharIndex ||
17181
+ properties.firstChar) + // usFirstCharIndex
17182
+ string16(lastCharIndex || properties.lastChar) + // usLastCharIndex
17183
+ string16(typoAscent) + // sTypoAscender
17184
+ string16(typoDescent) + // sTypoDescender
17185
+ '\x00\x64' + // sTypoLineGap (7%-10% of the unitsPerEM value)
17186
+ string16(winAscent) + // usWinAscent
17187
+ string16(winDescent) + // usWinDescent
17188
+ '\x00\x00\x00\x00' + // ulCodePageRange1 (Bits 0-31)
17189
+ '\x00\x00\x00\x00' + // ulCodePageRange2 (Bits 32-63)
17190
+ string16(properties.xHeight) + // sxHeight
17191
+ string16(properties.capHeight) + // sCapHeight
17192
+ string16(0) + // usDefaultChar
17193
+ string16(firstCharIndex || properties.firstChar) + // usBreakChar
17194
+ '\x00\x03'; // usMaxContext
17195
+ }
17196
+
17197
+ function createPostTable(properties) {
17198
+ var angle = Math.floor(properties.italicAngle * (Math.pow(2, 16)));
17199
+ return ('\x00\x03\x00\x00' + // Version number
17200
+ string32(angle) + // italicAngle
17201
+ '\x00\x00' + // underlinePosition
17202
+ '\x00\x00' + // underlineThickness
17203
+ string32(properties.fixedPitch) + // isFixedPitch
17204
+ '\x00\x00\x00\x00' + // minMemType42
17205
+ '\x00\x00\x00\x00' + // maxMemType42
17206
+ '\x00\x00\x00\x00' + // minMemType1
17207
+ '\x00\x00\x00\x00'); // maxMemType1
17208
+ }
17209
+
17210
+ function createNameTable(name, proto) {
17211
+ if (!proto) {
17212
+ proto = [[], []]; // no strings and unicode strings
17213
+ }
17214
+
17215
+ var strings = [
17216
+ proto[0][0] || 'Original licence', // 0.Copyright
17217
+ proto[0][1] || name, // 1.Font family
17218
+ proto[0][2] || 'Unknown', // 2.Font subfamily (font weight)
17219
+ proto[0][3] || 'uniqueID', // 3.Unique ID
17220
+ proto[0][4] || name, // 4.Full font name
17221
+ proto[0][5] || 'Version 0.11', // 5.Version
17222
+ proto[0][6] || '', // 6.Postscript name
17223
+ proto[0][7] || 'Unknown', // 7.Trademark
17224
+ proto[0][8] || 'Unknown', // 8.Manufacturer
17225
+ proto[0][9] || 'Unknown' // 9.Designer
17226
+ ];
17227
+
17228
+ // Mac want 1-byte per character strings while Windows want
17229
+ // 2-bytes per character, so duplicate the names table
17230
+ var stringsUnicode = [];
17231
+ var i, ii, j, jj, str;
17232
+ for (i = 0, ii = strings.length; i < ii; i++) {
17233
+ str = proto[1][i] || strings[i];
17234
+
17235
+ var strBufUnicode = [];
17236
+ for (j = 0, jj = str.length; j < jj; j++) {
17237
+ strBufUnicode.push(string16(str.charCodeAt(j)));
17238
+ }
17239
+ stringsUnicode.push(strBufUnicode.join(''));
17240
+ }
17241
+
17242
+ var names = [strings, stringsUnicode];
17243
+ var platforms = ['\x00\x01', '\x00\x03'];
17244
+ var encodings = ['\x00\x00', '\x00\x01'];
17245
+ var languages = ['\x00\x00', '\x04\x09'];
17246
+
17247
+ var namesRecordCount = strings.length * platforms.length;
17248
+ var nameTable =
17249
+ '\x00\x00' + // format
17250
+ string16(namesRecordCount) + // Number of names Record
17251
+ string16(namesRecordCount * 12 + 6); // Storage
17252
+
17253
+ // Build the name records field
17254
+ var strOffset = 0;
17255
+ for (i = 0, ii = platforms.length; i < ii; i++) {
17256
+ var strs = names[i];
17257
+ for (j = 0, jj = strs.length; j < jj; j++) {
17258
+ str = strs[j];
17259
+ var nameRecord =
17260
+ platforms[i] + // platform ID
17261
+ encodings[i] + // encoding ID
17262
+ languages[i] + // language ID
17263
+ string16(j) + // name ID
17264
+ string16(str.length) +
17265
+ string16(strOffset);
17266
+ nameTable += nameRecord;
17267
+ strOffset += str.length;
17268
+ }
17269
+ }
17270
+
17271
+ nameTable += strings.join('') + stringsUnicode.join('');
17272
+ return nameTable;
17273
+ }
17274
+
17275
+ Font.prototype = {
17276
+ name: null,
17277
+ font: null,
17278
+ mimetype: null,
17279
+ encoding: null,
17280
+ get renderer() {
17281
+ var renderer = FontRendererFactory.create(this);
17282
+ return shadow(this, 'renderer', renderer);
17283
+ },
17284
+
17285
+ exportData: function Font_exportData() {
17286
+ var data = {};
17287
+ for (var i in this) {
17288
+ if (this.hasOwnProperty(i)) {
17289
+ data[i] = this[i];
17290
+ }
17291
+ }
17292
+ return data;
17293
+ },
17294
+
17295
+ checkAndRepair: function Font_checkAndRepair(name, font, properties) {
17296
+ function readTableEntry(file) {
17297
+ var tag = bytesToString(file.getBytes(4));
17298
+
17299
+ var checksum = file.getInt32();
17300
+ var offset = file.getInt32() >>> 0;
17301
+ var length = file.getInt32() >>> 0;
17302
+
17303
+ // Read the table associated data
17304
+ var previousPosition = file.pos;
17305
+ file.pos = file.start ? file.start : 0;
17306
+ file.skip(offset);
17307
+ var data = file.getBytes(length);
17308
+ file.pos = previousPosition;
17309
+
17310
+ if (tag === 'head') {
17311
+ // clearing checksum adjustment
17312
+ data[8] = data[9] = data[10] = data[11] = 0;
17313
+ data[17] |= 0x20; //Set font optimized for cleartype flag
17314
+ }
17315
+
17316
+ return {
17317
+ tag: tag,
17318
+ checksum: checksum,
17319
+ length: length,
17320
+ offset: offset,
17321
+ data: data
17322
+ };
17323
+ }
17324
+
17325
+ function readOpenTypeHeader(ttf) {
17326
+ return {
17327
+ version: bytesToString(ttf.getBytes(4)),
17328
+ numTables: ttf.getUint16(),
17329
+ searchRange: ttf.getUint16(),
17330
+ entrySelector: ttf.getUint16(),
17331
+ rangeShift: ttf.getUint16()
17332
+ };
17333
+ }
17334
+
17335
+ /**
17336
+ * Read the appropriate subtable from the cmap according to 9.6.6.4 from
17337
+ * PDF spec
17338
+ */
17339
+ function readCmapTable(cmap, font, isSymbolicFont) {
17340
+ var segment;
17341
+ var start = (font.start ? font.start : 0) + cmap.offset;
17342
+ font.pos = start;
17343
+
17344
+ var version = font.getUint16();
17345
+ var numTables = font.getUint16();
17346
+
17347
+ var potentialTable;
17348
+ var canBreak = false;
17349
+ // There's an order of preference in terms of which cmap subtable to
17350
+ // use:
17351
+ // - non-symbolic fonts the preference is a 3,1 table then a 1,0 table
17352
+ // - symbolic fonts the preference is a 3,0 table then a 1,0 table
17353
+ // The following takes advantage of the fact that the tables are sorted
17354
+ // to work.
17355
+ for (var i = 0; i < numTables; i++) {
17356
+ var platformId = font.getUint16();
17357
+ var encodingId = font.getUint16();
17358
+ var offset = font.getInt32() >>> 0;
17359
+ var useTable = false;
17360
+
17361
+ if (platformId === 0 && encodingId === 0) {
17362
+ useTable = true;
17363
+ // Continue the loop since there still may be a higher priority
17364
+ // table.
17365
+ } else if (platformId === 1 && encodingId === 0) {
17366
+ useTable = true;
17367
+ // Continue the loop since there still may be a higher priority
17368
+ // table.
17369
+ } else if (platformId === 3 && encodingId === 1 &&
17370
+ (!isSymbolicFont || !potentialTable)) {
17371
+ useTable = true;
17372
+ if (!isSymbolicFont) {
17373
+ canBreak = true;
17374
+ }
17375
+ } else if (isSymbolicFont && platformId === 3 && encodingId === 0) {
17376
+ useTable = true;
17377
+ canBreak = true;
17378
+ }
17379
+
17380
+ if (useTable) {
17381
+ potentialTable = {
17382
+ platformId: platformId,
17383
+ encodingId: encodingId,
17384
+ offset: offset
17385
+ };
17386
+ }
17387
+ if (canBreak) {
17388
+ break;
17389
+ }
17390
+ }
17391
+
17392
+ if (!potentialTable) {
17393
+ warn('Could not find a preferred cmap table.');
17394
+ return {
17395
+ platformId: -1,
17396
+ encodingId: -1,
17397
+ mappings: [],
17398
+ hasShortCmap: false
17399
+ };
17400
+ }
17401
+
17402
+ font.pos = start + potentialTable.offset;
17403
+ var format = font.getUint16();
17404
+ var length = font.getUint16();
17405
+ var language = font.getUint16();
17406
+
17407
+ var hasShortCmap = false;
17408
+ var mappings = [];
17409
+ var j, glyphId;
17410
+
17411
+ // TODO(mack): refactor this cmap subtable reading logic out
17412
+ if (format === 0) {
17413
+ for (j = 0; j < 256; j++) {
17414
+ var index = font.getByte();
17415
+ if (!index) {
17416
+ continue;
17417
+ }
17418
+ mappings.push({
17419
+ charCode: j,
17420
+ glyphId: index
17421
+ });
17422
+ }
17423
+ hasShortCmap = true;
17424
+ } else if (format === 4) {
17425
+ // re-creating the table in format 4 since the encoding
17426
+ // might be changed
17427
+ var segCount = (font.getUint16() >> 1);
17428
+ font.getBytes(6); // skipping range fields
17429
+ var segIndex, segments = [];
17430
+ for (segIndex = 0; segIndex < segCount; segIndex++) {
17431
+ segments.push({ end: font.getUint16() });
17432
+ }
17433
+ font.getUint16();
17434
+ for (segIndex = 0; segIndex < segCount; segIndex++) {
17435
+ segments[segIndex].start = font.getUint16();
17436
+ }
17437
+
17438
+ for (segIndex = 0; segIndex < segCount; segIndex++) {
17439
+ segments[segIndex].delta = font.getUint16();
17440
+ }
17441
+
17442
+ var offsetsCount = 0;
17443
+ for (segIndex = 0; segIndex < segCount; segIndex++) {
17444
+ segment = segments[segIndex];
17445
+ var rangeOffset = font.getUint16();
17446
+ if (!rangeOffset) {
17447
+ segment.offsetIndex = -1;
17448
+ continue;
17449
+ }
17450
+
17451
+ var offsetIndex = (rangeOffset >> 1) - (segCount - segIndex);
17452
+ segment.offsetIndex = offsetIndex;
17453
+ offsetsCount = Math.max(offsetsCount, offsetIndex +
17454
+ segment.end - segment.start + 1);
17455
+ }
17456
+
17457
+ var offsets = [];
17458
+ for (j = 0; j < offsetsCount; j++) {
17459
+ offsets.push(font.getUint16());
17460
+ }
17461
+
17462
+ for (segIndex = 0; segIndex < segCount; segIndex++) {
17463
+ segment = segments[segIndex];
17464
+ start = segment.start;
17465
+ var end = segment.end;
17466
+ var delta = segment.delta;
17467
+ offsetIndex = segment.offsetIndex;
17468
+
17469
+ for (j = start; j <= end; j++) {
17470
+ if (j === 0xFFFF) {
17471
+ continue;
17472
+ }
17473
+
17474
+ glyphId = (offsetIndex < 0 ?
17475
+ j : offsets[offsetIndex + j - start]);
17476
+ glyphId = (glyphId + delta) & 0xFFFF;
17477
+ if (glyphId === 0) {
17478
+ continue;
17479
+ }
17480
+ mappings.push({
17481
+ charCode: j,
17482
+ glyphId: glyphId
17483
+ });
17484
+ }
17485
+ }
17486
+ } else if (format === 6) {
17487
+ // Format 6 is a 2-bytes dense mapping, which means the font data
17488
+ // lives glue together even if they are pretty far in the unicode
17489
+ // table. (This looks weird, so I can have missed something), this
17490
+ // works on Linux but seems to fails on Mac so let's rewrite the
17491
+ // cmap table to a 3-1-4 style
17492
+ var firstCode = font.getUint16();
17493
+ var entryCount = font.getUint16();
17494
+
17495
+ for (j = 0; j < entryCount; j++) {
17496
+ glyphId = font.getUint16();
17497
+ var charCode = firstCode + j;
17498
+
17499
+ mappings.push({
17500
+ charCode: charCode,
17501
+ glyphId: glyphId
17502
+ });
17503
+ }
17504
+ } else {
17505
+ error('cmap table has unsupported format: ' + format);
17506
+ }
17507
+
17508
+ // removing duplicate entries
17509
+ mappings.sort(function (a, b) {
17510
+ return a.charCode - b.charCode;
17511
+ });
17512
+ for (i = 1; i < mappings.length; i++) {
17513
+ if (mappings[i - 1].charCode === mappings[i].charCode) {
17514
+ mappings.splice(i, 1);
17515
+ i--;
17516
+ }
17517
+ }
17518
+
17519
+ return {
17520
+ platformId: potentialTable.platformId,
17521
+ encodingId: potentialTable.encodingId,
17522
+ mappings: mappings,
17523
+ hasShortCmap: hasShortCmap
17524
+ };
17525
+ }
17526
+
17527
+ function sanitizeMetrics(font, header, metrics, numGlyphs) {
17528
+ if (!header) {
17529
+ if (metrics) {
17530
+ metrics.data = null;
17531
+ }
17532
+ return;
17533
+ }
17534
+
17535
+ font.pos = (font.start ? font.start : 0) + header.offset;
17536
+ font.pos += header.length - 2;
17537
+ var numOfMetrics = font.getUint16();
17538
+
17539
+ if (numOfMetrics > numGlyphs) {
17540
+ info('The numOfMetrics (' + numOfMetrics + ') should not be ' +
17541
+ 'greater than the numGlyphs (' + numGlyphs + ')');
17542
+ // Reduce numOfMetrics if it is greater than numGlyphs
17543
+ numOfMetrics = numGlyphs;
17544
+ header.data[34] = (numOfMetrics & 0xff00) >> 8;
17545
+ header.data[35] = numOfMetrics & 0x00ff;
17546
+ }
17547
+
17548
+ var numOfSidebearings = numGlyphs - numOfMetrics;
17549
+ var numMissing = numOfSidebearings -
17550
+ ((metrics.length - numOfMetrics * 4) >> 1);
17551
+
17552
+ if (numMissing > 0) {
17553
+ // For each missing glyph, we set both the width and lsb to 0 (zero).
17554
+ // Since we need to add two properties for each glyph, this explains
17555
+ // the use of |numMissing * 2| when initializing the typed array.
17556
+ var entries = new Uint8Array(metrics.length + numMissing * 2);
17557
+ entries.set(metrics.data);
17558
+ metrics.data = entries;
17559
+ }
17560
+ }
17561
+
17562
+ function sanitizeGlyph(source, sourceStart, sourceEnd, dest, destStart,
17563
+ hintsValid) {
17564
+ if (sourceEnd - sourceStart <= 12) {
17565
+ // glyph with data less than 12 is invalid one
17566
+ return 0;
17567
+ }
17568
+ var glyf = source.subarray(sourceStart, sourceEnd);
17569
+ var contoursCount = (glyf[0] << 8) | glyf[1];
17570
+ if (contoursCount & 0x8000) {
17571
+ // complex glyph, writing as is
17572
+ dest.set(glyf, destStart);
17573
+ return glyf.length;
17574
+ }
17575
+
17576
+ var i, j = 10, flagsCount = 0;
17577
+ for (i = 0; i < contoursCount; i++) {
17578
+ var endPoint = (glyf[j] << 8) | glyf[j + 1];
17579
+ flagsCount = endPoint + 1;
17580
+ j += 2;
17581
+ }
17582
+ // skipping instructions
17583
+ var instructionsStart = j;
17584
+ var instructionsLength = (glyf[j] << 8) | glyf[j + 1];
17585
+ j += 2 + instructionsLength;
17586
+ var instructionsEnd = j;
17587
+ // validating flags
17588
+ var coordinatesLength = 0;
17589
+ for (i = 0; i < flagsCount; i++) {
17590
+ var flag = glyf[j++];
17591
+ if (flag & 0xC0) {
17592
+ // reserved flags must be zero, cleaning up
17593
+ glyf[j - 1] = flag & 0x3F;
17594
+ }
17595
+ var xyLength = ((flag & 2) ? 1 : (flag & 16) ? 0 : 2) +
17596
+ ((flag & 4) ? 1 : (flag & 32) ? 0 : 2);
17597
+ coordinatesLength += xyLength;
17598
+ if (flag & 8) {
17599
+ var repeat = glyf[j++];
17600
+ i += repeat;
17601
+ coordinatesLength += repeat * xyLength;
17602
+ }
17603
+ }
17604
+ // glyph without coordinates will be rejected
17605
+ if (coordinatesLength === 0) {
17606
+ return 0;
17607
+ }
17608
+ var glyphDataLength = j + coordinatesLength;
17609
+ if (glyphDataLength > glyf.length) {
17610
+ // not enough data for coordinates
17611
+ return 0;
17612
+ }
17613
+ if (!hintsValid && instructionsLength > 0) {
17614
+ dest.set(glyf.subarray(0, instructionsStart), destStart);
17615
+ dest.set([0, 0], destStart + instructionsStart);
17616
+ dest.set(glyf.subarray(instructionsEnd, glyphDataLength),
17617
+ destStart + instructionsStart + 2);
17618
+ glyphDataLength -= instructionsLength;
17619
+ if (glyf.length - glyphDataLength > 3) {
17620
+ glyphDataLength = (glyphDataLength + 3) & ~3;
17621
+ }
17622
+ return glyphDataLength;
17623
+ }
17624
+ if (glyf.length - glyphDataLength > 3) {
17625
+ // truncating and aligning to 4 bytes the long glyph data
17626
+ glyphDataLength = (glyphDataLength + 3) & ~3;
17627
+ dest.set(glyf.subarray(0, glyphDataLength), destStart);
17628
+ return glyphDataLength;
17629
+ }
17630
+ // glyph data is fine
17631
+ dest.set(glyf, destStart);
17632
+ return glyf.length;
17633
+ }
17634
+
17635
+ function sanitizeHead(head, numGlyphs, locaLength) {
17636
+ var data = head.data;
17637
+
17638
+ // Validate version:
17639
+ // Should always be 0x00010000
17640
+ var version = int32(data[0], data[1], data[2], data[3]);
17641
+ if (version >> 16 !== 1) {
17642
+ info('Attempting to fix invalid version in head table: ' + version);
17643
+ data[0] = 0;
17644
+ data[1] = 1;
17645
+ data[2] = 0;
17646
+ data[3] = 0;
17647
+ }
17648
+
17649
+ var indexToLocFormat = int16(data[50], data[51]);
17650
+ if (indexToLocFormat < 0 || indexToLocFormat > 1) {
17651
+ info('Attempting to fix invalid indexToLocFormat in head table: ' +
17652
+ indexToLocFormat);
17653
+
17654
+ // The value of indexToLocFormat should be 0 if the loca table
17655
+ // consists of short offsets, and should be 1 if the loca table
17656
+ // consists of long offsets.
17657
+ //
17658
+ // The number of entries in the loca table should be numGlyphs + 1.
17659
+ //
17660
+ // Using this information, we can work backwards to deduce if the
17661
+ // size of each offset in the loca table, and thus figure out the
17662
+ // appropriate value for indexToLocFormat.
17663
+
17664
+ var numGlyphsPlusOne = numGlyphs + 1;
17665
+ if (locaLength === numGlyphsPlusOne << 1) {
17666
+ // 0x0000 indicates the loca table consists of short offsets
17667
+ data[50] = 0;
17668
+ data[51] = 0;
17669
+ } else if (locaLength === numGlyphsPlusOne << 2) {
17670
+ // 0x0001 indicates the loca table consists of long offsets
17671
+ data[50] = 0;
17672
+ data[51] = 1;
17673
+ } else {
17674
+ warn('Could not fix indexToLocFormat: ' + indexToLocFormat);
17675
+ }
17676
+ }
17677
+ }
17678
+
17679
+ function sanitizeGlyphLocations(loca, glyf, numGlyphs,
17680
+ isGlyphLocationsLong, hintsValid,
17681
+ dupFirstEntry) {
17682
+ var itemSize, itemDecode, itemEncode;
17683
+ if (isGlyphLocationsLong) {
17684
+ itemSize = 4;
17685
+ itemDecode = function fontItemDecodeLong(data, offset) {
17686
+ return (data[offset] << 24) | (data[offset + 1] << 16) |
17687
+ (data[offset + 2] << 8) | data[offset + 3];
17688
+ };
17689
+ itemEncode = function fontItemEncodeLong(data, offset, value) {
17690
+ data[offset] = (value >>> 24) & 0xFF;
17691
+ data[offset + 1] = (value >> 16) & 0xFF;
17692
+ data[offset + 2] = (value >> 8) & 0xFF;
17693
+ data[offset + 3] = value & 0xFF;
17694
+ };
17695
+ } else {
17696
+ itemSize = 2;
17697
+ itemDecode = function fontItemDecode(data, offset) {
17698
+ return (data[offset] << 9) | (data[offset + 1] << 1);
17699
+ };
17700
+ itemEncode = function fontItemEncode(data, offset, value) {
17701
+ data[offset] = (value >> 9) & 0xFF;
17702
+ data[offset + 1] = (value >> 1) & 0xFF;
17703
+ };
17704
+ }
17705
+ var locaData = loca.data;
17706
+ var locaDataSize = itemSize * (1 + numGlyphs);
17707
+ // is loca.data too short or long?
17708
+ if (locaData.length !== locaDataSize) {
17709
+ locaData = new Uint8Array(locaDataSize);
17710
+ locaData.set(loca.data.subarray(0, locaDataSize));
17711
+ loca.data = locaData;
17712
+ }
17713
+ // removing the invalid glyphs
17714
+ var oldGlyfData = glyf.data;
17715
+ var oldGlyfDataLength = oldGlyfData.length;
17716
+ var newGlyfData = new Uint8Array(oldGlyfDataLength);
17717
+ var startOffset = itemDecode(locaData, 0);
17718
+ var writeOffset = 0;
17719
+ var missingGlyphData = {};
17720
+ itemEncode(locaData, 0, writeOffset);
17721
+ var i, j;
17722
+ for (i = 0, j = itemSize; i < numGlyphs; i++, j += itemSize) {
17723
+ var endOffset = itemDecode(locaData, j);
17724
+ if (endOffset > oldGlyfDataLength &&
17725
+ ((oldGlyfDataLength + 3) & ~3) === endOffset) {
17726
+ // Aspose breaks fonts by aligning the glyphs to the qword, but not
17727
+ // the glyf table size, which makes last glyph out of range.
17728
+ endOffset = oldGlyfDataLength;
17729
+ }
17730
+ if (endOffset > oldGlyfDataLength) {
17731
+ // glyph end offset points outside glyf data, rejecting the glyph
17732
+ itemEncode(locaData, j, writeOffset);
17733
+ startOffset = endOffset;
17734
+ continue;
17735
+ }
17736
+
17737
+ if (startOffset === endOffset) {
17738
+ missingGlyphData[i] = true;
17739
+ }
17740
+
17741
+ var newLength = sanitizeGlyph(oldGlyfData, startOffset, endOffset,
17742
+ newGlyfData, writeOffset, hintsValid);
17743
+ writeOffset += newLength;
17744
+ itemEncode(locaData, j, writeOffset);
17745
+ startOffset = endOffset;
17746
+ }
17747
+
17748
+ if (writeOffset === 0) {
17749
+ // glyf table cannot be empty -- redoing the glyf and loca tables
17750
+ // to have single glyph with one point
17751
+ var simpleGlyph = new Uint8Array(
17752
+ [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 49, 0]);
17753
+ for (i = 0, j = itemSize; i < numGlyphs; i++, j += itemSize) {
17754
+ itemEncode(locaData, j, simpleGlyph.length);
17755
+ }
17756
+ glyf.data = simpleGlyph;
17757
+ return missingGlyphData;
17758
+ }
17759
+
17760
+ if (dupFirstEntry) {
17761
+ var firstEntryLength = itemDecode(locaData, itemSize);
17762
+ if (newGlyfData.length > firstEntryLength + writeOffset) {
17763
+ glyf.data = newGlyfData.subarray(0, firstEntryLength + writeOffset);
17764
+ } else {
17765
+ glyf.data = new Uint8Array(firstEntryLength + writeOffset);
17766
+ glyf.data.set(newGlyfData.subarray(0, writeOffset));
17767
+ }
17768
+ glyf.data.set(newGlyfData.subarray(0, firstEntryLength), writeOffset);
17769
+ itemEncode(loca.data, locaData.length - itemSize,
17770
+ writeOffset + firstEntryLength);
17771
+ } else {
17772
+ glyf.data = newGlyfData.subarray(0, writeOffset);
17773
+ }
17774
+ return missingGlyphData;
17775
+ }
17776
+
17777
+ function readPostScriptTable(post, properties, maxpNumGlyphs) {
17778
+ var start = (font.start ? font.start : 0) + post.offset;
17779
+ font.pos = start;
17780
+
17781
+ var length = post.length, end = start + length;
17782
+ var version = font.getInt32();
17783
+ // skip rest to the tables
17784
+ font.getBytes(28);
17785
+
17786
+ var glyphNames;
17787
+ var valid = true;
17788
+ var i;
17789
+
17790
+ switch (version) {
17791
+ case 0x00010000:
17792
+ glyphNames = MacStandardGlyphOrdering;
17793
+ break;
17794
+ case 0x00020000:
17795
+ var numGlyphs = font.getUint16();
17796
+ if (numGlyphs !== maxpNumGlyphs) {
17797
+ valid = false;
17798
+ break;
17799
+ }
17800
+ var glyphNameIndexes = [];
17801
+ for (i = 0; i < numGlyphs; ++i) {
17802
+ var index = font.getUint16();
17803
+ if (index >= 32768) {
17804
+ valid = false;
17805
+ break;
17806
+ }
17807
+ glyphNameIndexes.push(index);
17808
+ }
17809
+ if (!valid) {
17810
+ break;
17811
+ }
17812
+ var customNames = [];
17813
+ var strBuf = [];
17814
+ while (font.pos < end) {
17815
+ var stringLength = font.getByte();
17816
+ strBuf.length = stringLength;
17817
+ for (i = 0; i < stringLength; ++i) {
17818
+ strBuf[i] = String.fromCharCode(font.getByte());
17819
+ }
17820
+ customNames.push(strBuf.join(''));
17821
+ }
17822
+ glyphNames = [];
17823
+ for (i = 0; i < numGlyphs; ++i) {
17824
+ var j = glyphNameIndexes[i];
17825
+ if (j < 258) {
17826
+ glyphNames.push(MacStandardGlyphOrdering[j]);
17827
+ continue;
17828
+ }
17829
+ glyphNames.push(customNames[j - 258]);
17830
+ }
17831
+ break;
17832
+ case 0x00030000:
17833
+ break;
17834
+ default:
17835
+ warn('Unknown/unsupported post table version ' + version);
17836
+ valid = false;
17837
+ break;
17838
+ }
17839
+ properties.glyphNames = glyphNames;
17840
+ return valid;
17841
+ }
17842
+
17843
+ function readNameTable(nameTable) {
17844
+ var start = (font.start ? font.start : 0) + nameTable.offset;
17845
+ font.pos = start;
17846
+
17847
+ var names = [[], []];
17848
+ var length = nameTable.length, end = start + length;
17849
+ var format = font.getUint16();
17850
+ var FORMAT_0_HEADER_LENGTH = 6;
17851
+ if (format !== 0 || length < FORMAT_0_HEADER_LENGTH) {
17852
+ // unsupported name table format or table "too" small
17853
+ return names;
17854
+ }
17855
+ var numRecords = font.getUint16();
17856
+ var stringsStart = font.getUint16();
17857
+ var records = [];
17858
+ var NAME_RECORD_LENGTH = 12;
17859
+ var i, ii;
17860
+
17861
+ for (i = 0; i < numRecords &&
17862
+ font.pos + NAME_RECORD_LENGTH <= end; i++) {
17863
+ var r = {
17864
+ platform: font.getUint16(),
17865
+ encoding: font.getUint16(),
17866
+ language: font.getUint16(),
17867
+ name: font.getUint16(),
17868
+ length: font.getUint16(),
17869
+ offset: font.getUint16()
17870
+ };
17871
+ // using only Macintosh and Windows platform/encoding names
17872
+ if ((r.platform === 1 && r.encoding === 0 && r.language === 0) ||
17873
+ (r.platform === 3 && r.encoding === 1 && r.language === 0x409)) {
17874
+ records.push(r);
17875
+ }
17876
+ }
17877
+ for (i = 0, ii = records.length; i < ii; i++) {
17878
+ var record = records[i];
17879
+ var pos = start + stringsStart + record.offset;
17880
+ if (pos + record.length > end) {
17881
+ continue; // outside of name table, ignoring
17882
+ }
17883
+ font.pos = pos;
17884
+ var nameIndex = record.name;
17885
+ if (record.encoding) {
17886
+ // unicode
17887
+ var str = '';
17888
+ for (var j = 0, jj = record.length; j < jj; j += 2) {
17889
+ str += String.fromCharCode(font.getUint16());
17890
+ }
17891
+ names[1][nameIndex] = str;
17892
+ } else {
17893
+ names[0][nameIndex] = bytesToString(font.getBytes(record.length));
17894
+ }
17895
+ }
17896
+ return names;
17897
+ }
17898
+
17899
+ var TTOpsStackDeltas = [
17900
+ 0, 0, 0, 0, 0, 0, 0, 0, -2, -2, -2, -2, 0, 0, -2, -5,
17901
+ -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, -1, 0, -1, -1, -1, -1,
17902
+ 1, -1, -999, 0, 1, 0, -1, -2, 0, -1, -2, -1, -1, 0, -1, -1,
17903
+ 0, 0, -999, -999, -1, -1, -1, -1, -2, -999, -2, -2, -999, 0, -2, -2,
17904
+ 0, 0, -2, 0, -2, 0, 0, 0, -2, -1, -1, 1, 1, 0, 0, -1,
17905
+ -1, -1, -1, -1, -1, -1, 0, 0, -1, 0, -1, -1, 0, -999, -1, -1,
17906
+ -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17907
+ -2, -999, -999, -999, -999, -999, -1, -1, -2, -2, 0, 0, 0, 0, -1, -1,
17908
+ -999, -2, -2, 0, 0, -1, -2, -2, 0, 0, 0, -1, -1, -1, -2];
17909
+ // 0xC0-DF == -1 and 0xE0-FF == -2
17910
+
17911
+ function sanitizeTTProgram(table, ttContext) {
17912
+ var data = table.data;
17913
+ var i = 0, j, n, b, funcId, pc, lastEndf = 0, lastDeff = 0;
17914
+ var stack = [];
17915
+ var callstack = [];
17916
+ var functionsCalled = [];
17917
+ var tooComplexToFollowFunctions =
17918
+ ttContext.tooComplexToFollowFunctions;
17919
+ var inFDEF = false, ifLevel = 0, inELSE = 0;
17920
+ for (var ii = data.length; i < ii;) {
17921
+ var op = data[i++];
17922
+ // The TrueType instruction set docs can be found at
17923
+ // https://developer.apple.com/fonts/TTRefMan/RM05/Chap5.html
17924
+ if (op === 0x40) { // NPUSHB - pushes n bytes
17925
+ n = data[i++];
17926
+ if (inFDEF || inELSE) {
17927
+ i += n;
17928
+ } else {
17929
+ for (j = 0; j < n; j++) {
17930
+ stack.push(data[i++]);
17931
+ }
17932
+ }
17933
+ } else if (op === 0x41) { // NPUSHW - pushes n words
17934
+ n = data[i++];
17935
+ if (inFDEF || inELSE) {
17936
+ i += n * 2;
17937
+ } else {
17938
+ for (j = 0; j < n; j++) {
17939
+ b = data[i++];
17940
+ stack.push((b << 8) | data[i++]);
17941
+ }
17942
+ }
17943
+ } else if ((op & 0xF8) === 0xB0) { // PUSHB - pushes bytes
17944
+ n = op - 0xB0 + 1;
17945
+ if (inFDEF || inELSE) {
17946
+ i += n;
17947
+ } else {
17948
+ for (j = 0; j < n; j++) {
17949
+ stack.push(data[i++]);
17950
+ }
17951
+ }
17952
+ } else if ((op & 0xF8) === 0xB8) { // PUSHW - pushes words
17953
+ n = op - 0xB8 + 1;
17954
+ if (inFDEF || inELSE) {
17955
+ i += n * 2;
17956
+ } else {
17957
+ for (j = 0; j < n; j++) {
17958
+ b = data[i++];
17959
+ stack.push((b << 8) | data[i++]);
17960
+ }
17961
+ }
17962
+ } else if (op === 0x2B && !tooComplexToFollowFunctions) { // CALL
17963
+ if (!inFDEF && !inELSE) {
17964
+ // collecting inforamtion about which functions are used
17965
+ funcId = stack[stack.length - 1];
17966
+ ttContext.functionsUsed[funcId] = true;
17967
+ if (funcId in ttContext.functionsStackDeltas) {
17968
+ stack.length += ttContext.functionsStackDeltas[funcId];
17969
+ } else if (funcId in ttContext.functionsDefined &&
17970
+ functionsCalled.indexOf(funcId) < 0) {
17971
+ callstack.push({data: data, i: i, stackTop: stack.length - 1});
17972
+ functionsCalled.push(funcId);
17973
+ pc = ttContext.functionsDefined[funcId];
17974
+ if (!pc) {
17975
+ warn('TT: CALL non-existent function');
17976
+ ttContext.hintsValid = false;
17977
+ return;
17978
+ }
17979
+ data = pc.data;
17980
+ i = pc.i;
17981
+ }
17982
+ }
17983
+ } else if (op === 0x2C && !tooComplexToFollowFunctions) { // FDEF
17984
+ if (inFDEF || inELSE) {
17985
+ warn('TT: nested FDEFs not allowed');
17986
+ tooComplexToFollowFunctions = true;
17987
+ }
17988
+ inFDEF = true;
17989
+ // collecting inforamtion about which functions are defined
17990
+ lastDeff = i;
17991
+ funcId = stack.pop();
17992
+ ttContext.functionsDefined[funcId] = {data: data, i: i};
17993
+ } else if (op === 0x2D) { // ENDF - end of function
17994
+ if (inFDEF) {
17995
+ inFDEF = false;
17996
+ lastEndf = i;
17997
+ } else {
17998
+ pc = callstack.pop();
17999
+ if (!pc) {
18000
+ warn('TT: ENDF bad stack');
18001
+ ttContext.hintsValid = false;
18002
+ return;
18003
+ }
18004
+ funcId = functionsCalled.pop();
18005
+ data = pc.data;
18006
+ i = pc.i;
18007
+ ttContext.functionsStackDeltas[funcId] =
18008
+ stack.length - pc.stackTop;
18009
+ }
18010
+ } else if (op === 0x89) { // IDEF - instruction definition
18011
+ if (inFDEF || inELSE) {
18012
+ warn('TT: nested IDEFs not allowed');
18013
+ tooComplexToFollowFunctions = true;
18014
+ }
18015
+ inFDEF = true;
18016
+ // recording it as a function to track ENDF
18017
+ lastDeff = i;
18018
+ } else if (op === 0x58) { // IF
18019
+ ++ifLevel;
18020
+ } else if (op === 0x1B) { // ELSE
18021
+ inELSE = ifLevel;
18022
+ } else if (op === 0x59) { // EIF
18023
+ if (inELSE === ifLevel) {
18024
+ inELSE = 0;
18025
+ }
18026
+ --ifLevel;
18027
+ } else if (op === 0x1C) { // JMPR
18028
+ if (!inFDEF && !inELSE) {
18029
+ var offset = stack[stack.length - 1];
18030
+ // only jumping forward to prevent infinite loop
18031
+ if (offset > 0) {
18032
+ i += offset - 1;
18033
+ }
18034
+ }
18035
+ }
18036
+ // Adjusting stack not extactly, but just enough to get function id
18037
+ if (!inFDEF && !inELSE) {
18038
+ var stackDelta = op <= 0x8E ? TTOpsStackDeltas[op] :
18039
+ op >= 0xC0 && op <= 0xDF ? -1 : op >= 0xE0 ? -2 : 0;
18040
+ if (op >= 0x71 && op <= 0x75) {
18041
+ n = stack.pop();
18042
+ if (n === n) {
18043
+ stackDelta = -n * 2;
18044
+ }
18045
+ }
18046
+ while (stackDelta < 0 && stack.length > 0) {
18047
+ stack.pop();
18048
+ stackDelta++;
18049
+ }
18050
+ while (stackDelta > 0) {
18051
+ stack.push(NaN); // pushing any number into stack
18052
+ stackDelta--;
18053
+ }
18054
+ }
18055
+ }
18056
+ ttContext.tooComplexToFollowFunctions = tooComplexToFollowFunctions;
18057
+ var content = [data];
18058
+ if (i > data.length) {
18059
+ content.push(new Uint8Array(i - data.length));
18060
+ }
18061
+ if (lastDeff > lastEndf) {
18062
+ warn('TT: complementing a missing function tail');
18063
+ // new function definition started, but not finished
18064
+ // complete function by [CLEAR, ENDF]
18065
+ content.push(new Uint8Array([0x22, 0x2D]));
18066
+ }
18067
+ foldTTTable(table, content);
18068
+ }
18069
+
18070
+ function checkInvalidFunctions(ttContext, maxFunctionDefs) {
18071
+ if (ttContext.tooComplexToFollowFunctions) {
18072
+ return;
18073
+ }
18074
+ if (ttContext.functionsDefined.length > maxFunctionDefs) {
18075
+ warn('TT: more functions defined than expected');
18076
+ ttContext.hintsValid = false;
18077
+ return;
18078
+ }
18079
+ for (var j = 0, jj = ttContext.functionsUsed.length; j < jj; j++) {
18080
+ if (j > maxFunctionDefs) {
18081
+ warn('TT: invalid function id: ' + j);
18082
+ ttContext.hintsValid = false;
18083
+ return;
18084
+ }
18085
+ if (ttContext.functionsUsed[j] && !ttContext.functionsDefined[j]) {
18086
+ warn('TT: undefined function: ' + j);
18087
+ ttContext.hintsValid = false;
18088
+ return;
18089
+ }
18090
+ }
18091
+ }
18092
+
18093
+ function foldTTTable(table, content) {
18094
+ if (content.length > 1) {
18095
+ // concatenating the content items
18096
+ var newLength = 0;
18097
+ var j, jj;
18098
+ for (j = 0, jj = content.length; j < jj; j++) {
18099
+ newLength += content[j].length;
18100
+ }
18101
+ newLength = (newLength + 3) & ~3;
18102
+ var result = new Uint8Array(newLength);
18103
+ var pos = 0;
18104
+ for (j = 0, jj = content.length; j < jj; j++) {
18105
+ result.set(content[j], pos);
18106
+ pos += content[j].length;
18107
+ }
18108
+ table.data = result;
18109
+ table.length = newLength;
18110
+ }
18111
+ }
18112
+
18113
+ function sanitizeTTPrograms(fpgm, prep, cvt) {
18114
+ var ttContext = {
18115
+ functionsDefined: [],
18116
+ functionsUsed: [],
18117
+ functionsStackDeltas: [],
18118
+ tooComplexToFollowFunctions: false,
18119
+ hintsValid: true
18120
+ };
18121
+ if (fpgm) {
18122
+ sanitizeTTProgram(fpgm, ttContext);
18123
+ }
18124
+ if (prep) {
18125
+ sanitizeTTProgram(prep, ttContext);
18126
+ }
18127
+ if (fpgm) {
18128
+ checkInvalidFunctions(ttContext, maxFunctionDefs);
18129
+ }
18130
+ if (cvt && (cvt.length & 1)) {
18131
+ var cvtData = new Uint8Array(cvt.length + 1);
18132
+ cvtData.set(cvt.data);
18133
+ cvt.data = cvtData;
18134
+ }
18135
+ return ttContext.hintsValid;
18136
+ }
18137
+
18138
+ // The following steps modify the original font data, making copy
18139
+ font = new Stream(new Uint8Array(font.getBytes()));
18140
+
18141
+ var VALID_TABLES = ['OS/2', 'cmap', 'head', 'hhea', 'hmtx', 'maxp',
18142
+ 'name', 'post', 'loca', 'glyf', 'fpgm', 'prep', 'cvt ', 'CFF '];
18143
+
18144
+ var header = readOpenTypeHeader(font);
18145
+ var numTables = header.numTables;
18146
+ var cff, cffFile;
18147
+
18148
+ var tables = { 'OS/2': null, cmap: null, head: null, hhea: null,
18149
+ hmtx: null, maxp: null, name: null, post: null };
18150
+ var table;
18151
+ for (var i = 0; i < numTables; i++) {
18152
+ table = readTableEntry(font);
18153
+ if (VALID_TABLES.indexOf(table.tag) < 0) {
18154
+ continue; // skipping table if it's not a required or optional table
18155
+ }
18156
+ if (table.length === 0) {
18157
+ continue; // skipping empty tables
18158
+ }
18159
+ tables[table.tag] = table;
18160
+ }
18161
+
18162
+ var isTrueType = !tables['CFF '];
18163
+ if (!isTrueType) {
18164
+ // OpenType font
18165
+ if (header.version === 'OTTO' ||
18166
+ !tables.head || !tables.hhea || !tables.maxp || !tables.post) {
18167
+ // no major tables: throwing everything at CFFFont
18168
+ cffFile = new Stream(tables['CFF '].data);
18169
+ cff = new CFFFont(cffFile, properties);
18170
+
18171
+ return this.convert(name, cff, properties);
18172
+ }
18173
+
18174
+ delete tables.glyf;
18175
+ delete tables.loca;
18176
+ delete tables.fpgm;
18177
+ delete tables.prep;
18178
+ delete tables['cvt '];
18179
+ this.isOpenType = true;
18180
+ } else {
18181
+ if (!tables.glyf || !tables.loca) {
18182
+ error('Required "glyf" or "loca" tables are not found');
18183
+ }
18184
+ this.isOpenType = false;
18185
+ }
18186
+
18187
+ if (!tables.maxp) {
18188
+ error('Required "maxp" table is not found');
18189
+ }
18190
+
18191
+ font.pos = (font.start || 0) + tables.maxp.offset;
18192
+ var version = font.getInt32();
18193
+ var numGlyphs = font.getUint16();
18194
+ var maxFunctionDefs = 0;
18195
+ if (version >= 0x00010000 && tables.maxp.length >= 22) {
18196
+ // maxZones can be invalid
18197
+ font.pos += 8;
18198
+ var maxZones = font.getUint16();
18199
+ if (maxZones > 2) { // reset to 2 if font has invalid maxZones
18200
+ tables.maxp.data[14] = 0;
18201
+ tables.maxp.data[15] = 2;
18202
+ }
18203
+ font.pos += 4;
18204
+ maxFunctionDefs = font.getUint16();
18205
+ }
18206
+
18207
+ var dupFirstEntry = false;
18208
+ if (properties.type === 'CIDFontType2' && properties.toUnicode &&
18209
+ properties.toUnicode.get(0) > '\u0000') {
18210
+ // oracle's defect (see 3427), duplicating first entry
18211
+ dupFirstEntry = true;
18212
+ numGlyphs++;
18213
+ tables.maxp.data[4] = numGlyphs >> 8;
18214
+ tables.maxp.data[5] = numGlyphs & 255;
18215
+ }
18216
+
18217
+ var hintsValid = sanitizeTTPrograms(tables.fpgm, tables.prep,
18218
+ tables['cvt '], maxFunctionDefs);
18219
+ if (!hintsValid) {
18220
+ delete tables.fpgm;
18221
+ delete tables.prep;
18222
+ delete tables['cvt '];
18223
+ }
18224
+
18225
+ // Ensure the hmtx table contains the advance width and
18226
+ // sidebearings information for numGlyphs in the maxp table
18227
+ sanitizeMetrics(font, tables.hhea, tables.hmtx, numGlyphs);
18228
+
18229
+ if (!tables.head) {
18230
+ error('Required "head" table is not found');
18231
+ }
18232
+
18233
+ sanitizeHead(tables.head, numGlyphs, isTrueType ? tables.loca.length : 0);
18234
+
18235
+ var missingGlyphs = {};
18236
+ if (isTrueType) {
18237
+ var isGlyphLocationsLong = int16(tables.head.data[50],
18238
+ tables.head.data[51]);
18239
+ missingGlyphs = sanitizeGlyphLocations(tables.loca, tables.glyf,
18240
+ numGlyphs, isGlyphLocationsLong,
18241
+ hintsValid, dupFirstEntry);
18242
+ }
18243
+
18244
+ if (!tables.hhea) {
18245
+ error('Required "hhea" table is not found');
18246
+ }
18247
+
18248
+ // Sanitizer reduces the glyph advanceWidth to the maxAdvanceWidth
18249
+ // Sometimes it's 0. That needs to be fixed
18250
+ if (tables.hhea.data[10] === 0 && tables.hhea.data[11] === 0) {
18251
+ tables.hhea.data[10] = 0xFF;
18252
+ tables.hhea.data[11] = 0xFF;
18253
+ }
18254
+
18255
+ // The 'post' table has glyphs names.
18256
+ if (tables.post) {
18257
+ var valid = readPostScriptTable(tables.post, properties, numGlyphs);
18258
+ if (!valid) {
18259
+ tables.post = null;
18260
+ }
18261
+ }
18262
+
18263
+ var charCodeToGlyphId = [], charCode, toUnicode = properties.toUnicode;
18264
+
18265
+ function hasGlyph(glyphId, charCode) {
18266
+ if (!missingGlyphs[glyphId]) {
18267
+ return true;
18268
+ }
18269
+ if (charCode >= 0 && toUnicode.has(charCode)) {
18270
+ return true;
18271
+ }
18272
+ return false;
18273
+ }
18274
+
18275
+ if (properties.type === 'CIDFontType2') {
18276
+ var cidToGidMap = properties.cidToGidMap || [];
18277
+ var isCidToGidMapEmpty = cidToGidMap.length === 0;
18278
+
18279
+ properties.cMap.forEach(function(charCode, cid) {
18280
+ assert(cid <= 0xffff, 'Max size of CID is 65,535');
18281
+ var glyphId = -1;
18282
+ if (isCidToGidMapEmpty) {
18283
+ glyphId = charCode;
18284
+ } else if (cidToGidMap[cid] !== undefined) {
18285
+ glyphId = cidToGidMap[cid];
18286
+ }
18287
+
18288
+ if (glyphId >= 0 && glyphId < numGlyphs &&
18289
+ hasGlyph(glyphId, charCode)) {
18290
+ charCodeToGlyphId[charCode] = glyphId;
18291
+ }
18292
+ });
18293
+ if (dupFirstEntry) {
18294
+ charCodeToGlyphId[0] = numGlyphs - 1;
18295
+ }
18296
+ } else {
18297
+ // Most of the following logic in this code branch is based on the
18298
+ // 9.6.6.4 of the PDF spec.
18299
+ var cmapTable = readCmapTable(tables.cmap, font, this.isSymbolicFont);
18300
+ var cmapPlatformId = cmapTable.platformId;
18301
+ var cmapEncodingId = cmapTable.encodingId;
18302
+ var cmapMappings = cmapTable.mappings;
18303
+ var cmapMappingsLength = cmapMappings.length;
18304
+ var hasEncoding = properties.differences.length ||
18305
+ !!properties.baseEncodingName;
18306
+
18307
+ // The spec seems to imply that if the font is symbolic the encoding
18308
+ // should be ignored, this doesn't appear to work for 'preistabelle.pdf'
18309
+ // where the the font is symbolic and it has an encoding.
18310
+ if (hasEncoding &&
18311
+ (cmapPlatformId === 3 && cmapEncodingId === 1 ||
18312
+ cmapPlatformId === 1 && cmapEncodingId === 0) ||
18313
+ (cmapPlatformId === -1 && cmapEncodingId === -1 && // Temporary hack
18314
+ !!Encodings[properties.baseEncodingName])) { // Temporary hack
18315
+ // When no preferred cmap table was found and |baseEncodingName| is
18316
+ // one of the predefined encodings, we seem to obtain a better
18317
+ // |charCodeToGlyphId| map from the code below (fixes bug 1057544).
18318
+ // TODO: Note that this is a hack which should be removed as soon as
18319
+ // we have proper support for more exotic cmap tables.
18320
+
18321
+ var baseEncoding = [];
18322
+ if (properties.baseEncodingName === 'MacRomanEncoding' ||
18323
+ properties.baseEncodingName === 'WinAnsiEncoding') {
18324
+ baseEncoding = Encodings[properties.baseEncodingName];
18325
+ }
18326
+ for (charCode = 0; charCode < 256; charCode++) {
18327
+ var glyphName;
18328
+ if (this.differences && charCode in this.differences) {
18329
+ glyphName = this.differences[charCode];
18330
+ } else if (charCode in baseEncoding &&
18331
+ baseEncoding[charCode] !== '') {
18332
+ glyphName = baseEncoding[charCode];
18333
+ } else {
18334
+ glyphName = Encodings.StandardEncoding[charCode];
18335
+ }
18336
+ if (!glyphName) {
18337
+ continue;
18338
+ }
18339
+ var unicodeOrCharCode;
18340
+ if (cmapPlatformId === 3 && cmapEncodingId === 1) {
18341
+ unicodeOrCharCode = GlyphsUnicode[glyphName];
18342
+ } else if (cmapPlatformId === 1 && cmapEncodingId === 0) {
18343
+ // TODO: the encoding needs to be updated with mac os table.
18344
+ unicodeOrCharCode = Encodings.MacRomanEncoding.indexOf(glyphName);
18345
+ }
18346
+
18347
+ var found = false;
18348
+ for (i = 0; i < cmapMappingsLength; ++i) {
18349
+ if (cmapMappings[i].charCode === unicodeOrCharCode &&
18350
+ hasGlyph(cmapMappings[i].glyphId, unicodeOrCharCode)) {
18351
+ charCodeToGlyphId[charCode] = cmapMappings[i].glyphId;
18352
+ found = true;
18353
+ break;
18354
+ }
18355
+ }
18356
+ if (!found && properties.glyphNames) {
18357
+ // Try to map using the post table. There are currently no known
18358
+ // pdfs that this fixes.
18359
+ var glyphId = properties.glyphNames.indexOf(glyphName);
18360
+ if (glyphId > 0 && hasGlyph(glyphId, -1)) {
18361
+ charCodeToGlyphId[charCode] = glyphId;
18362
+ }
18363
+ }
18364
+ }
18365
+ } else if (cmapPlatformId === 0 && cmapEncodingId === 0) {
18366
+ // Default Unicode semantics, use the charcodes as is.
18367
+ for (i = 0; i < cmapMappingsLength; ++i) {
18368
+ charCodeToGlyphId[cmapMappings[i].charCode] =
18369
+ cmapMappings[i].glyphId;
18370
+ }
18371
+ } else {
18372
+ // For (3, 0) cmap tables:
18373
+ // The charcode key being stored in charCodeToGlyphId is the lower
18374
+ // byte of the two-byte charcodes of the cmap table since according to
18375
+ // the spec: 'each byte from the string shall be prepended with the
18376
+ // high byte of the range [of charcodes in the cmap table], to form
18377
+ // a two-byte character, which shall be used to select the
18378
+ // associated glyph description from the subtable'.
18379
+ //
18380
+ // For (1, 0) cmap tables:
18381
+ // 'single bytes from the string shall be used to look up the
18382
+ // associated glyph descriptions from the subtable'. This means
18383
+ // charcodes in the cmap will be single bytes, so no-op since
18384
+ // glyph.charCode & 0xFF === glyph.charCode
18385
+ for (i = 0; i < cmapMappingsLength; ++i) {
18386
+ charCode = cmapMappings[i].charCode & 0xFF;
18387
+ charCodeToGlyphId[charCode] = cmapMappings[i].glyphId;
18388
+ }
18389
+ }
18390
+ }
18391
+
18392
+ if (charCodeToGlyphId.length === 0) {
18393
+ // defines at least one glyph
18394
+ charCodeToGlyphId[0] = 0;
18395
+ }
18396
+
18397
+ // Converting glyphs and ids into font's cmap table
18398
+ var newMapping = adjustMapping(charCodeToGlyphId, properties);
18399
+ this.toFontChar = newMapping.toFontChar;
18400
+ tables.cmap = {
18401
+ tag: 'cmap',
18402
+ data: createCmapTable(newMapping.charCodeToGlyphId)
18403
+ };
18404
+
18405
+ if (!tables['OS/2'] || !validateOS2Table(tables['OS/2'])) {
18406
+ // extract some more font properties from the OpenType head and
18407
+ // hhea tables; yMin and descent value are always negative
18408
+ var override = {
18409
+ unitsPerEm: int16(tables.head.data[18], tables.head.data[19]),
18410
+ yMax: int16(tables.head.data[42], tables.head.data[43]),
18411
+ yMin: int16(tables.head.data[38], tables.head.data[39]) - 0x10000,
18412
+ ascent: int16(tables.hhea.data[4], tables.hhea.data[5]),
18413
+ descent: int16(tables.hhea.data[6], tables.hhea.data[7]) - 0x10000
18414
+ };
18415
+
18416
+ tables['OS/2'] = {
18417
+ tag: 'OS/2',
18418
+ data: createOS2Table(properties, newMapping.charCodeToGlyphId,
18419
+ override)
18420
+ };
18421
+ }
18422
+
18423
+ // Rewrite the 'post' table if needed
18424
+ if (!tables.post) {
18425
+ tables.post = {
18426
+ tag: 'post',
18427
+ data: createPostTable(properties)
18428
+ };
18429
+ }
18430
+
18431
+ if (!isTrueType) {
18432
+ try {
18433
+ // Trying to repair CFF file
18434
+ cffFile = new Stream(tables['CFF '].data);
18435
+ var parser = new CFFParser(cffFile, properties);
18436
+ cff = parser.parse();
18437
+ var compiler = new CFFCompiler(cff);
18438
+ tables['CFF '].data = compiler.compile();
18439
+ } catch (e) {
18440
+ warn('Failed to compile font ' + properties.loadedName);
18441
+ }
18442
+ }
18443
+
18444
+ // Re-creating 'name' table
18445
+ if (!tables.name) {
18446
+ tables.name = {
18447
+ tag: 'name',
18448
+ data: createNameTable(this.name)
18449
+ };
18450
+ } else {
18451
+ // ... using existing 'name' table as prototype
18452
+ var namePrototype = readNameTable(tables.name);
18453
+ tables.name.data = createNameTable(name, namePrototype);
18454
+ }
18455
+
18456
+ var builder = new OpenTypeFileBuilder(header.version);
18457
+ for (var tableTag in tables) {
18458
+ builder.addTable(tableTag, tables[tableTag].data);
18459
+ }
18460
+ return builder.toArray();
18461
+ },
18462
+
18463
+ convert: function Font_convert(fontName, font, properties) {
18464
+ // TODO: Check the charstring widths to determine this.
18465
+ properties.fixedPitch = false;
18466
+
18467
+ var mapping = font.getGlyphMapping(properties);
18468
+ var newMapping = adjustMapping(mapping, properties);
18469
+ this.toFontChar = newMapping.toFontChar;
18470
+ var numGlyphs = font.numGlyphs;
18471
+
18472
+ function getCharCodes(charCodeToGlyphId, glyphId) {
18473
+ var charCodes = null;
18474
+ for (var charCode in charCodeToGlyphId) {
18475
+ if (glyphId === charCodeToGlyphId[charCode]) {
18476
+ if (!charCodes) {
18477
+ charCodes = [];
18478
+ }
18479
+ charCodes.push(charCode | 0);
18480
+ }
18481
+ }
18482
+ return charCodes;
18483
+ }
18484
+
18485
+ function createCharCode(charCodeToGlyphId, glyphId) {
18486
+ for (var charCode in charCodeToGlyphId) {
18487
+ if (glyphId === charCodeToGlyphId[charCode]) {
18488
+ return charCode | 0;
18489
+ }
18490
+ }
18491
+ newMapping.charCodeToGlyphId[newMapping.nextAvailableFontCharCode] =
18492
+ glyphId;
18493
+ return newMapping.nextAvailableFontCharCode++;
18494
+ }
18495
+
18496
+ var seacs = font.seacs;
18497
+ if (SEAC_ANALYSIS_ENABLED && seacs && seacs.length) {
18498
+ var matrix = properties.fontMatrix || FONT_IDENTITY_MATRIX;
18499
+ var charset = font.getCharset();
18500
+ var seacMap = Object.create(null);
18501
+ for (var glyphId in seacs) {
18502
+ glyphId |= 0;
18503
+ var seac = seacs[glyphId];
18504
+ var baseGlyphName = Encodings.StandardEncoding[seac[2]];
18505
+ var accentGlyphName = Encodings.StandardEncoding[seac[3]];
18506
+ var baseGlyphId = charset.indexOf(baseGlyphName);
18507
+ var accentGlyphId = charset.indexOf(accentGlyphName);
18508
+ if (baseGlyphId < 0 || accentGlyphId < 0) {
18509
+ continue;
18510
+ }
18511
+ var accentOffset = {
18512
+ x: seac[0] * matrix[0] + seac[1] * matrix[2] + matrix[4],
18513
+ y: seac[0] * matrix[1] + seac[1] * matrix[3] + matrix[5]
18514
+ };
18515
+
18516
+ var charCodes = getCharCodes(mapping, glyphId);
18517
+ if (!charCodes) {
18518
+ // There's no point in mapping it if the char code was never mapped
18519
+ // to begin with.
18520
+ continue;
18521
+ }
18522
+ for (var i = 0, ii = charCodes.length; i < ii; i++) {
18523
+ var charCode = charCodes[i];
18524
+ // Find a fontCharCode that maps to the base and accent glyphs.
18525
+ // If one doesn't exists, create it.
18526
+ var charCodeToGlyphId = newMapping.charCodeToGlyphId;
18527
+ var baseFontCharCode = createCharCode(charCodeToGlyphId,
18528
+ baseGlyphId);
18529
+ var accentFontCharCode = createCharCode(charCodeToGlyphId,
18530
+ accentGlyphId);
18531
+ seacMap[charCode] = {
18532
+ baseFontCharCode: baseFontCharCode,
18533
+ accentFontCharCode: accentFontCharCode,
18534
+ accentOffset: accentOffset
18535
+ };
18536
+ }
18537
+ }
18538
+ properties.seacMap = seacMap;
18539
+ }
18540
+
18541
+ var unitsPerEm = 1 / (properties.fontMatrix || FONT_IDENTITY_MATRIX)[0];
18542
+
18543
+ var builder = new OpenTypeFileBuilder('\x4F\x54\x54\x4F');
18544
+ // PostScript Font Program
18545
+ builder.addTable('CFF ', font.data);
18546
+ // OS/2 and Windows Specific metrics
18547
+ builder.addTable('OS/2', createOS2Table(properties,
18548
+ newMapping.charCodeToGlyphId));
18549
+ // Character to glyphs mapping
18550
+ builder.addTable('cmap', createCmapTable(newMapping.charCodeToGlyphId));
18551
+ // Font header
18552
+ builder.addTable('head',
18553
+ '\x00\x01\x00\x00' + // Version number
18554
+ '\x00\x00\x10\x00' + // fontRevision
18555
+ '\x00\x00\x00\x00' + // checksumAdjustement
18556
+ '\x5F\x0F\x3C\xF5' + // magicNumber
18557
+ '\x00\x00' + // Flags
18558
+ safeString16(unitsPerEm) + // unitsPerEM
18559
+ '\x00\x00\x00\x00\x9e\x0b\x7e\x27' + // creation date
18560
+ '\x00\x00\x00\x00\x9e\x0b\x7e\x27' + // modifification date
18561
+ '\x00\x00' + // xMin
18562
+ safeString16(properties.descent) + // yMin
18563
+ '\x0F\xFF' + // xMax
18564
+ safeString16(properties.ascent) + // yMax
18565
+ string16(properties.italicAngle ? 2 : 0) + // macStyle
18566
+ '\x00\x11' + // lowestRecPPEM
18567
+ '\x00\x00' + // fontDirectionHint
18568
+ '\x00\x00' + // indexToLocFormat
18569
+ '\x00\x00'); // glyphDataFormat
18570
+
18571
+ // Horizontal header
18572
+ builder.addTable('hhea',
18573
+ '\x00\x01\x00\x00' + // Version number
18574
+ safeString16(properties.ascent) + // Typographic Ascent
18575
+ safeString16(properties.descent) + // Typographic Descent
18576
+ '\x00\x00' + // Line Gap
18577
+ '\xFF\xFF' + // advanceWidthMax
18578
+ '\x00\x00' + // minLeftSidebearing
18579
+ '\x00\x00' + // minRightSidebearing
18580
+ '\x00\x00' + // xMaxExtent
18581
+ safeString16(properties.capHeight) + // caretSlopeRise
18582
+ safeString16(Math.tan(properties.italicAngle) *
18583
+ properties.xHeight) + // caretSlopeRun
18584
+ '\x00\x00' + // caretOffset
18585
+ '\x00\x00' + // -reserved-
18586
+ '\x00\x00' + // -reserved-
18587
+ '\x00\x00' + // -reserved-
18588
+ '\x00\x00' + // -reserved-
18589
+ '\x00\x00' + // metricDataFormat
18590
+ string16(numGlyphs)); // Number of HMetrics
18591
+
18592
+ // Horizontal metrics
18593
+ builder.addTable('hmtx', (function fontFieldsHmtx() {
18594
+ var charstrings = font.charstrings;
18595
+ var cffWidths = font.cff ? font.cff.widths : null;
18596
+ var hmtx = '\x00\x00\x00\x00'; // Fake .notdef
18597
+ for (var i = 1, ii = numGlyphs; i < ii; i++) {
18598
+ var width = 0;
18599
+ if (charstrings) {
18600
+ var charstring = charstrings[i - 1];
18601
+ width = 'width' in charstring ? charstring.width : 0;
18602
+ } else if (cffWidths) {
18603
+ width = Math.ceil(cffWidths[i] || 0);
18604
+ }
18605
+ hmtx += string16(width) + string16(0);
18606
+ }
18607
+ return hmtx;
18608
+ })());
18609
+
18610
+ // Maximum profile
18611
+ builder.addTable('maxp',
18612
+ '\x00\x00\x50\x00' + // Version number
18613
+ string16(numGlyphs)); // Num of glyphs
18614
+
18615
+ // Naming tables
18616
+ builder.addTable('name', createNameTable(fontName));
18617
+
18618
+ // PostScript informations
18619
+ builder.addTable('post', createPostTable(properties));
18620
+
18621
+ return builder.toArray();
18622
+ },
18623
+
18624
+ /**
18625
+ * Builds a char code to unicode map based on section 9.10 of the spec.
18626
+ * @param {Object} properties Font properties object.
18627
+ * @return {Object} A ToUnicodeMap object.
18628
+ */
18629
+ buildToUnicode: function Font_buildToUnicode(properties) {
18630
+ // Section 9.10.2 Mapping Character Codes to Unicode Values
18631
+ if (properties.toUnicode && properties.toUnicode.length !== 0) {
18632
+ return properties.toUnicode;
18633
+ }
18634
+ // According to the spec if the font is a simple font we should only map
18635
+ // to unicode if the base encoding is MacRoman, MacExpert, or WinAnsi or
18636
+ // the differences array only contains adobe standard or symbol set names,
18637
+ // in pratice it seems better to always try to create a toUnicode
18638
+ // map based of the default encoding.
18639
+ var toUnicode, charcode;
18640
+ if (!properties.composite /* is simple font */) {
18641
+ toUnicode = [];
18642
+ var encoding = properties.defaultEncoding.slice();
18643
+ var baseEncodingName = properties.baseEncodingName;
18644
+ // Merge in the differences array.
18645
+ var differences = properties.differences;
18646
+ for (charcode in differences) {
18647
+ encoding[charcode] = differences[charcode];
18648
+ }
18649
+ for (charcode in encoding) {
18650
+ // a) Map the character code to a character name.
18651
+ var glyphName = encoding[charcode];
18652
+ // b) Look up the character name in the Adobe Glyph List (see the
18653
+ // Bibliography) to obtain the corresponding Unicode value.
18654
+ if (glyphName === '') {
18655
+ continue;
18656
+ } else if (GlyphsUnicode[glyphName] === undefined) {
18657
+ // (undocumented) c) Few heuristics to recognize unknown glyphs
18658
+ // NOTE: Adobe Reader does not do this step, but OSX Preview does
18659
+ var code = 0;
18660
+ switch (glyphName[0]) {
18661
+ case 'G': // Gxx glyph
18662
+ if (glyphName.length === 3) {
18663
+ code = parseInt(glyphName.substr(1), 16);
18664
+ }
18665
+ break;
18666
+ case 'g': // g00xx glyph
18667
+ if (glyphName.length === 5) {
18668
+ code = parseInt(glyphName.substr(1), 16);
18669
+ }
18670
+ break;
18671
+ case 'C': // Cddd glyph
18672
+ case 'c': // cddd glyph
18673
+ if (glyphName.length >= 3) {
18674
+ code = +glyphName.substr(1);
18675
+ }
18676
+ break;
18677
+ }
18678
+ if (code) {
18679
+ // If |baseEncodingName| is one the predefined encodings,
18680
+ // and |code| equals |charcode|, using the glyph defined in the
18681
+ // baseEncoding seems to yield a better |toUnicode| mapping
18682
+ // (fixes issue 5070).
18683
+ if (baseEncodingName && code === +charcode) {
18684
+ var baseEncoding = Encodings[baseEncodingName];
18685
+ if (baseEncoding && (glyphName = baseEncoding[charcode])) {
18686
+ toUnicode[charcode] =
18687
+ String.fromCharCode(GlyphsUnicode[glyphName]);
18688
+ continue;
18689
+ }
18690
+ }
18691
+ toUnicode[charcode] = String.fromCharCode(code);
18692
+ }
18693
+ continue;
18694
+ }
18695
+ toUnicode[charcode] = String.fromCharCode(GlyphsUnicode[glyphName]);
18696
+ }
18697
+ return new ToUnicodeMap(toUnicode);
18698
+ }
18699
+ // If the font is a composite font that uses one of the predefined CMaps
18700
+ // listed in Table 118 (except Identity–H and Identity–V) or whose
18701
+ // descendant CIDFont uses the Adobe-GB1, Adobe-CNS1, Adobe-Japan1, or
18702
+ // Adobe-Korea1 character collection:
18703
+ if (properties.composite && (
18704
+ (properties.cMap.builtInCMap &&
18705
+ !(properties.cMap instanceof IdentityCMap)) ||
18706
+ (properties.cidSystemInfo.registry === 'Adobe' &&
18707
+ (properties.cidSystemInfo.ordering === 'GB1' ||
18708
+ properties.cidSystemInfo.ordering === 'CNS1' ||
18709
+ properties.cidSystemInfo.ordering === 'Japan1' ||
18710
+ properties.cidSystemInfo.ordering === 'Korea1')))) {
18711
+ // Then:
18712
+ // a) Map the character code to a character identifier (CID) according
18713
+ // to the font’s CMap.
18714
+ // b) Obtain the registry and ordering of the character collection used
18715
+ // by the font’s CMap (for example, Adobe and Japan1) from its
18716
+ // CIDSystemInfo dictionary.
18717
+ var registry = properties.cidSystemInfo.registry;
18718
+ var ordering = properties.cidSystemInfo.ordering;
18719
+ // c) Construct a second CMap name by concatenating the registry and
18720
+ // ordering obtained in step (b) in the format registry–ordering–UCS2
18721
+ // (for example, Adobe–Japan1–UCS2).
18722
+ var ucs2CMapName = new Name(registry + '-' + ordering + '-UCS2');
18723
+ // d) Obtain the CMap with the name constructed in step (c) (available
18724
+ // from the ASN Web site; see the Bibliography).
18725
+ var ucs2CMap = CMapFactory.create(ucs2CMapName,
18726
+ { url: PDFJS.cMapUrl, packed: PDFJS.cMapPacked }, null);
18727
+ var cMap = properties.cMap;
18728
+ toUnicode = [];
18729
+ cMap.forEach(function(charcode, cid) {
18730
+ assert(cid <= 0xffff, 'Max size of CID is 65,535');
18731
+ // e) Map the CID obtained in step (a) according to the CMap obtained
18732
+ // in step (d), producing a Unicode value.
18733
+ var ucs2 = ucs2CMap.lookup(cid);
18734
+ if (ucs2) {
18735
+ toUnicode[charcode] =
18736
+ String.fromCharCode((ucs2.charCodeAt(0) << 8) +
18737
+ ucs2.charCodeAt(1));
18738
+ }
18739
+ });
18740
+ return new ToUnicodeMap(toUnicode);
18741
+ }
18742
+
18743
+ // The viewer's choice, just use an identity map.
18744
+ return new IdentityToUnicodeMap(properties.firstChar,
18745
+ properties.lastChar);
18746
+ },
18747
+
18748
+ get spaceWidth() {
18749
+ if ('_shadowWidth' in this) {
18750
+ return this._shadowWidth;
18751
+ }
18752
+
18753
+ // trying to estimate space character width
18754
+ var possibleSpaceReplacements = ['space', 'minus', 'one', 'i'];
18755
+ var width;
18756
+ for (var i = 0, ii = possibleSpaceReplacements.length; i < ii; i++) {
18757
+ var glyphName = possibleSpaceReplacements[i];
18758
+ // if possible, getting width by glyph name
18759
+ if (glyphName in this.widths) {
18760
+ width = this.widths[glyphName];
18761
+ break;
18762
+ }
18763
+ var glyphUnicode = GlyphsUnicode[glyphName];
18764
+ // finding the charcode via unicodeToCID map
18765
+ var charcode = 0;
18766
+ if (this.composite) {
18767
+ if (this.cMap.contains(glyphUnicode)) {
18768
+ charcode = this.cMap.lookup(glyphUnicode);
18769
+ }
18770
+ }
18771
+ // ... via toUnicode map
18772
+ if (!charcode && 'toUnicode' in this) {
18773
+ charcode = this.toUnicode.charCodeOf(glyphUnicode);
18774
+ }
18775
+ // setting it to unicode if negative or undefined
18776
+ if (charcode <= 0) {
18777
+ charcode = glyphUnicode;
18778
+ }
18779
+ // trying to get width via charcode
18780
+ width = this.widths[charcode];
18781
+ if (width) {
18782
+ break; // the non-zero width found
18783
+ }
18784
+ }
18785
+ width = width || this.defaultWidth;
18786
+ // Do not shadow the property here. See discussion:
18787
+ // https://github.com/mozilla/pdf.js/pull/2127#discussion_r1662280
18788
+ this._shadowWidth = width;
18789
+ return width;
18790
+ },
18791
+
18792
+ charToGlyph: function Font_charToGlyph(charcode) {
18793
+ var fontCharCode, width, operatorListId;
18794
+
18795
+ var widthCode = charcode;
18796
+ if (this.cMap && this.cMap.contains(charcode)) {
18797
+ widthCode = this.cMap.lookup(charcode);
18798
+ }
18799
+ width = this.widths[widthCode];
18800
+ width = isNum(width) ? width : this.defaultWidth;
18801
+ var vmetric = this.vmetrics && this.vmetrics[widthCode];
18802
+
18803
+ var unicode = this.toUnicode.get(charcode) || charcode;
18804
+ if (typeof unicode === 'number') {
18805
+ unicode = String.fromCharCode(unicode);
18806
+ }
18807
+
18808
+ // First try the toFontChar map, if it's not there then try falling
18809
+ // back to the char code.
18810
+ fontCharCode = this.toFontChar[charcode] || charcode;
18811
+ if (this.missingFile) {
18812
+ fontCharCode = mapSpecialUnicodeValues(fontCharCode);
18813
+ }
18814
+
18815
+ if (this.isType3Font) {
18816
+ // Font char code in this case is actually a glyph name.
18817
+ operatorListId = fontCharCode;
18818
+ }
18819
+
18820
+ var accent = null;
18821
+ if (this.seacMap && this.seacMap[charcode]) {
18822
+ var seac = this.seacMap[charcode];
18823
+ fontCharCode = seac.baseFontCharCode;
18824
+ accent = {
18825
+ fontChar: String.fromCharCode(seac.accentFontCharCode),
18826
+ offset: seac.accentOffset
18827
+ };
18828
+ }
18829
+
18830
+ var fontChar = String.fromCharCode(fontCharCode);
18831
+
18832
+ var glyph = this.glyphCache[charcode];
18833
+ if (!glyph ||
18834
+ !glyph.matchesForCache(fontChar, unicode, accent, width, vmetric,
18835
+ operatorListId)) {
18836
+ glyph = new Glyph(fontChar, unicode, accent, width, vmetric,
18837
+ operatorListId);
18838
+ this.glyphCache[charcode] = glyph;
18839
+ }
18840
+ return glyph;
18841
+ },
18842
+
18843
+ charsToGlyphs: function Font_charsToGlyphs(chars) {
18844
+ var charsCache = this.charsCache;
18845
+ var glyphs, glyph, charcode;
18846
+
18847
+ // if we translated this string before, just grab it from the cache
18848
+ if (charsCache) {
18849
+ glyphs = charsCache[chars];
18850
+ if (glyphs) {
18851
+ return glyphs;
18852
+ }
18853
+ }
18854
+
18855
+ // lazily create the translation cache
18856
+ if (!charsCache) {
18857
+ charsCache = this.charsCache = Object.create(null);
18858
+ }
18859
+
18860
+ glyphs = [];
18861
+ var charsCacheKey = chars;
18862
+ var i = 0, ii;
18863
+
18864
+ if (this.cMap) {
18865
+ // composite fonts have multi-byte strings convert the string from
18866
+ // single-byte to multi-byte
18867
+ var c = {};
18868
+ while (i < chars.length) {
18869
+ this.cMap.readCharCode(chars, i, c);
18870
+ charcode = c.charcode;
18871
+ var length = c.length;
18872
+ i += length;
18873
+ glyph = this.charToGlyph(charcode);
18874
+ glyphs.push(glyph);
18875
+ // placing null after each word break charcode (ASCII SPACE)
18876
+ // Ignore occurences of 0x20 in multiple-byte codes.
18877
+ if (length === 1 && chars.charCodeAt(i - 1) === 0x20) {
18878
+ glyphs.push(null);
18879
+ }
18880
+ }
18881
+ } else {
18882
+ for (i = 0, ii = chars.length; i < ii; ++i) {
18883
+ charcode = chars.charCodeAt(i);
18884
+ glyph = this.charToGlyph(charcode);
18885
+ glyphs.push(glyph);
18886
+ if (charcode === 0x20) {
18887
+ glyphs.push(null);
18888
+ }
18889
+ }
18890
+ }
18891
+
18892
+ // Enter the translated string into the cache
18893
+ return (charsCache[charsCacheKey] = glyphs);
18894
+ }
18895
+ };
18896
+
18897
+ return Font;
18898
+ })();
18899
+
18900
+ var ErrorFont = (function ErrorFontClosure() {
18901
+ function ErrorFont(error) {
18902
+ this.error = error;
18903
+ this.loadedName = 'g_font_error';
18904
+ this.loading = false;
18905
+ }
18906
+
18907
+ ErrorFont.prototype = {
18908
+ charsToGlyphs: function ErrorFont_charsToGlyphs() {
18909
+ return [];
18910
+ },
18911
+ exportData: function ErrorFont_exportData() {
18912
+ return {error: this.error};
18913
+ }
18914
+ };
18915
+
18916
+ return ErrorFont;
18917
+ })();
18918
+
18919
+ /**
18920
+ * Shared logic for building a char code to glyph id mapping for Type1 and
18921
+ * simple CFF fonts. See section 9.6.6.2 of the spec.
18922
+ * @param {Object} properties Font properties object.
18923
+ * @param {Object} builtInEncoding The encoding contained within the actual font
18924
+ * data.
18925
+ * @param {Array} Array of glyph names where the index is the glyph ID.
18926
+ * @returns {Object} A char code to glyph ID map.
18927
+ */
18928
+ function type1FontGlyphMapping(properties, builtInEncoding, glyphNames) {
18929
+ var charCodeToGlyphId = Object.create(null);
18930
+ var glyphId, charCode, baseEncoding;
18931
+
18932
+ if (properties.baseEncodingName) {
18933
+ // If a valid base encoding name was used, the mapping is initialized with
18934
+ // that.
18935
+ baseEncoding = Encodings[properties.baseEncodingName];
18936
+ for (charCode = 0; charCode < baseEncoding.length; charCode++) {
18937
+ glyphId = glyphNames.indexOf(baseEncoding[charCode]);
18938
+ if (glyphId >= 0) {
18939
+ charCodeToGlyphId[charCode] = glyphId;
18940
+ } else {
18941
+ charCodeToGlyphId[charCode] = 0; // notdef
18942
+ }
18943
+ }
18944
+ } else if (!!(properties.flags & FontFlags.Symbolic)) {
18945
+ // For a symbolic font the encoding should be the fonts built-in
18946
+ // encoding.
18947
+ for (charCode in builtInEncoding) {
18948
+ charCodeToGlyphId[charCode] = builtInEncoding[charCode];
18949
+ }
18950
+ } else {
18951
+ // For non-symbolic fonts that don't have a base encoding the standard
18952
+ // encoding should be used.
18953
+ baseEncoding = Encodings.StandardEncoding;
18954
+ for (charCode = 0; charCode < baseEncoding.length; charCode++) {
18955
+ glyphId = glyphNames.indexOf(baseEncoding[charCode]);
18956
+ if (glyphId >= 0) {
18957
+ charCodeToGlyphId[charCode] = glyphId;
18958
+ } else {
18959
+ charCodeToGlyphId[charCode] = 0; // notdef
18960
+ }
18961
+ }
18962
+ }
18963
+
18964
+ // Lastly, merge in the differences.
18965
+ var differences = properties.differences;
18966
+ if (differences) {
18967
+ for (charCode in differences) {
18968
+ var glyphName = differences[charCode];
18969
+ glyphId = glyphNames.indexOf(glyphName);
18970
+ if (glyphId >= 0) {
18971
+ charCodeToGlyphId[charCode] = glyphId;
18972
+ } else {
18973
+ charCodeToGlyphId[charCode] = 0; // notdef
18974
+ }
18975
+ }
18976
+ }
18977
+ return charCodeToGlyphId;
18978
+ }
18979
+
18980
+ /*
18981
+ * CharStrings are encoded following the the CharString Encoding sequence
18982
+ * describe in Chapter 6 of the "Adobe Type1 Font Format" specification.
18983
+ * The value in a byte indicates a command, a number, or subsequent bytes
18984
+ * that are to be interpreted in a special way.
18985
+ *
18986
+ * CharString Number Encoding:
18987
+ * A CharString byte containing the values from 32 through 255 inclusive
18988
+ * indicate an integer. These values are decoded in four ranges.
18989
+ *
18990
+ * 1. A CharString byte containing a value, v, between 32 and 246 inclusive,
18991
+ * indicate the integer v - 139. Thus, the integer values from -107 through
18992
+ * 107 inclusive may be encoded in single byte.
18993
+ *
18994
+ * 2. A CharString byte containing a value, v, between 247 and 250 inclusive,
18995
+ * indicates an integer involving the next byte, w, according to the formula:
18996
+ * [(v - 247) x 256] + w + 108
18997
+ *
18998
+ * 3. A CharString byte containing a value, v, between 251 and 254 inclusive,
18999
+ * indicates an integer involving the next byte, w, according to the formula:
19000
+ * -[(v - 251) * 256] - w - 108
19001
+ *
19002
+ * 4. A CharString containing the value 255 indicates that the next 4 bytes
19003
+ * are a two complement signed integer. The first of these bytes contains the
19004
+ * highest order bits, the second byte contains the next higher order bits
19005
+ * and the fourth byte contain the lowest order bits.
19006
+ *
19007
+ *
19008
+ * CharString Command Encoding:
19009
+ * CharStrings commands are encoded in 1 or 2 bytes.
19010
+ *
19011
+ * Single byte commands are encoded in 1 byte that contains a value between
19012
+ * 0 and 31 inclusive.
19013
+ * If a command byte contains the value 12, then the value in the next byte
19014
+ * indicates a command. This "escape" mechanism allows many extra commands
19015
+ * to be encoded and this encoding technique helps to minimize the length of
19016
+ * the charStrings.
19017
+ */
19018
+ var Type1CharString = (function Type1CharStringClosure() {
19019
+ var COMMAND_MAP = {
19020
+ 'hstem': [1],
19021
+ 'vstem': [3],
19022
+ 'vmoveto': [4],
19023
+ 'rlineto': [5],
19024
+ 'hlineto': [6],
19025
+ 'vlineto': [7],
19026
+ 'rrcurveto': [8],
19027
+ 'callsubr': [10],
19028
+ 'flex': [12, 35],
19029
+ 'drop' : [12, 18],
19030
+ 'endchar': [14],
19031
+ 'rmoveto': [21],
19032
+ 'hmoveto': [22],
19033
+ 'vhcurveto': [30],
19034
+ 'hvcurveto': [31]
19035
+ };
19036
+
19037
+ function Type1CharString() {
19038
+ this.width = 0;
19039
+ this.lsb = 0;
19040
+ this.flexing = false;
19041
+ this.output = [];
19042
+ this.stack = [];
19043
+ }
19044
+
19045
+ Type1CharString.prototype = {
19046
+ convert: function Type1CharString_convert(encoded, subrs) {
19047
+ var count = encoded.length;
19048
+ var error = false;
19049
+ var wx, sbx, subrNumber;
19050
+ for (var i = 0; i < count; i++) {
19051
+ var value = encoded[i];
19052
+ if (value < 32) {
19053
+ if (value === 12) {
19054
+ value = (value << 8) + encoded[++i];
19055
+ }
19056
+ switch (value) {
19057
+ case 1: // hstem
19058
+ if (!HINTING_ENABLED) {
19059
+ this.stack = [];
19060
+ break;
19061
+ }
19062
+ error = this.executeCommand(2, COMMAND_MAP.hstem);
19063
+ break;
19064
+ case 3: // vstem
19065
+ if (!HINTING_ENABLED) {
19066
+ this.stack = [];
19067
+ break;
19068
+ }
19069
+ error = this.executeCommand(2, COMMAND_MAP.vstem);
19070
+ break;
19071
+ case 4: // vmoveto
19072
+ if (this.flexing) {
19073
+ if (this.stack.length < 1) {
19074
+ error = true;
19075
+ break;
19076
+ }
19077
+ // Add the dx for flex and but also swap the values so they are
19078
+ // the right order.
19079
+ var dy = this.stack.pop();
19080
+ this.stack.push(0, dy);
19081
+ break;
19082
+ }
19083
+ error = this.executeCommand(1, COMMAND_MAP.vmoveto);
19084
+ break;
19085
+ case 5: // rlineto
19086
+ error = this.executeCommand(2, COMMAND_MAP.rlineto);
19087
+ break;
19088
+ case 6: // hlineto
19089
+ error = this.executeCommand(1, COMMAND_MAP.hlineto);
19090
+ break;
19091
+ case 7: // vlineto
19092
+ error = this.executeCommand(1, COMMAND_MAP.vlineto);
19093
+ break;
19094
+ case 8: // rrcurveto
19095
+ error = this.executeCommand(6, COMMAND_MAP.rrcurveto);
19096
+ break;
19097
+ case 9: // closepath
19098
+ // closepath is a Type1 command that does not take argument and is
19099
+ // useless in Type2 and it can simply be ignored.
19100
+ this.stack = [];
19101
+ break;
19102
+ case 10: // callsubr
19103
+ if (this.stack.length < 1) {
19104
+ error = true;
19105
+ break;
19106
+ }
19107
+ subrNumber = this.stack.pop();
19108
+ error = this.convert(subrs[subrNumber], subrs);
19109
+ break;
19110
+ case 11: // return
19111
+ return error;
19112
+ case 13: // hsbw
19113
+ if (this.stack.length < 2) {
19114
+ error = true;
19115
+ break;
19116
+ }
19117
+ // To convert to type2 we have to move the width value to the
19118
+ // first part of the charstring and then use hmoveto with lsb.
19119
+ wx = this.stack.pop();
19120
+ sbx = this.stack.pop();
19121
+ this.lsb = sbx;
19122
+ this.width = wx;
19123
+ this.stack.push(wx, sbx);
19124
+ error = this.executeCommand(2, COMMAND_MAP.hmoveto);
19125
+ break;
19126
+ case 14: // endchar
19127
+ this.output.push(COMMAND_MAP.endchar[0]);
19128
+ break;
19129
+ case 21: // rmoveto
19130
+ if (this.flexing) {
19131
+ break;
19132
+ }
19133
+ error = this.executeCommand(2, COMMAND_MAP.rmoveto);
19134
+ break;
19135
+ case 22: // hmoveto
19136
+ if (this.flexing) {
19137
+ // Add the dy for flex.
19138
+ this.stack.push(0);
19139
+ break;
19140
+ }
19141
+ error = this.executeCommand(1, COMMAND_MAP.hmoveto);
19142
+ break;
19143
+ case 30: // vhcurveto
19144
+ error = this.executeCommand(4, COMMAND_MAP.vhcurveto);
19145
+ break;
19146
+ case 31: // hvcurveto
19147
+ error = this.executeCommand(4, COMMAND_MAP.hvcurveto);
19148
+ break;
19149
+ case (12 << 8) + 0: // dotsection
19150
+ // dotsection is a Type1 command to specify some hinting feature
19151
+ // for dots that do not take a parameter and it can safely be
19152
+ // ignored for Type2.
19153
+ this.stack = [];
19154
+ break;
19155
+ case (12 << 8) + 1: // vstem3
19156
+ if (!HINTING_ENABLED) {
19157
+ this.stack = [];
19158
+ break;
19159
+ }
19160
+ // [vh]stem3 are Type1 only and Type2 supports [vh]stem with
19161
+ // multiple parameters, so instead of returning [vh]stem3 take a
19162
+ // shortcut and return [vhstem] instead.
19163
+ error = this.executeCommand(2, COMMAND_MAP.vstem);
19164
+ break;
19165
+ case (12 << 8) + 2: // hstem3
19166
+ if (!HINTING_ENABLED) {
19167
+ this.stack = [];
19168
+ break;
19169
+ }
19170
+ // See vstem3.
19171
+ error = this.executeCommand(2, COMMAND_MAP.hstem);
19172
+ break;
19173
+ case (12 << 8) + 6: // seac
19174
+ // seac is like type 2's special endchar but it doesn't use the
19175
+ // first argument asb, so remove it.
19176
+ if (SEAC_ANALYSIS_ENABLED) {
19177
+ this.seac = this.stack.splice(-4, 4);
19178
+ error = this.executeCommand(0, COMMAND_MAP.endchar);
19179
+ } else {
19180
+ error = this.executeCommand(4, COMMAND_MAP.endchar);
19181
+ }
19182
+ break;
19183
+ case (12 << 8) + 7: // sbw
19184
+ if (this.stack.length < 4) {
19185
+ error = true;
19186
+ break;
19187
+ }
19188
+ // To convert to type2 we have to move the width value to the
19189
+ // first part of the charstring and then use rmoveto with
19190
+ // (dx, dy). The height argument will not be used for vmtx and
19191
+ // vhea tables reconstruction -- ignoring it.
19192
+ var wy = this.stack.pop();
19193
+ wx = this.stack.pop();
19194
+ var sby = this.stack.pop();
19195
+ sbx = this.stack.pop();
19196
+ this.lsb = sbx;
19197
+ this.width = wx;
19198
+ this.stack.push(wx, sbx, sby);
19199
+ error = this.executeCommand(3, COMMAND_MAP.rmoveto);
19200
+ break;
19201
+ case (12 << 8) + 12: // div
19202
+ if (this.stack.length < 2) {
19203
+ error = true;
19204
+ break;
19205
+ }
19206
+ var num2 = this.stack.pop();
19207
+ var num1 = this.stack.pop();
19208
+ this.stack.push(num1 / num2);
19209
+ break;
19210
+ case (12 << 8) + 16: // callothersubr
19211
+ if (this.stack.length < 2) {
19212
+ error = true;
19213
+ break;
19214
+ }
19215
+ subrNumber = this.stack.pop();
19216
+ var numArgs = this.stack.pop();
19217
+ if (subrNumber === 0 && numArgs === 3) {
19218
+ var flexArgs = this.stack.splice(this.stack.length - 17, 17);
19219
+ this.stack.push(
19220
+ flexArgs[2] + flexArgs[0], // bcp1x + rpx
19221
+ flexArgs[3] + flexArgs[1], // bcp1y + rpy
19222
+ flexArgs[4], // bcp2x
19223
+ flexArgs[5], // bcp2y
19224
+ flexArgs[6], // p2x
19225
+ flexArgs[7], // p2y
19226
+ flexArgs[8], // bcp3x
19227
+ flexArgs[9], // bcp3y
19228
+ flexArgs[10], // bcp4x
19229
+ flexArgs[11], // bcp4y
19230
+ flexArgs[12], // p3x
19231
+ flexArgs[13], // p3y
19232
+ flexArgs[14] // flexDepth
19233
+ // 15 = finalx unused by flex
19234
+ // 16 = finaly unused by flex
19235
+ );
19236
+ error = this.executeCommand(13, COMMAND_MAP.flex, true);
19237
+ this.flexing = false;
19238
+ this.stack.push(flexArgs[15], flexArgs[16]);
19239
+ } else if (subrNumber === 1 && numArgs === 0) {
19240
+ this.flexing = true;
19241
+ }
19242
+ break;
19243
+ case (12 << 8) + 17: // pop
19244
+ // Ignore this since it is only used with othersubr.
19245
+ break;
19246
+ case (12 << 8) + 33: // setcurrentpoint
19247
+ // Ignore for now.
19248
+ this.stack = [];
19249
+ break;
19250
+ default:
19251
+ warn('Unknown type 1 charstring command of "' + value + '"');
19252
+ break;
19253
+ }
19254
+ if (error) {
19255
+ break;
19256
+ }
19257
+ continue;
19258
+ } else if (value <= 246) {
19259
+ value = value - 139;
19260
+ } else if (value <= 250) {
19261
+ value = ((value - 247) * 256) + encoded[++i] + 108;
19262
+ } else if (value <= 254) {
19263
+ value = -((value - 251) * 256) - encoded[++i] - 108;
19264
+ } else {
19265
+ value = (encoded[++i] & 0xff) << 24 | (encoded[++i] & 0xff) << 16 |
19266
+ (encoded[++i] & 0xff) << 8 | (encoded[++i] & 0xff) << 0;
19267
+ }
19268
+ this.stack.push(value);
19269
+ }
19270
+ return error;
19271
+ },
19272
+
19273
+ executeCommand: function(howManyArgs, command, keepStack) {
19274
+ var stackLength = this.stack.length;
19275
+ if (howManyArgs > stackLength) {
19276
+ return true;
19277
+ }
19278
+ var start = stackLength - howManyArgs;
19279
+ for (var i = start; i < stackLength; i++) {
19280
+ var value = this.stack[i];
19281
+ if (value === (value | 0)) { // int
19282
+ this.output.push(28, (value >> 8) & 0xff, value & 0xff);
19283
+ } else { // fixed point
19284
+ value = (65536 * value) | 0;
19285
+ this.output.push(255,
19286
+ (value >> 24) & 0xFF,
19287
+ (value >> 16) & 0xFF,
19288
+ (value >> 8) & 0xFF,
19289
+ value & 0xFF);
19290
+ }
19291
+ }
19292
+ this.output.push.apply(this.output, command);
19293
+ if (keepStack) {
19294
+ this.stack.splice(start, howManyArgs);
19295
+ } else {
19296
+ this.stack.length = 0;
19297
+ }
19298
+ return false;
19299
+ }
19300
+ };
19301
+
19302
+ return Type1CharString;
19303
+ })();
19304
+
19305
+ /*
19306
+ * Type1Parser encapsulate the needed code for parsing a Type1 font
19307
+ * program. Some of its logic depends on the Type2 charstrings
19308
+ * structure.
19309
+ * Note: this doesn't really parse the font since that would require evaluation
19310
+ * of PostScript, but it is possible in most cases to extract what we need
19311
+ * without a full parse.
19312
+ */
19313
+ var Type1Parser = (function Type1ParserClosure() {
19314
+ /*
19315
+ * Decrypt a Sequence of Ciphertext Bytes to Produce the Original Sequence
19316
+ * of Plaintext Bytes. The function took a key as a parameter which can be
19317
+ * for decrypting the eexec block of for decoding charStrings.
19318
+ */
19319
+ var EEXEC_ENCRYPT_KEY = 55665;
19320
+ var CHAR_STRS_ENCRYPT_KEY = 4330;
19321
+
19322
+ function isHexDigit(code) {
19323
+ return code >= 48 && code <= 57 || // '0'-'9'
19324
+ code >= 65 && code <= 70 || // 'A'-'F'
19325
+ code >= 97 && code <= 102; // 'a'-'f'
19326
+ }
19327
+
19328
+ function decrypt(data, key, discardNumber) {
19329
+ var r = key | 0, c1 = 52845, c2 = 22719;
19330
+ var count = data.length;
19331
+ var decrypted = new Uint8Array(count);
19332
+ for (var i = 0; i < count; i++) {
19333
+ var value = data[i];
19334
+ decrypted[i] = value ^ (r >> 8);
19335
+ r = ((value + r) * c1 + c2) & ((1 << 16) - 1);
19336
+ }
19337
+ return Array.prototype.slice.call(decrypted, discardNumber);
19338
+ }
19339
+
19340
+ function decryptAscii(data, key, discardNumber) {
19341
+ var r = key | 0, c1 = 52845, c2 = 22719;
19342
+ var count = data.length, maybeLength = count >>> 1;
19343
+ var decrypted = new Uint8Array(maybeLength);
19344
+ var i, j;
19345
+ for (i = 0, j = 0; i < count; i++) {
19346
+ var digit1 = data[i];
19347
+ if (!isHexDigit(digit1)) {
19348
+ continue;
19349
+ }
19350
+ i++;
19351
+ var digit2;
19352
+ while (i < count && !isHexDigit(digit2 = data[i])) {
19353
+ i++;
19354
+ }
19355
+ if (i < count) {
19356
+ var value = parseInt(String.fromCharCode(digit1, digit2), 16);
19357
+ decrypted[j++] = value ^ (r >> 8);
19358
+ r = ((value + r) * c1 + c2) & ((1 << 16) - 1);
19359
+ }
19360
+ }
19361
+ return Array.prototype.slice.call(decrypted, discardNumber, j);
19362
+ }
19363
+
19364
+ function isSpecial(c) {
19365
+ return c === 0x2F || // '/'
19366
+ c === 0x5B || c === 0x5D || // '[', ']'
19367
+ c === 0x7B || c === 0x7D || // '{', '}'
19368
+ c === 0x28 || c === 0x29; // '(', ')'
19369
+ }
19370
+
19371
+ function Type1Parser(stream, encrypted) {
19372
+ if (encrypted) {
19373
+ var data = stream.getBytes();
19374
+ var isBinary = !(isHexDigit(data[0]) && isHexDigit(data[1]) &&
19375
+ isHexDigit(data[2]) && isHexDigit(data[3]));
19376
+ stream = new Stream(isBinary ? decrypt(data, EEXEC_ENCRYPT_KEY, 4) :
19377
+ decryptAscii(data, EEXEC_ENCRYPT_KEY, 4));
19378
+ }
19379
+ this.stream = stream;
19380
+ this.nextChar();
19381
+ }
19382
+
19383
+ Type1Parser.prototype = {
19384
+ readNumberArray: function Type1Parser_readNumberArray() {
19385
+ this.getToken(); // read '[' or '{' (arrays can start with either)
19386
+ var array = [];
19387
+ while (true) {
19388
+ var token = this.getToken();
19389
+ if (token === null || token === ']' || token === '}') {
19390
+ break;
19391
+ }
19392
+ array.push(parseFloat(token || 0));
19393
+ }
19394
+ return array;
19395
+ },
19396
+
19397
+ readNumber: function Type1Parser_readNumber() {
19398
+ var token = this.getToken();
19399
+ return parseFloat(token || 0);
19400
+ },
19401
+
19402
+ readInt: function Type1Parser_readInt() {
19403
+ // Use '| 0' to prevent setting a double into length such as the double
19404
+ // does not flow into the loop variable.
19405
+ var token = this.getToken();
19406
+ return parseInt(token || 0, 10) | 0;
19407
+ },
19408
+
19409
+ readBoolean: function Type1Parser_readBoolean() {
19410
+ var token = this.getToken();
19411
+
19412
+ // Use 1 and 0 since that's what type2 charstrings use.
19413
+ return token === 'true' ? 1 : 0;
19414
+ },
19415
+
19416
+ nextChar : function Type1_nextChar() {
19417
+ return (this.currentChar = this.stream.getByte());
19418
+ },
19419
+
19420
+ getToken: function Type1Parser_getToken() {
19421
+ // Eat whitespace and comments.
19422
+ var comment = false;
19423
+ var ch = this.currentChar;
19424
+ while (true) {
19425
+ if (ch === -1) {
19426
+ return null;
19427
+ }
19428
+
19429
+ if (comment) {
19430
+ if (ch === 0x0A || ch === 0x0D) {
19431
+ comment = false;
19432
+ }
19433
+ } else if (ch === 0x25) { // '%'
19434
+ comment = true;
19435
+ } else if (!Lexer.isSpace(ch)) {
19436
+ break;
19437
+ }
19438
+ ch = this.nextChar();
19439
+ }
19440
+ if (isSpecial(ch)) {
19441
+ this.nextChar();
19442
+ return String.fromCharCode(ch);
19443
+ }
19444
+ var token = '';
19445
+ do {
19446
+ token += String.fromCharCode(ch);
19447
+ ch = this.nextChar();
19448
+ } while (ch >= 0 && !Lexer.isSpace(ch) && !isSpecial(ch));
19449
+ return token;
19450
+ },
19451
+
19452
+ /*
19453
+ * Returns an object containing a Subrs array and a CharStrings
19454
+ * array extracted from and eexec encrypted block of data
19455
+ */
19456
+ extractFontProgram: function Type1Parser_extractFontProgram() {
19457
+ var stream = this.stream;
19458
+
19459
+ var subrs = [], charstrings = [];
19460
+ var program = {
19461
+ subrs: [],
19462
+ charstrings: [],
19463
+ properties: {
19464
+ 'privateData': {
19465
+ 'lenIV': 4
19466
+ }
19467
+ }
19468
+ };
19469
+ var token, length, data, lenIV, encoded;
19470
+ while ((token = this.getToken()) !== null) {
19471
+ if (token !== '/') {
19472
+ continue;
19473
+ }
19474
+ token = this.getToken();
19475
+ switch (token) {
19476
+ case 'CharStrings':
19477
+ // The number immediately following CharStrings must be greater or
19478
+ // equal to the number of CharStrings.
19479
+ this.getToken();
19480
+ this.getToken(); // read in 'dict'
19481
+ this.getToken(); // read in 'dup'
19482
+ this.getToken(); // read in 'begin'
19483
+ while(true) {
19484
+ token = this.getToken();
19485
+ if (token === null || token === 'end') {
19486
+ break;
19487
+ }
19488
+
19489
+ if (token !== '/') {
19490
+ continue;
19491
+ }
19492
+ var glyph = this.getToken();
19493
+ length = this.readInt();
19494
+ this.getToken(); // read in 'RD' or '-|'
19495
+ data = stream.makeSubStream(stream.pos, length);
19496
+ lenIV = program.properties.privateData['lenIV'];
19497
+ encoded = decrypt(data.getBytes(), CHAR_STRS_ENCRYPT_KEY, lenIV);
19498
+ // Skip past the required space and binary data.
19499
+ stream.skip(length);
19500
+ this.nextChar();
19501
+ token = this.getToken(); // read in 'ND' or '|-'
19502
+ if (token === 'noaccess') {
19503
+ this.getToken(); // read in 'def'
19504
+ }
19505
+ charstrings.push({
19506
+ glyph: glyph,
19507
+ encoded: encoded
19508
+ });
19509
+ }
19510
+ break;
19511
+ case 'Subrs':
19512
+ var num = this.readInt();
19513
+ this.getToken(); // read in 'array'
19514
+ while ((token = this.getToken()) === 'dup') {
19515
+ var index = this.readInt();
19516
+ length = this.readInt();
19517
+ this.getToken(); // read in 'RD' or '-|'
19518
+ data = stream.makeSubStream(stream.pos, length);
19519
+ lenIV = program.properties.privateData['lenIV'];
19520
+ encoded = decrypt(data.getBytes(), CHAR_STRS_ENCRYPT_KEY, lenIV);
19521
+ // Skip past the required space and binary data.
19522
+ stream.skip(length);
19523
+ this.nextChar();
19524
+ token = this.getToken(); // read in 'NP' or '|'
19525
+ if (token === 'noaccess') {
19526
+ this.getToken(); // read in 'put'
19527
+ }
19528
+ subrs[index] = encoded;
19529
+ }
19530
+ break;
19531
+ case 'BlueValues':
19532
+ case 'OtherBlues':
19533
+ case 'FamilyBlues':
19534
+ case 'FamilyOtherBlues':
19535
+ var blueArray = this.readNumberArray();
19536
+ // *Blue* values may contain invalid data: disables reading of
19537
+ // those values when hinting is disabled.
19538
+ if (blueArray.length > 0 && (blueArray.length % 2) === 0 &&
19539
+ HINTING_ENABLED) {
19540
+ program.properties.privateData[token] = blueArray;
19541
+ }
19542
+ break;
19543
+ case 'StemSnapH':
19544
+ case 'StemSnapV':
19545
+ program.properties.privateData[token] = this.readNumberArray();
19546
+ break;
19547
+ case 'StdHW':
19548
+ case 'StdVW':
19549
+ program.properties.privateData[token] =
19550
+ this.readNumberArray()[0];
19551
+ break;
19552
+ case 'BlueShift':
19553
+ case 'lenIV':
19554
+ case 'BlueFuzz':
19555
+ case 'BlueScale':
19556
+ case 'LanguageGroup':
19557
+ case 'ExpansionFactor':
19558
+ program.properties.privateData[token] = this.readNumber();
19559
+ break;
19560
+ case 'ForceBold':
19561
+ program.properties.privateData[token] = this.readBoolean();
19562
+ break;
19563
+ }
19564
+ }
19565
+
19566
+ for (var i = 0; i < charstrings.length; i++) {
19567
+ glyph = charstrings[i].glyph;
19568
+ encoded = charstrings[i].encoded;
19569
+ var charString = new Type1CharString();
19570
+ var error = charString.convert(encoded, subrs);
19571
+ var output = charString.output;
19572
+ if (error) {
19573
+ // It seems when FreeType encounters an error while evaluating a glyph
19574
+ // that it completely ignores the glyph so we'll mimic that behaviour
19575
+ // here and put an endchar to make the validator happy.
19576
+ output = [14];
19577
+ }
19578
+ program.charstrings.push({
19579
+ glyphName: glyph,
19580
+ charstring: output,
19581
+ width: charString.width,
19582
+ lsb: charString.lsb,
19583
+ seac: charString.seac
19584
+ });
19585
+ }
19586
+
19587
+ return program;
19588
+ },
19589
+
19590
+ extractFontHeader: function Type1Parser_extractFontHeader(properties) {
19591
+ var token;
19592
+ while ((token = this.getToken()) !== null) {
19593
+ if (token !== '/') {
19594
+ continue;
19595
+ }
19596
+ token = this.getToken();
19597
+ switch (token) {
19598
+ case 'FontMatrix':
19599
+ var matrix = this.readNumberArray();
19600
+ properties.fontMatrix = matrix;
19601
+ break;
19602
+ case 'Encoding':
19603
+ var encodingArg = this.getToken();
19604
+ var encoding;
19605
+ if (!/^\d+$/.test(encodingArg)) {
19606
+ // encoding name is specified
19607
+ encoding = Encodings[encodingArg];
19608
+ } else {
19609
+ encoding = [];
19610
+ var size = parseInt(encodingArg, 10) | 0;
19611
+ this.getToken(); // read in 'array'
19612
+
19613
+ for (var j = 0; j < size; j++) {
19614
+ token = this.getToken();
19615
+ // skipping till first dup or def (e.g. ignoring for statement)
19616
+ while (token !== 'dup' && token !== 'def') {
19617
+ token = this.getToken();
19618
+ if (token === null) {
19619
+ return; // invalid header
19620
+ }
19621
+ }
19622
+ if (token === 'def') {
19623
+ break; // read all array data
19624
+ }
19625
+ var index = this.readInt();
19626
+ this.getToken(); // read in '/'
19627
+ var glyph = this.getToken();
19628
+ encoding[index] = glyph;
19629
+ this.getToken(); // read the in 'put'
19630
+ }
19631
+ }
19632
+ properties.builtInEncoding = encoding;
19633
+ break;
19634
+ case 'FontBBox':
19635
+ var fontBBox = this.readNumberArray();
19636
+ // adjusting ascent/descent
19637
+ properties.ascent = fontBBox[3];
19638
+ properties.descent = fontBBox[1];
19639
+ properties.ascentScaled = true;
19640
+ break;
19641
+ }
19642
+ }
19643
+ }
19644
+ };
19645
+
19646
+ return Type1Parser;
19647
+ })();
19648
+
19649
+ /**
19650
+ * The CFF class takes a Type1 file and wrap it into a
19651
+ * 'Compact Font Format' which itself embed Type2 charstrings.
19652
+ */
19653
+ var CFFStandardStrings = [
19654
+ '.notdef', 'space', 'exclam', 'quotedbl', 'numbersign', 'dollar', 'percent',
19655
+ 'ampersand', 'quoteright', 'parenleft', 'parenright', 'asterisk', 'plus',
19656
+ 'comma', 'hyphen', 'period', 'slash', 'zero', 'one', 'two', 'three', 'four',
19657
+ 'five', 'six', 'seven', 'eight', 'nine', 'colon', 'semicolon', 'less',
19658
+ 'equal', 'greater', 'question', 'at', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
19659
+ 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
19660
+ 'X', 'Y', 'Z', 'bracketleft', 'backslash', 'bracketright', 'asciicircum',
19661
+ 'underscore', 'quoteleft', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
19662
+ 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
19663
+ 'z', 'braceleft', 'bar', 'braceright', 'asciitilde', 'exclamdown', 'cent',
19664
+ 'sterling', 'fraction', 'yen', 'florin', 'section', 'currency',
19665
+ 'quotesingle', 'quotedblleft', 'guillemotleft', 'guilsinglleft',
19666
+ 'guilsinglright', 'fi', 'fl', 'endash', 'dagger', 'daggerdbl',
19667
+ 'periodcentered', 'paragraph', 'bullet', 'quotesinglbase', 'quotedblbase',
19668
+ 'quotedblright', 'guillemotright', 'ellipsis', 'perthousand', 'questiondown',
19669
+ 'grave', 'acute', 'circumflex', 'tilde', 'macron', 'breve', 'dotaccent',
19670
+ 'dieresis', 'ring', 'cedilla', 'hungarumlaut', 'ogonek', 'caron', 'emdash',
19671
+ 'AE', 'ordfeminine', 'Lslash', 'Oslash', 'OE', 'ordmasculine', 'ae',
19672
+ 'dotlessi', 'lslash', 'oslash', 'oe', 'germandbls', 'onesuperior',
19673
+ 'logicalnot', 'mu', 'trademark', 'Eth', 'onehalf', 'plusminus', 'Thorn',
19674
+ 'onequarter', 'divide', 'brokenbar', 'degree', 'thorn', 'threequarters',
19675
+ 'twosuperior', 'registered', 'minus', 'eth', 'multiply', 'threesuperior',
19676
+ 'copyright', 'Aacute', 'Acircumflex', 'Adieresis', 'Agrave', 'Aring',
19677
+ 'Atilde', 'Ccedilla', 'Eacute', 'Ecircumflex', 'Edieresis', 'Egrave',
19678
+ 'Iacute', 'Icircumflex', 'Idieresis', 'Igrave', 'Ntilde', 'Oacute',
19679
+ 'Ocircumflex', 'Odieresis', 'Ograve', 'Otilde', 'Scaron', 'Uacute',
19680
+ 'Ucircumflex', 'Udieresis', 'Ugrave', 'Yacute', 'Ydieresis', 'Zcaron',
19681
+ 'aacute', 'acircumflex', 'adieresis', 'agrave', 'aring', 'atilde',
19682
+ 'ccedilla', 'eacute', 'ecircumflex', 'edieresis', 'egrave', 'iacute',
19683
+ 'icircumflex', 'idieresis', 'igrave', 'ntilde', 'oacute', 'ocircumflex',
19684
+ 'odieresis', 'ograve', 'otilde', 'scaron', 'uacute', 'ucircumflex',
19685
+ 'udieresis', 'ugrave', 'yacute', 'ydieresis', 'zcaron', 'exclamsmall',
19686
+ 'Hungarumlautsmall', 'dollaroldstyle', 'dollarsuperior', 'ampersandsmall',
19687
+ 'Acutesmall', 'parenleftsuperior', 'parenrightsuperior', 'twodotenleader',
19688
+ 'onedotenleader', 'zerooldstyle', 'oneoldstyle', 'twooldstyle',
19689
+ 'threeoldstyle', 'fouroldstyle', 'fiveoldstyle', 'sixoldstyle',
19690
+ 'sevenoldstyle', 'eightoldstyle', 'nineoldstyle', 'commasuperior',
19691
+ 'threequartersemdash', 'periodsuperior', 'questionsmall', 'asuperior',
19692
+ 'bsuperior', 'centsuperior', 'dsuperior', 'esuperior', 'isuperior',
19693
+ 'lsuperior', 'msuperior', 'nsuperior', 'osuperior', 'rsuperior', 'ssuperior',
19694
+ 'tsuperior', 'ff', 'ffi', 'ffl', 'parenleftinferior', 'parenrightinferior',
19695
+ 'Circumflexsmall', 'hyphensuperior', 'Gravesmall', 'Asmall', 'Bsmall',
19696
+ 'Csmall', 'Dsmall', 'Esmall', 'Fsmall', 'Gsmall', 'Hsmall', 'Ismall',
19697
+ 'Jsmall', 'Ksmall', 'Lsmall', 'Msmall', 'Nsmall', 'Osmall', 'Psmall',
19698
+ 'Qsmall', 'Rsmall', 'Ssmall', 'Tsmall', 'Usmall', 'Vsmall', 'Wsmall',
19699
+ 'Xsmall', 'Ysmall', 'Zsmall', 'colonmonetary', 'onefitted', 'rupiah',
19700
+ 'Tildesmall', 'exclamdownsmall', 'centoldstyle', 'Lslashsmall',
19701
+ 'Scaronsmall', 'Zcaronsmall', 'Dieresissmall', 'Brevesmall', 'Caronsmall',
19702
+ 'Dotaccentsmall', 'Macronsmall', 'figuredash', 'hypheninferior',
19703
+ 'Ogoneksmall', 'Ringsmall', 'Cedillasmall', 'questiondownsmall', 'oneeighth',
19704
+ 'threeeighths', 'fiveeighths', 'seveneighths', 'onethird', 'twothirds',
19705
+ 'zerosuperior', 'foursuperior', 'fivesuperior', 'sixsuperior',
19706
+ 'sevensuperior', 'eightsuperior', 'ninesuperior', 'zeroinferior',
19707
+ 'oneinferior', 'twoinferior', 'threeinferior', 'fourinferior',
19708
+ 'fiveinferior', 'sixinferior', 'seveninferior', 'eightinferior',
19709
+ 'nineinferior', 'centinferior', 'dollarinferior', 'periodinferior',
19710
+ 'commainferior', 'Agravesmall', 'Aacutesmall', 'Acircumflexsmall',
19711
+ 'Atildesmall', 'Adieresissmall', 'Aringsmall', 'AEsmall', 'Ccedillasmall',
19712
+ 'Egravesmall', 'Eacutesmall', 'Ecircumflexsmall', 'Edieresissmall',
19713
+ 'Igravesmall', 'Iacutesmall', 'Icircumflexsmall', 'Idieresissmall',
19714
+ 'Ethsmall', 'Ntildesmall', 'Ogravesmall', 'Oacutesmall', 'Ocircumflexsmall',
19715
+ 'Otildesmall', 'Odieresissmall', 'OEsmall', 'Oslashsmall', 'Ugravesmall',
19716
+ 'Uacutesmall', 'Ucircumflexsmall', 'Udieresissmall', 'Yacutesmall',
19717
+ 'Thornsmall', 'Ydieresissmall', '001.000', '001.001', '001.002', '001.003',
19718
+ 'Black', 'Bold', 'Book', 'Light', 'Medium', 'Regular', 'Roman', 'Semibold'
19719
+ ];
19720
+
19721
+ // Type1Font is also a CIDFontType0.
19722
+ var Type1Font = function Type1Font(name, file, properties) {
19723
+ // Some bad generators embed pfb file as is, we have to strip 6-byte headers.
19724
+ // Also, length1 and length2 might be off by 6 bytes as well.
19725
+ // http://www.math.ubc.ca/~cass/piscript/type1.pdf
19726
+ var PFB_HEADER_SIZE = 6;
19727
+ var headerBlockLength = properties.length1;
19728
+ var eexecBlockLength = properties.length2;
19729
+ var pfbHeader = file.peekBytes(PFB_HEADER_SIZE);
19730
+ var pfbHeaderPresent = pfbHeader[0] === 0x80 && pfbHeader[1] === 0x01;
19731
+ if (pfbHeaderPresent) {
19732
+ file.skip(PFB_HEADER_SIZE);
19733
+ headerBlockLength = (pfbHeader[5] << 24) | (pfbHeader[4] << 16) |
19734
+ (pfbHeader[3] << 8) | pfbHeader[2];
19735
+ }
19736
+
19737
+ // Get the data block containing glyphs and subrs informations
19738
+ var headerBlock = new Stream(file.getBytes(headerBlockLength));
19739
+ var headerBlockParser = new Type1Parser(headerBlock);
19740
+ headerBlockParser.extractFontHeader(properties);
19741
+
19742
+ if (pfbHeaderPresent) {
19743
+ pfbHeader = file.getBytes(PFB_HEADER_SIZE);
19744
+ eexecBlockLength = (pfbHeader[5] << 24) | (pfbHeader[4] << 16) |
19745
+ (pfbHeader[3] << 8) | pfbHeader[2];
19746
+ }
19747
+
19748
+ // Decrypt the data blocks and retrieve it's content
19749
+ var eexecBlock = new Stream(file.getBytes(eexecBlockLength));
19750
+ var eexecBlockParser = new Type1Parser(eexecBlock, true);
19751
+ var data = eexecBlockParser.extractFontProgram();
19752
+ for (var info in data.properties) {
19753
+ properties[info] = data.properties[info];
19754
+ }
19755
+
19756
+ var charstrings = data.charstrings;
19757
+ var type2Charstrings = this.getType2Charstrings(charstrings);
19758
+ var subrs = this.getType2Subrs(data.subrs);
19759
+
19760
+ this.charstrings = charstrings;
19761
+ this.data = this.wrap(name, type2Charstrings, this.charstrings,
19762
+ subrs, properties);
19763
+ this.seacs = this.getSeacs(data.charstrings);
19764
+ };
19765
+
19766
+ Type1Font.prototype = {
19767
+ get numGlyphs() {
19768
+ return this.charstrings.length + 1;
19769
+ },
19770
+
19771
+ getCharset: function Type1Font_getCharset() {
19772
+ var charset = ['.notdef'];
19773
+ var charstrings = this.charstrings;
19774
+ for (var glyphId = 0; glyphId < charstrings.length; glyphId++) {
19775
+ charset.push(charstrings[glyphId].glyphName);
19776
+ }
19777
+ return charset;
19778
+ },
19779
+
19780
+ getGlyphMapping: function Type1Font_getGlyphMapping(properties) {
19781
+ var charstrings = this.charstrings;
19782
+ var glyphNames = ['.notdef'], glyphId;
19783
+ for (glyphId = 0; glyphId < charstrings.length; glyphId++) {
19784
+ glyphNames.push(charstrings[glyphId].glyphName);
19785
+ }
19786
+ var encoding = properties.builtInEncoding;
19787
+ if (encoding) {
19788
+ var builtInEncoding = {};
19789
+ for (var charCode in encoding) {
19790
+ glyphId = glyphNames.indexOf(encoding[charCode]);
19791
+ if (glyphId >= 0) {
19792
+ builtInEncoding[charCode] = glyphId;
19793
+ }
19794
+ }
19795
+ }
19796
+
19797
+ return type1FontGlyphMapping(properties, builtInEncoding, glyphNames);
19798
+ },
19799
+
19800
+ getSeacs: function Type1Font_getSeacs(charstrings) {
19801
+ var i, ii;
19802
+ var seacMap = [];
19803
+ for (i = 0, ii = charstrings.length; i < ii; i++) {
19804
+ var charstring = charstrings[i];
19805
+ if (charstring.seac) {
19806
+ // Offset by 1 for .notdef
19807
+ seacMap[i + 1] = charstring.seac;
19808
+ }
19809
+ }
19810
+ return seacMap;
19811
+ },
19812
+
19813
+ getType2Charstrings: function Type1Font_getType2Charstrings(
19814
+ type1Charstrings) {
19815
+ var type2Charstrings = [];
19816
+ for (var i = 0, ii = type1Charstrings.length; i < ii; i++) {
19817
+ type2Charstrings.push(type1Charstrings[i].charstring);
19818
+ }
19819
+ return type2Charstrings;
19820
+ },
19821
+
19822
+ getType2Subrs: function Type1Font_getType2Subrs(type1Subrs) {
19823
+ var bias = 0;
19824
+ var count = type1Subrs.length;
19825
+ if (count < 1133) {
19826
+ bias = 107;
19827
+ } else if (count < 33769) {
19828
+ bias = 1131;
19829
+ } else {
19830
+ bias = 32768;
19831
+ }
19832
+
19833
+ // Add a bunch of empty subrs to deal with the Type2 bias
19834
+ var type2Subrs = [];
19835
+ var i;
19836
+ for (i = 0; i < bias; i++) {
19837
+ type2Subrs.push([0x0B]);
19838
+ }
19839
+
19840
+ for (i = 0; i < count; i++) {
19841
+ type2Subrs.push(type1Subrs[i]);
19842
+ }
19843
+
19844
+ return type2Subrs;
19845
+ },
19846
+
19847
+ wrap: function Type1Font_wrap(name, glyphs, charstrings, subrs, properties) {
19848
+ var cff = new CFF();
19849
+ cff.header = new CFFHeader(1, 0, 4, 4);
19850
+
19851
+ cff.names = [name];
19852
+
19853
+ var topDict = new CFFTopDict();
19854
+ // CFF strings IDs 0...390 are predefined names, so refering
19855
+ // to entries in our own String INDEX starts at SID 391.
19856
+ topDict.setByName('version', 391);
19857
+ topDict.setByName('Notice', 392);
19858
+ topDict.setByName('FullName', 393);
19859
+ topDict.setByName('FamilyName', 394);
19860
+ topDict.setByName('Weight', 395);
19861
+ topDict.setByName('Encoding', null); // placeholder
19862
+ topDict.setByName('FontMatrix', properties.fontMatrix);
19863
+ topDict.setByName('FontBBox', properties.bbox);
19864
+ topDict.setByName('charset', null); // placeholder
19865
+ topDict.setByName('CharStrings', null); // placeholder
19866
+ topDict.setByName('Private', null); // placeholder
19867
+ cff.topDict = topDict;
19868
+
19869
+ var strings = new CFFStrings();
19870
+ strings.add('Version 0.11'); // Version
19871
+ strings.add('See original notice'); // Notice
19872
+ strings.add(name); // FullName
19873
+ strings.add(name); // FamilyName
19874
+ strings.add('Medium'); // Weight
19875
+ cff.strings = strings;
19876
+
19877
+ cff.globalSubrIndex = new CFFIndex();
19878
+
19879
+ var count = glyphs.length;
19880
+ var charsetArray = [0];
19881
+ var i, ii;
19882
+ for (i = 0; i < count; i++) {
19883
+ var index = CFFStandardStrings.indexOf(charstrings[i].glyphName);
19884
+ // TODO: Insert the string and correctly map it. Previously it was
19885
+ // thought mapping names that aren't in the standard strings to .notdef
19886
+ // was fine, however in issue818 when mapping them all to .notdef the
19887
+ // adieresis glyph no longer worked.
19888
+ if (index === -1) {
19889
+ index = 0;
19890
+ }
19891
+ charsetArray.push((index >> 8) & 0xff, index & 0xff);
19892
+ }
19893
+ cff.charset = new CFFCharset(false, 0, [], charsetArray);
19894
+
19895
+ var charStringsIndex = new CFFIndex();
19896
+ charStringsIndex.add([0x8B, 0x0E]); // .notdef
19897
+ for (i = 0; i < count; i++) {
19898
+ charStringsIndex.add(glyphs[i]);
19899
+ }
19900
+ cff.charStrings = charStringsIndex;
19901
+
19902
+ var privateDict = new CFFPrivateDict();
19903
+ privateDict.setByName('Subrs', null); // placeholder
19904
+ var fields = [
19905
+ 'BlueValues',
19906
+ 'OtherBlues',
19907
+ 'FamilyBlues',
19908
+ 'FamilyOtherBlues',
19909
+ 'StemSnapH',
19910
+ 'StemSnapV',
19911
+ 'BlueShift',
19912
+ 'BlueFuzz',
19913
+ 'BlueScale',
19914
+ 'LanguageGroup',
19915
+ 'ExpansionFactor',
19916
+ 'ForceBold',
19917
+ 'StdHW',
19918
+ 'StdVW'
19919
+ ];
19920
+ for (i = 0, ii = fields.length; i < ii; i++) {
19921
+ var field = fields[i];
19922
+ if (!properties.privateData.hasOwnProperty(field)) {
19923
+ continue;
19924
+ }
19925
+ var value = properties.privateData[field];
19926
+ if (isArray(value)) {
19927
+ // All of the private dictionary array data in CFF must be stored as
19928
+ // "delta-encoded" numbers.
19929
+ for (var j = value.length - 1; j > 0; j--) {
19930
+ value[j] -= value[j - 1]; // ... difference from previous value
19931
+ }
19932
+ }
19933
+ privateDict.setByName(field, value);
19934
+ }
19935
+ cff.topDict.privateDict = privateDict;
19936
+
19937
+ var subrIndex = new CFFIndex();
19938
+ for (i = 0, ii = subrs.length; i < ii; i++) {
19939
+ subrIndex.add(subrs[i]);
19940
+ }
19941
+ privateDict.subrsIndex = subrIndex;
19942
+
19943
+ var compiler = new CFFCompiler(cff);
19944
+ return compiler.compile();
19945
+ }
19946
+ };
19947
+
19948
+ var CFFFont = (function CFFFontClosure() {
19949
+ function CFFFont(file, properties) {
19950
+ this.properties = properties;
19951
+
19952
+ var parser = new CFFParser(file, properties);
19953
+ this.cff = parser.parse();
19954
+ var compiler = new CFFCompiler(this.cff);
19955
+ this.seacs = this.cff.seacs;
19956
+ try {
19957
+ this.data = compiler.compile();
19958
+ } catch (e) {
19959
+ warn('Failed to compile font ' + properties.loadedName);
19960
+ // There may have just been an issue with the compiler, set the data
19961
+ // anyway and hope the font loaded.
19962
+ this.data = file;
19963
+ }
19964
+ }
19965
+
19966
+ CFFFont.prototype = {
19967
+ get numGlyphs() {
19968
+ return this.cff.charStrings.count;
19969
+ },
19970
+ getCharset: function CFFFont_getCharset() {
19971
+ return this.cff.charset.charset;
19972
+ },
19973
+ getGlyphMapping: function CFFFont_getGlyphMapping() {
19974
+ var cff = this.cff;
19975
+ var properties = this.properties;
19976
+ var charsets = cff.charset.charset;
19977
+ var charCodeToGlyphId;
19978
+ var glyphId;
19979
+
19980
+ if (properties.composite) {
19981
+ charCodeToGlyphId = Object.create(null);
19982
+ if (cff.isCIDFont) {
19983
+ // If the font is actually a CID font then we should use the charset
19984
+ // to map CIDs to GIDs.
19985
+ for (glyphId = 0; glyphId < charsets.length; glyphId++) {
19986
+ var cid = charsets[glyphId];
19987
+ var charCode = properties.cMap.charCodeOf(cid);
19988
+ charCodeToGlyphId[charCode] = glyphId;
19989
+ }
19990
+ } else {
19991
+ // If it is NOT actually a CID font then CIDs should be mapped
19992
+ // directly to GIDs.
19993
+ for (glyphId = 0; glyphId < cff.charStrings.count; glyphId++) {
19994
+ charCodeToGlyphId[glyphId] = glyphId;
19995
+ }
19996
+ }
19997
+ return charCodeToGlyphId;
19998
+ }
19999
+
20000
+ var encoding = cff.encoding ? cff.encoding.encoding : null;
20001
+ charCodeToGlyphId = type1FontGlyphMapping(properties, encoding, charsets);
20002
+ return charCodeToGlyphId;
20003
+ }
20004
+ };
20005
+
20006
+ return CFFFont;
20007
+ })();
20008
+
20009
+ var CFFParser = (function CFFParserClosure() {
20010
+ var CharstringValidationData = [
20011
+ null,
20012
+ { id: 'hstem', min: 2, stackClearing: true, stem: true },
20013
+ null,
20014
+ { id: 'vstem', min: 2, stackClearing: true, stem: true },
20015
+ { id: 'vmoveto', min: 1, stackClearing: true },
20016
+ { id: 'rlineto', min: 2, resetStack: true },
20017
+ { id: 'hlineto', min: 1, resetStack: true },
20018
+ { id: 'vlineto', min: 1, resetStack: true },
20019
+ { id: 'rrcurveto', min: 6, resetStack: true },
20020
+ null,
20021
+ { id: 'callsubr', min: 1, undefStack: true },
20022
+ { id: 'return', min: 0, undefStack: true },
20023
+ null, // 12
20024
+ null,
20025
+ { id: 'endchar', min: 0, stackClearing: true },
20026
+ null,
20027
+ null,
20028
+ null,
20029
+ { id: 'hstemhm', min: 2, stackClearing: true, stem: true },
20030
+ { id: 'hintmask', min: 0, stackClearing: true },
20031
+ { id: 'cntrmask', min: 0, stackClearing: true },
20032
+ { id: 'rmoveto', min: 2, stackClearing: true },
20033
+ { id: 'hmoveto', min: 1, stackClearing: true },
20034
+ { id: 'vstemhm', min: 2, stackClearing: true, stem: true },
20035
+ { id: 'rcurveline', min: 8, resetStack: true },
20036
+ { id: 'rlinecurve', min: 8, resetStack: true },
20037
+ { id: 'vvcurveto', min: 4, resetStack: true },
20038
+ { id: 'hhcurveto', min: 4, resetStack: true },
20039
+ null, // shortint
20040
+ { id: 'callgsubr', min: 1, undefStack: true },
20041
+ { id: 'vhcurveto', min: 4, resetStack: true },
20042
+ { id: 'hvcurveto', min: 4, resetStack: true }
20043
+ ];
20044
+ var CharstringValidationData12 = [
20045
+ null,
20046
+ null,
20047
+ null,
20048
+ { id: 'and', min: 2, stackDelta: -1 },
20049
+ { id: 'or', min: 2, stackDelta: -1 },
20050
+ { id: 'not', min: 1, stackDelta: 0 },
20051
+ null,
20052
+ null,
20053
+ null,
20054
+ { id: 'abs', min: 1, stackDelta: 0 },
20055
+ { id: 'add', min: 2, stackDelta: -1,
20056
+ stackFn: function stack_div(stack, index) {
20057
+ stack[index - 2] = stack[index - 2] + stack[index - 1];
20058
+ }
20059
+ },
20060
+ { id: 'sub', min: 2, stackDelta: -1,
20061
+ stackFn: function stack_div(stack, index) {
20062
+ stack[index - 2] = stack[index - 2] - stack[index - 1];
20063
+ }
20064
+ },
20065
+ { id: 'div', min: 2, stackDelta: -1,
20066
+ stackFn: function stack_div(stack, index) {
20067
+ stack[index - 2] = stack[index - 2] / stack[index - 1];
20068
+ }
20069
+ },
20070
+ null,
20071
+ { id: 'neg', min: 1, stackDelta: 0,
20072
+ stackFn: function stack_div(stack, index) {
20073
+ stack[index - 1] = -stack[index - 1];
20074
+ }
20075
+ },
20076
+ { id: 'eq', min: 2, stackDelta: -1 },
20077
+ null,
20078
+ null,
20079
+ { id: 'drop', min: 1, stackDelta: -1 },
20080
+ null,
20081
+ { id: 'put', min: 2, stackDelta: -2 },
20082
+ { id: 'get', min: 1, stackDelta: 0 },
20083
+ { id: 'ifelse', min: 4, stackDelta: -3 },
20084
+ { id: 'random', min: 0, stackDelta: 1 },
20085
+ { id: 'mul', min: 2, stackDelta: -1,
20086
+ stackFn: function stack_div(stack, index) {
20087
+ stack[index - 2] = stack[index - 2] * stack[index - 1];
20088
+ }
20089
+ },
20090
+ null,
20091
+ { id: 'sqrt', min: 1, stackDelta: 0 },
20092
+ { id: 'dup', min: 1, stackDelta: 1 },
20093
+ { id: 'exch', min: 2, stackDelta: 0 },
20094
+ { id: 'index', min: 2, stackDelta: 0 },
20095
+ { id: 'roll', min: 3, stackDelta: -2 },
20096
+ null,
20097
+ null,
20098
+ null,
20099
+ { id: 'hflex', min: 7, resetStack: true },
20100
+ { id: 'flex', min: 13, resetStack: true },
20101
+ { id: 'hflex1', min: 9, resetStack: true },
20102
+ { id: 'flex1', min: 11, resetStack: true }
20103
+ ];
20104
+
20105
+ function CFFParser(file, properties) {
20106
+ this.bytes = file.getBytes();
20107
+ this.properties = properties;
20108
+ }
20109
+ CFFParser.prototype = {
20110
+ parse: function CFFParser_parse() {
20111
+ var properties = this.properties;
20112
+ var cff = new CFF();
20113
+ this.cff = cff;
20114
+
20115
+ // The first five sections must be in order, all the others are reached
20116
+ // via offsets contained in one of the below.
20117
+ var header = this.parseHeader();
20118
+ var nameIndex = this.parseIndex(header.endPos);
20119
+ var topDictIndex = this.parseIndex(nameIndex.endPos);
20120
+ var stringIndex = this.parseIndex(topDictIndex.endPos);
20121
+ var globalSubrIndex = this.parseIndex(stringIndex.endPos);
20122
+
20123
+ var topDictParsed = this.parseDict(topDictIndex.obj.get(0));
20124
+ var topDict = this.createDict(CFFTopDict, topDictParsed, cff.strings);
20125
+
20126
+ cff.header = header.obj;
20127
+ cff.names = this.parseNameIndex(nameIndex.obj);
20128
+ cff.strings = this.parseStringIndex(stringIndex.obj);
20129
+ cff.topDict = topDict;
20130
+ cff.globalSubrIndex = globalSubrIndex.obj;
20131
+
20132
+ this.parsePrivateDict(cff.topDict);
20133
+
20134
+ cff.isCIDFont = topDict.hasName('ROS');
20135
+
20136
+ var charStringOffset = topDict.getByName('CharStrings');
20137
+ var charStringsAndSeacs = this.parseCharStrings(charStringOffset);
20138
+ cff.charStrings = charStringsAndSeacs.charStrings;
20139
+ cff.seacs = charStringsAndSeacs.seacs;
20140
+ cff.widths = charStringsAndSeacs.widths;
20141
+
20142
+ var fontMatrix = topDict.getByName('FontMatrix');
20143
+ if (fontMatrix) {
20144
+ properties.fontMatrix = fontMatrix;
20145
+ }
20146
+
20147
+ var fontBBox = topDict.getByName('FontBBox');
20148
+ if (fontBBox) {
20149
+ // adjusting ascent/descent
20150
+ properties.ascent = fontBBox[3];
20151
+ properties.descent = fontBBox[1];
20152
+ properties.ascentScaled = true;
20153
+ }
20154
+
20155
+ var charset, encoding;
20156
+ if (cff.isCIDFont) {
20157
+ var fdArrayIndex = this.parseIndex(topDict.getByName('FDArray')).obj;
20158
+ for (var i = 0, ii = fdArrayIndex.count; i < ii; ++i) {
20159
+ var dictRaw = fdArrayIndex.get(i);
20160
+ var fontDict = this.createDict(CFFTopDict, this.parseDict(dictRaw),
20161
+ cff.strings);
20162
+ this.parsePrivateDict(fontDict);
20163
+ cff.fdArray.push(fontDict);
20164
+ }
20165
+ // cid fonts don't have an encoding
20166
+ encoding = null;
20167
+ charset = this.parseCharsets(topDict.getByName('charset'),
20168
+ cff.charStrings.count, cff.strings, true);
20169
+ cff.fdSelect = this.parseFDSelect(topDict.getByName('FDSelect'),
20170
+ cff.charStrings.count);
20171
+ } else {
20172
+ charset = this.parseCharsets(topDict.getByName('charset'),
20173
+ cff.charStrings.count, cff.strings, false);
20174
+ encoding = this.parseEncoding(topDict.getByName('Encoding'),
20175
+ properties,
20176
+ cff.strings, charset.charset);
20177
+ }
20178
+ cff.charset = charset;
20179
+ cff.encoding = encoding;
20180
+
20181
+ return cff;
20182
+ },
20183
+ parseHeader: function CFFParser_parseHeader() {
20184
+ var bytes = this.bytes;
20185
+ var bytesLength = bytes.length;
20186
+ var offset = 0;
20187
+
20188
+ // Prevent an infinite loop, by checking that the offset is within the
20189
+ // bounds of the bytes array. Necessary in empty, or invalid, font files.
20190
+ while (offset < bytesLength && bytes[offset] !== 1) {
20191
+ ++offset;
20192
+ }
20193
+ if (offset >= bytesLength) {
20194
+ error('Invalid CFF header');
20195
+ } else if (offset !== 0) {
20196
+ info('cff data is shifted');
20197
+ bytes = bytes.subarray(offset);
20198
+ this.bytes = bytes;
20199
+ }
20200
+ var major = bytes[0];
20201
+ var minor = bytes[1];
20202
+ var hdrSize = bytes[2];
20203
+ var offSize = bytes[3];
20204
+ var header = new CFFHeader(major, minor, hdrSize, offSize);
20205
+ return { obj: header, endPos: hdrSize };
20206
+ },
20207
+ parseDict: function CFFParser_parseDict(dict) {
20208
+ var pos = 0;
20209
+
20210
+ function parseOperand() {
20211
+ var value = dict[pos++];
20212
+ if (value === 30) {
20213
+ return parseFloatOperand(pos);
20214
+ } else if (value === 28) {
20215
+ value = dict[pos++];
20216
+ value = ((value << 24) | (dict[pos++] << 16)) >> 16;
20217
+ return value;
20218
+ } else if (value === 29) {
20219
+ value = dict[pos++];
20220
+ value = (value << 8) | dict[pos++];
20221
+ value = (value << 8) | dict[pos++];
20222
+ value = (value << 8) | dict[pos++];
20223
+ return value;
20224
+ } else if (value >= 32 && value <= 246) {
20225
+ return value - 139;
20226
+ } else if (value >= 247 && value <= 250) {
20227
+ return ((value - 247) * 256) + dict[pos++] + 108;
20228
+ } else if (value >= 251 && value <= 254) {
20229
+ return -((value - 251) * 256) - dict[pos++] - 108;
20230
+ } else {
20231
+ error('255 is not a valid DICT command');
20232
+ }
20233
+ return -1;
20234
+ }
20235
+
20236
+ function parseFloatOperand() {
20237
+ var str = '';
20238
+ var eof = 15;
20239
+ var lookup = ['0', '1', '2', '3', '4', '5', '6', '7', '8',
20240
+ '9', '.', 'E', 'E-', null, '-'];
20241
+ var length = dict.length;
20242
+ while (pos < length) {
20243
+ var b = dict[pos++];
20244
+ var b1 = b >> 4;
20245
+ var b2 = b & 15;
20246
+
20247
+ if (b1 === eof) {
20248
+ break;
20249
+ }
20250
+ str += lookup[b1];
20251
+
20252
+ if (b2 === eof) {
20253
+ break;
20254
+ }
20255
+ str += lookup[b2];
20256
+ }
20257
+ return parseFloat(str);
20258
+ }
20259
+
20260
+ var operands = [];
20261
+ var entries = [];
20262
+
20263
+ pos = 0;
20264
+ var end = dict.length;
20265
+ while (pos < end) {
20266
+ var b = dict[pos];
20267
+ if (b <= 21) {
20268
+ if (b === 12) {
20269
+ b = (b << 8) | dict[++pos];
20270
+ }
20271
+ entries.push([b, operands]);
20272
+ operands = [];
20273
+ ++pos;
20274
+ } else {
20275
+ operands.push(parseOperand());
20276
+ }
20277
+ }
20278
+ return entries;
20279
+ },
20280
+ parseIndex: function CFFParser_parseIndex(pos) {
20281
+ var cffIndex = new CFFIndex();
20282
+ var bytes = this.bytes;
20283
+ var count = (bytes[pos++] << 8) | bytes[pos++];
20284
+ var offsets = [];
20285
+ var end = pos;
20286
+ var i, ii;
20287
+
20288
+ if (count !== 0) {
20289
+ var offsetSize = bytes[pos++];
20290
+ // add 1 for offset to determine size of last object
20291
+ var startPos = pos + ((count + 1) * offsetSize) - 1;
20292
+
20293
+ for (i = 0, ii = count + 1; i < ii; ++i) {
20294
+ var offset = 0;
20295
+ for (var j = 0; j < offsetSize; ++j) {
20296
+ offset <<= 8;
20297
+ offset += bytes[pos++];
20298
+ }
20299
+ offsets.push(startPos + offset);
20300
+ }
20301
+ end = offsets[count];
20302
+ }
20303
+ for (i = 0, ii = offsets.length - 1; i < ii; ++i) {
20304
+ var offsetStart = offsets[i];
20305
+ var offsetEnd = offsets[i + 1];
20306
+ cffIndex.add(bytes.subarray(offsetStart, offsetEnd));
20307
+ }
20308
+ return {obj: cffIndex, endPos: end};
20309
+ },
20310
+ parseNameIndex: function CFFParser_parseNameIndex(index) {
20311
+ var names = [];
20312
+ for (var i = 0, ii = index.count; i < ii; ++i) {
20313
+ var name = index.get(i);
20314
+ // OTS doesn't allow names to be over 127 characters.
20315
+ var length = Math.min(name.length, 127);
20316
+ var data = [];
20317
+ // OTS also only permits certain characters in the name.
20318
+ for (var j = 0; j < length; ++j) {
20319
+ var c = name[j];
20320
+ if (j === 0 && c === 0) {
20321
+ data[j] = c;
20322
+ continue;
20323
+ }
20324
+ if ((c < 33 || c > 126) || c === 91 /* [ */ || c === 93 /* ] */ ||
20325
+ c === 40 /* ( */ || c === 41 /* ) */ || c === 123 /* { */ ||
20326
+ c === 125 /* } */ || c === 60 /* < */ || c === 62 /* > */ ||
20327
+ c === 47 /* / */ || c === 37 /* % */ || c === 35 /* # */) {
20328
+ data[j] = 95;
20329
+ continue;
20330
+ }
20331
+ data[j] = c;
20332
+ }
20333
+ names.push(bytesToString(data));
20334
+ }
20335
+ return names;
20336
+ },
20337
+ parseStringIndex: function CFFParser_parseStringIndex(index) {
20338
+ var strings = new CFFStrings();
20339
+ for (var i = 0, ii = index.count; i < ii; ++i) {
20340
+ var data = index.get(i);
20341
+ strings.add(bytesToString(data));
20342
+ }
20343
+ return strings;
20344
+ },
20345
+ createDict: function CFFParser_createDict(Type, dict, strings) {
20346
+ var cffDict = new Type(strings);
20347
+ for (var i = 0, ii = dict.length; i < ii; ++i) {
20348
+ var pair = dict[i];
20349
+ var key = pair[0];
20350
+ var value = pair[1];
20351
+ cffDict.setByKey(key, value);
20352
+ }
20353
+ return cffDict;
20354
+ },
20355
+ parseCharStrings: function CFFParser_parseCharStrings(charStringOffset) {
20356
+ var charStrings = this.parseIndex(charStringOffset).obj;
20357
+ var seacs = [];
20358
+ var widths = [];
20359
+ var count = charStrings.count;
20360
+ for (var i = 0; i < count; i++) {
20361
+ var charstring = charStrings.get(i);
20362
+
20363
+ var stackSize = 0;
20364
+ var stack = [];
20365
+ var undefStack = true;
20366
+ var hints = 0;
20367
+ var valid = true;
20368
+ var data = charstring;
20369
+ var length = data.length;
20370
+ var firstStackClearing = true;
20371
+ for (var j = 0; j < length;) {
20372
+ var value = data[j++];
20373
+ var validationCommand = null;
20374
+ if (value === 12) {
20375
+ var q = data[j++];
20376
+ if (q === 0) {
20377
+ // The CFF specification state that the 'dotsection' command
20378
+ // (12, 0) is deprecated and treated as a no-op, but all Type2
20379
+ // charstrings processors should support them. Unfortunately
20380
+ // the font sanitizer don't. As a workaround the sequence (12, 0)
20381
+ // is replaced by a useless (0, hmoveto).
20382
+ data[j - 2] = 139;
20383
+ data[j - 1] = 22;
20384
+ stackSize = 0;
20385
+ } else {
20386
+ validationCommand = CharstringValidationData12[q];
20387
+ }
20388
+ } else if (value === 28) { // number (16 bit)
20389
+ stack[stackSize] = ((data[j] << 24) | (data[j + 1] << 16)) >> 16;
20390
+ j += 2;
20391
+ stackSize++;
20392
+ } else if (value === 14) {
20393
+ if (stackSize >= 4) {
20394
+ stackSize -= 4;
20395
+ if (SEAC_ANALYSIS_ENABLED) {
20396
+ seacs[i] = stack.slice(stackSize, stackSize + 4);
20397
+ valid = false;
20398
+ }
20399
+ }
20400
+ validationCommand = CharstringValidationData[value];
20401
+ } else if (value >= 32 && value <= 246) { // number
20402
+ stack[stackSize] = value - 139;
20403
+ stackSize++;
20404
+ } else if (value >= 247 && value <= 254) { // number (+1 bytes)
20405
+ stack[stackSize] = (value < 251 ?
20406
+ ((value - 247) << 8) + data[j] + 108 :
20407
+ -((value - 251) << 8) - data[j] - 108);
20408
+ j++;
20409
+ stackSize++;
20410
+ } else if (value === 255) { // number (32 bit)
20411
+ stack[stackSize] = ((data[j] << 24) | (data[j + 1] << 16) |
20412
+ (data[j + 2] << 8) | data[j + 3]) / 65536;
20413
+ j += 4;
20414
+ stackSize++;
20415
+ } else if (value === 19 || value === 20) {
20416
+ hints += stackSize >> 1;
20417
+ j += (hints + 7) >> 3; // skipping right amount of hints flag data
20418
+ stackSize %= 2;
20419
+ validationCommand = CharstringValidationData[value];
20420
+ } else {
20421
+ validationCommand = CharstringValidationData[value];
20422
+ }
20423
+ if (validationCommand) {
20424
+ if (validationCommand.stem) {
20425
+ hints += stackSize >> 1;
20426
+ }
20427
+ if ('min' in validationCommand) {
20428
+ if (!undefStack && stackSize < validationCommand.min) {
20429
+ warn('Not enough parameters for ' + validationCommand.id +
20430
+ '; actual: ' + stackSize +
20431
+ ', expected: ' + validationCommand.min);
20432
+ valid = false;
20433
+ break;
20434
+ }
20435
+ }
20436
+ if (firstStackClearing && validationCommand.stackClearing) {
20437
+ firstStackClearing = false;
20438
+ // the optional character width can be found before the first
20439
+ // stack-clearing command arguments
20440
+ stackSize -= validationCommand.min;
20441
+ if (stackSize >= 2 && validationCommand.stem) {
20442
+ // there are even amount of arguments for stem commands
20443
+ stackSize %= 2;
20444
+ } else if (stackSize > 1) {
20445
+ warn('Found too many parameters for stack-clearing command');
20446
+ }
20447
+ if (stackSize > 0 && stack[stackSize - 1] >= 0) {
20448
+ widths[i] = stack[stackSize - 1];
20449
+ }
20450
+ }
20451
+ if ('stackDelta' in validationCommand) {
20452
+ if ('stackFn' in validationCommand) {
20453
+ validationCommand.stackFn(stack, stackSize);
20454
+ }
20455
+ stackSize += validationCommand.stackDelta;
20456
+ } else if (validationCommand.stackClearing) {
20457
+ stackSize = 0;
20458
+ } else if (validationCommand.resetStack) {
20459
+ stackSize = 0;
20460
+ undefStack = false;
20461
+ } else if (validationCommand.undefStack) {
20462
+ stackSize = 0;
20463
+ undefStack = true;
20464
+ firstStackClearing = false;
20465
+ }
20466
+ }
20467
+ }
20468
+ if (!valid) {
20469
+ // resetting invalid charstring to single 'endchar'
20470
+ charStrings.set(i, new Uint8Array([14]));
20471
+ }
20472
+ }
20473
+ return { charStrings: charStrings, seacs: seacs, widths: widths };
20474
+ },
20475
+ emptyPrivateDictionary:
20476
+ function CFFParser_emptyPrivateDictionary(parentDict) {
20477
+ var privateDict = this.createDict(CFFPrivateDict, [],
20478
+ parentDict.strings);
20479
+ parentDict.setByKey(18, [0, 0]);
20480
+ parentDict.privateDict = privateDict;
20481
+ },
20482
+ parsePrivateDict: function CFFParser_parsePrivateDict(parentDict) {
20483
+ // no private dict, do nothing
20484
+ if (!parentDict.hasName('Private')) {
20485
+ this.emptyPrivateDictionary(parentDict);
20486
+ return;
20487
+ }
20488
+ var privateOffset = parentDict.getByName('Private');
20489
+ // make sure the params are formatted correctly
20490
+ if (!isArray(privateOffset) || privateOffset.length !== 2) {
20491
+ parentDict.removeByName('Private');
20492
+ return;
20493
+ }
20494
+ var size = privateOffset[0];
20495
+ var offset = privateOffset[1];
20496
+ // remove empty dicts or ones that refer to invalid location
20497
+ if (size === 0 || offset >= this.bytes.length) {
20498
+ this.emptyPrivateDictionary(parentDict);
20499
+ return;
20500
+ }
20501
+
20502
+ var privateDictEnd = offset + size;
20503
+ var dictData = this.bytes.subarray(offset, privateDictEnd);
20504
+ var dict = this.parseDict(dictData);
20505
+ var privateDict = this.createDict(CFFPrivateDict, dict,
20506
+ parentDict.strings);
20507
+ parentDict.privateDict = privateDict;
20508
+
20509
+ // Parse the Subrs index also since it's relative to the private dict.
20510
+ if (!privateDict.getByName('Subrs')) {
20511
+ return;
20512
+ }
20513
+ var subrsOffset = privateDict.getByName('Subrs');
20514
+ var relativeOffset = offset + subrsOffset;
20515
+ // Validate the offset.
20516
+ if (subrsOffset === 0 || relativeOffset >= this.bytes.length) {
20517
+ this.emptyPrivateDictionary(parentDict);
20518
+ return;
20519
+ }
20520
+ var subrsIndex = this.parseIndex(relativeOffset);
20521
+ privateDict.subrsIndex = subrsIndex.obj;
20522
+ },
20523
+ parseCharsets: function CFFParser_parseCharsets(pos, length, strings, cid) {
20524
+ if (pos === 0) {
20525
+ return new CFFCharset(true, CFFCharsetPredefinedTypes.ISO_ADOBE,
20526
+ ISOAdobeCharset);
20527
+ } else if (pos === 1) {
20528
+ return new CFFCharset(true, CFFCharsetPredefinedTypes.EXPERT,
20529
+ ExpertCharset);
20530
+ } else if (pos === 2) {
20531
+ return new CFFCharset(true, CFFCharsetPredefinedTypes.EXPERT_SUBSET,
20532
+ ExpertSubsetCharset);
20533
+ }
20534
+
20535
+ var bytes = this.bytes;
20536
+ var start = pos;
20537
+ var format = bytes[pos++];
20538
+ var charset = ['.notdef'];
20539
+ var id, count, i;
20540
+
20541
+ // subtract 1 for the .notdef glyph
20542
+ length -= 1;
20543
+
20544
+ switch (format) {
20545
+ case 0:
20546
+ for (i = 0; i < length; i++) {
20547
+ id = (bytes[pos++] << 8) | bytes[pos++];
20548
+ charset.push(cid ? id : strings.get(id));
20549
+ }
20550
+ break;
20551
+ case 1:
20552
+ while (charset.length <= length) {
20553
+ id = (bytes[pos++] << 8) | bytes[pos++];
20554
+ count = bytes[pos++];
20555
+ for (i = 0; i <= count; i++) {
20556
+ charset.push(cid ? id++ : strings.get(id++));
20557
+ }
20558
+ }
20559
+ break;
20560
+ case 2:
20561
+ while (charset.length <= length) {
20562
+ id = (bytes[pos++] << 8) | bytes[pos++];
20563
+ count = (bytes[pos++] << 8) | bytes[pos++];
20564
+ for (i = 0; i <= count; i++) {
20565
+ charset.push(cid ? id++ : strings.get(id++));
20566
+ }
20567
+ }
20568
+ break;
20569
+ default:
20570
+ error('Unknown charset format');
20571
+ }
20572
+ // Raw won't be needed if we actually compile the charset.
20573
+ var end = pos;
20574
+ var raw = bytes.subarray(start, end);
20575
+
20576
+ return new CFFCharset(false, format, charset, raw);
20577
+ },
20578
+ parseEncoding: function CFFParser_parseEncoding(pos,
20579
+ properties,
20580
+ strings,
20581
+ charset) {
20582
+ var encoding = {};
20583
+ var bytes = this.bytes;
20584
+ var predefined = false;
20585
+ var hasSupplement = false;
20586
+ var format, i, ii;
20587
+ var raw = null;
20588
+
20589
+ function readSupplement() {
20590
+ var supplementsCount = bytes[pos++];
20591
+ for (i = 0; i < supplementsCount; i++) {
20592
+ var code = bytes[pos++];
20593
+ var sid = (bytes[pos++] << 8) + (bytes[pos++] & 0xff);
20594
+ encoding[code] = charset.indexOf(strings.get(sid));
20595
+ }
20596
+ }
20597
+
20598
+ if (pos === 0 || pos === 1) {
20599
+ predefined = true;
20600
+ format = pos;
20601
+ var baseEncoding = pos ? Encodings.ExpertEncoding :
20602
+ Encodings.StandardEncoding;
20603
+ for (i = 0, ii = charset.length; i < ii; i++) {
20604
+ var index = baseEncoding.indexOf(charset[i]);
20605
+ if (index !== -1) {
20606
+ encoding[index] = i;
20607
+ }
20608
+ }
20609
+ } else {
20610
+ var dataStart = pos;
20611
+ format = bytes[pos++];
20612
+ switch (format & 0x7f) {
20613
+ case 0:
20614
+ var glyphsCount = bytes[pos++];
20615
+ for (i = 1; i <= glyphsCount; i++) {
20616
+ encoding[bytes[pos++]] = i;
20617
+ }
20618
+ break;
20619
+
20620
+ case 1:
20621
+ var rangesCount = bytes[pos++];
20622
+ var gid = 1;
20623
+ for (i = 0; i < rangesCount; i++) {
20624
+ var start = bytes[pos++];
20625
+ var left = bytes[pos++];
20626
+ for (var j = start; j <= start + left; j++) {
20627
+ encoding[j] = gid++;
20628
+ }
20629
+ }
20630
+ break;
20631
+
20632
+ default:
20633
+ error('Unknow encoding format: ' + format + ' in CFF');
20634
+ break;
20635
+ }
20636
+ var dataEnd = pos;
20637
+ if (format & 0x80) {
20638
+ // The font sanitizer does not support CFF encoding with a
20639
+ // supplement, since the encoding is not really used to map
20640
+ // between gid to glyph, let's overwrite what is declared in
20641
+ // the top dictionary to let the sanitizer think the font use
20642
+ // StandardEncoding, that's a lie but that's ok.
20643
+ bytes[dataStart] &= 0x7f;
20644
+ readSupplement();
20645
+ hasSupplement = true;
20646
+ }
20647
+ raw = bytes.subarray(dataStart, dataEnd);
20648
+ }
20649
+ format = format & 0x7f;
20650
+ return new CFFEncoding(predefined, format, encoding, raw);
20651
+ },
20652
+ parseFDSelect: function CFFParser_parseFDSelect(pos, length) {
20653
+ var start = pos;
20654
+ var bytes = this.bytes;
20655
+ var format = bytes[pos++];
20656
+ var fdSelect = [];
20657
+ var i;
20658
+
20659
+ switch (format) {
20660
+ case 0:
20661
+ for (i = 0; i < length; ++i) {
20662
+ var id = bytes[pos++];
20663
+ fdSelect.push(id);
20664
+ }
20665
+ break;
20666
+ case 3:
20667
+ var rangesCount = (bytes[pos++] << 8) | bytes[pos++];
20668
+ for (i = 0; i < rangesCount; ++i) {
20669
+ var first = (bytes[pos++] << 8) | bytes[pos++];
20670
+ var fdIndex = bytes[pos++];
20671
+ var next = (bytes[pos] << 8) | bytes[pos + 1];
20672
+ for (var j = first; j < next; ++j) {
20673
+ fdSelect.push(fdIndex);
20674
+ }
20675
+ }
20676
+ // Advance past the sentinel(next).
20677
+ pos += 2;
20678
+ break;
20679
+ default:
20680
+ error('Unknown fdselect format ' + format);
20681
+ break;
20682
+ }
20683
+ var end = pos;
20684
+ return new CFFFDSelect(fdSelect, bytes.subarray(start, end));
20685
+ }
20686
+ };
20687
+ return CFFParser;
20688
+ })();
20689
+
20690
+ // Compact Font Format
20691
+ var CFF = (function CFFClosure() {
20692
+ function CFF() {
20693
+ this.header = null;
20694
+ this.names = [];
20695
+ this.topDict = null;
20696
+ this.strings = new CFFStrings();
20697
+ this.globalSubrIndex = null;
20698
+
20699
+ // The following could really be per font, but since we only have one font
20700
+ // store them here.
20701
+ this.encoding = null;
20702
+ this.charset = null;
20703
+ this.charStrings = null;
20704
+ this.fdArray = [];
20705
+ this.fdSelect = null;
20706
+
20707
+ this.isCIDFont = false;
20708
+ }
20709
+ return CFF;
20710
+ })();
20711
+
20712
+ var CFFHeader = (function CFFHeaderClosure() {
20713
+ function CFFHeader(major, minor, hdrSize, offSize) {
20714
+ this.major = major;
20715
+ this.minor = minor;
20716
+ this.hdrSize = hdrSize;
20717
+ this.offSize = offSize;
20718
+ }
20719
+ return CFFHeader;
20720
+ })();
20721
+
20722
+ var CFFStrings = (function CFFStringsClosure() {
20723
+ function CFFStrings() {
20724
+ this.strings = [];
20725
+ }
20726
+ CFFStrings.prototype = {
20727
+ get: function CFFStrings_get(index) {
20728
+ if (index >= 0 && index <= 390) {
20729
+ return CFFStandardStrings[index];
20730
+ }
20731
+ if (index - 391 <= this.strings.length) {
20732
+ return this.strings[index - 391];
20733
+ }
20734
+ return CFFStandardStrings[0];
20735
+ },
20736
+ add: function CFFStrings_add(value) {
20737
+ this.strings.push(value);
20738
+ },
20739
+ get count() {
20740
+ return this.strings.length;
20741
+ }
20742
+ };
20743
+ return CFFStrings;
20744
+ })();
20745
+
20746
+ var CFFIndex = (function CFFIndexClosure() {
20747
+ function CFFIndex() {
20748
+ this.objects = [];
20749
+ this.length = 0;
20750
+ }
20751
+ CFFIndex.prototype = {
20752
+ add: function CFFIndex_add(data) {
20753
+ this.length += data.length;
20754
+ this.objects.push(data);
20755
+ },
20756
+ set: function CFFIndex_set(index, data) {
20757
+ this.length += data.length - this.objects[index].length;
20758
+ this.objects[index] = data;
20759
+ },
20760
+ get: function CFFIndex_get(index) {
20761
+ return this.objects[index];
20762
+ },
20763
+ get count() {
20764
+ return this.objects.length;
20765
+ }
20766
+ };
20767
+ return CFFIndex;
20768
+ })();
20769
+
20770
+ var CFFDict = (function CFFDictClosure() {
20771
+ function CFFDict(tables, strings) {
20772
+ this.keyToNameMap = tables.keyToNameMap;
20773
+ this.nameToKeyMap = tables.nameToKeyMap;
20774
+ this.defaults = tables.defaults;
20775
+ this.types = tables.types;
20776
+ this.opcodes = tables.opcodes;
20777
+ this.order = tables.order;
20778
+ this.strings = strings;
20779
+ this.values = {};
20780
+ }
20781
+ CFFDict.prototype = {
20782
+ // value should always be an array
20783
+ setByKey: function CFFDict_setByKey(key, value) {
20784
+ if (!(key in this.keyToNameMap)) {
20785
+ return false;
20786
+ }
20787
+ // ignore empty values
20788
+ if (value.length === 0) {
20789
+ return true;
20790
+ }
20791
+ var type = this.types[key];
20792
+ // remove the array wrapping these types of values
20793
+ if (type === 'num' || type === 'sid' || type === 'offset') {
20794
+ value = value[0];
20795
+ }
20796
+ this.values[key] = value;
20797
+ return true;
20798
+ },
20799
+ setByName: function CFFDict_setByName(name, value) {
20800
+ if (!(name in this.nameToKeyMap)) {
20801
+ error('Invalid dictionary name "' + name + '"');
20802
+ }
20803
+ this.values[this.nameToKeyMap[name]] = value;
20804
+ },
20805
+ hasName: function CFFDict_hasName(name) {
20806
+ return this.nameToKeyMap[name] in this.values;
20807
+ },
20808
+ getByName: function CFFDict_getByName(name) {
20809
+ if (!(name in this.nameToKeyMap)) {
20810
+ error('Invalid dictionary name "' + name + '"');
20811
+ }
20812
+ var key = this.nameToKeyMap[name];
20813
+ if (!(key in this.values)) {
20814
+ return this.defaults[key];
20815
+ }
20816
+ return this.values[key];
20817
+ },
20818
+ removeByName: function CFFDict_removeByName(name) {
20819
+ delete this.values[this.nameToKeyMap[name]];
20820
+ }
20821
+ };
20822
+ CFFDict.createTables = function CFFDict_createTables(layout) {
20823
+ var tables = {
20824
+ keyToNameMap: {},
20825
+ nameToKeyMap: {},
20826
+ defaults: {},
20827
+ types: {},
20828
+ opcodes: {},
20829
+ order: []
20830
+ };
20831
+ for (var i = 0, ii = layout.length; i < ii; ++i) {
20832
+ var entry = layout[i];
20833
+ var key = isArray(entry[0]) ? (entry[0][0] << 8) + entry[0][1] : entry[0];
20834
+ tables.keyToNameMap[key] = entry[1];
20835
+ tables.nameToKeyMap[entry[1]] = key;
20836
+ tables.types[key] = entry[2];
20837
+ tables.defaults[key] = entry[3];
20838
+ tables.opcodes[key] = isArray(entry[0]) ? entry[0] : [entry[0]];
20839
+ tables.order.push(key);
20840
+ }
20841
+ return tables;
20842
+ };
20843
+ return CFFDict;
20844
+ })();
20845
+
20846
+ var CFFTopDict = (function CFFTopDictClosure() {
20847
+ var layout = [
20848
+ [[12, 30], 'ROS', ['sid', 'sid', 'num'], null],
20849
+ [[12, 20], 'SyntheticBase', 'num', null],
20850
+ [0, 'version', 'sid', null],
20851
+ [1, 'Notice', 'sid', null],
20852
+ [[12, 0], 'Copyright', 'sid', null],
20853
+ [2, 'FullName', 'sid', null],
20854
+ [3, 'FamilyName', 'sid', null],
20855
+ [4, 'Weight', 'sid', null],
20856
+ [[12, 1], 'isFixedPitch', 'num', 0],
20857
+ [[12, 2], 'ItalicAngle', 'num', 0],
20858
+ [[12, 3], 'UnderlinePosition', 'num', -100],
20859
+ [[12, 4], 'UnderlineThickness', 'num', 50],
20860
+ [[12, 5], 'PaintType', 'num', 0],
20861
+ [[12, 6], 'CharstringType', 'num', 2],
20862
+ [[12, 7], 'FontMatrix', ['num', 'num', 'num', 'num', 'num', 'num'],
20863
+ [0.001, 0, 0, 0.001, 0, 0]],
20864
+ [13, 'UniqueID', 'num', null],
20865
+ [5, 'FontBBox', ['num', 'num', 'num', 'num'], [0, 0, 0, 0]],
20866
+ [[12, 8], 'StrokeWidth', 'num', 0],
20867
+ [14, 'XUID', 'array', null],
20868
+ [15, 'charset', 'offset', 0],
20869
+ [16, 'Encoding', 'offset', 0],
20870
+ [17, 'CharStrings', 'offset', 0],
20871
+ [18, 'Private', ['offset', 'offset'], null],
20872
+ [[12, 21], 'PostScript', 'sid', null],
20873
+ [[12, 22], 'BaseFontName', 'sid', null],
20874
+ [[12, 23], 'BaseFontBlend', 'delta', null],
20875
+ [[12, 31], 'CIDFontVersion', 'num', 0],
20876
+ [[12, 32], 'CIDFontRevision', 'num', 0],
20877
+ [[12, 33], 'CIDFontType', 'num', 0],
20878
+ [[12, 34], 'CIDCount', 'num', 8720],
20879
+ [[12, 35], 'UIDBase', 'num', null],
20880
+ // XXX: CID Fonts on DirectWrite 6.1 only seem to work if FDSelect comes
20881
+ // before FDArray.
20882
+ [[12, 37], 'FDSelect', 'offset', null],
20883
+ [[12, 36], 'FDArray', 'offset', null],
20884
+ [[12, 38], 'FontName', 'sid', null]
20885
+ ];
20886
+ var tables = null;
20887
+ function CFFTopDict(strings) {
20888
+ if (tables === null) {
20889
+ tables = CFFDict.createTables(layout);
20890
+ }
20891
+ CFFDict.call(this, tables, strings);
20892
+ this.privateDict = null;
20893
+ }
20894
+ CFFTopDict.prototype = Object.create(CFFDict.prototype);
20895
+ return CFFTopDict;
20896
+ })();
20897
+
20898
+ var CFFPrivateDict = (function CFFPrivateDictClosure() {
20899
+ var layout = [
20900
+ [6, 'BlueValues', 'delta', null],
20901
+ [7, 'OtherBlues', 'delta', null],
20902
+ [8, 'FamilyBlues', 'delta', null],
20903
+ [9, 'FamilyOtherBlues', 'delta', null],
20904
+ [[12, 9], 'BlueScale', 'num', 0.039625],
20905
+ [[12, 10], 'BlueShift', 'num', 7],
20906
+ [[12, 11], 'BlueFuzz', 'num', 1],
20907
+ [10, 'StdHW', 'num', null],
20908
+ [11, 'StdVW', 'num', null],
20909
+ [[12, 12], 'StemSnapH', 'delta', null],
20910
+ [[12, 13], 'StemSnapV', 'delta', null],
20911
+ [[12, 14], 'ForceBold', 'num', 0],
20912
+ [[12, 17], 'LanguageGroup', 'num', 0],
20913
+ [[12, 18], 'ExpansionFactor', 'num', 0.06],
20914
+ [[12, 19], 'initialRandomSeed', 'num', 0],
20915
+ [20, 'defaultWidthX', 'num', 0],
20916
+ [21, 'nominalWidthX', 'num', 0],
20917
+ [19, 'Subrs', 'offset', null]
20918
+ ];
20919
+ var tables = null;
20920
+ function CFFPrivateDict(strings) {
20921
+ if (tables === null) {
20922
+ tables = CFFDict.createTables(layout);
20923
+ }
20924
+ CFFDict.call(this, tables, strings);
20925
+ this.subrsIndex = null;
20926
+ }
20927
+ CFFPrivateDict.prototype = Object.create(CFFDict.prototype);
20928
+ return CFFPrivateDict;
20929
+ })();
20930
+
20931
+ var CFFCharsetPredefinedTypes = {
20932
+ ISO_ADOBE: 0,
20933
+ EXPERT: 1,
20934
+ EXPERT_SUBSET: 2
20935
+ };
20936
+ var CFFCharset = (function CFFCharsetClosure() {
20937
+ function CFFCharset(predefined, format, charset, raw) {
20938
+ this.predefined = predefined;
20939
+ this.format = format;
20940
+ this.charset = charset;
20941
+ this.raw = raw;
20942
+ }
20943
+ return CFFCharset;
20944
+ })();
20945
+
20946
+ var CFFEncoding = (function CFFEncodingClosure() {
20947
+ function CFFEncoding(predefined, format, encoding, raw) {
20948
+ this.predefined = predefined;
20949
+ this.format = format;
20950
+ this.encoding = encoding;
20951
+ this.raw = raw;
20952
+ }
20953
+ return CFFEncoding;
20954
+ })();
20955
+
20956
+ var CFFFDSelect = (function CFFFDSelectClosure() {
20957
+ function CFFFDSelect(fdSelect, raw) {
20958
+ this.fdSelect = fdSelect;
20959
+ this.raw = raw;
20960
+ }
20961
+ return CFFFDSelect;
20962
+ })();
20963
+
20964
+ // Helper class to keep track of where an offset is within the data and helps
20965
+ // filling in that offset once it's known.
20966
+ var CFFOffsetTracker = (function CFFOffsetTrackerClosure() {
20967
+ function CFFOffsetTracker() {
20968
+ this.offsets = {};
20969
+ }
20970
+ CFFOffsetTracker.prototype = {
20971
+ isTracking: function CFFOffsetTracker_isTracking(key) {
20972
+ return key in this.offsets;
20973
+ },
20974
+ track: function CFFOffsetTracker_track(key, location) {
20975
+ if (key in this.offsets) {
20976
+ error('Already tracking location of ' + key);
20977
+ }
20978
+ this.offsets[key] = location;
20979
+ },
20980
+ offset: function CFFOffsetTracker_offset(value) {
20981
+ for (var key in this.offsets) {
20982
+ this.offsets[key] += value;
20983
+ }
20984
+ },
20985
+ setEntryLocation: function CFFOffsetTracker_setEntryLocation(key,
20986
+ values,
20987
+ output) {
20988
+ if (!(key in this.offsets)) {
20989
+ error('Not tracking location of ' + key);
20990
+ }
20991
+ var data = output.data;
20992
+ var dataOffset = this.offsets[key];
20993
+ var size = 5;
20994
+ for (var i = 0, ii = values.length; i < ii; ++i) {
20995
+ var offset0 = i * size + dataOffset;
20996
+ var offset1 = offset0 + 1;
20997
+ var offset2 = offset0 + 2;
20998
+ var offset3 = offset0 + 3;
20999
+ var offset4 = offset0 + 4;
21000
+ // It's easy to screw up offsets so perform this sanity check.
21001
+ if (data[offset0] !== 0x1d || data[offset1] !== 0 ||
21002
+ data[offset2] !== 0 || data[offset3] !== 0 || data[offset4] !== 0) {
21003
+ error('writing to an offset that is not empty');
21004
+ }
21005
+ var value = values[i];
21006
+ data[offset0] = 0x1d;
21007
+ data[offset1] = (value >> 24) & 0xFF;
21008
+ data[offset2] = (value >> 16) & 0xFF;
21009
+ data[offset3] = (value >> 8) & 0xFF;
21010
+ data[offset4] = value & 0xFF;
21011
+ }
21012
+ }
21013
+ };
21014
+ return CFFOffsetTracker;
21015
+ })();
21016
+
21017
+ // Takes a CFF and converts it to the binary representation.
21018
+ var CFFCompiler = (function CFFCompilerClosure() {
21019
+ function CFFCompiler(cff) {
21020
+ this.cff = cff;
21021
+ }
21022
+ CFFCompiler.prototype = {
21023
+ compile: function CFFCompiler_compile() {
21024
+ var cff = this.cff;
21025
+ var output = {
21026
+ data: [],
21027
+ length: 0,
21028
+ add: function CFFCompiler_add(data) {
21029
+ this.data = this.data.concat(data);
21030
+ this.length = this.data.length;
21031
+ }
21032
+ };
21033
+
21034
+ // Compile the five entries that must be in order.
21035
+ var header = this.compileHeader(cff.header);
21036
+ output.add(header);
21037
+
21038
+ var nameIndex = this.compileNameIndex(cff.names);
21039
+ output.add(nameIndex);
21040
+
21041
+ if (cff.isCIDFont) {
21042
+ // The spec is unclear on how font matrices should relate to each other
21043
+ // when there is one in the main top dict and the sub top dicts.
21044
+ // Windows handles this differently than linux and osx so we have to
21045
+ // normalize to work on all.
21046
+ // Rules based off of some mailing list discussions:
21047
+ // - If main font has a matrix and subfont doesn't, use the main matrix.
21048
+ // - If no main font matrix and there is a subfont matrix, use the
21049
+ // subfont matrix.
21050
+ // - If both have matrices, concat together.
21051
+ // - If neither have matrices, use default.
21052
+ // To make this work on all platforms we move the top matrix into each
21053
+ // sub top dict and concat if necessary.
21054
+ if (cff.topDict.hasName('FontMatrix')) {
21055
+ var base = cff.topDict.getByName('FontMatrix');
21056
+ cff.topDict.removeByName('FontMatrix');
21057
+ for (var i = 0, ii = cff.fdArray.length; i < ii; i++) {
21058
+ var subDict = cff.fdArray[i];
21059
+ var matrix = base.slice(0);
21060
+ if (subDict.hasName('FontMatrix')) {
21061
+ matrix = Util.transform(matrix, subDict.getByName('FontMatrix'));
21062
+ }
21063
+ subDict.setByName('FontMatrix', matrix);
21064
+ }
21065
+ }
21066
+ }
21067
+
21068
+ var compiled = this.compileTopDicts([cff.topDict],
21069
+ output.length,
21070
+ cff.isCIDFont);
21071
+ output.add(compiled.output);
21072
+ var topDictTracker = compiled.trackers[0];
21073
+
21074
+ var stringIndex = this.compileStringIndex(cff.strings.strings);
21075
+ output.add(stringIndex);
21076
+
21077
+ var globalSubrIndex = this.compileIndex(cff.globalSubrIndex);
21078
+ output.add(globalSubrIndex);
21079
+
21080
+ // Now start on the other entries that have no specfic order.
21081
+ if (cff.encoding && cff.topDict.hasName('Encoding')) {
21082
+ if (cff.encoding.predefined) {
21083
+ topDictTracker.setEntryLocation('Encoding', [cff.encoding.format],
21084
+ output);
21085
+ } else {
21086
+ var encoding = this.compileEncoding(cff.encoding);
21087
+ topDictTracker.setEntryLocation('Encoding', [output.length], output);
21088
+ output.add(encoding);
21089
+ }
21090
+ }
21091
+
21092
+ if (cff.charset && cff.topDict.hasName('charset')) {
21093
+ if (cff.charset.predefined) {
21094
+ topDictTracker.setEntryLocation('charset', [cff.charset.format],
21095
+ output);
21096
+ } else {
21097
+ var charset = this.compileCharset(cff.charset);
21098
+ topDictTracker.setEntryLocation('charset', [output.length], output);
21099
+ output.add(charset);
21100
+ }
21101
+ }
21102
+
21103
+ var charStrings = this.compileCharStrings(cff.charStrings);
21104
+ topDictTracker.setEntryLocation('CharStrings', [output.length], output);
21105
+ output.add(charStrings);
21106
+
21107
+ if (cff.isCIDFont) {
21108
+ // For some reason FDSelect must be in front of FDArray on windows. OSX
21109
+ // and linux don't seem to care.
21110
+ topDictTracker.setEntryLocation('FDSelect', [output.length], output);
21111
+ var fdSelect = this.compileFDSelect(cff.fdSelect.raw);
21112
+ output.add(fdSelect);
21113
+ // It is unclear if the sub font dictionary can have CID related
21114
+ // dictionary keys, but the sanitizer doesn't like them so remove them.
21115
+ compiled = this.compileTopDicts(cff.fdArray, output.length, true);
21116
+ topDictTracker.setEntryLocation('FDArray', [output.length], output);
21117
+ output.add(compiled.output);
21118
+ var fontDictTrackers = compiled.trackers;
21119
+
21120
+ this.compilePrivateDicts(cff.fdArray, fontDictTrackers, output);
21121
+ }
21122
+
21123
+ this.compilePrivateDicts([cff.topDict], [topDictTracker], output);
21124
+
21125
+ // If the font data ends with INDEX whose object data is zero-length,
21126
+ // the sanitizer will bail out. Add a dummy byte to avoid that.
21127
+ output.add([0]);
21128
+
21129
+ return output.data;
21130
+ },
21131
+ encodeNumber: function CFFCompiler_encodeNumber(value) {
21132
+ if (parseFloat(value) === parseInt(value, 10) && !isNaN(value)) { // isInt
21133
+ return this.encodeInteger(value);
21134
+ } else {
21135
+ return this.encodeFloat(value);
21136
+ }
21137
+ },
21138
+ encodeFloat: function CFFCompiler_encodeFloat(num) {
21139
+ var value = num.toString();
21140
+
21141
+ // rounding inaccurate doubles
21142
+ var m = /\.(\d*?)(?:9{5,20}|0{5,20})\d{0,2}(?:e(.+)|$)/.exec(value);
21143
+ if (m) {
21144
+ var epsilon = parseFloat('1e' + ((m[2] ? +m[2] : 0) + m[1].length));
21145
+ value = (Math.round(num * epsilon) / epsilon).toString();
21146
+ }
21147
+
21148
+ var nibbles = '';
21149
+ var i, ii;
21150
+ for (i = 0, ii = value.length; i < ii; ++i) {
21151
+ var a = value[i];
21152
+ if (a === 'e') {
21153
+ nibbles += value[++i] === '-' ? 'c' : 'b';
21154
+ } else if (a === '.') {
21155
+ nibbles += 'a';
21156
+ } else if (a === '-') {
21157
+ nibbles += 'e';
21158
+ } else {
21159
+ nibbles += a;
21160
+ }
21161
+ }
21162
+ nibbles += (nibbles.length & 1) ? 'f' : 'ff';
21163
+ var out = [30];
21164
+ for (i = 0, ii = nibbles.length; i < ii; i += 2) {
21165
+ out.push(parseInt(nibbles.substr(i, 2), 16));
21166
+ }
21167
+ return out;
21168
+ },
21169
+ encodeInteger: function CFFCompiler_encodeInteger(value) {
21170
+ var code;
21171
+ if (value >= -107 && value <= 107) {
21172
+ code = [value + 139];
21173
+ } else if (value >= 108 && value <= 1131) {
21174
+ value = [value - 108];
21175
+ code = [(value >> 8) + 247, value & 0xFF];
21176
+ } else if (value >= -1131 && value <= -108) {
21177
+ value = -value - 108;
21178
+ code = [(value >> 8) + 251, value & 0xFF];
21179
+ } else if (value >= -32768 && value <= 32767) {
21180
+ code = [0x1c, (value >> 8) & 0xFF, value & 0xFF];
21181
+ } else {
21182
+ code = [0x1d,
21183
+ (value >> 24) & 0xFF,
21184
+ (value >> 16) & 0xFF,
21185
+ (value >> 8) & 0xFF,
21186
+ value & 0xFF];
21187
+ }
21188
+ return code;
21189
+ },
21190
+ compileHeader: function CFFCompiler_compileHeader(header) {
21191
+ return [
21192
+ header.major,
21193
+ header.minor,
21194
+ header.hdrSize,
21195
+ header.offSize
21196
+ ];
21197
+ },
21198
+ compileNameIndex: function CFFCompiler_compileNameIndex(names) {
21199
+ var nameIndex = new CFFIndex();
21200
+ for (var i = 0, ii = names.length; i < ii; ++i) {
21201
+ nameIndex.add(stringToBytes(names[i]));
21202
+ }
21203
+ return this.compileIndex(nameIndex);
21204
+ },
21205
+ compileTopDicts: function CFFCompiler_compileTopDicts(dicts,
21206
+ length,
21207
+ removeCidKeys) {
21208
+ var fontDictTrackers = [];
21209
+ var fdArrayIndex = new CFFIndex();
21210
+ for (var i = 0, ii = dicts.length; i < ii; ++i) {
21211
+ var fontDict = dicts[i];
21212
+ if (removeCidKeys) {
21213
+ fontDict.removeByName('CIDFontVersion');
21214
+ fontDict.removeByName('CIDFontRevision');
21215
+ fontDict.removeByName('CIDFontType');
21216
+ fontDict.removeByName('CIDCount');
21217
+ fontDict.removeByName('UIDBase');
21218
+ }
21219
+ var fontDictTracker = new CFFOffsetTracker();
21220
+ var fontDictData = this.compileDict(fontDict, fontDictTracker);
21221
+ fontDictTrackers.push(fontDictTracker);
21222
+ fdArrayIndex.add(fontDictData);
21223
+ fontDictTracker.offset(length);
21224
+ }
21225
+ fdArrayIndex = this.compileIndex(fdArrayIndex, fontDictTrackers);
21226
+ return {
21227
+ trackers: fontDictTrackers,
21228
+ output: fdArrayIndex
21229
+ };
21230
+ },
21231
+ compilePrivateDicts: function CFFCompiler_compilePrivateDicts(dicts,
21232
+ trackers,
21233
+ output) {
21234
+ for (var i = 0, ii = dicts.length; i < ii; ++i) {
21235
+ var fontDict = dicts[i];
21236
+ assert(fontDict.privateDict && fontDict.hasName('Private'),
21237
+ 'There must be an private dictionary.');
21238
+ var privateDict = fontDict.privateDict;
21239
+ var privateDictTracker = new CFFOffsetTracker();
21240
+ var privateDictData = this.compileDict(privateDict, privateDictTracker);
21241
+
21242
+ var outputLength = output.length;
21243
+ privateDictTracker.offset(outputLength);
21244
+ if (!privateDictData.length) {
21245
+ // The private dictionary was empty, set the output length to zero to
21246
+ // ensure the offset length isn't out of bounds in the eyes of the
21247
+ // sanitizer.
21248
+ outputLength = 0;
21249
+ }
21250
+
21251
+ trackers[i].setEntryLocation('Private',
21252
+ [privateDictData.length, outputLength],
21253
+ output);
21254
+ output.add(privateDictData);
21255
+
21256
+ if (privateDict.subrsIndex && privateDict.hasName('Subrs')) {
21257
+ var subrs = this.compileIndex(privateDict.subrsIndex);
21258
+ privateDictTracker.setEntryLocation('Subrs', [privateDictData.length],
21259
+ output);
21260
+ output.add(subrs);
21261
+ }
21262
+ }
21263
+ },
21264
+ compileDict: function CFFCompiler_compileDict(dict, offsetTracker) {
21265
+ var out = [];
21266
+ // The dictionary keys must be in a certain order.
21267
+ var order = dict.order;
21268
+ for (var i = 0; i < order.length; ++i) {
21269
+ var key = order[i];
21270
+ if (!(key in dict.values)) {
21271
+ continue;
21272
+ }
21273
+ var values = dict.values[key];
21274
+ var types = dict.types[key];
21275
+ if (!isArray(types)) {
21276
+ types = [types];
21277
+ }
21278
+ if (!isArray(values)) {
21279
+ values = [values];
21280
+ }
21281
+
21282
+ // Remove any empty dict values.
21283
+ if (values.length === 0) {
21284
+ continue;
21285
+ }
21286
+
21287
+ for (var j = 0, jj = types.length; j < jj; ++j) {
21288
+ var type = types[j];
21289
+ var value = values[j];
21290
+ switch (type) {
21291
+ case 'num':
21292
+ case 'sid':
21293
+ out = out.concat(this.encodeNumber(value));
21294
+ break;
21295
+ case 'offset':
21296
+ // For offsets we just insert a 32bit integer so we don't have to
21297
+ // deal with figuring out the length of the offset when it gets
21298
+ // replaced later on by the compiler.
21299
+ var name = dict.keyToNameMap[key];
21300
+ // Some offsets have the offset and the length, so just record the
21301
+ // position of the first one.
21302
+ if (!offsetTracker.isTracking(name)) {
21303
+ offsetTracker.track(name, out.length);
21304
+ }
21305
+ out = out.concat([0x1d, 0, 0, 0, 0]);
21306
+ break;
21307
+ case 'array':
21308
+ case 'delta':
21309
+ out = out.concat(this.encodeNumber(value));
21310
+ for (var k = 1, kk = values.length; k < kk; ++k) {
21311
+ out = out.concat(this.encodeNumber(values[k]));
21312
+ }
21313
+ break;
21314
+ default:
21315
+ error('Unknown data type of ' + type);
21316
+ break;
21317
+ }
21318
+ }
21319
+ out = out.concat(dict.opcodes[key]);
21320
+ }
21321
+ return out;
21322
+ },
21323
+ compileStringIndex: function CFFCompiler_compileStringIndex(strings) {
21324
+ var stringIndex = new CFFIndex();
21325
+ for (var i = 0, ii = strings.length; i < ii; ++i) {
21326
+ stringIndex.add(stringToBytes(strings[i]));
21327
+ }
21328
+ return this.compileIndex(stringIndex);
21329
+ },
21330
+ compileGlobalSubrIndex: function CFFCompiler_compileGlobalSubrIndex() {
21331
+ var globalSubrIndex = this.cff.globalSubrIndex;
21332
+ this.out.writeByteArray(this.compileIndex(globalSubrIndex));
21333
+ },
21334
+ compileCharStrings: function CFFCompiler_compileCharStrings(charStrings) {
21335
+ return this.compileIndex(charStrings);
21336
+ },
21337
+ compileCharset: function CFFCompiler_compileCharset(charset) {
21338
+ return this.compileTypedArray(charset.raw);
21339
+ },
21340
+ compileEncoding: function CFFCompiler_compileEncoding(encoding) {
21341
+ return this.compileTypedArray(encoding.raw);
21342
+ },
21343
+ compileFDSelect: function CFFCompiler_compileFDSelect(fdSelect) {
21344
+ return this.compileTypedArray(fdSelect);
21345
+ },
21346
+ compileTypedArray: function CFFCompiler_compileTypedArray(data) {
21347
+ var out = [];
21348
+ for (var i = 0, ii = data.length; i < ii; ++i) {
21349
+ out[i] = data[i];
21350
+ }
21351
+ return out;
21352
+ },
21353
+ compileIndex: function CFFCompiler_compileIndex(index, trackers) {
21354
+ trackers = trackers || [];
21355
+ var objects = index.objects;
21356
+ // First 2 bytes contains the number of objects contained into this index
21357
+ var count = objects.length;
21358
+
21359
+ // If there is no object, just create an index. This technically
21360
+ // should just be [0, 0] but OTS has an issue with that.
21361
+ if (count === 0) {
21362
+ return [0, 0, 0];
21363
+ }
21364
+
21365
+ var data = [(count >> 8) & 0xFF, count & 0xff];
21366
+
21367
+ var lastOffset = 1, i;
21368
+ for (i = 0; i < count; ++i) {
21369
+ lastOffset += objects[i].length;
21370
+ }
21371
+
21372
+ var offsetSize;
21373
+ if (lastOffset < 0x100) {
21374
+ offsetSize = 1;
21375
+ } else if (lastOffset < 0x10000) {
21376
+ offsetSize = 2;
21377
+ } else if (lastOffset < 0x1000000) {
21378
+ offsetSize = 3;
21379
+ } else {
21380
+ offsetSize = 4;
21381
+ }
21382
+
21383
+ // Next byte contains the offset size use to reference object in the file
21384
+ data.push(offsetSize);
21385
+
21386
+ // Add another offset after this one because we need a new offset
21387
+ var relativeOffset = 1;
21388
+ for (i = 0; i < count + 1; i++) {
21389
+ if (offsetSize === 1) {
21390
+ data.push(relativeOffset & 0xFF);
21391
+ } else if (offsetSize === 2) {
21392
+ data.push((relativeOffset >> 8) & 0xFF,
21393
+ relativeOffset & 0xFF);
21394
+ } else if (offsetSize === 3) {
21395
+ data.push((relativeOffset >> 16) & 0xFF,
21396
+ (relativeOffset >> 8) & 0xFF,
21397
+ relativeOffset & 0xFF);
21398
+ } else {
21399
+ data.push((relativeOffset >>> 24) & 0xFF,
21400
+ (relativeOffset >> 16) & 0xFF,
21401
+ (relativeOffset >> 8) & 0xFF,
21402
+ relativeOffset & 0xFF);
21403
+ }
21404
+
21405
+ if (objects[i]) {
21406
+ relativeOffset += objects[i].length;
21407
+ }
21408
+ }
21409
+
21410
+ for (i = 0; i < count; i++) {
21411
+ // Notify the tracker where the object will be offset in the data.
21412
+ if (trackers[i]) {
21413
+ trackers[i].offset(data.length);
21414
+ }
21415
+ for (var j = 0, jj = objects[i].length; j < jj; j++) {
21416
+ data.push(objects[i][j]);
21417
+ }
21418
+ }
21419
+ return data;
21420
+ }
21421
+ };
21422
+ return CFFCompiler;
21423
+ })();
21424
+
21425
+ // Workaround for seac on Windows.
21426
+ (function checkSeacSupport() {
21427
+ if (/Windows/.test(navigator.userAgent)) {
21428
+ SEAC_ANALYSIS_ENABLED = true;
21429
+ }
21430
+ })();
21431
+
21432
+ // Workaround for Private Use Area characters in Chrome on Windows
21433
+ // http://code.google.com/p/chromium/issues/detail?id=122465
21434
+ // https://github.com/mozilla/pdf.js/issues/1689
21435
+ (function checkChromeWindows() {
21436
+ if (/Windows.*Chrome/.test(navigator.userAgent)) {
21437
+ SKIP_PRIVATE_USE_RANGE_F000_TO_F01F = true;
21438
+ }
21439
+ })();
21440
+
21441
+
21442
+ var FontRendererFactory = (function FontRendererFactoryClosure() {
21443
+ function getLong(data, offset) {
21444
+ return (data[offset] << 24) | (data[offset + 1] << 16) |
21445
+ (data[offset + 2] << 8) | data[offset + 3];
21446
+ }
21447
+
21448
+ function getUshort(data, offset) {
21449
+ return (data[offset] << 8) | data[offset + 1];
21450
+ }
21451
+
21452
+ function parseCmap(data, start, end) {
21453
+ var offset = (getUshort(data, start + 2) === 1 ?
21454
+ getLong(data, start + 8) : getLong(data, start + 16));
21455
+ var format = getUshort(data, start + offset);
21456
+ var length, ranges, p, i;
21457
+ if (format === 4) {
21458
+ length = getUshort(data, start + offset + 2);
21459
+ var segCount = getUshort(data, start + offset + 6) >> 1;
21460
+ p = start + offset + 14;
21461
+ ranges = [];
21462
+ for (i = 0; i < segCount; i++, p += 2) {
21463
+ ranges[i] = {end: getUshort(data, p)};
21464
+ }
21465
+ p += 2;
21466
+ for (i = 0; i < segCount; i++, p += 2) {
21467
+ ranges[i].start = getUshort(data, p);
21468
+ }
21469
+ for (i = 0; i < segCount; i++, p += 2) {
21470
+ ranges[i].idDelta = getUshort(data, p);
21471
+ }
21472
+ for (i = 0; i < segCount; i++, p += 2) {
21473
+ var idOffset = getUshort(data, p);
21474
+ if (idOffset === 0) {
21475
+ continue;
21476
+ }
21477
+ ranges[i].ids = [];
21478
+ for (var j = 0, jj = ranges[i].end - ranges[i].start + 1; j < jj; j++) {
21479
+ ranges[i].ids[j] = getUshort(data, p + idOffset);
21480
+ idOffset += 2;
21481
+ }
21482
+ }
21483
+ return ranges;
21484
+ } else if (format === 12) {
21485
+ length = getLong(data, start + offset + 4);
21486
+ var groups = getLong(data, start + offset + 12);
21487
+ p = start + offset + 16;
21488
+ ranges = [];
21489
+ for (i = 0; i < groups; i++) {
21490
+ ranges.push({
21491
+ start: getLong(data, p),
21492
+ end: getLong(data, p + 4),
21493
+ idDelta: getLong(data, p + 8) - getLong(data, p)
21494
+ });
21495
+ p += 12;
21496
+ }
21497
+ return ranges;
21498
+ }
21499
+ error('not supported cmap: ' + format);
21500
+ }
21501
+
21502
+ function parseCff(data, start, end) {
21503
+ var properties = {};
21504
+ var parser = new CFFParser(new Stream(data, start, end - start),
21505
+ properties);
21506
+ var cff = parser.parse();
21507
+ return {
21508
+ glyphs: cff.charStrings.objects,
21509
+ subrs: (cff.topDict.privateDict && cff.topDict.privateDict.subrsIndex &&
21510
+ cff.topDict.privateDict.subrsIndex.objects),
21511
+ gsubrs: cff.globalSubrIndex && cff.globalSubrIndex.objects
21512
+ };
21513
+ }
21514
+
21515
+ function parseGlyfTable(glyf, loca, isGlyphLocationsLong) {
21516
+ var itemSize, itemDecode;
21517
+ if (isGlyphLocationsLong) {
21518
+ itemSize = 4;
21519
+ itemDecode = function fontItemDecodeLong(data, offset) {
21520
+ return (data[offset] << 24) | (data[offset + 1] << 16) |
21521
+ (data[offset + 2] << 8) | data[offset + 3];
21522
+ };
21523
+ } else {
21524
+ itemSize = 2;
21525
+ itemDecode = function fontItemDecode(data, offset) {
21526
+ return (data[offset] << 9) | (data[offset + 1] << 1);
21527
+ };
21528
+ }
21529
+ var glyphs = [];
21530
+ var startOffset = itemDecode(loca, 0);
21531
+ for (var j = itemSize; j < loca.length; j += itemSize) {
21532
+ var endOffset = itemDecode(loca, j);
21533
+ glyphs.push(glyf.subarray(startOffset, endOffset));
21534
+ startOffset = endOffset;
21535
+ }
21536
+ return glyphs;
21537
+ }
21538
+
21539
+ function lookupCmap(ranges, unicode) {
21540
+ var code = unicode.charCodeAt(0);
21541
+ var l = 0, r = ranges.length - 1;
21542
+ while (l < r) {
21543
+ var c = (l + r + 1) >> 1;
21544
+ if (code < ranges[c].start) {
21545
+ r = c - 1;
21546
+ } else {
21547
+ l = c;
21548
+ }
21549
+ }
21550
+ if (ranges[l].start <= code && code <= ranges[l].end) {
21551
+ return (ranges[l].idDelta + (ranges[l].ids ?
21552
+ ranges[l].ids[code - ranges[l].start] : code)) & 0xFFFF;
21553
+ }
21554
+ return 0;
21555
+ }
21556
+
21557
+ function compileGlyf(code, js, font) {
21558
+ function moveTo(x, y) {
21559
+ js.push('c.moveTo(' + x + ',' + y + ');');
21560
+ }
21561
+ function lineTo(x, y) {
21562
+ js.push('c.lineTo(' + x + ',' + y + ');');
21563
+ }
21564
+ function quadraticCurveTo(xa, ya, x, y) {
21565
+ js.push('c.quadraticCurveTo(' + xa + ',' + ya + ',' +
21566
+ x + ',' + y + ');');
21567
+ }
21568
+
21569
+ var i = 0;
21570
+ var numberOfContours = ((code[i] << 24) | (code[i + 1] << 16)) >> 16;
21571
+ var flags;
21572
+ var x = 0, y = 0;
21573
+ i += 10;
21574
+ if (numberOfContours < 0) {
21575
+ // composite glyph
21576
+ do {
21577
+ flags = (code[i] << 8) | code[i + 1];
21578
+ var glyphIndex = (code[i + 2] << 8) | code[i + 3];
21579
+ i += 4;
21580
+ var arg1, arg2;
21581
+ if ((flags & 0x01)) {
21582
+ arg1 = ((code[i] << 24) | (code[i + 1] << 16)) >> 16;
21583
+ arg2 = ((code[i + 2] << 24) | (code[i + 3] << 16)) >> 16;
21584
+ i += 4;
21585
+ } else {
21586
+ arg1 = code[i++]; arg2 = code[i++];
21587
+ }
21588
+ if ((flags & 0x02)) {
21589
+ x = arg1;
21590
+ y = arg2;
21591
+ } else {
21592
+ x = 0; y = 0; // TODO "they are points" ?
21593
+ }
21594
+ var scaleX = 1, scaleY = 1, scale01 = 0, scale10 = 0;
21595
+ if ((flags & 0x08)) {
21596
+ scaleX =
21597
+ scaleY = ((code[i] << 24) | (code[i + 1] << 16)) / 1073741824;
21598
+ i += 2;
21599
+ } else if ((flags & 0x40)) {
21600
+ scaleX = ((code[i] << 24) | (code[i + 1] << 16)) / 1073741824;
21601
+ scaleY = ((code[i + 2] << 24) | (code[i + 3] << 16)) / 1073741824;
21602
+ i += 4;
21603
+ } else if ((flags & 0x80)) {
21604
+ scaleX = ((code[i] << 24) | (code[i + 1] << 16)) / 1073741824;
21605
+ scale01 = ((code[i + 2] << 24) | (code[i + 3] << 16)) / 1073741824;
21606
+ scale10 = ((code[i + 4] << 24) | (code[i + 5] << 16)) / 1073741824;
21607
+ scaleY = ((code[i + 6] << 24) | (code[i + 7] << 16)) / 1073741824;
21608
+ i += 8;
21609
+ }
21610
+ var subglyph = font.glyphs[glyphIndex];
21611
+ if (subglyph) {
21612
+ js.push('c.save();');
21613
+ js.push('c.transform(' + scaleX + ',' + scale01 + ',' +
21614
+ scale10 + ',' + scaleY + ',' + x + ',' + y + ');');
21615
+ compileGlyf(subglyph, js, font);
21616
+ js.push('c.restore();');
21617
+ }
21618
+ } while ((flags & 0x20));
21619
+ } else {
21620
+ // simple glyph
21621
+ var endPtsOfContours = [];
21622
+ var j, jj;
21623
+ for (j = 0; j < numberOfContours; j++) {
21624
+ endPtsOfContours.push((code[i] << 8) | code[i + 1]);
21625
+ i += 2;
21626
+ }
21627
+ var instructionLength = (code[i] << 8) | code[i + 1];
21628
+ i += 2 + instructionLength; // skipping the instructions
21629
+ var numberOfPoints = endPtsOfContours[endPtsOfContours.length - 1] + 1;
21630
+ var points = [];
21631
+ while (points.length < numberOfPoints) {
21632
+ flags = code[i++];
21633
+ var repeat = 1;
21634
+ if ((flags & 0x08)) {
21635
+ repeat += code[i++];
21636
+ }
21637
+ while (repeat-- > 0) {
21638
+ points.push({flags: flags});
21639
+ }
21640
+ }
21641
+ for (j = 0; j < numberOfPoints; j++) {
21642
+ switch (points[j].flags & 0x12) {
21643
+ case 0x00:
21644
+ x += ((code[i] << 24) | (code[i + 1] << 16)) >> 16;
21645
+ i += 2;
21646
+ break;
21647
+ case 0x02:
21648
+ x -= code[i++];
21649
+ break;
21650
+ case 0x12:
21651
+ x += code[i++];
21652
+ break;
21653
+ }
21654
+ points[j].x = x;
21655
+ }
21656
+ for (j = 0; j < numberOfPoints; j++) {
21657
+ switch (points[j].flags & 0x24) {
21658
+ case 0x00:
21659
+ y += ((code[i] << 24) | (code[i + 1] << 16)) >> 16;
21660
+ i += 2;
21661
+ break;
21662
+ case 0x04:
21663
+ y -= code[i++];
21664
+ break;
21665
+ case 0x24:
21666
+ y += code[i++];
21667
+ break;
21668
+ }
21669
+ points[j].y = y;
21670
+ }
21671
+
21672
+ var startPoint = 0;
21673
+ for (i = 0; i < numberOfContours; i++) {
21674
+ var endPoint = endPtsOfContours[i];
21675
+ // contours might have implicit points, which is located in the middle
21676
+ // between two neighboring off-curve points
21677
+ var contour = points.slice(startPoint, endPoint + 1);
21678
+ if ((contour[0].flags & 1)) {
21679
+ contour.push(contour[0]); // using start point at the contour end
21680
+ } else if ((contour[contour.length - 1].flags & 1)) {
21681
+ // first is off-curve point, trying to use one from the end
21682
+ contour.unshift(contour[contour.length - 1]);
21683
+ } else {
21684
+ // start and end are off-curve points, creating implicit one
21685
+ var p = {
21686
+ flags: 1,
21687
+ x: (contour[0].x + contour[contour.length - 1].x) / 2,
21688
+ y: (contour[0].y + contour[contour.length - 1].y) / 2
21689
+ };
21690
+ contour.unshift(p);
21691
+ contour.push(p);
21692
+ }
21693
+ moveTo(contour[0].x, contour[0].y);
21694
+ for (j = 1, jj = contour.length; j < jj; j++) {
21695
+ if ((contour[j].flags & 1)) {
21696
+ lineTo(contour[j].x, contour[j].y);
21697
+ } else if ((contour[j + 1].flags & 1)){
21698
+ quadraticCurveTo(contour[j].x, contour[j].y,
21699
+ contour[j + 1].x, contour[j + 1].y);
21700
+ j++;
21701
+ } else {
21702
+ quadraticCurveTo(contour[j].x, contour[j].y,
21703
+ (contour[j].x + contour[j + 1].x) / 2,
21704
+ (contour[j].y + contour[j + 1].y) / 2);
21705
+ }
21706
+ }
21707
+ startPoint = endPoint + 1;
21708
+ }
21709
+ }
21710
+ }
21711
+
21712
+ function compileCharString(code, js, font) {
21713
+ var stack = [];
21714
+ var x = 0, y = 0;
21715
+ var stems = 0;
21716
+
21717
+ function moveTo(x, y) {
21718
+ js.push('c.moveTo(' + x + ',' + y + ');');
21719
+ }
21720
+ function lineTo(x, y) {
21721
+ js.push('c.lineTo(' + x + ',' + y + ');');
21722
+ }
21723
+ function bezierCurveTo(x1, y1, x2, y2, x, y) {
21724
+ js.push('c.bezierCurveTo(' + x1 + ',' + y1 + ',' + x2 + ',' + y2 + ',' +
21725
+ x + ',' + y + ');');
21726
+ }
21727
+
21728
+ function parse(code) {
21729
+ var i = 0;
21730
+ while (i < code.length) {
21731
+ var stackClean = false;
21732
+ var v = code[i++];
21733
+ var xa, xb, ya, yb, y1, y2, y3, n, subrCode;
21734
+ switch (v) {
21735
+ case 1: // hstem
21736
+ stems += stack.length >> 1;
21737
+ stackClean = true;
21738
+ break;
21739
+ case 3: // vstem
21740
+ stems += stack.length >> 1;
21741
+ stackClean = true;
21742
+ break;
21743
+ case 4: // vmoveto
21744
+ y += stack.pop();
21745
+ moveTo(x, y);
21746
+ stackClean = true;
21747
+ break;
21748
+ case 5: // rlineto
21749
+ while (stack.length > 0) {
21750
+ x += stack.shift();
21751
+ y += stack.shift();
21752
+ lineTo(x, y);
21753
+ }
21754
+ break;
21755
+ case 6: // hlineto
21756
+ while (stack.length > 0) {
21757
+ x += stack.shift();
21758
+ lineTo(x, y);
21759
+ if (stack.length === 0) {
21760
+ break;
21761
+ }
21762
+ y += stack.shift();
21763
+ lineTo(x, y);
21764
+ }
21765
+ break;
21766
+ case 7: // vlineto
21767
+ while (stack.length > 0) {
21768
+ y += stack.shift();
21769
+ lineTo(x, y);
21770
+ if (stack.length === 0) {
21771
+ break;
21772
+ }
21773
+ x += stack.shift();
21774
+ lineTo(x, y);
21775
+ }
21776
+ break;
21777
+ case 8: // rrcurveto
21778
+ while (stack.length > 0) {
21779
+ xa = x + stack.shift(); ya = y + stack.shift();
21780
+ xb = xa + stack.shift(); yb = ya + stack.shift();
21781
+ x = xb + stack.shift(); y = yb + stack.shift();
21782
+ bezierCurveTo(xa, ya, xb, yb, x, y);
21783
+ }
21784
+ break;
21785
+ case 10: // callsubr
21786
+ n = stack.pop() + font.subrsBias;
21787
+ subrCode = font.subrs[n];
21788
+ if (subrCode) {
21789
+ parse(subrCode);
21790
+ }
21791
+ break;
21792
+ case 11: // return
21793
+ return;
21794
+ case 12:
21795
+ v = code[i++];
21796
+ switch (v) {
21797
+ case 34: // flex
21798
+ xa = x + stack.shift();
21799
+ xb = xa + stack.shift(); y1 = y + stack.shift();
21800
+ x = xb + stack.shift();
21801
+ bezierCurveTo(xa, y, xb, y1, x, y1);
21802
+ xa = x + stack.shift();
21803
+ xb = xa + stack.shift();
21804
+ x = xb + stack.shift();
21805
+ bezierCurveTo(xa, y1, xb, y, x, y);
21806
+ break;
21807
+ case 35: // flex
21808
+ xa = x + stack.shift(); ya = y + stack.shift();
21809
+ xb = xa + stack.shift(); yb = ya + stack.shift();
21810
+ x = xb + stack.shift(); y = yb + stack.shift();
21811
+ bezierCurveTo(xa, ya, xb, yb, x, y);
21812
+ xa = x + stack.shift(); ya = y + stack.shift();
21813
+ xb = xa + stack.shift(); yb = ya + stack.shift();
21814
+ x = xb + stack.shift(); y = yb + stack.shift();
21815
+ bezierCurveTo(xa, ya, xb, yb, x, y);
21816
+ stack.pop(); // fd
21817
+ break;
21818
+ case 36: // hflex1
21819
+ xa = x + stack.shift(); y1 = y + stack.shift();
21820
+ xb = xa + stack.shift(); y2 = y1 + stack.shift();
21821
+ x = xb + stack.shift();
21822
+ bezierCurveTo(xa, y1, xb, y2, x, y2);
21823
+ xa = x + stack.shift();
21824
+ xb = xa + stack.shift(); y3 = y2 + stack.shift();
21825
+ x = xb + stack.shift();
21826
+ bezierCurveTo(xa, y2, xb, y3, x, y);
21827
+ break;
21828
+ case 37: // flex1
21829
+ var x0 = x, y0 = y;
21830
+ xa = x + stack.shift(); ya = y + stack.shift();
21831
+ xb = xa + stack.shift(); yb = ya + stack.shift();
21832
+ x = xb + stack.shift(); y = yb + stack.shift();
21833
+ bezierCurveTo(xa, ya, xb, yb, x, y);
21834
+ xa = x + stack.shift(); ya = y + stack.shift();
21835
+ xb = xa + stack.shift(); yb = ya + stack.shift();
21836
+ x = xb; y = yb;
21837
+ if (Math.abs(x - x0) > Math.abs(y - y0)) {
21838
+ x += stack.shift();
21839
+ } else {
21840
+ y += stack.shift();
21841
+ }
21842
+ bezierCurveTo(xa, ya, xb, yb, x, y);
21843
+ break;
21844
+ default:
21845
+ error('unknown operator: 12 ' + v);
21846
+ }
21847
+ break;
21848
+ case 14: // endchar
21849
+ if (stack.length >= 4) {
21850
+ var achar = stack.pop();
21851
+ var bchar = stack.pop();
21852
+ y = stack.pop();
21853
+ x = stack.pop();
21854
+ js.push('c.save();');
21855
+ js.push('c.translate('+ x + ',' + y + ');');
21856
+ var gid = lookupCmap(font.cmap, String.fromCharCode(
21857
+ font.glyphNameMap[Encodings.StandardEncoding[achar]]));
21858
+ compileCharString(font.glyphs[gid], js, font);
21859
+ js.push('c.restore();');
21860
+
21861
+ gid = lookupCmap(font.cmap, String.fromCharCode(
21862
+ font.glyphNameMap[Encodings.StandardEncoding[bchar]]));
21863
+ compileCharString(font.glyphs[gid], js, font);
21864
+ }
21865
+ return;
21866
+ case 18: // hstemhm
21867
+ stems += stack.length >> 1;
21868
+ stackClean = true;
21869
+ break;
21870
+ case 19: // hintmask
21871
+ stems += stack.length >> 1;
21872
+ i += (stems + 7) >> 3;
21873
+ stackClean = true;
21874
+ break;
21875
+ case 20: // cntrmask
21876
+ stems += stack.length >> 1;
21877
+ i += (stems + 7) >> 3;
21878
+ stackClean = true;
21879
+ break;
21880
+ case 21: // rmoveto
21881
+ y += stack.pop();
21882
+ x += stack.pop();
21883
+ moveTo(x, y);
21884
+ stackClean = true;
21885
+ break;
21886
+ case 22: // hmoveto
21887
+ x += stack.pop();
21888
+ moveTo(x, y);
21889
+ stackClean = true;
21890
+ break;
21891
+ case 23: // vstemhm
21892
+ stems += stack.length >> 1;
21893
+ stackClean = true;
21894
+ break;
21895
+ case 24: // rcurveline
21896
+ while (stack.length > 2) {
21897
+ xa = x + stack.shift(); ya = y + stack.shift();
21898
+ xb = xa + stack.shift(); yb = ya + stack.shift();
21899
+ x = xb + stack.shift(); y = yb + stack.shift();
21900
+ bezierCurveTo(xa, ya, xb, yb, x, y);
21901
+ }
21902
+ x += stack.shift();
21903
+ y += stack.shift();
21904
+ lineTo(x, y);
21905
+ break;
21906
+ case 25: // rlinecurve
21907
+ while (stack.length > 6) {
21908
+ x += stack.shift();
21909
+ y += stack.shift();
21910
+ lineTo(x, y);
21911
+ }
21912
+ xa = x + stack.shift(); ya = y + stack.shift();
21913
+ xb = xa + stack.shift(); yb = ya + stack.shift();
21914
+ x = xb + stack.shift(); y = yb + stack.shift();
21915
+ bezierCurveTo(xa, ya, xb, yb, x, y);
21916
+ break;
21917
+ case 26: // vvcurveto
21918
+ if (stack.length % 2) {
21919
+ x += stack.shift();
21920
+ }
21921
+ while (stack.length > 0) {
21922
+ xa = x; ya = y + stack.shift();
21923
+ xb = xa + stack.shift(); yb = ya + stack.shift();
21924
+ x = xb; y = yb + stack.shift();
21925
+ bezierCurveTo(xa, ya, xb, yb, x, y);
21926
+ }
21927
+ break;
21928
+ case 27: // hhcurveto
21929
+ if (stack.length % 2) {
21930
+ y += stack.shift();
21931
+ }
21932
+ while (stack.length > 0) {
21933
+ xa = x + stack.shift(); ya = y;
21934
+ xb = xa + stack.shift(); yb = ya + stack.shift();
21935
+ x = xb + stack.shift(); y = yb;
21936
+ bezierCurveTo(xa, ya, xb, yb, x, y);
21937
+ }
21938
+ break;
21939
+ case 28:
21940
+ stack.push(((code[i] << 24) | (code[i + 1] << 16)) >> 16);
21941
+ i += 2;
21942
+ break;
21943
+ case 29: // callgsubr
21944
+ n = stack.pop() + font.gsubrsBias;
21945
+ subrCode = font.gsubrs[n];
21946
+ if (subrCode) {
21947
+ parse(subrCode);
21948
+ }
21949
+ break;
21950
+ case 30: // vhcurveto
21951
+ while (stack.length > 0) {
21952
+ xa = x; ya = y + stack.shift();
21953
+ xb = xa + stack.shift(); yb = ya + stack.shift();
21954
+ x = xb + stack.shift();
21955
+ y = yb + (stack.length === 1 ? stack.shift() : 0);
21956
+ bezierCurveTo(xa, ya, xb, yb, x, y);
21957
+ if (stack.length === 0) {
21958
+ break;
21959
+ }
21960
+
21961
+ xa = x + stack.shift(); ya = y;
21962
+ xb = xa + stack.shift(); yb = ya + stack.shift();
21963
+ y = yb + stack.shift();
21964
+ x = xb + (stack.length === 1 ? stack.shift() : 0);
21965
+ bezierCurveTo(xa, ya, xb, yb, x, y);
21966
+ }
21967
+ break;
21968
+ case 31: // hvcurveto
21969
+ while (stack.length > 0) {
21970
+ xa = x + stack.shift(); ya = y;
21971
+ xb = xa + stack.shift(); yb = ya + stack.shift();
21972
+ y = yb + stack.shift();
21973
+ x = xb + (stack.length === 1 ? stack.shift() : 0);
21974
+ bezierCurveTo(xa, ya, xb, yb, x, y);
21975
+ if (stack.length === 0) {
21976
+ break;
21977
+ }
21978
+
21979
+ xa = x; ya = y + stack.shift();
21980
+ xb = xa + stack.shift(); yb = ya + stack.shift();
21981
+ x = xb + stack.shift();
21982
+ y = yb + (stack.length === 1 ? stack.shift() : 0);
21983
+ bezierCurveTo(xa, ya, xb, yb, x, y);
21984
+ }
21985
+ break;
21986
+ default:
21987
+ if (v < 32) {
21988
+ error('unknown operator: ' + v);
21989
+ }
21990
+ if (v < 247) {
21991
+ stack.push(v - 139);
21992
+ } else if (v < 251) {
21993
+ stack.push((v - 247) * 256 + code[i++] + 108);
21994
+ } else if (v < 255) {
21995
+ stack.push(-(v - 251) * 256 - code[i++] - 108);
21996
+ } else {
21997
+ stack.push(((code[i] << 24) | (code[i + 1] << 16) |
21998
+ (code[i + 2] << 8) | code[i + 3]) / 65536);
21999
+ i += 4;
22000
+ }
22001
+ break;
22002
+ }
22003
+ if (stackClean) {
22004
+ stack.length = 0;
22005
+ }
22006
+ }
22007
+ }
22008
+ parse(code);
22009
+ }
22010
+
22011
+ var noop = '';
22012
+
22013
+ function CompiledFont(fontMatrix) {
22014
+ this.compiledGlyphs = {};
22015
+ this.fontMatrix = fontMatrix;
22016
+ }
22017
+ CompiledFont.prototype = {
22018
+ getPathJs: function (unicode) {
22019
+ var gid = lookupCmap(this.cmap, unicode);
22020
+ var fn = this.compiledGlyphs[gid];
22021
+ if (!fn) {
22022
+ this.compiledGlyphs[gid] = fn = this.compileGlyph(this.glyphs[gid]);
22023
+ }
22024
+ return fn;
22025
+ },
22026
+
22027
+ compileGlyph: function (code) {
22028
+ if (!code || code.length === 0 || code[0] === 14) {
22029
+ return noop;
22030
+ }
22031
+
22032
+ var js = [];
22033
+ js.push('c.save();');
22034
+ js.push('c.transform(' + this.fontMatrix.join(',') + ');');
22035
+ js.push('c.scale(size, -size);');
22036
+
22037
+ this.compileGlyphImpl(code, js);
22038
+
22039
+ js.push('c.restore();');
22040
+
22041
+ return js.join('\n');
22042
+ },
22043
+
22044
+ compileGlyphImpl: function () {
22045
+ error('Children classes should implement this.');
22046
+ },
22047
+
22048
+ hasBuiltPath: function (unicode) {
22049
+ var gid = lookupCmap(this.cmap, unicode);
22050
+ return gid in this.compiledGlyphs;
22051
+ }
22052
+ };
22053
+
22054
+ function TrueTypeCompiled(glyphs, cmap, fontMatrix) {
22055
+ fontMatrix = fontMatrix || [0.000488, 0, 0, 0.000488, 0, 0];
22056
+ CompiledFont.call(this, fontMatrix);
22057
+
22058
+ this.glyphs = glyphs;
22059
+ this.cmap = cmap;
22060
+
22061
+ this.compiledGlyphs = [];
22062
+ }
22063
+
22064
+ Util.inherit(TrueTypeCompiled, CompiledFont, {
22065
+ compileGlyphImpl: function (code, js) {
22066
+ compileGlyf(code, js, this);
22067
+ }
22068
+ });
22069
+
22070
+ function Type2Compiled(cffInfo, cmap, fontMatrix, glyphNameMap) {
22071
+ fontMatrix = fontMatrix || [0.001, 0, 0, 0.001, 0, 0];
22072
+ CompiledFont.call(this, fontMatrix);
22073
+ this.glyphs = cffInfo.glyphs;
22074
+ this.gsubrs = cffInfo.gsubrs || [];
22075
+ this.subrs = cffInfo.subrs || [];
22076
+ this.cmap = cmap;
22077
+ this.glyphNameMap = glyphNameMap || GlyphsUnicode;
22078
+
22079
+ this.compiledGlyphs = [];
22080
+ this.gsubrsBias = (this.gsubrs.length < 1240 ?
22081
+ 107 : (this.gsubrs.length < 33900 ? 1131 : 32768));
22082
+ this.subrsBias = (this.subrs.length < 1240 ?
22083
+ 107 : (this.subrs.length < 33900 ? 1131 : 32768));
22084
+ }
22085
+
22086
+ Util.inherit(Type2Compiled, CompiledFont, {
22087
+ compileGlyphImpl: function (code, js) {
22088
+ compileCharString(code, js, this);
22089
+ }
22090
+ });
22091
+
22092
+
22093
+ return {
22094
+ create: function FontRendererFactory_create(font) {
22095
+ var data = new Uint8Array(font.data);
22096
+ var cmap, glyf, loca, cff, indexToLocFormat, unitsPerEm;
22097
+ var numTables = getUshort(data, 4);
22098
+ for (var i = 0, p = 12; i < numTables; i++, p += 16) {
22099
+ var tag = bytesToString(data.subarray(p, p + 4));
22100
+ var offset = getLong(data, p + 8);
22101
+ var length = getLong(data, p + 12);
22102
+ switch (tag) {
22103
+ case 'cmap':
22104
+ cmap = parseCmap(data, offset, offset + length);
22105
+ break;
22106
+ case 'glyf':
22107
+ glyf = data.subarray(offset, offset + length);
22108
+ break;
22109
+ case 'loca':
22110
+ loca = data.subarray(offset, offset + length);
22111
+ break;
22112
+ case 'head':
22113
+ unitsPerEm = getUshort(data, offset + 18);
22114
+ indexToLocFormat = getUshort(data, offset + 50);
22115
+ break;
22116
+ case 'CFF ':
22117
+ cff = parseCff(data, offset, offset + length);
22118
+ break;
22119
+ }
22120
+ }
22121
+
22122
+ if (glyf) {
22123
+ var fontMatrix = (!unitsPerEm ? font.fontMatrix :
22124
+ [1 / unitsPerEm, 0, 0, 1 / unitsPerEm, 0, 0]);
22125
+ return new TrueTypeCompiled(
22126
+ parseGlyfTable(glyf, loca, indexToLocFormat), cmap, fontMatrix);
22127
+ } else {
22128
+ return new Type2Compiled(cff, cmap, font.fontMatrix, font.glyphNameMap);
22129
+ }
22130
+ }
22131
+ };
22132
+ })();
22133
+
22134
+
22135
+ var GlyphsUnicode = {
22136
+ A: 0x0041,
22137
+ AE: 0x00C6,
22138
+ AEacute: 0x01FC,
22139
+ AEmacron: 0x01E2,
22140
+ AEsmall: 0xF7E6,
22141
+ Aacute: 0x00C1,
22142
+ Aacutesmall: 0xF7E1,
22143
+ Abreve: 0x0102,
22144
+ Abreveacute: 0x1EAE,
22145
+ Abrevecyrillic: 0x04D0,
22146
+ Abrevedotbelow: 0x1EB6,
22147
+ Abrevegrave: 0x1EB0,
22148
+ Abrevehookabove: 0x1EB2,
22149
+ Abrevetilde: 0x1EB4,
22150
+ Acaron: 0x01CD,
22151
+ Acircle: 0x24B6,
22152
+ Acircumflex: 0x00C2,
22153
+ Acircumflexacute: 0x1EA4,
22154
+ Acircumflexdotbelow: 0x1EAC,
22155
+ Acircumflexgrave: 0x1EA6,
22156
+ Acircumflexhookabove: 0x1EA8,
22157
+ Acircumflexsmall: 0xF7E2,
22158
+ Acircumflextilde: 0x1EAA,
22159
+ Acute: 0xF6C9,
22160
+ Acutesmall: 0xF7B4,
22161
+ Acyrillic: 0x0410,
22162
+ Adblgrave: 0x0200,
22163
+ Adieresis: 0x00C4,
22164
+ Adieresiscyrillic: 0x04D2,
22165
+ Adieresismacron: 0x01DE,
22166
+ Adieresissmall: 0xF7E4,
22167
+ Adotbelow: 0x1EA0,
22168
+ Adotmacron: 0x01E0,
22169
+ Agrave: 0x00C0,
22170
+ Agravesmall: 0xF7E0,
22171
+ Ahookabove: 0x1EA2,
22172
+ Aiecyrillic: 0x04D4,
22173
+ Ainvertedbreve: 0x0202,
22174
+ Alpha: 0x0391,
22175
+ Alphatonos: 0x0386,
22176
+ Amacron: 0x0100,
22177
+ Amonospace: 0xFF21,
22178
+ Aogonek: 0x0104,
22179
+ Aring: 0x00C5,
22180
+ Aringacute: 0x01FA,
22181
+ Aringbelow: 0x1E00,
22182
+ Aringsmall: 0xF7E5,
22183
+ Asmall: 0xF761,
22184
+ Atilde: 0x00C3,
22185
+ Atildesmall: 0xF7E3,
22186
+ Aybarmenian: 0x0531,
22187
+ B: 0x0042,
22188
+ Bcircle: 0x24B7,
22189
+ Bdotaccent: 0x1E02,
22190
+ Bdotbelow: 0x1E04,
22191
+ Becyrillic: 0x0411,
22192
+ Benarmenian: 0x0532,
22193
+ Beta: 0x0392,
22194
+ Bhook: 0x0181,
22195
+ Blinebelow: 0x1E06,
22196
+ Bmonospace: 0xFF22,
22197
+ Brevesmall: 0xF6F4,
22198
+ Bsmall: 0xF762,
22199
+ Btopbar: 0x0182,
22200
+ C: 0x0043,
22201
+ Caarmenian: 0x053E,
22202
+ Cacute: 0x0106,
22203
+ Caron: 0xF6CA,
22204
+ Caronsmall: 0xF6F5,
22205
+ Ccaron: 0x010C,
22206
+ Ccedilla: 0x00C7,
22207
+ Ccedillaacute: 0x1E08,
22208
+ Ccedillasmall: 0xF7E7,
22209
+ Ccircle: 0x24B8,
22210
+ Ccircumflex: 0x0108,
22211
+ Cdot: 0x010A,
22212
+ Cdotaccent: 0x010A,
22213
+ Cedillasmall: 0xF7B8,
22214
+ Chaarmenian: 0x0549,
22215
+ Cheabkhasiancyrillic: 0x04BC,
22216
+ Checyrillic: 0x0427,
22217
+ Chedescenderabkhasiancyrillic: 0x04BE,
22218
+ Chedescendercyrillic: 0x04B6,
22219
+ Chedieresiscyrillic: 0x04F4,
22220
+ Cheharmenian: 0x0543,
22221
+ Chekhakassiancyrillic: 0x04CB,
22222
+ Cheverticalstrokecyrillic: 0x04B8,
22223
+ Chi: 0x03A7,
22224
+ Chook: 0x0187,
22225
+ Circumflexsmall: 0xF6F6,
22226
+ Cmonospace: 0xFF23,
22227
+ Coarmenian: 0x0551,
22228
+ Csmall: 0xF763,
22229
+ D: 0x0044,
22230
+ DZ: 0x01F1,
22231
+ DZcaron: 0x01C4,
22232
+ Daarmenian: 0x0534,
22233
+ Dafrican: 0x0189,
22234
+ Dcaron: 0x010E,
22235
+ Dcedilla: 0x1E10,
22236
+ Dcircle: 0x24B9,
22237
+ Dcircumflexbelow: 0x1E12,
22238
+ Dcroat: 0x0110,
22239
+ Ddotaccent: 0x1E0A,
22240
+ Ddotbelow: 0x1E0C,
22241
+ Decyrillic: 0x0414,
22242
+ Deicoptic: 0x03EE,
22243
+ Delta: 0x2206,
22244
+ Deltagreek: 0x0394,
22245
+ Dhook: 0x018A,
22246
+ Dieresis: 0xF6CB,
22247
+ DieresisAcute: 0xF6CC,
22248
+ DieresisGrave: 0xF6CD,
22249
+ Dieresissmall: 0xF7A8,
22250
+ Digammagreek: 0x03DC,
22251
+ Djecyrillic: 0x0402,
22252
+ Dlinebelow: 0x1E0E,
22253
+ Dmonospace: 0xFF24,
22254
+ Dotaccentsmall: 0xF6F7,
22255
+ Dslash: 0x0110,
22256
+ Dsmall: 0xF764,
22257
+ Dtopbar: 0x018B,
22258
+ Dz: 0x01F2,
22259
+ Dzcaron: 0x01C5,
22260
+ Dzeabkhasiancyrillic: 0x04E0,
22261
+ Dzecyrillic: 0x0405,
22262
+ Dzhecyrillic: 0x040F,
22263
+ E: 0x0045,
22264
+ Eacute: 0x00C9,
22265
+ Eacutesmall: 0xF7E9,
22266
+ Ebreve: 0x0114,
22267
+ Ecaron: 0x011A,
22268
+ Ecedillabreve: 0x1E1C,
22269
+ Echarmenian: 0x0535,
22270
+ Ecircle: 0x24BA,
22271
+ Ecircumflex: 0x00CA,
22272
+ Ecircumflexacute: 0x1EBE,
22273
+ Ecircumflexbelow: 0x1E18,
22274
+ Ecircumflexdotbelow: 0x1EC6,
22275
+ Ecircumflexgrave: 0x1EC0,
22276
+ Ecircumflexhookabove: 0x1EC2,
22277
+ Ecircumflexsmall: 0xF7EA,
22278
+ Ecircumflextilde: 0x1EC4,
22279
+ Ecyrillic: 0x0404,
22280
+ Edblgrave: 0x0204,
22281
+ Edieresis: 0x00CB,
22282
+ Edieresissmall: 0xF7EB,
22283
+ Edot: 0x0116,
22284
+ Edotaccent: 0x0116,
22285
+ Edotbelow: 0x1EB8,
22286
+ Efcyrillic: 0x0424,
22287
+ Egrave: 0x00C8,
22288
+ Egravesmall: 0xF7E8,
22289
+ Eharmenian: 0x0537,
22290
+ Ehookabove: 0x1EBA,
22291
+ Eightroman: 0x2167,
22292
+ Einvertedbreve: 0x0206,
22293
+ Eiotifiedcyrillic: 0x0464,
22294
+ Elcyrillic: 0x041B,
22295
+ Elevenroman: 0x216A,
22296
+ Emacron: 0x0112,
22297
+ Emacronacute: 0x1E16,
22298
+ Emacrongrave: 0x1E14,
22299
+ Emcyrillic: 0x041C,
22300
+ Emonospace: 0xFF25,
22301
+ Encyrillic: 0x041D,
22302
+ Endescendercyrillic: 0x04A2,
22303
+ Eng: 0x014A,
22304
+ Enghecyrillic: 0x04A4,
22305
+ Enhookcyrillic: 0x04C7,
22306
+ Eogonek: 0x0118,
22307
+ Eopen: 0x0190,
22308
+ Epsilon: 0x0395,
22309
+ Epsilontonos: 0x0388,
22310
+ Ercyrillic: 0x0420,
22311
+ Ereversed: 0x018E,
22312
+ Ereversedcyrillic: 0x042D,
22313
+ Escyrillic: 0x0421,
22314
+ Esdescendercyrillic: 0x04AA,
22315
+ Esh: 0x01A9,
22316
+ Esmall: 0xF765,
22317
+ Eta: 0x0397,
22318
+ Etarmenian: 0x0538,
22319
+ Etatonos: 0x0389,
22320
+ Eth: 0x00D0,
22321
+ Ethsmall: 0xF7F0,
22322
+ Etilde: 0x1EBC,
22323
+ Etildebelow: 0x1E1A,
22324
+ Euro: 0x20AC,
22325
+ Ezh: 0x01B7,
22326
+ Ezhcaron: 0x01EE,
22327
+ Ezhreversed: 0x01B8,
22328
+ F: 0x0046,
22329
+ Fcircle: 0x24BB,
22330
+ Fdotaccent: 0x1E1E,
22331
+ F