View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Moshe Kamensky > Text-Bidi-0.03 > Text::Bidi



Annotate this POD


New  1
Open  2
View/Report Bugs
Module Version: 0.03   Source   Latest Release: Text-Bidi-2.12


Text::Bidi - Unicode bidi algorithm using libfribidi


    use Text::Bidi;
    $visual = log2vis($logical);

    ($visual, $paradir, $l2v, $v2l, $embedding) =
        log2vis($logical, $paradir);


The following functions can be exported:

All of them can be exported together using the :all tag.

Description ^

This module provides basic support for the Unicode bidirectional text (Bidi) algorithm, for displaying text consisting of both left-to-right and right-to-left written languages (like Hebrew and Arabic.) It does so using a swig interface file to the libfribidi library.

Though several libfribidi functions are provided by the swig interface file, the standard usage of this module is provided by one function, "log2vis()", that translates a logical string into a visual one. In addition, there are several utility functions, and some functions that implement part of the algorithm (see "Comparison with libfribidi and" for the reason this is needed.)

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);


        $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 do sub-class this class, and want the procedural interface to use your functions, put a line like

        $Text::Bidi::GlobalClass = __PACKAGE__;

in your module.

Types and Namespaces ^

The following constants are imported from the fribidi library:

In addition, the hash %Mirrored maps mirrored characters to their counter parts, and the scalar $Mirrored is a pattern that matches one mirrored character.

Functions ^

The following functions are of interest to the user


Create a new instance of a bidi translator. The following key-value parameters are allowed:


The width, in characters, of the displayed string. This affects the reordering algorithm. The default is undef, which will assume that no line-breaking happens.


A string of the characters that function as field (segment) separators. The default is "\x{2029}\x{09}\x{11}", which is (to my understanding) the unicode specification.


If true, "log2vis()" will remove any explicit bidi marks in the visual string, and adjust the mapping arrays accordingly. Default is true.

These parameters can be accessed using "get_width()", "set_width()" and similar functions.







Query or set the values of the corresponding parameters. See "new()" for details about the parameters.


This function provides the main functionality. It can be called, similarly to fribidi_log2vis of the fribidi library, as follows:

        ($vis, $dir, $l2v, $v2l, $levels) =
            log2vis($log[, $dir[, $width]])

The arguments are:


The logical string


Override the base direction of the paragraph. The possible values are $Text::Bidi::Type::RTL, $Text::Bidi::Type::LTR or $Text::Bidi::Type::ON. The default, if not given, is $Text::Bidi::Type::ON, which means that the direction should be determined according to the bidi algorithm.


The width at which the string is broken. This overrides, and has the same meaning, as the width parameter set by "set_width()". As with that parameter, a value of undef means that no line breaking should be done.

The outputs are as follows:


The visual string. In scalar context, this is the only parameter returned (and in this case the function may work slightly faster.)


An arrayref representing the map from the logical string to the visual one, so the $i-th character of the logical string will be in position $l2v-[$i]> of the visual one.


The inverse function, mapping characters in the visual string to the logical one.


The embedding levels - an arrayref assigning to each character of the logical string the nesting level of text of different directions to which it belongs. Pure left-to-right text has embedding level 0. A character is left-to-right (within this string) iff it has even embedding level.

Functions implementing parts of the algorithm

The following functions, that implement parts of the algorithm, are used by "log2vis()"


This function accepts an arrayref of embedding levels and returns an arrayref that, at place $i, contains a hash of intervals (to the index of the start of the interval it assigns the index of the end of it), such that each of them is a maximal interval of embedding levels at least $i. For example, to the embedding levels:


we get

        { 0 => 18 },
        { 2 => 14, 18 => 18 },
        { 5 => 6, 10 => 14 },
        { 10 => 12 }


This function implements the reordering part of the bidi algorithm (section 3.4, L1-L4.) The input is the logical string, the (arrayref of) embedding levels, the base dir of the paragraph, a position in the logical string, and a length. The default for the position is 0, and for the length till the end of the string. The function will return the v2l mapping, the modified embedding levels, the intervals for these levels (as computed by "levels2intervals()") and the visual string, all for the part of the string given by the position and the length, and assuming that the string is broken after this segment. In scalar context, only the visual string is returned.


Compute the inverse of a function given by an array. This is used to convert the "$v2l" mapping to "$l2v".

Utility functions

The following functions are available mainly for testing. See also Text::Bidi::CapRTL for a possibly simpler interface.


Convert a string where right-to-left text is represented by capital letters, and bidi marks by control sequences, to a string with actual right-to-left characters and bidi marks. The control sequences are of the form _C, where C is a character. Run

        fribidi --charsetdesc CapRTL

for a description of the translation table.


Perform the inverse of "caprtl_to_unicode()"

Comparison with libfribidi and ^

The module has mostly the same interface as FriBidi, the module written originally with the fribidi library. The main differences are:


The "caprtl_to_unicode()" and "unicode_to_caprtl()" functions currently do not work, because of what appears to be a bug in libfribidi. The details are in


Text::Bidi::CapRTL, Encode

The fribidi library:,


The unicode bidi algorithm:


Moshe Kamensky,


Copyright 2006 Moshe Kamensky, all rights reserved.

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

syntax highlighting: