/**
   * The specified dimensions are used to locate a matching MediaSize instance from amongst all the
   * standard MediaSize instances. If there is no exact match, the closest match is used.
   *
   * <p>The MediaSize is in turn used to locate the MediaSizeName object. This method may return
   * null if the closest matching MediaSize has no corresponding Media instance.
   *
   * <p>This method is useful for clients which have only dimensions and want to find a Media which
   * corresponds to the dimensions.
   *
   * @param x - X dimension
   * @param y - Y dimension.
   * @param units Unit conversion factor, e.g. <CODE>Size2DSyntax.INCH</CODE> or <CODE>
   *     Size2DSyntax.MM</CODE>
   * @return MediaSizeName matching these dimensions, or null.
   * @exception IllegalArgumentException if x <= 0, y <= 0, or units < 1
   */
  public static MediaSizeName findMedia(float x, float y, int units) {

    MediaSize match = MediaSize.ISO.A4;

    if (x <= 0.0f || y <= 0.0f || units < 1) {
      throw new IllegalArgumentException("args must be +ve values");
    }

    double ls = x * x + y * y;
    double tmp_ls;
    float[] dim;
    float diffx = x;
    float diffy = y;

    for (int i = 0; i < sizeVector.size(); i++) {
      MediaSize mediaSize = (MediaSize) sizeVector.elementAt(i);
      dim = mediaSize.getSize(units);
      if (x == dim[0] && y == dim[1]) {
        match = mediaSize;
        break;
      } else {
        diffx = x - dim[0];
        diffy = y - dim[1];
        tmp_ls = diffx * diffx + diffy * diffy;
        if (tmp_ls < ls) {
          ls = tmp_ls;
          match = mediaSize;
        }
      }
    }

    return match.getMediaSizeName();
  }
