1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /* $Id: CommonTextDecoration.java 1761021 2016-09-16 11:40:57Z ssteiner $ */
19 
20 package org.apache.fop.fo.properties;
21 
22 import java.awt.Color;
23 import java.util.List;
24 
25 import org.apache.fop.apps.FOUserAgent;
26 import org.apache.fop.fo.Constants;
27 import org.apache.fop.fo.PropertyList;
28 import org.apache.fop.fo.expr.PropertyException;
29 
30 /**
31  * Stores all information concerning text-decoration.
32  */
33 public class CommonTextDecoration {
34 
35     //using a bit-mask here
36     private static final int UNDERLINE    = 1;
37     private static final int OVERLINE     = 2;
38     private static final int LINE_THROUGH = 4;
39     private static final int BLINK        = 8;
40 
41     private int decoration;
42     private Color underColor;
43     private Color overColor;
44     private Color throughColor;
45 
46     /**
47      * Creates a new CommonTextDecoration object with default values.
48      */
CommonTextDecoration()49     public CommonTextDecoration() {
50     }
51 
52     /**
53      * Creates a CommonTextDecoration object from a property list.
54      * @param pList the property list to build the object for
55      * @return a CommonTextDecoration object or null if the obj would only have default values
56      * @throws PropertyException if there's a problem while processing the property
57      */
createFromPropertyList(PropertyList pList)58     public static CommonTextDecoration createFromPropertyList(PropertyList pList)
59                 throws PropertyException {
60         return calcTextDecoration(pList);
61     }
62 
calcTextDecoration(PropertyList pList)63     private static CommonTextDecoration calcTextDecoration(PropertyList pList)
64                 throws PropertyException {
65         assert pList != null;
66         CommonTextDecoration deco = null;
67         PropertyList parentList = pList.getParentPropertyList();
68         if (parentList != null) {
69             //Parent is checked first
70             deco = calcTextDecoration(parentList);
71         }
72         //For rules, see XSL 1.0, chapters 5.5.6 and 7.16.4
73         Property textDecoProp = pList.getExplicit(Constants.PR_TEXT_DECORATION);
74         if (textDecoProp != null) {
75             List list = textDecoProp.getList();
76             for (Object aList : list) {
77                 Property prop = (Property) aList;
78                 int propEnum = prop.getEnum();
79                 FOUserAgent ua = pList.getFObj() == null ? null : pList.getFObj().getUserAgent();
80                 if (propEnum == Constants.EN_NONE) {
81                     if (deco != null) {
82                         deco.decoration = 0;
83                     }
84                     return deco;
85                 } else if (propEnum == Constants.EN_UNDERLINE) {
86                     if (deco == null) {
87                         deco = new CommonTextDecoration();
88                     }
89                     deco.decoration |= UNDERLINE;
90                     deco.underColor = pList.get(Constants.PR_COLOR).getColor(ua);
91                 } else if (propEnum == Constants.EN_NO_UNDERLINE) {
92                     if (deco != null) {
93                         deco.decoration &= OVERLINE | LINE_THROUGH | BLINK;
94                         deco.underColor = pList.get(Constants.PR_COLOR).getColor(ua);
95                     }
96                 } else if (propEnum == Constants.EN_OVERLINE) {
97                     if (deco == null) {
98                         deco = new CommonTextDecoration();
99                     }
100                     deco.decoration |= OVERLINE;
101                     deco.overColor = pList.get(Constants.PR_COLOR).getColor(ua);
102                 } else if (propEnum == Constants.EN_NO_OVERLINE) {
103                     if (deco != null) {
104                         deco.decoration &= UNDERLINE | LINE_THROUGH | BLINK;
105                         deco.overColor = pList.get(Constants.PR_COLOR).getColor(ua);
106                     }
107                 } else if (propEnum == Constants.EN_LINE_THROUGH) {
108                     if (deco == null) {
109                         deco = new CommonTextDecoration();
110                     }
111                     deco.decoration |= LINE_THROUGH;
112                     deco.throughColor = pList.get(Constants.PR_COLOR).getColor(ua);
113                 } else if (propEnum == Constants.EN_NO_LINE_THROUGH) {
114                     if (deco != null) {
115                         deco.decoration &= UNDERLINE | OVERLINE | BLINK;
116                         deco.throughColor = pList.get(Constants.PR_COLOR).getColor(ua);
117                     }
118                 } else if (propEnum == Constants.EN_BLINK) {
119                     if (deco == null) {
120                         deco = new CommonTextDecoration();
121                     }
122                     deco.decoration |= BLINK;
123                 } else if (propEnum == Constants.EN_NO_BLINK) {
124                     if (deco != null) {
125                         deco.decoration &= UNDERLINE | OVERLINE | LINE_THROUGH;
126                     }
127                 } else {
128                     throw new PropertyException("Illegal value encountered: " + prop.getString());
129                 }
130             }
131         }
132         return deco;
133     }
134 
135     /** @return true if underline is active */
hasUnderline()136     public boolean hasUnderline() {
137         return (this.decoration & UNDERLINE) != 0;
138     }
139 
140     /** @return true if overline is active */
hasOverline()141     public boolean hasOverline() {
142         return (this.decoration & OVERLINE) != 0;
143     }
144 
145     /** @return true if line-through is active */
hasLineThrough()146     public boolean hasLineThrough() {
147         return (this.decoration & LINE_THROUGH) != 0;
148     }
149 
150     /** @return true if blink is active */
isBlinking()151     public boolean isBlinking() {
152         return (this.decoration & BLINK) != 0;
153     }
154 
155     /** @return the color of the underline mark */
getUnderlineColor()156     public Color getUnderlineColor() {
157         return this.underColor;
158     }
159 
160     /** @return the color of the overline mark */
getOverlineColor()161     public Color getOverlineColor() {
162         return this.overColor;
163     }
164 
165     /** @return the color of the line-through mark */
getLineThroughColor()166     public Color getLineThroughColor() {
167         return this.throughColor;
168     }
169 
170 }
171