NAME

Text::Bidi::Paragraph - Run the bidi algorithm on one paragraph

VERSION

version 2.18

SYNOPSIS

use Text::Bidi::Paragraph;

my $par = new Text::Bidi::Paragraph $logical;
my $offset = 0;
my $width = 80;
while ( $offset < $p->len ) {
    my $v = $p->visual($offset, $width);
    say $v;
    $offset += $width;
}

DESCRIPTION

This class provides the main interface for applying the bidi algorithm in full generality. In the case where the paragraph can be formatted at once, "log2vis" in Text::Bidi can be used as a shortcut.

A paragraph is processed by creating a Text::Bidi::Paragraph object:

$par = new Text::Bidi::Paragraph $logical;

Here $logical is the text of the paragraph. This applies the first stages of the bidi algorithm: computation of the embedding levels. Once this is done, the text can be displayed using the "visual" method, which does the reordering.

METHODS

new

my $par = new Text::Bidi::Paragraph $logical, ...;

Create a new object corresponding to a text $logical in logical order. The other arguments are key-value pairs. The only ones that have a meaning at the moment are bd, which supplies the Text::Bidi object to use, dir, which prescribes the direction of the paragraph, and shape, which determines shaping flags. The value of dir is a constant in Text::Bidi::Par:: (e.g., $Text::Bidi::Par::RTL; see Text::Bidi::Constants). The value of shape is a constant from fribidi_shape_arabic(3). If it is undef, no shaping is done. If it is missing, default shaping will be performed if the paragraph contains Arabic text.

Note that the mere creation of $par runs the bidi algorithm on the given text $logical up to the point of reordering (which is dealt with in "visual").

par

my $logical = $par->par;

Returns the logical (input) text corresponding to this paragraph.

dir

my $dir = $par->dir;

Returns the direction of this paragraph, a constant in the $Text::Bidi::Par:: namespace.

len

my $len = $par->len;

The length of this paragraph.

types

my $types = $par->types;

The Bidi types of the characters in this paragraph. Each element of @$types is a constant in the $Text::Bidi::Type:: namespace.

levels

my $levels = $par->levels;

The embedding levels for this paragraph. Each element of @$levels is an integer.

bd

my $bd = $par->bd;

The Text::Bidi object used to interface with libfribidi.

map

my $map = $par->map;

The map from the logical text to the visual, i.e., the values in $map are indices in the logical string, so that the $i-th character of the visual string is the character that occurs at $map->[$i] in the logical string.

This is updated on each call to "visual", so that the map for the full paragraph is correct only after calling "visual" for the whole text.

type_names

@types = $par->type_names;

Returns the list of bidi types as strings

is_rtl

my $rtl = $par->is_rtl;

Returns true if the direction of the paragraph is RTL (right to left).

ar_props

$props = $self->ar_props

Return the shaping properties (TODO)

shaped

$shaped = $self->shaped(flags)

Return the shaped paragraph, and fix ar_props (TODO)

visual

my $visual = $par->visual($offset, $length, $flags);

Return the visual representation of the part of the paragraph $par starting at $offset and of length $length. $par is a Text::Bidi::Paragraph object. All arguments are optional, with $offset defaulting to 0 and $length to the length till the end of the paragraph (see below from $flags).

Note that this method does not take care of right-justifying the text if the paragraph direction is RTL. Hence a typical application might look as follows:

my $visual = $par->visual($offset, $width, $flags);
my $len = length($visual);
$visual = (' ' x ($width - $len)) . $visual if $par->is_rtl;

Note also that the length of the result might be strictly less than $length.

The $flags argument, if defined, should be either a hashref or an integer. If it is a number, its meaning is the same as in fribidi_reorder_line(3). A hashref is converted to the corresponding values for keys whose value is true. The keys should be the same as the constants in fribidi-types.h, with the prefix FRIBIDI_FLAGS_ removed.

In addition, the $flags hashref may contain lower-case keys. The only one recognised at the moment is break. Its value, if given, should be a string at which the line should be broken. Hence, if this key is given, the actual length is potentially reduced, so that the line breaks at the given string (if possible). A typical value for break is ' '.

SEE ALSO

Text::Bidi

AUTHOR

Moshe Kamensky <kamensky@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2015 by Moshe Kamensky.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.