PrintTranslator.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "PrintTranslator.h"
  6. #include "gfxContext.h"
  7. #include "nsDeviceContext.h"
  8. #include "mozilla/gfx/RecordedEvent.h"
  9. #include "mozilla/gfx/RecordingTypes.h"
  10. #include "mozilla/UniquePtr.h"
  11. using namespace mozilla::gfx;
  12. namespace mozilla {
  13. namespace layout {
  14. PrintTranslator::PrintTranslator(nsDeviceContext* aDeviceContext)
  15. : mDeviceContext(aDeviceContext)
  16. {
  17. RefPtr<gfxContext> context = mDeviceContext->CreateReferenceRenderingContext();
  18. mBaseDT = context->GetDrawTarget();
  19. }
  20. bool
  21. PrintTranslator::TranslateRecording(std::istream& aRecording)
  22. {
  23. uint32_t magicInt;
  24. ReadElement(aRecording, magicInt);
  25. if (magicInt != mozilla::gfx::kMagicInt) {
  26. return false;
  27. }
  28. uint16_t majorRevision;
  29. ReadElement(aRecording, majorRevision);
  30. if (majorRevision != kMajorRevision) {
  31. return false;
  32. }
  33. uint16_t minorRevision;
  34. ReadElement(aRecording, minorRevision);
  35. if (minorRevision > kMinorRevision) {
  36. return false;
  37. }
  38. int32_t eventType;
  39. ReadElement(aRecording, eventType);
  40. while (aRecording.good()) {
  41. UniquePtr<RecordedEvent> recordedEvent(
  42. RecordedEvent::LoadEventFromStream(aRecording,
  43. static_cast<RecordedEvent::EventType>(eventType)));
  44. // Make sure that the whole event was read from the stream successfully.
  45. if (!aRecording.good() || !recordedEvent) {
  46. return false;
  47. }
  48. if (!recordedEvent->PlayEvent(this)) {
  49. return false;
  50. }
  51. ReadElement(aRecording, eventType);
  52. }
  53. return true;
  54. }
  55. already_AddRefed<DrawTarget>
  56. PrintTranslator::CreateDrawTarget(ReferencePtr aRefPtr,
  57. const gfx::IntSize &aSize,
  58. gfx::SurfaceFormat aFormat)
  59. {
  60. RefPtr<gfxContext> context = mDeviceContext->CreateRenderingContext();
  61. if (!context) {
  62. NS_WARNING("Failed to create rendering context for print.");
  63. return nullptr;
  64. }
  65. RefPtr<DrawTarget> drawTarget = context->GetDrawTarget();
  66. AddDrawTarget(aRefPtr, drawTarget);
  67. return drawTarget.forget();
  68. }
  69. FontType
  70. PrintTranslator::GetDesiredFontType()
  71. {
  72. switch (mBaseDT->GetBackendType()) {
  73. case BackendType::DIRECT2D:
  74. return FontType::DWRITE;
  75. case BackendType::CAIRO:
  76. return FontType::CAIRO;
  77. case BackendType::SKIA:
  78. return FontType::SKIA;
  79. default:
  80. return FontType::CAIRO;
  81. }
  82. }
  83. } // namespace layout
  84. } // namespace mozilla