<commit_before>// VisualDocument.cpp

#include "VisualDocument.h"
#include "TextDocumentReader.h"
#include "TextStyleReader.h"
#include "TextDocument.h"
#include "TextStyleRegistry.h"
#include "TextSelection.h"
#include "VisualPainter.h"
#include "TextLayoutArgs.h"
#include "UniscribeLayout.h"
#include "SimpleLayout.h"
#include "EmptyTextBlock.h"
#include "Assert.h"

#undef min
#undef max

VisualDocument::VisualDocument( const TextDocument& doc, TextStyleRegistry& styleRegistry )
	: m_doc( doc )
	, m_styleRegistry( styleRegistry )
	, m_lineCount( 1 )
{
	m_blocks.push_back( TextBlockPtr( new EmptyTextBlock( false, m_styleRegistry ) ) );
}

void VisualDocument::Draw( HDC hdc, RECT rect, TextSelection selection ) const
{
	VisualPainter painter( hdc, m_doc, m_styleRegistry, selection );

	BlockContaining_Result block = BlockContaining( rect.top );

	OffsetRect( &rect, 0, -block.yStart );

	DrawBackground( block, painter, rect );
	DrawText      ( block, painter, rect );
}

void VisualDocument::DrawBackground( BlockContaining_Result block, VisualPainter& painter, RECT rect ) const
{
	while ( block.it != m_blocks.end() && !IsRectEmpty( &rect ) )
	{
		painter.SetOrigin( block.textStart, block.yStart );

		block->DrawBackground( painter, rect );

		rect.bottom -= block->Height() - rect.top;
		rect.top = 0;

		block.yStart += block->Height();
		block.textStart += block->Length();
		++block.it;
	}
}

void VisualDocument::DrawText( BlockContaining_Result block, VisualPainter& painter, RECT rect ) const
{
	while ( block.it != m_blocks.end() && !IsRectEmpty( &rect ) )
	{
		painter.SetOrigin( block.textStart, block.yStart );

		block->DrawText( painter, rect );

		rect.bottom -= block->Height() - rect.top;
		rect.top = 0;

		block.yStart += block->Height();
		block.textStart += block->Length();
		++block.it;
	}
}

void VisualDocument::Update( HDC hdc, int maxWidth, TextChange change )
{
	BlockContaining_Result block = BlockContaining( change.start );

	size_t start = block.textStart;
	size_t count = 0;
	size_t modifiedCount = change.end + block.textStart;

	do
	{
		m_lineCount -= block->LineCount();
		count += block->Length();
		block.it = m_blocks.erase( block.it );
	}
	while ( count <= modifiedCount && block.it != m_blocks.end() );

	LayoutText( block.it, start, count + change.delta, hdc, maxWidth );
}

bool VisualDocument::IsSimpleText( UTF16Ref text ) const
{
	for ( const UTF16::Unit* it = text.begin(); it != text.end(); ++it )
		if ( *it >= 128 )
			return ScriptIsComplex( text.begin(), text.size(), SIC_COMPLEX ) != S_OK;

	return true;
}

void VisualDocument::LayoutText( TextBlockList::const_iterator it, size_t start, size_t count, HDC hdc, int maxWidth )
{
	if ( maxWidth != 0 )
		maxWidth = std::max( maxWidth, m_styleRegistry.avgCharWidth * 10 );<commit_msg>Don't update too much text.<commit_after>// VisualDocument.cpp

#include "VisualDocument.h"
#include "TextDocumentReader.h"
#include "TextStyleReader.h"
#include "TextDocument.h"
#include "TextStyleRegistry.h"
#include "TextSelection.h"
#include "VisualPainter.h"
#include "TextLayoutArgs.h"
#include "UniscribeLayout.h"
#include "SimpleLayout.h"
#include "EmptyTextBlock.h"
#include "Assert.h"

#undef min
#undef max

VisualDocument::VisualDocument( const TextDocument& doc, TextStyleRegistry& styleRegistry )
	: m_doc( doc )
	, m_styleRegistry( styleRegistry )
	, m_lineCount( 1 )
{
	m_blocks.push_back( TextBlockPtr( new EmptyTextBlock( false, m_styleRegistry ) ) );
}

void VisualDocument::Draw( HDC hdc, RECT rect, TextSelection selection ) const
{
	VisualPainter painter( hdc, m_doc, m_styleRegistry, selection );

	BlockContaining_Result block = BlockContaining( rect.top );

	OffsetRect( &rect, 0, -block.yStart );

	DrawBackground( block, painter, rect );
	DrawText      ( block, painter, rect );
}

void VisualDocument::DrawBackground( BlockContaining_Result block, VisualPainter& painter, RECT rect ) const
{
	while ( block.it != m_blocks.end() && !IsRectEmpty( &rect ) )
	{
		painter.SetOrigin( block.textStart, block.yStart );

		block->DrawBackground( painter, rect );

		rect.bottom -= block->Height() - rect.top;
		rect.top = 0;

		block.yStart += block->Height();
		block.textStart += block->Length();
		++block.it;
	}
}

void VisualDocument::DrawText( BlockContaining_Result block, VisualPainter& painter, RECT rect ) const
{
	while ( block.it != m_blocks.end() && !IsRectEmpty( &rect ) )
	{
		painter.SetOrigin( block.textStart, block.yStart );

		block->DrawText( painter, rect );

		rect.bottom -= block->Height() - rect.top;
		rect.top = 0;

		block.yStart += block->Height();
		block.textStart += block->Length();
		++block.it;
	}
}

void VisualDocument::Update( HDC hdc, int maxWidth, TextChange change )
{
	BlockContaining_Result block = BlockContaining( change.start );

	size_t start = block.textStart;
	size_t count = 0;
	size_t modifiedCount = change.end - block.textStart;

	do
	{
		m_lineCount -= block->LineCount();
		count += block->Length();
		block.it = m_blocks.erase( block.it );
	}
	while ( count <= modifiedCount && block.it != m_blocks.end() );

	LayoutText( block.it, start, count + change.delta, hdc, maxWidth );
}

bool VisualDocument::IsSimpleText( UTF16Ref text ) const
{
	for ( const UTF16::Unit* it = text.begin(); it != text.end(); ++it )
		if ( *it >= 128 )
			return ScriptIsComplex( text.begin(), text.size(), SIC_COMPLEX ) != S_OK;

	return true;
}

void VisualDocument::LayoutText( TextBlockList::const_iterator it, size_t start, size_t count, HDC hdc, int maxWidth )
{
	if ( maxWidth != 0 )
		maxWidth = std::max( maxWidth, m_styleRegistry.avgCharWidth * 10 );
{'fraction_non_alphanumeric': 0.14214463840399003, 'fraction_numerical': 0.005343783398646241, 'mean_word_length': 4.700507614213198, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7346345', 'n_tokens_mistral': 1985, 'n_tokens_neox': 1873, 'n_words': 454}