// File: jkingan[P]ladyheather/heather.cpp
// Function: vidstr(int, int, unsigned char, char*)
void vidstr(COORD row, COORD col, u08 attr, char *s)
{
char *p;
S32 px, py;
char work_str[MAX_TEXT_COLS+1];
char *chp;
char ch;
S32 word_x[UNIT_LEN+1];
S32 n_words;
S32 ux;
char last_uch;
char last_ch;
char uch;
S32 w;
S32 x;

   // this routine recognizes some global variables:
   //   blank_underscore: if set, converts underscore char to blanks
   //   graphics_coords:  if set, row and col are in pixels coordinates
   //                     if clear, they are in character coordinates
   //   no_x_margin:      if set, do not apply the "x" character margin offset
   //   no_y_margin:      if set, do not apply the "y" character margin offset
   //   print_using:      if not null, points to column alignment string
   //                     (note: Heather does not use this functionality, it can be deleted)

   if(graphics_coords) {  // row/col in pixels
      px = col;
      py = row;
   }
   else { // row/col in text positions
      px = col * TEXT_WIDTH;
      py = row * TEXT_HEIGHT;

      if((zoom_screen && (zoom_screen != 'P') && (zoom_screen != 'K')) || (py < (((PLOT_ROW/TEXT_HEIGHT)*TEXT_HEIGHT) - (TEXT_HEIGHT*2)))) {
         if(no_x_margin == 0) px += TEXT_X_MARGIN;   // looks better with margins
         if(no_y_margin == 0) py += TEXT_Y_MARGIN;
      }
   }


   if(print_using == NULL) {  // we are using a monospace font and not aligning columns
      strcpy(work_str, s);

      chp = work_str;
      while(*chp) {
         if(blank_underscore) {
            if(*chp == '_') *chp = ' ';
         }

         if(use_vc_fonts) {
            px += dot_char(px,py, *chp, attr);
         }
         else if(*chp == DEGREES) {
            px += deg_char(py,px, attr);
         }
         else if(*chp == PLUS_MINUS) {
            px += plusminus_char(py,px, attr);
         }
         else {  // dot-by-dot character rendering
            px += dot_char(px,py, *chp, attr);
         }
         ++chp;
      }

      return;
   }

   //
   // At each space->nonspace transition in *s, position cursor at equivalent
   // space->nonspace transition in *print_using
   //
   // This allows table-cell alignment with VFX's proportional font
   //
   // New kludge: the first column is right justified one space before the
   //             second column.
   //

   n_words = 0;
   ux = px;
   last_uch = '\0';

   while(*print_using) {
      uch = *print_using++;

      if(((uch != ' ') && (last_uch == ' ')) || (last_uch == '\0')) {
         word_x[n_words++] = ux;
         if(n_words >= UNIT_LEN) break;
      }

      last_uch = uch;
      if(blank_underscore) {
         if(uch == '_') uch = ' ';  // _ prints as a blank,  but does not cause a space transition
      }

      if(uch == DEGREES) ux += DEG_WIDTH;  //!!! kludge:  hardcoded char width
      else if(uch == PLUS_MINUS) ux += 8;  //!!! kludge:  hardcoded char width
      else ux += 8; // !!!!!! VFX_character_width(vfx_font, uch);
   }

   w = 0;
   x = px;
   last_ch = '\0';

   while(*s) {
      ch = *s++;

      if((ch != ' ') && (last_ch == ' ') && (w < n_words)) {
         x = word_x[w++];
         if(w == 1) {  // right justify the first column
            x = word_x[w];   // x = where the second column starts
            x -= 8; // !!!! VFX_character_width(vfx_font, ' ');  // back up a space
            p = s-1;
            while((*p != ' ') && (*p != '\0')) {  // back up the width of the text
               x -= 8; // !!!!VFX_character_width(vfx_font, *p);
               ++p;
            }
         }
      }

      last_ch = ch;
      if(blank_underscore) {
         if(ch == '_') ch = ' ';
      }

      if(use_vc_fonts) {
         x += dot_char(x,py, ch, attr);
      }
      else if(ch == DEGREES) {
         x += deg_char(py,x, attr);
      }
      else if(ch == PLUS_MINUS) {
         x += plusminus_char(py,x, attr);
      }
      else if(1) { // !!!!! testing dot-by-dot character rendering
         x += dot_char(x,py, ch, attr);
      }
   }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2b0, %rsp            # imm = 0x2B0
movb %dl, %al
movl %edi, -0x4(%rbp)
movl %esi, -0x8(%rbp)
movb %al, -0x9(%rbp)
movq %rcx, -0x18(%rbp)
cmpb $0x0, 0x266b47(%rip)    # 0x277a48
je 0x10f14
movl -0x8(%rbp), %eax
movl %eax, -0x24(%rbp)
movl -0x4(%rbp), %eax
movl %eax, -0x28(%rbp)
jmp 0x10fbb
movl -0x8(%rbp), %eax
imull 0x266ad2(%rip), %eax    # 0x2779f0
movl %eax, -0x24(%rbp)
movl -0x4(%rbp), %eax
imull 0x266ac9(%rip), %eax    # 0x2779f4
movl %eax, -0x28(%rbp)
cmpb $0x0, 0x35aa53(%rip)    # 0x36b988
je 0x10f4f
movzbl 0x35aa4a(%rip), %eax    # 0x36b988
cmpl $0x50, %eax
je 0x10f4f
movzbl 0x35aa3e(%rip), %eax    # 0x36b988
cmpl $0x4b, %eax
jne 0x10f82
movl -0x28(%rbp), %eax
movl %eax, -0x2a8(%rbp)
movl 0x266afe(%rip), %eax    # 0x277a5c
cltd
idivl 0x266a8f(%rip)          # 0x2779f4
movl %eax, %ecx
movl -0x2a8(%rbp), %eax
imull 0x266a80(%rip), %ecx    # 0x2779f4
movl 0x266a7a(%rip), %edx    # 0x2779f4
shll %edx
subl %edx, %ecx
cmpl %ecx, %eax
jge 0x10fb9
movzbl 0x266ab1(%rip), %eax    # 0x277a3a
cmpl $0x0, %eax
jne 0x10f9c
movl 0x266a5c(%rip), %eax    # 0x2779f0
shll %eax
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
movzbl 0x266a98(%rip), %eax    # 0x277a3b
cmpl $0x0, %eax
jne 0x10fb7
movl 0x266a46(%rip), %eax    # 0x2779f4
shll $0x0, %eax
addl -0x28(%rbp), %eax
movl %eax, -0x28(%rbp)
jmp 0x10fb9
jmp 0x10fbb
cmpq $0x0, 0x266a7d(%rip)    # 0x277a40
jne 0x110d4
leaq -0x230(%rbp), %rdi
movq -0x18(%rbp), %rsi
callq 0x9350
leaq -0x230(%rbp), %rax
movq %rax, -0x238(%rbp)
movq -0x238(%rbp), %rax
cmpb $0x0, (%rax)
je 0x110cf
cmpb $0x0, 0x26960b(%rip)    # 0x27a609
je 0x1101b
movq -0x238(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x5f, %eax
jne 0x11019
movq -0x238(%rbp), %rax
movb $0x20, (%rax)
jmp 0x1101b
cmpl $0x0, 0x2716fe(%rip)    # 0x282720
je 0x11047
movl -0x24(%rbp), %edi
movl -0x28(%rbp), %esi
movq -0x238(%rbp), %rax
movb (%rax), %al
movzbl %al, %edx
movzbl -0x9(%rbp), %ecx
callq 0x10450
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
jmp 0x110b8
movq -0x238(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x1, %eax
jne 0x1106d
movl -0x28(%rbp), %edi
movl -0x24(%rbp), %esi
movzbl -0x9(%rbp), %edx
callq 0x10e00
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
jmp 0x110b6
movq -0x238(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x1f, %eax
jne 0x11093
movl -0x28(%rbp), %edi
movl -0x24(%rbp), %esi
movzbl -0x9(%rbp), %edx
callq 0x10a90
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
jmp 0x110b4
movl -0x24(%rbp), %edi
movl -0x28(%rbp), %esi
movq -0x238(%rbp), %rax
movb (%rax), %al
movzbl %al, %edx
movzbl -0x9(%rbp), %ecx
callq 0x10450
addl -0x24(%rbp), %eax
movl %eax, -0x24(%rbp)
jmp 0x110b6
jmp 0x110b8
movq -0x238(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x238(%rbp)
jmp 0x10fe7
jmp 0x113ed
movl $0x0, -0x294(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x298(%rbp)
movb $0x0, -0x299(%rbp)
movq 0x26694b(%rip), %rax    # 0x277a40
cmpb $0x0, (%rax)
je 0x111e6
movq 0x26693b(%rip), %rax    # 0x277a40
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, 0x26692d(%rip)    # 0x277a40
movb (%rax), %al
movb %al, -0x29b(%rbp)
movsbl -0x29b(%rbp), %eax
cmpl $0x20, %eax
je 0x11133
movsbl -0x299(%rbp), %eax
cmpl $0x20, %eax
je 0x1113f
movsbl -0x299(%rbp), %eax
cmpl $0x0, %eax
jne 0x1116c
movl -0x298(%rbp), %ecx
movl -0x294(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x294(%rbp)
cltq
movl %ecx, -0x290(%rbp,%rax,4)
cmpl $0x12, -0x294(%rbp)
jl 0x1116a
jmp 0x111e6
jmp 0x1116c
movb -0x29b(%rbp), %al
movb %al, -0x299(%rbp)
cmpb $0x0, 0x26948a(%rip)    # 0x27a609
je 0x11196
movsbl -0x29b(%rbp), %eax
cmpl $0x5f, %eax
jne 0x11194
movb $0x20, -0x29b(%rbp)
jmp 0x11196
movsbl -0x29b(%rbp), %eax
cmpl $0x1, %eax
jne 0x111b3
movl -0x298(%rbp), %eax
addl $0x6, %eax
movl %eax, -0x298(%rbp)
jmp 0x111e1
movsbl -0x29b(%rbp), %eax
cmpl $0x1f, %eax
jne 0x111d0
movl -0x298(%rbp), %eax
addl $0x8, %eax
movl %eax, -0x298(%rbp)
jmp 0x111df
movl -0x298(%rbp), %eax
addl $0x8, %eax
movl %eax, -0x298(%rbp)
jmp 0x111e1
jmp 0x110ee
movl $0x0, -0x2a0(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0x2a4(%rbp)
movb $0x0, -0x29a(%rbp)
movq -0x18(%rbp), %rax
cmpb $0x0, (%rax)
je 0x113ed
movq -0x18(%rbp), %rax
movq %rax, %rcx
addq $0x1, %rcx
movq %rcx, -0x18(%rbp)
movb (%rax), %al
movb %al, -0x239(%rbp)
movsbl -0x239(%rbp), %eax
cmpl $0x20, %eax
je 0x11306
movsbl -0x29a(%rbp), %eax
cmpl $0x20, %eax
jne 0x11306
movl -0x2a0(%rbp), %eax
cmpl -0x294(%rbp), %eax
jge 0x11306
movl -0x2a0(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0x2a0(%rbp)
cltq
movl -0x290(%rbp,%rax,4), %eax
movl %eax, -0x2a4(%rbp)
cmpl $0x1, -0x2a0(%rbp)
jne 0x11304
movslq -0x2a0(%rbp), %rax
movl -0x290(%rbp,%rax,4), %eax
movl %eax, -0x2a4(%rbp)
movl -0x2a4(%rbp), %eax
subl $0x8, %eax
movl %eax, -0x2a4(%rbp)
movq -0x18(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movsbl (%rax), %ecx
xorl %eax, %eax
cmpl $0x20, %ecx
movb %al, -0x2a9(%rbp)
je 0x112d9
movq -0x20(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x2a9(%rbp)
movb -0x2a9(%rbp), %al
testb $0x1, %al
jne 0x112e5
jmp 0x11302
movl -0x2a4(%rbp), %eax
subl $0x8, %eax
movl %eax, -0x2a4(%rbp)
movq -0x20(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x20(%rbp)
jmp 0x112b2
jmp 0x11304
jmp 0x11306
movb -0x239(%rbp), %al
movb %al, -0x29a(%rbp)
cmpb $0x0, 0x2692f0(%rip)    # 0x27a609
je 0x11330
movsbl -0x239(%rbp), %eax
cmpl $0x5f, %eax
jne 0x1132e
movb $0x20, -0x239(%rbp)
jmp 0x11330
cmpl $0x0, 0x2713e9(%rip)    # 0x282720
je 0x11365
movl -0x2a4(%rbp), %edi
movl -0x28(%rbp), %esi
movb -0x239(%rbp), %al
movzbl %al, %edx
movzbl -0x9(%rbp), %ecx
callq 0x10450
addl -0x2a4(%rbp), %eax
movl %eax, -0x2a4(%rbp)
jmp 0x113e8
movsbl -0x239(%rbp), %eax
cmpl $0x1, %eax
jne 0x11391
movl -0x28(%rbp), %edi
movl -0x2a4(%rbp), %esi
movzbl -0x9(%rbp), %edx
callq 0x10e00
addl -0x2a4(%rbp), %eax
movl %eax, -0x2a4(%rbp)
jmp 0x113e6
movsbl -0x239(%rbp), %eax
cmpl $0x1f, %eax
jne 0x113bd
movl -0x28(%rbp), %edi
movl -0x2a4(%rbp), %esi
movzbl -0x9(%rbp), %edx
callq 0x10a90
addl -0x2a4(%rbp), %eax
movl %eax, -0x2a4(%rbp)
jmp 0x113e4
movl -0x2a4(%rbp), %edi
movl -0x28(%rbp), %esi
movb -0x239(%rbp), %al
movzbl %al, %edx
movzbl -0x9(%rbp), %ecx
callq 0x10450
addl -0x2a4(%rbp), %eax
movl %eax, -0x2a4(%rbp)
jmp 0x113e6
jmp 0x113e8
jmp 0x11200
addq $0x2b0, %rsp            # imm = 0x2B0
popq %rbp
retq
nopw %cs:(%rax,%rax)