SkPaint.h 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  1. /*
  2. * Copyright 2006 The Android Open Source Project
  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. /* Generated by tools/bookmaker from include/core/SkPaint.h and docs/SkPaint_Reference.bmh
  8. on 2018-08-28 10:32:58. Additional documentation and examples can be found at:
  9. https://skia.org/user/api/SkPaint_Reference
  10. You may edit either file directly. Structural changes to public interfaces require
  11. editing both files. After editing docs/SkPaint_Reference.bmh, run:
  12. bookmaker -b docs -i include/core/SkPaint.h -p
  13. to create an updated version of this file.
  14. */
  15. #ifndef SkPaint_DEFINED
  16. #define SkPaint_DEFINED
  17. #include "../private/SkTo.h"
  18. #include "SkBlendMode.h"
  19. #include "SkColor.h"
  20. #include "SkFilterQuality.h"
  21. #include "SkFontMetrics.h"
  22. #include "SkFontTypes.h"
  23. #include "SkMatrix.h"
  24. #include "SkRefCnt.h"
  25. #define SK_SUPPORT_LEGACY_FONTMETRICS_IN_PAINT
  26. class GrTextBlob;
  27. class SkAutoDescriptor;
  28. class SkColorFilter;
  29. class SkColorSpace;
  30. class SkData;
  31. class SkDescriptor;
  32. class SkDrawLooper;
  33. class SkGlyph;
  34. class SkGlyphRunBuilder;
  35. class SkGlyphRun;
  36. class SkGlyphRunListPainter;
  37. struct SkRect;
  38. class SkGlyphCache;
  39. class SkImageFilter;
  40. class SkMaskFilter;
  41. class SkPath;
  42. class SkPathEffect;
  43. struct SkPoint;
  44. class SkRunFont;
  45. class SkShader;
  46. class SkSurfaceProps;
  47. class SkTextBlob;
  48. class SkTextBlobRunIterator;
  49. class SkTypeface;
  50. #ifndef SK_SUPPORT_LEGACY_PAINT_TEXTMEASURE
  51. #define SK_SUPPORT_LEGACY_PAINT_TEXTMEASURE
  52. #endif
  53. /** \class SkPaint
  54. SkPaint controls options applied when drawing and measuring. SkPaint collects all
  55. options outside of the SkCanvas clip and SkCanvas matrix.
  56. Various options apply to text, strokes and fills, and images.
  57. Some options may not be implemented on all platforms; in these cases, setting
  58. the option has no effect. Some options are conveniences that duplicate SkCanvas
  59. functionality; for instance, text size is identical to matrix scale.
  60. SkPaint options are rarely exclusive; each option modifies a stage of the drawing
  61. pipeline and multiple pipeline stages may be affected by a single SkPaint.
  62. SkPaint collects effects and filters that describe single-pass and multiple-pass
  63. algorithms that alter the drawing geometry, color, and transparency. For instance,
  64. SkPaint does not directly implement dashing or blur, but contains the objects that do so.
  65. The objects contained by SkPaint are opaque, and cannot be edited outside of the SkPaint
  66. to affect it. The implementation is free to defer computations associated with the
  67. SkPaint, or ignore them altogether. For instance, some GPU implementations draw all
  68. SkPath geometries with anti-aliasing, regardless of how SkPaint::kAntiAlias_Flag
  69. is set in SkPaint.
  70. SkPaint describes a single color, a single font, a single image quality, and so on.
  71. Multiple colors are drawn either by using multiple paints or with objects like
  72. SkShader attached to SkPaint.
  73. */
  74. class SK_API SkPaint {
  75. public:
  76. /** Constructs SkPaint with default values.
  77. @return default initialized SkPaint
  78. */
  79. SkPaint();
  80. /** Makes a shallow copy of SkPaint. SkTypeface, SkPathEffect, SkShader,
  81. SkMaskFilter, SkColorFilter, SkDrawLooper, and SkImageFilter are shared
  82. between the original paint and the copy. Objects containing SkRefCnt increment
  83. their references by one.
  84. The referenced objects SkPathEffect, SkShader, SkMaskFilter, SkColorFilter,
  85. SkDrawLooper, and SkImageFilter cannot be modified after they are created.
  86. This prevents objects with SkRefCnt from being modified once SkPaint refers to them.
  87. @param paint original to copy
  88. @return shallow copy of paint
  89. */
  90. SkPaint(const SkPaint& paint);
  91. /** Implements a move constructor to avoid increasing the reference counts
  92. of objects referenced by the paint.
  93. After the call, paint is undefined, and can be safely destructed.
  94. @param paint original to move
  95. @return content of paint
  96. */
  97. SkPaint(SkPaint&& paint);
  98. /** Decreases SkPaint SkRefCnt of owned objects: SkTypeface, SkPathEffect, SkShader,
  99. SkMaskFilter, SkColorFilter, SkDrawLooper, and SkImageFilter. If the
  100. objects containing SkRefCnt go to zero, they are deleted.
  101. */
  102. ~SkPaint();
  103. /** Makes a shallow copy of SkPaint. SkTypeface, SkPathEffect, SkShader,
  104. SkMaskFilter, SkColorFilter, SkDrawLooper, and SkImageFilter are shared
  105. between the original paint and the copy. Objects containing SkRefCnt in the
  106. prior destination are decreased by one, and the referenced objects are deleted if the
  107. resulting count is zero. Objects containing SkRefCnt in the parameter paint
  108. are increased by one. paint is unmodified.
  109. @param paint original to copy
  110. @return content of paint
  111. */
  112. SkPaint& operator=(const SkPaint& paint);
  113. /** Moves the paint to avoid increasing the reference counts
  114. of objects referenced by the paint parameter. Objects containing SkRefCnt in the
  115. prior destination are decreased by one; those objects are deleted if the resulting count
  116. is zero.
  117. After the call, paint is undefined, and can be safely destructed.
  118. @param paint original to move
  119. @return content of paint
  120. */
  121. SkPaint& operator=(SkPaint&& paint);
  122. /** Compares a and b, and returns true if a and b are equivalent. May return false
  123. if SkTypeface, SkPathEffect, SkShader, SkMaskFilter, SkColorFilter,
  124. SkDrawLooper, or SkImageFilter have identical contents but different pointers.
  125. @param a SkPaint to compare
  126. @param b SkPaint to compare
  127. @return true if SkPaint pair are equivalent
  128. */
  129. SK_API friend bool operator==(const SkPaint& a, const SkPaint& b);
  130. /** Compares a and b, and returns true if a and b are not equivalent. May return true
  131. if SkTypeface, SkPathEffect, SkShader, SkMaskFilter, SkColorFilter,
  132. SkDrawLooper, or SkImageFilter have identical contents but different pointers.
  133. @param a SkPaint to compare
  134. @param b SkPaint to compare
  135. @return true if SkPaint pair are not equivalent
  136. */
  137. friend bool operator!=(const SkPaint& a, const SkPaint& b) {
  138. return !(a == b);
  139. }
  140. /** Returns a hash generated from SkPaint values and pointers.
  141. Identical hashes guarantee that the paints are
  142. equivalent, but differing hashes do not guarantee that the paints have differing
  143. contents.
  144. If operator==(const SkPaint& a, const SkPaint& b) returns true for two paints,
  145. their hashes are also equal.
  146. The hash returned is platform and implementation specific.
  147. @return a shallow hash
  148. */
  149. uint32_t getHash() const;
  150. /** Sets all SkPaint contents to their initial values. This is equivalent to replacing
  151. SkPaint with the result of SkPaint().
  152. */
  153. void reset();
  154. #ifdef SK_SUPPORT_LEGACY_NESTED_HINTINGENUM
  155. /** \enum SkPaint::Hinting
  156. Deprecated.
  157. Hinting adjusts the glyph outlines so that the shape provides a uniform
  158. look at a given point size on font engines that support it. Hinting may have a
  159. muted effect or no effect at all depending on the platform.
  160. The four levels roughly control corresponding features on platforms that use FreeType
  161. as the font engine.
  162. */
  163. enum Hinting : uint8_t {
  164. kNo_Hinting = 0, //!< glyph outlines unchanged
  165. kSlight_Hinting = 1, //!< minimal modification to improve constrast
  166. kNormal_Hinting = 2, //!< glyph outlines modified to improve constrast
  167. kFull_Hinting = 3, //!< modifies glyph outlines for maximum constrast
  168. };
  169. #endif
  170. /** Sets level of glyph outline adjustment.
  171. Does not check for valid values of hintingLevel.
  172. @param hintingLevel one of: SkFontHinting::kNone, SkFontHinting::kSlight,
  173. SkFontHinting::kNormal, SkFontHinting::kFull
  174. */
  175. void setHinting(SkFontHinting hintingLevel);
  176. /** Returns level of glyph outline adjustment.
  177. @return one of: SkFontHinting::kNone, SkFontHinting::kSlight, SkFontHinting::kNormal,
  178. SkFontHinting::kFull
  179. */
  180. SkFontHinting getHinting() const { return (SkFontHinting)fBitfields.fHinting; }
  181. /** \enum SkPaint::Flags
  182. The bit values stored in Flags.
  183. The default value for Flags, normally zero, can be changed at compile time
  184. with a custom definition of SkPaintDefaults_Flags.
  185. All flags can be read and written explicitly; Flags allows manipulating
  186. multiple settings at once.
  187. */
  188. enum Flags {
  189. kAntiAlias_Flag = 0x01, //!< mask for setting anti-alias
  190. kDither_Flag = 0x04, //!< mask for setting dither
  191. kFakeBoldText_Flag = 0x20, //!< mask for setting fake bold
  192. kLinearText_Flag = 0x40, //!< mask for setting linear text
  193. kSubpixelText_Flag = 0x80, //!< mask for setting subpixel text
  194. kLCDRenderText_Flag = 0x200, //!< mask for setting LCD text
  195. kEmbeddedBitmapText_Flag = 0x400, //!< mask for setting font embedded bitmaps
  196. kAutoHinting_Flag = 0x800, //!< mask for setting auto-hinting
  197. // 0x1000 used to be kVertical
  198. kAllFlags = 0xFFFF, //!< mask of all Flags
  199. };
  200. #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK
  201. /** Private.
  202. */
  203. enum ReserveFlags {
  204. kUnderlineText_ReserveFlag = 0x08, //!< to be deprecated soon
  205. kStrikeThruText_ReserveFlag = 0x10, //!< to be deprecated soon
  206. };
  207. #endif
  208. /** Returns paint settings described by SkPaint::Flags. Each setting uses one
  209. bit, and can be tested with SkPaint::Flags members.
  210. @return zero, one, or more bits described by SkPaint::Flags
  211. */
  212. uint32_t getFlags() const { return fBitfields.fFlags; }
  213. /** Replaces SkPaint::Flags with flags, the union of the SkPaint::Flags members.
  214. All SkPaint::Flags members may be cleared, or one or more may be set.
  215. @param flags union of SkPaint::Flags for SkPaint
  216. */
  217. void setFlags(uint32_t flags);
  218. /** Returns true if pixels on the active edges of SkPath may be drawn with partial transparency.
  219. Equivalent to getFlags() masked with kAntiAlias_Flag.
  220. @return kAntiAlias_Flag state
  221. */
  222. bool isAntiAlias() const {
  223. return SkToBool(this->getFlags() & kAntiAlias_Flag);
  224. }
  225. /** Requests, but does not require, that edge pixels draw opaque or with
  226. partial transparency.
  227. Sets kAntiAlias_Flag if aa is true.
  228. Clears kAntiAlias_Flag if aa is false.
  229. @param aa setting for kAntiAlias_Flag
  230. */
  231. void setAntiAlias(bool aa);
  232. /** Returns true if color error may be distributed to smooth color transition.
  233. Equivalent to getFlags() masked with kDither_Flag.
  234. @return kDither_Flag state
  235. */
  236. bool isDither() const {
  237. return SkToBool(this->getFlags() & kDither_Flag);
  238. }
  239. /** Requests, but does not require, to distribute color error.
  240. Sets kDither_Flag if dither is true.
  241. Clears kDither_Flag if dither is false.
  242. @param dither setting for kDither_Flag
  243. */
  244. void setDither(bool dither);
  245. /** Returns true if text is converted to SkPath before drawing and measuring.
  246. Equivalent to getFlags() masked with kLinearText_Flag.
  247. @return kLinearText_Flag state
  248. */
  249. bool isLinearText() const {
  250. return SkToBool(this->getFlags() & kLinearText_Flag);
  251. }
  252. /** Requests, but does not require, that glyphs are converted to SkPath
  253. before drawing and measuring.
  254. By default, kLinearText_Flag is clear.
  255. Sets kLinearText_Flag if linearText is true.
  256. Clears kLinearText_Flag if linearText is false.
  257. @param linearText setting for kLinearText_Flag
  258. */
  259. void setLinearText(bool linearText);
  260. /** Returns true if glyphs at different sub-pixel positions may differ on pixel edge coverage.
  261. Equivalent to getFlags() masked with kSubpixelText_Flag.
  262. @return kSubpixelText_Flag state
  263. */
  264. bool isSubpixelText() const {
  265. return SkToBool(this->getFlags() & kSubpixelText_Flag);
  266. }
  267. /** Requests, but does not require, that glyphs respect sub-pixel positioning.
  268. Sets kSubpixelText_Flag if subpixelText is true.
  269. Clears kSubpixelText_Flag if subpixelText is false.
  270. @param subpixelText setting for kSubpixelText_Flag
  271. */
  272. void setSubpixelText(bool subpixelText);
  273. /** Returns true if glyphs may use LCD striping to improve glyph edges.
  274. Returns true if SkPaint::Flags kLCDRenderText_Flag is set.
  275. @return kLCDRenderText_Flag state
  276. */
  277. bool isLCDRenderText() const {
  278. return SkToBool(this->getFlags() & kLCDRenderText_Flag);
  279. }
  280. /** Requests, but does not require, that glyphs use LCD striping for glyph edges.
  281. Sets kLCDRenderText_Flag if lcdText is true.
  282. Clears kLCDRenderText_Flag if lcdText is false.
  283. @param lcdText setting for kLCDRenderText_Flag
  284. */
  285. void setLCDRenderText(bool lcdText);
  286. /** Returns true if font engine may return glyphs from font bitmaps instead of from outlines.
  287. Equivalent to getFlags() masked with kEmbeddedBitmapText_Flag.
  288. @return kEmbeddedBitmapText_Flag state
  289. */
  290. bool isEmbeddedBitmapText() const {
  291. return SkToBool(this->getFlags() & kEmbeddedBitmapText_Flag);
  292. }
  293. /** Requests, but does not require, to use bitmaps in fonts instead of outlines.
  294. Sets kEmbeddedBitmapText_Flag if useEmbeddedBitmapText is true.
  295. Clears kEmbeddedBitmapText_Flag if useEmbeddedBitmapText is false.
  296. @param useEmbeddedBitmapText setting for kEmbeddedBitmapText_Flag
  297. */
  298. void setEmbeddedBitmapText(bool useEmbeddedBitmapText);
  299. /** Returns true if SkPaint::Hinting is set to SkFontHinting::kNormal or
  300. SkFontHinting::kFull, and if platform uses FreeType as the font manager.
  301. If true, instructs the font manager to always hint glyphs.
  302. Equivalent to getFlags() masked with kAutoHinting_Flag.
  303. @return kAutoHinting_Flag state
  304. */
  305. bool isAutohinted() const {
  306. return SkToBool(this->getFlags() & kAutoHinting_Flag);
  307. }
  308. /** Sets whether to always hint glyphs.
  309. If SkPaint::Hinting is set to SkFontHinting::kNormal or SkFontHinting::kFull
  310. and useAutohinter is set, instructs the font manager to always hint glyphs.
  311. auto-hinting has no effect if SkPaint::Hinting is set to SkFontHinting::kNone or
  312. SkFontHinting::kSlight.
  313. Only affects platforms that use FreeType as the font manager.
  314. Sets kAutoHinting_Flag if useAutohinter is true.
  315. Clears kAutoHinting_Flag if useAutohinter is false.
  316. @param useAutohinter setting for kAutoHinting_Flag
  317. */
  318. void setAutohinted(bool useAutohinter);
  319. /** Returns true if approximate bold by increasing the stroke width when creating glyph bitmaps
  320. from outlines.
  321. Equivalent to getFlags() masked with kFakeBoldText_Flag.
  322. @return kFakeBoldText_Flag state
  323. */
  324. bool isFakeBoldText() const {
  325. return SkToBool(this->getFlags() & kFakeBoldText_Flag);
  326. }
  327. /** Increases stroke width when creating glyph bitmaps to approximate a bold typeface.
  328. Sets kFakeBoldText_Flag if fakeBoldText is true.
  329. Clears kFakeBoldText_Flag if fakeBoldText is false.
  330. @param fakeBoldText setting for kFakeBoldText_Flag
  331. */
  332. void setFakeBoldText(bool fakeBoldText);
  333. /** Returns SkFilterQuality, the image filtering level. A lower setting
  334. draws faster; a higher setting looks better when the image is scaled.
  335. @return one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
  336. kMedium_SkFilterQuality, kHigh_SkFilterQuality
  337. */
  338. SkFilterQuality getFilterQuality() const {
  339. return (SkFilterQuality)fBitfields.fFilterQuality;
  340. }
  341. /** Sets SkFilterQuality, the image filtering level. A lower setting
  342. draws faster; a higher setting looks better when the image is scaled.
  343. Does not check to see if quality is valid.
  344. @param quality one of: kNone_SkFilterQuality, kLow_SkFilterQuality,
  345. kMedium_SkFilterQuality, kHigh_SkFilterQuality
  346. */
  347. void setFilterQuality(SkFilterQuality quality);
  348. /** \enum SkPaint::Style
  349. Set Style to fill, stroke, or both fill and stroke geometry.
  350. The stroke and fill
  351. share all paint attributes; for instance, they are drawn with the same color.
  352. Use kStrokeAndFill_Style to avoid hitting the same pixels twice with a stroke draw and
  353. a fill draw.
  354. */
  355. enum Style : uint8_t {
  356. kFill_Style, //!< set to fill geometry
  357. kStroke_Style, //!< set to stroke geometry
  358. kStrokeAndFill_Style, //!< sets to stroke and fill geometry
  359. };
  360. /** May be used to verify that SkPaint::Style is a legal value.
  361. */
  362. static constexpr int kStyleCount = kStrokeAndFill_Style + 1;
  363. /** Returns whether the geometry is filled, stroked, or filled and stroked.
  364. @return one of:kFill_Style, kStroke_Style, kStrokeAndFill_Style
  365. */
  366. Style getStyle() const { return (Style)fBitfields.fStyle; }
  367. /** Sets whether the geometry is filled, stroked, or filled and stroked.
  368. Has no effect if style is not a legal SkPaint::Style value.
  369. @param style one of: kFill_Style, kStroke_Style, kStrokeAndFill_Style
  370. */
  371. void setStyle(Style style);
  372. /** Retrieves alpha and RGB, unpremultiplied, packed into 32 bits.
  373. Use helpers SkColorGetA(), SkColorGetR(), SkColorGetG(), and SkColorGetB() to extract
  374. a color component.
  375. @return unpremultiplied ARGB
  376. */
  377. SkColor getColor() const { return fColor4f.toSkColor(); }
  378. /** Retrieves alpha and RGB, unpremultiplied, as four floating point values. RGB are
  379. are extended sRGB values (sRGB gamut, and encoded with the sRGB transfer function).
  380. @return unpremultiplied RGBA
  381. */
  382. SkColor4f getColor4f() const { return fColor4f; }
  383. /** Sets alpha and RGB used when stroking and filling. The color is a 32-bit value,
  384. unpremultiplied, packing 8-bit components for alpha, red, blue, and green.
  385. @param color unpremultiplied ARGB
  386. */
  387. void setColor(SkColor color);
  388. /** Sets alpha and RGB used when stroking and filling. The color is four floating
  389. point values, unpremultiplied. The color values are interpreted as being in
  390. the colorSpace. If colorSpace is nullptr, then color is assumed to be in the
  391. sRGB color space.
  392. @param color unpremultiplied RGBA
  393. @param colorSpace SkColorSpace describing the encoding of color
  394. */
  395. void setColor4f(const SkColor4f& color, SkColorSpace* colorSpace);
  396. /** Retrieves alpha from the color used when stroking and filling.
  397. @return alpha ranging from zero, fully transparent, to 255, fully opaque
  398. */
  399. uint8_t getAlpha() const { return sk_float_round2int(fColor4f.fA * 255); }
  400. /** Replaces alpha, leaving RGB
  401. unchanged. An out of range value triggers an assert in the debug
  402. build. a is a value from zero to 255.
  403. a set to zero makes color fully transparent; a set to 255 makes color
  404. fully opaque.
  405. @param a alpha component of color
  406. */
  407. void setAlpha(U8CPU a);
  408. /** Sets color used when drawing solid fills. The color components range from 0 to 255.
  409. The color is unpremultiplied; alpha sets the transparency independent of RGB.
  410. @param a amount of alpha, from fully transparent (0) to fully opaque (255)
  411. @param r amount of red, from no red (0) to full red (255)
  412. @param g amount of green, from no green (0) to full green (255)
  413. @param b amount of blue, from no blue (0) to full blue (255)
  414. */
  415. void setARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
  416. /** Returns the thickness of the pen used by SkPaint to
  417. outline the shape.
  418. @return zero for hairline, greater than zero for pen thickness
  419. */
  420. SkScalar getStrokeWidth() const { return fWidth; }
  421. /** Sets the thickness of the pen used by the paint to
  422. outline the shape.
  423. Has no effect if width is less than zero.
  424. @param width zero thickness for hairline; greater than zero for pen thickness
  425. */
  426. void setStrokeWidth(SkScalar width);
  427. /** Returns the limit at which a sharp corner is drawn beveled.
  428. @return zero and greater miter limit
  429. */
  430. SkScalar getStrokeMiter() const { return fMiterLimit; }
  431. /** Sets the limit at which a sharp corner is drawn beveled.
  432. Valid values are zero and greater.
  433. Has no effect if miter is less than zero.
  434. @param miter zero and greater miter limit
  435. */
  436. void setStrokeMiter(SkScalar miter);
  437. /** \enum SkPaint::Cap
  438. Cap draws at the beginning and end of an open path contour.
  439. */
  440. enum Cap {
  441. kButt_Cap, //!< no stroke extension
  442. kRound_Cap, //!< adds circle
  443. kSquare_Cap, //!< adds square
  444. kLast_Cap = kSquare_Cap, //!< largest Cap value
  445. kDefault_Cap = kButt_Cap, //!< equivalent to kButt_Cap
  446. };
  447. /** May be used to verify that SkPaint::Cap is a legal value.
  448. */
  449. static constexpr int kCapCount = kLast_Cap + 1;
  450. /** \enum SkPaint::Join
  451. Join specifies how corners are drawn when a shape is stroked. Join
  452. affects the four corners of a stroked rectangle, and the connected segments in a
  453. stroked path.
  454. Choose miter join to draw sharp corners. Choose round join to draw a circle with a
  455. radius equal to the stroke width on top of the corner. Choose bevel join to minimally
  456. connect the thick strokes.
  457. The fill path constructed to describe the stroked path respects the join setting but may
  458. not contain the actual join. For instance, a fill path constructed with round joins does
  459. not necessarily include circles at each connected segment.
  460. */
  461. enum Join : uint8_t {
  462. kMiter_Join, //!< extends to miter limit
  463. kRound_Join, //!< adds circle
  464. kBevel_Join, //!< connects outside edges
  465. kLast_Join = kBevel_Join, //!< equivalent to the largest value for Join
  466. kDefault_Join = kMiter_Join, //!< equivalent to kMiter_Join
  467. };
  468. /** May be used to verify that SkPaint::Join is a legal value.
  469. */
  470. static constexpr int kJoinCount = kLast_Join + 1;
  471. /** Returns the geometry drawn at the beginning and end of strokes.
  472. @return one of: kButt_Cap, kRound_Cap, kSquare_Cap
  473. */
  474. Cap getStrokeCap() const { return (Cap)fBitfields.fCapType; }
  475. /** Sets the geometry drawn at the beginning and end of strokes.
  476. @param cap one of: kButt_Cap, kRound_Cap, kSquare_Cap;
  477. has no effect if cap is not valid
  478. */
  479. void setStrokeCap(Cap cap);
  480. /** Returns the geometry drawn at the corners of strokes.
  481. @return one of: kMiter_Join, kRound_Join, kBevel_Join
  482. */
  483. Join getStrokeJoin() const { return (Join)fBitfields.fJoinType; }
  484. /** Sets the geometry drawn at the corners of strokes.
  485. @param join one of: kMiter_Join, kRound_Join, kBevel_Join;
  486. otherwise, has no effect
  487. */
  488. void setStrokeJoin(Join join);
  489. /** Returns the filled equivalent of the stroked path.
  490. @param src SkPath read to create a filled version
  491. @param dst resulting SkPath; may be the same as src, but may not be nullptr
  492. @param cullRect optional limit passed to SkPathEffect
  493. @param resScale if > 1, increase precision, else if (0 < resScale < 1) reduce precision
  494. to favor speed and size
  495. @return true if the path represents style fill, or false if it represents hairline
  496. */
  497. bool getFillPath(const SkPath& src, SkPath* dst, const SkRect* cullRect,
  498. SkScalar resScale = 1) const;
  499. /** Returns the filled equivalent of the stroked path.
  500. Replaces dst with the src path modified by SkPathEffect and style stroke.
  501. SkPathEffect, if any, is not culled. stroke width is created with default precision.
  502. @param src SkPath read to create a filled version
  503. @param dst resulting SkPath dst may be the same as src, but may not be nullptr
  504. @return true if the path represents style fill, or false if it represents hairline
  505. */
  506. bool getFillPath(const SkPath& src, SkPath* dst) const {
  507. return this->getFillPath(src, dst, nullptr, 1);
  508. }
  509. /** Returns optional colors used when filling a path, such as a gradient.
  510. Does not alter SkShader SkRefCnt.
  511. @return SkShader if previously set, nullptr otherwise
  512. */
  513. SkShader* getShader() const { return fShader.get(); }
  514. /** Returns optional colors used when filling a path, such as a gradient.
  515. Increases SkShader SkRefCnt by one.
  516. @return SkShader if previously set, nullptr otherwise
  517. */
  518. sk_sp<SkShader> refShader() const;
  519. /** Sets optional colors used when filling a path, such as a gradient.
  520. Sets SkShader to shader, decreasing SkRefCnt of the previous SkShader.
  521. Increments shader SkRefCnt by one.
  522. @param shader how geometry is filled with color; if nullptr, color is used instead
  523. */
  524. void setShader(sk_sp<SkShader> shader);
  525. /** Returns SkColorFilter if set, or nullptr.
  526. Does not alter SkColorFilter SkRefCnt.
  527. @return SkColorFilter if previously set, nullptr otherwise
  528. */
  529. SkColorFilter* getColorFilter() const { return fColorFilter.get(); }
  530. /** Returns SkColorFilter if set, or nullptr.
  531. Increases SkColorFilter SkRefCnt by one.
  532. @return SkColorFilter if set, or nullptr
  533. */
  534. sk_sp<SkColorFilter> refColorFilter() const;
  535. /** Sets SkColorFilter to filter, decreasing SkRefCnt of the previous
  536. SkColorFilter. Pass nullptr to clear SkColorFilter.
  537. Increments filter SkRefCnt by one.
  538. @param colorFilter SkColorFilter to apply to subsequent draw
  539. */
  540. void setColorFilter(sk_sp<SkColorFilter> colorFilter);
  541. /** Returns SkBlendMode.
  542. By default, returns SkBlendMode::kSrcOver.
  543. @return mode used to combine source color with destination color
  544. */
  545. SkBlendMode getBlendMode() const { return (SkBlendMode)fBlendMode; }
  546. /** Returns true if SkBlendMode is SkBlendMode::kSrcOver, the default.
  547. @return true if SkBlendMode is SkBlendMode::kSrcOver
  548. */
  549. bool isSrcOver() const { return (SkBlendMode)fBlendMode == SkBlendMode::kSrcOver; }
  550. /** Sets SkBlendMode to mode.
  551. Does not check for valid input.
  552. @param mode SkBlendMode used to combine source color and destination
  553. */
  554. void setBlendMode(SkBlendMode mode) { fBlendMode = (unsigned)mode; }
  555. /** Returns SkPathEffect if set, or nullptr.
  556. Does not alter SkPathEffect SkRefCnt.
  557. @return SkPathEffect if previously set, nullptr otherwise
  558. */
  559. SkPathEffect* getPathEffect() const { return fPathEffect.get(); }
  560. /** Returns SkPathEffect if set, or nullptr.
  561. Increases SkPathEffect SkRefCnt by one.
  562. @return SkPathEffect if previously set, nullptr otherwise
  563. */
  564. sk_sp<SkPathEffect> refPathEffect() const;
  565. /** Sets SkPathEffect to pathEffect, decreasing SkRefCnt of the previous
  566. SkPathEffect. Pass nullptr to leave the path geometry unaltered.
  567. Increments pathEffect SkRefCnt by one.
  568. @param pathEffect replace SkPath with a modification when drawn
  569. */
  570. void setPathEffect(sk_sp<SkPathEffect> pathEffect);
  571. /** Returns SkMaskFilter if set, or nullptr.
  572. Does not alter SkMaskFilter SkRefCnt.
  573. @return SkMaskFilter if previously set, nullptr otherwise
  574. */
  575. SkMaskFilter* getMaskFilter() const { return fMaskFilter.get(); }
  576. /** Returns SkMaskFilter if set, or nullptr.
  577. Increases SkMaskFilter SkRefCnt by one.
  578. @return SkMaskFilter if previously set, nullptr otherwise
  579. */
  580. sk_sp<SkMaskFilter> refMaskFilter() const;
  581. /** Sets SkMaskFilter to maskFilter, decreasing SkRefCnt of the previous
  582. SkMaskFilter. Pass nullptr to clear SkMaskFilter and leave SkMaskFilter effect on
  583. mask alpha unaltered.
  584. Increments maskFilter SkRefCnt by one.
  585. @param maskFilter modifies clipping mask generated from drawn geometry
  586. */
  587. void setMaskFilter(sk_sp<SkMaskFilter> maskFilter);
  588. /** Returns SkTypeface if set, or nullptr.
  589. Does not alter SkTypeface SkRefCnt.
  590. @return SkTypeface if previously set, nullptr otherwise
  591. */
  592. SkTypeface* getTypeface() const { return fTypeface.get(); }
  593. /** Increases SkTypeface SkRefCnt by one.
  594. @return SkTypeface if previously set, nullptr otherwise
  595. */
  596. sk_sp<SkTypeface> refTypeface() const;
  597. /** Sets SkTypeface to typeface, decreasing SkRefCnt of the previous SkTypeface.
  598. Pass nullptr to clear SkTypeface and use the default typeface. Increments
  599. typeface SkRefCnt by one.
  600. @param typeface font and style used to draw text
  601. */
  602. void setTypeface(sk_sp<SkTypeface> typeface);
  603. /** Returns SkImageFilter if set, or nullptr.
  604. Does not alter SkImageFilter SkRefCnt.
  605. @return SkImageFilter if previously set, nullptr otherwise
  606. */
  607. SkImageFilter* getImageFilter() const { return fImageFilter.get(); }
  608. /** Returns SkImageFilter if set, or nullptr.
  609. Increases SkImageFilter SkRefCnt by one.
  610. @return SkImageFilter if previously set, nullptr otherwise
  611. */
  612. sk_sp<SkImageFilter> refImageFilter() const;
  613. /** Sets SkImageFilter to imageFilter, decreasing SkRefCnt of the previous
  614. SkImageFilter. Pass nullptr to clear SkImageFilter, and remove SkImageFilter effect
  615. on drawing.
  616. Increments imageFilter SkRefCnt by one.
  617. @param imageFilter how SkImage is sampled when transformed
  618. */
  619. void setImageFilter(sk_sp<SkImageFilter> imageFilter);
  620. /** Returns SkDrawLooper if set, or nullptr.
  621. Does not alter SkDrawLooper SkRefCnt.
  622. @return SkDrawLooper if previously set, nullptr otherwise
  623. */
  624. SkDrawLooper* getDrawLooper() const { return fDrawLooper.get(); }
  625. /** Returns SkDrawLooper if set, or nullptr.
  626. Increases SkDrawLooper SkRefCnt by one.
  627. @return SkDrawLooper if previously set, nullptr otherwise
  628. */
  629. sk_sp<SkDrawLooper> refDrawLooper() const;
  630. /** Deprecated.
  631. (see skbug.com/6259)
  632. */
  633. SkDrawLooper* getLooper() const { return fDrawLooper.get(); }
  634. /** Sets SkDrawLooper to drawLooper, decreasing SkRefCnt of the previous
  635. drawLooper. Pass nullptr to clear SkDrawLooper and leave SkDrawLooper effect on
  636. drawing unaltered.
  637. Increments drawLooper SkRefCnt by one.
  638. @param drawLooper iterates through drawing one or more time, altering SkPaint
  639. */
  640. void setDrawLooper(sk_sp<SkDrawLooper> drawLooper);
  641. /** Deprecated.
  642. (see skbug.com/6259)
  643. */
  644. void setLooper(sk_sp<SkDrawLooper> drawLooper);
  645. /** Returns text size in points.
  646. @return typographic height of text
  647. */
  648. SkScalar getTextSize() const { return fTextSize; }
  649. /** Sets text size in points.
  650. Has no effect if textSize is not greater than or equal to zero.
  651. @param textSize typographic height of text
  652. */
  653. void setTextSize(SkScalar textSize);
  654. /** Returns text scale on x-axis.
  655. Default value is 1.
  656. @return text horizontal scale
  657. */
  658. SkScalar getTextScaleX() const { return fTextScaleX; }
  659. /** Sets text scale on x-axis.
  660. Default value is 1.
  661. @param scaleX text horizontal scale
  662. */
  663. void setTextScaleX(SkScalar scaleX);
  664. /** Returns text skew on x-axis.
  665. Default value is zero.
  666. @return additional shear on x-axis relative to y-axis
  667. */
  668. SkScalar getTextSkewX() const { return fTextSkewX; }
  669. /** Sets text skew on x-axis.
  670. Default value is zero.
  671. @param skewX additional shear on x-axis relative to y-axis
  672. */
  673. void setTextSkewX(SkScalar skewX);
  674. /** \enum SkPaint::TextEncoding
  675. TextEncoding determines whether text specifies character codes and their encoded
  676. size, or glyph indices. Characters are encoded as specified by the Unicode standard.
  677. Character codes encoded size are specified by UTF-8, UTF-16, or UTF-32.
  678. All character code formats are able to represent all of Unicode, differing only
  679. in the total storage required.
  680. UTF-8 (RFC 3629) encodes each character as one or more 8-bit bytes.
  681. UTF-16 (RFC 2781) encodes each character as one or two 16-bit words.
  682. UTF-32 encodes each character as one 32-bit word.
  683. font manager uses font data to convert character code points into glyph indices.
  684. A glyph index is a 16-bit word.
  685. TextEncoding is set to kUTF8_TextEncoding by default.
  686. */
  687. enum TextEncoding : uint8_t {
  688. kUTF8_TextEncoding, //!< uses bytes to represent UTF-8 or ASCII
  689. kUTF16_TextEncoding, //!< uses two byte words to represent most of Unicode
  690. kUTF32_TextEncoding, //!< uses four byte words to represent all of Unicode
  691. kGlyphID_TextEncoding, //!< uses two byte words to represent glyph indices
  692. };
  693. /** Returns SkPaint::TextEncoding.
  694. SkPaint::TextEncoding determines how character code points are mapped to font glyph indices.
  695. @return one of: kUTF8_TextEncoding, kUTF16_TextEncoding, kUTF32_TextEncoding, or
  696. kGlyphID_TextEncoding
  697. */
  698. TextEncoding getTextEncoding() const {
  699. return (TextEncoding)fBitfields.fTextEncoding;
  700. }
  701. /** Sets SkPaint::TextEncoding to encoding.
  702. SkPaint::TextEncoding determines how character code points are mapped to font glyph indices.
  703. Invalid values for encoding are ignored.
  704. @param encoding one of: kUTF8_TextEncoding, kUTF16_TextEncoding, kUTF32_TextEncoding, or
  705. kGlyphID_TextEncoding
  706. */
  707. void setTextEncoding(TextEncoding encoding);
  708. // Experimental
  709. void setTextEncoding(SkTextEncoding encoding) {
  710. this->setTextEncoding((TextEncoding)encoding);
  711. }
  712. #ifdef SK_SUPPORT_LEGACY_PAINT_TEXTMEASURE
  713. #ifdef SK_SUPPORT_LEGACY_FONTMETRICS_IN_PAINT
  714. /**
  715. SkFontMetrics is filled out by getFontMetrics(). SkFontMetrics contents reflect the values
  716. computed by font manager using SkTypeface. Values are set to zero if they are
  717. not available.
  718. All vertical values are relative to the baseline, on a y-axis pointing down.
  719. Zero is on the baseline, negative values are above the baseline, and positive
  720. values are below the baseline.
  721. fUnderlineThickness and fUnderlinePosition have a bit set in fFlags if their values
  722. are valid, since their value may be zero.
  723. fStrikeoutThickness and fStrikeoutPosition have a bit set in fFlags if their values
  724. are valid, since their value may be zero.
  725. */
  726. typedef SkFontMetrics FontMetrics;
  727. #endif
  728. /** Returns SkFontMetrics associated with SkTypeface.
  729. The return value is the recommended spacing between lines: the sum of metrics
  730. descent, ascent, and leading.
  731. If metrics is not nullptr, SkFontMetrics is copied to metrics.
  732. Results are scaled by text size but does not take into account
  733. dimensions required by text scale x, text skew x, fake bold,
  734. style stroke, and SkPathEffect.
  735. @param metrics storage for SkFontMetrics; may be nullptr
  736. @return recommended spacing between lines
  737. */
  738. SkScalar getFontMetrics(SkFontMetrics* metrics) const;
  739. /** Returns the recommended spacing between lines: the sum of metrics
  740. descent, ascent, and leading.
  741. Result is scaled by text size but does not take into account
  742. dimensions required by stroking and SkPathEffect.
  743. Returns the same result as getFontMetrics().
  744. @return recommended spacing between lines
  745. */
  746. SkScalar getFontSpacing() const { return this->getFontMetrics(nullptr); }
  747. /** Converts text into glyph indices.
  748. Returns the number of glyph indices represented by text.
  749. SkPaint::TextEncoding specifies how text represents characters or glyphs.
  750. glyphs may be nullptr, to compute the glyph count.
  751. Does not check text for valid character codes or valid glyph indices.
  752. If byteLength equals zero, returns zero.
  753. If byteLength includes a partial character, the partial character is ignored.
  754. If SkPaint::TextEncoding is kUTF8_TextEncoding and
  755. text contains an invalid UTF-8 sequence, zero is returned.
  756. @param text character storage encoded with SkPaint::TextEncoding
  757. @param byteLength length of character storage in bytes
  758. @param glyphs storage for glyph indices; may be nullptr
  759. @return number of glyphs represented by text of length byteLength
  760. */
  761. int textToGlyphs(const void* text, size_t byteLength,
  762. SkGlyphID glyphs[]) const;
  763. /** Returns true if all text corresponds to a non-zero glyph index.
  764. Returns false if any characters in text are not supported in
  765. SkTypeface.
  766. If SkPaint::TextEncoding is kGlyphID_TextEncoding,
  767. returns true if all glyph indices in text are non-zero;
  768. does not check to see if text contains valid glyph indices for SkTypeface.
  769. Returns true if byteLength is zero.
  770. @param text array of characters or glyphs
  771. @param byteLength number of bytes in text array
  772. @return true if all text corresponds to a non-zero glyph index
  773. */
  774. bool containsText(const void* text, size_t byteLength) const;
  775. /** Converts glyphs into text if possible.
  776. Glyph values without direct Unicode equivalents are mapped to zero.
  777. Uses the SkTypeface, but is unaffected
  778. by SkPaint::TextEncoding; the text values returned are equivalent to kUTF32_TextEncoding.
  779. Only supported on platforms that use FreeType as the font engine.
  780. @param glyphs array of indices into font
  781. @param count length of glyph array
  782. @param text storage for character codes, one per glyph
  783. */
  784. void glyphsToUnichars(const SkGlyphID glyphs[], int count, SkUnichar text[]) const;
  785. /** Returns the number of glyphs in text.
  786. Uses SkPaint::TextEncoding to count the glyphs.
  787. Returns the same result as textToGlyphs().
  788. @param text character storage encoded with SkPaint::TextEncoding
  789. @param byteLength length of character storage in bytes
  790. @return number of glyphs represented by text of length byteLength
  791. */
  792. int countText(const void* text, size_t byteLength) const;
  793. /** Returns the advance width of text.
  794. The advance is the normal distance to move before drawing additional text.
  795. Uses SkPaint::TextEncoding to decode text, SkTypeface to get the font metrics,
  796. and text size, text scale x, text skew x, stroke width, and
  797. SkPathEffect to scale the metrics and bounds.
  798. Returns the bounding box of text if bounds is not nullptr.
  799. The bounding box is computed as if the text was drawn at the origin.
  800. @param text character codes or glyph indices to be measured
  801. @param length number of bytes of text to measure
  802. @param bounds returns bounding box relative to (0, 0) if not nullptr
  803. @return advance width or height
  804. */
  805. SkScalar measureText(const void* text, size_t length, SkRect* bounds) const;
  806. /** Returns the advance width of text.
  807. The advance is the normal distance to move before drawing additional text.
  808. Uses SkPaint::TextEncoding to decode text, SkTypeface to get the font metrics,
  809. and text size to scale the metrics.
  810. Does not scale the advance or bounds by fake bold or SkPathEffect.
  811. @param text character codes or glyph indices to be measured
  812. @param length number of bytes of text to measure
  813. @return advance width or height
  814. */
  815. SkScalar measureText(const void* text, size_t length) const {
  816. return this->measureText(text, length, nullptr);
  817. }
  818. #endif
  819. /** Returns the bytes of text that fit within maxWidth.
  820. The text fragment fits if its advance width is less than or equal to maxWidth.
  821. Measures only while the advance is less than or equal to maxWidth.
  822. Returns the advance or the text fragment in measuredWidth if it not nullptr.
  823. Uses SkPaint::TextEncoding to decode text, SkTypeface to get the font metrics,
  824. and text size to scale the metrics.
  825. Does not scale the advance or bounds by fake bold or SkPathEffect.
  826. @param text character codes or glyph indices to be measured
  827. @param length number of bytes of text to measure
  828. @param maxWidth advance limit; text is measured while advance is less than maxWidth
  829. @param measuredWidth returns the width of the text less than or equal to maxWidth
  830. @return bytes of text that fit, always less than or equal to length
  831. */
  832. size_t breakText(const void* text, size_t length, SkScalar maxWidth,
  833. SkScalar* measuredWidth = nullptr) const;
  834. #ifdef SK_SUPPORT_LEGACY_PAINT_TEXTMEASURE
  835. /** Retrieves the advance and bounds for each glyph in text, and returns
  836. the glyph count in text.
  837. Both widths and bounds may be nullptr.
  838. If widths is not nullptr, widths must be an array of glyph count entries.
  839. if bounds is not nullptr, bounds must be an array of glyph count entries.
  840. Uses SkPaint::TextEncoding to decode text, SkTypeface to get the font metrics,
  841. and text size to scale the widths and bounds.
  842. Does not scale the advance by fake bold or SkPathEffect.
  843. Does include fake bold and SkPathEffect in the bounds.
  844. @param text character codes or glyph indices to be measured
  845. @param byteLength number of bytes of text to measure
  846. @param widths returns text advances for each glyph; may be nullptr
  847. @param bounds returns bounds for each glyph relative to (0, 0); may be nullptr
  848. @return glyph count in text
  849. */
  850. int getTextWidths(const void* text, size_t byteLength, SkScalar widths[],
  851. SkRect bounds[] = nullptr) const;
  852. /** Returns the geometry as SkPath equivalent to the drawn text.
  853. Uses SkPaint::TextEncoding to decode text, SkTypeface to get the glyph paths,
  854. and text size, fake bold, and SkPathEffect to scale and modify the glyph paths.
  855. All of the glyph paths are stored in path.
  856. Uses x, y, to position path.
  857. @param text character codes or glyph indices
  858. @param length number of bytes of text
  859. @param x x-axis value of the origin of the text
  860. @param y y-axis value of the origin of the text
  861. @param path geometry of the glyphs
  862. */
  863. void getTextPath(const void* text, size_t length, SkScalar x, SkScalar y,
  864. SkPath* path) const;
  865. /** Returns the geometry as SkPath equivalent to the drawn text.
  866. Uses SkPaint::TextEncoding to decode text, SkTypeface to get the glyph paths,
  867. and text size, fake bold, and SkPathEffect to scale and modify the glyph paths.
  868. All of the glyph paths are stored in path.
  869. Uses pos array to position path.
  870. pos contains a position for each glyph.
  871. @param text character codes or glyph indices
  872. @param length number of bytes of text
  873. @param pos positions of each glyph
  874. @param path geometry of the glyphs
  875. */
  876. void getPosTextPath(const void* text, size_t length,
  877. const SkPoint pos[], SkPath* path) const;
  878. #ifdef SK_SUPPORT_LEGACY_TEXTINTERCEPTS
  879. public:
  880. #else
  881. private:
  882. #endif
  883. /** Returns the number of intervals that intersect bounds.
  884. bounds describes a pair of lines parallel to the text advance.
  885. The return count is zero or a multiple of two, and is at most twice the number of glyphs in
  886. the string.
  887. Uses SkPaint::TextEncoding to decode text, SkTypeface to get the glyph paths,
  888. and text size, fake bold, and SkPathEffect to scale and modify the glyph paths.
  889. Uses x, y to position intervals.
  890. Pass nullptr for intervals to determine the size of the interval array.
  891. intervals are cached to improve performance for multiple calls.
  892. @param text character codes or glyph indices
  893. @param length number of bytes of text
  894. @param x x-axis value of the origin of the text
  895. @param y y-axis value of the origin of the text
  896. @param bounds lower and upper line parallel to the advance
  897. @param intervals returned intersections; may be nullptr
  898. @return number of intersections; may be zero
  899. */
  900. int getTextIntercepts(const void* text, size_t length, SkScalar x, SkScalar y,
  901. const SkScalar bounds[2], SkScalar* intervals) const;
  902. /** Returns the number of intervals that intersect bounds.
  903. bounds describes a pair of lines parallel to the text advance.
  904. The return count is zero or a multiple of two, and is at most twice the number of glyphs in
  905. the string.
  906. Uses SkPaint::TextEncoding to decode text, SkTypeface to get the glyph paths,
  907. and text size, fake bold, and SkPathEffect to scale and modify the glyph paths.
  908. Uses pos array to position intervals.
  909. Pass nullptr for intervals to determine the size of the interval array.
  910. intervals are cached to improve performance for multiple calls.
  911. @param text character codes or glyph indices
  912. @param length number of bytes of text
  913. @param pos positions of each glyph
  914. @param bounds lower and upper line parallel to the advance
  915. @param intervals returned intersections; may be nullptr
  916. @return number of intersections; may be zero
  917. */
  918. int getPosTextIntercepts(const void* text, size_t length, const SkPoint pos[],
  919. const SkScalar bounds[2], SkScalar* intervals) const;
  920. /** Returns the number of intervals that intersect bounds.
  921. bounds describes a pair of lines parallel to the text advance.
  922. The return count is zero or a multiple of two, and is at most twice the number of glyphs in
  923. the string.
  924. Uses SkPaint::TextEncoding to decode text, SkTypeface to get the glyph paths,
  925. and text size, fake bold, and SkPathEffect to scale and modify the glyph paths.
  926. Uses xpos array, constY to position intervals.
  927. Pass nullptr for intervals to determine the size of the interval array.
  928. intervals are cached to improve performance for multiple calls.
  929. @param text character codes or glyph indices
  930. @param length number of bytes of text
  931. @param xpos positions of each glyph on x-axis
  932. @param constY position of each glyph on y-axis
  933. @param bounds lower and upper line parallel to the advance
  934. @param intervals returned intersections; may be nullptr
  935. @return number of intersections; may be zero
  936. */
  937. int getPosTextHIntercepts(const void* text, size_t length, const SkScalar xpos[],
  938. SkScalar constY, const SkScalar bounds[2], SkScalar* intervals) const;
  939. public:
  940. /** Returns the number of intervals that intersect bounds.
  941. bounds describes a pair of lines parallel to the text advance.
  942. The return count is zero or a multiple of two, and is at most twice the number of glyphs in
  943. the string.
  944. Uses SkTypeface to get the glyph paths,
  945. and text size, fake bold, and SkPathEffect to scale and modify the glyph paths.
  946. Uses run array to position intervals.
  947. SkPaint::TextEncoding must be set to SkPaint::kGlyphID_TextEncoding.
  948. Pass nullptr for intervals to determine the size of the interval array.
  949. intervals are cached to improve performance for multiple calls.
  950. @param blob glyphs, positions, and text paint attributes
  951. @param bounds lower and upper line parallel to the advance
  952. @param intervals returned intersections; may be nullptr
  953. @return number of intersections; may be zero
  954. */
  955. int getTextBlobIntercepts(const SkTextBlob* blob, const SkScalar bounds[2],
  956. SkScalar* intervals) const;
  957. /** Returns the union of bounds of all glyphs.
  958. Returned dimensions are computed by font manager from font data,
  959. ignoring SkPaint::Hinting. Includes font metrics, but not fake bold or SkPathEffect.
  960. If text size is large, text scale is one, and text skew is zero,
  961. returns the bounds as:
  962. { SkFontMetrics::fXMin, SkFontMetrics::fTop, SkFontMetrics::fXMax, SkFontMetrics::fBottom }.
  963. @return union of bounds of all glyphs
  964. */
  965. SkRect getFontBounds() const;
  966. #endif
  967. /** Returns true if SkPaint prevents all drawing;
  968. otherwise, the SkPaint may or may not allow drawing.
  969. Returns true if, for example, SkBlendMode combined with alpha computes a
  970. new alpha of zero.
  971. @return true if SkPaint prevents all drawing
  972. */
  973. bool nothingToDraw() const;
  974. /** (to be made private)
  975. Returns true if SkPaint does not include elements requiring extensive computation
  976. to compute SkBaseDevice bounds of drawn geometry. For instance, SkPaint with SkPathEffect
  977. always returns false.
  978. @return true if SkPaint allows for fast computation of bounds
  979. */
  980. bool canComputeFastBounds() const;
  981. /** (to be made private)
  982. Only call this if canComputeFastBounds() returned true. This takes a
  983. raw rectangle (the raw bounds of a shape), and adjusts it for stylistic
  984. effects in the paint (e.g. stroking). If needed, it uses the storage
  985. parameter. It returns the adjusted bounds that can then be used
  986. for SkCanvas::quickReject tests.
  987. The returned SkRect will either be orig or storage, thus the caller
  988. should not rely on storage being set to the result, but should always
  989. use the returned value. It is legal for orig and storage to be the same
  990. SkRect.
  991. For example:
  992. if (!path.isInverseFillType() && paint.canComputeFastBounds()) {
  993. SkRect storage;
  994. if (canvas->quickReject(paint.computeFastBounds(path.getBounds(), &storage))) {
  995. return; // do not draw the path
  996. }
  997. }
  998. // draw the path
  999. @param orig geometry modified by SkPaint when drawn
  1000. @param storage computed bounds of geometry; may not be nullptr
  1001. @return fast computed bounds
  1002. */
  1003. const SkRect& computeFastBounds(const SkRect& orig, SkRect* storage) const {
  1004. // Things like stroking, etc... will do math on the bounds rect, assuming that it's sorted.
  1005. SkASSERT(orig.isSorted());
  1006. SkPaint::Style style = this->getStyle();
  1007. // ultra fast-case: filling with no effects that affect geometry
  1008. if (kFill_Style == style) {
  1009. uintptr_t effects = reinterpret_cast<uintptr_t>(this->getLooper());
  1010. effects |= reinterpret_cast<uintptr_t>(this->getMaskFilter());
  1011. effects |= reinterpret_cast<uintptr_t>(this->getPathEffect());
  1012. effects |= reinterpret_cast<uintptr_t>(this->getImageFilter());
  1013. if (!effects) {
  1014. return orig;
  1015. }
  1016. }
  1017. return this->doComputeFastBounds(orig, storage, style);
  1018. }
  1019. /** (to be made private)
  1020. @param orig geometry modified by SkPaint when drawn
  1021. @param storage computed bounds of geometry
  1022. @return fast computed bounds
  1023. */
  1024. const SkRect& computeFastStrokeBounds(const SkRect& orig,
  1025. SkRect* storage) const {
  1026. return this->doComputeFastBounds(orig, storage, kStroke_Style);
  1027. }
  1028. /** (to be made private)
  1029. Computes the bounds, overriding the SkPaint SkPaint::Style. This can be used to
  1030. account for additional width required by stroking orig, without
  1031. altering SkPaint::Style set to fill.
  1032. @param orig geometry modified by SkPaint when drawn
  1033. @param storage computed bounds of geometry
  1034. @param style overrides SkPaint::Style
  1035. @return fast computed bounds
  1036. */
  1037. const SkRect& doComputeFastBounds(const SkRect& orig, SkRect* storage,
  1038. Style style) const;
  1039. private:
  1040. friend class SkGlyphRun;
  1041. friend class SkGlyphRunBuilder;
  1042. SkPaint(const SkPaint&, const SkRunFont&);
  1043. sk_sp<SkTypeface> fTypeface;
  1044. sk_sp<SkPathEffect> fPathEffect;
  1045. sk_sp<SkShader> fShader;
  1046. sk_sp<SkMaskFilter> fMaskFilter;
  1047. sk_sp<SkColorFilter> fColorFilter;
  1048. sk_sp<SkDrawLooper> fDrawLooper;
  1049. sk_sp<SkImageFilter> fImageFilter;
  1050. SkScalar fTextSize;
  1051. SkScalar fTextScaleX;
  1052. SkScalar fTextSkewX;
  1053. SkColor4f fColor4f;
  1054. SkScalar fWidth;
  1055. SkScalar fMiterLimit;
  1056. uint32_t fBlendMode; // just need 5-6 bits
  1057. union {
  1058. struct {
  1059. // all of these bitfields should add up to 32
  1060. unsigned fFlags : 16;
  1061. unsigned fCapType : 2;
  1062. unsigned fJoinType : 2;
  1063. unsigned fStyle : 2;
  1064. unsigned fTextEncoding : 2; // 3 values
  1065. unsigned fHinting : 2;
  1066. unsigned fFilterQuality : 2;
  1067. //unsigned fFreeBits : 4;
  1068. } fBitfields;
  1069. uint32_t fBitfieldsUInt;
  1070. };
  1071. SkScalar measure_text(SkGlyphCache*, const char* text, size_t length,
  1072. int* count, SkRect* bounds) const;
  1073. /*
  1074. * The luminance color is used to determine which Gamma Canonical color to map to. This is
  1075. * really only used by backends which want to cache glyph masks, and need some way to know if
  1076. * they need to generate new masks based off a given color.
  1077. */
  1078. SkColor computeLuminanceColor() const;
  1079. /* This is the size we use when we ask for a glyph's path. We then
  1080. * post-transform it as we draw to match the request.
  1081. * This is done to try to re-use cache entries for the path.
  1082. *
  1083. * This value is somewhat arbitrary. In theory, it could be 1, since
  1084. * we store paths as floats. However, we get the path from the font
  1085. * scaler, and it may represent its paths as fixed-point (or 26.6),
  1086. * so we shouldn't ask for something too big (might overflow 16.16)
  1087. * or too small (underflow 26.6).
  1088. *
  1089. * This value could track kMaxSizeForGlyphCache, assuming the above
  1090. * constraints, but since we ask for unhinted paths, the two values
  1091. * need not match per-se.
  1092. */
  1093. static constexpr int kCanonicalTextSizeForPaths = 64;
  1094. static bool TooBigToUseCache(const SkMatrix& ctm, const SkMatrix& textM, SkScalar maxLimit);
  1095. // Set flags/hinting/textSize up to use for drawing text as paths.
  1096. // Returns scale factor to restore the original textSize, since will will
  1097. // have change it to kCanonicalTextSizeForPaths.
  1098. SkScalar setupForAsPaths();
  1099. static SkScalar MaxCacheSize2(SkScalar maxLimit);
  1100. friend class GrTextBlob;
  1101. friend class GrTextContext;
  1102. friend class GrGLPathRendering;
  1103. friend class GrPathRendering;
  1104. friend class SkAutoGlyphCacheNoGamma;
  1105. friend class SkCanonicalizePaint;
  1106. friend class SkCanvas;
  1107. friend class SkDraw;
  1108. friend class SkFont;
  1109. friend class SkGlyphRunListPainter;
  1110. friend class SkPaintPriv;
  1111. friend class SkPDFDevice;
  1112. friend class SkScalerContext; // for computeLuminanceColor()
  1113. friend class SkTextBaseIter;
  1114. friend class SkTextBlobCacheDiffCanvas;
  1115. };
  1116. #endif