# RGB format a text document using a highlight colour. # RGB 0-255 like HTML 0x00-0xFF # Output should be valid RTF but YMMV sub RTFhighlight { my ($text, $highlight, $r, $g, $b, $font) = @_; $font ||= "Arial"; $text =~ s/([\\{}])/\\$1/g; # escape \ and { } $text =~ s/\n/\\par\n/g; # convert \n to \par $text =~ s/\b\Q$highlight\E\b/\\cf1 $highlight\\cf0 /g; my $header = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fswiss\\fcharset0 $font;}}\n"; $header .= "{\\colortbl ;\\red$r\\green$g\\blue$b;}\n"; return $header.$text; } print RTFhighlight("hello be seabe be because", "be", 0, 0, 0xFF); #### use Win32::GUI; use warnings; use strict; my $Win = new Win32::GUI::Window( -left => 3, -top => 5, -width => 640, -height => 480, -name => "Win", ); my $font = Win32::GUI::Font->new( -name => "Courier New", -size => 12, ); $Win->AddButton( -text => "Run", -name => "Button1", -left => 23, -top => 10, -width => 56, -height => 36, -foreground => 0, ); my $RichText = $Win->AddRichEdit( -text => "Click Run.....\n", -name => "RichText_Output", # OutPut Rich Text -left => 41, -top => 91, -width => 580, -height => 313, -multiline => 1, -vscroll => 1, ); $RichText->SetLimitText(4194304); $Win->Show(); Win32::GUI::Dialog(); sub Win_Terminate { return -1; } sub Button1_Click { my $file = "bigfile.txt"; my $PATTERN = "be"; $RichText->Select(0,-1); $RichText->ReplaceSel("Loading and highlighting.....\n"); $Win->Update; open FILE, $file or die "Can't open $file because $!\n"; local $/; my $text = ; close FILE; $text = RTFhighlight($text, $PATTERN, 0xFF, 0, 0, "Courier New" ); $RichText->Select(0,-1); $RichText->ReplaceSel($text); } # rft format a document using a highlight colour. RGB 0-255 like HTML. sub RTFhighlight { my ($text, $highlight, $r, $g, $b, $font) = @_; $font ||= "Arial"; $text =~ s/([\\{}])/\\$1/g; # escape \ and { } $text =~ s/\n/\\par\n/g; # convert \n to \par $text =~ s/\b\Q$highlight\E\b/\\cf1 $highlight\\cf0 /g; my $header = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fswiss\\fcharset0 $font;}}\n"; $header .= "{\\colortbl ;\\red$r\\green$g\\blue$b;}\n"; return $header.$text; }