Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cpp/ActiveStrokeRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
#include <include/core/SkPaint.h>
#include <include/core/SkPoint.h>
#include <include/core/SkImage.h>
#include "SkiaDrawingEngine.h" // For Point struct
#include "DrawingTypes.h"
#include <string>

namespace nativedrawing {

Expand Down
170 changes: 170 additions & 0 deletions cpp/DrawingHistory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
#include "DrawingHistory.h"
#include "PathRenderer.h"
#include <algorithm>
#include <utility>

namespace nativedrawing {

void commitStrokeDelta(
std::vector<StrokeDelta>& undoStack,
std::vector<StrokeDelta>& redoStack,
StrokeDelta&& delta,
size_t maxHistoryEntries
) {
redoStack.clear();
undoStack.push_back(std::move(delta));
while (undoStack.size() > maxHistoryEntries) {
undoStack.erase(undoStack.begin());
}
}

void appendPixelEraseCircleToDelta(
std::vector<StrokeDelta::PixelEraseEntry>& pendingPixelEraseEntries,
size_t strokeIndex,
const EraserCircle& circle
) {
for (auto& entry : pendingPixelEraseEntries) {
if (entry.strokeIndex == strokeIndex) {
entry.addedCircles.push_back(circle);
return;
}
}

StrokeDelta::PixelEraseEntry entry;
entry.strokeIndex = strokeIndex;
entry.addedCircles.push_back(circle);
pendingPixelEraseEntries.push_back(std::move(entry));
}

void applyStrokeDelta(
const StrokeDelta& delta,
std::vector<Stroke>& strokes,
std::vector<EraserCircle>& eraserCircles,
PathRenderer& pathRenderer
) {
switch (delta.kind) {
case StrokeDelta::Kind::AddStrokes: {
for (const auto& stroke : delta.addedStrokes) {
strokes.push_back(stroke);
}
break;
}
case StrokeDelta::Kind::RemoveStrokes: {
for (auto it = delta.removedStrokes.rbegin(); it != delta.removedStrokes.rend(); ++it) {
if (it->first < strokes.size()) {
strokes.erase(strokes.begin() + static_cast<long>(it->first));
}
}
break;
}
case StrokeDelta::Kind::PixelErase: {
for (const auto& entry : delta.pixelEraseEntries) {
if (entry.strokeIndex >= strokes.size()) continue;
auto& target = strokes[entry.strokeIndex].erasedBy;
for (const auto& circle : entry.addedCircles) {
target.push_back(circle);
}
strokes[entry.strokeIndex].cachedEraserCount = 0;
}
break;
}
case StrokeDelta::Kind::MoveStrokes: {
for (size_t index : delta.moveIndices) {
if (index >= strokes.size()) continue;
auto& stroke = strokes[index];
for (auto& point : stroke.points) {
point.x += delta.moveDx;
point.y += delta.moveDy;
}
for (auto& circle : stroke.erasedBy) {
circle.x += delta.moveDx;
circle.y += delta.moveDy;
}
pathRenderer.smoothPath(stroke.points, stroke.path);
stroke.cachedEraserCount = 0;
}
break;
}
case StrokeDelta::Kind::ReplaceStrokes: {
for (const auto& [index, stroke] : delta.afterStrokes) {
if (index < strokes.size()) {
strokes[index] = stroke;
}
}
break;
}
case StrokeDelta::Kind::Clear: {
strokes.clear();
eraserCircles.clear();
break;
}
}
}

void revertStrokeDelta(
const StrokeDelta& delta,
std::vector<Stroke>& strokes,
std::vector<EraserCircle>& eraserCircles,
PathRenderer& pathRenderer
) {
switch (delta.kind) {
case StrokeDelta::Kind::AddStrokes: {
for (size_t i = 0; i < delta.addedStrokes.size(); ++i) {
if (!strokes.empty()) {
strokes.pop_back();
}
}
break;
}
case StrokeDelta::Kind::RemoveStrokes: {
for (const auto& [index, stroke] : delta.removedStrokes) {
size_t clamped = std::min(index, strokes.size());
strokes.insert(strokes.begin() + static_cast<long>(clamped), stroke);
}
break;
}
case StrokeDelta::Kind::PixelErase: {
for (const auto& entry : delta.pixelEraseEntries) {
if (entry.strokeIndex >= strokes.size()) continue;
auto& target = strokes[entry.strokeIndex].erasedBy;
for (size_t i = 0; i < entry.addedCircles.size() && !target.empty(); ++i) {
target.pop_back();
}
strokes[entry.strokeIndex].cachedEraserCount = 0;
}
break;
}
case StrokeDelta::Kind::MoveStrokes: {
for (size_t index : delta.moveIndices) {
if (index >= strokes.size()) continue;
auto& stroke = strokes[index];
for (auto& point : stroke.points) {
point.x -= delta.moveDx;
point.y -= delta.moveDy;
}
for (auto& circle : stroke.erasedBy) {
circle.x -= delta.moveDx;
circle.y -= delta.moveDy;
}
pathRenderer.smoothPath(stroke.points, stroke.path);
stroke.cachedEraserCount = 0;
}
break;
}
case StrokeDelta::Kind::ReplaceStrokes: {
for (const auto& [index, stroke] : delta.beforeStrokes) {
if (index < strokes.size()) {
strokes[index] = stroke;
}
}
break;
}
case StrokeDelta::Kind::Clear: {
strokes = delta.clearedStrokes;
eraserCircles = delta.clearedEraserCircles;
break;
}
}
}

} // namespace nativedrawing
37 changes: 37 additions & 0 deletions cpp/DrawingHistory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include "DrawingTypes.h"
#include <vector>

namespace nativedrawing {

class PathRenderer;

void commitStrokeDelta(
std::vector<StrokeDelta>& undoStack,
std::vector<StrokeDelta>& redoStack,
StrokeDelta&& delta,
size_t maxHistoryEntries
);

void appendPixelEraseCircleToDelta(
std::vector<StrokeDelta::PixelEraseEntry>& pendingPixelEraseEntries,
size_t strokeIndex,
const EraserCircle& circle
);

void applyStrokeDelta(
const StrokeDelta& delta,
std::vector<Stroke>& strokes,
std::vector<EraserCircle>& eraserCircles,
PathRenderer& pathRenderer
);

void revertStrokeDelta(
const StrokeDelta& delta,
std::vector<Stroke>& strokes,
std::vector<EraserCircle>& eraserCircles,
PathRenderer& pathRenderer
);

} // namespace nativedrawing
1 change: 1 addition & 0 deletions cpp/DrawingSelection.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "DrawingSelection.h"
#include "ShapeRecognition.h"
#include <include/effects/SkDashPathEffect.h>
#include <algorithm>
#include <cmath>
Expand Down
3 changes: 2 additions & 1 deletion cpp/DrawingSelection.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#include <unordered_set>
#include <limits>
#include <functional>
#include "SkiaDrawingEngine.h"
#include <include/core/SkCanvas.h>
#include "DrawingTypes.h"

namespace nativedrawing {

Expand Down
3 changes: 3 additions & 0 deletions cpp/DrawingSerialization.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "DrawingSerialization.h"
#include "ShapeRecognition.h"
#include <include/core/SkBlendMode.h>
#include <include/core/SkPathMeasure.h>
#include <algorithm>
#include <cstring>
#include <cstdio>
Expand Down
2 changes: 1 addition & 1 deletion cpp/DrawingSerialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <vector>
#include <cstdint>
#include "SkiaDrawingEngine.h"
#include "DrawingTypes.h"

namespace nativedrawing {

Expand Down
84 changes: 84 additions & 0 deletions cpp/DrawingTypes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "DrawingTypes.h"

#include <cmath>

#include <include/core/SkPathUtils.h>

namespace nativedrawing {

void Stroke::ensureEraserCacheValid() const {
if (erasedBy.size() == cachedEraserCount) {
return;
}

// Always rebuild from scratch to match live eraser rendering exactly.
cachedEraserPath.reset();
cachedEraserCount = 0;

if (erasedBy.empty()) {
return;
}

// Match EraserRenderer::drawEraserCirclesAsStrokes: group circles into
// strokes, create a path through centers, then stroke it with round caps.
constexpr float STROKE_BREAK_FACTOR = 2.0f;
size_t strokeStart = 0;

for (size_t i = 0; i <= erasedBy.size(); ++i) {
bool isLast = (i == erasedBy.size());
bool breakStroke = isLast;

if (!isLast && i > strokeStart) {
float dx = erasedBy[i].x - erasedBy[i - 1].x;
float dy = erasedBy[i].y - erasedBy[i - 1].y;
float dist = std::sqrt(dx * dx + dy * dy);
float avgRadius = (erasedBy[i].radius + erasedBy[i - 1].radius) / 2.0f;
if (dist > avgRadius * STROKE_BREAK_FACTOR) {
breakStroke = true;
}
}

if (breakStroke && i > strokeStart) {
size_t segmentLen = i - strokeStart;

if (segmentLen == 1) {
cachedEraserPath.addCircle(
erasedBy[strokeStart].x,
erasedBy[strokeStart].y,
erasedBy[strokeStart].radius
);
} else {
SkPath strokePath;
strokePath.moveTo(erasedBy[strokeStart].x, erasedBy[strokeStart].y);
for (size_t j = strokeStart + 1; j < i; ++j) {
strokePath.lineTo(erasedBy[j].x, erasedBy[j].y);
}

SkPaint strokePaint;
strokePaint.setStyle(SkPaint::kStroke_Style);
strokePaint.setStrokeWidth(erasedBy[strokeStart].radius * 2.0f);
strokePaint.setStrokeCap(SkPaint::kRound_Cap);
strokePaint.setStrokeJoin(SkPaint::kRound_Join);

SkPath filledPath;
if (skpathutils::FillPathWithPaint(strokePath, strokePaint, &filledPath)) {
cachedEraserPath.addPath(filledPath);
} else {
for (size_t j = strokeStart; j < i; ++j) {
cachedEraserPath.addCircle(
erasedBy[j].x,
erasedBy[j].y,
erasedBy[j].radius
);
}
}
}

strokeStart = i;
}
}

cachedEraserCount = erasedBy.size();
}

} // namespace nativedrawing
Loading
Loading