paragraph_builder.cc 14.4 KB
Newer Older
1 2 3 4
// Copyright 2015 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.

5
#include "flutter/lib/ui/text/paragraph_builder.h"
6

7
#include "flutter/common/settings.h"
8
#include "flutter/common/threads.h"
9
#include "flutter/lib/ui/ui_dart_state.h"
10 11 12 13 14
#include "flutter/sky/engine/core/rendering/RenderInline.h"
#include "flutter/sky/engine/core/rendering/RenderParagraph.h"
#include "flutter/sky/engine/core/rendering/RenderText.h"
#include "flutter/sky/engine/core/rendering/style/RenderStyle.h"
#include "flutter/sky/engine/platform/text/LocaleToScriptMapping.h"
15 16 17 18 19
#include "lib/ftl/tasks/task_runner.h"
#include "lib/tonic/converter/dart_converter.h"
#include "lib/tonic/dart_args.h"
#include "lib/tonic/dart_binding_macros.h"
#include "lib/tonic/dart_library_natives.h"
20 21 22 23 24 25
#include "lib/txt/src/font_style.h"
#include "lib/txt/src/font_weight.h"
#include "lib/txt/src/paragraph_style.h"
#include "lib/txt/src/text_align.h"
#include "lib/txt/src/text_decoration.h"
#include "lib/txt/src/text_style.h"
26

