bidi.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright 2015 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:generate go run gen.go gen_trieval.go gen_ranges.go
  5. // Package bidi contains functionality for bidirectional text support.
  6. //
  7. // See https://www.unicode.org/reports/tr9.
  8. //
  9. // NOTE: UNDER CONSTRUCTION. This API may change in backwards incompatible ways
  10. // and without notice.
  11. package bidi // import "golang.org/x/text/unicode/bidi"
  12. // TODO:
  13. // The following functionality would not be hard to implement, but hinges on
  14. // the definition of a Segmenter interface. For now this is up to the user.
  15. // - Iterate over paragraphs
  16. // - Segmenter to iterate over runs directly from a given text.
  17. // Also:
  18. // - Transformer for reordering?
  19. // - Transformer (validator, really) for Bidi Rule.
  20. // This API tries to avoid dealing with embedding levels for now. Under the hood
  21. // these will be computed, but the question is to which extent the user should
  22. // know they exist. We should at some point allow the user to specify an
  23. // embedding hierarchy, though.
  24. // A Direction indicates the overall flow of text.
  25. type Direction int
  26. const (
  27. // LeftToRight indicates the text contains no right-to-left characters and
  28. // that either there are some left-to-right characters or the option
  29. // DefaultDirection(LeftToRight) was passed.
  30. LeftToRight Direction = iota
  31. // RightToLeft indicates the text contains no left-to-right characters and
  32. // that either there are some right-to-left characters or the option
  33. // DefaultDirection(RightToLeft) was passed.
  34. RightToLeft
  35. // Mixed indicates text contains both left-to-right and right-to-left
  36. // characters.
  37. Mixed
  38. // Neutral means that text contains no left-to-right and right-to-left
  39. // characters and that no default direction has been set.
  40. Neutral
  41. )
  42. type options struct{}
  43. // An Option is an option for Bidi processing.
  44. type Option func(*options)
  45. // ICU allows the user to define embedding levels. This may be used, for example,
  46. // to use hierarchical structure of markup languages to define embeddings.
  47. // The following option may be a way to expose this functionality in this API.
  48. // // LevelFunc sets a function that associates nesting levels with the given text.
  49. // // The levels function will be called with monotonically increasing values for p.
  50. // func LevelFunc(levels func(p int) int) Option {
  51. // panic("unimplemented")
  52. // }
  53. // DefaultDirection sets the default direction for a Paragraph. The direction is
  54. // overridden if the text contains directional characters.
  55. func DefaultDirection(d Direction) Option {
  56. panic("unimplemented")
  57. }
  58. // A Paragraph holds a single Paragraph for Bidi processing.
  59. type Paragraph struct {
  60. // buffers
  61. }
  62. // SetBytes configures p for the given paragraph text. It replaces text
  63. // previously set by SetBytes or SetString. If b contains a paragraph separator
  64. // it will only process the first paragraph and report the number of bytes
  65. // consumed from b including this separator. Error may be non-nil if options are
  66. // given.
  67. func (p *Paragraph) SetBytes(b []byte, opts ...Option) (n int, err error) {
  68. panic("unimplemented")
  69. }
  70. // SetString configures p for the given paragraph text. It replaces text
  71. // previously set by SetBytes or SetString. If b contains a paragraph separator
  72. // it will only process the first paragraph and report the number of bytes
  73. // consumed from b including this separator. Error may be non-nil if options are
  74. // given.
  75. func (p *Paragraph) SetString(s string, opts ...Option) (n int, err error) {
  76. panic("unimplemented")
  77. }
  78. // IsLeftToRight reports whether the principle direction of rendering for this
  79. // paragraphs is left-to-right. If this returns false, the principle direction
  80. // of rendering is right-to-left.
  81. func (p *Paragraph) IsLeftToRight() bool {
  82. panic("unimplemented")
  83. }
  84. // Direction returns the direction of the text of this paragraph.
  85. //
  86. // The direction may be LeftToRight, RightToLeft, Mixed, or Neutral.
  87. func (p *Paragraph) Direction() Direction {
  88. panic("unimplemented")
  89. }
  90. // RunAt reports the Run at the given position of the input text.
  91. //
  92. // This method can be used for computing line breaks on paragraphs.
  93. func (p *Paragraph) RunAt(pos int) Run {
  94. panic("unimplemented")
  95. }
  96. // Order computes the visual ordering of all the runs in a Paragraph.
  97. func (p *Paragraph) Order() (Ordering, error) {
  98. panic("unimplemented")
  99. }
  100. // Line computes the visual ordering of runs for a single line starting and
  101. // ending at the given positions in the original text.
  102. func (p *Paragraph) Line(start, end int) (Ordering, error) {
  103. panic("unimplemented")
  104. }
  105. // An Ordering holds the computed visual order of runs of a Paragraph. Calling
  106. // SetBytes or SetString on the originating Paragraph invalidates an Ordering.
  107. // The methods of an Ordering should only be called by one goroutine at a time.
  108. type Ordering struct{}
  109. // Direction reports the directionality of the runs.
  110. //
  111. // The direction may be LeftToRight, RightToLeft, Mixed, or Neutral.
  112. func (o *Ordering) Direction() Direction {
  113. panic("unimplemented")
  114. }
  115. // NumRuns returns the number of runs.
  116. func (o *Ordering) NumRuns() int {
  117. panic("unimplemented")
  118. }
  119. // Run returns the ith run within the ordering.
  120. func (o *Ordering) Run(i int) Run {
  121. panic("unimplemented")
  122. }
  123. // TODO: perhaps with options.
  124. // // Reorder creates a reader that reads the runes in visual order per character.
  125. // // Modifiers remain after the runes they modify.
  126. // func (l *Runs) Reorder() io.Reader {
  127. // panic("unimplemented")
  128. // }
  129. // A Run is a continuous sequence of characters of a single direction.
  130. type Run struct {
  131. }
  132. // String returns the text of the run in its original order.
  133. func (r *Run) String() string {
  134. panic("unimplemented")
  135. }
  136. // Bytes returns the text of the run in its original order.
  137. func (r *Run) Bytes() []byte {
  138. panic("unimplemented")
  139. }
  140. // TODO: methods for
  141. // - Display order
  142. // - headers and footers
  143. // - bracket replacement.
  144. // Direction reports the direction of the run.
  145. func (r *Run) Direction() Direction {
  146. panic("unimplemented")
  147. }
  148. // Position of the Run within the text passed to SetBytes or SetString of the
  149. // originating Paragraph value.
  150. func (r *Run) Pos() (start, end int) {
  151. panic("unimplemented")
  152. }
  153. // AppendReverse reverses the order of characters of in, appends them to out,
  154. // and returns the result. Modifiers will still follow the runes they modify.
  155. // Brackets are replaced with their counterparts.
  156. func AppendReverse(out, in []byte) []byte {
  157. panic("unimplemented")
  158. }
  159. // ReverseString reverses the order of characters in s and returns a new string.
  160. // Modifiers will still follow the runes they modify. Brackets are replaced with
  161. // their counterparts.
  162. func ReverseString(s string) string {
  163. panic("unimplemented")
  164. }