Skip to content

Commit

Permalink
translated line height & letter spacing; Created new type for text po…
Browse files Browse the repository at this point in the history
…sitionData
  • Loading branch information
Cenadros committed Apr 25, 2024
1 parent 6595023 commit f217d3e
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 5 deletions.
7 changes: 5 additions & 2 deletions plugin-src/transformers/partials/transformTextStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import slugify from 'slugify';
import {
translateFontStyle,
translateHorizontalAlign,
translateLetterSpacing,
translateLineHeight,
translateTextDecoration,
translateTextTransform
} from '@plugin/translators';
Expand All @@ -26,7 +28,6 @@ export const transformTextStyle = (
| 'fills'
>
): Partial<TextStyle> => {
// @TODO: translate lineHeight and letterspacing
return {
fontFamily: segment.fontName.family,
fontId: `gfont-${slugify(segment.fontName.family.toLowerCase())}`,
Expand All @@ -36,6 +37,8 @@ export const transformTextStyle = (
fontVariantId: translateFontStyle(segment.fontName.style),
textAlign: translateHorizontalAlign(node.textAlignHorizontal),
textDecoration: translateTextDecoration(segment),
textTransform: translateTextTransform(segment)
textTransform: translateTextTransform(segment),
letterSpacing: translateLetterSpacing(segment),
lineHeight: translateLineHeight(segment)
};
};
3 changes: 2 additions & 1 deletion plugin-src/transformers/transformTextNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import {
translateGrowType,
translateStyledTextSegments,
translateTextPositionData,
translateVerticalAlign
} from '@plugin/translators';

Expand Down Expand Up @@ -50,7 +51,7 @@ export const transformTextNode = (node: TextNode, baseX: number, baseY: number):
]
},
growType: translateGrowType(node.textAutoResize),
positionData: segments,
positionData: translateTextPositionData(segments),
...transformDimensionAndPosition(node, baseX, baseY),
...transformEffects(node),
...transformSceneNode(node),
Expand Down
3 changes: 3 additions & 0 deletions plugin-src/translators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ export * from './translateFills';
export * from './translateFontStyle';
export * from './translateGrowType';
export * from './translateHorizontalAlign';
export * from './translateLetterSpacing';
export * from './translateLineHeight';
export * from './translateStrokes';
export * from './translateStyledTextSegments';
export * from './translateTextDecoration';
export * from './translateTextPositionData';
export * from './translateTextTransform';
export * from './translateVectorPaths';
export * from './translateVerticalAlign';
12 changes: 12 additions & 0 deletions plugin-src/translators/translateLetterSpacing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const translateLetterSpacing = (
segment: Pick<StyledTextSegment, 'letterSpacing' | 'fontSize'>
): number => {
switch (segment.letterSpacing.unit) {
case 'PIXELS':
return segment.letterSpacing.value;
case 'PERCENT':
return (segment.fontSize * segment.letterSpacing.value) / 100;
default:
return 0;
}
};
13 changes: 13 additions & 0 deletions plugin-src/translators/translateLineHeight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const translateLineHeight = (
segment: Pick<StyledTextSegment, 'lineHeight' | 'fontSize'>
): number => {
// Penpot reads lineHeight as a multiplier of the font size
switch (segment.lineHeight.unit) {
case 'PIXELS':
return segment.lineHeight.value / segment.fontSize;
case 'PERCENT':
return segment.lineHeight.value / 100;
default:
return 1.2; // This is the most common auto value for line-height
}
};
18 changes: 18 additions & 0 deletions plugin-src/translators/translateTextPositionData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { PositionData } from '@ui/lib/types/text/textAttributes';
import { TextNode } from '@ui/lib/types/text/textContent';

export const translateTextPositionData = (segments: TextNode[]): PositionData[] => {
return segments.map((segment): PositionData => {
return {
fills: segment.fills,
fontFamily: segment.fontFamily,
fontSize: segment.fontSize,
fontStyle: segment.fontStyle,
fontWeight: segment.fontWeight,
rtl: segment.direction === 'rtl',
text: segment.text,
textDecoration: segment.textDecoration,
textTransform: segment.textTransform
};
});
};
22 changes: 20 additions & 2 deletions ui-src/lib/types/text/textAttributes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
import { TextContent, TextNode } from './textContent';
import { Fill } from '../utils/fill';
import { TextContent } from './textContent';

export const TEXT_TYPE: unique symbol = Symbol.for('text');

export type TextAttributes = {
type: 'text' | typeof TEXT_TYPE;
content?: TextContent;
positionData: TextNode[];
positionData: PositionData[];
};

export type PositionData = {
// @TODO: figure out how to manage position and dimension
x?: number;
y?: number;
height?: number;
width?: number;
fills?: Fill[];
fontFamily?: string;
fontSize?: string;
fontStyle?: string;
fontWeight?: string;
rtl?: boolean;
text?: string;
textDecoration?: string;
textTransform?: string;
};

0 comments on commit f217d3e

Please sign in to comment.