Upstream version 11.39.266.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / fonts / harfbuzz / HarfBuzzFaceSkia.cpp
index b0daf53..e619155 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2012 Google Inc. All rights reserved.
+ * Copyright (c) 2014 BlackBerry Limited. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
 #include "SkUtils.h"
 #include "platform/fonts/FontPlatformData.h"
 #include "platform/fonts/SimpleFontData.h"
-#include "platform/fonts/GlyphBuffer.h"
 #include "platform/fonts/harfbuzz/HarfBuzzShaper.h"
 
 #include "hb.h"
 #include "wtf/HashMap.h"
 
-namespace WebCore {
+namespace blink {
 
 // Our implementation of the callbacks which HarfBuzz requires by using Skia
 // calls. See the HarfBuzz source for references about what these callbacks do.
@@ -86,6 +86,10 @@ static void SkiaGetGlyphWidthAndExtents(SkPaint* paint, hb_codepoint_t codepoint
 
 static hb_bool_t harfBuzzGetGlyph(hb_font_t* hbFont, void* fontData, hb_codepoint_t unicode, hb_codepoint_t variationSelector, hb_codepoint_t* glyph, void* userData)
 {
+    // Variation selectors not supported.
+    if (variationSelector)
+        return false;
+
     HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(fontData);
 
     WTF::HashMap<uint32_t, uint16_t>::AddResult result = hbFontData->m_glyphCacheForFaceCacheEntry->add(unicode, 0);
@@ -94,10 +98,10 @@ static hb_bool_t harfBuzzGetGlyph(hb_font_t* hbFont, void* fontData, hb_codepoin
         paint->setTextEncoding(SkPaint::kUTF32_TextEncoding);
         uint16_t glyph16;
         paint->textToGlyphs(&unicode, sizeof(hb_codepoint_t), &glyph16);
-        result.iterator->value = glyph16;
+        result.storedValue->value = glyph16;
         *glyph = glyph16;
     }
-    *glyph = result.iterator->value;
+    *glyph = result.storedValue->value;
     return !!*glyph;
 }
 
@@ -117,6 +121,50 @@ static hb_bool_t harfBuzzGetGlyphHorizontalOrigin(hb_font_t* hbFont, void* fontD
     return true;
 }
 
+static hb_position_t harfBuzzGetGlyphHorizontalKerning(hb_font_t*, void* fontData, hb_codepoint_t leftGlyph, hb_codepoint_t rightGlyph, void*)
+{
+    HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(fontData);
+    if (hbFontData->m_paint.isVerticalText()) {
+        // We don't support cross-stream kerning
+        return 0;
+    }
+
+    SkTypeface* typeface = hbFontData->m_paint.getTypeface();
+
+    const uint16_t glyphs[2] = { static_cast<uint16_t>(leftGlyph), static_cast<uint16_t>(rightGlyph) };
+    int32_t kerningAdjustments[1] = { 0 };
+
+    if (typeface->getKerningPairAdjustments(glyphs, 2, kerningAdjustments)) {
+        SkScalar upm = SkIntToScalar(typeface->getUnitsPerEm());
+        SkScalar size = hbFontData->m_paint.getTextSize();
+        return SkiaScalarToHarfBuzzPosition(SkScalarMulDiv(SkIntToScalar(kerningAdjustments[0]), size, upm));
+    }
+
+    return 0;
+}
+
+static hb_position_t harfBuzzGetGlyphVerticalKerning(hb_font_t*, void* fontData, hb_codepoint_t topGlyph, hb_codepoint_t bottomGlyph, void*)
+{
+    HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(fontData);
+    if (!hbFontData->m_paint.isVerticalText()) {
+        // We don't support cross-stream kerning
+        return 0;
+    }
+
+    SkTypeface* typeface = hbFontData->m_paint.getTypeface();
+
+    const uint16_t glyphs[2] = { static_cast<uint16_t>(topGlyph), static_cast<uint16_t>(bottomGlyph) };
+    int32_t kerningAdjustments[1] = { 0 };
+
+    if (typeface->getKerningPairAdjustments(glyphs, 2, kerningAdjustments)) {
+        SkScalar upm = SkIntToScalar(typeface->getUnitsPerEm());
+        SkScalar size = hbFontData->m_paint.getTextSize();
+        return SkiaScalarToHarfBuzzPosition(SkScalarMulDiv(SkIntToScalar(kerningAdjustments[0]), size, upm));
+    }
+
+    return 0;
+}
+
 static hb_bool_t harfBuzzGetGlyphExtents(hb_font_t* hbFont, void* fontData, hb_codepoint_t glyph, hb_glyph_extents_t* extents, void* userData)
 {
     HarfBuzzFontData* hbFontData = reinterpret_cast<HarfBuzzFontData*>(fontData);
@@ -135,7 +183,9 @@ static hb_font_funcs_t* harfBuzzSkiaGetFontFuncs()
         harfBuzzSkiaFontFuncs = hb_font_funcs_create();
         hb_font_funcs_set_glyph_func(harfBuzzSkiaFontFuncs, harfBuzzGetGlyph, 0, 0);
         hb_font_funcs_set_glyph_h_advance_func(harfBuzzSkiaFontFuncs, harfBuzzGetGlyphHorizontalAdvance, 0, 0);
+        hb_font_funcs_set_glyph_h_kerning_func(harfBuzzSkiaFontFuncs, harfBuzzGetGlyphHorizontalKerning, 0, 0);
         hb_font_funcs_set_glyph_h_origin_func(harfBuzzSkiaFontFuncs, harfBuzzGetGlyphHorizontalOrigin, 0, 0);
+        hb_font_funcs_set_glyph_v_kerning_func(harfBuzzSkiaFontFuncs, harfBuzzGetGlyphVerticalKerning, 0, 0);
         hb_font_funcs_set_glyph_extents_func(harfBuzzSkiaFontFuncs, harfBuzzGetGlyphExtents, 0, 0);
         hb_font_funcs_make_immutable(harfBuzzSkiaFontFuncs);
     }
@@ -188,9 +238,4 @@ hb_font_t* HarfBuzzFace::createFont()
     return font;
 }
 
-GlyphBufferAdvance HarfBuzzShaper::createGlyphBufferAdvance(float width, float height)
-{
-    return GlyphBufferAdvance(width, height);
-}
-
-} // namespace WebCore
+} // namespace blink