george@mozilla.com | c59b5da | 2012-08-23 00:39:08 +0000 | [diff] [blame^] | 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 | |
| 9 | #include "SkColorPriv.h" |
| 10 | #include "SkFDot6.h" |
| 11 | #include "SkFontHost_FreeType_common.h" |
| 12 | #include "SkPath.h" |
| 13 | |
| 14 | #include <ft2build.h> |
| 15 | #include FT_OUTLINE_H |
| 16 | #include FT_BITMAP_H |
| 17 | // In the past, FT_GlyphSlot_Own_Bitmap was defined in this header file. |
| 18 | #include FT_SYNTHESIS_H |
| 19 | |
| 20 | static FT_Pixel_Mode compute_pixel_mode(SkMask::Format format) { |
| 21 | switch (format) { |
| 22 | case SkMask::kBW_Format: |
| 23 | return FT_PIXEL_MODE_MONO; |
| 24 | case SkMask::kA8_Format: |
| 25 | default: |
| 26 | return FT_PIXEL_MODE_GRAY; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | |
| 32 | static uint16_t packTriple(unsigned r, unsigned g, unsigned b) { |
| 33 | return SkPackRGB16(r >> 3, g >> 2, b >> 3); |
| 34 | } |
| 35 | |
| 36 | static uint16_t grayToRGB16(U8CPU gray) { |
| 37 | SkASSERT(gray <= 255); |
| 38 | return SkPackRGB16(gray >> 3, gray >> 2, gray >> 3); |
| 39 | } |
| 40 | |
| 41 | static int bittst(const uint8_t data[], int bitOffset) { |
| 42 | SkASSERT(bitOffset >= 0); |
| 43 | int lowBit = data[bitOffset >> 3] >> (~bitOffset & 7); |
| 44 | return lowBit & 1; |
| 45 | } |
| 46 | |
| 47 | template<bool APPLY_PREBLEND> |
| 48 | static void copyFT2LCD16(const SkGlyph& glyph, const FT_Bitmap& bitmap, |
| 49 | int lcdIsBGR, bool lcdIsVert, const uint8_t* tableR, |
| 50 | const uint8_t* tableG, const uint8_t* tableB) { |
| 51 | if (lcdIsVert) { |
| 52 | SkASSERT(3 * glyph.fHeight == bitmap.rows); |
| 53 | } else { |
| 54 | SkASSERT(glyph.fHeight == bitmap.rows); |
| 55 | } |
| 56 | |
| 57 | uint16_t* dst = reinterpret_cast<uint16_t*>(glyph.fImage); |
| 58 | const size_t dstRB = glyph.rowBytes(); |
| 59 | const int width = glyph.fWidth; |
| 60 | const uint8_t* src = bitmap.buffer; |
| 61 | |
| 62 | switch (bitmap.pixel_mode) { |
| 63 | case FT_PIXEL_MODE_MONO: { |
| 64 | for (int y = 0; y < glyph.fHeight; ++y) { |
| 65 | for (int x = 0; x < width; ++x) { |
| 66 | dst[x] = -bittst(src, x); |
| 67 | } |
| 68 | dst = (uint16_t*)((char*)dst + dstRB); |
| 69 | src += bitmap.pitch; |
| 70 | } |
| 71 | } break; |
| 72 | case FT_PIXEL_MODE_GRAY: { |
| 73 | for (int y = 0; y < glyph.fHeight; ++y) { |
| 74 | for (int x = 0; x < width; ++x) { |
| 75 | dst[x] = grayToRGB16(src[x]); |
| 76 | } |
| 77 | dst = (uint16_t*)((char*)dst + dstRB); |
| 78 | src += bitmap.pitch; |
| 79 | } |
| 80 | } break; |
| 81 | default: { |
| 82 | SkASSERT(lcdIsVert || (glyph.fWidth * 3 == bitmap.width)); |
| 83 | for (int y = 0; y < glyph.fHeight; y++) { |
| 84 | if (lcdIsVert) { // vertical stripes |
| 85 | const uint8_t* srcR = src; |
| 86 | const uint8_t* srcG = srcR + bitmap.pitch; |
| 87 | const uint8_t* srcB = srcG + bitmap.pitch; |
| 88 | if (lcdIsBGR) { |
| 89 | SkTSwap(srcR, srcB); |
| 90 | } |
| 91 | for (int x = 0; x < width; x++) { |
| 92 | dst[x] = packTriple(sk_apply_lut_if<APPLY_PREBLEND>(*srcR++, tableR), |
| 93 | sk_apply_lut_if<APPLY_PREBLEND>(*srcG++, tableG), |
| 94 | sk_apply_lut_if<APPLY_PREBLEND>(*srcB++, tableB)); |
| 95 | } |
| 96 | src += 3 * bitmap.pitch; |
| 97 | } else { // horizontal stripes |
| 98 | const uint8_t* triple = src; |
| 99 | if (lcdIsBGR) { |
| 100 | for (int x = 0; x < width; x++) { |
| 101 | dst[x] = packTriple(sk_apply_lut_if<APPLY_PREBLEND>(triple[2], tableR), |
| 102 | sk_apply_lut_if<APPLY_PREBLEND>(triple[1], tableG), |
| 103 | sk_apply_lut_if<APPLY_PREBLEND>(triple[0], tableB)); |
| 104 | triple += 3; |
| 105 | } |
| 106 | } else { |
| 107 | for (int x = 0; x < width; x++) { |
| 108 | dst[x] = packTriple(sk_apply_lut_if<APPLY_PREBLEND>(triple[0], tableR), |
| 109 | sk_apply_lut_if<APPLY_PREBLEND>(triple[1], tableG), |
| 110 | sk_apply_lut_if<APPLY_PREBLEND>(triple[2], tableB)); |
| 111 | triple += 3; |
| 112 | } |
| 113 | } |
| 114 | src += bitmap.pitch; |
| 115 | } |
| 116 | dst = (uint16_t*)((char*)dst + dstRB); |
| 117 | } |
| 118 | } break; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /////////////////////////////////////////////////////////////////////////////// |
| 123 | |
| 124 | #define ft2sk(x) SkFixedToScalar(SkFDot6ToFixed(x)) |
| 125 | |
| 126 | #if FREETYPE_MAJOR >= 2 && FREETYPE_MINOR >= 2 |
| 127 | #define CONST_PARAM const |
| 128 | #else // older freetype doesn't use const here |
| 129 | #define CONST_PARAM |
| 130 | #endif |
| 131 | |
| 132 | static int move_proc(CONST_PARAM FT_Vector* pt, void* ctx) { |
| 133 | SkPath* path = (SkPath*)ctx; |
| 134 | path->close(); // to close the previous contour (if any) |
| 135 | path->moveTo(ft2sk(pt->x), -ft2sk(pt->y)); |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static int line_proc(CONST_PARAM FT_Vector* pt, void* ctx) { |
| 140 | SkPath* path = (SkPath*)ctx; |
| 141 | path->lineTo(ft2sk(pt->x), -ft2sk(pt->y)); |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int quad_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1, |
| 146 | void* ctx) { |
| 147 | SkPath* path = (SkPath*)ctx; |
| 148 | path->quadTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x), -ft2sk(pt1->y)); |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static int cubic_proc(CONST_PARAM FT_Vector* pt0, CONST_PARAM FT_Vector* pt1, |
| 153 | CONST_PARAM FT_Vector* pt2, void* ctx) { |
| 154 | SkPath* path = (SkPath*)ctx; |
| 155 | path->cubicTo(ft2sk(pt0->x), -ft2sk(pt0->y), ft2sk(pt1->x), |
| 156 | -ft2sk(pt1->y), ft2sk(pt2->x), -ft2sk(pt2->y)); |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | /////////////////////////////////////////////////////////////////////////////// |
| 161 | |
| 162 | void SkScalerContext_FreeType_Base::generateGlyphImage(FT_Face face, |
| 163 | const SkGlyph& glyph, |
| 164 | SkMaskGamma::PreBlend* maskPreBlend) |
| 165 | { |
| 166 | //Must be careful not to use these if maskPreBlend == NULL |
| 167 | const uint8_t* tableR = NULL; |
| 168 | const uint8_t* tableG = NULL; |
| 169 | const uint8_t* tableB = NULL; |
| 170 | if (maskPreBlend) { |
| 171 | tableR = maskPreBlend->fR; |
| 172 | tableG = maskPreBlend->fG; |
| 173 | tableB = maskPreBlend->fB; |
| 174 | } |
| 175 | |
| 176 | const bool doBGR = SkToBool(fRec.fFlags & SkScalerContext::kLCD_BGROrder_Flag); |
| 177 | const bool doVert = SkToBool(fRec.fFlags & SkScalerContext::kLCD_Vertical_Flag); |
| 178 | |
| 179 | switch ( face->glyph->format ) { |
| 180 | case FT_GLYPH_FORMAT_OUTLINE: { |
| 181 | FT_Outline* outline = &face->glyph->outline; |
| 182 | FT_BBox bbox; |
| 183 | FT_Bitmap target; |
| 184 | |
| 185 | if (fRec.fFlags & SkScalerContext::kEmbolden_Flag) { |
| 186 | emboldenOutline(face, outline); |
| 187 | } |
| 188 | |
| 189 | int dx = 0, dy = 0; |
| 190 | if (fRec.fFlags & SkScalerContext::kSubpixelPositioning_Flag) { |
| 191 | dx = SkFixedToFDot6(glyph.getSubXFixed()); |
| 192 | dy = SkFixedToFDot6(glyph.getSubYFixed()); |
| 193 | // negate dy since freetype-y-goes-up and skia-y-goes-down |
| 194 | dy = -dy; |
| 195 | } |
| 196 | FT_Outline_Get_CBox(outline, &bbox); |
| 197 | /* |
| 198 | what we really want to do for subpixel is |
| 199 | offset(dx, dy) |
| 200 | compute_bounds |
| 201 | offset(bbox & !63) |
| 202 | but that is two calls to offset, so we do the following, which |
| 203 | achieves the same thing with only one offset call. |
| 204 | */ |
| 205 | FT_Outline_Translate(outline, dx - ((bbox.xMin + dx) & ~63), |
| 206 | dy - ((bbox.yMin + dy) & ~63)); |
| 207 | |
| 208 | if (SkMask::kLCD16_Format == glyph.fMaskFormat) { |
| 209 | FT_Render_Glyph(face->glyph, doVert ? FT_RENDER_MODE_LCD_V : FT_RENDER_MODE_LCD); |
| 210 | if (maskPreBlend) { |
| 211 | copyFT2LCD16<true>(glyph, face->glyph->bitmap, doBGR, doVert, |
| 212 | tableR, tableG, tableB); |
| 213 | } else { |
| 214 | copyFT2LCD16<false>(glyph, face->glyph->bitmap, doBGR, doVert, |
| 215 | tableR, tableG, tableB); |
| 216 | } |
| 217 | } else { |
| 218 | target.width = glyph.fWidth; |
| 219 | target.rows = glyph.fHeight; |
| 220 | target.pitch = glyph.rowBytes(); |
| 221 | target.buffer = reinterpret_cast<uint8_t*>(glyph.fImage); |
| 222 | target.pixel_mode = compute_pixel_mode( |
| 223 | (SkMask::Format)fRec.fMaskFormat); |
| 224 | target.num_grays = 256; |
| 225 | |
| 226 | memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight); |
| 227 | FT_Outline_Get_Bitmap(face->glyph->library, outline, &target); |
| 228 | } |
| 229 | } break; |
| 230 | |
| 231 | case FT_GLYPH_FORMAT_BITMAP: { |
| 232 | if (fRec.fFlags & SkScalerContext::kEmbolden_Flag) { |
| 233 | FT_GlyphSlot_Own_Bitmap(face->glyph); |
| 234 | FT_Bitmap_Embolden(face->glyph->library, &face->glyph->bitmap, kBitmapEmboldenStrength, 0); |
| 235 | } |
| 236 | SkASSERT_CONTINUE(glyph.fWidth == face->glyph->bitmap.width); |
| 237 | SkASSERT_CONTINUE(glyph.fHeight == face->glyph->bitmap.rows); |
| 238 | SkASSERT_CONTINUE(glyph.fTop == -face->glyph->bitmap_top); |
| 239 | SkASSERT_CONTINUE(glyph.fLeft == face->glyph->bitmap_left); |
| 240 | |
| 241 | const uint8_t* src = (const uint8_t*)face->glyph->bitmap.buffer; |
| 242 | uint8_t* dst = (uint8_t*)glyph.fImage; |
| 243 | |
| 244 | if (face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_GRAY || |
| 245 | (face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO && |
| 246 | glyph.fMaskFormat == SkMask::kBW_Format)) { |
| 247 | unsigned srcRowBytes = face->glyph->bitmap.pitch; |
| 248 | unsigned dstRowBytes = glyph.rowBytes(); |
| 249 | unsigned minRowBytes = SkMin32(srcRowBytes, dstRowBytes); |
| 250 | unsigned extraRowBytes = dstRowBytes - minRowBytes; |
| 251 | |
| 252 | for (int y = face->glyph->bitmap.rows - 1; y >= 0; --y) { |
| 253 | memcpy(dst, src, minRowBytes); |
| 254 | memset(dst + minRowBytes, 0, extraRowBytes); |
| 255 | src += srcRowBytes; |
| 256 | dst += dstRowBytes; |
| 257 | } |
| 258 | } else if (face->glyph->bitmap.pixel_mode == FT_PIXEL_MODE_MONO && |
| 259 | glyph.fMaskFormat == SkMask::kA8_Format) { |
| 260 | for (int y = 0; y < face->glyph->bitmap.rows; ++y) { |
| 261 | uint8_t byte = 0; |
| 262 | int bits = 0; |
| 263 | const uint8_t* src_row = src; |
| 264 | uint8_t* dst_row = dst; |
| 265 | |
| 266 | for (int x = 0; x < face->glyph->bitmap.width; ++x) { |
| 267 | if (!bits) { |
| 268 | byte = *src_row++; |
| 269 | bits = 8; |
| 270 | } |
| 271 | |
| 272 | *dst_row++ = byte & 0x80 ? 0xff : 0; |
| 273 | bits--; |
| 274 | byte <<= 1; |
| 275 | } |
| 276 | |
| 277 | src += face->glyph->bitmap.pitch; |
| 278 | dst += glyph.rowBytes(); |
| 279 | } |
| 280 | } else if (SkMask::kLCD16_Format == glyph.fMaskFormat) { |
| 281 | if (maskPreBlend) { |
| 282 | copyFT2LCD16<true>(glyph, face->glyph->bitmap, doBGR, doVert, |
| 283 | tableR, tableG, tableB); |
| 284 | } else { |
| 285 | copyFT2LCD16<false>(glyph, face->glyph->bitmap, doBGR, doVert, |
| 286 | tableR, tableG, tableB); |
| 287 | } |
| 288 | } else { |
| 289 | SkDEBUGFAIL("unknown glyph bitmap transform needed"); |
| 290 | } |
| 291 | } break; |
| 292 | |
| 293 | default: |
| 294 | SkDEBUGFAIL("unknown glyph format"); |
| 295 | memset(glyph.fImage, 0, glyph.rowBytes() * glyph.fHeight); |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | // We used to always do this pre-USE_COLOR_LUMINANCE, but with colorlum, |
| 300 | // it is optional |
| 301 | #if defined(SK_GAMMA_APPLY_TO_A8) |
| 302 | if (SkMask::kA8_Format == glyph.fMaskFormat && maskPreBlend) { |
| 303 | uint8_t* SK_RESTRICT dst = (uint8_t*)glyph.fImage; |
| 304 | unsigned rowBytes = glyph.rowBytes(); |
| 305 | |
| 306 | for (int y = glyph.fHeight - 1; y >= 0; --y) { |
| 307 | for (int x = glyph.fWidth - 1; x >= 0; --x) { |
| 308 | dst[x] = tableG[dst[x]]; |
| 309 | } |
| 310 | dst += rowBytes; |
| 311 | } |
| 312 | } |
| 313 | #endif |
| 314 | } |
| 315 | |
| 316 | void SkScalerContext_FreeType_Base::generateGlyphPath(FT_Face face, |
| 317 | const SkGlyph& glyph, |
| 318 | SkPath* path) |
| 319 | { |
| 320 | if (fRec.fFlags & SkScalerContext::kEmbolden_Flag) { |
| 321 | emboldenOutline(face, &face->glyph->outline); |
| 322 | } |
| 323 | |
| 324 | FT_Outline_Funcs funcs; |
| 325 | |
| 326 | funcs.move_to = move_proc; |
| 327 | funcs.line_to = line_proc; |
| 328 | funcs.conic_to = quad_proc; |
| 329 | funcs.cubic_to = cubic_proc; |
| 330 | funcs.shift = 0; |
| 331 | funcs.delta = 0; |
| 332 | |
| 333 | FT_Error err = FT_Outline_Decompose(&face->glyph->outline, &funcs, path); |
| 334 | |
| 335 | if (err != 0) { |
| 336 | path->reset(); |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | path->close(); |
| 341 | } |
| 342 | |
| 343 | void SkScalerContext_FreeType_Base::emboldenOutline(FT_Face face, FT_Outline* outline) |
| 344 | { |
| 345 | FT_Pos strength; |
| 346 | strength = FT_MulFix(face->units_per_EM, face->size->metrics.y_scale) |
| 347 | / 24; |
| 348 | FT_Outline_Embolden(outline, strength); |
| 349 | } |