Token-Preserving Content-Stream AST

HPDFContentStream은 디코딩된 PDF content stream을 대상 편집하기 위한 bounded 무손실 syntax 모델을 제공합니다

파싱과 검사

HPDFParseContentStreamAST는 4,194,304 lexeme, 256 중첩 container라는 기본 한도로 THPDFContentStreamAST를 만듭니다. 더 타이트한 한도가 필요한 호출자는 THPDFContentStreamAST.Create를 직접 호출할 수 있습니다

THPDFContentLexemeKind는 공백, 주석, 숫자, 이름, literal/hexadecimal string, boolean, null, word, container 구분자, inline image 데이터, malformed 바이트를 구분합니다. 모든 입력 바이트는 정확히 하나의 THPDFContentLexeme에 속합니다

THPDFContentSyntaxNodeKind는 root, operation, operand, array, dictionary, procedure, inline image, malformed 영역을 구분합니다. 각 THPDFContentSyntaxNode는 parent, first-child, last-child, next-sibling 인덱스를 쓰므로 순회 시 자식 array를 할당하지 않습니다

GetLexeme, GetLexemeBytes, GetLexemeText, GetNode를 LexemeCount, NodeCount와 함께 쓰고, GetOriginalBytes는 독립적인 소스 복사본을 돌려줍니다

Valid, ErrorOffset, ErrorMessage는 byte 단위로 동일한 serialization을 유지한 채 malformed 입력을 보고하고, GetStatistics는 소스 크기, 개수, 최대 깊이, inline image 수, malformed 노드 수를 THPDFContentASTStatistics에 채웁니다

편집 overlay

ReplaceRange는 half-open byte offset을 쓰고, ReplaceLexeme, ReplaceNode, DeleteNode, InsertBeforeNode, InsertAfterNode는 syntax 레코드에서 범위를 유도합니다

각 THPDFContentEdit은 정렬된 overlay로 복사됩니다. 충돌하는 편집은 거부되고, 인접 범위는 유효하며, EditCount는 받아들인 편집 수를 보고하고, ClearEdits는 no-op 출력으로 되돌립니다

연산자 visitor

HPDFVisitContentStreamOperators는 이 AST 위에 reference counted visitor와 Delphi event callback을 얹고, 읽기 전용 operand view를 제공하며, 소스/연산자/lexeme/깊이/교체 budget을 강제하고, 성공 전에 변환된 출력을 재파싱합니다

직렬화

Serialize는 overflow 검사를 마친 크기 계산 후 출력 byte array 하나를 만들고, WriteToStream은 손대지 않은 소스 구간과 교체 구간을 호출자 소유 TStream에 바로 씁니다

편집이 없으면 어느 쪽 경로든 숫자 표기, string escape, 주석, 줄 끝, 구분자, inline image payload를 바꾸지 않고 소스를 바이트 단위로 그대로 재현합니다

var
  AST: THPDFContentStreamAST;
  Output: TBytes;
begin
  AST:= HPDFParseContentStreamAST(ContentBytes);
  try
    if AST.Valid and AST.ReplaceRange(StartOfs, EndOfs, ReplacementBytes) then
      AST.Serialize(Output);
  finally
    AST.Free;
  end;
end;