From 4e01a5e8178b301a329a99a797bfc237723410de Mon Sep 17 00:00:00 2001 From: sekelsenmat Date: Fri, 2 Dec 2011 17:06:32 +0000 Subject: [PATCH] customdrawn-android: Advances the text drawing code git-svn-id: trunk@33909 - --- .../src/com/pascal/lcltest/LCLActivity.java | 43 ++++++++++++++----- lcl/interfaces/customdrawn/customdrawnint.pas | 1 + .../customdrawn/customdrawnobject_android.inc | 1 + .../customdrawn/customdrawnwinapi_android.inc | 42 +++++++++++++----- 4 files changed, 67 insertions(+), 20 deletions(-) diff --git a/examples/androidlcl/android/src/com/pascal/lcltest/LCLActivity.java b/examples/androidlcl/android/src/com/pascal/lcltest/LCLActivity.java index 532f89b399..a126f62205 100755 --- a/examples/androidlcl/android/src/com/pascal/lcltest/LCLActivity.java +++ b/examples/androidlcl/android/src/com/pascal/lcltest/LCLActivity.java @@ -10,7 +10,9 @@ import android.view.*; public class LCLActivity extends Activity { + // ------------------------------------------- // Our drawing surface + // ------------------------------------------- private class LCLSurface extends SurfaceView { public LCLSurface(Context context) @@ -30,9 +32,9 @@ public class LCLActivity extends Activity //Log.v("lclproject", "LCLSurface.onDraw width=" + Integer.toString(lWidth) // + " height=" + Integer.toString(lHeight)); - Bitmap lclbitmap = Bitmap.createBitmap(lWidth, lHeight, Bitmap.Config.ARGB_8888); - LCLDrawToBitmap(lWidth, lHeight, lclbitmap); - canvas.drawBitmap(lclbitmap, 0, 0, null); + Bitmap localbitmap = Bitmap.createBitmap(lWidth, lHeight, Bitmap.Config.ARGB_8888); + LCLDrawToBitmap(lWidth, lHeight, localbitmap); + canvas.drawBitmap(localbitmap, 0, 0, null); } @@ -44,6 +46,10 @@ public class LCLActivity extends Activity } } + // ------------------------------------------- + // Activity Events + // ------------------------------------------- + /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) @@ -57,39 +63,56 @@ public class LCLActivity extends Activity LCLOnCreate(this); } + // ------------------------------------------- // JNI table of Pascal functions + // ------------------------------------------- public native int LCLDrawToBitmap(int width, int height, Bitmap bitmap); public native int LCLOnTouch(float x, float y, int action); public native int LCLOnCreate(LCLActivity lclactivity); + // ------------------------------------------- // Functions exported to the Pascal side + // ------------------------------------------- // input: String lcltext // output: int lclwidth, int lclheight public void LCLDoGetTextBounds() { - Paint paint = new Paint(); - Rect bounds = new Rect(); - paint.getTextBounds(lcltext, 0, lcltext.length(), bounds); - lclwidth = bounds.width(); - lclheight = bounds.height(); + Paint localpaint = new Paint(); + Rect localbounds = new Rect(); + localpaint.getTextBounds(lcltext, 0, lcltext.length(), localbounds); + lclwidth = localbounds.width(); + lclheight = localbounds.height(); } + // input: String lcltext, int lclwidth, int lclheight + // output: lclbitmap + public void LCLDoDrawText() + { + lclbitmap = Bitmap.createBitmap(lclwidth, lclheight, Bitmap.Config.ARGB_8888); + Canvas localcanvas = new Canvas(lclbitmap); + Paint localpaint = new Paint(); + localcanvas.drawText(lcltext, 0, 0, localpaint); + } + + // ------------------------------------------- // Fields exported to the Pascal side for easier data communication + // ------------------------------------------- public String lcltext; public int lclwidth; public int lclheight; + public Bitmap lclbitmap; static { try { - Log.i("lclproject", "Trying to load liblclapp.so"); + Log.i("lclapp", "Trying to load liblclapp.so"); System.loadLibrary("lclapp"); } catch(UnsatisfiedLinkError ule) { - Log.e("lclproject", "WARNING: Could not load liblclapp.so"); + Log.e("lclapp", "WARNING: Could not load liblclapp.so"); ule.printStackTrace(); } } diff --git a/lcl/interfaces/customdrawn/customdrawnint.pas b/lcl/interfaces/customdrawn/customdrawnint.pas index ddc9fa7f70..93fc3dbc86 100644 --- a/lcl/interfaces/customdrawn/customdrawnint.pas +++ b/lcl/interfaces/customdrawn/customdrawnint.pas @@ -219,6 +219,7 @@ var // Methods of our Activity javaMethod_LCLDoGetTextBounds: jmethodid = nil; + javaMethod_LCLDoDrawText: jmethodid = nil; // This is utilized to store the information such as invalidate requests in events eventResult: jint; diff --git a/lcl/interfaces/customdrawn/customdrawnobject_android.inc b/lcl/interfaces/customdrawn/customdrawnobject_android.inc index d093c70d9d..bb035fe9fb 100644 --- a/lcl/interfaces/customdrawn/customdrawnobject_android.inc +++ b/lcl/interfaces/customdrawn/customdrawnobject_android.inc @@ -171,6 +171,7 @@ begin // Read all method IDs javaMethod_LCLDoGetTextBounds := javaEnvRef^^.GetMethodID(javaEnvRef, javaActivityClass, 'LCLDoGetTextBounds', '()V'); + javaMethod_LCLDoDrawText := javaEnvRef^^.GetMethodID(javaEnvRef, javaActivityClass, 'LCLDoDrawText', '()V'); result:=JNI_VERSION_1_4;// 1_6 is another option end; diff --git a/lcl/interfaces/customdrawn/customdrawnwinapi_android.inc b/lcl/interfaces/customdrawn/customdrawnwinapi_android.inc index 28c2a66a4e..bcd1620fe2 100644 --- a/lcl/interfaces/customdrawn/customdrawnwinapi_android.inc +++ b/lcl/interfaces/customdrawn/customdrawnwinapi_android.inc @@ -2179,23 +2179,45 @@ end;*) ------------------------------------------------------------------------------} function TCDWidgetSet.ExtTextOut(DC: HDC; X, Y: Integer; Options: Longint; Rect: PRect; Str: PChar; Count: Longint; Dx: PInteger): Boolean; -{var - WideStr: WideString; - QtDC: TQtDeviceContext absolute DC; - B: Boolean; } +var + lJavaString: jstring; begin - {$ifdef VerboseCDWinAPI} - WriteLn('[WinAPI ExtTextOut]'); + {$ifdef VerboseCDText} + DebugLn(Format('[WinAPI ExtTextOut] DC=%x javaEnvRef=%x Str=%s', [DC, PtrInt(javaEnvRef), StrPas(Str)])); {$endif} Result := False; -{ if ((Options and (ETO_OPAQUE + ETO_CLIPPED)) <> 0) and (Rect = nil) then + if (Str = nil) or (Str = '') then Exit; + + if ((Options and (ETO_OPAQUE + ETO_CLIPPED)) <> 0) and (Rect = nil) then exit; if not IsValidDC(DC) then Exit; - if ((Options and ETO_OPAQUE) <> 0) then + if (javaEnvRef = nil) then Exit; + + // Prepare the input + lJavaString :=javaEnvRef^^.NewStringUTF(javaEnvRef, Str); + javaEnvRef^^.SetObjectField(javaEnvRef, javaActivityObject, JavaField_lcltext, lJavaString); + + // Call the method + javaEnvRef^^.CallVoidMethod(javaEnvRef, javaActivityObject, javaMethod_LCLDoGetTextBounds); + + // Call the method + javaEnvRef^^.CallVoidMethod(javaEnvRef, javaActivityObject, javaMethod_LCLDoDrawText); + +{ // Read the output + Size.cx := javaEnvRef^^.GetLongField(javaEnvRef, javaActivityObject, javaField_lclwidth); + Size.cy := javaEnvRef^^.GetLongField(javaEnvRef, javaActivityObject, javaField_lclheight);} + + {$ifdef VerboseCDText} + DebugLn('[WinAPI ExtTextOut]'); + {$endif} + + Result := True; + +{ if ((Options and ETO_OPAQUE) <> 0) then QtDC.fillRect(Rect^.Left, Rect^.Top, Rect^.Right - Rect^.Left, Rect^.Bottom - Rect^.Top); if Str <> nil then @@ -4129,8 +4151,8 @@ function TCDWidgetSet.GetTextMetrics(DC: HDC; var TM: TTextMetric): Boolean; QtDC: TQtDeviceContext absolute DC; FontWeight: Integer;} begin - {$ifdef VerboseCDWinAPI} - DebugLn('[WinAPI GetTextMetrics]'); + {$ifdef VerboseCDText} + DebugLn(Format('[WinAPI GetTextMetrics] DC=%x javaEnvRef=%x', [DC, PtrInt(javaEnvRef)])); {$endif} Result := IsValidDC(DC);