writer_spec.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. var _primitives = require("../../core/primitives.js");
  24. var _writer = require("../../core/writer.js");
  25. var _util = require("../../shared/util.js");
  26. var _stream = require("../../core/stream.js");
  27. describe("Writer", function () {
  28. describe("Incremental update", function () {
  29. it("should update a file with new objects", function () {
  30. const originalData = new Uint8Array();
  31. const newRefs = [{
  32. ref: _primitives.Ref.get(123, 0x2d),
  33. data: "abc\n"
  34. }, {
  35. ref: _primitives.Ref.get(456, 0x4e),
  36. data: "defg\n"
  37. }];
  38. const xrefInfo = {
  39. newRef: _primitives.Ref.get(789, 0),
  40. startXRef: 314,
  41. fileIds: ["id", ""],
  42. rootRef: null,
  43. infoRef: null,
  44. encryptRef: null,
  45. filename: "foo.pdf",
  46. info: {}
  47. };
  48. let data = (0, _writer.incrementalUpdate)({
  49. originalData,
  50. xrefInfo,
  51. newRefs
  52. });
  53. data = (0, _util.bytesToString)(data);
  54. const expected = "\nabc\n" + "defg\n" + "789 0 obj\n" + "<< /Size 790 /Prev 314 /Type /XRef /Index [0 1 123 1 456 1 789 1] " + "/ID [(id) (\x01#Eg\x89\xab\xcd\xef\xfe\xdc\xba\x98vT2\x10)] " + "/W [1 1 2] /Length 16>> stream\n" + "\x00\x01\xff\xff" + "\x01\x01\x00\x2d" + "\x01\x05\x00\x4e" + "\x01\x0a\x00\x00\n" + "endstream\n" + "endobj\n" + "startxref\n" + "10\n" + "%%EOF\n";
  55. expect(data).toEqual(expected);
  56. });
  57. it("should update a file, missing the /ID-entry, with new objects", function () {
  58. const originalData = new Uint8Array();
  59. const newRefs = [{
  60. ref: _primitives.Ref.get(123, 0x2d),
  61. data: "abc\n"
  62. }];
  63. const xrefInfo = {
  64. newRef: _primitives.Ref.get(789, 0),
  65. startXRef: 314,
  66. fileIds: null,
  67. rootRef: null,
  68. infoRef: null,
  69. encryptRef: null,
  70. filename: "foo.pdf",
  71. info: {}
  72. };
  73. let data = (0, _writer.incrementalUpdate)({
  74. originalData,
  75. xrefInfo,
  76. newRefs
  77. });
  78. data = (0, _util.bytesToString)(data);
  79. const expected = "\nabc\n" + "789 0 obj\n" + "<< /Size 790 /Prev 314 /Type /XRef /Index [0 1 123 1 789 1] " + "/W [1 1 2] /Length 12>> stream\n" + "\x00\x01\xff\xff" + "\x01\x01\x00\x2d" + "\x01\x05\x00\x00\n" + "endstream\n" + "endobj\n" + "startxref\n" + "5\n" + "%%EOF\n";
  80. expect(data).toEqual(expected);
  81. });
  82. });
  83. describe("writeDict", function () {
  84. it("should write a Dict", function () {
  85. const dict = new _primitives.Dict(null);
  86. dict.set("A", _primitives.Name.get("B"));
  87. dict.set("B", _primitives.Ref.get(123, 456));
  88. dict.set("C", 789);
  89. dict.set("D", "hello world");
  90. dict.set("E", "(hello\\world)");
  91. dict.set("F", [1.23001, 4.50001, 6]);
  92. const gdict = new _primitives.Dict(null);
  93. gdict.set("H", 123.00001);
  94. const string = "a stream";
  95. const stream = new _stream.StringStream(string);
  96. stream.dict = new _primitives.Dict(null);
  97. stream.dict.set("Length", string.length);
  98. gdict.set("I", stream);
  99. dict.set("G", gdict);
  100. const buffer = [];
  101. (0, _writer.writeDict)(dict, buffer, null);
  102. const expected = "<< /A /B /B 123 456 R /C 789 /D (hello world) " + "/E (\\(hello\\\\world\\)) /F [1.23 4.5 6] " + "/G << /H 123 /I << /Length 8>> stream\n" + "a stream\n" + "endstream\n>>>>";
  103. expect(buffer.join("")).toEqual(expected);
  104. });
  105. it("should write a Dict in escaping PDF names", function () {
  106. const dict = new _primitives.Dict(null);
  107. dict.set("\xfeA#", _primitives.Name.get("hello"));
  108. dict.set("B", _primitives.Name.get("#hello"));
  109. dict.set("C", _primitives.Name.get("he\xfello\xff"));
  110. const buffer = [];
  111. (0, _writer.writeDict)(dict, buffer, null);
  112. const expected = "<< /#feA#23 /hello /B /#23hello /C /he#fello#ff>>";
  113. expect(buffer.join("")).toEqual(expected);
  114. });
  115. });
  116. });