2
0

writer_spec.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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 (done) {
  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. encrypt: null,
  45. filename: "foo.pdf",
  46. info: {}
  47. };
  48. let data = (0, _writer.incrementalUpdate)(originalData, xrefInfo, newRefs);
  49. data = (0, _util.bytesToString)(data);
  50. 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";
  51. expect(data).toEqual(expected);
  52. done();
  53. });
  54. });
  55. describe("writeDict", function () {
  56. it("should write a Dict", function (done) {
  57. const dict = new _primitives.Dict(null);
  58. dict.set("A", _primitives.Name.get("B"));
  59. dict.set("B", _primitives.Ref.get(123, 456));
  60. dict.set("C", 789);
  61. dict.set("D", "hello world");
  62. dict.set("E", "(hello\\world)");
  63. dict.set("F", [1.23001, 4.50001, 6]);
  64. const gdict = new _primitives.Dict(null);
  65. gdict.set("H", 123.00001);
  66. const string = "a stream";
  67. const stream = new _stream.StringStream(string);
  68. stream.dict = new _primitives.Dict(null);
  69. stream.dict.set("Length", string.length);
  70. gdict.set("I", stream);
  71. dict.set("G", gdict);
  72. const buffer = [];
  73. (0, _writer.writeDict)(dict, buffer, null);
  74. 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>>>>";
  75. expect(buffer.join("")).toEqual(expected);
  76. done();
  77. });
  78. });
  79. });