// File: /workspace/github_c_makefile_star2_O2/OnionIoT[P]lv_example/lvgl/src/lv_objx/lv_label.c
// Function: lv_label_get_letter_on
uint16_t lv_label_get_letter_on(const lv_obj_t * label, lv_point_t * pos)
{
    LV_ASSERT_OBJ(label, LV_OBJX_NAME);
    LV_ASSERT_NULL(pos);

    const char * txt         = lv_label_get_text(label);
    lv_label_ext_t * ext     = lv_obj_get_ext_attr(label);
    uint32_t line_start      = 0;
    uint32_t new_line_start  = 0;
    lv_coord_t max_w         = lv_obj_get_width(label);
    const lv_style_t * style = lv_obj_get_style(label);
    const lv_font_t * font   = style->text.font;
    uint8_t letter_height    = lv_font_get_line_height(font);
    lv_coord_t y             = 0;
    lv_txt_flag_t flag       = LV_TXT_FLAG_NONE;
    uint16_t logical_pos;
    char *bidi_txt;

    if(ext->recolor != 0) flag |= LV_TXT_FLAG_RECOLOR;
    if(ext->expand != 0) flag |= LV_TXT_FLAG_EXPAND;

    lv_label_align_t align = lv_label_get_align(label);
    if(align == LV_LABEL_ALIGN_CENTER) flag |= LV_TXT_FLAG_CENTER;
    if(align == LV_LABEL_ALIGN_RIGHT) flag |= LV_TXT_FLAG_RIGHT;

    /*If the width will be expanded set the max length to very big */
    if(ext->long_mode == LV_LABEL_LONG_EXPAND) {
        max_w = LV_COORD_MAX;
    }

    /*Search the line of the index letter */;
    while(txt[line_start] != '\0') {
        new_line_start += lv_txt_get_next_line(&txt[line_start], font, style->text.letter_space, max_w, flag);

        if(pos->y <= y + letter_height) {
            /*The line is found (stored in 'line_start')*/
            /* Include the NULL terminator in the last line */
            uint32_t tmp = new_line_start;
            uint32_t letter;
            letter = lv_txt_encoded_prev(txt, &tmp);
            if(letter != '\n' && txt[new_line_start] == '\0' ) new_line_start++;
            break;
        }
        y += letter_height + style->text.line_space;

        line_start = new_line_start;
    }

#if LV_USE_BIDI
    bidi_txt = lv_draw_get_buf(new_line_start - line_start + 1);
    uint16_t txt_len = new_line_start - line_start;
    if(bidi_txt[new_line_start] == '\0') txt_len--;
    lv_bidi_process_paragraph(txt + line_start, bidi_txt, txt_len, lv_obj_get_base_dir(label), NULL, 0);
#else
    bidi_txt = (char*)txt + line_start;
#endif

    /*Calculate the x coordinate*/
    lv_coord_t x = 0;
    if(align == LV_LABEL_ALIGN_CENTER) {
        lv_coord_t line_w;
        line_w = lv_txt_get_width(bidi_txt, new_line_start - line_start, font, style->text.letter_space, flag);
        x += lv_obj_get_width(label) / 2 - line_w / 2;
    }
    else if(align == LV_LABEL_ALIGN_RIGHT) {
        lv_coord_t line_w;
        line_w = lv_txt_get_width(bidi_txt, new_line_start - line_start, font, style->text.letter_space, flag);
        x += lv_obj_get_width(label) - line_w;
    }

    lv_txt_cmd_state_t cmd_state = LV_TXT_CMD_STATE_WAIT;

    uint32_t i = 0;
    uint32_t i_act = i;
    uint32_t letter;
    uint32_t letter_next;

    if(new_line_start > 0) {
        while(i + line_start < new_line_start) {
            /* Get the current letter.*/
            letter = lv_txt_encoded_next(bidi_txt, &i);

            /*Get the next letter too for kerning*/
            letter_next = lv_txt_encoded_next(&bidi_txt[i], NULL);

            /*Handle the recolor command*/
            if((flag & LV_TXT_FLAG_RECOLOR) != 0) {
                if(lv_txt_is_cmd(&cmd_state, bidi_txt[i]) != false) {
                    continue; /*Skip the letter is it is part of a command*/
                }
            }

            x += lv_font_get_glyph_width(font, letter, letter_next);

            /*Finish if the x position or the last char of the line is reached*/
            if(pos->x < x || i + line_start == new_line_start) {
                i = i_act;
                break;
            }
            x += style->text.letter_space;
            i_act = i;
        }
    }

#if LV_USE_BIDI
    /*Handle Bidi*/
    bool is_rtl;
    logical_pos = lv_bidi_get_logical_pos(&txt[line_start], NULL, txt_len, lv_obj_get_base_dir(label), lv_txt_encoded_get_char_id(bidi_txt, i), &is_rtl);
    if (is_rtl) logical_pos++;
#else
    logical_pos = lv_txt_encoded_get_char_id(bidi_txt, i);
#endif

    return  logical_pos + lv_txt_encoded_get_char_id(txt, line_start);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rsi, %rbx
movq %rdi, %r13
callq 0x7994
testb %al, %al
je 0x19bcb
movq %rbx, %rdi
callq 0x7994
testb %al, %al
je 0x19bdc
movq %rbx, 0x38(%rsp)
movq %r13, %rdi
callq 0x18db9
movq %rax, %r14
movq %r13, %rdi
callq 0x6747
movq %rax, %rbx
movq %r13, %rdi
callq 0x5308
movl %eax, %ebp
movq %r13, %rdi
callq 0x6316
movq %rax, %r15
movq 0x28(%rax), %rax
movq %rax, 0x18(%rsp)
movzbl 0x10(%rax), %eax
movl %eax, 0xc(%rsp)
movb 0x18(%rbx), %al
movl %eax, %ecx
shrb $0x6, %cl
movl %ecx, %edx
andb $0x1, %dl
orb $0x2, %cl
testb %al, %al
movzbl %dl, %eax
movzbl %cl, %r12d
cmovnsl %eax, %r12d
movq %r13, 0x30(%rsp)
movq %r13, %rdi
callq 0x18d7f
movq %r15, %rcx
leal 0x4(%r12), %edx
cmpb $0x1, %al
movzbl %dl, %edx
movl %edx, 0x2c(%rsp)
cmovel %edx, %r12d
movl %r12d, %edx
orb $0x8, %dl
movb %al, 0x6(%rsp)
cmpb $0x2, %al
movzbl %dl, %eax
movl %eax, 0x28(%rsp)
cmovel %eax, %r12d
testb $0x7, 0x18(%rbx)
movl $0x7c18, %eax           # imm = 0x7C18
cmovnel %ebp, %eax
xorl %r15d, %r15d
cwtl
movl %eax, 0x10(%rsp)
movq %r12, 0x48(%rsp)
movzbl %r12b, %r12d
xorl %ebx, %ebx
movq %r14, 0x20(%rsp)
movl %ebx, %eax
leaq (%r14,%rax), %rbp
cmpb $0x0, (%r14,%rax)
je 0x19bed
movswl 0x30(%rcx), %edx
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
movq %rcx, %r13
movl 0x10(%rsp), %ecx
movl %r12d, %r8d
callq 0x28866
movq %rbx, %rcx
movzwl %ax, %r14d
addl %ecx, %r14d
movq 0x38(%rsp), %rax
movswl 0x2(%rax), %eax
movswl %r15w, %r15d
addl 0xc(%rsp), %r15d
cmpl %eax, %r15d
jge 0x19bf2
movzwl 0x32(%r13), %eax
addl %eax, %r15d
movl %r14d, %ebx
movq 0x20(%rsp), %r14
movq %r13, %rcx
jmp 0x19b6d
leaq 0x21572(%rip), %rdi     # 0x3b144
movq %r13, %rsi
callq 0x7b01
jmp 0x19bda
leaq 0x21561(%rip), %rdi     # 0x3b144
movq %rbx, %rsi
callq 0x7b01
jmp 0x19beb
movl %ebx, %r14d
jmp 0x19c1f
leaq 0x54(%rsp), %rsi
movl %r14d, (%rsi)
leaq 0x37a37(%rip), %rax     # 0x51638
movq 0x20(%rsp), %r15
movq %r15, %rdi
callq *(%rax)
cmpl $0xa, %eax
je 0x19c1c
movl %r14d, %eax
cmpb $0x1, (%r15,%rax)
adcl $0x0, %r14d
movq %r13, %rcx
movb 0x6(%rsp), %al
cmpb $0x1, %al
movq %rcx, 0x40(%rsp)
jne 0x19c73
movl %r14d, %eax
movq %rbx, %r13
subl %ebx, %eax
movswl 0x30(%rcx), %ecx
movzwl %ax, %esi
movq %rbp, %rdi
movq 0x18(%rsp), %rdx
movl 0x2c(%rsp), %r8d
callq 0x28bb3
movl %eax, %ebx
movq 0x30(%rsp), %rdi
callq 0x5308
movw $0x2, %cx
cwtd
idivw %cx
movl %eax, %ecx
movl %ebx, %eax
cwtd
movw $0xfffe, %si            # imm = 0xFFFE
idivw %si
addl %ecx, %eax
jmp 0x19cad
cmpb $0x2, %al
jne 0x19ca8
movl %r14d, %eax
movq %rbx, %r13
subl %ebx, %eax
movswl 0x30(%rcx), %ecx
movzwl %ax, %esi
movq %rbp, %rdi
movq 0x18(%rsp), %rdx
movl 0x28(%rsp), %r8d
callq 0x28bb3
movl %eax, %ebx
movq 0x30(%rsp), %rdi
callq 0x5308
subl %ebx, %eax
jmp 0x19cad
movq %rbx, %r13
xorl %eax, %eax
movb $0x0, 0x7(%rsp)
andl $0x0, 0x8(%rsp)
testl %r14d, %r14d
je 0x19d5e
movl %eax, 0xc(%rsp)
movq $0x0, 0x10(%rsp)
leaq 0x3795c(%rip), %r15     # 0x51630
movl 0x8(%rsp), %esi
leal (%rsi,%r13), %eax
cmpl %r14d, %eax
jae 0x19d60
movq %rbp, %rdi
leaq 0x8(%rsp), %rsi
callq *(%r15)
movl %eax, %ebx
movl 0x8(%rsp), %edi
addq %rbp, %rdi
xorl %esi, %esi
callq *(%r15)
movl %eax, %r12d
testb $0x1, 0x48(%rsp)
je 0x19d1b
movl 0x8(%rsp), %eax
movsbl (%rbp,%rax), %esi
leaq 0x7(%rsp), %rdi
callq 0x28c9d
testb %al, %al
jne 0x19cd4
movq 0x18(%rsp), %rdi
movl %ebx, %esi
movl %r12d, %edx
callq 0x2603b
movl 0xc(%rsp), %edx
addl %eax, %edx
movq 0x38(%rsp), %rax
cmpw %dx, (%rax)
jl 0x19d8b
movl 0x8(%rsp), %eax
leal (%rax,%r13), %ecx
cmpl %r14d, %ecx
je 0x19d8b
movq 0x40(%rsp), %rcx
addw 0x30(%rcx), %dx
movl %edx, 0xc(%rsp)
movq %rax, 0x10(%rsp)
jmp 0x19cd4
xorl %esi, %esi
leaq 0x378e1(%rip), %r14     # 0x51648
movq %rbp, %rdi
callq *(%r14)
movl %eax, %ebx
movq 0x20(%rsp), %rdi
movl %r13d, %esi
callq *(%r14)
addl %ebx, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x10(%rsp), %rsi
movl %esi, 0x8(%rsp)
jmp 0x19d60