Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Adopting Lazarus Text Justification to CE Lua

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Thu Jan 30, 2020 8:14 am    Post subject: Adopting Lazarus Text Justification to CE Lua Reply with quote

I have to try and build a Lazarus FP project for text justification by follows a tutorial from Lazarus forum, it's working properly.
Now I am trying to adopt the Lazarus pas script to CE Lua script, but need help to arrange the script so it matches to CE Lua environments.

Lazarus FP pas:

Code:
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 the 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



CE Lua script (NEED TO FIXS):

Code:
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

-- the unknown syntax in Lua sript
-- (1) slWords.DelimitedText
-- (2) Inc(w, Canvas.TextWidth(' ' + slWords[j]))
-- (3) RPosEx(a, str, len - 1)
-- (4) System.Insert(' ', str, len)
-- (5) Re-check the script logics and syntaxs


Would some experts giving help?.

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites