THotPDF.AssignSyntheticCodepointForGID / GetSyntheticCodepointForGID

THotPDF PUA 合成碼點配置器 (v2.119.68)

 

GSUB Engine  Auto Shaping Pipeline  Arabic Shaping

為沒有自然 Unicode codepoint 可透過字型 cmap 抵達的 OpenType GSUB substitute GIDs,設定並查詢 Private Use Area(U+E000 - U+F8FF)synthetic codepoints。這補上 v2.119.43-66 GSUB query 與 refinement APIs 留下的 producer-side GID-level emission gap

 

Delphi 語法:

function AssignSyntheticCodepointForGID(GID: Word; out SyntheticCP: Word): Boolean;

function GetSyntheticCodepointForGID(GID: Word): Word;

 

Why the API exists

v2.119.32-67 producer-side automatic shaping pipeline(Arabic / Latin / Devanagari)要求 GSUB engine 回傳的 substitute GIDs 可透過 Unicode codepoint 抵達;既有 hex-encoded text pipeline 輸出的是 codepoints 而非 GIDs,閱讀端會透過內嵌的 /CIDToGIDMap 把 codepoint 解析回 GID。對於可透過字型 cmap 取得自然 Unicode codepoint 的 substitute GIDs(Arabic Presentation Forms、Latin Standard Ligatures FB00-FB06),既有 pipeline 可正常運作

 

但落在 font-internal GIDs 上的 font-specific substitutes,例如多數 Devanagari cluster shapes、字型設計者只以 numbered GIDs 提供的 stylistic alternates、CJK ideographic variation sequences(IVS)、沒有對應 Presentation Form 的 discretionary ligatures,在字型 cmap 中完全沒有 codepoint。v2.119.68 之前,這些 GIDs 無法透過 producer-side hex pipeline 抵達;v2.119.68 允許呼叫端為任意 GID 在 Private Use Area 設定 synthetic codepoint,以補上此缺口

 

AssignSyntheticCodepointForGID semantics

為提供的 GID 設定下一個可用 PUA codepoint(從 U+E000 開始),並將設定鏡射到既有 producer-side hex pipeline + consumer-reader resolution chain 所依賴的每個 cache:

 

1. FUnicodeCpToGid[SyntheticCP] := GID - so the producer-side hex pipeline emits SyntheticCP into the text-showing operator and the consumer reader resolves SyntheticCP back to GID through /CIDToGIDMap at render time.

2. FAcroFormUnicodeAdvances[SyntheticCP] := em-fraction - so the v2.65 word-wrap calculator finds the correct hmtx advance for the synthetic codepoint when it appears in AcroForm text-field content.

3. FUnicodeSyntheticCpForGID[GID] := SyntheticCP - the per-GID reverse-lookup table used by GetSyntheticCodepointForGID to make repeat AssignSyntheticCodepointForGID calls idempotent (the second call with the same GID returns the already-allocated SyntheticCP).

 

成功時傳回 True,並把 SyntheticCP 設為分配到的 codepoint。遇下列防禦性條件時傳回 False(並把 SyntheticCP 留在 0):尚未註冊字型(RegisterUnicodeTTF 從未呼叫,或以空引數呼叫以重設狀態)、GID 無效(為零或超出 cmap 的 glyph 數)、PUA 範圍用盡(U+E000 - U+F8FF 的 6400 個 slot 全數分配)、進入時快取尚未初始化。

 

GetSyntheticCodepointForGID semantics

對既有分配做純函式查詢。若先前呼叫過 AssignSyntheticCodepointForGID(GID, ...),就傳回該 GID 分配到的 synthetic codepoint;否則傳回 0(0 不是有效的 PUA codepoint,因此同時可作為「尚未分配」的哨兵值)。此方法不會進行分配。在任何 AssignSyntheticCodepointForGID 執行前呼叫都是安全的。

 

Allocator state lifecycle

FUnicodeSyntheticCpForGID 與 next-available-PUA cursor(FUnicodeNextSyntheticCp)會在首次 AssignSyntheticCodepointForGID 呼叫時才延遲配置。cursor 起始為 0(未初始化),第一次配置時跳到 $E000;後續配置依序移到 $E001、$E002、…、$F8FF。這兩個欄位會在每次 RegisterUnicodeTTF('', nil) 時連同其餘 per-font subset 狀態一併重設,因此在多份文件間重複使用同一個 THotPDF 實例時,每份文件都從全新的 PUA cursor 開始。

 

