| 1 |
/* |
| 2 |
* Copyright 2006 The Android Open Source Project |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "include/core/SkTypes.h" |
| 9 |
#if defined(SK_BUILD_FOR_WIN) |
| 10 |
|
| 11 |
#include "include/core/SkData.h" |
| 12 |
#include "include/core/SkFontMetrics.h" |
| 13 |
#include "include/core/SkFontTypes.h" |
| 14 |
#include "include/core/SkPath.h" |
| 15 |
#include "include/core/SkPathBuilder.h" |
| 16 |
#include "include/core/SkStream.h" |
| 17 |
#include "include/core/SkString.h" |
| 18 |
#include "include/ports/SkTypeface_win.h" |
| 19 |
#include "include/private/base/SkMacros.h" |
| 20 |
#include "include/private/base/SkOnce.h" |
| 21 |
#include "include/private/base/SkTDArray.h" |
| 22 |
#include "include/private/base/SkTemplates.h" |
| 23 |
#include "include/private/base/SkTo.h" |
| 24 |
#include "src/base/SkBase64.h" |
| 25 |
#include "src/base/SkLeanWindows.h" |
| 26 |
#include "src/base/SkUTF.h" |
| 27 |
#include "src/core/SkAdvancedTypefaceMetrics.h" |
| 28 |
#include "src/core/SkColorData.h" |
| 29 |
#include "src/core/SkDescriptor.h" |
| 30 |
#include "src/core/SkFontDescriptor.h" |
| 31 |
#include "src/core/SkGlyph.h" |
| 32 |
#include "src/core/SkMaskGamma.h" |
| 33 |
#include "src/core/SkStrikeCache.h" |
| 34 |
#include "src/core/SkTypefaceCache.h" |
| 35 |
#include "src/sfnt/SkOTTable_OS_2.h" |
| 36 |
#include "src/sfnt/SkOTTable_maxp.h" |
| 37 |
#include "src/sfnt/SkOTTable_name.h" |
| 38 |
#include "src/sfnt/SkOTUtils.h" |
| 39 |
#include "src/sfnt/SkSFNTHeader.h" |
| 40 |
#include "src/utils/SkMatrix22.h" |
| 41 |
#include "src/utils/win/SkHRESULT.h" |
| 42 |
|
| 43 |
#include <tchar.h> |
| 44 |
#include <usp10.h> |
| 45 |
#include <objbase.h> |
| 46 |
|
| 47 |
using namespace skia_private; |
| 48 |
|
| 49 |
namespace { |
| 50 |
static inline const constexpr bool kSkShowTextBlitCoverage = false; |
| 51 |
} |
| 52 |
|
| 53 |
static void (*gEnsureLOGFONTAccessibleProc)(const LOGFONT&); |
| 54 |
|
| 55 |
void SkTypeface_SetEnsureLOGFONTAccessibleProc(void (*proc)(const LOGFONT&)) { |
| 56 |
gEnsureLOGFONTAccessibleProc = proc; |
| 57 |
} |
| 58 |
|
| 59 |
static void call_ensure_accessible(const LOGFONT& lf) { |
| 60 |
if (gEnsureLOGFONTAccessibleProc) { |
| 61 |
gEnsureLOGFONTAccessibleProc(lf); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/////////////////////////////////////////////////////////////////////////////// |
| 66 |
|
| 67 |
// always packed xxRRGGBB |
| 68 |
typedef uint32_t SkGdiRGB; |
| 69 |
|
| 70 |
// define this in your Makefile or .gyp to enforce AA requests |
| 71 |
// which GDI ignores at small sizes. This flag guarantees AA |
| 72 |
// for rotated text, regardless of GDI's notions. |
| 73 |
//#define SK_ENFORCE_ROTATED_TEXT_AA_ON_WINDOWS |
| 74 |
|
| 75 |
static bool isLCD(const SkScalerContextRec& rec) { |
| 76 |
return SkMask::kLCD16_Format == rec.fMaskFormat; |
| 77 |
} |
| 78 |
|
| 79 |
static bool bothZero(SkScalar a, SkScalar b) { |
| 80 |
return 0 == a && 0 == b; |
| 81 |
} |
| 82 |
|
| 83 |
// returns false if there is any non-90-rotation or skew |
| 84 |
static bool isAxisAligned(const SkScalerContextRec& rec) { |
| 85 |
return 0 == rec.fPreSkewX && |
| 86 |
(bothZero(rec.fPost2x2[0][1], rec.fPost2x2[1][0]) || |
| 87 |
bothZero(rec.fPost2x2[0][0], rec.fPost2x2[1][1])); |
| 88 |
} |
| 89 |
|
| 90 |
static bool needToRenderWithSkia(const SkScalerContextRec& rec) { |
| 91 |
#ifdef SK_ENFORCE_ROTATED_TEXT_AA_ON_WINDOWS |
| 92 |
// What we really want to catch is when GDI will ignore the AA request and give |
| 93 |
// us BW instead. Smallish rotated text is one heuristic, so this code is just |
| 94 |
// an approximation. We shouldn't need to do this for larger sizes, but at those |
| 95 |
// sizes, the quality difference gets less and less between our general |
| 96 |
// scanconverter and GDI's. |
| 97 |
if (SkMask::kA8_Format == rec.fMaskFormat && !isAxisAligned(rec)) { |
| 98 |
return true; |
| 99 |
} |
| 100 |
#endif |
| 101 |
return rec.getHinting() == SkFontHinting::kNone || rec.getHinting() == SkFontHinting::kSlight; |
| 102 |
} |
| 103 |
|
| 104 |
static void tchar_to_skstring(const TCHAR t[], SkString* s) { |
| 105 |
#ifdef UNICODE |
| 106 |
size_t sSize = WideCharToMultiByte(CP_UTF8, 0, t, -1, nullptr, 0, nullptr, nullptr); |
| 107 |
s->resize(sSize); |
| 108 |
WideCharToMultiByte(CP_UTF8, 0, t, -1, s->data(), sSize, nullptr, nullptr); |
| 109 |
#else |
| 110 |
s->set(t); |
| 111 |
#endif |
| 112 |
} |
| 113 |
|
| 114 |
static void dcfontname_to_skstring(HDC deviceContext, const LOGFONT& lf, SkString* familyName) { |
| 115 |
int fontNameLen; //length of fontName in TCHARS. |
| 116 |
if (0 == (fontNameLen = GetTextFace(deviceContext, 0, nullptr))) { |
| 117 |
call_ensure_accessible(lf); |
| 118 |
if (0 == (fontNameLen = GetTextFace(deviceContext, 0, nullptr))) { |
| 119 |
fontNameLen = 0; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
AutoSTArray<LF_FULLFACESIZE, TCHAR> fontName(fontNameLen+1); |
| 124 |
if (0 == GetTextFace(deviceContext, fontNameLen, fontName.get())) { |
| 125 |
call_ensure_accessible(lf); |
| 126 |
if (0 == GetTextFace(deviceContext, fontNameLen, fontName.get())) { |
| 127 |
fontName[0] = 0; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
tchar_to_skstring(fontName.get(), familyName); |
| 132 |
} |
| 133 |
|
| 134 |
static void make_canonical(LOGFONT* lf) { |
| 135 |
lf->lfHeight = -64; |
| 136 |
lf->lfWidth = 0; // lfWidth is related to lfHeight, not to the OS/2::usWidthClass. |
| 137 |
lf->lfQuality = CLEARTYPE_QUALITY;//PROOF_QUALITY; |
| 138 |
lf->lfCharSet = DEFAULT_CHARSET; |
| 139 |
// lf->lfClipPrecision = 64; |
| 140 |
} |
| 141 |
|
| 142 |
static SkFontStyle get_style(const LOGFONT& lf) { |
| 143 |
return SkFontStyle(lf.lfWeight, |
| 144 |
SkFontStyle::kNormal_Width, |
| 145 |
lf.lfItalic ? SkFontStyle::kItalic_Slant : SkFontStyle::kUpright_Slant); |
| 146 |
} |
| 147 |
|
| 148 |
static inline FIXED SkFixedToFIXED(SkFixed x) { |
| 149 |
return *(FIXED*)(&x); |
| 150 |
} |
| 151 |
static inline SkFixed SkFIXEDToFixed(FIXED x) { |
| 152 |
return *(SkFixed*)(&x); |
| 153 |
} |
| 154 |
|
| 155 |
static inline FIXED SkScalarToFIXED(SkScalar x) { |
| 156 |
return SkFixedToFIXED(SkScalarToFixed(x)); |
| 157 |
} |
| 158 |
|
| 159 |
static inline SkScalar SkFIXEDToScalar(FIXED x) { |
| 160 |
return SkFixedToScalar(SkFIXEDToFixed(x)); |
| 161 |
} |
| 162 |
|
| 163 |
static unsigned calculateGlyphCount(HDC hdc, const LOGFONT& lf) { |
| 164 |
TEXTMETRIC textMetric; |
| 165 |
if (0 == GetTextMetrics(hdc, &textMetric)) { |
| 166 |
textMetric.tmPitchAndFamily = TMPF_VECTOR; |
| 167 |
call_ensure_accessible(lf); |
| 168 |
GetTextMetrics(hdc, &textMetric); |
| 169 |
} |
| 170 |
|
| 171 |
if (!(textMetric.tmPitchAndFamily & TMPF_VECTOR)) { |
| 172 |
return textMetric.tmLastChar; |
| 173 |
} |
| 174 |
|
| 175 |
// The 'maxp' table stores the number of glyphs at offset 4, in 2 bytes. |
| 176 |
uint16_t glyphs; |
| 177 |
if (GDI_ERROR != GetFontData(hdc, SkOTTableMaximumProfile::TAG, 4, &glyphs, sizeof(glyphs))) { |
| 178 |
return SkEndian_SwapBE16(glyphs); |
| 179 |
} |
| 180 |
|
| 181 |
// Binary search for glyph count. |
| 182 |
static const MAT2 mat2 = {{0, 1}, {0, 0}, {0, 0}, {0, 1}}; |
| 183 |
int32_t max = UINT16_MAX + 1; |
| 184 |
int32_t min = 0; |
| 185 |
GLYPHMETRICS gm; |
| 186 |
while (min < max) { |
| 187 |
int32_t mid = min + ((max - min) / 2); |
| 188 |
if (GetGlyphOutlineW(hdc, mid, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0, |
| 189 |
nullptr, &mat2) == GDI_ERROR) { |
| 190 |
max = mid; |
| 191 |
} else { |
| 192 |
min = mid + 1; |
| 193 |
} |
| 194 |
} |
| 195 |
SkASSERT(min == max); |
| 196 |
return min; |
| 197 |
} |
| 198 |
|
| 199 |
static unsigned calculateUPEM(HDC hdc, const LOGFONT& lf) { |
| 200 |
TEXTMETRIC textMetric; |
| 201 |
if (0 == GetTextMetrics(hdc, &textMetric)) { |
| 202 |
textMetric.tmPitchAndFamily = TMPF_VECTOR; |
| 203 |
call_ensure_accessible(lf); |
| 204 |
GetTextMetrics(hdc, &textMetric); |
| 205 |
} |
| 206 |
|
| 207 |
if (!(textMetric.tmPitchAndFamily & TMPF_VECTOR)) { |
| 208 |
return textMetric.tmMaxCharWidth; |
| 209 |
} |
| 210 |
|
| 211 |
OUTLINETEXTMETRIC otm; |
| 212 |
unsigned int otmRet = GetOutlineTextMetrics(hdc, sizeof(otm), &otm); |
| 213 |
if (0 == otmRet) { |
| 214 |
call_ensure_accessible(lf); |
| 215 |
otmRet = GetOutlineTextMetrics(hdc, sizeof(otm), &otm); |
| 216 |
} |
| 217 |
|
| 218 |
return (0 == otmRet) ? 0 : otm.otmEMSquare; |
| 219 |
} |
| 220 |
|
| 221 |
class [[nodiscard]] SkAutoHDC { |
| 222 |
public: |
| 223 |
explicit SkAutoHDC(const LOGFONT& lf) |
| 224 |
: fHdc(::CreateCompatibleDC(nullptr)) |
| 225 |
, fFont(::CreateFontIndirect(&lf)) |
| 226 |
, fSavefont((HFONT)::SelectObject(fHdc, fFont)) |
| 227 |
{ } |
| 228 |
~SkAutoHDC() { |
| 229 |
if (fHdc) { |
| 230 |
::SelectObject(fHdc, fSavefont); |
| 231 |
::DeleteDC(fHdc); |
| 232 |
} |
| 233 |
if (fFont) { |
| 234 |
::DeleteObject(fFont); |
| 235 |
} |
| 236 |
} |
| 237 |
operator HDC() { return fHdc; } |
| 238 |
private: |
| 239 |
HDC fHdc; |
| 240 |
HFONT fFont; |
| 241 |
HFONT fSavefont; |
| 242 |
}; |
| 243 |
|
| 244 |
class LogFontTypeface : public SkTypeface { |
| 245 |
public: |
| 246 |
LogFontTypeface(const SkFontStyle& style, const LOGFONT& lf, bool serializeAsStream) |
| 247 |
: SkTypeface(style, false) |
| 248 |
, fLogFont(lf) |
| 249 |
, fSerializeAsStream(serializeAsStream) |
| 250 |
{ |
| 251 |
SkAutoHDC hdc(fLogFont); |
| 252 |
TEXTMETRIC textMetric; |
| 253 |
if (0 == GetTextMetrics(hdc, &textMetric)) { |
| 254 |
call_ensure_accessible(lf); |
| 255 |
if (0 == GetTextMetrics(hdc, &textMetric)) { |
| 256 |
textMetric.tmPitchAndFamily = TMPF_TRUETYPE; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
// The fixed pitch bit is set if the font is *not* fixed pitch. |
| 261 |
this->setIsFixedPitch((textMetric.tmPitchAndFamily & TMPF_FIXED_PITCH) == 0); |
| 262 |
this->setFontStyle(SkFontStyle(textMetric.tmWeight, style.width(), style.slant())); |
| 263 |
|
| 264 |
// Used a logfont on a memory context, should never get a device font. |
| 265 |
// Therefore all TMPF_DEVICE will be PostScript (cubic) fonts. |
| 266 |
// If the font has cubic outlines, it will not be rendered with ClearType. |
| 267 |
fCanBeLCD = !((textMetric.tmPitchAndFamily & TMPF_VECTOR) && |
| 268 |
(textMetric.tmPitchAndFamily & TMPF_DEVICE)); |
| 269 |
} |
| 270 |
|
| 271 |
LOGFONT fLogFont; |
| 272 |
bool fSerializeAsStream; |
| 273 |
bool fCanBeLCD; |
| 274 |
|
| 275 |
static sk_sp<LogFontTypeface> Make(const LOGFONT& lf) { |
| 276 |
return sk_sp<LogFontTypeface>(new LogFontTypeface(get_style(lf), lf, false)); |
| 277 |
} |
| 278 |
|
| 279 |
static void EnsureAccessible(const SkTypeface* face) { |
| 280 |
call_ensure_accessible(static_cast<const LogFontTypeface*>(face)->fLogFont); |
| 281 |
} |
| 282 |
|
| 283 |
protected: |
| 284 |
std::unique_ptr<SkStreamAsset> onOpenStream(int* ttcIndex) const override; |
| 285 |
sk_sp<SkTypeface> onMakeClone(const SkFontArguments& args) const override; |
| 286 |
std::unique_ptr<SkScalerContext> onCreateScalerContext(const SkScalerContextEffects&, |
| 287 |
const SkDescriptor*) const override; |
| 288 |
void onFilterRec(SkScalerContextRec*) const override; |
| 289 |
void getGlyphToUnicodeMap(SkSpan<SkUnichar>) const override; |
| 290 |
std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const override; |
| 291 |
void onGetFontDescriptor(SkFontDescriptor*, bool*) const override; |
| 292 |
void onCharsToGlyphs(SkSpan<const SkUnichar>, SkSpan<SkGlyphID>) const override; |
| 293 |
int onCountGlyphs() const override; |
| 294 |
void getPostScriptGlyphNames(SkString*) const override; |
| 295 |
int onGetUPEM() const override; |
| 296 |
void onGetFamilyName(SkString* familyName) const override; |
| 297 |
bool onGetPostScriptName(SkString*) const override { return false; } |
| 298 |
SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override; |
| 299 |
bool onGlyphMaskNeedsCurrentColor() const override { return false; } |
| 300 |
int onGetVariationDesignPosition( |
| 301 |
SkSpan<SkFontArguments::VariationPosition::Coordinate>) const override |
| 302 |
{ |
| 303 |
return -1; |
| 304 |
} |
| 305 |
int onGetVariationDesignParameters(SkSpan<SkFontParameters::Variation::Axis>) const override |
| 306 |
{ |
| 307 |
return -1; |
| 308 |
} |
| 309 |
int onGetTableTags(SkSpan<SkFontTableTag>) const override; |
| 310 |
size_t onGetTableData(SkFontTableTag, size_t offset, size_t length, void* data) const override; |
| 311 |
sk_sp<SkData> onCopyTableData(SkFontTableTag) const override; |
| 312 |
}; |
| 313 |
|
| 314 |
class FontMemResourceTypeface : public LogFontTypeface { |
| 315 |
public: |
| 316 |
/** |
| 317 |
* The created FontMemResourceTypeface takes ownership of fontMemResource. |
| 318 |
*/ |
| 319 |
static sk_sp<FontMemResourceTypeface> Make(const LOGFONT& lf, HANDLE fontMemResource) { |
| 320 |
return sk_sp<FontMemResourceTypeface>( |
| 321 |
new FontMemResourceTypeface(get_style(lf), lf, fontMemResource)); |
| 322 |
} |
| 323 |
|
| 324 |
protected: |
| 325 |
void weak_dispose() const override { |
| 326 |
RemoveFontMemResourceEx(fFontMemResource); |
| 327 |
INHERITED::weak_dispose(); |
| 328 |
} |
| 329 |
|
| 330 |
private: |
| 331 |
/** |
| 332 |
* Takes ownership of fontMemResource. |
| 333 |
*/ |
| 334 |
FontMemResourceTypeface(const SkFontStyle& style, const LOGFONT& lf, HANDLE fontMemResource) |
| 335 |
: LogFontTypeface(style, lf, true), fFontMemResource(fontMemResource) |
| 336 |
{ } |
| 337 |
|
| 338 |
HANDLE fFontMemResource; |
| 339 |
|
| 340 |
using INHERITED = LogFontTypeface; |
| 341 |
}; |
| 342 |
|
| 343 |
static const LOGFONT& get_default_font() { |
| 344 |
static LOGFONT gDefaultFont; |
| 345 |
return gDefaultFont; |
| 346 |
} |
| 347 |
|
| 348 |
static bool FindByLogFont(SkTypeface* face, void* ctx) { |
| 349 |
LogFontTypeface* lface = static_cast<LogFontTypeface*>(face); |
| 350 |
const LOGFONT* lf = reinterpret_cast<const LOGFONT*>(ctx); |
| 351 |
|
| 352 |
return !memcmp(&lface->fLogFont, lf, sizeof(LOGFONT)); |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* This is public. It first searches the cache, and if a match is not found, |
| 357 |
* it creates a new face. |
| 358 |
*/ |
| 359 |
sk_sp<SkTypeface> SkCreateTypefaceFromLOGFONT(const LOGFONT& origLF) { |
| 360 |
LOGFONT lf = origLF; |
| 361 |
make_canonical(&lf); |
| 362 |
sk_sp<SkTypeface> face = SkTypefaceCache::FindByProcAndRef(FindByLogFont, &lf); |
| 363 |
if (!face) { |
| 364 |
face = LogFontTypeface::Make(lf); |
| 365 |
SkTypefaceCache::Add(face); |
| 366 |
} |
| 367 |
return face; |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* The created SkTypeface takes ownership of fontMemResource. |
| 372 |
*/ |
| 373 |
sk_sp<SkTypeface> SkCreateFontMemResourceTypefaceFromLOGFONT(const LOGFONT& origLF, HANDLE fontMemResource) { |
| 374 |
LOGFONT lf = origLF; |
| 375 |
make_canonical(&lf); |
| 376 |
// We'll never get a cache hit, so no point in putting this in SkTypefaceCache. |
| 377 |
return FontMemResourceTypeface::Make(lf, fontMemResource); |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* This is public |
| 382 |
*/ |
| 383 |
void SkLOGFONTFromTypeface(const SkTypeface* face, LOGFONT* lf) { |
| 384 |
if (nullptr == face) { |
| 385 |
*lf = get_default_font(); |
| 386 |
} else { |
| 387 |
*lf = static_cast<const LogFontTypeface*>(face)->fLogFont; |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
// Construct Glyph to Unicode table. |
| 392 |
// Unicode code points that require conjugate pairs in utf16 are not |
| 393 |
// supported. |
| 394 |
// TODO(arthurhsu): Add support for conjugate pairs. It looks like that may |
| 395 |
// require parsing the TTF cmap table (platform 4, encoding 12) directly instead |
| 396 |
// of calling GetFontUnicodeRange(). |
| 397 |
static void populate_glyph_to_unicode(HDC fontHdc, const unsigned glyphCount, |
| 398 |
SkSpan<SkUnichar> glyphToUnicode) { |
| 399 |
sk_bzero(glyphToUnicode.data(), glyphToUnicode.size_bytes()); |
| 400 |
DWORD glyphSetBufferSize = GetFontUnicodeRanges(fontHdc, nullptr); |
| 401 |
if (!glyphSetBufferSize) { |
| 402 |
return; |
| 403 |
} |
| 404 |
|
| 405 |
std::unique_ptr<BYTE[]> glyphSetBuffer(new BYTE[glyphSetBufferSize]); |
| 406 |
GLYPHSET* glyphSet = |
| 407 |
reinterpret_cast<LPGLYPHSET>(glyphSetBuffer.get()); |
| 408 |
if (GetFontUnicodeRanges(fontHdc, glyphSet) != glyphSetBufferSize) { |
| 409 |
return; |
| 410 |
} |
| 411 |
|
| 412 |
for (DWORD i = 0; i < glyphSet->cRanges; ++i) { |
| 413 |
// There is no guarantee that within a Unicode range, the corresponding |
| 414 |
// glyph id in a font file are continuous. So, even if we have ranges, |
| 415 |
// we can't just use the first and last entry of the range to compute |
| 416 |
// result. We need to enumerate them one by one. |
| 417 |
int count = glyphSet->ranges[i].cGlyphs; |
| 418 |
AutoTArray<WCHAR> chars(count + 1); |
| 419 |
chars[count] = 0; // termintate string |
| 420 |
AutoTArray<WORD> glyph(count); |
| 421 |
for (USHORT j = 0; j < count; ++j) { |
| 422 |
chars[j] = glyphSet->ranges[i].wcLow + j; |
| 423 |
} |
| 424 |
GetGlyphIndicesW(fontHdc, chars.get(), count, glyph.get(), |
| 425 |
GGI_MARK_NONEXISTING_GLYPHS); |
| 426 |
// If the glyph ID is valid, and the glyph is not mapped, then we will |
| 427 |
// fill in the char id into the vector. If the glyph is mapped already, |
| 428 |
// skip it. |
| 429 |
// TODO(arthurhsu): better improve this. e.g. Get all used char ids from |
| 430 |
// font cache, then generate this mapping table from there. It's |
| 431 |
// unlikely to have collisions since glyph reuse happens mostly for |
| 432 |
// different Unicode pages. |
| 433 |
for (USHORT j = 0; j < count; ++j) { |
| 434 |
if (glyph[j] != 0xFFFF && glyph[j] < glyphCount && glyphToUnicode[glyph[j]] == 0) { |
| 435 |
glyphToUnicode[glyph[j]] = chars[j]; |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
////////////////////////////////////////////////////////////////////////////////////// |
| 442 |
|
| 443 |
static int alignTo32(int n) { |
| 444 |
return (n + 31) & ~31; |
| 445 |
} |
| 446 |
|
| 447 |
struct MyBitmapInfo : public BITMAPINFO { |
| 448 |
RGBQUAD fMoreSpaceForColors[1]; |
| 449 |
}; |
| 450 |
|
| 451 |
class HDCOffscreen { |
| 452 |
public: |
| 453 |
HDCOffscreen() = default; |
| 454 |
|
| 455 |
~HDCOffscreen() { |
| 456 |
if (fDC) { |
| 457 |
::SelectObject(fDC, fSavefont); |
| 458 |
::DeleteDC(fDC); |
| 459 |
} |
| 460 |
if (fBM) { |
| 461 |
DeleteObject(fBM); |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
void init(HFONT font, const XFORM& xform) { |
| 466 |
fFont = font; |
| 467 |
fXform = xform; |
| 468 |
} |
| 469 |
|
| 470 |
void* draw(const SkGlyph&, bool isBW, size_t* srcRBPtr); |
| 471 |
|
| 472 |
private: |
| 473 |
HDC fDC{nullptr}; |
| 474 |
HFONT fSavefont{nullptr}; |
| 475 |
HBITMAP fBM{nullptr}; |
| 476 |
HFONT fFont{nullptr}; |
| 477 |
XFORM fXform{1, 0, 0, 1, 0, 0}; |
| 478 |
void* fBits{nullptr}; // points into fBM |
| 479 |
int fWidth{0}; |
| 480 |
int fHeight{0}; |
| 481 |
bool fIsBW{false}; |
| 482 |
}; |
| 483 |
|
| 484 |
void* HDCOffscreen::draw(const SkGlyph& glyph, bool isBW, size_t* srcRBPtr) { |
| 485 |
// Can we share the scalercontext's fDDC, so we don't need to create |
| 486 |
// a separate fDC here? |
| 487 |
if (nullptr == fDC) { |
| 488 |
fDC = CreateCompatibleDC(0); |
| 489 |
if (nullptr == fDC) { |
| 490 |
return nullptr; |
| 491 |
} |
| 492 |
SetGraphicsMode(fDC, GM_ADVANCED); |
| 493 |
SetBkMode(fDC, TRANSPARENT); |
| 494 |
SetTextAlign(fDC, TA_LEFT | TA_BASELINE); |
| 495 |
fSavefont = (HFONT)SelectObject(fDC, fFont); |
| 496 |
|
| 497 |
COLORREF color = 0x00FFFFFF; |
| 498 |
SkDEBUGCODE(COLORREF prev =) SetTextColor(fDC, color); |
| 499 |
SkASSERT(prev != CLR_INVALID); |
| 500 |
} |
| 501 |
|
| 502 |
if (fBM && (fIsBW != isBW || fWidth < glyph.width() || fHeight < glyph.height())) { |
| 503 |
DeleteObject(fBM); |
| 504 |
fBM = nullptr; |
| 505 |
} |
| 506 |
fIsBW = isBW; |
| 507 |
|
| 508 |
fWidth = std::max(fWidth, glyph.width()); |
| 509 |
fHeight = std::max(fHeight, glyph.height()); |
| 510 |
|
| 511 |
int biWidth = isBW ? alignTo32(fWidth) : fWidth; |
| 512 |
|
| 513 |
if (nullptr == fBM) { |
| 514 |
MyBitmapInfo info; |
| 515 |
sk_bzero(&info, sizeof(info)); |
| 516 |
if (isBW) { |
| 517 |
RGBQUAD blackQuad = { 0, 0, 0, 0 }; |
| 518 |
RGBQUAD whiteQuad = { 0xFF, 0xFF, 0xFF, 0 }; |
| 519 |
info.bmiColors[0] = blackQuad; |
| 520 |
info.bmiColors[1] = whiteQuad; |
| 521 |
} |
| 522 |
info.bmiHeader.biSize = sizeof(info.bmiHeader); |
| 523 |
info.bmiHeader.biWidth = biWidth; |
| 524 |
info.bmiHeader.biHeight = fHeight; |
| 525 |
info.bmiHeader.biPlanes = 1; |
| 526 |
info.bmiHeader.biBitCount = isBW ? 1 : 32; |
| 527 |
info.bmiHeader.biCompression = BI_RGB; |
| 528 |
if (isBW) { |
| 529 |
info.bmiHeader.biClrUsed = 2; |
| 530 |
} |
| 531 |
fBM = CreateDIBSection(fDC, &info, DIB_RGB_COLORS, &fBits, 0, 0); |
| 532 |
if (nullptr == fBM) { |
| 533 |
return nullptr; |
| 534 |
} |
| 535 |
SelectObject(fDC, fBM); |
| 536 |
} |
| 537 |
|
| 538 |
// erase |
| 539 |
size_t srcRB = isBW ? (biWidth >> 3) : (fWidth << 2); |
| 540 |
size_t size = fHeight * srcRB; |
| 541 |
memset(fBits, 0, size); |
| 542 |
|
| 543 |
XFORM xform = fXform; |
| 544 |
xform.eDx = (float)-glyph.left(); |
| 545 |
xform.eDy = (float)-glyph.top(); |
| 546 |
SetWorldTransform(fDC, &xform); |
| 547 |
|
| 548 |
uint16_t glyphID = glyph.getGlyphID(); |
| 549 |
BOOL ret = ExtTextOutW(fDC, 0, 0, ETO_GLYPH_INDEX, nullptr, reinterpret_cast<LPCWSTR>(&glyphID), |
| 550 |
1, nullptr); |
| 551 |
GdiFlush(); |
| 552 |
if (0 == ret) { |
| 553 |
return nullptr; |
| 554 |
} |
| 555 |
*srcRBPtr = srcRB; |
| 556 |
// offset to the start of the image |
| 557 |
return (char*)fBits + (fHeight - glyph.height()) * srcRB; |
| 558 |
} |
| 559 |
|
| 560 |
////////////////////////////////////////////////////////////////////////////// |
| 561 |
#define BUFFERSIZE (1 << 13) |
| 562 |
|
| 563 |
class SkScalerContext_GDI : public SkScalerContext { |
| 564 |
public: |
| 565 |
SkScalerContext_GDI(LogFontTypeface&, |
| 566 |
const SkScalerContextEffects&, |
| 567 |
const SkDescriptor* desc); |
| 568 |
~SkScalerContext_GDI() override; |
| 569 |
|
| 570 |
// Returns true if the constructor was able to complete all of its |
| 571 |
// initializations (which may include calling GDI). |
| 572 |
bool isValid() const; |
| 573 |
|
| 574 |
protected: |
| 575 |
GlyphMetrics generateMetrics(const SkGlyph&, SkArenaAlloc*) override; |
| 576 |
void generateImage(const SkGlyph&, void* imageBuffer) override; |
| 577 |
std::optional<GeneratedPath> generatePath(const SkGlyph&) override; |
| 578 |
void generateFontMetrics(SkFontMetrics*) override; |
| 579 |
|
| 580 |
private: |
| 581 |
DWORD getGDIGlyphPath(SkGlyphID glyph, UINT flags, |
| 582 |
AutoSTMalloc<BUFFERSIZE, uint8_t>* glyphbuf); |
| 583 |
template<bool APPLY_PREBLEND> |
| 584 |
static void RGBToA8(const SkGdiRGB* SK_RESTRICT src, size_t srcRB, |
| 585 |
const SkGlyph& glyph, void* imageBuffer, const uint8_t* table8); |
| 586 |
|
| 587 |
template<bool APPLY_PREBLEND> |
| 588 |
static void RGBToLcd16(const SkGdiRGB* SK_RESTRICT src, size_t srcRB, const SkGlyph& glyph, |
| 589 |
void* imageBuffer, |
| 590 |
const uint8_t* tableR, const uint8_t* tableG, const uint8_t* tableB); |
| 591 |
|
| 592 |
HDCOffscreen fOffscreen; |
| 593 |
/** fGsA is the non-rotational part of total matrix without the text height scale. |
| 594 |
* Used to find the magnitude of advances. |
| 595 |
*/ |
| 596 |
MAT2 fGsA; |
| 597 |
/** The total matrix without the textSize. */ |
| 598 |
MAT2 fMat22; |
| 599 |
/** Scales font to EM size. */ |
| 600 |
MAT2 fHighResMat22; |
| 601 |
HDC fDDC; |
| 602 |
HFONT fSavefont; |
| 603 |
HFONT fFont; |
| 604 |
SCRIPT_CACHE fSC; |
| 605 |
|
| 606 |
/** The total matrix which also removes EM scale. */ |
| 607 |
SkMatrix fHiResMatrix; |
| 608 |
/** fG_inv is the inverse of the rotational part of the total matrix. |
| 609 |
* Used to set the direction of advances. |
| 610 |
*/ |
| 611 |
SkMatrix fG_inv; |
| 612 |
enum Type { |
| 613 |
kTrueType_Type, kBitmap_Type, kLine_Type |
| 614 |
} fType; |
| 615 |
bool fGenerateFromPath; |
| 616 |
TEXTMETRIC fTM; |
| 617 |
}; |
| 618 |
|
| 619 |
static FIXED SkFloatToFIXED(float x) { |
| 620 |
return SkFixedToFIXED(SkFloatToFixed(x)); |
| 621 |
} |
| 622 |
|
| 623 |
static inline float SkFIXEDToFloat(FIXED x) { |
| 624 |
return SkFixedToFloat(SkFIXEDToFixed(x)); |
| 625 |
} |
| 626 |
|
| 627 |
static BYTE compute_quality(const SkScalerContextRec& rec) { |
| 628 |
switch (rec.fMaskFormat) { |
| 629 |
case SkMask::kBW_Format: |
| 630 |
return NONANTIALIASED_QUALITY; |
| 631 |
case SkMask::kLCD16_Format: |
| 632 |
return CLEARTYPE_QUALITY; |
| 633 |
default: |
| 634 |
if (rec.fFlags & SkScalerContext::kGenA8FromLCD_Flag) { |
| 635 |
return CLEARTYPE_QUALITY; |
| 636 |
} else { |
| 637 |
return ANTIALIASED_QUALITY; |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
SkScalerContext_GDI::SkScalerContext_GDI(LogFontTypeface& rawTypeface, |
| 643 |
const SkScalerContextEffects& effects, |
| 644 |
const SkDescriptor* desc) |
| 645 |
: SkScalerContext(rawTypeface, effects, desc) |
| 646 |
, fDDC(nullptr) |
| 647 |
, fSavefont(nullptr) |
| 648 |
, fFont(nullptr) |
| 649 |
, fSC(nullptr) |
| 650 |
, fGenerateFromPath(false) |
| 651 |
{ |
| 652 |
LogFontTypeface* typeface = static_cast<LogFontTypeface*>(this->getTypeface()); |
| 653 |
|
| 654 |
fDDC = ::CreateCompatibleDC(nullptr); |
| 655 |
if (!fDDC) { |
| 656 |
return; |
| 657 |
} |
| 658 |
SetGraphicsMode(fDDC, GM_ADVANCED); |
| 659 |
SetBkMode(fDDC, TRANSPARENT); |
| 660 |
|
| 661 |
// When GDI hinting, remove the entire Y scale from sA and GsA. (Prevents 'linear' metrics.) |
| 662 |
// When not hinting, remove only the integer Y scale from sA and GsA. (Applied by GDI.) |
| 663 |
SkScalerContextRec::PreMatrixScale scaleConstraints = |
| 664 |
(fRec.getHinting() == SkFontHinting::kNone || fRec.getHinting() == SkFontHinting::kSlight) |
| 665 |
? SkScalerContextRec::PreMatrixScale::kVerticalInteger |
| 666 |
: SkScalerContextRec::PreMatrixScale::kVertical; |
| 667 |
SkVector scale; |
| 668 |
SkMatrix sA; |
| 669 |
SkMatrix GsA; |
| 670 |
SkMatrix A; |
| 671 |
fRec.computeMatrices(scaleConstraints, &scale, &sA, &GsA, &fG_inv, &A); |
| 672 |
|
| 673 |
fGsA.eM11 = SkScalarToFIXED(GsA.get(SkMatrix::kMScaleX)); |
| 674 |
fGsA.eM12 = SkScalarToFIXED(-GsA.get(SkMatrix::kMSkewY)); // This should be ~0. |
| 675 |
fGsA.eM21 = SkScalarToFIXED(-GsA.get(SkMatrix::kMSkewX)); |
| 676 |
fGsA.eM22 = SkScalarToFIXED(GsA.get(SkMatrix::kMScaleY)); |
| 677 |
|
| 678 |
// When not hinting, scale was computed with kVerticalInteger, so is already an integer. |
| 679 |
// The sA and GsA transforms will be used to create 'linear' metrics. |
| 680 |
|
| 681 |
// When hinting, scale was computed with kVertical, stating that our port can handle |
| 682 |
// non-integer scales. This is done so that sA and GsA are computed without any 'residual' |
| 683 |
// scale in them, preventing 'linear' metrics. However, GDI cannot actually handle non-integer |
| 684 |
// scales so we need to round in this case. This is fine, since all of the scale has been |
| 685 |
// removed from sA and GsA, so GDI will be handling the scale completely. |
| 686 |
SkScalar gdiTextSize = SkScalarRoundToScalar(scale.fY); |
| 687 |
|
| 688 |
// GDI will not accept a size of zero, so round the range [0, 1] to 1. |
| 689 |
// If the size was non-zero, the scale factors will also be non-zero and 1px tall text is drawn. |
| 690 |
// If the size actually was zero, the scale factors will also be zero, so GDI will draw nothing. |
| 691 |
if (gdiTextSize == 0) { |
| 692 |
gdiTextSize = SK_Scalar1; |
| 693 |
} |
| 694 |
|
| 695 |
LOGFONT lf = typeface->fLogFont; |
| 696 |
lf.lfHeight = -SkScalarTruncToInt(gdiTextSize); |
| 697 |
lf.lfQuality = compute_quality(fRec); |
| 698 |
fFont = CreateFontIndirect(&lf); |
| 699 |
if (!fFont) { |
| 700 |
return; |
| 701 |
} |
| 702 |
|
| 703 |
fSavefont = (HFONT)SelectObject(fDDC, fFont); |
| 704 |
|
| 705 |
if (0 == GetTextMetrics(fDDC, &fTM)) { |
| 706 |
call_ensure_accessible(lf); |
| 707 |
if (0 == GetTextMetrics(fDDC, &fTM)) { |
| 708 |
fTM.tmPitchAndFamily = TMPF_TRUETYPE; |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
XFORM xform; |
| 713 |
if (fTM.tmPitchAndFamily & TMPF_VECTOR) { |
| 714 |
// Used a logfont on a memory context, should never get a device font. |
| 715 |
// Therefore all TMPF_DEVICE will be PostScript fonts. |
| 716 |
|
| 717 |
// If TMPF_VECTOR is set, one of TMPF_TRUETYPE or TMPF_DEVICE means that |
| 718 |
// we have an outline font. Otherwise we have a vector FON, which is |
| 719 |
// scalable, but not an outline font. |
| 720 |
// This was determined by testing with Type1 PFM/PFB and |
| 721 |
// OpenTypeCFF OTF, as well as looking at Wine bugs and sources. |
| 722 |
if (fTM.tmPitchAndFamily & (TMPF_TRUETYPE | TMPF_DEVICE)) { |
| 723 |
// Truetype or PostScript. |
| 724 |
fType = SkScalerContext_GDI::kTrueType_Type; |
| 725 |
} else { |
| 726 |
// Stroked FON. |
| 727 |
fType = SkScalerContext_GDI::kLine_Type; |
| 728 |
} |
| 729 |
|
| 730 |
// fPost2x2 is column-major, left handed (y down). |
| 731 |
// XFORM 2x2 is row-major, left handed (y down). |
| 732 |
xform.eM11 = SkScalarToFloat(sA.get(SkMatrix::kMScaleX)); |
| 733 |
xform.eM12 = SkScalarToFloat(sA.get(SkMatrix::kMSkewY)); |
| 734 |
xform.eM21 = SkScalarToFloat(sA.get(SkMatrix::kMSkewX)); |
| 735 |
xform.eM22 = SkScalarToFloat(sA.get(SkMatrix::kMScaleY)); |
| 736 |
xform.eDx = 0; |
| 737 |
xform.eDy = 0; |
| 738 |
|
| 739 |
// MAT2 is row major, right handed (y up). |
| 740 |
fMat22.eM11 = SkFloatToFIXED(xform.eM11); |
| 741 |
fMat22.eM12 = SkFloatToFIXED(-xform.eM12); |
| 742 |
fMat22.eM21 = SkFloatToFIXED(-xform.eM21); |
| 743 |
fMat22.eM22 = SkFloatToFIXED(xform.eM22); |
| 744 |
|
| 745 |
if (needToRenderWithSkia(fRec)) { |
| 746 |
fGenerateFromPath = true; |
| 747 |
} |
| 748 |
|
| 749 |
// Create a hires matrix if we need linear metrics. |
| 750 |
if (this->isLinearMetrics()) { |
| 751 |
OUTLINETEXTMETRIC otm; |
| 752 |
UINT success = GetOutlineTextMetrics(fDDC, sizeof(otm), &otm); |
| 753 |
if (0 == success) { |
| 754 |
call_ensure_accessible(lf); |
| 755 |
success = GetOutlineTextMetrics(fDDC, sizeof(otm), &otm); |
| 756 |
} |
| 757 |
if (0 != success) { |
| 758 |
SkScalar upem = SkIntToScalar(otm.otmEMSquare); |
| 759 |
|
| 760 |
SkScalar gdiTextSizeToEMScale = upem / gdiTextSize; |
| 761 |
fHighResMat22.eM11 = SkScalarToFIXED(gdiTextSizeToEMScale); |
| 762 |
fHighResMat22.eM12 = SkScalarToFIXED(0); |
| 763 |
fHighResMat22.eM21 = SkScalarToFIXED(0); |
| 764 |
fHighResMat22.eM22 = SkScalarToFIXED(gdiTextSizeToEMScale); |
| 765 |
|
| 766 |
SkScalar removeEMScale = SkScalarInvert(upem); |
| 767 |
fHiResMatrix = A; |
| 768 |
fHiResMatrix.preScale(removeEMScale, removeEMScale); |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
} else { |
| 773 |
// Assume bitmap |
| 774 |
fType = SkScalerContext_GDI::kBitmap_Type; |
| 775 |
|
| 776 |
xform.eM11 = 1.0f; |
| 777 |
xform.eM12 = 0.0f; |
| 778 |
xform.eM21 = 0.0f; |
| 779 |
xform.eM22 = 1.0f; |
| 780 |
xform.eDx = 0.0f; |
| 781 |
xform.eDy = 0.0f; |
| 782 |
|
| 783 |
// fPost2x2 is column-major, left handed (y down). |
| 784 |
// MAT2 is row major, right handed (y up). |
| 785 |
fMat22.eM11 = SkScalarToFIXED(fRec.fPost2x2[0][0]); |
| 786 |
fMat22.eM12 = SkScalarToFIXED(-fRec.fPost2x2[1][0]); |
| 787 |
fMat22.eM21 = SkScalarToFIXED(-fRec.fPost2x2[0][1]); |
| 788 |
fMat22.eM22 = SkScalarToFIXED(fRec.fPost2x2[1][1]); |
| 789 |
} |
| 790 |
|
| 791 |
fOffscreen.init(fFont, xform); |
| 792 |
} |
| 793 |
|
| 794 |
SkScalerContext_GDI::~SkScalerContext_GDI() { |
| 795 |
if (fDDC) { |
| 796 |
::SelectObject(fDDC, fSavefont); |
| 797 |
::DeleteDC(fDDC); |
| 798 |
} |
| 799 |
if (fFont) { |
| 800 |
::DeleteObject(fFont); |
| 801 |
} |
| 802 |
if (fSC) { |
| 803 |
::ScriptFreeCache(&fSC); |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
bool SkScalerContext_GDI::isValid() const { |
| 808 |
return fDDC && fFont; |
| 809 |
} |
| 810 |
|
| 811 |
SkScalerContext::GlyphMetrics SkScalerContext_GDI::generateMetrics(const SkGlyph& glyph, |
| 812 |
SkArenaAlloc*) { |
| 813 |
SkASSERT(fDDC); |
| 814 |
|
| 815 |
GlyphMetrics mx(glyph.maskFormat()); |
| 816 |
|
| 817 |
if (fType == SkScalerContext_GDI::kBitmap_Type || fType == SkScalerContext_GDI::kLine_Type) { |
| 818 |
SIZE size; |
| 819 |
WORD glyphs = glyph.getGlyphID(); |
| 820 |
int width, height; |
| 821 |
if (0 == GetTextExtentPointI(fDDC, &glyphs, 1, &size)) { |
| 822 |
width = fTM.tmMaxCharWidth; |
| 823 |
height = fTM.tmHeight; |
| 824 |
} else { |
| 825 |
width = size.cx; |
| 826 |
height = size.cy; |
| 827 |
} |
| 828 |
|
| 829 |
// Bitmap FON cannot underhang, but vector FON may. |
| 830 |
// There appears no means of determining underhang of vector FON. |
| 831 |
int left = 0; |
| 832 |
int top = -fTM.tmAscent; |
| 833 |
|
| 834 |
mx.bounds = SkRect::MakeXYWH(left, top, width, height); |
| 835 |
mx.advance = SkVector{(float)width, 0}; |
| 836 |
|
| 837 |
// Vector FON will transform nicely, but bitmap FON do not. |
| 838 |
if (fType == SkScalerContext_GDI::kLine_Type) { |
| 839 |
SkRect bounds = SkRect::MakeXYWH(left, top, width, height); |
| 840 |
SkMatrix m; |
| 841 |
m.setAll(SkFIXEDToScalar(fMat22.eM11), -SkFIXEDToScalar(fMat22.eM21), 0, |
| 842 |
-SkFIXEDToScalar(fMat22.eM12), SkFIXEDToScalar(fMat22.eM22), 0, |
| 843 |
0, 0, 1); |
| 844 |
m.mapRect(&bounds); |
| 845 |
bounds.roundOut(&mx.bounds); |
| 846 |
} |
| 847 |
|
| 848 |
// Apply matrix to advance. |
| 849 |
mx.advance.fY = -SkFIXEDToFloat(fMat22.eM12) * mx.advance.fX; |
| 850 |
mx.advance.fX *= SkFIXEDToFloat(fMat22.eM11); |
| 851 |
|
| 852 |
// These do not have an outline path at all. |
| 853 |
mx.neverRequestPath = true; |
| 854 |
return mx; |
| 855 |
} |
| 856 |
|
| 857 |
UINT glyphId = glyph.getGlyphID(); |
| 858 |
|
| 859 |
GLYPHMETRICS gm; |
| 860 |
sk_bzero(&gm, sizeof(gm)); |
| 861 |
|
| 862 |
DWORD status = GetGlyphOutlineW(fDDC, glyphId, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0, nullptr, &fMat22); |
| 863 |
if (GDI_ERROR == status) { |
| 864 |
LogFontTypeface::EnsureAccessible(this->getTypeface()); |
| 865 |
status = GetGlyphOutlineW(fDDC, glyphId, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0, nullptr, &fMat22); |
| 866 |
if (GDI_ERROR == status) { |
| 867 |
return mx; |
| 868 |
} |
| 869 |
} |
| 870 |
|
| 871 |
bool empty = false; |
| 872 |
// The black box is either the embedded bitmap size or the outline extent. |
| 873 |
// It is 1x1 if nothing is to be drawn, but will also be 1x1 if something very small |
| 874 |
// is to be drawn, like a '.'. We need to outset '.' but do not wish to outset ' '. |
| 875 |
if (1 == gm.gmBlackBoxX && 1 == gm.gmBlackBoxY) { |
| 876 |
// If GetGlyphOutline with GGO_NATIVE returns 0, we know there was no outline. |
| 877 |
DWORD bufferSize = GetGlyphOutlineW(fDDC, glyphId, GGO_NATIVE | GGO_GLYPH_INDEX, &gm, 0, nullptr, &fMat22); |
| 878 |
empty = (0 == bufferSize); |
| 879 |
} |
| 880 |
|
| 881 |
|
| 882 |
if (!empty) { |
| 883 |
int y = -gm.gmptGlyphOrigin.y; |
| 884 |
int x = gm.gmptGlyphOrigin.x; |
| 885 |
// Outset, since the image may bleed out of the black box. |
| 886 |
// For embedded bitmaps the black box should be exact. |
| 887 |
// For outlines we need to outset by 1 in all directions for bleed. |
| 888 |
// For ClearType we need to outset by 2 for bleed. |
| 889 |
mx.bounds = SkRect::MakeXYWH(x, y, gm.gmBlackBoxX, gm.gmBlackBoxY).makeOutset(2, 2); |
| 890 |
} |
| 891 |
// TODO(benjaminwagner): What is the type of gm.gmCellInc[XY]? |
| 892 |
mx.advance.fX = (float)((int)gm.gmCellIncX); |
| 893 |
mx.advance.fY = (float)((int)gm.gmCellIncY); |
| 894 |
|
| 895 |
if ((fTM.tmPitchAndFamily & TMPF_VECTOR) && this->isLinearMetrics()) { |
| 896 |
sk_bzero(&gm, sizeof(gm)); |
| 897 |
status = GetGlyphOutlineW(fDDC, glyphId, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0, nullptr, &fHighResMat22); |
| 898 |
if (GDI_ERROR != status) { |
| 899 |
mx.advance = fHiResMatrix.mapPoint({SkIntToScalar(gm.gmCellIncX), |
| 900 |
SkIntToScalar(gm.gmCellIncY)}); |
| 901 |
} |
| 902 |
} else if (!isAxisAligned(this->fRec)) { |
| 903 |
status = GetGlyphOutlineW(fDDC, glyphId, GGO_METRICS | GGO_GLYPH_INDEX, &gm, 0, nullptr, &fGsA); |
| 904 |
if (GDI_ERROR != status) { |
| 905 |
mx.advance = fG_inv.mapPoint({SkIntToScalar(gm.gmCellIncX), SkIntToScalar(gm.gmCellIncY)}); |
| 906 |
} |
| 907 |
} |
| 908 |
|
| 909 |
mx.computeFromPath = fGenerateFromPath; |
| 910 |
return mx; |
| 911 |
} |
| 912 |
|
| 913 |
static const MAT2 gMat2Identity = {{0, 1}, {0, 0}, {0, 0}, {0, 1}}; |
| 914 |
void SkScalerContext_GDI::generateFontMetrics(SkFontMetrics* metrics) { |
| 915 |
if (nullptr == metrics) { |
| 916 |
return; |
| 917 |
} |
| 918 |
sk_bzero(metrics, sizeof(*metrics)); |
| 919 |
|
| 920 |
SkASSERT(fDDC); |
| 921 |
|
| 922 |
#ifndef SK_GDI_ALWAYS_USE_TEXTMETRICS_FOR_FONT_METRICS |
| 923 |
if (fType == SkScalerContext_GDI::kBitmap_Type || fType == SkScalerContext_GDI::kLine_Type) { |
| 924 |
#endif |
| 925 |
metrics->fTop = SkIntToScalar(-fTM.tmAscent); |
| 926 |
metrics->fAscent = SkIntToScalar(-fTM.tmAscent); |
| 927 |
metrics->fDescent = SkIntToScalar(fTM.tmDescent); |
| 928 |
metrics->fBottom = SkIntToScalar(fTM.tmDescent); |
| 929 |
metrics->fLeading = SkIntToScalar(fTM.tmExternalLeading); |
| 930 |
metrics->fAvgCharWidth = SkIntToScalar(fTM.tmAveCharWidth); |
| 931 |
metrics->fMaxCharWidth = SkIntToScalar(fTM.tmMaxCharWidth); |
| 932 |
metrics->fXMin = 0; |
| 933 |
metrics->fXMax = metrics->fMaxCharWidth; |
| 934 |
//metrics->fXHeight = 0; |
| 935 |
#ifndef SK_GDI_ALWAYS_USE_TEXTMETRICS_FOR_FONT_METRICS |
| 936 |
return; |
| 937 |
} |
| 938 |
#endif |
| 939 |
|
| 940 |
OUTLINETEXTMETRIC otm; |
| 941 |
|
| 942 |
uint32_t ret = GetOutlineTextMetrics(fDDC, sizeof(otm), &otm); |
| 943 |
if (0 == ret) { |
| 944 |
LogFontTypeface::EnsureAccessible(this->getTypeface()); |
| 945 |
ret = GetOutlineTextMetrics(fDDC, sizeof(otm), &otm); |
| 946 |
} |
| 947 |
if (0 == ret) { |
| 948 |
return; |
| 949 |
} |
| 950 |
|
| 951 |
#ifndef SK_GDI_ALWAYS_USE_TEXTMETRICS_FOR_FONT_METRICS |
| 952 |
metrics->fTop = SkIntToScalar(-otm.otmrcFontBox.top); |
| 953 |
metrics->fAscent = SkIntToScalar(-otm.otmAscent); |
| 954 |
metrics->fDescent = SkIntToScalar(-otm.otmDescent); |
| 955 |
metrics->fBottom = SkIntToScalar(-otm.otmrcFontBox.bottom); |
| 956 |
metrics->fLeading = SkIntToScalar(otm.otmLineGap); |
| 957 |
metrics->fAvgCharWidth = SkIntToScalar(otm.otmTextMetrics.tmAveCharWidth); |
| 958 |
metrics->fMaxCharWidth = SkIntToScalar(otm.otmTextMetrics.tmMaxCharWidth); |
| 959 |
metrics->fXMin = SkIntToScalar(otm.otmrcFontBox.left); |
| 960 |
metrics->fXMax = SkIntToScalar(otm.otmrcFontBox.right); |
| 961 |
#endif |
| 962 |
metrics->fUnderlineThickness = SkIntToScalar(otm.otmsUnderscoreSize); |
| 963 |
metrics->fUnderlinePosition = -SkIntToScalar(otm.otmsUnderscorePosition); |
| 964 |
|
| 965 |
metrics->fFlags |= SkFontMetrics::kUnderlineThicknessIsValid_Flag; |
| 966 |
metrics->fFlags |= SkFontMetrics::kUnderlinePositionIsValid_Flag; |
| 967 |
|
| 968 |
metrics->fXHeight = SkIntToScalar(otm.otmsXHeight); |
| 969 |
GLYPHMETRICS gm; |
| 970 |
sk_bzero(&gm, sizeof(gm)); |
| 971 |
DWORD len = GetGlyphOutlineW(fDDC, 'x', GGO_METRICS, &gm, 0, nullptr, &gMat2Identity); |
| 972 |
if (len != GDI_ERROR && gm.gmBlackBoxY > 0) { |
| 973 |
metrics->fXHeight = SkIntToScalar(gm.gmBlackBoxY); |
| 974 |
} |
| 975 |
} |
| 976 |
|
| 977 |
//////////////////////////////////////////////////////////////////////////////////////// |
| 978 |
|
| 979 |
static void build_power_table(uint8_t table[], float ee) { |
| 980 |
for (int i = 0; i < 256; i++) { |
| 981 |
float x = i / 255.f; |
| 982 |
x = std::pow(x, ee); |
| 983 |
int xx = SkScalarRoundToInt(x * 255); |
| 984 |
table[i] = SkToU8(xx); |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
/** |
| 989 |
* This will invert the gamma applied by GDI (gray-scale antialiased), so we |
| 990 |
* can get linear values. |
| 991 |
* |
| 992 |
* GDI grayscale appears to use a hard-coded gamma of 2.3. |
| 993 |
* |
| 994 |
* GDI grayscale appears to draw using the black and white rasterizer at four |
| 995 |
* times the size and then downsamples to compute the coverage mask. As a |
| 996 |
* result there are only seventeen total grays. This lack of fidelity means |
| 997 |
* that shifting into other color spaces is imprecise. |
| 998 |
*/ |
| 999 |
static const uint8_t* getInverseGammaTableGDI() { |
| 1000 |
static SkOnce once; |
| 1001 |
static uint8_t gTableGdi[256]; |
| 1002 |
once([]{ |
| 1003 |
build_power_table(gTableGdi, 2.3f); |
| 1004 |
}); |
| 1005 |
return gTableGdi; |
| 1006 |
} |
| 1007 |
|
| 1008 |
/** |
| 1009 |
* This will invert the gamma applied by GDI ClearType, so we can get linear |
| 1010 |
* values. |
| 1011 |
* |
| 1012 |
* GDI ClearType uses SPI_GETFONTSMOOTHINGCONTRAST / 1000 as the gamma value. |
| 1013 |
* If this value is not specified, the default is a gamma of 1.4. |
| 1014 |
*/ |
| 1015 |
static const uint8_t* getInverseGammaTableClearType() { |
| 1016 |
static SkOnce once; |
| 1017 |
static uint8_t gTableClearType[256]; |
| 1018 |
once([]{ |
| 1019 |
UINT level = 0; |
| 1020 |
if (!SystemParametersInfo(SPI_GETFONTSMOOTHINGCONTRAST, 0, &level, 0) || !level) { |
| 1021 |
// can't get the data, so use a default |
| 1022 |
level = 1400; |
| 1023 |
} |
| 1024 |
build_power_table(gTableClearType, level / 1000.0f); |
| 1025 |
}); |
| 1026 |
return gTableClearType; |
| 1027 |
} |
| 1028 |
|
| 1029 |
#include "src/core/SkColorData.h" |
| 1030 |
|
| 1031 |
//Cannot assume that the input rgb is gray due to possible setting of kGenA8FromLCD_Flag. |
| 1032 |
template<bool APPLY_PREBLEND> |
| 1033 |
static inline uint8_t rgb_to_a8(SkGdiRGB rgb, const uint8_t* table8) { |
| 1034 |
U8CPU r = (rgb >> 16) & 0xFF; |
| 1035 |
U8CPU g = (rgb >> 8) & 0xFF; |
| 1036 |
U8CPU b = (rgb >> 0) & 0xFF; |
| 1037 |
return sk_apply_lut_if<APPLY_PREBLEND>(SkComputeLuminance(r, g, b), table8); |
| 1038 |
} |
| 1039 |
|
| 1040 |
template<bool APPLY_PREBLEND> |
| 1041 |
static inline uint16_t rgb_to_lcd16(SkGdiRGB rgb, const uint8_t* tableR, |
| 1042 |
const uint8_t* tableG, |
| 1043 |
const uint8_t* tableB) { |
| 1044 |
U8CPU r = sk_apply_lut_if<APPLY_PREBLEND>((rgb >> 16) & 0xFF, tableR); |
| 1045 |
U8CPU g = sk_apply_lut_if<APPLY_PREBLEND>((rgb >> 8) & 0xFF, tableG); |
| 1046 |
U8CPU b = sk_apply_lut_if<APPLY_PREBLEND>((rgb >> 0) & 0xFF, tableB); |
| 1047 |
if constexpr (kSkShowTextBlitCoverage) { |
| 1048 |
r = std::max(r, 10u); |
| 1049 |
g = std::max(g, 10u); |
| 1050 |
b = std::max(b, 10u); |
| 1051 |
} |
| 1052 |
return SkPack888ToRGB16(r, g, b); |
| 1053 |
} |
| 1054 |
|
| 1055 |
template<bool APPLY_PREBLEND> |
| 1056 |
void SkScalerContext_GDI::RGBToA8(const SkGdiRGB* SK_RESTRICT src, size_t srcRB, |
| 1057 |
const SkGlyph& glyph, void* imageBuffer, const uint8_t* table8) { |
| 1058 |
const size_t dstRB = glyph.rowBytes(); |
| 1059 |
const int width = glyph.width(); |
| 1060 |
uint8_t* SK_RESTRICT dst = (uint8_t*)((char*)imageBuffer + (glyph.height() - 1) * dstRB); |
| 1061 |
|
| 1062 |
for (int y = 0; y < glyph.height(); y++) { |
| 1063 |
for (int i = 0; i < width; i++) { |
| 1064 |
dst[i] = rgb_to_a8<APPLY_PREBLEND>(src[i], table8); |
| 1065 |
if constexpr (kSkShowTextBlitCoverage) { |
| 1066 |
dst[i] = std::max<uint8_t>(dst[i], 10u); |
| 1067 |
} |
| 1068 |
} |
| 1069 |
src = SkTAddOffset<const SkGdiRGB>(src, srcRB); |
| 1070 |
dst -= dstRB; |
| 1071 |
} |
| 1072 |
} |
| 1073 |
|
| 1074 |
template<bool APPLY_PREBLEND> |
| 1075 |
void SkScalerContext_GDI::RGBToLcd16( |
| 1076 |
const SkGdiRGB* SK_RESTRICT src, size_t srcRB, const SkGlyph& glyph, void* imageBuffer, |
| 1077 |
const uint8_t* tableR, const uint8_t* tableG, const uint8_t* tableB) { |
| 1078 |
const size_t dstRB = glyph.rowBytes(); |
| 1079 |
const int width = glyph.width(); |
| 1080 |
uint16_t* SK_RESTRICT dst = (uint16_t*)((char*)imageBuffer + (glyph.height() - 1) * dstRB); |
| 1081 |
|
| 1082 |
for (int y = 0; y < glyph.height(); y++) { |
| 1083 |
for (int i = 0; i < width; i++) { |
| 1084 |
dst[i] = rgb_to_lcd16<APPLY_PREBLEND>(src[i], tableR, tableG, tableB); |
| 1085 |
} |
| 1086 |
src = SkTAddOffset<const SkGdiRGB>(src, srcRB); |
| 1087 |
dst = (uint16_t*)((char*)dst - dstRB); |
| 1088 |
} |
| 1089 |
} |
| 1090 |
|
| 1091 |
void SkScalerContext_GDI::generateImage(const SkGlyph& glyph, void* imageBuffer) { |
| 1092 |
SkASSERT(fDDC); |
| 1093 |
|
| 1094 |
if (fGenerateFromPath && glyph.path()) { |
| 1095 |
this->generateImageFromPath(glyph, imageBuffer); |
| 1096 |
return; |
| 1097 |
} |
| 1098 |
|
| 1099 |
const bool isBW = SkMask::kBW_Format == fRec.fMaskFormat; |
| 1100 |
const bool isAA = !isLCD(fRec); |
| 1101 |
|
| 1102 |
size_t srcRB; |
| 1103 |
void* bits = fOffscreen.draw(glyph, isBW, &srcRB); |
| 1104 |
if (nullptr == bits) { |
| 1105 |
LogFontTypeface::EnsureAccessible(this->getTypeface()); |
| 1106 |
bits = fOffscreen.draw(glyph, isBW, &srcRB); |
| 1107 |
if (nullptr == bits) { |
| 1108 |
sk_bzero(imageBuffer, glyph.imageSize()); |
| 1109 |
return; |
| 1110 |
} |
| 1111 |
} |
| 1112 |
|
| 1113 |
if (!isBW) { |
| 1114 |
const uint8_t* table; |
| 1115 |
//The offscreen contains a GDI blit if isAA and kGenA8FromLCD_Flag is not set. |
| 1116 |
//Otherwise the offscreen contains a ClearType blit. |
| 1117 |
if (isAA && !(fRec.fFlags & SkScalerContext::kGenA8FromLCD_Flag)) { |
| 1118 |
table = getInverseGammaTableGDI(); |
| 1119 |
} else { |
| 1120 |
table = getInverseGammaTableClearType(); |
| 1121 |
} |
| 1122 |
//Note that the following cannot really be integrated into the |
| 1123 |
//pre-blend, since we may not be applying the pre-blend; when we aren't |
| 1124 |
//applying the pre-blend it means that a filter wants linear anyway. |
| 1125 |
//Other code may also be applying the pre-blend, so we'd need another |
| 1126 |
//one with this and one without. |
| 1127 |
SkGdiRGB* addr = (SkGdiRGB*)bits; |
| 1128 |
for (int y = 0; y < glyph.height(); ++y) { |
| 1129 |
for (int x = 0; x < glyph.width(); ++x) { |
| 1130 |
int r = (addr[x] >> 16) & 0xFF; |
| 1131 |
int g = (addr[x] >> 8) & 0xFF; |
| 1132 |
int b = (addr[x] >> 0) & 0xFF; |
| 1133 |
addr[x] = (table[r] << 16) | (table[g] << 8) | table[b]; |
| 1134 |
} |
| 1135 |
addr = SkTAddOffset<SkGdiRGB>(addr, srcRB); |
| 1136 |
} |
| 1137 |
} |
| 1138 |
|
| 1139 |
size_t dstRB = glyph.rowBytes(); |
| 1140 |
if (isBW) { |
| 1141 |
const uint8_t* src = (const uint8_t*)bits; |
| 1142 |
uint8_t* dst = (uint8_t*)((char*)imageBuffer + (glyph.height() - 1) * dstRB); |
| 1143 |
for (int y = 0; y < glyph.height(); y++) { |
| 1144 |
memcpy(dst, src, dstRB); |
| 1145 |
src += srcRB; |
| 1146 |
dst -= dstRB; |
| 1147 |
} |
| 1148 |
if constexpr (kSkShowTextBlitCoverage) { |
| 1149 |
if (glyph.width() > 0 && glyph.height() > 0) { |
| 1150 |
int bitCount = glyph.width() & 7; |
| 1151 |
uint8_t* first = (uint8_t*)imageBuffer; |
| 1152 |
uint8_t* last = first + glyph.height() * dstRB - 1; |
| 1153 |
*first |= 1 << 7; |
| 1154 |
*last |= bitCount == 0 ? 1 : 1 << (8 - bitCount); |
| 1155 |
} |
| 1156 |
} |
| 1157 |
} else if (isAA) { |
| 1158 |
// since the caller may require A8 for maskfilters, we can't check for BW |
| 1159 |
// ... until we have the caller tell us that explicitly |
| 1160 |
const SkGdiRGB* src = (const SkGdiRGB*)bits; |
| 1161 |
if (fPreBlend.isApplicable()) { |
| 1162 |
RGBToA8<true>(src, srcRB, glyph, imageBuffer, fPreBlend.fG); |
| 1163 |
} else { |
| 1164 |
RGBToA8<false>(src, srcRB, glyph, imageBuffer, fPreBlend.fG); |
| 1165 |
} |
| 1166 |
} else { // LCD16 |
| 1167 |
const SkGdiRGB* src = (const SkGdiRGB*)bits; |
| 1168 |
SkASSERT(SkMask::kLCD16_Format == glyph.maskFormat()); |
| 1169 |
if (fPreBlend.isApplicable()) { |
| 1170 |
RGBToLcd16<true>(src, srcRB, glyph, imageBuffer, |
| 1171 |
fPreBlend.fR, fPreBlend.fG, fPreBlend.fB); |
| 1172 |
} else { |
| 1173 |
RGBToLcd16<false>(src, srcRB, glyph, imageBuffer, |
| 1174 |
fPreBlend.fR, fPreBlend.fG, fPreBlend.fB); |
| 1175 |
} |
| 1176 |
} |
| 1177 |
} |
| 1178 |
|
| 1179 |
namespace { |
| 1180 |
|
| 1181 |
class GDIGlyphbufferPointIter { |
| 1182 |
public: |
| 1183 |
GDIGlyphbufferPointIter(const uint8_t* glyphbuf, DWORD total_size) |
| 1184 |
: fHeaderIter(glyphbuf, total_size), fCurveIter(), fPointIter() |
| 1185 |
{ } |
| 1186 |
|
| 1187 |
POINTFX const * next() { |
| 1188 |
nextHeader: |
| 1189 |
if (!fCurveIter.isSet()) { |
| 1190 |
const TTPOLYGONHEADER* header = fHeaderIter.next(); |
| 1191 |
if (nullptr == header) { |
| 1192 |
return nullptr; |
| 1193 |
} |
| 1194 |
fCurveIter.set(header); |
| 1195 |
const TTPOLYCURVE* curve = fCurveIter.next(); |
| 1196 |
if (nullptr == curve) { |
| 1197 |
return nullptr; |
| 1198 |
} |
| 1199 |
fPointIter.set(curve); |
| 1200 |
return &header->pfxStart; |
| 1201 |
} |
| 1202 |
|
| 1203 |
const POINTFX* nextPoint = fPointIter.next(); |
| 1204 |
if (nullptr == nextPoint) { |
| 1205 |
const TTPOLYCURVE* curve = fCurveIter.next(); |
| 1206 |
if (nullptr == curve) { |
| 1207 |
fCurveIter.set(); |
| 1208 |
goto nextHeader; |
| 1209 |
} else { |
| 1210 |
fPointIter.set(curve); |
| 1211 |
} |
| 1212 |
nextPoint = fPointIter.next(); |
| 1213 |
} |
| 1214 |
return nextPoint; |
| 1215 |
} |
| 1216 |
|
| 1217 |
WORD currentCurveType() { |
| 1218 |
return fPointIter.fCurveType; |
| 1219 |
} |
| 1220 |
|
| 1221 |
private: |
| 1222 |
/** Iterates over all of the polygon headers in a glyphbuf. */ |
| 1223 |
class GDIPolygonHeaderIter { |
| 1224 |
public: |
| 1225 |
GDIPolygonHeaderIter(const uint8_t* glyphbuf, DWORD total_size) |
| 1226 |
: fCurPolygon(reinterpret_cast<const TTPOLYGONHEADER*>(glyphbuf)) |
| 1227 |
, fEndPolygon(SkTAddOffset<const TTPOLYGONHEADER>(glyphbuf, total_size)) |
| 1228 |
{ } |
| 1229 |
|
| 1230 |
const TTPOLYGONHEADER* next() { |
| 1231 |
if (fCurPolygon >= fEndPolygon) { |
| 1232 |
return nullptr; |
| 1233 |
} |
| 1234 |
const TTPOLYGONHEADER* thisPolygon = fCurPolygon; |
| 1235 |
fCurPolygon = SkTAddOffset<const TTPOLYGONHEADER>(fCurPolygon, fCurPolygon->cb); |
| 1236 |
return thisPolygon; |
| 1237 |
} |
| 1238 |
private: |
| 1239 |
const TTPOLYGONHEADER* fCurPolygon; |
| 1240 |
const TTPOLYGONHEADER* fEndPolygon; |
| 1241 |
}; |
| 1242 |
|
| 1243 |
/** Iterates over all of the polygon curves in a polygon header. */ |
| 1244 |
class GDIPolygonCurveIter { |
| 1245 |
public: |
| 1246 |
GDIPolygonCurveIter() : fCurCurve(nullptr), fEndCurve(nullptr) { } |
| 1247 |
|
| 1248 |
GDIPolygonCurveIter(const TTPOLYGONHEADER* curPolygon) |
| 1249 |
: fCurCurve(SkTAddOffset<const TTPOLYCURVE>(curPolygon, sizeof(TTPOLYGONHEADER))) |
| 1250 |
, fEndCurve(SkTAddOffset<const TTPOLYCURVE>(curPolygon, curPolygon->cb)) |
| 1251 |
{ } |
| 1252 |
|
| 1253 |
bool isSet() { return fCurCurve != nullptr; } |
| 1254 |
|
| 1255 |
void set(const TTPOLYGONHEADER* curPolygon) { |
| 1256 |
fCurCurve = SkTAddOffset<const TTPOLYCURVE>(curPolygon, sizeof(TTPOLYGONHEADER)); |
| 1257 |
fEndCurve = SkTAddOffset<const TTPOLYCURVE>(curPolygon, curPolygon->cb); |
| 1258 |
} |
| 1259 |
void set() { |
| 1260 |
fCurCurve = nullptr; |
| 1261 |
fEndCurve = nullptr; |
| 1262 |
} |
| 1263 |
|
| 1264 |
const TTPOLYCURVE* next() { |
| 1265 |
if (fCurCurve >= fEndCurve) { |
| 1266 |
return nullptr; |
| 1267 |
} |
| 1268 |
const TTPOLYCURVE* thisCurve = fCurCurve; |
| 1269 |
fCurCurve = SkTAddOffset<const TTPOLYCURVE>(fCurCurve, size_of_TTPOLYCURVE(*fCurCurve)); |
| 1270 |
return thisCurve; |
| 1271 |
} |
| 1272 |
private: |
| 1273 |
size_t size_of_TTPOLYCURVE(const TTPOLYCURVE& curve) { |
| 1274 |
return 2*sizeof(WORD) + curve.cpfx*sizeof(POINTFX); |
| 1275 |
} |
| 1276 |
const TTPOLYCURVE* fCurCurve; |
| 1277 |
const TTPOLYCURVE* fEndCurve; |
| 1278 |
}; |
| 1279 |
|
| 1280 |
/** Iterates over all of the polygon points in a polygon curve. */ |
| 1281 |
class GDIPolygonCurvePointIter { |
| 1282 |
public: |
| 1283 |
GDIPolygonCurvePointIter() : fCurveType(0), fCurPoint(nullptr), fEndPoint(nullptr) { } |
| 1284 |
|
| 1285 |
GDIPolygonCurvePointIter(const TTPOLYCURVE* curPolygon) |
| 1286 |
: fCurveType(curPolygon->wType) |
| 1287 |
, fCurPoint(&curPolygon->apfx[0]) |
| 1288 |
, fEndPoint(&curPolygon->apfx[curPolygon->cpfx]) |
| 1289 |
{ } |
| 1290 |
|
| 1291 |
bool isSet() { return fCurPoint != nullptr; } |
| 1292 |
|
| 1293 |
void set(const TTPOLYCURVE* curPolygon) { |
| 1294 |
fCurveType = curPolygon->wType; |
| 1295 |
fCurPoint = &curPolygon->apfx[0]; |
| 1296 |
fEndPoint = &curPolygon->apfx[curPolygon->cpfx]; |
| 1297 |
} |
| 1298 |
void set() { |
| 1299 |
fCurPoint = nullptr; |
| 1300 |
fEndPoint = nullptr; |
| 1301 |
} |
| 1302 |
|
| 1303 |
const POINTFX* next() { |
| 1304 |
if (fCurPoint >= fEndPoint) { |
| 1305 |
return nullptr; |
| 1306 |
} |
| 1307 |
const POINTFX* thisPoint = fCurPoint; |
| 1308 |
++fCurPoint; |
| 1309 |
return thisPoint; |
| 1310 |
} |
| 1311 |
|
| 1312 |
WORD fCurveType; |
| 1313 |
private: |
| 1314 |
const POINTFX* fCurPoint; |
| 1315 |
const POINTFX* fEndPoint; |
| 1316 |
}; |
| 1317 |
|
| 1318 |
GDIPolygonHeaderIter fHeaderIter; |
| 1319 |
GDIPolygonCurveIter fCurveIter; |
| 1320 |
GDIPolygonCurvePointIter fPointIter; |
| 1321 |
}; |
| 1322 |
|
| 1323 |
class SkGDIGeometrySink { |
| 1324 |
SkPathBuilder* fBuilder; |
| 1325 |
bool fStarted = false; |
| 1326 |
POINTFX fCurrent; |
| 1327 |
|
| 1328 |
void goingTo(const POINTFX pt) { |
| 1329 |
if (!fStarted) { |
| 1330 |
fStarted = true; |
| 1331 |
fBuilder->moveTo( SkFIXEDToScalar(fCurrent.x), |
| 1332 |
-SkFIXEDToScalar(fCurrent.y)); |
| 1333 |
} |
| 1334 |
fCurrent = pt; |
| 1335 |
} |
| 1336 |
|
| 1337 |
bool currentIsNot(const POINTFX pt) { |
| 1338 |
return fCurrent.x.value != pt.x.value || fCurrent.x.fract != pt.x.fract || |
| 1339 |
fCurrent.y.value != pt.y.value || fCurrent.y.fract != pt.y.fract; |
| 1340 |
} |
| 1341 |
|
| 1342 |
public: |
| 1343 |
SkGDIGeometrySink(SkPathBuilder* builder) : fBuilder(builder) {} |
| 1344 |
void process(const uint8_t* glyphbuf, DWORD total_size); |
| 1345 |
|
| 1346 |
/** It is possible for the hinted and unhinted versions of the same path to have |
| 1347 |
* a different number of points due to GDI's handling of flipped points. |
| 1348 |
* If this is detected, this will return false. |
| 1349 |
*/ |
| 1350 |
bool process(const uint8_t* glyphbuf, DWORD total_size, GDIGlyphbufferPointIter hintedYs); |
| 1351 |
}; |
| 1352 |
|
| 1353 |
void SkGDIGeometrySink::process(const uint8_t* glyphbuf, DWORD total_size) { |
| 1354 |
const uint8_t* cur_glyph = glyphbuf; |
| 1355 |
const uint8_t* end_glyph = glyphbuf + total_size; |
| 1356 |
|
| 1357 |
while (cur_glyph < end_glyph) { |
| 1358 |
const TTPOLYGONHEADER* th = (const TTPOLYGONHEADER*)cur_glyph; |
| 1359 |
|
| 1360 |
const uint8_t* end_poly = cur_glyph + th->cb; |
| 1361 |
const uint8_t* cur_poly = cur_glyph + sizeof(TTPOLYGONHEADER); |
| 1362 |
|
| 1363 |
fStarted = false; |
| 1364 |
fCurrent = th->pfxStart; |
| 1365 |
|
| 1366 |
while (cur_poly < end_poly) { |
| 1367 |
const TTPOLYCURVE* pc = (const TTPOLYCURVE*)cur_poly; |
| 1368 |
const POINTFX* apfx = pc->apfx; |
| 1369 |
const WORD cpfx = pc->cpfx; |
| 1370 |
|
| 1371 |
if (pc->wType == TT_PRIM_LINE) { |
| 1372 |
for (uint16_t i = 0; i < cpfx; i++) { |
| 1373 |
POINTFX pnt_b = apfx[i]; |
| 1374 |
if (this->currentIsNot(pnt_b)) { |
| 1375 |
this->goingTo(pnt_b); |
| 1376 |
fBuilder->lineTo( SkFIXEDToScalar(pnt_b.x), |
| 1377 |
-SkFIXEDToScalar(pnt_b.y)); |
| 1378 |
} |
| 1379 |
} |
| 1380 |
} |
| 1381 |
|
| 1382 |
if (pc->wType == TT_PRIM_QSPLINE) { |
| 1383 |
for (uint16_t u = 0; u < cpfx - 1; u++) { // Walk through points in spline |
| 1384 |
POINTFX pnt_b = apfx[u]; // B is always the current point |
| 1385 |
POINTFX pnt_c = apfx[u+1]; |
| 1386 |
|
| 1387 |
if (u < cpfx - 2) { // If not on last spline, compute C |
| 1388 |
pnt_c.x = SkFixedToFIXED(SkFixedAve(SkFIXEDToFixed(pnt_b.x), |
| 1389 |
SkFIXEDToFixed(pnt_c.x))); |
| 1390 |
pnt_c.y = SkFixedToFIXED(SkFixedAve(SkFIXEDToFixed(pnt_b.y), |
| 1391 |
SkFIXEDToFixed(pnt_c.y))); |
| 1392 |
} |
| 1393 |
|
| 1394 |
|
| 1395 |
if (this->currentIsNot(pnt_b) || this->currentIsNot(pnt_c)) { |
| 1396 |
this->goingTo(pnt_c); |
| 1397 |
fBuilder->quadTo( SkFIXEDToScalar(pnt_b.x), |
| 1398 |
-SkFIXEDToScalar(pnt_b.y), |
| 1399 |
SkFIXEDToScalar(pnt_c.x), |
| 1400 |
-SkFIXEDToScalar(pnt_c.y)); |
| 1401 |
} |
| 1402 |
} |
| 1403 |
} |
| 1404 |
|
| 1405 |
// Advance past this TTPOLYCURVE. |
| 1406 |
cur_poly += sizeof(WORD) * 2 + sizeof(POINTFX) * cpfx; |
| 1407 |
} |
| 1408 |
cur_glyph += th->cb; |
| 1409 |
if (this->fStarted) { |
| 1410 |
fBuilder->close(); |
| 1411 |
} |
| 1412 |
} |
| 1413 |
} |
| 1414 |
|
| 1415 |
#define move_next_expected_hinted_point(iter, pElem) do {\ |
| 1416 |
pElem = iter.next(); \ |
| 1417 |
if (nullptr == pElem) return false; \ |
| 1418 |
} while(0) |
| 1419 |
|
| 1420 |
bool SkGDIGeometrySink::process(const uint8_t* glyphbuf, DWORD total_size, |
| 1421 |
GDIGlyphbufferPointIter hintedYs) { |
| 1422 |
const uint8_t* cur_glyph = glyphbuf; |
| 1423 |
const uint8_t* end_glyph = glyphbuf + total_size; |
| 1424 |
|
| 1425 |
POINTFX const * hintedPoint; |
| 1426 |
|
| 1427 |
while (cur_glyph < end_glyph) { |
| 1428 |
const TTPOLYGONHEADER* th = (const TTPOLYGONHEADER*)cur_glyph; |
| 1429 |
|
| 1430 |
const uint8_t* end_poly = cur_glyph + th->cb; |
| 1431 |
const uint8_t* cur_poly = cur_glyph + sizeof(TTPOLYGONHEADER); |
| 1432 |
|
| 1433 |
move_next_expected_hinted_point(hintedYs, hintedPoint); |
| 1434 |
fStarted = false; |
| 1435 |
fCurrent = {th->pfxStart.x, hintedPoint->y}; |
| 1436 |
|
| 1437 |
while (cur_poly < end_poly) { |
| 1438 |
const TTPOLYCURVE* pc = (const TTPOLYCURVE*)cur_poly; |
| 1439 |
const POINTFX* apfx = pc->apfx; |
| 1440 |
const WORD cpfx = pc->cpfx; |
| 1441 |
|
| 1442 |
if (pc->wType == TT_PRIM_LINE) { |
| 1443 |
for (uint16_t i = 0; i < cpfx; i++) { |
| 1444 |
move_next_expected_hinted_point(hintedYs, hintedPoint); |
| 1445 |
POINTFX pnt_b = {apfx[i].x, hintedPoint->y}; |
| 1446 |
if (this->currentIsNot(pnt_b)) { |
| 1447 |
this->goingTo(pnt_b); |
| 1448 |
fBuilder->lineTo( SkFIXEDToScalar(pnt_b.x), |
| 1449 |
-SkFIXEDToScalar(pnt_b.y)); |
| 1450 |
} |
| 1451 |
} |
| 1452 |
} |
| 1453 |
|
| 1454 |
if (pc->wType == TT_PRIM_QSPLINE) { |
| 1455 |
POINTFX currentPoint = apfx[0]; |
| 1456 |
move_next_expected_hinted_point(hintedYs, hintedPoint); |
| 1457 |
// only take the hinted y if it wasn't flipped |
| 1458 |
if (hintedYs.currentCurveType() == TT_PRIM_QSPLINE) { |
| 1459 |
currentPoint.y = hintedPoint->y; |
| 1460 |
} |
| 1461 |
for (uint16_t u = 0; u < cpfx - 1; u++) { // Walk through points in spline |
| 1462 |
POINTFX pnt_b = currentPoint;//pc->apfx[u]; // B is always the current point |
| 1463 |
POINTFX pnt_c = apfx[u+1]; |
| 1464 |
move_next_expected_hinted_point(hintedYs, hintedPoint); |
| 1465 |
// only take the hinted y if it wasn't flipped |
| 1466 |
if (hintedYs.currentCurveType() == TT_PRIM_QSPLINE) { |
| 1467 |
pnt_c.y = hintedPoint->y; |
| 1468 |
} |
| 1469 |
currentPoint.x = pnt_c.x; |
| 1470 |
currentPoint.y = pnt_c.y; |
| 1471 |
|
| 1472 |
if (u < cpfx - 2) { // If not on last spline, compute C |
| 1473 |
pnt_c.x = SkFixedToFIXED(SkFixedAve(SkFIXEDToFixed(pnt_b.x), |
| 1474 |
SkFIXEDToFixed(pnt_c.x))); |
| 1475 |
pnt_c.y = SkFixedToFIXED(SkFixedAve(SkFIXEDToFixed(pnt_b.y), |
| 1476 |
SkFIXEDToFixed(pnt_c.y))); |
| 1477 |
} |
| 1478 |
|
| 1479 |
if (this->currentIsNot(pnt_b) || this->currentIsNot(pnt_c)) { |
| 1480 |
this->goingTo(pnt_c); |
| 1481 |
fBuilder->quadTo( SkFIXEDToScalar(pnt_b.x), |
| 1482 |
-SkFIXEDToScalar(pnt_b.y), |
| 1483 |
SkFIXEDToScalar(pnt_c.x), |
| 1484 |
-SkFIXEDToScalar(pnt_c.y)); |
| 1485 |
} |
| 1486 |
} |
| 1487 |
} |
| 1488 |
|
| 1489 |
// Advance past this TTPOLYCURVE. |
| 1490 |
cur_poly += sizeof(WORD) * 2 + sizeof(POINTFX) * cpfx; |
| 1491 |
} |
| 1492 |
cur_glyph += th->cb; |
| 1493 |
if (this->fStarted) { |
| 1494 |
fBuilder->close(); |
| 1495 |
} |
| 1496 |
} |
| 1497 |
return true; |
| 1498 |
} |
| 1499 |
} // namespace |
| 1500 |
|
| 1501 |
DWORD SkScalerContext_GDI::getGDIGlyphPath(SkGlyphID glyph, UINT flags, |
| 1502 |
AutoSTMalloc<BUFFERSIZE, uint8_t>* glyphbuf) |
| 1503 |
{ |
| 1504 |
GLYPHMETRICS gm; |
| 1505 |
|
| 1506 |
DWORD total_size = GetGlyphOutlineW(fDDC, glyph, flags, &gm, BUFFERSIZE, glyphbuf->get(), &fMat22); |
| 1507 |
// Sometimes GetGlyphOutlineW returns a number larger than BUFFERSIZE even if BUFFERSIZE > 0. |
| 1508 |
// It has been verified that this does not involve a buffer overrun. |
| 1509 |
if (GDI_ERROR == total_size || total_size > BUFFERSIZE) { |
| 1510 |
// GDI_ERROR because the BUFFERSIZE was too small, or because the data was not accessible. |
| 1511 |
// When the data is not accessable GetGlyphOutlineW fails rather quickly, |
| 1512 |
// so just try to get the size. If that fails then ensure the data is accessible. |
| 1513 |
total_size = GetGlyphOutlineW(fDDC, glyph, flags, &gm, 0, nullptr, &fMat22); |
| 1514 |
if (GDI_ERROR == total_size) { |
| 1515 |
LogFontTypeface::EnsureAccessible(this->getTypeface()); |
| 1516 |
total_size = GetGlyphOutlineW(fDDC, glyph, flags, &gm, 0, nullptr, &fMat22); |
| 1517 |
if (GDI_ERROR == total_size) { |
| 1518 |
// GetGlyphOutlineW is known to fail for some characters, such as spaces. |
| 1519 |
// In these cases, just return that the glyph does not have a shape. |
| 1520 |
return 0; |
| 1521 |
} |
| 1522 |
} |
| 1523 |
|
| 1524 |
glyphbuf->reset(total_size); |
| 1525 |
|
| 1526 |
DWORD ret = GetGlyphOutlineW(fDDC, glyph, flags, &gm, total_size, glyphbuf->get(), &fMat22); |
| 1527 |
if (GDI_ERROR == ret) { |
| 1528 |
LogFontTypeface::EnsureAccessible(this->getTypeface()); |
| 1529 |
ret = GetGlyphOutlineW(fDDC, glyph, flags, &gm, total_size, glyphbuf->get(), &fMat22); |
| 1530 |
if (GDI_ERROR == ret) { |
| 1531 |
SkASSERT(false); |
| 1532 |
return 0; |
| 1533 |
} |
| 1534 |
} |
| 1535 |
} |
| 1536 |
return total_size; |
| 1537 |
} |
| 1538 |
|
| 1539 |
std::optional<SkScalerContext::GeneratedPath> |
| 1540 |
SkScalerContext_GDI::generatePath(const SkGlyph& glyph) { |
| 1541 |
SkASSERT(fDDC); |
| 1542 |
|
| 1543 |
SkGlyphID glyphID = glyph.getGlyphID(); |
| 1544 |
|
| 1545 |
// Out of all the fonts on a typical Windows box, |
| 1546 |
// 25% of glyphs require more than 2KB. |
| 1547 |
// 1% of glyphs require more than 4KB. |
| 1548 |
// 0.01% of glyphs require more than 8KB. |
| 1549 |
// 8KB is less than 1% of the normal 1MB stack on Windows. |
| 1550 |
// Note that some web fonts glyphs require more than 20KB. |
| 1551 |
//static const DWORD BUFFERSIZE = (1 << 13); |
| 1552 |
|
| 1553 |
//GDI only uses hinted outlines when axis aligned. |
| 1554 |
UINT format = GGO_NATIVE | GGO_GLYPH_INDEX; |
| 1555 |
if (fRec.getHinting() == SkFontHinting::kNone || fRec.getHinting() == SkFontHinting::kSlight){ |
| 1556 |
format |= GGO_UNHINTED; |
| 1557 |
} |
| 1558 |
AutoSTMalloc<BUFFERSIZE, uint8_t> glyphbuf(BUFFERSIZE); |
| 1559 |
DWORD total_size = getGDIGlyphPath(glyphID, format, &glyphbuf); |
| 1560 |
if (0 == total_size) { |
| 1561 |
return {}; |
| 1562 |
} |
| 1563 |
|
| 1564 |
SkPathBuilder builder; |
| 1565 |
if (fRec.getHinting() != SkFontHinting::kSlight) { |
| 1566 |
SkGDIGeometrySink sink(&builder); |
| 1567 |
sink.process(glyphbuf, total_size); |
| 1568 |
} else { |
| 1569 |
AutoSTMalloc<BUFFERSIZE, uint8_t> hintedGlyphbuf(BUFFERSIZE); |
| 1570 |
//GDI only uses hinted outlines when axis aligned. |
| 1571 |
DWORD hinted_total_size = getGDIGlyphPath(glyphID, GGO_NATIVE | GGO_GLYPH_INDEX, |
| 1572 |
&hintedGlyphbuf); |
| 1573 |
if (0 == hinted_total_size) { |
| 1574 |
return {}; |
| 1575 |
} |
| 1576 |
|
| 1577 |
SkGDIGeometrySink sinkXBufYIter(&builder); |
| 1578 |
if (!sinkXBufYIter.process(glyphbuf, total_size, |
| 1579 |
GDIGlyphbufferPointIter(hintedGlyphbuf, hinted_total_size))) |
| 1580 |
{ |
| 1581 |
// Both path and sinkXBufYIter are in the state they were in at the time of failure. |
| 1582 |
builder.reset(); |
| 1583 |
SkGDIGeometrySink sink(&builder); |
| 1584 |
sink.process(glyphbuf, total_size); |
| 1585 |
} |
| 1586 |
} |
| 1587 |
return {{builder.detach(), false}}; |
| 1588 |
} |
| 1589 |
|
| 1590 |
static void logfont_for_name(const char* familyName, LOGFONT* lf) { |
| 1591 |
sk_bzero(lf, sizeof(LOGFONT)); |
| 1592 |
#ifdef UNICODE |
| 1593 |
// Get the buffer size needed first. |
| 1594 |
size_t str_len = ::MultiByteToWideChar(CP_UTF8, 0, familyName, |
| 1595 |
-1, nullptr, 0); |
| 1596 |
// Allocate a buffer (str_len already has terminating null |
| 1597 |
// accounted for). |
| 1598 |
wchar_t *wideFamilyName = new wchar_t[str_len]; |
| 1599 |
// Now actually convert the string. |
| 1600 |
::MultiByteToWideChar(CP_UTF8, 0, familyName, -1, |
| 1601 |
wideFamilyName, str_len); |
| 1602 |
::wcsncpy(lf->lfFaceName, wideFamilyName, LF_FACESIZE - 1); |
| 1603 |
delete [] wideFamilyName; |
| 1604 |
lf->lfFaceName[LF_FACESIZE-1] = L'\0'; |
| 1605 |
#else |
| 1606 |
::strncpy(lf->lfFaceName, familyName, LF_FACESIZE - 1); |
| 1607 |
lf->lfFaceName[LF_FACESIZE - 1] = '\0'; |
| 1608 |
#endif |
| 1609 |
} |
| 1610 |
|
| 1611 |
void LogFontTypeface::onGetFamilyName(SkString* familyName) const { |
| 1612 |
// Get the actual name of the typeface. The logfont may not know this. |
| 1613 |
SkAutoHDC hdc(fLogFont); |
| 1614 |
dcfontname_to_skstring(hdc, fLogFont, familyName); |
| 1615 |
} |
| 1616 |
|
| 1617 |
void LogFontTypeface::onGetFontDescriptor(SkFontDescriptor* desc, |
| 1618 |
bool* isLocalStream) const { |
| 1619 |
SkString familyName; |
| 1620 |
this->onGetFamilyName(&familyName); |
| 1621 |
desc->setFamilyName(familyName.c_str()); |
| 1622 |
desc->setStyle(this->fontStyle()); |
| 1623 |
*isLocalStream = this->fSerializeAsStream; |
| 1624 |
} |
| 1625 |
|
| 1626 |
void LogFontTypeface::getGlyphToUnicodeMap(SkSpan<SkUnichar> dstArray) const { |
| 1627 |
SkAutoHDC hdc(fLogFont); |
| 1628 |
unsigned int glyphCount = calculateGlyphCount(hdc, fLogFont); |
| 1629 |
populate_glyph_to_unicode(hdc, std::min<unsigned>(glyphCount, dstArray.size()), dstArray); |
| 1630 |
} |
| 1631 |
|
| 1632 |
std::unique_ptr<SkAdvancedTypefaceMetrics> LogFontTypeface::onGetAdvancedMetrics() const { |
| 1633 |
LOGFONT lf = fLogFont; |
| 1634 |
std::unique_ptr<SkAdvancedTypefaceMetrics> info(nullptr); |
| 1635 |
|
| 1636 |
// The design HFONT must be destroyed after the HDC |
| 1637 |
using HFONT_T = typename std::remove_pointer<HFONT>::type; |
| 1638 |
std::unique_ptr<HFONT_T, SkFunctionObject<DeleteObject>> designFont; |
| 1639 |
SkAutoHDC hdc(lf); |
| 1640 |
|
| 1641 |
const char stem_chars[] = {'i', 'I', '!', '1'}; |
| 1642 |
int16_t min_width; |
| 1643 |
unsigned glyphCount; |
| 1644 |
|
| 1645 |
// To request design units, create a logical font whose height is specified |
| 1646 |
// as unitsPerEm. |
| 1647 |
OUTLINETEXTMETRIC otm; |
| 1648 |
unsigned int otmRet = GetOutlineTextMetrics(hdc, sizeof(otm), &otm); |
| 1649 |
if (0 == otmRet) { |
| 1650 |
call_ensure_accessible(lf); |
| 1651 |
otmRet = GetOutlineTextMetrics(hdc, sizeof(otm), &otm); |
| 1652 |
} |
| 1653 |
if (!otmRet || !GetTextFace(hdc, LF_FACESIZE, lf.lfFaceName)) { |
| 1654 |
return info; |
| 1655 |
} |
| 1656 |
lf.lfHeight = -SkToS32(otm.otmEMSquare); |
| 1657 |
designFont.reset(CreateFontIndirect(&lf)); |
| 1658 |
SelectObject(hdc, designFont.get()); |
| 1659 |
if (!GetOutlineTextMetrics(hdc, sizeof(otm), &otm)) { |
| 1660 |
return info; |
| 1661 |
} |
| 1662 |
glyphCount = calculateGlyphCount(hdc, fLogFont); |
| 1663 |
|
| 1664 |
info.reset(new SkAdvancedTypefaceMetrics); |
| 1665 |
|
| 1666 |
SkOTTableOS2_V4::Type fsType; |
| 1667 |
if (sizeof(fsType) == this->getTableData(SkTEndian_SwapBE32(SkOTTableOS2::TAG), |
| 1668 |
offsetof(SkOTTableOS2_V4, fsType), |
| 1669 |
sizeof(fsType), |
| 1670 |
&fsType)) { |
| 1671 |
SkOTUtils::SetAdvancedTypefaceFlags(fsType, info.get()); |
| 1672 |
} else { |
| 1673 |
// If bit 1 is set, the font may not be embedded in a document. |
| 1674 |
// If bit 1 is clear, the font can be embedded. |
| 1675 |
// If bit 2 is set, the embedding is read-only. |
| 1676 |
if (otm.otmfsType & 0x1) { |
| 1677 |
info->fFlags |= SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag; |
| 1678 |
} |
| 1679 |
} |
| 1680 |
|
| 1681 |
if (glyphCount == 0 || (otm.otmTextMetrics.tmPitchAndFamily & TMPF_TRUETYPE) == 0) { |
| 1682 |
return info; |
| 1683 |
} |
| 1684 |
info->fType = SkAdvancedTypefaceMetrics::kTrueType_Font; |
| 1685 |
|
| 1686 |
// If this bit is clear the font is a fixed pitch font. |
| 1687 |
if (!(otm.otmTextMetrics.tmPitchAndFamily & TMPF_FIXED_PITCH)) { |
| 1688 |
info->fStyle |= SkAdvancedTypefaceMetrics::kFixedPitch_Style; |
| 1689 |
} |
| 1690 |
if (otm.otmTextMetrics.tmItalic) { |
| 1691 |
info->fStyle |= SkAdvancedTypefaceMetrics::kItalic_Style; |
| 1692 |
} |
| 1693 |
if (otm.otmTextMetrics.tmPitchAndFamily & FF_ROMAN) { |
| 1694 |
info->fStyle |= SkAdvancedTypefaceMetrics::kSerif_Style; |
| 1695 |
} else if (otm.otmTextMetrics.tmPitchAndFamily & FF_SCRIPT) { |
| 1696 |
info->fStyle |= SkAdvancedTypefaceMetrics::kScript_Style; |
| 1697 |
} |
| 1698 |
|
| 1699 |
// The main italic angle of the font, in tenths of a degree counterclockwise |
| 1700 |
// from vertical. |
| 1701 |
info->fItalicAngle = otm.otmItalicAngle / 10; |
| 1702 |
info->fAscent = SkToS16(otm.otmTextMetrics.tmAscent); |
| 1703 |
info->fDescent = SkToS16(-otm.otmTextMetrics.tmDescent); |
| 1704 |
// TODO(ctguil): Use alternate cap height calculation. |
| 1705 |
// MSDN says otmsCapEmHeight is not support but it is returning a value on |
| 1706 |
// my Win7 box. |
| 1707 |
info->fCapHeight = otm.otmsCapEmHeight; |
| 1708 |
info->fBBox = |
| 1709 |
SkIRect::MakeLTRB(otm.otmrcFontBox.left, otm.otmrcFontBox.top, |
| 1710 |
otm.otmrcFontBox.right, otm.otmrcFontBox.bottom); |
| 1711 |
|
| 1712 |
// Figure out a good guess for StemV - Min width of i, I, !, 1. |
| 1713 |
// This probably isn't very good with an italic font. |
| 1714 |
min_width = SHRT_MAX; |
| 1715 |
info->fStemV = 0; |
| 1716 |
for (size_t i = 0; i < std::size(stem_chars); i++) { |
| 1717 |
ABC abcWidths; |
| 1718 |
if (GetCharABCWidths(hdc, stem_chars[i], stem_chars[i], &abcWidths)) { |
| 1719 |
int16_t width = abcWidths.abcB; |
| 1720 |
if (width > 0 && width < min_width) { |
| 1721 |
min_width = width; |
| 1722 |
info->fStemV = min_width; |
| 1723 |
} |
| 1724 |
} |
| 1725 |
} |
| 1726 |
|
| 1727 |
return info; |
| 1728 |
} |
| 1729 |
|
| 1730 |
//Placeholder representation of a Base64 encoded GUID from create_unique_font_name. |
| 1731 |
#define BASE64_GUID_ID "XXXXXXXXXXXXXXXXXXXXXXXX" |
| 1732 |
//Length of GUID representation from create_id, including nullptr terminator. |
| 1733 |
#define BASE64_GUID_ID_LEN std::size(BASE64_GUID_ID) |
| 1734 |
|
| 1735 |
static_assert(BASE64_GUID_ID_LEN < LF_FACESIZE, "GUID_longer_than_facesize"); |
| 1736 |
|
| 1737 |
/** |
| 1738 |
NameID 6 Postscript names cannot have the character '/'. |
| 1739 |
It would be easier to hex encode the GUID, but that is 32 bytes, |
| 1740 |
and many systems have issues with names longer than 28 bytes. |
| 1741 |
The following need not be any standard base64 encoding. |
| 1742 |
The encoded value is never decoded. |
| 1743 |
*/ |
| 1744 |
static const char postscript_safe_base64_encode[] = |
| 1745 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 1746 |
"abcdefghijklmnopqrstuvwxyz" |
| 1747 |
"0123456789-_="; |
| 1748 |
|
| 1749 |
/** |
| 1750 |
Formats a GUID into Base64 and places it into buffer. |
| 1751 |
buffer should have space for at least BASE64_GUID_ID_LEN characters. |
| 1752 |
The string will always be null terminated. |
| 1753 |
XXXXXXXXXXXXXXXXXXXXXXXX0 |
| 1754 |
*/ |
| 1755 |
static void format_guid_b64(const GUID& guid, char* buffer, size_t bufferSize) { |
| 1756 |
SkASSERT(bufferSize >= BASE64_GUID_ID_LEN); |
| 1757 |
size_t written = SkBase64::Encode(&guid, sizeof(guid), buffer, postscript_safe_base64_encode); |
| 1758 |
SkASSERT(written < LF_FACESIZE); |
| 1759 |
buffer[written] = '\0'; |
| 1760 |
} |
| 1761 |
|
| 1762 |
/** |
| 1763 |
Creates a Base64 encoded GUID and places it into buffer. |
| 1764 |
buffer should have space for at least BASE64_GUID_ID_LEN characters. |
| 1765 |
The string will always be null terminated. |
| 1766 |
XXXXXXXXXXXXXXXXXXXXXXXX0 |
| 1767 |
*/ |
| 1768 |
static HRESULT create_unique_font_name(char* buffer, size_t bufferSize) { |
| 1769 |
GUID guid = {}; |
| 1770 |
if (FAILED(CoCreateGuid(&guid))) { |
| 1771 |
return E_UNEXPECTED; |
| 1772 |
} |
| 1773 |
format_guid_b64(guid, buffer, bufferSize); |
| 1774 |
|
| 1775 |
return S_OK; |
| 1776 |
} |
| 1777 |
|
| 1778 |
/** |
| 1779 |
Introduces a font to GDI. On failure will return nullptr. The returned handle |
| 1780 |
should eventually be passed to RemoveFontMemResourceEx. |
| 1781 |
*/ |
| 1782 |
static HANDLE activate_font(const SkData* fontData) { |
| 1783 |
DWORD numFonts = 0; |
| 1784 |
//AddFontMemResourceEx just copies the data, but does not specify const. |
| 1785 |
HANDLE fontHandle = AddFontMemResourceEx(const_cast<void*>(fontData->data()), |
| 1786 |
static_cast<DWORD>(fontData->size()), |
| 1787 |
nullptr, |
| 1788 |
&numFonts); |
| 1789 |
|
| 1790 |
if (fontHandle != nullptr && numFonts < 1) { |
| 1791 |
RemoveFontMemResourceEx(fontHandle); |
| 1792 |
return nullptr; |
| 1793 |
} |
| 1794 |
|
| 1795 |
return fontHandle; |
| 1796 |
} |
| 1797 |
|
| 1798 |
// Does not affect ownership of stream. |
| 1799 |
static sk_sp<SkTypeface> create_from_stream(std::unique_ptr<SkStreamAsset> stream) { |
| 1800 |
// Create a unique and unpredictable font name. |
| 1801 |
// Avoids collisions and access from CSS. |
| 1802 |
char familyName[BASE64_GUID_ID_LEN]; |
| 1803 |
const int familyNameSize = std::size(familyName); |
| 1804 |
if (FAILED(create_unique_font_name(familyName, familyNameSize))) { |
| 1805 |
return nullptr; |
| 1806 |
} |
| 1807 |
|
| 1808 |
// Change the name of the font. |
| 1809 |
sk_sp<SkData> rewrittenFontData(SkOTUtils::RenameFont(stream.get(), familyName, familyNameSize-1)); |
| 1810 |
if (nullptr == rewrittenFontData.get()) { |
| 1811 |
return nullptr; |
| 1812 |
} |
| 1813 |
|
| 1814 |
// Register the font with GDI. |
| 1815 |
HANDLE fontReference = activate_font(rewrittenFontData.get()); |
| 1816 |
if (nullptr == fontReference) { |
| 1817 |
return nullptr; |
| 1818 |
} |
| 1819 |
|
| 1820 |
// Create the typeface. |
| 1821 |
LOGFONT lf; |
| 1822 |
logfont_for_name(familyName, &lf); |
| 1823 |
|
| 1824 |
return sk_sp<SkTypeface>(SkCreateFontMemResourceTypefaceFromLOGFONT(lf, fontReference)); |
| 1825 |
} |
| 1826 |
|
| 1827 |
std::unique_ptr<SkStreamAsset> LogFontTypeface::onOpenStream(int* ttcIndex) const { |
| 1828 |
*ttcIndex = 0; |
| 1829 |
|
| 1830 |
const DWORD kTTCTag = SkEndian_SwapBE32(SkSetFourByteTag('t', 't', 'c', 'f')); |
| 1831 |
LOGFONT lf = fLogFont; |
| 1832 |
|
| 1833 |
SkAutoHDC hdc(lf); |
| 1834 |
|
| 1835 |
std::unique_ptr<SkStreamAsset> stream; |
| 1836 |
DWORD tables[2] = {kTTCTag, 0}; |
| 1837 |
for (size_t i = 0; i < std::size(tables); i++) { |
| 1838 |
DWORD bufferSize = GetFontData(hdc, tables[i], 0, nullptr, 0); |
| 1839 |
if (bufferSize == GDI_ERROR) { |
| 1840 |
call_ensure_accessible(lf); |
| 1841 |
bufferSize = GetFontData(hdc, tables[i], 0, nullptr, 0); |
| 1842 |
} |
| 1843 |
if (bufferSize != GDI_ERROR) { |
| 1844 |
sk_sp<SkData> streamData = SkData::MakeUninitialized(bufferSize); |
| 1845 |
if (GetFontData(hdc, tables[i], 0, streamData->writable_data(), bufferSize)) { |
| 1846 |
stream.reset(new SkMemoryStream(std::move(streamData))); |
| 1847 |
break; |
| 1848 |
} else { |
| 1849 |
stream.reset(); |
| 1850 |
} |
| 1851 |
} |
| 1852 |
} |
| 1853 |
return stream; |
| 1854 |
} |
| 1855 |
|
| 1856 |
sk_sp<SkTypeface> LogFontTypeface::onMakeClone(const SkFontArguments& args) const { |
| 1857 |
return sk_ref_sp(this); |
| 1858 |
} |
| 1859 |
|
| 1860 |
static void bmpCharsToGlyphs(HDC hdc, const WCHAR* bmpChars, int count, uint16_t* glyphs, |
| 1861 |
bool Ox1FHack) |
| 1862 |
{ |
| 1863 |
// Type1 fonts fail with uniscribe API. Use GetGlyphIndices for plane 0. |
| 1864 |
|
| 1865 |
/** Real documentation for GetGlyphIndicesW: |
| 1866 |
* |
| 1867 |
* When GGI_MARK_NONEXISTING_GLYPHS is not specified and a character does not map to a |
| 1868 |
* glyph, then the 'default character's glyph is returned instead. The 'default character' |
| 1869 |
* is available in fTM.tmDefaultChar. FON fonts have a default character, and there exists |
| 1870 |
* a usDefaultChar in the 'OS/2' table, version 2 and later. If there is no |
| 1871 |
* 'default character' specified by the font, then often the first character found is used. |
| 1872 |
* |
| 1873 |
* When GGI_MARK_NONEXISTING_GLYPHS is specified and a character does not map to a glyph, |
| 1874 |
* then the glyph 0xFFFF is used. In Windows XP and earlier, Bitmap/Vector FON usually use |
| 1875 |
* glyph 0x1F instead ('Terminal' appears to be special, returning 0xFFFF). |
| 1876 |
* Type1 PFM/PFB, TT, OT TT, OT CFF all appear to use 0xFFFF, even on XP. |
| 1877 |
*/ |
| 1878 |
DWORD result = GetGlyphIndicesW(hdc, bmpChars, count, glyphs, GGI_MARK_NONEXISTING_GLYPHS); |
| 1879 |
if (GDI_ERROR == result) { |
| 1880 |
for (int i = 0; i < count; ++i) { |
| 1881 |
glyphs[i] = 0; |
| 1882 |
} |
| 1883 |
return; |
| 1884 |
} |
| 1885 |
|
| 1886 |
if (Ox1FHack) { |
| 1887 |
for (int i = 0; i < count; ++i) { |
| 1888 |
if (0xFFFF == glyphs[i] || 0x1F == glyphs[i]) { |
| 1889 |
glyphs[i] = 0; |
| 1890 |
} |
| 1891 |
} |
| 1892 |
} else { |
| 1893 |
for (int i = 0; i < count; ++i) { |
| 1894 |
if (0xFFFF == glyphs[i]){ |
| 1895 |
glyphs[i] = 0; |
| 1896 |
} |
| 1897 |
} |
| 1898 |
} |
| 1899 |
} |
| 1900 |
|
| 1901 |
static uint16_t nonBmpCharToGlyph(HDC hdc, SCRIPT_CACHE* scriptCache, const WCHAR utf16[2]) { |
| 1902 |
uint16_t index = 0; |
| 1903 |
// Use uniscribe to detemine glyph index for non-BMP characters. |
| 1904 |
static const int numWCHAR = 2; |
| 1905 |
static const int maxItems = 2; |
| 1906 |
// MSDN states that this can be nullptr, but some things don't work then. |
| 1907 |
SCRIPT_CONTROL scriptControl; |
| 1908 |
memset(&scriptControl, 0, sizeof(scriptControl)); |
| 1909 |
// Add extra item to SCRIPT_ITEM to work around a bug (now documented). |
| 1910 |
// https://bugzilla.mozilla.org/show_bug.cgi?id=366643 |
| 1911 |
SCRIPT_ITEM si[maxItems + 1]; |
| 1912 |
int numItems; |
| 1913 |
HRZM(ScriptItemize(utf16, numWCHAR, maxItems, &scriptControl, nullptr, si, &numItems), |
| 1914 |
"Could not itemize character."); |
| 1915 |
|
| 1916 |
// Sometimes ScriptShape cannot find a glyph for a non-BMP and returns 2 space glyphs. |
| 1917 |
static const int maxGlyphs = 2; |
| 1918 |
SCRIPT_VISATTR vsa[maxGlyphs]; |
| 1919 |
WORD outGlyphs[maxGlyphs]; |
| 1920 |
WORD logClust[numWCHAR]; |
| 1921 |
int numGlyphs; |
| 1922 |
SCRIPT_ANALYSIS& script = si[0].a; |
| 1923 |
script.eScript = SCRIPT_UNDEFINED; |
| 1924 |
script.fRTL = FALSE; |
| 1925 |
script.fLayoutRTL = FALSE; |
| 1926 |
script.fLinkBefore = FALSE; |
| 1927 |
script.fLinkAfter = FALSE; |
| 1928 |
script.fLogicalOrder = FALSE; |
| 1929 |
script.fNoGlyphIndex = FALSE; |
| 1930 |
script.s.uBidiLevel = 0; |
| 1931 |
script.s.fOverrideDirection = 0; |
| 1932 |
script.s.fInhibitSymSwap = TRUE; |
| 1933 |
script.s.fCharShape = FALSE; |
| 1934 |
script.s.fDigitSubstitute = FALSE; |
| 1935 |
script.s.fInhibitLigate = FALSE; |
| 1936 |
script.s.fDisplayZWG = TRUE; |
| 1937 |
script.s.fArabicNumContext = FALSE; |
| 1938 |
script.s.fGcpClusters = FALSE; |
| 1939 |
script.s.fReserved = 0; |
| 1940 |
script.s.fEngineReserved = 0; |
| 1941 |
// For the future, 0x80040200 from here is USP_E_SCRIPT_NOT_IN_FONT |
| 1942 |
HRZM(ScriptShape(hdc, scriptCache, utf16, numWCHAR, maxGlyphs, &script, |
| 1943 |
outGlyphs, logClust, vsa, &numGlyphs), |
| 1944 |
"Could not shape character."); |
| 1945 |
if (1 == numGlyphs) { |
| 1946 |
index = outGlyphs[0]; |
| 1947 |
} |
| 1948 |
return index; |
| 1949 |
} |
| 1950 |
|
| 1951 |
void LogFontTypeface::onCharsToGlyphs(SkSpan<const SkUnichar> uni, SkSpan<SkGlyphID> glyphs) const { |
| 1952 |
SkASSERT(uni.size() == glyphs.size()); |
| 1953 |
|
| 1954 |
SkAutoHDC hdc(fLogFont); |
| 1955 |
|
| 1956 |
TEXTMETRIC tm; |
| 1957 |
if (0 == GetTextMetrics(hdc, &tm)) { |
| 1958 |
call_ensure_accessible(fLogFont); |
| 1959 |
if (0 == GetTextMetrics(hdc, &tm)) { |
| 1960 |
tm.tmPitchAndFamily = TMPF_TRUETYPE; |
| 1961 |
} |
| 1962 |
} |
| 1963 |
bool Ox1FHack = !(tm.tmPitchAndFamily & TMPF_VECTOR) /*&& winVer < Vista */; |
| 1964 |
|
| 1965 |
SCRIPT_CACHE sc = nullptr; |
| 1966 |
static const int scratchCount = 256; |
| 1967 |
WCHAR scratch[scratchCount]; |
| 1968 |
int glyphIndex = 0; |
| 1969 |
const int glyphCount = SkToInt(glyphs.size()); |
| 1970 |
const uint32_t* utf32 = reinterpret_cast<const uint32_t*>(uni.data()); |
| 1971 |
while (glyphIndex < glyphCount) { |
| 1972 |
// Try a run of bmp. |
| 1973 |
int glyphsLeft = std::min(glyphCount - glyphIndex, scratchCount); |
| 1974 |
int runLength = 0; |
| 1975 |
while (runLength < glyphsLeft && utf32[glyphIndex + runLength] <= 0xFFFF) { |
| 1976 |
scratch[runLength] = static_cast<WCHAR>(utf32[glyphIndex + runLength]); |
| 1977 |
++runLength; |
| 1978 |
} |
| 1979 |
if (runLength) { |
| 1980 |
bmpCharsToGlyphs(hdc, scratch, runLength, &glyphs[glyphIndex], Ox1FHack); |
| 1981 |
glyphIndex += runLength; |
| 1982 |
} |
| 1983 |
|
| 1984 |
// Try a run of non-bmp. |
| 1985 |
while (glyphIndex < glyphCount && utf32[glyphIndex] > 0xFFFF) { |
| 1986 |
SkUTF::ToUTF16(utf32[glyphIndex], reinterpret_cast<uint16_t*>(scratch)); |
| 1987 |
glyphs[glyphIndex] = nonBmpCharToGlyph(hdc, &sc, scratch); |
| 1988 |
++glyphIndex; |
| 1989 |
} |
| 1990 |
} |
| 1991 |
|
| 1992 |
if (sc) { |
| 1993 |
::ScriptFreeCache(&sc); |
| 1994 |
} |
| 1995 |
} |
| 1996 |
|
| 1997 |
int LogFontTypeface::onCountGlyphs() const { |
| 1998 |
SkAutoHDC hdc(fLogFont); |
| 1999 |
return calculateGlyphCount(hdc, fLogFont); |
| 2000 |
} |
| 2001 |
|
| 2002 |
void LogFontTypeface::getPostScriptGlyphNames(SkString*) const {} |
| 2003 |
|
| 2004 |
int LogFontTypeface::onGetUPEM() const { |
| 2005 |
SkAutoHDC hdc(fLogFont); |
| 2006 |
return calculateUPEM(hdc, fLogFont); |
| 2007 |
} |
| 2008 |
|
| 2009 |
SkTypeface::LocalizedStrings* LogFontTypeface::onCreateFamilyNameIterator() const { |
| 2010 |
sk_sp<SkTypeface::LocalizedStrings> nameIter = |
| 2011 |
SkOTUtils::LocalizedStrings_NameTable::MakeForFamilyNames(*this); |
| 2012 |
if (!nameIter) { |
| 2013 |
SkString familyName; |
| 2014 |
this->getFamilyName(&familyName); |
| 2015 |
SkString language("und"); //undetermined |
| 2016 |
nameIter = sk_make_sp<SkOTUtils::LocalizedStrings_SingleName>(familyName, language); |
| 2017 |
} |
| 2018 |
return nameIter.release(); |
| 2019 |
} |
| 2020 |
|
| 2021 |
int LogFontTypeface::onGetTableTags(SkSpan<SkFontTableTag> tags) const { |
| 2022 |
SkSFNTHeader header; |
| 2023 |
if (sizeof(header) != this->onGetTableData(0, 0, sizeof(header), &header)) { |
| 2024 |
return 0; |
| 2025 |
} |
| 2026 |
|
| 2027 |
size_t numTables = SkEndian_SwapBE16(header.numTables); |
| 2028 |
|
| 2029 |
if (!tags.empty()) { |
| 2030 |
size_t size = numTables * sizeof(SkSFNTHeader::TableDirectoryEntry); |
| 2031 |
AutoSTMalloc<0x20, SkSFNTHeader::TableDirectoryEntry> dir(numTables); |
| 2032 |
if (size != this->onGetTableData(0, sizeof(header), size, dir.get())) { |
| 2033 |
return 0; |
| 2034 |
} |
| 2035 |
|
| 2036 |
const size_t n = std::min(numTables, tags.size()); |
| 2037 |
for (size_t i = 0; i < n; ++i) { |
| 2038 |
tags[i] = SkEndian_SwapBE32(dir[i].tag); |
| 2039 |
} |
| 2040 |
} |
| 2041 |
return numTables; |
| 2042 |
} |
| 2043 |
|
| 2044 |
size_t LogFontTypeface::onGetTableData(SkFontTableTag tag, size_t offset, |
| 2045 |
size_t length, void* data) const |
| 2046 |
{ |
| 2047 |
LOGFONT lf = fLogFont; |
| 2048 |
SkAutoHDC hdc(lf); |
| 2049 |
|
| 2050 |
tag = SkEndian_SwapBE32(tag); |
| 2051 |
if (nullptr == data) { |
| 2052 |
length = 0; |
| 2053 |
} |
| 2054 |
DWORD bufferSize = GetFontData(hdc, tag, (DWORD) offset, data, (DWORD) length); |
| 2055 |
if (bufferSize == GDI_ERROR) { |
| 2056 |
call_ensure_accessible(lf); |
| 2057 |
bufferSize = GetFontData(hdc, tag, (DWORD) offset, data, (DWORD) length); |
| 2058 |
} |
| 2059 |
return bufferSize == GDI_ERROR ? 0 : bufferSize; |
| 2060 |
} |
| 2061 |
|
| 2062 |
sk_sp<SkData> LogFontTypeface::onCopyTableData(SkFontTableTag tag) const { |
| 2063 |
LOGFONT lf = fLogFont; |
| 2064 |
SkAutoHDC hdc(lf); |
| 2065 |
|
| 2066 |
tag = SkEndian_SwapBE32(tag); |
| 2067 |
DWORD size = GetFontData(hdc, tag, 0, nullptr, 0); |
| 2068 |
if (size == GDI_ERROR) { |
| 2069 |
call_ensure_accessible(lf); |
| 2070 |
size = GetFontData(hdc, tag, 0, nullptr, 0); |
| 2071 |
} |
| 2072 |
|
| 2073 |
sk_sp<SkData> data; |
| 2074 |
if (size != GDI_ERROR) { |
| 2075 |
data = SkData::MakeUninitialized(size); |
| 2076 |
if (GetFontData(hdc, tag, 0, data->writable_data(), size) == GDI_ERROR) { |
| 2077 |
data.reset(); |
| 2078 |
} |
| 2079 |
} |
| 2080 |
return data; |
| 2081 |
} |
| 2082 |
|
| 2083 |
std::unique_ptr<SkScalerContext> LogFontTypeface::onCreateScalerContext( |
| 2084 |
const SkScalerContextEffects& effects, const SkDescriptor* desc) const |
| 2085 |
{ |
| 2086 |
auto ctx = std::make_unique<SkScalerContext_GDI>( |
| 2087 |
*const_cast<LogFontTypeface*>(this), effects, desc); |
| 2088 |
if (ctx->isValid()) { |
| 2089 |
return std::move(ctx); |
| 2090 |
} |
| 2091 |
|
| 2092 |
ctx.reset(); |
| 2093 |
SkStrikeCache::PurgeAll(); |
| 2094 |
ctx = std::make_unique<SkScalerContext_GDI>( |
| 2095 |
*const_cast<LogFontTypeface*>(this), effects, desc); |
| 2096 |
if (ctx->isValid()) { |
| 2097 |
return std::move(ctx); |
| 2098 |
} |
| 2099 |
|
| 2100 |
return SkScalerContext::MakeEmpty( |
| 2101 |
*const_cast<LogFontTypeface*>(this), effects, desc); |
| 2102 |
} |
| 2103 |
|
| 2104 |
void LogFontTypeface::onFilterRec(SkScalerContextRec* rec) const { |
| 2105 |
rec->useStrokeForFakeBold(); |
| 2106 |
|
| 2107 |
if (rec->fFlags & SkScalerContext::kLCD_BGROrder_Flag || |
| 2108 |
rec->fFlags & SkScalerContext::kLCD_Vertical_Flag) |
| 2109 |
{ |
| 2110 |
rec->fMaskFormat = SkMask::kA8_Format; |
| 2111 |
rec->fFlags |= SkScalerContext::kGenA8FromLCD_Flag; |
| 2112 |
} |
| 2113 |
|
| 2114 |
unsigned flagsWeDontSupport = SkScalerContext::kForceAutohinting_Flag | |
| 2115 |
SkScalerContext::kEmbeddedBitmapText_Flag | |
| 2116 |
SkScalerContext::kEmbolden_Flag | |
| 2117 |
SkScalerContext::kLCD_BGROrder_Flag | |
| 2118 |
SkScalerContext::kLCD_Vertical_Flag; |
| 2119 |
rec->fFlags &= ~flagsWeDontSupport; |
| 2120 |
|
| 2121 |
SkFontHinting h = rec->getHinting(); |
| 2122 |
switch (h) { |
| 2123 |
case SkFontHinting::kNone: |
| 2124 |
break; |
| 2125 |
case SkFontHinting::kSlight: |
| 2126 |
// Only do slight hinting when axis aligned. |
| 2127 |
// TODO: re-enable slight hinting when FontHostTest can pass. |
| 2128 |
//if (!isAxisAligned(*rec)) { |
| 2129 |
h = SkFontHinting::kNone; |
| 2130 |
//} |
| 2131 |
break; |
| 2132 |
case SkFontHinting::kNormal: |
| 2133 |
case SkFontHinting::kFull: |
| 2134 |
// TODO: need to be able to distinguish subpixel positioned glyphs |
| 2135 |
// and linear metrics. |
| 2136 |
//rec->fFlags &= ~SkScalerContext::kSubpixelPositioning_Flag; |
| 2137 |
h = SkFontHinting::kNormal; |
| 2138 |
break; |
| 2139 |
default: |
| 2140 |
SkDEBUGFAIL("unknown hinting"); |
| 2141 |
} |
| 2142 |
//TODO: if this is a bitmap font, squash hinting and subpixel. |
| 2143 |
rec->setHinting(h); |
| 2144 |
|
| 2145 |
// turn this off since GDI might turn A8 into BW! Need a bigger fix. |
| 2146 |
#if 0 |
| 2147 |
// Disable LCD when rotated, since GDI's output is ugly |
| 2148 |
if (isLCD(*rec) && !isAxisAligned(*rec)) { |
| 2149 |
rec->fMaskFormat = SkMask::kA8_Format; |
| 2150 |
} |
| 2151 |
#endif |
| 2152 |
|
| 2153 |
if (!fCanBeLCD && isLCD(*rec)) { |
| 2154 |
rec->fMaskFormat = SkMask::kA8_Format; |
| 2155 |
rec->fFlags &= ~SkScalerContext::kGenA8FromLCD_Flag; |
| 2156 |
} |
| 2157 |
} |
| 2158 |
|
| 2159 |
/////////////////////////////////////////////////////////////////////////////// |
| 2160 |
|
| 2161 |
#include "include/core/SkDataTable.h" |
| 2162 |
#include "include/core/SkFontMgr.h" |
| 2163 |
|
| 2164 |
static bool valid_logfont_for_enum(const LOGFONT& lf) { |
| 2165 |
// TODO: Vector FON is unsupported and should not be listed. |
| 2166 |
return |
| 2167 |
// Ignore implicit vertical variants. |
| 2168 |
lf.lfFaceName[0] && lf.lfFaceName[0] != '@' |
| 2169 |
|
| 2170 |
// DEFAULT_CHARSET is used to get all fonts, but also implies all |
| 2171 |
// character sets. Filter assuming all fonts support ANSI_CHARSET. |
| 2172 |
&& ANSI_CHARSET == lf.lfCharSet |
| 2173 |
; |
| 2174 |
} |
| 2175 |
|
| 2176 |
/** An EnumFontFamExProc implementation which interprets builderParam as |
| 2177 |
* an SkTDArray<ENUMLOGFONTEX>* and appends logfonts which |
| 2178 |
* pass the valid_logfont_for_enum predicate. |
| 2179 |
*/ |
| 2180 |
static int CALLBACK enum_family_proc(const LOGFONT* lf, const TEXTMETRIC*, |
| 2181 |
DWORD fontType, LPARAM builderParam) { |
| 2182 |
if (valid_logfont_for_enum(*lf)) { |
| 2183 |
SkTDArray<ENUMLOGFONTEX>* array = (SkTDArray<ENUMLOGFONTEX>*)builderParam; |
| 2184 |
*array->append() = *(const ENUMLOGFONTEX*)lf; |
| 2185 |
} |
| 2186 |
return 1; // non-zero means continue |
| 2187 |
} |
| 2188 |
|
| 2189 |
class SkFontStyleSetGDI : public SkFontStyleSet { |
| 2190 |
public: |
| 2191 |
SkFontStyleSetGDI(const TCHAR familyName[]) { |
| 2192 |
LOGFONT lf; |
| 2193 |
sk_bzero(&lf, sizeof(lf)); |
| 2194 |
lf.lfCharSet = DEFAULT_CHARSET; |
| 2195 |
_tcscpy_s(lf.lfFaceName, familyName); |
| 2196 |
|
| 2197 |
HDC hdc = ::CreateCompatibleDC(nullptr); |
| 2198 |
::EnumFontFamiliesEx(hdc, &lf, enum_family_proc, (LPARAM)&fArray, 0); |
| 2199 |
::DeleteDC(hdc); |
| 2200 |
} |
| 2201 |
|
| 2202 |
int count() override { |
| 2203 |
return fArray.size(); |
| 2204 |
} |
| 2205 |
|
| 2206 |
void getStyle(int index, SkFontStyle* fs, SkString* styleName) override { |
| 2207 |
if (fs) { |
| 2208 |
*fs = get_style(fArray[index].elfLogFont); |
| 2209 |
} |
| 2210 |
if (styleName) { |
| 2211 |
const ENUMLOGFONTEX& ref = fArray[index]; |
| 2212 |
// For some reason, ENUMLOGFONTEX and LOGFONT disagree on their type in the |
| 2213 |
// non-unicode version. |
| 2214 |
// ENUMLOGFONTEX uses BYTE |
| 2215 |
// LOGFONT uses CHAR |
| 2216 |
// Here we assert they that the style name is logically the same (size) as |
| 2217 |
// a TCHAR, so we can use the same converter function. |
| 2218 |
SkASSERT(sizeof(TCHAR) == sizeof(ref.elfStyle[0])); |
| 2219 |
tchar_to_skstring((const TCHAR*)ref.elfStyle, styleName); |
| 2220 |
} |
| 2221 |
} |
| 2222 |
|
| 2223 |
sk_sp<SkTypeface> createTypeface(int index) override { |
| 2224 |
return SkCreateTypefaceFromLOGFONT(fArray[index].elfLogFont); |
| 2225 |
} |
| 2226 |
|
| 2227 |
sk_sp<SkTypeface> matchStyle(const SkFontStyle& pattern) override { |
| 2228 |
return this->matchStyleCSS3(pattern); |
| 2229 |
} |
| 2230 |
|
| 2231 |
private: |
| 2232 |
SkTDArray<ENUMLOGFONTEX> fArray; |
| 2233 |
}; |
| 2234 |
|
| 2235 |
class SkFontMgrGDI : public SkFontMgr { |
| 2236 |
public: |
| 2237 |
SkFontMgrGDI() { |
| 2238 |
LOGFONT lf; |
| 2239 |
sk_bzero(&lf, sizeof(lf)); |
| 2240 |
lf.lfCharSet = DEFAULT_CHARSET; |
| 2241 |
|
| 2242 |
HDC hdc = ::CreateCompatibleDC(nullptr); |
| 2243 |
::EnumFontFamiliesEx(hdc, &lf, enum_family_proc, (LPARAM)&fLogFontArray, 0); |
| 2244 |
::DeleteDC(hdc); |
| 2245 |
} |
| 2246 |
|
| 2247 |
protected: |
| 2248 |
int onCountFamilies() const override { |
| 2249 |
return fLogFontArray.size(); |
| 2250 |
} |
| 2251 |
|
| 2252 |
void onGetFamilyName(int index, SkString* familyName) const override { |
| 2253 |
SkASSERT(index < fLogFontArray.size()); |
| 2254 |
tchar_to_skstring(fLogFontArray[index].elfLogFont.lfFaceName, familyName); |
| 2255 |
} |
| 2256 |
|
| 2257 |
sk_sp<SkFontStyleSet> onCreateStyleSet(int index) const override { |
| 2258 |
SkASSERT(index < fLogFontArray.size()); |
| 2259 |
return sk_sp<SkFontStyleSet>( |
| 2260 |
new SkFontStyleSetGDI(fLogFontArray[index].elfLogFont.lfFaceName)); |
| 2261 |
} |
| 2262 |
|
| 2263 |
sk_sp<SkFontStyleSet> onMatchFamily(const char familyName[]) const override { |
| 2264 |
if (nullptr == familyName) { |
| 2265 |
familyName = ""; // do we need this check??? |
| 2266 |
} |
| 2267 |
LOGFONT lf; |
| 2268 |
logfont_for_name(familyName, &lf); |
| 2269 |
return sk_sp<SkFontStyleSet>(new SkFontStyleSetGDI(lf.lfFaceName)); |
| 2270 |
} |
| 2271 |
|
| 2272 |
sk_sp<SkTypeface> onMatchFamilyStyle(const char familyName[], |
| 2273 |
const SkFontStyle& fontstyle) const override { |
| 2274 |
// could be in base impl |
| 2275 |
sk_sp<SkFontStyleSet> sset(this->matchFamily(familyName)); |
| 2276 |
return sset->matchStyle(fontstyle); |
| 2277 |
} |
| 2278 |
|
| 2279 |
sk_sp<SkTypeface> onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&, |
| 2280 |
const char* bcp47[], int bcp47Count, |
| 2281 |
SkUnichar character) const override { |
| 2282 |
return nullptr; |
| 2283 |
} |
| 2284 |
|
| 2285 |
sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream, |
| 2286 |
int ttcIndex) const override { |
| 2287 |
if (ttcIndex != 0) { |
| 2288 |
return nullptr; |
| 2289 |
} |
| 2290 |
return create_from_stream(std::move(stream)); |
| 2291 |
} |
| 2292 |
|
| 2293 |
sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream, |
| 2294 |
const SkFontArguments& args) const override { |
| 2295 |
return this->makeFromStream(std::move(stream), args.getCollectionIndex()); |
| 2296 |
} |
| 2297 |
|
| 2298 |
sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData> data, int ttcIndex) const override { |
| 2299 |
// could be in base impl |
| 2300 |
return this->makeFromStream(std::unique_ptr<SkStreamAsset>(new SkMemoryStream(std::move(data))), |
| 2301 |
ttcIndex); |
| 2302 |
} |
| 2303 |
|
| 2304 |
sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override { |
| 2305 |
// could be in base impl |
| 2306 |
auto stream = SkStream::MakeFromFile(path); |
| 2307 |
return stream ? this->makeFromStream(std::move(stream), ttcIndex) : nullptr; |
| 2308 |
} |
| 2309 |
|
| 2310 |
sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle style) const override { |
| 2311 |
LOGFONT lf; |
| 2312 |
if (nullptr == familyName) { |
| 2313 |
lf = get_default_font(); |
| 2314 |
} else { |
| 2315 |
logfont_for_name(familyName, &lf); |
| 2316 |
} |
| 2317 |
|
| 2318 |
lf.lfWeight = style.weight(); |
| 2319 |
lf.lfItalic = style.slant() == SkFontStyle::kUpright_Slant ? FALSE : TRUE; |
| 2320 |
return sk_sp<SkTypeface>(SkCreateTypefaceFromLOGFONT(lf)); |
| 2321 |
} |
| 2322 |
|
| 2323 |
private: |
| 2324 |
SkTDArray<ENUMLOGFONTEX> fLogFontArray; |
| 2325 |
}; |
| 2326 |
|
| 2327 |
/////////////////////////////////////////////////////////////////////////////// |
| 2328 |
|
| 2329 |
sk_sp<SkFontMgr> SkFontMgr_New_GDI() { return sk_make_sp<SkFontMgrGDI>(); } |
| 2330 |
|
| 2331 |
#endif//defined(SK_BUILD_FOR_WIN) |