Exemple #2
0
  public void printableJob(Printable printable) throws PrintException {
    try {
      synchronized (this) {
        if (job != null) { // shouldn't happen
          throw new PrintException("already printing");
        } else {
          job = new sun.awt.windows.WPrinterJob();
        }
      }
      PrintService svc = getPrintService();
      job.setPrintService(svc);
      if (copies == 0) {
        Copies c = (Copies) svc.getDefaultAttributeValue(Copies.class);
        copies = c.getValue();
      }

      if (mediaName == null) {
        Object media = svc.getDefaultAttributeValue(Media.class);
        if (media instanceof MediaSizeName) {
          mediaName = (MediaSizeName) media;
          mediaSize = MediaSize.getMediaSizeForName(mediaName);
        }
      }

      if (orient == null) {
        orient = (OrientationRequested) svc.getDefaultAttributeValue(OrientationRequested.class);
      }

      job.setCopies(copies);
      job.setJobName(jobName);
      PageFormat pf = new PageFormat();
      if (mediaSize != null) {
        Paper p = new Paper();
        p.setSize(mediaSize.getX(MediaSize.INCH) * 72.0, mediaSize.getY(MediaSize.INCH) * 72.0);
        p.setImageableArea(72.0, 72.0, p.getWidth() - 144.0, p.getHeight() - 144.0);
        pf.setPaper(p);
      }
      if (orient == OrientationRequested.REVERSE_LANDSCAPE) {
        pf.setOrientation(PageFormat.REVERSE_LANDSCAPE);
      } else if (orient == OrientationRequested.LANDSCAPE) {
        pf.setOrientation(PageFormat.LANDSCAPE);
      }
      job.setPrintable(printable, pf);
      job.print(reqAttrSet);
      notifyEvent(PrintJobEvent.DATA_TRANSFER_COMPLETE);
      return;
    } catch (PrinterException pe) {
      notifyEvent(PrintJobEvent.JOB_FAILED);
      throw new PrintException(pe);
    } finally {
      printReturned = true;
      notifyEvent(PrintJobEvent.NO_MORE_EVENTS);
    }
  }
  /** Returns closest matching MediaSizeName among given array of Media */
  public static MediaSizeName findMedia(Media[] media, float x, float y, int units) {

    if (x <= 0.0f || y <= 0.0f || units < 1) {
      throw new IllegalArgumentException("args must be +ve values");
    }

    if (media == null || media.length == 0) {
      throw new IllegalArgumentException("args must have valid array of media");
    }

    int size = 0;
    MediaSizeName[] msn = new MediaSizeName[media.length];
    for (int i = 0; i < media.length; i++) {
      if (media[i] instanceof MediaSizeName) {
        msn[size++] = (MediaSizeName) media[i];
      }
    }

    if (size == 0) {
      return null;
    }

    int match = 0;

    double ls = x * x + y * y;
    double tmp_ls;
    float[] dim;
    float diffx = x;
    float diffy = y;

    for (int i = 0; i < size; i++) {
      MediaSize mediaSize = MediaSize.getMediaSizeForName(msn[i]);
      if (mediaSize == null) {
        continue;
      }
      dim = mediaSize.getSize(units);
      if (x == dim[0] && y == dim[1]) {
        match = i;
        break;
      } else {
        diffx = x - dim[0];
        diffy = y - dim[1];
        tmp_ls = diffx * diffx + diffy * diffy;
        if (tmp_ls < ls) {
          ls = tmp_ls;
          match = i;
        }
      }
    }

    return msn[match];
  }
 public CustomMediaSizeName(String name, String choice, float width, float length) {
   super(nextValue(name));
   choiceName = choice;
   customEnumTable.add(this);
   mediaName = null;
   try {
     mediaName = MediaSize.findMedia(width, length, MediaSize.INCH);
   } catch (IllegalArgumentException iae) {
   }
 }
  private final synchronized Paper addPaper(MediaSizeName media) {

    if (mediaToPaperMap == null) {
      mediaToPaperMap = new HashMap<MediaSizeName, Paper>();
    }

    Paper paper = predefinedPaperMap.get(media);
    if (paper == null) {
      MediaSize sz = MediaSize.getMediaSizeForName(media);
      if (sz != null) {
        int mm = (int) ((25400 / 72.0) + 0.5);
        paper = PrintHelper.createPaper(media.toString(), sz.getX(mm), sz.getY(mm), Units.MM);
      }
    }
    if (paper == null) {
      paper = Paper.NA_LETTER;
    }
    mediaToPaperMap.put(media, paper);
    return paper;
  }
  // {{{ getPageFormat() method
  public static PageFormat getPageFormat() {
    // convert from PrintRequestAttributeSet to the pageFormat
    PrinterJob prnJob = getPrintJob(" "); // $NON-NLS-1$
    PageFormat pf = prnJob.defaultPage();
    Paper pap = pf.getPaper();

    MediaSizeName media = (MediaSizeName) format.get(Media.class);
    MediaSize ms = MediaSize.getMediaSizeForName(media);

    MediaPrintableArea mediaarea = (MediaPrintableArea) format.get(MediaPrintableArea.class);
    if (mediaarea != null) {
      pap.setImageableArea(
          (mediaarea.getX(MediaPrintableArea.INCH) * 72),
          (mediaarea.getY(MediaPrintableArea.INCH) * 72),
          (mediaarea.getWidth(MediaPrintableArea.INCH) * 72),
          (mediaarea.getHeight(MediaPrintableArea.INCH) * 72));
    }
    if (ms != null) {
      pap.setSize((ms.getX(Size2DSyntax.INCH) * 72), (ms.getY(Size2DSyntax.INCH) * 72));
    }
    pf.setPaper(pap);

    OrientationRequested orientation =
        (OrientationRequested) format.get(OrientationRequested.class);
    if (orientation != null) {
      if (orientation.getValue() == OrientationRequested.LANDSCAPE.getValue()) {
        pf.setOrientation(PageFormat.LANDSCAPE);
      } else if (orientation.getValue() == OrientationRequested.REVERSE_LANDSCAPE.getValue()) {
        pf.setOrientation(PageFormat.REVERSE_LANDSCAPE);
      } else if (orientation.getValue() == OrientationRequested.PORTRAIT.getValue()) {
        pf.setOrientation(PageFormat.PORTRAIT);
      } else if (orientation.getValue() == OrientationRequested.REVERSE_PORTRAIT.getValue()) {
        // doesnt exist??
        // pf.setOrientation(PageFormat.REVERSE_PORTRAIT);
        // then just do the next best thing
        pf.setOrientation(PageFormat.PORTRAIT);
      }
    }
    return pf;
  } // }}}
 /**
  * For any given paper, this retrieves the hardware margins, or a reasonable and safe guess if
  * they aren't available.
  */
 public Rectangle2D printableArea(Paper paper) {
   int INCH = MediaSize.INCH;
   Rectangle2D area = null;
   MediaSizeName msn =
       MediaSize.findMedia(
           (float) paper.getWidth(), (float) paper.getHeight(), (int) (INCH / 72.0));
   if (msn != null) {
     PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
     pras.add(msn);
     MediaPrintableArea[] mpa =
         (MediaPrintableArea[])
             service.getSupportedAttributeValues(MediaPrintableArea.class, null, pras);
     if (mpa != null && mpa.length > 0) {
       int MPA_INCH = MediaPrintableArea.INCH;
       area =
           new Rectangle2D(
               mpa[0].getX(MPA_INCH),
               mpa[0].getY(MPA_INCH),
               mpa[0].getWidth(MPA_INCH),
               mpa[0].getHeight(MPA_INCH));
     }
   }
   // If we could not get the area for whatever reason,
   // then go with 0.75" margins unless they are too large
   // ie its a really small paper.
   if (area == null) {
     double pw = (paper.getWidth() / 72.0);
     ;
     double ph = (paper.getHeight() / 72.0);
     double iw, ih;
     if (pw < 3.0) {
       iw = 0.75 * pw;
     } else {
       iw = pw - 1.5;
     }
     if (ph < 3.0) {
       ih = 0.75 * ph;
     } else {
       ih = ph - 1.5;
     }
     double lm = (pw - iw) / 2.0;
     double tm = (ph - ih) / 2.0;
     area = new Rectangle2D(lm, tm, iw, ih);
   }
   return area;
 }
