lcvectororiginal.pas 118 KB

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