class_docstr: Manages grafting text-only PDFs onto regular PDFs.
class_docstr_tok: ["Manages","grafting","text-only","PDFs","onto","regular","PDFs","."]
code: def graft_page(
      self,
      *,
      pageno: int,
      image: Path | None,
      textpdf: Path | None,
      autorotate_correction: int,
  ):
      if textpdf and not self.font:
          self.font, self.font_key = self._find_font(textpdf)
  
      emplaced_page = False
      content_rotation = self.pdfinfo[pageno].rotation
      path_image = Path(image).resolve() if image else None
      if path_image is not None and path_image != self.path_base:
          # We are updating the old page with a rasterized PDF of the new
          # page (without changing objgen, to preserve references)
          log.debug("Emplacement update")
          with Pdf.open(path_image) as pdf_image:
              self.emplacements += 1
              foreign_image_page = pdf_image.pages[0]
              self.pdf_base.pages.append(foreign_image_page)
              local_image_page = self.pdf_base.pages[-1]
              self.pdf_base.pages[pageno].emplace(
                  local_image_page, retain=(Name.Parent,)
              )
              del self.pdf_base.pages[-1]
          emplaced_page = True
  
      # Calculate if the text is misaligned compared to the content
      if emplaced_page:
          content_rotation = autorotate_correction
      text_rotation = autorotate_correction
      text_misaligned = (text_rotation - content_rotation) % 360
      log.debug(
          f"Text rotation: (text, autorotate, content) -> text misalignment = "
          f"({text_rotation}, {autorotate_correction}, {content_rotation}) -> "
          f"{text_misaligned}"
      )
  
      if textpdf and self.font:
          if self.font_key is None:
              raise ValueError("Font key is not set")
          # Graft the text layer onto this page, whether new or old, possibly
          # rotating the text layer by the amount is misaligned.
          strip_old = self.context.options.redo_ocr
          self._graft_text_layer(
              page_num=pageno + 1,
              textpdf=textpdf,
              font=self.font,
              font_key=self.font_key,
              text_rotation=text_misaligned,
              strip_old_text=strip_old,
          )
  
      # Correct the overall page rotation if needed, now that the text and content
      # are aligned
      page_rotation = (content_rotation - autorotate_correction) % 360
      self.pdf_base.pages[pageno].Rotate = page_rotation
      log.debug(
          f"Page rotation: (content, auto) -> page = "
          f"({content_rotation}, {autorotate_correction}) -> {page_rotation}"
      )
      if self.emplacements % MAX_REPLACE_PAGES == 0:
          self.save_and_reload()
  
file_path: OCRmyPDF/src/ocrmypdf/_graft.py
imports: from __future__ import annotations
  import logging
  from contextlib import suppress
  from enum import Enum
  from pathlib import Path
  from pikepdf import Dictionary, Matrix, Name, Operator, Page, Pdf, PdfError, Stream, parse_content_stream, unparse_content_stream
  from ocrmypdf._jobcontext import PdfContext
label_desc: Use image node_id 2 for calling the OcrGrafter obj's underlying member method code with example usage: obj.graft_page() without return types