Device.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "mupdf/fitz.h"
  2. #include "mupdf/pdf.h"
  3. #include "MuPDF.h"
  4. #ifndef __DEVICE
  5. #define __DEVICE
  6. #pragma once
  7. int CloseDevice(fz_context* ctx, fz_device* dev);
  8. using namespace System;
  9. namespace MuPDF {
  10. [FlagsAttribute]
  11. public enum class DeviceHints {
  12. DontInterpolateImages = 1,
  13. NoCache = 2,
  14. DontDecodeImages = 4
  15. };
  16. public ref class Device : IDisposable {
  17. internal:
  18. Device(fz_device* device) : _device(device) {};
  19. ~Device() {
  20. ReleaseHandle();
  21. }
  22. property fz_device* Ptr {
  23. fz_device* get() {
  24. return _device;
  25. }
  26. }
  27. public:
  28. /// <summary>
  29. /// Creates a new draw device to render PDF page contents.
  30. /// </summary>
  31. /// <param name="pixmap">The pixmap to render page contents.</param>
  32. /// <returns>A draw device which paints content to the pixmap.</returns>
  33. static Device^ NewDraw(Pixmap^ pixmap) {
  34. return TryCreateDevice(fz_new_draw_device(Context::Ptr, fz_identity, pixmap->Ptr));
  35. }
  36. static Device^ NewDraw(Pixmap^ pixmap, Matrix matrix) {
  37. return TryCreateDevice(fz_new_draw_device(Context::Ptr, matrix, pixmap->Ptr));
  38. }
  39. static Device^ NewBox(Pixmap^ pixmap, Box box) {
  40. pin_ptr<Box> p = &box;
  41. return TryCreateDevice(fz_new_bbox_device(Context::Ptr, (fz_rect*)p));
  42. }
  43. static Device^ NewStructureText(TextPage^ textPage) {
  44. return TryCreateDevice(fz_new_stext_device(Context::Ptr, textPage->Ptr, NULL));
  45. }
  46. static Device^ NewStructureText(TextPage^ textPage, TextOptions^ options) {
  47. auto o = (fz_stext_options)options;
  48. return TryCreateDevice(fz_new_stext_device(Context::Ptr, textPage->Ptr, &o));
  49. }
  50. void EnableDeviceHints(DeviceHints hints) {
  51. fz_enable_device_hints(Context::Ptr, _device, (int)hints);
  52. }
  53. void DisableDeviceHints(DeviceHints hints) {
  54. fz_enable_device_hints(Context::Ptr, _device, (int)hints);
  55. }
  56. void Close() {
  57. if (!CloseDevice(Context::Ptr, _device)) {
  58. throw MuException::FromContext();
  59. }
  60. }
  61. private:
  62. fz_device* _device;
  63. void ReleaseHandle() {
  64. CloseDevice(Context::Ptr, _device);
  65. fz_drop_device(Context::Ptr, _device);
  66. _device = NULL;
  67. }
  68. static Device^ TryCreateDevice(fz_device* device) {
  69. if (device) {
  70. return gcnew Device(device);
  71. }
  72. throw MuException::FromContext();
  73. }
  74. };
  75. };
  76. #endif