blob: 4dd67af49b3c10bc2a5a43f59f377416fb9226f9 [file] [log] [blame]
// Copyright 2015 Google Inc. All Rights Reserved.
// 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
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
#include "cobalt/layout/anonymous_block_box.h"
#include "cobalt/cssom/keyword_value.h"
#include "cobalt/layout/inline_formatting_context.h"
#include "cobalt/layout/used_style.h"
#include "cobalt/math/transform_2d.h"
#include "cobalt/render_tree/glyph_buffer.h"
#include "cobalt/render_tree/text_node.h"
namespace cobalt {
namespace layout {
const scoped_refptr<cssom::CSSComputedStyleDeclaration>&
BaseDirection base_direction, UsedStyleProvider* used_style_provider,
LayoutStatTracker* layout_stat_tracker)
: BlockContainerBox(css_computed_style_declaration, base_direction,
used_style_provider, layout_stat_tracker),
css_computed_style_declaration->data()->font_weight())) {}
Box::Level AnonymousBlockBox::GetLevel() const { return kBlockLevel; }
AnonymousBlockBox* AnonymousBlockBox::AsAnonymousBlockBox() { return this; }
const AnonymousBlockBox* AnonymousBlockBox::AsAnonymousBlockBox() const {
return this;
void AnonymousBlockBox::SplitBidiLevelRuns() {
for (Boxes::const_iterator child_box_iterator = child_boxes().begin();
child_box_iterator != child_boxes().end();) {
Box* child_box = *child_box_iterator;
if (child_box->TrySplitAtSecondBidiLevelRun()) {
child_box_iterator = InsertSplitSiblingOfDirectChild(child_box_iterator);
} else {
bool AnonymousBlockBox::HasTrailingLineBreak() const {
return !child_boxes().empty() && child_boxes().back()->HasTrailingLineBreak();
void AnonymousBlockBox::RenderAndAnimateContent(
render_tree::CompositionNode::Builder* border_node_builder,
ContainerBox* stacking_context) const {
ContainerBox::RenderAndAnimateContent(border_node_builder, stacking_context);
if (computed_style()->visibility() != cssom::KeywordValue::GetVisible()) {
// Only add the ellipses to the render tree if the font is visible. The font
// is treated as transparent if a font is currently being downloaded and
// hasn't timed out: "In cases where textual content is loaded before
// downloadable fonts are available, user agents may... render text
// transparently with fallback fonts to avoid a flash of text using a fallback
// font. In cases where the font download fails user agents must display text,
// simply leaving transparent text is considered non-conformant behavior."
if (!ellipses_coordinates_.empty() && used_font_->IsVisible()) {
render_tree::ColorRGBA used_color = GetUsedColor(computed_style()->color());
// Only render the ellipses if the color is not completely transparent.
if (used_color.a() > 0.0f) {
char16 ellipsis_value = used_font_->GetEllipsisValue();
scoped_refptr<render_tree::GlyphBuffer> glyph_buffer =
used_font_->CreateGlyphBuffer(&ellipsis_value, 1, false);
for (EllipsesCoordinates::const_iterator iterator =
iterator != ellipses_coordinates_.end(); ++iterator) {
const math::Vector2dF& ellipsis_coordinates = *iterator;
// The render tree API considers text coordinates to be a position of
// a baseline, offset the text node accordingly.
border_node_builder->AddChild(new render_tree::TextNode(
ellipsis_coordinates, glyph_buffer, used_color));
bool AnonymousBlockBox::TryAddChild(const scoped_refptr<Box>& /*child_box*/) {
return false;
void AnonymousBlockBox::AddInlineLevelChild(
const scoped_refptr<Box>& child_box) {
DCHECK(child_box->GetLevel() == kInlineLevel ||
void AnonymousBlockBox::DumpClassName(std::ostream* stream) const {
*stream << "AnonymousBlockBox ";
scoped_ptr<FormattingContext> AnonymousBlockBox::UpdateRectOfInFlowChildBoxes(
const LayoutParams& child_layout_params) {
// Do any processing needed prior to ellipsis placement on all of the
// children.
for (Boxes::const_iterator child_ellipsis_iterator = child_boxes().begin();
child_ellipsis_iterator != child_boxes().end();
++child_ellipsis_iterator) {
// If ellipses are enabled then retrieve the ellipsis width for the font;
// otherwise, set the width to 0, which indicates that ellipses are not being
// used.
float ellipsis_width =
AreEllipsesEnabled() ? used_font_->GetEllipsisWidth() : 0;
// Lay out child boxes in the normal flow.
scoped_ptr<InlineFormattingContext> inline_formatting_context(
new InlineFormattingContext(
computed_style()->line_height(), used_font_->GetFontMetrics(),
child_layout_params, GetBaseDirection(),
computed_style()->text_align(), computed_style()->font_size(),
Boxes::const_iterator child_box_iterator = child_boxes().begin();
while (child_box_iterator != child_boxes().end()) {
Box* child_box = *child_box_iterator;
// Attempt to add the child box to the inline formatting context.
Box* child_box_before_wrap =
// If |child_box_before_wrap| is non-NULL, then trying to add the child
// box caused a line wrap to occur, and |child_box_before_wrap| is set to
// the last box that was successfully placed on the line. This can
// potentially be any of the child boxes previously added. Any boxes
// following the returned box, including ones that were previously
// added, still need to be added to the inline formatting context.
if (child_box_before_wrap) {
// Iterate backwards until the last box added to the line is found, and
// then increment the iterator, so that it is pointing at the location
// of the first box to add the next time through the loop.
while (*child_box_iterator != child_box_before_wrap) {
// If |child_box_before_wrap| has a split sibling, then this potentially
// means that a split occurred during the wrap, and a new box needs to
// be added to the container (this will also need to be the first box
// added to the inline formatting context).
// If the split sibling is from a previous split, then it would have
// already been added to the line and |child_box_iterator| should
// be currently pointing at it. If this is not the case, then we know
// that this is a new box produced during the wrap, and it must be
// added to the container. This will be the first box added during
// the next iteration of the loop.
Box* split_child_after_wrap = child_box_before_wrap->GetSplitSibling();
Boxes::const_iterator next_child_box_iterator = child_box_iterator + 1;
if (split_child_after_wrap &&
(next_child_box_iterator == child_boxes().end() ||
*next_child_box_iterator != split_child_after_wrap)) {
child_box_iterator =
ellipses_coordinates_ = inline_formatting_context->GetEllipsesCoordinates();
// Do any processing needed following ellipsis placement on all of the
// children.
for (Boxes::const_iterator child_ellipsis_iterator = child_boxes().begin();
child_ellipsis_iterator != child_boxes().end();
++child_ellipsis_iterator) {
return inline_formatting_context.PassAs<FormattingContext>();
bool AnonymousBlockBox::AreEllipsesEnabled() const {
return parent()->computed_style()->overflow() ==
cssom::KeywordValue::GetHidden() &&
parent()->computed_style()->text_overflow() ==
} // namespace layout
} // namespace cobalt