Exemple #8
0
  private String getMediaName(float width, float height) {
    MediaSizeName msn = MediaSize.findMedia(width, height, Size2DSyntax.MM);

    if (msn == null) return "Unbekannt";

    switch (msn.toString()) {
      case "iso-a0":
        return "DIN A0";
      case "iso-a1":
        return "DIN A1";
      case "iso-a2":
        return "DIN A2";
      case "iso-a3":
        return "DIN A3";
      case "iso-a4":
        return "DIN A4";
      case "iso-a5":
        return "DIN A5";
      case "iso-a6":
        return "DIN A6";
      case "iso-a7":
        return "DIN A7";
      case "iso-a8":
        return "DIN A8";
      case "iso-a9":
        return "DIN A9";
      case "iso-a10":
        return "DIN A10";
      case "iso-b0":
        return "DIN B0";
      case "iso-b1":
        return "DIN B1";
      case "iso-b2":
        return "DIN B2";
      case "iso-b3":
        return "DIN B3";
      case "iso-b4":
        return "DIN B4";
      case "iso-b5":
        return "DIN B5";
      case "iso-b6":
        return "DIN B6";
      case "iso-b7":
        return "DIN B7";
      case "iso-b8":
        return "DIN B8";
      case "iso-b9":
        return "DIN B9";
      case "iso-b10":
        return "DIN B10";
      case "na-letter":
        return "North American Letter";
      case "na-legal":
        return "North American Legal";
      case "na-8x10":
        return "North American 8x10 inch";
      case "na-5x7":
        return "North American 5x7 inch";
      case "executive":
        return "Executive";
      case "folio":
        return "Folio";
      case "invoice":
        return "Invoice";
      case "tabloid":
        return "Tabloid";
      case "ledger":
        return "Ledger";
      case "quarto":
        return "Quarto";
      case "iso-c0":
        return "DIN C0";
      case "iso-c1":
        return "DIN C1";
      case "iso-c2":
        return "DIN C2";
      case "iso-c3":
        return "DIN C3";
      case "iso-c4":
        return "DIN C4";
      case "iso-c5":
        return "DIN C5";
      case "iso-c6":
        return "DIN C6";
      case "iso-designated-long":
        return "ISO Designated Long size";
      case "na-10x13-envelope":
        return "North American 10x13 inch";
      case "na-9x12-envelope":
        return "North American 9x12 inch";
      case "na-number-10-envelope":
        return "North American number 10 business envelope";
      case "na-7x9-envelope":
        return "North American 7x9 inch envelope";
      case "na-9x11-envelope":
        return "North American 9x11 inch envelope";
      case "na-10x14-envelope":
        return "North American 10x14 inch envelope";
      case "na-number-9-envelope":
        return "North American number 9 business envelope";
      case "na-6x9-envelope":
        return "North American 6x9 inch envelope";
      case "na-10x15-envelope":
        return "North American 10x15 inch envelope";
      case "monarch-envelope":
        return "Monarch envelope";
      case "jis-b0":
        return "JIS B0";
      case "jis-b1":
        return "JIS B1";
      case "jis-b2":
        return "JIS B2";
      case "jis-b3":
        return "JIS B3";
      case "jis-b4":
        return "JIS B4";
      case "jis-b5":
        return "JIS B5";
      case "jis-b6":
        return "JIS B6";
      case "jis-b7":
        return "JIS B7";
      case "jis-b8":
        return "JIS B8";
      case "jis-b9":
        return "JIS B9";
      case "jis-b10":
        return "JIS B10";
      case "a":
        return "Engineering ANSI A";
      case "b":
        return "Engineering ANSI B";
      case "c":
        return "Engineering ANSI C";
      case "d":
        return "Engineering ANSI D";
      case "e":
        return "Engineering ANSI E";
      case "f":
        return "Engineering ANSI F";
      case "arch-a":
        return "Architectural ANSI A";
      case "arch-b":
        return "Architectural ANSI B";
      case "arch-c":
        return "Architectural ANSI C";
      case "arch-d":
        return "Architectural ANSI D";
      case "arch-e":
        return "Architectural ANSI E";
      case "japanese-postcard":
        return "Japanese Postcard";
      case "oufuko-postcard":
        return "Oufuko Postcard";
      case "italian-envelope":
        return "Italian Envelope";
      case "personal-envelope":
        return "Personal Envelope";
      case "na-number-11-envelope":
        return "North American Number 11 Envelope";
      case "na-number-12-envelope":
        return "North American Number 12 Envelope";
      case "na-number-14-envelope":
        return "North American Number 14 Envelope";
      default:
        return "Unbekannt";
    }
  }
