Skip to content

Open source CapCut alternative.

TypeScript
9.5 KB
Raw Blame
"use client";

import {
  hexAlpha,
  WORD_BASELINE_DROP,
  renderElementCanvas as kitRenderElementCanvas,
  renderElementPng as kitRenderElementPng,
  renderStillPng as kitRenderStillPng,
  renderOverlayFrames as kitRenderOverlayFrames,
  LINE_HEIGHT,
  type OverlayFrameSet,
  type PaintPhase,
  type RenderEnv,
  type StickerImage,
  type WordDraw,
} from "@donkeycut/effects-kit";
import type { CSSProperties } from "react";
import { faceEmAscent } from "./fontEm";
import { allowance, holdMemory } from "./memoryBudget";
import { createRasterCanvas, decodeRasterImage, rasterCanvasToPng } from "./raster";
import { fontStack, type MediaAsset, type Overlay } from "./types";
import { UserError } from "./userError";

// The shared text metrics and painters live in the effects kit; these
// re-exports keep the app's preview components on the same constants.
export {
  LINE_HEIGHT,
  PLATE_COLOR,
  PLATE_FILL,
  PLATE_OPACITY,
  PLATE_PAD_X,
  PLATE_PAD_Y,
  PLATE_RADIUS,
  plateFill,
  SHADOW,
} from "@donkeycut/effects-kit";

/** The css one word wears — the DOM twin of what the canvas painter draws for
 * a resolved `WordDraw`. A resize is real type size, so the line reflows
 * around it, which is what the burn-in lays out too; the word keeps the
 * line's own leading, so a bigger word never opens the line box up. A box is
 * box-shadow spread, which stays clear of the layout. */
export function wordDrawCss(d: WordDraw, lineHeight = LINE_HEIGHT): CSSProperties {
  const css: CSSProperties = { color: d.color };
  if (d.font !== undefined) css.fontFamily = fontStack(d.font);
  if (d.italic !== undefined) css.fontStyle = d.italic ? "italic" : "normal";
  if (d.weight !== undefined) css.fontWeight = d.weight;
  if (d.opacity < 0.999) css.opacity = d.opacity;
  // The word takes its room at the layout size and draws at its own, which
  // differ only while it is growing into its place: real type size for the
  // room, so the line reflows around a swollen word exactly as the burn-in
  // lays it out, and a transform for the rest, which leaves the layout alone.
  if (d.layoutScale !== 1) {
    css.fontSize = `${d.layoutScale}em`;
    css.lineHeight = lineHeight / d.layoutScale;
  }
  const shrink = d.scale / d.layoutScale;
  // A word drawn smaller than the room it holds shares the line's baseline, so
  // it hangs lower in its box than a scale about the box center would leave
  // it; the drop makes up the difference. Offsets are em of the size the word
  // draws at and the box is em of the size it takes room at, so they convert
  // by the ratio between the two.
  const sit = (WORD_BASELINE_DROP * (d.layoutScale - d.scale)) / d.layoutScale;
  if (shrink !== 1 || d.dx || d.dy || d.rotate) {
    css.display = "inline-block";
    css.transform =
      `translate(${d.dx * shrink}em, ${d.dy * shrink + sit}em) rotate(${d.rotate}deg)` +
      (shrink !== 1 ? ` scale(${shrink})` : "");
  }
  const mark = d.markAlpha ?? 0;
  if (d.mark === "box" && mark > 0.001) {
    const fill = hexAlpha(d.markColor ?? "#000000", mark);
    css.background = fill;
    css.boxShadow = `0 0 0 0.12em ${fill}`;
    css.borderRadius = "0.18em";
    css.textShadow = "none";
  } else if (d.mark === "underline" && mark > 0.001) {
    css.textDecoration = "underline";
    css.textDecorationColor = hexAlpha(d.color, mark);
    css.textDecorationThickness = "0.07em";
    css.textUnderlineOffset = "0.14em";
  }
  return css;
}

/** Decoded sticker images by asset id, shared across one page's renders, in
 * the order they were last asked for. An <img> decode handles SVG too
 * (createImageBitmap on an SVG blob does not). */
const stickerCache = new Map<string, Promise<StickerImage | null>>();
/** What each decoded sticker costs, filled in as they land. The cache above
 * holds promises, which cannot be asked their size; this is what makes the
 * pictures the page is holding countable. */
const stickerBytes = new Map<string, number>();

/** What the decoded stickers may stand on: a cut whose clips carry different
 * stickers holds every one it has drawn, and a sticker is a full-size picture
 * however small it is placed. */
const STICKER_TUNED = 24 * 2 ** 20;

const stickersHeld = (): number => {
  let n = 0;
  for (const bytes of stickerBytes.values()) n += bytes;
  return n;
};

holdMemory("stickers", stickersHeld);

/**
 * Drop the stickers a render has gone longest without asking for, once the
 * decoded ones are past their share.
 *
 * The one just asked for is never dropped, and neither is a decode still in
 * the air — a sticker on screen is asked for again on the next frame, and
 * dropping it would decode it once a frame. What is dropped is a picture no
 * frame has reached for since, and it decodes again the moment one does.
 */
