def insertTextbox(self, rect, buffer,<EOL>fontname="<STR_LIT>",<EOL>fontfile=None,<EOL>fontsize=<NUM_LIT:11>,<EOL>set_simple=<NUM_LIT:0>,<EOL>encoding=<NUM_LIT:0>,<EOL>color=None,<EOL>fill=None,<EOL>expandtabs=<NUM_LIT:1>,<EOL>border_width=<NUM_LIT:1>,<EOL>align=<NUM_LIT:0>,<EOL>render_mode=<NUM_LIT:0>,<EOL>rotate=<NUM_LIT:0>,<EOL>morph=None):
rect = Rect(rect)<EOL>if rect.isEmpty or rect.isInfinite:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>color_str = ColorCode(color, "<STR_LIT:c>")<EOL>fill_str = ColorCode(fill, "<STR_LIT:f>")<EOL>if fill is None and render_mode == <NUM_LIT:0>:    <EOL><INDENT>fill = color<EOL>fill_str = ColorCode(color, "<STR_LIT:f>")<EOL><DEDENT>if rotate % <NUM_LIT> != <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>rot = rotate<EOL>while rot < <NUM_LIT:0>: rot += <NUM_LIT><EOL>rot = rot % <NUM_LIT><EOL>if not bool(buffer):<EOL><INDENT>return rect.height if rot in (<NUM_LIT:0>, <NUM_LIT>) else rect.width<EOL><DEDENT>cmp90 = "<STR_LIT>"   <EOL>cmm90 = "<STR_LIT>"   <EOL>cm180 = "<STR_LIT>"  <EOL>height = self.height<EOL>fname = fontname<EOL>if fname.startswith("<STR_LIT:/>"):<EOL><INDENT>fname = fname[<NUM_LIT:1>:]<EOL><DEDENT>xref = self.page.insertFont(fontname=fname,<EOL>fontfile=fontfile,<EOL>encoding=encoding,<EOL>set_simple=set_simple,<EOL>)<EOL>fontinfo = CheckFontInfo(self.doc, xref)<EOL>fontdict = fontinfo[<NUM_LIT:1>]<EOL>ordering = fontdict["<STR_LIT>"]<EOL>simple = fontdict["<STR_LIT>"]<EOL>glyphs = fontdict["<STR_LIT>"]<EOL>bfname = fontdict["<STR_LIT:name>"]<EOL>if type(buffer) in (list, tuple):<EOL><INDENT>t0 = "<STR_LIT:\n>".join(buffer)<EOL><DEDENT>else:<EOL><INDENT>t0 = buffer<EOL><DEDENT>maxcode = max([ord(c) for c in t0])<EOL>if simple and maxcode > <NUM_LIT:255>:<EOL><INDENT>t0 = "<STR_LIT>".join([c if ord(c)<<NUM_LIT> else "<STR_LIT:?>" for c in t0])<EOL><DEDENT>t0 = t0.splitlines()<EOL>glyphs = self.doc.getCharWidths(xref, maxcode + <NUM_LIT:1>)<EOL>if simple and bfname not in ("<STR_LIT>", "<STR_LIT>"):<EOL><INDENT>tj_glyphs = None<EOL><DEDENT>else:<EOL><INDENT>tj_glyphs = glyphs<EOL><DEDENT>def pixlen(x):<EOL><INDENT>"""<STR_LIT>"""<EOL>if ordering < <NUM_LIT:0>:<EOL><INDENT>return sum([glyphs[ord(c)][<NUM_LIT:1>] for c in x]) * fontsize<EOL><DEDENT>else:<EOL><INDENT>return len(x) * fontsize<EOL><DEDENT><DEDENT>if ordering < <NUM_LIT:0>:<EOL><INDENT>blen = glyphs[<NUM_LIT:32>][<NUM_LIT:1>] * fontsize       <EOL><DEDENT>else:<EOL><INDENT>blen = fontsize<EOL><DEDENT>text = "<STR_LIT>"                               <EOL>lheight = fontsize * <NUM_LIT>                <EOL>if CheckMorph(morph):<EOL><INDENT>m1 = Matrix(<NUM_LIT:1>, <NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:1>, morph[<NUM_LIT:0>].x + self.x,<EOL>self.height - morph[<NUM_LIT:0>].y - self.y)<EOL>mat = ~m1 * morph[<NUM_LIT:1>] * m1<EOL>cm = "<STR_LIT>" % JM_TUPLE(mat)<EOL><DEDENT>else:<EOL><INDENT>cm = "<STR_LIT>"<EOL><DEDENT>progr = <NUM_LIT:1>                               <EOL>c_pnt = Point(<NUM_LIT:0>, fontsize)         <EOL>if rot == <NUM_LIT:0>:                            <EOL><INDENT>point = rect.tl + c_pnt             <EOL>pos = point.y + self.y              <EOL>maxwidth = rect.width               <EOL>maxpos = rect.y1 + self.y           <EOL><DEDENT>elif rot == <NUM_LIT>:                         <EOL><INDENT>c_pnt = Point(fontsize, <NUM_LIT:0>)     <EOL>point = rect.bl + c_pnt             <EOL>pos = point.x + self.x              <EOL>maxwidth = rect.height              <EOL>maxpos = rect.x1 + self.x           <EOL>cm += cmp90<EOL><DEDENT>elif rot == <NUM_LIT>:                        <EOL><INDENT>c_pnt = -Point(<NUM_LIT:0>, fontsize)    <EOL>point = rect.br + c_pnt             <EOL>pos = point.y + self.y              <EOL>maxwidth = rect.width               <EOL>progr = -<NUM_LIT:1>                          <EOL>maxpos = rect.y0 + self.y           <EOL>cm += cm180<EOL><DEDENT>else:                                   <EOL><INDENT>c_pnt = -Point(fontsize, <NUM_LIT:0>)    <EOL>point = rect.tr + c_pnt             <EOL>pos = point.x + self.x              <EOL>maxwidth = rect.height              <EOL>progr = -<NUM_LIT:1>                          <EOL>maxpos = rect.x0 + self.x           <EOL>cm += cmm90<EOL><DEDENT>just_tab = []                           <EOL>for i, line in enumerate(t0):<EOL><INDENT>line_t = line.expandtabs(expandtabs).split("<STR_LIT:U+0020>")  <EOL>lbuff = "<STR_LIT>"                          <EOL>rest = maxwidth                     <EOL>for word in line_t:<EOL><INDENT>pl_w = pixlen(word)             <EOL>if rest >= pl_w:                <EOL><INDENT>lbuff += word + "<STR_LIT:U+0020>"         <EOL>rest -= (pl_w + blen)       <EOL>continue<EOL><DEDENT>if len(lbuff) > <NUM_LIT:0>:<EOL><INDENT>lbuff = lbuff.rstrip() + "<STR_LIT:\n>"   <EOL>text += lbuff                   <EOL>pos += lheight * progr          <EOL>just_tab.append(True)           <EOL>lbuff = "<STR_LIT>"                      <EOL><DEDENT>rest = maxwidth                 <EOL>if pl_w <= maxwidth:            <EOL><INDENT>lbuff = word + "<STR_LIT:U+0020>"          <EOL>rest = maxwidth - pl_w - blen    <EOL>continue<EOL><DEDENT>if len(just_tab) > <NUM_LIT:0>:<EOL><INDENT>just_tab[-<NUM_LIT:1>] = False            <EOL><DEDENT>for c in word:<EOL><INDENT>if pixlen(lbuff) <= maxwidth - pixlen(c):<EOL><INDENT>lbuff += c<EOL><DEDENT>else:                       <EOL><INDENT>lbuff += "<STR_LIT:\n>"           <EOL>text += lbuff           <EOL>pos += lheight * progr  <EOL>just_tab.append(False)  <EOL>lbuff = c               <EOL><DEDENT><DEDENT>lbuff += "<STR_LIT:U+0020>"                    <EOL>rest = maxwidth - pixlen(lbuff) <EOL><DEDENT>if lbuff != "<STR_LIT>":                     <EOL><INDENT>text += lbuff.rstrip()          <EOL>just_tab.append(False)          <EOL><DEDENT>if i < len(t0) - <NUM_LIT:1>:                 <EOL><INDENT>text += "<STR_LIT:\n>"                    <EOL>pos += lheight * progr          <EOL><DEDENT><DEDENT>more = (pos - maxpos) * progr           <EOL>if more > <NUM_LIT>:                         <EOL><INDENT>return (-<NUM_LIT:1>) * more                  <EOL><DEDENT>more = abs(more)<EOL>if more < <NUM_LIT>:<EOL><INDENT>more = <NUM_LIT:0>                            <EOL><DEDENT>nres = "<STR_LIT>" + cm                <EOL>templ = "<STR_LIT>"<EOL>spacing = <NUM_LIT:0><EOL>text_t = text.splitlines()              <EOL>for i, t in enumerate(text_t):<EOL><INDENT>pl = maxwidth - pixlen(t)           <EOL>pnt = point + c_pnt * (i * <NUM_LIT>)     <EOL>if align == <NUM_LIT:1>:                      <EOL><INDENT>if rot in (<NUM_LIT:0>, <NUM_LIT>):<EOL><INDENT>pnt = pnt + Point(pl / <NUM_LIT:2>, <NUM_LIT:0>) * progr<EOL><DEDENT>else:<EOL><INDENT>pnt = pnt - Point(<NUM_LIT:0>, pl / <NUM_LIT:2>) * progr<EOL><DEDENT><DEDENT>elif align == <NUM_LIT:2>:                    <EOL><INDENT>if rot in (<NUM_LIT:0>, <NUM_LIT>):<EOL><INDENT>pnt = pnt + Point(pl, <NUM_LIT:0>) * progr<EOL><DEDENT>else:<EOL><INDENT>pnt = pnt - Point(<NUM_LIT:0>, pl) * progr<EOL><DEDENT><DEDENT>elif align == <NUM_LIT:3>:                    <EOL><INDENT>spaces = t.count("<STR_LIT:U+0020>")           <EOL>if spaces > <NUM_LIT:0> and just_tab[i]:  <EOL><INDENT>spacing = pl / spaces       <EOL><DEDENT>else:<EOL><INDENT>spacing = <NUM_LIT:0>                 <EOL><DEDENT><DEDENT>top  = height - pnt.y - self.y<EOL>left = pnt.x + self.x<EOL>if rot == <NUM_LIT>:<EOL><INDENT>left = height - pnt.y - self.y<EOL>top  = -pnt.x - self.x<EOL><DEDENT>elif rot == <NUM_LIT>:<EOL><INDENT>left = -height + pnt.y + self.y<EOL>top  = pnt.x + self.x<EOL><DEDENT>elif rot == <NUM_LIT>:<EOL><INDENT>left = -pnt.x - self.x<EOL>top  = -height + pnt.y + self.y<EOL><DEDENT>nres += templ % (left, top, fname, fontsize)<EOL>if render_mode > <NUM_LIT:0>:<EOL><INDENT>nres += "<STR_LIT>" % render_mode<EOL><DEDENT>if spacing != <NUM_LIT:0>:<EOL><INDENT>nres += "<STR_LIT>" % spacing<EOL><DEDENT>if color is not None:<EOL><INDENT>nres += color_str<EOL><DEDENT>if fill is not None:<EOL><INDENT>nres += fill_str<EOL><DEDENT>if border_width != <NUM_LIT:1>:<EOL><INDENT>nres += "<STR_LIT>" % border_width<EOL><DEDENT>nres += "<STR_LIT>" % getTJstr(t, tj_glyphs, simple, ordering)<EOL><DEDENT>nres += "<STR_LIT>"<EOL>self.text_cont += nres<EOL>self.updateRect(rect)<EOL>return more<EOL>
Insert text into a given rectangle.

        Args:
            rect -- the textbox to fill
            buffer -- text to be inserted
            fontname -- a Base-14 font, font name or '/name'
            fontfile -- name of a font file
            fontsize -- font size
            color -- RGB stroke color triple
            fill -- RGB fill color triple
            render_mode -- text rendering control
            border_width -- thickness of glyph borders
            expandtabs -- handles tabulators with string function
            align -- left, center, right, justified
            rotate -- 0, 90, 180, or 270 degrees
            morph -- morph box with  a matrix and a pivotal point
        Returns:
            unused or deficit rectangle area (float)