From cfd41f986925eee9391a192038130b60eb3b5dd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20N=C3=B6lke?= Date: Sat, 19 May 2018 13:44:21 +0200 Subject: [PATCH] feat(paragraph): apply text transformation to a paragraph (#35) Fixes #34 --- CHANGELOG.md | 1 + README.md | 1 + src/OdfAttributeName.ts | 5 ++-- src/index.ts | 1 + src/style/ITextProperties.ts | 17 +++++++++++++ src/style/ParagraphStyle.ts | 12 ++++++++++ src/style/TextProperties.ts | 29 ++++++++++++++++++++++ src/style/TextTransformation.ts | 6 +++++ test/integration.spec.ts | 7 ++++++ test/style/TextProperties.spec.ts | 40 +++++++++++++++++++++++++++++++ 10 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 src/style/TextTransformation.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a9fe64a..b4b81ab4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 6a1b261b..99f863c7 100644 --- a/README.md +++ b/README.md @@ -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); diff --git a/src/OdfAttributeName.ts b/src/OdfAttributeName.ts index 021658f0..ff9c3e62 100644 --- a/src/OdfAttributeName.ts +++ b/src/OdfAttributeName.ts @@ -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", diff --git a/src/index.ts b/src/index.ts index 5f4e5b3e..fa5d9476 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 diff --git a/src/style/ITextProperties.ts b/src/style/ITextProperties.ts index 3b6876c4..88643a94 100644 --- a/src/style/ITextProperties.ts +++ b/src/style/ITextProperties.ts @@ -1,4 +1,5 @@ import { Color } from "./Color"; +import { TextTransformation } from "./TextTransformation"; import { Typeface } from "./Typeface"; /** @@ -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. * diff --git a/src/style/ParagraphStyle.ts b/src/style/ParagraphStyle.ts index 73e4f610..0d900aa6 100644 --- a/src/style/ParagraphStyle.ts +++ b/src/style/ParagraphStyle.ts @@ -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"; /** @@ -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); @@ -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"); diff --git a/src/style/TextProperties.ts b/src/style/TextProperties.ts index d2a2742e..cddb97de 100644 --- a/src/style/TextProperties.ts +++ b/src/style/TextProperties.ts @@ -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; /** @@ -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; /** @@ -27,6 +30,7 @@ export class TextProperties implements ITextProperties { */ public constructor() { this.fontSize = DEFAULT_FONT_SIZE; + this.transformation = DEFAULT_TRANSFORMATION; this.typeface = DEFAULT_TYPEFACE; } @@ -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; @@ -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; } @@ -103,6 +118,7 @@ export class TextProperties implements ITextProperties { this.setFontSizeAttribute(textPropertiesElement); this.setFontStyleAttribute(textPropertiesElement); this.setFontWeightAttribute(textPropertiesElement); + this.setTextTransformAttribute(textPropertiesElement); } /** @@ -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); + } } diff --git a/src/style/TextTransformation.ts b/src/style/TextTransformation.ts new file mode 100644 index 00000000..eae466a8 --- /dev/null +++ b/src/style/TextTransformation.ts @@ -0,0 +1,6 @@ +export enum TextTransformation { + None = "none", + Capitalize = "capitalize", + Lowercase = "lowercase", + Uppercase = "uppercase", +} diff --git a/test/integration.spec.ts b/test/integration.spec.ts index 041cbba6..e6b5f0df 100644 --- a/test/integration.spec.ts +++ b/test/integration.spec.ts @@ -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"; @@ -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()); diff --git a/test/style/TextProperties.spec.ts b/test/style/TextProperties.spec.ts index 9d6657f7..667cc498 100644 --- a/test/style/TextProperties.spec.ts +++ b/test/style/TextProperties.spec.ts @@ -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"; @@ -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); @@ -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); @@ -134,6 +153,27 @@ describe(TextProperties.name, () => { expect(document.toString()).toMatch(//); }); + it("set the text-transform for capitalize", () => { + testStyle.setTextTransformation(TextTransformation.Capitalize); + paragraph.setStyle(testStyle); + + expect(document.toString()).toMatch(//); + }); + + it("set the text-transform for lowercase", () => { + testStyle.setTextTransformation(TextTransformation.Lowercase); + paragraph.setStyle(testStyle); + + expect(document.toString()).toMatch(//); + }); + + it("set the text-transform for uppercase", () => { + testStyle.setTextTransformation(TextTransformation.Uppercase); + paragraph.setStyle(testStyle); + + expect(document.toString()).toMatch(//); + }); + it("set the font-style for italic", () => { testStyle.setTypeface(Typeface.Italic); paragraph.setStyle(testStyle);