Skip to content

Commit

Permalink
Added text basic properties (#61)
Browse files Browse the repository at this point in the history
* fixes in text nodes

* fix comment

* allow design.penpot.app

* wip

* wip

* add position data

* wip agaaain

* added translate growtype

* use a random url just to get a 404 on gfonts call

* leave the config, just in case someone wants to try it

* translated line height & letter spacing; Created new type for text positionData

* text strokes

* several fixes

* compiled penpot lib with fix

* updated penpot lib

* fix lint

* remove proxy

* correctly translate font style

* fix grow type to reflect vertical trim

* Fix font segments

* remove comment

* changeset

---------

Co-authored-by: Alex Sánchez <[email protected]>
  • Loading branch information
jordisala1991 and Cenadros authored Apr 29, 2024
1 parent 942b393 commit 881ccab
Show file tree
Hide file tree
Showing 19 changed files with 5,525 additions and 5,475 deletions.
5 changes: 5 additions & 0 deletions .changeset/famous-lions-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"penpot-exporter": minor
---

Added text basic properties
32 changes: 24 additions & 8 deletions plugin-src/transformers/partials/transformTextStyle.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { translateTextDecoration, translateTextTransform } from '@plugin/translators';
import slugify from 'slugify';

import {
translateFontStyle,
translateFontVariantId,
translateHorizontalAlign,
translateLetterSpacing,
translateLineHeight,
translateTextDecoration,
translateTextTransform
} from '@plugin/translators';

import { TextStyle } from '@ui/lib/types/text/textContent';

export const transformTextStyle = (
node: Pick<
node: TextNode,
segment: Pick<
StyledTextSegment,
| 'characters'
| 'start'
Expand All @@ -19,11 +30,16 @@ export const transformTextStyle = (
>
): Partial<TextStyle> => {
return {
fontFamily: node.fontName.family,
fontSize: node.fontSize.toString(),
fontStyle: node.fontName.style,
fontWeight: node.fontWeight.toString(),
textDecoration: translateTextDecoration(node),
textTransform: translateTextTransform(node)
fontFamily: segment.fontName.family,
fontId: `gfont-${slugify(segment.fontName.family.toLowerCase())}`,
fontSize: segment.fontSize.toString(),
fontStyle: translateFontStyle(segment.fontName.style),
fontWeight: segment.fontWeight.toString(),
fontVariantId: translateFontVariantId(segment.fontName.style),
textAlign: translateHorizontalAlign(node.textAlignHorizontal),
textDecoration: translateTextDecoration(segment),
textTransform: translateTextTransform(segment),
letterSpacing: translateLetterSpacing(segment),
lineHeight: translateLineHeight(segment)
};
};
16 changes: 12 additions & 4 deletions plugin-src/transformers/transformTextNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ import {
transformFills,
transformProportion,
transformSceneNode,
transformStrokes,
transformTextStyle
} from '@plugin/transformers/partials';
import { translateStyledTextSegments } from '@plugin/translators';
import {
translateGrowType,
translateStyledTextSegments,
translateVerticalAlign
} from '@plugin/translators';

import { TextShape } from '@ui/lib/types/text/textShape';

Expand All @@ -28,24 +33,27 @@ export const transformTextNode = (node: TextNode, baseX: number, baseY: number):
name: node.name,
content: {
type: 'root',
verticalAlign: translateVerticalAlign(node.textAlignVertical),
children: [
{
type: 'paragraph-set',
children: [
{
type: 'paragraph',
children: translateStyledTextSegments(styledTextSegments, node.width, node.height),
...(styledTextSegments.length ? transformTextStyle(styledTextSegments[0]) : {}),
children: translateStyledTextSegments(node, styledTextSegments),
...(styledTextSegments.length ? transformTextStyle(node, styledTextSegments[0]) : {}),
...transformFills(node)
}
]
}
]
},
growType: translateGrowType(node),
...transformDimensionAndPosition(node, baseX, baseY),
...transformEffects(node),
...transformSceneNode(node),
...transformBlend(node),
...transformProportion(node)
...transformProportion(node),
...transformStrokes(node)
};
};
7 changes: 7 additions & 0 deletions plugin-src/translators/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
export * from './translateBlendMode';
export * from './translateShadowEffects';
export * from './translateFills';
export * from './translateFontStyle';
export * from './translateFontVariantId';
export * from './translateGrowType';
export * from './translateHorizontalAlign';
export * from './translateLetterSpacing';
export * from './translateLineHeight';
export * from './translateStrokes';
export * from './translateStyledTextSegments';
export * from './translateTextDecoration';
export * from './translateTextTransform';
export * from './translateVectorPaths';
export * from './translateVerticalAlign';
9 changes: 9 additions & 0 deletions plugin-src/translators/translateFontStyle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { TextFontStyle } from '@ui/lib/types/text/textContent';

