gpt4 book ai didi

Java - 如何编码MAC解码字体信息

转载 作者:搜寻专家 更新时间:2023-11-01 03:53:06 25 4
gpt4 key购买 nike

我正在尝试使用 MACClipboard 获取字体信息。下面是从 Clipboard 获取值的代码。

代码

Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(null);
DataFlavor dfRTF = new DataFlavor("text/rtf", "Rich Formatted Text");
DataFlavor dfTxt = DataFlavor.stringFlavor;
boolean hasTransferableRTFText = (contents != null)
&& contents.isDataFlavorSupported(dfRTF);
boolean hasTransferableTxtText = (contents != null)
&& contents.isDataFlavorSupported(dfTxt);
if (hasTransferableRTFText) {
try {
result = streamToString((InputStream)contents.getTransferData(dfRTF));
System.out.println("dfRTF "+result);
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (hasTransferableTxtText) {
try {
result = (String)contents.getTransferData(dfTxt);
System.out.println("dfTxt "+result);
} catch (Exception ex) {
ex.printStackTrace();
}
}

代码返回MAC解码值如下:

dfRTF {\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\ql\qnatural\pardirnatural

\f0\b\fs24 \cf0 Hello WORLD}
{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\ql\qnatural\pardirnatural

\f0\b\fs24 \cf0 Hello WORLD}

我知道 \f0\b\fs24\cf0 Hello WORLD} 包含字体信息,其中 \b 表示它的 BOLD
我如何对其进行编码以获得该特定值,例如我想知道它是 BOLDED 还是 NO。 请指教。

最佳答案

如果我很了解你需要什么,你可以尝试将其转换为 html 并使用像 jsoup 这样的库来提取所需的信息。

这是一个提取剪贴板中找到的第一个粗体文本的示例

package rtfTest;


import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

import javax.swing.JEditorPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.EditorKit;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;



public class TestRtf{
public static void main (String[] args) {
TestRtf t = new TestRtf();
t.readClipboard() ;
}

@SuppressWarnings("resource")
static String streamToString(java.io.InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}

public static String rtfToHtml(Reader rtf) throws IOException { // From http://www.codeproject.com/Tips/136483/Java-How-to-convert-RTF-into-HTML
JEditorPane p = new JEditorPane();
p.setContentType("text/rtf");
EditorKit kitRtf = p.getEditorKitForContentType("text/rtf");
try {
kitRtf.read(rtf, p.getDocument(), 0);
kitRtf = null;
EditorKit kitHtml = p.getEditorKitForContentType("text/html");
Writer writer = new StringWriter();
kitHtml.write(writer, p.getDocument(), 0, p.getDocument().getLength());
return writer.toString();
} catch (BadLocationException e) {
e.printStackTrace();
}
return null;
}

public void readClipboard () {
String result;
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(null);
DataFlavor dfRTF = new DataFlavor("text/rtf", "Rich Formatted Text");
DataFlavor dfTxt = DataFlavor.stringFlavor;

boolean hasTransferableRTFText = (contents != null)
&& contents.isDataFlavorSupported(dfRTF);
boolean hasTransferableTxtText = (contents != null)
&& contents.isDataFlavorSupported(dfTxt);
if (hasTransferableRTFText) {
try {
// result = streamToString((InputStream)contents.getTransferData(dfRTF));
// Convert rtf to html
result = rtfToHtml(new StringReader(streamToString((InputStream)contents.getTransferData(dfRTF))));
// Example of text extraction from html
// Parse html
Document doc = Jsoup.parse(result);
// Select first bold text
Element firstBoldElt = doc.select("b").first();
String firstBoldText = firstBoldElt.text();

System.out.println(firstBoldText);
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (hasTransferableTxtText) {
try {
result = (String)contents.getTransferData(dfTxt);
System.out.println("dfTxt "+ result);
} catch (Exception ex) {
ex.printStackTrace();
}
}}}

关于Java - 如何编码MAC解码字体信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18998765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com