consts.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # SPDX-FileCopyrightText: 2024 geisserml <geisserml@gmail.com>
  2. # SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
  3. import pypdfium2.raw as pdfium_c
  4. from pypdfium2.version import PDFIUM_INFO
  5. class _fallback_dict (dict):
  6. def get(self, key, default_prefix="Unhandled constant"):
  7. return dict.get(self, key, f"{default_prefix} {key}")
  8. #: Convert a rotation value in degrees to a PDFium constant.
  9. RotationToConst = {
  10. 0: 0,
  11. 90: 1,
  12. 180: 2,
  13. 270: 3,
  14. }
  15. #: Convert a PDFium rotation constant to a value in degrees. Inversion of :data:`.RotationToConst`.
  16. RotationToDegrees = {v: k for k, v in RotationToConst.items()}
  17. #: Get the number of channels for a PDFium bitmap format. (:attr:`FPDFBitmap_Unknown` is deliberately not handled.)
  18. BitmapTypeToNChannels = {
  19. pdfium_c.FPDFBitmap_Gray: 1,
  20. pdfium_c.FPDFBitmap_BGR: 3,
  21. pdfium_c.FPDFBitmap_BGRA: 4,
  22. pdfium_c.FPDFBitmap_BGRx: 4,
  23. }
  24. #: Convert a PDFium bitmap format to string, assuming BGR byte order. (:attr:`FPDFBitmap_Unknown` is deliberately not handled.)
  25. BitmapTypeToStr = {
  26. pdfium_c.FPDFBitmap_Gray: "L",
  27. pdfium_c.FPDFBitmap_BGR: "BGR",
  28. pdfium_c.FPDFBitmap_BGRA: "BGRA",
  29. pdfium_c.FPDFBitmap_BGRx: "BGRX",
  30. }
  31. #: Convert a PDFium bitmap format to string, assuming RGB byte order. (:attr:`FPDFBitmap_Unknown` is deliberately not handled.)
  32. BitmapTypeToStrReverse = {
  33. pdfium_c.FPDFBitmap_Gray: "L",
  34. pdfium_c.FPDFBitmap_BGR: "RGB",
  35. pdfium_c.FPDFBitmap_BGRA: "RGBA",
  36. pdfium_c.FPDFBitmap_BGRx: "RGBX",
  37. }
  38. #: Convert a string to PDFium bitmap format, assuming BGR byte order. Inversion of :data:`BitmapTypeToStr`.
  39. BitmapStrToConst = {v: k for k, v in BitmapTypeToStr.items()}
  40. #: Convert a string to PDFium bitmap format, assuming RGB byte order. Inversion of :data:`BitmapTypeToStrReverse`.
  41. BitmapStrReverseToConst = {v: k for k, v in BitmapTypeToStrReverse.items()}
  42. #: Convert a PDFium form type (:attr:`FORMTYPE_*`) to string.
  43. FormTypeToStr = _fallback_dict({
  44. pdfium_c.FORMTYPE_NONE: "None",
  45. pdfium_c.FORMTYPE_ACRO_FORM: "AcroForm",
  46. pdfium_c.FORMTYPE_XFA_FULL: "XFA",
  47. pdfium_c.FORMTYPE_XFA_FOREGROUND: "XFAF",
  48. })
  49. #: Convert a PDFium color space constant (:attr:`FPDF_COLORSPACE_*`) to string.
  50. ColorspaceToStr = _fallback_dict({
  51. pdfium_c.FPDF_COLORSPACE_UNKNOWN: "?",
  52. pdfium_c.FPDF_COLORSPACE_DEVICEGRAY: "DeviceGray",
  53. pdfium_c.FPDF_COLORSPACE_DEVICERGB: "DeviceRGB",
  54. pdfium_c.FPDF_COLORSPACE_DEVICECMYK: "DeviceCMYK",
  55. pdfium_c.FPDF_COLORSPACE_CALGRAY: "CalGray",
  56. pdfium_c.FPDF_COLORSPACE_CALRGB: "CalRGB",
  57. pdfium_c.FPDF_COLORSPACE_LAB: "Lab",
  58. pdfium_c.FPDF_COLORSPACE_ICCBASED: "ICCBased",
  59. pdfium_c.FPDF_COLORSPACE_SEPARATION: "Separation",
  60. pdfium_c.FPDF_COLORSPACE_DEVICEN: "DeviceN",
  61. pdfium_c.FPDF_COLORSPACE_INDEXED: "Indexed", # i. e. palettized
  62. pdfium_c.FPDF_COLORSPACE_PATTERN: "Pattern",
  63. })
  64. #: Convert a PDFium view mode constant (:attr:`PDFDEST_VIEW_*`) to string.
  65. ViewmodeToStr = _fallback_dict({
  66. pdfium_c.PDFDEST_VIEW_UNKNOWN_MODE: "?",
  67. pdfium_c.PDFDEST_VIEW_XYZ: "XYZ",
  68. pdfium_c.PDFDEST_VIEW_FIT: "Fit",
  69. pdfium_c.PDFDEST_VIEW_FITH: "FitH",
  70. pdfium_c.PDFDEST_VIEW_FITV: "FitV",
  71. pdfium_c.PDFDEST_VIEW_FITR: "FitR",
  72. pdfium_c.PDFDEST_VIEW_FITB: "FitB",
  73. pdfium_c.PDFDEST_VIEW_FITBH: "FitBH",
  74. pdfium_c.PDFDEST_VIEW_FITBV: "FitBV",
  75. })
  76. #: Convert a PDFium object type constant (:attr:`FPDF_PAGEOBJ_*`) to string.
  77. ObjectTypeToStr = _fallback_dict({
  78. pdfium_c.FPDF_PAGEOBJ_UNKNOWN: "?",
  79. pdfium_c.FPDF_PAGEOBJ_TEXT: "text",
  80. pdfium_c.FPDF_PAGEOBJ_PATH: "path",
  81. pdfium_c.FPDF_PAGEOBJ_IMAGE: "image",
  82. pdfium_c.FPDF_PAGEOBJ_SHADING: "shading",
  83. pdfium_c.FPDF_PAGEOBJ_FORM: "form",
  84. })
  85. #: Convert an object type string to a PDFium constant. Inversion of :data:`.ObjectTypeToStr`.
  86. ObjectTypeToConst = {v: k for k, v in ObjectTypeToStr.items()}
  87. #: Convert a PDFium page mode constant (:attr:`PAGEMODE_*`) to string.
  88. PageModeToStr = _fallback_dict({
  89. pdfium_c.PAGEMODE_UNKNOWN: "?",
  90. pdfium_c.PAGEMODE_USENONE: "None",
  91. pdfium_c.PAGEMODE_USEOUTLINES: "Outline",
  92. pdfium_c.PAGEMODE_USETHUMBS: "Thumbnails",
  93. pdfium_c.PAGEMODE_FULLSCREEN: "Full-screen",
  94. pdfium_c.PAGEMODE_USEOC: "Layers",
  95. pdfium_c.PAGEMODE_USEATTACHMENTS: "Attachments",
  96. })
  97. #: Convert a PDFium error constant (:attr:`FPDF_ERR_*`) to string.
  98. ErrorToStr = _fallback_dict({
  99. pdfium_c.FPDF_ERR_SUCCESS: "Success",
  100. pdfium_c.FPDF_ERR_UNKNOWN: "Unknown error",
  101. pdfium_c.FPDF_ERR_FILE: "File access error",
  102. pdfium_c.FPDF_ERR_FORMAT: "Data format error",
  103. pdfium_c.FPDF_ERR_PASSWORD: "Incorrect password error",
  104. pdfium_c.FPDF_ERR_SECURITY: "Unsupported security scheme error",
  105. pdfium_c.FPDF_ERR_PAGE: "Page not found or content error",
  106. })
  107. # known implication: causes eager evaluation of pdfium version
  108. if "XFA" in PDFIUM_INFO.flags:
  109. #: [V8/XFA builds only] Convert a PDFium XFA error constant (:attr:`FPDF_ERR_XFA*`) to string.
  110. XFAErrorToStr = _fallback_dict({
  111. pdfium_c.FPDF_ERR_XFALOAD: "Load error",
  112. pdfium_c.FPDF_ERR_XFALAYOUT: "Layout error",
  113. })
  114. #: Convert a PDFium unsupported constant (:attr:`FPDF_UNSP_*`) to string.
  115. UnsupportedInfoToStr = _fallback_dict({
  116. pdfium_c.FPDF_UNSP_DOC_XFAFORM: "XFA form",
  117. pdfium_c.FPDF_UNSP_DOC_PORTABLECOLLECTION: "Portable collection",
  118. pdfium_c.FPDF_UNSP_DOC_ATTACHMENT: "Attachment (incomplete support)", # https://crbug.com/pdfium/1945
  119. pdfium_c.FPDF_UNSP_DOC_SECURITY: "Security",
  120. pdfium_c.FPDF_UNSP_DOC_SHAREDREVIEW: "Shared review",
  121. pdfium_c.FPDF_UNSP_DOC_SHAREDFORM_ACROBAT: "Shared form (acrobat)",
  122. pdfium_c.FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM: "Shared form (filesystem)",
  123. pdfium_c.FPDF_UNSP_DOC_SHAREDFORM_EMAIL: "Shared form (email)",
  124. pdfium_c.FPDF_UNSP_ANNOT_3DANNOT: "3D annotation",
  125. pdfium_c.FPDF_UNSP_ANNOT_MOVIE: "Movie annotation",
  126. pdfium_c.FPDF_UNSP_ANNOT_SOUND: "Sound annotation",
  127. pdfium_c.FPDF_UNSP_ANNOT_SCREEN_MEDIA: "Screen media annotation",
  128. pdfium_c.FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA: "Screen rich media annotation",
  129. pdfium_c.FPDF_UNSP_ANNOT_ATTACHMENT: "Attachment annotation",
  130. pdfium_c.FPDF_UNSP_ANNOT_SIG: "Signature annotation",
  131. })