SkFontHost_FreeType_common.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /*
  2. * Copyright 2006-2012 The Android Open Source Project
  3. * Copyright 2012 Mozilla Foundation
  4. *
  5. * Use of this source code is governed by a BSD-style license that can be
  6. * found in the LICENSE file.
  7. */
  8. #include "include/core/SkBitmap.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkPath.h"
  12. #include "include/private/SkColorData.h"
  13. #include "include/private/SkTo.h"
  14. #include "src/core/SkFDot6.h"
  15. #include "src/ports/SkFontHost_FreeType_common.h"
  16. #include <utility>
  17. #include <ft2build.h>
  18. #include FT_FREETYPE_H
  19. #include FT_BITMAP_H
  20. #ifdef FT_COLOR_H
  21. # include FT_COLOR_H
  22. #endif
  23. #include FT_IMAGE_H
  24. #include FT_OUTLINE_H
  25. // In the past, FT_GlyphSlot_Own_Bitmap was defined in this header file.
  26. #include FT_SYNTHESIS_H
  27. // FT_LOAD_COLOR and the corresponding FT_Pixel_Mode::FT_PIXEL_MODE_BGRA
  28. // were introduced in FreeType 2.5.0.
  29. // The following may be removed once FreeType 2.5.0 is required to build.
  30. #ifndef FT_LOAD_COLOR
  31. # define FT_LOAD_COLOR ( 1L << 20 )
  32. # define FT_PIXEL_MODE_BGRA 7
  33. #endif
  34. #ifdef SK_DEBUG
  35. const char* SkTraceFtrGetError(int e) {
  36. switch ((FT_Error)e) {
  37. #undef FTERRORS_H_
  38. #define FT_ERRORDEF( e, v, s ) case v: return s;
  39. #define FT_ERROR_START_LIST
  40. #define FT_ERROR_END_LIST
  41. #include FT_ERRORS_H
  42. #undef FT_ERRORDEF
  43. #undef FT_ERROR_START_LIST
  44. #undef FT_ERROR_END_LIST
  45. default: return "";
  46. }
  47. }
  48. #endif // SK_DEBUG
  49. namespace {
  50. FT_Pixel_Mode compute_pixel_mode(SkMask::Format format) {
  51. switch (format) {
  52. case SkMask::kBW_Format:
  53. return FT_PIXEL_MODE_MONO;
  54. case SkMask::kA8_Format:
  55. default:
  56. return FT_PIXEL_MODE_GRAY;
  57. }
  58. }
  59. ///////////////////////////////////////////////////////////////////////////////
  60. uint16_t packTriple(U8CPU r, U8CPU g, U8CPU b) {
  61. #ifdef SK_SHOW_TEXT_BLIT_COVERAGE
  62. r = SkTMax(r, (U8CPU)0x40);
  63. g = SkTMax(g, (U8CPU)0x40);
  64. b = SkTMax(b, (U8CPU)0x40);
  65. #endif
  66. return SkPack888ToRGB16(r, g, b);
  67. }
  68. uint16_t grayToRGB16(U8CPU gray) {
  69. #ifdef SK_SHOW_TEXT_BLIT_COVERAGE
  70. gray = SkTMax(gray, (U8CPU)0x40);
  71. #endif
  72. return SkPack888ToRGB16(gray, gray, gray);
  73. }
  74. int bittst(const uint8_t data[], int bitOffset) {
  75. SkASSERT(bitOffset >= 0);
  76. int lowBit = data[bitOffset >> 3] >> (~bitOffset & 7);
  77. return lowBit & 1;
  78. }
  79. /**
  80. * Copies a FT_Bitmap into an SkMask with the same dimensions.
  81. *
  82. * FT_PIXEL_MODE_MONO
  83. * FT_PIXEL_MODE_GRAY
  84. * FT_PIXEL_MODE_LCD
  85. * FT_PIXEL_MODE_LCD_V
  86. */
  87. template<bool APPLY_PREBLEND>
  88. void copyFT2LCD16(const FT_Bitmap& bitmap, const SkMask& mask, int lcdIsBGR,
  89. const uint8_t* tableR, const uint8_t* tableG, const uint8_t* tableB)
  90. {
  91. SkASSERT(SkMask::kLCD16_Format == mask.fFormat);
  92. if (FT_PIXEL_MODE_LCD != bitmap.pixel_mode) {
  93. SkASSERT(mask.fBounds.width() == static_cast<int>(bitmap.width));
  94. }
  95. if (FT_PIXEL_MODE_LCD_V != bitmap.pixel_mode) {
  96. SkASSERT(mask.fBounds.height() == static_cast<int>(bitmap.rows));
  97. }
  98. const uint8_t* src = bitmap.buffer;
  99. uint16_t* dst = reinterpret_cast<uint16_t*>(mask.fImage);
  100. const size_t dstRB = mask.fRowBytes;
  101. const int width = mask.fBounds.width();
  102. const int height = mask.fBounds.height();
  103. switch (bitmap.pixel_mode) {
  104. case FT_PIXEL_MODE_MONO:
  105. for (int y = height; y --> 0;) {
  106. for (int x = 0; x < width; ++x) {
  107. dst[x] = -bittst(src, x);
  108. }
  109. dst = (uint16_t*)((char*)dst + dstRB);
  110. src += bitmap.pitch;
  111. }
  112. break;
  113. case FT_PIXEL_MODE_GRAY:
  114. for (int y = height; y --> 0;) {
  115. for (int x = 0; x < width; ++x) {
  116. dst[x] = grayToRGB16(src[x]);
  117. }
  118. dst = (uint16_t*)((char*)dst + dstRB);
  119. src += bitmap.pitch;
  120. }
  121. break;
  122. case FT_PIXEL_MODE_LCD:
  123. SkASSERT(3 * mask.fBounds.width() == static_cast<int>(bitmap.width));
  124. for (int y = height; y --> 0;) {
  125. const uint8_t* triple = src;
  126. if (lcdIsBGR) {
  127. for (int x = 0; x < width; x++) {
  128. dst[x] = packTriple(sk_apply_lut_if<APPLY_PREBLEND>(triple[2], tableR),
  129. sk_apply_lut_if<APPLY_PREBLEND>(triple[1], tableG),
  130. sk_apply_lut_if<APPLY_PREBLEND>(triple[0], tableB));
  131. triple += 3;
  132. }
  133. } else {
  134. for (int x = 0; x < width; x++) {
  135. dst[x] = packTriple(sk_apply_lut_if<APPLY_PREBLEND>(triple[0], tableR),
  136. sk_apply_lut_if<APPLY_PREBLEND>(triple[1], tableG),
  137. sk_apply_lut_if<APPLY_PREBLEND>(triple[2], tableB));
  138. triple += 3;
  139. }
  140. }
  141. src += bitmap.pitch;
  142. dst = (uint16_t*)((char*)dst + dstRB);
  143. }
  144. break;
  145. case FT_PIXEL_MODE_LCD_V:
  146. SkASSERT(3 * mask.fBounds.height() == static_cast<int>(bitmap.rows));
  147. for (int y = height; y --> 0;) {
  148. const uint8_t* srcR = src;
  149. const uint8_t* srcG = srcR + bitmap.pitch;
  150. const uint8_t* srcB = srcG + bitmap.pitch;
  151. if (lcdIsBGR) {
  152. using std::swap;
  153. swap(srcR, srcB);
  154. }
  155. for (int x = 0; x < width; x++) {
  156. dst[x] = packTriple(sk_apply_lut_if<APPLY_PREBLEND>(*srcR++, tableR),
  157. sk_apply_lut_if<APPLY_PREBLEND>(*srcG++, tableG),
  158. sk_apply_lut_if<APPLY_PREBLEND>(*srcB++, tableB));
  159. }
  160. src += 3 * bitmap.pitch;
  161. dst = (uint16_t*)((char*)dst + dstRB);
  162. }
  163. break;
  164. default:
  165. SkDEBUGF("FT_Pixel_Mode %d", bitmap.pixel_mode);
  166. SkDEBUGFAIL("unsupported FT_Pixel_Mode for LCD16");
  167. break;
  168. }
  169. }
  170. /**
  171. * Copies a FT_Bitmap into an SkMask with the same dimensions.
  172. *
  173. * Yes, No, Never Requested, Never Produced
  174. *
  175. * kBW kA8 k3D kARGB32 kLCD16
  176. * FT_PIXEL_MODE_MONO Y Y NR N Y
  177. * FT_PIXEL_MODE_GRAY N Y NR N Y
  178. * FT_PIXEL_MODE_GRAY2 NP NP NR NP NP
  179. * FT_PIXEL_MODE_GRAY4 NP NP NR NP NP
  180. * FT_PIXEL_MODE_LCD NP NP NR NP NP
  181. * FT_PIXEL_MODE_LCD_V NP NP NR NP NP
  182. * FT_PIXEL_MODE_BGRA N N NR Y N
  183. *
  184. * TODO: All of these N need to be Y or otherwise ruled out.
  185. */
  186. void copyFTBitmap(const FT_Bitmap& srcFTBitmap, SkMask& dstMask) {
  187. SkASSERTF(dstMask.fBounds.width() == static_cast<int>(srcFTBitmap.width),
  188. "dstMask.fBounds.width() = %d\n"
  189. "static_cast<int>(srcFTBitmap.width) = %d",
  190. dstMask.fBounds.width(),
  191. static_cast<int>(srcFTBitmap.width)
  192. );
  193. SkASSERTF(dstMask.fBounds.height() == static_cast<int>(srcFTBitmap.rows),
  194. "dstMask.fBounds.height() = %d\n"
  195. "static_cast<int>(srcFTBitmap.rows) = %d",
  196. dstMask.fBounds.height(),
  197. static_cast<int>(srcFTBitmap.rows)
  198. );
  199. const uint8_t* src = reinterpret_cast<const uint8_t*>(srcFTBitmap.buffer);
  200. const FT_Pixel_Mode srcFormat = static_cast<FT_Pixel_Mode>(srcFTBitmap.pixel_mode);
  201. // FT_Bitmap::pitch is an int and allowed to be negative.
  202. const int srcPitch = srcFTBitmap.pitch;
  203. const size_t srcRowBytes = SkTAbs(srcPitch);
  204. uint8_t* dst = dstMask.fImage;
  205. const SkMask::Format dstFormat = static_cast<SkMask::Format>(dstMask.fFormat);
  206. const size_t dstRowBytes = dstMask.fRowBytes;
  207. const size_t width = srcFTBitmap.width;
  208. const size_t height = srcFTBitmap.rows;
  209. if (SkMask::kLCD16_Format == dstFormat) {
  210. copyFT2LCD16<false>(srcFTBitmap, dstMask, false, nullptr, nullptr, nullptr);
  211. return;
  212. }
  213. if ((FT_PIXEL_MODE_MONO == srcFormat && SkMask::kBW_Format == dstFormat) ||
  214. (FT_PIXEL_MODE_GRAY == srcFormat && SkMask::kA8_Format == dstFormat))
  215. {
  216. size_t commonRowBytes = SkTMin(srcRowBytes, dstRowBytes);
  217. for (size_t y = height; y --> 0;) {
  218. memcpy(dst, src, commonRowBytes);
  219. src += srcPitch;
  220. dst += dstRowBytes;
  221. }
  222. } else if (FT_PIXEL_MODE_MONO == srcFormat && SkMask::kA8_Format == dstFormat) {
  223. for (size_t y = height; y --> 0;) {
  224. uint8_t byte = 0;
  225. int bits = 0;
  226. const uint8_t* src_row = src;
  227. uint8_t* dst_row = dst;
  228. for (size_t x = width; x --> 0;) {
  229. if (0 == bits) {
  230. byte = *src_row++;
  231. bits = 8;
  232. }
  233. *dst_row++ = byte & 0x80 ? 0xff : 0x00;
  234. bits--;
  235. byte <<= 1;
  236. }
  237. src += srcPitch;
  238. dst += dstRowBytes;
  239. }
  240. } else if (FT_PIXEL_MODE_BGRA == srcFormat && SkMask::kARGB32_Format == dstFormat) {
  241. // FT_PIXEL_MODE_BGRA is pre-multiplied.
  242. for (size_t y = height; y --> 0;) {
  243. const uint8_t* src_row = src;
  244. SkPMColor* dst_row = reinterpret_cast<SkPMColor*>(dst);
  245. for (size_t x = 0; x < width; ++x) {
  246. uint8_t b = *src_row++;
  247. uint8_t g = *src_row++;
  248. uint8_t r = *src_row++;
  249. uint8_t a = *src_row++;
  250. *dst_row++ = SkPackARGB32(a, r, g, b);
  251. #ifdef SK_SHOW_TEXT_BLIT_COVERAGE
  252. *(dst_row-1) = SkFourByteInterp256(*(dst_row-1), SK_ColorWHITE, 0x40);
  253. #endif
  254. }
  255. src += srcPitch;
  256. dst += dstRowBytes;
  257. }
  258. } else {
  259. SkDEBUGF("FT_Pixel_Mode %d, SkMask::Format %d\n", srcFormat, dstFormat);
  260. SkDEBUGFAIL("unsupported combination of FT_Pixel_Mode and SkMask::Format");
  261. }
  262. }
  263. inline int convert_8_to_1(unsigned byte) {
  264. SkASSERT(byte <= 0xFF);
  265. // Arbitrary decision that making the cutoff at 1/4 instead of 1/2 in general looks better.
  266. return (byte >> 6) != 0;
  267. }
  268. uint8_t pack_8_to_1(const uint8_t alpha[8]) {
  269. unsigned bits = 0;
  270. for (int i = 0; i < 8; ++i) {
  271. bits <<= 1;
  272. bits |= convert_8_to_1(alpha[i]);
  273. }
  274. return SkToU8(bits);
  275. }
  276. void packA8ToA1(const SkMask& mask, const uint8_t* src, size_t srcRB) {
  277. const int height = mask.fBounds.height();
  278. const int width = mask.fBounds.width();
  279. const int octs = width >> 3;
  280. const int leftOverBits = width & 7;
  281. uint8_t* dst = mask.fImage;
  282. const int dstPad = mask.fRowBytes - SkAlign8(width)/8;
  283. SkASSERT(dstPad >= 0);
  284. const int srcPad = srcRB - width;
  285. SkASSERT(srcPad >= 0);
  286. for (int y = 0; y < height; ++y) {
  287. for (int i = 0; i < octs; ++i) {
  288. *dst++ = pack_8_to_1(src);
  289. src += 8;
  290. }
  291. if (leftOverBits > 0) {
  292. unsigned bits = 0;
  293. int shift = 7;
  294. for (int i = 0; i < leftOverBits; ++i, --shift) {
  295. bits |= convert_8_to_1(*src++) << shift;
  296. }
  297. *dst++ = bits;
  298. }
  299. src += srcPad;
  300. dst += dstPad;
  301. }
  302. }
  303. inline SkMask::Format SkMaskFormat_for_SkColorType(SkColorType colorType) {
  304. switch (colorType) {
  305. case kAlpha_8_SkColorType:
  306. return SkMask::kA8_Format;
  307. case kN32_SkColorType:
  308. return SkMask::kARGB32_Format;
  309. default:
  310. SkDEBUGFAIL("unsupported SkBitmap::Config");
  311. return SkMask::kA8_Format;
  312. }
  313. }
  314. inline SkColorType SkColorType_for_FTPixelMode(FT_Pixel_Mode pixel_mode) {
  315. switch (pixel_mode) {
  316. case FT_PIXEL_MODE_MONO:
  317. case FT_PIXEL_MODE_GRAY:
  318. return kAlpha_8_SkColorType;
  319. case FT_PIXEL_MODE_BGRA:
  320. return kN32_SkColorType;
  321. default:
  322. SkDEBUGFAIL("unsupported FT_PIXEL_MODE");
  323. return kAlpha_8_SkColorType;
  324. }
  325. }
  326. inline SkColorType SkColorType_for_SkMaskFormat(SkMask::Format format) {
  327. switch (format) {
  328. case SkMask::kBW_Format:
  329. case SkMask::kA8_Format:
  330. case SkMask::kLCD16_Format:
  331. return kAlpha_8_SkColorType;
  332. case SkMask::kARGB32_Format:
  333. return kN32_SkColorType;
  334. default:
  335. SkDEBUGFAIL("unsupported destination SkBitmap::Config");
  336. return kAlpha_8_SkColorType;
  337. }
  338. }
  339. } // namespace
  340. void SkScalerContext_FreeType_Base::generateGlyphImage(
  341. FT_Face face,
  342. const SkGlyph& glyph,
  343. const SkMatrix& bitmapTransform)
  344. {
  345. const bool doBGR = SkToBool(fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag);
  346. const bool doVert = SkToBool(fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag);
  347. switch ( face->glyph->format ) {
  348. case FT_GLYPH_FORMAT_OUTLINE: {
  349. FT_Outline* outline = &face->glyph->outline;
  350. int dx = 0, dy = 0;
  351. if (this->isSubpixel()) {
  352. dx = SkFixedToFDot6(glyph.getSubXFixed());
  353. dy = SkFixedToFDot6(glyph.getSubYFixed());
  354. // negate dy since freetype-y-goes-up and skia-y-goes-down
  355. dy = -dy;
  356. }
  357. memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
  358. #ifdef FT_COLOR_H
  359. if (SkMask::kARGB32_Format == glyph.fMaskFormat) {
  360. SkBitmap dstBitmap;
  361. // TODO: mark this as sRGB when the blits will be sRGB.
  362. dstBitmap.setInfo(SkImageInfo::Make(glyph.fWidth, glyph.fHeight,
  363. kN32_SkColorType,
  364. kPremul_SkAlphaType),
  365. glyph.rowBytes());
  366. dstBitmap.setPixels(glyph.fImage);
  367. // Scale unscaledBitmap into dstBitmap.
  368. SkCanvas canvas(dstBitmap);
  369. #ifdef SK_SHOW_TEXT_BLIT_COVERAGE
  370. canvas.clear(0x33FF0000);
  371. #else
  372. canvas.clear(SK_ColorTRANSPARENT);
  373. #endif
  374. canvas.translate(-glyph.fLeft, -glyph.fTop);
  375. if (this->isSubpixel()) {
  376. canvas.translate(SkFixedToScalar(glyph.getSubXFixed()),
  377. SkFixedToScalar(glyph.getSubYFixed()));
  378. }
  379. SkPaint paint;
  380. paint.setAntiAlias(true);
  381. FT_Color *palette;
  382. FT_Error err = FT_Palette_Select(face, 0, &palette);
  383. if (err) {
  384. SK_TRACEFTR(err, "Could not get palette from %s fontFace.", face->family_name);
  385. return;
  386. }
  387. FT_LayerIterator layerIterator;
  388. layerIterator.p = NULL;
  389. FT_Bool haveLayers = false;
  390. FT_UInt layerGlyphIndex;
  391. FT_UInt layerColorIndex;
  392. while (FT_Get_Color_Glyph_Layer(face, glyph.getGlyphID(), &layerGlyphIndex,
  393. &layerColorIndex,
  394. &layerIterator)) {
  395. haveLayers = true;
  396. if (layerColorIndex == 0xFFFF) {
  397. paint.setColor(SK_ColorBLACK);
  398. } else {
  399. SkColor color = SkColorSetARGB(palette[layerColorIndex].alpha,
  400. palette[layerColorIndex].red,
  401. palette[layerColorIndex].green,
  402. palette[layerColorIndex].blue);
  403. paint.setColor(color);
  404. }
  405. SkPath path;
  406. if (this->generateFacePath(face, layerGlyphIndex, &path)) {
  407. canvas.drawPath(path, paint);
  408. }
  409. }
  410. if (!haveLayers) {
  411. SK_TRACEFTR(err, "Could not get layers from %s fontFace.", face->family_name);
  412. return;
  413. }
  414. } else
  415. #endif
  416. if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
  417. FT_Outline_Translate(outline, dx, dy);
  418. FT_Error err = FT_Render_Glyph(face->glyph, doVert ? FT_RENDER_MODE_LCD_V :
  419. FT_RENDER_MODE_LCD);
  420. if (err) {
  421. SK_TRACEFTR(err, "Could not render glyph %x.", face->glyph);
  422. return;
  423. }
  424. SkMask mask = glyph.mask();
  425. #ifdef SK_SHOW_TEXT_BLIT_COVERAGE
  426. memset(mask.fImage, 0x80, mask.fBounds.height() * mask.fRowBytes);
  427. #endif
  428. FT_GlyphSlotRec& ftGlyph = *face->glyph;
  429. if (!SkIRect::Intersects(mask.fBounds,
  430. SkIRect::MakeXYWH( ftGlyph.bitmap_left,
  431. -ftGlyph.bitmap_top,
  432. ftGlyph.bitmap.width,
  433. ftGlyph.bitmap.rows)))
  434. {
  435. return;
  436. }
  437. // If the FT_Bitmap extent is larger, discard bits of the bitmap outside the mask.
  438. // If the SkMask extent is larger, shrink mask to fit bitmap (clearing discarded).
  439. unsigned char* origBuffer = ftGlyph.bitmap.buffer;
  440. // First align the top left (origin).
  441. if (-ftGlyph.bitmap_top < mask.fBounds.fTop) {
  442. int32_t topDiff = mask.fBounds.fTop - (-ftGlyph.bitmap_top);
  443. ftGlyph.bitmap.buffer += ftGlyph.bitmap.pitch * topDiff;
  444. ftGlyph.bitmap.rows -= topDiff;
  445. ftGlyph.bitmap_top = -mask.fBounds.fTop;
  446. }
  447. if (ftGlyph.bitmap_left < mask.fBounds.fLeft) {
  448. int32_t leftDiff = mask.fBounds.fLeft - ftGlyph.bitmap_left;
  449. ftGlyph.bitmap.buffer += leftDiff;
  450. ftGlyph.bitmap.width -= leftDiff;
  451. ftGlyph.bitmap_left = mask.fBounds.fLeft;
  452. }
  453. if (mask.fBounds.fTop < -ftGlyph.bitmap_top) {
  454. mask.fImage += mask.fRowBytes * (-ftGlyph.bitmap_top - mask.fBounds.fTop);
  455. mask.fBounds.fTop = -ftGlyph.bitmap_top;
  456. }
  457. if (mask.fBounds.fLeft < ftGlyph.bitmap_left) {
  458. mask.fImage += sizeof(uint16_t) * (ftGlyph.bitmap_left - mask.fBounds.fLeft);
  459. mask.fBounds.fLeft = ftGlyph.bitmap_left;
  460. }
  461. // Origins aligned, clean up the width and height.
  462. int ftVertScale = (doVert ? 3 : 1);
  463. int ftHoriScale = (doVert ? 1 : 3);
  464. if (mask.fBounds.height() * ftVertScale < SkToInt(ftGlyph.bitmap.rows)) {
  465. ftGlyph.bitmap.rows = mask.fBounds.height() * ftVertScale;
  466. }
  467. if (mask.fBounds.width() * ftHoriScale < SkToInt(ftGlyph.bitmap.width)) {
  468. ftGlyph.bitmap.width = mask.fBounds.width() * ftHoriScale;
  469. }
  470. if (SkToInt(ftGlyph.bitmap.rows) < mask.fBounds.height() * ftVertScale) {
  471. mask.fBounds.fBottom = mask.fBounds.fTop + ftGlyph.bitmap.rows / ftVertScale;
  472. }
  473. if (SkToInt(ftGlyph.bitmap.width) < mask.fBounds.width() * ftHoriScale) {
  474. mask.fBounds.fRight = mask.fBounds.fLeft + ftGlyph.bitmap.width / ftHoriScale;
  475. }
  476. if (fPreBlend.isApplicable()) {
  477. copyFT2LCD16<true>(ftGlyph.bitmap, mask, doBGR,
  478. fPreBlend.fR, fPreBlend.fG, fPreBlend.fB);
  479. } else {
  480. copyFT2LCD16<false>(ftGlyph.bitmap, mask, doBGR,
  481. fPreBlend.fR, fPreBlend.fG, fPreBlend.fB);
  482. }
  483. // Restore the buffer pointer so FreeType can properly free it.
  484. ftGlyph.bitmap.buffer = origBuffer;
  485. } else {
  486. FT_BBox bbox;
  487. FT_Bitmap target;
  488. FT_Outline_Get_CBox(outline, &bbox);
  489. /*
  490. what we really want to do for subpixel is
  491. offset(dx, dy)
  492. compute_bounds
  493. offset(bbox & !63)
  494. but that is two calls to offset, so we do the following, which
  495. achieves the same thing with only one offset call.
  496. */
  497. FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63),
  498. dy - ((bbox.yMin + dy) & ~63));
  499. target.width = glyph.fWidth;
  500. target.rows = glyph.fHeight;
  501. target.pitch = glyph.rowBytes();
  502. target.buffer = reinterpret_cast<uint8_t*>(glyph.fImage);
  503. target.pixel_mode = compute_pixel_mode( (SkMask::Format)glyph.fMaskFormat);
  504. target.num_grays = 256;
  505. FT_Outline_Get_Bitmap(face->glyph->library, outline, &target);
  506. #ifdef SK_SHOW_TEXT_BLIT_COVERAGE
  507. for (int y = 0; y < glyph.fHeight; ++y) {
  508. for (int x = 0; x < glyph.fWidth; ++x) {
  509. uint8_t& a = ((uint8_t*)glyph.fImage)[(glyph.rowBytes() * y) + x];
  510. a = SkTMax<uint8_t>(a, 0x20);
  511. }
  512. }
  513. #endif
  514. }
  515. } break;
  516. case FT_GLYPH_FORMAT_BITMAP: {
  517. FT_Pixel_Mode pixel_mode = static_cast<FT_Pixel_Mode>(face->glyph->bitmap.pixel_mode);
  518. SkMask::Format maskFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
  519. // Assume that the other formats do not exist.
  520. SkASSERT(FT_PIXEL_MODE_MONO == pixel_mode ||
  521. FT_PIXEL_MODE_GRAY == pixel_mode ||
  522. FT_PIXEL_MODE_BGRA == pixel_mode);
  523. // These are the only formats this ScalerContext should request.
  524. SkASSERT(SkMask::kBW_Format == maskFormat ||
  525. SkMask::kA8_Format == maskFormat ||
  526. SkMask::kARGB32_Format == maskFormat ||
  527. SkMask::kLCD16_Format == maskFormat);
  528. // If no scaling needed, directly copy glyph bitmap.
  529. if (bitmapTransform.isIdentity()) {
  530. SkMask dstMask = glyph.mask();
  531. copyFTBitmap(face->glyph->bitmap, dstMask);
  532. break;
  533. }
  534. // Otherwise, scale the bitmap.
  535. // Copy the FT_Bitmap into an SkBitmap (either A8 or ARGB)
  536. SkBitmap unscaledBitmap;
  537. // TODO: mark this as sRGB when the blits will be sRGB.
  538. unscaledBitmap.allocPixels(SkImageInfo::Make(face->glyph->bitmap.width,
  539. face->glyph->bitmap.rows,
  540. SkColorType_for_FTPixelMode(pixel_mode),
  541. kPremul_SkAlphaType));
  542. SkMask unscaledBitmapAlias;
  543. unscaledBitmapAlias.fImage = reinterpret_cast<uint8_t*>(unscaledBitmap.getPixels());
  544. unscaledBitmapAlias.fBounds.set(0, 0, unscaledBitmap.width(), unscaledBitmap.height());
  545. unscaledBitmapAlias.fRowBytes = unscaledBitmap.rowBytes();
  546. unscaledBitmapAlias.fFormat = SkMaskFormat_for_SkColorType(unscaledBitmap.colorType());
  547. copyFTBitmap(face->glyph->bitmap, unscaledBitmapAlias);
  548. // Wrap the glyph's mask in a bitmap, unless the glyph's mask is BW or LCD.
  549. // BW requires an A8 target for resizing, which can then be down sampled.
  550. // LCD should use a 4x A8 target, which will then be down sampled.
  551. // For simplicity, LCD uses A8 and is replicated.
  552. int bitmapRowBytes = 0;
  553. if (SkMask::kBW_Format != maskFormat && SkMask::kLCD16_Format != maskFormat) {
  554. bitmapRowBytes = glyph.rowBytes();
  555. }
  556. SkBitmap dstBitmap;
  557. // TODO: mark this as sRGB when the blits will be sRGB.
  558. dstBitmap.setInfo(SkImageInfo::Make(glyph.fWidth, glyph.fHeight,
  559. SkColorType_for_SkMaskFormat(maskFormat),
  560. kPremul_SkAlphaType),
  561. bitmapRowBytes);
  562. if (SkMask::kBW_Format == maskFormat || SkMask::kLCD16_Format == maskFormat) {
  563. dstBitmap.allocPixels();
  564. } else {
  565. dstBitmap.setPixels(glyph.fImage);
  566. }
  567. // Scale unscaledBitmap into dstBitmap.
  568. SkCanvas canvas(dstBitmap);
  569. #ifdef SK_SHOW_TEXT_BLIT_COVERAGE
  570. canvas.clear(0x33FF0000);
  571. #else
  572. canvas.clear(SK_ColorTRANSPARENT);
  573. #endif
  574. canvas.translate(-glyph.fLeft, -glyph.fTop);
  575. canvas.concat(bitmapTransform);
  576. canvas.translate(face->glyph->bitmap_left, -face->glyph->bitmap_top);
  577. SkPaint paint;
  578. // Using kMedium FilterQuality will cause mipmaps to be generated. Use
  579. // kLow when the results will be roughly the same in order to avoid
  580. // the mipmap generation cost.
  581. // See skbug.com/6967
  582. if (bitmapTransform.getMinScale() < 0.5) {
  583. paint.setFilterQuality(kMedium_SkFilterQuality);
  584. } else {
  585. paint.setFilterQuality(kLow_SkFilterQuality);
  586. }
  587. canvas.drawBitmap(unscaledBitmap, 0, 0, &paint);
  588. // If the destination is BW or LCD, convert from A8.
  589. if (SkMask::kBW_Format == maskFormat) {
  590. // Copy the A8 dstBitmap into the A1 glyph.fImage.
  591. SkMask dstMask = glyph.mask();
  592. packA8ToA1(dstMask, dstBitmap.getAddr8(0, 0), dstBitmap.rowBytes());
  593. } else if (SkMask::kLCD16_Format == maskFormat) {
  594. // Copy the A8 dstBitmap into the LCD16 glyph.fImage.
  595. uint8_t* src = dstBitmap.getAddr8(0, 0);
  596. uint16_t* dst = reinterpret_cast<uint16_t*>(glyph.fImage);
  597. for (int y = dstBitmap.height(); y --> 0;) {
  598. for (int x = 0; x < dstBitmap.width(); ++x) {
  599. dst[x] = grayToRGB16(src[x]);
  600. }
  601. dst = (uint16_t*)((char*)dst + glyph.rowBytes());
  602. src += dstBitmap.rowBytes();
  603. }
  604. }
  605. } break;
  606. default:
  607. SkDEBUGFAIL("unknown glyph format");
  608. memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
  609. return;
  610. }
  611. // We used to always do this pre-USE_COLOR_LUMINANCE, but with colorlum,
  612. // it is optional
  613. #if defined(SK_GAMMA_APPLY_TO_A8)
  614. if (SkMask::kA8_Format == glyph.fMaskFormat && fPreBlend.isApplicable()) {
  615. uint8_t* SK_RESTRICT dst = (uint8_t*)glyph.fImage;
  616. unsigned rowBytes = glyph.rowBytes();
  617. for (int y = glyph.fHeight - 1; y >= 0; --y) {
  618. for (int x = glyph.fWidth - 1; x >= 0; --x) {
  619. dst[x] = fPreBlend.fG[dst[x]];
  620. }
  621. dst += rowBytes;
  622. }
  623. }
  624. #endif
  625. }
  626. ///////////////////////////////////////////////////////////////////////////////
  627. namespace {
  628. int move_proc(const FT_Vector* pt, void* ctx) {
  629. SkPath* path = (SkPath*)ctx;
  630. path->close(); // to close the previous contour (if any)
  631. path->moveTo(SkFDot6ToScalar(pt->x), -SkFDot6ToScalar(pt->y));
  632. return 0;
  633. }
  634. int line_proc(const FT_Vector* pt, void* ctx) {
  635. SkPath* path = (SkPath*)ctx;
  636. path->lineTo(SkFDot6ToScalar(pt->x), -SkFDot6ToScalar(pt->y));
  637. return 0;
  638. }
  639. int quad_proc(const FT_Vector* pt0, const FT_Vector* pt1, void* ctx) {
  640. SkPath* path = (SkPath*)ctx;
  641. path->quadTo(SkFDot6ToScalar(pt0->x), -SkFDot6ToScalar(pt0->y),
  642. SkFDot6ToScalar(pt1->x), -SkFDot6ToScalar(pt1->y));
  643. return 0;
  644. }
  645. int cubic_proc(const FT_Vector* pt0, const FT_Vector* pt1, const FT_Vector* pt2, void* ctx) {
  646. SkPath* path = (SkPath*)ctx;
  647. path->cubicTo(SkFDot6ToScalar(pt0->x), -SkFDot6ToScalar(pt0->y),
  648. SkFDot6ToScalar(pt1->x), -SkFDot6ToScalar(pt1->y),
  649. SkFDot6ToScalar(pt2->x), -SkFDot6ToScalar(pt2->y));
  650. return 0;
  651. }
  652. } // namespace
  653. bool SkScalerContext_FreeType_Base::generateGlyphPath(FT_Face face, SkPath* path) {
  654. FT_Outline_Funcs funcs;
  655. funcs.move_to = move_proc;
  656. funcs.line_to = line_proc;
  657. funcs.conic_to = quad_proc;
  658. funcs.cubic_to = cubic_proc;
  659. funcs.shift = 0;
  660. funcs.delta = 0;
  661. FT_Error err = FT_Outline_Decompose(&face->glyph->outline, &funcs, path);
  662. if (err != 0) {
  663. path->reset();
  664. return false;
  665. }
  666. path->close();
  667. return true;
  668. }
  669. bool SkScalerContext_FreeType_Base::generateFacePath(FT_Face face, SkGlyphID glyphID, SkPath* path) {
  670. uint32_t flags = 0; //fLoadGlyphFlags;
  671. flags |= FT_LOAD_NO_BITMAP; // ignore embedded bitmaps so we're sure to get the outline
  672. flags &= ~FT_LOAD_RENDER; // don't scan convert (we just want the outline)
  673. FT_Error err = FT_Load_Glyph(face, glyphID, flags);
  674. if (err != 0) {
  675. path->reset();
  676. return false;
  677. }
  678. if (!generateGlyphPath(face, path)) {
  679. path->reset();
  680. return false;
  681. }
  682. return true;
  683. }