michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "gfxFontMissingGlyphs.h" michael@0: #include "nsDeviceContext.h" michael@0: #include "gfxContext.h" michael@0: #include "gfxColor.h" michael@0: michael@0: #define CHAR_BITS(b00, b01, b02, b10, b11, b12, b20, b21, b22, b30, b31, b32, b40, b41, b42) \ michael@0: ((b00 << 0) | (b01 << 1) | (b02 << 2) | (b10 << 3) | (b11 << 4) | (b12 << 5) | \ michael@0: (b20 << 6) | (b21 << 7) | (b22 << 8) | (b30 << 9) | (b31 << 10) | (b32 << 11) | \ michael@0: (b40 << 12) | (b41 << 13) | (b42 << 14)) michael@0: michael@0: static const uint16_t glyphMicroFont[16] = { michael@0: CHAR_BITS(0, 1, 0, michael@0: 1, 0, 1, michael@0: 1, 0, 1, michael@0: 1, 0, 1, michael@0: 0, 1, 0), michael@0: CHAR_BITS(0, 1, 0, michael@0: 0, 1, 0, michael@0: 0, 1, 0, michael@0: 0, 1, 0, michael@0: 0, 1, 0), michael@0: CHAR_BITS(1, 1, 1, michael@0: 0, 0, 1, michael@0: 1, 1, 1, michael@0: 1, 0, 0, michael@0: 1, 1, 1), michael@0: CHAR_BITS(1, 1, 1, michael@0: 0, 0, 1, michael@0: 1, 1, 1, michael@0: 0, 0, 1, michael@0: 1, 1, 1), michael@0: CHAR_BITS(1, 0, 1, michael@0: 1, 0, 1, michael@0: 1, 1, 1, michael@0: 0, 0, 1, michael@0: 0, 0, 1), michael@0: CHAR_BITS(1, 1, 1, michael@0: 1, 0, 0, michael@0: 1, 1, 1, michael@0: 0, 0, 1, michael@0: 1, 1, 1), michael@0: CHAR_BITS(1, 1, 1, michael@0: 1, 0, 0, michael@0: 1, 1, 1, michael@0: 1, 0, 1, michael@0: 1, 1, 1), michael@0: CHAR_BITS(1, 1, 1, michael@0: 0, 0, 1, michael@0: 0, 0, 1, michael@0: 0, 0, 1, michael@0: 0, 0, 1), michael@0: CHAR_BITS(0, 1, 0, michael@0: 1, 0, 1, michael@0: 0, 1, 0, michael@0: 1, 0, 1, michael@0: 0, 1, 0), michael@0: CHAR_BITS(1, 1, 1, michael@0: 1, 0, 1, michael@0: 1, 1, 1, michael@0: 0, 0, 1, michael@0: 0, 0, 1), michael@0: CHAR_BITS(1, 1, 1, michael@0: 1, 0, 1, michael@0: 1, 1, 1, michael@0: 1, 0, 1, michael@0: 1, 0, 1), michael@0: CHAR_BITS(1, 1, 0, michael@0: 1, 0, 1, michael@0: 1, 1, 0, michael@0: 1, 0, 1, michael@0: 1, 1, 0), michael@0: CHAR_BITS(0, 1, 1, michael@0: 1, 0, 0, michael@0: 1, 0, 0, michael@0: 1, 0, 0, michael@0: 0, 1, 1), michael@0: CHAR_BITS(1, 1, 0, michael@0: 1, 0, 1, michael@0: 1, 0, 1, michael@0: 1, 0, 1, michael@0: 1, 1, 0), michael@0: CHAR_BITS(1, 1, 1, michael@0: 1, 0, 0, michael@0: 1, 1, 1, michael@0: 1, 0, 0, michael@0: 1, 1, 1), michael@0: CHAR_BITS(1, 1, 1, michael@0: 1, 0, 0, michael@0: 1, 1, 1, michael@0: 1, 0, 0, michael@0: 1, 0, 0) michael@0: }; michael@0: michael@0: /* Parameters that control the rendering of hexboxes. They look like this: michael@0: michael@0: BMP codepoints non-BMP codepoints michael@0: (U+0000 - U+FFFF) (U+10000 - U+10FFFF) michael@0: michael@0: +---------+ +-------------+ michael@0: | | | | michael@0: | HHH HHH | | HHH HHH HHH | michael@0: | HHH HHH | | HHH HHH HHH | michael@0: | HHH HHH | | HHH HHH HHH | michael@0: | HHH HHH | | HHH HHH HHH | michael@0: | HHH HHH | | HHH HHH HHH | michael@0: | | | | michael@0: | HHH HHH | | HHH HHH HHH | michael@0: | HHH HHH | | HHH HHH HHH | michael@0: | HHH HHH | | HHH HHH HHH | michael@0: | HHH HHH | | HHH HHH HHH | michael@0: | HHH HHH | | HHH HHH HHH | michael@0: | | | | michael@0: +---------+ +-------------+ michael@0: */ michael@0: michael@0: /** Width of a minifont glyph (see above) */ michael@0: static const int MINIFONT_WIDTH = 3; michael@0: /** Height of a minifont glyph (see above) */ michael@0: static const int MINIFONT_HEIGHT = 5; michael@0: /** michael@0: * Gap between minifont glyphs (both horizontal and vertical) and also michael@0: * the minimum desired gap between the box border and the glyphs michael@0: */ michael@0: static const int HEX_CHAR_GAP = 1; michael@0: /** michael@0: * The amount of space between the vertical edge of the glyphbox and the michael@0: * box border. We make this nonzero so that when multiple missing glyphs michael@0: * occur consecutively there's a gap between their rendered boxes. michael@0: */ michael@0: static const int BOX_HORIZONTAL_INSET = 1; michael@0: /** The width of the border */ michael@0: static const int BOX_BORDER_WIDTH = 1; michael@0: /** michael@0: * The scaling factor for the border opacity; this is multiplied by the current michael@0: * opacity being used to draw the text. michael@0: */ michael@0: static const gfxFloat BOX_BORDER_OPACITY = 0.5; michael@0: /** michael@0: * Draw a single hex character using the current color. A nice way to do this michael@0: * would be to fill in an A8 image surface and then use it as a mask michael@0: * to paint the current color. Tragically this doesn't currently work with the michael@0: * Quartz cairo backend which doesn't generally support masking with surfaces. michael@0: * So for now we just paint a bunch of rectangles... michael@0: */ michael@0: #ifndef MOZ_GFX_OPTIMIZE_MOBILE michael@0: static void michael@0: DrawHexChar(gfxContext *aContext, const gfxPoint& aPt, uint32_t aDigit) michael@0: { michael@0: aContext->NewPath(); michael@0: uint32_t glyphBits = glyphMicroFont[aDigit]; michael@0: int x, y; michael@0: for (y = 0; y < MINIFONT_HEIGHT; ++y) { michael@0: for (x = 0; x < MINIFONT_WIDTH; ++x) { michael@0: if (glyphBits & 1) { michael@0: aContext->Rectangle(gfxRect(x, y, 1, 1) + aPt, true); michael@0: } michael@0: glyphBits >>= 1; michael@0: } michael@0: } michael@0: aContext->Fill(); michael@0: } michael@0: #endif // MOZ_GFX_OPTIMIZE_MOBILE michael@0: michael@0: void michael@0: gfxFontMissingGlyphs::DrawMissingGlyph(gfxContext *aContext, michael@0: const gfxRect& aRect, michael@0: uint32_t aChar, michael@0: uint32_t aAppUnitsPerDevPixel) michael@0: { michael@0: aContext->Save(); michael@0: michael@0: gfxRGBA currentColor; michael@0: if (!aContext->GetDeviceColor(currentColor)) { michael@0: // We're currently drawing with some kind of pattern... Just draw michael@0: // the missing-glyph data in black. michael@0: currentColor = gfxRGBA(0,0,0,1); michael@0: } michael@0: michael@0: // Stroke a rectangle so that the stroke's left edge is inset one pixel michael@0: // from the left edge of the glyph box and the stroke's right edge michael@0: // is inset one pixel from the right edge of the glyph box. michael@0: gfxFloat halfBorderWidth = BOX_BORDER_WIDTH / 2.0; michael@0: gfxFloat borderLeft = aRect.X() + BOX_HORIZONTAL_INSET + halfBorderWidth; michael@0: gfxFloat borderRight = aRect.XMost() - BOX_HORIZONTAL_INSET - halfBorderWidth; michael@0: gfxRect borderStrokeRect(borderLeft, aRect.Y() + halfBorderWidth, michael@0: borderRight - borderLeft, michael@0: aRect.Height() - 2.0 * halfBorderWidth); michael@0: if (!borderStrokeRect.IsEmpty()) { michael@0: aContext->SetLineWidth(BOX_BORDER_WIDTH); michael@0: aContext->SetDash(gfxContext::gfxLineSolid); michael@0: aContext->SetLineCap(gfxContext::LINE_CAP_SQUARE); michael@0: aContext->SetLineJoin(gfxContext::LINE_JOIN_MITER); michael@0: gfxRGBA color = currentColor; michael@0: color.a *= BOX_BORDER_OPACITY; michael@0: aContext->SetDeviceColor(color); michael@0: aContext->NewPath(); michael@0: aContext->Rectangle(borderStrokeRect); michael@0: michael@0: #ifdef MOZ_GFX_OPTIMIZE_MOBILE michael@0: aContext->Fill(); michael@0: #else michael@0: aContext->Stroke(); michael@0: #endif michael@0: } michael@0: michael@0: #ifndef MOZ_GFX_OPTIMIZE_MOBILE michael@0: gfxPoint center(aRect.X() + aRect.Width() / 2, michael@0: aRect.Y() + aRect.Height() / 2); michael@0: gfxFloat halfGap = HEX_CHAR_GAP / 2.0; michael@0: gfxFloat top = -(MINIFONT_HEIGHT + halfGap); michael@0: aContext->SetDeviceColor(currentColor); michael@0: aContext->Translate(center); michael@0: // We always want integer scaling, otherwise the "bitmap" glyphs will look michael@0: // even uglier than usual when zoomed michael@0: int32_t scale = michael@0: std::max(1, nsDeviceContext::AppUnitsPerCSSPixel() / michael@0: aAppUnitsPerDevPixel); michael@0: aContext->Scale(gfxFloat(scale), gfxFloat(scale)); michael@0: if (aChar < 0x10000) { michael@0: if (aRect.Width() >= 2 * (MINIFONT_WIDTH + HEX_CHAR_GAP) && michael@0: aRect.Height() >= 2 * MINIFONT_HEIGHT + HEX_CHAR_GAP) { michael@0: // Draw 4 digits for BMP michael@0: gfxFloat left = -(MINIFONT_WIDTH + halfGap); michael@0: DrawHexChar(aContext, michael@0: gfxPoint(left, top), (aChar >> 12) & 0xF); michael@0: DrawHexChar(aContext, michael@0: gfxPoint(halfGap, top), (aChar >> 8) & 0xF); michael@0: DrawHexChar(aContext, michael@0: gfxPoint(left, halfGap), (aChar >> 4) & 0xF); michael@0: DrawHexChar(aContext, michael@0: gfxPoint(halfGap, halfGap), aChar & 0xF); michael@0: } michael@0: } else { michael@0: if (aRect.Width() >= 3 * (MINIFONT_WIDTH + HEX_CHAR_GAP) && michael@0: aRect.Height() >= 2 * MINIFONT_HEIGHT + HEX_CHAR_GAP) { michael@0: // Draw 6 digits for non-BMP michael@0: gfxFloat first = -(MINIFONT_WIDTH * 1.5 + HEX_CHAR_GAP); michael@0: gfxFloat second = -(MINIFONT_WIDTH / 2.0); michael@0: gfxFloat third = (MINIFONT_WIDTH / 2.0 + HEX_CHAR_GAP); michael@0: DrawHexChar(aContext, michael@0: gfxPoint(first, top), (aChar >> 20) & 0xF); michael@0: DrawHexChar(aContext, michael@0: gfxPoint(second, top), (aChar >> 16) & 0xF); michael@0: DrawHexChar(aContext, michael@0: gfxPoint(third, top), (aChar >> 12) & 0xF); michael@0: DrawHexChar(aContext, michael@0: gfxPoint(first, halfGap), (aChar >> 8) & 0xF); michael@0: DrawHexChar(aContext, michael@0: gfxPoint(second, halfGap), (aChar >> 4) & 0xF); michael@0: DrawHexChar(aContext, michael@0: gfxPoint(third, halfGap), aChar & 0xF); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: aContext->Restore(); michael@0: } michael@0: michael@0: gfxFloat michael@0: gfxFontMissingGlyphs::GetDesiredMinWidth(uint32_t aChar, michael@0: uint32_t aAppUnitsPerDevPixel) michael@0: { michael@0: /** michael@0: * The minimum desired width for a missing-glyph glyph box. I've laid it out michael@0: * like this so you can see what goes where. michael@0: */ michael@0: gfxFloat width = BOX_HORIZONTAL_INSET + BOX_BORDER_WIDTH + HEX_CHAR_GAP + michael@0: MINIFONT_WIDTH + HEX_CHAR_GAP + MINIFONT_WIDTH + michael@0: ((aChar < 0x10000) ? 0 : HEX_CHAR_GAP + MINIFONT_WIDTH) + michael@0: HEX_CHAR_GAP + BOX_BORDER_WIDTH + BOX_HORIZONTAL_INSET; michael@0: // Note that this will give us floating-point division, so the width will michael@0: // -not- be snapped to integer multiples of its basic pixel value michael@0: width *= gfxFloat(nsDeviceContext::AppUnitsPerCSSPixel()) / aAppUnitsPerDevPixel; michael@0: return width; michael@0: }