text_layer_spec.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* Copyright 2022 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import {
  16. renderTextLayer,
  17. TextLayerRenderTask,
  18. } from "../../src/display/text_layer.js";
  19. import { buildGetDocumentParams } from "./test_utils.js";
  20. import { getDocument } from "../../src/display/api.js";
  21. import { isNodeJS } from "../../src/shared/is_node.js";
  22. describe("textLayer", function () {
  23. it("creates textLayer from ReadableStream", async function () {
  24. if (isNodeJS) {
  25. pending("document.createElement is not supported in Node.js.");
  26. }
  27. const loadingTask = getDocument(buildGetDocumentParams("basicapi.pdf"));
  28. const pdfDocument = await loadingTask.promise;
  29. const page = await pdfDocument.getPage(1);
  30. const textContentItemsStr = [];
  31. const textLayerRenderTask = renderTextLayer({
  32. textContentSource: page.streamTextContent(),
  33. container: document.createElement("div"),
  34. viewport: page.getViewport(),
  35. textContentItemsStr,
  36. });
  37. expect(textLayerRenderTask instanceof TextLayerRenderTask).toEqual(true);
  38. await textLayerRenderTask.promise;
  39. expect(textContentItemsStr).toEqual([
  40. "Table Of Content",
  41. "",
  42. "Chapter 1",
  43. " ",
  44. "..........................................................",
  45. " ",
  46. "2",
  47. "",
  48. "Paragraph 1.1",
  49. " ",
  50. "......................................................",
  51. " ",
  52. "3",
  53. "",
  54. "page 1 / 3",
  55. ]);
  56. });
  57. });