Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move method refactoring. #171

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions fontbox/src/main/java/org/apache/fontbox/type1/Type1Font.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,137 @@ public static Type1Font createWithSegments(byte[] segment1, byte[] segment2) thr
return parser.parse(segment1, segment2);
}

/**
* Reads font attributes and assigns values to respective fields of a font object.
* @param key The attribute key indicating the specific font attribute.
* @param value list of tokens representing the value(s) associated with the attribute.
* @throws IOException If an input/output exception occurs during the process.
*/
public void readFontAttributes(String key, List<Token> value) throws IOException
{
switch (key)
{
case "version":
this.version = value.get(0).getText();
break;
case "Notice":
this.notice = value.get(0).getText();
break;
case "FullName":
this.fullName = value.get(0).getText();
break;
case "FamilyName":
this.familyName = value.get(0).getText();
break;
case "Weight":
this.weight = value.get(0).getText();
break;
case "ItalicAngle":
this.italicAngle = value.get(0).floatValue();
break;
case "isFixedPitch":
this.isFixedPitch = value.get(0).booleanValue();
break;
case "UnderlinePosition":
this.underlinePosition = value.get(0).floatValue();
break;
case "UnderlineThickness":
this.underlineThickness = value.get(0).floatValue();
break;
case "FontName":
this.fontName = value.get(0).getText();
break;
case "PaintType":
this.paintType = value.get(0).intValue();
break;
case "FontType":
this.fontType = value.get(0).intValue();
break;
case "FontMatrix":
this.fontMatrix = arrayToNumbers(value);
break;
case "FontBBox":
this.fontBBox = arrayToNumbers(value);
break;
case "UniqueID":
this.uniqueID = value.get(0).intValue();
break;
case "StrokeWidth":
this.strokeWidth = value.get(0).floatValue();
break;
case "FID":
this.fontID = value.get(0).getText();
break;
case "BlueValues":
this.blueValues = arrayToNumbers(value);
break;
case "OtherBlues":
this.otherBlues = arrayToNumbers(value);
break;
case "FamilyBlues":
this.familyBlues = arrayToNumbers(value);
break;
case "FamilyOtherBlues":
this.familyOtherBlues = arrayToNumbers(value);
break;
case "BlueScale":
this.blueScale = value.get(0).floatValue();
break;
case "BlueShift":
this.blueShift = value.get(0).intValue();
break;
case "BlueFuzz":
this.blueFuzz = value.get(0).intValue();
break;
case "StdHW":
this.stdHW = arrayToNumbers(value);
break;
case "StdVW":
this.stdVW = arrayToNumbers(value);
break;
case "StemSnapH":
this.stemSnapH = arrayToNumbers(value);
break;
case "StemSnapV":
this.stemSnapV = arrayToNumbers(value);
break;
case "ForceBold":
this.forceBold = value.get(0).booleanValue();
break;
case "LanguageGroup":
this.languageGroup = value.get(0).intValue();
break;
default:
break;
}
}

/**
* Extracts values from an array as numbers.
*/
private List<Number> arrayToNumbers(List<Token> value) throws IOException
{
List<Number> numbers = new ArrayList<>();
for (int i = 1, size = value.size() - 1; i < size; i++)
{
Token token = value.get(i);
if (token.getKind() == Token.REAL)
{
numbers.add(token.floatValue());
}
else if (token.getKind() == Token.INTEGER)
{
numbers.add(token.intValue());
}
else
{
throw new IOException("Expected INTEGER or REAL but got " + token +
" at array position " + i);
}
}
return numbers;
}

// font dictionary
String fontName = "";
Encoding encoding = null;
Expand Down
90 changes: 90 additions & 0 deletions fontbox/src/main/java/org/apache/fontbox/type1/Type1Lexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
/**
Expand Down Expand Up @@ -76,6 +79,93 @@ public Token nextToken() throws IOException
return curToken;
}

/**
* Reads a procedure.
*/
public List<Token> readProc() throws IOException
{
List<Token> value = new ArrayList<>();

int openProc = 1;
while (true)
{
if (this.peekToken() == null)
{
throw new IOException("Malformed procedure: missing token");
}

if (this.peekKind(Token.START_PROC))
{
openProc++;
}

Token token = this.nextToken();
value.add(token);

if (token.getKind() == Token.END_PROC)
{
openProc--;
if (openProc == 0)
{
break;
}
}
}
Token executeonly = readMaybe(Token.NAME, "executeonly");
if (executeonly != null)
{
value.add(executeonly);
}

return value;
}

/**
* Reads a procedure but without returning anything.
*/
public void readProcVoid() throws IOException
{
int openProc = 1;
while (true)
{
if (this.peekToken() == null)
{
throw new IOException("Malformed procedure: missing token");
}
if (this.peekKind(Token.START_PROC))
{
openProc++;
}

Token token = this.nextToken();

if (token.getKind() == Token.END_PROC)
{
openProc--;
if (openProc == 0)
{
break;
}
}
}
readMaybe(Token.NAME, "executeonly");
}

/**
* Reads the next token if and only if it is of the given kind and
* has the given value.
*
* @return token or null if not the expected one
*/
public Token readMaybe(Token.Kind kind, String name) throws IOException
{
if (this.peekKind(kind) && this.peekToken().getText().equals(name))
{
return this.nextToken();
}
return null;
}

/**
* Returns the next token without consuming it.
* @return The next token
Expand Down
Loading