Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions unicode/bidi/bidi.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ import (
type Direction int

const (
// Neutral means that text contains no left-to-right and right-to-left
// characters and that no default direction has been set.
Neutral Direction = iota

// LeftToRight indicates the text contains no right-to-left characters and
// that either there are some left-to-right characters or the option
// DefaultDirection(LeftToRight) was passed.
LeftToRight Direction = iota
LeftToRight

// RightToLeft indicates the text contains no left-to-right characters and
// that either there are some right-to-left characters or the option
Expand All @@ -42,10 +46,6 @@ const (
// Mixed indicates text contains both left-to-right and right-to-left
// characters.
Mixed

// Neutral means that text contains no left-to-right and right-to-left
// characters and that no default direction has been set.
Neutral
)

type options struct {
Expand Down Expand Up @@ -219,8 +219,11 @@ func (p *Paragraph) Order() (Ordering, error) {
fn(&p.options)
}
lvl := level(-1)
if p.options.defaultDirection == RightToLeft {
switch p.options.defaultDirection {
case RightToLeft:
lvl = 1
case LeftToRight:
lvl = 0
}
para, err := newParagraph(p.types, p.pairTypes, p.pairValues, lvl)
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions unicode/bidi/bidi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,38 @@ func TestMixedSimple(t *testing.T) {
}
}

func TestDirectionLTR(t *testing.T) {
str := "ع a"
p := Paragraph{}
p.SetString(str, DefaultDirection(LeftToRight))
order, err := p.Order()
if err != nil {
log.Fatal(err)
}

expectedRuns := []runInformation{
{"ع", RightToLeft, 0, 0},
{" a", LeftToRight, 1, 2},
}

if nr, want := order.NumRuns(), len(expectedRuns); nr != want {
t.Errorf("order.NumRuns() = %d; want %d", nr, want)
}

for i, want := range expectedRuns {
r := order.Run(i)
if got := r.String(); got != want.str {
t.Errorf("Run(%d) = %q; want %q", i, got, want.str)
}
if s, e := r.Pos(); s != want.start || e != want.end {
t.Errorf("Run(%d).start = %d, .end = %d; want start = %d, end = %d", i, s, e, want.start, want.end)
}
if d := r.Direction(); d != want.dir {
t.Errorf("Run(%d).Direction = %d; want %d", i, d, want.dir)
}
}
}

func TestDefaultDirection(t *testing.T) {
str := "+"
p := Paragraph{}
Expand Down