Examine vector path

Mac OSX specific forum
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Examine vector path

Post by wilbert »

Maybe useful for svg creation (or just for fun) :)

Code: Select all

Enumeration
  #kCGPathElementMoveToPoint
  #kCGPathElementAddLineToPoint
  #kCGPathElementAddQuadCurveToPoint
  #kCGPathElementAddCurveToPoint
  #kCGPathElementCloseSubpath
EndEnumeration

Structure CGPointArray
  p.CGPoint[0]
EndStructure

Structure CGPathElement Align #PB_Structure_AlignC
  type.l
  *points.CGPointArray
EndStructure

ImportC ""
  CGContextCopyPath(c)
  CGPathApply(path, *info, function)
  CGPathRelease(path)
EndImport

Procedure.s PointToString(*point.CGPoint)
  CompilerIf SizeOf(Integer) = 8
    ProcedureReturn StrD(*point\x, 2) + "," + StrD(*point\y, 2)
  CompilerElse
    ProcedureReturn StrF(*point\x, 2) + "," + StrF(*point\y, 2)
  CompilerEndIf
EndProcedure  
  
ProcedureC CGPathApplierFunction(*info, *element.CGPathElement)
  Select *element\type
    Case #kCGPathElementMoveToPoint
      Debug "M" + PointToString(*element\points\p[0])
    Case #kCGPathElementAddLineToPoint
      Debug "L" + PointToString(*element\points\p[0])
    Case #kCGPathElementAddQuadCurveToPoint
      Debug "Q" + PointToString(*element\points\p[0]) + "   " + PointToString(*element\points\p[1])
    Case #kCGPathElementAddCurveToPoint
      Debug "C" + PointToString(*element\points\p[0]) + "   " + PointToString(*element\points\p[1]) + "   " + PointToString(*element\points\p[2])
    Case #kCGPathElementCloseSubpath
      Debug "Z"
  EndSelect
EndProcedure

Procedure ExaminePath(c)
  Protected path = CGContextCopyPath(c)
  CGPathApply(path, #Null, @CGPathApplierFunction())
  CGPathRelease(path)      
EndProcedure


; test

w = 600
h = 400

OpenWindow(0, 0, 0, w, h, "vector drawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CanvasGadget(0,0,0,w,h)

c = StartVectorDrawing(CanvasVectorOutput(0))
If c
  
  AddPathCircle(100, 100, 20)
  AddPathCircle(150, 100, 25)
  
  ExaminePath(c)
    
  VectorSourceColor(RGBA(120, 120, 210, 255))
  FillPath()
  
  StopVectorDrawing()
EndIf

Repeat
  Event = WaitWindowEvent()       
Until Event = #PB_Event_CloseWindow
It's a bit strange the output of x64 and x86 isn't identical. :?
On x86 there's an additional curve element which doesn't seem to do anything :shock:
Windows (x64)
Raspberry Pi OS (Arm64)