path.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898
  1. // Copyright (C) 2004-2025 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include <string.h>
  24. #include <assert.h>
  25. // Thoughts for further optimisations:
  26. // All paths start with MoveTo. We could probably avoid most cases where
  27. // we store that. The next thing after a close must be a move.
  28. // Commands are MOVE, LINE, HORIZ, VERT, DEGEN, CURVE, CURVEV, CURVEY, QUAD, RECT.
  29. // We'd need to drop 2 to get us down to 3 bits.
  30. // Commands can be followed by CLOSE. Use 1 bit for close.
  31. // PDF 'RECT' implies close according to the spec, but I suspect
  32. // we can ignore this as filling closes implicitly.
  33. // We use a single bit in the path header to tell us whether we have
  34. // a trailing move. Trailing moves can always be stripped when path
  35. // construction completes.
  36. typedef enum
  37. {
  38. FZ_MOVETO = 'M',
  39. FZ_LINETO = 'L',
  40. FZ_DEGENLINETO = 'D',
  41. FZ_CURVETO = 'C',
  42. FZ_CURVETOV = 'V',
  43. FZ_CURVETOY = 'Y',
  44. FZ_HORIZTO = 'H',
  45. FZ_VERTTO = 'I',
  46. FZ_QUADTO = 'Q',
  47. FZ_RECTTO = 'R',
  48. FZ_MOVETOCLOSE = 'm',
  49. FZ_LINETOCLOSE = 'l',
  50. FZ_DEGENLINETOCLOSE = 'd',
  51. FZ_CURVETOCLOSE = 'c',
  52. FZ_CURVETOVCLOSE = 'v',
  53. FZ_CURVETOYCLOSE = 'y',
  54. FZ_HORIZTOCLOSE = 'h',
  55. FZ_VERTTOCLOSE = 'i',
  56. FZ_QUADTOCLOSE = 'q',
  57. } fz_path_item_kind;
  58. struct fz_path
  59. {
  60. int8_t refs;
  61. uint8_t packed;
  62. int cmd_len, cmd_cap;
  63. unsigned char *cmds;
  64. int coord_len, coord_cap;
  65. float *coords;
  66. fz_point current;
  67. fz_point begin;
  68. };
  69. typedef struct
  70. {
  71. int8_t refs;
  72. uint8_t packed;
  73. uint8_t coord_len;
  74. uint8_t cmd_len;
  75. } fz_packed_path;
  76. /*
  77. Paths are created UNPACKED. That means we have a fz_path
  78. structure with coords and cmds pointing to malloced blocks.
  79. After they have been completely constructed, callers may choose
  80. to 'pack' them into some target block of memory. If if coord_len
  81. and cmd_len are both < 256, then they are PACKED_FLAT into an
  82. fz_packed_path with the coords and cmds in the bytes afterwards,
  83. all inside the target block. If they cannot be accommodated in
  84. that way, then they are PACKED_OPEN, where an fz_path is put
  85. into the target block, and cmds and coords remain pointers to
  86. allocated blocks.
  87. */
  88. enum
  89. {
  90. FZ_PATH_UNPACKED = 0,
  91. FZ_PATH_PACKED_FLAT = 1,
  92. FZ_PATH_PACKED_OPEN = 2
  93. };
  94. #define LAST_CMD(path) ((path)->cmd_len > 0 ? (path)->cmds[(path)->cmd_len-1] : 0)
  95. fz_path *
  96. fz_new_path(fz_context *ctx)
  97. {
  98. fz_path *path;
  99. path = fz_malloc_struct(ctx, fz_path);
  100. path->refs = 1;
  101. path->packed = FZ_PATH_UNPACKED;
  102. path->current.x = 0;
  103. path->current.y = 0;
  104. path->begin.x = 0;
  105. path->begin.y = 0;
  106. return path;
  107. }
  108. /*
  109. Take an additional reference to
  110. a path.
  111. No modifications should be carried out on a path
  112. to which more than one reference is held, as
  113. this can cause race conditions.
  114. */
  115. fz_path *
  116. fz_keep_path(fz_context *ctx, const fz_path *pathc)
  117. {
  118. fz_path *path = (fz_path *)pathc; /* Explicit cast away of const */
  119. int trimmable = 0;
  120. if (path == NULL)
  121. return NULL;
  122. fz_lock(ctx, FZ_LOCK_ALLOC);
  123. /* Technically, we should only access ->refs with the lock held,
  124. * so do that here. We can't actually do the trimming here, because
  125. * to do so would do memory accesses with the ALLOC lock held. */
  126. if (path->refs == 1 && path->packed == FZ_PATH_UNPACKED)
  127. trimmable = 1;
  128. fz_keep_imp8_locked(ctx, path, &path->refs);
  129. fz_unlock(ctx, FZ_LOCK_ALLOC);
  130. /* This is thread safe, because we know that the only person
  131. * holding a reference to this thread is us. */
  132. if (trimmable)
  133. fz_trim_path(ctx, path);
  134. return path;
  135. }
  136. void
  137. fz_drop_path(fz_context *ctx, const fz_path *pathc)
  138. {
  139. fz_path *path = (fz_path *)pathc; /* Explicit cast away of const */
  140. if (fz_drop_imp8(ctx, path, &path->refs))
  141. {
  142. if (path->packed != FZ_PATH_PACKED_FLAT)
  143. {
  144. fz_free(ctx, path->cmds);
  145. fz_free(ctx, path->coords);
  146. }
  147. if (path->packed == FZ_PATH_UNPACKED)
  148. fz_free(ctx, path);
  149. }
  150. }
  151. int fz_packed_path_size(const fz_path *path)
  152. {
  153. switch (path->packed)
  154. {
  155. case FZ_PATH_UNPACKED:
  156. if (path->cmd_len > 255 || path->coord_len > 255)
  157. return sizeof(fz_path);
  158. return sizeof(fz_packed_path) + sizeof(float) * path->coord_len + sizeof(uint8_t) * path->cmd_len;
  159. case FZ_PATH_PACKED_OPEN:
  160. return sizeof(fz_path);
  161. case FZ_PATH_PACKED_FLAT:
  162. {
  163. fz_packed_path *pack = (fz_packed_path *)path;
  164. return sizeof(fz_packed_path) + sizeof(float) * pack->coord_len + sizeof(uint8_t) * pack->cmd_len;
  165. }
  166. default:
  167. assert("This never happens" == NULL);
  168. return 0;
  169. }
  170. }
  171. size_t
  172. fz_pack_path(fz_context *ctx, uint8_t *pack_, const fz_path *path)
  173. {
  174. uint8_t *ptr;
  175. size_t size;
  176. if (path->packed == FZ_PATH_PACKED_FLAT)
  177. {
  178. fz_packed_path *pack = (fz_packed_path *)path;
  179. fz_packed_path *out = (fz_packed_path *)pack_;
  180. size = sizeof(fz_packed_path) + sizeof(float) * pack->coord_len + sizeof(uint8_t) * pack->cmd_len;
  181. if (out)
  182. {
  183. out->refs = 1;
  184. out->packed = FZ_PATH_PACKED_FLAT;
  185. out->coord_len = pack->coord_len;
  186. out->cmd_len = pack->cmd_len;
  187. memcpy(&out[1], &pack[1], size - sizeof(*out));
  188. }
  189. return size;
  190. }
  191. size = sizeof(fz_packed_path) + sizeof(float) * path->coord_len + sizeof(uint8_t) * path->cmd_len;
  192. /* If the path can't be packed flat, then pack it open */
  193. if (path->cmd_len > 255 || path->coord_len > 255)
  194. {
  195. fz_path *pack = (fz_path *)pack_;
  196. if (pack != NULL)
  197. {
  198. pack->refs = 1;
  199. pack->packed = FZ_PATH_PACKED_OPEN;
  200. pack->current.x = 0;
  201. pack->current.y = 0;
  202. pack->begin.x = 0;
  203. pack->begin.y = 0;
  204. pack->coord_cap = path->coord_len;
  205. pack->coord_len = path->coord_len;
  206. pack->cmd_cap = path->cmd_len;
  207. pack->cmd_len = path->cmd_len;
  208. pack->coords = Memento_label(fz_malloc_array(ctx, path->coord_len, float), "path_packed_coords");
  209. fz_try(ctx)
  210. {
  211. pack->cmds = Memento_label(fz_malloc_array(ctx, path->cmd_len, uint8_t), "path_packed_cmds");
  212. }
  213. fz_catch(ctx)
  214. {
  215. fz_free(ctx, pack->coords);
  216. fz_rethrow(ctx);
  217. }
  218. memcpy(pack->coords, path->coords, sizeof(float) * path->coord_len);
  219. memcpy(pack->cmds, path->cmds, sizeof(uint8_t) * path->cmd_len);
  220. }
  221. return sizeof(fz_path);
  222. }
  223. else
  224. {
  225. fz_packed_path *pack = (fz_packed_path *)pack_;
  226. if (pack != NULL)
  227. {
  228. pack->refs = 1;
  229. pack->packed = FZ_PATH_PACKED_FLAT;
  230. pack->cmd_len = path->cmd_len;
  231. pack->coord_len = path->coord_len;
  232. ptr = (uint8_t *)&pack[1];
  233. memcpy(ptr, path->coords, sizeof(float) * path->coord_len);
  234. ptr += sizeof(float) * path->coord_len;
  235. memcpy(ptr, path->cmds, sizeof(uint8_t) * path->cmd_len);
  236. }
  237. return size;
  238. }
  239. }
  240. static void
  241. push_cmd(fz_context *ctx, fz_path *path, int cmd)
  242. {
  243. if (path->refs != 1)
  244. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot modify shared paths");
  245. if (path->cmd_len + 1 >= path->cmd_cap)
  246. {
  247. int new_cmd_cap = fz_maxi(16, path->cmd_cap * 2);
  248. path->cmds = fz_realloc_array(ctx, path->cmds, new_cmd_cap, unsigned char);
  249. path->cmd_cap = new_cmd_cap;
  250. }
  251. path->cmds[path->cmd_len++] = cmd;
  252. }
  253. static void
  254. push_coord(fz_context *ctx, fz_path *path, float x, float y)
  255. {
  256. if (path->coord_len + 2 >= path->coord_cap)
  257. {
  258. int new_coord_cap = fz_maxi(32, path->coord_cap * 2);
  259. path->coords = fz_realloc_array(ctx, path->coords, new_coord_cap, float);
  260. path->coord_cap = new_coord_cap;
  261. }
  262. path->coords[path->coord_len++] = x;
  263. path->coords[path->coord_len++] = y;
  264. path->current.x = x;
  265. path->current.y = y;
  266. }
  267. static void
  268. push_ord(fz_context *ctx, fz_path *path, float xy, int isx)
  269. {
  270. if (path->coord_len + 1 >= path->coord_cap)
  271. {
  272. int new_coord_cap = fz_maxi(32, path->coord_cap * 2);
  273. path->coords = fz_realloc_array(ctx, path->coords, new_coord_cap, float);
  274. path->coord_cap = new_coord_cap;
  275. }
  276. path->coords[path->coord_len++] = xy;
  277. if (isx)
  278. path->current.x = xy;
  279. else
  280. path->current.y = xy;
  281. }
  282. fz_point
  283. fz_currentpoint(fz_context *ctx, fz_path *path)
  284. {
  285. return path->current;
  286. }
  287. void
  288. fz_moveto(fz_context *ctx, fz_path *path, float x, float y)
  289. {
  290. if (path->packed)
  291. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Cannot modify a packed path");
  292. if (path->cmd_len > 0 && LAST_CMD(path) == FZ_MOVETO)
  293. {
  294. /* Collapse moveto followed by moveto. */
  295. path->coords[path->coord_len-2] = x;
  296. path->coords[path->coord_len-1] = y;
  297. path->current.x = x;
  298. path->current.y = y;
  299. path->begin = path->current;
  300. return;
  301. }
  302. push_cmd(ctx, path, FZ_MOVETO);
  303. push_coord(ctx, path, x, y);
  304. path->begin = path->current;
  305. }
  306. void
  307. fz_lineto(fz_context *ctx, fz_path *path, float x, float y)
  308. {
  309. float x0, y0;
  310. if (path->packed)
  311. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Cannot modify a packed path");
  312. x0 = path->current.x;
  313. y0 = path->current.y;
  314. if (path->cmd_len == 0)
  315. {
  316. fz_warn(ctx, "lineto with no current point");
  317. return;
  318. }
  319. /* (Anything other than MoveTo) followed by (LineTo the same place) is a nop */
  320. if (LAST_CMD(path) != FZ_MOVETO && x0 == x && y0 == y)
  321. return;
  322. if (x0 == x)
  323. {
  324. if (y0 == y)
  325. {
  326. if (LAST_CMD(path) != FZ_MOVETO)
  327. return;
  328. push_cmd(ctx, path, FZ_DEGENLINETO);
  329. }
  330. else
  331. {
  332. push_cmd(ctx, path, FZ_VERTTO);
  333. push_ord(ctx, path, y, 0);
  334. }
  335. }
  336. else if (y0 == y)
  337. {
  338. push_cmd(ctx, path, FZ_HORIZTO);
  339. push_ord(ctx, path, x, 1);
  340. }
  341. else
  342. {
  343. push_cmd(ctx, path, FZ_LINETO);
  344. push_coord(ctx, path, x, y);
  345. }
  346. }
  347. void
  348. fz_curveto(fz_context *ctx, fz_path *path,
  349. float x1, float y1,
  350. float x2, float y2,
  351. float x3, float y3)
  352. {
  353. float x0, y0;
  354. if (path->packed)
  355. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Cannot modify a packed path");
  356. x0 = path->current.x;
  357. y0 = path->current.y;
  358. if (path->cmd_len == 0)
  359. {
  360. fz_warn(ctx, "curveto with no current point");
  361. return;
  362. }
  363. /* Check for degenerate cases: */
  364. if (x0 == x1 && y0 == y1)
  365. {
  366. if (x2 == x3 && y2 == y3)
  367. {
  368. /* If (x1,y1)==(x2,y2) and prev wasn't a moveto, then skip */
  369. if (x1 == x2 && y1 == y2 && LAST_CMD(path) != FZ_MOVETO)
  370. return;
  371. /* Otherwise a line will suffice */
  372. fz_lineto(ctx, path, x3, y3);
  373. }
  374. else if (x1 == x2 && y1 == y2)
  375. {
  376. /* A line will suffice */
  377. fz_lineto(ctx, path, x3, y3);
  378. }
  379. else
  380. fz_curvetov(ctx, path, x2, y2, x3, y3);
  381. return;
  382. }
  383. else if (x2 == x3 && y2 == y3)
  384. {
  385. if (x1 == x2 && y1 == y2)
  386. {
  387. /* A line will suffice */
  388. fz_lineto(ctx, path, x3, y3);
  389. }
  390. else
  391. fz_curvetoy(ctx, path, x1, y1, x3, y3);
  392. return;
  393. }
  394. push_cmd(ctx, path, FZ_CURVETO);
  395. push_coord(ctx, path, x1, y1);
  396. push_coord(ctx, path, x2, y2);
  397. push_coord(ctx, path, x3, y3);
  398. }
  399. void
  400. fz_quadto(fz_context *ctx, fz_path *path,
  401. float x1, float y1,
  402. float x2, float y2)
  403. {
  404. float x0, y0;
  405. if (path->packed)
  406. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Cannot modify a packed path");
  407. x0 = path->current.x;
  408. y0 = path->current.y;
  409. if (path->cmd_len == 0)
  410. {
  411. fz_warn(ctx, "quadto with no current point");
  412. return;
  413. }
  414. /* Check for degenerate cases: */
  415. if ((x0 == x1 && y0 == y1) || (x1 == x2 && y1 == y2))
  416. {
  417. if (x0 == x2 && y0 == y2 && LAST_CMD(path) != FZ_MOVETO)
  418. return;
  419. /* A line will suffice */
  420. fz_lineto(ctx, path, x2, y2);
  421. return;
  422. }
  423. push_cmd(ctx, path, FZ_QUADTO);
  424. push_coord(ctx, path, x1, y1);
  425. push_coord(ctx, path, x2, y2);
  426. }
  427. void
  428. fz_curvetov(fz_context *ctx, fz_path *path, float x2, float y2, float x3, float y3)
  429. {
  430. float x0, y0;
  431. if (path->packed)
  432. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Cannot modify a packed path");
  433. x0 = path->current.x;
  434. y0 = path->current.y;
  435. if (path->cmd_len == 0)
  436. {
  437. fz_warn(ctx, "curveto with no current point");
  438. return;
  439. }
  440. /* Check for degenerate cases: */
  441. if (x2 == x3 && y2 == y3)
  442. {
  443. /* If (x0,y0)==(x2,y2) and prev wasn't a moveto, then skip */
  444. if (x0 == x2 && y0 == y2 && LAST_CMD(path) != FZ_MOVETO)
  445. return;
  446. /* Otherwise a line will suffice */
  447. fz_lineto(ctx, path, x3, y3);
  448. }
  449. else if (x0 == x2 && y0 == y2)
  450. {
  451. /* A line will suffice */
  452. fz_lineto(ctx, path, x3, y3);
  453. }
  454. push_cmd(ctx, path, FZ_CURVETOV);
  455. push_coord(ctx, path, x2, y2);
  456. push_coord(ctx, path, x3, y3);
  457. }
  458. void
  459. fz_curvetoy(fz_context *ctx, fz_path *path, float x1, float y1, float x3, float y3)
  460. {
  461. float x0, y0;
  462. if (path->packed)
  463. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Cannot modify a packed path");
  464. x0 = path->current.x;
  465. y0 = path->current.y;
  466. if (path->cmd_len == 0)
  467. {
  468. fz_warn(ctx, "curveto with no current point");
  469. return;
  470. }
  471. /* Check for degenerate cases: */
  472. if (x1 == x3 && y1 == y3)
  473. {
  474. /* If (x0,y0)==(x1,y1) and prev wasn't a moveto, then skip */
  475. if (x0 == x1 && y0 == y1 && LAST_CMD(path) != FZ_MOVETO)
  476. return;
  477. /* Otherwise a line will suffice */
  478. fz_lineto(ctx, path, x3, y3);
  479. }
  480. push_cmd(ctx, path, FZ_CURVETOY);
  481. push_coord(ctx, path, x1, y1);
  482. push_coord(ctx, path, x3, y3);
  483. }
  484. void
  485. fz_closepath(fz_context *ctx, fz_path *path)
  486. {
  487. uint8_t rep;
  488. if (path->packed)
  489. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Cannot modify a packed path");
  490. if (path->cmd_len == 0)
  491. {
  492. fz_warn(ctx, "closepath with no current point");
  493. return;
  494. }
  495. switch(LAST_CMD(path))
  496. {
  497. case FZ_MOVETO:
  498. rep = FZ_MOVETOCLOSE;
  499. break;
  500. case FZ_LINETO:
  501. rep = FZ_LINETOCLOSE;
  502. break;
  503. case FZ_DEGENLINETO:
  504. rep = FZ_DEGENLINETOCLOSE;
  505. break;
  506. case FZ_CURVETO:
  507. rep = FZ_CURVETOCLOSE;
  508. break;
  509. case FZ_CURVETOV:
  510. rep = FZ_CURVETOVCLOSE;
  511. break;
  512. case FZ_CURVETOY:
  513. rep = FZ_CURVETOYCLOSE;
  514. break;
  515. case FZ_HORIZTO:
  516. rep = FZ_HORIZTOCLOSE;
  517. break;
  518. case FZ_VERTTO:
  519. rep = FZ_VERTTOCLOSE;
  520. break;
  521. case FZ_QUADTO:
  522. rep = FZ_QUADTOCLOSE;
  523. break;
  524. case FZ_RECTTO:
  525. /* RectTo implies close */
  526. return;
  527. case FZ_MOVETOCLOSE:
  528. case FZ_LINETOCLOSE:
  529. case FZ_DEGENLINETOCLOSE:
  530. case FZ_CURVETOCLOSE:
  531. case FZ_CURVETOVCLOSE:
  532. case FZ_CURVETOYCLOSE:
  533. case FZ_HORIZTOCLOSE:
  534. case FZ_VERTTOCLOSE:
  535. case FZ_QUADTOCLOSE:
  536. /* CLOSE following a CLOSE is a NOP */
  537. return;
  538. default: /* default never happens */
  539. case 0:
  540. /* Closing an empty path is a NOP */
  541. return;
  542. }
  543. path->cmds[path->cmd_len-1] = rep;
  544. path->current = path->begin;
  545. }
  546. void
  547. fz_rectto(fz_context *ctx, fz_path *path, float x1, float y1, float x2, float y2)
  548. {
  549. if (path->packed)
  550. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Cannot modify a packed path");
  551. if (path->cmd_len > 0 && LAST_CMD(path) == FZ_MOVETO)
  552. {
  553. /* Collapse moveto followed by rectto. */
  554. path->coord_len -= 2;
  555. path->cmd_len--;
  556. }
  557. push_cmd(ctx, path, FZ_RECTTO);
  558. push_coord(ctx, path, x1, y1);
  559. push_coord(ctx, path, x2, y2);
  560. path->current = path->begin;
  561. }
  562. static inline void bound_expand(fz_rect *r, fz_point p)
  563. {
  564. if (p.x < r->x0) r->x0 = p.x;
  565. if (p.y < r->y0) r->y0 = p.y;
  566. if (p.x > r->x1) r->x1 = p.x;
  567. if (p.y > r->y1) r->y1 = p.y;
  568. }
  569. void fz_walk_path(fz_context *ctx, const fz_path *path, const fz_path_walker *proc, void *arg)
  570. {
  571. int i, k, cmd_len;
  572. float x=0, y=0, sx=0, sy=0;
  573. uint8_t *cmds;
  574. float *coords;
  575. switch (path->packed)
  576. {
  577. case FZ_PATH_UNPACKED:
  578. case FZ_PATH_PACKED_OPEN:
  579. cmd_len = path->cmd_len;
  580. coords = path->coords;
  581. cmds = path->cmds;
  582. break;
  583. case FZ_PATH_PACKED_FLAT:
  584. cmd_len = ((fz_packed_path *)path)->cmd_len;
  585. coords = (float *)&((fz_packed_path *)path)[1];
  586. cmds = (uint8_t *)&coords[((fz_packed_path *)path)->coord_len];
  587. break;
  588. default:
  589. assert("This never happens" == NULL);
  590. return;
  591. }
  592. if (cmd_len == 0)
  593. return;
  594. for (k=0, i = 0; i < cmd_len; i++)
  595. {
  596. uint8_t cmd = cmds[i];
  597. switch (cmd)
  598. {
  599. case FZ_CURVETO:
  600. case FZ_CURVETOCLOSE:
  601. proc->curveto(ctx, arg,
  602. coords[k],
  603. coords[k+1],
  604. coords[k+2],
  605. coords[k+3],
  606. x = coords[k+4],
  607. y = coords[k+5]);
  608. k += 6;
  609. if (cmd == FZ_CURVETOCLOSE)
  610. {
  611. if (proc->closepath)
  612. proc->closepath(ctx, arg);
  613. x = sx;
  614. y = sy;
  615. }
  616. break;
  617. case FZ_CURVETOV:
  618. case FZ_CURVETOVCLOSE:
  619. if (proc->curvetov)
  620. proc->curvetov(ctx, arg,
  621. coords[k],
  622. coords[k+1],
  623. x = coords[k+2],
  624. y = coords[k+3]);
  625. else
  626. {
  627. proc->curveto(ctx, arg,
  628. x,
  629. y,
  630. coords[k],
  631. coords[k+1],
  632. coords[k+2],
  633. coords[k+3]);
  634. x = coords[k+2];
  635. y = coords[k+3];
  636. }
  637. k += 4;
  638. if (cmd == FZ_CURVETOVCLOSE)
  639. {
  640. if (proc->closepath)
  641. proc->closepath(ctx, arg);
  642. x = sx;
  643. y = sy;
  644. }
  645. break;
  646. case FZ_CURVETOY:
  647. case FZ_CURVETOYCLOSE:
  648. if (proc->curvetoy)
  649. proc->curvetoy(ctx, arg,
  650. coords[k],
  651. coords[k+1],
  652. x = coords[k+2],
  653. y = coords[k+3]);
  654. else
  655. proc->curveto(ctx, arg,
  656. coords[k],
  657. coords[k+1],
  658. coords[k+2],
  659. coords[k+3],
  660. x = coords[k+2],
  661. y = coords[k+3]);
  662. k += 4;
  663. if (cmd == FZ_CURVETOYCLOSE)
  664. {
  665. if (proc->closepath)
  666. proc->closepath(ctx, arg);
  667. x = sx;
  668. y = sy;
  669. }
  670. break;
  671. case FZ_QUADTO:
  672. case FZ_QUADTOCLOSE:
  673. if (proc->quadto)
  674. proc->quadto(ctx, arg,
  675. coords[k],
  676. coords[k+1],
  677. x = coords[k+2],
  678. y = coords[k+3]);
  679. else
  680. {
  681. float c2x = coords[k] * 2;
  682. float c2y = coords[k+1] * 2;
  683. float c1x = (x + c2x) / 3;
  684. float c1y = (y + c2y) / 3;
  685. x = coords[k+2];
  686. y = coords[k+3];
  687. c2x = (c2x + x) / 3;
  688. c2y = (c2y + y) / 3;
  689. proc->curveto(ctx, arg,
  690. c1x,
  691. c1y,
  692. c2x,
  693. c2y,
  694. x,
  695. y);
  696. }
  697. k += 4;
  698. if (cmd == FZ_QUADTOCLOSE)
  699. {
  700. if (proc->closepath)
  701. proc->closepath(ctx, arg);
  702. x = sx;
  703. y = sy;
  704. }
  705. break;
  706. case FZ_MOVETO:
  707. case FZ_MOVETOCLOSE:
  708. proc->moveto(ctx, arg,
  709. x = coords[k],
  710. y = coords[k+1]);
  711. k += 2;
  712. sx = x;
  713. sy = y;
  714. if (cmd == FZ_MOVETOCLOSE)
  715. {
  716. if (proc->closepath)
  717. proc->closepath(ctx, arg);
  718. x = sx;
  719. y = sy;
  720. }
  721. break;
  722. case FZ_LINETO:
  723. case FZ_LINETOCLOSE:
  724. proc->lineto(ctx, arg,
  725. x = coords[k],
  726. y = coords[k+1]);
  727. k += 2;
  728. if (cmd == FZ_LINETOCLOSE)
  729. {
  730. if (proc->closepath)
  731. proc->closepath(ctx, arg);
  732. x = sx;
  733. y = sy;
  734. }
  735. break;
  736. case FZ_HORIZTO:
  737. case FZ_HORIZTOCLOSE:
  738. proc->lineto(ctx, arg,
  739. x = coords[k],
  740. y);
  741. k += 1;
  742. if (cmd == FZ_HORIZTOCLOSE)
  743. {
  744. if (proc->closepath)
  745. proc->closepath(ctx, arg);
  746. x = sx;
  747. y = sy;
  748. }
  749. break;
  750. case FZ_VERTTO:
  751. case FZ_VERTTOCLOSE:
  752. proc->lineto(ctx, arg,
  753. x,
  754. y = coords[k]);
  755. k += 1;
  756. if (cmd == FZ_VERTTOCLOSE)
  757. {
  758. if (proc->closepath)
  759. proc->closepath(ctx, arg);
  760. x = sx;
  761. y = sy;
  762. }
  763. break;
  764. case FZ_DEGENLINETO:
  765. case FZ_DEGENLINETOCLOSE:
  766. proc->lineto(ctx, arg,
  767. x,
  768. y);
  769. if (cmd == FZ_DEGENLINETOCLOSE)
  770. {
  771. if (proc->closepath)
  772. proc->closepath(ctx, arg);
  773. x = sx;
  774. y = sy;
  775. }
  776. break;
  777. case FZ_RECTTO:
  778. if (proc->rectto)
  779. {
  780. proc->rectto(ctx, arg,
  781. x = coords[k],
  782. y = coords[k+1],
  783. coords[k+2],
  784. coords[k+3]);
  785. }
  786. else
  787. {
  788. proc->moveto(ctx, arg,
  789. x = coords[k],
  790. y = coords[k+1]);
  791. proc->lineto(ctx, arg,
  792. coords[k+2],
  793. coords[k+1]);
  794. proc->lineto(ctx, arg,
  795. coords[k+2],
  796. coords[k+3]);
  797. proc->lineto(ctx, arg,
  798. coords[k],
  799. coords[k+3]);
  800. if (proc->closepath)
  801. proc->closepath(ctx, arg);
  802. }
  803. sx = x;
  804. sy = y;
  805. k += 4;
  806. break;
  807. }
  808. }
  809. }
  810. /*
  811. A couple of notes about the path bounding algorithm.
  812. Firstly, we don't expand the bounds immediately on a move, because
  813. a sequence of moves together will only actually use the last one,
  814. and trailing moves are ignored. This is achieved using 'trailing_move'.
  815. Secondly, we watch for paths that are entirely rectilinear (all segments
  816. move left/right/up/down only, with no curves). Such "only_right_angles"
  817. paths can be bounded with us ignoring any mitre limit. This is a really
  818. common case that can otherwise bloats simple boxes far more than is
  819. useful. This is particular annoying during table recognition!
  820. */
  821. typedef struct
  822. {
  823. fz_matrix ctm;
  824. fz_rect rect;
  825. fz_point move;
  826. int trailing_move;
  827. int first;
  828. int only_right_angles;
  829. fz_point prev;
  830. } bound_path_arg;
  831. static void
  832. bound_moveto(fz_context *ctx, void *arg_, float x, float y)
  833. {
  834. bound_path_arg *arg = (bound_path_arg *)arg_;
  835. arg->move = arg->prev = fz_transform_point_xy(x, y, arg->ctm);
  836. arg->trailing_move = 1;
  837. }
  838. static inline int
  839. eq0(float x)
  840. {
  841. return x >= -0.001 && x <= 0.001;
  842. }
  843. static void
  844. bound_lineto(fz_context *ctx, void *arg_, float x, float y)
  845. {
  846. bound_path_arg *arg = (bound_path_arg *)arg_;
  847. fz_point p = fz_transform_point_xy(x, y, arg->ctm);
  848. if (arg->first)
  849. {
  850. arg->rect.x0 = arg->rect.x1 = p.x;
  851. arg->rect.y0 = arg->rect.y1 = p.y;
  852. arg->first = 0;
  853. }
  854. else
  855. bound_expand(&arg->rect, p);
  856. if (arg->trailing_move)
  857. {
  858. arg->trailing_move = 0;
  859. bound_expand(&arg->rect, arg->move);
  860. }
  861. if (arg->only_right_angles && !eq0(arg->prev.x - p.x) && !eq0(arg->prev.y - p.y))
  862. arg->only_right_angles = 0;
  863. arg->prev = p;
  864. }
  865. static void
  866. bound_curveto(fz_context *ctx, void *arg_, float x1, float y1, float x2, float y2, float x3, float y3)
  867. {
  868. bound_path_arg *arg = (bound_path_arg *)arg_;
  869. fz_point p = fz_transform_point_xy(x1, y1, arg->ctm);
  870. if (arg->first)
  871. {
  872. arg->rect.x0 = arg->rect.x1 = p.x;
  873. arg->rect.y0 = arg->rect.y1 = p.y;
  874. arg->first = 0;
  875. }
  876. else
  877. bound_expand(&arg->rect, p);
  878. bound_expand(&arg->rect, fz_transform_point_xy(x2, y2, arg->ctm));
  879. bound_expand(&arg->rect, fz_transform_point_xy(x3, y3, arg->ctm));
  880. if (arg->trailing_move)
  881. {
  882. arg->trailing_move = 0;
  883. bound_expand(&arg->rect, arg->move);
  884. }
  885. arg->only_right_angles = 0;
  886. arg->prev = p;
  887. }
  888. static const fz_path_walker bound_path_walker =
  889. {
  890. bound_moveto,
  891. bound_lineto,
  892. bound_curveto,
  893. NULL
  894. };
  895. static fz_rect
  896. adjust_rect_for_stroke(fz_context *ctx, fz_rect r, const fz_stroke_state *stroke, fz_matrix ctm, int no_mitre)
  897. {
  898. float expand;
  899. if (!stroke)
  900. return r;
  901. expand = stroke->linewidth/2;
  902. if (expand == 0)
  903. expand = 0.5f;
  904. if (r.x1 == r.x0 || r.y1 == r.y0)
  905. {
  906. /* Mitring can't apply in this case. */
  907. }
  908. else if (!no_mitre && stroke->linejoin == FZ_LINEJOIN_MITER && stroke->miterlimit > 0.5f)
  909. {
  910. /* miter limit is expressed in terms of the linewidth, not half the line width. */
  911. expand *= stroke->miterlimit * 2;
  912. }
  913. else if (!no_mitre && stroke->linejoin == FZ_LINEJOIN_MITER_XPS && stroke->miterlimit > 1.0f)
  914. {
  915. /* for xps, miter limit is expressed in terms of half the linewidth. */
  916. expand *= stroke->miterlimit;
  917. }
  918. expand *= fz_matrix_max_expansion(ctm);
  919. r.x0 -= expand;
  920. r.y0 -= expand;
  921. r.x1 += expand;
  922. r.y1 += expand;
  923. return r;
  924. }
  925. fz_rect
  926. fz_bound_path(fz_context *ctx, const fz_path *path, const fz_stroke_state *stroke, fz_matrix ctm)
  927. {
  928. bound_path_arg arg;
  929. arg.ctm = ctm;
  930. arg.rect = fz_empty_rect;
  931. arg.trailing_move = 0;
  932. arg.first = 1;
  933. arg.only_right_angles = 1;
  934. fz_walk_path(ctx, path, &bound_path_walker, &arg);
  935. if (!arg.first && stroke)
  936. {
  937. arg.rect = adjust_rect_for_stroke(ctx, arg.rect, stroke, ctm, arg.only_right_angles);
  938. }
  939. return arg.rect;
  940. }
  941. fz_rect
  942. fz_adjust_rect_for_stroke(fz_context *ctx, fz_rect r, const fz_stroke_state *stroke, fz_matrix ctm)
  943. {
  944. return adjust_rect_for_stroke(ctx, r, stroke, ctm, 0);
  945. }
  946. void
  947. fz_transform_path(fz_context *ctx, fz_path *path, fz_matrix ctm)
  948. {
  949. int i, k, n;
  950. fz_point p, p1, p2, p3, q, s;
  951. if (path->packed)
  952. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Cannot transform a packed path");
  953. if (ctm.b == 0 && ctm.c == 0)
  954. {
  955. /* Simple, in place transform */
  956. i = 0;
  957. k = 0;
  958. while (i < path->cmd_len)
  959. {
  960. uint8_t cmd = path->cmds[i];
  961. switch (cmd)
  962. {
  963. case FZ_MOVETO:
  964. case FZ_LINETO:
  965. case FZ_MOVETOCLOSE:
  966. case FZ_LINETOCLOSE:
  967. n = 1;
  968. break;
  969. case FZ_DEGENLINETO:
  970. case FZ_DEGENLINETOCLOSE:
  971. n = 0;
  972. break;
  973. case FZ_CURVETO:
  974. case FZ_CURVETOCLOSE:
  975. n = 3;
  976. break;
  977. case FZ_RECTTO:
  978. s.x = path->coords[k];
  979. s.y = path->coords[k+1];
  980. n = 2;
  981. break;
  982. case FZ_CURVETOV:
  983. case FZ_CURVETOY:
  984. case FZ_QUADTO:
  985. case FZ_CURVETOVCLOSE:
  986. case FZ_CURVETOYCLOSE:
  987. case FZ_QUADTOCLOSE:
  988. n = 2;
  989. break;
  990. case FZ_HORIZTO:
  991. case FZ_HORIZTOCLOSE:
  992. q.x = path->coords[k];
  993. p = fz_transform_point(q, ctm);
  994. path->coords[k++] = p.x;
  995. n = 0;
  996. break;
  997. case FZ_VERTTO:
  998. case FZ_VERTTOCLOSE:
  999. q.y = path->coords[k];
  1000. p = fz_transform_point(q, ctm);
  1001. path->coords[k++] = p.y;
  1002. n = 0;
  1003. break;
  1004. default:
  1005. assert("Unknown path cmd" == NULL);
  1006. }
  1007. while (n > 0)
  1008. {
  1009. q.x = path->coords[k];
  1010. q.y = path->coords[k+1];
  1011. p = fz_transform_point(q, ctm);
  1012. path->coords[k++] = p.x;
  1013. path->coords[k++] = p.y;
  1014. n--;
  1015. }
  1016. switch (cmd)
  1017. {
  1018. case FZ_MOVETO:
  1019. case FZ_MOVETOCLOSE:
  1020. s = q;
  1021. break;
  1022. case FZ_LINETOCLOSE:
  1023. case FZ_DEGENLINETOCLOSE:
  1024. case FZ_CURVETOCLOSE:
  1025. case FZ_CURVETOVCLOSE:
  1026. case FZ_CURVETOYCLOSE:
  1027. case FZ_QUADTOCLOSE:
  1028. case FZ_HORIZTOCLOSE:
  1029. case FZ_VERTTOCLOSE:
  1030. case FZ_RECTTO:
  1031. q = s;
  1032. break;
  1033. }
  1034. i++;
  1035. }
  1036. }
  1037. else if (ctm.a == 0 && ctm.d == 0)
  1038. {
  1039. /* In place transform with command rewriting */
  1040. i = 0;
  1041. k = 0;
  1042. while (i < path->cmd_len)
  1043. {
  1044. uint8_t cmd = path->cmds[i];
  1045. switch (cmd)
  1046. {
  1047. case FZ_MOVETO:
  1048. case FZ_LINETO:
  1049. case FZ_MOVETOCLOSE:
  1050. case FZ_LINETOCLOSE:
  1051. n = 1;
  1052. break;
  1053. case FZ_DEGENLINETO:
  1054. case FZ_DEGENLINETOCLOSE:
  1055. n = 0;
  1056. break;
  1057. case FZ_CURVETO:
  1058. case FZ_CURVETOCLOSE:
  1059. n = 3;
  1060. break;
  1061. case FZ_RECTTO:
  1062. s.x = path->coords[k];
  1063. s.y = path->coords[k+1];
  1064. n = 2;
  1065. break;
  1066. case FZ_CURVETOV:
  1067. case FZ_CURVETOY:
  1068. case FZ_QUADTO:
  1069. case FZ_CURVETOVCLOSE:
  1070. case FZ_CURVETOYCLOSE:
  1071. case FZ_QUADTOCLOSE:
  1072. n = 2;
  1073. break;
  1074. case FZ_HORIZTO:
  1075. q.x = path->coords[k];
  1076. p = fz_transform_point(q, ctm);
  1077. path->coords[k++] = p.y;
  1078. path->cmds[i] = FZ_VERTTO;
  1079. n = 0;
  1080. break;
  1081. case FZ_HORIZTOCLOSE:
  1082. q.x = path->coords[k];
  1083. p = fz_transform_point(q, ctm);
  1084. path->coords[k++] = p.y;
  1085. path->cmds[i] = FZ_VERTTOCLOSE;
  1086. n = 0;
  1087. break;
  1088. case FZ_VERTTO:
  1089. q.y = path->coords[k];
  1090. p = fz_transform_point(q, ctm);
  1091. path->coords[k++] = p.x;
  1092. path->cmds[i] = FZ_HORIZTO;
  1093. n = 0;
  1094. break;
  1095. case FZ_VERTTOCLOSE:
  1096. q.y = path->coords[k];
  1097. p = fz_transform_point(q, ctm);
  1098. path->coords[k++] = p.x;
  1099. path->cmds[i] = FZ_HORIZTOCLOSE;
  1100. n = 0;
  1101. break;
  1102. default:
  1103. assert("Unknown path cmd" == NULL);
  1104. }
  1105. while (n > 0)
  1106. {
  1107. q.x = path->coords[k];
  1108. q.y = path->coords[k+1];
  1109. p = fz_transform_point(q, ctm);
  1110. path->coords[k++] = p.x;
  1111. path->coords[k++] = p.y;
  1112. n--;
  1113. }
  1114. switch (cmd)
  1115. {
  1116. case FZ_MOVETO:
  1117. case FZ_MOVETOCLOSE:
  1118. s = q;
  1119. break;
  1120. case FZ_LINETOCLOSE:
  1121. case FZ_DEGENLINETOCLOSE:
  1122. case FZ_CURVETOCLOSE:
  1123. case FZ_CURVETOVCLOSE:
  1124. case FZ_CURVETOYCLOSE:
  1125. case FZ_QUADTOCLOSE:
  1126. case FZ_HORIZTOCLOSE:
  1127. case FZ_VERTTOCLOSE:
  1128. case FZ_RECTTO:
  1129. q = s;
  1130. break;
  1131. }
  1132. i++;
  1133. }
  1134. }
  1135. else
  1136. {
  1137. int extra_coord = 0;
  1138. int extra_cmd = 0;
  1139. int coord_read, coord_write, cmd_read, cmd_write;
  1140. /* General case. Have to allow for rects/horiz/verts
  1141. * becoming non-rects/horiz/verts. */
  1142. for (i = 0; i < path->cmd_len; i++)
  1143. {
  1144. uint8_t cmd = path->cmds[i];
  1145. switch (cmd)
  1146. {
  1147. case FZ_HORIZTO:
  1148. case FZ_VERTTO:
  1149. case FZ_HORIZTOCLOSE:
  1150. case FZ_VERTTOCLOSE:
  1151. extra_coord += 1;
  1152. break;
  1153. case FZ_RECTTO:
  1154. extra_coord += 2;
  1155. extra_cmd += 3;
  1156. break;
  1157. default:
  1158. /* Do nothing */
  1159. break;
  1160. }
  1161. }
  1162. if (path->cmd_len + extra_cmd < path->cmd_cap)
  1163. {
  1164. path->cmds = fz_realloc_array(ctx, path->cmds, path->cmd_len + extra_cmd, unsigned char);
  1165. path->cmd_cap = path->cmd_len + extra_cmd;
  1166. }
  1167. if (path->coord_len + extra_coord < path->coord_cap)
  1168. {
  1169. path->coords = fz_realloc_array(ctx, path->coords, path->coord_len + extra_coord, float);
  1170. path->coord_cap = path->coord_len + extra_coord;
  1171. }
  1172. memmove(path->cmds + extra_cmd, path->cmds, path->cmd_len * sizeof(unsigned char));
  1173. path->cmd_len += extra_cmd;
  1174. memmove(path->coords + extra_coord, path->coords, path->coord_len * sizeof(float));
  1175. path->coord_len += extra_coord;
  1176. for (cmd_write = 0, cmd_read = extra_cmd, coord_write = 0, coord_read = extra_coord; cmd_read < path->cmd_len; i += 2)
  1177. {
  1178. uint8_t cmd = path->cmds[cmd_write++] = path->cmds[cmd_read++];
  1179. switch (cmd)
  1180. {
  1181. case FZ_MOVETO:
  1182. case FZ_LINETO:
  1183. case FZ_MOVETOCLOSE:
  1184. case FZ_LINETOCLOSE:
  1185. n = 1;
  1186. break;
  1187. case FZ_DEGENLINETO:
  1188. case FZ_DEGENLINETOCLOSE:
  1189. n = 0;
  1190. break;
  1191. case FZ_CURVETO:
  1192. case FZ_CURVETOCLOSE:
  1193. n = 3;
  1194. break;
  1195. case FZ_CURVETOV:
  1196. case FZ_CURVETOY:
  1197. case FZ_QUADTO:
  1198. case FZ_CURVETOVCLOSE:
  1199. case FZ_CURVETOYCLOSE:
  1200. case FZ_QUADTOCLOSE:
  1201. n = 2;
  1202. break;
  1203. case FZ_RECTTO:
  1204. p.x = path->coords[coord_read++];
  1205. p.y = path->coords[coord_read++];
  1206. p2.x = path->coords[coord_read++];
  1207. p2.y = path->coords[coord_read++];
  1208. p1.x = p2.x;
  1209. p1.y = p.y;
  1210. p3.x = p.x;
  1211. p3.y = p2.y;
  1212. s = p;
  1213. p = fz_transform_point(p, ctm);
  1214. p1 = fz_transform_point(p1, ctm);
  1215. p2 = fz_transform_point(p2, ctm);
  1216. p3 = fz_transform_point(p3, ctm);
  1217. path->coords[coord_write++] = p.x;
  1218. path->coords[coord_write++] = p.y;
  1219. path->coords[coord_write++] = p1.x;
  1220. path->coords[coord_write++] = p1.y;
  1221. path->coords[coord_write++] = p2.x;
  1222. path->coords[coord_write++] = p2.y;
  1223. path->coords[coord_write++] = p3.x;
  1224. path->coords[coord_write++] = p3.y;
  1225. path->cmds[cmd_write-1] = FZ_MOVETO;
  1226. path->cmds[cmd_write++] = FZ_LINETO;
  1227. path->cmds[cmd_write++] = FZ_LINETO;
  1228. path->cmds[cmd_write++] = FZ_LINETOCLOSE;
  1229. n = 0;
  1230. break;
  1231. case FZ_HORIZTO:
  1232. q.x = path->coords[coord_read++];
  1233. p = fz_transform_point(q, ctm);
  1234. path->coords[coord_write++] = p.x;
  1235. path->coords[coord_write++] = p.y;
  1236. path->cmds[cmd_write-1] = FZ_LINETO;
  1237. n = 0;
  1238. break;
  1239. case FZ_HORIZTOCLOSE:
  1240. p.x = path->coords[coord_read++];
  1241. p.y = q.y;
  1242. p = fz_transform_point(p, ctm);
  1243. path->coords[coord_write++] = p.x;
  1244. path->coords[coord_write++] = p.y;
  1245. path->cmds[cmd_write-1] = FZ_LINETOCLOSE;
  1246. q = s;
  1247. n = 0;
  1248. break;
  1249. case FZ_VERTTO:
  1250. q.y = path->coords[coord_read++];
  1251. p = fz_transform_point(q, ctm);
  1252. path->coords[coord_write++] = p.x;
  1253. path->coords[coord_write++] = p.y;
  1254. path->cmds[cmd_write-1] = FZ_LINETO;
  1255. n = 0;
  1256. break;
  1257. case FZ_VERTTOCLOSE:
  1258. p.x = q.x;
  1259. p.y = path->coords[coord_read++];
  1260. p = fz_transform_point(p, ctm);
  1261. path->coords[coord_write++] = p.x;
  1262. path->coords[coord_write++] = p.y;
  1263. path->cmds[cmd_write-1] = FZ_LINETOCLOSE;
  1264. q = s;
  1265. n = 0;
  1266. break;
  1267. default:
  1268. assert("Unknown path cmd" == NULL);
  1269. }
  1270. while (n > 0)
  1271. {
  1272. q.x = path->coords[coord_read++];
  1273. q.y = path->coords[coord_read++];
  1274. p = fz_transform_point(q, ctm);
  1275. path->coords[coord_write++] = p.x;
  1276. path->coords[coord_write++] = p.y;
  1277. n--;
  1278. }
  1279. switch (cmd)
  1280. {
  1281. case FZ_MOVETO:
  1282. case FZ_MOVETOCLOSE:
  1283. s = q;
  1284. break;
  1285. case FZ_LINETOCLOSE:
  1286. case FZ_DEGENLINETOCLOSE:
  1287. case FZ_CURVETOCLOSE:
  1288. case FZ_CURVETOYCLOSE:
  1289. case FZ_CURVETOVCLOSE:
  1290. case FZ_QUADTOCLOSE:
  1291. case FZ_HORIZTOCLOSE:
  1292. case FZ_VERTTOCLOSE:
  1293. case FZ_RECTTO:
  1294. q = s;
  1295. break;
  1296. }
  1297. }
  1298. }
  1299. }
  1300. void fz_trim_path(fz_context *ctx, fz_path *path)
  1301. {
  1302. if (path->packed)
  1303. fz_throw(ctx, FZ_ERROR_ARGUMENT, "Can't trim a packed path");
  1304. if (path->cmd_cap > path->cmd_len)
  1305. {
  1306. path->cmds = fz_realloc_array(ctx, path->cmds, path->cmd_len, unsigned char);
  1307. path->cmd_cap = path->cmd_len;
  1308. }
  1309. if (path->coord_cap > path->coord_len)
  1310. {
  1311. path->coords = fz_realloc_array(ctx, path->coords, path->coord_len, float);
  1312. path->coord_cap = path->coord_len;
  1313. }
  1314. }
  1315. const fz_stroke_state fz_default_stroke_state = {
  1316. -2, /* -2 is the magic number we use when we have stroke states stored on the stack */
  1317. FZ_LINECAP_BUTT, FZ_LINECAP_BUTT, FZ_LINECAP_BUTT,
  1318. FZ_LINEJOIN_MITER,
  1319. 1, 10,
  1320. 0, 0, { 0 }
  1321. };
  1322. fz_stroke_state *
  1323. fz_keep_stroke_state(fz_context *ctx, const fz_stroke_state *strokec)
  1324. {
  1325. fz_stroke_state *stroke = (fz_stroke_state *)strokec; /* Explicit cast away of const */
  1326. if (!stroke)
  1327. return NULL;
  1328. /* -2 is the magic number we use when we have stroke states stored on the stack */
  1329. if (stroke->refs == -2)
  1330. return fz_clone_stroke_state(ctx, stroke);
  1331. return fz_keep_imp(ctx, stroke, &stroke->refs);
  1332. }
  1333. int
  1334. fz_stroke_state_eq(fz_context *ctx, const fz_stroke_state *a, const fz_stroke_state *b)
  1335. {
  1336. int i;
  1337. if (a == NULL && b == NULL) return 1;
  1338. if (a == NULL && b != NULL) return 0;
  1339. if (a != NULL && b == NULL) return 0;
  1340. if (a->start_cap != b->start_cap) return 0;
  1341. if (a->dash_cap != b->dash_cap) return 0;
  1342. if (a->end_cap != b->end_cap) return 0;
  1343. if (a->linejoin != b->linejoin) return 0;
  1344. if (a->linewidth != b->linewidth) return 0;
  1345. if (a->miterlimit != b->miterlimit) return 0;
  1346. if (a->dash_phase != b->dash_phase) return 0;
  1347. if (a->dash_len != b->dash_len) return 0;
  1348. for (i = 0; i < a->dash_len; i++)
  1349. if (a->dash_list[i] != b->dash_list[i]) return 0;
  1350. return 1;
  1351. }
  1352. void
  1353. fz_drop_stroke_state(fz_context *ctx, const fz_stroke_state *strokec)
  1354. {
  1355. fz_stroke_state *stroke = (fz_stroke_state *)strokec; /* Explicit cast away of const */
  1356. if (fz_drop_imp(ctx, stroke, &stroke->refs))
  1357. fz_free(ctx, stroke);
  1358. }
  1359. fz_stroke_state *
  1360. fz_new_stroke_state_with_dash_len(fz_context *ctx, int len)
  1361. {
  1362. fz_stroke_state *state;
  1363. if (len < 0)
  1364. len = 0;
  1365. state = fz_malloc_flexible(ctx, fz_stroke_state, dash_list, len);
  1366. state->refs = 1;
  1367. state->start_cap = FZ_LINECAP_BUTT;
  1368. state->dash_cap = FZ_LINECAP_BUTT;
  1369. state->end_cap = FZ_LINECAP_BUTT;
  1370. state->linejoin = FZ_LINEJOIN_MITER;
  1371. state->linewidth = 1;
  1372. state->miterlimit = 10;
  1373. state->dash_phase = 0;
  1374. state->dash_len = 0;
  1375. return state;
  1376. }
  1377. fz_stroke_state *
  1378. fz_new_stroke_state(fz_context *ctx)
  1379. {
  1380. return fz_new_stroke_state_with_dash_len(ctx, 0);
  1381. }
  1382. fz_linecap
  1383. fz_linecap_from_string(const char *str)
  1384. {
  1385. if (!strcmp(str, "Round"))
  1386. return FZ_LINECAP_ROUND;
  1387. if (!strcmp(str, "Square"))
  1388. return FZ_LINECAP_SQUARE;
  1389. if (!strcmp(str, "Triangle"))
  1390. return FZ_LINECAP_TRIANGLE;
  1391. return FZ_LINECAP_BUTT;
  1392. }
  1393. const char *
  1394. fz_string_from_linecap(fz_linecap cap)
  1395. {
  1396. switch (cap) {
  1397. default:
  1398. case FZ_LINECAP_BUTT: return "Butt";
  1399. case FZ_LINECAP_ROUND: return "Round";
  1400. case FZ_LINECAP_SQUARE: return "Square";
  1401. case FZ_LINECAP_TRIANGLE: return "Triangle";
  1402. }
  1403. }
  1404. fz_linejoin
  1405. fz_linejoin_from_string(const char *str)
  1406. {
  1407. if (!strcmp(str, "Round"))
  1408. return FZ_LINEJOIN_ROUND;
  1409. if (!strcmp(str, "Bevel"))
  1410. return FZ_LINEJOIN_BEVEL;
  1411. if (!strcmp(str, "MiterXPS"))
  1412. return FZ_LINEJOIN_MITER_XPS;
  1413. return FZ_LINEJOIN_MITER;
  1414. }
  1415. const char *
  1416. fz_string_from_linejoin(fz_linejoin join)
  1417. {
  1418. switch (join) {
  1419. default:
  1420. case FZ_LINEJOIN_MITER: return "Miter";
  1421. case FZ_LINEJOIN_ROUND: return "Round";
  1422. case FZ_LINEJOIN_BEVEL: return "Bevel";
  1423. case FZ_LINEJOIN_MITER_XPS: return "MiterXPS";
  1424. }
  1425. }
  1426. fz_stroke_state *
  1427. fz_clone_stroke_state(fz_context *ctx, const fz_stroke_state *stroke)
  1428. {
  1429. fz_stroke_state *clone = fz_new_stroke_state_with_dash_len(ctx, stroke->dash_len);
  1430. size_t size = offsetof(fz_stroke_state, dash_list) + sizeof(float) * stroke->dash_len;
  1431. memcpy(clone, stroke, size);
  1432. clone->refs = 1;
  1433. return clone;
  1434. }
  1435. fz_stroke_state *
  1436. fz_unshare_stroke_state_with_dash_len(fz_context *ctx, fz_stroke_state *shared, int len)
  1437. {
  1438. int single;
  1439. fz_stroke_state *unshared;
  1440. fz_lock(ctx, FZ_LOCK_ALLOC);
  1441. single = (shared->refs == 1);
  1442. fz_unlock(ctx, FZ_LOCK_ALLOC);
  1443. if (single && len == shared->dash_len)
  1444. return shared;
  1445. unshared = fz_new_stroke_state_with_dash_len(ctx, len);
  1446. if (shared->dash_len >= len)
  1447. memcpy(unshared, shared, offsetof(fz_stroke_state, dash_list) + sizeof(float) * len);
  1448. else
  1449. memcpy(unshared, shared, offsetof(fz_stroke_state, dash_list));
  1450. unshared->refs = 1;
  1451. unshared->dash_len = len;
  1452. if (fz_drop_imp(ctx, shared, &shared->refs))
  1453. fz_free(ctx, shared);
  1454. return unshared;
  1455. }
  1456. fz_stroke_state *
  1457. fz_unshare_stroke_state(fz_context *ctx, fz_stroke_state *shared)
  1458. {
  1459. return fz_unshare_stroke_state_with_dash_len(ctx, shared, shared->dash_len);
  1460. }
  1461. static void *
  1462. clone_block(fz_context *ctx, void *block, size_t len)
  1463. {
  1464. void *target;
  1465. if (len == 0 || block == NULL)
  1466. return NULL;
  1467. target = fz_malloc(ctx, len);
  1468. memcpy(target, block, len);
  1469. return target;
  1470. }
  1471. fz_path *
  1472. fz_clone_path(fz_context *ctx, fz_path *path)
  1473. {
  1474. fz_path *new_path;
  1475. assert(ctx != NULL);
  1476. if (path == NULL)
  1477. return NULL;
  1478. new_path = fz_malloc_struct(ctx, fz_path);
  1479. new_path->refs = 1;
  1480. new_path->packed = FZ_PATH_UNPACKED;
  1481. fz_try(ctx)
  1482. {
  1483. switch(path->packed)
  1484. {
  1485. case FZ_PATH_UNPACKED:
  1486. case FZ_PATH_PACKED_OPEN:
  1487. new_path->cmd_len = path->cmd_len;
  1488. new_path->cmd_cap = path->cmd_cap;
  1489. new_path->cmds = Memento_label(clone_block(ctx, path->cmds, path->cmd_cap), "path_cmds");
  1490. new_path->coord_len = path->coord_len;
  1491. new_path->coord_cap = path->coord_cap;
  1492. new_path->coords = Memento_label(clone_block(ctx, path->coords, sizeof(float)*path->coord_cap), "path_coords");
  1493. new_path->current = path->current;
  1494. new_path->begin = path->begin;
  1495. break;
  1496. case FZ_PATH_PACKED_FLAT:
  1497. {
  1498. uint8_t *data;
  1499. float *xy;
  1500. int i;
  1501. fz_packed_path *ppath = (fz_packed_path *)path;
  1502. new_path->cmd_len = ppath->cmd_len;
  1503. new_path->cmd_cap = ppath->cmd_len;
  1504. new_path->coord_len = ppath->coord_len;
  1505. new_path->coord_cap = ppath->coord_len;
  1506. data = (uint8_t *)&ppath[1];
  1507. new_path->coords = Memento_label(clone_block(ctx, data, sizeof(float)*path->coord_cap), "path_coords");
  1508. data += sizeof(float) * path->coord_cap;
  1509. new_path->cmds = Memento_label(clone_block(ctx, data, path->cmd_cap), "path_cmds");
  1510. xy = new_path->coords;
  1511. for (i = 0; i < new_path->cmd_len; i++)
  1512. {
  1513. switch (new_path->cmds[i])
  1514. {
  1515. case FZ_MOVETOCLOSE:
  1516. case FZ_MOVETO:
  1517. new_path->current.x = *xy++;
  1518. new_path->current.y = *xy++;
  1519. new_path->begin.x = new_path->current.x;
  1520. new_path->begin.y = new_path->current.y;
  1521. break;
  1522. case FZ_CURVETO:
  1523. xy += 2;
  1524. /* fallthrough */
  1525. case FZ_CURVETOV:
  1526. case FZ_CURVETOY:
  1527. case FZ_QUADTO:
  1528. /* fallthrough */
  1529. xy += 2;
  1530. case FZ_LINETO:
  1531. new_path->current.x = *xy++;
  1532. new_path->current.y = *xy++;
  1533. break;
  1534. case FZ_DEGENLINETO:
  1535. break;
  1536. case FZ_HORIZTO:
  1537. new_path->current.x = *xy++;
  1538. break;
  1539. case FZ_VERTTO:
  1540. new_path->current.y = *xy++;
  1541. break;
  1542. case FZ_RECTTO:
  1543. xy += 2;
  1544. break;
  1545. case FZ_CURVETOCLOSE:
  1546. xy += 2;
  1547. /* fallthrough */
  1548. case FZ_CURVETOVCLOSE:
  1549. case FZ_CURVETOYCLOSE:
  1550. case FZ_QUADTOCLOSE:
  1551. case FZ_LINETOCLOSE:
  1552. xy++;
  1553. /* fallthrough */
  1554. case FZ_HORIZTOCLOSE:
  1555. case FZ_VERTTOCLOSE:
  1556. xy++;
  1557. /* fallthrough */
  1558. case FZ_DEGENLINETOCLOSE:
  1559. new_path->current.x = new_path->begin.x;
  1560. new_path->current.y = new_path->begin.y;
  1561. break;
  1562. }
  1563. }
  1564. }
  1565. default:
  1566. assert(!"Unknown packing method found in path");
  1567. }
  1568. }
  1569. fz_catch(ctx)
  1570. {
  1571. fz_free(ctx, new_path->coords);
  1572. fz_free(ctx, new_path->cmds);
  1573. fz_free(ctx, new_path);
  1574. fz_rethrow(ctx);
  1575. }
  1576. return new_path;
  1577. }
  1578. typedef struct
  1579. {
  1580. fz_matrix ctm;
  1581. fz_point p[4];
  1582. int count;
  1583. int trailing_move;
  1584. } rect_path_arg;
  1585. static void
  1586. rect_moveto(fz_context *ctx, void *arg_, float x, float y)
  1587. {
  1588. rect_path_arg *arg = (rect_path_arg *)arg_;
  1589. fz_point p = fz_transform_point_xy(x, y, arg->ctm);
  1590. /* If we've already decided that it's not a rectangle. Just exit. */
  1591. if (arg->count < 0)
  1592. return;
  1593. /* We should never get multiple successive moves, by construction. */
  1594. /* If we're starting out... */
  1595. if (arg->count == 0)
  1596. {
  1597. arg->p[0] = p;
  1598. arg->count = 1;
  1599. return;
  1600. }
  1601. /* Otherwise, any move is fine, as long as it's not followed by another line... */
  1602. arg->trailing_move = 1;
  1603. }
  1604. static void
  1605. rect_lineto(fz_context *ctx, void *arg_, float x, float y)
  1606. {
  1607. rect_path_arg *arg = (rect_path_arg *)arg_;
  1608. fz_point p = fz_transform_point_xy(x, y, arg->ctm);
  1609. /* If we've already decided that it's not a rectangle. Just exit. */
  1610. if (arg->count < 0)
  1611. return;
  1612. if (arg->trailing_move)
  1613. {
  1614. arg->count = -1;
  1615. return;
  1616. }
  1617. /* Watch for pesky lines back to the same place. */
  1618. if (arg->p[arg->count-1].x == p.x && arg->p[arg->count-1].y == p.y)
  1619. return;
  1620. if (arg->count < 4)
  1621. {
  1622. arg->p[arg->count++] = p;
  1623. return;
  1624. }
  1625. /* Allow for lines back to the start. */
  1626. if (arg->count == 4)
  1627. {
  1628. if (arg->p[0].x == p.x && arg->p[0].y == p.y)
  1629. {
  1630. arg->count++;
  1631. return;
  1632. }
  1633. }
  1634. arg->count = -1;
  1635. }
  1636. static void
  1637. rect_curveto(fz_context *ctx, void *arg_, float x1, float y1, float x2, float y2, float x3, float y3)
  1638. {
  1639. rect_path_arg *arg = (rect_path_arg *)arg_;
  1640. arg->count = -1;
  1641. }
  1642. static const fz_path_walker rect_path_walker =
  1643. {
  1644. rect_moveto,
  1645. rect_lineto,
  1646. rect_curveto,
  1647. NULL
  1648. };
  1649. int
  1650. fz_path_is_rect(fz_context *ctx, const fz_path *path, fz_matrix ctm)
  1651. {
  1652. return fz_path_is_rect_with_bounds(ctx, path, ctm, NULL);
  1653. }
  1654. int
  1655. fz_path_is_rect_with_bounds(fz_context *ctx, const fz_path *path, fz_matrix ctm, fz_rect *bounds)
  1656. {
  1657. rect_path_arg arg;
  1658. arg.ctm = ctm;
  1659. arg.trailing_move = 0;
  1660. arg.count = 0;
  1661. fz_walk_path(ctx, path, &rect_path_walker, &arg);
  1662. if (arg.count < 0)
  1663. return 0;
  1664. /* 3 entries are bad, unless the last one returns the first. */
  1665. if (arg.count == 3 && (arg.p[0].x != arg.p[2].x || arg.p[0].y != arg.p[2].y))
  1666. {
  1667. return 0;
  1668. }
  1669. if (arg.count == 2 || arg.count == 3)
  1670. {
  1671. if (arg.p[0].x == arg.p[1].x || arg.p[0].y == arg.p[1].y)
  1672. {
  1673. if (bounds)
  1674. {
  1675. bounds->x0 = fz_min(arg.p[0].x, arg.p[1].x);
  1676. bounds->x1 = fz_max(arg.p[0].x, arg.p[1].x);
  1677. bounds->y0 = fz_min(arg.p[0].y, arg.p[1].y);
  1678. bounds->y1 = fz_max(arg.p[0].y, arg.p[1].y);
  1679. }
  1680. return 1;
  1681. }
  1682. }
  1683. /* All that's left are 4 entry ones */
  1684. if (arg.count != 4)
  1685. return 0;
  1686. if (arg.p[0].x == arg.p[1].x)
  1687. {
  1688. /* p[0] p[3]
  1689. * p[1] p[2]
  1690. */
  1691. if (arg.p[1].y == arg.p[2].y && arg.p[0].y == arg.p[3].y && arg.p[2].x == arg.p[3].x)
  1692. {
  1693. if (bounds)
  1694. {
  1695. bounds->x0 = fz_min(arg.p[0].x, arg.p[3].x);
  1696. bounds->x1 = fz_max(arg.p[0].x, arg.p[3].x);
  1697. bounds->y0 = fz_min(arg.p[0].y, arg.p[1].y);
  1698. bounds->y1 = fz_max(arg.p[0].y, arg.p[1].y);
  1699. }
  1700. return 1;
  1701. }
  1702. }
  1703. if (arg.p[0].y == arg.p[1].y)
  1704. {
  1705. /* p[0] p[1]
  1706. * p[3] p[2]
  1707. */
  1708. if (arg.p[1].x == arg.p[2].x && arg.p[0].x == arg.p[3].x && arg.p[2].y == arg.p[3].y)
  1709. {
  1710. if (bounds)
  1711. {
  1712. bounds->x0 = fz_min(arg.p[0].x, arg.p[1].x);
  1713. bounds->x1 = fz_max(arg.p[0].x, arg.p[1].x);
  1714. bounds->y0 = fz_min(arg.p[0].y, arg.p[3].y);
  1715. bounds->y1 = fz_max(arg.p[0].y, arg.p[3].y);
  1716. }
  1717. return 1;
  1718. }
  1719. }
  1720. return 0;
  1721. }