Home | History | Annotate | Download | only in skia
      1 /*
      2  * Copyright (c) 2008, 2009 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "GlyphPageTreeNode.h"
     33 
     34 #include "Font.h"
     35 #include "HarfbuzzSkia.h"
     36 #include "SimpleFontData.h"
     37 
     38 #include "SkTemplates.h"
     39 #include "SkPaint.h"
     40 #include "SkUtils.h"
     41 
     42 namespace WebCore {
     43 
     44 static int substituteWithVerticalGlyphs(const SimpleFontData* fontData, uint16_t* glyphs, unsigned bufferLength)
     45 {
     46     HB_FaceRec_* hbFace = fontData->platformData().harfbuzzFace();
     47     if (!hbFace->gsub) {
     48         // if there is no GSUB table, treat it as not covered
     49         return 0Xffff;
     50     }
     51 
     52     HB_Buffer buffer;
     53     hb_buffer_new(&buffer);
     54     for (unsigned i = 0; i < bufferLength; ++i)
     55         hb_buffer_add_glyph(buffer, glyphs[i], 0, i);
     56 
     57     HB_UShort scriptIndex;
     58     HB_UShort featureIndex;
     59 
     60     HB_GSUB_Select_Script(hbFace->gsub, HB_MAKE_TAG('D', 'F', 'L', 'T'), &scriptIndex);
     61     HB_GSUB_Select_Feature(hbFace->gsub, HB_MAKE_TAG('v', 'e', 'r', 't'), scriptIndex, 0xffff, &featureIndex);
     62     HB_GSUB_Add_Feature(hbFace->gsub, featureIndex, 1);
     63     HB_GSUB_Select_Feature(hbFace->gsub, HB_MAKE_TAG('v', 'r', 't', '2'), scriptIndex, 0xffff, &featureIndex);
     64     HB_GSUB_Add_Feature(hbFace->gsub, featureIndex, 1);
     65 
     66     int error = HB_GSUB_Apply_String(hbFace->gsub, buffer);
     67     if (!error) {
     68         for (unsigned i = 0; i < bufferLength; ++i)
     69             glyphs[i] = static_cast<Glyph>(buffer->out_string[i].gindex);
     70     }
     71     return error;
     72 }
     73 
     74 bool GlyphPage::fill(unsigned offset, unsigned length, UChar* buffer, unsigned bufferLength, const SimpleFontData* fontData)
     75 {
     76     if (SkUTF16_IsHighSurrogate(buffer[bufferLength-1])) {
     77         SkDebugf("%s last char is high-surrogate", __FUNCTION__);
     78         return false;
     79     }
     80 
     81     SkPaint paint;
     82     fontData->platformData().setupPaint(&paint);
     83     paint.setTextEncoding(SkPaint::kUTF16_TextEncoding);
     84 
     85     SkAutoSTMalloc <GlyphPage::size, uint16_t> glyphStorage(length);
     86     uint16_t* glyphs = glyphStorage.get();
     87     // textToGlyphs takes a byte count, not a glyph count so we multiply by two.
     88     unsigned count = paint.textToGlyphs(buffer, bufferLength * 2, glyphs);
     89     if (count != length) {
     90         SkDebugf("%s count != length\n", __FUNCTION__);
     91         return false;
     92     }
     93 
     94     if (fontData->hasVerticalGlyphs()) {
     95         bool lookVariants = false;
     96         for (unsigned i = 0; i < bufferLength; ++i) {
     97             if (!Font::isCJKIdeograph(buffer[i])) {
     98                 lookVariants = true;
     99                 continue;
    100             }
    101         }
    102         if (lookVariants)
    103             substituteWithVerticalGlyphs(fontData, glyphs, bufferLength);
    104     }
    105 
    106     unsigned allGlyphs = 0; // track if any of the glyphIDs are non-zero
    107     for (unsigned i = 0; i < length; i++) {
    108         setGlyphDataForIndex(offset + i, glyphs[i], glyphs[i] ? fontData : NULL);
    109         allGlyphs |= glyphs[i];
    110     }
    111 
    112     return allGlyphs != 0;
    113 }
    114 
    115 }  // namespace WebCore
    116