]> git.lyx.org Git - lyx.git/blob - development/PAINTING_ANALYSIS
Cleanup Painter text() API
[lyx.git] / development / PAINTING_ANALYSIS
1 # -*- org -*-
2 Understanding the painting process
3
4 This file tries to describe the state of the metrics/painting
5 mechanism, and identify the improvements that could be made. The first
6 section can be read alone, although the context for them is really
7 given in the following ones.
8
9 Please keep this file up to date as the code evolves!!!
10
11 Abbreviations:
12 bv: BufferView
13 pm: ParagraphMetrics
14 tm::TextMetrics
15
16 * Questions / Ideas
17
18 These questions are consequences of the description made in the
19 following section. Some actions are proposed.
20
21 ** SinglePar update
22
23 The flag Update::SinglePar is set in many places but never acted on.
24 Instead, metrics update is skipped whenever the recorded height of
25 current paragraph did not change and Update::Force was not specified.
26 Is it better to keep that (which incurs extra work) or to condition it
27 on Update::SinglePar? If the first solution is kept, the flag
28 SingleParUpdate shall be removed.
29
30 Moreover, I fail to see (yet) where the 'single' part of the program
31 is acted on.
32
33 ** Buffer::change issues
34
35 When calling Buffer::changed outside of bv::processUpdateFlags,
36 how do we know that the update strategy is set correctly? It is
37 possible to reset the strategy at the end of bv::draw. What would be
38 a good value? NoScreenUpdate?
39
40 On a related note, what is the semantics of a call to
41 Buffer::changed(false)? What does the caller mean?
42
43 ** How to avoid redraw with FitCursor when the cursor is already OK?
44
45 In this case, we invoke Buffer::change(false) with drawing disabled
46 and NoScreenUpdate strategy.
47
48 In the draw phase, bv::checkCursorScrollOffset (the horizontal
49 scrolling machinery) will change the strategy to FullScreenUpdate if
50 the current row needs further scrolling.
51
52 When the update strategy it kept to NoScreenUpdate, there is currently
53 a no-draw full repaint, which should not be necessary. It would be
54 possible to avoid that if the call to checkCursorScrollOffset was done
55 in bv::processUpdateFlags instead of bv::draw.
56
57 The global idea would be to extend FitCursor to cover also horizontal
58 cursor.
59
60
61 * Proposals
62
63
64 * Clean-up of drawing code
65
66 The goal is to make painting with drawing disable fast enough that it
67 can be used after every metrics computation. Then we can separate real
68 drawing from metrics.
69
70 ** DONE RowPainter
71
72 Inset position is set in paintInset, paintOnlyInsets, and paintText.
73 This should be done only once in paintInset
74
75 ** DONE TextMetrics::drawParagraph
76
77 We can really simplify the code when drawing is disabled only
78 paintInset needs to be called.
79  + do right at the start when drawing is already disabled
80  + do it in the loop for rows that are not visible on screen.
81
82 The only thing we want to do here is to set inset positions (for
83 text). The other insets still use the painter with drawing disabled.
84
85 ** DONE Painter::text
86
87 We cannot remove (or make private) the version that uses a
88 FontInfo because it is used by PainterInfo::draw. Document this and
89 remove unused arguments rtl and double spacing. This would become a specialized helper.
90 Proposed solution: keep the existing function, but private and without
91 optional arguments.
92
93 Avoid to return (and thus compute) the width of strings?
94  + used by InsetSpecialChar (fixable)
95  + used by textDecoration() in text(): more difficult to fix
96
97 Idea: add a version of text where wordspacing and textwidth (giving
98 the width of strings) are required parameters and remove optional
99 version.
100
101 ==> more versions, no optional parameters.
102
103 ** make Inset::display() more useful
104
105 Extending the DisplayType enum would allow to remove special cases
106 from the code.
107
108 The enumeration could be like
109 : Inline = 0
110 : BreakBefore = 1 // break row before this inset
111 : BreakAfter = 2 // break row after this inset
112 : CanBreakAfter = 4 // optionally break row after this inset
113 : AlignLeft = 8
114 : AlignRight = 16
115 : NoBoundary = 32 // do not allow cursor to go at the end of the row
116 :                 //before display inset
117 : Display = BreakBefore|BreakAfter
118
119 A display equation would be Display, other could be Display|AlignLeft
120 BreakAfter can be used by Newline or separator insets
121 CanBreakAfter can be used by the optional hyphen InsetSpecialChar.
122
123 ** Set inset position during metrics phase
124
125 In order to do that, a no-paint drawing will be initiated after every
126 redoParagraph. This code path will need to be made as fast as possible.
127
128 Effect: avoid depending on actual drawing having taken place. In turn,
129 it will allow to do drawing on paint events, like any reasonable
130 application would do.
131
132 ** Cleanup after complete metrics
133    Then the following can be done:
134    + remove hack in InsetMathNest::drawSelection
135    + remove painting when not inside in drawParagraph
136    + remove Cursor::inCoordCache?
137
138 ** Use Row for MathData
139
140 It may not be so difficult. Implement x2pos and pos2x from
141 the TM:cursorX and TM::getPosNearX, and use them for both text and
142 math.
143
144 Will the strings display OK if drawing string-wise?
145
146 Then it would be possible to streamline drawing with disabled painter.
147
148 ** Paint directly to screen
149
150 Instead of using an intermediary pixmap. I have no idea of how
151 difficult it will prove.
152 One benefit will be that subpixel aliasing will work again (#9972)
153
154 ** Merging bv::updateMetrics and tm::metrics
155
156 While the full metrics computation tries hard to limit the number of
157 paragraphs that are rebroken, the version that is used for inner inset
158 does not try any such optimization. This can be very bad for very tall
159 insets. We should re-use the bv::updateMetrics logic:
160  + transfer all the logic of bv::updateMetrics to tm.
161  + Main InsetText should not be special.
162
163 The difficuly for a tall table cell for example, is that it may be
164 necessary to break the whole contents to know the width of the cell.
165
166
167 * Description of current drawing mechanism
168
169 ** Two stage drawing
170
171 There are two parts to drawing the work area:
172
173  + the metrics phase computes the size of insets and breaks the
174    paragraphs into rows. It stores the dimension of insets (both
175    normal and math) in bv::coordCache.
176
177  + the drawing phase draws the contents and caches the inset
178    positions. Since the caching of positions is useful in itself,
179    there is a provision for drawing "without" drawing when the only
180    thing we want is to cache inset positions
181    (Painter::setDrawingEnabled).
182
183 The machinery is controlled via bv::processUpdateFlags. This method is
184 called at the end of bv::mouseEventDispatch and in
185 GuiApplication::dispatch, via the updateCurrentView method. There are
186 also several calls in code related to dialogs. We should evaluate
187 whether this is correct.
188
189 Depending on the Update::flags passed to the method, it sets an update
190 strategy in (NoScreenUpdate, SingleParUpdate, FullScreenUpdate,
191 DecorationUpdate). It triggers a recomputation of the metrics when either:
192
193  + Update::Force has been specified
194  + Update::FitCursor has been specified and there is a need to scroll
195    the display.
196  + the current paragraph, after rebreak, does not have the same height as in
197    existing metrics. Note that the Update::SinglePar flag is *never*
198    taken into account.
199
200 The screen is drawn (with appropriate update strategy), except when
201 update flag is Update::None.
202
203
204 ** Metrics computation
205
206 This is triggered by bv::updateMetrics, which calls tm::redoParagraph for
207   + all visible paragraphs
208   + paragraph above the screen (up to one page)
209   + paragraphs below the screen (up to one page again)
210
211 The paragraphs outside of the screen are required to make PageUp/Down
212 work.
213
214 tm::redoParagraph will call Inset::metrics for each inset. In the case
215 of text insets, this will invoke recursively tm::metrics, which redoes
216 all the paragraphs of the inset.
217
218
219 ** Drawing the work area.
220
221 This is done in bv::draw. This method is triggered mainly by
222 Buffer::changed, which draws all the work areas that show the given buffer.
223
224 Note that, When Buffer::changed is called outside of
225 bv::processUpdateFlags, it is not clear whether the update strategy
226 has been set to a reasonable value beforehand.
227
228 The action depends on the update strategy:
229
230  + NoScreenUpdate: repaint the whole screen with drawing disabled.
231    This is only needed to update the inset positions. I am not sure
232    when this is necessary, actually. This is triggered when:
233    - Update::FitCursor is set but the cursor is already visible. It is
234      not clear why something needs to be done in this case, since this
235      should be equivalent to Update::None.
236    - this is also set when update flag is Update::None, but this value
237      is AFAICS not acted on in the code (meaning that nothing happens
238      at all in this case).
239
240  + FullScreenUpdate: repaint the whole screen. This is set when:
241    - updateMetrics has been invoked.
242    - scroll value of current row has changed (although this should not
243      be necessary).
244
245  + DecorationUpdate: actually like FullScreenUpdate, although it is
246    intended to only update inset decorations. This triggers when:
247    - Update::Decoration is set (without Update::Force) as flag.
248    - An hovered inset is detected.
249
250  + SingleParUpdate: only tries to repaint current paragraph in a way
251    that is not yet very clear to me.