Typical workflow (Devanagari cluster shape)

 

PDF.RegisterUnicodeTTF('NotoDeva', 'NotoSansDevanagari-Regular.ttf');

PDF.ShapingFeatures := [sfIndicShaping];

PDF.SetGSUBScript('deva');

 

// Get a font-internal cluster GID through the GSUB engine

ClusterGID := PDF.GetSingleSubstituteGlyph(BaseGID, 'nukt');

if ClusterGID <> BaseGID then

begin

  // Check if cmap reaches the substitute - usually no for Indic

  // clusters, since cluster GIDs are font-internal

  // Allocate a synthetic codepoint that the producer-side

  // hex pipeline can emit

  if PDF.AssignSyntheticCodepointForGID(ClusterGID, SyntheticCP) then

  begin

    // SyntheticCP is now in the U+E000-F8FF range; emit it

    // through UnicodeTextOut just like a normal codepoint

    PDF.CurrentPage.UnicodeTextOut(X, Y, 0, UnicodeChar(SyntheticCP));

    PDF.MarkUnicodeGlyphUsed(ClusterGID);

  end;

end;

 

冪等性範例

 

PDF.AssignSyntheticCodepointForGID(150, CP1);  // CP1 = $E000

PDF.AssignSyntheticCodepointForGID(151, CP2);  // CP2 = $E001

PDF.AssignSyntheticCodepointForGID(150, CP3);  // CP3 = $E000 (idempotent)

CP4 := PDF.GetSyntheticCodepointForGID(150);  // CP4 = $E000

CP5 := PDF.GetSyntheticCodepointForGID(999);  // CP5 = 0 (no assignment)

 

Consumer-reader behavior

閱讀端會在 text-showing operator 中看到 PUA codepoint,並透過內嵌於文件的 /CIDToGIDMap 解析回目標 GID,再用內嵌字型程式 render 該 GID。從閱讀端的角度來看,cmap 指向 GID 的「自然」Unicode codepoint,與 /CIDToGIDMap 指向 GID 的 PUA synthetic codepoint 並無差別,兩者都產生相同的 rendered glyph。

 

複製 / 貼上行為:當 ToUnicode CMap 把 PUA codepoint 宣告為 identity mapping 時,PUA codepoint 會以自身原樣往返。若呼叫端希望改為還原來源的 Unicode 字元(也就是產生 substitute 的輸入 run),可以用 RegisterToUnicodeReverseMapping 註冊反查對應,或透過 BeginTaggedContent 撰寫 ActualText marked-content 序列屬性,並在括住的內容中輸出 synthetic codepoints。對於含補充平面 Unicode 字元、以 RegisterUnicodeTTF 為後端的 AcroForm appearance streams,HotPDF 會自動套用相同的 internal-CID 模式。

 

Phase 8 roadmap closure

v2.119.68 / Phase 8c.6 收束了 Phase 8 GSUB engine 的roadmap:LookupType 1-8 的查詢 API(Phase 1-6)、Script / LangSys 選擇 API(Phase 7)、TTF subsetter closure 進入點(Phase 9)、靜態 post-pass 連字摺疊(v2.119.32 / 58 / 60 / 62)、需自行啟用的自動 pipeline(v2.119.59)、阿拉伯文 rlig + 拉丁文 liga / clig + rclt 的自動輸出(Phase 8b / 8c.2 / 8b / GSUB 'rclt')、ToUnicode 反查對應(v2.119.61 / 62 / 65)、advance 查詢(v2.119.64)、天城文 Indic 重排 pre-pass(v2.119.67),以及現在的 PUA synthetic codepoint GID 層輸出(v2.119.68),全部整合進單一 producer-side shaping 介面,涵蓋 OpenType 字型可能產生的各種 substitute glyph。

 

請參閱: OpenType GSUB Substitution Engine, Automatic Shaping Pipeline (Phase 8), Arabic / Persian / Urdu Shaping Support, Syriac / Mongolian / Devanagari Shaping, THotPDF.BeginTaggedContent