/**
  * Adjust the justification of each of our lines.
  * http://www.w3.org/TR/CSS21/text.html#propdef-text-align
  */
 protected function _text_align()
 {
     $style = $this->_frame->get_style();
     $w = $this->_frame->get_containing_block("w");
     $width = $style->length_in_pt($style->width, $w);
     switch ($style->text_align) {
         default:
         case "left":
             foreach ($this->_frame->get_line_boxes() as $line) {
                 if (!$line->left) {
                     continue;
                 }
                 foreach ($line->get_frames() as $frame) {
                     if ($frame instanceof Block_Frame_Decorator) {
                         continue;
                     }
                     $frame->set_position($frame->get_position("x") + $line->left);
                 }
             }
             return;
         case "right":
             foreach ($this->_frame->get_line_boxes() as $line) {
                 // Move each child over by $dx
                 $dx = $width - $line->w - $line->right;
                 foreach ($line->get_frames() as $frame) {
                     // Block frames are not aligned by text-align
                     if ($frame instanceof Block_Frame_Decorator) {
                         continue;
                     }
                     $frame->set_position($frame->get_position("x") + $dx);
                 }
             }
             break;
         case "justify":
             // We justify all lines except the last one
             $lines = $this->_frame->get_line_boxes();
             // needs to be a variable (strict standards)
             $lines = array_splice($lines, 0, -1);
             foreach ($lines as $i => $line) {
                 if ($line->br) {
                     unset($lines[$i]);
                 }
             }
             // One space character's width. Will be used to get a more accurate spacing
             $space_width = Font_Metrics::get_text_width(" ", $style->font_family, $style->font_size);
             foreach ($lines as $i => $line) {
                 if ($line->left) {
                     foreach ($line->get_frames() as $frame) {
                         if (!$frame instanceof Text_Frame_Decorator) {
                             continue;
                         }
                         $frame->set_position($frame->get_position("x") + $line->left);
                     }
                 }
                 // Only set the spacing if the line is long enough.  This is really
                 // just an aesthetic choice ;)
                 //if ( $line["left"] + $line["w"] + $line["right"] > self::MIN_JUSTIFY_WIDTH * $width ) {
                 // Set the spacing for each child
                 if ($line->wc > 1) {
                     $spacing = ($width - ($line->left + $line->w + $line->right) + $space_width) / ($line->wc - 1);
                 } else {
                     $spacing = 0;
                 }
                 $dx = 0;
                 foreach ($line->get_frames() as $frame) {
                     if (!$frame instanceof Text_Frame_Decorator) {
                         continue;
                     }
                     $text = $frame->get_text();
                     $spaces = mb_substr_count($text, " ");
                     $char_spacing = $style->length_in_pt($style->letter_spacing);
                     $_spacing = $spacing + $char_spacing;
                     $frame->set_position($frame->get_position("x") + $dx);
                     $frame->set_text_spacing($_spacing);
                     $dx += $spaces * $_spacing;
                 }
                 // The line (should) now occupy the entire width
                 $this->_frame->set_line($i, null, $width);
                 //}
             }
             break;
         case "center":
         case "centre":
             foreach ($this->_frame->get_line_boxes() as $line) {
                 // Centre each line by moving each frame in the line by:
                 $dx = ($width + $line->left - $line->w - $line->right) / 2;
                 foreach ($line->get_frames() as $frame) {
                     // Block frames are not aligned by text-align
                     if ($frame instanceof Block_Frame_Decorator) {
                         continue;
                     }
                     $frame->set_position($frame->get_position("x") + $dx);
                 }
             }
             break;
     }
 }