export const translateFontStyle = (style: string): TextFontStyle => {
if (style.toLowerCase().includes('italic')) {
return 'italic';
}

return 'normal';
};
3 changes: 3 additions & 0 deletions plugin-src/translators/translateFontVariantId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const translateFontVariantId = (style: string) => {
return style.toLowerCase().replace(/\s/g, '');
};
17 changes: 17 additions & 0 deletions plugin-src/translators/translateGrowType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { GrowType } from '@ui/lib/types/shape/shapeAttributes';

export const translateGrowType = (node: TextNode): GrowType => {
if (node.leadingTrim === 'CAP_HEIGHT') {
return 'fixed';
}

switch (node.textAutoResize) {
case 'WIDTH_AND_HEIGHT':
return 'auto-width';
case 'HEIGHT':
return 'auto-height';
case 'TRUNCATE':
default:
return 'fixed';
}
};
16 changes: 16 additions & 0 deletions plugin-src/translators/translateHorizontalAlign.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TextHorizontalAlign } from '@ui/lib/types/text/textContent';

export const translateHorizontalAlign = (
align: 'LEFT' | 'CENTER' | 'RIGHT' | 'JUSTIFIED'
): TextHorizontalAlign => {
switch (align) {
case 'RIGHT':
return 'right';
case 'CENTER':
return 'center';
case 'JUSTIFIED':
return 'justify';
default:
return 'left';
}
};
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;
}
};
10 changes: 10 additions & 0 deletions plugin-src/translators/translateLineHeight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const translateLineHeight = (
segment: Pick<StyledTextSegment, 'lineHeight' | 'fontSize'>
): number | undefined => {
switch (segment.lineHeight.unit) {
case 'PIXELS':
return segment.lineHeight.value / segment.fontSize;
case 'PERCENT':
return segment.lineHeight.value / 100;
}
};
13 changes: 6 additions & 7 deletions plugin-src/translators/translateStyledTextSegments.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { transformTextStyle } from '@plugin/transformers/partials';
import { translateFills } from '@plugin/translators/translateFills';

import { TextNode } from '@ui/lib/types/text/textContent';
import { TextNode as PenpotTextNode } from '@ui/lib/types/text/textContent';

export const translateStyledTextSegments = (
node: TextNode,
segments: Pick<
StyledTextSegment,
| 'characters'
Expand All @@ -17,17 +18,15 @@ export const translateStyledTextSegments = (
| 'textCase'
| 'textDecoration'
| 'fills'
>[],
width: number,
height: number
): TextNode[] => {
>[]
): PenpotTextNode[] => {
return segments.map(segment => {
figma.ui.postMessage({ type: 'FONT_NAME', data: segment.fontName.family });

return {
fills: translateFills(segment.fills, width, height),
fills: translateFills(segment.fills, node.width, node.height),
text: segment.characters,
...transformTextStyle(segment)
...transformTextStyle(node, segment)
};
});
};
12 changes: 12 additions & 0 deletions plugin-src/translators/translateVerticalAlign.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { TextVerticalAlign } from '@ui/lib/types/text/textContent';

export const translateVerticalAlign = (align: 'TOP' | 'CENTER' | 'BOTTOM'): TextVerticalAlign => {
switch (align) {
case 'BOTTOM':
return 'bottom';
case 'CENTER':
return 'center';
default:
return 'top';
}
};
Loading

0 comments on commit 881ccab

Please sign in to comment.