Skip to content
Open
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
26 changes: 21 additions & 5 deletions src/utils/font_size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,30 @@ export const DEFAULT_FONT_SIZES = [

export const DEFAULT_FONT_SIZE = 'default';

const SIZE_PATTERN = /([\d.]+)px/i;
const SIZE_PATTERN = /([\d.]+)(px|pt|pc|in|mm|cm|%|em)/i;

type Conversion = {
[key: string]: number;
};

const toPixel:Conversion = {
px: 1,
pt: 4 / 3,
in: 96,
pc: 16,
mm: 3.78,
cm: 37.8,
em: 16,
'%': 16,
};

export function convertToPX (styleValue: string): string {
const matches = styleValue.match(SIZE_PATTERN);
if (!matches) return '';
const value = matches[1];
if (!value) return '';
return value;
if (!matches) return DEFAULT_FONT_SIZE;
if (!matches[2]) return DEFAULT_FONT_SIZE;
const value = parseFloat(matches[1]) * toPixel[matches[2]];
if (!value) return DEFAULT_FONT_SIZE;
return value.toString();
}

export function setFontSize (tr: Transaction, type: MarkType, fontSize: string): Transaction {
Expand Down