Drawing e-reader covers with HTML Canvas: 16 grays, font subsets and titles without spaces
A Carta e-ink screen shows 16 levels of gray, so a smooth glow or gradient on a book cover collapses into a few flat steps with hard edges. What works is keeping ramps out of the lettering on e-ink covers and previewing the cover at 16 grays before it reaches the device.
More canvas traps showed up on the way: font subsets that are not loaded yet when you measure text, titles in scripts without spaces, and mixed reading directions.
I built NovelVerba, a tool that translates long Chinese, Japanese and Korean web novels from EPUB or TXT files. Books that start as a TXT file usually reach the reader as a blank tile with a file name, so I added a free cover maker: pick one of 17 genre templates or your own image, type the title, then download a JPG or attach it to an EPUB. It all runs on a canvas in the browser.
The title is in the reader's language, which for my users means Vietnamese with stacked tone marks, Thai with no spaces, and Arabic from right to left.
Sixteen grays turn a soft glow into rings
Each step on a 16-level screen is 17 values apart (255 / 15). A glow that fades across 40 values of brightness has two or three steps to land on, so instead of a halo you get a couple of flat rings.
The preview simulates that with a luma conversion that snaps to 16 levels:
const EINK_GRAY_LEVELS = 16;
function grayscale_canvas(ctx: CanvasRenderingContext2D, w: number, h: number, levels?: number) {
const image = ctx.getImageData(0, 0, w, h);
const data = image.data;
const step = levels === undefined ? 0 : 255 / (levels - 1);
for (let i = 0; i < data.length; i += 4) {
let gray = 0.299 * data[i] + 0.587 * data[i + 1] + 0.114 * data[i + 2];
if (step > 0) gray = Math.round(gray / step) * step;
data[i] = data[i + 1] = data[i + 2] = gray;
}
ctx.putImageData(image, 0, 0);
}
The snapping is for the preview only. The exported JPG keeps continuous gray, because the device converts the image for its own panel anyway, and quantizing it twice makes no sense.
The black and white mode changes the drawing itself, not just the colors. Gradient fills in the lettering become one flat ink color, glows are dropped, and any fading band behind the title goes too.
The template backgrounds are made with a calm area where the title sits, so nothing has to go behind the lettering. A user's own image has no such area, so in color mode it gets a soft band at the top, and a quick brightness check of the title area picks light or dark lettering as a starting point, with a manual switch when mid-tone art fools the guess.
Register every font subset, load only the one a title needs
The title face is Lora, split into latin, latin-ext and vietnamese subsets. My build merges all CSS into one stylesheet, so a CSS @font-face import would ship these faces on every page. Instead the tool registers FontFace objects with a unicodeRange when it loads:
for (const spec of LORA_FACES) {
document.fonts.add(new FontFace("Lora", `url(${spec.url})`, {
weight: "700",
style: spec.style,
unicodeRange: spec.unicode_range,
}));
}
Registering downloads nothing. The trap is measuring. Canvas layout depends on measureText, and document.fonts.load(font) without a string only fetches the latin subset. A Vietnamese title can then be measured in a fallback face, and the line breaks stop matching what gets drawn once the real subset arrives.
So every string that will be drawn goes in: document.fonts.load(font, title), and the same for author, series and volume. The preview also redraws if fonts were still loading when the first pass ran.
Wrapping titles that have no spaces
Splitting on spaces works for English and Vietnamese, not for Thai or Chinese. Intl.Segmenter gives word boundaries for both:
const segmenter = new Intl.Segmenter(undefined, { granularity: "word" });
for (const part of segmenter.segment(title.normalize("NFC"))) {
// part.segment, part.isWordLike
}
Punctuation needs one more rule: closing marks stick to the word before them and opening quotes to the word after, so no line ends on an opening mark.
The title then shrinks until it fits its box, and a word is never split. If one word, often a German compound, still does not fit at the smallest size, the tool says so and keeps the download disabled instead of cutting it.
Arabic, Thai, and a series line in two directions
- Letter spacing is forced to zero for Arabic and Thai, even when a template asks for tracking. Tracking breaks Arabic joins and pulls Thai marks away from their letters.
- Arabic is drawn with
ctx.direction = "rtl", and every font stack ends withNoto Sans Arabicand no generic family, because a generic serif could win the Arabic glyphs over the loaded face. - Series and volume share one line,
Series ยท 3, when it fits. Its base direction follows the series, likedir="auto"on the inputs, so a Latin series with an Arabic volume number does not flip.
Thai and Chinese titles use system fonts, since a full CJK web font runs to many megabytes. The cost is that the same title looks different on Windows, macOS and Android.
What is still open
- The 16-gray preview is an estimate, and I have not checked these covers on every device.
- System fonts make Thai and CJK covers inconsistent across machines.
If you have shipped canvas text for scripts you cannot bundle fonts for, how did you handle it? A subset per title, or the system font?
The cover maker is free at novelverba.com/novel-cover-maker. It runs in the browser, and your image and text never leave your browser.
Comments
No comments yet. Start the discussion.