bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 7 | #include "src/utils/win/SkDWriteNTDDI_VERSION.h" |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 8 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkTypes.h" |
Mike Klein | 8f11d4d | 2018-01-24 12:42:55 -0500 | [diff] [blame] | 10 | #if defined(SK_BUILD_FOR_WIN) |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 11 | |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 12 | #undef GetGlyphIndices |
| 13 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/codec/SkCodec.h" |
| 15 | #include "include/core/SkFontMetrics.h" |
| 16 | #include "include/core/SkPath.h" |
| 17 | #include "include/private/SkMutex.h" |
| 18 | #include "include/private/SkTo.h" |
| 19 | #include "src/core/SkDraw.h" |
| 20 | #include "src/core/SkEndian.h" |
| 21 | #include "src/core/SkGlyph.h" |
| 22 | #include "src/core/SkMaskGamma.h" |
| 23 | #include "src/core/SkRasterClip.h" |
| 24 | #include "src/core/SkScalerContext.h" |
| 25 | #include "src/core/SkSharedMutex.h" |
| 26 | #include "src/ports/SkScalerContext_win_dw.h" |
| 27 | #include "src/ports/SkTypeface_win_dw.h" |
| 28 | #include "src/sfnt/SkOTTable_EBLC.h" |
| 29 | #include "src/sfnt/SkOTTable_EBSC.h" |
| 30 | #include "src/sfnt/SkOTTable_gasp.h" |
| 31 | #include "src/sfnt/SkOTTable_maxp.h" |
| 32 | #include "src/utils/SkMatrix22.h" |
| 33 | #include "src/utils/win/SkDWrite.h" |
| 34 | #include "src/utils/win/SkDWriteGeometrySink.h" |
| 35 | #include "src/utils/win/SkHRESULT.h" |
| 36 | #include "src/utils/win/SkTScopedComPtr.h" |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 37 | |
| 38 | #include <dwrite.h> |
Ben Wagner | e0c9e00 | 2017-03-01 16:08:00 -0500 | [diff] [blame] | 39 | #include <dwrite_1.h> |
Ben Wagner | 297c3c8 | 2018-07-19 08:55:10 -0400 | [diff] [blame] | 40 | #include <dwrite_3.h> |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 41 | |
herb | c7378af | 2015-10-21 09:24:37 -0700 | [diff] [blame] | 42 | /* Note: |
| 43 | * In versions 8 and 8.1 of Windows, some calls in DWrite are not thread safe. |
| 44 | * The DWriteFactoryMutex protects the calls that are problematic. |
| 45 | */ |
herb | 6440f0b | 2015-10-27 14:37:00 -0700 | [diff] [blame] | 46 | static SkSharedMutex DWriteFactoryMutex; |
herb | c7378af | 2015-10-21 09:24:37 -0700 | [diff] [blame] | 47 | |
Herb Derby | 209ebc0 | 2019-05-13 13:38:09 -0400 | [diff] [blame] | 48 | typedef SkAutoSharedMutexExclusive Exclusive; |
herb | 6440f0b | 2015-10-27 14:37:00 -0700 | [diff] [blame] | 49 | typedef SkAutoSharedMutexShared Shared; |
herb | c7378af | 2015-10-21 09:24:37 -0700 | [diff] [blame] | 50 | |
Ben Wagner | 3e45a2f | 2017-11-01 11:47:39 -0400 | [diff] [blame] | 51 | static bool isLCD(const SkScalerContextRec& rec) { |
reed | d54d3fc | 2014-11-13 14:39:58 -0800 | [diff] [blame] | 52 | return SkMask::kLCD16_Format == rec.fMaskFormat; |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Ben Wagner | 9f59134 | 2017-04-13 11:02:50 -0400 | [diff] [blame] | 55 | static bool is_hinted(DWriteFontTypeface* typeface) { |
Herb Derby | 209ebc0 | 2019-05-13 13:38:09 -0400 | [diff] [blame] | 56 | Exclusive l(DWriteFactoryMutex); |
bungeman | 761b250 | 2014-06-30 12:19:41 -0700 | [diff] [blame] | 57 | AutoTDWriteTable<SkOTTableMaximumProfile> maxp(typeface->fDWriteFontFace.get()); |
| 58 | if (!maxp.fExists) { |
| 59 | return false; |
| 60 | } |
| 61 | if (maxp.fSize < sizeof(SkOTTableMaximumProfile::Version::TT)) { |
| 62 | return false; |
| 63 | } |
| 64 | if (maxp->version.version != SkOTTableMaximumProfile::Version::TT::VERSION) { |
| 65 | return false; |
| 66 | } |
Ben Wagner | 9f59134 | 2017-04-13 11:02:50 -0400 | [diff] [blame] | 67 | return (0 != maxp->version.tt.maxSizeOfInstructions); |
bungeman | 761b250 | 2014-06-30 12:19:41 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 70 | /** A GaspRange is inclusive, [min, max]. */ |
| 71 | struct GaspRange { |
| 72 | using Behavior = SkOTTableGridAndScanProcedure::GaspRange::behavior; |
Ben Wagner | 42a2a20 | 2017-04-24 12:40:03 -0400 | [diff] [blame] | 73 | GaspRange(int min, int max, int version, Behavior flags) |
| 74 | : fMin(min), fMax(max), fVersion(version), fFlags(flags) { } |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 75 | int fMin; |
| 76 | int fMax; |
Ben Wagner | 42a2a20 | 2017-04-24 12:40:03 -0400 | [diff] [blame] | 77 | int fVersion; |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 78 | Behavior fFlags; |
bungeman | 740c3f1 | 2014-06-23 08:29:23 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 81 | bool get_gasp_range(DWriteFontTypeface* typeface, int size, GaspRange* range) { |
Mike Reed | b66fa52 | 2017-03-15 23:51:22 +0000 | [diff] [blame] | 82 | AutoTDWriteTable<SkOTTableGridAndScanProcedure> gasp(typeface->fDWriteFontFace.get()); |
| 83 | if (!gasp.fExists) { |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 84 | return false; |
Mike Reed | b66fa52 | 2017-03-15 23:51:22 +0000 | [diff] [blame] | 85 | } |
| 86 | if (gasp.fSize < sizeof(SkOTTableGridAndScanProcedure)) { |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 87 | return false; |
Mike Reed | b66fa52 | 2017-03-15 23:51:22 +0000 | [diff] [blame] | 88 | } |
| 89 | if (gasp->version != SkOTTableGridAndScanProcedure::version0 && |
| 90 | gasp->version != SkOTTableGridAndScanProcedure::version1) |
| 91 | { |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 92 | return false; |
Mike Reed | b66fa52 | 2017-03-15 23:51:22 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | uint16_t numRanges = SkEndianSwap16(gasp->numRanges); |
| 96 | if (numRanges > 1024 || |
| 97 | gasp.fSize < sizeof(SkOTTableGridAndScanProcedure) + |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 98 | sizeof(SkOTTableGridAndScanProcedure::GaspRange) * numRanges) |
Mike Reed | b66fa52 | 2017-03-15 23:51:22 +0000 | [diff] [blame] | 99 | { |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 100 | return false; |
Mike Reed | b66fa52 | 2017-03-15 23:51:22 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | const SkOTTableGridAndScanProcedure::GaspRange* rangeTable = |
| 104 | SkTAfter<const SkOTTableGridAndScanProcedure::GaspRange>(gasp.get()); |
| 105 | int minPPEM = -1; |
| 106 | for (uint16_t i = 0; i < numRanges; ++i, ++rangeTable) { |
| 107 | int maxPPEM = SkEndianSwap16(rangeTable->maxPPEM); |
Ben Wagner | 34db1ee | 2017-03-15 15:22:29 -0400 | [diff] [blame] | 108 | if (minPPEM < size && size <= maxPPEM) { |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 109 | range->fMin = minPPEM + 1; |
| 110 | range->fMax = maxPPEM; |
Ben Wagner | 42a2a20 | 2017-04-24 12:40:03 -0400 | [diff] [blame] | 111 | range->fVersion = SkEndian_SwapBE16(gasp->version); |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 112 | range->fFlags = rangeTable->flags; |
| 113 | return true; |
Ben Wagner | a67615f | 2017-02-09 13:41:45 -0500 | [diff] [blame] | 114 | } |
| 115 | minPPEM = maxPPEM; |
| 116 | } |
| 117 | return false; |
| 118 | } |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 119 | /** If the rendering mode for the specified 'size' is gridfit, then place |
| 120 | * the gridfit range into 'range'. Otherwise, leave 'range' alone. |
| 121 | */ |
| 122 | static bool is_gridfit_only(GaspRange::Behavior flags) { |
| 123 | return flags.raw.value == GaspRange::Behavior::Raw::GridfitMask; |
| 124 | } |
Ben Wagner | a67615f | 2017-02-09 13:41:45 -0500 | [diff] [blame] | 125 | |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 126 | static bool has_bitmap_strike(DWriteFontTypeface* typeface, GaspRange range) { |
Herb Derby | 209ebc0 | 2019-05-13 13:38:09 -0400 | [diff] [blame] | 127 | Exclusive l(DWriteFactoryMutex); |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 128 | { |
| 129 | AutoTDWriteTable<SkOTTableEmbeddedBitmapLocation> eblc(typeface->fDWriteFontFace.get()); |
| 130 | if (!eblc.fExists) { |
| 131 | return false; |
| 132 | } |
| 133 | if (eblc.fSize < sizeof(SkOTTableEmbeddedBitmapLocation)) { |
| 134 | return false; |
| 135 | } |
| 136 | if (eblc->version != SkOTTableEmbeddedBitmapLocation::version_initial) { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | uint32_t numSizes = SkEndianSwap32(eblc->numSizes); |
bungeman | 740c3f1 | 2014-06-23 08:29:23 -0700 | [diff] [blame] | 141 | if (numSizes > 1024 || |
| 142 | eblc.fSize < sizeof(SkOTTableEmbeddedBitmapLocation) + |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 143 | sizeof(SkOTTableEmbeddedBitmapLocation::BitmapSizeTable) * numSizes) |
| 144 | { |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | const SkOTTableEmbeddedBitmapLocation::BitmapSizeTable* sizeTable = |
| 149 | SkTAfter<const SkOTTableEmbeddedBitmapLocation::BitmapSizeTable>(eblc.get()); |
| 150 | for (uint32_t i = 0; i < numSizes; ++i, ++sizeTable) { |
bungeman | 740c3f1 | 2014-06-23 08:29:23 -0700 | [diff] [blame] | 151 | if (sizeTable->ppemX == sizeTable->ppemY && |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 152 | range.fMin <= sizeTable->ppemX && sizeTable->ppemX <= range.fMax) |
bungeman | 740c3f1 | 2014-06-23 08:29:23 -0700 | [diff] [blame] | 153 | { |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 154 | // TODO: determine if we should dig through IndexSubTableArray/IndexSubTable |
| 155 | // to determine the actual number of glyphs with bitmaps. |
| 156 | |
| 157 | // TODO: Ensure that the bitmaps actually cover a significant portion of the strike. |
| 158 | |
bungeman | 740c3f1 | 2014-06-23 08:29:23 -0700 | [diff] [blame] | 159 | // TODO: Ensure that the bitmaps are bi-level? |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 160 | if (sizeTable->endGlyphIndex >= sizeTable->startGlyphIndex + 3) { |
| 161 | return true; |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | { |
| 168 | AutoTDWriteTable<SkOTTableEmbeddedBitmapScaling> ebsc(typeface->fDWriteFontFace.get()); |
| 169 | if (!ebsc.fExists) { |
| 170 | return false; |
| 171 | } |
| 172 | if (ebsc.fSize < sizeof(SkOTTableEmbeddedBitmapScaling)) { |
| 173 | return false; |
| 174 | } |
| 175 | if (ebsc->version != SkOTTableEmbeddedBitmapScaling::version_initial) { |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | uint32_t numSizes = SkEndianSwap32(ebsc->numSizes); |
bungeman | 740c3f1 | 2014-06-23 08:29:23 -0700 | [diff] [blame] | 180 | if (numSizes > 1024 || |
| 181 | ebsc.fSize < sizeof(SkOTTableEmbeddedBitmapScaling) + |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 182 | sizeof(SkOTTableEmbeddedBitmapScaling::BitmapScaleTable) * numSizes) |
| 183 | { |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | const SkOTTableEmbeddedBitmapScaling::BitmapScaleTable* scaleTable = |
| 188 | SkTAfter<const SkOTTableEmbeddedBitmapScaling::BitmapScaleTable>(ebsc.get()); |
| 189 | for (uint32_t i = 0; i < numSizes; ++i, ++scaleTable) { |
bungeman | 740c3f1 | 2014-06-23 08:29:23 -0700 | [diff] [blame] | 190 | if (scaleTable->ppemX == scaleTable->ppemY && |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 191 | range.fMin <= scaleTable->ppemX && scaleTable->ppemX <= range.fMax) { |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 192 | // EBSC tables are normally only found in bitmap only fonts. |
| 193 | return true; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | return false; |
| 199 | } |
| 200 | |
bungeman | 740c3f1 | 2014-06-23 08:29:23 -0700 | [diff] [blame] | 201 | static bool both_zero(SkScalar a, SkScalar b) { |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 202 | return 0 == a && 0 == b; |
| 203 | } |
| 204 | |
| 205 | // returns false if there is any non-90-rotation or skew |
Ben Wagner | 3e45a2f | 2017-11-01 11:47:39 -0400 | [diff] [blame] | 206 | static bool is_axis_aligned(const SkScalerContextRec& rec) { |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 207 | return 0 == rec.fPreSkewX && |
bungeman | 740c3f1 | 2014-06-23 08:29:23 -0700 | [diff] [blame] | 208 | (both_zero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) || |
| 209 | both_zero(rec.fPost2x2[0][0], rec.fPost2x2[1][1])); |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 210 | } |
| 211 | |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 212 | SkScalerContext_DW::SkScalerContext_DW(sk_sp<DWriteFontTypeface> typefaceRef, |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 213 | const SkScalerContextEffects& effects, |
bungeman@google.com | 6eddc77 | 2014-03-31 19:18:07 +0000 | [diff] [blame] | 214 | const SkDescriptor* desc) |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 215 | : SkScalerContext(std::move(typefaceRef), effects, desc) |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 216 | , fGlyphCount(-1) { |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 217 | |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 218 | DWriteFontTypeface* typeface = this->getDWriteTypeface(); |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 219 | fIsColorFont = typeface->fFactory2 && |
| 220 | typeface->fDWriteFontFace2 && |
| 221 | typeface->fDWriteFontFace2->IsColorFont(); |
bungeman | 73c7c3c | 2016-06-16 14:41:53 -0700 | [diff] [blame] | 222 | |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 223 | // In general, all glyphs should use NATURAL_SYMMETRIC |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 224 | // except when bi-level rendering is requested or there are embedded |
| 225 | // bi-level bitmaps (and the embedded bitmap flag is set and no rotation). |
| 226 | // |
| 227 | // DirectWrite's IDWriteFontFace::GetRecommendedRenderingMode does not do |
| 228 | // this. As a result, determine the actual size of the text and then see if |
| 229 | // there are any embedded bi-level bitmaps of that size. If there are, then |
| 230 | // force bitmaps by requesting bi-level rendering. |
| 231 | // |
| 232 | // FreeType allows for separate ppemX and ppemY, but DirectWrite assumes |
| 233 | // square pixels and only uses ppemY. Therefore the transform must track any |
| 234 | // non-uniform x-scale. |
| 235 | // |
| 236 | // Also, rotated glyphs should have the same absolute advance widths as |
| 237 | // horizontal glyphs and the subpixel flag should not affect glyph shapes. |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 238 | |
bungeman | 5f14c5e | 2014-12-05 12:26:44 -0800 | [diff] [blame] | 239 | SkVector scale; |
Ben Wagner | 1f5f6b8 | 2017-04-14 11:39:34 -0400 | [diff] [blame] | 240 | fRec.computeMatrices(SkScalerContextRec::kVertical_PreMatrixScale, &scale, &fSkXform); |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 241 | |
bungeman | 5f14c5e | 2014-12-05 12:26:44 -0800 | [diff] [blame] | 242 | fXform.m11 = SkScalarToFloat(fSkXform.getScaleX()); |
| 243 | fXform.m12 = SkScalarToFloat(fSkXform.getSkewY()); |
| 244 | fXform.m21 = SkScalarToFloat(fSkXform.getSkewX()); |
| 245 | fXform.m22 = SkScalarToFloat(fSkXform.getScaleY()); |
| 246 | fXform.dx = 0; |
| 247 | fXform.dy = 0; |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 248 | |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 249 | // realTextSize is the actual device size we want (as opposed to the size the user requested). |
| 250 | // gdiTextSize is the size we request when GDI compatible. |
| 251 | // If the scale is negative, this means the matrix will do the flip anyway. |
bungeman | 5f14c5e | 2014-12-05 12:26:44 -0800 | [diff] [blame] | 252 | const SkScalar realTextSize = scale.fY; |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 253 | // Due to floating point math, the lower bits are suspect. Round carefully. |
bungeman@google.com | 7c18351 | 2014-05-28 15:40:26 +0000 | [diff] [blame] | 254 | SkScalar gdiTextSize = SkScalarRoundToScalar(realTextSize * 64.0f) / 64.0f; |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 255 | if (gdiTextSize == 0) { |
| 256 | gdiTextSize = SK_Scalar1; |
| 257 | } |
| 258 | |
bungeman@google.com | 7c18351 | 2014-05-28 15:40:26 +0000 | [diff] [blame] | 259 | bool bitmapRequested = SkToBool(fRec.fFlags & SkScalerContext::kEmbeddedBitmapText_Flag); |
bungeman | 740c3f1 | 2014-06-23 08:29:23 -0700 | [diff] [blame] | 260 | bool treatLikeBitmap = false; |
bungeman@google.com | 7c18351 | 2014-05-28 15:40:26 +0000 | [diff] [blame] | 261 | bool axisAlignedBitmap = false; |
| 262 | if (bitmapRequested) { |
bungeman | 740c3f1 | 2014-06-23 08:29:23 -0700 | [diff] [blame] | 263 | // When embedded bitmaps are requested, treat the entire range like |
| 264 | // a bitmap strike if the range is gridfit only and contains a bitmap. |
| 265 | int bitmapPPEM = SkScalarTruncToInt(gdiTextSize); |
Ben Wagner | 42a2a20 | 2017-04-24 12:40:03 -0400 | [diff] [blame] | 266 | GaspRange range(bitmapPPEM, bitmapPPEM, 0, GaspRange::Behavior()); |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 267 | if (get_gasp_range(typeface, bitmapPPEM, &range)) { |
| 268 | if (!is_gridfit_only(range.fFlags)) { |
Ben Wagner | 42a2a20 | 2017-04-24 12:40:03 -0400 | [diff] [blame] | 269 | range = GaspRange(bitmapPPEM, bitmapPPEM, 0, GaspRange::Behavior()); |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 270 | } |
| 271 | } |
bungeman | 740c3f1 | 2014-06-23 08:29:23 -0700 | [diff] [blame] | 272 | treatLikeBitmap = has_bitmap_strike(typeface, range); |
| 273 | |
| 274 | axisAlignedBitmap = is_axis_aligned(fRec); |
bungeman@google.com | 7c18351 | 2014-05-28 15:40:26 +0000 | [diff] [blame] | 275 | } |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 276 | |
Ben Wagner | 42a2a20 | 2017-04-24 12:40:03 -0400 | [diff] [blame] | 277 | GaspRange range(0, 0xFFFF, 0, GaspRange::Behavior()); |
Ben Wagner | 9f59134 | 2017-04-13 11:02:50 -0400 | [diff] [blame] | 278 | |
bungeman@google.com | 7c18351 | 2014-05-28 15:40:26 +0000 | [diff] [blame] | 279 | // If the user requested aliased, do so with aliased compatible metrics. |
| 280 | if (SkMask::kBW_Format == fRec.fMaskFormat) { |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 281 | fTextSizeRender = gdiTextSize; |
bungeman@google.com | d715aaa | 2014-04-09 15:35:03 +0000 | [diff] [blame] | 282 | fRenderingMode = DWRITE_RENDERING_MODE_ALIASED; |
| 283 | fTextureType = DWRITE_TEXTURE_ALIASED_1x1; |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 284 | fTextSizeMeasure = gdiTextSize; |
bungeman@google.com | d715aaa | 2014-04-09 15:35:03 +0000 | [diff] [blame] | 285 | fMeasuringMode = DWRITE_MEASURING_MODE_GDI_CLASSIC; |
bungeman | df1640d | 2014-06-04 15:19:47 -0700 | [diff] [blame] | 286 | |
bungeman@google.com | 7c18351 | 2014-05-28 15:40:26 +0000 | [diff] [blame] | 287 | // If we can use a bitmap, use gdi classic rendering and measurement. |
| 288 | // This will not always provide a bitmap, but matches expected behavior. |
bungeman | 740c3f1 | 2014-06-23 08:29:23 -0700 | [diff] [blame] | 289 | } else if (treatLikeBitmap && axisAlignedBitmap) { |
bungeman@google.com | 7c18351 | 2014-05-28 15:40:26 +0000 | [diff] [blame] | 290 | fTextSizeRender = gdiTextSize; |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 291 | fRenderingMode = DWRITE_RENDERING_MODE_GDI_CLASSIC; |
bungeman@google.com | 7c18351 | 2014-05-28 15:40:26 +0000 | [diff] [blame] | 292 | fTextureType = DWRITE_TEXTURE_CLEARTYPE_3x1; |
| 293 | fTextSizeMeasure = gdiTextSize; |
| 294 | fMeasuringMode = DWRITE_MEASURING_MODE_GDI_CLASSIC; |
bungeman | df1640d | 2014-06-04 15:19:47 -0700 | [diff] [blame] | 295 | |
bungeman@google.com | 7c18351 | 2014-05-28 15:40:26 +0000 | [diff] [blame] | 296 | // If rotated but the horizontal text could have used a bitmap, |
| 297 | // render high quality rotated glyphs but measure using bitmap metrics. |
bungeman | 740c3f1 | 2014-06-23 08:29:23 -0700 | [diff] [blame] | 298 | } else if (treatLikeBitmap) { |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 299 | fTextSizeRender = gdiTextSize; |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 300 | fRenderingMode = DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC; |
bungeman@google.com | d715aaa | 2014-04-09 15:35:03 +0000 | [diff] [blame] | 301 | fTextureType = DWRITE_TEXTURE_CLEARTYPE_3x1; |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 302 | fTextSizeMeasure = gdiTextSize; |
| 303 | fMeasuringMode = DWRITE_MEASURING_MODE_GDI_CLASSIC; |
bungeman@google.com | 7c18351 | 2014-05-28 15:40:26 +0000 | [diff] [blame] | 304 | |
Ben Wagner | 42a2a20 | 2017-04-24 12:40:03 -0400 | [diff] [blame] | 305 | // If the font has a gasp table version 1, use it to determine symmetric rendering. |
| 306 | } else if (get_gasp_range(typeface, SkScalarRoundToInt(gdiTextSize), &range) && |
Ben Wagner | 42a2a20 | 2017-04-24 12:40:03 -0400 | [diff] [blame] | 307 | range.fVersion >= 1) |
Ben Wagner | 42a2a20 | 2017-04-24 12:40:03 -0400 | [diff] [blame] | 308 | { |
Ben Wagner | 9f59134 | 2017-04-13 11:02:50 -0400 | [diff] [blame] | 309 | fTextSizeRender = realTextSize; |
| 310 | fRenderingMode = range.fFlags.field.SymmetricSmoothing |
| 311 | ? DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC |
| 312 | : DWRITE_RENDERING_MODE_NATURAL; |
bungeman | 761b250 | 2014-06-30 12:19:41 -0700 | [diff] [blame] | 313 | fTextureType = DWRITE_TEXTURE_CLEARTYPE_3x1; |
| 314 | fTextSizeMeasure = realTextSize; |
| 315 | fMeasuringMode = DWRITE_MEASURING_MODE_NATURAL; |
| 316 | |
Ben Wagner | 9f59134 | 2017-04-13 11:02:50 -0400 | [diff] [blame] | 317 | // If the requested size is above 20px or there are no bytecode hints, use symmetric rendering. |
| 318 | } else if (realTextSize > SkIntToScalar(20) || !is_hinted(typeface)) { |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 319 | fTextSizeRender = realTextSize; |
Ben Wagner | 9f59134 | 2017-04-13 11:02:50 -0400 | [diff] [blame] | 320 | fRenderingMode = DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC; |
| 321 | fTextureType = DWRITE_TEXTURE_CLEARTYPE_3x1; |
| 322 | fTextSizeMeasure = realTextSize; |
| 323 | fMeasuringMode = DWRITE_MEASURING_MODE_NATURAL; |
| 324 | |
Ben Wagner | 42a2a20 | 2017-04-24 12:40:03 -0400 | [diff] [blame] | 325 | // Fonts with hints, no gasp or gasp version 0, and below 20px get non-symmetric rendering. |
| 326 | // Often such fonts have hints which were only tested with GDI ClearType classic. |
| 327 | // Some of these fonts rely on drop out control in the y direction in order to be legible. |
Ben Wagner | 9f59134 | 2017-04-13 11:02:50 -0400 | [diff] [blame] | 328 | // Tenor Sans |
| 329 | // https://fonts.google.com/specimen/Tenor+Sans |
| 330 | // Gill Sans W04 |
| 331 | // https://cdn.leagueoflegends.com/lolkit/1.1.9/resources/fonts/gill-sans-w04-book.woff |
| 332 | // https://na.leagueoflegends.com/en/news/game-updates/patch/patch-410-notes |
| 333 | // See https://crbug.com/385897 |
| 334 | } else { |
| 335 | fTextSizeRender = gdiTextSize; |
| 336 | fRenderingMode = DWRITE_RENDERING_MODE_NATURAL; |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 337 | fTextureType = DWRITE_TEXTURE_CLEARTYPE_3x1; |
| 338 | fTextSizeMeasure = realTextSize; |
bungeman@google.com | d715aaa | 2014-04-09 15:35:03 +0000 | [diff] [blame] | 339 | fMeasuringMode = DWRITE_MEASURING_MODE_NATURAL; |
| 340 | } |
| 341 | |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 342 | // DirectWrite2 allows for grayscale hinting. |
| 343 | fAntiAliasMode = DWRITE_TEXT_ANTIALIAS_MODE_CLEARTYPE; |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 344 | if (typeface->fFactory2 && typeface->fDWriteFontFace2 && |
Ben Wagner | e0a34e7 | 2017-03-17 11:23:32 -0400 | [diff] [blame] | 345 | SkMask::kA8_Format == fRec.fMaskFormat && |
| 346 | !(fRec.fFlags & SkScalerContext::kGenA8FromLCD_Flag)) |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 347 | { |
| 348 | // DWRITE_TEXTURE_ALIASED_1x1 is now misnamed, it must also be used with grayscale. |
| 349 | fTextureType = DWRITE_TEXTURE_ALIASED_1x1; |
| 350 | fAntiAliasMode = DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE; |
| 351 | } |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 352 | |
| 353 | // DirectWrite2 allows hinting to be disabled. |
| 354 | fGridFitMode = DWRITE_GRID_FIT_MODE_ENABLED; |
Ben Wagner | 5785e4a | 2019-05-07 16:50:29 -0400 | [diff] [blame] | 355 | if (fRec.getHinting() == SkFontHinting::kNone) { |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 356 | fGridFitMode = DWRITE_GRID_FIT_MODE_DISABLED; |
| 357 | if (fRenderingMode != DWRITE_RENDERING_MODE_ALIASED) { |
| 358 | fRenderingMode = DWRITE_RENDERING_MODE_NATURAL_SYMMETRIC; |
| 359 | } |
| 360 | } |
| 361 | |
Ben Wagner | f460eee | 2019-04-18 16:37:45 -0400 | [diff] [blame] | 362 | if (this->isLinearMetrics()) { |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 363 | fTextSizeMeasure = realTextSize; |
bungeman@google.com | d715aaa | 2014-04-09 15:35:03 +0000 | [diff] [blame] | 364 | fMeasuringMode = DWRITE_MEASURING_MODE_NATURAL; |
| 365 | } |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 366 | } |
| 367 | |
reed@google.com | 30ddd61 | 2013-07-30 17:47:39 +0000 | [diff] [blame] | 368 | SkScalerContext_DW::~SkScalerContext_DW() { |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 369 | } |
| 370 | |
reed@google.com | 30ddd61 | 2013-07-30 17:47:39 +0000 | [diff] [blame] | 371 | unsigned SkScalerContext_DW::generateGlyphCount() { |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 372 | if (fGlyphCount < 0) { |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 373 | fGlyphCount = this->getDWriteTypeface()->fDWriteFontFace->GetGlyphCount(); |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 374 | } |
| 375 | return fGlyphCount; |
| 376 | } |
| 377 | |
Ben Wagner | e541645 | 2018-08-09 14:03:42 -0400 | [diff] [blame] | 378 | bool SkScalerContext_DW::generateAdvance(SkGlyph* glyph) { |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 379 | glyph->fAdvanceX = 0; |
| 380 | glyph->fAdvanceY = 0; |
| 381 | |
| 382 | uint16_t glyphId = glyph->getGlyphID(); |
| 383 | DWRITE_GLYPH_METRICS gm; |
bungeman@google.com | d715aaa | 2014-04-09 15:35:03 +0000 | [diff] [blame] | 384 | |
| 385 | if (DWRITE_MEASURING_MODE_GDI_CLASSIC == fMeasuringMode || |
| 386 | DWRITE_MEASURING_MODE_GDI_NATURAL == fMeasuringMode) |
| 387 | { |
Herb Derby | 209ebc0 | 2019-05-13 13:38:09 -0400 | [diff] [blame] | 388 | Exclusive l(DWriteFactoryMutex); |
Ben Wagner | e541645 | 2018-08-09 14:03:42 -0400 | [diff] [blame] | 389 | HRBM(this->getDWriteTypeface()->fDWriteFontFace->GetGdiCompatibleGlyphMetrics( |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 390 | fTextSizeMeasure, |
bungeman@google.com | d715aaa | 2014-04-09 15:35:03 +0000 | [diff] [blame] | 391 | 1.0f, // pixelsPerDip |
Ben Wagner | 1f5f6b8 | 2017-04-14 11:39:34 -0400 | [diff] [blame] | 392 | // This parameter does not act like the lpmat2 parameter to GetGlyphOutlineW. |
| 393 | // If it did then GsA here and G_inv below to mapVectors. |
| 394 | nullptr, |
bungeman@google.com | d715aaa | 2014-04-09 15:35:03 +0000 | [diff] [blame] | 395 | DWRITE_MEASURING_MODE_GDI_NATURAL == fMeasuringMode, |
| 396 | &glyphId, 1, |
| 397 | &gm), |
| 398 | "Could not get gdi compatible glyph metrics."); |
| 399 | } else { |
Herb Derby | 209ebc0 | 2019-05-13 13:38:09 -0400 | [diff] [blame] | 400 | Exclusive l(DWriteFactoryMutex); |
Ben Wagner | e541645 | 2018-08-09 14:03:42 -0400 | [diff] [blame] | 401 | HRBM(this->getDWriteTypeface()->fDWriteFontFace->GetDesignGlyphMetrics(&glyphId, 1, &gm), |
bungeman@google.com | d715aaa | 2014-04-09 15:35:03 +0000 | [diff] [blame] | 402 | "Could not get design metrics."); |
| 403 | } |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 404 | |
| 405 | DWRITE_FONT_METRICS dwfm; |
herb | c7378af | 2015-10-21 09:24:37 -0700 | [diff] [blame] | 406 | { |
herb | 6440f0b | 2015-10-27 14:37:00 -0700 | [diff] [blame] | 407 | Shared l(DWriteFactoryMutex); |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 408 | this->getDWriteTypeface()->fDWriteFontFace->GetMetrics(&dwfm); |
herb | c7378af | 2015-10-21 09:24:37 -0700 | [diff] [blame] | 409 | } |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 410 | SkScalar advanceX = fTextSizeMeasure * gm.advanceWidth / dwfm.designUnitsPerEm; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 411 | |
Ben Wagner | 1f5f6b8 | 2017-04-14 11:39:34 -0400 | [diff] [blame] | 412 | SkVector advance = { advanceX, 0 }; |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 413 | if (DWRITE_MEASURING_MODE_GDI_CLASSIC == fMeasuringMode || |
| 414 | DWRITE_MEASURING_MODE_GDI_NATURAL == fMeasuringMode) |
| 415 | { |
bungeman | 761b250 | 2014-06-30 12:19:41 -0700 | [diff] [blame] | 416 | // DirectWrite produced 'compatible' metrics, but while close, |
| 417 | // the end result is not always an integer as it would be with GDI. |
Ben Wagner | 1f5f6b8 | 2017-04-14 11:39:34 -0400 | [diff] [blame] | 418 | advance.fX = SkScalarRoundToScalar(advance.fX); |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 419 | } |
Ben Wagner | 1f5f6b8 | 2017-04-14 11:39:34 -0400 | [diff] [blame] | 420 | fSkXform.mapVectors(&advance, 1); |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 421 | |
Ben Wagner | 1f5f6b8 | 2017-04-14 11:39:34 -0400 | [diff] [blame] | 422 | glyph->fAdvanceX = SkScalarToFloat(advance.fX); |
| 423 | glyph->fAdvanceY = SkScalarToFloat(advance.fY); |
Ben Wagner | e541645 | 2018-08-09 14:03:42 -0400 | [diff] [blame] | 424 | return true; |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 425 | } |
| 426 | |
bungeman | 683a376 | 2014-08-28 11:42:29 -0700 | [diff] [blame] | 427 | HRESULT SkScalerContext_DW::getBoundingBox(SkGlyph* glyph, |
| 428 | DWRITE_RENDERING_MODE renderingMode, |
| 429 | DWRITE_TEXTURE_TYPE textureType, |
| 430 | RECT* bbox) |
Ben Wagner | b2f7fce | 2014-08-27 19:17:41 -0400 | [diff] [blame] | 431 | { |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 432 | //Measure raster size. |
| 433 | fXform.dx = SkFixedToFloat(glyph->getSubXFixed()); |
| 434 | fXform.dy = SkFixedToFloat(glyph->getSubYFixed()); |
| 435 | |
| 436 | FLOAT advance = 0; |
| 437 | |
| 438 | UINT16 glyphId = glyph->getGlyphID(); |
| 439 | |
| 440 | DWRITE_GLYPH_OFFSET offset; |
| 441 | offset.advanceOffset = 0.0f; |
| 442 | offset.ascenderOffset = 0.0f; |
| 443 | |
| 444 | DWRITE_GLYPH_RUN run; |
| 445 | run.glyphCount = 1; |
| 446 | run.glyphAdvances = &advance; |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 447 | run.fontFace = this->getDWriteTypeface()->fDWriteFontFace.get(); |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 448 | run.fontEmSize = SkScalarToFloat(fTextSizeRender); |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 449 | run.bidiLevel = 0; |
| 450 | run.glyphIndices = &glyphId; |
| 451 | run.isSideways = FALSE; |
| 452 | run.glyphOffsets = &offset; |
herb | 6440f0b | 2015-10-27 14:37:00 -0700 | [diff] [blame] | 453 | |
| 454 | SkTScopedComPtr<IDWriteGlyphRunAnalysis> glyphRunAnalysis; |
herb | c7378af | 2015-10-21 09:24:37 -0700 | [diff] [blame] | 455 | { |
Herb Derby | 209ebc0 | 2019-05-13 13:38:09 -0400 | [diff] [blame] | 456 | Exclusive l(DWriteFactoryMutex); |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 457 | // IDWriteFactory2::CreateGlyphRunAnalysis is very bad at aliased glyphs. |
| 458 | if (this->getDWriteTypeface()->fFactory2 && |
| 459 | (fGridFitMode == DWRITE_GRID_FIT_MODE_DISABLED || |
| 460 | fAntiAliasMode == DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE)) |
| 461 | { |
| 462 | HRM(this->getDWriteTypeface()->fFactory2->CreateGlyphRunAnalysis( |
| 463 | &run, |
| 464 | &fXform, |
| 465 | renderingMode, |
| 466 | fMeasuringMode, |
| 467 | fGridFitMode, |
| 468 | fAntiAliasMode, |
| 469 | 0.0f, // baselineOriginX, |
| 470 | 0.0f, // baselineOriginY, |
| 471 | &glyphRunAnalysis), |
| 472 | "Could not create DW2 glyph run analysis."); |
| 473 | } else { |
| 474 | HRM(this->getDWriteTypeface()->fFactory->CreateGlyphRunAnalysis(&run, |
| 475 | 1.0f, // pixelsPerDip, |
| 476 | &fXform, |
| 477 | renderingMode, |
| 478 | fMeasuringMode, |
| 479 | 0.0f, // baselineOriginX, |
| 480 | 0.0f, // baselineOriginY, |
| 481 | &glyphRunAnalysis), |
| 482 | "Could not create glyph run analysis."); |
| 483 | } |
herb | 6440f0b | 2015-10-27 14:37:00 -0700 | [diff] [blame] | 484 | } |
| 485 | { |
| 486 | Shared l(DWriteFactoryMutex); |
herb | c7378af | 2015-10-21 09:24:37 -0700 | [diff] [blame] | 487 | HRM(glyphRunAnalysis->GetAlphaTextureBounds(textureType, bbox), |
| 488 | "Could not get texture bounds."); |
| 489 | } |
bungeman | 683a376 | 2014-08-28 11:42:29 -0700 | [diff] [blame] | 490 | return S_OK; |
Ben Wagner | b2f7fce | 2014-08-27 19:17:41 -0400 | [diff] [blame] | 491 | } |
| 492 | |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 493 | bool SkScalerContext_DW::isColorGlyph(const SkGlyph& glyph) { |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 494 | SkTScopedComPtr<IDWriteColorGlyphRunEnumerator> colorLayer; |
Ben Wagner | e0c9e00 | 2017-03-01 16:08:00 -0500 | [diff] [blame] | 495 | return getColorGlyphRun(glyph, &colorLayer); |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Bruce Wang | e398675 | 2018-07-15 17:11:25 -0400 | [diff] [blame] | 498 | bool SkScalerContext_DW::isPngGlyph(const SkGlyph& glyph) { |
| 499 | if (!this->getDWriteTypeface()->fDWriteFontFace4) { |
| 500 | return false; |
| 501 | } |
| 502 | |
| 503 | DWRITE_GLYPH_IMAGE_FORMATS f; |
| 504 | IDWriteFontFace4* fontFace4 = this->getDWriteTypeface()->fDWriteFontFace4.get(); |
| 505 | HRBM(fontFace4->GetGlyphImageFormats(glyph.getGlyphID(), 0, UINT32_MAX, &f), |
| 506 | "Cannot get glyph image formats."); |
| 507 | return f & DWRITE_GLYPH_IMAGE_FORMATS_PNG; |
| 508 | } |
| 509 | |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 510 | bool SkScalerContext_DW::getColorGlyphRun(const SkGlyph& glyph, |
| 511 | IDWriteColorGlyphRunEnumerator** colorGlyph) |
| 512 | { |
| 513 | FLOAT advance = 0; |
| 514 | UINT16 glyphId = glyph.getGlyphID(); |
| 515 | |
| 516 | DWRITE_GLYPH_OFFSET offset; |
| 517 | offset.advanceOffset = 0.0f; |
| 518 | offset.ascenderOffset = 0.0f; |
| 519 | |
| 520 | DWRITE_GLYPH_RUN run; |
| 521 | run.glyphCount = 1; |
| 522 | run.glyphAdvances = &advance; |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 523 | run.fontFace = this->getDWriteTypeface()->fDWriteFontFace.get(); |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 524 | run.fontEmSize = SkScalarToFloat(fTextSizeRender); |
| 525 | run.bidiLevel = 0; |
| 526 | run.glyphIndices = &glyphId; |
| 527 | run.isSideways = FALSE; |
| 528 | run.glyphOffsets = &offset; |
| 529 | |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 530 | HRESULT hr = this->getDWriteTypeface()->fFactory2->TranslateColorGlyphRun( |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 531 | 0, 0, &run, nullptr, fMeasuringMode, &fXform, 0, colorGlyph); |
| 532 | if (hr == DWRITE_E_NOCOLOR) { |
| 533 | return false; |
| 534 | } |
| 535 | HRBM(hr, "Failed to translate color glyph run"); |
| 536 | return true; |
| 537 | } |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 538 | |
Bruce Wang | 46f2563 | 2018-06-13 13:38:34 -0400 | [diff] [blame] | 539 | void SkScalerContext_DW::generateColorMetrics(SkGlyph* glyph) { |
| 540 | SkTScopedComPtr<IDWriteColorGlyphRunEnumerator> colorLayers; |
| 541 | HRVM(getColorGlyphRun(*glyph, &colorLayers), "Could not get color glyph run"); |
| 542 | SkASSERT(colorLayers.get()); |
| 543 | |
| 544 | SkRect bounds = SkRect::MakeEmpty(); |
| 545 | BOOL hasNextRun = FALSE; |
| 546 | while (SUCCEEDED(colorLayers->MoveNext(&hasNextRun)) && hasNextRun) { |
| 547 | const DWRITE_COLOR_GLYPH_RUN* colorGlyph; |
| 548 | HRVM(colorLayers->GetCurrentRun(&colorGlyph), "Could not get current color glyph run"); |
| 549 | |
| 550 | SkPath path; |
| 551 | SkTScopedComPtr<IDWriteGeometrySink> geometryToPath; |
| 552 | HRVM(SkDWriteGeometrySink::Create(&path, &geometryToPath), |
| 553 | "Could not create geometry to path converter."); |
| 554 | { |
Herb Derby | 209ebc0 | 2019-05-13 13:38:09 -0400 | [diff] [blame] | 555 | Exclusive l(DWriteFactoryMutex); |
Bruce Wang | 46f2563 | 2018-06-13 13:38:34 -0400 | [diff] [blame] | 556 | HRVM(colorGlyph->glyphRun.fontFace->GetGlyphRunOutline( |
| 557 | colorGlyph->glyphRun.fontEmSize, |
| 558 | colorGlyph->glyphRun.glyphIndices, |
| 559 | colorGlyph->glyphRun.glyphAdvances, |
| 560 | colorGlyph->glyphRun.glyphOffsets, |
| 561 | colorGlyph->glyphRun.glyphCount, |
| 562 | colorGlyph->glyphRun.isSideways, |
| 563 | colorGlyph->glyphRun.bidiLevel % 2, //rtl |
| 564 | geometryToPath.get()), |
| 565 | "Could not create glyph outline."); |
| 566 | } |
| 567 | bounds.join(path.getBounds()); |
| 568 | } |
Bruce Wang | 0bf5408 | 2018-07-18 12:36:56 -0400 | [diff] [blame] | 569 | SkMatrix matrix = fSkXform; |
Bruce Wang | 0bf5408 | 2018-07-18 12:36:56 -0400 | [diff] [blame] | 570 | if (this->isSubpixel()) { |
| 571 | matrix.postTranslate(SkFixedToScalar(glyph->getSubXFixed()), |
| 572 | SkFixedToScalar(glyph->getSubYFixed())); |
| 573 | } |
Bruce Wang | 0bf5408 | 2018-07-18 12:36:56 -0400 | [diff] [blame] | 574 | matrix.mapRect(&bounds); |
Bruce Wang | 46f2563 | 2018-06-13 13:38:34 -0400 | [diff] [blame] | 575 | // Round float bound values into integer. |
| 576 | SkIRect ibounds = bounds.roundOut(); |
| 577 | |
| 578 | glyph->fWidth = ibounds.fRight - ibounds.fLeft; |
| 579 | glyph->fHeight = ibounds.fBottom - ibounds.fTop; |
| 580 | glyph->fLeft = ibounds.fLeft; |
| 581 | glyph->fTop = ibounds.fTop; |
| 582 | } |
| 583 | |
Bruce Wang | e398675 | 2018-07-15 17:11:25 -0400 | [diff] [blame] | 584 | namespace { |
| 585 | struct Context { |
| 586 | SkTScopedComPtr<IDWriteFontFace4> fontFace4; |
| 587 | void* glyphDataContext; |
| 588 | Context(IDWriteFontFace4* face4, void* context) |
| 589 | : fontFace4(SkRefComPtr(face4)) |
| 590 | , glyphDataContext(context) |
| 591 | {} |
| 592 | }; |
| 593 | |
| 594 | static void ReleaseProc(const void* ptr, void* context) { |
| 595 | Context* ctx = (Context*)context; |
| 596 | ctx->fontFace4->ReleaseGlyphImageData(ctx->glyphDataContext); |
| 597 | delete ctx; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | void SkScalerContext_DW::generatePngMetrics(SkGlyph* glyph) { |
| 602 | SkASSERT(isPngGlyph(*glyph)); |
| 603 | SkASSERT(glyph->fMaskFormat == SkMask::Format::kARGB32_Format); |
| 604 | SkASSERT(this->getDWriteTypeface()->fDWriteFontFace4); |
| 605 | |
| 606 | IDWriteFontFace4* fontFace4 = this->getDWriteTypeface()->fDWriteFontFace4.get(); |
| 607 | DWRITE_GLYPH_IMAGE_DATA glyphData; |
| 608 | void* glyphDataContext; |
| 609 | HRVM(fontFace4->GetGlyphImageData(glyph->getGlyphID(), |
| 610 | fTextSizeRender, |
| 611 | DWRITE_GLYPH_IMAGE_FORMATS_PNG, |
| 612 | &glyphData, |
| 613 | &glyphDataContext), |
| 614 | "Glyph image data could not be acquired."); |
| 615 | |
| 616 | Context* context = new Context(fontFace4, glyphDataContext); |
| 617 | sk_sp<SkData> data = SkData::MakeWithProc(glyphData.imageData, |
| 618 | glyphData.imageDataSize, |
| 619 | &ReleaseProc, |
| 620 | context); |
| 621 | |
| 622 | std::unique_ptr<SkCodec> codec = SkCodec::MakeFromData(std::move(data)); |
| 623 | if (!codec) { |
| 624 | return; |
| 625 | } |
| 626 | |
| 627 | SkImageInfo info = codec->getInfo(); |
| 628 | SkRect bounds = SkRect::MakeLTRB(SkIntToScalar(info.bounds().fLeft), |
| 629 | SkIntToScalar(info.bounds().fTop), |
| 630 | SkIntToScalar(info.bounds().fRight), |
| 631 | SkIntToScalar(info.bounds().fBottom)); |
| 632 | |
| 633 | SkMatrix matrix = fSkXform; |
| 634 | SkScalar scale = fTextSizeRender / glyphData.pixelsPerEm; |
| 635 | matrix.preScale(scale, scale); |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 636 | matrix.preTranslate(-glyphData.horizontalLeftOrigin.x, -glyphData.horizontalLeftOrigin.y); |
Bruce Wang | e398675 | 2018-07-15 17:11:25 -0400 | [diff] [blame] | 637 | if (this->isSubpixel()) { |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 638 | matrix.postTranslate(SkFixedToScalar(glyph->getSubXFixed()), |
| 639 | SkFixedToScalar(glyph->getSubYFixed())); |
Bruce Wang | e398675 | 2018-07-15 17:11:25 -0400 | [diff] [blame] | 640 | } |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 641 | matrix.mapRect(&bounds); |
Bruce Wang | e398675 | 2018-07-15 17:11:25 -0400 | [diff] [blame] | 642 | bounds.roundOut(); |
| 643 | |
| 644 | glyph->fWidth = bounds.width(); |
| 645 | glyph->fHeight = bounds.height(); |
| 646 | glyph->fLeft = bounds.left(); |
| 647 | glyph->fTop = bounds.top(); |
| 648 | return; |
| 649 | } |
| 650 | |
bungeman | 683a376 | 2014-08-28 11:42:29 -0700 | [diff] [blame] | 651 | void SkScalerContext_DW::generateMetrics(SkGlyph* glyph) { |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 652 | |
| 653 | |
| 654 | // GetAlphaTextureBounds succeeds but sometimes returns empty bounds like |
| 655 | // { 0x80000000, 0x80000000, 0x80000000, 0x80000000 } |
| 656 | // for small, but not quite zero, sized glyphs. |
| 657 | // Only set as non-empty if the returned bounds are non-empty. |
| 658 | auto glyphCheckAndSetBounds = [](SkGlyph* glyph, const RECT& bbox) { |
| 659 | if (bbox.left >= bbox.right || bbox.top >= bbox.bottom) { |
| 660 | return false; |
| 661 | } |
| 662 | |
| 663 | // We're trying to pack left and top into int16_t, |
| 664 | // and width and height into uint16_t, after outsetting by 1. |
| 665 | if (!SkIRect::MakeXYWH(-32767, -32767, 65535, 65535).contains( |
| 666 | SkIRect::MakeLTRB(bbox.left, bbox.top, bbox.right, bbox.bottom))) { |
| 667 | return false; |
| 668 | } |
| 669 | |
| 670 | glyph->fWidth = SkToU16(bbox.right - bbox.left); |
| 671 | glyph->fHeight = SkToU16(bbox.bottom - bbox.top); |
| 672 | glyph->fLeft = SkToS16(bbox.left); |
| 673 | glyph->fTop = SkToS16(bbox.top); |
| 674 | return true; |
| 675 | }; |
| 676 | |
bungeman | 683a376 | 2014-08-28 11:42:29 -0700 | [diff] [blame] | 677 | glyph->fWidth = 0; |
| 678 | glyph->fHeight = 0; |
| 679 | glyph->fLeft = 0; |
| 680 | glyph->fTop = 0; |
Ben Wagner | e541645 | 2018-08-09 14:03:42 -0400 | [diff] [blame] | 681 | glyph->fMaskFormat = fRec.fMaskFormat; |
bungeman | 683a376 | 2014-08-28 11:42:29 -0700 | [diff] [blame] | 682 | |
Ben Wagner | e541645 | 2018-08-09 14:03:42 -0400 | [diff] [blame] | 683 | if (!this->generateAdvance(glyph)) { |
| 684 | return; |
| 685 | } |
bungeman | 683a376 | 2014-08-28 11:42:29 -0700 | [diff] [blame] | 686 | |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 687 | if (fIsColorFont && isColorGlyph(*glyph)) { |
| 688 | glyph->fMaskFormat = SkMask::kARGB32_Format; |
Bruce Wang | 46f2563 | 2018-06-13 13:38:34 -0400 | [diff] [blame] | 689 | generateColorMetrics(glyph); |
| 690 | return; |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 691 | } |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 692 | |
Bruce Wang | e398675 | 2018-07-15 17:11:25 -0400 | [diff] [blame] | 693 | if (fIsColorFont && isPngGlyph(*glyph)) { |
| 694 | glyph->fMaskFormat = SkMask::kARGB32_Format; |
| 695 | generatePngMetrics(glyph); |
| 696 | return; |
| 697 | } |
| 698 | |
bungeman | 683a376 | 2014-08-28 11:42:29 -0700 | [diff] [blame] | 699 | RECT bbox; |
| 700 | HRVM(this->getBoundingBox(glyph, fRenderingMode, fTextureType, &bbox), |
| 701 | "Requested bounding box could not be determined."); |
| 702 | |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 703 | if (glyphCheckAndSetBounds(glyph, bbox)) { |
bungeman | 683a376 | 2014-08-28 11:42:29 -0700 | [diff] [blame] | 704 | return; |
| 705 | } |
| 706 | |
| 707 | // GetAlphaTextureBounds succeeds but returns an empty RECT if there are no |
Ben Wagner | 79ac508 | 2019-02-04 13:13:17 -0500 | [diff] [blame] | 708 | // glyphs of the specified texture type or it is too big for smoothing. |
| 709 | // When this happens, try with the alternate texture type. |
| 710 | if (DWRITE_TEXTURE_ALIASED_1x1 != fTextureType || |
| 711 | DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE == fAntiAliasMode) |
| 712 | { |
bungeman | 683a376 | 2014-08-28 11:42:29 -0700 | [diff] [blame] | 713 | HRVM(this->getBoundingBox(glyph, |
| 714 | DWRITE_RENDERING_MODE_ALIASED, |
| 715 | DWRITE_TEXTURE_ALIASED_1x1, |
| 716 | &bbox), |
| 717 | "Fallback bounding box could not be determined."); |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 718 | if (glyphCheckAndSetBounds(glyph, bbox)) { |
bungeman | 683a376 | 2014-08-28 11:42:29 -0700 | [diff] [blame] | 719 | glyph->fForceBW = 1; |
Ben Wagner | 968664b | 2018-12-04 14:06:37 -0500 | [diff] [blame] | 720 | glyph->fMaskFormat = SkMask::kBW_Format; |
bungeman | 683a376 | 2014-08-28 11:42:29 -0700 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | // TODO: handle the case where a request for DWRITE_TEXTURE_ALIASED_1x1 |
| 724 | // fails, and try DWRITE_TEXTURE_CLEARTYPE_3x1. |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 727 | void SkScalerContext_DW::generateFontMetrics(SkFontMetrics* metrics) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 728 | if (nullptr == metrics) { |
bungeman | f73c237 | 2014-07-23 13:31:06 -0700 | [diff] [blame] | 729 | return; |
bungeman | 4107806 | 2014-07-07 08:16:37 -0700 | [diff] [blame] | 730 | } |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 731 | |
bungeman | 4107806 | 2014-07-07 08:16:37 -0700 | [diff] [blame] | 732 | sk_bzero(metrics, sizeof(*metrics)); |
bungeman@google.com | e1b9bad | 2013-08-27 21:51:37 +0000 | [diff] [blame] | 733 | |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 734 | DWRITE_FONT_METRICS dwfm; |
bungeman@google.com | d715aaa | 2014-04-09 15:35:03 +0000 | [diff] [blame] | 735 | if (DWRITE_MEASURING_MODE_GDI_CLASSIC == fMeasuringMode || |
| 736 | DWRITE_MEASURING_MODE_GDI_NATURAL == fMeasuringMode) |
| 737 | { |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 738 | this->getDWriteTypeface()->fDWriteFontFace->GetGdiCompatibleMetrics( |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 739 | fTextSizeRender, |
bungeman@google.com | d715aaa | 2014-04-09 15:35:03 +0000 | [diff] [blame] | 740 | 1.0f, // pixelsPerDip |
| 741 | &fXform, |
| 742 | &dwfm); |
| 743 | } else { |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 744 | this->getDWriteTypeface()->fDWriteFontFace->GetMetrics(&dwfm); |
bungeman@google.com | d715aaa | 2014-04-09 15:35:03 +0000 | [diff] [blame] | 745 | } |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 746 | |
bungeman@google.com | e1b9bad | 2013-08-27 21:51:37 +0000 | [diff] [blame] | 747 | SkScalar upem = SkIntToScalar(dwfm.designUnitsPerEm); |
commit-bot@chromium.org | 0bc406d | 2014-03-01 20:12:26 +0000 | [diff] [blame] | 748 | |
bungeman | f73c237 | 2014-07-23 13:31:06 -0700 | [diff] [blame] | 749 | metrics->fAscent = -fTextSizeRender * SkIntToScalar(dwfm.ascent) / upem; |
bungeman | 4107806 | 2014-07-07 08:16:37 -0700 | [diff] [blame] | 750 | metrics->fDescent = fTextSizeRender * SkIntToScalar(dwfm.descent) / upem; |
bungeman | 4107806 | 2014-07-07 08:16:37 -0700 | [diff] [blame] | 751 | metrics->fLeading = fTextSizeRender * SkIntToScalar(dwfm.lineGap) / upem; |
| 752 | metrics->fXHeight = fTextSizeRender * SkIntToScalar(dwfm.xHeight) / upem; |
Koji Ishii | a6725fc | 2017-01-22 02:30:51 +0900 | [diff] [blame] | 753 | metrics->fCapHeight = fTextSizeRender * SkIntToScalar(dwfm.capHeight) / upem; |
bungeman | 4107806 | 2014-07-07 08:16:37 -0700 | [diff] [blame] | 754 | metrics->fUnderlineThickness = fTextSizeRender * SkIntToScalar(dwfm.underlineThickness) / upem; |
| 755 | metrics->fUnderlinePosition = -(fTextSizeRender * SkIntToScalar(dwfm.underlinePosition) / upem); |
Ben Wagner | 219f362 | 2017-07-17 15:32:25 -0400 | [diff] [blame] | 756 | metrics->fStrikeoutThickness = fTextSizeRender * SkIntToScalar(dwfm.strikethroughThickness) / upem; |
| 757 | metrics->fStrikeoutPosition = -(fTextSizeRender * SkIntToScalar(dwfm.strikethroughPosition) / upem); |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 758 | |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 759 | metrics->fFlags |= SkFontMetrics::kUnderlineThicknessIsValid_Flag; |
| 760 | metrics->fFlags |= SkFontMetrics::kUnderlinePositionIsValid_Flag; |
| 761 | metrics->fFlags |= SkFontMetrics::kStrikeoutThicknessIsValid_Flag; |
| 762 | metrics->fFlags |= SkFontMetrics::kStrikeoutPositionIsValid_Flag; |
bungeman | f73c237 | 2014-07-23 13:31:06 -0700 | [diff] [blame] | 763 | |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 764 | if (this->getDWriteTypeface()->fDWriteFontFace1.get()) { |
bungeman | f73c237 | 2014-07-23 13:31:06 -0700 | [diff] [blame] | 765 | DWRITE_FONT_METRICS1 dwfm1; |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 766 | this->getDWriteTypeface()->fDWriteFontFace1->GetMetrics(&dwfm1); |
bungeman | f73c237 | 2014-07-23 13:31:06 -0700 | [diff] [blame] | 767 | metrics->fTop = -fTextSizeRender * SkIntToScalar(dwfm1.glyphBoxTop) / upem; |
| 768 | metrics->fBottom = -fTextSizeRender * SkIntToScalar(dwfm1.glyphBoxBottom) / upem; |
| 769 | metrics->fXMin = fTextSizeRender * SkIntToScalar(dwfm1.glyphBoxLeft) / upem; |
| 770 | metrics->fXMax = fTextSizeRender * SkIntToScalar(dwfm1.glyphBoxRight) / upem; |
| 771 | |
| 772 | metrics->fMaxCharWidth = metrics->fXMax - metrics->fXMin; |
bungeman | f548444 | 2014-09-10 07:49:05 -0700 | [diff] [blame] | 773 | return; |
bungeman | f73c237 | 2014-07-23 13:31:06 -0700 | [diff] [blame] | 774 | } |
bungeman | f548444 | 2014-09-10 07:49:05 -0700 | [diff] [blame] | 775 | |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 776 | AutoTDWriteTable<SkOTTableHead> head(this->getDWriteTypeface()->fDWriteFontFace.get()); |
bungeman | f548444 | 2014-09-10 07:49:05 -0700 | [diff] [blame] | 777 | if (head.fExists && |
| 778 | head.fSize >= sizeof(SkOTTableHead) && |
| 779 | head->version == SkOTTableHead::version1) |
| 780 | { |
| 781 | metrics->fTop = -fTextSizeRender * (int16_t)SkEndian_SwapBE16(head->yMax) / upem; |
| 782 | metrics->fBottom = -fTextSizeRender * (int16_t)SkEndian_SwapBE16(head->yMin) / upem; |
| 783 | metrics->fXMin = fTextSizeRender * (int16_t)SkEndian_SwapBE16(head->xMin) / upem; |
| 784 | metrics->fXMax = fTextSizeRender * (int16_t)SkEndian_SwapBE16(head->xMax) / upem; |
| 785 | |
| 786 | metrics->fMaxCharWidth = metrics->fXMax - metrics->fXMin; |
| 787 | return; |
| 788 | } |
| 789 | |
| 790 | metrics->fTop = metrics->fAscent; |
| 791 | metrics->fBottom = metrics->fDescent; |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | /////////////////////////////////////////////////////////////////////////////// |
| 795 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 796 | #include "include/private/SkColorData.h" |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 797 | |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 798 | void SkScalerContext_DW::BilevelToBW(const uint8_t* SK_RESTRICT src, const SkGlyph& glyph) { |
| 799 | const int width = glyph.width(); |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 800 | const size_t dstRB = (width + 7) >> 3; |
| 801 | uint8_t* SK_RESTRICT dst = static_cast<uint8_t*>(glyph.fImage); |
| 802 | |
| 803 | int byteCount = width >> 3; |
| 804 | int bitCount = width & 7; |
| 805 | |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 806 | for (int y = 0; y < glyph.height(); ++y) { |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 807 | if (byteCount > 0) { |
| 808 | for (int i = 0; i < byteCount; ++i) { |
| 809 | unsigned byte = 0; |
| 810 | byte |= src[0] & (1 << 7); |
| 811 | byte |= src[1] & (1 << 6); |
| 812 | byte |= src[2] & (1 << 5); |
| 813 | byte |= src[3] & (1 << 4); |
| 814 | byte |= src[4] & (1 << 3); |
| 815 | byte |= src[5] & (1 << 2); |
| 816 | byte |= src[6] & (1 << 1); |
| 817 | byte |= src[7] & (1 << 0); |
| 818 | dst[i] = byte; |
| 819 | src += 8; |
| 820 | } |
| 821 | } |
| 822 | if (bitCount > 0) { |
| 823 | unsigned byte = 0; |
| 824 | unsigned mask = 0x80; |
| 825 | for (int i = 0; i < bitCount; i++) { |
| 826 | byte |= (src[i]) & mask; |
| 827 | mask >>= 1; |
| 828 | } |
| 829 | dst[byteCount] = byte; |
| 830 | } |
| 831 | src += bitCount; |
| 832 | dst += dstRB; |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | template<bool APPLY_PREBLEND> |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 837 | void SkScalerContext_DW::GrayscaleToA8(const uint8_t* SK_RESTRICT src, |
| 838 | const SkGlyph& glyph, |
| 839 | const uint8_t* table8) { |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 840 | const size_t dstRB = glyph.rowBytes(); |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 841 | const int width = glyph.width(); |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 842 | uint8_t* SK_RESTRICT dst = static_cast<uint8_t*>(glyph.fImage); |
| 843 | |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 844 | for (int y = 0; y < glyph.height(); y++) { |
| 845 | for (int i = 0; i < width; i++) { |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 846 | U8CPU a = *(src++); |
| 847 | dst[i] = sk_apply_lut_if<APPLY_PREBLEND>(a, table8); |
| 848 | } |
| 849 | dst = SkTAddOffset<uint8_t>(dst, dstRB); |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | template<bool APPLY_PREBLEND> |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 854 | void SkScalerContext_DW::RGBToA8(const uint8_t* SK_RESTRICT src, |
| 855 | const SkGlyph& glyph, |
| 856 | const uint8_t* table8) { |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 857 | const size_t dstRB = glyph.rowBytes(); |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 858 | const int width = glyph.width(); |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 859 | uint8_t* SK_RESTRICT dst = static_cast<uint8_t*>(glyph.fImage); |
| 860 | |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 861 | for (int y = 0; y < glyph.height(); y++) { |
| 862 | for (int i = 0; i < width; i++) { |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 863 | U8CPU r = *(src++); |
| 864 | U8CPU g = *(src++); |
| 865 | U8CPU b = *(src++); |
| 866 | dst[i] = sk_apply_lut_if<APPLY_PREBLEND>((r + g + b) / 3, table8); |
| 867 | } |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 868 | dst = SkTAddOffset<uint8_t>(dst, dstRB); |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 869 | } |
| 870 | } |
| 871 | |
bungeman | 12f0312 | 2015-03-19 13:57:36 -0700 | [diff] [blame] | 872 | template<bool APPLY_PREBLEND, bool RGB> |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 873 | void SkScalerContext_DW::RGBToLcd16(const uint8_t* SK_RESTRICT src, const SkGlyph& glyph, |
| 874 | const uint8_t* tableR, const uint8_t* tableG, |
| 875 | const uint8_t* tableB) { |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 876 | const size_t dstRB = glyph.rowBytes(); |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 877 | const int width = glyph.width(); |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 878 | uint16_t* SK_RESTRICT dst = static_cast<uint16_t*>(glyph.fImage); |
| 879 | |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 880 | for (int y = 0; y < glyph.height(); y++) { |
| 881 | for (int i = 0; i < width; i++) { |
bungeman | 12f0312 | 2015-03-19 13:57:36 -0700 | [diff] [blame] | 882 | U8CPU r, g, b; |
| 883 | if (RGB) { |
| 884 | r = sk_apply_lut_if<APPLY_PREBLEND>(*(src++), tableR); |
| 885 | g = sk_apply_lut_if<APPLY_PREBLEND>(*(src++), tableG); |
| 886 | b = sk_apply_lut_if<APPLY_PREBLEND>(*(src++), tableB); |
| 887 | } else { |
| 888 | b = sk_apply_lut_if<APPLY_PREBLEND>(*(src++), tableB); |
| 889 | g = sk_apply_lut_if<APPLY_PREBLEND>(*(src++), tableG); |
| 890 | r = sk_apply_lut_if<APPLY_PREBLEND>(*(src++), tableR); |
| 891 | } |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 892 | dst[i] = SkPack888ToRGB16(r, g, b); |
| 893 | } |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 894 | dst = SkTAddOffset<uint16_t>(dst, dstRB); |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
Ben Wagner | b2f7fce | 2014-08-27 19:17:41 -0400 | [diff] [blame] | 898 | const void* SkScalerContext_DW::drawDWMask(const SkGlyph& glyph, |
| 899 | DWRITE_RENDERING_MODE renderingMode, |
| 900 | DWRITE_TEXTURE_TYPE textureType) |
| 901 | { |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 902 | int sizeNeeded = glyph.width() * glyph.height(); |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 903 | if (DWRITE_TEXTURE_CLEARTYPE_3x1 == textureType) { |
bungeman@google.com | d715aaa | 2014-04-09 15:35:03 +0000 | [diff] [blame] | 904 | sizeNeeded *= 3; |
| 905 | } |
| 906 | if (sizeNeeded > fBits.count()) { |
| 907 | fBits.setCount(sizeNeeded); |
| 908 | } |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 909 | |
bungeman@google.com | d715aaa | 2014-04-09 15:35:03 +0000 | [diff] [blame] | 910 | // erase |
| 911 | memset(fBits.begin(), 0, sizeNeeded); |
| 912 | |
| 913 | fXform.dx = SkFixedToFloat(glyph.getSubXFixed()); |
| 914 | fXform.dy = SkFixedToFloat(glyph.getSubYFixed()); |
| 915 | |
| 916 | FLOAT advance = 0.0f; |
| 917 | |
| 918 | UINT16 index = glyph.getGlyphID(); |
| 919 | |
| 920 | DWRITE_GLYPH_OFFSET offset; |
| 921 | offset.advanceOffset = 0.0f; |
| 922 | offset.ascenderOffset = 0.0f; |
| 923 | |
| 924 | DWRITE_GLYPH_RUN run; |
| 925 | run.glyphCount = 1; |
| 926 | run.glyphAdvances = &advance; |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 927 | run.fontFace = this->getDWriteTypeface()->fDWriteFontFace.get(); |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 928 | run.fontEmSize = SkScalarToFloat(fTextSizeRender); |
bungeman@google.com | d715aaa | 2014-04-09 15:35:03 +0000 | [diff] [blame] | 929 | run.bidiLevel = 0; |
| 930 | run.glyphIndices = &index; |
| 931 | run.isSideways = FALSE; |
| 932 | run.glyphOffsets = &offset; |
herb | c7378af | 2015-10-21 09:24:37 -0700 | [diff] [blame] | 933 | { |
herb | 6440f0b | 2015-10-27 14:37:00 -0700 | [diff] [blame] | 934 | SkTScopedComPtr<IDWriteGlyphRunAnalysis> glyphRunAnalysis; |
| 935 | { |
Herb Derby | 209ebc0 | 2019-05-13 13:38:09 -0400 | [diff] [blame] | 936 | Exclusive l(DWriteFactoryMutex); |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 937 | // IDWriteFactory2::CreateGlyphRunAnalysis is very bad at aliased glyphs. |
| 938 | if (this->getDWriteTypeface()->fFactory2 && |
| 939 | (fGridFitMode == DWRITE_GRID_FIT_MODE_DISABLED || |
| 940 | fAntiAliasMode == DWRITE_TEXT_ANTIALIAS_MODE_GRAYSCALE)) |
| 941 | { |
| 942 | HRNM(this->getDWriteTypeface()->fFactory2->CreateGlyphRunAnalysis(&run, |
| 943 | &fXform, |
| 944 | renderingMode, |
| 945 | fMeasuringMode, |
| 946 | fGridFitMode, |
| 947 | fAntiAliasMode, |
| 948 | 0.0f, // baselineOriginX, |
| 949 | 0.0f, // baselineOriginY, |
| 950 | &glyphRunAnalysis), |
| 951 | "Could not create DW2 glyph run analysis."); |
| 952 | } else { |
| 953 | HRNM(this->getDWriteTypeface()->fFactory->CreateGlyphRunAnalysis(&run, |
| 954 | 1.0f, // pixelsPerDip, |
| 955 | &fXform, |
| 956 | renderingMode, |
| 957 | fMeasuringMode, |
| 958 | 0.0f, // baselineOriginX, |
| 959 | 0.0f, // baselineOriginY, |
| 960 | &glyphRunAnalysis), |
| 961 | "Could not create glyph run analysis."); |
| 962 | } |
herb | 6440f0b | 2015-10-27 14:37:00 -0700 | [diff] [blame] | 963 | } |
herb | c7378af | 2015-10-21 09:24:37 -0700 | [diff] [blame] | 964 | //NOTE: this assumes that the glyph has already been measured |
| 965 | //with an exact same glyph run analysis. |
| 966 | RECT bbox; |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 967 | bbox.left = glyph.left(); |
| 968 | bbox.top = glyph.top(); |
| 969 | bbox.right = glyph.left() + glyph.width(); |
| 970 | bbox.bottom = glyph.top() + glyph.height(); |
herb | 6440f0b | 2015-10-27 14:37:00 -0700 | [diff] [blame] | 971 | { |
| 972 | Shared l(DWriteFactoryMutex); |
| 973 | HRNM(glyphRunAnalysis->CreateAlphaTexture(textureType, |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 974 | &bbox, |
| 975 | fBits.begin(), |
| 976 | sizeNeeded), |
herb | 6440f0b | 2015-10-27 14:37:00 -0700 | [diff] [blame] | 977 | "Could not draw mask."); |
| 978 | } |
herb | c7378af | 2015-10-21 09:24:37 -0700 | [diff] [blame] | 979 | } |
bungeman@google.com | d715aaa | 2014-04-09 15:35:03 +0000 | [diff] [blame] | 980 | return fBits.begin(); |
| 981 | } |
| 982 | |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 983 | void SkScalerContext_DW::generateColorGlyphImage(const SkGlyph& glyph) { |
| 984 | SkASSERT(isColorGlyph(glyph)); |
| 985 | SkASSERT(glyph.fMaskFormat == SkMask::Format::kARGB32_Format); |
| 986 | |
Herb Derby | 9b06f21 | 2019-06-21 14:25:47 -0400 | [diff] [blame] | 987 | memset(glyph.fImage, 0, glyph.imageSize()); |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 988 | |
| 989 | SkTScopedComPtr<IDWriteColorGlyphRunEnumerator> colorLayers; |
| 990 | getColorGlyphRun(glyph, &colorLayers); |
| 991 | SkASSERT(colorLayers.get()); |
| 992 | |
| 993 | SkMatrix matrix = fSkXform; |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 994 | matrix.postTranslate(-SkIntToScalar(glyph.left()), -SkIntToScalar(glyph.top())); |
Bruce Wang | 0bf5408 | 2018-07-18 12:36:56 -0400 | [diff] [blame] | 995 | if (this->isSubpixel()) { |
| 996 | matrix.postTranslate(SkFixedToScalar(glyph.getSubXFixed()), |
| 997 | SkFixedToScalar(glyph.getSubYFixed())); |
| 998 | } |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 999 | SkRasterClip rc(SkIRect::MakeWH(glyph.width(), glyph.height())); |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 1000 | SkDraw draw; |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 1001 | draw.fDst = SkPixmap(SkImageInfo::MakeN32(glyph.width(), glyph.height(), kPremul_SkAlphaType), |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 1002 | glyph.fImage, |
Herb Derby | ba321b6 | 2018-03-12 16:02:29 -0400 | [diff] [blame] | 1003 | glyph.rowBytesUsingFormat(SkMask::Format::kARGB32_Format)); |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 1004 | draw.fMatrix = &matrix; |
| 1005 | draw.fRC = &rc; |
| 1006 | |
| 1007 | SkPaint paint; |
Mike Reed | f738d42 | 2019-01-16 17:12:15 -0500 | [diff] [blame] | 1008 | paint.setAntiAlias(fRenderingMode != DWRITE_RENDERING_MODE_ALIASED); |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 1009 | |
| 1010 | BOOL hasNextRun = FALSE; |
| 1011 | while (SUCCEEDED(colorLayers->MoveNext(&hasNextRun)) && hasNextRun) { |
| 1012 | const DWRITE_COLOR_GLYPH_RUN* colorGlyph; |
| 1013 | HRVM(colorLayers->GetCurrentRun(&colorGlyph), "Could not get current color glyph run"); |
| 1014 | |
| 1015 | SkColor color; |
| 1016 | if (colorGlyph->paletteIndex != 0xffff) { |
Mike Reed | 35ee0e0 | 2017-08-06 22:29:57 -0400 | [diff] [blame] | 1017 | color = SkColorSetARGB(sk_float_round2int(colorGlyph->runColor.a * 255), |
| 1018 | sk_float_round2int(colorGlyph->runColor.r * 255), |
| 1019 | sk_float_round2int(colorGlyph->runColor.g * 255), |
| 1020 | sk_float_round2int(colorGlyph->runColor.b * 255)); |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 1021 | } else { |
| 1022 | // If all components of runColor are 0 or (equivalently) paletteIndex is 0xFFFF then |
| 1023 | // the 'current brush' is used. fRec.getLuminanceColor() is kinda sorta what is wanted |
| 1024 | // here, but not really, it will often be the wrong value because it wan't designed for |
| 1025 | // this. |
bungeman | 5f54434 | 2016-09-22 12:32:07 -0700 | [diff] [blame] | 1026 | // TODO: implement this fully, bug.skia.org/5788 |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 1027 | color = fRec.getLuminanceColor(); |
| 1028 | } |
| 1029 | paint.setColor(color); |
| 1030 | |
| 1031 | SkPath path; |
| 1032 | SkTScopedComPtr<IDWriteGeometrySink> geometryToPath; |
| 1033 | HRVM(SkDWriteGeometrySink::Create(&path, &geometryToPath), |
| 1034 | "Could not create geometry to path converter."); |
| 1035 | { |
Herb Derby | 209ebc0 | 2019-05-13 13:38:09 -0400 | [diff] [blame] | 1036 | Exclusive l(DWriteFactoryMutex); |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 1037 | HRVM(colorGlyph->glyphRun.fontFace->GetGlyphRunOutline( |
| 1038 | colorGlyph->glyphRun.fontEmSize, |
| 1039 | colorGlyph->glyphRun.glyphIndices, |
| 1040 | colorGlyph->glyphRun.glyphAdvances, |
| 1041 | colorGlyph->glyphRun.glyphOffsets, |
| 1042 | colorGlyph->glyphRun.glyphCount, |
| 1043 | colorGlyph->glyphRun.isSideways, |
| 1044 | colorGlyph->glyphRun.bidiLevel % 2, //rtl |
| 1045 | geometryToPath.get()), |
| 1046 | "Could not create glyph outline."); |
| 1047 | } |
| 1048 | draw.drawPath(path, paint, nullptr, true /* pathIsMutable */); |
| 1049 | } |
| 1050 | } |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 1051 | |
Bruce Wang | e398675 | 2018-07-15 17:11:25 -0400 | [diff] [blame] | 1052 | void SkScalerContext_DW::generatePngGlyphImage(const SkGlyph& glyph) { |
| 1053 | SkASSERT(isPngGlyph(glyph)); |
| 1054 | SkASSERT(glyph.fMaskFormat == SkMask::Format::kARGB32_Format); |
| 1055 | SkASSERT(this->getDWriteTypeface()->fDWriteFontFace4); |
| 1056 | |
| 1057 | IDWriteFontFace4* fontFace4 = this->getDWriteTypeface()->fDWriteFontFace4.get(); |
| 1058 | DWRITE_GLYPH_IMAGE_DATA glyphData; |
| 1059 | void* glyphDataContext; |
| 1060 | HRVM(fontFace4->GetGlyphImageData(glyph.getGlyphID(), |
| 1061 | fTextSizeRender, |
| 1062 | DWRITE_GLYPH_IMAGE_FORMATS_PNG, |
| 1063 | &glyphData, |
| 1064 | &glyphDataContext), |
| 1065 | "Glyph image data could not be acquired."); |
| 1066 | Context* context = new Context(fontFace4, glyphDataContext); |
| 1067 | sk_sp<SkData> data = SkData::MakeWithProc(glyphData.imageData, |
| 1068 | glyphData.imageDataSize, |
| 1069 | &ReleaseProc, |
| 1070 | context); |
| 1071 | sk_sp<SkImage> image = SkImage::MakeFromEncoded(std::move(data)); |
| 1072 | |
| 1073 | SkBitmap dstBitmap; |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 1074 | dstBitmap.setInfo(SkImageInfo::Make(glyph.width(), glyph.height(), |
Bruce Wang | e398675 | 2018-07-15 17:11:25 -0400 | [diff] [blame] | 1075 | kN32_SkColorType, |
| 1076 | kPremul_SkAlphaType), |
| 1077 | glyph.rowBytes()); |
| 1078 | dstBitmap.setPixels(glyph.fImage); |
| 1079 | |
| 1080 | SkCanvas canvas(dstBitmap); |
| 1081 | canvas.clear(SK_ColorTRANSPARENT); |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 1082 | canvas.translate(-glyph.left(), -glyph.top()); |
Bruce Wang | e398675 | 2018-07-15 17:11:25 -0400 | [diff] [blame] | 1083 | if (this->isSubpixel()) { |
| 1084 | canvas.translate(SkFixedToScalar(glyph.getSubXFixed()), |
| 1085 | SkFixedToScalar(glyph.getSubYFixed())); |
| 1086 | } |
| 1087 | canvas.concat(fSkXform); |
| 1088 | SkScalar ratio = fTextSizeRender / glyphData.pixelsPerEm; |
| 1089 | canvas.scale(ratio, ratio); |
Bruce Wang | 77bf48a | 2018-07-18 15:32:08 -0400 | [diff] [blame] | 1090 | canvas.translate(-glyphData.horizontalLeftOrigin.x, -glyphData.horizontalLeftOrigin.y); |
Bruce Wang | e398675 | 2018-07-15 17:11:25 -0400 | [diff] [blame] | 1091 | canvas.drawImage(image, 0, 0, nullptr); |
| 1092 | } |
| 1093 | |
bungeman@google.com | d715aaa | 2014-04-09 15:35:03 +0000 | [diff] [blame] | 1094 | void SkScalerContext_DW::generateImage(const SkGlyph& glyph) { |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 1095 | //Create the mask. |
Ben Wagner | b2f7fce | 2014-08-27 19:17:41 -0400 | [diff] [blame] | 1096 | DWRITE_RENDERING_MODE renderingMode = fRenderingMode; |
| 1097 | DWRITE_TEXTURE_TYPE textureType = fTextureType; |
| 1098 | if (glyph.fForceBW) { |
| 1099 | renderingMode = DWRITE_RENDERING_MODE_ALIASED; |
| 1100 | textureType = DWRITE_TEXTURE_ALIASED_1x1; |
| 1101 | } |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 1102 | |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 1103 | if (SkMask::kARGB32_Format == glyph.fMaskFormat) { |
Bruce Wang | e398675 | 2018-07-15 17:11:25 -0400 | [diff] [blame] | 1104 | if (fIsColorFont) { |
| 1105 | if (isColorGlyph(glyph)) { |
| 1106 | generateColorGlyphImage(glyph); |
| 1107 | return; |
| 1108 | } else if (isPngGlyph(glyph)) { |
| 1109 | generatePngGlyphImage(glyph); |
| 1110 | return; |
| 1111 | } |
| 1112 | } |
| 1113 | SkDEBUGFAIL("Could not generate image from the given color font format."); |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 1114 | return; |
| 1115 | } |
kulshin | c4b0915 | 2016-06-01 08:31:28 -0700 | [diff] [blame] | 1116 | |
Ben Wagner | b2f7fce | 2014-08-27 19:17:41 -0400 | [diff] [blame] | 1117 | const void* bits = this->drawDWMask(glyph, renderingMode, textureType); |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 1118 | if (!bits) { |
Herb Derby | 9b06f21 | 2019-06-21 14:25:47 -0400 | [diff] [blame] | 1119 | sk_bzero(glyph.fImage, glyph.imageSize()); |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 1120 | return; |
| 1121 | } |
| 1122 | |
| 1123 | //Copy the mask into the glyph. |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 1124 | const uint8_t* src = (const uint8_t*)bits; |
Ben Wagner | b2f7fce | 2014-08-27 19:17:41 -0400 | [diff] [blame] | 1125 | if (DWRITE_RENDERING_MODE_ALIASED == renderingMode) { |
Ben Wagner | 968664b | 2018-12-04 14:06:37 -0500 | [diff] [blame] | 1126 | SkASSERT(SkMask::kBW_Format == glyph.fMaskFormat); |
| 1127 | SkASSERT(DWRITE_TEXTURE_ALIASED_1x1 == textureType); |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 1128 | BilevelToBW(src, glyph); |
bungeman@google.com | d715aaa | 2014-04-09 15:35:03 +0000 | [diff] [blame] | 1129 | } else if (!isLCD(fRec)) { |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 1130 | if (textureType == DWRITE_TEXTURE_ALIASED_1x1) { |
| 1131 | if (fPreBlend.isApplicable()) { |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 1132 | GrayscaleToA8<true>(src, glyph, fPreBlend.fG); |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 1133 | } else { |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 1134 | GrayscaleToA8<false>(src, glyph, fPreBlend.fG); |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 1135 | } |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 1136 | } else { |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 1137 | if (fPreBlend.isApplicable()) { |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 1138 | RGBToA8<true>(src, glyph, fPreBlend.fG); |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 1139 | } else { |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 1140 | RGBToA8<false>(src, glyph, fPreBlend.fG); |
Ben Wagner | 2225306 | 2017-03-16 12:38:46 -0400 | [diff] [blame] | 1141 | } |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 1142 | } |
reed | d54d3fc | 2014-11-13 14:39:58 -0800 | [diff] [blame] | 1143 | } else { |
| 1144 | SkASSERT(SkMask::kLCD16_Format == glyph.fMaskFormat); |
bungeman@google.com | a76de72 | 2012-10-26 19:35:54 +0000 | [diff] [blame] | 1145 | if (fPreBlend.isApplicable()) { |
bungeman | 12f0312 | 2015-03-19 13:57:36 -0700 | [diff] [blame] | 1146 | if (fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag) { |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 1147 | RGBToLcd16<true, false>(src, glyph, fPreBlend.fR, fPreBlend.fG, fPreBlend.fB); |
bungeman | 12f0312 | 2015-03-19 13:57:36 -0700 | [diff] [blame] | 1148 | } else { |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 1149 | RGBToLcd16<true, true>(src, glyph, fPreBlend.fR, fPreBlend.fG, fPreBlend.fB); |
bungeman | 12f0312 | 2015-03-19 13:57:36 -0700 | [diff] [blame] | 1150 | } |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 1151 | } else { |
bungeman | 12f0312 | 2015-03-19 13:57:36 -0700 | [diff] [blame] | 1152 | if (fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag) { |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 1153 | RGBToLcd16<false, false>(src, glyph, fPreBlend.fR, fPreBlend.fG, fPreBlend.fB); |
bungeman | 12f0312 | 2015-03-19 13:57:36 -0700 | [diff] [blame] | 1154 | } else { |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 1155 | RGBToLcd16<false, true>(src, glyph, fPreBlend.fR, fPreBlend.fG, fPreBlend.fB); |
bungeman | 12f0312 | 2015-03-19 13:57:36 -0700 | [diff] [blame] | 1156 | } |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 1157 | } |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 1158 | } |
| 1159 | } |
| 1160 | |
Ben Wagner | 5ddb308 | 2018-03-29 11:18:06 -0400 | [diff] [blame] | 1161 | bool SkScalerContext_DW::generatePath(SkGlyphID glyph, SkPath* path) { |
dcheng | 2f19b55 | 2015-07-07 19:35:53 -0700 | [diff] [blame] | 1162 | SkASSERT(path); |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 1163 | |
| 1164 | path->reset(); |
| 1165 | |
| 1166 | SkTScopedComPtr<IDWriteGeometrySink> geometryToPath; |
Ben Wagner | 5ddb308 | 2018-03-29 11:18:06 -0400 | [diff] [blame] | 1167 | HRBM(SkDWriteGeometrySink::Create(path, &geometryToPath), |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 1168 | "Could not create geometry to path converter."); |
Ben Wagner | 6e9ac12 | 2016-11-11 14:31:06 -0500 | [diff] [blame] | 1169 | UINT16 glyphId = SkTo<UINT16>(glyph); |
herb | c7378af | 2015-10-21 09:24:37 -0700 | [diff] [blame] | 1170 | { |
Herb Derby | 209ebc0 | 2019-05-13 13:38:09 -0400 | [diff] [blame] | 1171 | Exclusive l(DWriteFactoryMutex); |
herb | c7378af | 2015-10-21 09:24:37 -0700 | [diff] [blame] | 1172 | //TODO: convert to<->from DIUs? This would make a difference if hinting. |
| 1173 | //It may not be needed, it appears that DirectWrite only hints at em size. |
Ben Wagner | 5ddb308 | 2018-03-29 11:18:06 -0400 | [diff] [blame] | 1174 | HRBM(this->getDWriteTypeface()->fDWriteFontFace->GetGlyphRunOutline( |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 1175 | SkScalarToFloat(fTextSizeRender), |
| 1176 | &glyphId, |
| 1177 | nullptr, //advances |
| 1178 | nullptr, //offsets |
| 1179 | 1, //num glyphs |
| 1180 | FALSE, //sideways |
| 1181 | FALSE, //rtl |
| 1182 | geometryToPath.get()), |
| 1183 | "Could not create glyph outline."); |
herb | c7378af | 2015-10-21 09:24:37 -0700 | [diff] [blame] | 1184 | } |
bungeman@google.com | 091f51b | 2013-01-10 18:56:18 +0000 | [diff] [blame] | 1185 | |
bungeman@google.com | 058670b | 2014-05-01 20:39:14 +0000 | [diff] [blame] | 1186 | path->transform(fSkXform); |
Ben Wagner | 5ddb308 | 2018-03-29 11:18:06 -0400 | [diff] [blame] | 1187 | return true; |
bungeman@google.com | e8f0592 | 2012-08-16 16:13:40 +0000 | [diff] [blame] | 1188 | } |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 1189 | |
Mike Klein | 8f11d4d | 2018-01-24 12:42:55 -0500 | [diff] [blame] | 1190 | #endif//defined(SK_BUILD_FOR_WIN) |