SkShaper: optionally disable harfbuzz
also, re-enable warnings. motivation: used by me for PDF testing. BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2201153002 Review-Url: https://codereview.chromium.org/2201153002
This commit is contained in:
parent
bc20a701d8
commit
13cba49982
@ -463,18 +463,22 @@
|
|||||||
{
|
{
|
||||||
'target_name': 'using_skia_and_harfbuzz',
|
'target_name': 'using_skia_and_harfbuzz',
|
||||||
'type': 'executable',
|
'type': 'executable',
|
||||||
'sources': [
|
'sources': [ '../tools/using_skia_and_harfbuzz.cpp', ],
|
||||||
'../tools/using_skia_and_harfbuzz.cpp',
|
'variables': { 'skia_example_use_harfbuzz%': 1, },
|
||||||
'../tools/SkShaper.cpp',
|
'conditions': [
|
||||||
|
[ 'skia_example_use_harfbuzz',
|
||||||
|
{
|
||||||
|
'dependencies': [ 'harfbuzz.gyp:harfbuzz', ],
|
||||||
|
'sources' : [ '../tools/SkShaper_harfbuzz.cpp', ],
|
||||||
|
}, {
|
||||||
|
'sources' : [ '../tools/SkShaper_primitive.cpp', ],
|
||||||
|
},
|
||||||
|
]
|
||||||
],
|
],
|
||||||
'dependencies': [
|
'dependencies': [
|
||||||
'skia_lib.gyp:skia_lib',
|
'skia_lib.gyp:skia_lib',
|
||||||
'pdf.gyp:pdf',
|
'pdf.gyp:pdf',
|
||||||
'harfbuzz.gyp:harfbuzz',
|
|
||||||
],
|
],
|
||||||
'cflags': [ '-w', ],
|
|
||||||
'msvs_settings': { 'VCCLCompilerTool': { 'WarningLevel': '0', }, },
|
|
||||||
'xcode_settings': { 'WARNING_CFLAGS': [ '-w', ], },
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'target_name': 'visualize_color_gamut',
|
'target_name': 'visualize_color_gamut',
|
||||||
|
@ -17,8 +17,10 @@ class SkPaint;
|
|||||||
class SkTextBlobBuilder;
|
class SkTextBlobBuilder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Shapes text using harfbuzz and places the shaped text into a
|
Shapes text using HarfBuzz and places the shaped text into a
|
||||||
TextBlob.
|
TextBlob.
|
||||||
|
|
||||||
|
If compiled without HarfBuzz, fall back on SkPaint::textToGlyphs.
|
||||||
*/
|
*/
|
||||||
class SkShaper {
|
class SkShaper {
|
||||||
public:
|
public:
|
||||||
|
@ -23,7 +23,7 @@ std::unique_ptr<hb_blob_t, HBFBlobDel> stream_to_blob(std::unique_ptr<SkStreamAs
|
|||||||
size_t size = asset->getLength();
|
size_t size = asset->getLength();
|
||||||
std::unique_ptr<hb_blob_t, HBFBlobDel> blob;
|
std::unique_ptr<hb_blob_t, HBFBlobDel> blob;
|
||||||
if (const void* base = asset->getMemoryBase()) {
|
if (const void* base = asset->getMemoryBase()) {
|
||||||
blob.reset(hb_blob_create((char*)base, size,
|
blob.reset(hb_blob_create((char*)base, SkToUInt(size),
|
||||||
HB_MEMORY_MODE_READONLY, asset.release(),
|
HB_MEMORY_MODE_READONLY, asset.release(),
|
||||||
[](void* p) { delete (SkStreamAsset*)p; }));
|
[](void* p) { delete (SkStreamAsset*)p; }));
|
||||||
} else {
|
} else {
|
||||||
@ -31,7 +31,7 @@ std::unique_ptr<hb_blob_t, HBFBlobDel> stream_to_blob(std::unique_ptr<SkStreamAs
|
|||||||
SkAutoMalloc autoMalloc(size);
|
SkAutoMalloc autoMalloc(size);
|
||||||
asset->read(autoMalloc.get(), size);
|
asset->read(autoMalloc.get(), size);
|
||||||
void* ptr = autoMalloc.get();
|
void* ptr = autoMalloc.get();
|
||||||
blob.reset(hb_blob_create((char*)autoMalloc.release(), size,
|
blob.reset(hb_blob_create((char*)autoMalloc.release(), SkToUInt(size),
|
||||||
HB_MEMORY_MODE_READONLY, ptr, sk_free));
|
HB_MEMORY_MODE_READONLY, ptr, sk_free));
|
||||||
}
|
}
|
||||||
SkASSERT(blob);
|
SkASSERT(blob);
|
||||||
@ -116,8 +116,8 @@ SkScalar SkShaper::shape(SkTextBlobBuilder* builder,
|
|||||||
for (unsigned i = 0; i < len; i++) {
|
for (unsigned i = 0; i < len; i++) {
|
||||||
runBuffer.glyphs[i] = info[i].codepoint;
|
runBuffer.glyphs[i] = info[i].codepoint;
|
||||||
reinterpret_cast<SkPoint*>(runBuffer.pos)[i] =
|
reinterpret_cast<SkPoint*>(runBuffer.pos)[i] =
|
||||||
SkPoint::Make(x + pos[i].x_offset * textSizeX,
|
SkPoint::Make(SkDoubleToScalar(x + pos[i].x_offset * textSizeX),
|
||||||
y - pos[i].y_offset * textSizeY);
|
SkDoubleToScalar(y - pos[i].y_offset * textSizeY));
|
||||||
x += pos[i].x_advance * textSizeX;
|
x += pos[i].x_advance * textSizeX;
|
||||||
y += pos[i].y_advance * textSizeY;
|
y += pos[i].y_advance * textSizeY;
|
||||||
}
|
}
|
51
tools/SkShaper_primitive.cpp
Normal file
51
tools/SkShaper_primitive.cpp
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2016 Google Inc.
|
||||||
|
*
|
||||||
|
* Use of this source code is governed by a BSD-style license that can be
|
||||||
|
* found in the LICENSE file.
|
||||||
|
*/
|
||||||
|
#include "SkShaper.h"
|
||||||
|
#include "SkStream.h"
|
||||||
|
#include "SkTextBlob.h"
|
||||||
|
#include "SkTypeface.h"
|
||||||
|
|
||||||
|
struct SkShaper::Impl {
|
||||||
|
sk_sp<SkTypeface> fTypeface;
|
||||||
|
};
|
||||||
|
|
||||||
|
SkShaper::SkShaper(sk_sp<SkTypeface> tf) : fImpl(new Impl) {
|
||||||
|
fImpl->fTypeface = tf ? std::move(tf) : SkTypeface::MakeDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
SkShaper::~SkShaper() {}
|
||||||
|
|
||||||
|
bool SkShaper::good() const { return true; }
|
||||||
|
|
||||||
|
SkScalar SkShaper::shape(SkTextBlobBuilder* builder,
|
||||||
|
const SkPaint& srcPaint,
|
||||||
|
const char* utf8text,
|
||||||
|
size_t textBytes,
|
||||||
|
SkPoint point) const {
|
||||||
|
SkPaint paint(srcPaint);
|
||||||
|
paint.setTypeface(fImpl->fTypeface);
|
||||||
|
paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
|
||||||
|
int glyphCount = paint.countText(utf8text, textBytes);
|
||||||
|
if (glyphCount <= 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
SkRect bounds;
|
||||||
|
(void)paint.measureText(utf8text, textBytes, &bounds);
|
||||||
|
paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
|
||||||
|
const SkTextBlobBuilder::RunBuffer& runBuffer = builder->allocRunPosH(
|
||||||
|
paint, glyphCount, point.y(), &bounds);
|
||||||
|
paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
|
||||||
|
(void)paint.textToGlyphs(utf8text, textBytes, runBuffer.glyphs);
|
||||||
|
(void)paint.getTextWidths(utf8text, textBytes, runBuffer.pos);
|
||||||
|
SkScalar x = point.x();
|
||||||
|
for (int i = 0; i < glyphCount; ++i) {
|
||||||
|
SkScalar w = runBuffer.pos[i];
|
||||||
|
runBuffer.pos[i] = x;
|
||||||
|
x += w;
|
||||||
|
}
|
||||||
|
return (SkScalar)x;
|
||||||
|
}
|
@ -134,7 +134,7 @@ public:
|
|||||||
glyph_paint.setColor(SK_ColorBLACK);
|
glyph_paint.setColor(SK_ColorBLACK);
|
||||||
glyph_paint.setFlags(SkPaint::kAntiAlias_Flag |
|
glyph_paint.setFlags(SkPaint::kAntiAlias_Flag |
|
||||||
SkPaint::kSubpixelText_Flag);
|
SkPaint::kSubpixelText_Flag);
|
||||||
glyph_paint.setTextSize(config->font_size.value);
|
glyph_paint.setTextSize(SkDoubleToScalar(config->font_size.value));
|
||||||
}
|
}
|
||||||
|
|
||||||
void WriteLine(const SkShaper& shaper, const char *text, size_t textBytes) {
|
void WriteLine(const SkShaper& shaper, const char *text, size_t textBytes) {
|
||||||
@ -142,8 +142,9 @@ public:
|
|||||||
if (pageCanvas) {
|
if (pageCanvas) {
|
||||||
document->endPage();
|
document->endPage();
|
||||||
}
|
}
|
||||||
pageCanvas = document->beginPage(config->page_width.value,
|
pageCanvas = document->beginPage(
|
||||||
config->page_height.value);
|
SkDoubleToScalar(config->page_width.value),
|
||||||
|
SkDoubleToScalar(config->page_height.value));
|
||||||
pageCanvas->drawPaint(white_paint);
|
pageCanvas->drawPaint(white_paint);
|
||||||
current_x = config->left_margin.value;
|
current_x = config->left_margin.value;
|
||||||
current_y = config->line_spacing_ratio.value * config->font_size.value;
|
current_y = config->line_spacing_ratio.value * config->font_size.value;
|
||||||
@ -151,8 +152,9 @@ public:
|
|||||||
SkTextBlobBuilder textBlobBuilder;
|
SkTextBlobBuilder textBlobBuilder;
|
||||||
shaper.shape(&textBlobBuilder, glyph_paint, text, textBytes, SkPoint{0, 0});
|
shaper.shape(&textBlobBuilder, glyph_paint, text, textBytes, SkPoint{0, 0});
|
||||||
sk_sp<const SkTextBlob> blob(textBlobBuilder.build());
|
sk_sp<const SkTextBlob> blob(textBlobBuilder.build());
|
||||||
pageCanvas->drawTextBlob(blob.get(), current_x, current_y, glyph_paint);
|
pageCanvas->drawTextBlob(
|
||||||
|
blob.get(), SkDoubleToScalar(current_x),
|
||||||
|
SkDoubleToScalar(current_y), glyph_paint);
|
||||||
// Advance to the next line.
|
// Advance to the next line.
|
||||||
current_y += config->line_spacing_ratio.value * config->font_size.value;
|
current_y += config->line_spacing_ratio.value * config->font_size.value;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user