Problem printing unicode bookmark to pdf file

Hi,
I am using Livecode Create 1.0 (DP12) in Classic Mode in macOS Tahoe 26.5.

I could not print unicode bookmarks to an pdf file using the print unicode titleString with level levelNum at locationPoint

I tried in a loop to print a card with a browser widget to pdf and also print unicode bookmarks. In the loop the browser widget is populated with CSS-formatted chords/lyrics for each song in a playlist. Many song names us unicode characters like Alexandra Căpitănescu - Choke Me or Björn Skifs - Michelangelo. In order to print the browser widget i had to create a snapshot and print the image from a “print card”.
Printing the card worked fine but setting the bookmark with these kind of titles failed.

According to the print bookmark documentation

If the unicode adjective is specified, then titleString is interpreted as a UTF-16 encoded string, otherwise it is interpreted as the native (single-byte) text encoding.

I tried every form of textEncode variant but could not find anything that worked.

After consulting AI I managed to find a workaround by injecting bookmarks after completing the print. The code below. The bold faced lines in my code ExportLyricsPdf (shortened) and the AI supported code is the solution (not shortend).

command ExportLyricsPdf
– ask file to get tPdfPath…
– Configure the UI to print…
– Define snapshot window and rect…
– Compensate for macOS toolbar…
– Define pdf rect…
– Set printer options…
– Set upscale image dimensions…
– Get playlist with “Artist - Title”…

  open printing to pdf tPdfPath
  repeat with tSongNr = 1 to tNumSongs
       -- load song into browser widget...
        import snapshot from stack tWinID at size tUpscaledSize
       -- copy image to card tPrintCard...
       print card tPrintCard
       -- Delete snapshot images...
       -- Create the bookmark tBookmark from playlist
       **put tBookmark into pBookmarkList[tSongNr]["title"]**
       **put tSongNr into pBookmarkList[tSongNr]["page"]**
       -- print break (but not on last song)
  end repeat
  close printing
  **injectUnicodeOutlines tPdfPath, pBookmarkList**

end ExportLyricsPdf

– AI supported code below

on injectUnicodeOutlines pFilePath, pBookmarksArray
if there is not a file pFilePath then exit injectUnicodeOutlines

– 1. Read the compiled PDF cleanly as pure binary data
put url (“binfile:” & pFilePath) into tRawPDF
put the length of tRawPDF into tOriginalSize

– Setup layout pointers
put 99500 into tStartObjID
put the number of elements in pBookmarksArray into tCount
put “” into tNewObjects

– A. Create the parent Outline tree structure
put cr & tStartObjID & " 0 obj" & cr after tNewObjects
put “<<” & cr after tNewObjects
put " /Type /Outlines" & cr after tNewObjects
put " /First " & (tStartObjID + 1) & " 0 R" & cr after tNewObjects
put " /Last " & (tStartObjID + tCount) & " 0 R" & cr after tNewObjects
put " /Count " & tCount & cr after tNewObjects
put “>>” & cr & “endobj” & cr after tNewObjects

– B. Loop and build individual interactive bookmarks
repeat with i = 1 to tCount
put pBookmarksArray[i][“title”] into tTitle
put pBookmarksArray[i][“page”] into tPageNum

  -- Convert string title to valid UTF-16BE hex brackets
  put formatUnicodeHexBookmark(tTitle) into tHexTitle
  
  put (tStartObjID + i) & " 0 obj" & cr after tNewObjects
  put "<<" & cr after tNewObjects
  put "  /Title " & tHexTitle & cr after tNewObjects
  put "  /Parent " & tStartObjID & " 0 R" & cr after tNewObjects
  
  if i > 1 then put "  /Prev " & (tStartObjID + i - 1) & " 0 R" & cr after tNewObjects
  if i < tCount then put "  /Next " & (tStartObjID + i + 1) & " 0 R" & cr after tNewObjects
  
  -- Action Block: Safely redirects the viewport to a specific page index number
  -- [tPageNum - 1] handles the 0-indexed layout required for internal PDF page destinations
  put "  /A <<" & cr after tNewObjects
  put "    /Type /Action" & cr after tNewObjects
  put "    /S /GoTo" & cr after tNewObjects
  put "    /D [" & (tPageNum - 1) & " /XYZ 0 2000 null]" & cr after tNewObjects
  put "  >>" & cr after tNewObjects
  put ">>" & cr & "endobj" & cr after tNewObjects

end repeat

– C. Construct an isolated Catalog replacement entry over the file layout
put (tStartObjID + tCount + 1) & " 0 obj" & cr after tNewObjects
put “<<” & cr after tNewObjects
put " /Type /Catalog" & cr after tNewObjects
put " /Outlines " & tStartObjID & " 0 R" & cr after tNewObjects
put " /Pages 2 0 R" & cr after tNewObjects – standard master folder node
put “>>” & cr & “endobj” & cr after tNewObjects

– D. Append standard PDF Incremental Update cross-reference trailer table
put “xref” & cr after tNewObjects
put “0 1” & cr after tNewObjects
put "0000000000 65535 f " & cr after tNewObjects
put tStartObjID & " " & (tCount + 2) & cr after tNewObjects

put “trailer” & cr after tNewObjects
put “<<” & cr after tNewObjects
put " /Size " & (tStartObjID + tCount + 2) & cr after tNewObjects
put " /Root " & (tStartObjID + tCount + 1) & " 0 R" & cr after tNewObjects
put “>>” & cr after tNewObjects
put “startxref” & cr after tNewObjects
put tOriginalSize & cr after tNewObjects
put “%%EOF” after tNewObjects

– Commit raw data out cleanly to the file update chain
put tNewObjects after tRawPDF
put tRawPDF into url (“binfile:” & pFilePath)
end injectUnicodeOutlines

function formatUnicodeHexBookmark pText
put numToByte(254) & numToByte(255) into tBOM
put textEncode(pText, “UTF-16BE”) into tBinaryText
put tBOM & tBinaryText into tFullBinary

put “” into tHexResult
repeat for each byte tByte in tFullBinary
put byteToNum(tByte) into tNum
get baseConvert(tNum, 10, 16)
if length(it) is 1 then put “0” before it
put it after tHexResult
end repeat
return “<” & toupper(tHexResult) & “>”
end formatUnicodeHexBookmark

Hope this code can help anybody!
Hope that the print unicode bookmark can be fixed or better explained if I got it all wrong!?!?

Regards
/Peter