Xojo Conferences

Platforms to show: All Mac Windows Linux Cross-Platform

/MacCG/CoreGraphics PDF/PDF using Save and Restore
Required plugins for this example: MBS MacCG Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCG/CoreGraphics PDF/PDF using Save and Restore
This example is the version from Sun, 23th Sep 2017.
Project "PDF using Save and Restore.rbp"
MenuBar Menu
MenuItem UntitledMenu3 = ""
MenuItem UntitledMenu2 = "File"
MenuItem FileQuit = "Quit"
MenuItem UntitledMenu0 = "Edit"
MenuItem EditUndo = "Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cut"
MenuItem EditCopy = "Copy"
MenuItem EditPaste = "Paste"
MenuItem EditClear = "Clear"
End MenuBar
Class Window1 Inherits Window
EventHandler Sub Open() // Original example can be found on Apple's Website. // Translation from C is quite easy ;-) dim c as CGContextMBS dim f as FolderItem dim r as CGRectMBS dim kFullCircleInRadians as single dim rotation as single dim i as Integer kFullCircleInRadians=3.14159265358979323846 * 2.0 const size=100.0 f=SpecialFolder.Desktop.Child("RB Save&Restore Demo.pdf") r=CGMakeRectMBS(0,0,size,size) // Create the context c=CGNewPDFDocumentMBS(f,r,"PDF from Xojo","Christian Schmitz","MBS Plugin 2.7") if c<>nil then // We must begin a new page before drawing to a PDF context c.BeginPage(r) for i=1 to 50 rotation=i*kFullCircleInRadians/50 // Start a new path c.BeginPath // Save the graphics state so we can start with a // fresh rotation each time c.SaveGState // // Translate the CTM to the center of the page, then // rotate the CTM by our current translation. Note // that order is important -- if you rotate the CTM // before the translation, the translation coordinates // will be rotated. In this example, we want to draw // in the center of the page, so we translate before // we rotate. // c.TranslateCTM size*0.5,size*0.5 c.RotateCTM rotation // // Draw a simple horizontal line // - First, set the current point to 0,0 // - Second, add the horizontal line // (We now have a path consisting of a horizontal line) // - Third, stroke the line to paint it onto the page // c.MoveToPoint 0,0 c.AddLineToPoint size*0.5,0 c.StrokePath // Restore the graphics state c.RestoreGState next // We've finished rendering the page c.EndPage c.Flush c=nil // to force the file to be written as we want to launch it f.Launch else MsgBox "Failed to creator file!" end if quit End EventHandler
Sub PaintCircularGradient(c as cgcontextMBS, x as single, y as single, size as single, fromcolor() as single, tocolor() as single, steper as single) // // PaintCircularGradient // // Fill (with a gradient) a circle of size "size" extending outward in // all directions from the center point "x,y". The gradient is circular, // extending outward from the center of the circle to the edge of the // circle. // dim diffcolor(2) as Single dim index as Integer dim r,g,b as Single dim kFullCircleAngle as Single // // Precalculate the difference between the two colors. // Subtractions tend to be cheap compared to multiplies, // but it's generally good practice to move unchanging // values out of inner loops. (In theory, compilers // should do this for you. In practice, most compilers // do not.) // diffcolor(0)=tocolor(0)-fromcolor(0) diffcolor(1)=tocolor(1)-fromcolor(1) diffcolor(2)=tocolor(2)-fromcolor(2) // 2 is a full circle in radians kFullCircleAngle = 2 * 3.14159265358979323846 index=size while index>0 r=fromcolor(0)+(diffcolor(0)*index/size) g=fromcolor(1)+(diffcolor(1)*index/size) b=fromcolor(2)+(diffcolor(2)*index/size) // // To calculate a blend between two colors, imagine a line with // fixed endpoints. At either end is one solid color, in between // is the range of blended colors between the two solid colors. // To calculate the blend, we multiply the difference between // the two color values by the desired point along the line // (represented here by "index"), divide by the size of the line to // keep the value within the color range, and add the result to the // starting end point. // c.SetRGBFillColor r,g,b,1.0 c.AddArc x, y, index, kFullCircleAngle, 0.0, true c.FillPath indeX=index-steper wend End Sub
End Class
Class App Inherits Application
End Class
End Project

See also:

Feedback, Comments & Corrections

The items on this page are in the following plugins: MBS MacCG Plugin.

MBS Xojo tutorial videos