The OpenD Programming Language

1 /**
2 Simple "style" API for Flow Document.
3 
4 Copyright: Guillaume Piolat 2022.
5 License:   $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
6 */
7 module printed.flow.style;
8 
9 import printed.canvas.irenderer;
10 
11 /// Similar intent than CSS display property.
12 /// Only outside display is considered.
13 enum DisplayStyle
14 {
15     inline,   /// Insert in same line flow.
16     block,    /// Insert line-break before and after.
17     listItem, /// Same as block but also display ListStyleType
18 }
19 
20 /// More or less similar to CSS list-style-type 
21 enum ListStyleType
22 {
23     inherit,
24     disc,
25     decimal
26 }
27 
28 struct TagStyle
29 {
30 public:
31     /// Text color. "" means "inherit".
32     string color = "";
33 
34     /// Display inline or block.
35     DisplayStyle display = DisplayStyle.inline;
36 
37     /// Font size in em. 1.0f means "inherit"
38     float fontSizeEm = 1.0f;  
39 
40     /// Font face. `null` means "inherit"
41     string fontFace = null;   
42 
43     /// Font weight. -1 means "inherit"
44     FontWeight fontWeight = cast(FontWeight)-1; 
45 
46     /// Font style. -1 means "inherit"
47     FontStyle fontStyle = cast(FontStyle)-1;
48 
49     /// Text-alignment.
50     /// WARNING: this does very little, only TextAlign.start and TextAlign.left are supported!
51     /// -1 means "inherit".
52     TextAlign textAlign;
53 
54     /// Margins, in em (unused)
55     float marginTopEm = 0;    /// Only used when hasBlockDisplay() 
56     float marginRightMm = 0;  /// Unused.
57     float marginBottomEm = 0; /// Only used when hasBlockDisplay()
58     float marginLeftMm = 0;   /// Warning: in mm not em.
59 
60     ListStyleType listStyleType;
61 
62     bool hasBlockDisplay() pure const
63     {
64         return display == DisplayStyle.block || display == DisplayStyle.listItem;
65     }
66 }
67 
68 /// Style options for various tags.
69 /// Helpful reference: https://www.w3.org/TR/CSS22/sample.html
70 /// Note: there is no <b> and <i> style, as commonmark-d doesn't generate them.
71 struct StyleOptions
72 {
73     string color = "black";                    /// Default text color.
74     float fontSizePt = 10.0f;                  /// Default font size, in points.
75     string fontFace = "Helvetica";             /// Default font face.
76     FontWeight fontWeight = FontWeight.normal; /// Default font weight.
77     FontStyle fontStyle = FontStyle.normal;    /// Default font italic-ness
78 
79     /// Default text alignment (only TextAlign.start and TextAlign.left are supported!)
80     TextAlign textAlign = TextAlign.start;
81     
82     float pageLeftMarginMm   = 19;   /// Left margin, in millimeters.
83     float pageRightMarginMm  = 19;   /// Right margin, in millimeters.
84     float pageTopMarginMm    = 25.4; /// Top margin, in millimeters.
85     float pageBottomMarginMm = 36.7; /// Bottom margin, in millimeters.
86 
87     float paragraphTextIndentMm = 5; /// <p> first line text indentation, in millimeters.
88 
89     /// An empty callback you can override to decorate each page of the document.
90     /// You cannot call other `FlowDocument` function from inside this callback.
91     /// `pageCount` is the page number, starting with 1.
92     /// This is called before anything else is drawn on a page.
93     void delegate (IRenderingContext2D context, int pageCount) onEnterPage = null;
94 
95     TagStyle p      = TagStyle("", DisplayStyle.block,  1.0f,  null, cast(FontWeight)-1, cast(FontStyle)-1, cast(TextAlign)-1, 1.12, 0, 1.12,   0, ListStyleType.inherit);
96 
97     TagStyle b      = TagStyle("", DisplayStyle.inline, 1.0f,  null, FontWeight.bold,    cast(FontStyle)-1, cast(TextAlign)-1,     0, 0,    0,   0, ListStyleType.inherit);
98     TagStyle strong = TagStyle("", DisplayStyle.inline, 1.0f,  null, FontWeight.bold,    cast(FontStyle)-1, cast(TextAlign)-1,     0, 0,    0,   0, ListStyleType.inherit);
99 
100     TagStyle i      = TagStyle("", DisplayStyle.inline, 1.0f,  null, cast(FontWeight)-1, FontStyle.italic,  cast(TextAlign)-1,     0, 0,    0,   0, ListStyleType.inherit);
101     TagStyle em     = TagStyle("", DisplayStyle.inline, 1.0f,  null, cast(FontWeight)-1, FontStyle.italic,  cast(TextAlign)-1,     0, 0,    0,   0, ListStyleType.inherit);
102 
103     TagStyle h1     = TagStyle("", DisplayStyle.block,  2.0f,  null, cast(FontWeight)-1, cast(FontStyle)-1, cast(TextAlign)-1,  0.67, 0, 0.67,   0, ListStyleType.inherit);
104     TagStyle h2     = TagStyle("", DisplayStyle.block,  1.5f,  null, cast(FontWeight)-1, cast(FontStyle)-1, cast(TextAlign)-1,  0.75, 0, 0.75,   0, ListStyleType.inherit);
105     TagStyle h3     = TagStyle("", DisplayStyle.block, 1.17f,  null, cast(FontWeight)-1, cast(FontStyle)-1, cast(TextAlign)-1,  0.83, 0, 0.83,   0, ListStyleType.inherit);
106     TagStyle h4     = TagStyle("", DisplayStyle.block,  1.0f,  null, cast(FontWeight)-1, cast(FontStyle)-1, cast(TextAlign)-1,  1.12, 0, 1.12,   0, ListStyleType.inherit);
107     TagStyle h5     = TagStyle("", DisplayStyle.block, 0.83f,  null, cast(FontWeight)-1, cast(FontStyle)-1, cast(TextAlign)-1,  1.50, 0,  1.5,   0, ListStyleType.inherit);
108     TagStyle h6     = TagStyle("", DisplayStyle.block, 0.75f,  null, cast(FontWeight)-1, cast(FontStyle)-1, cast(TextAlign)-1,  1.67, 0, 1.67,   0, ListStyleType.inherit);
109 
110     TagStyle pre    = TagStyle("", DisplayStyle.inline, 1.0f,"Courier New",cast(FontWeight)-1,cast(FontStyle)-1,cast(TextAlign)-1, 0, 0,    0,   0, ListStyleType.inherit);
111     TagStyle code   = TagStyle("", DisplayStyle.inline, 1.0f,"Courier New",cast(FontWeight)-1, cast(FontStyle)-1,cast(TextAlign)-1,0, 0,    0,   0, ListStyleType.inherit);
112 
113     TagStyle ol     = TagStyle("", DisplayStyle.block,  1.0f,  null, cast(FontWeight)-1, cast(FontStyle)-1, cast(TextAlign)-1,  1.12, 0, 1.12,   0, ListStyleType.decimal);
114     TagStyle ul     = TagStyle("", DisplayStyle.block,  1.0f,  null, cast(FontWeight)-1, cast(FontStyle)-1, cast(TextAlign)-1,  1.12, 0, 1.12,   0, ListStyleType.disc);
115     TagStyle li     = TagStyle("",DisplayStyle.listItem,1.0f,  null, cast(FontWeight)-1, cast(FontStyle)-1, cast(TextAlign)-1,     0, 0,    0,10.5, ListStyleType.inherit);
116     TagStyle img    = TagStyle("", DisplayStyle.block,  1.0f,  null, cast(FontWeight)-1, cast(FontStyle)-1, cast(TextAlign)-1,  1.12, 0, 1.12,   0, ListStyleType.inherit);
117 }