Printing text at 0degree and 90 degrees

Just starting out? Need help? Post your questions and find answers here.
tony_bruguier
User
User
Posts: 11
Joined: Mon Jun 21, 2004 11:38 pm
Location: Pasadena, CA
Contact:

Printing text at 0degree and 90 degrees

Post by tony_bruguier »

Hi PBers,

I'd like to know if I could print some text at a 90 degree angle. I searched thru the archive, and what is suggested is landscape/portrait thingy. But I need to do 0 degrees and 90 degrees at the same time (tricky huh?).

Thanks, as usual, for your help!
Smiles,
Tony
GreenGiant
Enthusiast
Enthusiast
Posts: 252
Joined: Fri Feb 20, 2004 5:43 pm

Post by GreenGiant »

Print, as in on a printer, or print as in print on the screen?
tony_bruguier
User
User
Posts: 11
Joined: Mon Jun 21, 2004 11:38 pm
Location: Pasadena, CA
Contact:

Post by tony_bruguier »

[quote="GreenGiant"]Print, as in on a printer, or print as in print on the screen?[/quote]

On a printer.

Sorry about the inaccuracy.
Tony
GreenGiant
Enthusiast
Enthusiast
Posts: 252
Joined: Fri Feb 20, 2004 5:43 pm

Post by GreenGiant »

Okay, this may look like I asked you then did the opposite because in my example I am drawing it to the screen. But all you'd need to do is put the StartDrawing(PrinterOutput()) bit. The main thing I'm using is PlgBlt_() to rotate an image. The procedure I made just rotates any image through 90 degrees. I hope you can work out from it how to do 90 degrees the other way too. Anyway, here you are,

Code: Select all

Dim rect.Point(2)

