1# This example demonstrates the use of multiline 2D text using
2# vtkTextMappers.  It shows several justifications as well as single-line
3# and multiple-line text inputs.
4
5#
6# First we include the VTK Tcl packages which will make available
7# all of the vtk commands to Tcl
8#
9package require vtk
10package require vtkinteraction
11
12set font_size 14
13
14# Create the text mappers and the associated Actor2Ds.
15
16# The font and text properties (except justification) are the same for each
17# single line mapper. Let's create a common text property object
18
19vtkTextProperty singleLineTextProp
20    singleLineTextProp SetFontSize $font_size
21    singleLineTextProp SetFontFamilyToArial
22    singleLineTextProp BoldOff
23    singleLineTextProp ItalicOff
24    singleLineTextProp ShadowOff
25
26# The font and text properties (except justification) are the same for each
27# multi line mapper. Let's create a common text property object
28
29vtkTextProperty multiLineTextProp
30    multiLineTextProp ShallowCopy singleLineTextProp
31    multiLineTextProp BoldOn
32    multiLineTextProp ItalicOn
33    multiLineTextProp ShadowOn
34
35# The text is on a single line and bottom-justified.
36vtkTextMapper singleLineTextB
37    singleLineTextB SetInput "Single line (bottom)"
38    set tprop [singleLineTextB GetTextProperty]
39    $tprop ShallowCopy singleLineTextProp
40    $tprop SetVerticalJustificationToBottom
41    $tprop SetColor 1 0 0
42vtkActor2D singleLineTextActorB
43    singleLineTextActorB SetMapper singleLineTextB
44    [singleLineTextActorB GetPositionCoordinate] \
45          SetCoordinateSystemToNormalizedDisplay
46    [singleLineTextActorB GetPositionCoordinate] SetValue 0.05 0.85
47
48# The text is on a single line and center-justified (vertical justification).
49vtkTextMapper singleLineTextC
50    singleLineTextC SetInput "Single line (centered)"
51    set tprop [singleLineTextC GetTextProperty]
52    $tprop ShallowCopy singleLineTextProp
53    $tprop SetVerticalJustificationToCentered
54    $tprop SetColor 0 1 0
55vtkActor2D singleLineTextActorC
56    singleLineTextActorC SetMapper singleLineTextC
57    [singleLineTextActorC GetPositionCoordinate] \
58          SetCoordinateSystemToNormalizedDisplay
59    [singleLineTextActorC GetPositionCoordinate] SetValue 0.05 0.75
60
61# The text is on a single line and top-justified.
62vtkTextMapper singleLineTextT
63    singleLineTextT SetInput "Single line (top)"
64    set tprop [singleLineTextT GetTextProperty]
65    $tprop ShallowCopy singleLineTextProp
66    $tprop SetVerticalJustificationToTop
67    $tprop SetColor 0 0 1
68vtkActor2D singleLineTextActorT
69    singleLineTextActorT SetMapper singleLineTextT
70    [singleLineTextActorT GetPositionCoordinate] \
71          SetCoordinateSystemToNormalizedDisplay
72    [singleLineTextActorT GetPositionCoordinate] SetValue 0.05 0.65
73
74# The text is on multiple lines and left- and top-justified.
75vtkTextMapper textMapperL
76    textMapperL SetInput "This is\nmulti-line\ntext output\n(left-top)"
77    set tprop [textMapperL GetTextProperty]
78    $tprop ShallowCopy multiLineTextProp
79    $tprop SetJustificationToLeft
80    $tprop SetVerticalJustificationToTop
81    $tprop SetColor 1 0 0
82vtkActor2D textActorL
83    textActorL SetMapper textMapperL
84    [textActorL GetPositionCoordinate] SetCoordinateSystemToNormalizedDisplay
85    [textActorL GetPositionCoordinate] SetValue 0.05 0.5
86
87# The text is on multiple lines and center-justified (both horizontal and
88# vertical).
89vtkTextMapper textMapperC
90    textMapperC SetInput "This is\nmulti-line\ntext output\n(centered)"
91    set tprop [textMapperC GetTextProperty]
92    $tprop ShallowCopy multiLineTextProp
93    $tprop SetJustificationToCentered
94    $tprop SetVerticalJustificationToCentered
95    $tprop SetColor 0 1 0
96vtkActor2D textActorC
97    textActorC SetMapper textMapperC
98    [textActorC GetPositionCoordinate] SetCoordinateSystemToNormalizedDisplay
99    [textActorC GetPositionCoordinate] SetValue 0.5 0.5
100
101# The text is on multiple lines and right- and bottom-justified.
102vtkTextMapper textMapperR
103    textMapperR SetInput "This is\nmulti-line\ntext output\n(right-bottom)"
104    set tprop [textMapperR GetTextProperty]
105    $tprop ShallowCopy multiLineTextProp
106    $tprop SetJustificationToRight
107    $tprop SetVerticalJustificationToBottom
108    $tprop SetColor 0 0 1
109vtkActor2D textActorR
110    textActorR SetMapper textMapperR
111    [textActorR GetPositionCoordinate] SetCoordinateSystemToNormalizedDisplay
112    [textActorR GetPositionCoordinate] SetValue 0.95 0.5
113
114# Draw the grid to demonstrate the placement of the text.
115
116# Set up the necessary points.
117vtkPoints Pts
118    Pts InsertNextPoint 0.05 0.0 0.0
119    Pts InsertNextPoint 0.05 1.0 0.0
120    Pts InsertNextPoint 0.5 0.0 0.0
121    Pts InsertNextPoint 0.5 1.0 0.0
122    Pts InsertNextPoint 0.95 0.0 0.0
123    Pts InsertNextPoint 0.95 1.0 0.0
124    Pts InsertNextPoint 0.0 0.5 0.0
125    Pts InsertNextPoint 1.0 0.5 0.0
126    Pts InsertNextPoint 0.00 0.85 0.0
127    Pts InsertNextPoint 0.50 0.85 0.0
128    Pts InsertNextPoint 0.00 0.75 0.0
129    Pts InsertNextPoint 0.50 0.75 0.0
130    Pts InsertNextPoint 0.00 0.65 0.0
131    Pts InsertNextPoint 0.50 0.65 0.0
132# Set up the lines that use these points.
133vtkCellArray Lines
134    Lines InsertNextCell 2
135    Lines InsertCellPoint 0
136    Lines InsertCellPoint 1
137    Lines InsertNextCell 2
138    Lines InsertCellPoint 2
139    Lines InsertCellPoint 3
140    Lines InsertNextCell 2
141    Lines InsertCellPoint 4
142    Lines InsertCellPoint 5
143    Lines InsertNextCell 2
144    Lines InsertCellPoint 6
145    Lines InsertCellPoint 7
146    Lines InsertNextCell 2
147    Lines InsertCellPoint 8
148    Lines InsertCellPoint 9
149    Lines InsertNextCell 2
150    Lines InsertCellPoint 10
151    Lines InsertCellPoint 11
152    Lines InsertNextCell 2
153    Lines InsertCellPoint 12
154    Lines InsertCellPoint 13
155# Create a grid that uses these points and lines.
156vtkPolyData Grid
157    Grid SetPoints Pts
158    Grid SetLines Lines
159# Set up the coordinate system.
160vtkCoordinate normCoords
161    normCoords SetCoordinateSystemToNormalizedViewport
162
163# Set up the mapper and actor (2D) for the grid.
164vtkPolyDataMapper2D mapper
165    mapper SetInputData Grid
166    mapper SetTransformCoordinate normCoords
167vtkActor2D gridActor
168    gridActor SetMapper mapper
169    [gridActor GetProperty] SetColor 0.1 0.1 0.1
170
171# Create the Renderer, RenderWindow, and RenderWindowInteractor
172#
173vtkRenderer ren1
174vtkRenderWindow renWin
175    renWin SetMultiSamples 0
176    renWin AddRenderer ren1
177vtkRenderWindowInteractor iren
178    iren SetRenderWindow renWin
179
180# Add the actors to the renderer; set the background and size; zoom in
181# closer to the image; render
182#
183ren1 AddActor2D gridActor
184ren1 AddActor2D textActorL
185ren1 AddActor2D textActorC
186ren1 AddActor2D textActorR
187ren1 AddActor2D singleLineTextActorB
188ren1 AddActor2D singleLineTextActorC
189ren1 AddActor2D singleLineTextActorT
190
191ren1 SetBackground 1 1 1
192renWin SetSize 500 300
193[ren1 GetActiveCamera] Zoom 1.5
194renWin Render
195
196# Set the user method (bound to key 'u')
197#
198iren AddObserver UserEvent {wm deiconify .vtkInteract}
199
200# Withdraw the default tk window.
201wm withdraw .
202
203
204
205