strokefill.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright 2011 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "gm/gm.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkFont.h"
  11. #include "include/core/SkFontStyle.h"
  12. #include "include/core/SkPaint.h"
  13. #include "include/core/SkPath.h"
  14. #include "include/core/SkRect.h"
  15. #include "include/core/SkScalar.h"
  16. #include "include/core/SkString.h"
  17. #include "include/core/SkTypeface.h"
  18. #include "include/core/SkTypes.h"
  19. #include "src/core/SkPathPriv.h"
  20. #include "src/core/SkTextFormatParams.h"
  21. #include "tools/ToolUtils.h"
  22. /* Generated on a Mac with:
  23. * paint.setTypeface(SkTypeface::CreateByName("Papyrus"));
  24. * paint.getTextPath("H", 1, 100, 80, &textPath);
  25. */
  26. static SkPath papyrus_hello() {
  27. SkPath path;
  28. path.moveTo(169.824f, 83.4102f);
  29. path.lineTo(167.285f, 85.6074f);
  30. path.lineTo(166.504f, 87.2188f);
  31. path.lineTo(165.82f, 86.7793f);
  32. path.lineTo(165.186f, 87.1211f);
  33. path.lineTo(164.6f, 88.1953f);
  34. path.lineTo(161.914f, 89.416f);
  35. path.lineTo(161.719f, 89.2207f);
  36. path.lineTo(160.596f, 88.8789f);
  37. path.lineTo(160.498f, 87.6094f);
  38. path.lineTo(160.693f, 86.3887f);
  39. path.lineTo(161.621f, 84.6797f);
  40. path.lineTo(161.279f, 83.2148f);
  41. path.lineTo(161.523f, 81.9941f);
  42. path.lineTo(162.012f, 79.1133f);
  43. path.lineTo(162.695f, 76.623f);
  44. path.lineTo(162.305f, 73.4004f);
  45. path.lineTo(162.207f, 72.4238f);
  46. path.lineTo(163.477f, 71.4961f);
  47. path.quadTo(163.525f, 71.0078f, 163.525f, 70.2754f);
  48. path.quadTo(163.525f, 68.8594f, 162.793f, 67.1992f);
  49. path.lineTo(163.623f, 64.6113f);
  50. path.lineTo(162.598f, 63.6836f);
  51. path.lineTo(163.184f, 61.0957f);
  52. path.lineTo(162.695f, 60.8027f);
  53. path.lineTo(162.988f, 52.5996f);
  54. path.lineTo(162.402f, 52.1113f);
  55. path.lineTo(161.914f, 50.1094f);
  56. path.lineTo(161.523f, 50.5f);
  57. path.quadTo(160.645f, 50.5f, 160.327f, 50.6465f);
  58. path.quadTo(160.01f, 50.793f, 159.424f, 51.4766f);
  59. path.lineTo(158.203f, 52.7949f);
  60. path.lineTo(156.299f, 52.0137f);
  61. path.quadTo(154.346f, 52.5996f, 154.004f, 52.5996f);
  62. path.quadTo(154.053f, 52.5996f, 152.783f, 52.4043f);
  63. path.quadTo(151.465f, 51.3789f, 150.488f, 51.3789f);
  64. path.quadTo(150.342f, 51.3789f, 150.269f, 51.4033f);
  65. path.quadTo(150.195f, 51.4277f, 150.098f, 51.4766f);
  66. path.lineTo(145.02f, 51.916f);
  67. path.lineTo(139.893f, 51.2812f);
  68. path.lineTo(135.693f, 51.623f);
  69. path.lineTo(133.594f, 51.0859f);
  70. path.lineTo(130.42f, 52.0137f);
  71. path.lineTo(125.488f, 51.4766f);
  72. path.lineTo(124.219f, 51.623f);
  73. path.lineTo(122.705f, 50.5f);
  74. path.lineTo(122.217f, 51.7207f);
  75. path.lineTo(116.211f, 51.623f);
  76. path.quadTo(114.99f, 52.7949f, 114.99f, 53.8203f);
  77. path.lineTo(115.576f, 57.6777f);
  78. path.quadTo(115.723f, 58.2148f, 115.723f, 59.2891f);
  79. path.quadTo(115.723f, 60.2656f, 115.186f, 61.1934f);
  80. path.lineTo(115.479f, 64.2207f);
  81. path.quadTo(114.795f, 64.6602f, 114.648f, 65.0752f);
  82. path.quadTo(114.502f, 65.4902f, 114.502f, 66.3203f);
  83. path.quadTo(114.893f, 66.9551f, 115.918f, 67.1992f);
  84. path.lineTo(116.016f, 69.2988f);
  85. path.lineTo(116.016f, 75.6953f);
  86. path.lineTo(116.016f, 75.9883f);
  87. path.lineTo(116.113f, 77.209f);
  88. path.quadTo(116.113f, 77.6484f, 115.479f, 78.0879f);
  89. path.lineTo(115.576f, 79.1133f);
  90. path.lineTo(116.309f, 82.0918f);
  91. path.lineTo(116.406f, 83.1172f);
  92. path.quadTo(116.406f, 85.2656f, 114.404f, 86.291f);
  93. path.lineTo(111.914f, 87.5117f);
  94. path.lineTo(109.717f, 88.7812f);
  95. path.lineTo(108.398f, 89.416f);
  96. path.lineTo(108.105f, 88.9766f);
  97. path.lineTo(107.617f, 88.9766f);
  98. path.quadTo(107.08f, 87.9023f, 107.08f, 87.0234f);
  99. path.quadTo(107.08f, 85.998f, 107.324f, 85.6074f);
  100. path.lineTo(108.398f, 83.1172f);
  101. path.lineTo(108.887f, 81.4082f);
  102. path.lineTo(109.619f, 81.1152f);
  103. path.lineTo(109.717f, 79.5039f);
  104. path.lineTo(108.887f, 78.7227f);
  105. path.quadTo(109.619f, 77.3555f, 109.619f, 77.1113f);
  106. path.quadTo(109.619f, 76.8672f, 108.887f, 75.5977f);
  107. path.lineTo(108.789f, 74.7188f);
  108. path.lineTo(109.18f, 66.6133f);
  109. path.lineTo(108.691f, 64.416f);
  110. path.quadTo(108.691f, 63.293f, 109.521f, 62.707f);
  111. path.lineTo(110.205f, 62.1211f);
  112. path.quadTo(110.449f, 61.877f, 110.596f, 60.998f);
  113. path.quadTo(109.082f, 60.4609f, 109.082f, 59.1914f);
  114. path.lineTo(109.082f, 58.9961f);
  115. path.lineTo(109.424f, 55.2852f);
  116. path.lineTo(108.789f, 53.7227f);
  117. path.lineTo(108.789f, 48.1074f);
  118. path.lineTo(108.594f, 45.3242f);
  119. path.lineTo(108.691f, 43.8105f);
  120. path.lineTo(107.91f, 43.9082f);
  121. path.lineTo(107.715f, 44.1035f);
  122. path.lineTo(107.324f, 42.7852f);
  123. path.lineTo(107.715f, 43.1758f);
  124. path.lineTo(107.91f, 41.3203f);
  125. path.quadTo(109.717f, 40.2949f, 109.717f, 39.2207f);
  126. path.quadTo(109.717f, 37.707f, 107.715f, 37.4141f);
  127. path.lineTo(106.982f, 33.9961f);
  128. path.lineTo(106.982f, 33.2148f);
  129. path.lineTo(106.689f, 29.3086f);
  130. path.lineTo(106.689f, 26.1836f);
  131. path.lineTo(107.91f, 25.6953f);
  132. path.lineTo(109.521f, 24.5234f);
  133. path.lineTo(112.109f, 23.3027f);
  134. path.lineTo(114.014f, 22.5215f);
  135. path.lineTo(115.479f, 23.1074f);
  136. path.quadTo(115.479f, 24.0352f, 116.113f, 25.1094f);
  137. path.quadTo(115.381f, 25.8906f, 115.259f, 26.3789f);
  138. path.quadTo(115.137f, 26.8672f, 114.99f, 28.8203f);
  139. path.lineTo(114.893f, 29.9922f);
  140. path.lineTo(115.82f, 33.4102f);
  141. path.lineTo(116.016f, 35.4121f);
  142. path.lineTo(115.576f, 35.4121f);
  143. path.lineTo(114.697f, 37.707f);
  144. path.lineTo(115.381f, 38.4883f);
  145. path.lineTo(115.186f, 39.5137f);
  146. path.lineTo(114.697f, 40.3926f);
  147. path.lineTo(114.209f, 41.418f);
  148. path.lineTo(114.404f, 42.4922f);
  149. path.lineTo(114.795f, 43.3223f);
  150. path.quadTo(115.186f, 44.1035f, 115.186f, 46.4961f);
  151. path.lineTo(116.309f, 46.7891f);
  152. path.lineTo(125, 47.2773f);
  153. path.lineTo(126.318f, 48.2051f);
  154. path.lineTo(129.59f, 48.5957f);
  155. path.lineTo(130.615f, 48.498f);
  156. path.lineTo(130.908f, 48.0098f);
  157. path.lineTo(134.277f, 48.2051f);
  158. path.lineTo(134.717f, 48.3027f);
  159. path.quadTo(135.4f, 47.7168f, 136.084f, 47.7168f);
  160. path.lineTo(137.109f, 47.8145f);
  161. path.lineTo(137.109f, 48.2051f);
  162. path.lineTo(137.695f, 48.5957f);
  163. path.lineTo(138.818f, 48.498f);
  164. path.lineTo(144.189f, 48.4004f);
  165. path.lineTo(146.191f, 48.2051f);
  166. path.lineTo(147.998f, 48.791f);
  167. path.quadTo(148.877f, 47.5215f, 150.098f, 47.5215f);
  168. path.lineTo(150.293f, 47.5215f);
  169. path.lineTo(152.783f, 47.7168f);
  170. path.lineTo(157.52f, 47.2773f);
  171. path.lineTo(160.303f, 47.4238f);
  172. path.lineTo(161.279f, 47.1797f);
  173. path.lineTo(161.621f, 46.1055f);
  174. path.lineTo(162.5f, 43.9082f);
  175. path.lineTo(162.305f, 38.293f);
  176. path.lineTo(162.5f, 37.1211f);
  177. path.lineTo(161.816f, 35.998f);
  178. path.lineTo(160.596f, 34.7773f);
  179. path.lineTo(160.596f, 32.6777f);
  180. path.lineTo(160.303f, 30.4805f);
  181. path.lineTo(160.889f, 29.4062f);
  182. path.quadTo(160.303f, 27.4043f, 160.303f, 26.2812f);
  183. path.quadTo(160.303f, 25.3535f, 160.889f, 25.207f);
  184. path.lineTo(162.207f, 25.0117f);
  185. path.lineTo(163.721f, 23.8887f);
  186. path.lineTo(164.893f, 23.8887f);
  187. path.quadTo(165.625f, 23.6934f, 166.797f, 22.5215f);
  188. path.lineTo(168.213f, 23.2051f);
  189. path.lineTo(168.701f, 25.5f);
  190. path.lineTo(169.092f, 26.8184f);
  191. path.lineTo(167.676f, 31.9941f);
  192. path.lineTo(168.018f, 34.6797f);
  193. path.lineTo(167.822f, 35.8027f);
  194. path.lineTo(167.285f, 35.8027f);
  195. path.quadTo(166.602f, 35.8027f, 166.602f, 36.7793f);
  196. path.quadTo(166.846f, 37.3652f, 167.578f, 37.707f);
  197. path.lineTo(168.506f, 38.1953f);
  198. path.lineTo(168.799f, 39.5137f);
  199. path.lineTo(169.092f, 41.5156f);
  200. path.lineTo(168.994f, 42.1016f);
  201. path.lineTo(168.213f, 43.6152f);
  202. path.lineTo(168.408f, 52.502f);
  203. path.lineTo(168.213f, 60.1191f);
  204. path.lineTo(168.994f, 61.291f);
  205. path.quadTo(168.604f, 63.0488f, 168.604f, 63.9766f);
  206. path.lineTo(168.604f, 64.123f);
  207. path.lineTo(168.604f, 64.3184f);
  208. path.lineTo(168.604f, 64.9043f);
  209. path.lineTo(168.604f, 66.0762f);
  210. path.quadTo(167.578f, 67.1504f, 167.578f, 67.5898f);
  211. path.quadTo(167.578f, 67.7852f, 167.676f, 67.7852f);
  212. path.lineTo(168.994f, 70.0801f);
  213. path.lineTo(168.701f, 76.7207f);
  214. path.lineTo(169.824f, 83.4102f);
  215. path.close();
  216. return path;
  217. }
  218. /* Generated on a Mac with:
  219. * paint.setTypeface(SkTypeface::CreateByName("Hiragino Maru Gothic Pro"));
  220. * const unsigned char hyphen[] = { 0xE3, 0x83, 0xBC };
  221. * paint.getTextPath(hyphen, SK_ARRAY_COUNT(hyphen), 400, 80, &textPath);
  222. */
  223. static SkPath hiragino_maru_gothic_pro_dash() {
  224. SkPath path;
  225. path.moveTo(488, 55.1f);
  226. path.cubicTo(490.5f, 55.1f, 491.9f, 53.5f, 491.9f, 50.8f);
  227. path.cubicTo(491.9f, 48.2f, 490.5f, 46.3f, 487.9f, 46.3f);
  228. path.lineTo(412, 46.3f);
  229. path.cubicTo(409.4f, 46.3f, 408, 48.2f, 408, 50.8f);
  230. path.cubicTo(408, 53.5f, 409.4f, 55.1f, 411.9f, 55.1f);
  231. path.lineTo(488, 55.1f);
  232. path.close();
  233. return path;
  234. }
  235. static void show_bold(SkCanvas* canvas, const char* text,
  236. SkScalar x, SkScalar y, const SkPaint& paint, const SkFont& font) {
  237. canvas->drawString(text, x, y, font, paint);
  238. SkFont f(font);
  239. f.setEmbolden(true);
  240. canvas->drawString(text, x, y + 120, f, paint);
  241. }
  242. static void path_bold(SkCanvas* canvas, const SkPath& path,
  243. const SkPaint& paint, float textSize) {
  244. SkPaint p(paint);
  245. canvas->drawPath(path, p);
  246. p.setStyle(SkPaint::kStrokeAndFill_Style);
  247. SkScalar fakeBoldScale = SkScalarInterpFunc(textSize,
  248. kStdFakeBoldInterpKeys, kStdFakeBoldInterpValues,
  249. kStdFakeBoldInterpLength);
  250. SkScalar extra = textSize * fakeBoldScale;
  251. p.setStrokeWidth(extra);
  252. canvas->save();
  253. canvas->translate(0, 120);
  254. canvas->drawPath(path, p);
  255. canvas->restore();
  256. }
  257. DEF_SIMPLE_GM_BG_NAME(strokefill, canvas, 640, 480, SK_ColorWHITE,
  258. SkString("stroke-fill")) {
  259. SkScalar x = SkIntToScalar(100);
  260. SkScalar y = SkIntToScalar(88);
  261. // use the portable typeface to generically test the fake bold code everywhere
  262. // (as long as the freetype option to do the bolding itself isn't enabled)
  263. SkFont font(ToolUtils::create_portable_typeface("serif", SkFontStyle()), 100);
  264. SkPaint paint;
  265. paint.setAntiAlias(true);
  266. paint.setStrokeWidth(SkIntToScalar(5));
  267. // use paths instead of text to test the path data on all platforms, since the
  268. // Mac-specific font may change or is not available everywhere
  269. path_bold(canvas, papyrus_hello(), paint, font.getSize());
  270. path_bold(canvas, hiragino_maru_gothic_pro_dash(), paint, font.getSize());
  271. show_bold(canvas, "Hi There", x + SkIntToScalar(430), y, paint, font);
  272. paint.setStyle(SkPaint::kStrokeAndFill_Style);
  273. SkPath path;
  274. path.setFillType(SkPath::kWinding_FillType);
  275. path.addCircle(x, y + SkIntToScalar(200), SkIntToScalar(50), SkPath::kCW_Direction);
  276. path.addCircle(x, y + SkIntToScalar(200), SkIntToScalar(40), SkPath::kCCW_Direction);
  277. canvas->drawPath(path, paint);
  278. SkPath path2;
  279. path2.setFillType(SkPath::kWinding_FillType);
  280. path2.addCircle(x + SkIntToScalar(120), y + SkIntToScalar(200), SkIntToScalar(50), SkPath::kCCW_Direction);
  281. path2.addCircle(x + SkIntToScalar(120), y + SkIntToScalar(200), SkIntToScalar(40), SkPath::kCW_Direction);
  282. canvas->drawPath(path2, paint);
  283. path2.reset();
  284. path2.addCircle(x + SkIntToScalar(240), y + SkIntToScalar(200), SkIntToScalar(50), SkPath::kCCW_Direction);
  285. canvas->drawPath(path2, paint);
  286. SkASSERT(SkPathPriv::CheapIsFirstDirection(path2, SkPathPriv::kCCW_FirstDirection));
  287. path2.reset();
  288. SkASSERT(!SkPathPriv::CheapComputeFirstDirection(path2, nullptr));
  289. path2.addCircle(x + SkIntToScalar(360), y + SkIntToScalar(200), SkIntToScalar(50), SkPath::kCW_Direction);
  290. SkASSERT(SkPathPriv::CheapIsFirstDirection(path2, SkPathPriv::kCW_FirstDirection));
  291. canvas->drawPath(path2, paint);
  292. SkRect r = SkRect::MakeXYWH(x - SkIntToScalar(50), y + SkIntToScalar(280),
  293. SkIntToScalar(100), SkIntToScalar(100));
  294. SkPath path3;
  295. path3.setFillType(SkPath::kWinding_FillType);
  296. path3.addRect(r, SkPath::kCW_Direction);
  297. r.inset(SkIntToScalar(10), SkIntToScalar(10));
  298. path3.addRect(r, SkPath::kCCW_Direction);
  299. canvas->drawPath(path3, paint);
  300. r = SkRect::MakeXYWH(x + SkIntToScalar(70), y + SkIntToScalar(280),
  301. SkIntToScalar(100), SkIntToScalar(100));
  302. SkPath path4;
  303. path4.setFillType(SkPath::kWinding_FillType);
  304. path4.addRect(r, SkPath::kCCW_Direction);
  305. r.inset(SkIntToScalar(10), SkIntToScalar(10));
  306. path4.addRect(r, SkPath::kCW_Direction);
  307. canvas->drawPath(path4, paint);
  308. r = SkRect::MakeXYWH(x + SkIntToScalar(190), y + SkIntToScalar(280),
  309. SkIntToScalar(100), SkIntToScalar(100));
  310. path4.reset();
  311. SkASSERT(!SkPathPriv::CheapComputeFirstDirection(path4, nullptr));
  312. path4.addRect(r, SkPath::kCCW_Direction);
  313. SkASSERT(SkPathPriv::CheapIsFirstDirection(path4, SkPathPriv::kCCW_FirstDirection));
  314. path4.moveTo(0, 0); // test for crbug.com/247770
  315. canvas->drawPath(path4, paint);
  316. r = SkRect::MakeXYWH(x + SkIntToScalar(310), y + SkIntToScalar(280),
  317. SkIntToScalar(100), SkIntToScalar(100));
  318. path4.reset();
  319. SkASSERT(!SkPathPriv::CheapComputeFirstDirection(path4, nullptr));
  320. path4.addRect(r, SkPath::kCW_Direction);
  321. SkASSERT(SkPathPriv::CheapIsFirstDirection(path4, SkPathPriv::kCW_FirstDirection));
  322. path4.moveTo(0, 0); // test for crbug.com/247770
  323. canvas->drawPath(path4, paint);
  324. }
  325. DEF_SIMPLE_GM(bug339297, canvas, 640, 480) {
  326. SkPath path;
  327. path.moveTo(-469515, -10354890);
  328. path.cubicTo(771919.62f, -10411179, 2013360.1f, -10243774, 3195542.8f, -9860664);
  329. path.lineTo(3195550, -9860655);
  330. path.lineTo(3195539, -9860652);
  331. path.lineTo(3195539, -9860652);
  332. path.lineTo(3195539, -9860652);
  333. path.cubicTo(2013358.1f, -10243761, 771919.25f, -10411166, -469513.84f, -10354877);
  334. path.lineTo(-469515, -10354890);
  335. path.close();
  336. canvas->translate(258, 10365663);
  337. SkPaint paint;
  338. paint.setAntiAlias(true);
  339. paint.setColor(SK_ColorBLACK);
  340. paint.setStyle(SkPaint::kFill_Style);
  341. canvas->drawPath(path, paint);
  342. paint.setColor(SK_ColorRED);
  343. paint.setStyle(SkPaint::kStroke_Style);
  344. paint.setStrokeWidth(1);
  345. canvas->drawPath(path, paint);
  346. }
  347. DEF_SIMPLE_GM(bug339297_as_clip, canvas, 640, 480) {
  348. SkPath path;
  349. path.moveTo(-469515, -10354890);
  350. path.cubicTo(771919.62f, -10411179, 2013360.1f, -10243774, 3195542.8f, -9860664);
  351. path.lineTo(3195550, -9860655);
  352. path.lineTo(3195539, -9860652);
  353. path.lineTo(3195539, -9860652);
  354. path.lineTo(3195539, -9860652);
  355. path.cubicTo(2013358.1f, -10243761, 771919.25f, -10411166, -469513.84f, -10354877);
  356. path.lineTo(-469515, -10354890);
  357. path.close();
  358. canvas->translate(258, 10365663);
  359. canvas->save();
  360. canvas->clipPath(path, true);
  361. canvas->clear(SK_ColorBLACK);
  362. canvas->restore();
  363. SkPaint paint;
  364. paint.setAntiAlias(true);
  365. paint.setStyle(SkPaint::kFill_Style);
  366. paint.setColor(SK_ColorRED);
  367. paint.setStyle(SkPaint::kStroke_Style);
  368. paint.setStrokeWidth(1);
  369. canvas->drawPath(path, paint);
  370. }
  371. DEF_SIMPLE_GM(bug6987, canvas, 200, 200) {
  372. SkPaint paint;
  373. paint.setStyle(SkPaint::kStroke_Style);
  374. paint.setStrokeWidth(0.0001f);
  375. paint.setAntiAlias(true);
  376. SkPath path;
  377. canvas->save();
  378. canvas->scale(50000.0f, 50000.0f);
  379. path.moveTo(0.0005f, 0.0004f);
  380. path.lineTo(0.0008f, 0.0010f);
  381. path.lineTo(0.0002f, 0.0010f);
  382. path.close();
  383. canvas->drawPath(path, paint);
  384. canvas->restore();
  385. }