You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2016/08/07 12:04:26 UTC
svn commit: r1755435 - in
/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf:
OpenTypeFont.java TrueTypeFont.java
Author: lehmi
Date: Sun Aug 7 12:04:25 2016
New Revision: 1755435
URL: http://svn.apache.org/viewvc?rev=1755435&view=rev
Log:
PDFBOX-2852: DRY-refactoring to improve readability of synchronized code
Modified:
pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/OpenTypeFont.java
pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/TrueTypeFont.java
Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/OpenTypeFont.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/OpenTypeFont.java?rev=1755435&r1=1755434&r2=1755435&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/OpenTypeFont.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/OpenTypeFont.java Sun Aug 7 12:04:25 2016
@@ -49,22 +49,17 @@ public class OpenTypeFont extends TrueTy
*
* @return The "CFF" table.
*/
- public synchronized CFFTable getCFF() throws IOException
+ public CFFTable getCFF() throws IOException
{
if (!isPostScript)
{
throw new UnsupportedOperationException("TTF fonts do not have a CFF table");
}
- CFFTable cff = (CFFTable)tables.get(CFFTable.TAG);
- if (cff != null && !cff.getInitialized())
- {
- readTable(cff);
- }
- return cff;
+ return (CFFTable) getTable(CFFTable.TAG);
}
@Override
- public synchronized GlyphTable getGlyph() throws IOException
+ public GlyphTable getGlyph() throws IOException
{
if (isPostScript)
{
Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/TrueTypeFont.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/TrueTypeFont.java?rev=1755435&r1=1755434&r2=1755435&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/TrueTypeFont.java (original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/TrueTypeFont.java Sun Aug 7 12:04:25 2016
@@ -123,21 +123,33 @@ public class TrueTypeFont implements Fon
data.seek(currentPosition);
return bytes;
}
-
+
/**
- * This will get the naming table for the true type font.
+ * This will get the table for the given tag.
*
- * @return The naming table.
+ * @param tag the name of the table to be returned
+ * @return The table with the given tag.
* @throws IOException if there was an error reading the table.
*/
- public synchronized NamingTable getNaming() throws IOException
+ protected synchronized TTFTable getTable(String tag) throws IOException
{
- NamingTable naming = (NamingTable)tables.get( NamingTable.TAG );
- if (naming != null && !naming.getInitialized())
+ TTFTable ttfTable = tables.get(tag);
+ if (ttfTable != null && !ttfTable.getInitialized())
{
- readTable(naming);
+ readTable(ttfTable);
}
- return naming;
+ return ttfTable;
+ }
+
+ /**
+ * This will get the naming table for the true type font.
+ *
+ * @return The naming table.
+ * @throws IOException if there was an error reading the table.
+ */
+ public NamingTable getNaming() throws IOException
+ {
+ return (NamingTable) getTable(NamingTable.TAG);
}
/**
@@ -146,14 +158,9 @@ public class TrueTypeFont implements Fon
* @return The postscript table.
* @throws IOException if there was an error reading the table.
*/
- public synchronized PostScriptTable getPostScript() throws IOException
+ public PostScriptTable getPostScript() throws IOException
{
- PostScriptTable postscript = (PostScriptTable)tables.get( PostScriptTable.TAG );
- if (postscript != null && !postscript.getInitialized())
- {
- readTable(postscript);
- }
- return postscript;
+ return (PostScriptTable) getTable(PostScriptTable.TAG);
}
/**
@@ -162,30 +169,20 @@ public class TrueTypeFont implements Fon
* @return The OS/2 table.
* @throws IOException if there was an error reading the table.
*/
- public synchronized OS2WindowsMetricsTable getOS2Windows() throws IOException
+ public OS2WindowsMetricsTable getOS2Windows() throws IOException
{
- OS2WindowsMetricsTable os2WindowsMetrics = (OS2WindowsMetricsTable)tables.get( OS2WindowsMetricsTable.TAG );
- if (os2WindowsMetrics != null && !os2WindowsMetrics.getInitialized())
- {
- readTable(os2WindowsMetrics);
- }
- return os2WindowsMetrics;
+ return (OS2WindowsMetricsTable) getTable(OS2WindowsMetricsTable.TAG);
}
-
+
/**
* Get the maxp table for this TTF.
*
* @return The maxp table.
* @throws IOException if there was an error reading the table.
*/
- public synchronized MaximumProfileTable getMaximumProfile() throws IOException
+ public MaximumProfileTable getMaximumProfile() throws IOException
{
- MaximumProfileTable maximumProfile = (MaximumProfileTable)tables.get( MaximumProfileTable.TAG );
- if (maximumProfile != null && !maximumProfile.getInitialized())
- {
- readTable(maximumProfile);
- }
- return maximumProfile;
+ return (MaximumProfileTable) getTable(MaximumProfileTable.TAG);
}
/**
@@ -194,14 +191,9 @@ public class TrueTypeFont implements Fon
* @return The head table.
* @throws IOException if there was an error reading the table.
*/
- public synchronized HeaderTable getHeader() throws IOException
+ public HeaderTable getHeader() throws IOException
{
- HeaderTable header = (HeaderTable)tables.get( HeaderTable.TAG );
- if (header != null && !header.getInitialized())
- {
- readTable(header);
- }
- return header;
+ return (HeaderTable) getTable(HeaderTable.TAG);
}
/**
@@ -210,14 +202,9 @@ public class TrueTypeFont implements Fon
* @return The hhea table.
* @throws IOException if there was an error reading the table.
*/
- public synchronized HorizontalHeaderTable getHorizontalHeader() throws IOException
+ public HorizontalHeaderTable getHorizontalHeader() throws IOException
{
- HorizontalHeaderTable horizontalHeader = (HorizontalHeaderTable)tables.get( HorizontalHeaderTable.TAG );
- if (horizontalHeader != null && !horizontalHeader.getInitialized())
- {
- readTable(horizontalHeader);
- }
- return horizontalHeader;
+ return (HorizontalHeaderTable) getTable(HorizontalHeaderTable.TAG);
}
/**
@@ -226,14 +213,9 @@ public class TrueTypeFont implements Fon
* @return The hmtx table.
* @throws IOException if there was an error reading the table.
*/
- public synchronized HorizontalMetricsTable getHorizontalMetrics() throws IOException
+ public HorizontalMetricsTable getHorizontalMetrics() throws IOException
{
- HorizontalMetricsTable horizontalMetrics = (HorizontalMetricsTable)tables.get( HorizontalMetricsTable.TAG );
- if (horizontalMetrics != null && !horizontalMetrics.getInitialized())
- {
- readTable(horizontalMetrics);
- }
- return horizontalMetrics;
+ return (HorizontalMetricsTable) getTable(HorizontalMetricsTable.TAG);
}
/**
@@ -242,14 +224,9 @@ public class TrueTypeFont implements Fon
* @return The loca table.
* @throws IOException if there was an error reading the table.
*/
- public synchronized IndexToLocationTable getIndexToLocation() throws IOException
+ public IndexToLocationTable getIndexToLocation() throws IOException
{
- IndexToLocationTable indexToLocation = (IndexToLocationTable)tables.get( IndexToLocationTable.TAG );
- if (indexToLocation != null && !indexToLocation.getInitialized())
- {
- readTable(indexToLocation);
- }
- return indexToLocation;
+ return (IndexToLocationTable) getTable(IndexToLocationTable.TAG);
}
/**
@@ -258,30 +235,20 @@ public class TrueTypeFont implements Fon
* @return The glyf table.
* @throws IOException if there was an error reading the table.
*/
- public synchronized GlyphTable getGlyph() throws IOException
+ public GlyphTable getGlyph() throws IOException
{
- GlyphTable glyph = (GlyphTable)tables.get( GlyphTable.TAG );
- if (glyph != null && !glyph.getInitialized())
- {
- readTable(glyph);
- }
- return glyph;
+ return (GlyphTable) getTable(GlyphTable.TAG);
}
/**
* Get the "cmap" table for this TTF.
*
* @return The "cmap" table.
- * @throws IOException if there was an error reading the table.
- */
- public synchronized CmapTable getCmap() throws IOException
+ * @throws IOException if there was an error reading the table.
+ */
+ public CmapTable getCmap() throws IOException
{
- CmapTable cmap = (CmapTable)tables.get( CmapTable.TAG );
- if (cmap != null && !cmap.getInitialized())
- {
- readTable(cmap);
- }
- return cmap;
+ return (CmapTable) getTable(CmapTable.TAG);
}
/**
@@ -290,14 +257,9 @@ public class TrueTypeFont implements Fon
* @return The vhea table.
* @throws IOException if there was an error reading the table.
*/
- public synchronized VerticalHeaderTable getVerticalHeader() throws IOException
+ public VerticalHeaderTable getVerticalHeader() throws IOException
{
- VerticalHeaderTable verticalHeader = (VerticalHeaderTable)tables.get( VerticalHeaderTable.TAG );
- if (verticalHeader != null && !verticalHeader.getInitialized())
- {
- readTable(verticalHeader);
- }
- return verticalHeader;
+ return (VerticalHeaderTable) getTable(VerticalHeaderTable.TAG);
}
/**
@@ -306,14 +268,9 @@ public class TrueTypeFont implements Fon
* @return The vmtx table.
* @throws IOException if there was an error reading the table.
*/
- public synchronized VerticalMetricsTable getVerticalMetrics() throws IOException
+ public VerticalMetricsTable getVerticalMetrics() throws IOException
{
- VerticalMetricsTable verticalMetrics = (VerticalMetricsTable)tables.get( VerticalMetricsTable.TAG );
- if (verticalMetrics != null && !verticalMetrics.getInitialized())
- {
- readTable(verticalMetrics);
- }
- return verticalMetrics;
+ return (VerticalMetricsTable) getTable(VerticalMetricsTable.TAG);
}
/**
@@ -322,14 +279,9 @@ public class TrueTypeFont implements Fon
* @return The VORG table.
* @throws IOException if there was an error reading the table.
*/
- public synchronized VerticalOriginTable getVerticalOrigin() throws IOException
+ public VerticalOriginTable getVerticalOrigin() throws IOException
{
- VerticalOriginTable verticalOrigin = (VerticalOriginTable)tables.get( VerticalOriginTable.TAG );
- if (verticalOrigin != null && !verticalOrigin.getInitialized())
- {
- readTable(verticalOrigin);
- }
- return verticalOrigin;
+ return (VerticalOriginTable) getTable(VerticalOriginTable.TAG);
}
/**
@@ -338,14 +290,9 @@ public class TrueTypeFont implements Fon
* @return The "kern" table.
* @throws IOException if there was an error reading the table.
*/
- public synchronized KerningTable getKerning() throws IOException
+ public KerningTable getKerning() throws IOException
{
- KerningTable kerning = (KerningTable)tables.get( KerningTable.TAG );
- if (kerning != null && !kerning.getInitialized())
- {
- readTable(kerning);
- }
- return kerning;
+ return (KerningTable) getTable(KerningTable.TAG);
}
/**