lcvectororiginal.pas 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559
  1. unit LCVectorOriginal;
  2. {$mode objfpc}{$H+}
  3. {$modeswitch advancedrecords}
  4. interface
  5. uses
  6. Classes, SysUtils, BGRABitmap, BGRALayerOriginal, fgl, BGRAGradientOriginal, BGRABitmapTypes,
  7. BGRAPen, LCVectorialFill, LCResourceString;
  8. const
  9. InfiniteRect : TRect = (Left: -MaxLongInt; Top: -MaxLongInt; Right: MaxLongInt; Bottom: MaxLongInt);
  10. EmptyTextureId = 0;
  11. DefaultShapeOutlineWidth = 2;
  12. MediumShapeCost = 100;
  13. //not translated because unexpected internal errors are not useful for users
  14. errDuplicateVectorClass = 'Duplicate class name "%1" for vector shape';
  15. errMergeNotAllowed = 'Merge not allowed';
  16. errCannotBeComputedFromShape = 'Cannot be computed from shape';
  17. errFillFieldMismatch = 'Fill field mismatch';
  18. errInvalidStoredPointer = 'Invalid stored pointer';
  19. errUndefinedContainer = 'Undefined container';
  20. errContainerAlreadyAssigned = 'Container already assigned';
  21. errDiffHandlerOnlyDuringUpdate = 'Diff handler expected only between BeginUpdate and EndUpdate';
  22. errUnexpectedNil = 'Unexpected nil value';
  23. errContainerMismatch = 'Container mismatch';
  24. errAlreadyRemovingShape = 'Already removing shape';
  25. errUnableToFindTexture = 'Unable to find texture';
  26. type
  27. TVectorOriginal = class;
  28. ArrayOfBGRABitmap = array of TBGRABitmap;
  29. TVectorShapeDiff = class;
  30. TShapeChangeEvent = procedure(ASender: TObject; ABounds: TRectF; ADiff: TVectorShapeDiff) of object;
  31. TShapeEditingChangeEvent = procedure(ASender: TObject) of object;
  32. TShapeRemoveQuery = procedure(ASender: TObject; var AHandled: boolean) of object;
  33. TRenderBoundsOption = (rboAssumePenFill, rboAssumeBackFill);
  34. TRenderBoundsOptions = set of TRenderBoundsOption;
  35. TVectorShapeField = (vsfPenFill, vsfPenWidth, vsfPenStyle, vsfJoinStyle, vsfBackFill, vsfOutlineFill, vsfOutlineWidth);
  36. TVectorShapeFields = set of TVectorShapeField;
  37. TVectorShapeUsermode = (vsuEdit, vsuCreate, vsuEditPenFill, vsuEditBackFill, vsuEditOutlineFill,
  38. vsuCurveSetAuto, vsuCurveSetCurve, vsuCurveSetAngle,
  39. vsuEditText);
  40. TVectorShapeUsermodes = set of TVectorShapeUsermode;
  41. TVectorShape = class;
  42. TVectorShapes = specialize TFPGList<TVectorShape>;
  43. { TVectorShapeDiff }
  44. TVectorShapeDiff = class
  45. constructor Create(AStartShape: TVectorShape); virtual; abstract;
  46. procedure ComputeDiff(AEndShape: TVectorShape); virtual; abstract;
  47. procedure Apply(AStartShape: TVectorShape); virtual; abstract;
  48. procedure Unapply(AEndShape: TVectorShape); virtual; abstract;
  49. function CanAppend(ADiff: TVectorShapeDiff): boolean; virtual;
  50. procedure Append(ADiff: TVectorShapeDiff); virtual; abstract;
  51. function IsIdentity: boolean; virtual; abstract;
  52. end;
  53. TCustomMultiSelectionDiff = class(TVectorShapeDiff)
  54. protected
  55. function GetShapeCount: integer; virtual; abstract;
  56. function GetShapeId(AIndex: integer): integer; virtual; abstract;
  57. public
  58. property ShapeCount: integer read GetShapeCount;
  59. property ShapeId[AIndex: integer]: integer read GetShapeId;
  60. end;
  61. TVectorShapeDiffList = specialize TFPGList<TVectorShapeDiff>;
  62. TVectorShapeDiffAny = class of TVectorShapeDiff;
  63. { TVectorShapeComposedDiff }
  64. TVectorShapeComposedDiff = class(TVectorShapeDiff)
  65. protected
  66. FDiffs: array of TVectorShapeDiff;
  67. public
  68. constructor Create(ADiffs: TVectorShapeDiffList);
  69. constructor Create(ADiffs: array of TVectorShapeDiff);
  70. constructor Create({%H-}AStartShape: TVectorShape); override;
  71. destructor Destroy; override;
  72. procedure ComputeDiff({%H-}AEndShape: TVectorShape); override;
  73. procedure Apply(AStartShape: TVectorShape); override;
  74. procedure Unapply(AEndShape: TVectorShape); override;
  75. function CanAppend(ADiff: TVectorShapeDiff): boolean; override;
  76. procedure Append(ADiff: TVectorShapeDiff); override;
  77. function IsIdentity: boolean; override;
  78. function GetMultiselection: TCustomMultiSelectionDiff;
  79. end;
  80. { TVectorShapeEmbeddedFillDiff }
  81. TVectorShapeEmbeddedFillDiff = class(TVectorShapeDiff)
  82. protected
  83. FField: TVectorShapeField;
  84. FFillDiff: TCustomVectorialFillDiff;
  85. public
  86. constructor Create(AField: TVectorShapeField; AFillDiff: TCustomVectorialFillDiff);
  87. constructor Create({%H-}AStartShape: TVectorShape); override;
  88. destructor Destroy; override;
  89. procedure ComputeDiff({%H-}AEndShape: TVectorShape); override;
  90. procedure Apply(AStartShape: TVectorShape); override;
  91. procedure Unapply(AEndShape: TVectorShape); override;
  92. function CanAppend(ADiff: TVectorShapeDiff): boolean; override;
  93. procedure Append(ADiff: TVectorShapeDiff); override;
  94. function IsIdentity: boolean; override;
  95. end;
  96. { TVectorShapeCommonDiff }
  97. TVectorShapeCommonDiff = class(TVectorShapeDiff)
  98. protected
  99. FStartPenWidth: single;
  100. FStartPenStyle: TBGRAPenStyle;
  101. FStartOutlineWidth: single;
  102. FStartJoinStyle: TPenJoinStyle;
  103. FEndPenWidth: single;
  104. FEndPenStyle: TBGRAPenStyle;
  105. FEndOutlineWidth: single;
  106. FEndJoinStyle: TPenJoinStyle;
  107. public
  108. constructor Create(AStartShape: TVectorShape); override;
  109. procedure ComputeDiff(AEndShape: TVectorShape); override;
  110. procedure Apply(AStartShape: TVectorShape); override;
  111. procedure Unapply(AEndShape: TVectorShape); override;
  112. procedure Append(ADiff: TVectorShapeDiff); override;
  113. function IsIdentity: boolean; override;
  114. end;
  115. { TVectorShapeCommonFillDiff }
  116. TVectorShapeCommonFillDiff = class(TVectorShapeDiff)
  117. protected
  118. FStartPenFill: TVectorialFill;
  119. FStartBackFill: TVectorialFill;
  120. FStartOutlineFill: TVectorialFill;
  121. FEndPenFill: TVectorialFill;
  122. FEndBackFill: TVectorialFill;
  123. FEndOutlineFill: TVectorialFill;
  124. public
  125. constructor Create(AStartShape: TVectorShape); override;
  126. destructor Destroy; override;
  127. procedure ComputeDiff(AEndShape: TVectorShape); override;
  128. procedure Apply(AStartShape: TVectorShape); override;
  129. procedure Unapply(AEndShape: TVectorShape); override;
  130. procedure Append(ADiff: TVectorShapeDiff); override;
  131. function IsIdentity: boolean; override;
  132. end;
  133. IVectorMultishape = interface
  134. procedure ClearShapes;
  135. procedure AddShape(AShape: TVectorShape);
  136. procedure RemoveShape(AShape: TVectorShape);
  137. function ContainsShape(AShape: TVectorShape): boolean;
  138. function ShapeCount: integer;
  139. function GetShape(AIndex: integer): TVectorShape;
  140. function SetShapes(AShapes: TVectorShapes): boolean;
  141. function FrontShape: TVectorShape;
  142. function BackShape: TVectorShape;
  143. procedure SetOnSelectionChange(AHandler: TNotifyEvent);
  144. function GetOnSelectionChange: TNotifyEvent;
  145. end;
  146. { TVectorShape }
  147. TVectorShape = class
  148. private
  149. FId: integer;
  150. FOnRemoveQuery: TShapeRemoveQuery;
  151. FRenderIteration: integer; // increased at each BeginUpdate
  152. FOnChange: TShapeChangeEvent;
  153. FOnEditingChange: TShapeEditingChangeEvent;
  154. FUpdateCount, FUpdateEditingCount: integer;
  155. FBoundsBeforeUpdate: TRectF;
  156. FPenFill, FBackFill, FOutlineFill: TVectorialFill;
  157. FStoreTexturePointer: boolean;
  158. FStroker: TBGRAPenStroker;
  159. FUsermode: TVectorShapeUsermode;
  160. FContainer: TVectorOriginal;
  161. FRemoving: boolean;
  162. FDiffs: TVectorShapeDiffList;
  163. FFillChangeWithoutUpdate: boolean;
  164. FFillBeforeChangeBounds: TRectF;
  165. function GetIsUpdating: boolean;
  166. procedure SetContainer(AValue: TVectorOriginal);
  167. function GetFill(var AFillVariable: TVectorialFill): TVectorialFill;
  168. procedure SetFill(var AFillVariable: TVectorialFill; AValue: TVectorialFill; AUpdate: boolean);
  169. procedure SetId(AValue: integer);
  170. protected
  171. FPenWidth: single;
  172. FOutlineWidth: single;
  173. procedure BeginEditingUpdate;
  174. procedure EndEditingUpdate;
  175. procedure DoOnChange(ABoundsBefore: TRectF; ADiff: TVectorShapeDiff); virtual;
  176. function GetIsBack: boolean; virtual;
  177. function GetIsFront: boolean; virtual;
  178. function GetPenColor: TBGRAPixel; virtual;
  179. function GetPenWidth: single; virtual;
  180. function GetPenStyle: TBGRAPenStyle; virtual;
  181. function GetJoinStyle: TPenJoinStyle;
  182. function GetBackFill: TVectorialFill; virtual;
  183. function GetPenFill: TVectorialFill; virtual;
  184. function GetOutlineFill: TVectorialFill; virtual;
  185. function GetOutlineWidth: single; virtual;
  186. procedure SetPenColor(AValue: TBGRAPixel); virtual;
  187. procedure SetPenWidth(AValue: single); virtual;
  188. procedure SetPenStyle({%H-}AValue: TBGRAPenStyle); virtual;
  189. procedure SetJoinStyle(AValue: TPenJoinStyle); virtual;
  190. procedure SetBackFill(AValue: TVectorialFill); virtual;
  191. procedure SetPenFill(AValue: TVectorialFill); virtual;
  192. procedure SetOutlineFill(AValue: TVectorialFill); virtual;
  193. procedure SetOutlineWidth(AValue: single); virtual;
  194. procedure SetUsermode(AValue: TVectorShapeUsermode); virtual;
  195. function LoadTexture(AStorage: TBGRACustomOriginalStorage; AName: string): TBGRABitmap;
  196. procedure SaveTexture(AStorage: TBGRACustomOriginalStorage; AName: string; AValue: TBGRABitmap);
  197. procedure LoadFill(AStorage: TBGRACustomOriginalStorage; AObjectName: string; var AValue: TVectorialFill);
  198. procedure SaveFill(AStorage: TBGRACustomOriginalStorage; AObjectName: string; AValue: TVectorialFill);
  199. function ComputeStroke(APoints: ArrayOfTPointF; AClosed: boolean; AStrokeMatrix: TAffineMatrix): ArrayOfTPointF; virtual;
  200. function ComputeStrokeEnvelope(APoints: ArrayOfTPointF; AClosed: boolean; AWidth: single): ArrayOfTPointF; virtual;
  201. function GetStroker: TBGRAPenStroker;
  202. procedure FillChange({%H-}ASender: TObject; var ADiff: TCustomVectorialFillDiff); virtual;
  203. procedure FillBeforeChange({%H-}ASender: TObject); virtual;
  204. procedure UpdateRenderStorage(ARenderBounds: TRect; AImage: TBGRACustomBitmap = nil);
  205. procedure DiscardRenderStorage;
  206. procedure RetrieveRenderStorage(AMatrix: TAffineMatrix; out ARenderBounds: TRect; out AImage: TBGRABitmap);
  207. function CanHaveRenderStorage: boolean;
  208. function AddDiffHandler(AClass: TVectorShapeDiffAny): TVectorShapeDiff;
  209. procedure AddFillDiffHandler(AFill: TVectorialFill; ADiff: TCustomVectorialFillDiff);
  210. function GetDiffHandler(AClass: TVectorShapeDiffAny): TVectorShapeDiff;
  211. function GetIsFollowingMouse: boolean; virtual;
  212. function GetPenVisible(AAssumePenFill: boolean = False): boolean; virtual;
  213. function GetPenVisibleNow: boolean;
  214. function GetBackVisible: boolean; virtual;
  215. function GetOutlineVisible: boolean; virtual;
  216. property Stroker: TBGRAPenStroker read GetStroker;
  217. public
  218. constructor Create(AContainer: TVectorOriginal); virtual;
  219. class function CreateFromStorage(AStorage: TBGRACustomOriginalStorage; AContainer: TVectorOriginal): TVectorShape;
  220. destructor Destroy; override;
  221. procedure BeginUpdate(ADiffHandler: TVectorShapeDiffAny=nil); virtual;
  222. procedure EndUpdate; virtual;
  223. procedure FillFit;
  224. procedure QuickDefine(constref APoint1,APoint2: TPointF); virtual; abstract;
  225. //one of the two Render functions must be overriden
  226. procedure Render(ADest: TBGRABitmap; AMatrix: TAffineMatrix; ADraft: boolean); overload; virtual;
  227. procedure Render(ADest: TBGRABitmap; ARenderOffset: TPoint; AMatrix: TAffineMatrix; ADraft: boolean); overload; virtual;
  228. function GetRenderBounds(ADestRect: TRect; AMatrix: TAffineMatrix; AOptions: TRenderBoundsOptions = []): TRectF; virtual; abstract;
  229. function SuggestGradientBox(AMatrix: TAffineMatrix): TAffineBox; virtual;
  230. function PointInShape(APoint: TPointF): boolean; overload; virtual; abstract;
  231. function PointInShape(APoint: TPointF; ARadius: single): boolean; overload; virtual; abstract;
  232. function PointInBack(APoint: TPointF): boolean; overload; virtual;
  233. function PointInPen(APoint: TPointF): boolean; overload; virtual;
  234. procedure ConfigureCustomEditor(AEditor: TBGRAOriginalEditor); virtual; abstract;
  235. procedure ConfigureEditor(AEditor: TBGRAOriginalEditor); virtual;
  236. procedure LoadFromStorage(AStorage: TBGRACustomOriginalStorage); virtual;
  237. procedure SaveToStorage(AStorage: TBGRACustomOriginalStorage); virtual;
  238. procedure MouseMove({%H-}Shift: TShiftState; {%H-}X, {%H-}Y: single; var {%H-}ACursor: TOriginalEditorCursor; var {%H-}AHandled: boolean); virtual;
  239. procedure MouseDown({%H-}RightButton: boolean; {%H-}Shift: TShiftState; {%H-}X, {%H-}Y: single; var {%H-}ACursor: TOriginalEditorCursor; var {%H-}AHandled: boolean); virtual;
  240. procedure MouseUp({%H-}RightButton: boolean; {%H-}Shift: TShiftState; {%H-}X, {%H-}Y: single; var {%H-}ACursor: TOriginalEditorCursor; var {%H-}AHandled: boolean); virtual;
  241. procedure KeyDown({%H-}Shift: TShiftState; {%H-}Key: TSpecialKey; var {%H-}AHandled: boolean); virtual;
  242. procedure KeyUp({%H-}Shift: TShiftState; {%H-}Key: TSpecialKey; var {%H-}AHandled: boolean); virtual;
  243. procedure KeyPress({%H-}UTF8Key: string; var {%H-}AHandled: boolean); virtual;
  244. procedure BringToFront; virtual;
  245. procedure SendToBack; virtual;
  246. procedure MoveUp(APassNonIntersectingShapes: boolean); virtual;
  247. procedure MoveDown(APassNonIntersectingShapes: boolean); virtual;
  248. procedure Remove;
  249. procedure AlignHorizontally(AAlign: TAlignment; const AMatrix: TAffineMatrix; const ABounds: TRect); virtual;
  250. procedure AlignVertically(AAlign: TTextLayout; const AMatrix: TAffineMatrix; const ABounds: TRect); virtual;
  251. function GetAlignBounds(const ALayoutRect: TRect; const AMatrix: TAffineMatrix): TRectF; virtual;
  252. procedure AlignTransform(const AMatrix: TAffineMatrix); virtual;
  253. function Duplicate: TVectorShape;
  254. class function StorageClassName: RawByteString; virtual; abstract;
  255. function GetIsSlow(const {%H-}AMatrix: TAffineMatrix): boolean; virtual;
  256. function GetGenericCost: integer; virtual;
  257. function GetUsedTextures: ArrayOfBGRABitmap; virtual;
  258. function GetAsMultishape: IVectorMultishape; virtual;
  259. procedure Transform(const AMatrix: TAffineMatrix); virtual;
  260. procedure TransformFrame(const AMatrix: TAffineMatrix); virtual; abstract;
  261. procedure TransformFill(const AMatrix: TAffineMatrix; ABackOnly: boolean); virtual;
  262. function AllowShearTransform: boolean; virtual;
  263. function MultiFields: TVectorShapeFields; virtual;
  264. class function Fields: TVectorShapeFields; virtual;
  265. class function Usermodes: TVectorShapeUsermodes; virtual;
  266. function MultiUsermodes: TVectorShapeUsermodes; virtual;
  267. class function PreferPixelCentered: boolean; virtual;
  268. class function CreateEmpty: boolean; virtual; //create shape even if empty?
  269. property OnChange: TShapeChangeEvent read FOnChange write FOnChange;
  270. property OnEditingChange: TShapeEditingChangeEvent read FOnEditingChange write FOnEditingChange;
  271. property OnRemoveQuery: TShapeRemoveQuery read FOnRemoveQuery write FOnRemoveQuery;
  272. property PenColor: TBGRAPixel read GetPenColor write SetPenColor;
  273. property PenFill: TVectorialFill read GetPenFill write SetPenFill;
  274. property BackFill: TVectorialFill read GetBackFill write SetBackFill;
  275. property OutlineFill: TVectorialFill read GetOutlineFill write SetOutlineFill;
  276. property PenWidth: single read GetPenWidth write SetPenWidth;
  277. property PenStyle: TBGRAPenStyle read GetPenStyle write SetPenStyle;
  278. property OutlineWidth: single read GetOutlineWidth write SetOutlineWidth;
  279. property JoinStyle: TPenJoinStyle read GetJoinStyle write SetJoinStyle;
  280. property Usermode: TVectorShapeUsermode read FUsermode write SetUsermode;
  281. property Container: TVectorOriginal read FContainer write SetContainer;
  282. property IsFront: boolean read GetIsFront;
  283. property IsBack: boolean read GetIsBack;
  284. property IsRemoving: boolean read FRemoving;
  285. property Id: integer read FId write SetId;
  286. property IsFollowingMouse: boolean read GetIsFollowingMouse;
  287. property IsUpdating: boolean read GetIsUpdating;
  288. property BackVisible: boolean read GetBackVisible;
  289. property PenVisible: boolean read GetPenVisibleNow;
  290. property OutlineVisible: boolean read GetOutlineVisible;
  291. end;
  292. TVectorShapeAny = class of TVectorShape;
  293. TVectorOriginalSelectShapeEvent = procedure(ASender: TObject; AShape: TVectorShape; APreviousShape: TVectorShape) of object;
  294. { TVectorOriginalShapeDiff }
  295. TVectorOriginalShapeDiff = class(TBGRAOriginalDiff)
  296. protected
  297. FShapeIndex: integer;
  298. FShapeDiff: TVectorShapeDiff;
  299. function GetShape(AOriginal: TBGRALayerCustomOriginal): TVectorShape;
  300. public
  301. constructor Create(AShapeIndex: integer; AShapeDiff: TVectorShapeDiff);
  302. destructor Destroy; override;
  303. procedure Apply(AOriginal: TBGRALayerCustomOriginal); override;
  304. procedure Unapply(AOriginal: TBGRALayerCustomOriginal); override;
  305. function CanAppend(ADiff: TBGRAOriginalDiff): boolean; override;
  306. procedure Append(ADiff: TBGRAOriginalDiff); override;
  307. function IsIdentity: boolean; override;
  308. end;
  309. { TVectorOriginalShapeRangeDiff }
  310. TVectorOriginalShapeRangeDiff = class(TBGRAOriginalDiff)
  311. protected
  312. FRangeStart: integer;
  313. FShapesBefore, FShapesAfter: TVectorShapes;
  314. FSelectedShapeBefore, FSelectedShapeAfter: integer;
  315. public
  316. constructor Create(ARangeStart: integer; AShapesBefore, AShapesAfter: TVectorShapes;
  317. ASelectedShapeBefore, ASelectedShapeAfter: integer);
  318. destructor Destroy; override;
  319. procedure Apply(AOriginal: TBGRALayerCustomOriginal); override;
  320. procedure Unapply(AOriginal: TBGRALayerCustomOriginal); override;
  321. function CanAppend({%H-}ADiff: TBGRAOriginalDiff): boolean; override;
  322. procedure Append({%H-}ADiff: TBGRAOriginalDiff); override;
  323. function IsIdentity: boolean; override;
  324. end;
  325. { TVectorOriginalMoveShapeToIndexDiff }
  326. TVectorOriginalMoveShapeToIndexDiff = class(TBGRAOriginalDiff)
  327. protected
  328. FFromIndex,FToIndex: array of integer;
  329. FShapeCount: integer;
  330. procedure InternalMove(AOriginal: TBGRALayerCustomOriginal; AFromIndex,AToIndex: array of integer; ASendDiff: boolean);
  331. public
  332. constructor Create(AFromIndex,AToIndex: array of integer);
  333. procedure Apply(AOriginal: TBGRALayerCustomOriginal); overload; override;
  334. procedure Apply(AOriginal: TBGRALayerCustomOriginal; ASendDiff: boolean); overload;
  335. procedure Unapply(AOriginal: TBGRALayerCustomOriginal); override;
  336. function CanAppend(ADiff: TBGRAOriginalDiff): boolean; override;
  337. procedure Append(ADiff: TBGRAOriginalDiff); override;
  338. function IsIdentity: boolean; override;
  339. end;
  340. TVectorOriginalEditor = class;
  341. { TVectorOriginal }
  342. TVectorOriginal = class(TBGRALayerCustomOriginal)
  343. private
  344. procedure MultiSelection_SelectionChange(Sender: TObject);
  345. protected
  346. FShapes: TVectorShapes;
  347. FDeletedShapes: TVectorShapes;
  348. FSelectedShape: TVectorShape;
  349. FMultiselection: TVectorShape;
  350. FFrozenShapesUnderSelection,
  351. FFrozenShapesOverSelection: TBGRABitmap;
  352. FFrozenShapesUnderBounds,
  353. FFrozenShapesOverBounds: TRect;
  354. FFrozenShapesRenderOffset: TPoint;
  355. FFrozenShapesComputed: boolean;
  356. FFrozenShapeMatrix: TAffineMatrix;
  357. FUnfrozenRangeStart, FUnfrozenRangeEnd: integer;
  358. FOnSelectShape: TVectorOriginalSelectShapeEvent;
  359. FTextures: array of record
  360. Bitmap: TBGRABitmap;
  361. Id, Counter: integer;
  362. end;
  363. FTextureCount: integer;
  364. FLastTextureId: integer;
  365. FLastShapeId: integer;
  366. procedure FreeDeletedShapes;
  367. procedure OnShapeChange(ASender: TObject; ABounds: TRectF; ADiff: TVectorShapeDiff);
  368. procedure OnShapeEditingChange({%H-}ASender: TObject);
  369. procedure DiscardFrozenShapes;
  370. function GetShape(AIndex: integer): TVectorShape;
  371. function GetTextureId(ABitmap: TBGRABitmap): integer;
  372. function IndexOfTexture(AId: integer): integer;
  373. procedure AddTextureWithId(ATexture: TBGRABitmap; AId: integer);
  374. procedure ClearTextures;
  375. function GetShapeCount: integer;
  376. function OpenShapeRenderStorage(AShapeIndex: integer; ACreate: boolean): TBGRACustomOriginalStorage;
  377. procedure DiscardUnusedRenderStorage;
  378. function InternalInsertShape(AShape: TVectorShape; AIndex: integer): TRectF;
  379. function InternalInsertShapeRange(AShapes: TVectorShapes; AIndex: integer): TRectF;
  380. function InternalDeleteShapeRange(AStartIndex,ACount: integer): TRectF;
  381. function GetNewShapeId: integer;
  382. public
  383. constructor Create; override;
  384. destructor Destroy; override;
  385. procedure Clear;
  386. function AddTexture(ATexture: TBGRABitmap): integer;
  387. function GetTexture(AId: integer): TBGRABitmap;
  388. procedure DiscardUnusedTextures;
  389. function AddShape(AShape: TVectorShape): integer; overload;
  390. function AddShape(AShape: TVectorShape; AUsermode: TVectorShapeUsermode): integer; overload;
  391. function AddShapes(AShapes: TVectorShapes): integer;
  392. procedure InsertShape(AShape: TVectorShape; AIndex: integer);
  393. procedure InsertShapes(AShapes: TVectorShapes; AIndex: integer);
  394. function RemoveShape(AShape: TVectorShape): boolean;
  395. procedure DeleteShape(AIndex: integer);
  396. procedure DeleteShapeRange(AStartIndex,ACount: integer);
  397. procedure ReplaceShape(AIndex: integer; ANewShape: TVectorShape);
  398. procedure ReplaceShapeRange(AStartIndex: integer; ACountBefore: integer; ANewShapes: TVectorShapes);
  399. function SelectShapes(AShapes: TVectorShapes): boolean;
  400. function SelectShape(AIndex: integer; AToggle: boolean = false): boolean; overload;
  401. function SelectShape(AShape: TVectorShape; AToggle: boolean = false): boolean; overload;
  402. function DeselectShapes: boolean;
  403. procedure DeselectShape(AIndex: integer); overload;
  404. procedure DeselectShape(AShape: TVectorShape); overload;
  405. function GetShapesCost: integer;
  406. function PreferDraftMode(AEditor: TBGRAOriginalEditor; const AMatrix: TAffineMatrix): boolean;
  407. function MouseClick(APoint: TPointF; ARadius: single; AToggle: boolean): boolean;
  408. procedure Render(ADest: TBGRABitmap; ARenderOffset: TPoint; AMatrix: TAffineMatrix; ADraft: boolean); override;
  409. procedure ConfigureEditor(AEditor: TBGRAOriginalEditor); override;
  410. function CreateEditor: TBGRAOriginalEditor; override;
  411. function GetRenderBounds(ADestRect: TRect; {%H-}AMatrix: TAffineMatrix): TRect; overload; override;
  412. function GetRenderBounds(ADestRect: TRect; {%H-}AMatrix: TAffineMatrix; AStartIndex, AEndIndex: integer): TRect; overload;
  413. function GetAlignBounds(ADestRect: TRect; {%H-}AMatrix: TAffineMatrix): TRect;
  414. procedure LoadFromStorage(AStorage: TBGRACustomOriginalStorage); override;
  415. procedure SaveToStorage(AStorage: TBGRACustomOriginalStorage); override;
  416. function IndexOfShape(AShape: TVectorShape): integer;
  417. function FindShapeById(AId: integer): TVectorShape;
  418. procedure MoveShapeToIndex(AFromIndex, AToIndex: integer); overload;
  419. procedure MoveShapeToIndex(AFromIndex, AToIndex: array of integer); overload;
  420. class function StorageClassName: RawByteString; override;
  421. property OnSelectShape: TVectorOriginalSelectShapeEvent read FOnSelectShape write FOnSelectShape;
  422. property SelectedShape: TVectorShape read FSelectedShape;
  423. property ShapeCount: integer read GetShapeCount;
  424. property Shape[AIndex: integer]: TVectorShape read GetShape;
  425. end;
  426. { TVectorOriginalEditor }
  427. TVectorOriginalEditor = class(TBGRAOriginalEditor)
  428. protected
  429. FOriginal: TVectorOriginal;
  430. FLabels: array of record
  431. Coord: TPointF;
  432. Text: string;
  433. HorizAlign: TAlignment;
  434. VertAlign: TTextLayout;
  435. Padding: integer;
  436. end;
  437. function NiceText(ADest: TBGRABitmap; x, y: integer; const ALayoutRect: TRect;
  438. AText: string; AHorizAlign: TAlignment; AVertAlign: TTextLayout;
  439. APadding: integer): TRect;
  440. public
  441. constructor Create(AOriginal: TVectorOriginal);
  442. procedure Clear; override;
  443. function Render(ADest: TBGRABitmap; const ALayoutRect: TRect): TRect; override;
  444. function GetRenderBounds(const ALayoutRect: TRect): TRect; override;
  445. procedure AddLabel(const ACoord: TPointF; AText: string; AHorizAlign: TAlignment; AVertAlign: TTextLayout);
  446. procedure AddLabel(APointIndex: integer; AText: string; AHorizAlign: TAlignment; AVertAlign: TTextLayout);
  447. procedure MouseMove(Shift: TShiftState; ViewX, ViewY: single; out ACursor: TOriginalEditorCursor; out AHandled: boolean); override;
  448. procedure MouseDown(RightButton: boolean; Shift: TShiftState; ViewX, ViewY: single; out ACursor: TOriginalEditorCursor; out AHandled: boolean); override;
  449. procedure MouseUp(RightButton: boolean; {%H-}Shift: TShiftState; {%H-}ViewX, {%H-}ViewY: single; out ACursor: TOriginalEditorCursor; out AHandled: boolean); override;
  450. procedure KeyDown(Shift: TShiftState; Key: TSpecialKey; out AHandled: boolean); override;
  451. procedure KeyUp(Shift: TShiftState; Key: TSpecialKey; out AHandled: boolean); override;
  452. procedure KeyPress(UTF8Key: string; out AHandled: boolean); override;
  453. end;
  454. function MatrixForPixelCentered(const AMatrix: TAffineMatrix): TAffineMatrix;
  455. procedure RegisterVectorShape(AClass: TVectorShapeAny);
  456. function GetVectorShapeByStorageClassName(AName: string): TVectorShapeAny;
  457. var
  458. VectorMultiselectionFactory: TVectorShapeAny;
  459. implementation
  460. uses math, BGRATransform, BGRAFillInfo, BGRAGraphics, BGRAPath, Types,
  461. BGRAText, BGRATextFX;
  462. function MatrixForPixelCentered(const AMatrix: TAffineMatrix): TAffineMatrix;
  463. begin
  464. result := AffineMatrixTranslation(-0.5,-0.5) * AMatrix * AffineMatrixTranslation(0.5,0.5);
  465. end;
  466. var
  467. VectorShapeClasses: array of TVectorShapeAny;
  468. function GetVectorShapeByStorageClassName(AName: string): TVectorShapeAny;
  469. var
  470. i: Integer;
  471. begin
  472. for i := 0 to high(VectorShapeClasses) do
  473. if VectorShapeClasses[i].StorageClassName = AName then exit(VectorShapeClasses[i]);
  474. exit(nil);
  475. end;
  476. procedure RegisterVectorShape(AClass: TVectorShapeAny);
  477. var
  478. i: Integer;
  479. begin
  480. for i := 0 to high(VectorShapeClasses) do
  481. if VectorShapeClasses[i]=AClass then exit;
  482. if Assigned(GetVectorShapeByStorageClassName(AClass.StorageClassName)) then
  483. raise exception.Create(StringReplace(errDuplicateVectorClass, '%1', AClass.StorageClassName, []));
  484. setlength(VectorShapeClasses, length(VectorShapeClasses)+1);
  485. VectorShapeClasses[high(VectorShapeClasses)] := AClass;
  486. end;
  487. { TVectorOriginalMoveShapeToIndexDiff }
  488. type
  489. TMovedShape = record
  490. shape: TVectorShape;
  491. targetIndex: integer;
  492. class operator =(const ms1, ms2: TMovedShape): boolean;
  493. end;
  494. class operator TMovedShape.=(const ms1, ms2: TMovedShape): boolean;
  495. begin
  496. result := (ms1.shape = ms2.shape) and (ms1.targetIndex = ms2.targetIndex);
  497. end;
  498. function CompareMovedShapeTargetIndex(const ms1, ms2: TMovedShape): integer;
  499. begin
  500. result := ms1.targetIndex - ms2.targetIndex;
  501. end;
  502. procedure TVectorOriginalMoveShapeToIndexDiff.InternalMove(AOriginal: TBGRALayerCustomOriginal; AFromIndex,
  503. AToIndex: array of integer; ASendDiff: boolean);
  504. type
  505. TMovedShapeList = specialize TFPGList<TMovedShape>;
  506. var
  507. movedShapes: TMovedShapeList;
  508. ms: TMovedShape;
  509. r: TRectF;
  510. i: Integer;
  511. orig: TVectorOriginal;
  512. begin
  513. if FShapeCount = 0 then exit;
  514. orig := AOriginal as TVectorOriginal;
  515. movedShapes := TMovedShapeList.Create;
  516. for i := 0 to FShapeCount-1 do
  517. begin
  518. ms.shape := orig.Shape[AFromIndex[i]];
  519. ms.targetIndex:= AToIndex[i];
  520. movedShapes.Add(ms);
  521. end;
  522. movedShapes.Sort(@CompareMovedShapeTargetIndex);
  523. if movedShapes[0].targetIndex > orig.IndexOfShape(movedShapes[0].shape) then
  524. begin
  525. for i := movedShapes.Count-1 downto 0 do
  526. orig.FShapes.Move(orig.IndexOfShape(movedShapes[i].shape), movedShapes[i].targetIndex);
  527. end else
  528. for i := 0 to movedShapes.Count-1 do
  529. orig.FShapes.Move(orig.IndexOfShape(movedShapes[i].shape), movedShapes[i].targetIndex);
  530. orig.DiscardFrozenShapes;
  531. r := EmptyRectF;
  532. for i := 0 to movedShapes.Count-1 do
  533. r := r.Union(movedShapes[i].shape.GetRenderBounds(InfiniteRect, AffineMatrixIdentity), true);
  534. movedShapes.Free;
  535. if ASendDiff then orig.NotifyChange(r,self)
  536. else orig.NotifyChange(r);
  537. end;
  538. constructor TVectorOriginalMoveShapeToIndexDiff.Create(AFromIndex,
  539. AToIndex: array of integer);
  540. var
  541. i: Integer;
  542. begin
  543. if length(AFromIndex) <> length(AToIndex) then
  544. raise exception.Create('Dimension mismatch');
  545. FShapeCount:= length(AFromIndex);
  546. setlength(FFromIndex, FShapeCount);
  547. setlength(FToIndex, FShapeCount);
  548. for i := 0 to FShapeCount-1 do
  549. begin
  550. FFromIndex[i] := AFromIndex[i];
  551. FToIndex[i] := AToIndex[i];
  552. end;
  553. end;
  554. procedure TVectorOriginalMoveShapeToIndexDiff.Apply(
  555. AOriginal: TBGRALayerCustomOriginal);
  556. begin
  557. Apply(AOriginal, False);
  558. end;
  559. procedure TVectorOriginalMoveShapeToIndexDiff.Apply(
  560. AOriginal: TBGRALayerCustomOriginal; ASendDiff: boolean);
  561. begin
  562. InternalMove(AOriginal, FFromIndex, FToIndex, ASendDiff);
  563. end;
  564. procedure TVectorOriginalMoveShapeToIndexDiff.Unapply(
  565. AOriginal: TBGRALayerCustomOriginal);
  566. begin
  567. InternalMove(AOriginal, FToIndex, FFromIndex, False);
  568. end;
  569. function TVectorOriginalMoveShapeToIndexDiff.CanAppend(ADiff: TBGRAOriginalDiff): boolean;
  570. var
  571. other: TVectorOriginalMoveShapeToIndexDiff;
  572. i: Integer;
  573. begin
  574. if ADiff is TVectorOriginalMoveShapeToIndexDiff then
  575. begin
  576. other := TVectorOriginalMoveShapeToIndexDiff(ADiff);
  577. if other.FShapeCount <> FShapeCount then exit(false);
  578. for i := 0 to FShapeCount-1 do
  579. if other.FFromIndex[i] <> FToIndex[i] then exit(false);
  580. result := true;
  581. end else
  582. result := false;
  583. end;
  584. procedure TVectorOriginalMoveShapeToIndexDiff.Append(ADiff: TBGRAOriginalDiff);
  585. var
  586. other: TVectorOriginalMoveShapeToIndexDiff;
  587. i: Integer;
  588. begin
  589. if CanAppend(ADiff) then
  590. begin
  591. other := ADiff as TVectorOriginalMoveShapeToIndexDiff;
  592. for i := 0 to FShapeCount-1 do
  593. FToIndex[i] := other.FToIndex[i];
  594. end;
  595. end;
  596. function TVectorOriginalMoveShapeToIndexDiff.IsIdentity: boolean;
  597. var
  598. i: Integer;
  599. begin
  600. for i := 0 to FShapeCount-1 do
  601. if FFromIndex[i] <> FToIndex[i] then
  602. exit(false);
  603. result := true;
  604. end;
  605. { TVectorShapeDiff }
  606. function TVectorShapeDiff.CanAppend(ADiff: TVectorShapeDiff): boolean;
  607. begin
  608. result := (ADiff.ClassType = self.ClassType);
  609. end;
  610. { TVectorShapeCommonFillDiff }
  611. constructor TVectorShapeCommonFillDiff.Create(AStartShape: TVectorShape);
  612. begin
  613. with AStartShape do
  614. begin
  615. if Assigned(FPenFill) and (FPenFill.FillType <> vftNone) then
  616. FStartPenFill := FPenFill.Duplicate;
  617. if Assigned(FBackFill) and (FBackFill.FillType <> vftNone) then
  618. FStartBackFill := FBackFill.Duplicate;
  619. if Assigned(FOutlineFill) and (FOutlineFill.FillType <> vftNone) then
  620. FStartOutlineFill := FOutlineFill.Duplicate;
  621. end;
  622. end;
  623. destructor TVectorShapeCommonFillDiff.Destroy;
  624. begin
  625. FStartPenFill.Free;
  626. FStartBackFill.Free;
  627. FStartOutlineFill.Free;
  628. FEndPenFill.Free;
  629. FEndBackFill.Free;
  630. FEndOutlineFill.Free;
  631. inherited Destroy;
  632. end;
  633. procedure TVectorShapeCommonFillDiff.ComputeDiff(AEndShape: TVectorShape);
  634. begin
  635. with AEndShape do
  636. begin
  637. if Assigned(FPenFill) and (FPenFill.FillType <> vftNone) then
  638. FEndPenFill := FPenFill.Duplicate;
  639. if Assigned(FBackFill) and (FBackFill.FillType <> vftNone) then
  640. FEndBackFill := FBackFill.Duplicate;
  641. if Assigned(FOutlineFill) and (FOutlineFill.FillType <> vftNone) then
  642. FEndOutlineFill := FOutlineFill.Duplicate;
  643. end;
  644. end;
  645. procedure TVectorShapeCommonFillDiff.Apply(AStartShape: TVectorShape);
  646. begin
  647. with AStartShape do
  648. begin
  649. BeginUpdate;
  650. SetFill(FPenFill, FEndPenFill, False);
  651. SetFill(FBackFill, FEndBackFill, False);
  652. SetFill(FOutlineFill, FEndOutlineFill, False);
  653. EndUpdate;
  654. end;
  655. end;
  656. procedure TVectorShapeCommonFillDiff.Unapply(AEndShape: TVectorShape);
  657. begin
  658. with AEndShape do
  659. begin
  660. BeginUpdate;
  661. SetFill(FPenFill, FStartPenFill, False);
  662. SetFill(FBackFill, FStartBackFill, False);
  663. SetFill(FOutlineFill, FStartOutlineFill, False);
  664. EndUpdate;
  665. end;
  666. end;
  667. procedure TVectorShapeCommonFillDiff.Append(ADiff: TVectorShapeDiff);
  668. var
  669. next: TVectorShapeCommonFillDiff;
  670. begin
  671. next := ADiff as TVectorShapeCommonFillDiff;
  672. if Assigned(next.FEndPenFill) then
  673. begin
  674. if FEndPenFill = nil then FEndPenFill := TVectorialFill.Create;
  675. FEndPenFill.Assign(next.FEndPenFill);
  676. end else FreeAndNil(FEndPenFill);
  677. if Assigned(next.FEndBackFill) then
  678. begin
  679. if FEndBackFill = nil then FEndBackFill := TVectorialFill.Create;
  680. FEndBackFill.Assign(next.FEndBackFill);
  681. end else FreeAndNil(FEndBackFill);
  682. if Assigned(next.FEndOutlineFill) then
  683. begin
  684. if FEndOutlineFill = nil then FEndOutlineFill := TVectorialFill.Create;
  685. FEndOutlineFill.Assign(next.FEndOutlineFill);
  686. end else FreeAndNil(FEndOutlineFill);
  687. end;
  688. function TVectorShapeCommonFillDiff.IsIdentity: boolean;
  689. begin
  690. result := TVectorialFill.Equal(FStartPenFill, FEndPenFill) and
  691. TVectorialFill.Equal(FStartBackFill, FEndBackFill) and
  692. TVectorialFill.Equal(FStartOutlineFill, FEndOutlineFill);
  693. end;
  694. { TVectorOriginalShapeRangeDiff }
  695. constructor TVectorOriginalShapeRangeDiff.Create(ARangeStart: integer;
  696. AShapesBefore, AShapesAfter: TVectorShapes;
  697. ASelectedShapeBefore, ASelectedShapeAfter: integer);
  698. var
  699. i: Integer;
  700. begin
  701. FRangeStart := ARangeStart;
  702. FShapesBefore := TVectorShapes.Create;
  703. if Assigned(AShapesBefore) then
  704. for i := 0 to AShapesBefore.Count-1 do
  705. FShapesBefore.Add(AShapesBefore[i].Duplicate);
  706. FSelectedShapeBefore:= ASelectedShapeBefore;
  707. FShapesAfter := TVectorShapes.Create;
  708. if Assigned(AShapesAfter) then
  709. for i := 0 to AShapesAfter.Count-1 do
  710. FShapesAfter.Add(AShapesAfter[i].Duplicate);
  711. FSelectedShapeAfter:= ASelectedShapeAfter;
  712. end;
  713. destructor TVectorOriginalShapeRangeDiff.Destroy;
  714. var
  715. i: Integer;
  716. begin
  717. for i := 0 to FShapesBefore.Count-1 do FShapesBefore[i].Free;
  718. FShapesBefore.Free;
  719. for i := 0 to FShapesAfter.Count-1 do FShapesAfter[i].Free;
  720. FShapesAfter.Free;
  721. inherited Destroy;
  722. end;
  723. procedure TVectorOriginalShapeRangeDiff.Apply(
  724. AOriginal: TBGRALayerCustomOriginal);
  725. var
  726. i: Integer;
  727. rRemove, rInsert: TRectF;
  728. insCopy: TVectorShapes;
  729. begin
  730. with (AOriginal as TVectorOriginal) do
  731. begin
  732. rRemove := InternalDeleteShapeRange(FRangeStart, FShapesBefore.Count);
  733. insCopy := TVectorShapes.Create;
  734. for i := 0 to FShapesAfter.Count-1 do insCopy.Add(FShapesAfter[i].Duplicate);
  735. rInsert := InternalInsertShapeRange(insCopy, FRangeStart);
  736. insCopy.Free;
  737. NotifyChange(TRectF.Union(rRemove,rInsert,True));
  738. SelectShape(FSelectedShapeAfter);
  739. end;
  740. end;
  741. procedure TVectorOriginalShapeRangeDiff.Unapply(
  742. AOriginal: TBGRALayerCustomOriginal);
  743. var
  744. i: Integer;
  745. rRemove, rInsert: TRectF;
  746. insCopy: TVectorShapes;
  747. begin
  748. with (AOriginal as TVectorOriginal) do
  749. begin
  750. rRemove := InternalDeleteShapeRange(FRangeStart, FShapesAfter.Count);
  751. insCopy := TVectorShapes.Create;
  752. for i := 0 to FShapesBefore.Count-1 do insCopy.Add(FShapesBefore[i].Duplicate);
  753. rInsert := InternalInsertShapeRange(insCopy, FRangeStart);
  754. insCopy.Free;
  755. NotifyChange(TRectF.Union(rRemove,rInsert,True));
  756. SelectShape(FSelectedShapeBefore);
  757. end;
  758. end;
  759. function TVectorOriginalShapeRangeDiff.CanAppend(ADiff: TBGRAOriginalDiff): boolean;
  760. begin
  761. result := false;
  762. end;
  763. procedure TVectorOriginalShapeRangeDiff.Append(ADiff: TBGRAOriginalDiff);
  764. begin
  765. raise exception.Create(errMergeNotAllowed);
  766. end;
  767. function TVectorOriginalShapeRangeDiff.IsIdentity: boolean;
  768. begin
  769. result := false;
  770. end;
  771. { TVectorOriginalShapeDiff }
  772. function TVectorOriginalShapeDiff.GetShape(AOriginal: TBGRALayerCustomOriginal): TVectorShape;
  773. procedure UpdateMultiSelection(AOriginal: TVectorOriginal; AMultiDiff: TCustomMultiSelectionDiff);
  774. var
  775. i: Integer;
  776. containedShapes: TVectorShapes;
  777. s, s2: TVectorShape;
  778. begin
  779. containedShapes := TVectorShapes.Create;
  780. for i := 0 to AMultiDiff.ShapeCount-1 do
  781. begin
  782. s2 := AOriginal.FindShapeById(AMultiDiff.ShapeId[i]);
  783. if Assigned(s2) then containedShapes.Add(s2);
  784. end;
  785. AOriginal.SelectShapes(containedShapes);
  786. containedShapes.Free;
  787. end;
  788. var
  789. multiDiff: TCustomMultiSelectionDiff;
  790. orig: TVectorOriginal;
  791. begin
  792. orig := (AOriginal as TVectorOriginal);
  793. if FShapeIndex = -2 then
  794. begin
  795. result := orig.FMultiselection;
  796. if FShapeDiff is TCustomMultiSelectionDiff then
  797. UpdateMultiSelection(orig, TCustomMultiSelectionDiff(FShapeDiff)) else
  798. if FShapeDiff is TVectorShapeComposedDiff then
  799. begin
  800. multiDiff := TVectorShapeComposedDiff(FShapeDiff).GetMultiselection;
  801. if Assigned(multiDiff) then UpdateMultiSelection(orig, multiDiff);
  802. end;
  803. end else
  804. result := orig.Shape[FShapeIndex];
  805. end;
  806. constructor TVectorOriginalShapeDiff.Create(AShapeIndex: integer;
  807. AShapeDiff: TVectorShapeDiff);
  808. begin
  809. FShapeIndex := AShapeIndex;
  810. FShapeDiff := AShapeDiff;
  811. end;
  812. destructor TVectorOriginalShapeDiff.Destroy;
  813. begin
  814. FShapeDiff.Free;
  815. inherited Destroy;
  816. end;
  817. procedure TVectorOriginalShapeDiff.Apply(AOriginal: TBGRALayerCustomOriginal);
  818. begin
  819. FShapeDiff.Apply(GetShape(AOriginal));
  820. end;
  821. procedure TVectorOriginalShapeDiff.Unapply(AOriginal: TBGRALayerCustomOriginal);
  822. begin
  823. FShapeDiff.Unapply(GetShape(AOriginal));
  824. end;
  825. function TVectorOriginalShapeDiff.CanAppend(ADiff: TBGRAOriginalDiff): boolean;
  826. begin
  827. result := (ADiff is TVectorOriginalShapeDiff) and
  828. (TVectorOriginalShapeDiff(ADiff).FShapeIndex = FShapeIndex) and
  829. (FShapeDiff.CanAppend(TVectorOriginalShapeDiff(ADiff).FShapeDiff));
  830. end;
  831. procedure TVectorOriginalShapeDiff.Append(ADiff: TBGRAOriginalDiff);
  832. begin
  833. if CanAppend(ADiff) then
  834. FShapeDiff.Append(TVectorOriginalShapeDiff(ADiff).FShapeDiff)
  835. else
  836. raise exception.Create(errMergeNotAllowed);
  837. end;
  838. function TVectorOriginalShapeDiff.IsIdentity: boolean;
  839. begin
  840. result := FShapeDiff.IsIdentity;
  841. end;
  842. { TVectorShapeCommonDiff }
  843. constructor TVectorShapeCommonDiff.Create(AStartShape: TVectorShape);
  844. begin
  845. with AStartShape do
  846. begin
  847. FStartPenWidth:= PenWidth;
  848. FStartPenStyle:= DuplicatePenStyle(PenStyle);
  849. FStartOutlineWidth:= OutlineWidth;
  850. FStartJoinStyle:= JoinStyle;
  851. end;
  852. end;
  853. procedure TVectorShapeCommonDiff.ComputeDiff(AEndShape: TVectorShape);
  854. begin
  855. with AEndShape do
  856. begin
  857. FEndPenWidth:= PenWidth;
  858. FEndPenStyle:= DuplicatePenStyle(PenStyle);
  859. FEndOutlineWidth:= OutlineWidth;
  860. FEndJoinStyle:= JoinStyle;
  861. end;
  862. end;
  863. procedure TVectorShapeCommonDiff.Apply(AStartShape: TVectorShape);
  864. begin
  865. with AStartShape do
  866. begin
  867. BeginUpdate;
  868. FPenWidth := FEndPenWidth;
  869. Stroker.CustomPenStyle := DuplicatePenStyle(FEndPenStyle);
  870. FOutlineWidth := FEndOutlineWidth;
  871. Stroker.JoinStyle := FEndJoinStyle;
  872. EndUpdate;
  873. end;
  874. end;
  875. procedure TVectorShapeCommonDiff.Unapply(AEndShape: TVectorShape);
  876. begin
  877. with AEndShape do
  878. begin
  879. BeginUpdate;
  880. FPenWidth := FStartPenWidth;
  881. Stroker.CustomPenStyle := DuplicatePenStyle(FStartPenStyle);
  882. FOutlineWidth := FStartOutlineWidth;
  883. Stroker.JoinStyle := FStartJoinStyle;
  884. EndUpdate;
  885. end;
  886. end;
  887. procedure TVectorShapeCommonDiff.Append(ADiff: TVectorShapeDiff);
  888. var
  889. next: TVectorShapeCommonDiff;
  890. begin
  891. next := ADiff as TVectorShapeCommonDiff;
  892. FEndPenWidth:= next.FEndPenWidth;
  893. FEndPenStyle:= DuplicatePenStyle(next.FEndPenStyle);
  894. FEndOutlineWidth:= next.FEndOutlineWidth;
  895. FEndJoinStyle:= next.FEndJoinStyle;
  896. end;
  897. function TVectorShapeCommonDiff.IsIdentity: boolean;
  898. begin
  899. result := (FStartPenWidth = FEndPenWidth) and
  900. PenStyleEqual(FStartPenStyle, FEndPenStyle) and
  901. (FStartOutlineWidth = FEndOutlineWidth) and
  902. (FStartJoinStyle = FEndJoinStyle);
  903. end;
  904. { TVectorShapeEmbeddedFillDiff }
  905. constructor TVectorShapeEmbeddedFillDiff.Create(AField: TVectorShapeField;
  906. AFillDiff: TCustomVectorialFillDiff);
  907. begin
  908. FField := AField;
  909. FFillDiff := AFillDiff;
  910. end;
  911. constructor TVectorShapeEmbeddedFillDiff.Create(AStartShape: TVectorShape);
  912. begin
  913. raise exception.Create(errCannotBeComputedFromShape);
  914. end;
  915. destructor TVectorShapeEmbeddedFillDiff.Destroy;
  916. begin
  917. FFillDiff.Free;
  918. inherited Destroy;
  919. end;
  920. procedure TVectorShapeEmbeddedFillDiff.ComputeDiff(AEndShape: TVectorShape);
  921. begin
  922. raise exception.Create(errCannotBeComputedFromShape);
  923. end;
  924. procedure TVectorShapeEmbeddedFillDiff.Apply(AStartShape: TVectorShape);
  925. begin
  926. case FField of
  927. vsfPenFill: FFillDiff.Apply(AStartShape.PenFill);
  928. vsfBackFill: FFillDiff.Apply(AStartShape.BackFill);
  929. vsfOutlineFill: FFillDiff.Apply(AStartShape.OutlineFill);
  930. end;
  931. end;
  932. procedure TVectorShapeEmbeddedFillDiff.Unapply(AEndShape: TVectorShape);
  933. begin
  934. case FField of
  935. vsfPenFill: FFillDiff.Unapply(AEndShape.PenFill);
  936. vsfBackFill: FFillDiff.Unapply(AEndShape.BackFill);
  937. vsfOutlineFill: FFillDiff.Unapply(AEndShape.OutlineFill);
  938. end;
  939. end;
  940. function TVectorShapeEmbeddedFillDiff.CanAppend(ADiff: TVectorShapeDiff): boolean;
  941. begin
  942. result := (ADiff is TVectorShapeEmbeddedFillDiff) and
  943. (TVectorShapeEmbeddedFillDiff(ADiff).FField = FField) and
  944. FFillDiff.CanAppend(TVectorShapeEmbeddedFillDiff(ADiff).FFillDiff);
  945. end;
  946. procedure TVectorShapeEmbeddedFillDiff.Append(ADiff: TVectorShapeDiff);
  947. var
  948. next: TVectorShapeEmbeddedFillDiff;
  949. begin
  950. next := ADiff as TVectorShapeEmbeddedFillDiff;
  951. if next.FField <> FField then raise exception.Create(errFillFieldMismatch);
  952. FFillDiff.Append(next.FFillDiff);
  953. end;
  954. function TVectorShapeEmbeddedFillDiff.IsIdentity: boolean;
  955. begin
  956. result := FFillDiff.IsIdentity;
  957. end;
  958. { TVectorShapeComposedDiff }
  959. constructor TVectorShapeComposedDiff.Create(ADiffs: TVectorShapeDiffList);
  960. var
  961. i: Integer;
  962. begin
  963. setlength(FDiffs, ADiffs.Count);
  964. for i := 0 to high(FDiffs) do
  965. FDiffs[i] := ADiffs[i];
  966. end;
  967. constructor TVectorShapeComposedDiff.Create(ADiffs: array of TVectorShapeDiff);
  968. var
  969. i: Integer;
  970. begin
  971. setlength(FDiffs, length(ADiffs));
  972. for i := 0 to high(FDiffs) do
  973. FDiffs[i] := ADiffs[i];
  974. end;
  975. constructor TVectorShapeComposedDiff.Create(AStartShape: TVectorShape);
  976. begin
  977. raise exception.Create(errCannotBeComputedFromShape);
  978. end;
  979. destructor TVectorShapeComposedDiff.Destroy;
  980. var
  981. i: Integer;
  982. begin
  983. for i := 0 to high(FDiffs) do
  984. FDiffs[i].Free;
  985. FDiffs := nil;
  986. inherited Destroy;
  987. end;
  988. procedure TVectorShapeComposedDiff.ComputeDiff(AEndShape: TVectorShape);
  989. begin
  990. raise exception.Create(errCannotBeComputedFromShape);
  991. end;
  992. procedure TVectorShapeComposedDiff.Apply(AStartShape: TVectorShape);
  993. var
  994. i: Integer;
  995. begin
  996. AStartShape.BeginUpdate;
  997. for i := 0 to high(FDiffs) do
  998. FDiffs[i].Apply(AStartShape);
  999. AStartShape.EndUpdate;
  1000. end;
  1001. procedure TVectorShapeComposedDiff.Unapply(AEndShape: TVectorShape);
  1002. var
  1003. i: Integer;
  1004. begin
  1005. AEndShape.BeginUpdate;
  1006. for i := high(FDiffs) downto 0 do
  1007. FDiffs[i].Unapply(AEndShape);
  1008. AEndShape.EndUpdate;
  1009. end;
  1010. function TVectorShapeComposedDiff.CanAppend(ADiff: TVectorShapeDiff): boolean;
  1011. var
  1012. next: TVectorShapeComposedDiff;
  1013. i: Integer;
  1014. begin
  1015. if ADiff is TVectorShapeComposedDiff then
  1016. begin
  1017. next := TVectorShapeComposedDiff(ADiff);
  1018. for i := 0 to high(next.FDiffs) do
  1019. if not CanAppend(next.FDiffs[i]) then exit(false);
  1020. result := true;
  1021. end else
  1022. begin
  1023. for i := high(FDiffs) downto 0 do
  1024. if FDiffs[i].CanAppend(ADiff) then exit(true);
  1025. exit(false);
  1026. end;
  1027. end;
  1028. procedure TVectorShapeComposedDiff.Append(ADiff: TVectorShapeDiff);
  1029. var
  1030. next: TVectorShapeComposedDiff;
  1031. i: Integer;
  1032. begin
  1033. if ADiff is TVectorShapeComposedDiff then
  1034. begin
  1035. next := TVectorShapeComposedDiff(ADiff);
  1036. for i := 0 to high(next.FDiffs) do
  1037. Append(next.FDiffs[i]);
  1038. end else
  1039. begin
  1040. for i := high(FDiffs) downto 0 do
  1041. if FDiffs[i].CanAppend(ADiff) then
  1042. begin
  1043. FDiffs[i].Append(ADiff);
  1044. exit;
  1045. end;
  1046. end;
  1047. end;
  1048. function TVectorShapeComposedDiff.IsIdentity: boolean;
  1049. var
  1050. i: Integer;
  1051. begin
  1052. for i := 0 to high(FDiffs) do
  1053. if not FDiffs[i].IsIdentity then exit(false);
  1054. result := true;
  1055. end;
  1056. function TVectorShapeComposedDiff.GetMultiselection: TCustomMultiSelectionDiff;
  1057. var
  1058. i: Integer;
  1059. begin
  1060. for i := 0 to high(FDiffs) do
  1061. if FDiffs[i] is TCustomMultiSelectionDiff then
  1062. exit(TCustomMultiSelectionDiff(FDiffs[i]));
  1063. result := nil;
  1064. end;
  1065. { TVectorOriginalEditor }
  1066. constructor TVectorOriginalEditor.Create(AOriginal: TVectorOriginal);
  1067. begin
  1068. inherited Create;
  1069. FOriginal := AOriginal;
  1070. end;
  1071. procedure TVectorOriginalEditor.Clear;
  1072. begin
  1073. inherited Clear;
  1074. FLabels:= nil;
  1075. end;
  1076. function TVectorOriginalEditor.Render(ADest: TBGRABitmap;
  1077. const ALayoutRect: TRect): TRect;
  1078. var
  1079. i: Integer;
  1080. ptF: TPointF;
  1081. r: Classes.TRect;
  1082. begin
  1083. Result:=inherited Render(ADest, ALayoutRect);
  1084. for i := 0 to high(FLabels) do
  1085. if not isEmptyPointF(FLabels[i].Coord) then
  1086. begin
  1087. ptF := OriginalCoordToView(FLabels[i].Coord);
  1088. r := NiceText(ADest, round(ptF.x),round(ptF.y), ALayoutRect, FLabels[i].Text, FLabels[i].HorizAlign, FLabels[i].VertAlign, FLabels[i].Padding);
  1089. if not IsRectEmpty(r) then
  1090. begin
  1091. if IsRectEmpty(result) then result:= r
  1092. else UnionRect(result, result, r);
  1093. end;
  1094. end;
  1095. end;
  1096. function TVectorOriginalEditor.GetRenderBounds(const ALayoutRect: TRect): TRect;
  1097. var
  1098. i: Integer;
  1099. ptF: TPointF;
  1100. r: Classes.TRect;
  1101. begin
  1102. Result:=inherited GetRenderBounds(ALayoutRect);
  1103. for i := 0 to high(FLabels) do
  1104. if not isEmptyPointF(FLabels[i].Coord) then
  1105. begin
  1106. ptF := OriginalCoordToView(FLabels[i].Coord);
  1107. r := NiceText(nil, round(ptF.x),round(ptF.y), ALayoutRect, FLabels[i].Text, FLabels[i].HorizAlign, FLabels[i].VertAlign, FLabels[i].Padding);
  1108. if not IsRectEmpty(r) then
  1109. begin
  1110. if IsRectEmpty(result) then result:= r
  1111. else UnionRect(result, result, r);
  1112. end;
  1113. end;
  1114. end;
  1115. procedure TVectorOriginalEditor.AddLabel(const ACoord: TPointF; AText: string;
  1116. AHorizAlign: TAlignment; AVertAlign: TTextLayout);
  1117. begin
  1118. setlength(FLabels, length(FLabels)+1);
  1119. with FLabels[high(FLabels)] do
  1120. begin
  1121. Coord := ACoord;
  1122. Text:= AText;
  1123. HorizAlign:= AHorizAlign;
  1124. VertAlign:= AVertAlign;
  1125. Padding := 0;
  1126. end;
  1127. end;
  1128. procedure TVectorOriginalEditor.AddLabel(APointIndex: integer; AText: string;
  1129. AHorizAlign: TAlignment; AVertAlign: TTextLayout);
  1130. begin
  1131. setlength(FLabels, length(FLabels)+1);
  1132. with FLabels[high(FLabels)] do
  1133. begin
  1134. Coord := PointCoord[APointIndex];
  1135. Text:= AText;
  1136. HorizAlign:= AHorizAlign;
  1137. VertAlign:= AVertAlign;
  1138. Padding := round(PointSize);
  1139. end;
  1140. end;
  1141. function TVectorOriginalEditor.NiceText(ADest: TBGRABitmap; x, y: integer;
  1142. const ALayoutRect: TRect; AText: string; AHorizAlign: TAlignment;
  1143. AVertAlign: TTextLayout; APadding: integer): TRect;
  1144. var fx: TBGRATextEffect;
  1145. f: TFont;
  1146. previousClip: TRect;
  1147. shadowRadius: integer;
  1148. begin
  1149. f := TFont.Create;
  1150. f.Name := 'default';
  1151. f.Height := round(PointSize*2.5);
  1152. fx := TBGRATextEffect.Create(AText,f,true);
  1153. if (AVertAlign = tlTop) and (AHorizAlign = taCenter) and (y+APadding+fx.TextSize.cy > ALayoutRect.Bottom) then AVertAlign:= tlBottom;
  1154. if (AVertAlign = tlBottom) and (AHorizAlign = taCenter) and (y-APadding-fx.TextSize.cy < ALayoutRect.Top) then AVertAlign:= tlTop;
  1155. if (AHorizAlign = taLeftJustify) and (AVertAlign = tlCenter) and (x+APadding+fx.TextSize.cx > ALayoutRect.Right) then AHorizAlign:= taRightJustify;
  1156. if (AHorizAlign = taRightJustify) and (AVertAlign = tlCenter) and (x-APadding-fx.TextSize.cx < ALayoutRect.Left) then AHorizAlign:= taLeftJustify;
  1157. if AVertAlign = tlBottom then y := y-APadding-fx.TextSize.cy else
  1158. if AVertAlign = tlCenter then y := y-fx.TextSize.cy div 2 else inc(y,APadding);
  1159. if y+fx.TextSize.cy > ALayoutRect.Bottom then y := ALayoutRect.Bottom-fx.TextSize.cy;
  1160. if y < ALayoutRect.Top then y := ALayoutRect.Top;
  1161. if AHorizAlign = taRightJustify then x := x-APadding-fx.TextSize.cx else
  1162. if AHorizAlign = taCenter then x := x-fx.TextSize.cx div 2 else inc(x,APadding);
  1163. if x+fx.TextSize.cx > ALayoutRect.Right then x := ALayoutRect.Right-fx.TextSize.cx;
  1164. if x < ALayoutRect.Left then x := ALayoutRect.Left;
  1165. shadowRadius:= round(PointSize*0.5);
  1166. result := rect(x,y,x+fx.TextWidth+2*shadowRadius,y+fx.TextHeight+2*shadowRadius);
  1167. if Assigned(ADest) then
  1168. begin
  1169. previousClip := ADest.ClipRect;
  1170. ADest.ClipRect := result;
  1171. if shadowRadius <> 0 then
  1172. fx.DrawShadow(ADest,x+shadowRadius,y+shadowRadius,shadowRadius,BGRABlack);
  1173. fx.DrawOutline(ADest,x,y,BGRABlack);
  1174. fx.Draw(ADest,x,y,BGRAWhite);
  1175. ADest.ClipRect := previousClip;
  1176. end;
  1177. fx.Free;
  1178. f.Free;
  1179. end;
  1180. procedure TVectorOriginalEditor.MouseMove(Shift: TShiftState; ViewX, ViewY: single; out
  1181. ACursor: TOriginalEditorCursor; out AHandled: boolean);
  1182. var
  1183. ptF: TPointF;
  1184. begin
  1185. inherited MouseMove(Shift, ViewX, ViewY, ACursor, AHandled);
  1186. if not AHandled and Assigned(FOriginal) and Assigned(FOriginal.SelectedShape) then
  1187. begin
  1188. ptF := ViewCoordToOriginal(PointF(ViewX,ViewY));
  1189. if GridActive then ptF := SnapToGrid(ptF, False);
  1190. with ptF do FOriginal.SelectedShape.MouseMove(Shift, X,Y, ACursor, AHandled);
  1191. end;
  1192. end;
  1193. procedure TVectorOriginalEditor.MouseDown(RightButton: boolean;
  1194. Shift: TShiftState; ViewX, ViewY: single; out ACursor: TOriginalEditorCursor; out
  1195. AHandled: boolean);
  1196. var
  1197. ptF: TPointF;
  1198. begin
  1199. inherited MouseDown(RightButton, Shift, ViewX, ViewY, ACursor, AHandled);
  1200. if not AHandled and Assigned(FOriginal) and Assigned(FOriginal.SelectedShape) then
  1201. begin
  1202. ptF := ViewCoordToOriginal(PointF(ViewX,ViewY));
  1203. if GridActive then ptF := SnapToGrid(ptF, False);
  1204. with ptF do FOriginal.SelectedShape.MouseDown(RightButton, Shift, X,Y, ACursor, AHandled);
  1205. end;
  1206. end;
  1207. procedure TVectorOriginalEditor.MouseUp(RightButton: boolean;
  1208. Shift: TShiftState; ViewX, ViewY: single; out ACursor: TOriginalEditorCursor; out
  1209. AHandled: boolean);
  1210. var
  1211. ptF: TPointF;
  1212. begin
  1213. inherited MouseUp(RightButton, Shift, ViewX, ViewY, ACursor, AHandled);
  1214. if not AHandled and Assigned(FOriginal) and Assigned(FOriginal.SelectedShape) then
  1215. begin
  1216. ptF := ViewCoordToOriginal(PointF(ViewX,ViewY));
  1217. if GridActive then ptF := SnapToGrid(ptF, False);
  1218. with ptF do FOriginal.SelectedShape.MouseUp(RightButton, Shift, X,Y, ACursor, AHandled);
  1219. end;
  1220. end;
  1221. procedure TVectorOriginalEditor.KeyDown(Shift: TShiftState; Key: TSpecialKey; out
  1222. AHandled: boolean);
  1223. begin
  1224. if Assigned(FOriginal) and Assigned(FOriginal.SelectedShape) then
  1225. begin
  1226. AHandled := false;
  1227. FOriginal.SelectedShape.KeyDown(Shift, Key, AHandled);
  1228. if AHandled then exit;
  1229. if (Key = skReturn) and ([ssShift,ssCtrl,ssAlt]*Shift = []) then
  1230. begin
  1231. FOriginal.DeselectShapes;
  1232. AHandled := true;
  1233. exit;
  1234. end else
  1235. if (Key = skEscape) and ([ssShift,ssCtrl,ssAlt]*Shift = []) and (FOriginal.SelectedShape.Usermode = vsuCreate) then
  1236. begin
  1237. FOriginal.SelectedShape.Remove;
  1238. AHandled:= true;
  1239. end;
  1240. end;
  1241. inherited KeyDown(Shift, Key, AHandled);
  1242. end;
  1243. procedure TVectorOriginalEditor.KeyUp(Shift: TShiftState; Key: TSpecialKey; out
  1244. AHandled: boolean);
  1245. begin
  1246. if Assigned(FOriginal) and Assigned(FOriginal.SelectedShape) then
  1247. begin
  1248. AHandled := false;
  1249. FOriginal.SelectedShape.KeyUp(Shift, Key, AHandled);
  1250. if AHandled then exit;
  1251. end;
  1252. inherited KeyUp(Shift, Key, AHandled);
  1253. end;
  1254. procedure TVectorOriginalEditor.KeyPress(UTF8Key: string; out
  1255. AHandled: boolean);
  1256. begin
  1257. if Assigned(FOriginal) and Assigned(FOriginal.SelectedShape) then
  1258. begin
  1259. AHandled := false;
  1260. FOriginal.SelectedShape.KeyPress(UTF8Key, AHandled);
  1261. if AHandled then exit;
  1262. end;
  1263. inherited KeyPress(UTF8Key, AHandled);
  1264. end;
  1265. { TVectorShape }
  1266. function TVectorShape.GetIsSlow(const AMatrix: TAffineMatrix): boolean;
  1267. begin
  1268. result := false;
  1269. end;
  1270. function TVectorShape.GetGenericCost: integer;
  1271. begin
  1272. if vsfBackFill in Fields then
  1273. begin
  1274. case BackFill.FillType of
  1275. vftGradient: result := 25;
  1276. vftTexture: result := 10;
  1277. vftSolid: result := 4;
  1278. else {vftNone} result := 1;
  1279. end
  1280. end else
  1281. if vsfPenStyle in Fields then
  1282. begin
  1283. if PenStyleEqual(PenStyle, SolidPenStyle) or
  1284. PenStyleEqual(PenStyle, ClearPenStyle) then
  1285. result := 1
  1286. else
  1287. result := 2;
  1288. end else
  1289. result := 1;
  1290. end;
  1291. function TVectorShape.GetUsedTextures: ArrayOfBGRABitmap;
  1292. var
  1293. f: TVectorShapeFields;
  1294. nb: integer;
  1295. begin
  1296. f := Fields;
  1297. setlength(result, 3);
  1298. nb := 0;
  1299. if (vsfBackFill in f) and (BackFill.FillType = vftTexture) then
  1300. begin
  1301. result[nb] := BackFill.Texture;
  1302. inc(nb);
  1303. end;
  1304. if (vsfPenFill in f) and (PenFill.FillType = vftTexture) then
  1305. begin
  1306. result[nb] := PenFill.Texture;
  1307. inc(nb);
  1308. end;
  1309. if (vsfOutlineFill in f) and (OutlineFill.FillType = vftTexture) then
  1310. begin
  1311. result[nb] := OutlineFill.Texture;
  1312. inc(nb);
  1313. end;
  1314. setlength(result, nb);
  1315. end;
  1316. function TVectorShape.GetAsMultishape: IVectorMultishape;
  1317. begin
  1318. result := nil;
  1319. end;
  1320. procedure TVectorShape.Transform(const AMatrix: TAffineMatrix);
  1321. var
  1322. zoom: Single;
  1323. begin
  1324. if IsAffineMatrixIdentity(AMatrix) then exit;
  1325. BeginUpdate;
  1326. TransformFrame(AMatrix);
  1327. TransformFill(AMatrix, False);
  1328. zoom := (VectLen(AMatrix[1,1],AMatrix[2,1])+VectLen(AMatrix[1,2],AMatrix[2,2]))/2;
  1329. if vsfPenWidth in Fields then PenWidth := zoom*PenWidth;
  1330. if vsfOutlineWidth in Fields then OutlineWidth := zoom*OutlineWidth;
  1331. EndUpdate;
  1332. end;
  1333. class function TVectorShape.Fields: TVectorShapeFields;
  1334. begin
  1335. result := [];
  1336. end;
  1337. function TVectorShape.GetJoinStyle: TPenJoinStyle;
  1338. begin
  1339. result := Stroker.JoinStyle;
  1340. end;
  1341. procedure TVectorShape.SetJoinStyle(AValue: TPenJoinStyle);
  1342. begin
  1343. if Stroker.JoinStyle = AValue then exit;
  1344. BeginUpdate(TVectorShapeCommonDiff);
  1345. Stroker.JoinStyle := AValue;
  1346. EndUpdate;
  1347. end;
  1348. procedure TVectorShape.SetUsermode(AValue: TVectorShapeUsermode);
  1349. begin
  1350. if FUsermode=AValue then Exit;
  1351. BeginEditingUpdate;
  1352. FUsermode:=AValue;
  1353. EndEditingUpdate;
  1354. end;
  1355. function TVectorShape.LoadTexture(AStorage: TBGRACustomOriginalStorage;
  1356. AName: string): TBGRABitmap;
  1357. var
  1358. texId: Integer;
  1359. pointerData: RawByteString;
  1360. begin
  1361. if FStoreTexturePointer then
  1362. begin
  1363. result := nil;
  1364. pointerData := AStorage.RawString[AName+'-ptr'];
  1365. if length(pointerData)<>sizeof(result) then
  1366. raise exception.Create(errInvalidStoredPointer);
  1367. move(pointerData[1],result,sizeof(result));
  1368. end else
  1369. if Assigned(Container) then
  1370. begin
  1371. texId := AStorage.Int[AName+'-id'];
  1372. result := Container.GetTexture(texId);
  1373. end else
  1374. raise exception.Create(errUndefinedContainer);
  1375. end;
  1376. procedure TVectorShape.SaveTexture(AStorage: TBGRACustomOriginalStorage;
  1377. AName: string; AValue: TBGRABitmap);
  1378. var
  1379. texId: Integer;
  1380. pointerData: RawByteString;
  1381. begin
  1382. if FStoreTexturePointer then
  1383. begin
  1384. setlength(pointerData, sizeof(AValue));
  1385. move(AValue, pointerData[1], length(pointerData));
  1386. AStorage.RawString[AName+'-ptr'] := pointerData;
  1387. end else
  1388. if Assigned(Container) then
  1389. begin
  1390. texId := Container.GetTextureId(AValue);
  1391. AStorage.Int[AName+'-id'] := texId;
  1392. end else
  1393. raise exception.Create(errUndefinedContainer);
  1394. end;
  1395. procedure TVectorShape.LoadFill(AStorage: TBGRACustomOriginalStorage;
  1396. AObjectName: string; var AValue: TVectorialFill);
  1397. var
  1398. obj: TBGRACustomOriginalStorage;
  1399. tex: TBGRABitmap;
  1400. texOpacity: integer;
  1401. origin, xAxis, yAxis: TPointF;
  1402. grad: TBGRALayerGradientOriginal;
  1403. repetition: TTextureRepetition;
  1404. c: TBGRAPixel;
  1405. begin
  1406. if AValue = nil then
  1407. begin
  1408. AValue := TVectorialFill.Create;
  1409. AValue.OnChange := @FillChange;
  1410. end;
  1411. obj := AStorage.OpenObject(AObjectName+'-fill');
  1412. if obj = nil then
  1413. begin
  1414. c := AStorage.Color[AObjectName+'-color'];
  1415. if c.alpha <> 0 then
  1416. AValue.SetSolid(c);
  1417. exit;
  1418. end;
  1419. try
  1420. case obj.RawString['class'] of
  1421. 'solid': AValue.SetSolid(obj.Color['color']);
  1422. 'texture': begin
  1423. tex := LoadTexture(obj, 'tex');
  1424. origin := obj.PointF['origin'];
  1425. xAxis := obj.PointF['x-axis'];
  1426. yAxis := obj.PointF['y-axis'];
  1427. texOpacity := obj.IntDef['opacity',255];
  1428. if texOpacity < 0 then texOpacity:= 0;
  1429. if texOpacity > 255 then texOpacity:= 255;
  1430. case obj.RawString['repetition'] of
  1431. 'none': repetition := trNone;
  1432. 'repeat-x': repetition := trRepeatX;
  1433. 'repeat-y': repetition := trRepeatY;
  1434. else repetition := trRepeatBoth;
  1435. end;
  1436. AValue.SetTexture(tex, AffineMatrix(xAxis,yAxis,origin), texOpacity, repetition)
  1437. end;
  1438. 'gradient': begin
  1439. grad := TBGRALayerGradientOriginal.Create;
  1440. grad.LoadFromStorage(obj);
  1441. AValue.SetGradient(grad,true);
  1442. end;
  1443. else AValue.Clear;
  1444. end;
  1445. finally
  1446. obj.Free;
  1447. end;
  1448. end;
  1449. procedure TVectorShape.SaveFill(AStorage: TBGRACustomOriginalStorage;
  1450. AObjectName: string; AValue: TVectorialFill);
  1451. var
  1452. obj: TBGRACustomOriginalStorage;
  1453. m: TAffineMatrix;
  1454. ft: TVectorialFillType;
  1455. begin
  1456. AStorage.RemoveObject(AObjectName+'-fill');
  1457. AStorage.RemoveAttribute(AObjectName+'-color');
  1458. if Assigned(AValue) then
  1459. begin
  1460. ft := AValue.FillType;
  1461. if ft = vftSolid then
  1462. begin
  1463. AStorage.Color[AObjectName+'-color'] := AValue.SolidColor;
  1464. exit;
  1465. end else
  1466. if not (ft in [vftTexture,vftGradient]) then exit;
  1467. obj := AStorage.CreateObject(AObjectName+'-fill');
  1468. try
  1469. if ft = vftSolid then
  1470. begin
  1471. obj.RawString['class'] := 'solid';
  1472. obj.Color['color'] := AValue.SolidColor;
  1473. end
  1474. else
  1475. if ft = vftTexture then
  1476. begin
  1477. obj.RawString['class'] := 'texture';
  1478. SaveTexture(obj, 'tex', AValue.Texture);
  1479. m := AValue.TextureMatrix;
  1480. obj.PointF['origin'] := PointF(m[1,3],m[2,3]);
  1481. obj.PointF['x-axis'] := PointF(m[1,1],m[2,1]);
  1482. obj.PointF['y-axis'] := PointF(m[1,2],m[2,2]);
  1483. if AValue.TextureOpacity<>255 then
  1484. obj.Int['opacity'] := AValue.TextureOpacity;
  1485. case AValue.TextureRepetition of
  1486. trNone: obj.RawString['repetition'] := 'none';
  1487. trRepeatX: obj.RawString['repetition'] := 'repeat-x';
  1488. trRepeatY: obj.RawString['repetition'] := 'repeat-y';
  1489. trRepeatBoth: obj.RemoveAttribute('repetition');
  1490. end;
  1491. end else
  1492. if ft = vftGradient then
  1493. begin
  1494. obj.RawString['class'] := 'gradient';
  1495. AValue.Gradient.SaveToStorage(obj);
  1496. end else
  1497. obj.RawString['class'] := 'none';
  1498. finally
  1499. obj.Free;
  1500. end;
  1501. end;
  1502. end;
  1503. class function TVectorShape.Usermodes: TVectorShapeUsermodes;
  1504. begin
  1505. result := [vsuEdit];
  1506. if vsfBackFill in Fields then result += [vsuEditBackFill];
  1507. if vsfPenFill in Fields then result += [vsuEditPenFill];
  1508. if vsfOutlineFill in Fields then result += [vsuEditOutlineFill];
  1509. end;
  1510. function TVectorShape.MultiUsermodes: TVectorShapeUsermodes;
  1511. var
  1512. f: TVectorShapeFields;
  1513. begin
  1514. result := [vsuEdit];
  1515. f := MultiFields;
  1516. if vsfBackFill in f then result += [vsuEditBackFill];
  1517. if vsfPenFill in f then result += [vsuEditPenFill];
  1518. if vsfOutlineFill in f then result += [vsuEditOutlineFill];
  1519. end;
  1520. class function TVectorShape.PreferPixelCentered: boolean;
  1521. begin
  1522. result := true;
  1523. end;
  1524. class function TVectorShape.CreateEmpty: boolean;
  1525. begin
  1526. result := false;
  1527. end;
  1528. procedure TVectorShape.SetContainer(AValue: TVectorOriginal);
  1529. begin
  1530. if FContainer=AValue then Exit;
  1531. if Assigned(FContainer) then raise exception.Create(errContainerAlreadyAssigned);
  1532. FContainer:=AValue;
  1533. end;
  1534. function TVectorShape.GetIsUpdating: boolean;
  1535. begin
  1536. result := FUpdateCount > 0;
  1537. end;
  1538. function TVectorShape.GetOutlineWidth: single;
  1539. begin
  1540. result := FOutlineWidth;
  1541. end;
  1542. function TVectorShape.GetFill(var AFillVariable: TVectorialFill): TVectorialFill;
  1543. begin
  1544. if AFillVariable = nil then
  1545. begin
  1546. AFillVariable := TVectorialFill.Create;
  1547. AFillVariable.OnChange := @FillChange;
  1548. AFillVariable.OnBeforeChange := @FillBeforeChange;
  1549. end;
  1550. result := AFillVariable;
  1551. end;
  1552. procedure TVectorShape.SetFill(var AFillVariable: TVectorialFill;
  1553. AValue: TVectorialFill; AUpdate: boolean);
  1554. var
  1555. sharedTex: TBGRABitmap;
  1556. freeTex: Boolean;
  1557. begin
  1558. if Assigned(AFillVariable) then
  1559. begin
  1560. if AFillVariable.Equals(AValue) then exit;
  1561. end else
  1562. if AValue=nil then exit;
  1563. if not AUpdate then FFillChangeWithoutUpdate := true;
  1564. freeTex := Assigned(AFillVariable) and Assigned(AFillVariable.Texture) and
  1565. not (Assigned(AValue) and (AValue.FillType = vftTexture) and (AValue.Texture = AFillVariable.Texture));
  1566. if AValue = nil then
  1567. begin
  1568. AFillVariable.Clear; //trigger event
  1569. FreeAndNil(AFillVariable);
  1570. end else
  1571. if AValue.FillType = vftTexture then
  1572. begin
  1573. if Assigned(Container) then
  1574. sharedTex := Container.GetTexture(Container.AddTexture(AValue.Texture))
  1575. else
  1576. sharedTex := AValue.Texture;
  1577. GetFill(AFillVariable).SetTexture(sharedTex, AValue.TextureMatrix, AValue.TextureOpacity, AValue.TextureRepetition);
  1578. end else
  1579. GetFill(AFillVariable).Assign(AValue);
  1580. if Assigned(Container) and freeTex then Container.DiscardUnusedTextures;
  1581. if not AUpdate then FFillChangeWithoutUpdate := false;
  1582. end;
  1583. procedure TVectorShape.SetId(AValue: integer);
  1584. begin
  1585. if FId=AValue then Exit;
  1586. FId:=AValue;
  1587. end;
  1588. procedure TVectorShape.SetOutlineWidth(AValue: single);
  1589. begin
  1590. if AValue < 0 then AValue := 0;
  1591. if FOutlineWidth=AValue then Exit;
  1592. BeginUpdate(TVectorShapeCommonDiff);
  1593. FOutlineWidth:=AValue;
  1594. EndUpdate;
  1595. end;
  1596. procedure TVectorShape.SetOutlineFill(AValue: TVectorialFill);
  1597. begin
  1598. SetFill(FOutlineFill, AValue, True);
  1599. end;
  1600. function TVectorShape.GetIsBack: boolean;
  1601. begin
  1602. result := Assigned(Container) and (Container.IndexOfShape(self)=0);
  1603. end;
  1604. function TVectorShape.GetIsFollowingMouse: boolean;
  1605. begin
  1606. result := false;
  1607. end;
  1608. function TVectorShape.GetPenVisible(AAssumePenFill: boolean): boolean;
  1609. var
  1610. f: TVectorShapeFields;
  1611. begin
  1612. f := Fields;
  1613. result := (vsfPenFill in f) and (not PenFill.IsFullyTransparent or AAssumePenFill);
  1614. if result and (vsfPenWidth in f) then result := result and (PenWidth>0);
  1615. if result and (vsfPenStyle in f) then result := result and not IsClearPenStyle(PenStyle);
  1616. end;
  1617. function TVectorShape.GetPenVisibleNow: boolean;
  1618. begin
  1619. result := GetPenVisible(False);
  1620. end;
  1621. function TVectorShape.GetBackVisible: boolean;
  1622. begin
  1623. result := (vsfBackFill in Fields) and not BackFill.IsFullyTransparent;
  1624. end;
  1625. function TVectorShape.GetOutlineVisible: boolean;
  1626. begin
  1627. result := (vsfOutlineFill in Fields) and not OutlineFill.IsFullyTransparent and
  1628. (not (vsfOutlineWidth in Fields) or (OutlineWidth > 0));
  1629. end;
  1630. procedure TVectorShape.TransformFill(const AMatrix: TAffineMatrix; ABackOnly: boolean);
  1631. begin
  1632. BeginUpdate;
  1633. if vsfBackFill in Fields then BackFill.Transform(AMatrix);
  1634. if not ABackOnly then
  1635. begin
  1636. if vsfPenFill in Fields then PenFill.Transform(AMatrix);
  1637. if vsfOutlineFill in Fields then OutlineFill.Transform(AMatrix);
  1638. end;
  1639. EndUpdate;
  1640. end;
  1641. function TVectorShape.AllowShearTransform: boolean;
  1642. begin
  1643. result := true;
  1644. end;
  1645. function TVectorShape.MultiFields: TVectorShapeFields;
  1646. begin
  1647. result := Fields;
  1648. end;
  1649. function TVectorShape.GetIsFront: boolean;
  1650. begin
  1651. result := Assigned(Container) and (Container.IndexOfShape(self)=Container.ShapeCount-1);
  1652. end;
  1653. function TVectorShape.GetOutlineFill: TVectorialFill;
  1654. begin
  1655. result := GetFill(FOutlineFill);
  1656. end;
  1657. procedure TVectorShape.BeginUpdate(ADiffHandler: TVectorShapeDiffAny);
  1658. begin
  1659. if FUpdateCount = 0 then
  1660. begin
  1661. FBoundsBeforeUpdate := GetRenderBounds(InfiniteRect, AffineMatrixIdentity);
  1662. Inc(FRenderIteration);
  1663. end;
  1664. inc(FUpdateCount);
  1665. if ADiffHandler<>nil then AddDiffHandler(ADiffHandler);
  1666. end;
  1667. procedure TVectorShape.EndUpdate;
  1668. var
  1669. i: Integer;
  1670. comp: TVectorShapeComposedDiff;
  1671. begin
  1672. if FUpdateCount > 0 then
  1673. begin
  1674. dec(FUpdateCount);
  1675. if FUpdateCount = 0 then
  1676. begin
  1677. if Assigned(FDiffs) and (FDiffs.Count > 0) then
  1678. begin
  1679. for i := 0 to FDiffs.Count-1 do
  1680. FDiffs[i].ComputeDiff(self);
  1681. if FDiffs.Count = 1 then
  1682. DoOnChange(FBoundsBeforeUpdate, FDiffs[0])
  1683. else
  1684. begin
  1685. comp := TVectorShapeComposedDiff.Create(FDiffs);
  1686. DoOnChange(FBoundsBeforeUpdate, comp);
  1687. end;
  1688. FDiffs.Clear;
  1689. end else
  1690. DoOnChange(FBoundsBeforeUpdate, nil);
  1691. end;
  1692. end;
  1693. end;
  1694. procedure TVectorShape.FillFit;
  1695. var
  1696. box: TAffineBox;
  1697. begin
  1698. BeginUpdate;
  1699. box := SuggestGradientBox(AffineMatrixIdentity);
  1700. if vsfPenFill in Fields then PenFill.FitGeometry(box);
  1701. if vsfBackFill in Fields then BackFill.FitGeometry(box);
  1702. if vsfOutlineFill in Fields then OutlineFill.FitGeometry(box);
  1703. EndUpdate;
  1704. end;
  1705. procedure TVectorShape.BeginEditingUpdate;
  1706. begin
  1707. inc(FUpdateEditingCount);
  1708. end;
  1709. procedure TVectorShape.EndEditingUpdate;
  1710. begin
  1711. if FUpdateEditingCount > 0 then
  1712. begin
  1713. dec(FUpdateEditingCount);
  1714. if FUpdateEditingCount = 0 then
  1715. begin
  1716. if Assigned(FOnEditingChange) then
  1717. FOnEditingChange(self);
  1718. end;
  1719. end;
  1720. end;
  1721. procedure TVectorShape.DoOnChange(ABoundsBefore: TRectF; ADiff: TVectorShapeDiff);
  1722. var
  1723. boundsAfter: TRectF;
  1724. begin
  1725. if Assigned(FOnChange) then
  1726. begin
  1727. boundsAfter := GetRenderBounds(InfiniteRect, AffineMatrixIdentity);
  1728. FOnChange(self, boundsAfter.Union(ABoundsBefore, true), ADiff);
  1729. end else
  1730. ADiff.Free;
  1731. end;
  1732. function TVectorShape.GetPenColor: TBGRAPixel;
  1733. begin
  1734. if Assigned(FPenFill) then
  1735. result := FPenFill.SolidColor
  1736. else
  1737. result := BGRAPixelTransparent;
  1738. end;
  1739. function TVectorShape.GetPenWidth: single;
  1740. begin
  1741. result := FPenWidth;
  1742. end;
  1743. function TVectorShape.GetPenStyle: TBGRAPenStyle;
  1744. begin
  1745. result := Stroker.CustomPenStyle;
  1746. end;
  1747. function TVectorShape.GetBackFill: TVectorialFill;
  1748. begin
  1749. result := GetFill(FBackFill);
  1750. end;
  1751. function TVectorShape.GetPenFill: TVectorialFill;
  1752. begin
  1753. result := GetFill(FPenFill);
  1754. end;
  1755. function TVectorShape.ComputeStroke(APoints: ArrayOfTPointF; AClosed: boolean; AStrokeMatrix: TAffineMatrix): ArrayOfTPointF;
  1756. begin
  1757. Stroker.StrokeMatrix := AStrokeMatrix;
  1758. if AClosed then
  1759. result := Stroker.ComputePolygon(APoints, PenWidth)
  1760. else
  1761. result := Stroker.ComputePolyline(APoints, PenWidth, PenColor);
  1762. end;
  1763. function TVectorShape.ComputeStrokeEnvelope(APoints: ArrayOfTPointF;
  1764. AClosed: boolean; AWidth: single): ArrayOfTPointF;
  1765. var
  1766. opt: TBGRAPolyLineOptions;
  1767. begin
  1768. opt := [];
  1769. if AClosed then include(opt, plCycle);
  1770. result := ComputeWidePolyPolylinePoints(APoints, AWidth, BGRABlack, pecRound, pjsMiter, SolidPenStyle, opt);
  1771. end;
  1772. function TVectorShape.GetStroker: TBGRAPenStroker;
  1773. begin
  1774. if FStroker = nil then
  1775. begin
  1776. FStroker := TBGRAPenStroker.Create;
  1777. FStroker.MiterLimit:= 2;
  1778. end;
  1779. result := FStroker;
  1780. end;
  1781. procedure TVectorShape.FillChange(ASender: TObject; var ADiff: TCustomVectorialFillDiff);
  1782. var
  1783. field: TVectorShapeField;
  1784. r: TRectF;
  1785. begin
  1786. r := FFillBeforeChangeBounds;
  1787. FFillBeforeChangeBounds := EmptyRectF;
  1788. if FFillChangeWithoutUpdate then exit;
  1789. //if shape is not being updating, send the fill diff as such
  1790. if not IsUpdating then
  1791. begin
  1792. inc(FRenderIteration);
  1793. if ASender = FPenFill then field := vsfPenFill
  1794. else if ASender = FBackFill then field := vsfBackFill
  1795. else if ASender = FOutlineFill then field := vsfOutlineFill
  1796. else
  1797. begin
  1798. ADiff.Free;
  1799. DoOnChange(r, nil);
  1800. exit;
  1801. end;
  1802. if Assigned(ADiff) then
  1803. begin
  1804. DoOnChange(r, TVectorShapeEmbeddedFillDiff.Create(field, ADiff));
  1805. ADiff := nil;
  1806. end else
  1807. DoOnChange(r, nil);
  1808. end else
  1809. AddFillDiffHandler(ASender as TVectorialFill, ADiff);
  1810. end;
  1811. procedure TVectorShape.FillBeforeChange(ASender: TObject);
  1812. begin
  1813. FFillBeforeChangeBounds := GetRenderBounds(InfiniteRect, AffineMatrixIdentity);
  1814. end;
  1815. procedure TVectorShape.UpdateRenderStorage(ARenderBounds: TRect; AImage: TBGRACustomBitmap);
  1816. var
  1817. obj: TBGRACustomOriginalStorage;
  1818. imgStream: TMemoryStream;
  1819. begin
  1820. if CanHaveRenderStorage then
  1821. begin
  1822. obj := Container.RenderStorage.CreateObject(inttostr(Id));
  1823. obj.Int['iteration'] := FRenderIteration;
  1824. obj.Rectangle['bounds'] := ARenderBounds;
  1825. if Assigned(AImage) then
  1826. begin
  1827. imgStream := TMemoryStream.Create;
  1828. AImage.Serialize(imgStream);
  1829. obj.WriteFile('image.data', imgStream, true, true);
  1830. end else
  1831. obj.RemoveFile('image.data');
  1832. obj.Free;
  1833. end;
  1834. end;
  1835. procedure TVectorShape.DiscardRenderStorage;
  1836. begin
  1837. if CanHaveRenderStorage then
  1838. Container.RenderStorage.RemoveObject(inttostr(Id));
  1839. end;
  1840. procedure TVectorShape.RetrieveRenderStorage(AMatrix: TAffineMatrix; out
  1841. ARenderBounds: TRect; out AImage: TBGRABitmap);
  1842. var
  1843. stream: TMemoryStream;
  1844. shapeStorage: TBGRACustomOriginalStorage;
  1845. begin
  1846. ARenderBounds := EmptyRect;
  1847. AImage := nil;
  1848. if Assigned(Container) and Assigned(Container.RenderStorage) and (Container.RenderStorage.AffineMatrix['last-matrix']=AMatrix) then
  1849. begin
  1850. shapeStorage := Container.RenderStorage.OpenObject(inttostr(Id));
  1851. if Assigned(shapeStorage) then
  1852. begin
  1853. if shapeStorage.Int['iteration'] = FRenderIteration then
  1854. begin
  1855. ARenderBounds := shapeStorage.Rectangle['bounds'];
  1856. stream := TMemoryStream.Create;
  1857. if shapeStorage.ReadFile('image.data', stream) and (stream.Size>0) then
  1858. begin
  1859. stream.Position:= 0;
  1860. AImage := TBGRABitmap.Create;
  1861. AImage.Deserialize(stream);
  1862. end;
  1863. stream.Free;
  1864. end;
  1865. shapeStorage.Free;
  1866. end;
  1867. end;
  1868. end;
  1869. function TVectorShape.CanHaveRenderStorage: boolean;
  1870. begin
  1871. result := (Id <> 0) and Assigned(Container) and Assigned(Container.RenderStorage);
  1872. end;
  1873. function TVectorShape.AddDiffHandler(AClass: TVectorShapeDiffAny): TVectorShapeDiff;
  1874. var
  1875. i: Integer;
  1876. begin
  1877. result := nil;
  1878. if not IsUpdating then
  1879. raise exception.Create(errDiffHandlerOnlyDuringUpdate);
  1880. if Assigned(FOnChange) then
  1881. begin
  1882. if FDiffs = nil then FDiffs := TVectorShapeDiffList.Create;
  1883. for i := 0 to FDiffs.Count-1 do
  1884. if FDiffs[i] is AClass then exit(FDiffs[i]);
  1885. result := AClass.Create(self);
  1886. FDiffs.Add(result);
  1887. end;
  1888. end;
  1889. procedure TVectorShape.AddFillDiffHandler(AFill: TVectorialFill; ADiff: TCustomVectorialFillDiff);
  1890. var
  1891. h: TVectorShapeCommonFillDiff;
  1892. begin
  1893. if Assigned(AFill) and Assigned(ADiff) then
  1894. begin
  1895. //make sure there is a handler for fill diff
  1896. if GetDiffHandler(TVectorShapeCommonFillDiff)=nil then
  1897. begin
  1898. h := AddDiffHandler(TVectorShapeCommonFillDiff) as TVectorShapeCommonFillDiff;
  1899. if Assigned(h) then
  1900. begin
  1901. //handler is initialized with current fill that is already changed
  1902. //so we need to fix the start value using diff
  1903. if AFill = FPenFill then
  1904. begin
  1905. if h.FStartPenFill=nil then h.FStartPenFill := TVectorialFill.Create;
  1906. ADiff.Unapply(h.FStartPenFill)
  1907. end
  1908. else if AFill = FBackFill then
  1909. begin
  1910. if h.FStartBackFill=nil then h.FStartBackFill := TVectorialFill.Create;
  1911. ADiff.Unapply(h.FStartBackFill);
  1912. end
  1913. else if AFill = FOutlineFill then
  1914. begin
  1915. if h.FStartOutlineFill=nil then h.FStartOutlineFill := TVectorialFill.Create;
  1916. ADiff.Unapply(h.FStartOutlineFill);
  1917. end;
  1918. end;
  1919. end;
  1920. end;
  1921. end;
  1922. function TVectorShape.GetDiffHandler(AClass: TVectorShapeDiffAny): TVectorShapeDiff;
  1923. var
  1924. i: Integer;
  1925. begin
  1926. if Assigned(FDiffs) then
  1927. begin
  1928. for i := 0 to FDiffs.Count-1 do
  1929. if FDiffs[i] is AClass then exit(FDiffs[i]);
  1930. end;
  1931. result := nil;
  1932. end;
  1933. procedure TVectorShape.SetPenColor(AValue: TBGRAPixel);
  1934. var
  1935. vf: TVectorialFill;
  1936. begin
  1937. vf := TVectorialFill.CreateAsSolid(AValue);
  1938. PenFill := vf;
  1939. vf.Free;
  1940. end;
  1941. procedure TVectorShape.SetPenWidth(AValue: single);
  1942. begin
  1943. if AValue < 0 then AValue := 0;
  1944. if FPenWidth = AValue then exit;
  1945. BeginUpdate(TVectorShapeCommonDiff);
  1946. FPenWidth := AValue;
  1947. EndUpdate;
  1948. end;
  1949. procedure TVectorShape.SetPenStyle(AValue: TBGRAPenStyle);
  1950. begin
  1951. if PenStyleEqual(AValue, PenStyle) then exit;
  1952. BeginUpdate(TVectorShapeCommonDiff);
  1953. Stroker.CustomPenStyle := AValue;
  1954. EndUpdate;
  1955. end;
  1956. procedure TVectorShape.SetBackFill(AValue: TVectorialFill);
  1957. begin
  1958. SetFill(FBackFill, AValue, True);
  1959. end;
  1960. procedure TVectorShape.SetPenFill(AValue: TVectorialFill);
  1961. begin
  1962. SetFill(FPenFill, AValue, True);
  1963. end;
  1964. constructor TVectorShape.Create(AContainer: TVectorOriginal);
  1965. begin
  1966. FContainer := AContainer;
  1967. FPenFill := nil;
  1968. FPenWidth := 1;
  1969. FOutlineWidth := DefaultShapeOutlineWidth;
  1970. FStroker := nil;
  1971. FOnChange := nil;
  1972. FOnEditingChange := nil;
  1973. FBackFill := nil;
  1974. FOutlineFill := nil;
  1975. FUsermode:= vsuEdit;
  1976. FRemoving:= false;
  1977. FId := 0;
  1978. FRenderIteration:= 0;
  1979. FFillBeforeChangeBounds := EmptyRectF;
  1980. end;
  1981. class function TVectorShape.CreateFromStorage(
  1982. AStorage: TBGRACustomOriginalStorage; AContainer: TVectorOriginal): TVectorShape;
  1983. var
  1984. objClassName: RawByteString;
  1985. shapeClass: TVectorShapeAny;
  1986. begin
  1987. objClassName := AStorage.RawString['class'];
  1988. if objClassName = '' then raise exception.Create(rsShapeClassNotSpecified);
  1989. shapeClass:= GetVectorShapeByStorageClassName(objClassName);
  1990. if shapeClass = nil then raise exception.Create(StringReplace(rsUnknownShapeClass, '%1', objClassName, []));
  1991. result := shapeClass.Create(AContainer);
  1992. result.LoadFromStorage(AStorage);
  1993. end;
  1994. destructor TVectorShape.Destroy;
  1995. var
  1996. i: Integer;
  1997. begin
  1998. FreeAndNil(FStroker);
  1999. FreeAndNil(FPenFill);
  2000. FreeAndNil(FBackFill);
  2001. FreeAndNil(FOutlineFill);
  2002. if Assigned(FDiffs) then
  2003. for i := 0 to FDiffs.Count-1 do
  2004. FDiffs[i].Free;
  2005. FreeAndNil(FDiffs);
  2006. inherited Destroy;
  2007. end;
  2008. procedure TVectorShape.Render(ADest: TBGRABitmap; AMatrix: TAffineMatrix;
  2009. ADraft: boolean);
  2010. begin
  2011. Render(ADest, Point(0,0), AMatrix, ADraft);
  2012. end;
  2013. procedure TVectorShape.Render(ADest: TBGRABitmap; ARenderOffset: TPoint;
  2014. AMatrix: TAffineMatrix; ADraft: boolean);
  2015. begin
  2016. Render(ADest, AffineMatrixTranslation(ARenderOffset.X,ARenderOffset.Y)*AMatrix, ADraft);
  2017. end;
  2018. function TVectorShape.SuggestGradientBox(AMatrix: TAffineMatrix): TAffineBox;
  2019. var
  2020. rF: TRectF;
  2021. begin
  2022. rF := GetRenderBounds(InfiniteRect, AMatrix, [rboAssumeBackFill]);
  2023. result := TAffineBox.AffineBox(rF);
  2024. end;
  2025. function TVectorShape.PointInBack(APoint: TPointF): boolean;
  2026. begin
  2027. result := false;
  2028. end;
  2029. function TVectorShape.PointInPen(APoint: TPointF): boolean;
  2030. begin
  2031. result := false;
  2032. end;
  2033. procedure TVectorShape.ConfigureEditor(AEditor: TBGRAOriginalEditor);
  2034. begin
  2035. if (Usermode = vsuEditBackFill) and BackFill.IsEditable then
  2036. BackFill.ConfigureEditor(AEditor)
  2037. else
  2038. if (Usermode = vsuEditPenFill) and PenFill.IsEditable then
  2039. PenFill.ConfigureEditor(AEditor)
  2040. else
  2041. if (Usermode = vsuEditOutlineFill) and OutlineFill.IsEditable then
  2042. OutlineFill.ConfigureEditor(AEditor)
  2043. else
  2044. ConfigureCustomEditor(AEditor);
  2045. end;
  2046. procedure TVectorShape.LoadFromStorage(AStorage: TBGRACustomOriginalStorage);
  2047. var
  2048. f: TVectorShapeFields;
  2049. begin
  2050. f := Fields;
  2051. if f <> [] then
  2052. begin
  2053. BeginUpdate;
  2054. Id := AStorage.Int['id'];
  2055. FRenderIteration := AStorage.Int['iteration'];
  2056. if vsfPenFill in f then LoadFill(AStorage, 'pen', FPenFill);
  2057. if vsfPenWidth in f then PenWidth := AStorage.FloatDef['pen-width', 0];
  2058. if vsfPenStyle in f then PenStyle := AStorage.FloatArray['pen-style'];
  2059. if vsfJoinStyle in f then
  2060. case AStorage.RawString['join-style'] of
  2061. 'round': JoinStyle := pjsRound;
  2062. 'bevel': JoinStyle := pjsBevel;
  2063. else JoinStyle := pjsMiter;
  2064. end;
  2065. if vsfBackFill in f then LoadFill(AStorage, 'back', FBackFill);
  2066. if vsfOutlineFill in f then LoadFill(AStorage, 'outline', FOutlineFill);
  2067. if vsfOutlineWidth in f then OutlineWidth := AStorage.FloatDef['outline-width', DefaultShapeOutlineWidth];
  2068. EndUpdate;
  2069. end;
  2070. end;
  2071. procedure TVectorShape.SaveToStorage(AStorage: TBGRACustomOriginalStorage);
  2072. var
  2073. f: TVectorShapeFields;
  2074. begin
  2075. AStorage.Int['id'] := Id;
  2076. AStorage.Int['iteration'] := FRenderIteration;
  2077. f := Fields;
  2078. if vsfPenFill in f then SaveFill(AStorage, 'pen', FPenFill);
  2079. if vsfPenWidth in f then AStorage.Float['pen-width'] := PenWidth;
  2080. if vsfPenStyle in f then AStorage.FloatArray['pen-style'] := PenStyle;
  2081. if vsfJoinStyle in f then
  2082. case JoinStyle of
  2083. pjsRound: AStorage.RawString['join-style'] := 'round';
  2084. pjsBevel: AStorage.RawString['join-style'] := 'bevel';
  2085. else AStorage.RawString['join-style'] := 'miter';
  2086. end;
  2087. if vsfBackFill in f then SaveFill(AStorage, 'back', FBackFill);
  2088. if OutlineVisible then
  2089. begin
  2090. if vsfOutlineFill in f then SaveFill(AStorage, 'outline', FOutlineFill);
  2091. if vsfOutlineWidth in f then AStorage.Float['outline-width'] := FOutlineWidth
  2092. else AStorage.RemoveAttribute('outline-width');
  2093. end else
  2094. begin
  2095. AStorage.RemoveObject('outline-fill');
  2096. AStorage.RemoveAttribute('outline-color');
  2097. AStorage.RemoveAttribute('outline-width');
  2098. end;
  2099. end;
  2100. procedure TVectorShape.MouseMove(Shift: TShiftState; X, Y: single; var
  2101. ACursor: TOriginalEditorCursor; var AHandled: boolean);
  2102. begin
  2103. //nothing
  2104. end;
  2105. procedure TVectorShape.MouseDown(RightButton: boolean; Shift: TShiftState; X,
  2106. Y: single; var ACursor: TOriginalEditorCursor; var AHandled: boolean);
  2107. begin
  2108. //nothing
  2109. end;
  2110. procedure TVectorShape.MouseUp(RightButton: boolean; Shift: TShiftState; X,
  2111. Y: single; var ACursor: TOriginalEditorCursor; var AHandled: boolean);
  2112. begin
  2113. //nothing
  2114. end;
  2115. procedure TVectorShape.KeyDown(Shift: TShiftState; Key: TSpecialKey;
  2116. var AHandled: boolean);
  2117. begin
  2118. //nothing
  2119. end;
  2120. procedure TVectorShape.KeyUp(Shift: TShiftState; Key: TSpecialKey;
  2121. var AHandled: boolean);
  2122. begin
  2123. //nothing
  2124. end;
  2125. procedure TVectorShape.KeyPress(UTF8Key: string; var AHandled: boolean);
  2126. begin
  2127. //nothing
  2128. end;
  2129. procedure TVectorShape.BringToFront;
  2130. begin
  2131. if Assigned(Container) then
  2132. Container.MoveShapeToIndex(Container.IndexOfShape(self),Container.ShapeCount-1);
  2133. end;
  2134. procedure TVectorShape.SendToBack;
  2135. begin
  2136. if Assigned(Container) then
  2137. Container.MoveShapeToIndex(Container.IndexOfShape(self),0);
  2138. end;
  2139. procedure TVectorShape.MoveUp(APassNonIntersectingShapes: boolean);
  2140. var
  2141. movedShapeBounds, otherShapeBounds: TRectF;
  2142. sourceIdx,idx: integer;
  2143. begin
  2144. if not Assigned(Container) then exit;
  2145. sourceIdx := Container.IndexOfShape(self);
  2146. if sourceIdx = Container.ShapeCount-1 then exit;
  2147. idx := sourceIdx;
  2148. if APassNonIntersectingShapes then
  2149. begin
  2150. movedShapeBounds := self.GetAlignBounds(InfiniteRect, AffineMatrixIdentity);
  2151. while idx < Container.ShapeCount-2 do
  2152. begin
  2153. otherShapeBounds := Container.Shape[idx+1].GetAlignBounds(InfiniteRect, AffineMatrixIdentity);
  2154. if movedShapeBounds.IntersectsWith(otherShapeBounds) then break;
  2155. inc(idx);
  2156. end;
  2157. end;
  2158. inc(idx);
  2159. Container.MoveShapeToIndex(sourceIdx, idx);
  2160. end;
  2161. procedure TVectorShape.MoveDown(APassNonIntersectingShapes: boolean);
  2162. var
  2163. movedShapeBounds, otherShapeBounds: TRectF;
  2164. sourceIdx,idx: integer;
  2165. begin
  2166. if not Assigned(Container) then exit;
  2167. sourceIdx := Container.IndexOfShape(self);
  2168. if sourceIdx = 0 then exit;
  2169. idx := sourceIdx;
  2170. if APassNonIntersectingShapes then
  2171. begin
  2172. movedShapeBounds := self.GetAlignBounds(InfiniteRect, AffineMatrixIdentity);
  2173. while idx > 1 do
  2174. begin
  2175. otherShapeBounds := Container.Shape[idx-1].GetAlignBounds(InfiniteRect, AffineMatrixIdentity);
  2176. if movedShapeBounds.IntersectsWith(otherShapeBounds) then break;
  2177. dec(idx);
  2178. end;
  2179. end;
  2180. dec(idx);
  2181. Container.MoveShapeToIndex(sourceIdx, idx);
  2182. end;
  2183. procedure TVectorShape.Remove;
  2184. var handled: boolean;
  2185. begin
  2186. if Assigned(OnRemoveQuery) then
  2187. begin
  2188. handled := false;
  2189. OnRemoveQuery(self, handled);
  2190. if handled then exit;
  2191. end;
  2192. if Assigned(Container) then Container.RemoveShape(self)
  2193. else raise exception.Create(errUndefinedContainer);
  2194. end;
  2195. procedure TVectorShape.AlignHorizontally(AAlign: TAlignment;
  2196. const AMatrix: TAffineMatrix; const ABounds: TRect);
  2197. var
  2198. sb: TRectF;
  2199. m: TAffineMatrix;
  2200. begin
  2201. sb := GetAlignBounds(ABounds, AMatrix);
  2202. case AAlign of
  2203. taRightJustify: m := AffineMatrixTranslation(ABounds.Right-sb.Right,0);
  2204. taCenter: m := AffineMatrixTranslation((ABounds.Left+ABounds.Right-sb.Left-sb.Right)/2,0);
  2205. else m := AffineMatrixTranslation(ABounds.Left-sb.Left,0);
  2206. end;
  2207. AlignTransform(AffineMatrixInverse(AMatrix)*m*AMatrix);
  2208. end;
  2209. procedure TVectorShape.AlignVertically(AAlign: TTextLayout;
  2210. const AMatrix: TAffineMatrix; const ABounds: TRect);
  2211. var
  2212. sb: TRectF;
  2213. m: TAffineMatrix;
  2214. begin
  2215. sb := GetAlignBounds(ABounds, AMatrix);
  2216. case AAlign of
  2217. tlBottom: m := AffineMatrixTranslation(0,ABounds.Bottom-sb.Bottom);
  2218. tlCenter: m := AffineMatrixTranslation(0,(ABounds.Top+ABounds.Bottom-sb.Top-sb.Bottom)/2);
  2219. else m := AffineMatrixTranslation(0,ABounds.Top-sb.Top);
  2220. end;
  2221. AlignTransform(AffineMatrixInverse(AMatrix)*m*AMatrix);
  2222. end;
  2223. function TVectorShape.GetAlignBounds(const ALayoutRect: TRect;
  2224. const AMatrix: TAffineMatrix): TRectF;
  2225. begin
  2226. result := GetRenderBounds(ALayoutRect, AMatrix, []);
  2227. end;
  2228. procedure TVectorShape.AlignTransform(const AMatrix: TAffineMatrix);
  2229. begin
  2230. Transform(AMatrix);
  2231. end;
  2232. function TVectorShape.Duplicate: TVectorShape;
  2233. var temp: TBGRAMemOriginalStorage;
  2234. shapeClass: TVectorShapeAny;
  2235. begin
  2236. shapeClass:= GetVectorShapeByStorageClassName(StorageClassName);
  2237. if shapeClass = nil then raise exception.Create(StringReplace(rsUnknownShapeClass, '%1', StorageClassName, []));
  2238. result := nil;
  2239. temp := TBGRAMemOriginalStorage.Create;
  2240. FStoreTexturePointer:= true;
  2241. try
  2242. SaveToStorage(temp);
  2243. result := shapeClass.Create(nil);
  2244. result.FStoreTexturePointer := true;
  2245. result.LoadFromStorage(temp);
  2246. finally
  2247. temp.Free;
  2248. FStoreTexturePointer:= false;
  2249. if assigned(result) then
  2250. result.FStoreTexturePointer := false;
  2251. end;
  2252. end;
  2253. { TVectorOriginal }
  2254. function TVectorOriginal.GetShapeCount: integer;
  2255. begin
  2256. result := FShapes.Count;
  2257. end;
  2258. function TVectorOriginal.OpenShapeRenderStorage(AShapeIndex: integer; ACreate: boolean): TBGRACustomOriginalStorage;
  2259. var
  2260. shapeId: Integer;
  2261. begin
  2262. if Assigned(RenderStorage) then
  2263. begin
  2264. shapeId := Shape[AShapeIndex].Id;
  2265. if ACreate then
  2266. result := RenderStorage.CreateObject(inttostr(shapeId))
  2267. else
  2268. result := RenderStorage.OpenObject(inttostr(shapeId));
  2269. end
  2270. else
  2271. result := nil;
  2272. end;
  2273. function TVectorOriginal.FindShapeById(AId: integer): TVectorShape;
  2274. var
  2275. i: Integer;
  2276. begin
  2277. for i := 0 to FShapes.Count-1 do
  2278. if FShapes[i].Id = AId then exit(FShapes[i]);
  2279. exit(nil);
  2280. end;
  2281. procedure TVectorOriginal.DiscardUnusedRenderStorage;
  2282. var
  2283. objs: TStringList;
  2284. shapeId, errPos, i: integer;
  2285. begin
  2286. if Assigned(RenderStorage) then
  2287. begin
  2288. objs := TStringList.Create;
  2289. RenderStorage.EnumerateObjects(objs);
  2290. for i := 0 to objs.Count-1 do
  2291. begin
  2292. val(objs[i], shapeId, errPos);
  2293. if errPos = 0 then
  2294. begin
  2295. if FindShapeById(shapeId) = nil then
  2296. RenderStorage.RemoveObject(objs[i]);
  2297. end;
  2298. end;
  2299. objs.Free;
  2300. end;
  2301. end;
  2302. function TVectorOriginal.InternalInsertShape(AShape: TVectorShape;
  2303. AIndex: integer): TRectF;
  2304. var
  2305. texs: ArrayOfBGRABitmap;
  2306. i: Integer;
  2307. begin
  2308. if AShape = nil then raise exception.Create(errUnexpectedNil);
  2309. if AShape.Container <> self then
  2310. begin
  2311. if AShape.Container = nil then
  2312. AShape.Container := self
  2313. else
  2314. raise exception.Create(errContainerMismatch);
  2315. end;
  2316. if (AIndex < 0) or (AIndex > FShapes.Count) then
  2317. raise exception.Create(rsIndexOutOfBounds);
  2318. FShapes.Insert(AIndex, AShape);
  2319. texs := AShape.GetUsedTextures;
  2320. for i := 0 to high(texs) do AddTexture(texs[i]);
  2321. AShape.OnChange := @OnShapeChange;
  2322. AShape.OnEditingChange := @OnShapeEditingChange;
  2323. DiscardFrozenShapes;
  2324. result := AShape.GetRenderBounds(InfiniteRect, AffineMatrixIdentity);
  2325. end;
  2326. function TVectorOriginal.InternalInsertShapeRange(AShapes: TVectorShapes;
  2327. AIndex: integer): TRectF;
  2328. var
  2329. i: Integer;
  2330. r: TRectF;
  2331. begin
  2332. result := EmptyRectF;
  2333. if Assigned(AShapes) then
  2334. for i := 0 to AShapes.Count-1 do
  2335. begin
  2336. r := InternalInsertShape(AShapes[i], AIndex+i);
  2337. if not r.IsEmpty then
  2338. begin
  2339. if result.IsEmpty then result := r
  2340. else result := result.Union(r);
  2341. end;
  2342. end;
  2343. end;
  2344. function TVectorOriginal.InternalDeleteShapeRange(AStartIndex, ACount: integer): TRectF;
  2345. var
  2346. r: TRectF;
  2347. s: TVectorShape;
  2348. i: Integer;
  2349. begin
  2350. result := EmptyRectF;
  2351. if (AStartIndex < 0) or (AStartIndex+ACount > ShapeCount) then
  2352. raise exception.Create(rsIndexOutOfBounds);
  2353. for i := AStartIndex to AStartIndex+ACount-1 do
  2354. if Shape[i].FRemoving then
  2355. raise exception.Create(errAlreadyRemovingShape);
  2356. for i := AStartIndex to AStartIndex+ACount-1 do Shape[i].FRemoving := true;
  2357. for i := AStartIndex to AStartIndex+ACount-1 do DeselectShape(i);
  2358. for i := AStartIndex+ACount-1 downto AStartIndex do
  2359. begin
  2360. s := Shape[i];
  2361. s.OnChange := nil;
  2362. s.OnEditingChange := nil;
  2363. r := s.GetRenderBounds(InfiniteRect, AffineMatrixIdentity);
  2364. result := TRectF.Union(result,r,True);
  2365. FShapes.Delete(i);
  2366. FDeletedShapes.Add(s);
  2367. s.FRemoving:= false;
  2368. end;
  2369. DiscardFrozenShapes;
  2370. end;
  2371. function TVectorOriginal.GetNewShapeId: integer;
  2372. begin
  2373. inc(FLastShapeId);
  2374. result := FLastShapeId;
  2375. end;
  2376. function TVectorOriginal.GetShape(AIndex: integer): TVectorShape;
  2377. begin
  2378. result := FShapes[AIndex];
  2379. end;
  2380. procedure TVectorOriginal.MultiSelection_SelectionChange(Sender: TObject);
  2381. begin
  2382. if FMultiselection = FSelectedShape then
  2383. begin
  2384. DiscardFrozenShapes;
  2385. NotifyEditorChange;
  2386. end;
  2387. end;
  2388. procedure TVectorOriginal.FreeDeletedShapes;
  2389. var
  2390. i: Integer;
  2391. begin
  2392. for i := 0 to FDeletedShapes.Count-1 do
  2393. FDeletedShapes[i].Free;
  2394. FDeletedShapes.Clear
  2395. end;
  2396. procedure TVectorOriginal.OnShapeChange(ASender: TObject; ABounds: TRectF; ADiff: TVectorShapeDiff);
  2397. var
  2398. embed: TVectorOriginalShapeDiff;
  2399. idxShape: Integer;
  2400. begin
  2401. if ASender <> FSelectedShape then DiscardFrozenShapes;
  2402. if DiffExpected and Assigned(ADiff) then
  2403. begin
  2404. if ASender = FMultiselection then
  2405. idxShape := -2
  2406. else idxShape := IndexOfShape(ASender as TVectorShape);
  2407. embed := TVectorOriginalShapeDiff.Create(idxShape, ADiff);
  2408. ADiff := nil;
  2409. NotifyChange(ABounds, embed);
  2410. end else
  2411. NotifyChange(ABounds);
  2412. ADiff.Free;
  2413. end;
  2414. procedure TVectorOriginal.OnShapeEditingChange(ASender: TObject);
  2415. begin
  2416. if ASender = FSelectedShape then
  2417. NotifyEditorChange;
  2418. end;
  2419. procedure TVectorOriginal.DiscardFrozenShapes;
  2420. begin
  2421. FFrozenShapesComputed:= false;
  2422. FreeAndNil(FFrozenShapesUnderSelection);
  2423. FreeAndNil(FFrozenShapesOverSelection);
  2424. end;
  2425. function TVectorOriginal.GetTextureId(ABitmap: TBGRABitmap): integer;
  2426. var
  2427. i: Integer;
  2428. begin
  2429. if (ABitmap = nil) or (ABitmap.NbPixels = 0) then exit(EmptyTextureId);
  2430. for i := 0 to FTextureCount-1 do
  2431. if FTextures[i].Bitmap = ABitmap then exit(FTextures[i].Id);
  2432. for i := 0 to FTextureCount-1 do
  2433. if FTextures[i].Bitmap.Equals(ABitmap) then exit(FTextures[i].Id);
  2434. exit(-1);
  2435. end;
  2436. function TVectorOriginal.IndexOfTexture(AId: integer): integer;
  2437. var
  2438. i: Integer;
  2439. begin
  2440. if AId = EmptyTextureId then exit(-1);
  2441. for i := 0 to FTextureCount-1 do
  2442. if FTextures[i].Id = AId then exit(i);
  2443. exit(-1);
  2444. end;
  2445. procedure TVectorOriginal.AddTextureWithId(ATexture: TBGRABitmap; AId: integer);
  2446. begin
  2447. if FTextureCount >= length(FTextures) then
  2448. setlength(FTextures, FTextureCount*2+2);
  2449. if AId > FLastTextureId then FLastTextureId:= AId;
  2450. FTextures[FTextureCount].Bitmap := ATexture.NewReference as TBGRABitmap;
  2451. FTextures[FTextureCount].Id := AId;
  2452. inc(FTextureCount);
  2453. end;
  2454. procedure TVectorOriginal.ClearTextures;
  2455. var
  2456. i: Integer;
  2457. begin
  2458. //note that there are still shapes that could use textures
  2459. for i := 0 to FTextureCount-1 do
  2460. begin
  2461. FTextures[i].Bitmap.FreeReference;
  2462. FTextures[i].Bitmap := nil;
  2463. end;
  2464. FTextureCount := 0;
  2465. FTextures := nil;
  2466. FLastTextureId:= EmptyTextureId;
  2467. end;
  2468. constructor TVectorOriginal.Create;
  2469. begin
  2470. inherited Create;
  2471. FShapes := TVectorShapes.Create;
  2472. FDeletedShapes := TVectorShapes.Create;
  2473. FSelectedShape := nil;
  2474. FFrozenShapesUnderSelection := nil;
  2475. FFrozenShapesOverSelection := nil;
  2476. FFrozenShapesComputed:= false;
  2477. FLastTextureId:= EmptyTextureId;
  2478. FLastShapeId:= 0;
  2479. if VectorMultiselectionFactory <> nil then
  2480. begin
  2481. FMultiselection := VectorMultiselectionFactory.Create(self);
  2482. FMultiselection.Id := -2;
  2483. FMultiselection.OnChange := @OnShapeChange;
  2484. FMultiselection.OnEditingChange := @OnShapeEditingChange;
  2485. FMultiselection.GetAsMultishape.SetOnSelectionChange(@MultiSelection_SelectionChange);
  2486. end
  2487. else FMultiselection := nil;
  2488. end;
  2489. destructor TVectorOriginal.Destroy;
  2490. var
  2491. i: Integer;
  2492. begin
  2493. FMultiselection.Free;
  2494. FSelectedShape := nil;
  2495. for i := 0 to FShapes.Count-1 do
  2496. FShapes[i].Free;
  2497. FreeAndNil(FShapes);
  2498. FreeDeletedShapes;
  2499. FreeAndNil(FDeletedShapes);
  2500. FreeAndNil(FFrozenShapesUnderSelection);
  2501. FreeAndNil(FFrozenShapesOverSelection);
  2502. ClearTextures;
  2503. inherited Destroy;
  2504. end;
  2505. procedure TVectorOriginal.Clear;
  2506. var
  2507. i: Integer;
  2508. begin
  2509. if FShapes.Count > 0 then
  2510. begin
  2511. DeselectShapes;
  2512. for i := 0 to FShapes.Count-1 do
  2513. FDeletedShapes.Add(FShapes[i]);
  2514. FShapes.Clear;
  2515. FLastShapeId:= 0;
  2516. ClearTextures;
  2517. NotifyChange;
  2518. end;
  2519. end;
  2520. function TVectorOriginal.AddTexture(ATexture: TBGRABitmap): integer;
  2521. begin
  2522. result := GetTextureId(ATexture);
  2523. if result <> -1 then exit;
  2524. result:= FLastTextureId+1;
  2525. AddTextureWithId(ATexture, result);
  2526. end;
  2527. function TVectorOriginal.GetTexture(AId: integer): TBGRABitmap;
  2528. var
  2529. index: Integer;
  2530. begin
  2531. index := IndexOfTexture(AId);
  2532. if index = -1 then
  2533. result := nil
  2534. else
  2535. result := FTextures[index].Bitmap;
  2536. end;
  2537. procedure TVectorOriginal.DiscardUnusedTextures;
  2538. var
  2539. i, j: Integer;
  2540. texs: array Of TBGRABitmap;
  2541. begin
  2542. for i := 0 to FTextureCount-1 do
  2543. FTextures[i].Counter:= 0;
  2544. for i := 0 to FShapes.Count-1 do
  2545. begin
  2546. texs := FShapes[i].GetUsedTextures;
  2547. for j := 0 to high(texs) do
  2548. inc(FTextures[IndexOfTexture(GetTextureId(texs[j]))].Counter);
  2549. end;
  2550. for i := FTextureCount-1 downto 0 do
  2551. if FTextures[i].Counter = 0 then
  2552. begin
  2553. FTextures[i].Bitmap.FreeReference;
  2554. FTextures[i].Bitmap := nil;
  2555. for j := i to FTextureCount-2 do
  2556. FTextures[j] := FTextures[j+1];
  2557. dec(FTextureCount);
  2558. end;
  2559. if FTextureCount < length(FTextures) div 2 then
  2560. setlength(FTextures, FTextureCount);
  2561. end;
  2562. function TVectorOriginal.AddShape(AShape: TVectorShape): integer;
  2563. begin
  2564. result := ShapeCount;
  2565. InsertShape(AShape, result);
  2566. end;
  2567. function TVectorOriginal.AddShape(AShape: TVectorShape;
  2568. AUsermode: TVectorShapeUsermode): integer;
  2569. begin
  2570. result := AddShape(AShape);
  2571. AShape.Usermode:= AUsermode;
  2572. SelectShape(result);
  2573. end;
  2574. function TVectorOriginal.AddShapes(AShapes: TVectorShapes): integer;
  2575. begin
  2576. result := ShapeCount;
  2577. InsertShapes(AShapes, result);
  2578. end;
  2579. procedure TVectorOriginal.InsertShape(AShape: TVectorShape; AIndex: integer);
  2580. var
  2581. newShapes: TVectorShapes;
  2582. begin
  2583. newShapes := TVectorShapes.Create;
  2584. newShapes.Add(AShape);
  2585. ReplaceShapeRange(AIndex,0,newShapes);
  2586. newShapes.Free;
  2587. end;
  2588. procedure TVectorOriginal.InsertShapes(AShapes: TVectorShapes;
  2589. AIndex: integer);
  2590. begin
  2591. ReplaceShapeRange(AIndex, 0, AShapes);
  2592. end;
  2593. function TVectorOriginal.RemoveShape(AShape: TVectorShape): boolean;
  2594. var
  2595. idx: LongInt;
  2596. multiSel: IVectorMultishape;
  2597. startIndex, endIndex, nextIndex, i, selCount: Integer;
  2598. begin
  2599. if AShape.FRemoving then exit(false);
  2600. if (AShape = FMultiselection) and Assigned(FMultiselection) then
  2601. begin
  2602. multiSel := FMultiselection.GetAsMultishape;
  2603. selCount := multiSel.ShapeCount;
  2604. if selCount = 0 then exit;
  2605. endIndex := IndexOfShape(multiSel.GetShape(selCount-1));
  2606. startIndex := endIndex;
  2607. i := selCount-2;
  2608. while i >= 0 do
  2609. begin
  2610. nextIndex := IndexOfShape(multiSel.GetShape(i));
  2611. if nextIndex < startIndex-1 then
  2612. begin
  2613. DeleteShapeRange(startIndex, endIndex-startIndex+1);
  2614. endIndex := nextIndex;
  2615. startIndex := endIndex;
  2616. end else
  2617. startIndex := nextIndex;
  2618. dec(i);
  2619. end;
  2620. DeleteShapeRange(startIndex, endIndex-startIndex+1);
  2621. end else
  2622. begin
  2623. idx := FShapes.IndexOf(AShape);
  2624. if idx = -1 then exit(false);
  2625. DeleteShapeRange(idx, 1);
  2626. result := true;
  2627. end;
  2628. end;
  2629. procedure TVectorOriginal.DeleteShape(AIndex: integer);
  2630. begin
  2631. DeleteShapeRange(AIndex, 1);
  2632. end;
  2633. procedure TVectorOriginal.DeleteShapeRange(AStartIndex, ACount: integer);
  2634. begin
  2635. ReplaceShapeRange(AStartIndex, ACount, nil);
  2636. end;
  2637. procedure TVectorOriginal.ReplaceShape(AIndex: integer; ANewShape: TVectorShape);
  2638. var newShapes: TVectorShapes;
  2639. begin
  2640. if ANewShape = nil then raise exception.Create(errUnexpectedNil);
  2641. newShapes:= TVectorShapes.Create;
  2642. newShapes.Add(ANewShape);
  2643. ReplaceShapeRange(AIndex, 1, newShapes);
  2644. newShapes.Free;
  2645. end;
  2646. procedure TVectorOriginal.ReplaceShapeRange(AStartIndex: integer;
  2647. ACountBefore: integer; ANewShapes: TVectorShapes);
  2648. var
  2649. rDelete, rInsert: TRectF;
  2650. removed: TVectorShapes;
  2651. diff: TVectorOriginalShapeRangeDiff;
  2652. i: Integer;
  2653. begin
  2654. if (AStartIndex < 0) or (AStartIndex+ACountBefore > ShapeCount) then
  2655. raise exception.Create(rsIndexOutOfBounds);
  2656. if Assigned(ANewShapes) then
  2657. for i := 0 to ANewShapes.Count-1 do
  2658. if ANewShapes[i] is VectorMultiselectionFactory then
  2659. raise exception.Create('Cannot add a multiselection as a shape');
  2660. if Assigned(ANewShapes) then
  2661. for i := 0 to ANewShapes.Count-1 do
  2662. ANewShapes[i].Id := GetNewShapeId;
  2663. if DiffExpected then
  2664. begin
  2665. if ACountBefore > 0 then
  2666. begin
  2667. removed := TVectorShapes.Create;
  2668. for i := 0 to ACountBefore-1 do removed.Add(Shape[AStartIndex+i]);
  2669. end else removed := nil;
  2670. diff := TVectorOriginalShapeRangeDiff.Create(AStartIndex, removed, ANewShapes,
  2671. -1,-1);
  2672. removed.Free;
  2673. end else diff := nil;
  2674. rDelete := InternalDeleteShapeRange(AStartIndex, ACountBefore);
  2675. rInsert := InternalInsertShapeRange(ANewShapes, AStartIndex);
  2676. NotifyChange(TRectF.Union(rDelete,rInsert,True), diff);
  2677. end;
  2678. function TVectorOriginal.SelectShapes(AShapes: TVectorShapes): boolean;
  2679. begin
  2680. if AShapes.Count = 0 then result := DeselectShapes
  2681. else if AShapes.Count = 1 then result := SelectShape(AShapes[0])
  2682. else
  2683. begin
  2684. FSelectedShape := FMultiselection;
  2685. if FMultiselection.GetAsMultishape.SetShapes(AShapes) then
  2686. NotifyEditorChange;
  2687. end;
  2688. end;
  2689. function TVectorOriginal.SelectShape(AIndex: integer; AToggle: boolean): boolean;
  2690. begin
  2691. if AIndex=-1 then result := SelectShape(nil, AToggle)
  2692. else
  2693. begin
  2694. if (AIndex < 0) or (AIndex >= FShapes.Count) then
  2695. raise ERangeError.Create(rsIndexOutOfBounds);
  2696. result := SelectShape(FShapes[AIndex], AToggle);
  2697. end;
  2698. end;
  2699. function TVectorOriginal.SelectShape(AShape: TVectorShape; AToggle: boolean): boolean;
  2700. var
  2701. prevSel, newSel: TVectorShape;
  2702. prevMode: TVectorShapeUsermode;
  2703. multiSel: IVectorMultishape;
  2704. begin
  2705. result := false;
  2706. //when selecting nothing
  2707. if AShape = nil then
  2708. begin
  2709. if not AToggle then
  2710. result := DeselectShapes;
  2711. exit;
  2712. end;
  2713. //selecting current selection
  2714. if AShape = FSelectedShape then
  2715. begin
  2716. if AToggle then
  2717. result := DeselectShapes;
  2718. exit;
  2719. end;
  2720. //check selected shape exists
  2721. if AShape <> nil then
  2722. if FShapes.IndexOf(AShape)=-1 then
  2723. raise exception.Create(rsShapeNotFound);
  2724. //case of modifying multiselection
  2725. if (FSelectedShape = FMultiselection) and Assigned(FMultiselection) and AToggle then
  2726. begin
  2727. multiSel := FSelectedShape.GetAsMultishape;
  2728. if multiSel.ContainsShape(AShape) then
  2729. begin
  2730. multiSel.RemoveShape(AShape);
  2731. if multiSel.ShapeCount = 0 then
  2732. begin
  2733. FSelectedShape := nil;
  2734. exit(true);
  2735. end else
  2736. if multiSel.ShapeCount > 1 then
  2737. exit(true) else
  2738. begin
  2739. SelectShape(multiSel.GetShape(0));
  2740. exit(true);
  2741. end;
  2742. end else
  2743. begin
  2744. multiSel.AddShape(AShape);
  2745. exit(true);
  2746. end;
  2747. end;
  2748. //changing selection completely
  2749. prevSel := FSelectedShape;
  2750. if Assigned(prevSel) then
  2751. begin
  2752. prevMode := prevSel.Usermode;
  2753. prevSel.Usermode := vsuEdit;
  2754. end else
  2755. prevMode := vsuEdit;
  2756. //becomes a multiselection
  2757. if AToggle and (prevSel <> nil) and Assigned(FMultiselection) then
  2758. begin
  2759. multiSel := FMultiselection.GetAsMultishape;
  2760. multiSel.ClearShapes;
  2761. multiSel.AddShape(prevSel);
  2762. multiSel.AddShape(AShape);
  2763. newSel := FMultiselection;
  2764. end else
  2765. begin
  2766. //otherwise simple selection
  2767. newSel := AShape;
  2768. end;
  2769. //transfering user mode
  2770. if (prevMode = vsuEditBackFill) and (prevMode in newSel.Usermodes) and
  2771. newSel.BackFill.IsEditable then newSel.Usermode:= prevMode;
  2772. if (prevMode = vsuEditPenFill) and (prevMode in newSel.Usermodes) and
  2773. newSel.PenFill.IsEditable then newSel.Usermode:= prevMode;
  2774. if (prevMode = vsuEditOutlineFill) and (prevMode in newSel.Usermodes) and
  2775. newSel.OutlineFill.IsEditable then newSel.Usermode:= prevMode;
  2776. if (prevMode = vsuEditText) and (prevMode in newSel.Usermodes) then
  2777. newSel.Usermode := prevMode;
  2778. FSelectedShape := newSel;
  2779. DiscardFrozenShapes;
  2780. NotifyEditorChange;
  2781. if Assigned(FOnSelectShape) then
  2782. FOnSelectShape(self, FSelectedShape, prevSel);
  2783. if (prevSel = FMultiselection) and Assigned(FMultiselection) then
  2784. FMultiselection.GetAsMultishape.ClearShapes;
  2785. end;
  2786. function TVectorOriginal.DeselectShapes: boolean;
  2787. var
  2788. prev: TVectorShape;
  2789. begin
  2790. if SelectedShape = nil then exit(false);
  2791. prev := SelectedShape;
  2792. SelectedShape.Usermode := vsuEdit;
  2793. FSelectedShape := nil;
  2794. if (prev = FMultiselection) and Assigned(FMultiselection) then
  2795. FMultiselection.GetAsMultishape.ClearShapes;
  2796. DiscardFrozenShapes;
  2797. NotifyEditorChange;
  2798. if Assigned(FOnSelectShape) then
  2799. FOnSelectShape(self, nil, prev);
  2800. result := true;
  2801. end;
  2802. procedure TVectorOriginal.DeselectShape(AIndex: integer);
  2803. begin
  2804. if (AIndex >= 0) and (AIndex < ShapeCount) then
  2805. DeselectShape(Shape[AIndex]);
  2806. end;
  2807. procedure TVectorOriginal.DeselectShape(AShape: TVectorShape);
  2808. var
  2809. multiSel: IVectorMultishape;
  2810. begin
  2811. if AShape = SelectedShape then DeselectShapes else
  2812. begin
  2813. if (SelectedShape = FMultiselection) and Assigned(FMultiselection) then
  2814. begin
  2815. multiSel := SelectedShape.GetAsMultishape;
  2816. if multiSel.ContainsShape(AShape) then
  2817. multiSel.RemoveShape(AShape);
  2818. if multiSel.ShapeCount = 1 then
  2819. SelectShape(multiSel.GetShape(0));
  2820. end;
  2821. end;
  2822. end;
  2823. function TVectorOriginal.GetShapesCost: integer;
  2824. var
  2825. i: Integer;
  2826. begin
  2827. result := 0;
  2828. for i := 0 to ShapeCount-1 do
  2829. inc(result, Shape[i].GetGenericCost);
  2830. end;
  2831. function TVectorOriginal.PreferDraftMode(AEditor: TBGRAOriginalEditor; const AMatrix: TAffineMatrix): boolean;
  2832. begin
  2833. if Assigned(SelectedShape) and Assigned(AEditor) then
  2834. begin
  2835. result := (AEditor.IsMovingPoint or SelectedShape.IsFollowingMouse) and
  2836. SelectedShape.GetIsSlow(AMatrix);
  2837. end else
  2838. result := false;
  2839. end;
  2840. function TVectorOriginal.MouseClick(APoint: TPointF; ARadius: single; AToggle: boolean): boolean;
  2841. var
  2842. i: LongInt;
  2843. begin
  2844. for i:= FShapes.Count-1 downto 0 do
  2845. if FShapes[i].PointInShape(APoint) then
  2846. begin
  2847. if SelectedShape <> FShapes[i] then
  2848. begin
  2849. SelectShape(i, AToggle);
  2850. exit(true);
  2851. end else
  2852. exit(false);
  2853. end;
  2854. for i:= FShapes.Count-1 downto 0 do
  2855. if FShapes[i].PointInShape(APoint, ARadius) then
  2856. begin
  2857. if SelectedShape <> FShapes[i] then
  2858. begin
  2859. SelectShape(i, AToggle);
  2860. exit(true);
  2861. end else
  2862. exit(false);
  2863. end;
  2864. if (SelectedShape <> nil) and not AToggle then
  2865. begin
  2866. DeselectShapes;
  2867. exit(true);
  2868. end else
  2869. exit(false);
  2870. end;
  2871. procedure TVectorOriginal.Render(ADest: TBGRABitmap; ARenderOffset: TPoint; AMatrix: TAffineMatrix;
  2872. ADraft: boolean);
  2873. var
  2874. i: Integer;
  2875. idxSelected, newUnfrozenRangeStart, newUnfrozenRangeEnd: LongInt;
  2876. shapeRectF, clipRectF, allRectF: TRectF;
  2877. mOfs: TAffineMatrix;
  2878. multiSel: IVectorMultishape;
  2879. ofsRange: TPoint;
  2880. oldClip: TRect;
  2881. begin
  2882. if FSelectedShape <> FMultiselection then
  2883. begin
  2884. idxSelected := FShapes.IndexOf(FSelectedShape);
  2885. if idxSelected = -1 then
  2886. begin
  2887. FSelectedShape := nil;
  2888. newUnfrozenRangeStart := 0;
  2889. newUnfrozenRangeEnd := ShapeCount;
  2890. end else
  2891. begin
  2892. newUnfrozenRangeStart := idxSelected;
  2893. newUnfrozenRangeEnd := idxSelected+1;
  2894. end;
  2895. end else
  2896. begin
  2897. multiSel := FMultiselection.GetAsMultishape;
  2898. if multiSel.ShapeCount = 0 then
  2899. begin
  2900. FSelectedShape := nil;
  2901. newUnfrozenRangeStart := 0;
  2902. newUnfrozenRangeEnd := ShapeCount;
  2903. end;
  2904. newUnfrozenRangeStart := IndexOfShape(multiSel.BackShape);
  2905. newUnfrozenRangeEnd := IndexOfShape(multiSel.FrontShape)+1;
  2906. end;
  2907. if (newUnfrozenRangeStart <> FUnfrozenRangeStart) or
  2908. (newUnfrozenRangeEnd <> FUnfrozenRangeEnd) or
  2909. (AMatrix <> FFrozenShapeMatrix) then
  2910. DiscardFrozenShapes;
  2911. with ADest.ClipRect do
  2912. clipRectF := RectF(Left,Top,Right,Bottom);
  2913. mOfs := AffineMatrixTranslation(ARenderOffset.X,ARenderOffset.Y)*AMatrix;
  2914. if FFrozenShapesComputed then
  2915. begin
  2916. if Assigned(FFrozenShapesUnderSelection) then
  2917. ADest.PutImage(ARenderOffset.X-FFrozenShapesRenderOffset.X+FFrozenShapesUnderBounds.Left,
  2918. ARenderOffset.Y-FFrozenShapesRenderOffset.Y+FFrozenShapesUnderBounds.Top,
  2919. FFrozenShapesUnderSelection, dmSet);
  2920. for i := FUnfrozenRangeStart to FUnfrozenRangeEnd-1 do
  2921. begin
  2922. shapeRectF := FShapes[i].GetRenderBounds(ADest.ClipRect, mOfs, []);
  2923. if shapeRectF.IntersectsWith(clipRectF) then
  2924. begin
  2925. with shapeRectF do
  2926. oldClip := ADest.IntersectClip(rect(floor(Left), floor(Top), ceil(Right), ceil(Bottom)));
  2927. FShapes[i].Render(ADest, ARenderOffset, AMatrix, ADraft);
  2928. ADest.ClipRect := oldClip;
  2929. end;
  2930. end;
  2931. if Assigned(FFrozenShapesOverSelection) then
  2932. ADest.PutImage(ARenderOffset.X-FFrozenShapesRenderOffset.X+FFrozenShapesOverBounds.Left,
  2933. ARenderOffset.Y-FFrozenShapesRenderOffset.Y+FFrozenShapesOverBounds.Top,
  2934. FFrozenShapesOverSelection, dmDrawWithTransparency);
  2935. end else
  2936. begin
  2937. if (newUnfrozenRangeStart > 0) or (newUnfrozenRangeEnd < ShapeCount) then
  2938. begin
  2939. allRectF := rectF(0,0,ADest.Width,ADest.Height);
  2940. FUnfrozenRangeStart := newUnfrozenRangeStart;
  2941. FUnfrozenRangeEnd := newUnfrozenRangeEnd;
  2942. if FUnfrozenRangeStart > 0 then
  2943. begin
  2944. FreeAndNil(FFrozenShapesUnderSelection);
  2945. FFrozenShapesUnderBounds := GetRenderBounds(rect(0,0,ADest.Width,ADest.Height), mOfs,
  2946. 0, FUnfrozenRangeStart-1);
  2947. FFrozenShapesUnderBounds.Intersect(rect(0,0,ADest.Width,ADest.Height));
  2948. FFrozenShapesUnderSelection := TBGRABitmap.Create(FFrozenShapesUnderBounds.Width, FFrozenShapesUnderBounds.Height);
  2949. ofsRange := Point(ARenderOffset.X - FFrozenShapesUnderBounds.Left,
  2950. ARenderOffset.Y - FFrozenShapesUnderBounds.Top);
  2951. for i:= 0 to FUnfrozenRangeStart-1 do
  2952. begin
  2953. shapeRectF := FShapes[i].GetRenderBounds(rect(0,0,ADest.Width,ADest.Height), mOfs, []);
  2954. if shapeRectF.IntersectsWith(allRectF) then
  2955. begin
  2956. shapeRectF.Offset(-FFrozenShapesUnderBounds.Left, -FFrozenShapesUnderBounds.Top);
  2957. with shapeRectF do
  2958. oldClip := FFrozenShapesUnderSelection.IntersectClip(rect(floor(Left), floor(Top), ceil(Right), ceil(Bottom)));
  2959. FShapes[i].Render(FFrozenShapesUnderSelection, ofsRange, AMatrix, false);
  2960. FFrozenShapesUnderSelection.ClipRect := oldClip;
  2961. end;
  2962. end;
  2963. ADest.PutImage(FFrozenShapesUnderBounds.Left, FFrozenShapesUnderBounds.Top,
  2964. FFrozenShapesUnderSelection, dmSet);
  2965. end;
  2966. for i := FUnfrozenRangeStart to FUnfrozenRangeEnd-1 do
  2967. if FShapes[i].GetRenderBounds(ADest.ClipRect, mOfs, []).IntersectsWith(clipRectF) then
  2968. FShapes[i].Render(ADest, ARenderOffset, AMatrix, ADraft);
  2969. if FUnfrozenRangeEnd < FShapes.Count then
  2970. begin
  2971. FreeAndNil(FFrozenShapesOverSelection);
  2972. FFrozenShapesOverBounds := GetRenderBounds(rect(0,0,ADest.Width,ADest.Height), AMatrix,
  2973. FUnfrozenRangeEnd, FShapes.Count-1);
  2974. FFrozenShapesOverBounds.Intersect(rect(0,0,ADest.Width,ADest.Height));
  2975. FFrozenShapesOverSelection := TBGRABitmap.Create(FFrozenShapesOverBounds.Width, FFrozenShapesOverBounds.Height);
  2976. ofsRange := Point(ARenderOffset.X - FFrozenShapesOverBounds.Left,
  2977. ARenderOffset.Y - FFrozenShapesOverBounds.Top);
  2978. for i:= FUnfrozenRangeEnd to FShapes.Count-1 do
  2979. begin
  2980. shapeRectF := FShapes[i].GetRenderBounds(rect(0,0,ADest.Width,ADest.Height), mOfs, []);
  2981. if shapeRectF.IntersectsWith(allRectF) then
  2982. begin
  2983. shapeRectF.Offset(-FFrozenShapesOverBounds.Left, -FFrozenShapesOverBounds.Top);
  2984. with shapeRectF do
  2985. oldClip := FFrozenShapesOverSelection.IntersectClip(rect(floor(Left), floor(Top), ceil(Right), ceil(Bottom)));
  2986. FShapes[i].Render(FFrozenShapesOverSelection, ofsRange, AMatrix, false);
  2987. FFrozenShapesOverSelection.ClipRect := oldClip;
  2988. end;
  2989. end;
  2990. ADest.PutImage(FFrozenShapesOverBounds.Left, FFrozenShapesOverBounds.Top,
  2991. FFrozenShapesOverSelection, dmDrawWithTransparency);
  2992. end;
  2993. FFrozenShapesRenderOffset := ARenderOffset;
  2994. FFrozenShapesComputed := true;
  2995. FFrozenShapeMatrix := AMatrix;
  2996. end else
  2997. begin
  2998. for i:= 0 to FShapes.Count-1 do
  2999. if FShapes[i].GetRenderBounds(ADest.ClipRect, mOfs, []).IntersectsWith(clipRectF) then
  3000. FShapes[i].Render(ADest, ARenderOffset, AMatrix, ADraft);
  3001. end;
  3002. end;
  3003. DiscardUnusedRenderStorage;
  3004. end;
  3005. procedure TVectorOriginal.ConfigureEditor(AEditor: TBGRAOriginalEditor);
  3006. begin
  3007. inherited ConfigureEditor(AEditor);
  3008. if Assigned(FSelectedShape) then
  3009. begin
  3010. if (FShapes.IndexOf(FSelectedShape)=-1) and
  3011. (FSelectedShape <> FMultiselection) then
  3012. begin
  3013. FSelectedShape := nil;
  3014. DiscardFrozenShapes;
  3015. end
  3016. else
  3017. FSelectedShape.ConfigureEditor(AEditor);
  3018. end;
  3019. //no more reference to event handlers
  3020. FreeDeletedShapes;
  3021. end;
  3022. function TVectorOriginal.CreateEditor: TBGRAOriginalEditor;
  3023. begin
  3024. Result:= TVectorOriginalEditor.Create(self);
  3025. end;
  3026. function TVectorOriginal.GetRenderBounds(ADestRect: TRect;
  3027. AMatrix: TAffineMatrix): TRect;
  3028. begin
  3029. result := GetRenderBounds(ADestRect, AMatrix, 0, ShapeCount-1);
  3030. end;
  3031. function TVectorOriginal.GetRenderBounds(ADestRect: TRect;
  3032. AMatrix: TAffineMatrix; AStartIndex, AEndIndex: integer): TRect;
  3033. var
  3034. area, shapeArea: TRectF;
  3035. i: Integer;
  3036. shapeDir: TBGRACustomOriginalStorage;
  3037. useStorage: Boolean;
  3038. iteration: LongInt;
  3039. begin
  3040. area:= EmptyRectF;
  3041. useStorage := Assigned(RenderStorage) and (RenderStorage.AffineMatrix['last-matrix']=AMatrix);
  3042. for i:= AStartIndex to AEndIndex do
  3043. begin
  3044. if useStorage then
  3045. begin
  3046. shapeDir := OpenShapeRenderStorage(i, false);
  3047. if Assigned(shapeDir) then
  3048. begin
  3049. iteration := shapeDir.Int['iteration'];
  3050. if iteration = FShapes[i].FRenderIteration then
  3051. begin
  3052. shapeArea := shapeDir.RectangleF['bounds'];
  3053. area := area.Union(shapeArea, true);
  3054. shapeDir.Free;
  3055. continue;
  3056. end;
  3057. end;
  3058. end;
  3059. shapeArea := FShapes[i].GetRenderBounds(ADestRect, AMatrix);
  3060. area := area.Union(shapeArea, true);
  3061. end;
  3062. if IsEmptyRectF(area) then
  3063. result := EmptyRect
  3064. else
  3065. result := rect(floor(area.Left),floor(area.Top),ceil(area.Right),ceil(area.Bottom));
  3066. end;
  3067. function TVectorOriginal.GetAlignBounds(ADestRect: TRect; AMatrix: TAffineMatrix): TRect;
  3068. var
  3069. area, shapeArea: TRectF;
  3070. i: Integer;
  3071. begin
  3072. area:= EmptyRectF;
  3073. for i:= 0 to FShapes.Count-1 do
  3074. begin
  3075. shapeArea := FShapes[i].GetAlignBounds(ADestRect, AMatrix);
  3076. area := area.Union(shapeArea, true);
  3077. end;
  3078. if IsEmptyRectF(area) then
  3079. result := EmptyRect
  3080. else
  3081. result := rect(floor(area.Left),floor(area.Top),ceil(area.Right),ceil(area.Bottom));
  3082. end;
  3083. procedure TVectorOriginal.LoadFromStorage(AStorage: TBGRACustomOriginalStorage);
  3084. var
  3085. nb: LongInt;
  3086. i: Integer;
  3087. shapeObj, texObj: TBGRACustomOriginalStorage;
  3088. texName: String;
  3089. loadedShape: TVectorShape;
  3090. idList: array of single;
  3091. mem: TMemoryStream;
  3092. texId: integer;
  3093. bmp: TBGRABitmap;
  3094. begin
  3095. Clear;
  3096. texObj := AStorage.OpenObject('textures');
  3097. if Assigned(texObj) then
  3098. begin
  3099. try
  3100. idList := texObj.FloatArray['id'];
  3101. for i := 0 to high(idList) do
  3102. begin
  3103. texId:= round(idList[i]);
  3104. texName:= 'tex'+inttostr(texId);
  3105. mem := TMemoryStream.Create;
  3106. try
  3107. if not texObj.ReadFile(texName+'.png', mem) and
  3108. not texObj.ReadFile(texName+'.jpg', mem) then
  3109. raise exception.Create(errUnableToFindTexture);
  3110. mem.Position:= 0;
  3111. bmp := TBGRABitmap.Create(mem);
  3112. AddTextureWithId(bmp, texId);
  3113. bmp.FreeReference;
  3114. finally
  3115. mem.Free;
  3116. end;
  3117. end;
  3118. finally
  3119. texObj.Free;
  3120. end;
  3121. end;
  3122. nb := AStorage.Int['count'];
  3123. for i:= 0 to nb-1 do
  3124. begin
  3125. shapeObj := AStorage.OpenObject('shape'+inttostr(i+1));
  3126. if shapeObj <> nil then
  3127. try
  3128. loadedShape := TVectorShape.CreateFromStorage(shapeObj, self);
  3129. loadedShape.OnChange := @OnShapeChange;
  3130. loadedShape.OnEditingChange := @OnShapeEditingChange;
  3131. if loadedShape.Id > FLastShapeId then FLastShapeId := loadedShape.Id;
  3132. FShapes.Add(loadedShape);
  3133. finally
  3134. shapeObj.Free;
  3135. end;
  3136. end;
  3137. for i := 0 to ShapeCount-1 do
  3138. if Shape[i].Id = 0 then
  3139. Shape[i].Id := GetNewShapeId;
  3140. NotifyChange;
  3141. end;
  3142. procedure TVectorOriginal.SaveToStorage(AStorage: TBGRACustomOriginalStorage);
  3143. var
  3144. nb: LongInt;
  3145. i, texIndex: Integer;
  3146. shapeObj, texObj: TBGRACustomOriginalStorage;
  3147. idList: array of single;
  3148. texName: String;
  3149. mem: TMemoryStream;
  3150. texId: integer;
  3151. begin
  3152. nb := AStorage.Int['count'];
  3153. for i := 0 to nb-1 do AStorage.RemoveObject('shape'+inttostr(i+1));
  3154. AStorage.Int['count'] := 0;
  3155. for i := 0 to FShapes.Count-1 do
  3156. begin
  3157. shapeObj := AStorage.CreateObject('shape'+inttostr(i+1));
  3158. shapeObj.RawString['class'] := FShapes[i].StorageClassName;
  3159. try
  3160. FShapes[i].SaveToStorage(shapeObj);
  3161. AStorage.Int['count'] := i+1;
  3162. finally
  3163. shapeObj.Free;
  3164. end;
  3165. end;
  3166. if FTextureCount = 0 then
  3167. AStorage.RemoveObject('textures')
  3168. else
  3169. begin
  3170. texObj := nil;
  3171. try
  3172. texObj := AStorage.OpenObject('textures');
  3173. if texObj = nil then
  3174. texObj := AStorage.CreateObject('textures');
  3175. for i := 0 to FTextureCount-1 do
  3176. FTextures[i].Counter:= 0;
  3177. idList := texObj.FloatArray['id'];
  3178. for i := 0 to high(idList) do
  3179. begin
  3180. texId := round(idList[i]);
  3181. texIndex:= IndexOfTexture(texId);
  3182. if texIndex=-1 then
  3183. begin
  3184. texName := 'tex'+inttostr(texId);
  3185. texObj.RemoveFile(texName+'.png');
  3186. texObj.RemoveFile(texName+'.jpg');
  3187. end else
  3188. inc(FTextures[texIndex].Counter);
  3189. end;
  3190. setlength(idList, FTextureCount);
  3191. for i := 0 to FTextureCount-1 do
  3192. begin
  3193. if FTextures[i].Counter = 0 then
  3194. begin
  3195. texName := 'tex'+inttostr(FTextures[i].Id);
  3196. mem := TMemoryStream.Create;
  3197. try
  3198. FTextures[i].Bitmap.SaveToStreamAsPng(mem);
  3199. texObj.WriteFile(texName+'.png', mem, false);
  3200. finally
  3201. mem.Free;
  3202. end;
  3203. end;
  3204. idList[i] := FTextures[i].Id;
  3205. end;
  3206. texObj.FloatArray['id'] := idList;
  3207. finally
  3208. texObj.Free;
  3209. end;
  3210. end;
  3211. end;
  3212. function TVectorOriginal.IndexOfShape(AShape: TVectorShape): integer;
  3213. begin
  3214. result := FShapes.IndexOf(AShape);
  3215. end;
  3216. procedure TVectorOriginal.MoveShapeToIndex(AFromIndex, AToIndex: integer);
  3217. begin
  3218. MoveShapeToIndex([AFromIndex], [AToIndex]);
  3219. end;
  3220. procedure TVectorOriginal.MoveShapeToIndex(AFromIndex,
  3221. AToIndex: array of integer);
  3222. var
  3223. diff: TVectorOriginalMoveShapeToIndexDiff;
  3224. begin
  3225. diff := TVectorOriginalMoveShapeToIndexDiff.Create(AFromIndex, AToIndex);
  3226. if diff.IsIdentity then
  3227. begin
  3228. diff.Free;
  3229. exit;
  3230. end;
  3231. diff.Apply(self, true);
  3232. end;
  3233. class function TVectorOriginal.StorageClassName: RawByteString;
  3234. begin
  3235. result := 'vector';
  3236. end;
  3237. initialization
  3238. RegisterLayerOriginal(TVectorOriginal);
  3239. end.