text-sizing-protocol.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. The text sizing protocol
  2. ==============================================
  3. .. versionadded:: 0.40.0
  4. Classically, because the terminal is a grid of equally sized characters, only
  5. a single text size was supported in terminals, with one minor exception, some
  6. characters were allowed to be rendered in two cells, to accommodate East Asian
  7. square aspect ratio characters and Emoji. Here, by single text size we mean the
  8. font size of all text on the screen is the same.
  9. This protocol allows text to be displayed in the terminal in different sizes
  10. both larger and smaller than the base text. It also solves the long standing
  11. problem of robustly determining the width (in cells) a character should have.
  12. Applications can interleave text of different sizes on the screen allowing for
  13. typographic niceties like headlines, superscripts, etc.
  14. Note that this protocol is fully backwards compatible, terminals that implement
  15. it will continue to work just the same with applications that do not use it.
  16. Because of this, it is not fully flexible in the font sizes it allows, as it
  17. still has to work with the character cell grid based fundamental nature of the
  18. terminal. Public discussion of this protocol is :iss:`here <8226>`.
  19. Quickstart
  20. --------------
  21. Using this protocol to display different sized text is very simple, let's
  22. illustrate with a few examples to give us a flavor:
  23. .. code-block:: sh
  24. printf "\e]_text_size_code;s=2;Double sized text\a\n\n"
  25. printf "\e]_text_size_code;s=3;Triple sized text\a\n\n\n"
  26. printf "\e]_text_size_code;n=1:d=2;Half sized text\a\n"
  27. Note that the last example, of half sized text, has half height characters, but
  28. they still each take one cell, this can be fixed with a little more work:
  29. .. code-block:: sh
  30. printf "\e]_text_size_code;n=1:d=2:w=1;Ha\a\e]66;n=1:d=2:w=1;lf\a\n"
  31. The ``w=1`` mechanism allows the program to tell the terminal what width the text
  32. should take. This not only fixes using smaller text but also solves the long
  33. standing terminal ecosystem bugs caused by the client program not knowing how
  34. many cells the terminal will render some text in.
  35. The escape code
  36. -----------------
  37. There is a single escape code used by this protocol. It is sent by client
  38. programs to the terminal emulator to tell it to render the specified text
  39. at the specified size. It is an ``OSC`` code of the form::
  40. <OSC> _text_size_code ; metadata ; text <terminator>
  41. Here, ``OSC`` is the bytes ``ESC ] (0x1b 0x5b)``. The ``metadata`` is a colon
  42. separated list of ``key=value`` pairs. The final part of the escape code is the
  43. text which is simply plain text encoded as :ref:`safe_utf8`. Spaces in this
  44. definition are for clarity only and should be ignored. The ``terminator`` is
  45. either the byte ``BEL (0x7)`` or the bytes ``ESC ST (0x1b 0x5c)``.
  46. There are only a handful of metadata keys, defined in the table below:
  47. .. csv-table:: The text sizing metadata keys
  48. :header: "Key", "Value", "Default", "Description"
  49. "s", "Integer from 1 to 7", "1", "The overall scale, the text will be rendered in a block of ``s * w`` by ``s`` cells"
  50. "w", "Integer from 0 to 7", "0", "The width, in cells, in which the text should be rendered. When zero, the terminal should calculate the width as it would for normal text, splitting it up into scaled cells."
  51. "n", "Integer from 0 to 15", "0", "The numerator for the fractional scale."
  52. "d", "Integer from 0 to 15", "0", "The denominator for the fractional scale. Must be ``> n`` when non-zero."
  53. "v", "Integer from 0 to 2", "0", "The vertical alignment to use for fractionally scaled text. ``0`` - top, ``1`` - bottom, ``2`` - centered"
  54. How it works
  55. ------------------
  56. This protocol works by allowing the client program to tell the terminal to
  57. render text in multiple cells. The terminal can then adjust the actual font
  58. size used to render the specified text as appropriate for the specified space.
  59. The space to render is controlled by four metadata keys, ``s (scale)``, ``w (width)``, ``n (numerator)``
  60. and ``d (denominator)``. The most important are the ``s`` and ``w`` keys. The text
  61. will be rendered in a block of ``s * w`` by ``s`` cells. A special case is ``w=0``
  62. (the default), which means the terminal splits up the text into cells as it
  63. would normally without this protocol, but now each cell is an ``s by s`` block of
  64. cells instead. So, for example, if the text is ``abc`` and ``s=2`` the terminal would normally
  65. split it into three cells::
  66. │a│b│c│
  67. But, because ``s=2`` it instead gets split as::
  68. │a░│b░│c░│
  69. │░░│░░│░░│
  70. The terminal multiplies the font size by ``s`` when rendering these
  71. characters and thus ends up rendering text at twice the base size.
  72. When ``w`` is a non-zero value, it specifies the width in scaled cells of the
  73. following text. Note that **all** the text in that escape code must be rendered
  74. in ``s * w`` cells. If it does not fit, the terminal is free to do whatever it
  75. feels is best, including truncating the text or downsizing the font size when
  76. rendering it. It is up to client applications to use the ``w`` key wisely and not
  77. try to render too much text in too few cells. When sending a string of text
  78. with non zero ``w`` to the terminal emulator, the way to do it is to split up the
  79. text into chunks that fit in ``w`` cells and send one escape code per chunk. So
  80. for the string: ``cool-🐈`` the actual escape codes would be (ignoring the header
  81. and trailers)::
  82. w=1;c w=1;o w=1;o w=1;l w=1;- w=2:🐈
  83. Note, in particular, how the last character, the cat emoji, ``🐈`` has ``w=2``.
  84. In practice client applications can assume that terminal emulators get the
  85. width of all ASCII characters correct and use the ``w=0`` form for efficient
  86. transmission, so that the above becomes::
  87. cool- w=2:🐈
  88. The use of non-zero ``w`` should mainly be restricted to non-ASCII characters and
  89. when using fractional scaling, as described below.
  90. Fractional scaling
  91. ^^^^^^^^^^^^^^^^^^^^^^^
  92. Using the main scale parameter (``s``) gives us only 7 font sizes. Fortunately,
  93. this protocol allows specifying fractional scaling, fractional scaling is
  94. applied on top of the main scale specified by ``s``. It allows niceties like:
  95. * Normal sized text but with half a line of blank space above and half a line below (``s=2:n=1:d=2:v=2``)
  96. * Superscripts (``n=1:d=2``)
  97. * Subscripts (``n=1:d=2:v=1``)
  98. * ...
  99. The fractional scale **does not** affect the number of cells the text occupies,
  100. instead, it just adjust the rendered font size within those cells.
  101. The fraction is specified using an integer numerator and denominator (``n`` and
  102. ``d``). In addition, by using the ``v`` key one can vertically align the
  103. fractionally scaled text at top, bottom or middle.
  104. When using fractional scaling one often wants to fit more than a single
  105. character per cell. To accommodate that, there is the ``w`` key. This specifies
  106. the number of cells in which to render the text. For example, for a superscript
  107. one would typically split the string into pairs of characters and use the
  108. following for each pair::
  109. OSC _text_size_code ; n=1:d=2:w=1 ; ab <terminator>
  110. ... repeat for each pair of characters
  111. Fixing the character width issue for the terminal ecosystem
  112. ---------------------------------------------------------------------
  113. Terminals create user interfaces using text displayed in a cell grid. For
  114. terminal software that creates sophisticated user interfaces it is particularly
  115. important that the client program running in the terminal and the terminal
  116. itself agree on how many cells a particular string should be rendered in. If
  117. the two disagree, then the entire user interface can be broken, leading to
  118. catastrophic failures.
  119. Fundamentally, this is a co-ordination problem. Both the client program and the
  120. terminal have to somehow share the same database of character properties and
  121. the same algorithm for computing string lengths in cells based on that shared
  122. database. Sadly, there is no such shared database in reality. The closest we
  123. have is the Unicode standard. Unfortunately, the Unicode standard has a new
  124. version almost every year and actually changes the width assigned to some
  125. characters in different versions. Furthermore, to actually get the "correct"
  126. width for a string using that standard one has to do grapheme segmentation,
  127. which is an `extremely complex algorithm
  128. <https://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries>`__.
  129. Expecting all terminals and all terminal programs to have both up-to-date
  130. character databases and a bug free implementation of this algorithm is not
  131. realistic.
  132. So instead, this protocol solves the issue robustly by removing the
  133. co-ordination problem and putting only one actor in charge of determining
  134. string width. The client becomes responsible for doing whatever level of
  135. grapheme segmentation it is comfortable with using whatever Unicode database is
  136. at its disposal and then it can transmit the segmented string to the terminal
  137. with the appropriate ``w`` values so that the terminal renders the text in the
  138. exact number of cells the client expects.
  139. .. note::
  140. It is possible for a terminal to implement only the width part of this spec
  141. and ignore the scale part. This escape code works with only the `w` key as
  142. well as a means of specifying how many cells each piece of text occupies.
  143. See the section on :ref:`detect_text_sizing` on how client applications can
  144. query for terminal emulator support.
  145. Wrapping and overwriting behavior
  146. -------------------------------------
  147. If the multicell block (``s * w by s`` cells) is larger than the screen size in either
  148. dimension, the terminal must discard the character. Note that in particular
  149. this means that resizing a terminal screen so that it is too small to fit a
  150. multicell character can cause the character to be lost.
  151. When drawing a multicell character, if wrapping is enabled (DECAWM is set) and
  152. the character's width (``s * w``) does not fit on the current line, the cursor is
  153. moved to the start of the next line and the character is drawn there.
  154. If wrapping is disabled and the character's width does not fit on the current
  155. line, the cursor is moved back as far as needed to fit ``s * w`` cells and then
  156. the character is drawn, following the overwriting rules described below.
  157. When drawing text either normal text or text specified via this escape code,
  158. and this text would overwrite an existing multicell character, the following
  159. rules must be followed, in decreasing order of precedence:
  160. #. If the text is a combining character it is added to the existing multicell
  161. character
  162. #. If the text will overwrite the top-left cell of the multicell character, the
  163. entire multicell character must be erased
  164. #. If the text will overwrite any cell in the topmost row of the multicell
  165. character, the entire multicell character must be replaced by spaces (this
  166. rule is present for backwards compatibility with how overwriting works for
  167. wide characters)
  168. #. If the text will overwrite cells from a row after the first row, then cursor should be moved past the
  169. cells of the multicell character on that row and only then the text should be
  170. written. Note that this behavior is independent of the value of DECAWM. This
  171. is done for simplicity of implementation.
  172. The skipping behavior of the last rule can be complex requiring the terminal to
  173. skip over lots of cells, but it is needed to allow wrapping in the presence of
  174. multicell characters that extend over more than a single line.
  175. .. _detect_text_sizing:
  176. Detecting if the terminal supports this protocol
  177. -----------------------------------------------------
  178. To detect support for this protocol use the `CPR (Cursor Position Report)
  179. <https://vt100.net/docs/vt510-rm/CPR.html>`__ escape code. Send a ``CPR``
  180. followed by ``\e]_text_size_code;w=2; \a`` which will draw a space character in
  181. two cells, followed by another ``CPR``. Then send ``\e]_text_size_code;s=2; \a``
  182. which will draw a space in a ``2 by 2`` block of cells, followed by another
  183. ``CPR``.
  184. Then wait for the three responses from the terminal to the three CPR queries.
  185. If the cursor position in the three responses is the same, the terminal does
  186. not support this protocol at all, if the second response has a different cursor
  187. position then the width part is supported and if the third response has yet
  188. another position, the scale part is supported.
  189. Interaction with other terminal controls
  190. --------------------------------------------------
  191. This protocol does not change the character grid based nature of the terminal.
  192. Most terminal controls assume one character per cell so it is important to
  193. specify how these controls interact with the multicell characters created by
  194. this protocol.
  195. Cursor movement
  196. ^^^^^^^^^^^^^^^^^^^
  197. Cursor movement is unaffected by multicell characters, all cursor movement
  198. commands move the cursor position by single cell increments, as has always been
  199. the case for terminals. This means that the cursor can be placed at any
  200. individual single cell inside a larger multicell character.
  201. When a multicell character is created using this protocol, the cursor moves
  202. `s * w` cells to the right, in the same row it was in.
  203. Terminals *should* display a large cursor covering the entire multicell block
  204. when the actual cursor position is on any cell within the block. Block cursors
  205. cover all the cells of the multicell character, bar cursors appear in all the
  206. cells in the first column of the character and so on.
  207. Editing controls
  208. ^^^^^^^^^^^^^^^^^^^^^^^^^
  209. There are many controls used to edit existing screen content such as
  210. inserting characters, deleting characters and lines, etc. These were all
  211. originally specified for the one character per cell paradigm. Here we specify
  212. their interactions with multicell characters.
  213. **Insert characters** (``CSI @`` aka ``ICH``)
  214. When inserting ``n`` characters at cursor position ``x, y`` all characters
  215. after ``x`` on line ``y`` are supposed to be right shifted. This means
  216. that any multi-line character that intersects with the cells on line ``y`` at ``x``
  217. and beyond must be erased. Any single line multicell character that is
  218. split by the cells at ``x`` and ``x + n - 1`` must also be erased.
  219. **Delete characters** (``CSI P`` aka ``DCH``)
  220. When deleting ``n`` characters at cursor position ``x, y`` all characters
  221. after ``x`` on line ``y`` are supposed to be left shifted. This means
  222. that any multi-line character that intersects with the cells on line ``y`` at ``x``
  223. and beyond must be erased. Any single line multicell character that is
  224. split by the cells at ``x`` and ``x + n - 1`` must also be erased.
  225. **Erase characters** (``CSI X`` aka ``ECH``)
  226. When erasing ``n`` characters at cursor position ``x, y`` the ``n`` cells
  227. starting at ``x`` are supposed to be cleared. This means that any multicell
  228. character that intersects with the ``n`` cells starting at ``x`` must be
  229. erased.
  230. **Erase display** (``CSI J`` aka ``ED``)
  231. Any multicell character intersecting with the erased region of the screen
  232. must be erased. When using mode ``22`` the contents of the screen are first
  233. copied into the history, including all multicell characters.
  234. **Erase in line** (``CSI K`` aka ``EL``)
  235. Works just like erase characters above. Any multicell character
  236. intersecting with the erased cells in the line is erased.
  237. **Insert lines** (``CSI L`` aka ``IL``)
  238. When inserting ``n`` lines at cursor position ``y`` any multi-line
  239. characters that are split at the line ``y`` must be erased. A split happens
  240. when the second or subsequent row of the multi-line character is on the line
  241. ``y``. The insertion causes ``n`` lines to be removed from the bottom of
  242. the screen, any multi-line characters are split at the bottom of the screen
  243. must be erased. A split is when any row of the multi-line character except
  244. the last row is on the last line of the screen after the insertion of ``n``
  245. lines.
  246. **Delete lines** (``CSI M`` aka ``DL``)
  247. When deleting ``n`` lines at cursor position ``y`` any multicell character
  248. that intersects the deleted lines must be erased.