consts.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # SPDX-FileCopyrightText: 2026 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_BGRx: 4,
  22. pdfium_c.FPDFBitmap_BGRA: 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_BGRx: "BGRX",
  29. pdfium_c.FPDFBitmap_BGRA: "BGRA",
  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_BGRx: "RGBX",
  36. pdfium_c.FPDFBitmap_BGRA: "RGBA",
  37. }
  38. if PDFIUM_INFO.build >= 7098:
  39. # New pixel format FPDFBitmap_BGRA_Premul
  40. # Skia-only at the time of writing. Added for completeness and to satisfy the test suite.
  41. BitmapTypeToNChannels[pdfium_c.FPDFBitmap_BGRA_Premul] = 4
  42. BitmapTypeToStr[pdfium_c.FPDFBitmap_BGRA_Premul] = "BGRa"
  43. BitmapTypeToStrReverse[pdfium_c.FPDFBitmap_BGRA_Premul] = "RGBa"
  44. # TODO consider a bi-directional dict in the future?
  45. #: Convert a string to PDFium bitmap format, assuming BGR byte order. Inversion of :data:`BitmapTypeToStr`.
  46. BitmapStrToConst = {v: k for k, v in BitmapTypeToStr.items()}
  47. #: Convert a string to PDFium bitmap format, assuming RGB byte order. Inversion of :data:`BitmapTypeToStrReverse`.
  48. BitmapStrReverseToConst = {v: k for k, v in BitmapTypeToStrReverse.items()}
  49. #: Convert a PDFium form type (:attr:`FORMTYPE_*`) to string.
  50. FormTypeToStr = _fallback_dict({
  51. pdfium_c.FORMTYPE_NONE: "None",
  52. pdfium_c.FORMTYPE_ACRO_FORM: "AcroForm",
  53. pdfium_c.FORMTYPE_XFA_FULL: "XFA",
  54. pdfium_c.FORMTYPE_XFA_FOREGROUND: "XFAF",
  55. })
  56. #: Convert a PDFium color space constant (:attr:`FPDF_COLORSPACE_*`) to string.
  57. ColorspaceToStr = _fallback_dict({
  58. pdfium_c.FPDF_COLORSPACE_UNKNOWN: "?",
  59. pdfium_c.FPDF_COLORSPACE_DEVICEGRAY: "DeviceGray",
  60. pdfium_c.FPDF_COLORSPACE_DEVICERGB: "DeviceRGB",
  61. pdfium_c.FPDF_COLORSPACE_DEVICECMYK: "DeviceCMYK",
  62. pdfium_c.FPDF_COLORSPACE_CALGRAY: "CalGray",
  63. pdfium_c.FPDF_COLORSPACE_CALRGB: "CalRGB",
  64. pdfium_c.FPDF_COLORSPACE_LAB: "Lab",
  65. pdfium_c.FPDF_COLORSPACE_ICCBASED: "ICCBased",
  66. pdfium_c.FPDF_COLORSPACE_SEPARATION: "Separation",
  67. pdfium_c.FPDF_COLORSPACE_DEVICEN: "DeviceN",
  68. pdfium_c.FPDF_COLORSPACE_INDEXED: "Indexed", # i.e. palettized
  69. pdfium_c.FPDF_COLORSPACE_PATTERN: "Pattern",
  70. })
  71. #: Convert a PDFium view mode constant (:attr:`PDFDEST_VIEW_*`) to string.
  72. ViewmodeToStr = _fallback_dict({
  73. pdfium_c.PDFDEST_VIEW_UNKNOWN_MODE: "?",
  74. pdfium_c.PDFDEST_VIEW_XYZ: "XYZ",
  75. pdfium_c.PDFDEST_VIEW_FIT: "Fit",
  76. pdfium_c.PDFDEST_VIEW_FITH: "FitH",
  77. pdfium_c.PDFDEST_VIEW_FITV: "FitV",
  78. pdfium_c.PDFDEST_VIEW_FITR: "FitR",
  79. pdfium_c.PDFDEST_VIEW_FITB: "FitB",
  80. pdfium_c.PDFDEST_VIEW_FITBH: "FitBH",
  81. pdfium_c.PDFDEST_VIEW_FITBV: "FitBV",
  82. })
  83. #: Convert a PDFium object type constant (:attr:`FPDF_PAGEOBJ_*`) to string.
  84. ObjectTypeToStr = _fallback_dict({
  85. pdfium_c.FPDF_PAGEOBJ_UNKNOWN: "?",
  86. pdfium_c.FPDF_PAGEOBJ_TEXT: "text",
  87. pdfium_c.FPDF_PAGEOBJ_PATH: "path",
  88. pdfium_c.FPDF_PAGEOBJ_IMAGE: "image",
  89. pdfium_c.FPDF_PAGEOBJ_SHADING: "shading",
  90. pdfium_c.FPDF_PAGEOBJ_FORM: "form",
  91. })
  92. #: Convert an object type string to a PDFium constant. Inversion of :data:`.ObjectTypeToStr`.
  93. ObjectTypeToConst = {v: k for k, v in ObjectTypeToStr.items()}
  94. #: Convert a PDFium page mode constant (:attr:`PAGEMODE_*`) to string.
  95. PageModeToStr = _fallback_dict({
  96. pdfium_c.PAGEMODE_UNKNOWN: "?",
  97. pdfium_c.PAGEMODE_USENONE: "None",
  98. pdfium_c.PAGEMODE_USEOUTLINES: "Outline",
  99. pdfium_c.PAGEMODE_USETHUMBS: "Thumbnails",
  100. pdfium_c.PAGEMODE_FULLSCREEN: "Full-screen",
  101. pdfium_c.PAGEMODE_USEOC: "Layers",
  102. pdfium_c.PAGEMODE_USEATTACHMENTS: "Attachments",
  103. })
  104. #: Convert a PDFium error constant (:attr:`FPDF_ERR_*`) to string.
  105. ErrorToStr = _fallback_dict({
  106. pdfium_c.FPDF_ERR_SUCCESS: "Success",
  107. pdfium_c.FPDF_ERR_UNKNOWN: "Unknown error",
  108. pdfium_c.FPDF_ERR_FILE: "File access error",
  109. pdfium_c.FPDF_ERR_FORMAT: "Data format error",
  110. pdfium_c.FPDF_ERR_PASSWORD: "Incorrect password error",
  111. pdfium_c.FPDF_ERR_SECURITY: "Unsupported security scheme error",
  112. pdfium_c.FPDF_ERR_PAGE: "Page not found or content error",
  113. })
  114. if "XFA" in PDFIUM_INFO.flags: # pragma: no cover
  115. #: [XFA builds only] Convert a PDFium XFA error constant (:attr:`FPDF_ERR_XFA*`) to string.
  116. XFAErrorToStr = _fallback_dict({
  117. pdfium_c.FPDF_ERR_XFALOAD: "Load error",
  118. pdfium_c.FPDF_ERR_XFALAYOUT: "Layout error",
  119. })
  120. #: Convert a PDFium unsupported constant (:attr:`FPDF_UNSP_*`) to string.
  121. UnsupportedInfoToStr = _fallback_dict({
  122. pdfium_c.FPDF_UNSP_DOC_XFAFORM: "XFA form",
  123. pdfium_c.FPDF_UNSP_DOC_PORTABLECOLLECTION: "Portable collection",
  124. # https://crbug.com/pdfium/1945
  125. pdfium_c.FPDF_UNSP_DOC_ATTACHMENT: "Attachment (incomplete support)",
  126. pdfium_c.FPDF_UNSP_DOC_SECURITY: "Security",
  127. pdfium_c.FPDF_UNSP_DOC_SHAREDREVIEW: "Shared review",
  128. pdfium_c.FPDF_UNSP_DOC_SHAREDFORM_ACROBAT: "Shared form (acrobat)",
  129. pdfium_c.FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM: "Shared form (filesystem)",
  130. pdfium_c.FPDF_UNSP_DOC_SHAREDFORM_EMAIL: "Shared form (email)",
  131. pdfium_c.FPDF_UNSP_ANNOT_3DANNOT: "3D annotation",
  132. pdfium_c.FPDF_UNSP_ANNOT_MOVIE: "Movie annotation",
  133. pdfium_c.FPDF_UNSP_ANNOT_SOUND: "Sound annotation",
  134. pdfium_c.FPDF_UNSP_ANNOT_SCREEN_MEDIA: "Screen media annotation",
  135. pdfium_c.FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA: "Screen rich media annotation",
  136. pdfium_c.FPDF_UNSP_ANNOT_ATTACHMENT: "Attachment annotation",
  137. pdfium_c.FPDF_UNSP_ANNOT_SIG: "Signature annotation",
  138. })