clip-ft-glyph.diff 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # HG changeset patch
  2. # User Lee Salzman <lsalzman@mozilla.com>
  3. # Date 1504120456 14400
  4. # Wed Aug 30 15:14:16 2017 -0400
  5. # Node ID 708d52f954b6d7ca2497fcb5b5084c6483300e89
  6. # Parent 33224536ce20d942576cd4b9ffb350d6dce397bc
  7. clip FreeType glyph bitmap to mask in Skia
  8. MozReview-Commit-ID: 9NqLj9SkHFo
  9. diff --git a/gfx/skia/skia/src/ports/SkFontHost_FreeType_common.cpp b/gfx/skia/skia/src/ports/SkFontHost_FreeType_common.cpp
  10. --- a/gfx/skia/skia/src/ports/SkFontHost_FreeType_common.cpp
  11. +++ b/gfx/skia/skia/src/ports/SkFontHost_FreeType_common.cpp
  12. @@ -390,65 +390,131 @@ void SkScalerContext_FreeType_Base::gene
  13. const SkMatrix& bitmapTransform)
  14. {
  15. const bool doBGR = SkToBool(fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag);
  16. const bool doVert = SkToBool(fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag);
  17. switch ( face->glyph->format ) {
  18. case FT_GLYPH_FORMAT_OUTLINE: {
  19. FT_Outline* outline = &face->glyph->outline;
  20. - FT_BBox bbox;
  21. - FT_Bitmap target;
  22. int dx = 0, dy = 0;
  23. if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) {
  24. dx = SkFixedToFDot6(glyph.getSubXFixed());
  25. dy = SkFixedToFDot6(glyph.getSubYFixed());
  26. // negate dy since freetype-y-goes-up and skia-y-goes-down
  27. dy = -dy;
  28. }
  29. - FT_Outline_Get_CBox(outline, &bbox);
  30. - /*
  31. - what we really want to do for subpixel is
  32. - offset(dx, dy)
  33. - compute_bounds
  34. - offset(bbox & !63)
  35. - but that is two calls to offset, so we do the following, which
  36. - achieves the same thing with only one offset call.
  37. - */
  38. - FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63),
  39. - dy - ((bbox.yMin + dy) & ~63));
  40. +
  41. + memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
  42. if (SkMask::kLCD16_Format == glyph.fMaskFormat) {
  43. + FT_Outline_Translate(outline, dx, dy);
  44. FT_Error err = FT_Render_Glyph(face->glyph, doVert ? FT_RENDER_MODE_LCD_V :
  45. FT_RENDER_MODE_LCD);
  46. if (err) {
  47. SK_TRACEFTR(err, "Could not render glyph.");
  48. - sk_bzero(glyph.fImage, glyph.computeImageSize());
  49. return;
  50. }
  51. +
  52. SkMask mask;
  53. glyph.toMask(&mask);
  54. +#ifdef SK_SHOW_TEXT_BLIT_COVERAGE
  55. + memset(mask.fImage, 0x80, mask.fBounds.height() * mask.fRowBytes);
  56. +#endif
  57. + FT_GlyphSlotRec& ftGlyph = *face->glyph;
  58. +
  59. + if (!SkIRect::Intersects(mask.fBounds,
  60. + SkIRect::MakeXYWH( ftGlyph.bitmap_left,
  61. + -ftGlyph.bitmap_top,
  62. + ftGlyph.bitmap.width,
  63. + ftGlyph.bitmap.rows)))
  64. + {
  65. + return;
  66. + }
  67. +
  68. + // If the FT_Bitmap extent is larger, discard bits of the bitmap outside the mask.
  69. + // If the SkMask extent is larger, shrink mask to fit bitmap (clearing discarded).
  70. + unsigned char* origBuffer = ftGlyph.bitmap.buffer;
  71. + // First align the top left (origin).
  72. + if (-ftGlyph.bitmap_top < mask.fBounds.fTop) {
  73. + int32_t topDiff = mask.fBounds.fTop - (-ftGlyph.bitmap_top);
  74. + ftGlyph.bitmap.buffer += ftGlyph.bitmap.pitch * topDiff;
  75. + ftGlyph.bitmap.rows -= topDiff;
  76. + ftGlyph.bitmap_top = -mask.fBounds.fTop;
  77. + }
  78. + if (ftGlyph.bitmap_left < mask.fBounds.fLeft) {
  79. + int32_t leftDiff = mask.fBounds.fLeft - ftGlyph.bitmap_left;
  80. + ftGlyph.bitmap.buffer += leftDiff;
  81. + ftGlyph.bitmap.width -= leftDiff;
  82. + ftGlyph.bitmap_left = mask.fBounds.fLeft;
  83. + }
  84. + if (mask.fBounds.fTop < -ftGlyph.bitmap_top) {
  85. + mask.fImage += mask.fRowBytes * (-ftGlyph.bitmap_top - mask.fBounds.fTop);
  86. + mask.fBounds.fTop = -ftGlyph.bitmap_top;
  87. + }
  88. + if (mask.fBounds.fLeft < ftGlyph.bitmap_left) {
  89. + mask.fImage += sizeof(uint16_t) * (ftGlyph.bitmap_left - mask.fBounds.fLeft);
  90. + mask.fBounds.fLeft = ftGlyph.bitmap_left;
  91. + }
  92. + // Origins aligned, clean up the width and height.
  93. + int ftVertScale = (doVert ? 3 : 1);
  94. + int ftHoriScale = (doVert ? 1 : 3);
  95. + if (mask.fBounds.height() * ftVertScale < SkToInt(ftGlyph.bitmap.rows)) {
  96. + ftGlyph.bitmap.rows = mask.fBounds.height() * ftVertScale;
  97. + }
  98. + if (mask.fBounds.width() * ftHoriScale < SkToInt(ftGlyph.bitmap.width)) {
  99. + ftGlyph.bitmap.width = mask.fBounds.width() * ftHoriScale;
  100. + }
  101. + if (SkToInt(ftGlyph.bitmap.rows) < mask.fBounds.height() * ftVertScale) {
  102. + mask.fBounds.fBottom = mask.fBounds.fTop + ftGlyph.bitmap.rows / ftVertScale;
  103. + }
  104. + if (SkToInt(ftGlyph.bitmap.width) < mask.fBounds.width() * ftHoriScale) {
  105. + mask.fBounds.fRight = mask.fBounds.fLeft + ftGlyph.bitmap.width / ftHoriScale;
  106. + }
  107. if (fPreBlend.isApplicable()) {
  108. - copyFT2LCD16<true>(face->glyph->bitmap, mask, doBGR,
  109. + copyFT2LCD16<true>(ftGlyph.bitmap, mask, doBGR,
  110. fPreBlend.fR, fPreBlend.fG, fPreBlend.fB);
  111. } else {
  112. - copyFT2LCD16<false>(face->glyph->bitmap, mask, doBGR,
  113. + copyFT2LCD16<false>(ftGlyph.bitmap, mask, doBGR,
  114. fPreBlend.fR, fPreBlend.fG, fPreBlend.fB);
  115. }
  116. + // Restore the buffer pointer so FreeType can properly free it.
  117. + ftGlyph.bitmap.buffer = origBuffer;
  118. } else {
  119. + FT_BBox bbox;
  120. + FT_Bitmap target;
  121. + FT_Outline_Get_CBox(outline, &bbox);
  122. + /*
  123. + what we really want to do for subpixel is
  124. + offset(dx, dy)
  125. + compute_bounds
  126. + offset(bbox & !63)
  127. + but that is two calls to offset, so we do the following, which
  128. + achieves the same thing with only one offset call.
  129. + */
  130. + FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63),
  131. + dy - ((bbox.yMin + dy) & ~63));
  132. +
  133. target.width = glyph.fWidth;
  134. target.rows = glyph.fHeight;
  135. target.pitch = glyph.rowBytes();
  136. target.buffer = reinterpret_cast<uint8_t*>(glyph.fImage);
  137. target.pixel_mode = compute_pixel_mode( (SkMask::Format)fRec.fMaskFormat);
  138. target.num_grays = 256;
  139. - memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight);
  140. FT_Outline_Get_Bitmap(face->glyph->library, outline, &target);
  141. +#ifdef SK_SHOW_TEXT_BLIT_COVERAGE
  142. + for (int y = 0; y < glyph.fHeight; ++y) {
  143. + for (int x = 0; x < glyph.fWidth; ++x) {
  144. + uint8_t& a = ((uint8_t*)glyph.fImage)[(glyph.rowBytes() * y) + x];
  145. + a = SkTMax<uint8_t>(a, 0x20);
  146. + }
  147. + }
  148. +#endif
  149. }
  150. } break;
  151. case FT_GLYPH_FORMAT_BITMAP: {
  152. FT_Pixel_Mode pixel_mode = static_cast<FT_Pixel_Mode>(face->glyph->bitmap.pixel_mode);
  153. SkMask::Format maskFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
  154. // Assume that the other formats do not exist.