{"id":4730,"date":"2024-06-06T09:47:09","date_gmt":"2024-06-06T07:47:09","guid":{"rendered":"https:\/\/oopm.org\/?page_id=4730"},"modified":"2025-01-21T13:37:52","modified_gmt":"2025-01-21T12:37:52","slug":"10-3a-a-simple-text-formatter-system","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=4730","title":{"rendered":"10.3 A simple text formatter system"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages4730&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/pdf.png\" alt=\"image_pdf\" title=\"View PDF\" \/><\/a><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages4730&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"wp-block-paragraph\">In this section, we present another example of using nested classes. The domain is text formatting as known from e.g. Microsoft Word, and LaTex. The complexity of most of these systems is huge. Here we will thus stick to a very simple text formatting system.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The phenomena of the domain are documents that consists of a number of paragraphs. The paragraphs of a document are formatted according to a given template, which defines a number of styles for formating the paragraphs in the document. A style may e.g. define the font being used, the size of the font, the form of the font, etc. A paragraph may be left-aligned, centred, right-aligned, og with flushed margins.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In addition to styles, there are also properties associated with a document like page orientation (portrait or landscape), paper size (like A4, A3, and US letter), and number of columns. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To avoid ending up with a large example, we will keep the possible template and style properties to a minimum. Our text formatter can handle documents with two types of paragraphs: headings and body text. Headings are automatically numbered sequentially and body texts are indented by three spaces. A document has only one property which is the maximum length of a line in a paragraph.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Given a simple text formatter, represented by the class <code>SimpleTextFormatter<\/code>, it can be use as shown here.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>myFormatter: <strong>obj<\/strong> SimpleTextFormatter\n   doc: <strong>obj<\/strong> StandardDocument(StandardTemplate, 10)\n   doc.addHeading(\"Introduction\")\n   doc.addBodyText(\"Once upon a time, ...\")\n   doc.addHeading(\"The problem\")\n   doc.addBodyText(\"There was a programmer, ...\")\n   doc.print <\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The objects <code>myFormatter<\/code> is derived from <code>SimpleTextFormatter<\/code>.<\/li>\n\n\n\n<li>It defines an object <code>doc<\/code> which is an instance of class <code>StandardDocument<\/code>.\n<ul class=\"wp-block-list\">\n<li><code>StandardDocument<\/code> has two arguments: <code>StandardTemplate<\/code> and <code>10<\/code>.<\/li>\n\n\n\n<li><code>StardardTemplate<\/code> is an attribute of <code>SimpleTextFormatter<\/code> and defines the template to be used for <code>doc<\/code>.<\/li>\n\n\n\n<li>The argument <code>10<\/code> sets the maximum line length to 10.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Then 4 paragraphs are added to <code>doc<\/code> by invoking <code>doc.addHeading<\/code> and <code>doc.addBodyText<\/code>.\n<ul class=\"wp-block-list\">\n<li><code>addHeading<\/code> uses a heading style and <code>addBodyText<\/code> uses a body style defined in <code>StandardTemplate<\/code>.<\/li>\n\n\n\n<li>Each invocation has a <code>String<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Finally <code>doc<\/code> is printed giving the output:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>1. Introduction\n   Once up\n   on a tim\n   e, ...\n2. The problem\n   There w\n   as a pro\n   grammer,\n    ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A real text formatter should of course not insert newlines inside a word, but as mentioned, we keep it simple here.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Class <code>SimpleTextFormatter<\/code> has the following structure:<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><code><strong>class<\/strong> SimpleTextFormatter:\n   <strong>class<\/strong> Document:\n      :::\n   <strong>class<\/strong> Template:\n      :::\n   <strong>class<\/strong> Style:\n      :::\n   <strong>class<\/strong> StandardTemplate: Template\n      :::\n   <strong>class<\/strong> StandardDocument: Document\n      :::<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It defines classes <code>Document<\/code>, <code>Template<\/code>, <code>Style<\/code>, <code>StandardTemplate<\/code>, and <code>StandardDocument<\/code>. <code>StandardTemplate<\/code> is a subclass of <code>Template<\/code>, and <code>StandardDocument<\/code> is a subclass of <code>Document<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Class <code>Document<\/code> has the following structure<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><code><strong>class<\/strong> Document(theTemplate: <strong>ref<\/strong> Template, lineMax: <strong>var<\/strong> integer):\n    <strong>class<\/strong> Paragraph(theContent: <strong>var<\/strong> String, theStyle: <strong>ref<\/strong> Style):\n        format -&gt; formattedPar: <strong>var<\/strong> String:&lt;\n            :::  \n    addParagraph(theContent: <strong>var<\/strong> String, theStyle: <strong>ref<\/strong> Style):\n       :::\n    format -&gt; formattedDoc: <strong>var<\/strong> String:\n       ...\n    print:\n       :::<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Class <code>Document<\/code> has the following attributes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Two parameters <code>theTemplate<\/code> and <code>lineMax<\/code>.<\/li>\n\n\n\n<li>A local class <code>Paragraph<\/code> representing a paragraph of the document.<\/li>\n\n\n\n<li>A method <code>addParagraph<\/code> for adding a <code>Paragraph<\/code> to the <code>Document<\/code>.<\/li>\n\n\n\n<li>A method <code>format<\/code> that returns a formatted version of the <code>Document<\/code>.<\/li>\n\n\n\n<li>A <code>print<\/code> method.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Class <code>Paragraph<\/code> has the following attributes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Two parameters <code>theContent<\/code> and <code>theStyle<\/code>.<\/li>\n\n\n\n<li>A virtual method <code>format<\/code> that returns a formatted version of the paragraph.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Class <code>Template<\/code> has the structure:<\/p>\n\n\n\n<pre class=\"wp-block-code has-tiny-font-size\"><code><strong>class<\/strong> Template:\n   heading: <strong>ref<\/strong> Style\n   bodyText: <strong>ref<\/strong> Style \n   ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It has two attributes <code>heading<\/code> and <code>bodyText<\/code> holding references to the styles for paragraphs being headings and paragraphs being body text.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Class <code>Style<\/code> has the structure:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Style -&gt; s: <strong>ref<\/strong> Style:\n   format(content: <strong>var<\/strong> String, lineMax: <strong>var<\/strong> integer) \n                                           -&gt; formattedString: <strong>var<\/strong> String:&lt;\n      ...\n   ... <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It has a virtual method <code>format<\/code> for formatting the <code>String<\/code> being hold in <code>content<\/code> given that <code>lineMax<\/code> defines the maximum line length.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the next section we show the details of the above classes and methods. The reader may skip this part during a first reading.<\/p>\n<div style=\"display:flex; gap:10px;justify-content:center\" class=\"wps-pgfw-pdf-generate-icon__wrapper-frontend\">\n\t\t<a  href=\"https:\/\/oopm.org?action=genpdf&amp;id=4730\" class=\"pgfw-single-pdf-download-button\" ><img src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-generator-for-wp\/admin\/src\/images\/PDF_Tray.svg\" title=\"Generate PDF\" style=\"width:auto; height:45px;\"><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>In this section, we present another example of using nested classes. The domain is text formatting as known from e.g. Microsoft Word, and LaTex. The complexity of most of these systems is huge. Here we will thus stick to a very simple text formatting system. The phenomena of the domain are documents that consists of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4730","page","type-page","status-publish","hentry"],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/4730","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4730"}],"version-history":[{"count":47,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/4730\/revisions"}],"predecessor-version":[{"id":11306,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/4730\/revisions\/11306"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1232"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4730"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}