Пример #1
0
 private void createMarks(List<Mark> acc, Mark.ENTRY_PART part, String text, int firstMissing) {
   char[] chars = text.toCharArray();
   int i = firstMissing;
   while ((i = editorFont.canDisplayUpTo(chars, i, chars.length)) != -1) {
     int cp = Character.codePointAt(chars, i);
     int start = i;
     i += Character.charCount(cp);
     Font font = FontFallbackManager.getCapableFont(cp);
     if (font == null) {
       continue;
     }
     // Look ahead to try to group as many characters as possible into this run.
     for (int cpn, ccn, j = i; j < chars.length; j += ccn) {
       cpn = Character.codePointAt(chars, j);
       ccn = Character.charCount(cpn);
       if (!editorFont.canDisplay(cpn) && font.canDisplay(cpn)) {
         i += ccn;
       } else {
         break;
       }
     }
     Mark m = new Mark(part, start, i);
     m.attributes = getAttributes(font);
     acc.add(m);
   }
 }
Пример #2
0
  @NotNull
  public static String getHtmlWithFonts(@NotNull String input, int style, @NotNull Font baseFont) {
    int start = baseFont.canDisplayUpTo(input);
    if (start == -1) return input;

    Font font = null;
    StringBuilder result = new StringBuilder();
    for (int i = start; i < input.length(); i++) {
      char c = input.charAt(i);
      if (baseFont.canDisplay(c)) {
        if (font != null) result.append("</font>");
        result.append(c);
        font = null;
      } else if (font != null && font.canDisplay(c)) {
        result.append(c);
      } else {
        if (font != null) result.append("</font>");
        font = getFontAbleToDisplay(c, baseFont.getSize(), style, baseFont.getFamily());
        if (font != baseFont) result.append("<font face=\"").append(font.getFamily()).append("\">");
        result.append(c);
      }
    }
    if (font != null) result.append("</font>");

    return result.toString();
  }
Пример #3
0
 private static boolean isValidFont(final Font font) {
   try {
     return font.canDisplay('a')
         && font.canDisplay('z')
         && font.canDisplay('A')
         && font.canDisplay('Z')
         && font.canDisplay('0')
         && font.canDisplay('1');
   } catch (Exception e) {
     // JRE has problems working with the font. Just skip.
     return false;
   }
 }
Пример #4
0
 private boolean canDisplayImpl(char c) {
   if (USE_ALTERNATIVE_CAN_DISPLAY_PROCEDURE) {
     return myFont.createGlyphVector(DUMMY_CONTEXT, new char[] {c}).getGlyphCode(0) > 0;
   } else {
     return myFont.canDisplay(c);
   }
 }
Пример #5
0
  @Nullable
  Font getFontAbleToDisplay(LookupElementPresentation p) {
    String sampleString = p.getItemText() + p.getTailText() + p.getTypeText();

    // assume a single font can display all lookup item chars
    Set<Font> fonts = ContainerUtil.newHashSet();
    for (int i = 0; i < sampleString.length(); i++) {
      fonts.add(
          EditorUtil.fontForChar(sampleString.charAt(i), Font.PLAIN, myLookup.getEditor())
              .getFont());
    }

    eachFont:
    for (Font font : fonts) {
      if (font.equals(myNormalFont)) continue;

      for (int i = 0; i < sampleString.length(); i++) {
        if (!font.canDisplay(sampleString.charAt(i))) {
          continue eachFont;
        }
      }
      return font;
    }
    return null;
  }
