Skip to content

Commit

Permalink
feat(paragraph): apply text transformation to a paragraph (#35)
Browse files Browse the repository at this point in the history
Fixes #34
  • Loading branch information
connium authored May 19, 2018
1 parent 90bb536 commit cfd41f9
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- **chore:** Add static code analysis with Better Code Hub
- **paragraph:** Set color, font size and typeface to the text of a paragraph, closes [#26](https://github.com/connium/simple-odf/issues/26)
- **paragraph:** Set font family to a paragraph, closes [#27](https://github.com/connium/simple-odf/issues/27)
- **paragraph:** Apply text transformation to a paragraph, closes [#34](https://github.com/connium/simple-odf/issues/34)

### Changed
- **chore:** Run TSLint on production and test code
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const style1 = new simpleOdf.ParagraphStyle();
// text formatting
style1.setColor(simpleOdf.Color.fromRgb(255, 0, 0));
style1.setFontSize(20);
style1.setTextTransformation(simpleOdf.TextTransformation.Uppercase);
style1.setTypeface(simpleOdf.Typeface.Bold);
// paragraph formatting
style1.setHorizontalAlignment(simpleOdf.HorizontalAlignment.Center);
Expand Down
5 changes: 3 additions & 2 deletions src/OdfAttributeName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ export enum OdfAttributeName {
FormatFontStyle = "fo:font-style",
FormatFontWeight = "fo:font-weight",
FormatTextAlign = "fo:text-align",

FormatTextTransform = "fo:text-transform",

OfficeMimetype = "office:mimetype",
OfficeVersion = "office:version",

StyleFamily = "style:family",
StyleFontName = "style:font-name",
StyleName = "style:name",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export { IParagraphStyle } from "./style/IParagraphStyle";
export { ParagraphStyle } from "./style/ParagraphStyle";
export { TabStop } from "./style/TabStop";
export { TabStopType } from "./style/TabStopType";
export { TextTransformation } from "./style/TextTransformation";
export { Typeface } from "./style/Typeface";

// text
Expand Down
17 changes: 17 additions & 0 deletions src/style/ITextProperties.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Color } from "./Color";
import { TextTransformation } from "./TextTransformation";
import { Typeface } from "./Typeface";

/**
Expand Down Expand Up @@ -76,6 +77,22 @@ export interface ITextProperties {
*/
getFontSize(): number;

/**
* Sets the transformation that will be applied to the text.
*
* @param {TextTransformation} transformation The transformation to apply
* @since 0.4.0
*/
setTextTransformation(transformation: TextTransformation): void;

/**
* Returns the transformation that will be applied to the text.
*
* @returns {TextTransformation} The transformation to apply
* @since 0.4.0
*/
getTextTransformation(): TextTransformation;

/**
* Sets the typeface that will be applied to the text.
*
Expand Down
12 changes: 12 additions & 0 deletions src/style/ParagraphStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { StyleHelper } from "./StyleHelper";
import { TabStop } from "./TabStop";
import { TabStopType } from "./TabStopType";
import { TextProperties } from "./TextProperties";
import { TextTransformation } from "./TextTransformation";
import { Typeface } from "./Typeface";

/**
Expand Down Expand Up @@ -58,6 +59,16 @@ export class ParagraphStyle implements IParagraphStyle {
return this.textProperties.getFontSize();
}

/** @inheritDoc */
public setTextTransformation(transformation: TextTransformation): void {
this.textProperties.setTextTransformation(transformation);
}

/** @inheritDoc */
public getTextTransformation(): TextTransformation {
return this.textProperties.getTextTransformation();
}

/** @inheritDoc */
public setTypeface(typeface: Typeface): void {
return this.textProperties.setTypeface(typeface);
Expand Down Expand Up @@ -141,6 +152,7 @@ export class ParagraphStyle implements IParagraphStyle {
hash.update(color !== undefined ? color.toHex() : "");
hash.update(this.textProperties.getFontName() || "");
hash.update(this.textProperties.getFontSize().toString());
hash.update(this.textProperties.getTextTransformation());
hash.update(this.textProperties.getTypeface().toString());

return hash.digest("hex");
Expand Down
29 changes: 29 additions & 0 deletions src/style/TextProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { OdfAttributeName } from "../OdfAttributeName";
import { OdfElementName } from "../OdfElementName";
import { Color } from "./Color";
import { ITextProperties } from "./ITextProperties";
import { TextTransformation } from "./TextTransformation";
import { Typeface } from "./Typeface";

const MINIMAL_FONT_SIZE = 2;
const DEFAULT_FONT_SIZE = 12;
const DEFAULT_TRANSFORMATION = TextTransformation.None;
const DEFAULT_TYPEFACE = Typeface.Normal;

/**
Expand All @@ -18,6 +20,7 @@ export class TextProperties implements ITextProperties {
private color: Color | undefined;
private fontName: string | undefined;
private fontSize: number;
private transformation: TextTransformation;
private typeface: Typeface;

/**
Expand All @@ -27,6 +30,7 @@ export class TextProperties implements ITextProperties {
*/
public constructor() {
this.fontSize = DEFAULT_FONT_SIZE;
this.transformation = DEFAULT_TRANSFORMATION;
this.typeface = DEFAULT_TYPEFACE;
}

Expand Down Expand Up @@ -60,6 +64,16 @@ export class TextProperties implements ITextProperties {
return this.fontSize;
}

/** @inheritDoc */
public setTextTransformation(transformation: TextTransformation): void {
this.transformation = transformation;
}

/** @inheritDoc */
public getTextTransformation(): TextTransformation {
return this.transformation;
}

/** @inheritDoc */
public setTypeface(typeface: Typeface): void {
this.typeface = typeface;
Expand All @@ -80,6 +94,7 @@ export class TextProperties implements ITextProperties {
return this.color === undefined
&& this.fontName === undefined
&& this.fontSize === DEFAULT_FONT_SIZE
&& this.transformation === DEFAULT_TRANSFORMATION
&& this.typeface === DEFAULT_TYPEFACE;
}

Expand All @@ -103,6 +118,7 @@ export class TextProperties implements ITextProperties {
this.setFontSizeAttribute(textPropertiesElement);
this.setFontStyleAttribute(textPropertiesElement);
this.setFontWeightAttribute(textPropertiesElement);
this.setTextTransformAttribute(textPropertiesElement);
}

/**
Expand Down Expand Up @@ -171,4 +187,17 @@ export class TextProperties implements ITextProperties {
textPropertiesElement.setAttribute(OdfAttributeName.FormatFontWeight, "bold");
}
}

/**
* Sets the `text-transform` attribute if a transformation is set.
*
* @param {Element} textPropertiesElement The element which will take the attribute
*/
private setTextTransformAttribute(textPropertiesElement: Element): void {
if (this.transformation === DEFAULT_TRANSFORMATION) {
return;
}

textPropertiesElement.setAttribute(OdfAttributeName.FormatTextTransform, this.transformation);
}
}
6 changes: 6 additions & 0 deletions src/style/TextTransformation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export enum TextTransformation {
None = "none",
Capitalize = "capitalize",
Lowercase = "lowercase",
Uppercase = "uppercase",
}
7 changes: 7 additions & 0 deletions test/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { HorizontalAlignment } from "../src/style/HorizontalAlignment";
import { ParagraphStyle } from "../src/style/ParagraphStyle";
import { TabStop } from "../src/style/TabStop";
import { TabStopType } from "../src/style/TabStopType";
import { TextTransformation } from "../src/style/TextTransformation";
import { Typeface } from "../src/style/Typeface";
import { TextDocument } from "../src/TextDocument";

Expand Down Expand Up @@ -87,6 +88,12 @@ xdescribe("integration", () => {
paragraph.getStyle().setFontSize(8);
});

it("text transformation", () => {
const paragraph = document.addParagraph("Some uppercase text");
paragraph.setStyle(new ParagraphStyle());
paragraph.getStyle().setTextTransformation(TextTransformation.Uppercase);
});

it("typeface", () => {
const paragraph = document.addParagraph("Some bold text");
paragraph.setStyle(new ParagraphStyle());
Expand Down
40 changes: 40 additions & 0 deletions test/style/TextProperties.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Color } from "../../src/style/Color";
import { ParagraphStyle } from "../../src/style/ParagraphStyle";
import { TextProperties } from "../../src/style/TextProperties";
import { TextTransformation } from "../../src/style/TextTransformation";
import { Typeface } from "../../src/style/Typeface";
import { Paragraph } from "../../src/text/Paragraph";
import { TextDocument } from "../../src/TextDocument";
Expand Down Expand Up @@ -65,6 +66,18 @@ describe(TextProperties.name, () => {
});
});

describe("#getTextTransformation", () => {
it("return `None` as default", () => {
expect(properties.getTextTransformation()).toBe(TextTransformation.None);
});

it("return the current transformation", () => {
properties.setTextTransformation(TextTransformation.Uppercase);

expect(properties.getTextTransformation()).toBe(TextTransformation.Uppercase);
});
});

describe("#getTypeface", () => {
it("return the current typeface", () => {
expect(properties.getTypeface()).toBe(Typeface.Normal);
Expand Down Expand Up @@ -98,6 +111,12 @@ describe(TextProperties.name, () => {
expect(properties.isDefault()).toBe(false);
});

it("return false if typeface was set", () => {
properties.setTextTransformation(TextTransformation.Uppercase);

expect(properties.isDefault()).toBe(false);
});

it("return false if typeface was set", () => {
properties.setTypeface(Typeface.BoldItalic);

Expand Down Expand Up @@ -134,6 +153,27 @@ describe(TextProperties.name, () => {
expect(document.toString()).toMatch(/<style:text-properties fo:font-size="23pt"\/>/);
});

it("set the text-transform for capitalize", () => {
testStyle.setTextTransformation(TextTransformation.Capitalize);
paragraph.setStyle(testStyle);

expect(document.toString()).toMatch(/<style:text-properties fo:text-transform="capitalize"\/>/);
});

it("set the text-transform for lowercase", () => {
testStyle.setTextTransformation(TextTransformation.Lowercase);
paragraph.setStyle(testStyle);

expect(document.toString()).toMatch(/<style:text-properties fo:text-transform="lowercase"\/>/);
});

it("set the text-transform for uppercase", () => {
testStyle.setTextTransformation(TextTransformation.Uppercase);
paragraph.setStyle(testStyle);

expect(document.toString()).toMatch(/<style:text-properties fo:text-transform="uppercase"\/>/);
});

it("set the font-style for italic", () => {
testStyle.setTypeface(Typeface.Italic);
paragraph.setStyle(testStyle);
Expand Down

0 comments on commit cfd41f9

Please sign in to comment.