| 1 | /* |
| 2 | This file is part of the KDE libraries |
| 3 | SPDX-FileCopyrightText: 2007 Mirko Stocker <me@misto.ch> |
| 4 | SPDX-FileCopyrightText: 2003-2005 Hamish Rodda <rodda@kde.org> |
| 5 | SPDX-FileCopyrightText: 2001 Christoph Cullmann <cullmann@kde.org> |
| 6 | SPDX-FileCopyrightText: 2001 Joseph Wenninger <jowenn@kde.org> |
| 7 | SPDX-FileCopyrightText: 1999 Jochen Wilhelmy <digisnap@cs.tu-berlin.de> |
| 8 | |
| 9 | SPDX-License-Identifier: LGPL-2.0-only |
| 10 | */ |
| 11 | |
| 12 | #ifndef KATE_RENDERER_H |
| 13 | #define KATE_RENDERER_H |
| 14 | |
| 15 | #include "kateconfig.h" |
| 16 | #include "ktexteditor/range.h" |
| 17 | |
| 18 | #include <QFlags> |
| 19 | #include <QFont> |
| 20 | #include <QFontMetricsF> |
| 21 | #include <QTextLine> |
| 22 | |
| 23 | namespace KTextEditor |
| 24 | { |
| 25 | class DocumentPrivate; |
| 26 | class ViewPrivate; |
| 27 | class Attribute; |
| 28 | } |
| 29 | class KateRendererConfig; |
| 30 | namespace Kate |
| 31 | { |
| 32 | class TextFolding; |
| 33 | class TextLine; |
| 34 | } |
| 35 | |
| 36 | class KateTextLayout; |
| 37 | class KateLineLayout; |
| 38 | typedef QExplicitlySharedDataPointer<KTextEditor::Attribute> AttributePtr; |
| 39 | |
| 40 | namespace KTextEditor |
| 41 | { |
| 42 | enum class caretStyles { |
| 43 | Line, |
| 44 | Block, |
| 45 | Underline, |
| 46 | Half, |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Handles all of the work of rendering the text |
| 52 | * (used for the views and printing) |
| 53 | * |
| 54 | **/ |
| 55 | class KateRenderer |
| 56 | { |
| 57 | public: |
| 58 | /** |
| 59 | * Style of Caret |
| 60 | * |
| 61 | * The caret is displayed as a vertical bar (Line), a filled box |
| 62 | * (Block), a horizontal bar (Underline), or a half-height filled |
| 63 | * box (Half). The default is Line. |
| 64 | * |
| 65 | * Line Block Underline Half |
| 66 | * |
| 67 | * ## _ ######### _ _ |
| 68 | * ## __| | #####| |# __| | __| | |
| 69 | * ## / _' | ##/ _' |# / _' | / _' | |
| 70 | * ##| (_| | #| (#| |# | (_| | #| (#| |# |
| 71 | * ## \__,_| ##\__,_|# \__,_| ##\__,_|# |
| 72 | * ## ######### ######### ######### |
| 73 | */ |
| 74 | |
| 75 | /** |
| 76 | * Constructor |
| 77 | * @param doc document to render |
| 78 | * @param folding folding information |
| 79 | * @param view view which is output (0 for example for rendering to print) |
| 80 | */ |
| 81 | explicit KateRenderer(KTextEditor::DocumentPrivate *doc, Kate::TextFolding &folding, KTextEditor::ViewPrivate *view = nullptr); |
| 82 | |
| 83 | KateRenderer(const KateRenderer &) = delete; |
| 84 | KateRenderer &operator=(const KateRenderer &) = delete; |
| 85 | |
| 86 | /** |
| 87 | * Returns the document to which this renderer is bound |
| 88 | */ |
| 89 | KTextEditor::DocumentPrivate *doc() const |
| 90 | { |
| 91 | return m_doc; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns the folding info to which this renderer is bound |
| 96 | * @return folding info |
| 97 | */ |
| 98 | Kate::TextFolding &folding() const |
| 99 | { |
| 100 | return m_folding; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns the view to which this renderer is bound |
| 105 | */ |
| 106 | KTextEditor::ViewPrivate *view() const |
| 107 | { |
| 108 | return m_view; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * update the highlighting attributes |
| 113 | * (for example after an hl change or after hl config changed) |
| 114 | */ |
| 115 | void updateAttributes(); |
| 116 | |
| 117 | /** |
| 118 | * Determine whether the caret (text cursor) will be drawn. |
| 119 | * @return should it be drawn? |
| 120 | */ |
| 121 | inline bool drawCaret() const |
| 122 | { |
| 123 | return m_drawCaret; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Set whether the caret (text cursor) will be drawn. |
| 128 | * @param drawCaret should caret be drawn? |
| 129 | */ |
| 130 | void setDrawCaret(bool drawCaret); |
| 131 | |
| 132 | /** |
| 133 | * The style of the caret (text cursor) to be painted. |
| 134 | * @return caretStyle |
| 135 | */ |
| 136 | inline KTextEditor::caretStyles caretStyle() const |
| 137 | { |
| 138 | return m_caretStyle; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Set the style of caret to be painted. |
| 143 | * @param style style to set |
| 144 | */ |
| 145 | void setCaretStyle(KTextEditor::caretStyles style); |
| 146 | |
| 147 | /** |
| 148 | * Set a \a brush with which to override drawing of the caret. Set to QColor() to clear. |
| 149 | */ |
| 150 | void setCaretOverrideColor(const QColor &color); |
| 151 | |
| 152 | /** |
| 153 | * @returns whether tabs should be shown (ie. a small mark |
| 154 | * drawn to identify a tab) |
| 155 | * @return tabs should be shown |
| 156 | */ |
| 157 | inline bool showTabs() const |
| 158 | { |
| 159 | return m_showTabs; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Set whether a mark should be painted to help identifying tabs. |
| 164 | * @param showTabs show the tabs? |
| 165 | */ |
| 166 | void setShowTabs(bool showTabs); |
| 167 | |
| 168 | /** |
| 169 | * Set which spaces should be rendered |
| 170 | */ |
| 171 | void setShowSpaces(KateDocumentConfig::WhitespaceRendering showSpaces); |
| 172 | |
| 173 | /** |
| 174 | * @returns whether which spaces should be rendered |
| 175 | */ |
| 176 | inline KateDocumentConfig::WhitespaceRendering showSpaces() const |
| 177 | { |
| 178 | return m_showSpaces; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Update marker size shown. |
| 183 | */ |
| 184 | void updateMarkerSize(); |
| 185 | |
| 186 | /** |
| 187 | * @returns whether non-printable spaces should be shown |
| 188 | */ |
| 189 | inline bool showNonPrintableSpaces() const |
| 190 | { |
| 191 | return m_showNonPrintableSpaces; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Set whether box should be drawn around non-printable spaces |
| 196 | */ |
| 197 | void setShowNonPrintableSpaces(const bool showNonPrintableSpaces); |
| 198 | |
| 199 | /** |
| 200 | * Sets the width of the tab. Helps performance. |
| 201 | * @param tabWidth new tab width |
| 202 | */ |
| 203 | void setTabWidth(int tabWidth); |
| 204 | |
| 205 | /** |
| 206 | * @returns whether indent lines should be shown |
| 207 | * @return indent lines should be shown |
| 208 | */ |
| 209 | bool showIndentLines() const; |
| 210 | |
| 211 | /** |
| 212 | * Set whether a guide should be painted to help identifying indent lines. |
| 213 | * @param showLines show the indent lines? |
| 214 | */ |
| 215 | void setShowIndentLines(bool showLines); |
| 216 | |
| 217 | /** |
| 218 | * Sets the width of the tab. Helps performance. |
| 219 | * @param indentWidth new indent width |
| 220 | */ |
| 221 | void setIndentWidth(int indentWidth); |
| 222 | |
| 223 | /** |
| 224 | * Show the view's selection? |
| 225 | * @return show sels? |
| 226 | */ |
| 227 | inline bool showSelections() const |
| 228 | { |
| 229 | return m_showSelections; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Set whether the view's selections should be shown. |
| 234 | * The default is true. |
| 235 | * @param showSelections show the selections? |
| 236 | */ |
| 237 | void setShowSelections(bool showSelections); |
| 238 | |
| 239 | /** |
| 240 | * Change to a different font (soon to be font set?) |
| 241 | */ |
| 242 | void addToFontSize(qreal step) const; |
| 243 | void resetFontSizes() const; |
| 244 | |
| 245 | /** |
| 246 | * Access currently used font. |
| 247 | * @return current font |
| 248 | */ |
| 249 | const QFont ¤tFont() const |
| 250 | { |
| 251 | return m_font; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Access currently used font metrics. |
| 256 | * @return current font metrics |
| 257 | */ |
| 258 | const QFontMetricsF ¤tFontMetrics() const |
| 259 | { |
| 260 | return m_fontMetrics; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * @return whether the renderer is configured to paint in a |
| 265 | * printer-friendly fashion. |
| 266 | */ |
| 267 | bool isPrinterFriendly() const; |
| 268 | |
| 269 | /** |
| 270 | * Configure this renderer to paint in a printer-friendly fashion. |
| 271 | * |
| 272 | * Sets the other options appropriately if true. |
| 273 | */ |
| 274 | void setPrinterFriendly(bool printerFriendly); |
| 275 | |
| 276 | /** |
| 277 | * Text width & height calculation functions... |
| 278 | */ |
| 279 | void layoutLine(Kate::TextLine textLine, KateLineLayout *line, int maxwidth = -1, bool cacheLayout = false, bool skipSelections = false) const; |
| 280 | |
| 281 | /** |
| 282 | * This is a smaller QString::isRightToLeft(). It's also marked as internal to kate |
| 283 | * instead of internal to Qt, so we can modify. This method searches for the first |
| 284 | * strong character in the paragraph and then returns its direction. In case of a |
| 285 | * line without any strong characters, the direction is forced to be LTR. |
| 286 | * |
| 287 | * Back in KDE 4.1 this method counted chars, which lead to unwanted side effects. |
| 288 | * (see https://bugs.kde.org/show_bug.cgi?id=178594). As this function is internal |
| 289 | * the way it work will probably change between releases. Be warned! |
| 290 | */ |
| 291 | static bool isLineRightToLeft(QStringView str); |
| 292 | |
| 293 | /** |
| 294 | * The ultimate decoration creation function. |
| 295 | * |
| 296 | * \param selectionsOnly return decorations for selections and/or dynamic highlighting. |
| 297 | */ |
| 298 | QList<QTextLayout::FormatRange> decorationsForLine(const Kate::TextLine &textLine, int line, bool skipSelections = false) const; |
| 299 | |
| 300 | // Width calculators |
| 301 | qreal spaceWidth() const; |
| 302 | |
| 303 | /** |
| 304 | * Returns the x position of cursor \p col on the line \p range. |
| 305 | */ |
| 306 | qreal cursorToX(const KateTextLayout &range, int col, bool returnPastLine = false) const; |
| 307 | /// \overload |
| 308 | qreal cursorToX(const KateTextLayout &range, const KTextEditor::Cursor pos, bool returnPastLine = false) const; |
| 309 | |
| 310 | /** |
| 311 | * Returns the real cursor which is occupied by the specified x value, or that closest to it. |
| 312 | * If \p returnPastLine is true, the column will be extrapolated out with the assumption |
| 313 | * that the extra characters are spaces. |
| 314 | */ |
| 315 | KTextEditor::Cursor xToCursor(const KateTextLayout &range, int x, bool returnPastLine = false) const; |
| 316 | |
| 317 | // Font height |
| 318 | uint fontHeight() const; |
| 319 | |
| 320 | // Line height |
| 321 | int lineHeight() const; |
| 322 | |
| 323 | // Document height |
| 324 | uint documentHeight() const; |
| 325 | |
| 326 | // Selection boundaries |
| 327 | bool getSelectionBounds(int line, int lineLength, int &start, int &end) const; |
| 328 | |
| 329 | /** |
| 330 | * Flags to customize the paintTextLine function behavior |
| 331 | */ |
| 332 | enum PaintTextLineFlag { |
| 333 | /** |
| 334 | * Skip drawing the dashed underline at the start of a folded block of text? |
| 335 | */ |
| 336 | SkipDrawFirstInvisibleLineUnderlined = 0x1, |
| 337 | /** |
| 338 | * Skip drawing the line selection |
| 339 | * This is useful when we are drawing the draggable pixmap for drag event |
| 340 | */ |
| 341 | SkipDrawLineSelection = 0x2 |
| 342 | }; |
| 343 | Q_DECLARE_FLAGS(PaintTextLineFlags, PaintTextLineFlag) |
| 344 | |
| 345 | /** |
| 346 | * This is the ultimate function to perform painting of a text line. |
| 347 | * |
| 348 | * The text line is painted from the upper limit of (0,0). To move that, |
| 349 | * apply a transform to your painter. |
| 350 | * |
| 351 | * @param paint painter to use |
| 352 | * @param range layout to use in painting this line |
| 353 | * @param textClipRect clip rect for text to not paint lines outside the visible area. |
| 354 | * @param xStart starting width in pixels. |
| 355 | * @param xEnd ending width in pixels. |
| 356 | * @param cursor position of the caret, if placed on the current line. |
| 357 | * @param flags flags for customizing the drawing of the line |
| 358 | */ |
| 359 | void paintTextLine(QPainter &paint, |
| 360 | KateLineLayout *range, |
| 361 | int xStart, |
| 362 | int xEnd, |
| 363 | const QRectF &textClipRect = QRectF(), |
| 364 | const KTextEditor::Cursor *cursor = nullptr, |
| 365 | PaintTextLineFlags flags = PaintTextLineFlags()); |
| 366 | |
| 367 | /** |
| 368 | * Paint the background of a line |
| 369 | * |
| 370 | * Split off from the main @ref paintTextLine method to make it smaller. As it's being |
| 371 | * called only once per line it shouldn't noticably affect performance and it |
| 372 | * helps readability a LOT. |
| 373 | * |
| 374 | * @param paint painter to use |
| 375 | * @param layout layout to use in painting this line |
| 376 | * @param currentViewLine if one of the view lines is the current line, set |
| 377 | * this to the index; otherwise -1. |
| 378 | * @param xStart starting width in pixels. |
| 379 | * @param xEnd ending width in pixels. |
| 380 | */ |
| 381 | void paintTextLineBackground(QPainter &paint, KateLineLayout *layout, int currentViewLine, int xStart, int xEnd); |
| 382 | |
| 383 | void paintTextBackground(QPainter &paint, KateLineLayout *layout, const QList<QTextLayout::FormatRange> &selRanges, int xStart) const; |
| 384 | |
| 385 | /** |
| 386 | * This takes an in index, and returns all the attributes for it. |
| 387 | * For example, if you have a ktextline, and want the KTextEditor::Attribute |
| 388 | * for a given position, do: |
| 389 | * |
| 390 | * attribute(myktextline->attribute(position)); |
| 391 | */ |
| 392 | const AttributePtr &attribute(uint pos) const; |
| 393 | AttributePtr specificAttribute(int context) const; |
| 394 | |
| 395 | /** |
| 396 | * Paints a range of text into @a d. This function is mainly used to paint the pixmap |
| 397 | * when dragging text. |
| 398 | * |
| 399 | * Please note that this will not paint the selection background but only the text. |
| 400 | * |
| 401 | * @param d the paint device |
| 402 | * @param startLine start line |
| 403 | * @param xStart start pos on @a startLine in pixels |
| 404 | * @param endLine end line |
| 405 | * @param xEnd end pos on @a endLine in pixels |
| 406 | * @param scale the amount of scaling to apply. Default is 1.0, negative values are not supported |
| 407 | */ |
| 408 | void paintSelection(QPaintDevice *d, int startLine, int xStart, int endLine, int xEnd, int viewWidth, qreal scale = 1.0); |
| 409 | |
| 410 | private: |
| 411 | /** |
| 412 | * Paint a trailing space on position (x, y). |
| 413 | */ |
| 414 | void paintSpaces(QPainter &paint, const QPointF *points, const int count) const; |
| 415 | /** |
| 416 | * Paint a tab stop marker on position (x, y). |
| 417 | */ |
| 418 | void paintTabstop(QPainter &paint, qreal x, qreal y) const; |
| 419 | |
| 420 | /** |
| 421 | * Paint a non-breaking space marker on position (x, y). |
| 422 | */ |
| 423 | void paintNonBreakSpace(QPainter &paint, qreal x, qreal y) const; |
| 424 | |
| 425 | /** |
| 426 | * Paint non printable spaces bounding box |
| 427 | */ |
| 428 | void paintNonPrintableSpaces(QPainter &paint, qreal x, qreal y, const QChar &chr); |
| 429 | |
| 430 | /** Paint a SciTE-like indent marker. */ |
| 431 | void paintIndentMarker(QPainter &paint, uint x, int line); |
| 432 | |
| 433 | static void assignSelectionBrushesFromAttribute(QTextLayout::FormatRange &target, const KTextEditor::Attribute &attribute); |
| 434 | |
| 435 | void paintCaret(KTextEditor::Cursor cursor, KateLineLayout *range, QPainter &paint, int xStart, int xEnd); |
| 436 | |
| 437 | // update font height |
| 438 | void updateFontHeight(); |
| 439 | |
| 440 | bool hasCustomLineHeight() const; |
| 441 | |
| 442 | KTextEditor::DocumentPrivate *const m_doc; |
| 443 | Kate::TextFolding &m_folding; |
| 444 | KTextEditor::ViewPrivate *const m_view; |
| 445 | |
| 446 | // cache of config values |
| 447 | int m_tabWidth; |
| 448 | int m_indentWidth; |
| 449 | int m_fontHeight; |
| 450 | float m_fontAscent; |
| 451 | |
| 452 | // if we are at bracket, this will have the X for the opener |
| 453 | int m_currentBracketX = -1; |
| 454 | |
| 455 | // The bracket range, if we are at a bracket |
| 456 | KTextEditor::Range m_currentBracketRange = KTextEditor::Range::invalid(); |
| 457 | |
| 458 | // some internal flags |
| 459 | KTextEditor::caretStyles m_caretStyle; |
| 460 | bool m_drawCaret; |
| 461 | bool m_showSelections; |
| 462 | bool m_showTabs; |
| 463 | KateDocumentConfig::WhitespaceRendering m_showSpaces = KateDocumentConfig::None; |
| 464 | float m_markerSize; |
| 465 | bool m_showNonPrintableSpaces; |
| 466 | bool m_printerFriendly; |
| 467 | QColor m_caretOverrideColor; |
| 468 | |
| 469 | QList<AttributePtr> m_attributes; |
| 470 | |
| 471 | /** |
| 472 | * Configuration |
| 473 | */ |
| 474 | public: |
| 475 | void updateConfig(); |
| 476 | |
| 477 | inline KateRendererConfig *config() const |
| 478 | { |
| 479 | return m_config.get(); |
| 480 | } |
| 481 | |
| 482 | private: |
| 483 | std::unique_ptr<KateRendererConfig> const m_config; |
| 484 | |
| 485 | /** |
| 486 | * cached font, was perhaps adjusted for current DPIs |
| 487 | */ |
| 488 | QFont m_font; |
| 489 | |
| 490 | /** |
| 491 | * cached font metrics |
| 492 | */ |
| 493 | QFontMetricsF m_fontMetrics; |
| 494 | }; |
| 495 | |
| 496 | #endif |
| 497 | |