-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Ability to mutate run collection on the CanvasFormattedText
This change adds a set of APIs to allow manipulating the run collection on a CanvasFormattedText. set, append, insert, delete , get run are implemented. One major change here is to introduce CanvasFormattedTextRun as an interface. This is because we want to share identitiy of a run between javascript and C++, so that in the future we can support things like changing the color/font on a text run efficiently. That is consider the following snippet 1 var a = canvas_formatted_text[0]; 2 a.setColor("Blue"); 3 canvas_formatted_text[0] = a; if text runs are a dictionary, at line 3 the C++ code would see a totally new textrun and will not be able to re-use the layout text that backs it. This move to make text runs a object type, also results in making the above code easier to write as just 1 canvas_formatted_text[0].setColor("Blue") One other decision that was made, was to prohibit adding runs already in a CanvasFormattedText the same or another CanvasFormattedText. Doing so will throw a javascript exception. This is because supporting it would force us to make handling cases like 1 canvas_formatted_text[0] = canvas_formatted_text[1]; //not allowed tricky and non intuitive. -If we supported it by removing the run from index 1 and replacing the run in index 0, the developer may not expect that. -We can't support it by having both 0, 1 point to the same text run since layout block flow does not work that way. -The final option of creating a copy of 1 and placing it in 0 also may be un-intutive for the developer, setting color on 1 will not change color on 0 for example. Bug: 1155764 Change-Id: I65038365468e8b4fe3f894ca64d79042e2e60acd Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2693694 Commit-Queue: Sushanth Rajasankar <Sushraja@microsoft.com> Reviewed-by: Fernando Serboncini <fserb@chromium.org> Cr-Commit-Position: refs/heads/master@{#857794}
- Loading branch information
1 parent
8031f3f
commit 54637bb
Showing
14 changed files
with
648 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
third_party/blink/renderer/modules/canvas/canvas2d/canvas_formatted_text_run.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2021 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. | ||
#include "third_party/blink/renderer/modules/canvas/canvas2d/canvas_formatted_text_run.h" | ||
|
||
namespace blink { | ||
|
||
CanvasFormattedTextRun::CanvasFormattedTextRun( | ||
ExecutionContext* execution_context, | ||
const String text) | ||
: text_(text) { | ||
scoped_refptr<ComputedStyle> style = ComputedStyle::Create(); | ||
style->SetDisplay(EDisplay::kInline); | ||
// Refrain from extending the use of document, apart from creating layout | ||
// text. In the future we should handle execution_context's from worker | ||
// threads that do not have a document. | ||
auto* window = To<LocalDOMWindow>(execution_context); | ||
layout_text_ = | ||
LayoutText::CreateAnonymous(*(window->document()), std::move(style), | ||
text.Impl(), LegacyLayout::kAuto); | ||
layout_text_->SetIsLayoutNGObjectForCanvasFormattedText(true); | ||
} | ||
|
||
void CanvasFormattedTextRun::Dispose() { | ||
AllowDestroyingLayoutObjectInFinalizerScope scope; | ||
if (layout_text_) | ||
layout_text_->Destroy(); | ||
} | ||
|
||
void CanvasFormattedTextRun::Trace(Visitor* visitor) const { | ||
ScriptWrappable::Trace(visitor); | ||
} | ||
|
||
} // namespace blink |
50 changes: 50 additions & 0 deletions
50
third_party/blink/renderer/modules/canvas/canvas2d/canvas_formatted_text_run.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2021 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. | ||
|
||
#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_CANVAS_CANVAS2D_CANVAS_FORMATTED_TEXT_RUN_H_ | ||
#define THIRD_PARTY_BLINK_RENDERER_MODULES_CANVAS_CANVAS2D_CANVAS_FORMATTED_TEXT_RUN_H_ | ||
|
||
#include "third_party/blink/renderer/core/frame/local_dom_window.h" | ||
#include "third_party/blink/renderer/core/layout/layout_text.h" | ||
#include "third_party/blink/renderer/modules/modules_export.h" | ||
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h" | ||
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h" | ||
|
||
namespace blink { | ||
|
||
class MODULES_EXPORT CanvasFormattedTextRun final : public ScriptWrappable { | ||
DEFINE_WRAPPERTYPEINFO(); | ||
USING_PRE_FINALIZER(CanvasFormattedTextRun, Dispose); | ||
|
||
public: | ||
static CanvasFormattedTextRun* Create(ExecutionContext* execution_context, | ||
const String text) { | ||
return MakeGarbageCollected<CanvasFormattedTextRun>(execution_context, | ||
text); | ||
} | ||
|
||
CanvasFormattedTextRun(ExecutionContext*, const String text); | ||
CanvasFormattedTextRun(const CanvasFormattedTextRun&) = delete; | ||
CanvasFormattedTextRun& operator=(const CanvasFormattedTextRun&) = delete; | ||
|
||
String text() const { return text_; } | ||
void setText(const String text) { text_ = text; } | ||
|
||
unsigned length() const { return text_.length(); } | ||
|
||
LayoutText* GetLayoutObject() { return layout_text_; } | ||
|
||
void Trace(Visitor* visitor) const override; | ||
|
||
void Dispose(); | ||
|
||
private: | ||
String text_; | ||
|
||
LayoutText* layout_text_; | ||
}; | ||
|
||
} // namespace blink | ||
|
||
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_CANVAS_CANVAS2D_CANVAS_FORMATTED_TEXT_RUN_H_ |
13 changes: 8 additions & 5 deletions
13
third_party/blink/renderer/modules/canvas/canvas2d/canvas_formatted_text_run.idl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
// Copyright 2020 The Chromium Authors. All rights reserved. | ||
// Copyright 2021 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. | ||
[ | ||
RuntimeEnabled=CanvasFormattedText, | ||
Exposed=Window | ||
] | ||
interface CanvasFormattedTextRun{ | ||
[CallWith = ExecutionContext] constructor(DOMString text); | ||
|
||
dictionary CanvasFormattedTextRun { | ||
required DOMString text; | ||
DOMString font; | ||
DOMString color; | ||
attribute DOMString text; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.