Xojo Conferences

Platforms to show: All Mac Windows Linux Cross-Platform

/MacCG/CoreGraphics PDF/PDF with paths and gradient
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 with paths and gradient
This example is the version from Sun, 23th Sep 2017.
Project "PDF with paths and gradient.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 kSize, kHalfSize, kTenthSize as Single dim location(5) as CGPointMBS dim rgbBlue(2) as single dim rgbwhite(2) as single rgbBlue(0)=0.4 rgbBlue(1)=0.7 rgbBlue(2)=1.0 rgbwhite(0)=1.0 rgbwhite(1)=1.0 rgbwhite(2)=1.0 const kMediaHeight=100.0 const kMediaWidth=100.0 f=SpecialFolder.Desktop.Child("RB Path Demo2.pdf") r=CGMakeRectMBS(0,0,kMediaWidth,kMediaHeight) // Create the context c=CGNewPDFDocumentMBS(f,r,"PDF from Xojo","Christian Schmitz","MBS Plugin 2.7") if c<>nil then kSize=kMediaHeight*0.9 kHalfSize=kSize*0.5 kTenthSize=kSize*0.1 location(0)=CGMakePointMBS(kTenthSize,0) location(1)=CGMakePointMBS(kHalfSize,kSize-kTenthSize) location(2)=CGMakePointMBS(kSize-kTenthSize,0) location(3)=CGMakePointMBS(0,kHalfSize) location(4)=CGMakePointMBS(kSize,kHalfSize) location(5)=CGMakePointMBS(kTenthSize,0) // We must begin a new page before drawing to a PDF context c.BeginPage(r) // // Just for fun, skew the star slightly by rotating // the CTM a little bit. // // Since the rotation will otherwise cause the tip of // one edge of the star to be clipped, we translate // the CTM ten units horizontally to make sure the // entire star is visible in the resulting image. // c.TranslateCTM(10,0) c.RotateCTM(0.1) // Clip to the star's interior. // // Normally, we'd save the graphics state before // clipping, and restore after drawing, to preserve // the current clipping path. In this case, we leave // it clipped to the interior of the star. When we // stroke the outline below, only the interior of // the stroke line is painted, making the star appear // less substantial. c.AddLines location c.EOClip // Fill the clipped area and restore the clipping path PaintCircularGradient c,kMediaWidth*0.5, kMediaWidth*0.5, kMediaWidth, rgbwhite, rgbBlue, 2 // FillPath and EOFillPath always clear the path, // so we need to add the lines again to draw the star outline c.AddLines location c.StrokePath // 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 blog