2025-12-06 00:34:16 +11:00
|
|
|
#pragma once
|
|
|
|
|
#include "EpdFont.h"
|
|
|
|
|
|
|
|
|
|
class EpdFontFamily {
|
|
|
|
|
public:
|
feat: Add CSS parsing and CSS support in EPUBs (#411)
## Summary
* **What is the goal of this PR?**
- Adds basic CSS parsing to EPUBs and determine the CSS rules when
rendering to the screen so that text is styled correctly. Currently
supports bold, underline, italics, margin, padding, and text alignment
## Additional Context
- My main reason for wanting this is that the book I'm currently
reading, Carl's Doomsday Scenario (2nd in the Dungeon Crawler Carl
series), relies _a lot_ on styled text for telling parts of the story.
When text is bolded, it's supposed to be a message that's rendered
"on-screen" in the story. When characters are "chatting" with each
other, the text is bolded and their names are underlined. Plus, normal
emphasis is provided with italicizing words here and there. So, this
greatly improves my experience reading this book on the Xteink, and I
figured it was useful enough for others too.
- For transparency: I'm a software engineer, but I'm mostly frontend and
TypeScript/JavaScript. It's been _years_ since I did any C/C++, so I
would not be surprised if I'm doing something dumb along the way in this
code. Please don't hesitate to ask for changes if something looks off. I
heavily relied on Claude Code for help, and I had a lot of inspiration
from how [microreader](https://github.com/CidVonHighwind/microreader)
achieves their CSS parsing and styling. I did give this as good of a
code review as I could and went through everything, and _it works on my
machine_ 😄
### Before


### After


---
### AI Usage
Did you use AI tools to help write this code? **YES**, Claude Code
2026-02-05 05:28:10 -05:00
|
|
|
enum Style : uint8_t { REGULAR = 0, BOLD = 1, ITALIC = 2, BOLD_ITALIC = 3, UNDERLINE = 4 };
|
2025-12-31 12:11:36 +10:00
|
|
|
|
2025-12-06 00:34:16 +11:00
|
|
|
explicit EpdFontFamily(const EpdFont* regular, const EpdFont* bold = nullptr, const EpdFont* italic = nullptr,
|
|
|
|
|
const EpdFont* boldItalic = nullptr)
|
|
|
|
|
: regular(regular), bold(bold), italic(italic), boldItalic(boldItalic) {}
|
|
|
|
|
~EpdFontFamily() = default;
|
2025-12-31 12:11:36 +10:00
|
|
|
void getTextDimensions(const char* string, int* w, int* h, Style style = REGULAR) const;
|
|
|
|
|
const EpdFontData* getData(Style style = REGULAR) const;
|
|
|
|
|
const EpdGlyph* getGlyph(uint32_t cp, Style style = REGULAR) const;
|
feat: Support for kerning and ligatures (#873)
## Summary
**What is the goal of this PR?**
Improved typesetting, including
[kerning](https://en.wikipedia.org/wiki/Kerning) and
[ligatures](https://en.wikipedia.org/wiki/Ligature_(writing)#Latin_alphabet).
**What changes are included?**
- The script to convert built-in fonts now adds kerning and ligature
information to the generated font headers.
- Epub page layout calculates proper kerning spaces and makes ligature
substitutions according to the selected font.



## Additional Context
- I am not a typography expert.
- The implementation has been reworked from the earlier version, so it
is no longer necessary to omit Open Dyslexic, and kerning data now
covers all fonts, styles, and codepoints for which we include bitmap
data.
- Claude Opus 4.6 helped with a lot of this.
- There's an included test epub document with lots of kerning and
ligature examples, shown in the photos.
**_After some time to mature, I think this change is in decent shape to
merge and get people testing._**
After opening this PR I came across #660, which overlaps in adding
ligature support.
---
### AI Usage
While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.
Did you use AI tools to help write this code? _**YES, Claude Opus 4.6**_
---------
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-24 02:31:43 -06:00
|
|
|
int8_t getKerning(uint32_t leftCp, uint32_t rightCp, Style style = REGULAR) const;
|
|
|
|
|
uint32_t applyLigatures(uint32_t cp, const char*& text, Style style = REGULAR) const;
|
2025-12-31 12:11:36 +10:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const EpdFont* regular;
|
|
|
|
|
const EpdFont* bold;
|
|
|
|
|
const EpdFont* italic;
|
|
|
|
|
const EpdFont* boldItalic;
|
2025-12-06 00:34:16 +11:00
|
|
|
|
2025-12-31 12:11:36 +10:00
|
|
|
const EpdFont* getFont(Style style) const;
|
2025-12-06 00:34:16 +11:00
|
|
|
};
|