From dc3381fc8194a6192af39539c6ac9787b20209d3 Mon Sep 17 00:00:00 2001 From: "reed@android.com" Date: Thu, 11 Feb 2010 16:05:15 +0000 Subject: [PATCH] update save/restore comments to mention that the drawFilter is also saved add wrapper for harfbuzz font/fontclass git-svn-id: http://skia.googlecode.com/svn/trunk@500 2bbb7eff-a529-9590-31e7-b0007b416f81 --- include/core/SkCanvas.h | 31 +++--- include/ports/SkHarfBuzzFont.h | 66 +++++++++++ src/ports/SkHarfBuzzFont.cpp | 195 +++++++++++++++++++++++++++++++++ 3 files changed, 274 insertions(+), 18 deletions(-) create mode 100644 include/ports/SkHarfBuzzFont.h create mode 100644 src/ports/SkHarfBuzzFont.cpp diff --git a/include/core/SkCanvas.h b/include/core/SkCanvas.h index 3a10ca4031..f277a6be45 100644 --- a/include/core/SkCanvas.h +++ b/include/core/SkCanvas.h @@ -115,11 +115,12 @@ public: kARGB_ClipLayer_SaveFlag = 0x1F }; - /** This call saves the current matrix and clip information, and pushes a + /** This call saves the current matrix, clip, and drawFilter, and pushes a copy onto a private stack. Subsequent calls to translate, scale, - rotate, skew, concat or clipRect, clipPath all operate on this copy. - When the balancing call to restore() is made, this copy is deleted and - the previous matrix/clip state is restored. + rotate, skew, concat or clipRect, clipPath, and setDrawFilter all + operate on this copy. + When the balancing call to restore() is made, the previous matrix, clip, + and drawFilter are restored. @return The value to pass to restoreToCount() to balance this save() */ virtual int save(SaveFlags flags = kMatrixClip_SaveFlag); @@ -127,10 +128,7 @@ public: /** This behaves the same as save(), but in addition it allocates an offscreen bitmap. All drawing calls are directed there, and only when the balancing call to restore() is made is that offscreen transfered to - the canvas (or the previous layer). Subsequent calls to translate, - scale, rotate, skew, concat or clipRect, clipPath all operate on this - copy. When the balancing call to restore() is made, this copy is deleted - and the previous matrix/clip state is restored. + the canvas (or the previous layer). @param bounds (may be null) the maximum size the offscreen bitmap needs to be (in local coordinates) @param paint (may be null) This is copied, and is applied to the @@ -144,10 +142,7 @@ public: /** This behaves the same as save(), but in addition it allocates an offscreen bitmap. All drawing calls are directed there, and only when the balancing call to restore() is made is that offscreen transfered to - the canvas (or the previous layer). Subsequent calls to translate, - scale, rotate, skew, concat or clipRect, clipPath all operate on this - copy. When the balancing call to restore() is made, this copy is deleted - and the previous matrix/clip state is restored. + the canvas (or the previous layer). @param bounds (may be null) the maximum size the offscreen bitmap needs to be (in local coordinates) @param alpha This is applied to the offscreen when restore() is called. @@ -158,8 +153,9 @@ public: SaveFlags flags = kARGB_ClipLayer_SaveFlag); /** This call balances a previous call to save(), and is used to remove all - modifications to the matrix/clip state since the last save call. It is - an error to call restore() more times than save() was called. + modifications to the matrix/clip/drawFilter state since the last save + call. + It is an error to call restore() more times than save() was called. */ virtual void restore(); @@ -653,8 +649,7 @@ public: virtual SkBounder* setBounder(SkBounder* bounder); /** Get the current filter object. The filter's reference count is not - affected. The filter is part of the state this is affected by - save/restore. + affected. The filter is saved/restored, just like the matrix and clip. @return the canvas' filter (or NULL). */ SkDrawFilter* getDrawFilter() const; @@ -662,8 +657,8 @@ public: /** Set the new filter (or NULL). Pass NULL to clear any existing filter. As a convenience, the parameter is returned. If an existing filter exists, its refcnt is decrement. If the new filter is not null, its - refcnt is incremented. The filter is part of the state this is affected - by save/restore. + refcnt is incremented. The filter is saved/restored, just like the + matrix and clip. @param filter the new filter (or NULL) @return the new filter */ diff --git a/include/ports/SkHarfBuzzFont.h b/include/ports/SkHarfBuzzFont.h new file mode 100644 index 0000000000..b1fce0e86e --- /dev/null +++ b/include/ports/SkHarfBuzzFont.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2009, Google Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef SkHarfBuzzFont_DEFINED +#define SkHarfBuzzFont_DEFINED + +extern "C" { +#include "harfbuzz-shaper.h" +//#include "harfbuzz-unicode.h" +} + +#include "SkTypes.h" + +class SkPaint; +class SkTypeface; + +class SkHarfBuzzFont { +public: + /** The subclass returns the typeface for this font, or NULL + */ + virtual SkTypeface* getTypeface() const = 0; + /** The subclass sets the text related attributes of the paint. + e.g. textSize, typeface, textSkewX, etc. + All of the attributes that could effect how the text is measured. + Color information (e.g. color, xfermode, shader, etc.) are not required. + */ + virtual void setupPaint(SkPaint*) const = 0; + + /** Implementation of HB_GetFontTableFunc, using SkHarfBuzzFont* as + the first parameter. + */ + static HB_Error GetFontTableFunc(void* skharfbuzzfont, const HB_Tag tag, + HB_Byte* buffer, HB_UInt* len); + + static const HB_FontClass& GetFontClass(); +}; + +#endif + diff --git a/src/ports/SkHarfBuzzFont.cpp b/src/ports/SkHarfBuzzFont.cpp new file mode 100644 index 0000000000..bb229a1ad6 --- /dev/null +++ b/src/ports/SkHarfBuzzFont.cpp @@ -0,0 +1,195 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "SkHarfBuzzFont.h" +#include "SkFontHost.h" +#include "SkPaint.h" +#include "SkPath.h" + +// HB_Fixed is a 26.6 fixed point format. +static inline HB_Fixed SkScalarToHarfbuzzFixed(SkScalar value) { +#ifdef SK_SCALAR_IS_FLOAT + return static_cast(value * 64); +#else + // convert .16 to .6 + return value >> (16 - 6); +#endif +} + +static HB_Bool stringToGlyphs(HB_Font hbFont, const HB_UChar16* characters, + hb_uint32 length, HB_Glyph* glyphs, + hb_uint32* glyphsSize, HB_Bool isRTL) { + SkHarfBuzzFont* font = reinterpret_cast(hbFont->userData); + SkPaint paint; + + paint.setTypeface(font->getTypeface()); + paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); + int numGlyphs = paint.textToGlyphs(characters, length * sizeof(uint16_t), + reinterpret_cast(glyphs)); + + // HB_Glyph is 32-bit, but Skia outputs only 16-bit numbers. So our + // |glyphs| array needs to be converted. + for (int i = numGlyphs - 1; i >= 0; --i) { + uint16_t value; + // We use a memcpy to avoid breaking strict aliasing rules. + memcpy(&value, reinterpret_cast(glyphs) + sizeof(uint16_t) * i, sizeof(uint16_t)); + glyphs[i] = value; + } + + *glyphsSize = numGlyphs; + return 1; +} + +static void glyphsToAdvances(HB_Font hbFont, const HB_Glyph* glyphs, + hb_uint32 numGlyphs, HB_Fixed* advances, int flags) { + SkHarfBuzzFont* font = reinterpret_cast(hbFont->userData); + SkPaint paint; + + font->setupPaint(&paint); + paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); + + SkAutoMalloc storage(numGlyphs * (sizeof(SkScalar) + sizeof(uint16_t))); + SkScalar* scalarWidths = reinterpret_cast(storage.get()); + uint16_t* glyphs16 = reinterpret_cast(scalarWidths + numGlyphs); + + // convert HB 32bit glyphs to skia's 16bit + for (hb_uint32 i = 0; i < numGlyphs; ++i) { + glyphs16[i] = SkToU16(glyphs[i]); + } + paint.getTextWidths(glyphs16, numGlyphs * sizeof(uint16_t), scalarWidths); + + for (hb_uint32 i = 0; i < numGlyphs; ++i) { + advances[i] = SkScalarToHarfbuzzFixed(scalarWidths[i]); + } +} + +static HB_Bool canRender(HB_Font hbFont, const HB_UChar16* characters, + hb_uint32 length) { + SkHarfBuzzFont* font = reinterpret_cast(hbFont->userData); + SkPaint paint; + + paint.setTypeface(font->getTypeface()); + paint.setTextEncoding(SkPaint::kUTF16_TextEncoding); + return paint.containsText(characters, length * sizeof(uint16_t)); +} + +static HB_Error getOutlinePoint(HB_Font hbFont, HB_Glyph glyph, int flags, + hb_uint32 index, HB_Fixed* xPos, HB_Fixed* yPos, + hb_uint32* resultingNumPoints) { + SkHarfBuzzFont* font = reinterpret_cast(hbFont->userData); + SkPaint paint; + + font->setupPaint(&paint); + paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); + if (flags & HB_ShaperFlag_UseDesignMetrics) { + paint.setHinting(SkPaint::kNo_Hinting); + } + + SkPath path; + uint16_t glyph16 = SkToU16(glyph); + paint.getTextPath(&glyph16, sizeof(glyph16), 0, 0, &path); + int numPoints = path.countPoints(); + if (index >= numPoints) { + return HB_Err_Invalid_SubTable; + } + + SkPoint pt = path.getPoint(index); + *xPos = SkScalarToHarfbuzzFixed(pt.fX); + *yPos = SkScalarToHarfbuzzFixed(pt.fY); + *resultingNumPoints = numPoints; + + return HB_Err_Ok; +} + +static void getGlyphMetrics(HB_Font hbFont, HB_Glyph glyph, + HB_GlyphMetrics* metrics) { + SkHarfBuzzFont* font = reinterpret_cast(hbFont->userData); + SkPaint paint; + + font->setupPaint(&paint); + paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); + + SkScalar width; + SkRect bounds; + uint16_t glyph16 = SkToU16(glyph); + paint.getTextWidths(&glyph16, sizeof(glyph16), &width, &bounds); + + metrics->x = SkScalarToHarfbuzzFixed(bounds.fLeft); + metrics->y = SkScalarToHarfbuzzFixed(bounds.fTop); + metrics->width = SkScalarToHarfbuzzFixed(bounds.width()); + metrics->height = SkScalarToHarfbuzzFixed(bounds.height()); + + metrics->xOffset = SkScalarToHarfbuzzFixed(width); + // We can't actually get the |y| correct because Skia doesn't export + // the vertical advance. However, nor we do ever render vertical text at + // the moment so it's unimportant. + metrics->yOffset = 0; +} + +static HB_Fixed getFontMetric(HB_Font hbFont, HB_FontMetric metric) +{ + SkHarfBuzzFont* font = reinterpret_cast(hbFont->userData); + SkPaint paint; + SkPaint::FontMetrics skiaMetrics; + + font->setupPaint(&paint); + paint.getFontMetrics(&skiaMetrics); + + switch (metric) { + case HB_FontAscent: + return SkScalarToHarfbuzzFixed(-skiaMetrics.fAscent); + default: + SkDebugf("--- unknown harfbuzz metric enum %d\n", metric); + return 0; + } +} + +static HB_FontClass gSkHarfBuzzFontClass = { + stringToGlyphs, + glyphsToAdvances, + canRender, + getOutlinePoint, + getGlyphMetrics, + getFontMetric, +}; + +const HB_FontClass& SkHarfBuzzFont::GetFontClass() { + return gSkHarfBuzzFontClass; +} + +HB_Error SkHarfBuzzFont::GetFontTableFunc(void* voidface, const HB_Tag tag, + HB_Byte* buffer, HB_UInt* len) { + SkHarfBuzzFont* font = reinterpret_cast(voidface); + uint32_t uniqueID = SkTypeface::UniqueID(font->getTypeface()); + + const size_t tableSize = SkFontHost::GetTableSize(uniqueID, tag); + if (!tableSize) { + return HB_Err_Invalid_Argument; + } + // If Harfbuzz specified a NULL buffer then it's asking for the size. + if (!buffer) { + *len = tableSize; + return HB_Err_Ok; + } + + if (*len < tableSize) { + // is this right, or should we just copy less than the full table? + return HB_Err_Invalid_Argument; + } + SkFontHost::GetTableData(uniqueID, tag, 0, tableSize, buffer); + return HB_Err_Ok; +} +