// File: /workspace/github_cpp_makefile_star2_O1/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, %ebx
movl %esi, %r14d
movl %edi, %esi
cmpb $0x0, 0x20a073(%rip)    # 0x218868
jne 0xe85b
movl 0x20a012(%rip), %r8d    # 0x218810
movl %r8d, %r9d
imull %r14d, %r9d
movl 0x20a009(%rip), %edi    # 0x218814
imull %edi, %esi
movzbl 0x2fdf93(%rip), %eax    # 0x30c7a8
testl %eax, %eax
je 0xe823
cmpl $0x50, %eax
je 0xe823
cmpl $0x4b, %eax
jne 0xe837
movl 0x20a053(%rip), %ecx    # 0x21887c
movl %ecx, %eax
cltd
idivl %edi
leal (%rdx,%rdi,2), %eax
subl %eax, %ecx
cmpl %ecx, %esi
jge 0xe858
addl $0x2, %r14d
imull %r14d, %r8d
cmpb $0x0, 0x20a014(%rip)    # 0x21885a
cmovel %r8d, %r9d
xorl %eax, %eax
cmpb $0x0, 0x20a008(%rip)    # 0x21885b
cmovel %edi, %eax
addl %eax, %esi
movl %r9d, %r14d
cmpq $0x0, 0x209ffd(%rip)    # 0x218860
movl %esi, -0x30(%rbp)
je 0xe908
movq 0x209fed(%rip), %rax    # 0x218860
cmpb $0x0, (%rax)
je 0xe9a0
movb 0x20cba7(%rip), %cl     # 0x21b429
incq %rax
xorl %r11d, %r11d
movl $0x20, %edx
movl %r14d, %edi
xorl %r13d, %r13d
movl %r11d, %r10d
movq %rax, 0x209fc3(%rip)    # 0x218860
movb -0x1(%rax), %r11b
cmpb $0x20, %r11b
setne %r8b
cmpb $0x20, %r10b
sete %r9b
testb %r10b, %r10b
je 0xe8bb
andb %r8b, %r9b
je 0xe8d3
movslq %r13d, %r8
incl %r13d
movl %edi, -0x240(%rbp,%r8,4)
cmpl $0x10, %r8d
jg 0xe9a3
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 0xe893
jmp 0xe9a3
leaq -0x240(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x9340
movl -0x30(%rbp), %esi
movb (%r13), %al
testb %al, %al
je 0xea8d
movzbl %bl, %r15d
cmpb $0x5f, %al
jne 0xe93f
cmpb $0x0, 0x20caf1(%rip)    # 0x21b429
je 0xe93f
movb $0x20, (%r13)
cmpl $0x0, 0x214bfa(%rip)    # 0x223540
movzbl (%r13), %edx
je 0xe96e
movl %r14d, %edi
movl %r15d, %ecx
callq 0xdfcc
addl %eax, %r14d
movb 0x1(%r13), %al
incq %r13
testb %al, %al
movl -0x30(%rbp), %esi
jne 0xe92d
jmp 0xea8d
cmpl $0x1f, %edx
je 0xe98c
cmpl $0x1, %edx
jne 0xe94d
movl %esi, %edi
movl %r14d, %esi
movl %r15d, %edx
callq 0xe709
movl $0x6, %eax
jmp 0xe958
movl %esi, %edi
movl %r14d, %esi
movl %r15d, %edx
callq 0xe465
movl $0x8, %eax
jmp 0xe958
xorl %r13d, %r13d
movb (%r15), %al
testb %al, %al
je 0xea8d
movl -0x23c(%rbp), %ecx
addl $-0x8, %ecx
movl %ecx, -0x34(%rbp)
xorl %r12d, %r12d
movzbl %bl, %ecx
movl %ecx, -0x2c(%rbp)
xorl %ebx, %ebx
movl %ebx, %ecx
movl %eax, %ebx
cmpb $0x20, %al
setne %al
cmpb $0x20, %cl
sete %cl
andb %al, %cl
cmpl %r13d, %r12d
setl %al
andb %cl, %al
cmpb $0x1, %al
jne 0xea15
testl %r12d, %r12d
je 0xe9f7
movslq %r12d, %rax
incl %r12d
movl -0x240(%rbp,%rax,4), %r14d
jmp 0xea15
movl -0x34(%rbp), %r14d
movq %r15, %rax
movzbl (%rax), %ecx
testb $-0x21, %cl
je 0xea0f
incq %rax
addl $-0x8, %r14d
jmp 0xe9fe
movl $0x1, %r12d
cmpb $0x5f, %bl
movzbl %bl, %ecx
movl %ecx, %eax
movl $0x20, %edx
cmovel %edx, %eax
cmpb $0x0, 0x20c9fd(%rip)    # 0x21b429
cmovel %ecx, %eax
cmpl $0x0, 0x214b0a(%rip)    # 0x223540
movzbl %al, %edx
je 0xea5c
movl %r14d, %edi
movl -0x2c(%rbp), %ecx
callq 0xdfcc
incq %r15
addl %eax, %r14d
movb (%r15), %al
testb %al, %al
movl -0x30(%rbp), %esi
jne 0xe9c5
jmp 0xea8d
cmpb $0x1f, %al
je 0xea79
cmpl $0x1, %edx
jne 0xea3b
movl %esi, %edi
movl %r14d, %esi
movl -0x2c(%rbp), %edx
callq 0xe709
movl $0x6, %eax
jmp 0xea46
movl %esi, %edi
movl %r14d, %esi
movl -0x2c(%rbp), %edx
callq 0xe465
movl $0x8, %eax
jmp 0xea46
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq