技術記事

PDFium Component: Delphi での word tracking and speech cursoring

Delphi/C++Builder には PDFium VCL Component のワークフローを、Lazarus/FPC には PDFium LCL Component のワークフローを組み込み、表示、レンダリング、フォーム、印刷、プリフライトレポート、標準対応の検証をソースコード付きコンポーネントで実装できます。

この記事は developers building read-aloud, study, review, or assisted-reading features in Delphi PDF viewers 向けです。word tracking and speech cursoring を単なるコンポーネント呼び出しではなく、本番向けのドキュメントエンジニアリングとして扱います。

実務上のリスクは speech highlighting becomes distracting or wrong when word boxes, ligatures, rotation, hidden text, and timing do not match the spoken stream です。そのため、明確な契約、観測可能な診断、実際の顧客ファイルに近い回帰サンプルが必要です。

アーキテクチャ上の判断

Synchronize speech with extracted text, not pixels alone. text extraction source, word segmentation policy, and language assumptions / highlight style, scroll behavior, pause and resume rules, and user controls

  • text extraction source, word segmentation policy, and language assumptions
  • highlight style, scroll behavior, pause and resume rules, and user controls
  • fallback behavior for image-only pages, hidden text, ligatures, and rotated content
  • whether speech timing is driven by the engine, the viewer, or a shared coordinator

実装フロー

Track words through a stable text map. The order below keeps the workflow reviewable for Delphi and C++Builder teams.

  1. extract page text and word bounds before speech playback begins
  2. build a word map that links text offsets, page numbers, and viewer coordinates
  3. call the tracking layer as the speech engine advances through words or offsets
  4. scroll the viewport only when the current word leaves the comfortable reading zone
  5. record skipped words, missing boxes, and timing drift for diagnostics

検証エビデンス

Synchronization evidence for assisted reading. Keep these fields with the output or support record.

  • page number, word text, text offset, bounding box, and speech timestamp
  • tracking method used, including TrackReadingWordAt when the viewer supports it
  • fallback reason for words without reliable geometry
  • latency between speech event and visible cursor update

Word boxes are viewer data

Word-synchronized reading needs a mapping between extracted text, page coordinates, and speech timing. A robust implementation knows when text is unavailable, when a word box is ambiguous, and how to keep the viewport aligned with the current spoken word.

Review questions before release

Before this reaches production, the team should be able to answer these questions without reading source code.

  • Who owns text extraction source, word segmentation policy, and language assumptions?
  • What evidence proves page number, word text, text offset, bounding box, and speech timestamp?
  • What happens when ligatures can represent multiple characters inside one visual glyph?
  • Which regression file covers record skipped words, missing boxes, and timing drift for diagnostics?

Engineering review notes for word tracking and speech cursoring

Use these review notes to make sure the feature has moved beyond a demo and can be defended during release, support, and customer escalation.

  • Decision: text extraction source, word segmentation policy, and language assumptions. Implementation pressure point: build a word map that links text offsets, page numbers, and viewer coordinates. Acceptance evidence: fallback reason for words without reliable geometry. Regression trigger: rapid speech rates can require coalescing highlight updates to avoid flicker
  • Decision: highlight style, scroll behavior, pause and resume rules, and user controls. Implementation pressure point: call the tracking layer as the speech engine advances through words or offsets. Acceptance evidence: latency between speech event and visible cursor update. Regression trigger: ligatures can represent multiple characters inside one visual glyph
  • Decision: fallback behavior for image-only pages, hidden text, ligatures, and rotated content. Implementation pressure point: scroll the viewport only when the current word leaves the comfortable reading zone. Acceptance evidence: page number, word text, text offset, bounding box, and speech timestamp. Regression trigger: rotated or vertical text needs coordinate handling separate from normal pages

境界ケース

  • ligatures can represent multiple characters inside one visual glyph
  • rotated or vertical text needs coordinate handling separate from normal pages
  • OCR text layers may not align perfectly with scanned images
  • rapid speech rates can require coalescing highlight updates to avoid flicker

Delphi / C++Builder notes

PDFium Component should sit behind a small service boundary that receives files, streams, profiles, and credentials, then returns output paths, warnings, metrics, and validation status. Important terms include TrackReadingWordAt, speech cursor, word bounds, text extraction, highlight, reading assistance.

Delphi コード例

次の Delphi スケッチは、このテーマに対する実用的なサービス境界を示します。ポリシー確認、ログ記録、検証を製品呼び出しの狭い部分の外側に置くと、ワークフローをテストしやすくなります。

procedure TSpeechForm.SpeakWordAtCursor(PageNo, CharIndex: Integer);
var
  UnitInfo: TReadingUnit;
begin
  UnitInfo := LocateWordUnit(PdfView, PageNo, CharIndex);
  HighlightBounds(UnitInfo.PageNo, UnitInfo.Bounds);
  SpeechQueue.Speak(UnitInfo.Text);
  StoreCursorPosition(UnitInfo.PageNo, UnitInfo.EndChar);
end;

本番チェックリスト

  • Run the workflow on an empty file, a normal customer file, and a worst-case file
  • Open the generated PDF with the target viewer, validator, printer, or downstream application
  • Log product version, profile version, input hash, output path, elapsed time, and warning count
  • Keep passwords, certificates, temporary files, and customer data under explicit retention rules
  • Add regression documents when a customer file exposes a new edge case

Product documentation

PDFium Component