0

I followed Custom edit control win32 second answer to create custom edit control, but the problem is that, I render every letter separate, so I can have different text colors. And for every render call, I have to calculate text offset to start rendering from top of the screen (0,0), so i don't have to render whole text. And if I have a 200kB file and scroll to bottom of the file (and do some editing there), there is just too much lag, since I have to go trough all that text and find all '\n' (indicates line number) for offset.

Render function:

int Screen_X = 0, Screen_Y = 0;
size_t Text_YOffset = Calc_TextYPos(m_Screen_YOff);  //Screen pos(0,0) to text
size_t Text_Size = m_Text.size();
COLORREF Text_ColorOld = 0;
for (size_t pos = Text_YOffset; pos < Text_Size; pos++) {
    if (m_Text.at(pos) == L'\n') {
        Screen_Y++; Screen_X = 0;
        continue;
    }

    if (Screen_X < m_Screen_XOff) { Screen_X++; continue; }
    if (m_Screen_MaxX < Screen_X) continue;
    if (m_Screen_MaxY < Screen_Y) break;

    if (m_Text_Color.at(pos) != Text_ColorOld) {
        Text_ColorOld = m_Text_Color.at(pos);
        if (SetTextColor(hDC, Text_ColorOld) == CLR_INVALID) {
            MB_ERR("'SetTextColor' Failed!");
            PostQuitMessage(-1);
       }
   }

    CHECK_ERR(TextOut(hDC, (Screen_X - m_Screen_XOff) * m_Char_Width, Screen_Y * m_Char_Height, &m_Text.at(pos), 1), ERR_MSG_TEXT_OUT);
    Screen_X++;
}

Calc_TextYPos:

size_t Edit_Control::Calc_TextYPos(int Screen_y) {
    if (Screen_y == 0) return 0;
    size_t Offset = 0;

    size_t Text_Size = m_Text.size();
    for (size_t pos = 0; pos < Text_Size; pos++) {
        if (m_Text.at(pos) == L'\n' && Screen_y != 0) {
            Screen_y--;
            Offset = pos + 1;
        }
        if (Screen_y == 0) return Offset;
    }

    return Offset;
}

Am I taking the wrong path here and should use "different algorithm" for rendering text (in different colors), or if not, how can I optimize this code? I like this approach (for caret) since, selecting text is really easy.

I also came across What is the fastest way to draw formatted text in the Win32 API? but, it doesn't answer my question. It only mentions about rendering function (ExtTextOut), but I don't need that. I need a fast way of calculating line offset(s) on big strings.

  • 1
    It's fine to use TextOut for rendering text. The slowness is not coming from TextOut. It's coming from your algorithm for calculating the coordinates. In particular, the `Calc_TextYPos` function reads through the entire string looking for `\n` characters, and for a 200KB string, that will take a long time. You need to make your own code more efficient. TextOut is not where your performance bottleneck is. – Raymond Chen Jan 10 '20 at 20:35
  • @RaymondChen I know, but I have no clue how should `Calc_TextYPos` look like. :( – Ivan Ambic Jan 10 '20 at 20:58
  • You should rephrase the question, since the way it's written, it's asking for faster rendering, but your problem is that you're spending too much time thinking and not rendering. Designing a high-performance edit control is a complicated topic, perhaps too broad for this site. You can start by precalculating line offsets for faster lookups. – Raymond Chen Jan 11 '20 at 00:11

0 Answers0