Module: Prawn::Rtl::Connector

Defined in:
lib/prawn/rtl/connector.rb,
lib/prawn/rtl/connector/logic.rb

Overview

Provides bidirectional text support and Arabic letter connection for Prawn PDF generation.

This module handles RTL (Right-to-Left) text processing by:

  • Connecting Arabic script letters according to their contextual forms

  • Reordering text using the Unicode Bidirectional Algorithm

  • Supporting multiple RTL languages (Arabic, Hebrew, Persian, Urdu, etc.)

  • Handling mixed LTR/RTL text properly

Examples:

Fix Arabic text for PDF rendering

text = "مرحبا بالعالم"
fixed_text = Prawn::Rtl::Connector.fix_rtl(text)

Fix Hebrew text for PDF rendering

text = "שלום עולם"
fixed_text = Prawn::Rtl::Connector.fix_rtl(text)

Fix mixed LTR/RTL text

text = "Hello مرحبا World"
fixed_text = Prawn::Rtl::Connector.fix_rtl(text)

Defined Under Namespace

Modules: Logic

Class Method Summary collapse

Class Method Details

.connect(string) ⇒ String

Connects Arabic letters according to their contextual forms.

Parameters:

  • string (String)

    the text containing Arabic letters to connect

Returns:

  • (String)

    the text with properly connected Arabic letters



32
33
34
# File 'lib/prawn/rtl/connector.rb', line 32

def self.connect(string)
  Prawn::Rtl::Connector::Logic.transform(string)
end

.fix_rtl(string) ⇒ String

Fixes RTL text by connecting Arabic letters and reordering for visual display.

This is the main entry point for processing RTL text. It detects if the text contains RTL characters and applies both letter connection and bidirectional reordering if needed.

Parameters:

  • string (String)

    the text to process

Returns:

  • (String)

    the processed text ready for PDF rendering



44
45
46
47
48
# File 'lib/prawn/rtl/connector.rb', line 44

def self.fix_rtl(string)
  return string unless include_rtl?(string)

  reorder(connect(string))
end

.include_rtl?(string) ⇒ Boolean

Checks if a string contains RTL (Right-to-Left) characters.

Parameters:

  • string (String)

    the text to check

Returns:

  • (Boolean)

    true if the text contains RTL characters, false otherwise



65
66
67
# File 'lib/prawn/rtl/connector.rb', line 65

def self.include_rtl?(string)
  Bidi.contains_rtl?(string)
end

.reorder(string) ⇒ String

Reorders text according to the Unicode Bidirectional Algorithm.

Uses ICU’s BiDi implementation via FFI to visually reorder mixed LTR/RTL text for correct display.

Parameters:

  • string (String)

    the text to reorder

Returns:

  • (String)

    the visually reordered text



57
58
59
# File 'lib/prawn/rtl/connector.rb', line 57

def self.reorder(string)
  Bidi.reorder(string, direction: :rtl)
end