pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1830499 - in /pdfbox/trunk: examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ examples/src/main/resources/org/apache/pdfbox/resources/ttf/ fontbox/src/main/java/org/apache/fontbox/ttf/gsub/ fontbox/src/test/java/org/apache/fontbox...
Date Sun, 29 Apr 2018 09:50:52 GMT
Author: tilman
Date: Sun Apr 29 09:50:52 2018
New Revision: 1830499

URL: http://svn.apache.org/viewvc?rev=1830499&view=rev
Log:
PDFBOX-4189: Bengali fix for GSUB: ja phala not rendering properly, by Palash Ray

Added:
    pdfbox/trunk/examples/src/main/resources/org/apache/pdfbox/resources/ttf/bengali-samples.txt
  (with props)
    pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/ttf/GSUBTableDebugger.java   (with
props)
    pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/ttf/gsub/GSUBTablePrintUtil.java
  (with props)
Modified:
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/BengaliPdfGenerationHelloWorld.java
    pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/gsub/GlyphSubstitutionDataExtractor.java
    pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/gsub/GsubWorkerForBengali.java
    pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/ttf/GlyphSubstitutionTableTest.java

Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/BengaliPdfGenerationHelloWorld.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/BengaliPdfGenerationHelloWorld.java?rev=1830499&r1=1830498&r2=1830499&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/BengaliPdfGenerationHelloWorld.java
(original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/BengaliPdfGenerationHelloWorld.java
Sun Apr 29 09:50:52 2018
@@ -17,39 +17,37 @@
 
 package org.apache.pdfbox.examples.pdmodel;
 
+import java.io.BufferedReader;
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.StringTokenizer;
 
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.PDPage;
 import org.apache.pdfbox.pdmodel.PDPageContentStream;
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
 import org.apache.pdfbox.pdmodel.font.PDFont;
 import org.apache.pdfbox.pdmodel.font.PDType0Font;
-import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
 
 /**
  * Inspired from <a href=
  * "https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldTTF.java?view=markup">PdfBox
- * Example</a>. This attempts to correctly demonstrate to what extent Bengali text
rendering is
- * supported. First, we render some text, and then embed an image with the correct text displayed
on
- * the next page.
+ * Example</a>. This attempts to correctly demonstrate to what extent Bengali text
rendering is supported. First, we
+ * render some text, and then embed an image with the correct text displayed on the next
page.
  *
  * @author Palash Ray
  *
  */
 public class BengaliPdfGenerationHelloWorld
 {
-    /**
-     * The unicode of this is given below:
-     * 
-     * <pre>
-     * \u0986\u09ae\u09bf  \u0995\u09cb\u09a8 \u09aa\u09a5\u09c7  \u0995\u09cd\u09b7\u09c0\u09b0\u09c7\u09b0
\u09b7\u09a8\u09cd\u09a1  \u09aa\u09c1\u09a4\u09c1\u09b2 \u09b0\u09c1\u09aa\u09cb  \u0997\u0999\u09cd\u0997\u09be
\u098b\u09b7\u09bf
-     * </pre>
-     * 
-     */
-    private static final String BANGLA_TEXT_1 = "আমি কোন পথে
ক্ষীরের লক্ষ্মী ষন্ড
পুতুল রুপো গঙ্গা ঋষি";
-    private static final String BANGLA_TEXT_2 = "দ্রুত গাঢ়
শেয়াল অলস কুকুর জুড়ে
জাম্প ধুর্ত  হঠাৎ ভাঙেনি
মৌলিক ঐশি দৈ";
-    private static final String BANGLA_TEXT_3 = "ঋষি কল্লোল
ব্যাস নির্ভয় ";
+    private static final int LINE_GAP = 5;
+    private static final String LOHIT_BENGALI_TTF = "/org/apache/pdfbox/resources/ttf/Lohit-Bengali.ttf";
+    private static final String TEXT_SOURCE_FILE = "/org/apache/pdfbox/resources/ttf/bengali-samples.txt";
+    private static final int FONT_SIZE = 20;
+    private static final int MARGIN = 20;
 
     static
     {
@@ -66,7 +64,7 @@ public class BengaliPdfGenerationHelloWo
     }
 
     private BengaliPdfGenerationHelloWorld()
-    {        
+    {
     }
 
     public static void main(String[] args) throws IOException, URISyntaxException
@@ -84,36 +82,139 @@ public class BengaliPdfGenerationHelloWo
 
         try (PDDocument doc = new PDDocument())
         {
-            PDPage page1 = new PDPage();
-            doc.addPage(page1);
-
-            PDFont font = PDType0Font.load(doc, BengaliPdfGenerationHelloWorld.class
-                    .getResourceAsStream("/org/apache/pdfbox/resources/ttf/Lohit-Bengali.ttf"),
+            PDFont font = PDType0Font.load(doc,
+                    BengaliPdfGenerationHelloWorld.class.getResourceAsStream(LOHIT_BENGALI_TTF),
                     true);
+            PDRectangle rectangle = getPageSize();
+            float workablePageWidth = rectangle.getWidth() - 2 * MARGIN;
+            float workablePageHeight = rectangle.getHeight() - 2 * MARGIN;
+
+            List<List<String>> pagedTexts = getReAlignedTextBasedOnPageHeight(
+                    getReAlignedTextBasedOnPageWidth(getBengaliTextFromFile(), font,
+                            workablePageWidth),
+                    font, workablePageHeight);
 
-            try (PDPageContentStream contents = new PDPageContentStream(doc, page1))
+            for (List<String> linesForPage : pagedTexts)
             {
-                contents.beginText();
-                contents.setFont(font, 12);
-                contents.newLineAtOffset(10, 750);
-                contents.showText(BANGLA_TEXT_1);
-                contents.newLineAtOffset(0, -50);
-                contents.showText(BANGLA_TEXT_2);
-                contents.newLineAtOffset(0, -30);
-                contents.showText(BANGLA_TEXT_3);
-                contents.endText();
-                
-                PDImageXObject pdImage = PDImageXObject
-                        .createFromFile(BengaliPdfGenerationHelloWorld.class
-                                .getResource(
-                                        "/org/apache/pdfbox/resources/ttf/bengali-correct-text.png")
-                                // getFile() doesn't work if there is a space in the path
-                                .toURI().getPath(), doc);
-                contents.drawImage(pdImage, 0, 300, pdImage.getWidth(), pdImage.getHeight());
+                PDPage page = new PDPage(getPageSize());
+                doc.addPage(page);
+
+                try (PDPageContentStream contents = new PDPageContentStream(doc, page))
+                {
+                    contents.beginText();
+                    contents.setFont(font, FONT_SIZE);
+                    contents.newLineAtOffset(rectangle.getLowerLeftX() + MARGIN,
+                            rectangle.getUpperRightY() - MARGIN);
+
+                    for (String line : linesForPage)
+                    {
+                        contents.showText(line);
+                        contents.newLineAtOffset(0, -(FONT_SIZE + LINE_GAP));
+                    }
+
+                    contents.endText();
+
+                }
             }
 
             doc.save(filename);
         }
     }
 
+    private static List<List<String>> getReAlignedTextBasedOnPageHeight(List<String>
originalLines,
+            PDFont font, float workablePageHeight)
+    {
+        final float newLineHeight = font.getFontDescriptor().getFontBoundingBox().getHeight()
/ 1000
+                * FONT_SIZE + LINE_GAP;
+        List<List<String>> realignedTexts = new ArrayList<>();
+        float consumedHeight = 0;
+        List<String> linesInAPage = new ArrayList<>();
+        for (String line : originalLines)
+        {
+            if (newLineHeight + consumedHeight < workablePageHeight)
+            {
+                consumedHeight += newLineHeight;
+            }
+            else
+            {
+                consumedHeight = newLineHeight;
+                realignedTexts.add(linesInAPage);
+                linesInAPage = new ArrayList<>();
+            }
+
+            linesInAPage.add(line);
+        }
+        return realignedTexts;
+    }
+
+    private static List<String> getReAlignedTextBasedOnPageWidth(List<String>
originalLines,
+            PDFont font, float workablePageWidth) throws IOException
+    {
+        List<String> uniformlyWideTexts = new ArrayList<>();
+        float consumedWidth = 0;
+        StringBuilder sb = new StringBuilder();
+        for (String line : originalLines)
+        {
+            float newTokenWidth = 0;
+            StringTokenizer st = new StringTokenizer(line, " ", true);
+            while (st.hasMoreElements())
+            {
+                String token = st.nextToken();
+                newTokenWidth = font.getStringWidth(token) / 1000 * FONT_SIZE;
+                if (newTokenWidth + consumedWidth < workablePageWidth)
+                {
+                    consumedWidth += newTokenWidth;
+                }
+                else
+                {
+                    // add a new text chunk
+                    uniformlyWideTexts.add(sb.toString());
+                    consumedWidth = newTokenWidth;
+                    sb = new StringBuilder();
+                }
+
+                sb.append(token);
+            }
+
+            // add a new text chunk
+            uniformlyWideTexts.add(sb.toString());
+            consumedWidth = newTokenWidth;
+            sb = new StringBuilder();
+        }
+
+        return uniformlyWideTexts;
+    }
+
+    private static PDRectangle getPageSize()
+    {
+        return PDRectangle.A4;
+    }
+
+    private static List<String> getBengaliTextFromFile() throws IOException
+    {
+        List<String> lines = new ArrayList<>();
+
+        try (BufferedReader br = new BufferedReader(new InputStreamReader(
+                BengaliPdfGenerationHelloWorld.class.getResourceAsStream(TEXT_SOURCE_FILE)));)
+        {
+            while (true)
+            {
+                String line = br.readLine();
+
+                if (line == null)
+                {
+                    break;
+                }
+
+                if (line.startsWith("#"))
+                {
+                    continue;
+                }
+                lines.add(line);
+            }
+        }
+
+        return lines;
+    }
+
 }

Added: pdfbox/trunk/examples/src/main/resources/org/apache/pdfbox/resources/ttf/bengali-samples.txt
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/resources/org/apache/pdfbox/resources/ttf/bengali-samples.txt?rev=1830499&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/resources/org/apache/pdfbox/resources/ttf/bengali-samples.txt
(added)
+++ pdfbox/trunk/examples/src/main/resources/org/apache/pdfbox/resources/ttf/bengali-samples.txt
Sun Apr 29 09:50:52 2018
@@ -0,0 +1,105 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+# 
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+#
+#				NOTE:	This file contains bengali text, and is used for testing the correctness of PDF
generation
+#						using the Gsub system, specially rendering of compound characters
+#
+
+
+দুই বিঘা জমি
+
+শুধু বিঘে দুই ছিল মোর
ভুঁই    আর সবই গেছে ঋণে।
+বাবু বলিলেন , ‘ বুঝেছ
উপেন ,    এ জমি লইব কিনে।
'
+কহিলাম আমি , ‘ তুমি
ভূস্বামী ,    ভূমির অন্ত
নাই।
+চেয়ে দেখো মোর আছে বড়ো
- জোর    মরিবার মতো ঠাঁই।
'
+শুনি রাজা কহে , ‘ বাপু
, জানো তো হে , করেছি বাগানখানা
+পেলে দুই বিঘে প্রস্থ
ও দিঘে    সমান হইবে টানা
—
+ওটা দিতে হবে। ' কহিলাম
তবে    বক্ষে জুড়িয়া
পাণি
+সজল চক্ষে , ‘ করুণ রক্ষে
   গরিবের ভিটেখানি ।
+সপ্ত পুরুষ যেথায় মানুষ
   সে মাটি সোনার বাড়া
,
+দৈন্যের দায়ে বেচিব
সে মায়ে    এমনি লক্ষ্মীছাড়া
! '
+আঁখি করি লাল রাজা ক্ষণকাল
   রহিল মৌনভাবে ,
+কহিলেন শেষে ক্রূর
হাসি হেসে ,   ‘ আচ্ছা
, সে দেখা যাবে। '
+ 
+পরে মাস দেড়ে ভিটে মাটি
ছেড়ে    বাহির হইনু পথে
—
+করিল ডিক্রি , সকলই
বিক্রি    মিথ্যা দেনার
খতে।
+এ জগতে , হায় , সেই বেশি
চায়    আছে যার ভূরি ভূরি
—
+রাজার হস্ত করে সমস্ত
   কাঙালের ধন চুরি।
+মনে ভাবিলাম মোরে ভগবান
   রাখিবে না মোহগর্তে
,
+তাই লিখি দিল বিশ্বনিখিল
   দু বিঘার পরিবর্তে
।
+সন্ন্যাসীবেশে ফিরি
দেশে দেশে    হইয়া সাধুর
শিষ্য
+কত হেরিলাম মনোহর ধাম
, কত মনোরম দৃশ্য !
+ভূধরে সাগরে বিজনে
নগরে    যখন যেখানে ভ্রমি
+তবু নিশিদিনে ভুলিতে
পারি নে    সেই দুই বিঘা
জমি।
+হাটে মাঠে বাটে এই মতো
কাটে    বছর পনেরো - ষোলো
—
+একদিন শেষে ফিরিবারে
দেশে    বড়ই বাসনা হল।
+ 
+নমোনমো নম সুন্দরী
মম    জননী বঙ্গভূমি
!
+গঙ্গার তীর স্নিগ্ধ
সমীর ,    জীবন জুড়ালে
তুমি।
+অবারিত মাঠ , গগনললাট
  চুমে তব পদধূলি ,
+ছায়াসুনিবিড় শান্তির
নীড়    ছোটো ছোটো গ্রামগুলি।

+
+পল্লবঘন আম্রকানন
   রাখালের খেলাগেহ ,
+স্তব্ধ অতল দিঘি কালোজল
—   নিশীথশীতল স্নেহ।
+বুকভরা মধু বঙ্গের
বধূ    জল লয়ে যায় ঘরে
—
+ 
+মা বলিতে প্রাণ করে
আনচান ,    চোখে আসে জল
ভরে।
+দুই দিন পরে দ্বিতীয়
প্রহরে    প্রবেশিনু
নিজগ্রামে —
+কুমোরের বাড়ি দক্ষিণে
ছাড়ি    রথতলা করি বামে
,
+রাখি হাটখোলা , নন্দীর
গোলা , মন্দির করি পাছে
+তৃষাতুর শেষে পঁহুছিনু
এসে    আমার বাড়ির কাছে
।
+ 
+ধিক্‌ ধিক্‌ ওরে , শতধিক্‌
তোরে ,    নিলাজ কুলটা
ভূমি !
+যখনি যাহার তখনি তাহার
,    এই কি জননী তুমি !
+সে কি মনে হবে একদিন
যবে    ছিলে দরিদ্রমাতা
+আঁচল ভরিয়া রাখিতে
ধরিয়া    ফল ফুল শাক পাতা
!
+আজ কোন্‌ রীতে কারে
ভুলাইতে    ধরেছ বিলাসবেশ
—
+পাঁচরঙা পাতা অঞ্চলে
গাঁথা , পুষ্পে খচিত
কেশ !
+আমি তোর লাগি ফিরেছি
বিবাগি    গৃহহারা সুখহীন
—
+তুই হেথা বসি ওরে রাক্ষসী
,    হাসিয়া কাটাস দিন
!
+ধনীর আদরে গরব না ধরে!
   এতই হয়েছ ভিন্ন
+কোনোখানে লেশ নাহি
অবশেষ    সেদিনের কোনো
চিহ্ন !
+কল্যাণময়ী ছিলে তুমি
অয়ি ,    ক্ষুধাহরা সুধারাশি
!
+যত হাসো আজ যত করো সাজ
   ছিলে দেবী , হলে দাসী।
+ 
+বিদীর্ণ হিয়া ফিরিয়া
ফিরিয়া    চারি দিকে
চেয়ে দেখি —
+প্রাচীরের কাছে এখনো
যে আছে ,    সেই আমগাছ একি
!
+বসি তার তলে নয়নের জলে
   শান্ত হইল ব্যথা ,
+একে একে মনে উদিল স্মরণে
   বালক - কালের কথা ।
+সেই মনে পড়ে জ্যৈষ্ঠের
ঝড়ে   রাত্রে নাহিকো
ঘুম ,
+অতি ভোরে উঠি তাড়াতাড়ি
ছুটি    আম কুড়াবার ধুম
।
+সেই সুমধুর স্তব্ধ
দুপুর ,    পাঠশালা - পলায়ন
—
+ভাবিলাম হায় আর কি কোথায়
   ফিরে পাব সে জীবন !
+সহসা বাতাস ফেলি গেল
শ্বাস    শাখা দুলাইয়া
গাছে , 
+
+দুটি পাকা ফল লভিল ভূতল
   আমার কোলের কাছে।
+ভাবিলাম মনে বুঝি এতখনে
   আমারে চিনিল মাতা
,
+স্নেহের সে দানে বহু
সম্মানে    বারেক ঠেকানু
মাথা।
+ 
+হেনকালে হায় যমদূত
- প্রায়   কোথা হতে এল
মালী ,
+ঝুঁটি - বাঁধা উড়ে সপ্তম
সুরে    পাড়িতে লাগিল
গালি।
+কহিলাম তবে , ‘ আমি তো
নীরবে    দিয়েছি আমার
সব —
+  দুটি ফল তার করি অধিকার
,    এত তারি কলরব ! '
+চিনিল না মোরে , নিয়ে
গেল ধরে   কাঁধে তুলি
লাঠিগাছ —
+বাবু ছিপ হাতে পারিষদ
- সাথে    ধরিতেছিলেন
মাছ।
+শুনি বিবরণ ক্রোধে
তিনি কন ,    ‘ মারিয়া করিব
খুন ! '
+বাবু যত বলে পারিষদ
- দলে    বলে তার শতগুণ।
+আমি কহিলাম , ‘ শুধু
দুটি আম   ভিখ মাগি মহাশয়
! '
+বাবু কহে হেসে , ‘ বেটা
সাধুবেশে    পাকা চোর
অতিশয়। '
+আমি শুনে হাসি আঁখিজলে
ভাসি ,    এই ছিল মোর ঘটে
—
+তুমি মহারাজ সাধু হলে
আজ ,    আমি আজ চোর বটে !

+

Propchange: pdfbox/trunk/examples/src/main/resources/org/apache/pdfbox/resources/ttf/bengali-samples.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/gsub/GlyphSubstitutionDataExtractor.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/gsub/GlyphSubstitutionDataExtractor.java?rev=1830499&r1=1830498&r2=1830499&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/gsub/GlyphSubstitutionDataExtractor.java
(original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/gsub/GlyphSubstitutionDataExtractor.java
Sun Apr 29 09:50:52 2018
@@ -114,15 +114,17 @@ public class GlyphSubstitutionDataExtrac
             FeatureRecord featureRecord, LookupListTable lookupListTable)
     {
 
-        LOG.debug("*********** extracting GSUB data for the feature: "
-                + featureRecord.getFeatureTag());
-
         Map<List<Integer>, Integer> glyphSubstitutionMap = new LinkedHashMap<>();
         for (int lookupIndex : featureRecord.getFeatureTable().getLookupListIndices())
         {
             LookupTable lookupTable = lookupListTable.getLookups()[lookupIndex];
             extractData(glyphSubstitutionMap, lookupTable);
         }
+
+        LOG.debug("*********** extracting GSUB data for the feature: "
+                + featureRecord.getFeatureTag() + ", glyphSubstitutionMap: "
+                + glyphSubstitutionMap);
+
         gsubData.put(featureRecord.getFeatureTag(),
                 Collections.unmodifiableMap(glyphSubstitutionMap));
     }

Modified: pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/gsub/GsubWorkerForBengali.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/gsub/GsubWorkerForBengali.java?rev=1830499&r1=1830498&r2=1830499&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/gsub/GsubWorkerForBengali.java
(original)
+++ pdfbox/trunk/fontbox/src/main/java/org/apache/fontbox/ttf/gsub/GsubWorkerForBengali.java
Sun Apr 29 09:50:52 2018
@@ -49,7 +49,7 @@ public class GsubWorkerForBengali implem
      * "https://docs.microsoft.com/en-us/typography/script-development/bengali">https://docs.microsoft.com/en-us/typography/script-development/bengali</a>
      */
     private static final List<String> FEATURES_IN_ORDER = Arrays.asList("locl", "nukt",
"akhn",
-            "rphf", "blwf", "half", "pstf", "vatu", "cjct", INIT_FEATURE, "pres", "abvs",
"blws",
+            "rphf", "blwf", "pstf", "half", "vatu", "cjct", INIT_FEATURE, "pres", "abvs",
"blws",
             "psts", "haln", "calt");
 
     private static final char[] BEFORE_HALF_CHARS = new char[] { '\u09BF', '\u09C7', '\u09C8'
};

Added: pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/ttf/GSUBTableDebugger.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/ttf/GSUBTableDebugger.java?rev=1830499&view=auto
==============================================================================
--- pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/ttf/GSUBTableDebugger.java (added)
+++ pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/ttf/GSUBTableDebugger.java Sun Apr
29 09:50:52 2018
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.fontbox.ttf;
+
+import java.io.IOException;
+
+import org.apache.fontbox.ttf.gsub.GSUBTablePrintUtil;
+import org.apache.fontbox.ttf.model.GsubData;
+import org.junit.Test;
+
+/**
+ * This class is to be used mainly for debugging purposes. Prints the GSUB Feature table
for debugging.
+ * 
+ * @author Palash Ray
+ */
+public class GSUBTableDebugger
+{
+
+    private static final String LOHIT_BENGALI_FONT_FILE = "/ttf/Lohit-Bengali.ttf";
+
+    @Test
+    public void printLohitBengaliTTF() throws IOException
+    {
+        MemoryTTFDataStream memoryTTFDataStream = new MemoryTTFDataStream(
+                GSUBTableDebugger.class.getResourceAsStream(LOHIT_BENGALI_FONT_FILE));
+
+        memoryTTFDataStream.seek(GlyphSubstitutionTableTest.DATA_POSITION_FOR_GSUB_TABLE);
+
+        GlyphSubstitutionTable glyphSubstitutionTable = new GlyphSubstitutionTable(null);
+
+        glyphSubstitutionTable.read(null, memoryTTFDataStream);
+
+        TrueTypeFont trueTypeFont = new TTFParser()
+                .parse(GSUBTableDebugger.class.getResourceAsStream(LOHIT_BENGALI_FONT_FILE));
+
+        GsubData gsubData = glyphSubstitutionTable.getGsubData();
+        new GSUBTablePrintUtil().printCharacterToGlyph(gsubData,
+                trueTypeFont.getUnicodeCmapLookup());
+    }
+
+}

Propchange: pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/ttf/GSUBTableDebugger.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/ttf/GlyphSubstitutionTableTest.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/ttf/GlyphSubstitutionTableTest.java?rev=1830499&r1=1830498&r2=1830499&view=diff
==============================================================================
--- pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/ttf/GlyphSubstitutionTableTest.java
(original)
+++ pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/ttf/GlyphSubstitutionTableTest.java
Sun Apr 29 09:50:52 2018
@@ -40,7 +40,7 @@ import org.junit.Test;
 public class GlyphSubstitutionTableTest
 {
 
-    private static final int DATA_POSITION_FOR_GSUB_TABLE = 120544;
+    static final int DATA_POSITION_FOR_GSUB_TABLE = 120544;
 
     private static final Collection<String> EXPECTED_FEATURE_NAMES = Arrays.asList("abvs",
"akhn",
             "blwf", "blws", "half", "haln", "init", "nukt", "pres", "pstf", "rphf", "vatu");

Added: pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/ttf/gsub/GSUBTablePrintUtil.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/ttf/gsub/GSUBTablePrintUtil.java?rev=1830499&view=auto
==============================================================================
--- pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/ttf/gsub/GSUBTablePrintUtil.java
(added)
+++ pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/ttf/gsub/GSUBTablePrintUtil.java
Sun Apr 29 09:50:52 2018
@@ -0,0 +1,143 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.fontbox.ttf.gsub;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.fontbox.ttf.CmapLookup;
+import org.apache.fontbox.ttf.GSUBTableDebugger;
+import org.apache.fontbox.ttf.model.GsubData;
+import org.apache.fontbox.ttf.model.ScriptFeature;
+
+/**
+ * This class is to be used mainly for debugging purposes. It is used in {@link GSUBTableDebugger}.
It is used to print
+ * all the GSUB Glyphs to characters. The format is: <br>
+ * 
+ * &lt;Serial no.&gt;.) &lt;Space separated characters to be replaced&gt;
: RawUnicode: [&lt;Space separated unicode
+ * representation of each character to be replaced in hexadecimal&gt;] : &lt;The
compound character&gt; : &lt;The
+ * GlyphId with which these characters are replaced&gt;
+ * 
+ * @author Palash Ray
+ * 
+ */
+public class GSUBTablePrintUtil
+{
+
+    public void printCharacterToGlyph(GsubData gsubData, CmapLookup cmap)
+    {
+        System.err.println(
+                "Format:\n<Serial no.>.) <Space separated characters to be replaced>
: RawUnicode: [<Space separated unicode representation of each character to be replaced
in hexadecimal>] : <The compound character> : <The GlyphId with which these characters
are replaced>");
+        Map<Integer, List<Integer>> rawGSubTableData = new HashMap<>();
+
+        for (String featureName : gsubData.getSupportedFeatures())
+        {
+            ScriptFeature scriptFeature = gsubData.getFeature(featureName);
+            for (List<Integer> glyphsToBeReplaced : scriptFeature.getAllGlyphIdsForSubstitution())
+            {
+                rawGSubTableData.put(scriptFeature.getReplacementForGlyphs(glyphsToBeReplaced),
+                        glyphsToBeReplaced);
+            }
+
+        }
+
+        for (String featureName : gsubData.getSupportedFeatures())
+        {
+            System.out
+                    .println("******************      " + featureName + "      ******************");
+            ScriptFeature scriptFeature = gsubData.getFeature(featureName);
+            int index = 0;
+            for (List<Integer> glyphsToBeReplaced : scriptFeature.getAllGlyphIdsForSubstitution())
+            {
+                String unicodeText = getUnicodeString(rawGSubTableData, cmap, glyphsToBeReplaced);
+                System.out.println(++index + ".) " + getExplainedUnicodeText(unicodeText)
+ " : "
+                        + scriptFeature.getReplacementForGlyphs(glyphsToBeReplaced));
+            }
+
+        }
+
+    }
+
+    private String getUnicodeChar(Map<Integer, List<Integer>> rawGSubTableData,
CmapLookup cmap,
+            Integer glyphId)
+    {
+        List<Integer> keyChars = cmap.getCharCodes(glyphId);
+
+        // its a compound glyph
+        if (keyChars == null)
+        {
+            List<Integer> constituentGlyphs = rawGSubTableData.get(glyphId);
+
+            if (constituentGlyphs == null || constituentGlyphs.isEmpty())
+            {
+                String message = "lookup for the glyphId: " + glyphId
+                        + " failed, as no corresponding Unicode char found mapped to it";
+                throw new IllegalStateException(message);
+            }
+            else
+            {
+                return getUnicodeString(rawGSubTableData, cmap, constituentGlyphs);
+            }
+        }
+        else
+        {
+            StringBuilder sb = new StringBuilder();
+            for (int unicodeChar : keyChars)
+            {
+                sb.append((char) unicodeChar);
+            }
+            return sb.toString();
+        }
+
+    }
+
+    private String getUnicodeString(Map<Integer, List<Integer>> rawGSubTableData,
CmapLookup cmap,
+            List<Integer> glyphIDs)
+    {
+        StringBuilder sb = new StringBuilder();
+        for (Integer glyphId : glyphIDs)
+        {
+            sb.append(getUnicodeChar(rawGSubTableData, cmap, glyphId));
+        }
+        return sb.toString();
+    }
+
+    private String getExplainedUnicodeText(String unicodeText)
+    {
+        StringBuilder sb = new StringBuilder();
+
+        for (char unicode : unicodeText.toCharArray())
+        {
+            sb.append(unicode).append(" ");
+        }
+        sb.append(":");
+
+        sb.append(" RawUnicode: [");
+        for (char unicode : unicodeText.toCharArray())
+        {
+            sb.append("\\u0").append(Integer.toHexString(unicode).toUpperCase()).append("
");
+        }
+        sb.append("] : ");
+
+        sb.append(unicodeText);
+
+        return sb.toString();
+    }
+
+}

Propchange: pdfbox/trunk/fontbox/src/test/java/org/apache/fontbox/ttf/gsub/GSUBTablePrintUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message