Exemple #9
0
  private void getAttributeValues(DocFlavor flavor) throws PrintException {

    if (reqAttrSet.get(Fidelity.class) == Fidelity.FIDELITY_TRUE) {
      fidelity = true;
    } else {
      fidelity = false;
    }

    Class category;
    Attribute[] attrs = reqAttrSet.toArray();
    for (int i = 0; i < attrs.length; i++) {
      Attribute attr = attrs[i];
      category = attr.getCategory();
      if (fidelity == true) {
        if (!service.isAttributeCategorySupported(category)) {
          notifyEvent(PrintJobEvent.JOB_FAILED);
          throw new PrintJobAttributeException("unsupported category: " + category, category, null);
        } else if (!service.isAttributeValueSupported(attr, flavor, null)) {
          notifyEvent(PrintJobEvent.JOB_FAILED);
          throw new PrintJobAttributeException("unsupported attribute: " + attr, null, attr);
        }
      }
      if (category == Destination.class) {
        URI uri = ((Destination) attr).getURI();
        if (!"file".equals(uri.getScheme())) {
          notifyEvent(PrintJobEvent.JOB_FAILED);
          throw new PrintException("Not a file: URI");
        } else {
          try {
            mDestination = (new File(uri)).getPath();
          } catch (Exception e) {
            throw new PrintException(e);
          }
          // check write access
          SecurityManager security = System.getSecurityManager();
          if (security != null) {
            try {
              security.checkWrite(mDestination);
            } catch (SecurityException se) {
              notifyEvent(PrintJobEvent.JOB_FAILED);
              throw new PrintException(se);
            }
          }
        }
      } else if (category == JobName.class) {
        jobName = ((JobName) attr).getValue();
      } else if (category == Copies.class) {
        copies = ((Copies) attr).getValue();
      } else if (category == Media.class) {
        if (attr instanceof MediaSizeName) {
          mediaName = (MediaSizeName) attr;
          // If requested MediaSizeName is not supported,
          // get the corresponding media size - this will
          // be used to create a new PageFormat.
          if (!service.isAttributeValueSupported(attr, null, null)) {
            mediaSize = MediaSize.getMediaSizeForName(mediaName);
          }
        }
      } else if (category == OrientationRequested.class) {
        orient = (OrientationRequested) attr;
      }
    }
  }