Xojo Conferences

Platforms to show: All Mac Windows Linux Cross-Platform

/MacCF/ATS/ATSUI Text rendering with highlighing
Required plugins for this example: MBS MacCF Plugin, MBS MacCG Plugin, MBS Util Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCF/ATS/ATSUI Text rendering with highlighing
This example is the version from Sun, 17th Mar 2012.
Project "ATSUI Text rendering with highlighing.rbp"
Filetype image/jpeg
End FileTypes
Class Window1 Inherits Window
EventHandler Sub EnableMenuItems() FileSave.Enable End EventHandler
EventHandler Function MouseDown(X As Integer, Y As Integer) As Boolean t.SetCGContext self.CGContextMBS.Handle t.HighlightText(HighlightX, HighlightY, HighlightStart, HighlightEnd) Return true End EventHandler
EventHandler Sub MouseUp(X As Integer, Y As Integer) t.SetCGContext self.CGContextMBS.Handle t.UnhighlightText(HighlightX, HighlightY, HighlightStart, HighlightEnd) End EventHandler
EventHandler Sub Paint(g As Graphics) draw(self.CGContextMBS, g.Width, g.Height) End EventHandler
Function FileSave() As Boolean dim f as FolderItem dim c as CGPictureContextMBS f=GetSaveFolderItem("image/jpeg","") if f<>nil then c=new CGPictureContextMBS(self.Width,self.Height) // Fill white c.SetRGBFillColor 1,1,1,1 c.FillRect CGMakeRectMBS(0,0,600,800) call draw c,c.BitmapWidth,c.BitmapHeight f.SaveAsJPEG c.CopyPicture end if End Function
Sub Draw(cg as cgcontextMBS, width as integer, height as integer) dim s1,s2,s3 as ATSUStyleMBS dim a as string dim TextBefore as Double dim TextAfter as Double dim Ascent as Double dim Descent as Double dim endLineOffset,offset as integer dim linewidth,linecount as integer dim m as MemoryBlock dim lineheight as double dim p,i as integer dim x,y as Double dim lineend,linestart as integer dim LineBreaks(0) as integer dim LineHeights(0) as integer const kATSUFromTextBeginning=-1 const kATSUToTextEnd=-1 cg.SetRGBFillColor 0,0,0,1 // black cg.SetRGBStrokeColor 0,0,0,1 // black a=TheText a=UnicodeStringMBS(a) s1=new ATSUStyleMBS s1.Create s1.Size=18 s1.bold=true s2=new ATSUStyleMBS s2.Create s2.Size=14 s2.bold=true s3=new ATSUStyleMBS s3.Create s3.Size=12 t=new ATSUTextLayoutMBS t.Create t.SetTextPointerLocation a,kATSUFromTextBeginning,kATSUToTextEnd,len(a) t.SetRunStyle s1,kATSUFromTextBeginning, 36 t.SetRunStyle s2,36, 80 t.SetRunStyle s3,80, kATSUToTextEnd linewidth=width-20 // measure and break each line, thus setting a soft line break that we'll grab back later while offset<len(a) endLineOffset=t.BreakLine(offset,linewidth,true) // it could be that the line width is so narrow that even a single character won't fit. // in that case, ATSUBreakLine returns noErr but endLineOffset returns equal to offset. // if that's the case, let's break out... if endLineOffset=offset then exit end if t.MeasureText offset,endLineOffset-offset,TextBefore, TextAfter, Ascent, Descent // let's get ascent & descent to calculate the line height later. LineBreaks.Append endLineOffset lineheights.Append Descent+Ascent offset=endLineOffset linecount=linecount+1 wend // Where to draw to t.SetCGContext cg.Handle t.SetHighlightingMethod t.kRedrawHighlighting, 1, 1, 1, 1 HighlightStart = 0 HighlightEnd = LineBreaks(1) HighlightX = 2 HighlightY = Height-lineheights(1)+Descent x=2 y=Height-lineheights(1) lineStart=0 for i=1 to linecount lineend=LineBreaks(i) lineheight=lineheights(i) t.DrawText linestart, lineend-lineStart,x,y+Descent y=y-lineheight lineStart = lineEnd next End Sub
Property HighlightEnd As Integer
Property HighlightStart As Integer
Property HighlightX As Double
Property HighlightY As Double
Property t As ATSUTextLayoutMBS
End Class
MenuBar Menu
MenuItem UntitledMenu3 = ""
MenuItem UntitledMenu2 = "File"
MenuItem FileSave = "Save"
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 App Inherits Application
End Class
Module Constants
Const TheText = "Apple Reports First Quarter Results\rRevenue Increases 36 Percent Year-Over-Year \r\rCUPERTINO, California—January 14, 2004—Apple® today announced financial results for its fiscal 2004 first quarter ended December 27, 2003. For the quarter, the Company posted a net profit of $63 million, or $.17 per diluted share. These results compare to a net loss of $8 million, or $.02 per diluted share, in the year-ago quarter. Revenue for the quarter reached a four-year high of $2.006 billion, up 36 percent from the year-ago quarter. Gross margin was 26.7 percent, down from 27.6 percent in the year-ago quarter. International sales accounted for 44 percent of the quarter’s revenue.\r\rThe quarter’s results include an after-tax investment gain of $3 million which increased earnings per diluted share by $.01.\r\rApple shipped 829 thousand Macintosh® units during the quarter, up 12 percent from the year-ago quarter, as well as 733 thousand iPod® units, up 235 percent from the year-ago quarter.\r\r“It was an outstanding quarter for Apple, with double-digit unit and revenue growth and over 730,000 iPods sold,” said Steve Jobs, Apple’s CEO. ""We’re kicking off 2004 with strong momentum, especially for Mac OS X, which is now used by almost 40 percent of our installed base, iPod and the iTunes Music Store, which has a 70 percent share of the legal music download market.”\r\r“We are very pleased to have exceeded our revenue and profit targets for the first quarter,” said Fred Anderson, Apple’s CFO. “Continued strong asset management enabled us to increase cash by $225 million to just under $4.8 billion. Looking ahead to the second quarter of fiscal 2004, we expect our third consecutive quarter of year-over-year double-digit growth in both revenue and earnings, with revenue of about $1.8 billion and earnings per diluted share of $.08 to $.10.”"
End Module
Module DrawCode
End Module
End Project

See also:

Feedback, Comments & Corrections

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

MBS Xojo Plugins