# File lib/pdf/writer.rb, line 1604
1604:   def add_text_wrap(x, y, width, text, size = nil, justification = :left, angle = 0, test = false)
1605:     if text.kind_of?(Numeric) and size.kind_of?(String)
1606:       text, size = size, text
1607:       warn PDF::Writer::Lang[:add_textw_parameters_reversed] % caller[0]
1608:     end
1609: 
1610:     if size.nil? or size <= 0
1611:       size = @font_size
1612:     end
1613: 
1614:       # Need to store the initial text state, as this will change during the
1615:       # width calculation, but will need to be re-set before printing, so
1616:       # that the chars work out right
1617:     t_CTS = @current_text_state.dup
1618: 
1619:     select_font("Helvetica") if @fonts.empty?
1620:     return "" if width <= 0
1621: 
1622:     w = brk = brkw = 0
1623:     font = @current_font
1624:     tw = width / size.to_f * 1000
1625: 
1626:     pos = -1
1627:     loop do
1628:       pos += 1
1629:       break if pos == text.size
1630:       font_change = true
1631:       tag_size, text, font_change = quick_text_tags(text, pos, font_change)
1632:       if tag_size != 0
1633:         if font_change
1634:           current_font!
1635:           font = @current_font
1636:         end
1637:         pos += (tag_size - 1)
1638:       else
1639:         w += char_width(font, text[pos, 1])
1640: 
1641:         if w > tw # We need to truncate this line
1642:           if brk > 0 # There is somewhere to break the line.
1643:             if text[brk] == " "
1644:               tmp = text[0, brk]
1645:             else
1646:               tmp = text[0, brk + 1]
1647:             end
1648:             x, adjust = adjust_wrapped_text(tmp, brkw, width, x, justification)
1649: 
1650:               # Reset the text state
1651:             @current_text_state = t_CTS.dup
1652:             current_font!
1653:             add_text(x, y, tmp, size, angle, adjust) unless test
1654:             return text[brk + 1..-1]
1655:           else # just break before the current character
1656:             tmp = text[0, pos]
1657: #           tmpw = (w - char_width(font, text[pos, 1])) * size / 1000.0
1658:             x, adjust = adjust_wrapped_text(tmp, brkw, width, x, justification)
1659: 
1660:               # Reset the text state
1661:             @current_text_state = t_CTS.dup
1662:             current_font!
1663:             add_text(x, y, tmp, size, angle, adjust) unless test
1664:             return text[pos..-1]
1665:           end
1666:         end
1667: 
1668:         if text[pos] == ?-
1669:           brk = pos
1670:           brkw = w * size / 1000.0
1671:         end
1672: 
1673:         if text[pos, 1] == " "
1674:           brk = pos
1675:           ctmp = text[pos]
1676:           ctmp = @fonts[font].differences[ctmp] unless @fonts[font].differences.nil?
1677:           z = @fonts[font].c[tmp].nil? ? 0 : @fonts[font].c[tmp]['WX']
1678:           brkw = (w - z) * size / 1000.0
1679:         end
1680:       end
1681:     end
1682: 
1683:       # There was no need to break this line.
1684:     justification = :left if justification == :full
1685:     tmpw = (w * size) / 1000.0
1686:     x, adjust = adjust_wrapped_text(text, tmpw, width, x, justification)
1687:       # reset the text state
1688:     @current_text_state = t_CTS.dup
1689:     current_font!
1690:     add_text(x, y, text, size, angle, adjust) unless test
1691:     return ""
1692:   end