Skip to content
80 changes: 80 additions & 0 deletions demo/embed_fonts.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import streams, nimPDF, unittest

proc draw_title(doc: PDF, text: string) =
let size = getSizeFromName("A4")

# Use Helvetica (Base14) for title to avoid affecting KaiTi embedding status
doc.setFont("Helvetica", {FS_BOLD}, 12, ENC_STANDARD)
let tw = doc.getTextWidth(text)
let x = size.width.toMM / 2 - tw / 2

doc.setFillColor(0, 0, 0)
doc.drawText(x, 10.0, text)
doc.setStrokeColor(0, 0, 0)
doc.drawRect(10, 15, size.width.toMM - 20, size.height.toMM - 25)
doc.stroke()

proc createPDF(doc: PDF) =
let size = getSizeFromName("A4")
let SAMP_TEXT = "The Quick Brown Fox Jump Over The Lazy Dog"

doc.addPage(size, PGO_PORTRAIT)
draw_title(doc, "FONT RENDERING MODES DEMO")

# Example 1: Font with renderMode=frmEmbed
doc.setFillColor(0, 0, 0) # Set black color for text
doc.setFont("Calligrapher", {FS_REGULAR}, 10, ENC_STANDARD, renderMode = frmEmbed)
doc.drawText(15, 30, "Calligrapher - renderMode=frmEmbed")

# Example 2: Korean text with Eunjin font (embedded)
doc.setFillColor(0, 0, 0) # Set black color for text
doc.setFont("Eunjin", {FS_REGULAR}, 10, ENC_UTF8, renderMode = frmEmbed)
doc.drawText(15, 60, "은진 - 한국어 테스트 (embedded)")

# Example 3: Path rendering (text drawn as vector paths)
doc.setFillColor(0, 0, 0) # Set black color for text
doc.setFont("KaiTi", {FS_REGULAR}, 10, enc = ENC_UTF8, renderMode = frmPathRendering)
doc.drawText(15, 90, "你好世界 (text drawn as vector paths)")

# Example 4: Base14 font (embedding flag ignored)
doc.setFillColor(0, 0, 0) # Set black color for text
doc.setFont("Times", {FS_REGULAR}, 10, ENC_STANDARD, renderMode = frmEmbed)
doc.drawText(15, 120, "Times (Base14 ignores flag)")

# Example 5: FreeMono with renderMode=frmDefault
doc.setFillColor(0, 0, 0) # Set black color for text
doc.setFont("FreeMono", {FS_REGULAR}, 10, ENC_UTF8, renderMode = frmDefault)
doc.drawText(15, 150, "FreeMono - The Quick Brown Fox Jump Over The Lazy Dog")


doc.setInfo(DI_TITLE, "Font Rendering Modes Demo")
doc.setInfo(DI_AUTHOR, "Andri Lim")
doc.setInfo(DI_SUBJECT, "Demonstrating three font rendering modes: frmDefault, frmEmbed, frmPathRendering")

proc main(): bool {.discardable.} =
#echo currentSourcePath()
var fileName = "embed_fonts.pdf"
var file = newFileStream(fileName, fmWrite)

if file != nil:
var opts = newPDFOptions()
opts.addFontsPath("fonts")

echo "Creating PDF with font rendering modes demonstration..."
echo "- frmDefault: No embedding, standard text rendering"
echo "- frmEmbed: Text rendering with font embedding"
echo "- frmPathRendering: Text drawn as vector paths (not selectable)"
echo "- Check text copying/pasting to verify different behaviors"

var doc = newPDF(opts)
doc.createPDF()
doc.writePDF(file)
file.close()
echo "PDF created successfully: ", fileName
echo "Try copying text from different lines to see embedding effects"
return true

echo "cannot open: ", fileName
result = false

main()
Binary file added demo/embed_fonts.pdf
Binary file not shown.
32 changes: 14 additions & 18 deletions demo/test.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import
std/[streams, strutils],
nimPDF,
unittest

import streams, nimPDF, unittest, strutils
proc draw_title(doc: PDF, text:string) =
let size = getSizeFromName("A4")

Expand Down Expand Up @@ -46,48 +42,48 @@ proc test(doc: PDF) =
var tw = doc.getTextWidth(text)
var th = doc.getTextHeight(text)
check:
tw.formatFloat(ffDecimal, 2) == "11.39"
th.formatFloat(ffDecimal, 2) == "3.59"
$tw == "11.39"
$th == "3.59"

test "getTextWidth and GetTextHeight TTF":
doc.setFont("FreeMono", {FS_REGULAR}, 5)
var tw = doc.getTextWidth(text)
var th = doc.getTextHeight(text)
check:
tw.formatFloat(ffDecimal, 2) == "15.00"
th.formatFloat(ffDecimal, 2) == "3.02"
$tw.formatFloat(ffDecimal, 2) == "15.00"
$th.formatFloat(ffDecimal, 2) == "3.02"

test "getVTextWidth and GetVTextHeight base14":
doc.setFont("Helvetica", {FS_REGULAR}, 5)
var tw = doc.getVTextWidth(text)
var th = doc.getVTextHeight(text)
check:
tw.formatFloat(ffDecimal, 2) == "3.61"
th.formatFloat(ffDecimal, 2) == "14.05"
$tw.formatFloat(ffDecimal, 2) == "3.61"
$th.formatFloat(ffDecimal, 2) == "14.05"

test "getTextWidth and GetTextHeight TTF":
doc.setFont("FreeMono", {FS_REGULAR}, 5)
var tw = doc.getVTextWidth(text)
var th = doc.getVTextHeight(text)
check:
tw.formatFloat(ffDecimal, 2) == "3.00"
th.formatFloat(ffDecimal, 3) == "13.325"
$tw.formatFloat(ffDecimal, 2) == "3.00"
$th.formatFloat(ffDecimal, 3) == "13.325"

proc main() =
proc main(): bool {.discardable.} =
#echo currentSourcePath()
var fileName = "test.pdf"
var file = newFileStream(fileName, fmWrite)

if file != nil:
var opts = newPDFOptions()
opts.addFontsPath("fonts")
var doc = newPDF(opts)
var doc = newPDF()
doc.createPDF()
doc.writePDF(file)
doc.test()
file.close()
echo "OK"
return
return true

echo "cannot open: ", fileName
result = false

main()
3 changes: 2 additions & 1 deletion demo/test_all.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ import
link_annot,
pagelabels,
test,
text_annot
text_annot,
embed_fonts
Loading
Loading