-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For #73 - Implements font-face at rule for the Java2D renderer. [ci s…
…kip]
- Loading branch information
Showing
11 changed files
with
679 additions
and
224 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...ltopdf-core/src/main/java/com/openhtmltopdf/outputdevice/helper/FontFaceFontSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.openhtmltopdf.outputdevice.helper; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
|
||
import com.openhtmltopdf.extend.FSSupplier; | ||
import com.openhtmltopdf.layout.SharedContext; | ||
import com.openhtmltopdf.util.XRLog; | ||
|
||
public class FontFaceFontSupplier implements FSSupplier<InputStream> { | ||
private final String src; | ||
private final SharedContext ctx; | ||
|
||
public FontFaceFontSupplier(SharedContext ctx, String src) { | ||
this.src = src; | ||
this.ctx = ctx; | ||
} | ||
|
||
@Override | ||
public InputStream supply() { | ||
byte[] font1 = ctx.getUserAgentCallback().getBinaryResource(src); | ||
|
||
if (font1 == null) { | ||
XRLog.exception("Could not load @font-face font: " + src); | ||
return null; | ||
} | ||
|
||
return new ByteArrayInputStream(font1); | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
openhtmltopdf-core/src/main/java/com/openhtmltopdf/outputdevice/helper/FontFamily.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package com.openhtmltopdf.outputdevice.helper; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
import com.openhtmltopdf.css.constants.IdentValue; | ||
|
||
public class FontFamily<T extends MinimalFontDescription> { | ||
private List<T> _fontDescriptions; | ||
|
||
public FontFamily() { | ||
} | ||
|
||
public List<T> getFontDescriptions() { | ||
return _fontDescriptions; | ||
} | ||
|
||
public void addFontDescription(T descr) { | ||
if (_fontDescriptions == null) { | ||
_fontDescriptions = new ArrayList<T>(); | ||
} | ||
_fontDescriptions.add(descr); | ||
Collections.sort(_fontDescriptions, | ||
new Comparator<T>() { | ||
public int compare(T o1, T o2) { | ||
return o1.getWeight() - o2.getWeight(); | ||
} | ||
}); | ||
} | ||
|
||
public void setName(String fontFamilyName) { | ||
} | ||
|
||
public T match(int desiredWeight, IdentValue style) { | ||
if (_fontDescriptions == null) { | ||
throw new RuntimeException("fontDescriptions is null"); | ||
} | ||
|
||
List<T> candidates = new ArrayList<T>(); | ||
|
||
for (T description : _fontDescriptions) { | ||
if (description.getStyle() == style) { | ||
candidates.add(description); | ||
} | ||
} | ||
|
||
if (candidates.size() == 0) { | ||
if (style == IdentValue.ITALIC) { | ||
return match(desiredWeight, IdentValue.OBLIQUE); | ||
} else if (style == IdentValue.OBLIQUE) { | ||
return match(desiredWeight, IdentValue.NORMAL); | ||
} else { | ||
candidates.addAll(_fontDescriptions); | ||
} | ||
} | ||
|
||
T result = findByWeight(candidates, desiredWeight, SM_EXACT); | ||
|
||
if (result != null) { | ||
return result; | ||
} else { | ||
if (desiredWeight <= 500) { | ||
return findByWeight(candidates, desiredWeight, SM_LIGHTER_OR_DARKER); | ||
} else { | ||
return findByWeight(candidates, desiredWeight, SM_DARKER_OR_LIGHTER); | ||
} | ||
} | ||
} | ||
|
||
private static final int SM_EXACT = 1; | ||
private static final int SM_LIGHTER_OR_DARKER = 2; | ||
private static final int SM_DARKER_OR_LIGHTER = 3; | ||
|
||
private T findByWeight(List<T> matches, int desiredWeight, int searchMode) { | ||
if (searchMode == SM_EXACT) { | ||
for (T descr : matches) { | ||
if (descr.getWeight() == desiredWeight) { | ||
return descr; | ||
} | ||
} | ||
return null; | ||
} else if (searchMode == SM_LIGHTER_OR_DARKER){ | ||
int offset = 0; | ||
T descr = null; | ||
for (offset = 0; offset < matches.size(); offset++) { | ||
descr = matches.get(offset); | ||
if (descr.getWeight() > desiredWeight) { | ||
break; | ||
} | ||
} | ||
|
||
if (offset > 0 && descr.getWeight() > desiredWeight) { | ||
return matches.get(offset - 1); | ||
} else { | ||
return descr; | ||
} | ||
|
||
} else if (searchMode == SM_DARKER_OR_LIGHTER) { | ||
int offset = 0; | ||
T descr = null; | ||
for (offset = matches.size() - 1; offset >= 0; offset--) { | ||
descr = matches.get(offset); | ||
if (descr.getWeight() < desiredWeight) { | ||
break; | ||
} | ||
} | ||
|
||
if (offset != matches.size() - 1 && descr.getWeight() < desiredWeight) { | ||
return matches.get(offset + 1); | ||
} else { | ||
return descr; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...ltopdf-core/src/main/java/com/openhtmltopdf/outputdevice/helper/FontFileFontSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.openhtmltopdf.outputdevice.helper; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.InputStream; | ||
|
||
import com.openhtmltopdf.extend.FSSupplier; | ||
import com.openhtmltopdf.util.XRLog; | ||
|
||
public class FontFileFontSupplier implements FSSupplier<InputStream>{ | ||
private final String path; | ||
|
||
public FontFileFontSupplier(String path) { | ||
this.path = path; | ||
} | ||
|
||
@Override | ||
public InputStream supply() { | ||
try { | ||
return new FileInputStream(this.path); | ||
} catch (FileNotFoundException e) { | ||
XRLog.exception("While trying to add font from directory, file seems to have disappeared."); | ||
return null; | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...tmltopdf-core/src/main/java/com/openhtmltopdf/outputdevice/helper/FontResolverHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.openhtmltopdf.outputdevice.helper; | ||
|
||
import com.openhtmltopdf.css.constants.IdentValue; | ||
|
||
public class FontResolverHelper { | ||
public static int convertWeightToInt(IdentValue weight) { | ||
if (weight == IdentValue.NORMAL) { | ||
return 400; | ||
} else if (weight == IdentValue.BOLD) { | ||
return 700; | ||
} else if (weight == IdentValue.FONT_WEIGHT_100) { | ||
return 100; | ||
} else if (weight == IdentValue.FONT_WEIGHT_200) { | ||
return 200; | ||
} else if (weight == IdentValue.FONT_WEIGHT_300) { | ||
return 300; | ||
} else if (weight == IdentValue.FONT_WEIGHT_400) { | ||
return 400; | ||
} else if (weight == IdentValue.FONT_WEIGHT_500) { | ||
return 500; | ||
} else if (weight == IdentValue.FONT_WEIGHT_600) { | ||
return 600; | ||
} else if (weight == IdentValue.FONT_WEIGHT_700) { | ||
return 700; | ||
} else if (weight == IdentValue.FONT_WEIGHT_800) { | ||
return 800; | ||
} else if (weight == IdentValue.FONT_WEIGHT_900) { | ||
return 900; | ||
} else if (weight == IdentValue.LIGHTER) { | ||
// FIXME | ||
return 400; | ||
} else if (weight == IdentValue.BOLDER) { | ||
// FIXME | ||
return 700; | ||
} | ||
throw new IllegalArgumentException(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...opdf-core/src/main/java/com/openhtmltopdf/outputdevice/helper/MinimalFontDescription.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.openhtmltopdf.outputdevice.helper; | ||
|
||
import com.openhtmltopdf.css.constants.IdentValue; | ||
|
||
public interface MinimalFontDescription { | ||
public int getWeight(); | ||
public IdentValue getStyle(); | ||
} |
43 changes: 43 additions & 0 deletions
43
openhtmltopdf-java2d/src/main/java/com/openhtmltopdf/java2d/Java2DFont.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* {{{ header & license | ||
* Copyright (c) 2006 Wisconsin Court System | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public License | ||
* as published by the Free Software Foundation; either version 2.1 | ||
* of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
* }}} | ||
*/ | ||
package com.openhtmltopdf.java2d; | ||
|
||
import java.awt.Font; | ||
import java.util.List; | ||
|
||
import com.openhtmltopdf.render.FSFont; | ||
|
||
public class Java2DFont implements FSFont { | ||
private final List<Font> _fonts; | ||
private final float _size; | ||
|
||
public Java2DFont(List<Font> fonts, float size) { | ||
_fonts = fonts; | ||
_size = size; | ||
} | ||
|
||
public float getSize2D() { | ||
return _size; | ||
} | ||
|
||
public List<Font> getAWTFonts() { | ||
return _fonts; | ||
} | ||
} |
Oops, something went wrong.