# File lib/pdf/writer.rb, line 1667
1667:   def add_text_wrap(x, y, width, text, size = nil, justification = :left, angle = 0, test = false)
1668:     if text.kind_of?(Numeric) and size.kind_of?(String)
1669:       text, size = size, text
1670:       warn PDF::Writer::Lang[:add_textw_parameters_reversed] % caller[0]
1671:     end
1672: 
1673:     if size.nil? or size <= 0
1674:       size = @font_size
1675:     end
1676: 
1677:       # Need to store the initial text state, as this will change during the
1678:       # width calculation, but will need to be re-set before printing, so
1679:       # that the chars work out right
1680:     t_CTS = @current_text_state.dup
1681: 
1682:     select_font("Helvetica") if @fonts.empty?
1683:     return "" if width <= 0
1684: 
1685:     w = brk = brkw = 0
1686:     font = @current_font
1687:     tw = width / size.to_f * 1000
1688: 
1689:     pos = -1
1690:     loop do
1691:       pos += 1
1692:       break if pos == text.size
1693:       font_change = true
1694:       tag_size, text, font_change = quick_text_tags(text, pos, font_change)
1695:       if tag_size != 0
1696:         if font_change
1697:           current_font!
1698:           font = @current_font
1699:         end
1700:         pos += (tag_size - 1)
1701:       else
1702:         w += char_width(font, text[pos, 1])
1703: 
1704:         if w > tw # We need to truncate this line
1705:           if brk > 0 # There is somewhere to break the line.
1706:             if text[brk] == " "
1707:               tmp = text[0, brk]
1708:             else
1709:               tmp = text[0, brk + 1]
1710:             end
1711:             x, adjust = adjust_wrapped_text(tmp, brkw, width, x, justification)
1712: 
1713:               # Reset the text state
1714:             @current_text_state = t_CTS.dup
1715:             current_font!
1716:             add_text(x, y, tmp, size, angle, adjust) unless test
1717:             return text[brk + 1..-1]
1718:           else # just break before the current character
1719:             tmp = text[0, pos]
1720: #           tmpw = (w - char_width(font, text[pos, 1])) * size / 1000.0
1721:             x, adjust = adjust_wrapped_text(tmp, brkw, width, x, justification)
1722: 
1723:               # Reset the text state
1724:             @current_text_state = t_CTS.dup
1725:             current_font!
1726:             add_text(x, y, tmp, size, angle, adjust) unless test
1727:             return text[pos..-1]
1728:           end
1729:         end
1730: 
1731:         if text[pos] == ?-
1732:           brk = pos
1733:           brkw = w * size / 1000.0
1734:         end
1735: 
1736:         if text[pos, 1] == " "
1737:           brk = pos
1738:           ctmp = text[pos]
1739:           ctmp = @fonts[font].differences[ctmp] unless @fonts[font].differences.nil?
1740:           z = @fonts[font].c[tmp].nil? ? 0 : @fonts[font].c[tmp]['WX']
1741:           brkw = (w - z) * size / 1000.0
1742:         end
1743:       end
1744:     end
1745: 
1746:       # There was no need to break this line.
1747:     justification = :left if justification == :full
1748:     tmpw = (w * size) / 1000.0
1749:     x, adjust = adjust_wrapped_text(text, tmpw, width, x, justification)
1750:       # reset the text state
1751:     @current_text_state = t_CTS.dup
1752:     current_font!
1753:     add_text(x, y, text, size, angle, adjust) unless test
1754:     return ""
1755:   end