printing.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (C) 2017, David PHAM-VAN <dev.nfet.net@gmail.com>
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "printing.h"
  17. #include "print_job.h"
  18. namespace nfet {
  19. extern std::unique_ptr<flutter::MethodChannel<flutter::EncodableValue>> channel;
  20. Printing::Printing() {}
  21. Printing::~Printing() {}
  22. void Printing::onPageRasterized(std::vector<uint8_t> data,
  23. int width,
  24. int height,
  25. PrintJob* job) {
  26. channel->InvokeMethod(
  27. "onPageRasterized",
  28. std::make_unique<flutter::EncodableValue>(
  29. flutter::EncodableValue(flutter::EncodableMap{
  30. {flutter::EncodableValue("image"), flutter::EncodableValue(data)},
  31. {flutter::EncodableValue("width"),
  32. flutter::EncodableValue(width)},
  33. {flutter::EncodableValue("height"),
  34. flutter::EncodableValue(height)},
  35. {flutter::EncodableValue("job"),
  36. flutter::EncodableValue(job->id())},
  37. })));
  38. }
  39. void Printing::onPageRasterEnd(PrintJob* job, const std::string& error) {
  40. auto map = flutter::EncodableMap{
  41. {flutter::EncodableValue("job"), flutter::EncodableValue(job->id())},
  42. };
  43. if (!error.empty()) {
  44. map[flutter::EncodableValue("error")] = flutter::EncodableValue(error);
  45. }
  46. channel->InvokeMethod(
  47. "onPageRasterEnd",
  48. std::make_unique<flutter::EncodableValue>(flutter::EncodableValue(map)));
  49. }
  50. class OnLayoutResult : public flutter::MethodResult<flutter::EncodableValue> {
  51. public:
  52. OnLayoutResult(PrintJob* job) : job{job} {}
  53. private:
  54. PrintJob* job;
  55. protected:
  56. void SuccessInternal(const flutter::EncodableValue* result) {
  57. auto doc = std::get<std::vector<uint8_t>>(*result);
  58. job->writeJob(doc);
  59. delete job;
  60. }
  61. void ErrorInternal(const std::string& error_code,
  62. const std::string& error_message,
  63. const flutter::EncodableValue* error_details) {
  64. delete job;
  65. }
  66. void NotImplementedInternal() { delete job; }
  67. };
  68. void Printing::onLayout(PrintJob* job,
  69. double pageWidth,
  70. double pageHeight,
  71. double marginLeft,
  72. double marginTop,
  73. double marginRight,
  74. double marginBottom) {
  75. channel->InvokeMethod("onLayout",
  76. std::make_unique<flutter::EncodableValue>(
  77. flutter::EncodableValue(flutter::EncodableMap{
  78. {flutter::EncodableValue("job"),
  79. flutter::EncodableValue(job->id())},
  80. {flutter::EncodableValue("width"),
  81. flutter::EncodableValue(pageWidth)},
  82. {flutter::EncodableValue("height"),
  83. flutter::EncodableValue(pageHeight)},
  84. {flutter::EncodableValue("marginLeft"),
  85. flutter::EncodableValue(marginLeft)},
  86. {flutter::EncodableValue("marginTop"),
  87. flutter::EncodableValue(marginTop)},
  88. {flutter::EncodableValue("marginRight"),
  89. flutter::EncodableValue(marginRight)},
  90. {flutter::EncodableValue("marginBottom"),
  91. flutter::EncodableValue(marginBottom)},
  92. })),
  93. std::make_unique<OnLayoutResult>(job));
  94. }
  95. // send completion status to flutter
  96. void Printing::onCompleted(PrintJob* job,
  97. bool completed,
  98. const std::string& error) {
  99. auto map = flutter::EncodableMap{
  100. {flutter::EncodableValue("job"), flutter::EncodableValue(job->id())},
  101. {flutter::EncodableValue("completed"),
  102. flutter::EncodableValue(completed)},
  103. };
  104. if (!error.empty()) {
  105. map[flutter::EncodableValue("error")] = flutter::EncodableValue(error);
  106. }
  107. channel->InvokeMethod(
  108. "onCompleted",
  109. std::make_unique<flutter::EncodableValue>(flutter::EncodableValue(map)));
  110. }
  111. } // namespace nfet