OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 #define __STDC_LIMIT_MACROS | 7 #define __STDC_LIMIT_MACROS |
8 | 8 |
9 #include "SkDraw.h" | 9 #include "SkDraw.h" |
10 #include "SkBlitter.h" | 10 #include "SkBlitter.h" |
11 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
12 #include "SkColorPriv.h" | 12 #include "SkColorPriv.h" |
13 #include "SkDevice.h" | 13 #include "SkDevice.h" |
14 #include "SkDeviceLooper.h" | 14 #include "SkDeviceLooper.h" |
15 #include "SkFixed.h" | 15 #include "SkFixed.h" |
16 #include "SkMaskFilter.h" | 16 #include "SkMaskFilter.h" |
17 #include "SkMatrix.h" | |
17 #include "SkPaint.h" | 18 #include "SkPaint.h" |
18 #include "SkPathEffect.h" | 19 #include "SkPathEffect.h" |
19 #include "SkRasterClip.h" | 20 #include "SkRasterClip.h" |
20 #include "SkRasterizer.h" | 21 #include "SkRasterizer.h" |
21 #include "SkRRect.h" | 22 #include "SkRRect.h" |
22 #include "SkScan.h" | 23 #include "SkScan.h" |
23 #include "SkShader.h" | 24 #include "SkShader.h" |
24 #include "SkSmallAllocator.h" | 25 #include "SkSmallAllocator.h" |
25 #include "SkString.h" | 26 #include "SkString.h" |
26 #include "SkStroke.h" | 27 #include "SkStroke.h" |
27 #include "SkStrokeRec.h" | 28 #include "SkStrokeRec.h" |
29 #include "SkTemplates.h" | |
28 #include "SkTextMapStateProc.h" | 30 #include "SkTextMapStateProc.h" |
29 #include "SkTLazy.h" | 31 #include "SkTLazy.h" |
32 #include "SkUtility.h" | |
30 #include "SkUtils.h" | 33 #include "SkUtils.h" |
31 #include "SkVertState.h" | 34 #include "SkVertState.h" |
32 | 35 |
33 #include "SkAutoKern.h" | 36 #include "SkAutoKern.h" |
34 #include "SkBitmapProcShader.h" | 37 #include "SkBitmapProcShader.h" |
35 #include "SkDrawProcs.h" | 38 #include "SkDrawProcs.h" |
36 #include "SkMatrixUtils.h" | 39 #include "SkMatrixUtils.h" |
37 | 40 |
38 //#define TRACE_BITMAP_DRAWS | 41 //#define TRACE_BITMAP_DRAWS |
39 | 42 |
(...skipping 1392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1432 prevXPos = xpos; | 1435 prevXPos = xpos; |
1433 } | 1436 } |
1434 } | 1437 } |
1435 | 1438 |
1436 // disable warning : local variable used without having been initialized | 1439 // disable warning : local variable used without having been initialized |
1437 #if defined _WIN32 && _MSC_VER >= 1300 | 1440 #if defined _WIN32 && _MSC_VER >= 1300 |
1438 #pragma warning ( push ) | 1441 #pragma warning ( push ) |
1439 #pragma warning ( disable : 4701 ) | 1442 #pragma warning ( disable : 4701 ) |
1440 #endif | 1443 #endif |
1441 | 1444 |
1442 ////////////////////////////////////////////////////////////////////////////// | 1445 //////////////////////////////////////////////////////////////////////////////// //////////////////// |
1443 | 1446 |
1444 static void D1G_RectClip(const SkDraw1Glyph& state, Sk48Dot16 fx, Sk48Dot16 fy, const SkGlyph& glyph) { | 1447 static void D1G_RectClip(const SkDraw1Glyph& state, Sk48Dot16 fx, Sk48Dot16 fy, const SkGlyph& glyph) { |
1445 // Prevent glyphs from being drawn outside of or straddling the edge of devi ce space. | 1448 // Prevent glyphs from being drawn outside of or straddling the edge of devi ce space. |
1446 if ((fx >> 16) > INT_MAX - (INT16_MAX + UINT16_MAX) || | 1449 if ((fx >> 16) > INT_MAX - (INT16_MAX + UINT16_MAX) || |
1447 (fx >> 16) < INT_MIN - (INT16_MIN + 0 /*UINT16_MIN*/) || | 1450 (fx >> 16) < INT_MIN - (INT16_MIN + 0 /*UINT16_MIN*/) || |
1448 (fy >> 16) > INT_MAX - (INT16_MAX + UINT16_MAX) || | 1451 (fy >> 16) > INT_MAX - (INT16_MAX + UINT16_MAX) || |
1449 (fy >> 16) < INT_MIN - (INT16_MIN + 0 /*UINT16_MIN*/)) | 1452 (fy >> 16) < INT_MIN - (INT16_MIN + 0 /*UINT16_MIN*/)) |
1450 { | 1453 { |
1451 return; | 1454 return; |
1452 } | 1455 } |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1719 fDevice->drawPath(*this, *path, paint, &matrix, false); | 1722 fDevice->drawPath(*this, *path, paint, &matrix, false); |
1720 } else { | 1723 } else { |
1721 this->drawPath(*path, paint, &matrix, false); | 1724 this->drawPath(*path, paint, &matrix, false); |
1722 } | 1725 } |
1723 } | 1726 } |
1724 } | 1727 } |
1725 pos += scalarsPerPosition; | 1728 pos += scalarsPerPosition; |
1726 } | 1729 } |
1727 } | 1730 } |
1728 | 1731 |
1732 //////////////////////////////////////////////////////////////////////////////// //////////////////// | |
1733 // Calculate a type with the same size as the max of all the Ts. | |
1734 template <typename... Ts> struct MaxSizeOf; | |
1735 | |
1736 template <> struct MaxSizeOf<> { static const size_t value = 0; }; | |
1737 | |
1738 template <typename H, typename... Ts> struct MaxSizeOf<H, Ts...> { | |
1739 static const size_t value = | |
1740 sizeof(H) >= MaxSizeOf<Ts...>::value ? sizeof(H) : MaxSizeOf<Ts...>::val ue; | |
1741 }; | |
1742 | |
1743 // UntaggedVariant is a pile of memory that can hold one of the Ts. It provides a way | |
1744 // to initialize that memory in a typesafe way. | |
1745 template <typename... Ts> | |
1746 class UntaggedVariant { | |
1747 public: | |
1748 UntaggedVariant() {} | |
1749 ~UntaggedVariant() {} | |
1750 UntaggedVariant(const UntaggedVariant&) = delete; | |
1751 UntaggedVariant& operator=(const UntaggedVariant&) = delete; | |
1752 UntaggedVariant(UntaggedVariant&&) = delete; | |
1753 UntaggedVariant& operator=(UntaggedVariant&&) = delete; | |
1754 | |
1755 template <typename Variant, typename... Args> | |
1756 void initialize(Args &&... args) { | |
1757 SkASSERT(sizeof(Variant) <= sizeof(fSpace)); | |
1758 #if defined(_MSC_VER) && _MSC_VER < 1900 | |
1759 #define alignof __alignof | |
1760 #endif | |
1761 SkASSERT(alignof(Variant) <= alignof(Space)); | |
1762 new (&fSpace) Variant(skstd::forward<Args>(args)...); | |
1763 } | |
1764 | |
1765 private: | |
1766 typedef SkAlignedSStorage<MaxSizeOf<Ts...>::value> Space; | |
1767 Space fSpace; | |
1768 }; | |
1769 | |
1770 // PolymorphicVariant holds subclasses of Base without slicing. Ts must be subcl asses of Base. | |
1771 template <typename Base, typename... Ts> | |
1772 class PolymorphicVariant { | |
1773 public: | |
1774 typedef UntaggedVariant<Ts...> Variants; | |
1775 template <typename Initializer> | |
1776 PolymorphicVariant(Initializer&& initializer) { | |
1777 initializer(&fVariants); | |
1778 } | |
1779 ~PolymorphicVariant() { get()->~Base(); } | |
1780 Base* get() const { return reinterpret_cast<Base*>(&fVariants); } | |
1781 Base* operator->() const { return get(); } | |
1782 Base& operator*() const { return *get(); } | |
1783 | |
1784 private: | |
1785 mutable Variants fVariants; | |
1786 }; | |
1787 | |
1788 // PositionReaderInterface reads a point from the pos vector. | |
1789 // * HorizontalPositions - assumes a common Y for many X values. | |
1790 // * ArbitraryPositions - a list of (X,Y) pairs. | |
1791 class PositionReaderInterface : SkNoncopyable { | |
1792 public: | |
1793 virtual ~PositionReaderInterface() { } | |
1794 virtual const SkPoint nextPoint() = 0; | |
mtklein
2015/11/09 16:06:40
The const here is sort of pointless eh?
herb_g
2015/11/09 16:37:11
Done.
| |
1795 }; | |
1796 | |
1797 class HorizontalPositions final : public PositionReaderInterface { | |
1798 public: | |
1799 HorizontalPositions(const SkScalar* positions) | |
1800 : fPositions(positions) { } | |
1801 | |
1802 const SkPoint nextPoint() override { | |
1803 SkScalar x = *fPositions++; | |
1804 return {x, 0}; | |
1805 } | |
1806 private: | |
1807 const SkScalar* fPositions; | |
1808 }; | |
1809 | |
1810 class ArbitraryPositions final : public PositionReaderInterface { | |
1811 public: | |
1812 ArbitraryPositions(const SkScalar* positions) | |
1813 : fPositions(positions) { } | |
1814 const SkPoint nextPoint() override { | |
1815 SkPoint to_return {fPositions[0], fPositions[1]}; | |
1816 fPositions += 2; | |
1817 return to_return; | |
1818 } | |
1819 | |
1820 private: | |
1821 const SkScalar* fPositions; | |
1822 }; | |
1823 | |
1824 typedef PolymorphicVariant<PositionReaderInterface, HorizontalPositions, Arbitra ryPositions> | |
1825 PositionReader; | |
1826 | |
1827 // MapperInterface given a point map it through the matrix. There are several sh ortcut variants. | |
1828 // * TranslationMapper - assumes a translation only matrix. | |
1829 // * XScaleMapper - assumes an X scaling and a translation. | |
1830 // * GeneralMapper - Does all other matricies. | |
1831 class MapperInterface : SkNoncopyable { | |
1832 public: | |
1833 virtual ~MapperInterface() {} | |
1834 virtual SkPoint map(SkPoint position) const = 0; | |
1835 }; | |
1836 | |
1837 class TranslationMapper final : public MapperInterface { | |
1838 public: | |
1839 TranslationMapper(const SkMatrix& matrix, const SkPoint origin) | |
1840 : fTranslate(matrix.mapXY(origin.fX, origin.fY)) { } | |
1841 SkPoint map(SkPoint position) const override { | |
1842 return position + fTranslate; | |
1843 } | |
1844 | |
1845 private: | |
1846 const SkPoint fTranslate; | |
1847 }; | |
1848 | |
1849 class XScaleMapper final : public MapperInterface { | |
1850 public: | |
1851 XScaleMapper(const SkMatrix& matrix, const SkPoint origin) | |
1852 : fTranslate(matrix.mapXY(origin.fX, origin.fY)) | |
1853 , fXScale(matrix.getScaleX()) { } | |
1854 SkPoint map(SkPoint position) const override { | |
1855 return {SkScalarMul(fXScale, position.fX) + fTranslate.fX, fTranslate.fY }; | |
mtklein
2015/11/09 16:06:40
SkScalarMul(x,y) can be safely replaced with (x*y)
herb_g
2015/11/09 16:37:11
Done.
| |
1856 } | |
1857 | |
1858 private: | |
1859 const SkPoint fTranslate; | |
1860 const SkScalar fXScale; | |
1861 }; | |
1862 | |
1863 // The caller must keep matrix alive while this class is used. | |
1864 class GeneralMapper final : public MapperInterface { | |
1865 public: | |
1866 GeneralMapper(const SkMatrix& matrix, const SkPoint origin) | |
1867 : fOrigin(origin) | |
1868 , fMatrix(matrix) | |
1869 , fMapProc(matrix.getMapXYProc()) { } | |
1870 SkPoint map(SkPoint position) const override { | |
1871 SkPoint result; | |
1872 fMapProc(fMatrix, position.fX + fOrigin.fX, position.fY + fOrigin.fY, &r esult); | |
1873 return result; | |
1874 } | |
1875 | |
1876 private: | |
1877 const SkPoint fOrigin; | |
1878 const SkMatrix& fMatrix; | |
1879 const SkMatrix::MapXYProc fMapProc; | |
1880 }; | |
1881 | |
1882 typedef PolymorphicVariant<MapperInterface, TranslationMapper, XScaleMapper, Gen eralMapper> Mapper; | |
1883 | |
1884 //////////////////////////////////////////////////////////////////////////////// //////////////////// | |
1885 // Text alignment handles shifting the glyph based on its width. | |
1886 static SkPoint text_alignment_adjustment(SkPaint::Align textAlignment, const SkG lyph& glyph) { | |
1887 switch (textAlignment) { | |
1888 case SkPaint::kLeft_Align: | |
1889 return {0.0f, 0.0f}; | |
1890 break; | |
1891 case SkPaint::kCenter_Align: | |
1892 return {SkFixedToScalar(glyph.fAdvanceX >> 1), | |
1893 SkFixedToScalar(glyph.fAdvanceY >> 1)}; | |
1894 break; | |
1895 case SkPaint::kRight_Align: | |
1896 return {SkFixedToScalar(glyph.fAdvanceX), | |
1897 SkFixedToScalar(glyph.fAdvanceY)}; | |
1898 break; | |
1899 } | |
1900 // Even though the entire enum is covered above, MVSC doesn't think so. Make it happy. | |
1901 SkFAIL("Should never get here."); | |
1902 return {0.0f, 0.0f}; | |
1903 } | |
1904 | |
1905 // The "call" to SkFixedToScalar is actually a macro. It's macros all the way do wn. | |
1906 static const SkScalar kSubpixelRounding = SkFixedToScalar(SkGlyph::kSubpixelRoun d); | |
1907 | |
1908 //////////////////////////////////////////////////////////////////////////////// //////////////////// | |
1909 // Functions for handling sub-pixel aligned positions. | |
1910 // The subpixel_position_rounding function returns a point suitable for rounding a sub-pixel | |
1911 // positioned glyph. | |
1912 static SkPoint subpixel_position_rounding(SkAxisAlignment axisAlignment) { | |
1913 switch (axisAlignment) { | |
1914 case kX_SkAxisAlignment: | |
1915 return {SkFixedToScalar(SkGlyph::kSubpixelRound), SK_ScalarHalf}; | |
1916 case kY_SkAxisAlignment: | |
1917 return {SK_ScalarHalf, kSubpixelRounding}; | |
1918 case kNone_SkAxisAlignment: | |
1919 return {kSubpixelRounding, kSubpixelRounding}; | |
1920 default: | |
mtklein
2015/11/09 16:06:40
I sort of like your default-less style in text_ali
herb_g
2015/11/09 16:37:11
Done.
| |
1921 SkFAIL("Should not get here."); | |
1922 return {0.0f, 0.0f}; | |
1923 } | |
1924 } | |
1925 | |
1926 // The subpixel_position_alignment function produces a suitable position for the glyph cache to | |
1927 // produce the correct sub-pixel alignment. If a position is aligned with an axi s a shortcut of 0 | |
1928 // is used for the sub-pixel position. | |
1929 static SkIPoint subpixel_position_alignment(SkAxisAlignment axisAlignment, SkPoi nt position) { | |
1930 switch (axisAlignment) { | |
1931 case kX_SkAxisAlignment: | |
1932 return {SkScalarToFixed(position.fX + kSubpixelRounding), 0}; | |
1933 case kY_SkAxisAlignment: | |
1934 return {0, SkScalarToFixed(position.fY + kSubpixelRounding)}; | |
1935 case kNone_SkAxisAlignment: | |
1936 return {SkScalarToFixed(position.fX + kSubpixelRounding), | |
1937 SkScalarToFixed(position.fY + kSubpixelRounding)}; | |
1938 default: | |
mtklein
2015/11/09 16:06:40
Ditto.
herb_g
2015/11/09 16:37:11
Done.
| |
1939 SkFAIL("Should not get here."); | |
1940 return {0, 0}; | |
1941 } | |
1942 } | |
1943 | |
1944 // GlyphFindAndPlaceInterface given the text and position finds the correct glyp h and does glyph | |
1945 // specific position adjustment. The findAndPositionGlyph method takes text and position and calls | |
1946 // processOneGlyph with the correct glyph, final position and rounding terms. Th e final position | |
1947 // is not rounded yet and is the responsibility of processOneGlyph. | |
1948 template <typename GlyphProcessor> | |
mtklein
2015/11/09 16:06:40
As a name, I think I'd have followed this better i
herb_g
2015/11/09 16:37:11
Done.
| |
1949 class GlyphFindAndPlaceInterface : SkNoncopyable { | |
1950 public: | |
1951 struct Result { | |
1952 const SkGlyph* fGlyph; | |
1953 Sk48Dot16 fX; | |
1954 Sk48Dot16 fY; | |
1955 }; | |
1956 virtual ~GlyphFindAndPlaceInterface() { }; | |
1957 // This should be a pure virtual, but some versions of GCC <= 4.8 have a bug that causes a | |
1958 // compile error. | |
1959 // See GCC bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60277 | |
1960 virtual void findAndPositionGlyph(const char** text, SkPoint position, | |
1961 GlyphProcessor&& processOneGlyph) {}; | |
1962 }; | |
1963 | |
1964 // GlyphFindAndPlaceForSubpixel handles finding and placing glyphs when sub-pixe l positioning is | |
1965 // requested. After it has found and placed the glyph it calls the templated fun ction | |
1966 // GlyphProcessor in order to actually perform an action. | |
1967 template <typename GlyphProcessor, SkPaint::Align kTextAlignment, SkAxisAlignmen t kAxisAlignment> | |
1968 class GlyphFindAndPlaceForSubpixel final : public GlyphFindAndPlaceInterface<Gly phProcessor> { | |
1969 public: | |
1970 GlyphFindAndPlaceForSubpixel(SkGlyphCache* cache, SkDrawCacheProc glyphCache Proc) | |
1971 : fCache(cache) | |
1972 , fGlyphCacheProc(glyphCacheProc) { | |
1973 } | |
1974 void findAndPositionGlyph(const char** text, SkPoint position, | |
1975 GlyphProcessor&& processOneGlyph) override { | |
1976 SkPoint finalPosition = position; | |
1977 if (kTextAlignment != SkPaint::kLeft_Align) { | |
1978 // Get the width of an un-sub-pixel positioned glyph for calculating the alignment. | |
1979 // This is not needed for kLeftAlign because its adjustment is alway s {0, 0}. | |
1980 const char* tempText = *text; | |
1981 const SkGlyph& metricGlyph = fGlyphCacheProc(fCache, &tempText, 0, 0 ); | |
1982 | |
1983 if (metricGlyph.fWidth <= 0) { | |
1984 return; | |
1985 } | |
1986 | |
1987 // Adjust the final position by the alignment adjustment. | |
1988 finalPosition -= text_alignment_adjustment(kTextAlignment, metricGly ph); | |
1989 } | |
1990 | |
1991 // Find the glyph. | |
1992 SkIPoint lookupPosition = subpixel_position_alignment(kAxisAlignment, fi nalPosition); | |
1993 const SkGlyph& renderGlyph = fGlyphCacheProc( | |
1994 fCache, text, lookupPosition.fX, lookupPosition.fY); | |
1995 | |
1996 // If the glyph has no width (no pixels) then don't bother processing it . | |
1997 if (renderGlyph.fWidth > 0) { | |
1998 processOneGlyph(renderGlyph, finalPosition, subpixel_position_roundi ng(kAxisAlignment)); | |
1999 } | |
2000 } | |
2001 | |
2002 private: | |
2003 SkGlyphCache* const fCache; | |
2004 SkDrawCacheProc fGlyphCacheProc; | |
2005 }; | |
2006 | |
2007 // GlyphFindAndPlaceForFullPixel handles finding and placing glyphs when no sub- pixel positioning | |
2008 // is requested. | |
2009 template <typename GlyphProcessor, SkPaint::Align kTextAlignment> | |
2010 class GlyphFindAndPlaceForFullPixel final : public GlyphFindAndPlaceInterface<Gl yphProcessor> { | |
2011 public: | |
2012 GlyphFindAndPlaceForFullPixel(SkGlyphCache* cache, SkDrawCacheProc glyphCach eProc) | |
2013 : fCache(cache) | |
2014 , fGlyphCacheProc(glyphCacheProc) { } | |
2015 void findAndPositionGlyph(const char** text, SkPoint position, | |
2016 GlyphProcessor&& processOneGlyph) override { | |
2017 SkPoint finalPosition = position; | |
2018 const SkGlyph& glyph = fGlyphCacheProc(fCache, text, 0, 0); | |
2019 if (glyph.fWidth <= 0) { | |
2020 return; | |
2021 } | |
2022 finalPosition -= text_alignment_adjustment(kTextAlignment, glyph); | |
2023 processOneGlyph(glyph, finalPosition, {SK_ScalarHalf, SK_ScalarHalf}); | |
2024 } | |
2025 | |
2026 private: | |
2027 SkGlyphCache* const fCache; | |
2028 SkDrawCacheProc fGlyphCacheProc; | |
2029 }; | |
2030 | |
2031 // GlyphFindAndPlace is a large variant that encapsulates the multiple types of finding and | |
2032 // placing a glyph. There are three factors that go into the different factors. | |
2033 // * Is sub-pixel positioned - a boolean that says whether to use sub-pixel posi tioning. | |
2034 // * Text alignment - indicates if the glyph should be placed to the right, cent ered or left of a | |
2035 // given position. | |
2036 // * Axis alignment - indicates if the glyphs final sub-pixel position should be rounded to a | |
2037 // whole pixel if the glyph is aligned with an axis. This is only used for sub -pixel positioning | |
2038 // and allows the baseline to look crisp. | |
2039 template <typename GlyphProcessor> | |
2040 using GlyphFindAndPlace = | |
2041 PolymorphicVariant< | |
2042 GlyphFindAndPlaceInterface<GlyphProcessor>, | |
2043 // Subpixel | |
2044 GlyphFindAndPlaceForSubpixel<GlyphProcessor, SkPaint::kLeft_Align, kNo ne_SkAxisAlignment>, | |
2045 GlyphFindAndPlaceForSubpixel<GlyphProcessor, SkPaint::kLeft_Align, kX_ SkAxisAlignment >, | |
2046 GlyphFindAndPlaceForSubpixel<GlyphProcessor, SkPaint::kLeft_Align, kY_ SkAxisAlignment >, | |
2047 GlyphFindAndPlaceForSubpixel<GlyphProcessor, SkPaint::kCenter_Align, kNo ne_SkAxisAlignment>, | |
2048 GlyphFindAndPlaceForSubpixel<GlyphProcessor, SkPaint::kCenter_Align, kX_ SkAxisAlignment >, | |
2049 GlyphFindAndPlaceForSubpixel<GlyphProcessor, SkPaint::kCenter_Align, kY_ SkAxisAlignment >, | |
2050 GlyphFindAndPlaceForSubpixel<GlyphProcessor, SkPaint::kRight_Align, kNo ne_SkAxisAlignment>, | |
2051 GlyphFindAndPlaceForSubpixel<GlyphProcessor, SkPaint::kRight_Align, kX_ SkAxisAlignment >, | |
2052 GlyphFindAndPlaceForSubpixel<GlyphProcessor, SkPaint::kRight_Align, kY_ SkAxisAlignment >, | |
2053 // Full pixel | |
2054 GlyphFindAndPlaceForFullPixel<GlyphProcessor, SkPaint::kLeft_Align >, | |
2055 GlyphFindAndPlaceForFullPixel<GlyphProcessor, SkPaint::kCenter_Align>, | |
2056 GlyphFindAndPlaceForFullPixel<GlyphProcessor, SkPaint::kRight_Align > | |
2057 >; | |
2058 | |
2059 // init_subpixel is a helper function for initializing all the variants of | |
2060 // GlyphFindAndPlaceForSubpixel. | |
2061 template <typename GlyphProcessor, SkPaint::Align kTextAlignment> | |
2062 static void init_subpixel( | |
2063 typename GlyphFindAndPlace<GlyphProcessor>::Variants* to_init, | |
2064 SkAxisAlignment axisAlignment, | |
2065 SkGlyphCache* cache, | |
2066 SkDrawCacheProc glyphCacheProc) { | |
2067 switch (axisAlignment) { | |
2068 case kX_SkAxisAlignment: | |
2069 to_init->template initialize<GlyphFindAndPlaceForSubpixel< | |
2070 GlyphProcessor, kTextAlignment, kX_SkAxisAlignment>>( | |
2071 cache, glyphCacheProc); | |
2072 break; | |
2073 case kNone_SkAxisAlignment: | |
2074 to_init->template initialize<GlyphFindAndPlaceForSubpixel< | |
2075 GlyphProcessor, kTextAlignment, kNone_SkAxisAlignment>>( | |
2076 cache, glyphCacheProc); | |
2077 break; | |
2078 case kY_SkAxisAlignment: | |
2079 to_init->template initialize<GlyphFindAndPlaceForSubpixel< | |
2080 GlyphProcessor, kTextAlignment, kY_SkAxisAlignment>>( | |
2081 cache, glyphCacheProc); | |
2082 break; | |
2083 } | |
2084 } | |
2085 | |
2086 // specialized_process_pos_text is a version of ProcessPosText that de-virtualiz es the different | |
2087 // components used. It returns true if it can handle the situation, otherwise it returns false. | |
2088 // This allows greater inlining freedom to the compiler. Currently, there is onl y one specialized | |
2089 // variant: sub-pixel position, left-aligned, x-axis-aligned, translation, and o ne scalar per | |
2090 // position entry. | |
2091 // * This is by far the most common type of text Blink draws. | |
2092 template <typename GlyphProcessor> | |
2093 static bool specialized_process_pos_text(const char *const text, size_t byteLeng th, | |
2094 const SkPoint &offset, const SkMatrix & matrix, | |
2095 const SkScalar pos[], int scalarsPerPos ition, | |
2096 SkPaint::Align textAlignment, | |
2097 SkDrawCacheProc &glyphCacheProc, | |
mtklein
2015/11/09 16:06:40
Usually we put the * or & on the type.
It's also
herb_g
2015/11/09 16:37:11
Yep. Sometimes when I upgrade my IDE, my prefs get
| |
2098 SkGlyphCache *cache, GlyphProcessor &&p rocessOneGlyph) { | |
2099 SkAxisAlignment axisAlignment = SkComputeAxisAlignmentForHText(matrix); | |
2100 uint32_t mtype = matrix.getType(); | |
2101 if (scalarsPerPosition == 1 | |
2102 && textAlignment == SkPaint::kLeft_Align | |
2103 && axisAlignment == kX_SkAxisAlignment | |
2104 && cache->isSubpixel() | |
2105 && mtype <= SkMatrix::kTranslate_Mask) { | |
2106 typedef GlyphFindAndPlaceForSubpixel< | |
2107 GlyphProcessor, SkPaint::kLeft_Align, kX_SkAxisAlignment> Positioner ; | |
2108 HorizontalPositions positions{pos}; | |
2109 TranslationMapper mapper{matrix, offset}; | |
2110 Positioner positioner(cache, glyphCacheProc); | |
2111 const char *cursor = text; | |
2112 const char *stop = text + byteLength; | |
2113 while (cursor < stop) { | |
2114 SkPoint mappedPoint = mapper.TranslationMapper::map( | |
2115 positions.HorizontalPositions::nextPoint()); | |
2116 positioner.Positioner::findAndPositionGlyph( | |
2117 &cursor, mappedPoint, skstd::forward<GlyphProcessor>(processOneG lyph)); | |
mtklein
2015/11/09 16:06:40
Out of curiosity, what happens if we don't forward
herb_g
2015/11/09 16:37:11
Things get slower if I use const T& style, but I h
| |
2118 } | |
2119 return true; | |
2120 } | |
2121 return false; | |
2122 } | |
2123 | |
2124 // ProcessPosText handles all cases for finding and positioning glyphs. It has a very large | |
2125 // multiplicity. It figures out the glpyh, position and rounding and pass those parameters to | |
2126 // processOneGlyph. | |
2127 // | |
2128 // The routine processOneGlyph passed in by the client has the following signatu re: | |
2129 // void f(const SkGlyph& glyph, SkPoint position, SkPoint rounding); | |
2130 // | |
2131 // * Sub-pixel positioning (2) - use sub-pixel positioning. | |
2132 // * Text alignment (3) - text alignment with respect to the glyph's width. | |
2133 // * Matrix type (3) - special cases for translation and X-coordinate scaling. | |
2134 // * Components per position (2) - the positions vector can have a common Y with different Xs, or | |
2135 // XY-pairs. | |
2136 // * Axis Alignment (for sub-pixel positioning) (3) - when using sub-pixel posit ioning, round to | |
2137 // a whole coordinate instead of using sub-pixel positioning. | |
2138 // The number of variations is 108 for sub-pixel and 36 for full-pixel. | |
2139 // This routine handles all of them using inline polymorphic variable (no heap a llocation). | |
2140 template <typename GlyphProcessor> | |
2141 static void process_pos_text(const char text[], size_t byteLength, | |
2142 const SkPoint& offset, const SkMatrix& matrix, | |
2143 const SkScalar pos[], int scalarsPerPosition, | |
2144 SkPaint::Align textAlignment, SkDrawCacheProc& glyp hCacheProc, | |
2145 SkGlyphCache* cache, GlyphProcessor&& processOneGly ph) { | |
2146 | |
2147 PositionReader positionReader { | |
2148 [&](PositionReader::Variants* to_init) { | |
2149 if (2 == scalarsPerPosition) { | |
2150 to_init->initialize<ArbitraryPositions>(pos); | |
2151 } else { | |
2152 to_init->initialize<HorizontalPositions>(pos); | |
2153 } | |
2154 } | |
2155 }; | |
2156 | |
2157 Mapper mapper { | |
2158 [&] (Mapper::Variants* to_init) { | |
2159 uint32_t mtype = matrix.getType(); | |
2160 if (mtype & (SkMatrix::kAffine_Mask | SkMatrix::kPerspective_Mask) | |
2161 || scalarsPerPosition == 2) { | |
2162 to_init->initialize<GeneralMapper>(matrix, offset); | |
2163 } else if (mtype & SkMatrix::kScale_Mask) { | |
2164 to_init->initialize<XScaleMapper>(matrix, offset); | |
2165 } else { | |
2166 to_init->initialize<TranslationMapper>(matrix, offset); | |
2167 } | |
2168 } | |
2169 }; | |
2170 | |
2171 GlyphFindAndPlace<GlyphProcessor> findAndPosition{ | |
2172 [&](typename GlyphFindAndPlace<GlyphProcessor>::Variants* to_init) { | |
2173 if (cache->isSubpixel()) { | |
2174 SkAxisAlignment axisAlignment = SkComputeAxisAlignmentForHText(m atrix); | |
2175 switch (textAlignment) { | |
2176 case SkPaint::kLeft_Align: | |
2177 init_subpixel<GlyphProcessor, SkPaint::kLeft_Align>( | |
2178 to_init, axisAlignment, cache, glyphCacheProc); | |
2179 break; | |
2180 case SkPaint::kCenter_Align: | |
2181 init_subpixel<GlyphProcessor, SkPaint::kCenter_Align>( | |
2182 to_init, axisAlignment, cache, glyphCacheProc); | |
2183 break; | |
2184 case SkPaint::kRight_Align: | |
2185 init_subpixel<GlyphProcessor, SkPaint::kRight_Align>( | |
2186 to_init, axisAlignment, cache, glyphCacheProc); | |
2187 break; | |
2188 } | |
2189 } else { | |
2190 switch (textAlignment) { | |
2191 case SkPaint::kLeft_Align: | |
2192 to_init->template initialize<GlyphFindAndPlaceForFullPix el<GlyphProcessor, | |
2193 SkPaint::kLeft_Align>>( | |
2194 cache, glyphCacheProc); | |
2195 break; | |
2196 case SkPaint::kCenter_Align: | |
2197 to_init->template initialize<GlyphFindAndPlaceForFullPix el<GlyphProcessor, | |
2198 SkPaint::kCenter_Align>>( | |
2199 cache, glyphCacheProc); | |
2200 break; | |
2201 case SkPaint::kRight_Align: | |
2202 to_init->template initialize<GlyphFindAndPlaceForFullPix el<GlyphProcessor, | |
2203 SkPaint::kRight_Align>>( | |
2204 cache, glyphCacheProc); | |
2205 break; | |
2206 } | |
2207 } | |
2208 } | |
2209 }; | |
2210 | |
2211 const char* stop = text + byteLength; | |
2212 while (text < stop) { | |
2213 SkPoint mappedPoint = mapper->map(positionReader->nextPoint()); | |
2214 findAndPosition->findAndPositionGlyph( | |
2215 &text, mappedPoint, skstd::forward<GlyphProcessor>(processOneGlyph)) ; | |
mtklein
2015/11/09 16:06:40
Same?
herb_g
2015/11/09 16:37:11
See above
| |
2216 } | |
2217 } | |
2218 | |
1729 void SkDraw::drawPosText(const char text[], size_t byteLength, | 2219 void SkDraw::drawPosText(const char text[], size_t byteLength, |
1730 const SkScalar pos[], int scalarsPerPosition, | 2220 const SkScalar pos[], int scalarsPerPosition, |
1731 const SkPoint& offset, const SkPaint& paint) const { | 2221 const SkPoint& offset, const SkPaint& paint) const { |
1732 SkASSERT(byteLength == 0 || text != nullptr); | 2222 SkASSERT(byteLength == 0 || text != nullptr); |
1733 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); | 2223 SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
1734 | 2224 |
1735 SkDEBUGCODE(this->validate();) | 2225 SkDEBUGCODE(this->validate();) |
1736 | 2226 |
1737 // nothing to draw | 2227 // nothing to draw |
1738 if (text == nullptr || byteLength == 0 || fRC->isEmpty()) { | 2228 if (text == nullptr || byteLength == 0 || fRC->isEmpty()) { |
(...skipping 14 matching lines...) Expand all Loading... | |
1753 SkBlitter* blitter = nullptr; | 2243 SkBlitter* blitter = nullptr; |
1754 if (needsRasterTextBlit(*this)) { | 2244 if (needsRasterTextBlit(*this)) { |
1755 blitterChooser.choose(fDst, *fMatrix, paint); | 2245 blitterChooser.choose(fDst, *fMatrix, paint); |
1756 blitter = blitterChooser.get(); | 2246 blitter = blitterChooser.get(); |
1757 if (fRC->isAA()) { | 2247 if (fRC->isAA()) { |
1758 wrapper.init(*fRC, blitter); | 2248 wrapper.init(*fRC, blitter); |
1759 blitter = wrapper.getBlitter(); | 2249 blitter = wrapper.getBlitter(); |
1760 } | 2250 } |
1761 } | 2251 } |
1762 | 2252 |
1763 const char* stop = text + byteLength; | |
1764 SkTextAlignProc alignProc(paint.getTextAlign()); | |
1765 SkDraw1Glyph d1g; | 2253 SkDraw1Glyph d1g; |
1766 SkDraw1Glyph::Proc proc = d1g.init(this, blitter, cache, paint); | 2254 SkDraw1Glyph::Proc proc = d1g.init(this, blitter, cache, paint); |
1767 SkTextMapStateProc tmsProc(*fMatrix, offset, scalarsPerPosition); | |
1768 | 2255 |
1769 if (cache->isSubpixel()) { | 2256 auto glyphProcessor = |
mtklein
2015/11/09 16:06:40
processOneGlyph, for consistency?
herb_g
2015/11/09 16:37:11
Done.
| |
1770 // maybe we should skip the rounding if linearText is set | 2257 [&](const SkGlyph &glyph, SkPoint position, SkPoint rounding) { |
mtklein
2015/11/09 16:06:40
const SkGlyph&
herb_g
2015/11/09 16:37:11
Grrr.
| |
1771 SkAxisAlignment baseline = SkComputeAxisAlignmentForHText(*fMatrix); | 2258 position += rounding; |
2259 proc(d1g, SkScalarToFixed(position.fX), SkScalarToFixed(position.fY) , glyph); | |
2260 }; | |
1772 | 2261 |
1773 SkFixed fxMask = ~0; | 2262 SkPaint::Align textAlignment = paint.getTextAlign(); |
1774 SkFixed fyMask = ~0; | 2263 if (!specialized_process_pos_text(text, byteLength, offset, *fMatrix, pos, s calarsPerPosition, |
1775 if (kX_SkAxisAlignment == baseline) { | 2264 textAlignment, glyphCacheProc, cache, glyphProcessor)) { |
1776 fyMask = 0; | 2265 process_pos_text(text, byteLength, offset, *fMatrix, pos, scalarsPerPosi tion, |
1777 d1g.fHalfSampleY = SK_ScalarHalf; | 2266 textAlignment, glyphCacheProc, cache, glyphProcessor); |
1778 } else if (kY_SkAxisAlignment == baseline) { | |
1779 fxMask = 0; | |
1780 d1g.fHalfSampleX = SK_ScalarHalf; | |
1781 } | |
1782 | |
1783 if (SkPaint::kLeft_Align == paint.getTextAlign()) { | |
1784 while (text < stop) { | |
1785 SkPoint tmsLoc; | |
1786 tmsProc(pos, &tmsLoc); | |
1787 | |
1788 Sk48Dot16 fx = SkScalarTo48Dot16(tmsLoc.fX + d1g.fHalfSampleX); | |
1789 Sk48Dot16 fy = SkScalarTo48Dot16(tmsLoc.fY + d1g.fHalfSampleY); | |
1790 | |
1791 const SkGlyph& glyph = glyphCacheProc(cache, &text, fx & fxMask, fy & fyMask); | |
1792 | |
1793 if (glyph.fWidth) { | |
1794 proc(d1g, fx, fy, glyph); | |
1795 } | |
1796 pos += scalarsPerPosition; | |
1797 } | |
1798 } else { | |
1799 while (text < stop) { | |
1800 const char* currentText = text; | |
1801 const SkGlyph& metricGlyph = glyphCacheProc(cache, &text, 0, 0); | |
1802 | |
1803 if (metricGlyph.fWidth) { | |
1804 SkDEBUGCODE(SkFixed prevAdvX = metricGlyph.fAdvanceX;) | |
1805 SkDEBUGCODE(SkFixed prevAdvY = metricGlyph.fAdvanceY;) | |
1806 SkPoint tmsLoc; | |
1807 tmsProc(pos, &tmsLoc); | |
1808 | |
1809 SkPoint alignLoc; | |
1810 alignProc(tmsLoc, metricGlyph, &alignLoc); | |
1811 | |
1812 Sk48Dot16 fx = SkScalarTo48Dot16(alignLoc.fX + d1g.fHalfSamp leX); | |
1813 Sk48Dot16 fy = SkScalarTo48Dot16(alignLoc.fY + d1g.fHalfSamp leY); | |
1814 | |
1815 // have to call again, now that we've been "aligned" | |
1816 const SkGlyph& glyph = glyphCacheProc(cache, ¤tText, | |
1817 fx & fxMask, fy & fyMa sk); | |
1818 // the assumption is that the metrics haven't changed | |
1819 SkASSERT(prevAdvX == glyph.fAdvanceX); | |
1820 SkASSERT(prevAdvY == glyph.fAdvanceY); | |
1821 SkASSERT(glyph.fWidth); | |
1822 | |
1823 proc(d1g, fx, fy, glyph); | |
1824 } | |
1825 pos += scalarsPerPosition; | |
1826 } | |
1827 } | |
1828 } else { // not subpixel | |
1829 if (SkPaint::kLeft_Align == paint.getTextAlign()) { | |
1830 while (text < stop) { | |
1831 // the last 2 parameters are ignored | |
1832 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); | |
1833 | |
1834 if (glyph.fWidth) { | |
1835 SkPoint tmsLoc; | |
1836 tmsProc(pos, &tmsLoc); | |
1837 | |
1838 proc(d1g, | |
1839 SkScalarTo48Dot16(tmsLoc.fX + SK_ScalarHalf), //d1g.fHa lfSampleX, | |
1840 SkScalarTo48Dot16(tmsLoc.fY + SK_ScalarHalf), //d1g.fHa lfSampleY, | |
1841 glyph); | |
1842 } | |
1843 pos += scalarsPerPosition; | |
1844 } | |
1845 } else { | |
1846 while (text < stop) { | |
1847 // the last 2 parameters are ignored | |
1848 const SkGlyph& glyph = glyphCacheProc(cache, &text, 0, 0); | |
1849 | |
1850 if (glyph.fWidth) { | |
1851 SkPoint tmsLoc; | |
1852 tmsProc(pos, &tmsLoc); | |
1853 | |
1854 SkPoint alignLoc; | |
1855 alignProc(tmsLoc, glyph, &alignLoc); | |
1856 | |
1857 proc(d1g, | |
1858 SkScalarTo48Dot16(alignLoc.fX + SK_ScalarHalf), //d1g.f HalfSampleX, | |
1859 SkScalarTo48Dot16(alignLoc.fY + SK_ScalarHalf), //d1g.f HalfSampleY, | |
1860 glyph); | |
1861 } | |
1862 pos += scalarsPerPosition; | |
1863 } | |
1864 } | |
1865 } | 2267 } |
1866 } | 2268 } |
1867 | 2269 |
1868 #if defined _WIN32 && _MSC_VER >= 1300 | 2270 #if defined _WIN32 && _MSC_VER >= 1300 |
1869 #pragma warning ( pop ) | 2271 #pragma warning ( pop ) |
1870 #endif | 2272 #endif |
1871 | 2273 |
1872 /////////////////////////////////////////////////////////////////////////////// | 2274 /////////////////////////////////////////////////////////////////////////////// |
1873 | 2275 |
1874 static SkScan::HairRCProc ChooseHairProc(bool doAntiAlias) { | 2276 static SkScan::HairRCProc ChooseHairProc(bool doAntiAlias) { |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2258 mask->fImage = SkMask::AllocImage(size); | 2660 mask->fImage = SkMask::AllocImage(size); |
2259 memset(mask->fImage, 0, mask->computeImageSize()); | 2661 memset(mask->fImage, 0, mask->computeImageSize()); |
2260 } | 2662 } |
2261 | 2663 |
2262 if (SkMask::kJustComputeBounds_CreateMode != mode) { | 2664 if (SkMask::kJustComputeBounds_CreateMode != mode) { |
2263 draw_into_mask(*mask, devPath, style); | 2665 draw_into_mask(*mask, devPath, style); |
2264 } | 2666 } |
2265 | 2667 |
2266 return true; | 2668 return true; |
2267 } | 2669 } |
OLD | NEW |