1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
NAME
Text::Bidi - Unicode bidi algorithm using libfribidi
VERSION
version 2.18
SYNOPSIS
# Each displayed line is a "paragraph"
use Text::Bidi qw(log2vis);
($par, $map, $visual) = log2vis($logical);
# or just
$visual = log2vis(...);
# For real paragraphs, need to specify the display width
($par, $map, $visual) = log2vis($logical, $width);
# object oriented approach allows one to display line by line
$p = new Text::Bidi::Paragraph $logical;
$visual = $p->visual($off, $len);
EXPORT
The following functions can be exported (nothing is exported by
default):
* "log2vis"
* "is_bidi"
* "get_mirror_char"
* "get_bidi_type_name"
* "fribidi_version"
* "unicode_version"
* "fribidi_version_num"
All of them can be exported together using the :all tag.
DESCRIPTION
This module provides basic support for the Unicode bidirectional (Bidi)
text algorithm, for displaying text consisting of both left-to-right
and right-to-left written languages (such as Hebrew and Arabic.) It
does so via a swig interface file to the libfribidi library.
The fundamental purpose of the bidi algorithm is to reorder text given
in logical order into text in visually correct order, suitable for
display using standard printing commands. ``Logical order'' means that
the characters are given in the order in which they would be read if
printed correctly. The direction of the text is determined by
properties of the Unicode characters, usually without additional hints.
See http://www.unicode.org/unicode/reports/tr9/ for more details on the
problem and the algorithm.
Standard usage
The bidi algorithm works in two stages. The first is on the level of a
paragraph, where the direction of each character is computed. The
second is on the level of the lines to be displayed. The main practical
difference is that the first stage requires only the text of the
paragraph, while the second requires knowledge of the width of the
displayed lines. The module (or the library) does not determine how the
text is broken into paragraphs.
The full interface is provided by Text::Bidi::Paragraph, see there for
details. This module provides an abbreviation, "log2vis", which
combines creating a paragraph object with calling "visual" in
Text::Bidi::Paragraph on it. It is particularly useful in the case that
the whole paragraph should be displayed at once, and the display width
is known:
$visual = log2vis($logical, $width);
There are more options (see "log2vis"), but this is essentially it. The
rest of this documentation will probably be useful only to people who
are familiar with libfribidi and who wish to extend or modify the
module.
The object-oriented approach
All functions here can be called using either a procedural or an object
oriented approach. For example, you may do either
$visual = log2vis($logical);
or
$bidi = new Text::Bidi;
$visual = $bidi->log2vis($logical);
The advantages of the second form is that it is easier to move to a
sub-class, and that two or more objects with different parameters can
be used simultaneously. If you are interested in deriving from this
class, please see "SUBCLASSING".
FUNCTIONS
get_bidi_type_name
say $tb->get_bidi_type_name($Text::Bidi::Type::LTR); # says 'LTR'
Return the string representation of a Bidi character type, as in
fribidi_get_bidi_type_name(3). Note that for the above example, one
needs to use Text::Bidi::Constants.
log2vis
($p, $visual) = log2vis($logical[,$width[,$dir[,$flags]]]);
Convert the input paragraph $logical to visual. This constructs a
Text::Bidi::Paragraph object, and calls "visual" in
Text::Bidi::Paragraph several times, as required. $width is the maximum
width of a line, defaulting to the whole length of the paragraph. $dir
is the base direction of the paragraph, determined automatically if not
provided. $flags is as in "visual" in Text::Bidi::Paragraph. The
paragraph will be justified to the right if it is RTL.
The output consists of the Text::Bidi::Paragraph object $p and the
visual string $visual.
is_bidi()
my $bidi = is_bidi($logical);
Returns true if the input $logical contains bidi characters. Otherwise,
the output of the bidi algorithm will be identical to the input, hence
this helps if we want to short-circuit.
get_mirror_char()
my $mir = get_mirror_char('['); # $mir == ']'
Return the mirror character of the input, possibly itself.
fribidi_version
say fribidi_version();
Returns the version information for the fribidi library
fribidi_version_num
say fribidi_version_num();
Returns the version number for the fribidi library
unicode_version
say unicode_version();
Returns the Unicode version used by the fribidi library
SUBCLASSING
The rest of the documentation is only interesting if you would like to
derive from this class. The methods listed under "METHODS" are wrappers
around the similarly named functions in libfribidi, and may be useful
for this purpose.
If you do sub-class this class, and would like the procedural interface
to use your functions, put a line like
$Text::Bidi::GlobalClass = __PACKAGE__;
in your module.
METHODS
new
$tb = new Text::Bidi [tie_byte => ..., tie_long => ...];
Create a new Text::Bidi object. If the tie_byte or tie_long options are
given, they should be the names (strings) of the classes used as dual
life arrays, most probably derived class of Text::Bidi::Array::Byte and
Text::Bidi::Array::Long, respectively.
This method is probably of little interest for standard (procedural)
use.
utf8_to_internal
$la = $tb->utf8_to_internal($str);
Convert the Perl string $str into the representation used by
libfribidi. The result will be a Text::Bidi::Array::Long.
internal_to_utf8
$str = $tb->internal_to_utf8($la);
Convert the long array $la, representing a string encoded in to format
used by libfribidi, into a Perl string. The array $la can be either a
Text::Bidi::Array::Long, or anything that can be used to construct it.
get_bidi_types
$types = $tb->get_bidi_types($internal);
Returns a Text::Bidi::Array::Long with the list of Bidi types of the
text given by $internal, a representation of the paragraph text, as
returned by utf8_to_internal(). Wraps fribidi_get_bidi_types(3).
get_joining_types
$types = $tb->get_joining_types($internal);
Returns a Text::Bidi::Array::Byte with the list of joining types of the
text given by $internal, a representation of the paragraph text, as
returned by "utf8_to_internal". Wraps fribidi_get_joining_types(3).
get_joining_type_name
say $tb->get_joining_type_name($Text::Bidi::Joining::U); # says 'U'
Return the string representation of a joining character type, as in
fribidi_get_joining_type_name(3). Note that for the above example, one
needs to use Text::Bidi::Constants.
get_par_embedding_levels
($odir, $lvl) = $tb->get_par_embedding_levels($types[, $dir]);
Return the embedding levels of the characters, whose types are given by
$types. $types is a Text::Bidi::Array::Long of Bidi types, as returned
by "get_bidi_types". $dir is the base paragraph direction. If not
given, it defaults to FRIBIDI_PAR_ON (neutral).
The output is the resolved paragraph direction $odir, and the
Text::Bidi::Array::Byte array $lvl of embedding levels.
join_arabic
$props = $tb->join_arabic($bidi_types, $lvl, $join_types);
Returns a Text::Bidi::Array::Byte with $props, as returned by
fribidi_join_arabic(3). The inputs are $bidi_types, as returned by
"get_bidi_types", $lvl, as returned by "get_par_embedding_levels", and
$join_types as returned by "get_joining_types". Wraps
fribidi_join_arabic(3).
shaped
($newp, $shaped) = $tb->shaped($flags, $lvl, $prop, $internal);
Returns the internal representation of the paragraph, with shaping
applied. The internal representation of the original paragraph (as
returned by "utf8_to_internal") should be passed in $internal, while
the embedding levels (as returned by "get_par_embedding_levels") should
be in $lvl. See the documentation of fribidi-arabic.h for $flags, but
as a special case, a value of undef here skips shaping (returning
($prop, $internal)), while any other false value becomes the default.
$prop is as returned by "join_arabic". This method wraps
fribidi_shape_arabic(3).
mirrored
$mirrored = $tb->mirrored($lvl, $internal);
Returns the internal representation of the paragraph, with mirroring
applied. The internal representation of the original paragraph (as
returned by "utf8_to_internal") should be passed in $internal, while
the embedding levels (as returned by "get_par_embedding_levels") should
be in $lvl. This method wraps fribidi_shape_mirroring(3).
reorder
$str = $tb->reorder($in, $map[, $offset[, $len]]);
say $tb->reorder([qw(A B C)], [2, 0, 1]); # says CAB
View the array ref $map as a permutation, and permute the list (of
characters) $in according to it. The result is joined, to obtain a
string. If $offset and $len are given, returns only that part of the
resulting string.
reorder_map
($elout, $mout) = $tb->reorder_map($types, $offset, $len, $par,
$map, $el, $flags);
Compute the reordering map for bidi types given by $types, for the
interval starting with $offset of length $len. Note that this part of
the algorithm depends on the interval in an essential way. $types is an
array of types, as computed by "get_bidi_types". The other arguments
are optional:
$par
The base paragraph direction. Computed via "get_par_embedding_levels"
if not defined.
$map
An array ref (or a Text::Bidi::Array::Long) from a previous call
(with a different interval). The method is called repeatedly for the
same paragraph, with different intervals, and the reordering map is
updated for the given interval. If not defined, initialised to the
identity map.
$el
The embedding levels. If not given, computed by a call to
"get_par_embedding_levels".
$flags
A specification of flags, as described in fribidi_reorder_line(3).
The flags can be given either as a number (using
$Text::Bidi::Flags::.. from Text::Bidi::Constants), or as a hashref
of the form {REORDER_NSM => 1}. Defaults to FRIBIDI_FLAGS_DEFAULT.
The output consists of the modified map $mout (a
Text::Bidi::Array::Long), and possibly modified embedding levels
$elout.
method remove_bidi_marks
($v, $to, $from, $levels) =
$tb->remove_bidi_marks($v[, $to[, $from[, $levels]]])
Remove the explicit bidi marks from $v. The optional arguments, if
given, are the map from the logical to the visual string, the inverse
map, and embedding levels, respectively, as returned by "reorder_map".
The inverse map $from can be obtained from the direct one $to by a
command like:
@$from[@$map] = 0..$#$map
Each of the arguments can be undef, in which case it will be skipped.
This implements step X9, see fribidi_remove_bidi_marks(3).
BUGS
There are no real tests for any of this.
Shaping is not supported (probably), since I don't know what it is.
Help welcome!
SEE ALSO
Text::Bidi::Paragraph
Text::Bidi::Constants
Encode
The fribidi library <http://fribidi.org/>
Swig <http://www.swig.org>
The unicode bidi algorithm
<http://www.unicode.org/unicode/reports/tr9/>
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.
|