assert(!(attr & TATTR_COMBINING));
void do_text(Context ctx, int x, int y, wchar_t *text, int len,	     unsigned long attr, int lattr){    Session *s = ctx;    int style;    struct do_text_args a;    RgnHandle textrgn, saveclip;#if TARGET_API_MAC_CARBON    RgnHandle visrgn;#endif    char mactextbuf[1024];    wchar_t *unitextptr;    int i, fontwidth;    ByteCount iread, olen;    OSStatus err;    static DeviceLoopDrawingUPP do_text_for_device_upp = NULL;    assert(len <= 1024);    SetPort((GrafPtr)GetWindowPort(s->window));    fontwidth = s->font_width;    if ((lattr & LATTR_MODE) != LATTR_NORM)	fontwidth *= 2;    /* First check this text is relevant */    a.textrect.top = y * s->font_height;    a.textrect.bottom = (y + 1) * s->font_height;    a.textrect.left = x * fontwidth;    a.textrect.right = (x + len) * fontwidth;    if (a.textrect.right > s->term->cols * s->font_width)	a.textrect.right = s->term->cols * s->font_width;#if TARGET_API_MAC_CARBON    visrgn = NewRgn();    GetPortVisibleRegion(GetWindowPort(s->window), visrgn);    if (!RectInRgn(&a.textrect, visrgn)) {	DisposeRgn(visrgn);	return;    }    DisposeRgn(visrgn);#else    if (!RectInRgn(&a.textrect, s->window->visRgn))	return;#endif    if (s->uni_to_font != NULL) {	err = ConvertFromUnicodeToText(s->uni_to_font, len * sizeof(UniChar),				       text, kUnicodeUseFallbacksMask,				       0, NULL, NULL, NULL,				       1024, &iread, &olen, mactextbuf);	if (err != noErr && err != kTECUsedFallbacksStatus)	    olen = 0;    } else  if (s->font_charset != CS_NONE) {	/* XXX this is bogus if wchar_t and UniChar are different sizes. */	unitextptr = (wchar_t *)text;	olen = charset_from_unicode(&unitextptr, &len, mactextbuf, 1024,				    s->font_charset, NULL, ".", 1);    } else	olen = 0;    a.s = s;    a.text = mactextbuf;    a.len = olen;    a.attr = attr;    a.lattr = lattr;    switch (lattr & LATTR_MODE) {      case LATTR_NORM:	TextSize(s->cfg.font.size);	a.numer = s->font_stdnumer;	a.denom = s->font_stddenom;	break;      case LATTR_WIDE:	TextSize(s->cfg.font.size);	a.numer = s->font_widenumer;	a.denom = s->font_widedenom;	break;      case LATTR_TOP:      case LATTR_BOT:	TextSize(s->cfg.font.size * 2);	a.numer = s->font_bignumer;	a.denom = s->font_bigdenom;	break;    }    SetPort((GrafPtr)GetWindowPort(s->window));    TextFont(s->fontnum);    style = s->cfg.font.face;    if ((attr & ATTR_BOLD) && !s->cfg.bold_colour)    	style |= bold;    if (attr & ATTR_UNDER)	style |= underline;    TextFace(style);    TextMode(srcOr);    if (HAVE_COLOR_QD())	if (style & bold) {	    SpaceExtra(s->font_boldadjust << 16);	    CharExtra(s->font_boldadjust << 16);	} else {	    SpaceExtra(0);	    CharExtra(0);	}    saveclip = NewRgn();    GetClip(saveclip);    ClipRect(&a.textrect);    textrgn = NewRgn();    RectRgn(textrgn, &a.textrect);    if (HAVE_COLOR_QD()) {	if (do_text_for_device_upp == NULL)	    do_text_for_device_upp =		NewDeviceLoopDrawingUPP(&do_text_for_device);	DeviceLoop(textrgn, do_text_for_device_upp, (long)&a, 0);    } else	do_text_for_device(1, 0, NULL, (long)&a);    SetClip(saveclip);    DisposeRgn(saveclip);    DisposeRgn(textrgn);    /* Tell the window manager about it in case this isn't an update */#if TARGET_API_MAC_CARBON    ValidWindowRect(s->window, &a.textrect);#else    ValidRect(&a.textrect);#endif}