27
namespace blink {
28 29
namespace {

A
Adam Barth 已提交
30 31
// TextStyle

32 33 34 35 36 37
const int tsColorIndex = 1;
const int tsTextDecorationIndex = 2;
const int tsTextDecorationColorIndex = 3;
const int tsTextDecorationStyleIndex = 4;
const int tsFontWeightIndex = 5;
const int tsFontStyleIndex = 6;
A
Adam Barth 已提交
38 39 40 41 42 43
const int tsTextBaselineIndex = 7;
const int tsFontFamilyIndex = 8;
const int tsFontSizeIndex = 9;
const int tsLetterSpacingIndex = 10;
const int tsWordSpacingIndex = 11;
const int tsHeightIndex = 12;
44 45 46 47 48 49 50

const int tsColorMask = 1 << tsColorIndex;
const int tsTextDecorationMask = 1 << tsTextDecorationIndex;
const int tsTextDecorationColorMask = 1 << tsTextDecorationColorIndex;
const int tsTextDecorationStyleMask = 1 << tsTextDecorationStyleIndex;
const int tsFontWeightMask = 1 << tsFontWeightIndex;
const int tsFontStyleMask = 1 << tsFontStyleIndex;
A
Adam Barth 已提交
51
const int tsTextBaselineMask = 1 << tsTextBaselineIndex;
52 53 54 55
const int tsFontFamilyMask = 1 << tsFontFamilyIndex;
const int tsFontSizeMask = 1 << tsFontSizeIndex;
const int tsLetterSpacingMask = 1 << tsLetterSpacingIndex;
const int tsWordSpacingMask = 1 << tsWordSpacingIndex;
A
Adam Barth 已提交
56
const int tsHeightMask = 1 << tsHeightIndex;
A
Adam Barth 已提交
57 58 59

// ParagraphStyle

60
const int psTextAlignIndex = 1;
A
Adam Barth 已提交
61 62
const int psFontWeightIndex = 2;
const int psFontStyleIndex = 3;
63
const int psMaxLinesIndex = 4;
64 65 66
const int psFontFamilyIndex = 5;
const int psFontSizeIndex = 6;
const int psLineHeightIndex = 7;
67
const int psEllipsisIndex = 8;
A
Adam Barth 已提交
68

69
const int psTextAlignMask = 1 << psTextAlignIndex;
A
Adam Barth 已提交
70 71
const int psFontWeightMask = 1 << psFontWeightIndex;
const int psFontStyleMask = 1 << psFontStyleIndex;
72
const int psMaxLinesMask = 1 << psMaxLinesIndex;
A
Adam Barth 已提交
73 74
const int psFontFamilyMask = 1 << psFontFamilyIndex;
const int psFontSizeMask = 1 << psFontSizeIndex;
75
const int psLineHeightMask = 1 << psLineHeightIndex;
76
const int psEllipsisMask = 1 << psEllipsisIndex;
A
Adam Barth 已提交
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
float getComputedSizeFromSpecifiedSize(float specifiedSize) {
  if (specifiedSize < std::numeric_limits<float>::epsilon())
    return 0.0f;
  return specifiedSize;
}

void createFontForDocument(RenderStyle* style) {
  FontDescription fontDescription = FontDescription();
  fontDescription.setScript(
      localeToScriptCodeForFontSelection(style->locale()));

  // Using 14px default to match Material Design English Body1:
  // http://www.google.com/design/spec/style/typography.html#typography-typeface
  const float defaultFontSize = 14.0;

  fontDescription.setSpecifiedSize(defaultFontSize);
  fontDescription.setComputedSize(defaultFontSize);

  FontOrientation fontOrientation = Horizontal;
  NonCJKGlyphOrientation glyphOrientation = NonCJKGlyphOrientationVerticalRight;

  fontDescription.setOrientation(fontOrientation);
  fontDescription.setNonCJKGlyphOrientation(glyphOrientation);
  style->setFontDescription(fontDescription);
  style->font().update(UIDartState::Current()->font_selector());
}

105 106 107 108 109 110
PassRefPtr<RenderStyle> decodeParagraphStyle(RenderStyle* parentStyle,
                                             tonic::Int32List& encoded,
                                             const std::string& fontFamily,
                                             double fontSize,
                                             double lineHeight,
                                             const std::string& ellipsis) {
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  FTL_DCHECK(encoded.num_elements() == 5);

  RefPtr<RenderStyle> style = RenderStyle::create();
  style->inheritFrom(parentStyle);
  style->setDisplay(PARAGRAPH);

  int32_t mask = encoded[0];

  if (mask & psTextAlignMask)
    style->setTextAlign(static_cast<ETextAlign>(encoded[psTextAlignIndex]));

  if (mask & (psFontWeightMask | psFontStyleMask | psFontFamilyMask |
              psFontSizeMask)) {
    FontDescription fontDescription = style->fontDescription();

    if (mask & psFontWeightMask)
      fontDescription.setWeight(
          static_cast<FontWeight>(encoded[psFontWeightIndex]));

    if (mask & psFontStyleMask)
      fontDescription.setStyle(
          static_cast<FontStyle>(encoded[psFontStyleIndex]));

    if (mask & psFontFamilyMask) {
      FontFamily family;
      family.setFamily(String::fromUTF8(fontFamily));
      fontDescription.setFamily(family);
    }

    if (mask & psFontSizeMask) {
      fontDescription.setSpecifiedSize(fontSize);
      fontDescription.setIsAbsoluteSize(true);
      fontDescription.setComputedSize(
          getComputedSizeFromSpecifiedSize(fontSize));
    }

    style->setFontDescription(fontDescription);
    style->font().update(UIDartState::Current()->font_selector());
  }

  if (mask & psLineHeightMask)
    style->setLineHeight(Length(lineHeight * 100.0, Percent));

154
  if (mask & psMaxLinesMask)
155
    style->setMaxLines(encoded[psMaxLinesIndex]);
156 157

  if (mask & psEllipsisMask)
158 159 160 161 162 163 164 165 166 167
    style->setEllipsis(AtomicString::fromUTF8(ellipsis.c_str()));

  return style.release();
}

Color getColorFromARGB(int argb) {
  return Color((argb & 0x00FF0000) >> 16, (argb & 0x0000FF00) >> 8,
               (argb & 0x000000FF) >> 0, (argb & 0xFF000000) >> 24);
}

168
}  // namespace
169

A
Adam Barth 已提交
170 171 172 173
static void ParagraphBuilder_constructor(Dart_NativeArguments args) {
  DartCallConstructor(&ParagraphBuilder::create, args);
}

174
IMPLEMENT_WRAPPERTYPEINFO(ui, ParagraphBuilder);
A
Adam Barth 已提交
175

176
#define FOR_EACH_BINDING(V)      \
A
Adam Barth 已提交
177
  V(ParagraphBuilder, pushStyle) \
178 179
  V(ParagraphBuilder, pop)       \
  V(ParagraphBuilder, addText)   \
A
Adam Barth 已提交
180 181 182 183
  V(ParagraphBuilder, build)

FOR_EACH_BINDING(DART_NATIVE_CALLBACK)

184
void ParagraphBuilder::RegisterNatives(tonic::DartLibraryNatives* natives) {
185
  natives->Register(
186
      {{"ParagraphBuilder_constructor", ParagraphBuilder_constructor, 6, true},
187
       FOR_EACH_BINDING(DART_REGISTER_NATIVE)});
A
Adam Barth 已提交
188 189
}

190 191 192 193 194 195
ftl::RefPtr<ParagraphBuilder> ParagraphBuilder::create(
    tonic::Int32List& encoded,
    const std::string& fontFamily,
    double fontSize,
    double lineHeight,
    const std::string& ellipsis) {
196 197
  return ftl::MakeRefCounted<ParagraphBuilder>(encoded, fontFamily, fontSize,
                                               lineHeight, ellipsis);
198 199 200 201 202 203 204
}

ParagraphBuilder::ParagraphBuilder(tonic::Int32List& encoded,
                                   const std::string& fontFamily,
                                   double fontSize,
                                   double lineHeight,
                                   const std::string& ellipsis) {
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
  if (!Settings::Get().using_blink) {
    int32_t mask = encoded[0];
    txt::ParagraphStyle style;
    if (mask & psTextAlignMask)
      style.text_align = txt::TextAlign(encoded[psTextAlignIndex]);

    if (mask & (psFontWeightMask | psFontStyleMask | psFontFamilyMask |
                psFontSizeMask)) {
      if (mask & psFontWeightMask)
        style.font_weight =
            static_cast<txt::FontWeight>(encoded[psFontWeightIndex]);

      if (mask & psFontStyleMask)
        style.font_style =
            static_cast<txt::FontStyle>(encoded[psFontStyleIndex]);

      if (mask & psFontFamilyMask)
        style.font_family = fontFamily;

      if (mask & psFontSizeMask)
        style.font_size = fontSize;
    }
227

228 229
    if (mask & psLineHeightMask)
      style.line_height = lineHeight;
230

231 232
    if (mask & psMaxLinesMask)
      style.max_lines = encoded[psMaxLinesIndex];
233

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    if (mask & psEllipsisMask)
      style.ellipsis = ellipsis;

    m_paragraphBuilder.SetParagraphStyle(style);
  } else {
    // Blink version.
    createRenderView();

    RefPtr<RenderStyle> paragraphStyle =
        decodeParagraphStyle(m_renderView->style(), encoded, fontFamily,
                             fontSize, lineHeight, ellipsis);
    encoded.Release();

    m_renderParagraph = new RenderParagraph();
    m_renderParagraph->setStyle(paragraphStyle.release());

    m_currentRenderObject = m_renderParagraph;
    m_renderView->addChild(m_currentRenderObject);
  }

}  // namespace blink
255

256
ParagraphBuilder::~ParagraphBuilder() {
257 258
  if (m_renderView) {
    RenderView* renderView = m_renderView.leakPtr();
259
    Threads::UI()->PostTask([renderView]() { renderView->destroy(); });
260
  }
261 262
}

263 264 265 266 267 268
void ParagraphBuilder::pushStyle(tonic::Int32List& encoded,
                                 const std::string& fontFamily,
                                 double fontSize,
                                 double letterSpacing,
                                 double wordSpacing,
                                 double height) {
A
Adam Barth 已提交
269
  FTL_DCHECK(encoded.num_elements() == 8);
270 271 272

  int32_t mask = encoded[0];

273 274
  if (!Settings::Get().using_blink) {
    txt::TextStyle tstyle;
275

276 277
    if (mask & tsColorMask)
      tstyle.color = encoded[tsColorIndex];
278

279 280 281 282
    if (mask & tsTextDecorationMask) {
      tstyle.decoration =
          static_cast<txt::TextDecoration>(encoded[tsTextDecorationIndex]);
    }
283

284 285
    if (mask & tsTextDecorationColorMask)
      tstyle.decoration_color = encoded[tsTextDecorationColorIndex];
286

287 288 289
    if (mask & tsTextDecorationStyleMask)
      tstyle.decoration_style = static_cast<txt::TextDecorationStyle>(
          encoded[tsTextDecorationStyleIndex]);
A
Adam Barth 已提交
290

291 292 293 294
    if (mask & tsTextBaselineMask) {
      // TODO(abarth): Implement TextBaseline. The CSS version of this
      // property wasn't wired up either.
    }
A
Adam Barth 已提交
295

296 297 298 299 300
    if (mask & (tsFontWeightMask | tsFontStyleMask | tsFontFamilyMask |
                tsFontSizeMask | tsLetterSpacingMask | tsWordSpacingMask)) {
      if (mask & tsFontWeightMask)
        tstyle.font_weight =
            static_cast<txt::FontWeight>(encoded[tsFontWeightIndex]);
A
Adam Barth 已提交
301

302 303 304
      if (mask & tsFontStyleMask)
        tstyle.font_style =
            static_cast<txt::FontStyle>(encoded[tsFontStyleIndex]);
305

306 307 308 309 310 311 312 313 314 315 316
      if (mask & tsFontFamilyMask)
        tstyle.font_family = fontFamily;

      if (mask & tsFontSizeMask)
        tstyle.font_size = fontSize;

      if (mask & tsLetterSpacingMask)
        tstyle.letter_spacing = letterSpacing;

      if (mask & tsWordSpacingMask)
        tstyle.word_spacing = wordSpacing;
317
    }
A
Adam Barth 已提交
318

319 320
    if (mask & tsHeightMask) {
      tstyle.height = height;
321 322
    }

323 324 325 326 327
    m_paragraphBuilder.PushStyle(tstyle);
  } else {
    // Blink Version.
    RefPtr<RenderStyle> style = RenderStyle::create();
    style->inheritFrom(m_currentRenderObject->style());
A
Adam Barth 已提交
328

329 330
    if (mask & tsColorMask)
      style->setColor(getColorFromARGB(encoded[tsColorIndex]));
331

332 333 334 335 336
    if (mask & tsTextDecorationMask) {
      style->setTextDecoration(
          static_cast<TextDecoration>(encoded[tsTextDecorationIndex]));
      style->applyTextDecorations();
    }
337

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
    if (mask & tsTextDecorationColorMask)
      style->setTextDecorationColor(
          StyleColor(getColorFromARGB(encoded[tsTextDecorationColorIndex])));

    if (mask & tsTextDecorationStyleMask)
      style->setTextDecorationStyle(static_cast<TextDecorationStyle>(
          encoded[tsTextDecorationStyleIndex]));

    if (mask & tsTextBaselineMask) {
      // TODO(abarth): Implement TextBaseline. The CSS version of this
      // property wasn't wired up either.
    }

    if (mask & (tsFontWeightMask | tsFontStyleMask | tsFontFamilyMask |
                tsFontSizeMask | tsLetterSpacingMask | tsWordSpacingMask)) {
      FontDescription fontDescription = style->fontDescription();

      if (mask & tsFontWeightMask)
        fontDescription.setWeight(
            static_cast<FontWeight>(encoded[tsFontWeightIndex]));
358

359 360 361
      if (mask & tsFontStyleMask)
        fontDescription.setStyle(
            static_cast<FontStyle>(encoded[tsFontStyleIndex]));
362

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
      if (mask & tsFontFamilyMask) {
        FontFamily family;
        family.setFamily(String::fromUTF8(fontFamily));
        fontDescription.setFamily(family);
      }

      if (mask & tsFontSizeMask) {
        fontDescription.setSpecifiedSize(fontSize);
        fontDescription.setIsAbsoluteSize(true);
        fontDescription.setComputedSize(
            getComputedSizeFromSpecifiedSize(fontSize));
      }

      if (mask & tsLetterSpacingMask)
        fontDescription.setLetterSpacing(letterSpacing);

      if (mask & tsWordSpacingMask)
        fontDescription.setWordSpacing(wordSpacing);

      style->setFontDescription(fontDescription);
      style->font().update(UIDartState::Current()->font_selector());
    }

    if (mask & tsHeightMask) {
      style->setLineHeight(Length(height * 100.0, Percent));
    }

    encoded.Release();

    RenderObject* span = new RenderInline();
    span->setStyle(style.release());
    m_currentRenderObject->addChild(span);
    m_currentRenderObject = span;
  }
397 398
}

399
void ParagraphBuilder::pop() {
400 401 402 403 404 405 406
  if (!Settings::Get().using_blink) {
    m_paragraphBuilder.Pop();
  } else {
    // Blink Version.
    if (m_currentRenderObject)
      m_currentRenderObject = m_currentRenderObject->parent();
  }
407 408
}

409
void ParagraphBuilder::addText(const std::string& text) {
410 411 412 413 414 415 416 417 418 419 420 421
  if (!Settings::Get().using_blink) {
    m_paragraphBuilder.AddText(text);
  } else {
    // Blink Version.
    if (!m_currentRenderObject)
      return;
    RenderText* renderText = new RenderText(String::fromUTF8(text).impl());
    RefPtr<RenderStyle> style = RenderStyle::create();
    style->inheritFrom(m_currentRenderObject->style());
    renderText->setStyle(style.release());
    m_currentRenderObject->addChild(renderText);
  }
422 423
}

424
ftl::RefPtr<Paragraph> ParagraphBuilder::build() {
425
  m_currentRenderObject = nullptr;
426 427 428 429 430
  if (!Settings::Get().using_blink) {
    return Paragraph::Create(m_paragraphBuilder.Build());
  } else {
    return Paragraph::Create(m_renderView.release());
  }
431 432
}

433 434 435 436 437 438
void ParagraphBuilder::createRenderView() {
  RefPtr<RenderStyle> style = RenderStyle::create();
  style->setRTLOrdering(LogicalOrder);
  style->setZIndex(0);
  style->setUserModify(READ_ONLY);
  createFontForDocument(style.get());
A
Adam Barth 已提交
439

440 441
  m_renderView = adoptPtr(new RenderView());
  m_renderView->setStyle(style.release());
A
Adam Barth 已提交
442 443
}

444
}  // namespace blink