Add example program from prototype

Change-Id: Ia6f6c442db57abe484a3efe33ecbc7553743bbb3
This commit is contained in:
Adam Barth
2017-05-11 09:38:03 -07:00
parent cdbb8b0c9d
commit 2697ab61cb
6 changed files with 188 additions and 4 deletions

View File

@@ -1,6 +1,16 @@
# Copyright 2017 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# 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.
group("txt") {
testonly = true
@@ -9,5 +19,6 @@ group("txt") {
"libs/minikin",
"src",
"tests/unittest($host_toolchain)",
"examples:txt_example($host_toolchain)",
]
}

View File

@@ -0,0 +1,27 @@
# 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.
executable("txt_example") {
sources = [
"main.cc",
]
deps = [
"//dart/runtime:libdart_jit",
"//flutter/fml",
"//lib/txt/src",
"//third_party/icu:icuuc",
"//third_party/skia",
]
}

View File

@@ -0,0 +1,73 @@
/*
* 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 <unicode/unistr.h>
#include <SkCanvas.h>
#include <SkGraphics.h>
#include <SkImageEncoder.h>
#include "flutter/fml/icu_util.h"
#include "lib/txt/src/paragraph_builder.h"
namespace txt {
int runTest() {
const char* utf8_text =
"fine world that we live in is called Earth. It's pretty nice, as far as "
"I can tell. Rock, rock on. "
"\xe0\xa4\xa8\xe0\xa4\xae\xe0\xa4\xb8\xe0\xa5\x8d\xe0\xa4\xa4\xe0\xa5"
"\x87";
icu::UnicodeString text = icu::UnicodeString::fromUTF8(utf8_text);
ParagraphStyle paragraph_style;
ParagraphBuilder builder(paragraph_style);
TextStyle style;
style.color = SK_ColorBLUE;
style.font_size = 32.0;
builder.PushStyle(style);
builder.AddText(text.getBuffer(), text.length());
style.color = SK_ColorYELLOW;
builder.PushStyle(style);
builder.AddText(text.getBuffer(), text.length());
builder.Pop();
builder.AddText(text.getBuffer(), text.length());
builder.Pop();
auto paragraph = builder.Build();
int width = 800;
int height = 600;
paragraph->Layout(width);
SkAutoGraphics ag;
SkBitmap bitmap;
bitmap.allocN32Pixels(width, height);
SkCanvas canvas(bitmap);
paragraph->Paint(&canvas, 10.0, 200.0);
SkFILEWStream file("foo.png");
return SkEncodeImage(&file, bitmap, SkEncodedImageFormat::kPNG, 100)
? EXIT_SUCCESS
: EXIT_FAILURE;
}
} // namespace txt
int main(int argc, const char** argv) {
fml::icu::InitializeICU();
return txt::runTest();
}

View File

@@ -27,12 +27,13 @@ static_library("src") {
"paragraph_builder.cc",
"paragraph_builder.h",
"paragraph_style.h",
"styled_runs.cc",
"styled_runs.h",
"text_align.h",
"text_style.h",
]
deps = [
public_deps = [
"//lib/ftl",
"//lib/txt/libs/minikin",
"//third_party/skia",

View File

@@ -84,6 +84,10 @@ hb_face_t* FontSkia::CreateHarfBuzzFace() const {
return hb_face_create_for_tables(GetTable, typeface_.get(), 0);
}
const std::vector<minikin::FontVariation>& FontSkia::GetAxes() const {
return variations_;
}
const sk_sp<SkTypeface>& FontSkia::GetSkTypeface() const {
return typeface_;
}

View File

@@ -0,0 +1,68 @@
/*
* Copyright (C) 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/styled_runs.h"
namespace txt {
StyledRuns::StyledRuns() = default;
StyledRuns::~StyledRuns() = default;
StyledRuns::StyledRuns(StyledRuns&& other) {
styles_.swap(other.styles_);
runs_.swap(other.runs_);
}
const StyledRuns& StyledRuns::operator=(StyledRuns&& other) {
styles_.swap(other.styles_);
runs_.swap(other.runs_);
return *this;
}
void StyledRuns::swap(StyledRuns& other) {
styles_.swap(other.styles_);
runs_.swap(other.runs_);
}
size_t StyledRuns::AddStyle(const TextStyle& style) {
const size_t style_index = styles_.size();
styles_.push_back(style);
return style_index;
}
void StyledRuns::StartRun(size_t style_index, size_t start) {
runs_.push_back(IndexedRun{style_index, start, start});
}
void StyledRuns::EndRunIfNeeded(size_t end) {
if (runs_.empty())
return;
IndexedRun& run = runs_.back();
if (run.start == end) {
// The run is empty. We can skip it.
runs_.pop_back();
} else {
run.end = end;
}
}
StyledRuns::Run StyledRuns::GetRun(size_t index) const {
const IndexedRun& run = runs_[index];
return Run{styles_[run.style_index], run.start, run.end};
}
} // namespace txt