Context.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "MuPDF.h"
  2. #include <thread>
  3. #include <mutex>
  4. #include <iostream>
  5. using namespace System::Threading;
  6. #pragma unmanaged
  7. static fz_pixmap* GetPixmap(fz_context* ctx, fz_colorspace* cs, int width, int height) {
  8. fz_pixmap* p;
  9. MuTryReturn(ctx, fz_new_pixmap(ctx, cs, width, height, NULL, 0), p);
  10. }
  11. static fz_pixmap* GetPixmap(fz_context* ctx, fz_colorspace* cs, fz_irect rect) {
  12. fz_pixmap* p;
  13. MuTryReturn(ctx, fz_new_pixmap_with_bbox(ctx, cs, rect, NULL, 0), p);
  14. }
  15. DLLEXP fz_stream* OpenFile(fz_context* ctx, const wchar_t* filePath) {
  16. fz_stream* r;
  17. MuTryReturn(ctx, fz_open_file_w(ctx, filePath), r);
  18. }
  19. DLLEXP pdf_document* OpenPdfDocumentStream(fz_context* ctx, fz_stream* stream) {
  20. pdf_document* r;
  21. MuTryReturn(ctx, pdf_open_document_with_stream(ctx, stream), r);
  22. }
  23. DLLEXP int CloseDocumentWriter(fz_context* ctx, fz_document_writer* writer) {
  24. MuTry(ctx, fz_close_document_writer(ctx, writer));
  25. }
  26. struct internal_state {
  27. /* Constructor. */
  28. internal_state() {
  29. m_locks.user = this;
  30. m_locks.lock = lock;
  31. m_locks.unlock = unlock;
  32. m_ctx = nullptr;
  33. reinit(true);
  34. }
  35. void reinit(bool multithreaded) {
  36. fz_drop_context(m_ctx);
  37. m_multithreaded = multithreaded;
  38. m_ctx = fz_new_context(NULL /*alloc*/, (multithreaded) ? &m_locks : nullptr, FZ_STORE_DEFAULT);
  39. fz_register_document_handlers(m_ctx);
  40. }
  41. static void lock(void* user, int lock) {
  42. internal_state* self = (internal_state*)user;
  43. assert(self->m_multithreaded);
  44. self->m_mutexes[lock].lock();
  45. }
  46. static void unlock(void* user, int lock) {
  47. internal_state* self = (internal_state*)user;
  48. assert(self->m_multithreaded);
  49. self->m_mutexes[lock].unlock();
  50. }
  51. ~internal_state() {
  52. fz_drop_context(m_ctx);
  53. }
  54. bool m_multithreaded;
  55. fz_context* m_ctx;
  56. std::mutex m_mutex; /* Serialise access to m_ctx. fixme: not actually necessary. */
  57. /* Provide thread support to mupdf. */
  58. std::mutex m_mutexes[FZ_LOCK_MAX];
  59. fz_locks_context m_locks;
  60. };
  61. static internal_state s_state;
  62. #pragma managed
  63. MuPDF::Context^ MuPDF::Context::Instance::get() {
  64. if (_Instance) {
  65. return _Instance;
  66. }
  67. if (_MainInstance->_disposed) {
  68. return nullptr;
  69. }
  70. return _Instance = gcnew Context(fz_clone_context(s_state.m_ctx), true);
  71. }
  72. MuPDF::Document^ MuPDF::Context::OpenDocument(String^ filePath) {
  73. Stream^ s = gcnew Stream(filePath);
  74. try {
  75. auto doc = gcnew Document(s->Ptr);
  76. doc->FilePath = filePath;
  77. return doc;
  78. }
  79. catch (Exception^) {
  80. delete s;
  81. throw;
  82. }
  83. }
  84. MuPDF::Colorspace^ MuPDF::Context::GetColorspace(ColorspaceKind kind) {
  85. return gcnew Colorspace(GetFzColorspace(kind));
  86. }
  87. MuPDF::Pixmap^ MuPDF::Context::CreatePixmap(ColorspaceKind colorspace, int width, int height) {
  88. fz_pixmap* pixmap = GetPixmap(_context, GetFzColorspace(colorspace), width, height);
  89. if (pixmap) {
  90. return gcnew Pixmap(pixmap);
  91. }
  92. throw MuException::FromContext();
  93. }
  94. MuPDF::Pixmap^ MuPDF::Context::CreatePixmap(ColorspaceKind colorspace, BBox box) {
  95. fz_pixmap* pixmap = GetPixmap(_context, GetFzColorspace(colorspace), box);
  96. if (pixmap) {
  97. return gcnew Pixmap(pixmap);
  98. }
  99. throw MuException::FromContext();
  100. }
  101. MuPDF::Context^ MuPDF::Context::MakeMainContext() {
  102. return gcnew Context(s_state.m_ctx, false);
  103. }
  104. void MuPDF::Context::ReleaseHandle() {
  105. if (_isCloned) {
  106. fz_drop_context(_context);
  107. _context = NULL;
  108. _disposed = true;
  109. return;
  110. }
  111. // HACK:
  112. // The following statement often causes AccessViolationException for unknown reason.
  113. // Since the main instance is a static one which is finalized only when the program exits,
  114. // we skip it and let the OS do the dirty job.
  115. // fz_drop_context(_context);
  116. _context = NULL;
  117. _disposed = true;
  118. }
  119. fz_colorspace* MuPDF::Context::GetFzColorspace(ColorspaceKind kind) {
  120. switch (kind) {
  121. case ColorspaceKind::Rgb: return fz_device_rgb(_context);
  122. case ColorspaceKind::Cmyk: return fz_device_cmyk(_context);
  123. case ColorspaceKind::Gray: return fz_device_gray(_context);
  124. case ColorspaceKind::Bgr: return fz_device_bgr(_context);
  125. case ColorspaceKind::Lab: return fz_device_lab(_context);
  126. }
  127. throw gcnew MuException("Invalid colorspace kind.");
  128. }
  129. void MuPDF::install_load_windows_font_funcs(fz_context* ctx)
  130. {
  131. }