Procedure RotateImage(ImageNumber)
  height=ImageHeight()
  width=ImageWidth()
  If height>width
    temp=CreateImage(#PB_Any,height,height)
  Else
    temp=CreateImage(#PB_Any,width,width)
  EndIf
  rect(0)\x=height-1
  rect(0)\y=0
  rect(1)\x=height-1
  rect(1)\y=width-1
  rect(2)\x=0
  rect(2)\y=0
  DC=StartDrawing(ImageOutput())
    DrawImage(UseImage(ImageNumber),0,0)
    PlgBlt_(DC,@rect(),DC,0,0,width,height,0,0,0)
  StopDrawing()
  GrabImage(temp,ImageNumber,0,0,height,width)
  FreeImage(temp)
EndProcedure 


OpenWindow(0,0,0,400,400,#PB_Window_SystemMenu | #PB_Window_ScreenCentered,"test")
CreateImage(0,400,400)

DC=StartDrawing(ImageOutput())
  Box(0,0,400,400,$FFFFFF)
  Locate(50,50)
  FrontColor(255,0,0)
  DrawingMode(1)
  DrawText("This is sideways")
StopDrawing()

RotateImage(0)

StartDrawing(ImageOutput())
  Locate(10,10)
  DrawText("This is straight")
StopDrawing()

CreateGadgetList(WindowID(0))
ImageGadget(0,0,0,400,400,UseImage(0))

Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow
Just let me know if you have any questions about it.

EDIT: I think ebs' way is probably better, didnt know that one.
Last edited by GreenGiant on Fri Aug 20, 2004 10:03 pm, edited 1 time in total.
ebs
Enthusiast
Enthusiast
Posts: 566
Joined: Fri Apr 25, 2003 11:08 pm

Post by ebs »

Tony,

Take a look at the PrintRotatedText() procedure in the following code. I took an example of printing rotated text on an image and modified it to use the printer instead.

Regards,
Eric

Code: Select all

;- draw rotated text on specified image
Procedure DrawRotatedText(Image.l, Text.s, X.l, Y.l, FontName.s, Size.l, Angle.l, Weight.l, Italic.l, Underline.l, StrikeOut.l, Color.l)
  PrevFont.l
  hFont.l
  ImageDC.l
  
  ; get image device context for API calls
  UseImage(Image)
  ImageDC.l = StartDrawing(ImageOutput())
  
  ; font size is in pixels and negative
  ; if you are specifying the character height
  lfHeight.l = Size * GetDeviceCaps_(ImageDC, #LOGPIXELSY) / 72
  
  ; create rotated font and select into image DC
  hFont = CreateFont_(lfHeight, 0, Angle * 10, 0, Weight, Italic, Underline, StrikeOut, #ANSI_CHARSET, #OUT_TT_PRECIS, #CLIP_LH_ANGLES, #PROOF_QUALITY, #DEFAULT_PITCH|#FF_DONTCARE, FontName) 
  PrevFont = SelectObject_(ImageDC, hFont)
  
  ; set position
  Locate(X, Y)
  ; transparent background
  DrawingMode(1)
  ; draw text in specified color
  FrontColor(Red(Color), Green(Color), Blue(Color))
  DrawText(Text)
  StopDrawing()
  
  ; clean up by restoring original font
  SelectObject_(ImageDC, PrevFont)
  DeleteObject_(hFont)
EndProcedure

;- draw rotated text on printer
Procedure PrintRotatedText(Text.s, X.l, Y.l, FontName.s, Size.l, Angle.l, Weight.l, Italic.l, Underline.l, StrikeOut.l, Color.l)
  PrevFont.l
  hFont.l
  PrinterDC.l
  
  ; get printer device context for API calls
  PrinterDC.l = StartDrawing(PrinterOutput())
  
  ; font size is in pixels and negative
  ; if you are specifying the character height
  lfHeight.l = Size * GetDeviceCaps_(PrinterDC, #LOGPIXELSY) / 72
  
  ; create rotated font and select into printer DC
  hFont = CreateFont_(lfHeight, 0, Angle * 10, 0, Weight, Italic, Underline, StrikeOut, #ANSI_CHARSET, #OUT_TT_PRECIS, #CLIP_LH_ANGLES, #PROOF_QUALITY, #DEFAULT_PITCH|#FF_DONTCARE, FontName) 
  PrevFont = SelectObject_(PrinterDC, hFont)
  
  ; set position
  Locate(X, Y)
  ; transparent background
  DrawingMode(1)
  ; draw text in specified color
  FrontColor(Red(Color), Green(Color), Blue(Color))
  DrawText(Text)
  StopDrawing()
  
  ; clean up by restoring original font
  SelectObject_(PrinterDC, PrevFont)
  DeleteObject_(hFont)
EndProcedure

OpenWindow(0, 0, 0, 400, 300, #PB_Window_ScreenCentered|#PB_Window_SystemMenu, "Rotated Text Test")
CreateGadgetList(WindowID())
ImageGadget(1, 10, 10, 380, 280, 0)
CreateImage(2, 380, 280)
StartDrawing(ImageOutput())
Box(0, 0, ImageWidth(), ImageHeight(), RGB(128, 128, 128))
StopDrawing()

; rotated text on image
DrawRotatedText(2, "Rotated Text?", 190, 140, "Arial", 12, 0, #FW_SEMIBOLD, #False, #False, #False, RGB(255, 0, 0))
DrawRotatedText(2, "Rotated Text?", 190, 140, "Arial", 12, 90, #FW_SEMIBOLD, #False, #False, #False, RGB(0, 255, 0))
DrawRotatedText(2, "Rotated Text?", 190, 140, "Arial", 12, 180, #FW_SEMIBOLD, #False, #False, #False, RGB(0, 0, 255))
DrawRotatedText(2, "Rotated Text?", 190, 140, "Arial", 12, 270, #FW_SEMIBOLD, #False, #False, #False, RGB(255, 255, 255))
SetGadgetState(1, ImageID())

; rotated text on printer
If DefaultPrinter()
  StartPrinting("Rotated Text")
  
  PrintRotatedText("Rotated Text?", 1000, 2000, "Arial", 12, 0, #FW_SEMIBOLD, #False, #False, #False, RGB(255, 0, 0))
  PrintRotatedText("Rotated Text?", 1000, 2000, "Arial", 12, 90, #FW_SEMIBOLD, #False, #False, #False, RGB(0, 255, 0))
  PrintRotatedText("Rotated Text?", 1000, 2000, "Arial", 12, 180, #FW_SEMIBOLD, #False, #False, #False, RGB(0, 0, 255))
  PrintRotatedText("Rotated Text?", 1000, 2000, "Arial", 12, 270, #FW_SEMIBOLD, #False, #False, #False, RGB(0, 0, 0))

  StopPrinting()
EndIf

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
tony_bruguier
User
User
Posts: 11
Joined: Mon Jun 21, 2004 11:38 pm
Location: Pasadena, CA
Contact:

Post by tony_bruguier »

Oh my God! Thank you Thank you! Humm, you're on my top list when I get a gmail invitation :P

Smiles,
Tony
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

tony_bruguier,

I thought again to your post and tried to design something non API dependant ... Maybe this may help. Tell me suggestions / comments or come back with any question :

Code: Select all

;
; Testing the possibility to design a procedure to draw a rotated label without using any API commands.
; F.Weil : 20040822
;
Enumeration
  #Window_Main
  #Font
  #Image_Temp
  #Image_Background
  #Image_Text
EndEnumeration

Dim TextPixels.l(0, 0)
Global WindowXSize.l, WindowYSize.l, kPi.f

;
; DrawRotatedLabel
; (
; ImageWidth.l,              Image width
; ImageHeight.l,             Image height
; TextLeft.l,                Text anchor point x coordinate
; TextTop.l,                 Text anchor point y coordinate
; TextCX.l,                  Text rotation center x coordinate
; TextCY.l,                  Text rotation center y coordinate
; DegAngle.l                 Angle in degrees for absolute rotation
; )
;
; In fact this would work for any given image. This procedure is not optimized to run fast (should be downgraded to FASM).
; Here I assume that #Black is the transparent color to not process. This should also be entered as a procedure parameter.
; I did store pixels in a glbal array to design this "test level" procedure, but it should be changed so that the array to use would be an address.
; Also in this benchmark, I assume to draw directly onto the screen (or a sprite or anything inside StartDrawing() / StopDrawing()).
;
; The rotation DegAngle parameter is in integer degrees for easyness of use, but may be changed to a floating real number (which would be just
; shorter in the procedure code).
;
Procedure DrawRotatedLabel(ImageWidth.l, ImageHeight.l, TextLeft.l, TextTop.l, TextCX.l, TextCY.l, DegAngle.l)
  ;
  ; Conversion of DegAngle (integer degrees) to RadAngle for calculating with regular Sin() / Cos() functions
  ;
  RadAngle.f = DegAngle * kPi
  CRadAngle.f = Cos(RadAngle)
  SRadAngle.f = Sin(RadAngle)
  ;
  ; Transofrmation of anchor point coordinates to rotated anchor point
  ;
  rCX = TextCX + (TextLeft - TextCX) * CRadAngle - (TextTop - TextCY) * SRadAngle
  rCY = TextCY + (TextTop - TextCY) * CRadAngle + (TextLeft - TextCX) * SRadAngle
  ;
  ; Parse all pixels and draw rotated pixels if not transparent
  ;
  For x = 0 To ImageWidth - 1
    For y = 0 To ImageHeight - 1
      Pixel = TextPixels(x, y)
      If Pixel <> #Black
          rx = x * CRadAngle - y * SRadAngle + rCX
          ry = y * CRadAngle + x * SRadAngle + rCY
          If rx => 0 And rx <= WindowXSize And ry => 0 And ry <= WindowYSize
              Plot(rx, ry, Pixel)
          EndIf
      EndIf
    Next
  Next
EndProcedure

;
; Main program starts here
;
  WindowXSize = 640
  WindowYSize = 480

  kPi = 3.14159265 / 180
  ;
  ; Create a background image
  ;
  ImageID_Background = CreateImage(#Image_Background, WindowXSize, WindowYSize)
  StartDrawing(ImageOutput())
    For x = 0 To WindowXSize
      For y = 0 To WindowYSize
        Plot(x, y, Color)
        Color + 1
      Next
    Next
  StopDrawing()

  Quit = #FALSE
  FontName.s = "Verdana"
  FontSize = 40
  FontHeight = FontSize * 1.6
  DrawingFontID = LoadFont(#Font, FontName, FontSize, #PB_Font_HighQuality)
  TextLeft = 200 ; Defines the anchor point o the text To rotate
  TextTop = 200
  TextCX = 250 ; Defines the rotate center point
  TextCY = 250
  ImageWidth = 200 ; Defines the text image width / height
  ImageHeight = 100
  
  ;
  ; Create an image with a text inside
  ; 
  ImageID_Text = CreateImage(#Image_Text, ImageWidth, ImageHeight)
  StartDrawing(ImageOutput())
    FontName.s = "Verdana"
    FontSize.l = 16
    FontAttributes.l = #PB_Font_HighQuality | #PB_Font_Bold
    DrawingFont(LoadFont(0, FontName, FontSize, FontAttributes))
    DrawingMode(1)
    BackColor(0, 0, $40)
    FrontColor($FF, $FF, $C0)
    Locate(20, 20)
    Text.s = "Hello world !"
    TextLength = TextLength(Text)
    TextHeight = FontSize * 1.6
    DrawText(Text)
    DrawingMode(4)
    Box(10, 10, TextLength + 20, TextHeight + 20, $C0FFFF)
    Dim TextPixels.l(ImageWidth, ImageHeight)
    For x = 0 To ImageWidth
      For y = 0 To ImageHeight
        TextPixels(x, y) = Point(x, y)
      Next
    Next
  StopDrawing()

  If OpenWindow(#Window_Main, 0, 0, WindowXSize, WindowYSize, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_ScreenCentered, "Window")
      If InitSprite() And OpenWindowedScreen(WindowID(#Window_Main), 0, 0, WindowXSize, WindowYSize, #NULL, 0, 0)
          AddKeyboardShortcut(#Window_Main, #PB_Shortcut_Escape, #PB_Shortcut_Escape)
          If CreateGadgetList(WindowID(#Window_Main))
          EndIf
          Repeat
            Select WindowEvent()
              Case #PB_Event_CloseWindow
                Quit = #TRUE
              Case #PB_event_Menu
                Select EventMenuID()
                  Case #PB_Shortcut_Escape
                    Quit = #TRUE
                EndSelect
              EndSelect
              FlipBuffers(0)
              ClearScreen(0, 0, $50)
              StartDrawing(ScreenOutput())
                DrawImage(ImageID_Background, 0, 0)
                DegAngle + 1.0
                DrawRotatedLabel(ImageWidth, ImageHeight, TextLeft, TextTop, TextCX, TextCY, DegAngle)
              StopDrawing()
              Delay(1)
          Until Quit
      EndIf
  EndIf
End
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
tony_bruguier
User
User
Posts: 11
Joined: Mon Jun 21, 2004 11:38 pm
Location: Pasadena, CA
Contact:

Post by tony_bruguier »

Do you guys want a gmail invitations?
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post by Ollivier »

Procedure updated for 4.30:

Code: Select all

Procedure RotateImage(ImageNumber.I)
   Protected Result.I
   Protected Width.I
   Protected Height.I
   Protected Temp.I
   Protected *Ptr
   Protected SoI.I   
   SoI = SizeOf(INTEGER)
   *Ptr = AllocateMemory(6 * SoI)
   Height = ImageHeight(ImageNumber)
   Width = ImageWidth(ImageNumber)
   If Height > Width
      Temp = CreateImage(#PB_Any, Height, Height)
   Else
      Temp = CreateImage(#PB_Any, Width, Width)
   EndIf
   PokeI(*Ptr, Height): *Ptr + SoI
   PokeI(*Ptr, 0):      *Ptr + SoI
   PokeI(*Ptr, Height): *Ptr + SoI
   PokeI(*Ptr, Width):  *Ptr + SoI
   PokeI(*Ptr, 0):      *Ptr + SoI
   PokeI(*Ptr, 0):      *Ptr + SoI
   DC = StartDrawing(ImageOutput(Temp) )
      DrawImage(ImageID(ImageNumber),0,0)
      PlgBlt_(DC, *Ptr - (6 * SoI), DC, 0, 0, Width, Height, 0, 0, 0)
   StopDrawing()
   If ImageNumber > 99999
      FreeImage(ImageNumber)
      Result = GrabImage(Temp, -1, 0, 0, Height, Width)
   Else
      Result = GrabImage(Temp, ImageNumber, 0, 0, Height, Width)
   EndIf
   FreeImage(Temp)
   ProcedureReturn Result
EndProcedure




;**************
; Test 
;**************

For I = 0 To 1
   Img = CreateImage(#PB_Any, 256, 16)
   StartDrawing(ImageOutput(Img) )
      Box(0, 0, 256, 16, #Blue)
      DrawText(0, 0, "Cliquez sur ce message...", #White, #Blue)
   StopDrawing()
   If I = 1
      RotateImage(Img) ; *************
   EndIf
   WW = ImageWidth(Img)
   HH = ImageHeight(Img)
   OpenWindow(0, 0, 0, WW, HH, "", $80000001)
   ImageGadget(0, 0, 0, WW, HH, ImageID(Img) )
   Repeat
   Until WaitWindowEvent() = #PB_Event_Gadget
   CloseWindow(0)
   FreeImage(Img)
Next I
End
Post Reply