Skip to content

Commit

Permalink
Add BitmapFontOCR
Browse files Browse the repository at this point in the history
  • Loading branch information
ollydev committed Dec 23, 2024
1 parent 54535df commit 261997f
Show file tree
Hide file tree
Showing 8 changed files with 471 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Examples/image_drawtext.simba
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ begin
myImage.DrawColor := Colors.RED;
myImage.DrawBox(myBox);
myImage.DrawColor := Colors.BLACK;
myImage.DrawText(Now.ToString('c'), myBox, [EDrawTextAlign.CENTER, EDrawTextAlign.VERTICAL_CENTER]);
myImage.DrawText(Now.ToString('c'), myBox, [EImageTextAlign.CENTER, EImageTextAlign.VERTICAL_CENTER]);

myImage.FontBold := True;
myImage.FontSize := 50;
Expand Down
12 changes: 6 additions & 6 deletions Examples/mouse_teleport_event.simba
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
var
TPA: TPointArray; // stores the teleport events so we can view at the end

procedure MouseTeleportEvent(var Sender: TTarget; P: TPoint);
procedure MouseTeleportEvent(var Target: TTarget; Data: TTargetEventData);
begin
WriteLn('Mouse teleported to: ', P);
TPA += P;
WriteLn('Mouse teleported to: ', Data.MouseTeleport);
TPA += [Data.MouseTeleport.X, Data.MouseTeleport.Y];
end;

var
Event: TMouseTeleportEvent;
Event: TTargetEvent;
begin
Event := Target.AddMouseEvent(@MouseTeleportEvent);
Event := Target.AddEvent(ETargetEventType.MOUSE_TELEPORT, @MouseTeleportEvent);
Target.MouseTeleport([200,200]);
Target.MouseMove([600,600]);
Target.RemoveMouseEvent(Event); // Remove the event
Target.RemoveEvent(ETargetEventType.MOUSE_TELEPORT, Event); // Remove the event

Target.MouseMove([200, 600]); // The event has been removed so this wont be "recorded" when we show the path

Expand Down
2 changes: 2 additions & 0 deletions Examples/randomleft.simba
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Random left returns numbers weighted to the left number

const
SampleCount = 1000000;
Range = 10;
Expand Down
97 changes: 97 additions & 0 deletions Source/script/imports/simba.import_bitmapfontocr.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
unit simba.import_bitmapfontocr;

{$i simba.inc}

interface

uses
Classes, SysUtils,
simba.base, simba.script;

procedure ImportBitmapFontOCR(Script: TSimbaScript);

implementation

uses
lptypes, lpvartypes,
simba.bitmapfontocr,
simba.image;

type
PBitmapFont = ^TBitmapFont;
PBitmapFontOCR = ^TBitmapFontOCR;

procedure _LapeBitmapFontOCR_AddFont(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PBitmapFont(Result)^ := PBitmapFontOCR(Params^[0])^.AddFont(PString(Params^[1])^, PInteger(Params^[2])^);
end;

procedure _LapeBitmapFontOCR_Recognize1(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PString(Result)^ := PBitmapFontOCR(Params^[0])^.Recognize(PSimbaImage(Params^[1])^, PBitmapFont(Params^[2])^, PPoint(Params^[3])^);
end;

procedure _LapeBitmapFontOCR_Recognize2(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PString(Result)^ := PBitmapFontOCR(Params^[0])^.Recognize(PSimbaImage(Params^[1])^, PBitmapFont(Params^[2])^, PBox(Params^[3])^);
end;

procedure _LapeBitmapFontOCR_RecognizeLines(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
begin
PStringArray(Result)^ := PBitmapFontOCR(Params^[0])^.RecognizeLines(PSimbaImage(Params^[1])^, PBitmapFont(Params^[2])^, PBox(Params^[3])^);
end;

procedure ImportBitmapFontOCR(Script: TSimbaScript);
begin
with Script.Compiler do
begin
addGlobalType([
'record',
' Glyphs: array of record',
' Value: Char;',
' Width: Integer;',
' Height: Integer;',
'',
' points: TPointArray;',
' shadow: TPointArray;',
' background: TPointArray;',
'',
' BestMatch: Integer;',
' end;',
'',
' height: Integer;',
' spaceWidth: Integer;',
'end;'],
'TBitmapFont'
);

addGlobalType([
'record',
' Text: String;',
' Hits: Integer;',
' Bounds: TBox;',
'end;'],
'TBitmapFontMatch'
);

addGlobalType([
'record',
' tolerance: Single;',
' shadowTolerance: Single;',
' Blacklist: set of Char;',
' maxWalk: Integer;',
' matches: array of TBitmapFontMatch;',
'end'],
'TBitmapFontOCR'
);

addGlobalFunc('function TBitmapFontOCR.AddFont(dir: String; SpaceWidth: Integer): TBitmapFont;', @_LapeBitmapFontOCR_AddFont);

addGlobalFunc('function TBitmapFontOCR.Recognize(img: TImage; font: TBitmapFont; p: TPoint): String; overload;', @_LapeBitmapFontOCR_Recognize1);
addGlobalFunc('function TBitmapFontOCR.Recognize(img: TImage; font: TBitmapFont; bounds: TBox): String; overload;', @_LapeBitmapFontOCR_Recognize2);
addGlobalFunc('function TBitmapFontOCR.RecognizeLines(img: TImage; font: TBitmapFont; bounds: TBox): TStringArray;', @_LapeBitmapFontOCR_RecognizeLines);
end;
end;

end.

3 changes: 2 additions & 1 deletion Source/script/simba.script_imports.pas
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ implementation

// Simba classes
simba.import_image, simba.import_externalcanvas, simba.import_dtm, simba.import_matchtemplate,
simba.import_json, simba.import_imagebox, simba.import_shapebox,
simba.import_json, simba.import_imagebox, simba.import_shapebox, simba.import_bitmapfontocr,

// LCL
simba.import_lcl_system, simba.import_lcl_graphics, simba.import_lcl_controls,
Expand Down Expand Up @@ -83,6 +83,7 @@ procedure AddSimbaImports(Script: TSimbaScript);
ImportExternalCanvas(Script);
ImportMatchTemplate(Script);
ImportJSON(Script);
ImportBitmapFontOCR(Script);

ImportDateTime(Script);
ImportEncoding(Script);
Expand Down
Loading

0 comments on commit 261997f

Please sign in to comment.