Respect letter spacing and word spacing. Remove ParagraphConstraint.

Change-Id: Ie833eab67b50be40b7ea38f9c82577cb46e0e1a7
This commit is contained in:
Gary Qian
2017-06-16 16:32:50 -07:00
parent 1493b676ff
commit 6baa479ed0
7 changed files with 52 additions and 95 deletions

View File

@@ -38,8 +38,6 @@ static_library("txt") {
"src/paragraph.h",
"src/paragraph_builder.cc",
"src/paragraph_builder.h",
"src/paragraph_constraints.cc",
"src/paragraph_constraints.h",
"src/paragraph_style.cc",
"src/paragraph_style.h",
"src/styled_runs.cc",

View File

@@ -52,7 +52,7 @@ int runTest() {
int width = 800;
int height = 600;
paragraph->Layout(ParagraphConstraints(width));
paragraph->Layout(width);
SkAutoGraphics ag;
SkBitmap bitmap;

View File

@@ -128,12 +128,12 @@ void Paragraph::AddRunsToLineBreaker(const std::string& rootdir) {
}
}
void Paragraph::Layout(const ParagraphConstraints& constraints,
void Paragraph::Layout(double width,
const std::string& rootdir,
const double x_offset,
const double y_offset) {
breaker_.setLineWidths(0.0f, 0, constraints.width());
width_ = constraints.width();
breaker_.setLineWidths(0.0f, 0, width);
width_ = width;
AddRunsToLineBreaker(rootdir);
size_t breaks_count = breaker_.computeBreaks();
const int* breaks = breaker_.getBreaks();
@@ -149,12 +149,14 @@ void Paragraph::Layout(const ParagraphConstraints& constraints,
// Reset member variables so Layout still works when called more than once
max_intrinsic_width_ = 0.0f;
lines_ = 0;
lines_ = 1;
minikin::Layout layout;
SkScalar x = x_offset;
y_ = y_offset;
size_t break_index = 0;
double letter_spacing_offset = 0.0f;
double word_spacing_offset = 0.0f;
for (size_t run_index = 0; run_index < runs_.size(); ++run_index) {
auto run = runs_.GetRun(run_index);
auto collection =
@@ -163,8 +165,13 @@ void Paragraph::Layout(const ParagraphConstraints& constraints,
GetFontAndMinikinPaint(run.style, &font, &minikin_paint);
GetPaint(run.style, &paint);
// Subtract inital offset to avoid big gap at start of run.
letter_spacing_offset -= run.style.letter_spacing;
size_t layout_start = run.start;
while (layout_start < run.end && !DidExceedMaxLines()) {
while (layout_start < run.end &&
GetLineCount() <= paragraph_style_.max_lines) {
const size_t next_break = (break_index > breaks_count - 1)
? std::numeric_limits<size_t>::max()
: breaks[break_index];
@@ -187,11 +194,20 @@ void Paragraph::Layout(const ParagraphConstraints& constraints,
const size_t glyph_index = blob_start + blob_index;
buffer.glyphs[blob_index] = layout.getGlyphId(glyph_index);
const size_t pos_index = 2 * blob_index;
buffer.pos[pos_index] = layout.getX(glyph_index);
letter_spacing_offset += run.style.letter_spacing;
buffer.pos[pos_index] = layout.getX(glyph_index) +
letter_spacing_offset + word_spacing_offset;
buffer.pos[pos_index + 1] = layout.getY(glyph_index);
}
blob_start += blob_length;
max_intrinsic_width_ += layout.getX(blob_start - 1);
// Subtract letter offset to avoid big gap at end of run.
letter_spacing_offset -= run.style.letter_spacing;
word_spacing_offset += run.style.word_spacing;
max_intrinsic_width_ +=
layout.getX(blob_start - 1) + letter_spacing_offset;
}
// TODO(abarth): We could keep the same SkTextBlobBuilder as long as the
@@ -201,11 +217,15 @@ void Paragraph::Layout(const ParagraphConstraints& constraints,
if (layout_end == next_break) {
x = 0.0f;
letter_spacing_offset = 0.0f;
word_spacing_offset = 0.0f;
// TODO(abarth): Use the line height, which is something like the max
// font_size for runs in this line times the paragraph's line height.
y_ += run.style.font_size * run.style.height;
break_index += 1;
lines_++;
if (lines_ > paragraph_style_.max_lines)
lines_--;
} else {
x += layout.getAdvance();
}
@@ -220,10 +240,12 @@ const ParagraphStyle& Paragraph::GetParagraphStyle() const {
}
double Paragraph::GetAlphabeticBaseline() const {
// TODO(garyq): Implement.
return FLT_MAX;
}
double Paragraph::GetIdeographicBaseline() const {
// TODO(garyq): Implement.
return FLT_MAX;
}
@@ -232,6 +254,7 @@ double Paragraph::GetMaxIntrinsicWidth() const {
}
double Paragraph::GetMinIntrinsicWidth() const {
// TODO(garyq): Implement.
return min_intrinsic_width_;
}
@@ -253,10 +276,13 @@ void Paragraph::Paint(SkCanvas* canvas, double x, double y) {
}
}
int Paragraph::GetLineCount() const {
// FTL_LOG(INFO) << "Lines: " << lines_;
return lines_;
}
bool Paragraph::DidExceedMaxLines() const {
if (y_ / (paragraph_style_.font_size * paragraph_style_.line_height) >
paragraph_style_.max_lines)
// if (lines_ > paragraph_style_.max_lines)
if (GetLineCount() > paragraph_style_.max_lines)
return true;
return false;
}

View File

@@ -21,7 +21,6 @@
#include "lib/ftl/macros.h"
#include "lib/txt/src/paint_record.h"
#include "lib/txt/src/paragraph_constraints.h"
#include "lib/txt/src/paragraph_style.h"
#include "lib/txt/src/styled_runs.h"
#include "minikin/LineBreaker.h"
@@ -38,7 +37,7 @@ class Paragraph {
~Paragraph();
void Layout(const ParagraphConstraints& constraints,
void Layout(double width,
const std::string& rootdir = "",
const double x_offset = 0.0,
const double y_offset = 0.0);
@@ -57,6 +56,8 @@ class Paragraph {
double GetMinIntrinsicWidth() const;
int GetLineCount() const;
bool DidExceedMaxLines() const;
private:
@@ -76,9 +77,9 @@ class Paragraph {
ParagraphStyle paragraph_style_;
SkScalar y_ = 0.0f; // Height of the paragraph after Layout().
double width_ = 0.0f;
int lines_ = 0;
int lines_ = 1;
double max_intrinsic_width_ = 0.0f;
double min_intrinsic_width_ = FLT_MAX;
double min_intrinsic_width_ = 0;
void SetText(std::vector<uint16_t> text, StyledRuns runs);

View File

@@ -1,23 +0,0 @@
/*
* Copyright 2017 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "lib/txt/src/paragraph_constraints.h"
namespace txt {
ParagraphConstraints::ParagraphConstraints(double width) : width_(width) {}
} // namespace txt

View File

@@ -1,38 +0,0 @@
/*
* Copyright 2017 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LIB_TXT_SRC_PARAGRAPH_CONSTRAINTS_H_
#define LIB_TXT_SRC_PARAGRAPH_CONSTRAINTS_H_
#include "lib/ftl/macros.h"
namespace txt {
class ParagraphConstraints {
public:
explicit ParagraphConstraints(double width);
double width() const { return width_; }
private:
double width_;
FTL_DISALLOW_COPY_AND_ASSIGN(ParagraphConstraints);
};
} // namespace txt
#endif // LIB_TXT_SRC_PARAGRAPH_CONSTRAINTS_H_

View File

@@ -43,8 +43,7 @@ TEST_F(RenderTest, SimpleParagraph) {
builder.Pop();
auto paragraph = builder.Build();
paragraph->Layout(txt::ParagraphConstraints{GetTestCanvasWidth()},
txt::GetFontDir());
paragraph->Layout(GetTestCanvasWidth(), txt::GetFontDir());
paragraph->Paint(GetCanvas(), 10.0, 15.0);
@@ -77,8 +76,7 @@ TEST_F(RenderTest, SimpleRedParagraph) {
builder.Pop();
auto paragraph = builder.Build();
paragraph->Layout(txt::ParagraphConstraints{GetTestCanvasWidth()},
txt::GetFontDir());
paragraph->Layout(GetTestCanvasWidth(), txt::GetFontDir());
paragraph->Paint(GetCanvas(), 10.0, 15.0);
@@ -98,7 +96,7 @@ TEST_F(RenderTest, RainbowParagraph) {
auto icu_text1 = icu::UnicodeString::fromUTF8(text1);
std::u16string u16_text1(icu_text1.getBuffer(),
icu_text1.getBuffer() + icu_text1.length());
const char* text2 = "BigGreenDefault ";
const char* text2 = "Big Green Default ";
auto icu_text2 = icu::UnicodeString::fromUTF8(text2);
std::u16string u16_text2(icu_text2.getBuffer(),
icu_text2.getBuffer() + icu_text2.length());
@@ -129,8 +127,8 @@ TEST_F(RenderTest, RainbowParagraph) {
txt::TextStyle text_style2;
text_style2.font_size = 50;
// Letter spacing not yet implemented
text_style2.letter_spacing = 100;
text_style2.letter_spacing = 10;
text_style2.word_spacing = 30;
text_style2.font_weight = txt::FontWeight::w600;
text_style2.fake_bold = true;
text_style2.color = SK_ColorGREEN;
@@ -159,8 +157,7 @@ TEST_F(RenderTest, RainbowParagraph) {
builder.Pop();
auto paragraph = builder.Build();
paragraph->Layout(txt::ParagraphConstraints{GetTestCanvasWidth()},
txt::GetFontDir());
paragraph->Layout(GetTestCanvasWidth(), txt::GetFontDir());
paragraph->Paint(GetCanvas(), 10.0, 50.0);
@@ -199,8 +196,7 @@ TEST_F(RenderTest, DefaultStyleParagraph) {
builder.Pop();
auto paragraph = builder.Build();
paragraph->Layout(txt::ParagraphConstraints{GetTestCanvasWidth()},
txt::GetFontDir());
paragraph->Layout(GetTestCanvasWidth(), txt::GetFontDir());
paragraph->Paint(GetCanvas(), 10.0, 15.0);
@@ -236,8 +232,7 @@ TEST_F(RenderTest, BoldParagraph) {
builder.Pop();
auto paragraph = builder.Build();
paragraph->Layout(txt::ParagraphConstraints{GetTestCanvasWidth()},
txt::GetFontDir());
paragraph->Layout(GetTestCanvasWidth(), txt::GetFontDir());
paragraph->Paint(GetCanvas(), 10.0, 60.0);
@@ -302,8 +297,7 @@ TEST_F(RenderTest, LinebreakParagraph) {
builder.Pop();
auto paragraph = builder.Build();
paragraph->Layout(txt::ParagraphConstraints{GetTestCanvasWidth()},
txt::GetFontDir());
paragraph->Layout(GetTestCanvasWidth(), txt::GetFontDir());
paragraph->Paint(GetCanvas(), 5.0, 30.0);
@@ -338,8 +332,7 @@ TEST_F(RenderTest, ItalicsParagraph) {
builder.Pop();
auto paragraph = builder.Build();
paragraph->Layout(txt::ParagraphConstraints{GetTestCanvasWidth()},
txt::GetFontDir());
paragraph->Layout(GetTestCanvasWidth(), txt::GetFontDir());
paragraph->Paint(GetCanvas(), 10.0, 35.0);