Screenshot.cpp 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) 2022-2024, Tim Flynn <trflynn89@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Bitmap.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/DOM/ElementFactory.h>
  9. #include <LibWeb/HTML/BrowsingContext.h>
  10. #include <LibWeb/HTML/HTMLCanvasElement.h>
  11. #include <LibWeb/HTML/TagNames.h>
  12. #include <LibWeb/HTML/TraversableNavigable.h>
  13. #include <LibWeb/Namespace.h>
  14. #include <LibWeb/Page/Page.h>
  15. #include <LibWeb/WebDriver/Screenshot.h>
  16. namespace Web::WebDriver {
  17. // https://w3c.github.io/webdriver/#dfn-draw-a-bounding-box-from-the-framebuffer
  18. ErrorOr<GC::Ref<HTML::HTMLCanvasElement>, WebDriver::Error> draw_bounding_box_from_the_framebuffer(HTML::BrowsingContext& browsing_context, DOM::Element& element, Gfx::IntRect rect)
  19. {
  20. // 1. If either the initial viewport's width or height is 0 CSS pixels, return error with error code unable to capture screen.
  21. auto viewport_rect = browsing_context.top_level_traversable()->viewport_rect();
  22. if (viewport_rect.is_empty())
  23. return Error::from_code(ErrorCode::UnableToCaptureScreen, "Viewport is empty"sv);
  24. auto viewport_device_rect = browsing_context.page().enclosing_device_rect(viewport_rect).to_type<int>();
  25. // 2. Let paint width be the initial viewport's width – min(rectangle x coordinate, rectangle x coordinate + rectangle width dimension).
  26. auto paint_width = viewport_device_rect.width() - min(rect.x(), rect.x() + rect.width());
  27. // 3. Let paint height be the initial viewport's height – min(rectangle y coordinate, rectangle y coordinate + rectangle height dimension).
  28. auto paint_height = viewport_device_rect.height() - min(rect.y(), rect.y() + rect.height());
  29. // 4. Let canvas be a new canvas element, and set its width and height to paint width and paint height, respectively.
  30. auto canvas_element = DOM::create_element(element.document(), HTML::TagNames::canvas, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
  31. auto& canvas = verify_cast<HTML::HTMLCanvasElement>(*canvas_element);
  32. // FIXME: Handle DevicePixelRatio in HiDPI mode.
  33. MUST(canvas.set_width(paint_width));
  34. MUST(canvas.set_height(paint_height));
  35. // FIXME: 5. Let context, a canvas context mode, be the result of invoking the 2D context creation algorithm given canvas as the target.
  36. if (!canvas.allocate_painting_surface(paint_width, paint_height))
  37. return Error::from_code(ErrorCode::UnableToCaptureScreen, "Unable to create a screenshot bitmap"sv);
  38. // 6. Complete implementation specific steps equivalent to drawing the region of the framebuffer specified by the following coordinates onto context:
  39. // - X coordinate: rectangle x coordinate
  40. // - Y coordinate: rectangle y coordinate
  41. // - Width: paint width
  42. // - Height: paint height
  43. Gfx::IntRect paint_rect { rect.x(), rect.y(), paint_width, paint_height };
  44. auto bitmap = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, canvas.surface()->size()));
  45. auto backing_store = Web::Painting::BitmapBackingStore(bitmap);
  46. browsing_context.page().client().paint(paint_rect.to_type<Web::DevicePixels>(), backing_store);
  47. canvas.surface()->write_from_bitmap(*bitmap);
  48. // 7. Return success with canvas.
  49. return canvas;
  50. }
  51. // https://w3c.github.io/webdriver/#dfn-encoding-a-canvas-as-base64
  52. Response encode_canvas_element(HTML::HTMLCanvasElement& canvas)
  53. {
  54. // FIXME: 1. If the canvas element’s bitmap’s origin-clean flag is set to false, return error with error code unable to capture screen.
  55. // 2. If the canvas element’s bitmap has no pixels (i.e. either its horizontal dimension or vertical dimension is zero) then return error with error code unable to capture screen.
  56. if (canvas.surface()->size().is_empty())
  57. return Error::from_code(ErrorCode::UnableToCaptureScreen, "Captured screenshot is empty"sv);
  58. // 3. Let file be a serialization of the canvas element’s bitmap as a file, using "image/png" as an argument.
  59. // 4. Let data url be a data: URL representing file. [RFC2397]
  60. auto data_url = canvas.to_data_url("image/png"sv, JS::js_undefined());
  61. // 5. Let index be the index of "," in data url.
  62. auto index = data_url.find_byte_offset(',');
  63. VERIFY(index.has_value());
  64. // 6. Let encoded string be a substring of data url using (index + 1) as the start argument.
  65. auto encoded_string = MUST(data_url.substring_from_byte_offset(*index + 1));
  66. // 7. Return success with data encoded string.
  67. return JsonValue { encoded_string.to_byte_string() };
  68. }
  69. }