THotPDF.SetFormUnicodeFontDict Method

 

THotPDF.SetFormUnicodeFontDict

THotPDF

 

先頭

caller-supplied Unicode font を AcroForm default-resources dict に登録し、HotPDF の auto-appearance-stream generator (PDF 1.7 ISO 32000-1 12.7.4.3) が non-ASCII Tx widget initial values を v2.46.0 の empty-AP placeholder ではなく直接 /AP に render できるようにします。populate する indirect font dict を allocate するには CreateIndirectFontDict と組み合わせます

 

Delphi syntax:

procedure SetFormUnicodeFontDict(const LogicalName: AnsiString; FontDict: THPDFDictionaryObject);

 

C++ syntax:

void SetFormUnicodeFontDict(const AnsiString& LogicalName, THPDFDictionaryObject* FontDict);

 

説明

Once both LogicalName and FontDict are populated, three things change in subsequent emission: (a) the AcroForm-level /DA string switches from '/Helv 12 Tf 0 0 0 rg' to '/<LogicalName> 12 Tf 0 0 0 rg'; (b) the AcroForm /DR /Font sub-dict gains an entry /<LogicalName> <FontDict-indirect-ref> alongside the existing /Helv + /ZaDb; (c) every Tx widget added through AddTextField or AddRichTextField gets its per-field /DA set the same way. When AutoFormAppearances is on and the widget's initial value contains any byte >= 0x80, GenerateTextFieldAP emits a real /AP /N Form XObject whose content stream selects the registered font via Tf and renders the text as a CID hex-string Tj operator; the Form's /Resources/Font sub-dict references the same indirect font so the AP is self-contained.

registered font が RegisterUnicodeTTF 由来の場合、supplementary-plane Unicode characters (例: U+1F600) は UTF-16 surrogate pair を 2 つの Identity-H CIDs に分割せず、内部的に割り当てられた single CID 経由で出力されます。HotPDF は finalization time に generated /CIDToGIDMap/W/ToUnicode streams を refresh するため、internal CID は実際の font glyph に map され、text extraction は元の UTF-16BE surrogate pair に戻ります

ASCII Tx fields は引き続き /Helv を使用し、v2.46.0 callers と byte-identical な /AP output を出力します。empty LogicalName + nil FontDict を渡すと registration を clear し、/Helv-only behavior に戻ります。typical workflow は、BeginDoc の直後、AddTextField の前に SetFormUnicodeFontDict を 1 回呼び出すことです。先に作成された widgets は retroactively updated されません

 

LogicalName - /DR/Font 内の key と、/DA string 内の Tf operand として使用される PDF Name です。典型例は 'F0' です。registration 時には empty にできません。non-nil FontDict と empty name の組み合わせは raise します

FontDict - an indirect THPDFDictionaryObject holding a valid PDF Font resource. The caller is responsible for building the font dict correctly - typically a Type 0 / CIDFontType2 + Identity-H + ToUnicode CMap composite font; for stroked-only or simple display use a Type 1 / TrueType subset works too. HotPDF does not validate the contents; it just serialises the reference.

 

Scope - v2.56.0 covers single-line Tx widgets only. Multi-line (ffMultiline) and comb (ffComb) Tx widgets with non-ASCII initial values still emit the v2.46.0 empty-AP placeholder; the Unicode-AP extension for those branches is scheduled for v2.57.0+. RTL bidi shaping (Arabic / Hebrew) requires Unicode Bidi Algorithm (UAX #9) + Arabic contextual joining; also v2.57.0+ scope.

 

Code Example

// Register a Type 0 / CIDFontType2 + Identity-H composite font for
// AcroForm Tx widget AP rendering. (Real production code adds a
// /FontFile2 stream and a ToUnicode CMap so the glyphs actually
// render - omitted here for brevity.)
var
  F0, Descendant, FontDescriptor, CIDSysInfo: THPDFDictionaryObject;
  Descendants: THPDFArrayObject;
  R: TRect;
begin
  HPDF.BeginDoc;
  HPDF.AutoFormAppearances := True;

  F0 := HPDF.CreateIndirectFontDict;          // empty indirect dict
  F0.AddNameValue('Type', 'Font');
  F0.AddNameValue('Subtype', 'Type0');
  F0.AddNameValue('BaseFont', 'MyCJK');
  F0.AddNameValue('Encoding', 'Identity-H');

  CIDSysInfo := THPDFDictionaryObject.Create(nil);
  CIDSysInfo.AddStringValue('Registry', 'Adobe');
  CIDSysInfo.AddStringValue('Ordering', 'Identity');
  CIDSysInfo.AddNumericValue('Supplement', 0);

  FontDescriptor := THPDFDictionaryObject.Create(nil);
  FontDescriptor.AddNameValue('Type', 'FontDescriptor');
  // ... fill required Ascent / Descent / FontBBox / etc

  Descendant := THPDFDictionaryObject.Create(nil);
  Descendant.AddNameValue('Type', 'Font');
  Descendant.AddNameValue('Subtype', 'CIDFontType2');
  Descendant.AddNameValue('BaseFont', 'MyCJK');
  Descendant.AddValue('CIDSystemInfo', CIDSysInfo);
  Descendant.AddValue('FontDescriptor', FontDescriptor);
  Descendant.AddNumericValue('DW', 1000);

  Descendants := THPDFArrayObject.Create(nil);
  Descendants.AddObject(Descendant);
  F0.AddValue('DescendantFonts', Descendants);

  HPDF.SetFormUnicodeFontDict('F0', F0);

  R.Left := 60; R.Top := 60; R.Right := 280; R.Bottom := 100;
  HPDF.CurrentPage.AddTextField('Greeting', '你好世界', R);

  HPDF.EndDoc;
end;

 

関連項目

CreateIndirectFontDict, AutoFormAppearances, AddTextField, AddRichTextField, Version