Мне нужно построить проект Lazarus, чтобы добавить выравнивание текста в прямоугольнике холста. Мне нужно применить скрипт Lazarus pas к Cheat Engine Lua, чтобы нарисовать текст внутри прямоугольного холста с выравниванием текста, по крайней мере, для одного абзаца. Мне нужно исправить скрипт Cheat Engine Lua.
скрипт Lazarus pas (работал)
unit Main;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
strutils;
type
{ TForm1 }
TForm1 = class(TForm)
btnWrite: TButton;
procedure btnWriteClick(Sender: TObject);
private
{ private declarations }
procedure JustifyText(R :TRect; s :string);
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
const
T = 'In typesetting, justification (sometimes referred to as "full justification") is the typographic alignment setting of text or images within a column or "measure" to align along both the left and right margin. Text set this way is said to be "justified."' + LineEnding +
'In justified text, the spaces between words, and, to a far lesser extent, between glyphs or letters (known as “tracking”), are stretched or sometimes compressed in order to make the text align with both the left and right margins. When using justification, it is customary to treat the last line of a paragraph separately by left or right aligning it, depending on the language direction. Lines in which the spaces have been stretched beyond their normal width are called loose lines, while those whose spaces have been compressed are called tight lines.' + LineEnding +
'Justification sometimes leads to typographic anomalies. One example: when justification is used in narrow columns, exceptionally large spaces appear between only two or three words (creating what is called a loose line).' + LineEnding +
'A second example occurs when the spaces between words line up approximately above one another in several loose lines, a distracting river of white space may appear.[2] Rivers appear in right-aligned, left-aligned and centered settings too, but are more likely to flow in justified text due to extra word spacing. Since there is no added white space built into a typical full stop (period), other than that above the full stop itself, full stops only marginally contribute to the river effect.' + LineEnding +
'Both of these problems are reduced by the addition of hyphenation. With older typesetting systems and WYSIWYG word processors, this was at one time done manually, where the compositor or author added hyphenation on a case-by-case basis. Currently, most typesetting systems (also called layout programs) and modern word processors hyphenate automatically by using a hyphenation algorithm. Professional typesetting programs almost always provide for the further use of an exception dictionary, in part because no algorithm hyphenates all words correctly, and in part because different publishers will follow different dictionaries. Different publishers may also have different rules about permissible hyphenation. Most publishers follow a basic system such as the Chicago Manual of Style or Oxford style, but will overlay their own "house style," which further restrict permissible hyphenation.';
procedure TForm1.JustifyText(R :TRect; S :string);
{Every lineending is treated as paragraph break.}
var
slParagraphs, slLines, slWords :TStringList;
i, w, j, len, p :integer;
str, a :string;
begin
if R.Right < 200 then Exit;
slParagraphs := TStringList.Create;
slLines := TStringList.Create;
slWords := TStringList.Create;
try
slParagraphs.Text := S;
slWords.Delimiter := ' ';
p := 0;
for i := 0 to slParagraphs.Count - 1 do
begin
slLines.Clear;
slWords.Clear;
slWords.DelimitedText := slParagraphs[i];
// wrap words in lines
w := 0;
str := '';
if slWords.Count > 0 then
begin
for j := 0 to slWords.count - 1 do
begin
if (w + Canvas.TextWidth(' ' + slWords[j]) <= R.Right - R.Left ) then
begin
str := str + ' ' + slWords[j];
Inc(w, Canvas.TextWidth(' ' + slWords[j]));
end
else
begin
w := Canvas.TextWidth(slWords[j] + ' ');
// add spaces to the line
a := ' ';
len := Length(str);
while Canvas.TextWidth(str) <= r.right - r.left - Canvas.TextWidth(' ') do
begin
str := Trim(str);
len := RPosEx(a, str, len - 1);
if len > 1 {0} then
System.Insert(' ', str, len)
else
begin
// back to the end of the line
len := Length(str);
a := a + ' ';
end;
end;
slLines.Add(str);
str := slWords[j];
end;
end;
end;
slLines.Add(str);
// write the lines
for j := 0 to slLines.Count - 1 do
begin
Canvas.TextOut(R.Left,
i * Canvas.TextHeight(slLines[j]) + p + R.Top,
Trim(slLines[j]));
Inc(p, Canvas.TextHeight(slLines[j]));
end;
end;
finally
slParagraphs.Free;
slLines.Free;
slWords.Free;
end;
end;
{ TForm1 }
procedure TForm1.btnWriteClick(Sender: TObject);
var
r :TRect;
begin
//Canvas.Brush.Color := Color;
r.Left := 20;
r.Top := 10;
r.Right := Width - 20;
r.Bottom := Height;
JustifyText(r, T);
end;
end.
и этот скрипт Cheat Engine Lua, я должен попробовать:
function Trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
function JustifyText(aRect, s) --- aRect = rectagle s = string
local slParagraphs, slLines, slWords --- StringList
local i, w, j, len, p --- integer
local str, a --- string
if aRect.Right < 200 then break --- Exit ?
slParagraphs = createStringlist()
slLines = createStringlist()
slWords = createStringlist()
slParagraphs.Text = s
slWords.Delimiter = ' '
p = 0
for i = 0, slParagraphs.Count - 1 do
slLines.Clear()
slWords.Clear()
slWords.DelimitedText = slParagraphs[i]
--wrap words in lines
w = 0
str = ''
if slWords.Count > 0 then
for j = 0,slWords.Count - 1 do
if (w + Canvas.TextWidth(' ' + slWords[j]) <= aRect.Right - aRect.Left ) then
str = str ..' '.. slWords[j]
w = w + Canvas.TextWidth(' ' + slWords[j]) -- Inc(w, Canvas.TextWidth(' ' + slWords[j])) -- increment ??
end
else
w = Canvas.TextWidth(slWords[j] + ' ')
-- add spaces to the line
a = ' '
len = string.len(str) --- (Length(str) ?
while Canvas.TextWidth(str) <= aRect.right - aRect.left - Canvas.TextWidth(' ') do
str = Trim(str)
len = string.match(str, a, len-1) --len = RPosEx(a, str, len - 1)
if len > 1 {0} then
str = str..' ' --System.Insert(' ', str, len)
else
-- back to the end of the line
len = string.len(str) -- Length(str)?
a = a..' '
end
end
slLines.Add(str)
str = slWords[j]
end
end
end
slLines.Add(str)
-- write the lines
for j = 0, slLines.Count - 1 do
Canvas.TextOut(aRect.Left,i * Canvas.TextHeight(slLines[j]) + p + aRect.Top,Trim(slLines[j]))
p = p + Canvas.TextHeight(slLines[j] --- Inc(p, Canvas.TextHeight(slLines[j])) -- increment ??
end
end
slParagraphs.Destroy()
slLines.Destroy()
slWords.Destroy()
end
end
end
function WriteClick(r, Txt)
r = aRect.Canvas
-- Canvas.Brush.Color = Color
r.Left = 20
r.Top = 10
r.Right = Width - 20;
r.Bottom = Height
JustifyText(r, T)
end
Примечание:
Неизвестный синтаксис в сценарии Lua:
- slWords.DelimitedText
- В c (w, Canvas.TextWidth (' '+ slWords [j]))
- RPosEx (a, str, len - 1)
- System.Insert (' ', str, len)
- Перепроверить логика и синтаксис скрипта
Есть ли шанс заставить работать скрипт CE Lua?