code: def splitIntoLines(self, maxWidth, maxHeight, splitted=False):
          
          self.lines = []
          self.height = 0
          self.maxWidth = self.width = maxWidth
          self.maxHeight = maxHeight
          boxStack = []
  
          style = self.style
          x = 0
  
          # Start with indent in first line of text
          if not splitted:
              x = style["textIndent"]
  
          lenText = len(self)
          pos = 0
          while pos < lenText:
  
              # Reset values for new line
              posBegin = pos
              line = Line(style)
  
              # Update boxes for next line
              for box in copy.copy(boxStack):
                  box["x"] = 0
                  line.append(BoxBegin(box))
  
              while pos < lenText:
  
                  # Get fragment, its width and set X
                  frag = self[pos]
                  fragWidth = frag["width"]
                  frag["x"] = x
                  pos += 1
  
                  # Keep in mind boxes for next lines
                  if isinstance(frag, BoxBegin):
                      boxStack.append(frag)
                  elif isinstance(frag, BoxEnd):
                      boxStack.pop()
  
                  # If space or linebreak handle special way
                  if frag.isSoft:
                      if frag.isLF:
                          line.append(frag)
                          break
                          # First element of line should not be a space
                      if x == 0:
                          continue
                          # Keep in mind last possible line break
  
                  # The elements exceed the current line
                  elif fragWidth + x > maxWidth:
                      break
  
                  # Add fragment to line and update x
                  x += fragWidth
                  line.append(frag)
  
              # Remove trailing white spaces
              while line and line[-1].name in ("space", "br"):
                  line.pop()
  
              # Add line to list
              line.dumpFragments()
              # if line:
              self.height += line.doLayout(self.width)
              self.lines.append(line)
  
              # If not enough space for current line force to split
              if self.height > maxHeight:
                  return posBegin
  
              # Reset variables
              x = 0
  
          # Apply alignment
          self.lines[- 1].isLast = True
          for line in self.lines:
              line.doAlignment(maxWidth, style["textAlign"])
  
          return None
docs: Split text into lines and calculate X positions. If we need more
          space in height than available we return the rest of the text