// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movq %rcx, %r15
movl %edx, %r12d
movl %esi, %r14d
movl %edi, %esi
cmpb $0x0, 0x20a0bb(%rip)    # 0x218868
jne 0xe813
movl 0x20a05a(%rip), %r8d    # 0x218810
movl %r8d, %r9d
imull %r14d, %r9d
movl 0x20a051(%rip), %edi    # 0x218814
imull %edi, %esi
movzbl 0x2fdfdb(%rip), %eax    # 0x30c7a8
testl %eax, %eax
je 0xe7db
cmpl $0x50, %eax
je 0xe7db
cmpl $0x4b, %eax
jne 0xe7ef
movl 0x20a09b(%rip), %ecx    # 0x21887c
movl %ecx, %eax
cltd
idivl %edi
leal (%rdx,%rdi,2), %eax
subl %eax, %ecx
cmpl %ecx, %esi
jge 0xe810
addl $0x2, %r14d
imull %r14d, %r8d
cmpb $0x0, 0x20a05c(%rip)    # 0x21885a
cmovel %r8d, %r9d
xorl %eax, %eax
cmpb $0x0, 0x20a050(%rip)    # 0x21885b
cmovel %edi, %eax
addl %eax, %esi
movl %r9d, %r14d
movq 0x20a046(%rip), %rax    # 0x218860
testq %rax, %rax
movl %esi, -0x34(%rbp)
je 0xe8c7
cmpb $0x0, (%rax)
je 0xe962
movb 0x20cbf4(%rip), %cl     # 0x21b429
incq %rax
xorl %r11d, %r11d
movl $0x20, %edx
movl %r14d, %edi
movl $0x0, -0x30(%rbp)
movl %r11d, %r10d
movq %rax, 0x20a00c(%rip)    # 0x218860
movb -0x1(%rax), %r11b
cmpb $0x20, %r11b
setne %r8b
cmpb $0x20, %r10b
sete %r9b
testb %r10b, %r10b
je 0xe872
andb %r8b, %r9b
je 0xe892
movl -0x30(%rbp), %r9d
movslq %r9d, %r8
incl %r9d
movl %r9d, -0x30(%rbp)
movl %edi, -0x240(%rbp,%r8,4)
cmpl $0x10, %r8d
jg 0xe969
cmpb $0x5f, %r11b
movzbl %r11b, %r8d
movl %r8d, %r9d
cmovel %edx, %r9d
testb %cl, %cl
cmovel %r8d, %r9d
xorl %r8d, %r8d
cmpb $0x1, %r9b
setne %r8b
leal (%rdi,%r8,2), %edi
addl $0x6, %edi
cmpb $0x0, (%rax)
leaq 0x1(%rax), %rax
jne 0xe84a
jmp 0xe969
leaq -0x240(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x9340
movl -0x34(%rbp), %esi
movb (%r13), %al
testb %al, %al
je 0xea63
leaq -0x23f(%rbp), %rbx
movzbl %r12b, %r15d
cmpb $0x5f, %al
jne 0xe906
cmpb $0x0, 0x20cb2b(%rip)    # 0x21b429
je 0xe906
movb $0x20, -0x1(%rbx)
movb $0x20, %al
cmpl $0x0, 0x214c33(%rip)    # 0x223540
movzbl %al, %edx
je 0xe931
movl %r14d, %edi
movl %r15d, %ecx
callq 0xdfa5
addl %eax, %r14d
movb (%rbx), %al
incq %rbx
testb %al, %al
movl -0x34(%rbp), %esi
jne 0xe8f3
jmp 0xea63
cmpb $0x1f, %al
je 0xe94e
cmpl $0x1, %edx
jne 0xe912
movl %esi, %edi
movl %r14d, %esi
movl %r15d, %edx
callq 0xe6c0
movl $0x6, %eax
jmp 0xe91d
movl %esi, %edi
movl %r14d, %esi
movl %r15d, %edx
callq 0xe41c
movl $0x8, %eax
jmp 0xe91d
movl $0x0, -0x30(%rbp)
movb (%r15), %al
testb %al, %al
je 0xea63
movl -0x23c(%rbp), %ecx
leaq 0x1(%r15), %rbx
addl $-0x8, %ecx
movl %ecx, -0x38(%rbp)
xorl %r13d, %r13d
movzbl %r12b, %ecx
movl %ecx, -0x2c(%rbp)
xorl %r12d, %r12d
movl %r12d, %ecx
movl %eax, %r12d
cmpb $0x20, %al
setne %al
cmpb $0x20, %cl
sete %cl
andb %al, %cl
cmpl -0x30(%rbp), %r13d
setl %al
andb %cl, %al
cmpb $0x1, %al
jne 0xe9e6
testl %r13d, %r13d
je 0xe9c6
movslq %r13d, %rax
incl %r13d
movl -0x240(%rbp,%rax,4), %r14d
jmp 0xe9e6
movl -0x38(%rbp), %r14d
movq %rbx, %rax
movl %r12d, %ecx
testb $-0x21, %cl
je 0xe9e0
movb (%rax), %cl
incq %rax
addl $-0x8, %r14d
jmp 0xe9d0
movl $0x1, %r13d
cmpb $0x5f, %r12b
movzbl %r12b, %ecx
movl %ecx, %eax
movl $0x20, %edx
cmovel %edx, %eax
cmpb $0x0, 0x20ca2a(%rip)    # 0x21b429
cmovel %ecx, %eax
cmpl $0x0, 0x214b37(%rip)    # 0x223540
movzbl %al, %edx
je 0xea32
movl %r14d, %edi
movl -0x2c(%rbp), %ecx
callq 0xdfa5
incq %r15
addl %eax, %r14d
movb (%r15), %al
incq %rbx
testb %al, %al
movl -0x34(%rbp), %esi
jne 0xe991
jmp 0xea63
cmpb $0x1f, %al
je 0xea4f
cmpl $0x1, %edx
jne 0xea0e
movl %esi, %edi
movl %r14d, %esi
movl -0x2c(%rbp), %edx
callq 0xe6c0
movl $0x6, %eax
jmp 0xea19
movl %esi, %edi
movl %r14d, %esi
movl -0x2c(%rbp), %edx
callq 0xe41c
movl $0x8, %eax
jmp 0xea19
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq