Page 1 of 1

[PB 5.72/5.73 x64] DrawRotatedText () problem

Posted: Mon May 01, 2023 12:43 am
by marc_256
Hi,

For my CAD program I want to use a Measurement Tool.
I like to use DrawRotatedText () for this.
but I have some problems ...

1) The quality of the text is very bad if not on a multiple of 90.0 deg.
Even when I use HightQuality when loading the font
LoadFont (0, "Courier New", 10, #PB_Font_HighQuality)

2) If the text is rotated 0.0, 90.0, 180.0, 270.0, the Y position is OK
Else the Y position have an offset other than when using multiples of 90.0 deg.
See images below and testcode.

Q1) How can I optimize/improve the font quality ?

Q2) What can I do for having the same Y position in every rotation ?


Image

Image

Image


[PB 5.72/5.73 x64] Win 8.1/10 / OPENGL

Thanks for testing,
Marc

Code: Select all


	EnableExplicit

Global Event.i

	If InitSprite () = 0
		MessageRequester("Error", "Can't open the [SPRITE] system", 0)
		End
	EndIf

	If InitKeyboard () = 0
		MessageRequester("Error", "Can't open the [KEYBOARD] system", 0)
		End
	EndIf

	If InitMouse () = 0
		MessageRequester("Error", "Can't open the [MOUSE] system", 0)
		End
	EndIf


	LoadFont (0, "Courier New", 10, #PB_Font_HighQuality)


	OpenWindow (1, 0, 0, 800, 600, "Test", #PB_Window_SystemMenu)				; | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget)
		OpenWindowedScreen ( WindowID (1), 0, 0, 800, 600, 0, 0, 0, #PB_Screen_NoSynchronization)


Repeat

	Event = WaitWindowEvent ()

	Select Event


	EndSelect

	ExamineKeyboard ()

	ExamineMouse ()

		ClearScreen ($000000)

		StartDrawing ( ScreenOutput ())

			DrawingMode (#PB_2DDrawing_Default)
			LineXY (200, 300, 400, 300, $FFE0E000)
			LineXY (200, 300, 400, 100, $FFE0E000)
			LineXY (200, 300, 200, 100, $FFE0E000)
			LineXY (200, 300, 400, 500, $FFE0E000)
			LineXY (200, 300, 200, 500, $FFE0E000)

			DrawingMode (#PB_2DDrawing_Transparent | #PB_2DDrawing_AlphaBlend)
			DrawingFont ( FontID (0))
			DrawRotatedText (200, 300, "     Test Rotated Text -  0.0 deg", 0.0, $FFE0E0E0)
			DrawRotatedText (200, 300, "     Test Rotated Text - 45.0 deg", 45.0, $FFE0E0E0)
			DrawRotatedText (200, 300, "     Test Rotated Text - 90.0 deg", 90.0, $FFE0E0E0)
			DrawRotatedText (200, 300, "     Test Rotated Text - 315.0 deg", 315.0, $FFE0E0E0)
			DrawRotatedText (200, 300, "     Test Rotated Text - 270.0 deg", 270.0, $FFE0E0E0)

		StopDrawing ()

		FlipBuffers ()

		Delay (1)

Until KeyboardPushed (#PB_Key_Escape) Or Event = #PB_Event_CloseWindow

	End


Re: [PB 5.72/5.73 x64] DrawRotatedText () problem

Posted: Mon May 01, 2023 1:24 am
by netmaestro
You're using the 2DDrawing library. If you switch to the Vector Drawing library your results should be much better. You may have to use ImageVectorOutput and show the image on your windowed screen if you're committed to using sprites & screen. Your lines will look better too as they're antialiased.

Re: [PB 5.72/5.73 x64] DrawRotatedText () problem

Posted: Mon May 01, 2023 7:12 am
by PeDe
Hello Marc,

I calculate the absolute height and width of the rotated text. With this I can position the text for the label of a coordinate well. But it is also not completely accurate, errors do not stand out. I would also like to know how to do this correctly.
The text is displayed more nicely with VectorDrawing as already mentioned by @netmaestro.

Here is an example with VectorDrawing:
https://www.dreisiebner.at/temp/PB_Plot.png

Peter

Code: Select all

; Drehung.
	rCos = Abs(Cos(Radian(*uGadget\uPlot\uAxes\uXAxis\uLabels\iRotation)))
	rSin = Abs(Sin(Radian(*uGadget\uPlot\uAxes\uXAxis\uLabels\iRotation)))

; Die maximale Höhe der Beschriftung für die Marker ermitteln.
ForEach *uGadget\uPlot\uAxes\uXAxis\uLabels\sLabelsList()
	If (fLog And fLogPower)
		; Bei logarithmischer Darstellung immer den gleichen Text verwenden mit der maximalen Breite. z.B. "10^12.0".
		TextSize("00^00" + sUnit, \eDrawingType, *uGadget\uPlot\uAxes\uXAxis\uLabels\uFont,
			@iWidth, @iHeight, DebugView::MPInstance)
	Else
		TextSize(*uGadget\uPlot\uAxes\uXAxis\uLabels\sLabelsList(), \eDrawingType,
			*uGadget\uPlot\uAxes\uXAxis\uLabels\uFont, @iWidth, @iHeight, DebugView::MPInstance)
	EndIf
	
	; Die Höhe des gedrehten Texts berechnen.
	iTemp = Round((rSin * iWidth) + (rCos * iHeight), #PB_Round_Nearest)

	If (iTemp > \iXAxisLabelMaxHeight)
		\iXAxisLabelMaxHeight = iTemp
	EndIf
	
	; Die ermittelte Breite und Höhe der Beschriftung für das Zeichnen sichern.
	AddElement(\XAxisTicksList())
	\XAxisTicksList()\iLabelWidth = iWidth
	\XAxisTicksList()\iLabelHeight = iHeight
	
Next

Re: [PB 5.72/5.73 x64] DrawRotatedText () problem

Posted: Mon May 01, 2023 1:50 pm
by marc_256
Hello,

@ netmaestro
I don't need very well drawed lines,
as I use in my program Line Data, perspective calculations, clipping calculations for 10000s of lines/frame,
for me, speed, speed and speed is the most important thing.
See example image below.

The Vector Drawing library is very nice, but ...
Will this be slower than the standard 2DDrawing library ?

@ Pede
I will try understand your program 8)
But for me, i looks like a bug ...
I don't change my calculations for 90.0 deg and multiples, as for the other degrees in between.


Thanks,
Marc


Image

Re: [PB 5.72/5.73 x64] DrawRotatedText () problem

Posted: Mon May 01, 2023 2:57 pm
by PeDe
Hello Marc,

the vector drawing commands are much slower due to the anti-aliasing. But there are exceptions, like drawing rotated text. You can also use 2D and vector alternately, but the function 'StartVectorDrawing(...)' is relatively slow. You should call it only once per drawing.

Peter

Re: [PB 5.72/5.73 x64] DrawRotatedText () problem

Posted: Mon May 01, 2023 3:09 pm
by RASHAD
Hi marc_256
You can try using 2DDraing when you need it inside only one Vector drawing
See next example
Try and let us know the conclusion
Happy coding

Code: Select all

StartVectorDrawing(ImageVectorOutput(0))

  StartDrawing(ImageOutput(0))
  StopDrawing()
  
  StartDrawing(ImageOutput(0))
  StopDrawing()

StopVectorDrawing()

Re: [PB 5.72/5.73 x64] DrawRotatedText () problem

Posted: Tue May 02, 2023 9:49 am
by pf shadoko
by curiosity, I zoomed to see how the text display works
we can see a blueness on the left side of the letters and a redness on the right side
it's probably done to improve the display on the RGB matrix of the pixels of the LCD screens

note : some fonts have a better display than others
with the "consolas" font, the luminausity of the text remains uniform whatever the angle (unlike "courrier new")

Code: Select all

	EnableExplicit

Global Event.i, i,a.f,zoom

	If InitSprite () = 0
		MessageRequester("Error", "Can't open the [SPRITE] system", 0)
		End
	EndIf

	If InitKeyboard () = 0
		MessageRequester("Error", "Can't open the [KEYBOARD] system", 0)
		End
	EndIf

	If InitMouse () = 0
		MessageRequester("Error", "Can't open the [MOUSE] system", 0)
		End
	EndIf


	;LoadFont (0, "Courier New", 10, #PB_Font_HighQuality)
	LoadFont (0, "consolas", 10)


	OpenWindow (1, 0, 0, 800, 600, "Test", #PB_Window_SystemMenu)				; | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget)
		OpenWindowedScreen ( WindowID (1), 0, 0, 800, 600, 0, 0, 0, #PB_Screen_NoSynchronization)
		
		CreateSprite(0,400,300)
Repeat

	Event = WaitWindowEvent ()

	Select Event


	EndSelect
	
	ExamineKeyboard ()
	
	ExamineMouse ()
	
	ClearScreen ($000000)
	
	StartDrawing ( SpriteOutput(0))
	
	DrawingMode (#PB_2DDrawing_Default)
	Box(0,0,OutputWidth(),OutputHeight(),0)
	
	DrawingMode (#PB_2DDrawing_Transparent | #PB_2DDrawing_AlphaBlend)
	DrawingFont ( FontID (0))
	For i=0 To 16
	  a=i*22.5
	  LineXY (200, 200, 200+Cos(Radian(a))*200, 200+Sin(Radian(a))*200, $FFE0E000)
	  DrawRotatedText (200, 200, "     Test Rotated Text -  "+a+" deg", a, $FFE0E0E0)
	Next
	
	StopDrawing ()
	zoom=2
	ZoomSprite(0,400*zoom,300*zoom)
	DisplaySprite(0,0,0)
	FlipBuffers ()
	
	Delay (1)
	
Until KeyboardPushed (#PB_Key_Escape) Or Event = #PB_Event_CloseWindow

End


Re: [PB 5.72/5.73 x64] DrawRotatedText () problem

Posted: Mon May 08, 2023 2:57 am
by Demivec
Here is some sample code based on your original code that I've cobbled together to see what can be done to address the issues you brought out.

It uses a procedure to modify positioning and even rotation slightly to better align rotated text to a center line.

The procedure requires a font of at least 14 pts in height and is highly font dependent at the moment. I'm looking for future modifications to the code that will include adjustments that are based on any given font as well as point size.

It is at least a starting point.

Push the space bar to cycle between the two methods of drawing rotated text.

Code: Select all

EnableExplicit

;need a font at least 14 pts in height
Procedure cDrawRotatedText(x, y, text.s, angle.f, color = $808080FF)
  Protected ot = TextHeight(text) / 2, otx, oty
  Protected m.f = 1 + 0.1 * 5 ;correction for offsets (mostly guestimate from observations)
  
  angle = Mod(angle + 0.12, 360) ;correction to angle (correction for uneven spacing at 45 degree increments around circle)
  
  otx = -ot * Sin(Radian(angle)) * m: oty = -ot * Cos(Radian(angle)) * m ;text offsets to center on angle line
  DrawRotatedText(x + otx, y + oty, text, angle, color)
EndProcedure



Global Event.i

If InitSprite () = 0
  MessageRequester("Error", "Can't open the [SPRITE] system", 0)
  End
EndIf

If InitKeyboard () = 0
  MessageRequester("Error", "Can't open the [KEYBOARD] system", 0)
  End
EndIf

If InitMouse () = 0
  MessageRequester("Error", "Can't open the [MOUSE] system", 0)
  End
EndIf

LoadFont (0, "Courier New", 14, #PB_Font_HighQuality)

OpenWindow (1, 0, 0, 1000, 1000, "Test - [Space Bar] alternates drawing method", #PB_Window_SystemMenu)				; | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget)
OpenWindowedScreen ( WindowID (1), 0, 0, 1000, 1000, 0, 0, 0, #PB_Screen_NoSynchronization)

Define cdx, cdy, ot, otx, oty, drawTextOption = 0, i

cdx = 500: cdy = 500 ;center of drawing (lines)
ReleaseMouse(#True)
Repeat
  
  Repeat:
    Event = WindowEvent ()
    If  Event = #PB_Event_CloseWindow
      End
    EndIf
  Until Event = 0
  
  ExamineKeyboard ()
  ExamineMouse ()
  
  If KeyboardReleased(#PB_Key_Space)
    drawTextOption = (drawTextOption + 1) % 2
  EndIf
  
  ClearScreen ($000000)
  
  StartDrawing ( ScreenOutput ())
  
  DrawingMode (#PB_2DDrawing_Default)
  LineXY (cdx, cdy, cdx + 500, cdy      , RGBA($FF, $E0, $E0, $E0))
  LineXY (cdx, cdy, cdx + 500, cdy - 500, RGBA($FF, $E0, $E0, $E0))
  LineXY (cdx, cdy, cdx      , cdy - 500, RGBA($FF, $E0, $E0, $E0))
  
  LineXY (cdx, cdy, cdx - 500, cdy - 500, RGBA($FF, $E0, $E0, $E0))
  LineXY (cdx, cdy, cdx - 500, cdy      , RGBA($FF, $E0, $E0, $E0))
  LineXY (cdx, cdy, cdx - 500, cdy + 500, RGBA($FF, $E0, $E0, $E0))
  
  LineXY (cdx, cdy, cdx + 500, cdy + 500, RGBA($FF, $E0, $E0, $E0))
  LineXY (cdx, cdy, cdx      , cdy + 500, RGBA($FF, $E0, $E0, $E0))
  
  DrawingMode (#PB_2DDrawing_Transparent | #PB_2DDrawing_AlphaBlend)
  DrawingFont ( FontID (0))
  
  Select drawTextOption
    Case 0
      For i = 0 To 359 Step 5
        DrawRotatedText (cdx, cdy, "Test Rotated Text - " + RSet(StrF(i, 2), 6, "0") + " deg", i, RGBA($FF, $E0, $E0, $E0))
      Next
      
      DrawText (0, 0, "PB DrawRotatedText")
    Case 1
      For i = 0 To 359 Step 5
        cDrawRotatedText (cdx, cdy, "Test Rotated Text - " + RSet(StrF(i, 2), 6, "0") + " deg", i, RGBA($FF, $E0, $E0, $E0))
        DrawText (0, 0, "custom DrawRotatedText")
      Next
  EndSelect
  
  StopDrawing ()
  
  
  FlipBuffers ()
  
  Delay (1)
  
Until KeyboardPushed (#PB_Key_Escape)

End