Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | |
| 8 #include <hb-ot.h> | |
| 9 | |
| 10 #include "SkShaper.h" | 7 #include "SkShaper.h" |
| 11 #include "SkStream.h" | 8 #include "SkStream.h" |
| 12 #include "SkTextBlob.h" | 9 #include "SkTextBlob.h" |
| 13 #include "SkTypeface.h" | 10 #include "SkTypeface.h" |
| 14 | 11 |
| 12 #ifdef SK_ENABLE_HARFBUZZ | |
|
bungeman-skia
2016/08/02 21:38:36
I know it's a bit more work, but these two impleme
hal.canary
2016/08/02 23:00:45
Done.
| |
| 13 | |
| 14 #include <hb-ot.h> | |
| 15 | |
| 15 static const int FONT_SIZE_SCALE = 512; | 16 static const int FONT_SIZE_SCALE = 512; |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 struct HBFBlobDel { | 19 struct HBFBlobDel { |
| 19 void operator()(hb_blob_t* b) { hb_blob_destroy(b); } | 20 void operator()(hb_blob_t* b) { hb_blob_destroy(b); } |
| 20 }; | 21 }; |
| 21 | 22 |
| 22 std::unique_ptr<hb_blob_t, HBFBlobDel> stream_to_blob(std::unique_ptr<SkStreamAs set> asset) { | 23 std::unique_ptr<hb_blob_t, HBFBlobDel> stream_to_blob(std::unique_ptr<SkStreamAs set> asset) { |
| 23 size_t size = asset->getLength(); | 24 size_t size = asset->getLength(); |
| 24 std::unique_ptr<hb_blob_t, HBFBlobDel> blob; | 25 std::unique_ptr<hb_blob_t, HBFBlobDel> blob; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 109 | 110 |
| 110 double x = point.x(); | 111 double x = point.x(); |
| 111 double y = point.y(); | 112 double y = point.y(); |
| 112 | 113 |
| 113 double textSizeY = paint.getTextSize() / (double)FONT_SIZE_SCALE; | 114 double textSizeY = paint.getTextSize() / (double)FONT_SIZE_SCALE; |
| 114 double textSizeX = textSizeY * paint.getTextScaleX(); | 115 double textSizeX = textSizeY * paint.getTextScaleX(); |
| 115 | 116 |
| 116 for (unsigned i = 0; i < len; i++) { | 117 for (unsigned i = 0; i < len; i++) { |
| 117 runBuffer.glyphs[i] = info[i].codepoint; | 118 runBuffer.glyphs[i] = info[i].codepoint; |
| 118 reinterpret_cast<SkPoint*>(runBuffer.pos)[i] = | 119 reinterpret_cast<SkPoint*>(runBuffer.pos)[i] = |
| 119 SkPoint::Make(x + pos[i].x_offset * textSizeX, | 120 SkPoint::Make(SkDoubleToScalar(x + pos[i].x_offset * textSizeX), |
| 120 y - pos[i].y_offset * textSizeY); | 121 SkDoubleToScalar(y - pos[i].y_offset * textSizeY)) ; |
| 121 x += pos[i].x_advance * textSizeX; | 122 x += pos[i].x_advance * textSizeX; |
| 122 y += pos[i].y_advance * textSizeY; | 123 y += pos[i].y_advance * textSizeY; |
| 123 } | 124 } |
| 124 hb_buffer_clear_contents(buffer); | 125 hb_buffer_clear_contents(buffer); |
| 125 return (SkScalar)x; | 126 return (SkScalar)x; |
| 126 } | 127 } |
| 128 | |
| 129 #else // !SK_ENABLE_HARFBUZZ | |
| 130 | |
| 131 struct SkShaper::Impl { | |
| 132 sk_sp<SkTypeface> fTypeface; | |
| 133 }; | |
| 134 | |
| 135 SkShaper::SkShaper(sk_sp<SkTypeface> tf) : fImpl(new Impl) { | |
| 136 fImpl->fTypeface = tf ? std::move(tf) : SkTypeface::MakeDefault(); | |
| 137 } | |
| 138 | |
| 139 SkShaper::~SkShaper() {} | |
| 140 | |
| 141 bool SkShaper::good() const { return true; } | |
| 142 | |
| 143 SkScalar SkShaper::shape(SkTextBlobBuilder* builder, | |
| 144 const SkPaint& srcPaint, | |
| 145 const char* utf8text, | |
| 146 size_t textBytes, | |
| 147 SkPoint point) const { | |
| 148 SkPaint paint(srcPaint); | |
| 149 paint.setTypeface(fImpl->fTypeface); | |
| 150 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); | |
| 151 int glyphCount = paint.countText(utf8text, textBytes); | |
| 152 if (glyphCount <= 0) { | |
| 153 return 0; | |
| 154 } | |
| 155 SkRect bounds; | |
| 156 (void)paint.measureText(utf8text, textBytes, &bounds); | |
| 157 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); | |
| 158 const SkTextBlobBuilder::RunBuffer& runBuffer = builder->allocRunPosH( | |
| 159 paint, glyphCount, point.y(), &bounds); | |
| 160 paint.setTextEncoding(SkPaint::kUTF8_TextEncoding); | |
| 161 (void)paint.textToGlyphs(utf8text, textBytes, runBuffer.glyphs); | |
| 162 (void)paint.getTextWidths(utf8text, textBytes, runBuffer.pos); | |
| 163 SkScalar x = point.x(); | |
| 164 for (int i = 0; i < glyphCount; ++i) { | |
| 165 SkScalar w = runBuffer.pos[i]; | |
| 166 runBuffer.pos[i] = x; | |
| 167 x += w; | |
| 168 } | |
| 169 return (SkScalar)x; | |
| 170 } | |
| 171 | |
| 172 #endif // !SK_ENABLE_HARFBUZZ | |
| OLD | NEW |