Posted to tcl by emiliano at Sun Mar 22 11:56:33 GMT 2020view raw

  1. The canvas postscript command is somewhat underdocumented.
  2.  
  3. FONTMAP
  4.  
  5. The -fontmap option maps a font used in a canvas, such as the one get by
  6. $canvas itemconfigure $textitem -font
  7. with a pair
  8. [list $fontname $size]
  9.  
  10. This can fail for several reasons:
  11.  
  12. * the provided value is not a list
  13. * the list length is != 2
  14. * the font name is the empty string
  15. * the font name has an space in it
  16. * the size is not a valid double
  17. * the size is <= 0
  18.  
  19. All these conditions are tested in Tk_PostscriptFont(), file generic/tkCanvPs.c
  20.  
  21. The postscript font name *should* match one of the 35 postscript level 2 fonts
  22. (note that the font slant and weight are encoded in the font name)
  23.  
  24. AvantGarde-Book
  25. AvantGarde-BookOblique
  26. AvantGarde-Demi
  27. AvantGarde-DemiOblique
  28. Bookman-Demi
  29. Bookman-DemiItalic
  30. Bookman-Light
  31. Bookman-LightItalic
  32. Courier
  33. Courier-Bold
  34. Courier-BoldOblique
  35. Courier-Oblique
  36. Helvetica
  37. Helvetica-Bold
  38. Helvetica-BoldOblique
  39. Helvetica-Narrow
  40. Helvetica-Narrow-Bold
  41. Helvetica-Narrow-BoldOblique
  42. Helvetica-Narrow-Oblique
  43. Helvetica-Oblique
  44. NewCenturySchlbk-Bold
  45. NewCenturySchlbk-BoldItalic
  46. NewCenturySchlbk-Italic
  47. NewCenturySchlbk-Roman
  48. Palatino-Bold
  49. Palatino-BoldItalic
  50. Palatino-Italic
  51. Palatino-Roman
  52. Symbol
  53. Times-Bold
  54. Times-BoldItalic
  55. Times-Italic
  56. Times-Roman
  57. ZapfChancery-MediumItalic
  58. ZapfDingbats
  59.  
  60. If -fontmap is not provided, or a given font is not found in the fontmap, then
  61. a font name is "guessed" (Tk_PostscriptFontName() in generic/tkFont.c) from
  62. a subset of the above list.
  63.  
  64. Example of use:
  65.  
  66. canvas .c
  67. pack .c
  68. set item [.c create text 200 100 -text "Hello\nWorld"]
  69. update idletasks
  70. # replace the default font for Courier 12 on postscript output
  71. set fmap([.c itemcget $item -font]) [list Courier 12]
  72. set ps [.c postscript -fontmap fmap]
  73.  
  74. The following will raise errors when postscript is generated (see above for a
  75. list of error conditions)
  76.  
  77. # not a list
  78. set fmap([.c itemcget $item -font]) "{"
  79. # list of lenght != 2
  80. set fmap([.c itemcget $item -font]) [list Courier 12 Bold]
  81. # font name empty
  82. set fmap([.c itemcget $item -font]) [list {} 12]
  83. # font name with space
  84. set fmap([.c itemcget $item -font]) [list {Courier New} 12]
  85. # size not a double
  86. set fmap([.c itemcget $item -font]) [list Courier Foo]
  87. # size <= 0
  88. set fmap([.c itemcget $item -font]) [list Courier -12]
  89.  
  90. The error produced is
  91.  
  92. % catch {.c postscript -fontmap fmap} error options
  93. % set error
  94. bad font map entry for "TkDefaultFont": "{"
  95. % dict get $options -errorcode
  96. TK CANVAS PS FONTMAP
  97. % dict get $options -errorinfo
  98. bad font map entry for "TkDefaultFont": "{"
  99. (generating Postscript for item 1)
  100. invoked from within
  101. ".c postscript -fontmap fmap"
  102. %
  103.  
  104. WARNING: You can specify a nonexistent font and it will be used without further
  105. checking. This font map
  106.  
  107. set fmap([.c itemcget $item -font]) [list NONEXISTENT 12]
  108.  
  109. will produce an output but it *may* give an error at print or display time
  110. (ghostscript provide fallback fonts).
  111.  
  112. COLORMAP
  113.  
  114. The -colormap option is similar to the -fontmap, with the difference there is
  115. absolutely no checking of the value provided. As the documentation says, it
  116. should map one of the colors provided to the canvas items via the
  117. -{active|disabled}*outline or -{active|disabled}*fill options with the
  118. corresponding postscript code to get a given color in the format
  119. [format {%f %f %f setrgbcolor} $red $green $blue]. The three components red,
  120. green and blue must be in the range 0.0 - 1.0
  121.  
  122. Example:
  123.  
  124. canvas .c
  125. pack .c
  126. set item [.c create text 200 100 -text "Hello\nWorld" -fill blue]
  127. update idletasks
  128. # convert "blue" to "green3" on postscript output
  129. set rgb [lmap val [winfo rgb .c "green3"] {expr {$val/65535.0}}]
  130. set cmap([.c itemcget $item -fill]) \
  131. [format {%.3f %.3f %.3f setrgbcolor} {*}$rgb]
  132. set ps [.c postscript -colormap cmap]
  133.  
  134. WARNING: You can specify anything in the color map and it will be used without
  135. further checking. Any invalid postscript code will raise an error at print or
  136. display time. The following map
  137.  
  138. set cmap([.c itemcget $item -fill]) "watch me crash!"
  139.  
  140. will cause an error at runtime.
  141.  
  142. Last but not least, you can use the same array for both -fontmap and -colormap
  143. as long as there's no name clashes between fonts and colors.
  144. The only way to get such clashes is using named fonts with names that are also
  145. valid colors (as accepted by Tk_GetColor())
  146.  
  147. font create blue -size 12
  148. font create #ff443c -size 12
  149.