aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/shape
diff options
context:
space:
mode:
authorGravatar Ben Wagner <bungeman@google.com>2018-01-25 14:37:17 -0500
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2018-01-25 21:28:22 +0000
commit5d4dd8bef5d681648d1eff081959531aab3ecd30 (patch)
tree01757adbeb4cede8877ee27506da503ef87348d2 /tools/shape
parentc1ad0d1dff947e96889c4448675924f2beb388d8 (diff)
SkShaper to account for line wrap.
Now that line wrap is possible, handle it. Change-Id: Ibe2c849cedfd91167c2d48352999052b61d47e9d Reviewed-on: https://skia-review.googlesource.com/99885 Reviewed-by: Hal Canary <halcanary@google.com> Commit-Queue: Ben Wagner <bungeman@google.com>
Diffstat (limited to 'tools/shape')
-rw-r--r--tools/shape/SkShaper.h2
-rw-r--r--tools/shape/SkShaper_harfbuzz.cpp34
-rw-r--r--tools/shape/SkShaper_primitive.cpp23
-rw-r--r--tools/shape/using_skia_and_harfbuzz.cpp9
4 files changed, 36 insertions, 32 deletions
diff --git a/tools/shape/SkShaper.h b/tools/shape/SkShaper.h
index ee6c88104d..190a4d834e 100644
--- a/tools/shape/SkShaper.h
+++ b/tools/shape/SkShaper.h
@@ -28,7 +28,7 @@ public:
~SkShaper();
bool good() const;
- SkScalar shape(SkTextBlobBuilder* dest,
+ SkPoint shape(SkTextBlobBuilder* dest,
const SkPaint& srcPaint,
const char* utf8text,
size_t textBytes,
diff --git a/tools/shape/SkShaper_harfbuzz.cpp b/tools/shape/SkShaper_harfbuzz.cpp
index 33b232ce3e..6315a96a02 100644
--- a/tools/shape/SkShaper_harfbuzz.cpp
+++ b/tools/shape/SkShaper_harfbuzz.cpp
@@ -472,20 +472,17 @@ bool SkShaper::good() const {
fImpl->fBreakIterator;
}
-SkScalar SkShaper::shape(SkTextBlobBuilder* builder,
- const SkPaint& srcPaint,
- const char* utf8,
- size_t utf8Bytes,
- bool leftToRight,
- SkPoint point,
- SkScalar width) const {
+SkPoint SkShaper::shape(SkTextBlobBuilder* builder,
+ const SkPaint& srcPaint,
+ const char* utf8,
+ size_t utf8Bytes,
+ bool leftToRight,
+ SkPoint point,
+ SkScalar width) const {
sk_sp<SkFontMgr> fontMgr = SkFontMgr::RefDefault();
SkASSERT(builder);
UBiDiLevel defaultLevel = leftToRight ? UBIDI_DEFAULT_LTR : UBIDI_DEFAULT_RTL;
//hb_script_t script = ...
- double x = point.x();
- //double y = point.y();
- SkPoint currentPoint = point;
SkTArray<ShapedRun> runs;
{
@@ -494,7 +491,7 @@ SkScalar SkShaper::shape(SkTextBlobBuilder* builder,
SkTLazy<BiDiRunIterator> maybeBidi(BiDiRunIterator::Make(utf8, utf8Bytes, defaultLevel));
BiDiRunIterator* bidi = maybeBidi.getMaybeNull();
if (!bidi) {
- return (SkScalar)x;
+ return point;
}
runSegmenter.insert(bidi);
@@ -502,7 +499,7 @@ SkScalar SkShaper::shape(SkTextBlobBuilder* builder,
SkTLazy<ScriptRunIterator> maybeScript(ScriptRunIterator::Make(utf8, utf8Bytes, hbUnicode));
ScriptRunIterator* script = maybeScript.getMaybeNull();
if (!script) {
- return (SkScalar)x;
+ return point;
}
runSegmenter.insert(script);
@@ -512,7 +509,7 @@ SkScalar SkShaper::shape(SkTextBlobBuilder* builder,
std::move(fontMgr)));
FontRunIterator* font = maybeFont.getMaybeNull();
if (!font) {
- return (SkScalar)x;
+ return point;
}
runSegmenter.insert(font);
@@ -524,13 +521,13 @@ SkScalar SkShaper::shape(SkTextBlobBuilder* builder,
std::unique_ptr<UText, SkFunctionWrapper<UText*, UText, utext_close>> autoClose(&utf8UText);
if (U_FAILURE(status)) {
SkDebugf("Could not create utf8UText: %s", u_errorName(status));
- return (SkScalar)x;
+ return point;
}
breakIterator.setText(&utf8UText, status);
//utext_close(&utf8UText);
if (U_FAILURE(status)) {
SkDebugf("Could not setText on break iterator: %s", u_errorName(status));
- return (SkScalar)x;
+ return point;
}
}
@@ -556,7 +553,7 @@ SkScalar SkShaper::shape(SkTextBlobBuilder* builder,
size_t utf8runLength = utf8End - utf8Start;
if (!SkTFitsIn<int>(utf8runLength)) {
SkDebugf("Shaping error: utf8 too long");
- return (SkScalar)x;
+ return point;
}
hb_buffer_set_script(buffer, script->currentScript());
hb_direction_t direction = is_LTR(bidi->currentLevel()) ? HB_DIRECTION_LTR:HB_DIRECTION_RTL;
@@ -580,7 +577,7 @@ SkScalar SkShaper::shape(SkTextBlobBuilder* builder,
if (!SkTFitsIn<int>(len)) {
SkDebugf("Shaping error: too many glyphs");
- return (SkScalar)x;
+ return point;
}
SkPaint paint(srcPaint);
@@ -663,6 +660,7 @@ SkScalar SkShaper::shape(SkTextBlobBuilder* builder,
}
// Reorder the runs and glyphs per line and write them out.
+ SkPoint currentPoint = point;
{
ShapedRunGlyphIterator previousBreak(runs);
ShapedRunGlyphIterator glyphIterator(runs);
@@ -721,5 +719,5 @@ SkScalar SkShaper::shape(SkTextBlobBuilder* builder,
}
}
- return (SkScalar)x;
+ return currentPoint;
}
diff --git a/tools/shape/SkShaper_primitive.cpp b/tools/shape/SkShaper_primitive.cpp
index f236d89a2a..06a8bec41c 100644
--- a/tools/shape/SkShaper_primitive.cpp
+++ b/tools/shape/SkShaper_primitive.cpp
@@ -29,13 +29,13 @@ unsigned utf8_lead_byte_to_count(const char* ptr) {
return (((0xE5 << 24) >> ((unsigned)c >> 4 << 1)) & 3) + 1;
}
-SkScalar SkShaper::shape(SkTextBlobBuilder* builder,
- const SkPaint& srcPaint,
- const char* utf8text,
- size_t textBytes,
- bool leftToRight,
- SkPoint point,
- SkScalar width) const {
+SkPoint SkShaper::shape(SkTextBlobBuilder* builder,
+ const SkPaint& srcPaint,
+ const char* utf8text,
+ size_t textBytes,
+ bool leftToRight,
+ SkPoint point,
+ SkScalar width) const {
sk_ignore_unused_variable(leftToRight);
SkPaint paint(srcPaint);
@@ -43,9 +43,12 @@ SkScalar SkShaper::shape(SkTextBlobBuilder* builder,
paint.setTextEncoding(SkPaint::kUTF8_TextEncoding);
int glyphCount = paint.countText(utf8text, textBytes);
if (glyphCount <= 0) {
- return 0;
+ return point;
}
SkRect bounds;
+ SkPaint::FontMetrics metrics;
+ paint.getFontMetrics(&metrics);
+ point.fY -= metrics.fAscent;
(void)paint.measureText(utf8text, textBytes, &bounds);
paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
const SkTextBlobBuilder::RunBuffer& runBuffer =
@@ -67,5 +70,7 @@ SkScalar SkShaper::shape(SkTextBlobBuilder* builder,
runBuffer.pos[i] = x;
x += w;
}
- return (SkScalar)x;
+ point.fY += metrics.fDescent + metrics.fLeading;
+
+ return point;
}
diff --git a/tools/shape/using_skia_and_harfbuzz.cpp b/tools/shape/using_skia_and_harfbuzz.cpp
index 0320009a3a..01d9a3ed86 100644
--- a/tools/shape/using_skia_and_harfbuzz.cpp
+++ b/tools/shape/using_skia_and_harfbuzz.cpp
@@ -112,7 +112,7 @@ struct Config {
DoubleOption font_size = DoubleOption("-z", "Font size", 8.0f);
DoubleOption left_margin = DoubleOption("-m", "Left margin", 20.0f);
DoubleOption line_spacing_ratio =
- DoubleOption("-h", "Line spacing ratio", 1.5f);
+ DoubleOption("-h", "Line spacing ratio", 0.25f);
StringOption output_file_name =
StringOption("-o", ".pdf output file name", "out-skiahf.pdf");
@@ -150,14 +150,15 @@ public:
current_y = config->line_spacing_ratio.value * config->font_size.value;
}
SkTextBlobBuilder textBlobBuilder;
- shaper.shape(&textBlobBuilder, glyph_paint, text, textBytes, true, SkPoint{0, 0},
- config->page_width.value - 2*config->left_margin.value);
+ SkPoint endPoint = shaper.shape(&textBlobBuilder, glyph_paint, text, textBytes, true,
+ SkPoint{0, 0},
+ config->page_width.value - 2*config->left_margin.value);
sk_sp<const SkTextBlob> blob = textBlobBuilder.make();
pageCanvas->drawTextBlob(
blob.get(), SkDoubleToScalar(current_x),
SkDoubleToScalar(current_y), glyph_paint);
// Advance to the next line.
- current_y += config->line_spacing_ratio.value * config->font_size.value;
+ current_y += endPoint.y() + config->line_spacing_ratio.value * config->font_size.value;
}
private: