Posted to tcl by chw at Fri Aug 05 21:03:22 GMT 2016view raw

  1. package require tkpath
  2. package require pdf4tcl
  3.  
  4. set textnl \
  5. "\U1F601\U1F602\U1F603\n\U1F604\U1F605\U1F606\n\U1F607\U1F608\U1F609"
  6.  
  7. set text "\U1F601\U1F602\U1F603\U1F604\U1F605\U1F606\U1F607\U1F608\U1F609"
  8.  
  9. # Make font set list
  10.  
  11. proc mkset {text} {
  12. set result {}
  13. foreach i [split $text {}] {
  14. set t 0
  15. scan $i %c t
  16. lappend result $t
  17. }
  18. return $result
  19. }
  20.  
  21. # Symbola font for Emojis
  22. # undroidwish has it built in, otherwise hopefully somewhere else ...
  23. set symttf [file join $tk_library fonts Symbola.ttf]
  24. if {![file readable $symttf]} {
  25. set symttf [file join ~ .fonts Symbola.ttf]
  26. if {![file readable $symttf]} {
  27. set symttf Symbola.ttf
  28. }
  29. }
  30.  
  31. pdf4tcl::loadBaseTrueTypeFont BaseSymbola $symttf
  32. pdf4tcl::createFontSpecEnc BaseSymbola Symbola [mkset $text]
  33.  
  34. set w .c
  35. pack [tkp::canvas $w -width 400 -height 400 -bg white]
  36.  
  37. $w create prect 10 10 390 390 -fill {} -stroke blue -strokewidth 2
  38. $w create ptext 200 200 -fontfamily Symbola -fontsize 60 \
  39. -fill red2 -stroke black -strokewidth 1 -textanchor c \
  40. -text $textnl
  41.  
  42. pdf4tcl::new mypdf -paper a4
  43. mypdf startPage
  44. mypdf canvas $w -fontmap {Symbola Symbola}
  45. # rendered directly for comparison
  46. mypdf setFont 30 Symbola
  47. mypdf text $text -x 50 -y 100
  48. mypdf setFont 30 Helvetica
  49. mypdf text "Hello World!" -x 350 -y 100
  50. mypdf write -file out.pdf
  51. mypdf destroy
  52.