writer_spec.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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)({
  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. done();
  57. });
  58. });
  59. describe("writeDict", function () {
  60. it("should write a Dict", function (done) {
  61. const dict = new _primitives.Dict(null);
  62. dict.set("A", _primitives.Name.get("B"));
  63. dict.set("B", _primitives.Ref.get(123, 456));
  64. dict.set("C", 789);
  65. dict.set("D", "hello world");
  66. dict.set("E", "(hello\\world)");
  67. dict.set("F", [1.23001, 4.50001, 6]);
  68. const gdict = new _primitives.Dict(null);
  69. gdict.set("H", 123.00001);
  70. const string = "a stream";
  71. const stream = new _stream.StringStream(string);
  72. stream.dict = new _primitives.Dict(null);
  73. stream.dict.set("Length", string.length);
  74. gdict.set("I", stream);
  75. dict.set("G", gdict);
  76. const buffer = [];
  77. (0, _writer.writeDict)(dict, buffer, null);
  78. 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>>>>";
  79. expect(buffer.join("")).toEqual(expected);
  80. done();
  81. });
  82. it("should write a Dict in escaping PDF names", function (done) {
  83. const dict = new _primitives.Dict(null);
  84. dict.set("\xfeA#", _primitives.Name.get("hello"));
  85. dict.set("B", _primitives.Name.get("#hello"));
  86. dict.set("C", _primitives.Name.get("he\xfello\xff"));
  87. const buffer = [];
  88. (0, _writer.writeDict)(dict, buffer, null);
  89. const expected = "<< /#feA#23 /hello /B /#23hello /C /he#fello#ff>>";
  90. expect(buffer.join("")).toEqual(expected);
  91. done();
  92. });
  93. });
  94. });