diff --git a/engine/src/flutter/BUILD.gn b/engine/src/flutter/BUILD.gn index 4e19aa46f0..36804272fd 100644 --- a/engine/src/flutter/BUILD.gn +++ b/engine/src/flutter/BUILD.gn @@ -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)", ] } diff --git a/engine/src/flutter/examples/BUILD.gn b/engine/src/flutter/examples/BUILD.gn new file mode 100644 index 0000000000..bd19515179 --- /dev/null +++ b/engine/src/flutter/examples/BUILD.gn @@ -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", + ] +} diff --git a/engine/src/flutter/examples/main.cc b/engine/src/flutter/examples/main.cc new file mode 100644 index 0000000000..4d1b5b88a5 --- /dev/null +++ b/engine/src/flutter/examples/main.cc @@ -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 + +#include +#include +#include + +#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(); +} diff --git a/engine/src/flutter/src/BUILD.gn b/engine/src/flutter/src/BUILD.gn index a790b4e4d6..2d603bb38a 100644 --- a/engine/src/flutter/src/BUILD.gn +++ b/engine/src/flutter/src/BUILD.gn @@ -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", diff --git a/engine/src/flutter/src/font_skia.cc b/engine/src/flutter/src/font_skia.cc index 295a749bc2..c6b3bc8d8f 100644 --- a/engine/src/flutter/src/font_skia.cc +++ b/engine/src/flutter/src/font_skia.cc @@ -84,6 +84,10 @@ hb_face_t* FontSkia::CreateHarfBuzzFace() const { return hb_face_create_for_tables(GetTable, typeface_.get(), 0); } +const std::vector& FontSkia::GetAxes() const { + return variations_; +} + const sk_sp& FontSkia::GetSkTypeface() const { return typeface_; } diff --git a/engine/src/flutter/src/styled_runs.cc b/engine/src/flutter/src/styled_runs.cc new file mode 100644 index 0000000000..3f5f79da62 --- /dev/null +++ b/engine/src/flutter/src/styled_runs.cc @@ -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