function sweepStickers(asked: string): void {
  const cap = allowance("stickers", STICKER_TUNED);
  let held = stickersHeld();
  if (held <= cap) return;
  for (const id of [...stickerCache.keys()]) {
    if (held <= cap) break;
    const bytes = stickerBytes.get(id);
    if (id === asked || bytes === undefined) continue;
    stickerCache.delete(id);
    stickerBytes.delete(id);
    held -= bytes;
  }
}

/** Why each sticker's last read came back empty, by asset id: what an export
 * names when it stops on one. */
const stickerMisses = new Map<string, string>();

async function decodeSticker(asset: MediaAsset): Promise<StickerImage | null> {
  try {
    const res = await fetch(asset.url);
    if (!res.ok) {
      throw new Error(`HTTP ${res.status}`);
    }
    const img = await decodeRasterImage(await res.blob());
    if (!img) {
      throw new Error("not a picture");
    }
    stickerMisses.delete(asset.id);
    return img;
  } catch (err) {
    stickerMisses.set(asset.id, err instanceof Error ? err.message : String(err));
    return null;
  }
}

/** The kit render env bound to this app: Cut's font stacks and their em
 * boxes, and sticker assets resolved from the given project media (their URLs
 * already point at the active backend — local engine or signed R2). */
export function cutRenderEnv(assets: MediaAsset[]): RenderEnv {
  return {
    fontStack,
    emAscent: faceEmAscent,
    createCanvas: (w, h) => createRasterCanvas(w, h) as HTMLCanvasElement,
    canvasToPngBlob: (canvas) => rasterCanvasToPng(canvas),
    resolveLottie: (assetId) =>
      import("./lottieAssets").then((m) => m.sharedLottieHandle(assetId, assets)),
    resolveAsset: (assetId) => {
      const asset = assets.find((a) => a.id === assetId);
      if (!asset) return Promise.resolve(null);
      let hit = stickerCache.get(asset.id);
      if (hit) {
        // Asked for again: move it to the young end of the cache.
        stickerCache.delete(asset.id);
      } else {
        hit = decodeSticker(asset).then((img) => {
          // A failed decode is not worth pinning; the next render retries.
          if (!img) stickerCache.delete(asset.id);
          else {
            stickerBytes.set(asset.id, img.width * img.height * 4);
            sweepStickers(asset.id);
          }
          return img;
        });
      }
      stickerCache.set(asset.id, hit);
      return hit;
    },
  };
}

/**
 * Read every sticker an export draws before its first frame. The preview skips
 * a sticker it cannot read and asks again on the next frame; an export draws
 * each frame once, so a sticker it cannot read stops it by name. The pictures
 * stay in the sticker cache for the frames that follow.
 */
export async function requireStickers(overlays: Overlay[], assets: MediaAsset[]): Promise<void> {
  const env = cutRenderEnv(assets);
  for (const o of overlays) {
    // Lottie stickers seek their own player, and an element with no asset
    // draws nothing in the preview either.
    if (o.kind !== "sticker" || o.hidden || o.lottie || !o.assetId) {
      continue;
    }
    const asset = assets.find((a) => a.id === o.assetId);
    if (!asset || (await env.resolveAsset!(asset.id))) {
      continue;
    }
    const why = stickerMisses.get(asset.id) ?? "no picture";
    const name = o.name ?? asset.name;
    throw new UserError("sticker_unreadable", `The sticker "${name}" could not be read (${why}).`, { name, reason: why });
  }
}

/**
 * Render an overlay element (text, shape, or sticker) to a transparent
 * full-frame PNG at the export resolution, matching the DOM preview's
 * metrics. `assets` supplies sticker bytes; text and shapes need none.
 */
export function renderElementPng(
  overlay: Overlay,
  width: number,
  height: number,
  assets: MediaAsset[] = [],
  phase?: PaintPhase
): Promise<Blob> {
  return kitRenderElementPng(overlay, width, height, cutRenderEnv(assets), phase);
}

/** An element that holds still as one full-frame PNG, its blur baked in. */
export function renderStillPng(overlay: Overlay, width: number, height: number, assets: MediaAsset[] = []): Promise<Blob> {
  return kitRenderStillPng(overlay, width, height, cutRenderEnv(assets));
}

/** The element drawn on a canvas at the export resolution, for a picture
 * drawn straight away. */
export function renderElementCanvas(
  overlay: Overlay,
  width: number,
  height: number,
  assets: MediaAsset[] = [],
  phase?: PaintPhase
): Promise<HTMLCanvasElement> {
  return kitRenderElementCanvas(overlay, width, height, cutRenderEnv(assets), phase);
}

/** Rasterize an animated element into its region-cropped frame set (the
 * export's ffconcat slideshow food), bound to this app's render env. */
export function renderElementFrames(
  overlay: Overlay,
  width: number,
  height: number,
  fps: number,
  assets: MediaAsset[] = []
): Promise<OverlayFrameSet> {
  return kitRenderOverlayFrames(overlay, width, height, fps, cutRenderEnv(assets));
}