*/
public class Word extends LayoutElement {
- float letterspacing = 0.0f;
- LinkedList<Letter> letters;
-
- public Word() {
- letters = new LinkedList<>();
- }
-
- public String asJSON(PDRectangle cropbox, float y, float scaleX, float scaleY, float rotation) {
-
- if (size() == 0) {
- return "";
- }
- if (round(this.getTotalWidth()) == 0.0f) {
- return "";
- }
-
- if (startX() < 0 && this.getTotalWidth() <= startX() * -1) {
- return "";
- } else if (y < 0) {
- return "";
- } else if (startX() > cropbox.getWidth()) {
- return "";
- }
-
- String word = this.asText();
-
- String rot = "";
- if (rotation != 0.0f) {
- rot = ",\"rotation\":" + round(rotation);
- }
- String sx = "";
- if (scaleX != 1.0f) {
- sx = ",\"scaleX\":" + round(scaleX);
- }
- String sy = "";
- if (scaleY != 1.0f) {
- sy = ",\"scaleY\":" + round(scaleY);
- }
-
- return "{\"word\":\"" + StringUtil.removeAccents(word).toLowerCase() + "\",\"x\":" + round(startX()) + ",\"y\":" + round(letters.get(0).y) + ",\"width\":" + round(this.getTotalWidth()) + ",\"lw\":[" + String.join(",", this.getLettersWidths()) + "],\"height\":" + round(this.getHeight()) + rot + sx + sy + "}";
- }
-
- public String asText() {
- String word = "";
-
- for (int i = 0; i < letters.size(); i++) {
- Letter l = letters.get(i);
- word += l.asText();
- }
- return word;
- }
-
- public String asText(PDRectangle cropbox) {
- if (startX() > cropbox.getWidth()) {
- return "";
- }
- return asText();
- }
-
- public LinkedList<String> getLettersWidths() {
- LinkedList<String> lw = new LinkedList<>();
- for (int i = 0; i < letters.size(); i++) {
- Letter l = letters.get(i);
- lw.add(String.valueOf(round(l.width)));
- }
- return lw;
- }
-
- public void addLetter(Letter l) {
- letters.add(l);
- }
-
- public int size() {
- return letters.size();
- }
-
- public float nextPosition() {
- if (size() == 0) {
- return 0.0f;
- }
- return letters.get(size() - 1).getNextPosition();
- }
-
- public boolean isSpace() {
- return size() == 1 && letters.get(0).isSpace();
- }
-
- public boolean isSeparator() {
- return size() == 1 && letters.get(0).isSeparator();
- }
-
- public float startX() {
- if (size() == 0) {
- return 0.0f;
- }
- return letters.get(0).x;
- }
-
- public float getTotalWidth() {
- return nextPosition() - startX();
- }
-
- public float getHeight() {
- float res = 0.0f;
- for (int i = 0; i < size(); i++) {
- res = Math.max(letters.get(i).height, res);
- }
- return res;
- }
-
- public float getCharsWidth() {
- float res = 0.0f;
- for (int i = 0; i < size(); i++) {
- res += letters.get(i).width;
- }
- return res;
- }
-
- public float getLetterSpacing() {
- return (getTotalWidth() - getCharsWidth()) / ((float) size());
- }
-
- protected float getMinWidth() {
- float res = Float.MAX_VALUE;
- for (int i = 0; i < size(); i++) {
- res = Math.min(letters.get(i).width, res);
- }
- return res;
- }
-
- public boolean goodCandidate(Letter l, float spaceWidth, float rotation) {
- // Si le mot est vide, la nouvelle lettre est toujours une bonne candidate
- if (size() == 0 && !l.isSeparator()) {
- return true;
- }
- // Si on tombe sur un espace, on ferme forcément le mot
- if (l.isSeparator()) {
- return false;
- }
-
- // Sinon, on va tester la marge entre le dernier caractère est le candidat
- float offset = round(l.x - nextPosition());
- float minWidth = Math.min(l.width, Math.min(spaceWidth, getMinWidth()));
-
- if (offset < 0.0f) {
- if (size() > 0 && l.x <= letters.getLast().x) {
- // Si la lettre que l'on tente d'insérer débute avant ou sur le
- // caractère précédent, on change de mot pour permettre la
- // superposition
- return false;
- } else {
- // Si la différence est négative, à priori, on a une marge plus faible.
- // On va ajouter cette lettre au mot, et les largeurs seront ajustées
- // par le letterSpacing moyen
- return true;
- }
- } else if (offset > minWidth * 0.75f) {
- return false;
- }
-
- return true;
- }
+ float letterspacing = 0.0f;
+ LinkedList<Letter> letters;
+
+ static int order = 0;
+
+ public Word() {
+ letters = new LinkedList<>();
+ }
+
+ public String asJSON(PDRectangle cropbox, float y, float scaleX, float scaleY, float rotation) {
+
+ if (size() == 0) {
+ return "";
+ }
+ if (round(this.getTotalWidth()) == 0.0f) {
+ return "";
+ }
+
+ if (startX() < 0 && this.getTotalWidth() <= startX() * -1) {
+ return "";
+ } else if (y < 0) {
+ return "";
+ } else if (startX() > cropbox.getWidth()) {
+ return "";
+ }
+
+ String word = this.asText();
+
+ String rot = "";
+ if (rotation != 0.0f) {
+ rot = ",\"rotation\":" + round(rotation);
+ }
+ String sx = "";
+ if (scaleX != 1.0f) {
+ sx = ",\"scaleX\":" + round(scaleX);
+ }
+ String sy = "";
+ if (scaleY != 1.0f) {
+ sy = ",\"scaleY\":" + round(scaleY);
+ }
+ String w = StringUtil.removeAccents(word).toLowerCase();
+ if (!StringUtil.removeSpaces(StringUtil.removePoints(w)).isEmpty()) {
+ order++;
+ }
+ return "{\"o\":" + order + ",\"word\":\"" + w + "\",\"x\":" + round(startX()) + ",\"y\":" + round(letters.get(0).y) + ",\"width\":" + round(this.getTotalWidth()) + ",\"lw\":[" + String.join(",", this.getLettersWidths()) + "],\"height\":" + round(this.getHeight()) + rot + sx + sy + "}";
+ }
+
+ public String asText() {
+ String word = "";
+
+ for (int i = 0; i < letters.size(); i++) {
+ Letter l = letters.get(i);
+ word += l.asText();
+ }
+ return word;
+ }
+
+ public String asText(PDRectangle cropbox) {
+ if (startX() > cropbox.getWidth()) {
+ return "";
+ }
+ return asText();
+ }
+
+ public LinkedList<String> getLettersWidths() {
+ LinkedList<String> lw = new LinkedList<>();
+ for (int i = 0; i < letters.size(); i++) {
+ Letter l = letters.get(i);
+ lw.add(String.valueOf(round(l.width)));
+ }
+ return lw;
+ }
+
+ public void addLetter(Letter l) {
+ letters.add(l);
+ }
+
+ public int size() {
+ return letters.size();
+ }
+
+ public float nextPosition() {
+ if (size() == 0) {
+ return 0.0f;
+ }
+ return letters.get(size() - 1).getNextPosition();
+ }
+
+ public boolean isSpace() {
+ return size() == 1 && letters.get(0).isSpace();
+ }
+
+ public boolean isSeparator() {
+ return size() == 1 && letters.get(0).isSeparator();
+ }
+
+ public float startX() {
+ if (size() == 0) {
+ return 0.0f;
+ }
+ return letters.get(0).x;
+ }
+
+ public float getTotalWidth() {
+ return nextPosition() - startX();
+ }
+
+ public float getHeight() {
+ float res = 0.0f;
+ for (int i = 0; i < size(); i++) {
+ res = Math.max(letters.get(i).height, res);
+ }
+ return res;
+ }
+
+ public float getCharsWidth() {
+ float res = 0.0f;
+ for (int i = 0; i < size(); i++) {
+ res += letters.get(i).width;
+ }
+ return res;
+ }
+
+ public float getLetterSpacing() {
+ return (getTotalWidth() - getCharsWidth()) / ((float) size());
+ }
+
+ protected float getMinWidth() {
+ float res = Float.MAX_VALUE;
+ for (int i = 0; i < size(); i++) {
+ res = Math.min(letters.get(i).width, res);
+ }
+ return res;
+ }
+
+ public boolean goodCandidate(Letter l, float spaceWidth, float rotation) {
+ // Si le mot est vide, la nouvelle lettre est toujours une bonne candidate
+ if (size() == 0 && !l.isSeparator()) {
+ return true;
+ }
+ // Si on tombe sur un espace, on ferme forcément le mot
+ if (l.isSeparator()) {
+ return false;
+ }
+
+ // Sinon, on va tester la marge entre le dernier caractère est le candidat
+ float offset = round(l.x - nextPosition());
+ float minWidth = Math.min(l.width, Math.min(spaceWidth, getMinWidth()));
+
+ if (offset < 0.0f) {
+ if (size() > 0 && l.x <= letters.getLast().x) {
+ // Si la lettre que l'on tente d'insérer débute avant ou sur le
+ // caractère précédent, on change de mot pour permettre la
+ // superposition
+ return false;
+ } else {
+ // Si la différence est négative, à priori, on a une marge plus faible.
+ // On va ajouter cette lettre au mot, et les largeurs seront ajustées
+ // par le letterSpacing moyen
+ return true;
+ }
+ } else if (offset > minWidth * 0.75f) {
+ return false;
+ }
+
+ return true;
+ }
}