Platforms to show: All Mac Windows Linux Cross-Platform

/MacCocoa/Print RTF via NSGraphics


Required plugins for this example: MBS MacBase Plugin, MBS Main Plugin, MBS MacCocoa Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCocoa/Print RTF via NSGraphics

This example is the version from Thu, 22th Feb 2017.

Project "Print RTF via NSGraphics.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Const testRTF = "{\rtf1\ansi\ansicpg1252\cocoartf1504\cocoasubrtf810\n{\fonttbl\f0\fswiss\fcharset0 Helvetica;}\n{\colortbl;\red255\green255\blue255;\red164\green8\blue0;}\n{\*\expandedcolortbl;;\csgenericrgb\c64314\c3137\c0;}\n\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0\n\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0\n\n\f0\b\fs24 \cf2 Hello World}"
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action() dim oldGraphics as NSGraphicsMBS = NSGraphicsMBS.graphicsContext dim ps as new PrinterSetup dim g as Graphics = OpenPrinterDialog(ps) if g = nil then Return end if // draw RTF g.DrawLine 100, 100, 200, 100 g.DrawString "Hello World", 100, 100 drawRTF g, testRTF, 100, 120, ps g.DrawLine 100, 120, 200, 120 // replace font g.ForeColor = &c000000 g.DrawRect 100, 200, 400, 200 drawRTF g, testRTF, 100, 200, ps, 400, 200, NSFontMBS.fontWithName("Times", 20) // calc rect g.ForeColor = &cFF0000 dim s as NSSizeMBS = CalcRTF(g, testRTF, 100, 200, 400, 200, NSFontMBS.fontWithName("Times", 20)) g.DrawRect 100, 200, s.Width, s.Height // restore old context if oldGraphics <> nil then oldGraphics.setCurrentContext oldGraphics = nil end if // set dummy to avoid crash when closing print progress dialog SetDummyGraphics // free graphics g = nil SetDummyGraphics End EventHandler
End Control
Control Canvas1 Inherits Canvas
ControlInstance Canvas1 Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect) dim ps as PrinterSetup = nil // nil for non printing // draw RTF g.DrawLine 100, 100, 200, 100 g.DrawString "Hello World", 100, 100 drawRTF g, testRTF, 100, 120, ps g.DrawLine 100, 120, 200, 120 // replace font g.ForeColor = &c000000 g.DrawRect 100, 200, 400, 200 drawRTF g, testRTF, 100, 200, ps, 400, 200, NSFontMBS.fontWithName("Times", 20) // calc rect g.ForeColor = &cFF0000 dim s as NSSizeMBS = CalcRTF(g, testRTF, 100, 200, 400, 200, NSFontMBS.fontWithName("Times", 20)) g.DrawRect 100, 200, s.Width, s.Height End EventHandler
End Control
Control PushButton2 Inherits PushButton
ControlInstance PushButton2 Inherits PushButton
EventHandler Sub Action() canvas1.Invalidate End EventHandler
End Control
Function CalcRTF(g as Graphics, RTF as string, x as Double, y as Double, MaxWidth as integer = 999, MaxHeight as Integer = 999, OverwriteFont as NSFontMBS = nil) As NSSizeMBS dim at as new NSMutableAttributedStringMBS call at.initWithRTF(testRTF) if OverwriteFont <> nil then at.addAttribute(at.NSFontAttributeName, OverwriteFont, new NSRangeMBS(0, at.Length)) end if dim h as integer = g.Handle(g.HandleTypeCGContextRef) dim n as NSGraphicsMBS = NSGraphicsMBS.graphicsContextWithCGContextHandle(h) dim s as new NSSizeMBS(MaxWidth, MaxHeight) dim r as NSRectMBS = n.boundingRectWithSize(at, s) Return r.Size End Function
Sub DrawRTF(g as Graphics, RTF as string, x as Double, y as Double, ps as PrinterSetup, MaxWidth as integer = 999, MaxHeight as Integer = 999, OverwriteFont as NSFontMBS = nil) dim at as new NSMutableAttributedStringMBS call at.initWithRTF(testRTF) if OverwriteFont <> nil then at.addAttribute(at.NSFontAttributeName, OverwriteFont, new NSRangeMBS(0, at.Length)) end if dim h as integer = g.Handle(g.HandleTypeCGContextRef) dim n as NSGraphicsMBS = NSGraphicsMBS.graphicsContextWithCGContextHandle(h) dim s as new NSSizeMBS(MaxWidth, MaxHeight) dim r as NSRectMBS = n.boundingRectWithSize(at, s) dim rHeight as integer = r.Height if ps = nil then r.X = x r.Y = g.Height - y - rHeight else dim PageLeft as integer = ps.PageLeft dim PageTop as integer = ps.PageTop dim PageHeight as integer = ps.PageHeight r.X = x - PageLeft r.y = PageHeight - y + PageTop - rHeight end if n.drawInRect(at, r) n = nil End Sub
Sub SetDummyGraphics() static image as new NSImageMBS(10,10) static g as NSGraphicsMBS = NSGraphicsMBS.graphicsContextWithNSImage(image) g.setCurrentContext End Sub
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
End Project

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


The biggest plugin in space...