Пример #6
0
  private void importTTFButtonActionPerformed(ActionEvent evt) {
    TreeItem item = mainPanel.tagTree.getCurrentTreeItem();
    if (item instanceof FontTag) {
      FontTag ft = (FontTag) item;

      JFileChooser fc = new JFileChooser();
      fc.setCurrentDirectory(new File(Configuration.lastOpenDir.get()));
      FileFilter ttfFilter =
          new FileFilter() {
            @Override
            public boolean accept(File f) {
              return (f.getName().toLowerCase().endsWith(".ttf")) || (f.isDirectory());
            }

            @Override
            public String getDescription() {
              return "TTF files";
            }
          };
      fc.setFileFilter(ttfFilter);

      fc.setAcceptAllFileFilterUsed(false);
      JFrame fr = new JFrame();
      View.setWindowIcon(fr);
      int returnVal = fc.showOpenDialog(fr);
      if (returnVal == JFileChooser.APPROVE_OPTION) {
        Configuration.lastOpenDir.set(
            Helper.fixDialogFile(fc.getSelectedFile()).getParentFile().getAbsolutePath());
        File selfile = Helper.fixDialogFile(fc.getSelectedFile());
        Set<Integer> selChars = new HashSet<>();
        try {
          Font f = Font.createFont(Font.TRUETYPE_FONT, selfile);
          int required[] = new int[] {0x0001, 0x0000, 0x000D, 0x0020};
          loopi:
          for (char i = 0; i < Character.MAX_VALUE; i++) {
            for (int r : required) {
              if (r == i) {
                continue loopi;
              }
            }
            if (f.canDisplay((int) i)) {
              selChars.add((int) i);
            }
          }
          fontAddChars(ft, selChars, f);
          mainPanel.reload(true);
        } catch (FontFormatException ex) {
          JOptionPane.showMessageDialog(mainPanel, "Invalid TTF font");
        } catch (IOException ex) {
          Logger.getLogger(FontPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
    }
  }
 private TextLayout createAndCacheTextLayout(
     int fragmentIndex, Font basefont, FontRenderContext fontRenderContext) {
   String text = myFragments.get(fragmentIndex);
   AttributedString string = new AttributedString(text);
   int start = 0;
   int end = text.length();
   AttributedCharacterIterator it =
       string.getIterator(new AttributedCharacterIterator.Attribute[0], start, end);
   Font currentFont = basefont;
   int currentIndex = start;
   for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
     Font font = basefont;
     if (!font.canDisplay(c)) {
       for (SuitableFontProvider provider : SuitableFontProvider.EP_NAME.getExtensions()) {
         font =
             provider.getFontAbleToDisplay(
                 c, basefont.getSize(), basefont.getStyle(), basefont.getFamily());
         if (font != null) break;
       }
     }
     int i = it.getIndex();
     if (!Comparing.equal(currentFont, font)) {
       if (i > currentIndex) {
         string.addAttribute(TextAttribute.FONT, currentFont, currentIndex, i);
       }
       currentFont = font;
       currentIndex = i;
     }
   }
   if (currentIndex < end) {
     string.addAttribute(TextAttribute.FONT, currentFont, currentIndex, end);
   }
   TextLayout layout = new TextLayout(string.getIterator(), fontRenderContext);
   if (fragmentIndex >= myLayouts.size()) {
     myLayouts.addAll(Collections.nCopies(fragmentIndex - myLayouts.size() + 1, null));
   }
   myLayouts.set(fragmentIndex, layout);
   myLayoutFont = getBaseFont();
   return layout;
 }
 public static BitmapFont createFromFont(Font font) {
   font = font.deriveFont(FONT_SIZE);
   int imageWidth = CHAR_WIDTH * NUM_CHARACTERS;
   //noinspection UndesirableClassUsage
   BufferedImage image =
       new BufferedImage(imageWidth, CHAR_HEIGHT, BufferedImage.TYPE_BYTE_BINARY);
   Graphics2D g = image.createGraphics();
   g.setColor(Color.white);
   g.fillRect(0, 0, imageWidth, CHAR_HEIGHT);
   g.setColor(Color.black);
   g.setFont(font);
   char[] c = new char[1];
   for (c[0] = 0; c[0] < NUM_CHARACTERS; c[0]++) {
     if (font.canDisplay(c[0])) {
       int x = c[0] * CHAR_WIDTH;
       g.setClip(x, 0, CHAR_WIDTH, CHAR_HEIGHT);
       g.drawChars(c, 0, 1, x, CHAR_HEIGHT - CHAR_DESCENT);
     }
   }
   g.dispose();
   return new BitmapFont(image);
 }
Пример #9
0
 private void parseProblemGlyphs() {
   myCheckedForProblemGlyphs = true;
   BufferedImage buffer = UIUtil.createImage(20, 20, BufferedImage.TYPE_INT_RGB);
   final Graphics graphics = buffer.getGraphics();
   if (!(graphics instanceof Graphics2D)) {
     return;
   }
   final FontRenderContext context = ((Graphics2D) graphics).getFontRenderContext();
   char[] charBuffer = new char[1];
   for (char c = 0; c < 128; c++) {
     if (!myFont.canDisplay(c)) {
       continue;
     }
     charBuffer[0] = c;
     final GlyphVector vector = myFont.createGlyphVector(context, charBuffer);
     final float y = vector.getGlyphMetrics(0).getAdvanceY();
     if (Math.round(y) != 0) {
       mySymbolsToBreakDrawingIteration.add(c);
     }
   }
   myHasGlyphsToBreakDrawingIteration = !mySymbolsToBreakDrawingIteration.isEmpty();
 }
Пример #10
0
  public static void convertQuick() {
    try {

      HashMap<String, ArrayList<Character>> charList = new HashMap<String, ArrayList<Character>>();

      Font font = new Font("Droid Sans Fallback", 16, Font.PLAIN);
      int totalQuickColumn = 3;
      FileInputStream fis = new FileInputStream("quick-classic.txt");
      InputStreamReader input = new InputStreamReader(fis, "UTF-8");
      BufferedReader reader = new BufferedReader(input);
      String str = null;
      int index = 0;
      int total = 0;
      ArrayList<String> keyList = new ArrayList<String>();
      System.out.println("#define QUICK_COLUMN " + totalQuickColumn);
      System.out.println("const jchar quick[][QUICK_COLUMN] = {");
      do {
        str = reader.readLine();
        if (str == null) break;
        index = str.indexOf('\t');
        if (index < 0) index = str.indexOf(' ');
        if (index > 0) {
          if (font.canDisplay(str.charAt(index + 1))) {
            StringBuffer sb = new StringBuffer();
            // System.out.print("\t { ");
            if ((int) str.charAt(1) == 9 || str.charAt(1) == ' ') {
              // System.out.print("'" + str.charAt(0) + "',   0, ");
              sb.append(str.charAt(0));
            } else {
              sb.append(str.charAt(0));
              sb.append(str.charAt(1));
              // System.out.print("'" + str.charAt(0) + "', '" + str.charAt(1) + "', ");
            }
            String key = sb.toString();
            // System.out.println((int) str.charAt(index + 1) + " }, ");
            Character ch = new Character(str.charAt(index + 1));

            if (!keyList.contains(key)) keyList.add(key);

            if (charList.containsKey(key)) {
              charList.get(key).add(ch);
            } else {
              ArrayList<Character> c = new ArrayList<Character>();
              c.add(ch);
              charList.put(key, c);
            }
            total++;
          }
        }
      } while (str != null);
      for (int count = 0; count < keyList.size(); count++) {
        String k = keyList.get(count);
        ArrayList<Character> l = charList.get(k);
        for (int loop = 0; loop < l.size(); loop++) {
          if (k.length() == 1) {
            System.out.println("\t { '" + k.charAt(0) + "',   0, " + l.get(loop) + " }, ");
          } else {
            System.out.println(
                "\t { '" + k.charAt(0) + "', '" + k.charAt(1) + "', " + l.get(loop) + " }, ");
          }
        }
      }
      System.out.println("};");
      System.out.println("jint quick_index[" + total + "];");
      System.out.println("jint quick_frequency[" + total + "];");
      reader.close();
      input.close();
      fis.close();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
Пример #11
0
  public static void convertCangjieHK() {
    try {
      Font font = new Font("Droid Sans Fallback", 16, Font.PLAIN);
      ArrayList<String> codeList = new ArrayList<String>();
      HashMap<String, ArrayList<CangjieChar>> codeMap =
          new HashMap<String, ArrayList<CangjieChar>>();
      int totalCangjieColumn = 7;
      FileInputStream fis = new FileInputStream("cangjie3.txt");
      InputStreamReader input = new InputStreamReader(fis, "UTF-8");
      BufferedReader reader = new BufferedReader(input);
      String str = null;
      int index = 0;
      int total = 0;
      char column[] = new char[5];
      boolean hkchar = false;

      System.out.println("#define CANGJIE_COLUMN " + totalCangjieColumn);
      System.out.println("const jchar cangjie[][CANGJIE_COLUMN] = {");
      do {
        str = reader.readLine();
        if (str == null) break;
        if (str.compareTo("#####") == 0) {
          hkchar = true;
          continue;
        }
        index = str.indexOf('\t');
        if (index < 0) index = str.indexOf(' ');
        if (index > 0 && font.canDisplay(str.charAt(index + 1))) {
          int type = Character.getType(str.charAt(index + 1));
          if (Character.isLetter(str.charAt(index + 1))
              || type == Character.START_PUNCTUATION
              || type == Character.END_PUNCTUATION
              || type == Character.OTHER_PUNCTUATION
              || type == Character.MATH_SYMBOL
              || type == Character.DASH_PUNCTUATION
              || type == Character.CONNECTOR_PUNCTUATION
              || type == Character.OTHER_SYMBOL
              || type == Character.INITIAL_QUOTE_PUNCTUATION
              || type == Character.FINAL_QUOTE_PUNCTUATION
              || type == Character.SPACE_SEPARATOR) {
            // System.out.print("\t { ");
            // for (int count = 0; count < 5; count++) {
            //     if (count < index) {
            // 	column[count] = str.charAt(count);
            // 	if (column[count] < 'a' || column[count] > 'z') column[count] = 0;
            // 	if (((int) column[count]) >= 10 || ((int) column[count]) <= 99) System.out.print('
            // ');
            // 	if (((int) column[count]) <= 9) System.out.print(' ');
            // 	System.out.print(((int)	column[count]));
            //     } else {
            // 	System.out.print("  0");
            //     }
            //     System.out.print(", ");
            // }
            // System.out.println((int) str.charAt(index + 1) + " }, ");

            String cangjie = str.substring(0, index).trim();
            char ch = str.charAt(index + 1);
            if (!codeList.contains(cangjie)) codeList.add(cangjie);
            ArrayList<CangjieChar> list = null;
            if (codeMap.containsKey(cangjie)) {
              list = codeMap.get(cangjie);
            } else {
              list = new ArrayList<CangjieChar>();
            }
            CangjieChar cc = new CangjieChar(ch, hkchar);
            list.add(cc);
            codeMap.put(cangjie, list);

            total++;
          } else {
            System.err.println(
                "Character Not Found : "
                    + str.charAt(index + 1)
                    + " "
                    + Character.getType(str.charAt(index + 1)));
          }
        }
      } while (str != null);

      Collections.sort(codeList);

      for (int count0 = 0; count0 < codeList.size(); count0++) {
        String _str = codeList.get(count0);
        ArrayList<CangjieChar> ca = codeMap.get(_str);
        for (int count1 = 0; count1 < ca.size(); count1++) {
          for (int count2 = 0; count2 < 5; count2++) {
            if (count2 < _str.length()) System.out.print("'" + _str.charAt(count2) + "', ");
            else System.out.print("  0, ");
          }
          System.out.println(((int) ca.get(count1).c) + ", " + (ca.get(count1).hk ? 1 : 0) + ", ");
        }
      }

      System.out.println("};");
      System.out.println("jint cangjie_index[" + total + "];");
      System.out.println("jint cangjie_frequency[" + total + "];");
      reader.close();
      input.close();
      fis.close();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
Пример #12
0
  /** @param args the command line arguments */
  public static void main(String args[]) {
    java.awt.Font runenfont = new java.awt.Font("Runenprojekt", java.awt.Font.PLAIN, 14);
    java.awt.Font unicodefont = new java.awt.Font("Arial Unicode MS", java.awt.Font.PLAIN, 14);
    int i = 0;
    Format numberFormat = new Format();
    numberFormat.setProperty("font:name", "Arial Unicode MS");
    numberFormat.setProperty("chunk-border", "lrb");
    numberFormat.setID("nf");
    Format runenFormat = new Format();
    runenFormat.setProperty("chunk-border", "lrb");
    runenFormat.setProperty("font:name", "Runenprojekt");
    runenFormat.setID("rf");

    ItBundle itb = new ItBundle();
    ItLine numberLine = new ItLine();
    ItLabel numberLabel = new ItLabel();
    numberLabel.setFormat(numberFormat);
    Run nr = new Run();
    nr.setText("Unicode dezimal");
    numberLabel.addRun(nr);
    numberLine.setLabel(numberLabel);

    ItLine charLine1 = new ItLine();
    ItLabel charLabel1 = new ItLabel();
    charLabel1.setFormat(numberFormat);
    Run cr1 = new Run();
    cr1.setText("Runenfont");
    charLabel1.addRun(cr1);
    charLine1.setLabel(charLabel1);

    ItLine charLine2 = new ItLine();
    ItLabel charLabel2 = new ItLabel();
    charLabel2.setFormat(numberFormat);
    Run cr2 = new Run();
    cr2.setText("Arial Unicode MS");
    charLabel2.addRun(cr2);
    charLine2.setLabel(charLabel2);

    for (char c = 0; c < Character.MAX_VALUE; c++) {
      if ((c <= 255) || (runenfont.canDisplay(c))) {
        System.out.println(i + " " + c);
        SyncPoint sp = new SyncPoint();
        sp.setID("S" + Integer.toString(i));
        sp.setFormat(numberFormat);
        itb.getSyncPoints().addSyncPoint(sp);

        ItChunk itc = new ItChunk();
        Run run = new Run();
        run.setFormat(numberFormat);
        run.setText(Integer.toString(i));
        itc.setFormat(numberFormat);
        itc.setStart(sp);
        itc.addRun(run);
        numberLine.addItChunk(itc);

        itc = new ItChunk();
        run = new Run();
        run.setFormat(runenFormat);
        if (runenfont.canDisplay(c)) {
          run.setText(String.valueOf(c));
        } else {
          run.setText("no glyph");
        }
        itc.setStart(sp);
        itc.setFormat(runenFormat);
        itc.addRun(run);
        charLine1.addItChunk(itc);

        itc = new ItChunk();
        run = new Run();
        run.setFormat(numberFormat);
        if (unicodefont.canDisplay(c)) {
          run.setText(String.valueOf(c));
        } else {
          run.setText("no glyph");
        }
        itc.setStart(sp);
        itc.setFormat(numberFormat);
        itc.addRun(run);
        charLine2.addItChunk(itc);
      }
      i++;
    }
    InterlinearText it = new InterlinearText();
    it.getFormats().addFormat(numberFormat);
    it.getFormats().addFormat(runenFormat);
    itb.addItLine(numberLine);
    itb.addItLine(charLine1);
    itb.addItLine(charLine2);
    SyncPoint sp = new SyncPoint();
    sp.setID("END");
    sp.setFormat(numberFormat);
    itb.getSyncPoints().addSyncPoint(sp);
    itb.implyEnds();
    it.addItElement(itb);
    it.trim(new PageOutputParameters());
    try {
      it.writeHTMLToFile("c:\\out.html", new HTMLParameters());
      it.writeXMLToFile("c:\\out.xml");
      it.writeTestRTF("c:\\out.rtf", new RTFParameters());
    } catch (java.io.IOException ioe) {
      ioe.printStackTrace();
    }
  }
Пример #13
0
 public static boolean unicodeStringTest(Font swtFont, String s) {
   FontData fd = swtFont.getFontData()[0];
   java.awt.Font awtFont = new java.awt.Font(fd.getName(), 0, fd.getHeight());
   for (int i = 0; i < s.length(); i++) if (!awtFont.canDisplay(s.charAt(i))) return false;
   return true;
 }
Пример #14
0
  private void fontAddChars(FontTag ft, Set<Integer> selChars, Font font) {
    FontTag f = (FontTag) mainPanel.tagTree.getCurrentTreeItem();
    String oldchars = f.getCharacters();
    for (int ic : selChars) {
      char c = (char) ic;
      if (oldchars.indexOf((int) c) == -1) {
        font = font.deriveFont(f.getFontStyle(), 1024);
        if (!font.canDisplay(c)) {
          String msg = translate("error.font.nocharacter").replace("%char%", "" + c);
          Logger.getLogger(FontPanel.class.getName()).log(Level.SEVERE, msg);
          View.showMessageDialog(null, msg, translate("error"), JOptionPane.ERROR_MESSAGE);
          return;
        }
      }
    }

    String[] yesno =
        new String[] {
          translate("button.yes"),
          translate("button.no"),
          translate("button.yes.all"),
          translate("button.no.all")
        };
    boolean yestoall = false;
    boolean notoall = false;
    for (int ic : selChars) {
      char c = (char) ic;
      if (oldchars.indexOf((int) c) > -1) {
        int opt = -1;
        if (!(yestoall || notoall)) {
          opt =
              View.showOptionDialog(
                  null,
                  translate("message.font.add.exists").replace("%char%", "" + c),
                  translate("message.warning"),
                  JOptionPane.YES_NO_OPTION,
                  JOptionPane.WARNING_MESSAGE,
                  null,
                  yesno,
                  translate("button.yes"));
          if (opt == 2) {
            yestoall = true;
          }
          if (opt == 3) {
            notoall = true;
          }
        }

        if (yestoall) {
          opt = 0; // yes
        } else if (notoall) {
          opt = 1; // no
        }

        if (opt == 1) {
          continue;
        }
      }
      f.addCharacter(c, font);
      oldchars += c;
    }

    int fontId = ft.getFontId();
    if (updateTextsCheckBox.isSelected()) {
      SWF swf = ft.getSwf();
      for (Tag tag : swf.getTags()) {
        if (tag instanceof TextTag) {
          TextTag textTag = (TextTag) tag;
          if (textTag.getFontIds().contains(fontId)) {
            String text = textTag.getFormattedText().text;
            mainPanel.saveText(textTag, text, null);
          }
        }
      }
    }
    ft.setModified(true);
    ft.getSwf().clearImageCache();
  }