📄 layouttext.java
字号:
gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4); getContentPane().add(jLabel4, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 3; gridBagConstraints.gridwidth = 3; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4); getContentPane().add(textFont, gridBagConstraints); textItalic.setText("Italic"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 4; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4); getContentPane().add(textItalic, gridBagConstraints); textBold.setText("Bold"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 4; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4); getContentPane().add(textBold, gridBagConstraints); jLabel5.setText("Layer:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 5; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4); getContentPane().add(jLabel5, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 5; gridBagConstraints.gridwidth = 3; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4); getContentPane().add(textLayer, gridBagConstraints); jLabel6.setText("Message:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 7; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4); getContentPane().add(jLabel6, gridBagConstraints); textUnderline.setText("Underline"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 3; gridBagConstraints.gridy = 4; getContentPane().add(textUnderline, gridBagConstraints); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 7; gridBagConstraints.gridwidth = 3; gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; gridBagConstraints.weighty = 1.0; gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4); getContentPane().add(textMessage, gridBagConstraints); invertDots.setText("Reverse-video"); invertDots.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0)); invertDots.setMargin(new java.awt.Insets(0, 0, 0, 0)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 6; gridBagConstraints.gridwidth = 4; gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4); getContentPane().add(invertDots, gridBagConstraints); pack(); }// </editor-fold>//GEN-END:initComponents private void cancel(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancel {//GEN-HEADEREND:event_cancel closeDialog(null); }//GEN-LAST:event_cancel private void ok(java.awt.event.ActionEvent evt)//GEN-FIRST:event_ok {//GEN-HEADEREND:event_ok grabDialogValues(); Cell curCell = WindowFrame.needCurCell(); if (curCell == null) return; // determine the primitive to use for the layout NodeProto primNode = Technology.getCurrent().findNodeProto(lastLayer); if (primNode == null) { System.out.println("Cannot find " + lastLayer + " primitive"); return; } // get the raster int yOffset = 0; String [] strings = lastMessage.split("\n"); for(int i=0; i<strings.length; i++) { String str = strings[i]; Raster ras = renderText(str, lastFont, lastSize, lastItalic, lastBold, lastUnderline); if (ras == null) return; DataBufferByte dbb = (DataBufferByte)ras.getDataBuffer(); byte [] samples = dbb.getData(); // create the layout text new CreateLayoutText(curCell, primNode, lastScale, lastSeparation, ras.getWidth(), ras.getHeight(), yOffset, lastInvertDots, samples); yOffset += ras.getHeight(); } closeDialog(null); }//GEN-LAST:event_ok /** * Method to convert text to an array of pixels. * This is used for text rendering, as well as for creating "layout text" which is placed as geometry in the circuit. * @param msg the string of text to be converted. * @param fontName the name of the font to use. * @param tSize the size of the font to use. * @param italic true to make the text italic. * @param bold true to make the text bold. * @param underline true to underline the text. * @param invertDots true to invert dot placement. * @return a Raster with the text bits. */ private static Raster renderText(String msg, String fontName, int tSize, boolean italic, boolean bold, boolean underline) { int fontStyle = Font.PLAIN; if (italic) fontStyle |= Font.ITALIC; if (bold) fontStyle |= Font.BOLD; Font font = new Font(fontName, fontStyle, tSize); if (font == null) { System.out.println("Could not find font "+font+" to render text: "+msg); return null; } // convert the text to a GlyphVector FontRenderContext frc = new FontRenderContext(null, true, true); GlyphVector gv = font.createGlyphVector(frc, msg); LineMetrics lm = font.getLineMetrics(msg, frc); // figure bounding box of text Rectangle2D rasRect = gv.getLogicalBounds(); int width = (int)rasRect.getWidth(); int height = (int)(lm.getHeight()+0.5); if (width <= 0 || height <= 0) return null; fontStyle = font.getStyle(); if (underline) height++; Rectangle2D rasBounds = new Rectangle2D.Double(0, lm.getAscent()-lm.getLeading(), width, height); // if the new image is larger than what is saved, must rebuild // create a new text buffer BufferedImage textImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); Graphics2D g2 = textImage.createGraphics(); // now render it g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2.setColor(new Color(255,255,255)); g2.drawGlyphVector(gv, (float)-rasBounds.getX(), lm.getAscent()-lm.getLeading()); if (underline) g2.drawLine(0, height-1, width-1, height-1); // return the bits return textImage.getData(); } /** Closes the dialog */ private void closeDialog(java.awt.event.WindowEvent evt)//GEN-FIRST:event_closeDialog { grabDialogValues(); setVisible(false); dispose(); }//GEN-LAST:event_closeDialog private void grabDialogValues() { lastSize = TextUtils.atoi(textSize.getText()); lastScale = TextUtils.atof(textScale.getText()); lastSeparation = TextUtils.atof(dotSeparation.getText()); lastItalic = textItalic.isSelected(); lastBold = textBold.isSelected(); lastUnderline = textUnderline.isSelected(); lastInvertDots = invertDots.isSelected(); lastLayer = (String)textLayer.getSelectedItem(); lastFont = (String)textFont.getSelectedItem(); lastMessage = textMessage.getText(); } /** * Class to create a cell in a new thread. */ private static class CreateLayoutText extends Job { private Cell curCell; private NodeProto primNode; private double lastScale; private double lastSeparation; private int wid, hei, yOffset; private boolean invertDots; private byte [] samples; protected CreateLayoutText( Cell curCell, NodeProto primNode, double lastScale, double lastSeparation, int wid, int hei, int yOffset, boolean invertDots, byte [] samples) { super("Create Layout Text", User.getUserTool(), Job.Type.CHANGE, null, null, Job.Priority.USER); this.curCell = curCell; this.primNode = primNode; this.lastScale = lastScale; this.lastSeparation = lastSeparation; this.wid = wid; this.hei = hei; this.yOffset = yOffset; this.invertDots = invertDots; this.samples = samples; startJob(); } public boolean doIt() throws JobException { double width = lastScale - lastSeparation; double height = lastScale - lastSeparation; int samp = 0; for(int y=0; y<hei; y++) { for(int x=0; x<wid; x++) { if (samples[samp++] == 0 != invertDots) continue; Point2D center = new Point2D.Double(x*lastScale, -(y+yOffset)*lastScale); NodeInst.newInstance(primNode, center, width, height, curCell); } } return true; } } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton cancel; private javax.swing.JTextField dotSeparation; private javax.swing.JCheckBox invertDots; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JButton ok; private javax.swing.JCheckBox textBold; private javax.swing.JComboBox textFont; private javax.swing.JCheckBox textItalic; private javax.swing.JComboBox textLayer; private javax.swing.JTextArea textMessage; private javax.swing.JTextField textScale; private javax.swing.JTextField textSize; private javax.swing.JCheckBox textUnderline; // End of variables declaration//GEN-END:variables}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -