In the poppy, “Drawingfront ()” slows down drawing a lot, so I don’t want to use it, but without it the height and width of the text will not be applied correctly after changing the font.
Code: Select all
Global DrawingFont
Procedure DrawRotatedText_(x.CGFloat, y.CGFloat, Text.s, Angle.CGFloat, FrontColor=$ffffff, BackColor=0)
Protected.CGFloat r,g,b,a
Protected.i Transform, NSString, Attributes, Color
Protected Size.NSSize, ZeroPoint.NSPoint
CocoaMessage(@Attributes, 0, "NSMutableDictionary dictionaryWithCapacity:", 2)
If DrawingFont
CocoaMessage(0, Attributes, "setValue:", DrawingFont, "forKey:$", @"NSFont")
EndIf
r = Red(FrontColor)/255 : g = Green(FrontColor)/255 : b = Blue(FrontColor)/255 : a = 1
Color = CocoaMessage(0, 0, "NSColor colorWithDeviceRed:@", @r, "green:@", @g, "blue:@", @b, "alpha:@", @a)
CocoaMessage(0, Attributes, "setValue:", Color, "forKey:$", @"NSColor")
r = Red(BackColor)/255 : g = Green(BackColor)/255 : b = Blue(BackColor)/255 : a = 0
Color = CocoaMessage(0, 0, "NSColor colorWithDeviceRed:@", @r, "green:@", @g, "blue:@", @b, "alpha:@", @a)
CocoaMessage(0, Attributes, "setValue:", Color, "forKey:$", @"NSBackgroundColor")
NSString = CocoaMessage(0, 0, "NSString stringWithString:$", @Text)
CocoaMessage(@Size, NSString, "sizeWithAttributes:", Attributes)
CocoaMessage(0, 0, "NSGraphicsContext saveGraphicsState")
y = OutputHeight()-y
Transform = CocoaMessage(0, 0, "NSAffineTransform transform")
CocoaMessage(0, Transform, "translateXBy:@", @x, "yBy:@", @y)
CocoaMessage(0, Transform, "rotateByDegrees:@", @Angle)
x = 0 : y = -Size\height
CocoaMessage(0, Transform, "translateXBy:@", @x, "yBy:@", @y)
CocoaMessage(0, Transform, "concat")
CocoaMessage(0, NSString, "drawAtPoint:@", @ZeroPoint, "withAttributes:", Attributes)
CocoaMessage(0, 0, "NSGraphicsContext restoreGraphicsState")
EndProcedure
Procedure ClipOutput_(x, y, width, height)
Protected Rect.NSRect
Rect\origin\x = x
Rect\origin\y = OutputHeight()-height-y
Rect\size\width = width
Rect\size\height = height
CocoaMessage(0, CocoaMessage(0, 0, "NSBezierPath bezierPathWithRect:@", @Rect), "setClip")
EndProcedure
Macro PB(Function)
Function
EndMacro
Macro ClipOutput(x, y, width, height)
PB(ClipOutput)(x, y, width, height)
ClipOutput_(x, y, width, height)
EndMacro
Macro DrawRotatedText(x, y, Text, Angle, FrontColor=$ffffff, BackColor=0)
DrawRotatedText_(x, y, Text, Angle, FrontColor, BackColor)
EndMacro
Macro DrawingFont(FontID)
DrawingFont = FontID
; PB(DrawingFont)(FontID)
EndMacro
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; example
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LoadFont(0, "arial", 15)
LoadFont(1, "arial", 10)
Procedure draw_gadget(x, y, w, h)
Protected i, ix, iy
ClipOutput(x, y, w, h)
; item of the gadget
For i=0 To 10
ix = i*24
DrawingMode(#PB_2DDrawing_Outlined)
Box(x,y+ix, h,24)
; text of the item
If i=2
DrawingFont(FontID(0))
Else
DrawingFont(FontID(1))
EndIf
Debug TextHeight("a")
DrawingMode(#PB_2DDrawing_Transparent)
DrawRotatedText(x, y+ix+(24-TextHeight("a"))/2, "clip line "+Str(i)+" rotated text", 0, 0)
Next
; frame gadget
DrawingMode(#PB_2DDrawing_Outlined)
Box(x,y, w,h, $FFFF00)
EndProcedure
OpenWindow(0, 0, 0, 300, 300, "sample demonstration fixes clip output", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CreateImage(0, 300, 300, 24, $c0c0c0)
StartDrawing(ImageOutput(0))
; 1 - gadget
draw_gadget(50, 30, 80, 100)
; 2 - gadget
draw_gadget(150, 90, 80, 100)
; 3 - gadget
draw_gadget(50, 180, 80, 100)
StopDrawing()
ImageGadget(0, 0, 0, 200, 200, ImageID(0))
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow