Recent

Author Topic: jDrawingView Text measurement  (Read 3272 times)

CC

  • Full Member
  • ***
  • Posts: 149
jDrawingView Text measurement
« on: November 03, 2017, 12:30:09 am »
Hi,

One important piece seem to be missing from  jDrawingView. Before Drawing a text to place it properly it is important to know the  place it would occupy in both direction. 

jmpessoa

  • Hero Member
  • *****
  • Posts: 2301
Re: jDrawingView Text measurement
« Reply #1 on: November 03, 2017, 12:47:32 am »

Ok.

I will try implement text measurement!

Thank you!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

CC

  • Full Member
  • ***
  • Posts: 149
Re: jDrawingView Text measurement
« Reply #2 on: November 03, 2017, 01:33:02 am »
jmpessoa

There is an other issue with DrawText: it is not anti-aliased, which makes it unusable.

I am testing jView+jCanvas which has very similar functionality and DrawText is anti-aliased here. So it would be better to implement Text measurement for jCanvas if you can.

CC

  • Full Member
  • ***
  • Posts: 149
Re: jDrawingView Text measurement
« Reply #3 on: November 03, 2017, 02:14:00 am »
Bold fonts seem to be missing as well.

CC

  • Full Member
  • ***
  • Posts: 149
Re: jDrawingView Text measurement
« Reply #4 on: November 03, 2017, 09:36:54 am »
A  higher level solution would be to add horizontal end vertical  alignment parameters to the DrawText function. If the Android API has no published functionality for this I can help you to create it on the pascal side.

CC

  • Full Member
  • ***
  • Posts: 149
Re: jDrawingView Text measurement
« Reply #5 on: November 05, 2017, 11:57:54 pm »
This solution is working for me to align a text in a rectangle.

Add these to the source files:

....\lamw\java\lamwdesigner\jCanvas.java

Code: Java  [Select][+][-]
  1.         public  void drawTextAligned(String text, float _left, float _top, float _right, float _bottom, float _alignhorizontal , float _alignvertical ) {
  2.                 Rect bounds = new Rect();
  3.                 paint.getTextBounds(text, 0, text.length(), bounds);
  4.                 float x = _left + (_right - _left  - bounds.width()) * _alignhorizontal;
  5.                 float y = _top + (_bottom - _top  - bounds.height()) * _alignvertical + bounds.height();
  6.                 canvas.drawText(text,x,y,paint);                
  7.         }


....\lamw\android_bridges\And_jni_Bridge.pas
Code: Pascal  [Select][+][-]
  1. procedure jCanvas_drawTextAligned(env: PJNIEnv; Canv: jObject; const text: string;
  2.   _left, _top, _right, _bottom, _alignhorizontal , _alignvertical: single);
  3. var
  4.   _jMethod : jMethodID = nil;
  5.   _jParams : Array[0..6] of jValue;
  6.   cls: jClass;
  7.  begin
  8.   _jParams[0].l := env^.NewStringUTF(env, pchar(text) );
  9.   _jParams[1].F := _left;
  10.   _jParams[2].F := _top;
  11.   _jParams[3].F := _right;
  12.   _jParams[4].F := _bottom;
  13.   _jParams[5].F := _alignhorizontal;
  14.   _jParams[6].F := _alignvertical;
  15.   cls := env^.GetObjectClass(env, Canv);
  16.   _jMethod:= env^.GetMethodID(env, cls, 'drawTextAligned', '(Ljava/lang/String;FFFFFF)V');
  17.   env^.CallVoidMethodA(env,Canv,_jMethod,@_jParams);
  18.   env^.DeleteLocalRef(env,_jParams[0].l);
  19.   env^.DeleteLocalRef(env, cls);
  20. end;
  21.  

....\lamw\android_bridges\Laz_And_Controls.pas

Code: Pascal  [Select][+][-]
  1. procedure jCanvas.drawTextAligned(Text: string; _left, _top, _right,
  2.   _bottom, _alignhorizontal, _alignvertical: single);
  3. begin
  4.   if FInitialized then
  5.     jCanvas_drawTextAligned(FjEnv, FjObject, Text, _left, _top, _right,
  6.      _bottom, _alignhorizontal, _alignvertical );
  7. end;
  8.  

alignhorizontal / alignvertical
 0    : align left / top
 0.5  :to the middle
 1     :align right / bottom
« Last Edit: November 06, 2017, 02:47:28 pm by CC »

jmpessoa

  • Hero Member
  • *****
  • Posts: 2301
Re: jDrawingView Text measurement
« Reply #6 on: November 06, 2017, 01:07:08 am »

Great!

I will add to git repo!

Thank you!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

jmpessoa

  • Hero Member
  • *****
  • Posts: 2301
Re: jDrawingView Text measurement
« Reply #7 on: November 07, 2017, 05:36:19 am »


Done!

Added  DrawTextAligned  to  jCanvas and jDrawingView
and fixed  jDrawingView anti alias.

Note: Method signature changed to

DrawTextAligned(_text: string; _left, _top, _right, _bottom: single;
                           _alignHorizontal:  TTextAlignHorizontal;
                           _alignVertical: TTextAlignVertical);

where:
 
TTextAlignHorizontal = (thLeft, thRight, thCenter); 
TTextAlignVertical = (tvTop, tvBottom, tvCenter);

Thank you!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

CC

  • Full Member
  • ***
  • Posts: 149
Re: jDrawingView Text measurement
« Reply #8 on: November 11, 2017, 07:14:54 pm »
Thank you!

 

TinyPortal © 2005-2018